7ab5d719004176f45a7ffa9537650816d504bb79
[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/stringify.h"
44 #include "bfd.h"
45 #include "elf-bfd.h"
46 #include "symtab.h"
47 #include "gdbtypes.h"
48 #include "objfiles.h"
49 #include "dwarf2.h"
50 #include "buildsym.h"
51 #include "demangle.h"
52 #include "gdb-demangle.h"
53 #include "filenames.h" /* for DOSish file names */
54 #include "language.h"
55 #include "complaints.h"
56 #include "dwarf2/expr.h"
57 #include "dwarf2/loc.h"
58 #include "cp-support.h"
59 #include "hashtab.h"
60 #include "command.h"
61 #include "gdbcmd.h"
62 #include "block.h"
63 #include "addrmap.h"
64 #include "typeprint.h"
65 #include "psympriv.h"
66 #include "c-lang.h"
67 #include "go-lang.h"
68 #include "valprint.h"
69 #include "gdbcore.h" /* for gnutarget */
70 #include "gdb/gdb-index.h"
71 #include "gdb_bfd.h"
72 #include "f-lang.h"
73 #include "source.h"
74 #include "build-id.h"
75 #include "namespace.h"
76 #include "gdbsupport/function-view.h"
77 #include "gdbsupport/gdb_optional.h"
78 #include "gdbsupport/underlying.h"
79 #include "gdbsupport/hash_enum.h"
80 #include "filename-seen-cache.h"
81 #include "producer.h"
82 #include <fcntl.h>
83 #include <algorithm>
84 #include <unordered_map>
85 #include "gdbsupport/selftest.h"
86 #include "rust-lang.h"
87 #include "gdbsupport/pathstuff.h"
88 #include "count-one-bits.h"
89 #include "debuginfod-support.h"
90
91 /* When == 1, print basic high level tracing messages.
92 When > 1, be more verbose.
93 This is in contrast to the low level DIE reading of dwarf_die_debug. */
94 static unsigned int dwarf_read_debug = 0;
95
96 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
97
98 #define dwarf_read_debug_printf(fmt, ...) \
99 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
100 ##__VA_ARGS__)
101
102 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
103
104 #define dwarf_read_debug_printf_v(fmt, ...) \
105 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
106 ##__VA_ARGS__)
107
108 /* When non-zero, dump DIEs after they are read in. */
109 static unsigned int dwarf_die_debug = 0;
110
111 /* When non-zero, dump line number entries as they are read in. */
112 unsigned int dwarf_line_debug = 0;
113
114 /* When true, cross-check physname against demangler. */
115 static bool check_physname = false;
116
117 /* When true, do not reject deprecated .gdb_index sections. */
118 static bool use_deprecated_index_sections = false;
119
120 /* This is used to store the data that is always per objfile. */
121 static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
122
123 /* These are used to store the dwarf2_per_bfd objects.
124
125 objfiles having the same BFD, which doesn't require relocations, are going to
126 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
127
128 Other objfiles are not going to share a dwarf2_per_bfd with any other
129 objfiles, so they'll have their own version kept in the _objfile_data_key
130 version. */
131 static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
132 static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
133
134 /* The "aclass" indices for various kinds of computed DWARF symbols. */
135
136 static int dwarf2_locexpr_index;
137 static int dwarf2_loclist_index;
138 static int dwarf2_locexpr_block_index;
139 static int dwarf2_loclist_block_index;
140
141 /* Size of .debug_loclists section header for 32-bit DWARF format. */
142 #define LOCLIST_HEADER_SIZE32 12
143
144 /* Size of .debug_loclists section header for 64-bit DWARF format. */
145 #define LOCLIST_HEADER_SIZE64 20
146
147 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
148 #define RNGLIST_HEADER_SIZE32 12
149
150 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
151 #define RNGLIST_HEADER_SIZE64 20
152
153 /* An index into a (C++) symbol name component in a symbol name as
154 recorded in the mapped_index's symbol table. For each C++ symbol
155 in the symbol table, we record one entry for the start of each
156 component in the symbol in a table of name components, and then
157 sort the table, in order to be able to binary search symbol names,
158 ignoring leading namespaces, both completion and regular look up.
159 For example, for symbol "A::B::C", we'll have an entry that points
160 to "A::B::C", another that points to "B::C", and another for "C".
161 Note that function symbols in GDB index have no parameter
162 information, just the function/method names. You can convert a
163 name_component to a "const char *" using the
164 'mapped_index::symbol_name_at(offset_type)' method. */
165
166 struct name_component
167 {
168 /* Offset in the symbol name where the component starts. Stored as
169 a (32-bit) offset instead of a pointer to save memory and improve
170 locality on 64-bit architectures. */
171 offset_type name_offset;
172
173 /* The symbol's index in the symbol and constant pool tables of a
174 mapped_index. */
175 offset_type idx;
176 };
177
178 /* Base class containing bits shared by both .gdb_index and
179 .debug_name indexes. */
180
181 struct mapped_index_base
182 {
183 mapped_index_base () = default;
184 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
185
186 /* The name_component table (a sorted vector). See name_component's
187 description above. */
188 std::vector<name_component> name_components;
189
190 /* How NAME_COMPONENTS is sorted. */
191 enum case_sensitivity name_components_casing;
192
193 /* Return the number of names in the symbol table. */
194 virtual size_t symbol_name_count () const = 0;
195
196 /* Get the name of the symbol at IDX in the symbol table. */
197 virtual const char *symbol_name_at
198 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
199
200 /* Return whether the name at IDX in the symbol table should be
201 ignored. */
202 virtual bool symbol_name_slot_invalid (offset_type idx) const
203 {
204 return false;
205 }
206
207 /* Build the symbol name component sorted vector, if we haven't
208 yet. */
209 void build_name_components (dwarf2_per_objfile *per_objfile);
210
211 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
212 possible matches for LN_NO_PARAMS in the name component
213 vector. */
214 std::pair<std::vector<name_component>::const_iterator,
215 std::vector<name_component>::const_iterator>
216 find_name_components_bounds (const lookup_name_info &ln_no_params,
217 enum language lang,
218 dwarf2_per_objfile *per_objfile) const;
219
220 /* Prevent deleting/destroying via a base class pointer. */
221 protected:
222 ~mapped_index_base() = default;
223 };
224
225 /* A description of the mapped index. The file format is described in
226 a comment by the code that writes the index. */
227 struct mapped_index final : public mapped_index_base
228 {
229 /* A slot/bucket in the symbol table hash. */
230 struct symbol_table_slot
231 {
232 const offset_type name;
233 const offset_type vec;
234 };
235
236 /* Index data format version. */
237 int version = 0;
238
239 /* The address table data. */
240 gdb::array_view<const gdb_byte> address_table;
241
242 /* The symbol table, implemented as a hash table. */
243 gdb::array_view<symbol_table_slot> symbol_table;
244
245 /* A pointer to the constant pool. */
246 const char *constant_pool = nullptr;
247
248 bool symbol_name_slot_invalid (offset_type idx) const override
249 {
250 const auto &bucket = this->symbol_table[idx];
251 return bucket.name == 0 && bucket.vec == 0;
252 }
253
254 /* Convenience method to get at the name of the symbol at IDX in the
255 symbol table. */
256 const char *symbol_name_at
257 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
258 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
259
260 size_t symbol_name_count () const override
261 { return this->symbol_table.size (); }
262 };
263
264 /* A description of the mapped .debug_names.
265 Uninitialized map has CU_COUNT 0. */
266 struct mapped_debug_names final : public mapped_index_base
267 {
268 bfd_endian dwarf5_byte_order;
269 bool dwarf5_is_dwarf64;
270 bool augmentation_is_gdb;
271 uint8_t offset_size;
272 uint32_t cu_count = 0;
273 uint32_t tu_count, bucket_count, name_count;
274 const gdb_byte *cu_table_reordered, *tu_table_reordered;
275 const uint32_t *bucket_table_reordered, *hash_table_reordered;
276 const gdb_byte *name_table_string_offs_reordered;
277 const gdb_byte *name_table_entry_offs_reordered;
278 const gdb_byte *entry_pool;
279
280 struct index_val
281 {
282 ULONGEST dwarf_tag;
283 struct attr
284 {
285 /* Attribute name DW_IDX_*. */
286 ULONGEST dw_idx;
287
288 /* Attribute form DW_FORM_*. */
289 ULONGEST form;
290
291 /* Value if FORM is DW_FORM_implicit_const. */
292 LONGEST implicit_const;
293 };
294 std::vector<attr> attr_vec;
295 };
296
297 std::unordered_map<ULONGEST, index_val> abbrev_map;
298
299 const char *namei_to_name
300 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
301
302 /* Implementation of the mapped_index_base virtual interface, for
303 the name_components cache. */
304
305 const char *symbol_name_at
306 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
307 { return namei_to_name (idx, per_objfile); }
308
309 size_t symbol_name_count () const override
310 { return this->name_count; }
311 };
312
313 /* See dwarf2read.h. */
314
315 dwarf2_per_objfile *
316 get_dwarf2_per_objfile (struct objfile *objfile)
317 {
318 return dwarf2_objfile_data_key.get (objfile);
319 }
320
321 /* Default names of the debugging sections. */
322
323 /* Note that if the debugging section has been compressed, it might
324 have a name like .zdebug_info. */
325
326 static const struct dwarf2_debug_sections dwarf2_elf_names =
327 {
328 { ".debug_info", ".zdebug_info" },
329 { ".debug_abbrev", ".zdebug_abbrev" },
330 { ".debug_line", ".zdebug_line" },
331 { ".debug_loc", ".zdebug_loc" },
332 { ".debug_loclists", ".zdebug_loclists" },
333 { ".debug_macinfo", ".zdebug_macinfo" },
334 { ".debug_macro", ".zdebug_macro" },
335 { ".debug_str", ".zdebug_str" },
336 { ".debug_str_offsets", ".zdebug_str_offsets" },
337 { ".debug_line_str", ".zdebug_line_str" },
338 { ".debug_ranges", ".zdebug_ranges" },
339 { ".debug_rnglists", ".zdebug_rnglists" },
340 { ".debug_types", ".zdebug_types" },
341 { ".debug_addr", ".zdebug_addr" },
342 { ".debug_frame", ".zdebug_frame" },
343 { ".eh_frame", NULL },
344 { ".gdb_index", ".zgdb_index" },
345 { ".debug_names", ".zdebug_names" },
346 { ".debug_aranges", ".zdebug_aranges" },
347 23
348 };
349
350 /* List of DWO/DWP sections. */
351
352 static const struct dwop_section_names
353 {
354 struct dwarf2_section_names abbrev_dwo;
355 struct dwarf2_section_names info_dwo;
356 struct dwarf2_section_names line_dwo;
357 struct dwarf2_section_names loc_dwo;
358 struct dwarf2_section_names loclists_dwo;
359 struct dwarf2_section_names macinfo_dwo;
360 struct dwarf2_section_names macro_dwo;
361 struct dwarf2_section_names rnglists_dwo;
362 struct dwarf2_section_names str_dwo;
363 struct dwarf2_section_names str_offsets_dwo;
364 struct dwarf2_section_names types_dwo;
365 struct dwarf2_section_names cu_index;
366 struct dwarf2_section_names tu_index;
367 }
368 dwop_section_names =
369 {
370 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
371 { ".debug_info.dwo", ".zdebug_info.dwo" },
372 { ".debug_line.dwo", ".zdebug_line.dwo" },
373 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
374 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
375 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
376 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
377 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
378 { ".debug_str.dwo", ".zdebug_str.dwo" },
379 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
380 { ".debug_types.dwo", ".zdebug_types.dwo" },
381 { ".debug_cu_index", ".zdebug_cu_index" },
382 { ".debug_tu_index", ".zdebug_tu_index" },
383 };
384
385 /* local data types */
386
387 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
388 begin with a header, which contains the following information. */
389 struct loclists_rnglists_header
390 {
391 /* A 4-byte or 12-byte length containing the length of the
392 set of entries for this compilation unit, not including the
393 length field itself. */
394 unsigned int length;
395
396 /* A 2-byte version identifier. */
397 short version;
398
399 /* A 1-byte unsigned integer containing the size in bytes of an address on
400 the target system. */
401 unsigned char addr_size;
402
403 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
404 on the target system. */
405 unsigned char segment_collector_size;
406
407 /* A 4-byte count of the number of offsets that follow the header. */
408 unsigned int offset_entry_count;
409 };
410
411 /* Type used for delaying computation of method physnames.
412 See comments for compute_delayed_physnames. */
413 struct delayed_method_info
414 {
415 /* The type to which the method is attached, i.e., its parent class. */
416 struct type *type;
417
418 /* The index of the method in the type's function fieldlists. */
419 int fnfield_index;
420
421 /* The index of the method in the fieldlist. */
422 int index;
423
424 /* The name of the DIE. */
425 const char *name;
426
427 /* The DIE associated with this method. */
428 struct die_info *die;
429 };
430
431 /* Internal state when decoding a particular compilation unit. */
432 struct dwarf2_cu
433 {
434 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
435 dwarf2_per_objfile *per_objfile);
436
437 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
438
439 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
440 Create the set of symtabs used by this TU, or if this TU is sharing
441 symtabs with another TU and the symtabs have already been created
442 then restore those symtabs in the line header.
443 We don't need the pc/line-number mapping for type units. */
444 void setup_type_unit_groups (struct die_info *die);
445
446 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
447 buildsym_compunit constructor. */
448 struct compunit_symtab *start_symtab (const char *name,
449 const char *comp_dir,
450 CORE_ADDR low_pc);
451
452 /* Reset the builder. */
453 void reset_builder () { m_builder.reset (); }
454
455 /* Return a type that is a generic pointer type, the size of which
456 matches the address size given in the compilation unit header for
457 this CU. */
458 struct type *addr_type () const;
459
460 /* Find an integer type the same size as the address size given in
461 the compilation unit header for this CU. UNSIGNED_P controls if
462 the integer is unsigned or not. */
463 struct type *addr_sized_int_type (bool unsigned_p) const;
464
465 /* The header of the compilation unit. */
466 struct comp_unit_head header {};
467
468 /* Base address of this compilation unit. */
469 gdb::optional<CORE_ADDR> base_address;
470
471 /* The language we are debugging. */
472 enum language language = language_unknown;
473 const struct language_defn *language_defn = nullptr;
474
475 const char *producer = nullptr;
476
477 private:
478 /* The symtab builder for this CU. This is only non-NULL when full
479 symbols are being read. */
480 std::unique_ptr<buildsym_compunit> m_builder;
481
482 public:
483 /* The generic symbol table building routines have separate lists for
484 file scope symbols and all all other scopes (local scopes). So
485 we need to select the right one to pass to add_symbol_to_list().
486 We do it by keeping a pointer to the correct list in list_in_scope.
487
488 FIXME: The original dwarf code just treated the file scope as the
489 first local scope, and all other local scopes as nested local
490 scopes, and worked fine. Check to see if we really need to
491 distinguish these in buildsym.c. */
492 struct pending **list_in_scope = nullptr;
493
494 /* Hash table holding all the loaded partial DIEs
495 with partial_die->offset.SECT_OFF as hash. */
496 htab_t partial_dies = nullptr;
497
498 /* Storage for things with the same lifetime as this read-in compilation
499 unit, including partial DIEs. */
500 auto_obstack comp_unit_obstack;
501
502 /* Backlink to our per_cu entry. */
503 struct dwarf2_per_cu_data *per_cu;
504
505 /* The dwarf2_per_objfile that owns this. */
506 dwarf2_per_objfile *per_objfile;
507
508 /* How many compilation units ago was this CU last referenced? */
509 int last_used = 0;
510
511 /* A hash table of DIE cu_offset for following references with
512 die_info->offset.sect_off as hash. */
513 htab_t die_hash = nullptr;
514
515 /* Full DIEs if read in. */
516 struct die_info *dies = nullptr;
517
518 /* A set of pointers to dwarf2_per_cu_data objects for compilation
519 units referenced by this one. Only set during full symbol processing;
520 partial symbol tables do not have dependencies. */
521 htab_t dependencies = nullptr;
522
523 /* Header data from the line table, during full symbol processing. */
524 struct line_header *line_header = nullptr;
525 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
526 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
527 this is the DW_TAG_compile_unit die for this CU. We'll hold on
528 to the line header as long as this DIE is being processed. See
529 process_die_scope. */
530 die_info *line_header_die_owner = nullptr;
531
532 /* A list of methods which need to have physnames computed
533 after all type information has been read. */
534 std::vector<delayed_method_info> method_list;
535
536 /* To be copied to symtab->call_site_htab. */
537 htab_t call_site_htab = nullptr;
538
539 /* Non-NULL if this CU came from a DWO file.
540 There is an invariant here that is important to remember:
541 Except for attributes copied from the top level DIE in the "main"
542 (or "stub") file in preparation for reading the DWO file
543 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
544 Either there isn't a DWO file (in which case this is NULL and the point
545 is moot), or there is and either we're not going to read it (in which
546 case this is NULL) or there is and we are reading it (in which case this
547 is non-NULL). */
548 struct dwo_unit *dwo_unit = nullptr;
549
550 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
551 Note this value comes from the Fission stub CU/TU's DIE. */
552 gdb::optional<ULONGEST> addr_base;
553
554 /* The DW_AT_GNU_ranges_base attribute, if present.
555
556 This is only relevant in the context of pre-DWARF 5 split units. In this
557 context, there is a .debug_ranges section in the linked executable,
558 containing all the ranges data for all the compilation units. Each
559 skeleton/stub unit has (if needed) a DW_AT_GNU_ranges_base attribute that
560 indicates the base of its contribution to that section. The DW_AT_ranges
561 attributes in the split-unit are of the form DW_FORM_sec_offset and point
562 into the .debug_ranges section of the linked file. However, they are not
563 "true" DW_FORM_sec_offset, because they are relative to the base of their
564 compilation unit's contribution, rather than relative to the beginning of
565 the section. The DW_AT_GNU_ranges_base value must be added to it to make
566 it relative to the beginning of the section.
567
568 Note that the value is zero when we are not in a pre-DWARF 5 split-unit
569 case, so this value can be added without needing to know whether we are in
570 this case or not.
571
572 N.B. If a DW_AT_ranges attribute is found on the DW_TAG_compile_unit in the
573 skeleton/stub, it must not have the base added, as it already points to the
574 right place. And since the DW_TAG_compile_unit DIE in the split-unit can't
575 have a DW_AT_ranges attribute, we can use the
576
577 die->tag != DW_AT_compile_unit
578
579 to determine whether the base should be added or not. */
580 ULONGEST gnu_ranges_base = 0;
581
582 /* The DW_AT_rnglists_base attribute, if present.
583
584 This is used when processing attributes of form DW_FORM_rnglistx in
585 non-split units. Attributes of this form found in a split unit don't
586 use it, as split-unit files have their own non-shared .debug_rnglists.dwo
587 section. */
588 ULONGEST rnglists_base = 0;
589
590 /* The DW_AT_loclists_base attribute if present. */
591 ULONGEST loclist_base = 0;
592
593 /* When reading debug info generated by older versions of rustc, we
594 have to rewrite some union types to be struct types with a
595 variant part. This rewriting must be done after the CU is fully
596 read in, because otherwise at the point of rewriting some struct
597 type might not have been fully processed. So, we keep a list of
598 all such types here and process them after expansion. */
599 std::vector<struct type *> rust_unions;
600
601 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
602 files, the value is implicitly zero. For DWARF 5 version DWO files, the
603 value is often implicit and is the size of the header of
604 .debug_str_offsets section (8 or 4, depending on the address size). */
605 gdb::optional<ULONGEST> str_offsets_base;
606
607 /* Mark used when releasing cached dies. */
608 bool mark : 1;
609
610 /* This CU references .debug_loc. See the symtab->locations_valid field.
611 This test is imperfect as there may exist optimized debug code not using
612 any location list and still facing inlining issues if handled as
613 unoptimized code. For a future better test see GCC PR other/32998. */
614 bool has_loclist : 1;
615
616 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
617 if all the producer_is_* fields are valid. This information is cached
618 because profiling CU expansion showed excessive time spent in
619 producer_is_gxx_lt_4_6. */
620 bool checked_producer : 1;
621 bool producer_is_gxx_lt_4_6 : 1;
622 bool producer_is_gcc_lt_4_3 : 1;
623 bool producer_is_icc : 1;
624 bool producer_is_icc_lt_14 : 1;
625 bool producer_is_codewarrior : 1;
626
627 /* When true, the file that we're processing is known to have
628 debugging info for C++ namespaces. GCC 3.3.x did not produce
629 this information, but later versions do. */
630
631 bool processing_has_namespace_info : 1;
632
633 struct partial_die_info *find_partial_die (sect_offset sect_off);
634
635 /* If this CU was inherited by another CU (via specification,
636 abstract_origin, etc), this is the ancestor CU. */
637 dwarf2_cu *ancestor;
638
639 /* Get the buildsym_compunit for this CU. */
640 buildsym_compunit *get_builder ()
641 {
642 /* If this CU has a builder associated with it, use that. */
643 if (m_builder != nullptr)
644 return m_builder.get ();
645
646 /* Otherwise, search ancestors for a valid builder. */
647 if (ancestor != nullptr)
648 return ancestor->get_builder ();
649
650 return nullptr;
651 }
652 };
653
654 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
655 This includes type_unit_group and quick_file_names. */
656
657 struct stmt_list_hash
658 {
659 /* The DWO unit this table is from or NULL if there is none. */
660 struct dwo_unit *dwo_unit;
661
662 /* Offset in .debug_line or .debug_line.dwo. */
663 sect_offset line_sect_off;
664 };
665
666 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
667 an object of this type. This contains elements of type unit groups
668 that can be shared across objfiles. The non-shareable parts are in
669 type_unit_group_unshareable. */
670
671 struct type_unit_group
672 {
673 /* dwarf2read.c's main "handle" on a TU symtab.
674 To simplify things we create an artificial CU that "includes" all the
675 type units using this stmt_list so that the rest of the code still has
676 a "per_cu" handle on the symtab. */
677 struct dwarf2_per_cu_data per_cu;
678
679 /* The TUs that share this DW_AT_stmt_list entry.
680 This is added to while parsing type units to build partial symtabs,
681 and is deleted afterwards and not used again. */
682 std::vector<signatured_type *> *tus;
683
684 /* The data used to construct the hash key. */
685 struct stmt_list_hash hash;
686 };
687
688 /* These sections are what may appear in a (real or virtual) DWO file. */
689
690 struct dwo_sections
691 {
692 struct dwarf2_section_info abbrev;
693 struct dwarf2_section_info line;
694 struct dwarf2_section_info loc;
695 struct dwarf2_section_info loclists;
696 struct dwarf2_section_info macinfo;
697 struct dwarf2_section_info macro;
698 struct dwarf2_section_info rnglists;
699 struct dwarf2_section_info str;
700 struct dwarf2_section_info str_offsets;
701 /* In the case of a virtual DWO file, these two are unused. */
702 struct dwarf2_section_info info;
703 std::vector<dwarf2_section_info> types;
704 };
705
706 /* CUs/TUs in DWP/DWO files. */
707
708 struct dwo_unit
709 {
710 /* Backlink to the containing struct dwo_file. */
711 struct dwo_file *dwo_file;
712
713 /* The "id" that distinguishes this CU/TU.
714 .debug_info calls this "dwo_id", .debug_types calls this "signature".
715 Since signatures came first, we stick with it for consistency. */
716 ULONGEST signature;
717
718 /* The section this CU/TU lives in, in the DWO file. */
719 struct dwarf2_section_info *section;
720
721 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
722 sect_offset sect_off;
723 unsigned int length;
724
725 /* For types, offset in the type's DIE of the type defined by this TU. */
726 cu_offset type_offset_in_tu;
727 };
728
729 /* include/dwarf2.h defines the DWP section codes.
730 It defines a max value but it doesn't define a min value, which we
731 use for error checking, so provide one. */
732
733 enum dwp_v2_section_ids
734 {
735 DW_SECT_MIN = 1
736 };
737
738 /* Data for one DWO file.
739
740 This includes virtual DWO files (a virtual DWO file is a DWO file as it
741 appears in a DWP file). DWP files don't really have DWO files per se -
742 comdat folding of types "loses" the DWO file they came from, and from
743 a high level view DWP files appear to contain a mass of random types.
744 However, to maintain consistency with the non-DWP case we pretend DWP
745 files contain virtual DWO files, and we assign each TU with one virtual
746 DWO file (generally based on the line and abbrev section offsets -
747 a heuristic that seems to work in practice). */
748
749 struct dwo_file
750 {
751 dwo_file () = default;
752 DISABLE_COPY_AND_ASSIGN (dwo_file);
753
754 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
755 For virtual DWO files the name is constructed from the section offsets
756 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
757 from related CU+TUs. */
758 const char *dwo_name = nullptr;
759
760 /* The DW_AT_comp_dir attribute. */
761 const char *comp_dir = nullptr;
762
763 /* The bfd, when the file is open. Otherwise this is NULL.
764 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
765 gdb_bfd_ref_ptr dbfd;
766
767 /* The sections that make up this DWO file.
768 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
769 sections (for lack of a better name). */
770 struct dwo_sections sections {};
771
772 /* The CUs in the file.
773 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
774 an extension to handle LLVM's Link Time Optimization output (where
775 multiple source files may be compiled into a single object/dwo pair). */
776 htab_up cus;
777
778 /* Table of TUs in the file.
779 Each element is a struct dwo_unit. */
780 htab_up tus;
781 };
782
783 /* These sections are what may appear in a DWP file. */
784
785 struct dwp_sections
786 {
787 /* These are used by all DWP versions (1, 2 and 5). */
788 struct dwarf2_section_info str;
789 struct dwarf2_section_info cu_index;
790 struct dwarf2_section_info tu_index;
791
792 /* These are only used by DWP version 2 and version 5 files.
793 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
794 sections are referenced by section number, and are not recorded here.
795 In DWP version 2 or 5 there is at most one copy of all these sections,
796 each section being (effectively) comprised of the concatenation of all of
797 the individual sections that exist in the version 1 format.
798 To keep the code simple we treat each of these concatenated pieces as a
799 section itself (a virtual section?). */
800 struct dwarf2_section_info abbrev;
801 struct dwarf2_section_info info;
802 struct dwarf2_section_info line;
803 struct dwarf2_section_info loc;
804 struct dwarf2_section_info loclists;
805 struct dwarf2_section_info macinfo;
806 struct dwarf2_section_info macro;
807 struct dwarf2_section_info rnglists;
808 struct dwarf2_section_info str_offsets;
809 struct dwarf2_section_info types;
810 };
811
812 /* These sections are what may appear in a virtual DWO file in DWP version 1.
813 A virtual DWO file is a DWO file as it appears in a DWP file. */
814
815 struct virtual_v1_dwo_sections
816 {
817 struct dwarf2_section_info abbrev;
818 struct dwarf2_section_info line;
819 struct dwarf2_section_info loc;
820 struct dwarf2_section_info macinfo;
821 struct dwarf2_section_info macro;
822 struct dwarf2_section_info str_offsets;
823 /* Each DWP hash table entry records one CU or one TU.
824 That is recorded here, and copied to dwo_unit.section. */
825 struct dwarf2_section_info info_or_types;
826 };
827
828 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
829 In version 2, the sections of the DWO files are concatenated together
830 and stored in one section of that name. Thus each ELF section contains
831 several "virtual" sections. */
832
833 struct virtual_v2_or_v5_dwo_sections
834 {
835 bfd_size_type abbrev_offset;
836 bfd_size_type abbrev_size;
837
838 bfd_size_type line_offset;
839 bfd_size_type line_size;
840
841 bfd_size_type loc_offset;
842 bfd_size_type loc_size;
843
844 bfd_size_type loclists_offset;
845 bfd_size_type loclists_size;
846
847 bfd_size_type macinfo_offset;
848 bfd_size_type macinfo_size;
849
850 bfd_size_type macro_offset;
851 bfd_size_type macro_size;
852
853 bfd_size_type rnglists_offset;
854 bfd_size_type rnglists_size;
855
856 bfd_size_type str_offsets_offset;
857 bfd_size_type str_offsets_size;
858
859 /* Each DWP hash table entry records one CU or one TU.
860 That is recorded here, and copied to dwo_unit.section. */
861 bfd_size_type info_or_types_offset;
862 bfd_size_type info_or_types_size;
863 };
864
865 /* Contents of DWP hash tables. */
866
867 struct dwp_hash_table
868 {
869 uint32_t version, nr_columns;
870 uint32_t nr_units, nr_slots;
871 const gdb_byte *hash_table, *unit_table;
872 union
873 {
874 struct
875 {
876 const gdb_byte *indices;
877 } v1;
878 struct
879 {
880 /* This is indexed by column number and gives the id of the section
881 in that column. */
882 #define MAX_NR_V2_DWO_SECTIONS \
883 (1 /* .debug_info or .debug_types */ \
884 + 1 /* .debug_abbrev */ \
885 + 1 /* .debug_line */ \
886 + 1 /* .debug_loc */ \
887 + 1 /* .debug_str_offsets */ \
888 + 1 /* .debug_macro or .debug_macinfo */)
889 int section_ids[MAX_NR_V2_DWO_SECTIONS];
890 const gdb_byte *offsets;
891 const gdb_byte *sizes;
892 } v2;
893 struct
894 {
895 /* This is indexed by column number and gives the id of the section
896 in that column. */
897 #define MAX_NR_V5_DWO_SECTIONS \
898 (1 /* .debug_info */ \
899 + 1 /* .debug_abbrev */ \
900 + 1 /* .debug_line */ \
901 + 1 /* .debug_loclists */ \
902 + 1 /* .debug_str_offsets */ \
903 + 1 /* .debug_macro */ \
904 + 1 /* .debug_rnglists */)
905 int section_ids[MAX_NR_V5_DWO_SECTIONS];
906 const gdb_byte *offsets;
907 const gdb_byte *sizes;
908 } v5;
909 } section_pool;
910 };
911
912 /* Data for one DWP file. */
913
914 struct dwp_file
915 {
916 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
917 : name (name_),
918 dbfd (std::move (abfd))
919 {
920 }
921
922 /* Name of the file. */
923 const char *name;
924
925 /* File format version. */
926 int version = 0;
927
928 /* The bfd. */
929 gdb_bfd_ref_ptr dbfd;
930
931 /* Section info for this file. */
932 struct dwp_sections sections {};
933
934 /* Table of CUs in the file. */
935 const struct dwp_hash_table *cus = nullptr;
936
937 /* Table of TUs in the file. */
938 const struct dwp_hash_table *tus = nullptr;
939
940 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
941 htab_up loaded_cus;
942 htab_up loaded_tus;
943
944 /* Table to map ELF section numbers to their sections.
945 This is only needed for the DWP V1 file format. */
946 unsigned int num_sections = 0;
947 asection **elf_sections = nullptr;
948 };
949
950 /* Struct used to pass misc. parameters to read_die_and_children, et
951 al. which are used for both .debug_info and .debug_types dies.
952 All parameters here are unchanging for the life of the call. This
953 struct exists to abstract away the constant parameters of die reading. */
954
955 struct die_reader_specs
956 {
957 /* The bfd of die_section. */
958 bfd* abfd;
959
960 /* The CU of the DIE we are parsing. */
961 struct dwarf2_cu *cu;
962
963 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
964 struct dwo_file *dwo_file;
965
966 /* The section the die comes from.
967 This is either .debug_info or .debug_types, or the .dwo variants. */
968 struct dwarf2_section_info *die_section;
969
970 /* die_section->buffer. */
971 const gdb_byte *buffer;
972
973 /* The end of the buffer. */
974 const gdb_byte *buffer_end;
975
976 /* The abbreviation table to use when reading the DIEs. */
977 struct abbrev_table *abbrev_table;
978 };
979
980 /* A subclass of die_reader_specs that holds storage and has complex
981 constructor and destructor behavior. */
982
983 class cutu_reader : public die_reader_specs
984 {
985 public:
986
987 cutu_reader (dwarf2_per_cu_data *this_cu,
988 dwarf2_per_objfile *per_objfile,
989 struct abbrev_table *abbrev_table,
990 dwarf2_cu *existing_cu,
991 bool skip_partial);
992
993 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
994 dwarf2_per_objfile *per_objfile,
995 struct dwarf2_cu *parent_cu = nullptr,
996 struct dwo_file *dwo_file = nullptr);
997
998 DISABLE_COPY_AND_ASSIGN (cutu_reader);
999
1000 const gdb_byte *info_ptr = nullptr;
1001 struct die_info *comp_unit_die = nullptr;
1002 bool dummy_p = false;
1003
1004 /* Release the new CU, putting it on the chain. This cannot be done
1005 for dummy CUs. */
1006 void keep ();
1007
1008 private:
1009 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
1010 dwarf2_per_objfile *per_objfile,
1011 dwarf2_cu *existing_cu);
1012
1013 struct dwarf2_per_cu_data *m_this_cu;
1014 std::unique_ptr<dwarf2_cu> m_new_cu;
1015
1016 /* The ordinary abbreviation table. */
1017 abbrev_table_up m_abbrev_table_holder;
1018
1019 /* The DWO abbreviation table. */
1020 abbrev_table_up m_dwo_abbrev_table;
1021 };
1022
1023 /* When we construct a partial symbol table entry we only
1024 need this much information. */
1025 struct partial_die_info : public allocate_on_obstack
1026 {
1027 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1028
1029 /* Disable assign but still keep copy ctor, which is needed
1030 load_partial_dies. */
1031 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1032
1033 /* Adjust the partial die before generating a symbol for it. This
1034 function may set the is_external flag or change the DIE's
1035 name. */
1036 void fixup (struct dwarf2_cu *cu);
1037
1038 /* Read a minimal amount of information into the minimal die
1039 structure. */
1040 const gdb_byte *read (const struct die_reader_specs *reader,
1041 const struct abbrev_info &abbrev,
1042 const gdb_byte *info_ptr);
1043
1044 /* Compute the name of this partial DIE. This memoizes the
1045 result, so it is safe to call multiple times. */
1046 const char *name (dwarf2_cu *cu);
1047
1048 /* Offset of this DIE. */
1049 const sect_offset sect_off;
1050
1051 /* DWARF-2 tag for this DIE. */
1052 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1053
1054 /* Assorted flags describing the data found in this DIE. */
1055 const unsigned int has_children : 1;
1056
1057 unsigned int is_external : 1;
1058 unsigned int is_declaration : 1;
1059 unsigned int has_type : 1;
1060 unsigned int has_specification : 1;
1061 unsigned int has_pc_info : 1;
1062 unsigned int may_be_inlined : 1;
1063
1064 /* This DIE has been marked DW_AT_main_subprogram. */
1065 unsigned int main_subprogram : 1;
1066
1067 /* Flag set if the SCOPE field of this structure has been
1068 computed. */
1069 unsigned int scope_set : 1;
1070
1071 /* Flag set if the DIE has a byte_size attribute. */
1072 unsigned int has_byte_size : 1;
1073
1074 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1075 unsigned int has_const_value : 1;
1076
1077 /* Flag set if any of the DIE's children are template arguments. */
1078 unsigned int has_template_arguments : 1;
1079
1080 /* Flag set if fixup has been called on this die. */
1081 unsigned int fixup_called : 1;
1082
1083 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1084 unsigned int is_dwz : 1;
1085
1086 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1087 unsigned int spec_is_dwz : 1;
1088
1089 unsigned int canonical_name : 1;
1090
1091 /* The name of this DIE. Normally the value of DW_AT_name, but
1092 sometimes a default name for unnamed DIEs. */
1093 const char *raw_name = nullptr;
1094
1095 /* The linkage name, if present. */
1096 const char *linkage_name = nullptr;
1097
1098 /* The scope to prepend to our children. This is generally
1099 allocated on the comp_unit_obstack, so will disappear
1100 when this compilation unit leaves the cache. */
1101 const char *scope = nullptr;
1102
1103 /* Some data associated with the partial DIE. The tag determines
1104 which field is live. */
1105 union
1106 {
1107 /* The location description associated with this DIE, if any. */
1108 struct dwarf_block *locdesc;
1109 /* The offset of an import, for DW_TAG_imported_unit. */
1110 sect_offset sect_off;
1111 } d {};
1112
1113 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1114 CORE_ADDR lowpc = 0;
1115 CORE_ADDR highpc = 0;
1116
1117 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1118 DW_AT_sibling, if any. */
1119 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1120 could return DW_AT_sibling values to its caller load_partial_dies. */
1121 const gdb_byte *sibling = nullptr;
1122
1123 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1124 DW_AT_specification (or DW_AT_abstract_origin or
1125 DW_AT_extension). */
1126 sect_offset spec_offset {};
1127
1128 /* Pointers to this DIE's parent, first child, and next sibling,
1129 if any. */
1130 struct partial_die_info *die_parent = nullptr;
1131 struct partial_die_info *die_child = nullptr;
1132 struct partial_die_info *die_sibling = nullptr;
1133
1134 friend struct partial_die_info *
1135 dwarf2_cu::find_partial_die (sect_offset sect_off);
1136
1137 private:
1138 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1139 partial_die_info (sect_offset sect_off)
1140 : partial_die_info (sect_off, DW_TAG_padding, 0)
1141 {
1142 }
1143
1144 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1145 int has_children_)
1146 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1147 {
1148 is_external = 0;
1149 is_declaration = 0;
1150 has_type = 0;
1151 has_specification = 0;
1152 has_pc_info = 0;
1153 may_be_inlined = 0;
1154 main_subprogram = 0;
1155 scope_set = 0;
1156 has_byte_size = 0;
1157 has_const_value = 0;
1158 has_template_arguments = 0;
1159 fixup_called = 0;
1160 is_dwz = 0;
1161 spec_is_dwz = 0;
1162 canonical_name = 0;
1163 }
1164 };
1165
1166 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1167 but this would require a corresponding change in unpack_field_as_long
1168 and friends. */
1169 static int bits_per_byte = 8;
1170
1171 struct variant_part_builder;
1172
1173 /* When reading a variant, we track a bit more information about the
1174 field, and store it in an object of this type. */
1175
1176 struct variant_field
1177 {
1178 int first_field = -1;
1179 int last_field = -1;
1180
1181 /* A variant can contain other variant parts. */
1182 std::vector<variant_part_builder> variant_parts;
1183
1184 /* If we see a DW_TAG_variant, then this will be set if this is the
1185 default branch. */
1186 bool default_branch = false;
1187 /* If we see a DW_AT_discr_value, then this will be the discriminant
1188 value. */
1189 ULONGEST discriminant_value = 0;
1190 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1191 data. */
1192 struct dwarf_block *discr_list_data = nullptr;
1193 };
1194
1195 /* This represents a DW_TAG_variant_part. */
1196
1197 struct variant_part_builder
1198 {
1199 /* The offset of the discriminant field. */
1200 sect_offset discriminant_offset {};
1201
1202 /* Variants that are direct children of this variant part. */
1203 std::vector<variant_field> variants;
1204
1205 /* True if we're currently reading a variant. */
1206 bool processing_variant = false;
1207 };
1208
1209 struct nextfield
1210 {
1211 int accessibility = 0;
1212 int virtuality = 0;
1213 /* Variant parts need to find the discriminant, which is a DIE
1214 reference. We track the section offset of each field to make
1215 this link. */
1216 sect_offset offset;
1217 struct field field {};
1218 };
1219
1220 struct fnfieldlist
1221 {
1222 const char *name = nullptr;
1223 std::vector<struct fn_field> fnfields;
1224 };
1225
1226 /* The routines that read and process dies for a C struct or C++ class
1227 pass lists of data member fields and lists of member function fields
1228 in an instance of a field_info structure, as defined below. */
1229 struct field_info
1230 {
1231 /* List of data member and baseclasses fields. */
1232 std::vector<struct nextfield> fields;
1233 std::vector<struct nextfield> baseclasses;
1234
1235 /* Set if the accessibility of one of the fields is not public. */
1236 bool non_public_fields = false;
1237
1238 /* Member function fieldlist array, contains name of possibly overloaded
1239 member function, number of overloaded member functions and a pointer
1240 to the head of the member function field chain. */
1241 std::vector<struct fnfieldlist> fnfieldlists;
1242
1243 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1244 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1245 std::vector<struct decl_field> typedef_field_list;
1246
1247 /* Nested types defined by this class and the number of elements in this
1248 list. */
1249 std::vector<struct decl_field> nested_types_list;
1250
1251 /* If non-null, this is the variant part we are currently
1252 reading. */
1253 variant_part_builder *current_variant_part = nullptr;
1254 /* This holds all the top-level variant parts attached to the type
1255 we're reading. */
1256 std::vector<variant_part_builder> variant_parts;
1257
1258 /* Return the total number of fields (including baseclasses). */
1259 int nfields () const
1260 {
1261 return fields.size () + baseclasses.size ();
1262 }
1263 };
1264
1265 /* Loaded secondary compilation units are kept in memory until they
1266 have not been referenced for the processing of this many
1267 compilation units. Set this to zero to disable caching. Cache
1268 sizes of up to at least twenty will improve startup time for
1269 typical inter-CU-reference binaries, at an obvious memory cost. */
1270 static int dwarf_max_cache_age = 5;
1271 static void
1272 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1273 struct cmd_list_element *c, const char *value)
1274 {
1275 fprintf_filtered (file, _("The upper bound on the age of cached "
1276 "DWARF compilation units is %s.\n"),
1277 value);
1278 }
1279 \f
1280 /* local function prototypes */
1281
1282 static void dwarf2_find_base_address (struct die_info *die,
1283 struct dwarf2_cu *cu);
1284
1285 static dwarf2_psymtab *create_partial_symtab
1286 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1287 const char *name);
1288
1289 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1290 const gdb_byte *info_ptr,
1291 struct die_info *type_unit_die);
1292
1293 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
1294
1295 static void scan_partial_symbols (struct partial_die_info *,
1296 CORE_ADDR *, CORE_ADDR *,
1297 int, struct dwarf2_cu *);
1298
1299 static void add_partial_symbol (struct partial_die_info *,
1300 struct dwarf2_cu *);
1301
1302 static void add_partial_namespace (struct partial_die_info *pdi,
1303 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1304 int set_addrmap, struct dwarf2_cu *cu);
1305
1306 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1307 CORE_ADDR *highpc, int set_addrmap,
1308 struct dwarf2_cu *cu);
1309
1310 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1311 struct dwarf2_cu *cu);
1312
1313 static void add_partial_subprogram (struct partial_die_info *pdi,
1314 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1315 int need_pc, struct dwarf2_cu *cu);
1316
1317 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1318
1319 static struct partial_die_info *load_partial_dies
1320 (const struct die_reader_specs *, const gdb_byte *, int);
1321
1322 /* A pair of partial_die_info and compilation unit. */
1323 struct cu_partial_die_info
1324 {
1325 /* The compilation unit of the partial_die_info. */
1326 struct dwarf2_cu *cu;
1327 /* A partial_die_info. */
1328 struct partial_die_info *pdi;
1329
1330 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1331 : cu (cu),
1332 pdi (pdi)
1333 { /* Nothing. */ }
1334
1335 private:
1336 cu_partial_die_info () = delete;
1337 };
1338
1339 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1340 struct dwarf2_cu *);
1341
1342 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1343 struct attribute *, struct attr_abbrev *,
1344 const gdb_byte *);
1345
1346 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1347 struct attribute *attr, dwarf_tag tag);
1348
1349 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1350
1351 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1352 dwarf2_section_info *, sect_offset);
1353
1354 static const char *read_indirect_string
1355 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
1356 const struct comp_unit_head *, unsigned int *);
1357
1358 static const char *read_indirect_string_at_offset
1359 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
1360
1361 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1362 const gdb_byte *,
1363 unsigned int *);
1364
1365 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1366 ULONGEST str_index);
1367
1368 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1369 ULONGEST str_index);
1370
1371 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1372
1373 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1374 struct dwarf2_cu *);
1375
1376 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1377 struct dwarf2_cu *cu);
1378
1379 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1380
1381 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1382 struct dwarf2_cu *cu);
1383
1384 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1385
1386 static struct die_info *die_specification (struct die_info *die,
1387 struct dwarf2_cu **);
1388
1389 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1390 struct dwarf2_cu *cu);
1391
1392 static void dwarf_decode_lines (struct line_header *, const char *,
1393 struct dwarf2_cu *, dwarf2_psymtab *,
1394 CORE_ADDR, int decode_mapping);
1395
1396 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1397 const char *);
1398
1399 static struct symbol *new_symbol (struct die_info *, struct type *,
1400 struct dwarf2_cu *, struct symbol * = NULL);
1401
1402 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1403 struct dwarf2_cu *);
1404
1405 static void dwarf2_const_value_attr (const struct attribute *attr,
1406 struct type *type,
1407 const char *name,
1408 struct obstack *obstack,
1409 struct dwarf2_cu *cu, LONGEST *value,
1410 const gdb_byte **bytes,
1411 struct dwarf2_locexpr_baton **baton);
1412
1413 static struct type *read_subrange_index_type (struct die_info *die,
1414 struct dwarf2_cu *cu);
1415
1416 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1417
1418 static int need_gnat_info (struct dwarf2_cu *);
1419
1420 static struct type *die_descriptive_type (struct die_info *,
1421 struct dwarf2_cu *);
1422
1423 static void set_descriptive_type (struct type *, struct die_info *,
1424 struct dwarf2_cu *);
1425
1426 static struct type *die_containing_type (struct die_info *,
1427 struct dwarf2_cu *);
1428
1429 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1430 struct dwarf2_cu *);
1431
1432 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1433
1434 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1435
1436 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1437
1438 static char *typename_concat (struct obstack *obs, const char *prefix,
1439 const char *suffix, int physname,
1440 struct dwarf2_cu *cu);
1441
1442 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1443
1444 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1445
1446 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1447
1448 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1449
1450 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1451
1452 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1453
1454 /* Return the .debug_loclists section to use for cu. */
1455 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1456
1457 /* Return the .debug_rnglists section to use for cu. */
1458 static struct dwarf2_section_info *cu_debug_rnglists_section
1459 (struct dwarf2_cu *cu, dwarf_tag tag);
1460
1461 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1462 values. Keep the items ordered with increasing constraints compliance. */
1463 enum pc_bounds_kind
1464 {
1465 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1466 PC_BOUNDS_NOT_PRESENT,
1467
1468 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1469 were present but they do not form a valid range of PC addresses. */
1470 PC_BOUNDS_INVALID,
1471
1472 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1473 PC_BOUNDS_RANGES,
1474
1475 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1476 PC_BOUNDS_HIGH_LOW,
1477 };
1478
1479 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1480 CORE_ADDR *, CORE_ADDR *,
1481 struct dwarf2_cu *,
1482 dwarf2_psymtab *);
1483
1484 static void get_scope_pc_bounds (struct die_info *,
1485 CORE_ADDR *, CORE_ADDR *,
1486 struct dwarf2_cu *);
1487
1488 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1489 CORE_ADDR, struct dwarf2_cu *);
1490
1491 static void dwarf2_add_field (struct field_info *, struct die_info *,
1492 struct dwarf2_cu *);
1493
1494 static void dwarf2_attach_fields_to_type (struct field_info *,
1495 struct type *, struct dwarf2_cu *);
1496
1497 static void dwarf2_add_member_fn (struct field_info *,
1498 struct die_info *, struct type *,
1499 struct dwarf2_cu *);
1500
1501 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1502 struct type *,
1503 struct dwarf2_cu *);
1504
1505 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1506
1507 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1508
1509 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1510
1511 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1512
1513 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1514
1515 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1516
1517 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1518
1519 static struct type *read_module_type (struct die_info *die,
1520 struct dwarf2_cu *cu);
1521
1522 static const char *namespace_name (struct die_info *die,
1523 int *is_anonymous, struct dwarf2_cu *);
1524
1525 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1526
1527 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1528 bool * = nullptr);
1529
1530 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1531 struct dwarf2_cu *);
1532
1533 static struct die_info *read_die_and_siblings_1
1534 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1535 struct die_info *);
1536
1537 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1538 const gdb_byte *info_ptr,
1539 const gdb_byte **new_info_ptr,
1540 struct die_info *parent);
1541
1542 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1543 struct die_info **, const gdb_byte *,
1544 int);
1545
1546 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1547 struct die_info **, const gdb_byte *);
1548
1549 static void process_die (struct die_info *, struct dwarf2_cu *);
1550
1551 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1552 struct objfile *);
1553
1554 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1555
1556 static const char *dwarf2_full_name (const char *name,
1557 struct die_info *die,
1558 struct dwarf2_cu *cu);
1559
1560 static const char *dwarf2_physname (const char *name, struct die_info *die,
1561 struct dwarf2_cu *cu);
1562
1563 static struct die_info *dwarf2_extension (struct die_info *die,
1564 struct dwarf2_cu **);
1565
1566 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1567
1568 static void dump_die_for_error (struct die_info *);
1569
1570 static void dump_die_1 (struct ui_file *, int level, int max_level,
1571 struct die_info *);
1572
1573 /*static*/ void dump_die (struct die_info *, int max_level);
1574
1575 static void store_in_ref_table (struct die_info *,
1576 struct dwarf2_cu *);
1577
1578 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1579 const struct attribute *,
1580 struct dwarf2_cu **);
1581
1582 static struct die_info *follow_die_ref (struct die_info *,
1583 const struct attribute *,
1584 struct dwarf2_cu **);
1585
1586 static struct die_info *follow_die_sig (struct die_info *,
1587 const struct attribute *,
1588 struct dwarf2_cu **);
1589
1590 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1591 struct dwarf2_cu *);
1592
1593 static struct type *get_DW_AT_signature_type (struct die_info *,
1594 const struct attribute *,
1595 struct dwarf2_cu *);
1596
1597 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1598 dwarf2_per_objfile *per_objfile);
1599
1600 static void read_signatured_type (signatured_type *sig_type,
1601 dwarf2_per_objfile *per_objfile);
1602
1603 static int attr_to_dynamic_prop (const struct attribute *attr,
1604 struct die_info *die, struct dwarf2_cu *cu,
1605 struct dynamic_prop *prop, struct type *type);
1606
1607 /* memory allocation interface */
1608
1609 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1610
1611 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1612
1613 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1614
1615 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1616 struct dwarf2_loclist_baton *baton,
1617 const struct attribute *attr);
1618
1619 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1620 struct symbol *sym,
1621 struct dwarf2_cu *cu,
1622 int is_block);
1623
1624 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1625 const gdb_byte *info_ptr,
1626 struct abbrev_info *abbrev);
1627
1628 static hashval_t partial_die_hash (const void *item);
1629
1630 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1631
1632 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1633 (sect_offset sect_off, unsigned int offset_in_dwz,
1634 dwarf2_per_objfile *per_objfile);
1635
1636 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1637 struct die_info *comp_unit_die,
1638 enum language pretend_language);
1639
1640 static struct type *set_die_type (struct die_info *, struct type *,
1641 struct dwarf2_cu *, bool = false);
1642
1643 static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
1644
1645 static int create_all_type_units (dwarf2_per_objfile *per_objfile);
1646
1647 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1648 dwarf2_per_objfile *per_objfile,
1649 dwarf2_cu *existing_cu,
1650 bool skip_partial,
1651 enum language pretend_language);
1652
1653 static void process_full_comp_unit (dwarf2_cu *cu,
1654 enum language pretend_language);
1655
1656 static void process_full_type_unit (dwarf2_cu *cu,
1657 enum language pretend_language);
1658
1659 static void dwarf2_add_dependence (struct dwarf2_cu *,
1660 struct dwarf2_per_cu_data *);
1661
1662 static void dwarf2_mark (struct dwarf2_cu *);
1663
1664 static struct type *get_die_type_at_offset (sect_offset,
1665 dwarf2_per_cu_data *per_cu,
1666 dwarf2_per_objfile *per_objfile);
1667
1668 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1669
1670 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1671 dwarf2_per_objfile *per_objfile,
1672 enum language pretend_language);
1673
1674 static void process_queue (dwarf2_per_objfile *per_objfile);
1675
1676 /* Class, the destructor of which frees all allocated queue entries. This
1677 will only have work to do if an error was thrown while processing the
1678 dwarf. If no error was thrown then the queue entries should have all
1679 been processed, and freed, as we went along. */
1680
1681 class dwarf2_queue_guard
1682 {
1683 public:
1684 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1685 : m_per_objfile (per_objfile)
1686 {
1687 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1688
1689 m_per_objfile->per_bfd->queue.emplace ();
1690 }
1691
1692 /* Free any entries remaining on the queue. There should only be
1693 entries left if we hit an error while processing the dwarf. */
1694 ~dwarf2_queue_guard ()
1695 {
1696 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1697
1698 m_per_objfile->per_bfd->queue.reset ();
1699 }
1700
1701 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1702
1703 private:
1704 dwarf2_per_objfile *m_per_objfile;
1705 };
1706
1707 dwarf2_queue_item::~dwarf2_queue_item ()
1708 {
1709 /* Anything still marked queued is likely to be in an
1710 inconsistent state, so discard it. */
1711 if (per_cu->queued)
1712 {
1713 per_objfile->remove_cu (per_cu);
1714 per_cu->queued = 0;
1715 }
1716 }
1717
1718 /* The return type of find_file_and_directory. Note, the enclosed
1719 string pointers are only valid while this object is valid. */
1720
1721 struct file_and_directory
1722 {
1723 /* The filename. This is never NULL. */
1724 const char *name;
1725
1726 /* The compilation directory. NULL if not known. If we needed to
1727 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1728 points directly to the DW_AT_comp_dir string attribute owned by
1729 the obstack that owns the DIE. */
1730 const char *comp_dir;
1731
1732 /* If we needed to build a new string for comp_dir, this is what
1733 owns the storage. */
1734 std::string comp_dir_storage;
1735 };
1736
1737 static file_and_directory find_file_and_directory (struct die_info *die,
1738 struct dwarf2_cu *cu);
1739
1740 static htab_up allocate_signatured_type_table ();
1741
1742 static htab_up allocate_dwo_unit_table ();
1743
1744 static struct dwo_unit *lookup_dwo_unit_in_dwp
1745 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1746 const char *comp_dir, ULONGEST signature, int is_debug_types);
1747
1748 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1749
1750 static struct dwo_unit *lookup_dwo_comp_unit
1751 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1752 ULONGEST signature);
1753
1754 static struct dwo_unit *lookup_dwo_type_unit
1755 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1756
1757 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1758
1759 /* A unique pointer to a dwo_file. */
1760
1761 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1762
1763 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1764
1765 static void check_producer (struct dwarf2_cu *cu);
1766
1767 static void free_line_header_voidp (void *arg);
1768 \f
1769 /* Various complaints about symbol reading that don't abort the process. */
1770
1771 static void
1772 dwarf2_debug_line_missing_file_complaint (void)
1773 {
1774 complaint (_(".debug_line section has line data without a file"));
1775 }
1776
1777 static void
1778 dwarf2_debug_line_missing_end_sequence_complaint (void)
1779 {
1780 complaint (_(".debug_line section has line "
1781 "program sequence without an end"));
1782 }
1783
1784 static void
1785 dwarf2_complex_location_expr_complaint (void)
1786 {
1787 complaint (_("location expression too complex"));
1788 }
1789
1790 static void
1791 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1792 int arg3)
1793 {
1794 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1795 arg1, arg2, arg3);
1796 }
1797
1798 static void
1799 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1800 {
1801 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1802 arg1, arg2);
1803 }
1804
1805 /* Hash function for line_header_hash. */
1806
1807 static hashval_t
1808 line_header_hash (const struct line_header *ofs)
1809 {
1810 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1811 }
1812
1813 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1814
1815 static hashval_t
1816 line_header_hash_voidp (const void *item)
1817 {
1818 const struct line_header *ofs = (const struct line_header *) item;
1819
1820 return line_header_hash (ofs);
1821 }
1822
1823 /* Equality function for line_header_hash. */
1824
1825 static int
1826 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1827 {
1828 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1829 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1830
1831 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1832 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1833 }
1834
1835 \f
1836
1837 /* See declaration. */
1838
1839 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1840 bool can_copy_)
1841 : obfd (obfd),
1842 can_copy (can_copy_)
1843 {
1844 if (names == NULL)
1845 names = &dwarf2_elf_names;
1846
1847 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1848 locate_sections (obfd, sec, *names);
1849 }
1850
1851 dwarf2_per_bfd::~dwarf2_per_bfd ()
1852 {
1853 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1854 per_cu->imported_symtabs_free ();
1855
1856 for (signatured_type *sig_type : all_type_units)
1857 sig_type->per_cu.imported_symtabs_free ();
1858
1859 /* Everything else should be on this->obstack. */
1860 }
1861
1862 /* See read.h. */
1863
1864 void
1865 dwarf2_per_objfile::remove_all_cus ()
1866 {
1867 gdb_assert (!this->per_bfd->queue.has_value ());
1868
1869 for (auto pair : m_dwarf2_cus)
1870 delete pair.second;
1871
1872 m_dwarf2_cus.clear ();
1873 }
1874
1875 /* A helper class that calls free_cached_comp_units on
1876 destruction. */
1877
1878 class free_cached_comp_units
1879 {
1880 public:
1881
1882 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1883 : m_per_objfile (per_objfile)
1884 {
1885 }
1886
1887 ~free_cached_comp_units ()
1888 {
1889 m_per_objfile->remove_all_cus ();
1890 }
1891
1892 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1893
1894 private:
1895
1896 dwarf2_per_objfile *m_per_objfile;
1897 };
1898
1899 /* See read.h. */
1900
1901 bool
1902 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1903 {
1904 gdb_assert (per_cu->index < this->m_symtabs.size ());
1905
1906 return this->m_symtabs[per_cu->index] != nullptr;
1907 }
1908
1909 /* See read.h. */
1910
1911 compunit_symtab *
1912 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1913 {
1914 gdb_assert (per_cu->index < this->m_symtabs.size ());
1915
1916 return this->m_symtabs[per_cu->index];
1917 }
1918
1919 /* See read.h. */
1920
1921 void
1922 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1923 compunit_symtab *symtab)
1924 {
1925 gdb_assert (per_cu->index < this->m_symtabs.size ());
1926 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1927
1928 this->m_symtabs[per_cu->index] = symtab;
1929 }
1930
1931 /* Try to locate the sections we need for DWARF 2 debugging
1932 information and return true if we have enough to do something.
1933 NAMES points to the dwarf2 section names, or is NULL if the standard
1934 ELF names are used. CAN_COPY is true for formats where symbol
1935 interposition is possible and so symbol values must follow copy
1936 relocation rules. */
1937
1938 int
1939 dwarf2_has_info (struct objfile *objfile,
1940 const struct dwarf2_debug_sections *names,
1941 bool can_copy)
1942 {
1943 if (objfile->flags & OBJF_READNEVER)
1944 return 0;
1945
1946 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1947
1948 if (per_objfile == NULL)
1949 {
1950 dwarf2_per_bfd *per_bfd;
1951
1952 /* We can share a "dwarf2_per_bfd" with other objfiles if the BFD
1953 doesn't require relocations and if there aren't partial symbols
1954 from some other reader. */
1955 if (!objfile_has_partial_symbols (objfile)
1956 && !gdb_bfd_requires_relocations (objfile->obfd))
1957 {
1958 /* See if one has been created for this BFD yet. */
1959 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1960
1961 if (per_bfd == nullptr)
1962 {
1963 /* No, create it now. */
1964 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1965 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1966 }
1967 }
1968 else
1969 {
1970 /* No sharing possible, create one specifically for this objfile. */
1971 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1972 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1973 }
1974
1975 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1976 }
1977
1978 return (!per_objfile->per_bfd->info.is_virtual
1979 && per_objfile->per_bfd->info.s.section != NULL
1980 && !per_objfile->per_bfd->abbrev.is_virtual
1981 && per_objfile->per_bfd->abbrev.s.section != NULL);
1982 }
1983
1984 /* When loading sections, we look either for uncompressed section or for
1985 compressed section names. */
1986
1987 static int
1988 section_is_p (const char *section_name,
1989 const struct dwarf2_section_names *names)
1990 {
1991 if (names->normal != NULL
1992 && strcmp (section_name, names->normal) == 0)
1993 return 1;
1994 if (names->compressed != NULL
1995 && strcmp (section_name, names->compressed) == 0)
1996 return 1;
1997 return 0;
1998 }
1999
2000 /* See declaration. */
2001
2002 void
2003 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
2004 const dwarf2_debug_sections &names)
2005 {
2006 flagword aflag = bfd_section_flags (sectp);
2007
2008 if ((aflag & SEC_HAS_CONTENTS) == 0)
2009 {
2010 }
2011 else if (elf_section_data (sectp)->this_hdr.sh_size
2012 > bfd_get_file_size (abfd))
2013 {
2014 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2015 warning (_("Discarding section %s which has a section size (%s"
2016 ") larger than the file size [in module %s]"),
2017 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2018 bfd_get_filename (abfd));
2019 }
2020 else if (section_is_p (sectp->name, &names.info))
2021 {
2022 this->info.s.section = sectp;
2023 this->info.size = bfd_section_size (sectp);
2024 }
2025 else if (section_is_p (sectp->name, &names.abbrev))
2026 {
2027 this->abbrev.s.section = sectp;
2028 this->abbrev.size = bfd_section_size (sectp);
2029 }
2030 else if (section_is_p (sectp->name, &names.line))
2031 {
2032 this->line.s.section = sectp;
2033 this->line.size = bfd_section_size (sectp);
2034 }
2035 else if (section_is_p (sectp->name, &names.loc))
2036 {
2037 this->loc.s.section = sectp;
2038 this->loc.size = bfd_section_size (sectp);
2039 }
2040 else if (section_is_p (sectp->name, &names.loclists))
2041 {
2042 this->loclists.s.section = sectp;
2043 this->loclists.size = bfd_section_size (sectp);
2044 }
2045 else if (section_is_p (sectp->name, &names.macinfo))
2046 {
2047 this->macinfo.s.section = sectp;
2048 this->macinfo.size = bfd_section_size (sectp);
2049 }
2050 else if (section_is_p (sectp->name, &names.macro))
2051 {
2052 this->macro.s.section = sectp;
2053 this->macro.size = bfd_section_size (sectp);
2054 }
2055 else if (section_is_p (sectp->name, &names.str))
2056 {
2057 this->str.s.section = sectp;
2058 this->str.size = bfd_section_size (sectp);
2059 }
2060 else if (section_is_p (sectp->name, &names.str_offsets))
2061 {
2062 this->str_offsets.s.section = sectp;
2063 this->str_offsets.size = bfd_section_size (sectp);
2064 }
2065 else if (section_is_p (sectp->name, &names.line_str))
2066 {
2067 this->line_str.s.section = sectp;
2068 this->line_str.size = bfd_section_size (sectp);
2069 }
2070 else if (section_is_p (sectp->name, &names.addr))
2071 {
2072 this->addr.s.section = sectp;
2073 this->addr.size = bfd_section_size (sectp);
2074 }
2075 else if (section_is_p (sectp->name, &names.frame))
2076 {
2077 this->frame.s.section = sectp;
2078 this->frame.size = bfd_section_size (sectp);
2079 }
2080 else if (section_is_p (sectp->name, &names.eh_frame))
2081 {
2082 this->eh_frame.s.section = sectp;
2083 this->eh_frame.size = bfd_section_size (sectp);
2084 }
2085 else if (section_is_p (sectp->name, &names.ranges))
2086 {
2087 this->ranges.s.section = sectp;
2088 this->ranges.size = bfd_section_size (sectp);
2089 }
2090 else if (section_is_p (sectp->name, &names.rnglists))
2091 {
2092 this->rnglists.s.section = sectp;
2093 this->rnglists.size = bfd_section_size (sectp);
2094 }
2095 else if (section_is_p (sectp->name, &names.types))
2096 {
2097 struct dwarf2_section_info type_section;
2098
2099 memset (&type_section, 0, sizeof (type_section));
2100 type_section.s.section = sectp;
2101 type_section.size = bfd_section_size (sectp);
2102
2103 this->types.push_back (type_section);
2104 }
2105 else if (section_is_p (sectp->name, &names.gdb_index))
2106 {
2107 this->gdb_index.s.section = sectp;
2108 this->gdb_index.size = bfd_section_size (sectp);
2109 }
2110 else if (section_is_p (sectp->name, &names.debug_names))
2111 {
2112 this->debug_names.s.section = sectp;
2113 this->debug_names.size = bfd_section_size (sectp);
2114 }
2115 else if (section_is_p (sectp->name, &names.debug_aranges))
2116 {
2117 this->debug_aranges.s.section = sectp;
2118 this->debug_aranges.size = bfd_section_size (sectp);
2119 }
2120
2121 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2122 && bfd_section_vma (sectp) == 0)
2123 this->has_section_at_zero = true;
2124 }
2125
2126 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2127 SECTION_NAME. */
2128
2129 void
2130 dwarf2_get_section_info (struct objfile *objfile,
2131 enum dwarf2_section_enum sect,
2132 asection **sectp, const gdb_byte **bufp,
2133 bfd_size_type *sizep)
2134 {
2135 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2136 struct dwarf2_section_info *info;
2137
2138 /* We may see an objfile without any DWARF, in which case we just
2139 return nothing. */
2140 if (per_objfile == NULL)
2141 {
2142 *sectp = NULL;
2143 *bufp = NULL;
2144 *sizep = 0;
2145 return;
2146 }
2147 switch (sect)
2148 {
2149 case DWARF2_DEBUG_FRAME:
2150 info = &per_objfile->per_bfd->frame;
2151 break;
2152 case DWARF2_EH_FRAME:
2153 info = &per_objfile->per_bfd->eh_frame;
2154 break;
2155 default:
2156 gdb_assert_not_reached ("unexpected section");
2157 }
2158
2159 info->read (objfile);
2160
2161 *sectp = info->get_bfd_section ();
2162 *bufp = info->buffer;
2163 *sizep = info->size;
2164 }
2165
2166 /* A helper function to find the sections for a .dwz file. */
2167
2168 static void
2169 locate_dwz_sections (bfd *abfd, asection *sectp, dwz_file *dwz_file)
2170 {
2171 /* Note that we only support the standard ELF names, because .dwz
2172 is ELF-only (at the time of writing). */
2173 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2174 {
2175 dwz_file->abbrev.s.section = sectp;
2176 dwz_file->abbrev.size = bfd_section_size (sectp);
2177 }
2178 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2179 {
2180 dwz_file->info.s.section = sectp;
2181 dwz_file->info.size = bfd_section_size (sectp);
2182 }
2183 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2184 {
2185 dwz_file->str.s.section = sectp;
2186 dwz_file->str.size = bfd_section_size (sectp);
2187 }
2188 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2189 {
2190 dwz_file->line.s.section = sectp;
2191 dwz_file->line.size = bfd_section_size (sectp);
2192 }
2193 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2194 {
2195 dwz_file->macro.s.section = sectp;
2196 dwz_file->macro.size = bfd_section_size (sectp);
2197 }
2198 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2199 {
2200 dwz_file->gdb_index.s.section = sectp;
2201 dwz_file->gdb_index.size = bfd_section_size (sectp);
2202 }
2203 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2204 {
2205 dwz_file->debug_names.s.section = sectp;
2206 dwz_file->debug_names.size = bfd_section_size (sectp);
2207 }
2208 }
2209
2210 /* Attempt to find a .dwz file (whose full path is represented by
2211 FILENAME) in all of the specified debug file directories provided.
2212
2213 Return the equivalent gdb_bfd_ref_ptr of the .dwz file found, or
2214 nullptr if it could not find anything. */
2215
2216 static gdb_bfd_ref_ptr
2217 dwz_search_other_debugdirs (std::string &filename, bfd_byte *buildid,
2218 size_t buildid_len)
2219 {
2220 /* Let's assume that the path represented by FILENAME has the
2221 "/.dwz/" subpath in it. This is what (most) GNU/Linux
2222 distributions do, anyway. */
2223 size_t dwz_pos = filename.find ("/.dwz/");
2224
2225 if (dwz_pos == std::string::npos)
2226 return nullptr;
2227
2228 /* This is an obvious assertion, but it's here more to educate
2229 future readers of this code that FILENAME at DWZ_POS *must*
2230 contain a directory separator. */
2231 gdb_assert (IS_DIR_SEPARATOR (filename[dwz_pos]));
2232
2233 gdb_bfd_ref_ptr dwz_bfd;
2234 std::vector<gdb::unique_xmalloc_ptr<char>> debugdir_vec
2235 = dirnames_to_char_ptr_vec (debug_file_directory);
2236
2237 for (const gdb::unique_xmalloc_ptr<char> &debugdir : debugdir_vec)
2238 {
2239 /* The idea is to iterate over the
2240 debug file directories provided by the user and
2241 replace the hard-coded path in the "filename" by each
2242 debug-file-directory.
2243
2244 For example, suppose that filename is:
2245
2246 /usr/lib/debug/.dwz/foo.dwz
2247
2248 And suppose that we have "$HOME/bar" as the
2249 debug-file-directory. We would then adjust filename
2250 to look like:
2251
2252 $HOME/bar/.dwz/foo.dwz
2253
2254 which would hopefully allow us to find the alt debug
2255 file. */
2256 std::string ddir = debugdir.get ();
2257
2258 if (ddir.empty ())
2259 continue;
2260
2261 /* Make sure the current debug-file-directory ends with a
2262 directory separator. This is needed because, if FILENAME
2263 contains something like "/usr/lib/abcde/.dwz/foo.dwz" and
2264 DDIR is "/usr/lib/abc", then could wrongfully skip it
2265 below. */
2266 if (!IS_DIR_SEPARATOR (ddir.back ()))
2267 ddir += SLASH_STRING;
2268
2269 /* Check whether the beginning of FILENAME is DDIR. If it is,
2270 then we are dealing with a file which we already attempted to
2271 open before, so we just skip it and continue processing the
2272 remaining debug file directories. */
2273 if (filename.size () > ddir.size ()
2274 && filename.compare (0, ddir.size (), ddir) == 0)
2275 continue;
2276
2277 /* Replace FILENAME's default debug-file-directory with
2278 DDIR. */
2279 std::string new_filename = ddir + &filename[dwz_pos + 1];
2280
2281 dwz_bfd = gdb_bfd_open (new_filename.c_str (), gnutarget);
2282
2283 if (dwz_bfd == nullptr)
2284 continue;
2285
2286 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2287 {
2288 dwz_bfd.reset (nullptr);
2289 continue;
2290 }
2291
2292 /* Found it. */
2293 break;
2294 }
2295
2296 return dwz_bfd;
2297 }
2298
2299 /* See dwarf2read.h. */
2300
2301 struct dwz_file *
2302 dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
2303 {
2304 bfd_size_type buildid_len_arg;
2305 size_t buildid_len;
2306 bfd_byte *buildid;
2307
2308 if (per_bfd->dwz_file != NULL)
2309 return per_bfd->dwz_file.get ();
2310
2311 bfd_set_error (bfd_error_no_error);
2312 gdb::unique_xmalloc_ptr<char> data
2313 (bfd_get_alt_debug_link_info (per_bfd->obfd,
2314 &buildid_len_arg, &buildid));
2315 if (data == NULL)
2316 {
2317 if (bfd_get_error () == bfd_error_no_error)
2318 return NULL;
2319 error (_("could not read '.gnu_debugaltlink' section: %s"),
2320 bfd_errmsg (bfd_get_error ()));
2321 }
2322
2323 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2324
2325 buildid_len = (size_t) buildid_len_arg;
2326
2327 std::string filename = data.get ();
2328
2329 if (!IS_ABSOLUTE_PATH (filename.c_str ()))
2330 {
2331 gdb::unique_xmalloc_ptr<char> abs
2332 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
2333
2334 filename = ldirname (abs.get ()) + SLASH_STRING + filename;
2335 }
2336
2337 /* First try the file name given in the section. If that doesn't
2338 work, try to use the build-id instead. */
2339 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename.c_str (), gnutarget));
2340 if (dwz_bfd != NULL)
2341 {
2342 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2343 dwz_bfd.reset (nullptr);
2344 }
2345
2346 if (dwz_bfd == NULL)
2347 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2348
2349 if (dwz_bfd == nullptr)
2350 {
2351 /* If the user has provided us with different
2352 debug file directories, we can try them in order. */
2353 dwz_bfd = dwz_search_other_debugdirs (filename, buildid, buildid_len);
2354 }
2355
2356 if (dwz_bfd == nullptr)
2357 {
2358 gdb::unique_xmalloc_ptr<char> alt_filename;
2359 const char *origname = bfd_get_filename (per_bfd->obfd);
2360
2361 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2362 buildid_len,
2363 origname,
2364 &alt_filename));
2365
2366 if (fd.get () >= 0)
2367 {
2368 /* File successfully retrieved from server. */
2369 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
2370
2371 if (dwz_bfd == nullptr)
2372 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2373 alt_filename.get ());
2374 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2375 dwz_bfd.reset (nullptr);
2376 }
2377 }
2378
2379 if (dwz_bfd == NULL)
2380 error (_("could not find '.gnu_debugaltlink' file for %s"),
2381 bfd_get_filename (per_bfd->obfd));
2382
2383 std::unique_ptr<struct dwz_file> result
2384 (new struct dwz_file (std::move (dwz_bfd)));
2385
2386 for (asection *sec : gdb_bfd_sections (result->dwz_bfd))
2387 locate_dwz_sections (result->dwz_bfd.get (), sec, result.get ());
2388
2389 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2390 per_bfd->dwz_file = std::move (result);
2391 return per_bfd->dwz_file.get ();
2392 }
2393 \f
2394 /* DWARF quick_symbols_functions support. */
2395
2396 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2397 unique line tables, so we maintain a separate table of all .debug_line
2398 derived entries to support the sharing.
2399 All the quick functions need is the list of file names. We discard the
2400 line_header when we're done and don't need to record it here. */
2401 struct quick_file_names
2402 {
2403 /* The data used to construct the hash key. */
2404 struct stmt_list_hash hash;
2405
2406 /* The number of entries in file_names, real_names. */
2407 unsigned int num_file_names;
2408
2409 /* The file names from the line table, after being run through
2410 file_full_name. */
2411 const char **file_names;
2412
2413 /* The file names from the line table after being run through
2414 gdb_realpath. These are computed lazily. */
2415 const char **real_names;
2416 };
2417
2418 /* When using the index (and thus not using psymtabs), each CU has an
2419 object of this type. This is used to hold information needed by
2420 the various "quick" methods. */
2421 struct dwarf2_per_cu_quick_data
2422 {
2423 /* The file table. This can be NULL if there was no file table
2424 or it's currently not read in.
2425 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2426 struct quick_file_names *file_names;
2427
2428 /* A temporary mark bit used when iterating over all CUs in
2429 expand_symtabs_matching. */
2430 unsigned int mark : 1;
2431
2432 /* True if we've tried to read the file table and found there isn't one.
2433 There will be no point in trying to read it again next time. */
2434 unsigned int no_file_data : 1;
2435 };
2436
2437 /* Utility hash function for a stmt_list_hash. */
2438
2439 static hashval_t
2440 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2441 {
2442 hashval_t v = 0;
2443
2444 if (stmt_list_hash->dwo_unit != NULL)
2445 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2446 v += to_underlying (stmt_list_hash->line_sect_off);
2447 return v;
2448 }
2449
2450 /* Utility equality function for a stmt_list_hash. */
2451
2452 static int
2453 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2454 const struct stmt_list_hash *rhs)
2455 {
2456 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2457 return 0;
2458 if (lhs->dwo_unit != NULL
2459 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2460 return 0;
2461
2462 return lhs->line_sect_off == rhs->line_sect_off;
2463 }
2464
2465 /* Hash function for a quick_file_names. */
2466
2467 static hashval_t
2468 hash_file_name_entry (const void *e)
2469 {
2470 const struct quick_file_names *file_data
2471 = (const struct quick_file_names *) e;
2472
2473 return hash_stmt_list_entry (&file_data->hash);
2474 }
2475
2476 /* Equality function for a quick_file_names. */
2477
2478 static int
2479 eq_file_name_entry (const void *a, const void *b)
2480 {
2481 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2482 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2483
2484 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2485 }
2486
2487 /* Delete function for a quick_file_names. */
2488
2489 static void
2490 delete_file_name_entry (void *e)
2491 {
2492 struct quick_file_names *file_data = (struct quick_file_names *) e;
2493 int i;
2494
2495 for (i = 0; i < file_data->num_file_names; ++i)
2496 {
2497 xfree ((void*) file_data->file_names[i]);
2498 if (file_data->real_names)
2499 xfree ((void*) file_data->real_names[i]);
2500 }
2501
2502 /* The space for the struct itself lives on the obstack, so we don't
2503 free it here. */
2504 }
2505
2506 /* Create a quick_file_names hash table. */
2507
2508 static htab_up
2509 create_quick_file_names_table (unsigned int nr_initial_entries)
2510 {
2511 return htab_up (htab_create_alloc (nr_initial_entries,
2512 hash_file_name_entry, eq_file_name_entry,
2513 delete_file_name_entry, xcalloc, xfree));
2514 }
2515
2516 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2517 function is unrelated to symtabs, symtab would have to be created afterwards.
2518 You should call age_cached_comp_units after processing the CU. */
2519
2520 static dwarf2_cu *
2521 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2522 bool skip_partial)
2523 {
2524 if (per_cu->is_debug_types)
2525 load_full_type_unit (per_cu, per_objfile);
2526 else
2527 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2528 skip_partial, language_minimal);
2529
2530 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2531 if (cu == nullptr)
2532 return nullptr; /* Dummy CU. */
2533
2534 dwarf2_find_base_address (cu->dies, cu);
2535
2536 return cu;
2537 }
2538
2539 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2540
2541 static void
2542 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2543 dwarf2_per_objfile *per_objfile, bool skip_partial)
2544 {
2545 /* Skip type_unit_groups, reading the type units they contain
2546 is handled elsewhere. */
2547 if (per_cu->type_unit_group_p ())
2548 return;
2549
2550 {
2551 /* The destructor of dwarf2_queue_guard frees any entries left on
2552 the queue. After this point we're guaranteed to leave this function
2553 with the dwarf queue empty. */
2554 dwarf2_queue_guard q_guard (per_objfile);
2555
2556 if (!per_objfile->symtab_set_p (per_cu))
2557 {
2558 queue_comp_unit (per_cu, per_objfile, language_minimal);
2559 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2560
2561 /* If we just loaded a CU from a DWO, and we're working with an index
2562 that may badly handle TUs, load all the TUs in that DWO as well.
2563 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2564 if (!per_cu->is_debug_types
2565 && cu != NULL
2566 && cu->dwo_unit != NULL
2567 && per_objfile->per_bfd->index_table != NULL
2568 && per_objfile->per_bfd->index_table->version <= 7
2569 /* DWP files aren't supported yet. */
2570 && get_dwp_file (per_objfile) == NULL)
2571 queue_and_load_all_dwo_tus (cu);
2572 }
2573
2574 process_queue (per_objfile);
2575 }
2576
2577 /* Age the cache, releasing compilation units that have not
2578 been used recently. */
2579 per_objfile->age_comp_units ();
2580 }
2581
2582 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2583 the per-objfile for which this symtab is instantiated.
2584
2585 Returns the resulting symbol table. */
2586
2587 static struct compunit_symtab *
2588 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2589 dwarf2_per_objfile *per_objfile,
2590 bool skip_partial)
2591 {
2592 gdb_assert (per_objfile->per_bfd->using_index);
2593
2594 if (!per_objfile->symtab_set_p (per_cu))
2595 {
2596 free_cached_comp_units freer (per_objfile);
2597 scoped_restore decrementer = increment_reading_symtab ();
2598 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2599 process_cu_includes (per_objfile);
2600 }
2601
2602 return per_objfile->get_symtab (per_cu);
2603 }
2604
2605 /* See declaration. */
2606
2607 dwarf2_per_cu_data *
2608 dwarf2_per_bfd::get_cutu (int index)
2609 {
2610 if (index >= this->all_comp_units.size ())
2611 {
2612 index -= this->all_comp_units.size ();
2613 gdb_assert (index < this->all_type_units.size ());
2614 return &this->all_type_units[index]->per_cu;
2615 }
2616
2617 return this->all_comp_units[index];
2618 }
2619
2620 /* See declaration. */
2621
2622 dwarf2_per_cu_data *
2623 dwarf2_per_bfd::get_cu (int index)
2624 {
2625 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2626
2627 return this->all_comp_units[index];
2628 }
2629
2630 /* See declaration. */
2631
2632 signatured_type *
2633 dwarf2_per_bfd::get_tu (int index)
2634 {
2635 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2636
2637 return this->all_type_units[index];
2638 }
2639
2640 /* See read.h. */
2641
2642 dwarf2_per_cu_data *
2643 dwarf2_per_bfd::allocate_per_cu ()
2644 {
2645 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2646 result->per_bfd = this;
2647 result->index = m_num_psymtabs++;
2648 return result;
2649 }
2650
2651 /* See read.h. */
2652
2653 signatured_type *
2654 dwarf2_per_bfd::allocate_signatured_type ()
2655 {
2656 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2657 result->per_cu.per_bfd = this;
2658 result->per_cu.index = m_num_psymtabs++;
2659 return result;
2660 }
2661
2662 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2663 obstack, and constructed with the specified field values. */
2664
2665 static dwarf2_per_cu_data *
2666 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2667 struct dwarf2_section_info *section,
2668 int is_dwz,
2669 sect_offset sect_off, ULONGEST length)
2670 {
2671 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
2672 the_cu->sect_off = sect_off;
2673 the_cu->length = length;
2674 the_cu->section = section;
2675 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2676 struct dwarf2_per_cu_quick_data);
2677 the_cu->is_dwz = is_dwz;
2678 return the_cu;
2679 }
2680
2681 /* A helper for create_cus_from_index that handles a given list of
2682 CUs. */
2683
2684 static void
2685 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2686 const gdb_byte *cu_list, offset_type n_elements,
2687 struct dwarf2_section_info *section,
2688 int is_dwz)
2689 {
2690 for (offset_type i = 0; i < n_elements; i += 2)
2691 {
2692 gdb_static_assert (sizeof (ULONGEST) >= 8);
2693
2694 sect_offset sect_off
2695 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2696 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2697 cu_list += 2 * 8;
2698
2699 dwarf2_per_cu_data *per_cu
2700 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2701 length);
2702 per_bfd->all_comp_units.push_back (per_cu);
2703 }
2704 }
2705
2706 /* Read the CU list from the mapped index, and use it to create all
2707 the CU objects for PER_BFD. */
2708
2709 static void
2710 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2711 const gdb_byte *cu_list, offset_type cu_list_elements,
2712 const gdb_byte *dwz_list, offset_type dwz_elements)
2713 {
2714 gdb_assert (per_bfd->all_comp_units.empty ());
2715 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2716
2717 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2718 &per_bfd->info, 0);
2719
2720 if (dwz_elements == 0)
2721 return;
2722
2723 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2724 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2725 &dwz->info, 1);
2726 }
2727
2728 /* Create the signatured type hash table from the index. */
2729
2730 static void
2731 create_signatured_type_table_from_index
2732 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2733 const gdb_byte *bytes, offset_type elements)
2734 {
2735 gdb_assert (per_bfd->all_type_units.empty ());
2736 per_bfd->all_type_units.reserve (elements / 3);
2737
2738 htab_up sig_types_hash = allocate_signatured_type_table ();
2739
2740 for (offset_type i = 0; i < elements; i += 3)
2741 {
2742 struct signatured_type *sig_type;
2743 ULONGEST signature;
2744 void **slot;
2745 cu_offset type_offset_in_tu;
2746
2747 gdb_static_assert (sizeof (ULONGEST) >= 8);
2748 sect_offset sect_off
2749 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2750 type_offset_in_tu
2751 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2752 BFD_ENDIAN_LITTLE);
2753 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2754 bytes += 3 * 8;
2755
2756 sig_type = per_bfd->allocate_signatured_type ();
2757 sig_type->signature = signature;
2758 sig_type->type_offset_in_tu = type_offset_in_tu;
2759 sig_type->per_cu.is_debug_types = 1;
2760 sig_type->per_cu.section = section;
2761 sig_type->per_cu.sect_off = sect_off;
2762 sig_type->per_cu.v.quick
2763 = OBSTACK_ZALLOC (&per_bfd->obstack,
2764 struct dwarf2_per_cu_quick_data);
2765
2766 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2767 *slot = sig_type;
2768
2769 per_bfd->all_type_units.push_back (sig_type);
2770 }
2771
2772 per_bfd->signatured_types = std::move (sig_types_hash);
2773 }
2774
2775 /* Create the signatured type hash table from .debug_names. */
2776
2777 static void
2778 create_signatured_type_table_from_debug_names
2779 (dwarf2_per_objfile *per_objfile,
2780 const mapped_debug_names &map,
2781 struct dwarf2_section_info *section,
2782 struct dwarf2_section_info *abbrev_section)
2783 {
2784 struct objfile *objfile = per_objfile->objfile;
2785
2786 section->read (objfile);
2787 abbrev_section->read (objfile);
2788
2789 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2790 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2791
2792 htab_up sig_types_hash = allocate_signatured_type_table ();
2793
2794 for (uint32_t i = 0; i < map.tu_count; ++i)
2795 {
2796 struct signatured_type *sig_type;
2797 void **slot;
2798
2799 sect_offset sect_off
2800 = (sect_offset) (extract_unsigned_integer
2801 (map.tu_table_reordered + i * map.offset_size,
2802 map.offset_size,
2803 map.dwarf5_byte_order));
2804
2805 comp_unit_head cu_header;
2806 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2807 abbrev_section,
2808 section->buffer + to_underlying (sect_off),
2809 rcuh_kind::TYPE);
2810
2811 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
2812 sig_type->signature = cu_header.signature;
2813 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2814 sig_type->per_cu.is_debug_types = 1;
2815 sig_type->per_cu.section = section;
2816 sig_type->per_cu.sect_off = sect_off;
2817 sig_type->per_cu.v.quick
2818 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
2819 struct dwarf2_per_cu_quick_data);
2820
2821 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2822 *slot = sig_type;
2823
2824 per_objfile->per_bfd->all_type_units.push_back (sig_type);
2825 }
2826
2827 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2828 }
2829
2830 /* Read the address map data from the mapped index, and use it to
2831 populate the objfile's psymtabs_addrmap. */
2832
2833 static void
2834 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2835 struct mapped_index *index)
2836 {
2837 struct objfile *objfile = per_objfile->objfile;
2838 struct gdbarch *gdbarch = objfile->arch ();
2839 const gdb_byte *iter, *end;
2840 struct addrmap *mutable_map;
2841 CORE_ADDR baseaddr;
2842
2843 auto_obstack temp_obstack;
2844
2845 mutable_map = addrmap_create_mutable (&temp_obstack);
2846
2847 iter = index->address_table.data ();
2848 end = iter + index->address_table.size ();
2849
2850 baseaddr = objfile->text_section_offset ();
2851
2852 while (iter < end)
2853 {
2854 ULONGEST hi, lo, cu_index;
2855 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2856 iter += 8;
2857 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2858 iter += 8;
2859 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2860 iter += 4;
2861
2862 if (lo > hi)
2863 {
2864 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2865 hex_string (lo), hex_string (hi));
2866 continue;
2867 }
2868
2869 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
2870 {
2871 complaint (_(".gdb_index address table has invalid CU number %u"),
2872 (unsigned) cu_index);
2873 continue;
2874 }
2875
2876 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2877 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2878 addrmap_set_empty (mutable_map, lo, hi - 1,
2879 per_objfile->per_bfd->get_cu (cu_index));
2880 }
2881
2882 objfile->partial_symtabs->psymtabs_addrmap
2883 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2884 }
2885
2886 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2887 populate the objfile's psymtabs_addrmap. */
2888
2889 static void
2890 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2891 struct dwarf2_section_info *section)
2892 {
2893 struct objfile *objfile = per_objfile->objfile;
2894 bfd *abfd = objfile->obfd;
2895 struct gdbarch *gdbarch = objfile->arch ();
2896 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2897
2898 auto_obstack temp_obstack;
2899 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2900
2901 std::unordered_map<sect_offset,
2902 dwarf2_per_cu_data *,
2903 gdb::hash_enum<sect_offset>>
2904 debug_info_offset_to_per_cu;
2905 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
2906 {
2907 const auto insertpair
2908 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2909 if (!insertpair.second)
2910 {
2911 warning (_("Section .debug_aranges in %s has duplicate "
2912 "debug_info_offset %s, ignoring .debug_aranges."),
2913 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2914 return;
2915 }
2916 }
2917
2918 section->read (objfile);
2919
2920 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2921
2922 const gdb_byte *addr = section->buffer;
2923
2924 while (addr < section->buffer + section->size)
2925 {
2926 const gdb_byte *const entry_addr = addr;
2927 unsigned int bytes_read;
2928
2929 const LONGEST entry_length = read_initial_length (abfd, addr,
2930 &bytes_read);
2931 addr += bytes_read;
2932
2933 const gdb_byte *const entry_end = addr + entry_length;
2934 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2935 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2936 if (addr + entry_length > section->buffer + section->size)
2937 {
2938 warning (_("Section .debug_aranges in %s entry at offset %s "
2939 "length %s exceeds section length %s, "
2940 "ignoring .debug_aranges."),
2941 objfile_name (objfile),
2942 plongest (entry_addr - section->buffer),
2943 plongest (bytes_read + entry_length),
2944 pulongest (section->size));
2945 return;
2946 }
2947
2948 /* The version number. */
2949 const uint16_t version = read_2_bytes (abfd, addr);
2950 addr += 2;
2951 if (version != 2)
2952 {
2953 warning (_("Section .debug_aranges in %s entry at offset %s "
2954 "has unsupported version %d, ignoring .debug_aranges."),
2955 objfile_name (objfile),
2956 plongest (entry_addr - section->buffer), version);
2957 return;
2958 }
2959
2960 const uint64_t debug_info_offset
2961 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2962 addr += offset_size;
2963 const auto per_cu_it
2964 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2965 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2966 {
2967 warning (_("Section .debug_aranges in %s entry at offset %s "
2968 "debug_info_offset %s does not exists, "
2969 "ignoring .debug_aranges."),
2970 objfile_name (objfile),
2971 plongest (entry_addr - section->buffer),
2972 pulongest (debug_info_offset));
2973 return;
2974 }
2975 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2976
2977 const uint8_t address_size = *addr++;
2978 if (address_size < 1 || address_size > 8)
2979 {
2980 warning (_("Section .debug_aranges in %s entry at offset %s "
2981 "address_size %u is invalid, ignoring .debug_aranges."),
2982 objfile_name (objfile),
2983 plongest (entry_addr - section->buffer), address_size);
2984 return;
2985 }
2986
2987 const uint8_t segment_selector_size = *addr++;
2988 if (segment_selector_size != 0)
2989 {
2990 warning (_("Section .debug_aranges in %s entry at offset %s "
2991 "segment_selector_size %u is not supported, "
2992 "ignoring .debug_aranges."),
2993 objfile_name (objfile),
2994 plongest (entry_addr - section->buffer),
2995 segment_selector_size);
2996 return;
2997 }
2998
2999 /* Must pad to an alignment boundary that is twice the address
3000 size. It is undocumented by the DWARF standard but GCC does
3001 use it. */
3002 for (size_t padding = ((-(addr - section->buffer))
3003 & (2 * address_size - 1));
3004 padding > 0; padding--)
3005 if (*addr++ != 0)
3006 {
3007 warning (_("Section .debug_aranges in %s entry at offset %s "
3008 "padding is not zero, ignoring .debug_aranges."),
3009 objfile_name (objfile),
3010 plongest (entry_addr - section->buffer));
3011 return;
3012 }
3013
3014 for (;;)
3015 {
3016 if (addr + 2 * address_size > entry_end)
3017 {
3018 warning (_("Section .debug_aranges in %s entry at offset %s "
3019 "address list is not properly terminated, "
3020 "ignoring .debug_aranges."),
3021 objfile_name (objfile),
3022 plongest (entry_addr - section->buffer));
3023 return;
3024 }
3025 ULONGEST start = extract_unsigned_integer (addr, address_size,
3026 dwarf5_byte_order);
3027 addr += address_size;
3028 ULONGEST length = extract_unsigned_integer (addr, address_size,
3029 dwarf5_byte_order);
3030 addr += address_size;
3031 if (start == 0 && length == 0)
3032 break;
3033 if (start == 0 && !per_objfile->per_bfd->has_section_at_zero)
3034 {
3035 /* Symbol was eliminated due to a COMDAT group. */
3036 continue;
3037 }
3038 ULONGEST end = start + length;
3039 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3040 - baseaddr);
3041 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3042 - baseaddr);
3043 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3044 }
3045 }
3046
3047 objfile->partial_symtabs->psymtabs_addrmap
3048 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3049 }
3050
3051 /* Find a slot in the mapped index INDEX for the object named NAME.
3052 If NAME is found, set *VEC_OUT to point to the CU vector in the
3053 constant pool and return true. If NAME cannot be found, return
3054 false. */
3055
3056 static bool
3057 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3058 offset_type **vec_out)
3059 {
3060 offset_type hash;
3061 offset_type slot, step;
3062 int (*cmp) (const char *, const char *);
3063
3064 gdb::unique_xmalloc_ptr<char> without_params;
3065 if (current_language->la_language == language_cplus
3066 || current_language->la_language == language_fortran
3067 || current_language->la_language == language_d)
3068 {
3069 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3070 not contain any. */
3071
3072 if (strchr (name, '(') != NULL)
3073 {
3074 without_params = cp_remove_params (name);
3075
3076 if (without_params != NULL)
3077 name = without_params.get ();
3078 }
3079 }
3080
3081 /* Index version 4 did not support case insensitive searches. But the
3082 indices for case insensitive languages are built in lowercase, therefore
3083 simulate our NAME being searched is also lowercased. */
3084 hash = mapped_index_string_hash ((index->version == 4
3085 && case_sensitivity == case_sensitive_off
3086 ? 5 : index->version),
3087 name);
3088
3089 slot = hash & (index->symbol_table.size () - 1);
3090 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3091 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3092
3093 for (;;)
3094 {
3095 const char *str;
3096
3097 const auto &bucket = index->symbol_table[slot];
3098 if (bucket.name == 0 && bucket.vec == 0)
3099 return false;
3100
3101 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3102 if (!cmp (name, str))
3103 {
3104 *vec_out = (offset_type *) (index->constant_pool
3105 + MAYBE_SWAP (bucket.vec));
3106 return true;
3107 }
3108
3109 slot = (slot + step) & (index->symbol_table.size () - 1);
3110 }
3111 }
3112
3113 /* A helper function that reads the .gdb_index from BUFFER and fills
3114 in MAP. FILENAME is the name of the file containing the data;
3115 it is used for error reporting. DEPRECATED_OK is true if it is
3116 ok to use deprecated sections.
3117
3118 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3119 out parameters that are filled in with information about the CU and
3120 TU lists in the section.
3121
3122 Returns true if all went well, false otherwise. */
3123
3124 static bool
3125 read_gdb_index_from_buffer (const char *filename,
3126 bool deprecated_ok,
3127 gdb::array_view<const gdb_byte> buffer,
3128 struct mapped_index *map,
3129 const gdb_byte **cu_list,
3130 offset_type *cu_list_elements,
3131 const gdb_byte **types_list,
3132 offset_type *types_list_elements)
3133 {
3134 const gdb_byte *addr = &buffer[0];
3135
3136 /* Version check. */
3137 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3138 /* Versions earlier than 3 emitted every copy of a psymbol. This
3139 causes the index to behave very poorly for certain requests. Version 3
3140 contained incomplete addrmap. So, it seems better to just ignore such
3141 indices. */
3142 if (version < 4)
3143 {
3144 static int warning_printed = 0;
3145 if (!warning_printed)
3146 {
3147 warning (_("Skipping obsolete .gdb_index section in %s."),
3148 filename);
3149 warning_printed = 1;
3150 }
3151 return 0;
3152 }
3153 /* Index version 4 uses a different hash function than index version
3154 5 and later.
3155
3156 Versions earlier than 6 did not emit psymbols for inlined
3157 functions. Using these files will cause GDB not to be able to
3158 set breakpoints on inlined functions by name, so we ignore these
3159 indices unless the user has done
3160 "set use-deprecated-index-sections on". */
3161 if (version < 6 && !deprecated_ok)
3162 {
3163 static int warning_printed = 0;
3164 if (!warning_printed)
3165 {
3166 warning (_("\
3167 Skipping deprecated .gdb_index section in %s.\n\
3168 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3169 to use the section anyway."),
3170 filename);
3171 warning_printed = 1;
3172 }
3173 return 0;
3174 }
3175 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3176 of the TU (for symbols coming from TUs),
3177 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3178 Plus gold-generated indices can have duplicate entries for global symbols,
3179 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3180 These are just performance bugs, and we can't distinguish gdb-generated
3181 indices from gold-generated ones, so issue no warning here. */
3182
3183 /* Indexes with higher version than the one supported by GDB may be no
3184 longer backward compatible. */
3185 if (version > 8)
3186 return 0;
3187
3188 map->version = version;
3189
3190 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3191
3192 int i = 0;
3193 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3194 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3195 / 8);
3196 ++i;
3197
3198 *types_list = addr + MAYBE_SWAP (metadata[i]);
3199 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3200 - MAYBE_SWAP (metadata[i]))
3201 / 8);
3202 ++i;
3203
3204 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3205 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3206 map->address_table
3207 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3208 ++i;
3209
3210 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3211 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3212 map->symbol_table
3213 = gdb::array_view<mapped_index::symbol_table_slot>
3214 ((mapped_index::symbol_table_slot *) symbol_table,
3215 (mapped_index::symbol_table_slot *) symbol_table_end);
3216
3217 ++i;
3218 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3219
3220 return 1;
3221 }
3222
3223 /* Callback types for dwarf2_read_gdb_index. */
3224
3225 typedef gdb::function_view
3226 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3227 get_gdb_index_contents_ftype;
3228 typedef gdb::function_view
3229 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3230 get_gdb_index_contents_dwz_ftype;
3231
3232 /* Read .gdb_index. If everything went ok, initialize the "quick"
3233 elements of all the CUs and return 1. Otherwise, return 0. */
3234
3235 static int
3236 dwarf2_read_gdb_index
3237 (dwarf2_per_objfile *per_objfile,
3238 get_gdb_index_contents_ftype get_gdb_index_contents,
3239 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3240 {
3241 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3242 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3243 struct dwz_file *dwz;
3244 struct objfile *objfile = per_objfile->objfile;
3245 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
3246
3247 gdb::array_view<const gdb_byte> main_index_contents
3248 = get_gdb_index_contents (objfile, per_bfd);
3249
3250 if (main_index_contents.empty ())
3251 return 0;
3252
3253 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3254 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3255 use_deprecated_index_sections,
3256 main_index_contents, map.get (), &cu_list,
3257 &cu_list_elements, &types_list,
3258 &types_list_elements))
3259 return 0;
3260
3261 /* Don't use the index if it's empty. */
3262 if (map->symbol_table.empty ())
3263 return 0;
3264
3265 /* If there is a .dwz file, read it so we can get its CU list as
3266 well. */
3267 dwz = dwarf2_get_dwz_file (per_bfd);
3268 if (dwz != NULL)
3269 {
3270 struct mapped_index dwz_map;
3271 const gdb_byte *dwz_types_ignore;
3272 offset_type dwz_types_elements_ignore;
3273
3274 gdb::array_view<const gdb_byte> dwz_index_content
3275 = get_gdb_index_contents_dwz (objfile, dwz);
3276
3277 if (dwz_index_content.empty ())
3278 return 0;
3279
3280 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3281 1, dwz_index_content, &dwz_map,
3282 &dwz_list, &dwz_list_elements,
3283 &dwz_types_ignore,
3284 &dwz_types_elements_ignore))
3285 {
3286 warning (_("could not read '.gdb_index' section from %s; skipping"),
3287 bfd_get_filename (dwz->dwz_bfd.get ()));
3288 return 0;
3289 }
3290 }
3291
3292 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3293 dwz_list_elements);
3294
3295 if (types_list_elements)
3296 {
3297 /* We can only handle a single .debug_types when we have an
3298 index. */
3299 if (per_bfd->types.size () != 1)
3300 return 0;
3301
3302 dwarf2_section_info *section = &per_bfd->types[0];
3303
3304 create_signatured_type_table_from_index (per_bfd, section, types_list,
3305 types_list_elements);
3306 }
3307
3308 create_addrmap_from_index (per_objfile, map.get ());
3309
3310 per_bfd->index_table = std::move (map);
3311 per_bfd->using_index = 1;
3312 per_bfd->quick_file_names_table =
3313 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3314
3315 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
3316 objfiles using the same BFD. */
3317 gdb_assert (per_bfd->partial_symtabs == nullptr);
3318 per_bfd->partial_symtabs = objfile->partial_symtabs;
3319
3320 return 1;
3321 }
3322
3323 /* die_reader_func for dw2_get_file_names. */
3324
3325 static void
3326 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3327 const gdb_byte *info_ptr,
3328 struct die_info *comp_unit_die)
3329 {
3330 struct dwarf2_cu *cu = reader->cu;
3331 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3332 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3333 struct dwarf2_per_cu_data *lh_cu;
3334 struct attribute *attr;
3335 void **slot;
3336 struct quick_file_names *qfn;
3337
3338 gdb_assert (! this_cu->is_debug_types);
3339
3340 /* Our callers never want to match partial units -- instead they
3341 will match the enclosing full CU. */
3342 if (comp_unit_die->tag == DW_TAG_partial_unit)
3343 {
3344 this_cu->v.quick->no_file_data = 1;
3345 return;
3346 }
3347
3348 lh_cu = this_cu;
3349 slot = NULL;
3350
3351 line_header_up lh;
3352 sect_offset line_offset {};
3353
3354 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3355 if (attr != nullptr && attr->form_is_unsigned ())
3356 {
3357 struct quick_file_names find_entry;
3358
3359 line_offset = (sect_offset) attr->as_unsigned ();
3360
3361 /* We may have already read in this line header (TU line header sharing).
3362 If we have we're done. */
3363 find_entry.hash.dwo_unit = cu->dwo_unit;
3364 find_entry.hash.line_sect_off = line_offset;
3365 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
3366 &find_entry, INSERT);
3367 if (*slot != NULL)
3368 {
3369 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3370 return;
3371 }
3372
3373 lh = dwarf_decode_line_header (line_offset, cu);
3374 }
3375 if (lh == NULL)
3376 {
3377 lh_cu->v.quick->no_file_data = 1;
3378 return;
3379 }
3380
3381 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
3382 qfn->hash.dwo_unit = cu->dwo_unit;
3383 qfn->hash.line_sect_off = line_offset;
3384 gdb_assert (slot != NULL);
3385 *slot = qfn;
3386
3387 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3388
3389 int offset = 0;
3390 if (strcmp (fnd.name, "<unknown>") != 0)
3391 ++offset;
3392
3393 qfn->num_file_names = offset + lh->file_names_size ();
3394 qfn->file_names =
3395 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
3396 qfn->num_file_names);
3397 if (offset != 0)
3398 qfn->file_names[0] = xstrdup (fnd.name);
3399 for (int i = 0; i < lh->file_names_size (); ++i)
3400 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3401 fnd.comp_dir).release ();
3402 qfn->real_names = NULL;
3403
3404 lh_cu->v.quick->file_names = qfn;
3405 }
3406
3407 /* A helper for the "quick" functions which attempts to read the line
3408 table for THIS_CU. */
3409
3410 static struct quick_file_names *
3411 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3412 dwarf2_per_objfile *per_objfile)
3413 {
3414 /* This should never be called for TUs. */
3415 gdb_assert (! this_cu->is_debug_types);
3416 /* Nor type unit groups. */
3417 gdb_assert (! this_cu->type_unit_group_p ());
3418
3419 if (this_cu->v.quick->file_names != NULL)
3420 return this_cu->v.quick->file_names;
3421 /* If we know there is no line data, no point in looking again. */
3422 if (this_cu->v.quick->no_file_data)
3423 return NULL;
3424
3425 cutu_reader reader (this_cu, per_objfile);
3426 if (!reader.dummy_p)
3427 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3428
3429 if (this_cu->v.quick->no_file_data)
3430 return NULL;
3431 return this_cu->v.quick->file_names;
3432 }
3433
3434 /* A helper for the "quick" functions which computes and caches the
3435 real path for a given file name from the line table. */
3436
3437 static const char *
3438 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3439 struct quick_file_names *qfn, int index)
3440 {
3441 if (qfn->real_names == NULL)
3442 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3443 qfn->num_file_names, const char *);
3444
3445 if (qfn->real_names[index] == NULL)
3446 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3447
3448 return qfn->real_names[index];
3449 }
3450
3451 static struct symtab *
3452 dw2_find_last_source_symtab (struct objfile *objfile)
3453 {
3454 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3455 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3456 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3457
3458 if (cust == NULL)
3459 return NULL;
3460
3461 return compunit_primary_filetab (cust);
3462 }
3463
3464 /* Traversal function for dw2_forget_cached_source_info. */
3465
3466 static int
3467 dw2_free_cached_file_names (void **slot, void *info)
3468 {
3469 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3470
3471 if (file_data->real_names)
3472 {
3473 int i;
3474
3475 for (i = 0; i < file_data->num_file_names; ++i)
3476 {
3477 xfree ((void*) file_data->real_names[i]);
3478 file_data->real_names[i] = NULL;
3479 }
3480 }
3481
3482 return 1;
3483 }
3484
3485 static void
3486 dw2_forget_cached_source_info (struct objfile *objfile)
3487 {
3488 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3489
3490 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
3491 dw2_free_cached_file_names, NULL);
3492 }
3493
3494 /* Helper function for dw2_map_symtabs_matching_filename that expands
3495 the symtabs and calls the iterator. */
3496
3497 static int
3498 dw2_map_expand_apply (struct objfile *objfile,
3499 struct dwarf2_per_cu_data *per_cu,
3500 const char *name, const char *real_path,
3501 gdb::function_view<bool (symtab *)> callback)
3502 {
3503 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3504
3505 /* Don't visit already-expanded CUs. */
3506 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3507 if (per_objfile->symtab_set_p (per_cu))
3508 return 0;
3509
3510 /* This may expand more than one symtab, and we want to iterate over
3511 all of them. */
3512 dw2_instantiate_symtab (per_cu, per_objfile, false);
3513
3514 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3515 last_made, callback);
3516 }
3517
3518 /* Implementation of the map_symtabs_matching_filename method. */
3519
3520 static bool
3521 dw2_map_symtabs_matching_filename
3522 (struct objfile *objfile, const char *name, const char *real_path,
3523 gdb::function_view<bool (symtab *)> callback)
3524 {
3525 const char *name_basename = lbasename (name);
3526 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3527
3528 /* The rule is CUs specify all the files, including those used by
3529 any TU, so there's no need to scan TUs here. */
3530
3531 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3532 {
3533 /* We only need to look at symtabs not already expanded. */
3534 if (per_objfile->symtab_set_p (per_cu))
3535 continue;
3536
3537 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3538 if (file_data == NULL)
3539 continue;
3540
3541 for (int j = 0; j < file_data->num_file_names; ++j)
3542 {
3543 const char *this_name = file_data->file_names[j];
3544 const char *this_real_name;
3545
3546 if (compare_filenames_for_search (this_name, name))
3547 {
3548 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3549 callback))
3550 return true;
3551 continue;
3552 }
3553
3554 /* Before we invoke realpath, which can get expensive when many
3555 files are involved, do a quick comparison of the basenames. */
3556 if (! basenames_may_differ
3557 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3558 continue;
3559
3560 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
3561 if (compare_filenames_for_search (this_real_name, name))
3562 {
3563 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3564 callback))
3565 return true;
3566 continue;
3567 }
3568
3569 if (real_path != NULL)
3570 {
3571 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3572 gdb_assert (IS_ABSOLUTE_PATH (name));
3573 if (this_real_name != NULL
3574 && FILENAME_CMP (real_path, this_real_name) == 0)
3575 {
3576 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3577 callback))
3578 return true;
3579 continue;
3580 }
3581 }
3582 }
3583 }
3584
3585 return false;
3586 }
3587
3588 /* Struct used to manage iterating over all CUs looking for a symbol. */
3589
3590 struct dw2_symtab_iterator
3591 {
3592 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3593 dwarf2_per_objfile *per_objfile;
3594 /* If set, only look for symbols that match that block. Valid values are
3595 GLOBAL_BLOCK and STATIC_BLOCK. */
3596 gdb::optional<block_enum> block_index;
3597 /* The kind of symbol we're looking for. */
3598 domain_enum domain;
3599 /* The list of CUs from the index entry of the symbol,
3600 or NULL if not found. */
3601 offset_type *vec;
3602 /* The next element in VEC to look at. */
3603 int next;
3604 /* The number of elements in VEC, or zero if there is no match. */
3605 int length;
3606 /* Have we seen a global version of the symbol?
3607 If so we can ignore all further global instances.
3608 This is to work around gold/15646, inefficient gold-generated
3609 indices. */
3610 int global_seen;
3611 };
3612
3613 /* Initialize the index symtab iterator ITER, common part. */
3614
3615 static void
3616 dw2_symtab_iter_init_common (struct dw2_symtab_iterator *iter,
3617 dwarf2_per_objfile *per_objfile,
3618 gdb::optional<block_enum> block_index,
3619 domain_enum domain)
3620 {
3621 iter->per_objfile = per_objfile;
3622 iter->block_index = block_index;
3623 iter->domain = domain;
3624 iter->next = 0;
3625 iter->global_seen = 0;
3626 iter->vec = NULL;
3627 iter->length = 0;
3628 }
3629
3630 /* Initialize the index symtab iterator ITER, const char *NAME variant. */
3631
3632 static void
3633 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3634 dwarf2_per_objfile *per_objfile,
3635 gdb::optional<block_enum> block_index,
3636 domain_enum domain,
3637 const char *name)
3638 {
3639 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3640
3641 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3642 /* index is NULL if OBJF_READNOW. */
3643 if (index == NULL)
3644 return;
3645
3646 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3647 iter->length = MAYBE_SWAP (*iter->vec);
3648 }
3649
3650 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3651
3652 static void
3653 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3654 dwarf2_per_objfile *per_objfile,
3655 gdb::optional<block_enum> block_index,
3656 domain_enum domain, offset_type namei)
3657 {
3658 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3659
3660 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3661 /* index is NULL if OBJF_READNOW. */
3662 if (index == NULL)
3663 return;
3664
3665 gdb_assert (!index->symbol_name_slot_invalid (namei));
3666 const auto &bucket = index->symbol_table[namei];
3667
3668 iter->vec = (offset_type *) (index->constant_pool
3669 + MAYBE_SWAP (bucket.vec));
3670 iter->length = MAYBE_SWAP (*iter->vec);
3671 }
3672
3673 /* Return the next matching CU or NULL if there are no more. */
3674
3675 static struct dwarf2_per_cu_data *
3676 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3677 {
3678 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3679
3680 for ( ; iter->next < iter->length; ++iter->next)
3681 {
3682 offset_type cu_index_and_attrs =
3683 MAYBE_SWAP (iter->vec[iter->next + 1]);
3684 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3685 gdb_index_symbol_kind symbol_kind =
3686 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3687 /* Only check the symbol attributes if they're present.
3688 Indices prior to version 7 don't record them,
3689 and indices >= 7 may elide them for certain symbols
3690 (gold does this). */
3691 int attrs_valid =
3692 (per_objfile->per_bfd->index_table->version >= 7
3693 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3694
3695 /* Don't crash on bad data. */
3696 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3697 + per_objfile->per_bfd->all_type_units.size ()))
3698 {
3699 complaint (_(".gdb_index entry has bad CU index"
3700 " [in module %s]"), objfile_name (per_objfile->objfile));
3701 continue;
3702 }
3703
3704 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3705
3706 /* Skip if already read in. */
3707 if (per_objfile->symtab_set_p (per_cu))
3708 continue;
3709
3710 /* Check static vs global. */
3711 if (attrs_valid)
3712 {
3713 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3714
3715 if (iter->block_index.has_value ())
3716 {
3717 bool want_static = *iter->block_index == STATIC_BLOCK;
3718
3719 if (is_static != want_static)
3720 continue;
3721 }
3722
3723 /* Work around gold/15646. */
3724 if (!is_static
3725 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3726 {
3727 if (iter->global_seen)
3728 continue;
3729
3730 iter->global_seen = 1;
3731 }
3732 }
3733
3734 /* Only check the symbol's kind if it has one. */
3735 if (attrs_valid)
3736 {
3737 switch (iter->domain)
3738 {
3739 case VAR_DOMAIN:
3740 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3741 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3742 /* Some types are also in VAR_DOMAIN. */
3743 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3744 continue;
3745 break;
3746 case STRUCT_DOMAIN:
3747 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3748 continue;
3749 break;
3750 case LABEL_DOMAIN:
3751 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3752 continue;
3753 break;
3754 case MODULE_DOMAIN:
3755 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3756 continue;
3757 break;
3758 default:
3759 break;
3760 }
3761 }
3762
3763 ++iter->next;
3764 return per_cu;
3765 }
3766
3767 return NULL;
3768 }
3769
3770 static struct compunit_symtab *
3771 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3772 const char *name, domain_enum domain)
3773 {
3774 struct compunit_symtab *stab_best = NULL;
3775 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3776
3777 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3778
3779 struct dw2_symtab_iterator iter;
3780 struct dwarf2_per_cu_data *per_cu;
3781
3782 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
3783
3784 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3785 {
3786 struct symbol *sym, *with_opaque = NULL;
3787 struct compunit_symtab *stab
3788 = dw2_instantiate_symtab (per_cu, per_objfile, false);
3789 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3790 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3791
3792 sym = block_find_symbol (block, name, domain,
3793 block_find_non_opaque_type_preferred,
3794 &with_opaque);
3795
3796 /* Some caution must be observed with overloaded functions
3797 and methods, since the index will not contain any overload
3798 information (but NAME might contain it). */
3799
3800 if (sym != NULL
3801 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3802 return stab;
3803 if (with_opaque != NULL
3804 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3805 stab_best = stab;
3806
3807 /* Keep looking through other CUs. */
3808 }
3809
3810 return stab_best;
3811 }
3812
3813 static void
3814 dw2_print_stats (struct objfile *objfile)
3815 {
3816 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3817 int total = (per_objfile->per_bfd->all_comp_units.size ()
3818 + per_objfile->per_bfd->all_type_units.size ());
3819 int count = 0;
3820
3821 for (int i = 0; i < total; ++i)
3822 {
3823 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3824
3825 if (!per_objfile->symtab_set_p (per_cu))
3826 ++count;
3827 }
3828 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3829 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3830 }
3831
3832 /* This dumps minimal information about the index.
3833 It is called via "mt print objfiles".
3834 One use is to verify .gdb_index has been loaded by the
3835 gdb.dwarf2/gdb-index.exp testcase. */
3836
3837 static void
3838 dw2_dump (struct objfile *objfile)
3839 {
3840 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3841
3842 gdb_assert (per_objfile->per_bfd->using_index);
3843 printf_filtered (".gdb_index:");
3844 if (per_objfile->per_bfd->index_table != NULL)
3845 {
3846 printf_filtered (" version %d\n",
3847 per_objfile->per_bfd->index_table->version);
3848 }
3849 else
3850 printf_filtered (" faked for \"readnow\"\n");
3851 printf_filtered ("\n");
3852 }
3853
3854 static void
3855 dw2_expand_symtabs_for_function (struct objfile *objfile,
3856 const char *func_name)
3857 {
3858 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3859
3860 struct dw2_symtab_iterator iter;
3861 struct dwarf2_per_cu_data *per_cu;
3862
3863 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
3864
3865 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3866 dw2_instantiate_symtab (per_cu, per_objfile, false);
3867
3868 }
3869
3870 static void
3871 dw2_expand_all_symtabs (struct objfile *objfile)
3872 {
3873 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3874 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3875 + per_objfile->per_bfd->all_type_units.size ());
3876
3877 for (int i = 0; i < total_units; ++i)
3878 {
3879 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3880
3881 /* We don't want to directly expand a partial CU, because if we
3882 read it with the wrong language, then assertion failures can
3883 be triggered later on. See PR symtab/23010. So, tell
3884 dw2_instantiate_symtab to skip partial CUs -- any important
3885 partial CU will be read via DW_TAG_imported_unit anyway. */
3886 dw2_instantiate_symtab (per_cu, per_objfile, true);
3887 }
3888 }
3889
3890 static void
3891 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3892 const char *fullname)
3893 {
3894 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3895
3896 /* We don't need to consider type units here.
3897 This is only called for examining code, e.g. expand_line_sal.
3898 There can be an order of magnitude (or more) more type units
3899 than comp units, and we avoid them if we can. */
3900
3901 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3902 {
3903 /* We only need to look at symtabs not already expanded. */
3904 if (per_objfile->symtab_set_p (per_cu))
3905 continue;
3906
3907 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3908 if (file_data == NULL)
3909 continue;
3910
3911 for (int j = 0; j < file_data->num_file_names; ++j)
3912 {
3913 const char *this_fullname = file_data->file_names[j];
3914
3915 if (filename_cmp (this_fullname, fullname) == 0)
3916 {
3917 dw2_instantiate_symtab (per_cu, per_objfile, false);
3918 break;
3919 }
3920 }
3921 }
3922 }
3923
3924 static void
3925 dw2_expand_symtabs_matching_symbol
3926 (mapped_index_base &index,
3927 const lookup_name_info &lookup_name_in,
3928 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3929 enum search_domain kind,
3930 gdb::function_view<bool (offset_type)> match_callback,
3931 dwarf2_per_objfile *per_objfile);
3932
3933 static void
3934 dw2_expand_symtabs_matching_one
3935 (dwarf2_per_cu_data *per_cu,
3936 dwarf2_per_objfile *per_objfile,
3937 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3938 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3939
3940 static void
3941 dw2_map_matching_symbols
3942 (struct objfile *objfile,
3943 const lookup_name_info &name, domain_enum domain,
3944 int global,
3945 gdb::function_view<symbol_found_callback_ftype> callback,
3946 symbol_compare_ftype *ordered_compare)
3947 {
3948 /* Used for Ada. */
3949 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3950
3951 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3952
3953 if (per_objfile->per_bfd->index_table != nullptr)
3954 {
3955 mapped_index &index = *per_objfile->per_bfd->index_table;
3956
3957 const char *match_name = name.ada ().lookup_name ().c_str ();
3958 auto matcher = [&] (const char *symname)
3959 {
3960 if (ordered_compare == nullptr)
3961 return true;
3962 return ordered_compare (symname, match_name) == 0;
3963 };
3964
3965 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3966 [&] (offset_type namei)
3967 {
3968 struct dw2_symtab_iterator iter;
3969 struct dwarf2_per_cu_data *per_cu;
3970
3971 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3972 namei);
3973 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3974 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3975 nullptr);
3976 return true;
3977 }, per_objfile);
3978 }
3979 else
3980 {
3981 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3982 proceed assuming all symtabs have been read in. */
3983 }
3984
3985 for (compunit_symtab *cust : objfile->compunits ())
3986 {
3987 const struct block *block;
3988
3989 if (cust == NULL)
3990 continue;
3991 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3992 if (!iterate_over_symbols_terminated (block, name,
3993 domain, callback))
3994 return;
3995 }
3996 }
3997
3998 /* Starting from a search name, return the string that finds the upper
3999 bound of all strings that start with SEARCH_NAME in a sorted name
4000 list. Returns the empty string to indicate that the upper bound is
4001 the end of the list. */
4002
4003 static std::string
4004 make_sort_after_prefix_name (const char *search_name)
4005 {
4006 /* When looking to complete "func", we find the upper bound of all
4007 symbols that start with "func" by looking for where we'd insert
4008 the closest string that would follow "func" in lexicographical
4009 order. Usually, that's "func"-with-last-character-incremented,
4010 i.e. "fund". Mind non-ASCII characters, though. Usually those
4011 will be UTF-8 multi-byte sequences, but we can't be certain.
4012 Especially mind the 0xff character, which is a valid character in
4013 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4014 rule out compilers allowing it in identifiers. Note that
4015 conveniently, strcmp/strcasecmp are specified to compare
4016 characters interpreted as unsigned char. So what we do is treat
4017 the whole string as a base 256 number composed of a sequence of
4018 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4019 to 0, and carries 1 to the following more-significant position.
4020 If the very first character in SEARCH_NAME ends up incremented
4021 and carries/overflows, then the upper bound is the end of the
4022 list. The string after the empty string is also the empty
4023 string.
4024
4025 Some examples of this operation:
4026
4027 SEARCH_NAME => "+1" RESULT
4028
4029 "abc" => "abd"
4030 "ab\xff" => "ac"
4031 "\xff" "a" "\xff" => "\xff" "b"
4032 "\xff" => ""
4033 "\xff\xff" => ""
4034 "" => ""
4035
4036 Then, with these symbols for example:
4037
4038 func
4039 func1
4040 fund
4041
4042 completing "func" looks for symbols between "func" and
4043 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4044 which finds "func" and "func1", but not "fund".
4045
4046 And with:
4047
4048 funcÿ (Latin1 'ÿ' [0xff])
4049 funcÿ1
4050 fund
4051
4052 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4053 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4054
4055 And with:
4056
4057 ÿÿ (Latin1 'ÿ' [0xff])
4058 ÿÿ1
4059
4060 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4061 the end of the list.
4062 */
4063 std::string after = search_name;
4064 while (!after.empty () && (unsigned char) after.back () == 0xff)
4065 after.pop_back ();
4066 if (!after.empty ())
4067 after.back () = (unsigned char) after.back () + 1;
4068 return after;
4069 }
4070
4071 /* See declaration. */
4072
4073 std::pair<std::vector<name_component>::const_iterator,
4074 std::vector<name_component>::const_iterator>
4075 mapped_index_base::find_name_components_bounds
4076 (const lookup_name_info &lookup_name_without_params, language lang,
4077 dwarf2_per_objfile *per_objfile) const
4078 {
4079 auto *name_cmp
4080 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4081
4082 const char *lang_name
4083 = lookup_name_without_params.language_lookup_name (lang);
4084
4085 /* Comparison function object for lower_bound that matches against a
4086 given symbol name. */
4087 auto lookup_compare_lower = [&] (const name_component &elem,
4088 const char *name)
4089 {
4090 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
4091 const char *elem_name = elem_qualified + elem.name_offset;
4092 return name_cmp (elem_name, name) < 0;
4093 };
4094
4095 /* Comparison function object for upper_bound that matches against a
4096 given symbol name. */
4097 auto lookup_compare_upper = [&] (const char *name,
4098 const name_component &elem)
4099 {
4100 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
4101 const char *elem_name = elem_qualified + elem.name_offset;
4102 return name_cmp (name, elem_name) < 0;
4103 };
4104
4105 auto begin = this->name_components.begin ();
4106 auto end = this->name_components.end ();
4107
4108 /* Find the lower bound. */
4109 auto lower = [&] ()
4110 {
4111 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
4112 return begin;
4113 else
4114 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
4115 } ();
4116
4117 /* Find the upper bound. */
4118 auto upper = [&] ()
4119 {
4120 if (lookup_name_without_params.completion_mode ())
4121 {
4122 /* In completion mode, we want UPPER to point past all
4123 symbols names that have the same prefix. I.e., with
4124 these symbols, and completing "func":
4125
4126 function << lower bound
4127 function1
4128 other_function << upper bound
4129
4130 We find the upper bound by looking for the insertion
4131 point of "func"-with-last-character-incremented,
4132 i.e. "fund". */
4133 std::string after = make_sort_after_prefix_name (lang_name);
4134 if (after.empty ())
4135 return end;
4136 return std::lower_bound (lower, end, after.c_str (),
4137 lookup_compare_lower);
4138 }
4139 else
4140 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4141 } ();
4142
4143 return {lower, upper};
4144 }
4145
4146 /* See declaration. */
4147
4148 void
4149 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
4150 {
4151 if (!this->name_components.empty ())
4152 return;
4153
4154 this->name_components_casing = case_sensitivity;
4155 auto *name_cmp
4156 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4157
4158 /* The code below only knows how to break apart components of C++
4159 symbol names (and other languages that use '::' as
4160 namespace/module separator) and Ada symbol names. */
4161 auto count = this->symbol_name_count ();
4162 for (offset_type idx = 0; idx < count; idx++)
4163 {
4164 if (this->symbol_name_slot_invalid (idx))
4165 continue;
4166
4167 const char *name = this->symbol_name_at (idx, per_objfile);
4168
4169 /* Add each name component to the name component table. */
4170 unsigned int previous_len = 0;
4171
4172 if (strstr (name, "::") != nullptr)
4173 {
4174 for (unsigned int current_len = cp_find_first_component (name);
4175 name[current_len] != '\0';
4176 current_len += cp_find_first_component (name + current_len))
4177 {
4178 gdb_assert (name[current_len] == ':');
4179 this->name_components.push_back ({previous_len, idx});
4180 /* Skip the '::'. */
4181 current_len += 2;
4182 previous_len = current_len;
4183 }
4184 }
4185 else
4186 {
4187 /* Handle the Ada encoded (aka mangled) form here. */
4188 for (const char *iter = strstr (name, "__");
4189 iter != nullptr;
4190 iter = strstr (iter, "__"))
4191 {
4192 this->name_components.push_back ({previous_len, idx});
4193 iter += 2;
4194 previous_len = iter - name;
4195 }
4196 }
4197
4198 this->name_components.push_back ({previous_len, idx});
4199 }
4200
4201 /* Sort name_components elements by name. */
4202 auto name_comp_compare = [&] (const name_component &left,
4203 const name_component &right)
4204 {
4205 const char *left_qualified
4206 = this->symbol_name_at (left.idx, per_objfile);
4207 const char *right_qualified
4208 = this->symbol_name_at (right.idx, per_objfile);
4209
4210 const char *left_name = left_qualified + left.name_offset;
4211 const char *right_name = right_qualified + right.name_offset;
4212
4213 return name_cmp (left_name, right_name) < 0;
4214 };
4215
4216 std::sort (this->name_components.begin (),
4217 this->name_components.end (),
4218 name_comp_compare);
4219 }
4220
4221 /* Helper for dw2_expand_symtabs_matching that works with a
4222 mapped_index_base instead of the containing objfile. This is split
4223 to a separate function in order to be able to unit test the
4224 name_components matching using a mock mapped_index_base. For each
4225 symbol name that matches, calls MATCH_CALLBACK, passing it the
4226 symbol's index in the mapped_index_base symbol table. */
4227
4228 static void
4229 dw2_expand_symtabs_matching_symbol
4230 (mapped_index_base &index,
4231 const lookup_name_info &lookup_name_in,
4232 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4233 enum search_domain kind,
4234 gdb::function_view<bool (offset_type)> match_callback,
4235 dwarf2_per_objfile *per_objfile)
4236 {
4237 lookup_name_info lookup_name_without_params
4238 = lookup_name_in.make_ignore_params ();
4239
4240 /* Build the symbol name component sorted vector, if we haven't
4241 yet. */
4242 index.build_name_components (per_objfile);
4243
4244 /* The same symbol may appear more than once in the range though.
4245 E.g., if we're looking for symbols that complete "w", and we have
4246 a symbol named "w1::w2", we'll find the two name components for
4247 that same symbol in the range. To be sure we only call the
4248 callback once per symbol, we first collect the symbol name
4249 indexes that matched in a temporary vector and ignore
4250 duplicates. */
4251 std::vector<offset_type> matches;
4252
4253 struct name_and_matcher
4254 {
4255 symbol_name_matcher_ftype *matcher;
4256 const char *name;
4257
4258 bool operator== (const name_and_matcher &other) const
4259 {
4260 return matcher == other.matcher && strcmp (name, other.name) == 0;
4261 }
4262 };
4263
4264 /* A vector holding all the different symbol name matchers, for all
4265 languages. */
4266 std::vector<name_and_matcher> matchers;
4267
4268 for (int i = 0; i < nr_languages; i++)
4269 {
4270 enum language lang_e = (enum language) i;
4271
4272 const language_defn *lang = language_def (lang_e);
4273 symbol_name_matcher_ftype *name_matcher
4274 = lang->get_symbol_name_matcher (lookup_name_without_params);
4275
4276 name_and_matcher key {
4277 name_matcher,
4278 lookup_name_without_params.language_lookup_name (lang_e)
4279 };
4280
4281 /* Don't insert the same comparison routine more than once.
4282 Note that we do this linear walk. This is not a problem in
4283 practice because the number of supported languages is
4284 low. */
4285 if (std::find (matchers.begin (), matchers.end (), key)
4286 != matchers.end ())
4287 continue;
4288 matchers.push_back (std::move (key));
4289
4290 auto bounds
4291 = index.find_name_components_bounds (lookup_name_without_params,
4292 lang_e, per_objfile);
4293
4294 /* Now for each symbol name in range, check to see if we have a name
4295 match, and if so, call the MATCH_CALLBACK callback. */
4296
4297 for (; bounds.first != bounds.second; ++bounds.first)
4298 {
4299 const char *qualified
4300 = index.symbol_name_at (bounds.first->idx, per_objfile);
4301
4302 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4303 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4304 continue;
4305
4306 matches.push_back (bounds.first->idx);
4307 }
4308 }
4309
4310 std::sort (matches.begin (), matches.end ());
4311
4312 /* Finally call the callback, once per match. */
4313 ULONGEST prev = -1;
4314 for (offset_type idx : matches)
4315 {
4316 if (prev != idx)
4317 {
4318 if (!match_callback (idx))
4319 break;
4320 prev = idx;
4321 }
4322 }
4323
4324 /* Above we use a type wider than idx's for 'prev', since 0 and
4325 (offset_type)-1 are both possible values. */
4326 static_assert (sizeof (prev) > sizeof (offset_type), "");
4327 }
4328
4329 #if GDB_SELF_TEST
4330
4331 namespace selftests { namespace dw2_expand_symtabs_matching {
4332
4333 /* A mock .gdb_index/.debug_names-like name index table, enough to
4334 exercise dw2_expand_symtabs_matching_symbol, which works with the
4335 mapped_index_base interface. Builds an index from the symbol list
4336 passed as parameter to the constructor. */
4337 class mock_mapped_index : public mapped_index_base
4338 {
4339 public:
4340 mock_mapped_index (gdb::array_view<const char *> symbols)
4341 : m_symbol_table (symbols)
4342 {}
4343
4344 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4345
4346 /* Return the number of names in the symbol table. */
4347 size_t symbol_name_count () const override
4348 {
4349 return m_symbol_table.size ();
4350 }
4351
4352 /* Get the name of the symbol at IDX in the symbol table. */
4353 const char *symbol_name_at
4354 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
4355 {
4356 return m_symbol_table[idx];
4357 }
4358
4359 private:
4360 gdb::array_view<const char *> m_symbol_table;
4361 };
4362
4363 /* Convenience function that converts a NULL pointer to a "<null>"
4364 string, to pass to print routines. */
4365
4366 static const char *
4367 string_or_null (const char *str)
4368 {
4369 return str != NULL ? str : "<null>";
4370 }
4371
4372 /* Check if a lookup_name_info built from
4373 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4374 index. EXPECTED_LIST is the list of expected matches, in expected
4375 matching order. If no match expected, then an empty list is
4376 specified. Returns true on success. On failure prints a warning
4377 indicating the file:line that failed, and returns false. */
4378
4379 static bool
4380 check_match (const char *file, int line,
4381 mock_mapped_index &mock_index,
4382 const char *name, symbol_name_match_type match_type,
4383 bool completion_mode,
4384 std::initializer_list<const char *> expected_list,
4385 dwarf2_per_objfile *per_objfile)
4386 {
4387 lookup_name_info lookup_name (name, match_type, completion_mode);
4388
4389 bool matched = true;
4390
4391 auto mismatch = [&] (const char *expected_str,
4392 const char *got)
4393 {
4394 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4395 "expected=\"%s\", got=\"%s\"\n"),
4396 file, line,
4397 (match_type == symbol_name_match_type::FULL
4398 ? "FULL" : "WILD"),
4399 name, string_or_null (expected_str), string_or_null (got));
4400 matched = false;
4401 };
4402
4403 auto expected_it = expected_list.begin ();
4404 auto expected_end = expected_list.end ();
4405
4406 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4407 NULL, ALL_DOMAIN,
4408 [&] (offset_type idx)
4409 {
4410 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
4411 const char *expected_str
4412 = expected_it == expected_end ? NULL : *expected_it++;
4413
4414 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4415 mismatch (expected_str, matched_name);
4416 return true;
4417 }, per_objfile);
4418
4419 const char *expected_str
4420 = expected_it == expected_end ? NULL : *expected_it++;
4421 if (expected_str != NULL)
4422 mismatch (expected_str, NULL);
4423
4424 return matched;
4425 }
4426
4427 /* The symbols added to the mock mapped_index for testing (in
4428 canonical form). */
4429 static const char *test_symbols[] = {
4430 "function",
4431 "std::bar",
4432 "std::zfunction",
4433 "std::zfunction2",
4434 "w1::w2",
4435 "ns::foo<char*>",
4436 "ns::foo<int>",
4437 "ns::foo<long>",
4438 "ns2::tmpl<int>::foo2",
4439 "(anonymous namespace)::A::B::C",
4440
4441 /* These are used to check that the increment-last-char in the
4442 matching algorithm for completion doesn't match "t1_fund" when
4443 completing "t1_func". */
4444 "t1_func",
4445 "t1_func1",
4446 "t1_fund",
4447 "t1_fund1",
4448
4449 /* A UTF-8 name with multi-byte sequences to make sure that
4450 cp-name-parser understands this as a single identifier ("função"
4451 is "function" in PT). */
4452 u8"u8função",
4453
4454 /* \377 (0xff) is Latin1 'ÿ'. */
4455 "yfunc\377",
4456
4457 /* \377 (0xff) is Latin1 'ÿ'. */
4458 "\377",
4459 "\377\377123",
4460
4461 /* A name with all sorts of complications. Starts with "z" to make
4462 it easier for the completion tests below. */
4463 #define Z_SYM_NAME \
4464 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4465 "::tuple<(anonymous namespace)::ui*, " \
4466 "std::default_delete<(anonymous namespace)::ui>, void>"
4467
4468 Z_SYM_NAME
4469 };
4470
4471 /* Returns true if the mapped_index_base::find_name_component_bounds
4472 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4473 in completion mode. */
4474
4475 static bool
4476 check_find_bounds_finds (mapped_index_base &index,
4477 const char *search_name,
4478 gdb::array_view<const char *> expected_syms,
4479 dwarf2_per_objfile *per_objfile)
4480 {
4481 lookup_name_info lookup_name (search_name,
4482 symbol_name_match_type::FULL, true);
4483
4484 auto bounds = index.find_name_components_bounds (lookup_name,
4485 language_cplus,
4486 per_objfile);
4487
4488 size_t distance = std::distance (bounds.first, bounds.second);
4489 if (distance != expected_syms.size ())
4490 return false;
4491
4492 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4493 {
4494 auto nc_elem = bounds.first + exp_elem;
4495 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
4496 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4497 return false;
4498 }
4499
4500 return true;
4501 }
4502
4503 /* Test the lower-level mapped_index::find_name_component_bounds
4504 method. */
4505
4506 static void
4507 test_mapped_index_find_name_component_bounds ()
4508 {
4509 mock_mapped_index mock_index (test_symbols);
4510
4511 mock_index.build_name_components (NULL /* per_objfile */);
4512
4513 /* Test the lower-level mapped_index::find_name_component_bounds
4514 method in completion mode. */
4515 {
4516 static const char *expected_syms[] = {
4517 "t1_func",
4518 "t1_func1",
4519 };
4520
4521 SELF_CHECK (check_find_bounds_finds
4522 (mock_index, "t1_func", expected_syms,
4523 NULL /* per_objfile */));
4524 }
4525
4526 /* Check that the increment-last-char in the name matching algorithm
4527 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4528 {
4529 static const char *expected_syms1[] = {
4530 "\377",
4531 "\377\377123",
4532 };
4533 SELF_CHECK (check_find_bounds_finds
4534 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
4535
4536 static const char *expected_syms2[] = {
4537 "\377\377123",
4538 };
4539 SELF_CHECK (check_find_bounds_finds
4540 (mock_index, "\377\377", expected_syms2,
4541 NULL /* per_objfile */));
4542 }
4543 }
4544
4545 /* Test dw2_expand_symtabs_matching_symbol. */
4546
4547 static void
4548 test_dw2_expand_symtabs_matching_symbol ()
4549 {
4550 mock_mapped_index mock_index (test_symbols);
4551
4552 /* We let all tests run until the end even if some fails, for debug
4553 convenience. */
4554 bool any_mismatch = false;
4555
4556 /* Create the expected symbols list (an initializer_list). Needed
4557 because lists have commas, and we need to pass them to CHECK,
4558 which is a macro. */
4559 #define EXPECT(...) { __VA_ARGS__ }
4560
4561 /* Wrapper for check_match that passes down the current
4562 __FILE__/__LINE__. */
4563 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4564 any_mismatch |= !check_match (__FILE__, __LINE__, \
4565 mock_index, \
4566 NAME, MATCH_TYPE, COMPLETION_MODE, \
4567 EXPECTED_LIST, NULL)
4568
4569 /* Identity checks. */
4570 for (const char *sym : test_symbols)
4571 {
4572 /* Should be able to match all existing symbols. */
4573 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4574 EXPECT (sym));
4575
4576 /* Should be able to match all existing symbols with
4577 parameters. */
4578 std::string with_params = std::string (sym) + "(int)";
4579 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4580 EXPECT (sym));
4581
4582 /* Should be able to match all existing symbols with
4583 parameters and qualifiers. */
4584 with_params = std::string (sym) + " ( int ) const";
4585 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4586 EXPECT (sym));
4587
4588 /* This should really find sym, but cp-name-parser.y doesn't
4589 know about lvalue/rvalue qualifiers yet. */
4590 with_params = std::string (sym) + " ( int ) &&";
4591 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4592 {});
4593 }
4594
4595 /* Check that the name matching algorithm for completion doesn't get
4596 confused with Latin1 'ÿ' / 0xff. */
4597 {
4598 static const char str[] = "\377";
4599 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4600 EXPECT ("\377", "\377\377123"));
4601 }
4602
4603 /* Check that the increment-last-char in the matching algorithm for
4604 completion doesn't match "t1_fund" when completing "t1_func". */
4605 {
4606 static const char str[] = "t1_func";
4607 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4608 EXPECT ("t1_func", "t1_func1"));
4609 }
4610
4611 /* Check that completion mode works at each prefix of the expected
4612 symbol name. */
4613 {
4614 static const char str[] = "function(int)";
4615 size_t len = strlen (str);
4616 std::string lookup;
4617
4618 for (size_t i = 1; i < len; i++)
4619 {
4620 lookup.assign (str, i);
4621 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4622 EXPECT ("function"));
4623 }
4624 }
4625
4626 /* While "w" is a prefix of both components, the match function
4627 should still only be called once. */
4628 {
4629 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4630 EXPECT ("w1::w2"));
4631 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4632 EXPECT ("w1::w2"));
4633 }
4634
4635 /* Same, with a "complicated" symbol. */
4636 {
4637 static const char str[] = Z_SYM_NAME;
4638 size_t len = strlen (str);
4639 std::string lookup;
4640
4641 for (size_t i = 1; i < len; i++)
4642 {
4643 lookup.assign (str, i);
4644 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4645 EXPECT (Z_SYM_NAME));
4646 }
4647 }
4648
4649 /* In FULL mode, an incomplete symbol doesn't match. */
4650 {
4651 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4652 {});
4653 }
4654
4655 /* A complete symbol with parameters matches any overload, since the
4656 index has no overload info. */
4657 {
4658 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4659 EXPECT ("std::zfunction", "std::zfunction2"));
4660 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4661 EXPECT ("std::zfunction", "std::zfunction2"));
4662 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4663 EXPECT ("std::zfunction", "std::zfunction2"));
4664 }
4665
4666 /* Check that whitespace is ignored appropriately. A symbol with a
4667 template argument list. */
4668 {
4669 static const char expected[] = "ns::foo<int>";
4670 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4671 EXPECT (expected));
4672 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4673 EXPECT (expected));
4674 }
4675
4676 /* Check that whitespace is ignored appropriately. A symbol with a
4677 template argument list that includes a pointer. */
4678 {
4679 static const char expected[] = "ns::foo<char*>";
4680 /* Try both completion and non-completion modes. */
4681 static const bool completion_mode[2] = {false, true};
4682 for (size_t i = 0; i < 2; i++)
4683 {
4684 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4685 completion_mode[i], EXPECT (expected));
4686 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4687 completion_mode[i], EXPECT (expected));
4688
4689 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4690 completion_mode[i], EXPECT (expected));
4691 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4692 completion_mode[i], EXPECT (expected));
4693 }
4694 }
4695
4696 {
4697 /* Check method qualifiers are ignored. */
4698 static const char expected[] = "ns::foo<char*>";
4699 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4700 symbol_name_match_type::FULL, true, EXPECT (expected));
4701 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4702 symbol_name_match_type::FULL, true, EXPECT (expected));
4703 CHECK_MATCH ("foo < char * > ( int ) const",
4704 symbol_name_match_type::WILD, true, EXPECT (expected));
4705 CHECK_MATCH ("foo < char * > ( int ) &&",
4706 symbol_name_match_type::WILD, true, EXPECT (expected));
4707 }
4708
4709 /* Test lookup names that don't match anything. */
4710 {
4711 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4712 {});
4713
4714 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4715 {});
4716 }
4717
4718 /* Some wild matching tests, exercising "(anonymous namespace)",
4719 which should not be confused with a parameter list. */
4720 {
4721 static const char *syms[] = {
4722 "A::B::C",
4723 "B::C",
4724 "C",
4725 "A :: B :: C ( int )",
4726 "B :: C ( int )",
4727 "C ( int )",
4728 };
4729
4730 for (const char *s : syms)
4731 {
4732 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4733 EXPECT ("(anonymous namespace)::A::B::C"));
4734 }
4735 }
4736
4737 {
4738 static const char expected[] = "ns2::tmpl<int>::foo2";
4739 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4740 EXPECT (expected));
4741 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4742 EXPECT (expected));
4743 }
4744
4745 SELF_CHECK (!any_mismatch);
4746
4747 #undef EXPECT
4748 #undef CHECK_MATCH
4749 }
4750
4751 static void
4752 run_test ()
4753 {
4754 test_mapped_index_find_name_component_bounds ();
4755 test_dw2_expand_symtabs_matching_symbol ();
4756 }
4757
4758 }} // namespace selftests::dw2_expand_symtabs_matching
4759
4760 #endif /* GDB_SELF_TEST */
4761
4762 /* If FILE_MATCHER is NULL or if PER_CU has
4763 dwarf2_per_cu_quick_data::MARK set (see
4764 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4765 EXPANSION_NOTIFY on it. */
4766
4767 static void
4768 dw2_expand_symtabs_matching_one
4769 (dwarf2_per_cu_data *per_cu,
4770 dwarf2_per_objfile *per_objfile,
4771 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4772 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4773 {
4774 if (file_matcher == NULL || per_cu->v.quick->mark)
4775 {
4776 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4777
4778 compunit_symtab *symtab
4779 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4780 gdb_assert (symtab != nullptr);
4781
4782 if (expansion_notify != NULL && symtab_was_null)
4783 expansion_notify (symtab);
4784 }
4785 }
4786
4787 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4788 matched, to expand corresponding CUs that were marked. IDX is the
4789 index of the symbol name that matched. */
4790
4791 static void
4792 dw2_expand_marked_cus
4793 (dwarf2_per_objfile *per_objfile, offset_type idx,
4794 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4795 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4796 search_domain kind)
4797 {
4798 offset_type *vec, vec_len, vec_idx;
4799 bool global_seen = false;
4800 mapped_index &index = *per_objfile->per_bfd->index_table;
4801
4802 vec = (offset_type *) (index.constant_pool
4803 + MAYBE_SWAP (index.symbol_table[idx].vec));
4804 vec_len = MAYBE_SWAP (vec[0]);
4805 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4806 {
4807 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4808 /* This value is only valid for index versions >= 7. */
4809 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4810 gdb_index_symbol_kind symbol_kind =
4811 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4812 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4813 /* Only check the symbol attributes if they're present.
4814 Indices prior to version 7 don't record them,
4815 and indices >= 7 may elide them for certain symbols
4816 (gold does this). */
4817 int attrs_valid =
4818 (index.version >= 7
4819 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4820
4821 /* Work around gold/15646. */
4822 if (attrs_valid
4823 && !is_static
4824 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4825 {
4826 if (global_seen)
4827 continue;
4828
4829 global_seen = true;
4830 }
4831
4832 /* Only check the symbol's kind if it has one. */
4833 if (attrs_valid)
4834 {
4835 switch (kind)
4836 {
4837 case VARIABLES_DOMAIN:
4838 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4839 continue;
4840 break;
4841 case FUNCTIONS_DOMAIN:
4842 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4843 continue;
4844 break;
4845 case TYPES_DOMAIN:
4846 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4847 continue;
4848 break;
4849 case MODULES_DOMAIN:
4850 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4851 continue;
4852 break;
4853 default:
4854 break;
4855 }
4856 }
4857
4858 /* Don't crash on bad data. */
4859 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4860 + per_objfile->per_bfd->all_type_units.size ()))
4861 {
4862 complaint (_(".gdb_index entry has bad CU index"
4863 " [in module %s]"), objfile_name (per_objfile->objfile));
4864 continue;
4865 }
4866
4867 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4868 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4869 expansion_notify);
4870 }
4871 }
4872
4873 /* If FILE_MATCHER is non-NULL, set all the
4874 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4875 that match FILE_MATCHER. */
4876
4877 static void
4878 dw_expand_symtabs_matching_file_matcher
4879 (dwarf2_per_objfile *per_objfile,
4880 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4881 {
4882 if (file_matcher == NULL)
4883 return;
4884
4885 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4886 htab_eq_pointer,
4887 NULL, xcalloc, xfree));
4888 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4889 htab_eq_pointer,
4890 NULL, xcalloc, xfree));
4891
4892 /* The rule is CUs specify all the files, including those used by
4893 any TU, so there's no need to scan TUs here. */
4894
4895 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4896 {
4897 QUIT;
4898
4899 per_cu->v.quick->mark = 0;
4900
4901 /* We only need to look at symtabs not already expanded. */
4902 if (per_objfile->symtab_set_p (per_cu))
4903 continue;
4904
4905 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4906 if (file_data == NULL)
4907 continue;
4908
4909 if (htab_find (visited_not_found.get (), file_data) != NULL)
4910 continue;
4911 else if (htab_find (visited_found.get (), file_data) != NULL)
4912 {
4913 per_cu->v.quick->mark = 1;
4914 continue;
4915 }
4916
4917 for (int j = 0; j < file_data->num_file_names; ++j)
4918 {
4919 const char *this_real_name;
4920
4921 if (file_matcher (file_data->file_names[j], false))
4922 {
4923 per_cu->v.quick->mark = 1;
4924 break;
4925 }
4926
4927 /* Before we invoke realpath, which can get expensive when many
4928 files are involved, do a quick comparison of the basenames. */
4929 if (!basenames_may_differ
4930 && !file_matcher (lbasename (file_data->file_names[j]),
4931 true))
4932 continue;
4933
4934 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4935 if (file_matcher (this_real_name, false))
4936 {
4937 per_cu->v.quick->mark = 1;
4938 break;
4939 }
4940 }
4941
4942 void **slot = htab_find_slot (per_cu->v.quick->mark
4943 ? visited_found.get ()
4944 : visited_not_found.get (),
4945 file_data, INSERT);
4946 *slot = file_data;
4947 }
4948 }
4949
4950 static void
4951 dw2_expand_symtabs_matching
4952 (struct objfile *objfile,
4953 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4954 const lookup_name_info *lookup_name,
4955 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4956 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4957 enum search_domain kind)
4958 {
4959 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4960
4961 /* index_table is NULL if OBJF_READNOW. */
4962 if (!per_objfile->per_bfd->index_table)
4963 return;
4964
4965 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4966
4967 if (symbol_matcher == NULL && lookup_name == NULL)
4968 {
4969 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4970 {
4971 QUIT;
4972
4973 dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4974 file_matcher, expansion_notify);
4975 }
4976 return;
4977 }
4978
4979 mapped_index &index = *per_objfile->per_bfd->index_table;
4980
4981 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4982 symbol_matcher,
4983 kind, [&] (offset_type idx)
4984 {
4985 dw2_expand_marked_cus (per_objfile, idx, file_matcher, expansion_notify,
4986 kind);
4987 return true;
4988 }, per_objfile);
4989 }
4990
4991 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4992 symtab. */
4993
4994 static struct compunit_symtab *
4995 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4996 CORE_ADDR pc)
4997 {
4998 int i;
4999
5000 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5001 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5002 return cust;
5003
5004 if (cust->includes == NULL)
5005 return NULL;
5006
5007 for (i = 0; cust->includes[i]; ++i)
5008 {
5009 struct compunit_symtab *s = cust->includes[i];
5010
5011 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5012 if (s != NULL)
5013 return s;
5014 }
5015
5016 return NULL;
5017 }
5018
5019 static struct compunit_symtab *
5020 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5021 struct bound_minimal_symbol msymbol,
5022 CORE_ADDR pc,
5023 struct obj_section *section,
5024 int warn_if_readin)
5025 {
5026 struct dwarf2_per_cu_data *data;
5027 struct compunit_symtab *result;
5028
5029 if (!objfile->partial_symtabs->psymtabs_addrmap)
5030 return NULL;
5031
5032 CORE_ADDR baseaddr = objfile->text_section_offset ();
5033 data = (struct dwarf2_per_cu_data *) addrmap_find
5034 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
5035 if (!data)
5036 return NULL;
5037
5038 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5039 if (warn_if_readin && per_objfile->symtab_set_p (data))
5040 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5041 paddress (objfile->arch (), pc));
5042
5043 result = recursively_find_pc_sect_compunit_symtab
5044 (dw2_instantiate_symtab (data, per_objfile, false), pc);
5045
5046 gdb_assert (result != NULL);
5047 return result;
5048 }
5049
5050 static void
5051 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5052 void *data, int need_fullname)
5053 {
5054 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5055
5056 if (!per_objfile->per_bfd->filenames_cache)
5057 {
5058 per_objfile->per_bfd->filenames_cache.emplace ();
5059
5060 htab_up visited (htab_create_alloc (10,
5061 htab_hash_pointer, htab_eq_pointer,
5062 NULL, xcalloc, xfree));
5063
5064 /* The rule is CUs specify all the files, including those used
5065 by any TU, so there's no need to scan TUs here. We can
5066 ignore file names coming from already-expanded CUs. */
5067
5068 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5069 {
5070 if (per_objfile->symtab_set_p (per_cu))
5071 {
5072 void **slot = htab_find_slot (visited.get (),
5073 per_cu->v.quick->file_names,
5074 INSERT);
5075
5076 *slot = per_cu->v.quick->file_names;
5077 }
5078 }
5079
5080 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5081 {
5082 /* We only need to look at symtabs not already expanded. */
5083 if (per_objfile->symtab_set_p (per_cu))
5084 continue;
5085
5086 quick_file_names *file_data
5087 = dw2_get_file_names (per_cu, per_objfile);
5088 if (file_data == NULL)
5089 continue;
5090
5091 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
5092 if (*slot)
5093 {
5094 /* Already visited. */
5095 continue;
5096 }
5097 *slot = file_data;
5098
5099 for (int j = 0; j < file_data->num_file_names; ++j)
5100 {
5101 const char *filename = file_data->file_names[j];
5102 per_objfile->per_bfd->filenames_cache->seen (filename);
5103 }
5104 }
5105 }
5106
5107 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
5108 {
5109 gdb::unique_xmalloc_ptr<char> this_real_name;
5110
5111 if (need_fullname)
5112 this_real_name = gdb_realpath (filename);
5113 (*fun) (filename, this_real_name.get (), data);
5114 });
5115 }
5116
5117 static int
5118 dw2_has_symbols (struct objfile *objfile)
5119 {
5120 return 1;
5121 }
5122
5123 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5124 {
5125 dw2_has_symbols,
5126 dw2_find_last_source_symtab,
5127 dw2_forget_cached_source_info,
5128 dw2_map_symtabs_matching_filename,
5129 dw2_lookup_symbol,
5130 NULL,
5131 dw2_print_stats,
5132 dw2_dump,
5133 dw2_expand_symtabs_for_function,
5134 dw2_expand_all_symtabs,
5135 dw2_expand_symtabs_with_fullname,
5136 dw2_map_matching_symbols,
5137 dw2_expand_symtabs_matching,
5138 dw2_find_pc_sect_compunit_symtab,
5139 NULL,
5140 dw2_map_symbol_filenames
5141 };
5142
5143 /* DWARF-5 debug_names reader. */
5144
5145 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5146 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5147
5148 /* A helper function that reads the .debug_names section in SECTION
5149 and fills in MAP. FILENAME is the name of the file containing the
5150 section; it is used for error reporting.
5151
5152 Returns true if all went well, false otherwise. */
5153
5154 static bool
5155 read_debug_names_from_section (struct objfile *objfile,
5156 const char *filename,
5157 struct dwarf2_section_info *section,
5158 mapped_debug_names &map)
5159 {
5160 if (section->empty ())
5161 return false;
5162
5163 /* Older elfutils strip versions could keep the section in the main
5164 executable while splitting it for the separate debug info file. */
5165 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5166 return false;
5167
5168 section->read (objfile);
5169
5170 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
5171
5172 const gdb_byte *addr = section->buffer;
5173
5174 bfd *const abfd = section->get_bfd_owner ();
5175
5176 unsigned int bytes_read;
5177 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5178 addr += bytes_read;
5179
5180 map.dwarf5_is_dwarf64 = bytes_read != 4;
5181 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5182 if (bytes_read + length != section->size)
5183 {
5184 /* There may be multiple per-CU indices. */
5185 warning (_("Section .debug_names in %s length %s does not match "
5186 "section length %s, ignoring .debug_names."),
5187 filename, plongest (bytes_read + length),
5188 pulongest (section->size));
5189 return false;
5190 }
5191
5192 /* The version number. */
5193 uint16_t version = read_2_bytes (abfd, addr);
5194 addr += 2;
5195 if (version != 5)
5196 {
5197 warning (_("Section .debug_names in %s has unsupported version %d, "
5198 "ignoring .debug_names."),
5199 filename, version);
5200 return false;
5201 }
5202
5203 /* Padding. */
5204 uint16_t padding = read_2_bytes (abfd, addr);
5205 addr += 2;
5206 if (padding != 0)
5207 {
5208 warning (_("Section .debug_names in %s has unsupported padding %d, "
5209 "ignoring .debug_names."),
5210 filename, padding);
5211 return false;
5212 }
5213
5214 /* comp_unit_count - The number of CUs in the CU list. */
5215 map.cu_count = read_4_bytes (abfd, addr);
5216 addr += 4;
5217
5218 /* local_type_unit_count - The number of TUs in the local TU
5219 list. */
5220 map.tu_count = read_4_bytes (abfd, addr);
5221 addr += 4;
5222
5223 /* foreign_type_unit_count - The number of TUs in the foreign TU
5224 list. */
5225 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5226 addr += 4;
5227 if (foreign_tu_count != 0)
5228 {
5229 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5230 "ignoring .debug_names."),
5231 filename, static_cast<unsigned long> (foreign_tu_count));
5232 return false;
5233 }
5234
5235 /* bucket_count - The number of hash buckets in the hash lookup
5236 table. */
5237 map.bucket_count = read_4_bytes (abfd, addr);
5238 addr += 4;
5239
5240 /* name_count - The number of unique names in the index. */
5241 map.name_count = read_4_bytes (abfd, addr);
5242 addr += 4;
5243
5244 /* abbrev_table_size - The size in bytes of the abbreviations
5245 table. */
5246 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5247 addr += 4;
5248
5249 /* augmentation_string_size - The size in bytes of the augmentation
5250 string. This value is rounded up to a multiple of 4. */
5251 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5252 addr += 4;
5253 map.augmentation_is_gdb = ((augmentation_string_size
5254 == sizeof (dwarf5_augmentation))
5255 && memcmp (addr, dwarf5_augmentation,
5256 sizeof (dwarf5_augmentation)) == 0);
5257 augmentation_string_size += (-augmentation_string_size) & 3;
5258 addr += augmentation_string_size;
5259
5260 /* List of CUs */
5261 map.cu_table_reordered = addr;
5262 addr += map.cu_count * map.offset_size;
5263
5264 /* List of Local TUs */
5265 map.tu_table_reordered = addr;
5266 addr += map.tu_count * map.offset_size;
5267
5268 /* Hash Lookup Table */
5269 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5270 addr += map.bucket_count * 4;
5271 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5272 addr += map.name_count * 4;
5273
5274 /* Name Table */
5275 map.name_table_string_offs_reordered = addr;
5276 addr += map.name_count * map.offset_size;
5277 map.name_table_entry_offs_reordered = addr;
5278 addr += map.name_count * map.offset_size;
5279
5280 const gdb_byte *abbrev_table_start = addr;
5281 for (;;)
5282 {
5283 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5284 addr += bytes_read;
5285 if (index_num == 0)
5286 break;
5287
5288 const auto insertpair
5289 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5290 if (!insertpair.second)
5291 {
5292 warning (_("Section .debug_names in %s has duplicate index %s, "
5293 "ignoring .debug_names."),
5294 filename, pulongest (index_num));
5295 return false;
5296 }
5297 mapped_debug_names::index_val &indexval = insertpair.first->second;
5298 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5299 addr += bytes_read;
5300
5301 for (;;)
5302 {
5303 mapped_debug_names::index_val::attr attr;
5304 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5305 addr += bytes_read;
5306 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5307 addr += bytes_read;
5308 if (attr.form == DW_FORM_implicit_const)
5309 {
5310 attr.implicit_const = read_signed_leb128 (abfd, addr,
5311 &bytes_read);
5312 addr += bytes_read;
5313 }
5314 if (attr.dw_idx == 0 && attr.form == 0)
5315 break;
5316 indexval.attr_vec.push_back (std::move (attr));
5317 }
5318 }
5319 if (addr != abbrev_table_start + abbrev_table_size)
5320 {
5321 warning (_("Section .debug_names in %s has abbreviation_table "
5322 "of size %s vs. written as %u, ignoring .debug_names."),
5323 filename, plongest (addr - abbrev_table_start),
5324 abbrev_table_size);
5325 return false;
5326 }
5327 map.entry_pool = addr;
5328
5329 return true;
5330 }
5331
5332 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5333 list. */
5334
5335 static void
5336 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
5337 const mapped_debug_names &map,
5338 dwarf2_section_info &section,
5339 bool is_dwz)
5340 {
5341 if (!map.augmentation_is_gdb)
5342 {
5343 for (uint32_t i = 0; i < map.cu_count; ++i)
5344 {
5345 sect_offset sect_off
5346 = (sect_offset) (extract_unsigned_integer
5347 (map.cu_table_reordered + i * map.offset_size,
5348 map.offset_size,
5349 map.dwarf5_byte_order));
5350 /* We don't know the length of the CU, because the CU list in a
5351 .debug_names index can be incomplete, so we can't use the start
5352 of the next CU as end of this CU. We create the CUs here with
5353 length 0, and in cutu_reader::cutu_reader we'll fill in the
5354 actual length. */
5355 dwarf2_per_cu_data *per_cu
5356 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5357 sect_off, 0);
5358 per_bfd->all_comp_units.push_back (per_cu);
5359 }
5360 return;
5361 }
5362
5363 sect_offset sect_off_prev;
5364 for (uint32_t i = 0; i <= map.cu_count; ++i)
5365 {
5366 sect_offset sect_off_next;
5367 if (i < map.cu_count)
5368 {
5369 sect_off_next
5370 = (sect_offset) (extract_unsigned_integer
5371 (map.cu_table_reordered + i * map.offset_size,
5372 map.offset_size,
5373 map.dwarf5_byte_order));
5374 }
5375 else
5376 sect_off_next = (sect_offset) section.size;
5377 if (i >= 1)
5378 {
5379 const ULONGEST length = sect_off_next - sect_off_prev;
5380 dwarf2_per_cu_data *per_cu
5381 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5382 sect_off_prev, length);
5383 per_bfd->all_comp_units.push_back (per_cu);
5384 }
5385 sect_off_prev = sect_off_next;
5386 }
5387 }
5388
5389 /* Read the CU list from the mapped index, and use it to create all
5390 the CU objects for this dwarf2_per_objfile. */
5391
5392 static void
5393 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
5394 const mapped_debug_names &map,
5395 const mapped_debug_names &dwz_map)
5396 {
5397 gdb_assert (per_bfd->all_comp_units.empty ());
5398 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5399
5400 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
5401 false /* is_dwz */);
5402
5403 if (dwz_map.cu_count == 0)
5404 return;
5405
5406 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5407 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
5408 true /* is_dwz */);
5409 }
5410
5411 /* Read .debug_names. If everything went ok, initialize the "quick"
5412 elements of all the CUs and return true. Otherwise, return false. */
5413
5414 static bool
5415 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
5416 {
5417 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5418 mapped_debug_names dwz_map;
5419 struct objfile *objfile = per_objfile->objfile;
5420 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5421
5422 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5423 &per_objfile->per_bfd->debug_names, *map))
5424 return false;
5425
5426 /* Don't use the index if it's empty. */
5427 if (map->name_count == 0)
5428 return false;
5429
5430 /* If there is a .dwz file, read it so we can get its CU list as
5431 well. */
5432 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5433 if (dwz != NULL)
5434 {
5435 if (!read_debug_names_from_section (objfile,
5436 bfd_get_filename (dwz->dwz_bfd.get ()),
5437 &dwz->debug_names, dwz_map))
5438 {
5439 warning (_("could not read '.debug_names' section from %s; skipping"),
5440 bfd_get_filename (dwz->dwz_bfd.get ()));
5441 return false;
5442 }
5443 }
5444
5445 create_cus_from_debug_names (per_bfd, *map, dwz_map);
5446
5447 if (map->tu_count != 0)
5448 {
5449 /* We can only handle a single .debug_types when we have an
5450 index. */
5451 if (per_bfd->types.size () != 1)
5452 return false;
5453
5454 dwarf2_section_info *section = &per_bfd->types[0];
5455
5456 create_signatured_type_table_from_debug_names
5457 (per_objfile, *map, section, &per_bfd->abbrev);
5458 }
5459
5460 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
5461
5462 per_bfd->debug_names_table = std::move (map);
5463 per_bfd->using_index = 1;
5464 per_bfd->quick_file_names_table =
5465 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
5466
5467 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
5468 objfiles using the same BFD. */
5469 gdb_assert (per_bfd->partial_symtabs == nullptr);
5470 per_bfd->partial_symtabs = objfile->partial_symtabs;
5471
5472 return true;
5473 }
5474
5475 /* Type used to manage iterating over all CUs looking for a symbol for
5476 .debug_names. */
5477
5478 class dw2_debug_names_iterator
5479 {
5480 public:
5481 dw2_debug_names_iterator (const mapped_debug_names &map,
5482 gdb::optional<block_enum> block_index,
5483 domain_enum domain,
5484 const char *name, dwarf2_per_objfile *per_objfile)
5485 : m_map (map), m_block_index (block_index), m_domain (domain),
5486 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5487 m_per_objfile (per_objfile)
5488 {}
5489
5490 dw2_debug_names_iterator (const mapped_debug_names &map,
5491 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
5492 : m_map (map),
5493 m_search (search),
5494 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5495 m_per_objfile (per_objfile)
5496 {}
5497
5498 dw2_debug_names_iterator (const mapped_debug_names &map,
5499 block_enum block_index, domain_enum domain,
5500 uint32_t namei, dwarf2_per_objfile *per_objfile)
5501 : m_map (map), m_block_index (block_index), m_domain (domain),
5502 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5503 m_per_objfile (per_objfile)
5504 {}
5505
5506 /* Return the next matching CU or NULL if there are no more. */
5507 dwarf2_per_cu_data *next ();
5508
5509 private:
5510 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5511 const char *name,
5512 dwarf2_per_objfile *per_objfile);
5513 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5514 uint32_t namei,
5515 dwarf2_per_objfile *per_objfile);
5516
5517 /* The internalized form of .debug_names. */
5518 const mapped_debug_names &m_map;
5519
5520 /* If set, only look for symbols that match that block. Valid values are
5521 GLOBAL_BLOCK and STATIC_BLOCK. */
5522 const gdb::optional<block_enum> m_block_index;
5523
5524 /* The kind of symbol we're looking for. */
5525 const domain_enum m_domain = UNDEF_DOMAIN;
5526 const search_domain m_search = ALL_DOMAIN;
5527
5528 /* The list of CUs from the index entry of the symbol, or NULL if
5529 not found. */
5530 const gdb_byte *m_addr;
5531
5532 dwarf2_per_objfile *m_per_objfile;
5533 };
5534
5535 const char *
5536 mapped_debug_names::namei_to_name
5537 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
5538 {
5539 const ULONGEST namei_string_offs
5540 = extract_unsigned_integer ((name_table_string_offs_reordered
5541 + namei * offset_size),
5542 offset_size,
5543 dwarf5_byte_order);
5544 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
5545 }
5546
5547 /* Find a slot in .debug_names for the object named NAME. If NAME is
5548 found, return pointer to its pool data. If NAME cannot be found,
5549 return NULL. */
5550
5551 const gdb_byte *
5552 dw2_debug_names_iterator::find_vec_in_debug_names
5553 (const mapped_debug_names &map, const char *name,
5554 dwarf2_per_objfile *per_objfile)
5555 {
5556 int (*cmp) (const char *, const char *);
5557
5558 gdb::unique_xmalloc_ptr<char> without_params;
5559 if (current_language->la_language == language_cplus
5560 || current_language->la_language == language_fortran
5561 || current_language->la_language == language_d)
5562 {
5563 /* NAME is already canonical. Drop any qualifiers as
5564 .debug_names does not contain any. */
5565
5566 if (strchr (name, '(') != NULL)
5567 {
5568 without_params = cp_remove_params (name);
5569 if (without_params != NULL)
5570 name = without_params.get ();
5571 }
5572 }
5573
5574 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5575
5576 const uint32_t full_hash = dwarf5_djb_hash (name);
5577 uint32_t namei
5578 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5579 (map.bucket_table_reordered
5580 + (full_hash % map.bucket_count)), 4,
5581 map.dwarf5_byte_order);
5582 if (namei == 0)
5583 return NULL;
5584 --namei;
5585 if (namei >= map.name_count)
5586 {
5587 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5588 "[in module %s]"),
5589 namei, map.name_count,
5590 objfile_name (per_objfile->objfile));
5591 return NULL;
5592 }
5593
5594 for (;;)
5595 {
5596 const uint32_t namei_full_hash
5597 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5598 (map.hash_table_reordered + namei), 4,
5599 map.dwarf5_byte_order);
5600 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5601 return NULL;
5602
5603 if (full_hash == namei_full_hash)
5604 {
5605 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5606
5607 #if 0 /* An expensive sanity check. */
5608 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5609 {
5610 complaint (_("Wrong .debug_names hash for string at index %u "
5611 "[in module %s]"),
5612 namei, objfile_name (dwarf2_per_objfile->objfile));
5613 return NULL;
5614 }
5615 #endif
5616
5617 if (cmp (namei_string, name) == 0)
5618 {
5619 const ULONGEST namei_entry_offs
5620 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5621 + namei * map.offset_size),
5622 map.offset_size, map.dwarf5_byte_order);
5623 return map.entry_pool + namei_entry_offs;
5624 }
5625 }
5626
5627 ++namei;
5628 if (namei >= map.name_count)
5629 return NULL;
5630 }
5631 }
5632
5633 const gdb_byte *
5634 dw2_debug_names_iterator::find_vec_in_debug_names
5635 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5636 {
5637 if (namei >= map.name_count)
5638 {
5639 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5640 "[in module %s]"),
5641 namei, map.name_count,
5642 objfile_name (per_objfile->objfile));
5643 return NULL;
5644 }
5645
5646 const ULONGEST namei_entry_offs
5647 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5648 + namei * map.offset_size),
5649 map.offset_size, map.dwarf5_byte_order);
5650 return map.entry_pool + namei_entry_offs;
5651 }
5652
5653 /* See dw2_debug_names_iterator. */
5654
5655 dwarf2_per_cu_data *
5656 dw2_debug_names_iterator::next ()
5657 {
5658 if (m_addr == NULL)
5659 return NULL;
5660
5661 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5662 struct objfile *objfile = m_per_objfile->objfile;
5663 bfd *const abfd = objfile->obfd;
5664
5665 again:
5666
5667 unsigned int bytes_read;
5668 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5669 m_addr += bytes_read;
5670 if (abbrev == 0)
5671 return NULL;
5672
5673 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5674 if (indexval_it == m_map.abbrev_map.cend ())
5675 {
5676 complaint (_("Wrong .debug_names undefined abbrev code %s "
5677 "[in module %s]"),
5678 pulongest (abbrev), objfile_name (objfile));
5679 return NULL;
5680 }
5681 const mapped_debug_names::index_val &indexval = indexval_it->second;
5682 enum class symbol_linkage {
5683 unknown,
5684 static_,
5685 extern_,
5686 } symbol_linkage_ = symbol_linkage::unknown;
5687 dwarf2_per_cu_data *per_cu = NULL;
5688 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5689 {
5690 ULONGEST ull;
5691 switch (attr.form)
5692 {
5693 case DW_FORM_implicit_const:
5694 ull = attr.implicit_const;
5695 break;
5696 case DW_FORM_flag_present:
5697 ull = 1;
5698 break;
5699 case DW_FORM_udata:
5700 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5701 m_addr += bytes_read;
5702 break;
5703 case DW_FORM_ref4:
5704 ull = read_4_bytes (abfd, m_addr);
5705 m_addr += 4;
5706 break;
5707 case DW_FORM_ref8:
5708 ull = read_8_bytes (abfd, m_addr);
5709 m_addr += 8;
5710 break;
5711 case DW_FORM_ref_sig8:
5712 ull = read_8_bytes (abfd, m_addr);
5713 m_addr += 8;
5714 break;
5715 default:
5716 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5717 dwarf_form_name (attr.form),
5718 objfile_name (objfile));
5719 return NULL;
5720 }
5721 switch (attr.dw_idx)
5722 {
5723 case DW_IDX_compile_unit:
5724 /* Don't crash on bad data. */
5725 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
5726 {
5727 complaint (_(".debug_names entry has bad CU index %s"
5728 " [in module %s]"),
5729 pulongest (ull),
5730 objfile_name (objfile));
5731 continue;
5732 }
5733 per_cu = per_bfd->get_cutu (ull);
5734 break;
5735 case DW_IDX_type_unit:
5736 /* Don't crash on bad data. */
5737 if (ull >= per_bfd->all_type_units.size ())
5738 {
5739 complaint (_(".debug_names entry has bad TU index %s"
5740 " [in module %s]"),
5741 pulongest (ull),
5742 objfile_name (objfile));
5743 continue;
5744 }
5745 per_cu = &per_bfd->get_tu (ull)->per_cu;
5746 break;
5747 case DW_IDX_die_offset:
5748 /* In a per-CU index (as opposed to a per-module index), index
5749 entries without CU attribute implicitly refer to the single CU. */
5750 if (per_cu == NULL)
5751 per_cu = per_bfd->get_cu (0);
5752 break;
5753 case DW_IDX_GNU_internal:
5754 if (!m_map.augmentation_is_gdb)
5755 break;
5756 symbol_linkage_ = symbol_linkage::static_;
5757 break;
5758 case DW_IDX_GNU_external:
5759 if (!m_map.augmentation_is_gdb)
5760 break;
5761 symbol_linkage_ = symbol_linkage::extern_;
5762 break;
5763 }
5764 }
5765
5766 /* Skip if already read in. */
5767 if (m_per_objfile->symtab_set_p (per_cu))
5768 goto again;
5769
5770 /* Check static vs global. */
5771 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5772 {
5773 const bool want_static = *m_block_index == STATIC_BLOCK;
5774 const bool symbol_is_static =
5775 symbol_linkage_ == symbol_linkage::static_;
5776 if (want_static != symbol_is_static)
5777 goto again;
5778 }
5779
5780 /* Match dw2_symtab_iter_next, symbol_kind
5781 and debug_names::psymbol_tag. */
5782 switch (m_domain)
5783 {
5784 case VAR_DOMAIN:
5785 switch (indexval.dwarf_tag)
5786 {
5787 case DW_TAG_variable:
5788 case DW_TAG_subprogram:
5789 /* Some types are also in VAR_DOMAIN. */
5790 case DW_TAG_typedef:
5791 case DW_TAG_structure_type:
5792 break;
5793 default:
5794 goto again;
5795 }
5796 break;
5797 case STRUCT_DOMAIN:
5798 switch (indexval.dwarf_tag)
5799 {
5800 case DW_TAG_typedef:
5801 case DW_TAG_structure_type:
5802 break;
5803 default:
5804 goto again;
5805 }
5806 break;
5807 case LABEL_DOMAIN:
5808 switch (indexval.dwarf_tag)
5809 {
5810 case 0:
5811 case DW_TAG_variable:
5812 break;
5813 default:
5814 goto again;
5815 }
5816 break;
5817 case MODULE_DOMAIN:
5818 switch (indexval.dwarf_tag)
5819 {
5820 case DW_TAG_module:
5821 break;
5822 default:
5823 goto again;
5824 }
5825 break;
5826 default:
5827 break;
5828 }
5829
5830 /* Match dw2_expand_symtabs_matching, symbol_kind and
5831 debug_names::psymbol_tag. */
5832 switch (m_search)
5833 {
5834 case VARIABLES_DOMAIN:
5835 switch (indexval.dwarf_tag)
5836 {
5837 case DW_TAG_variable:
5838 break;
5839 default:
5840 goto again;
5841 }
5842 break;
5843 case FUNCTIONS_DOMAIN:
5844 switch (indexval.dwarf_tag)
5845 {
5846 case DW_TAG_subprogram:
5847 break;
5848 default:
5849 goto again;
5850 }
5851 break;
5852 case TYPES_DOMAIN:
5853 switch (indexval.dwarf_tag)
5854 {
5855 case DW_TAG_typedef:
5856 case DW_TAG_structure_type:
5857 break;
5858 default:
5859 goto again;
5860 }
5861 break;
5862 case MODULES_DOMAIN:
5863 switch (indexval.dwarf_tag)
5864 {
5865 case DW_TAG_module:
5866 break;
5867 default:
5868 goto again;
5869 }
5870 default:
5871 break;
5872 }
5873
5874 return per_cu;
5875 }
5876
5877 static struct compunit_symtab *
5878 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5879 const char *name, domain_enum domain)
5880 {
5881 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5882
5883 const auto &mapp = per_objfile->per_bfd->debug_names_table;
5884 if (!mapp)
5885 {
5886 /* index is NULL if OBJF_READNOW. */
5887 return NULL;
5888 }
5889 const auto &map = *mapp;
5890
5891 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
5892
5893 struct compunit_symtab *stab_best = NULL;
5894 struct dwarf2_per_cu_data *per_cu;
5895 while ((per_cu = iter.next ()) != NULL)
5896 {
5897 struct symbol *sym, *with_opaque = NULL;
5898 compunit_symtab *stab
5899 = dw2_instantiate_symtab (per_cu, per_objfile, false);
5900 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5901 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5902
5903 sym = block_find_symbol (block, name, domain,
5904 block_find_non_opaque_type_preferred,
5905 &with_opaque);
5906
5907 /* Some caution must be observed with overloaded functions and
5908 methods, since the index will not contain any overload
5909 information (but NAME might contain it). */
5910
5911 if (sym != NULL
5912 && strcmp_iw (sym->search_name (), name) == 0)
5913 return stab;
5914 if (with_opaque != NULL
5915 && strcmp_iw (with_opaque->search_name (), name) == 0)
5916 stab_best = stab;
5917
5918 /* Keep looking through other CUs. */
5919 }
5920
5921 return stab_best;
5922 }
5923
5924 /* This dumps minimal information about .debug_names. It is called
5925 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5926 uses this to verify that .debug_names has been loaded. */
5927
5928 static void
5929 dw2_debug_names_dump (struct objfile *objfile)
5930 {
5931 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5932
5933 gdb_assert (per_objfile->per_bfd->using_index);
5934 printf_filtered (".debug_names:");
5935 if (per_objfile->per_bfd->debug_names_table)
5936 printf_filtered (" exists\n");
5937 else
5938 printf_filtered (" faked for \"readnow\"\n");
5939 printf_filtered ("\n");
5940 }
5941
5942 static void
5943 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5944 const char *func_name)
5945 {
5946 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5947
5948 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5949 if (per_objfile->per_bfd->debug_names_table)
5950 {
5951 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5952
5953 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
5954 per_objfile);
5955
5956 struct dwarf2_per_cu_data *per_cu;
5957 while ((per_cu = iter.next ()) != NULL)
5958 dw2_instantiate_symtab (per_cu, per_objfile, false);
5959 }
5960 }
5961
5962 static void
5963 dw2_debug_names_map_matching_symbols
5964 (struct objfile *objfile,
5965 const lookup_name_info &name, domain_enum domain,
5966 int global,
5967 gdb::function_view<symbol_found_callback_ftype> callback,
5968 symbol_compare_ftype *ordered_compare)
5969 {
5970 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5971
5972 /* debug_names_table is NULL if OBJF_READNOW. */
5973 if (!per_objfile->per_bfd->debug_names_table)
5974 return;
5975
5976 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5977 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5978
5979 const char *match_name = name.ada ().lookup_name ().c_str ();
5980 auto matcher = [&] (const char *symname)
5981 {
5982 if (ordered_compare == nullptr)
5983 return true;
5984 return ordered_compare (symname, match_name) == 0;
5985 };
5986
5987 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5988 [&] (offset_type namei)
5989 {
5990 /* The name was matched, now expand corresponding CUs that were
5991 marked. */
5992 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
5993 per_objfile);
5994
5995 struct dwarf2_per_cu_data *per_cu;
5996 while ((per_cu = iter.next ()) != NULL)
5997 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5998 nullptr);
5999 return true;
6000 }, per_objfile);
6001
6002 /* It's a shame we couldn't do this inside the
6003 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
6004 that have already been expanded. Instead, this loop matches what
6005 the psymtab code does. */
6006 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
6007 {
6008 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
6009 if (symtab != nullptr)
6010 {
6011 const struct block *block
6012 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
6013 if (!iterate_over_symbols_terminated (block, name,
6014 domain, callback))
6015 break;
6016 }
6017 }
6018 }
6019
6020 static void
6021 dw2_debug_names_expand_symtabs_matching
6022 (struct objfile *objfile,
6023 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6024 const lookup_name_info *lookup_name,
6025 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6026 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6027 enum search_domain kind)
6028 {
6029 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6030
6031 /* debug_names_table is NULL if OBJF_READNOW. */
6032 if (!per_objfile->per_bfd->debug_names_table)
6033 return;
6034
6035 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
6036
6037 if (symbol_matcher == NULL && lookup_name == NULL)
6038 {
6039 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
6040 {
6041 QUIT;
6042
6043 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
6044 expansion_notify);
6045 }
6046 return;
6047 }
6048
6049 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
6050
6051 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
6052 symbol_matcher,
6053 kind, [&] (offset_type namei)
6054 {
6055 /* The name was matched, now expand corresponding CUs that were
6056 marked. */
6057 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
6058
6059 struct dwarf2_per_cu_data *per_cu;
6060 while ((per_cu = iter.next ()) != NULL)
6061 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
6062 expansion_notify);
6063 return true;
6064 }, per_objfile);
6065 }
6066
6067 const struct quick_symbol_functions dwarf2_debug_names_functions =
6068 {
6069 dw2_has_symbols,
6070 dw2_find_last_source_symtab,
6071 dw2_forget_cached_source_info,
6072 dw2_map_symtabs_matching_filename,
6073 dw2_debug_names_lookup_symbol,
6074 NULL,
6075 dw2_print_stats,
6076 dw2_debug_names_dump,
6077 dw2_debug_names_expand_symtabs_for_function,
6078 dw2_expand_all_symtabs,
6079 dw2_expand_symtabs_with_fullname,
6080 dw2_debug_names_map_matching_symbols,
6081 dw2_debug_names_expand_symtabs_matching,
6082 dw2_find_pc_sect_compunit_symtab,
6083 NULL,
6084 dw2_map_symbol_filenames
6085 };
6086
6087 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6088 to either a dwarf2_per_bfd or dwz_file object. */
6089
6090 template <typename T>
6091 static gdb::array_view<const gdb_byte>
6092 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6093 {
6094 dwarf2_section_info *section = &section_owner->gdb_index;
6095
6096 if (section->empty ())
6097 return {};
6098
6099 /* Older elfutils strip versions could keep the section in the main
6100 executable while splitting it for the separate debug info file. */
6101 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
6102 return {};
6103
6104 section->read (obj);
6105
6106 /* dwarf2_section_info::size is a bfd_size_type, while
6107 gdb::array_view works with size_t. On 32-bit hosts, with
6108 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6109 is 32-bit. So we need an explicit narrowing conversion here.
6110 This is fine, because it's impossible to allocate or mmap an
6111 array/buffer larger than what size_t can represent. */
6112 return gdb::make_array_view (section->buffer, section->size);
6113 }
6114
6115 /* Lookup the index cache for the contents of the index associated to
6116 DWARF2_OBJ. */
6117
6118 static gdb::array_view<const gdb_byte>
6119 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
6120 {
6121 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6122 if (build_id == nullptr)
6123 return {};
6124
6125 return global_index_cache.lookup_gdb_index (build_id,
6126 &dwarf2_per_bfd->index_cache_res);
6127 }
6128
6129 /* Same as the above, but for DWZ. */
6130
6131 static gdb::array_view<const gdb_byte>
6132 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6133 {
6134 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6135 if (build_id == nullptr)
6136 return {};
6137
6138 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6139 }
6140
6141 /* See symfile.h. */
6142
6143 bool
6144 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6145 {
6146 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6147 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6148
6149 /* If we're about to read full symbols, don't bother with the
6150 indices. In this case we also don't care if some other debug
6151 format is making psymtabs, because they are all about to be
6152 expanded anyway. */
6153 if ((objfile->flags & OBJF_READNOW))
6154 {
6155 /* When using READNOW, the using_index flag (set below) indicates that
6156 PER_BFD was already initialized, when we loaded some other objfile. */
6157 if (per_bfd->using_index)
6158 {
6159 *index_kind = dw_index_kind::GDB_INDEX;
6160 per_objfile->resize_symtabs ();
6161 return true;
6162 }
6163
6164 per_bfd->using_index = 1;
6165 create_all_comp_units (per_objfile);
6166 create_all_type_units (per_objfile);
6167 per_bfd->quick_file_names_table
6168 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
6169 per_objfile->resize_symtabs ();
6170
6171 for (int i = 0; i < (per_bfd->all_comp_units.size ()
6172 + per_bfd->all_type_units.size ()); ++i)
6173 {
6174 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
6175
6176 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6177 struct dwarf2_per_cu_quick_data);
6178 }
6179
6180 /* Return 1 so that gdb sees the "quick" functions. However,
6181 these functions will be no-ops because we will have expanded
6182 all symtabs. */
6183 *index_kind = dw_index_kind::GDB_INDEX;
6184 return true;
6185 }
6186
6187 /* Was a debug names index already read when we processed an objfile sharing
6188 PER_BFD? */
6189 if (per_bfd->debug_names_table != nullptr)
6190 {
6191 *index_kind = dw_index_kind::DEBUG_NAMES;
6192 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
6193 per_objfile->resize_symtabs ();
6194 return true;
6195 }
6196
6197 /* Was a GDB index already read when we processed an objfile sharing
6198 PER_BFD? */
6199 if (per_bfd->index_table != nullptr)
6200 {
6201 *index_kind = dw_index_kind::GDB_INDEX;
6202 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
6203 per_objfile->resize_symtabs ();
6204 return true;
6205 }
6206
6207 /* There might already be partial symtabs built for this BFD. This happens
6208 when loading the same binary twice with the index-cache enabled. If so,
6209 don't try to read an index. The objfile / per_objfile initialization will
6210 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
6211 code path. */
6212 if (per_bfd->partial_symtabs != nullptr)
6213 return false;
6214
6215 if (dwarf2_read_debug_names (per_objfile))
6216 {
6217 *index_kind = dw_index_kind::DEBUG_NAMES;
6218 per_objfile->resize_symtabs ();
6219 return true;
6220 }
6221
6222 if (dwarf2_read_gdb_index (per_objfile,
6223 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
6224 get_gdb_index_contents_from_section<dwz_file>))
6225 {
6226 *index_kind = dw_index_kind::GDB_INDEX;
6227 per_objfile->resize_symtabs ();
6228 return true;
6229 }
6230
6231 /* ... otherwise, try to find the index in the index cache. */
6232 if (dwarf2_read_gdb_index (per_objfile,
6233 get_gdb_index_contents_from_cache,
6234 get_gdb_index_contents_from_cache_dwz))
6235 {
6236 global_index_cache.hit ();
6237 *index_kind = dw_index_kind::GDB_INDEX;
6238 per_objfile->resize_symtabs ();
6239 return true;
6240 }
6241
6242 global_index_cache.miss ();
6243 return false;
6244 }
6245
6246 \f
6247
6248 /* Build a partial symbol table. */
6249
6250 void
6251 dwarf2_build_psymtabs (struct objfile *objfile)
6252 {
6253 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6254 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6255
6256 if (per_bfd->partial_symtabs != nullptr)
6257 {
6258 /* Partial symbols were already read, so now we can simply
6259 attach them. */
6260 objfile->partial_symtabs = per_bfd->partial_symtabs;
6261 per_objfile->resize_symtabs ();
6262 return;
6263 }
6264
6265 try
6266 {
6267 /* This isn't really ideal: all the data we allocate on the
6268 objfile's obstack is still uselessly kept around. However,
6269 freeing it seems unsafe. */
6270 psymtab_discarder psymtabs (objfile);
6271 dwarf2_build_psymtabs_hard (per_objfile);
6272 psymtabs.keep ();
6273
6274 per_objfile->resize_symtabs ();
6275
6276 /* (maybe) store an index in the cache. */
6277 global_index_cache.store (per_objfile);
6278 }
6279 catch (const gdb_exception_error &except)
6280 {
6281 exception_print (gdb_stderr, except);
6282 }
6283
6284 /* Finish by setting the local reference to partial symtabs, so that
6285 we don't try to read them again if reading another objfile with the same
6286 BFD. If we can't in fact share, this won't make a difference anyway as
6287 the dwarf2_per_bfd object won't be shared. */
6288 per_bfd->partial_symtabs = objfile->partial_symtabs;
6289 }
6290
6291 /* Find the base address of the compilation unit for range lists and
6292 location lists. It will normally be specified by DW_AT_low_pc.
6293 In DWARF-3 draft 4, the base address could be overridden by
6294 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6295 compilation units with discontinuous ranges. */
6296
6297 static void
6298 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6299 {
6300 struct attribute *attr;
6301
6302 cu->base_address.reset ();
6303
6304 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6305 if (attr != nullptr)
6306 cu->base_address = attr->as_address ();
6307 else
6308 {
6309 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6310 if (attr != nullptr)
6311 cu->base_address = attr->as_address ();
6312 }
6313 }
6314
6315 /* Helper function that returns the proper abbrev section for
6316 THIS_CU. */
6317
6318 static struct dwarf2_section_info *
6319 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6320 {
6321 struct dwarf2_section_info *abbrev;
6322 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6323
6324 if (this_cu->is_dwz)
6325 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
6326 else
6327 abbrev = &per_bfd->abbrev;
6328
6329 return abbrev;
6330 }
6331
6332 /* Fetch the abbreviation table offset from a comp or type unit header. */
6333
6334 static sect_offset
6335 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
6336 struct dwarf2_section_info *section,
6337 sect_offset sect_off)
6338 {
6339 bfd *abfd = section->get_bfd_owner ();
6340 const gdb_byte *info_ptr;
6341 unsigned int initial_length_size, offset_size;
6342 uint16_t version;
6343
6344 section->read (per_objfile->objfile);
6345 info_ptr = section->buffer + to_underlying (sect_off);
6346 read_initial_length (abfd, info_ptr, &initial_length_size);
6347 offset_size = initial_length_size == 4 ? 4 : 8;
6348 info_ptr += initial_length_size;
6349
6350 version = read_2_bytes (abfd, info_ptr);
6351 info_ptr += 2;
6352 if (version >= 5)
6353 {
6354 /* Skip unit type and address size. */
6355 info_ptr += 2;
6356 }
6357
6358 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6359 }
6360
6361 /* A partial symtab that is used only for include files. */
6362 struct dwarf2_include_psymtab : public partial_symtab
6363 {
6364 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6365 : partial_symtab (filename, objfile)
6366 {
6367 }
6368
6369 void read_symtab (struct objfile *objfile) override
6370 {
6371 /* It's an include file, no symbols to read for it.
6372 Everything is in the includer symtab. */
6373
6374 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6375 expansion of the includer psymtab. We use the dependencies[0] field to
6376 model the includer. But if we go the regular route of calling
6377 expand_psymtab here, and having expand_psymtab call expand_dependencies
6378 to expand the includer, we'll only use expand_psymtab on the includer
6379 (making it a non-toplevel psymtab), while if we expand the includer via
6380 another path, we'll use read_symtab (making it a toplevel psymtab).
6381 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6382 psymtab, and trigger read_symtab on the includer here directly. */
6383 includer ()->read_symtab (objfile);
6384 }
6385
6386 void expand_psymtab (struct objfile *objfile) override
6387 {
6388 /* This is not called by read_symtab, and should not be called by any
6389 expand_dependencies. */
6390 gdb_assert (false);
6391 }
6392
6393 bool readin_p (struct objfile *objfile) const override
6394 {
6395 return includer ()->readin_p (objfile);
6396 }
6397
6398 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6399 {
6400 return nullptr;
6401 }
6402
6403 private:
6404 partial_symtab *includer () const
6405 {
6406 /* An include psymtab has exactly one dependency: the psymtab that
6407 includes it. */
6408 gdb_assert (this->number_of_dependencies == 1);
6409 return this->dependencies[0];
6410 }
6411 };
6412
6413 /* Allocate a new partial symtab for file named NAME and mark this new
6414 partial symtab as being an include of PST. */
6415
6416 static void
6417 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6418 struct objfile *objfile)
6419 {
6420 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6421
6422 if (!IS_ABSOLUTE_PATH (subpst->filename))
6423 subpst->dirname = pst->dirname;
6424
6425 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6426 subpst->dependencies[0] = pst;
6427 subpst->number_of_dependencies = 1;
6428 }
6429
6430 /* Read the Line Number Program data and extract the list of files
6431 included by the source file represented by PST. Build an include
6432 partial symtab for each of these included files. */
6433
6434 static void
6435 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6436 struct die_info *die,
6437 dwarf2_psymtab *pst)
6438 {
6439 line_header_up lh;
6440 struct attribute *attr;
6441
6442 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6443 if (attr != nullptr && attr->form_is_unsigned ())
6444 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
6445 if (lh == NULL)
6446 return; /* No linetable, so no includes. */
6447
6448 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6449 that we pass in the raw text_low here; that is ok because we're
6450 only decoding the line table to make include partial symtabs, and
6451 so the addresses aren't really used. */
6452 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6453 pst->raw_text_low (), 1);
6454 }
6455
6456 static hashval_t
6457 hash_signatured_type (const void *item)
6458 {
6459 const struct signatured_type *sig_type
6460 = (const struct signatured_type *) item;
6461
6462 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6463 return sig_type->signature;
6464 }
6465
6466 static int
6467 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6468 {
6469 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6470 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6471
6472 return lhs->signature == rhs->signature;
6473 }
6474
6475 /* Allocate a hash table for signatured types. */
6476
6477 static htab_up
6478 allocate_signatured_type_table ()
6479 {
6480 return htab_up (htab_create_alloc (41,
6481 hash_signatured_type,
6482 eq_signatured_type,
6483 NULL, xcalloc, xfree));
6484 }
6485
6486 /* A helper function to add a signatured type CU to a table. */
6487
6488 static int
6489 add_signatured_type_cu_to_table (void **slot, void *datum)
6490 {
6491 struct signatured_type *sigt = (struct signatured_type *) *slot;
6492 std::vector<signatured_type *> *all_type_units
6493 = (std::vector<signatured_type *> *) datum;
6494
6495 all_type_units->push_back (sigt);
6496
6497 return 1;
6498 }
6499
6500 /* A helper for create_debug_types_hash_table. Read types from SECTION
6501 and fill them into TYPES_HTAB. It will process only type units,
6502 therefore DW_UT_type. */
6503
6504 static void
6505 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
6506 struct dwo_file *dwo_file,
6507 dwarf2_section_info *section, htab_up &types_htab,
6508 rcuh_kind section_kind)
6509 {
6510 struct objfile *objfile = per_objfile->objfile;
6511 struct dwarf2_section_info *abbrev_section;
6512 bfd *abfd;
6513 const gdb_byte *info_ptr, *end_ptr;
6514
6515 abbrev_section = (dwo_file != NULL
6516 ? &dwo_file->sections.abbrev
6517 : &per_objfile->per_bfd->abbrev);
6518
6519 dwarf_read_debug_printf ("Reading %s for %s:",
6520 section->get_name (),
6521 abbrev_section->get_file_name ());
6522
6523 section->read (objfile);
6524 info_ptr = section->buffer;
6525
6526 if (info_ptr == NULL)
6527 return;
6528
6529 /* We can't set abfd until now because the section may be empty or
6530 not present, in which case the bfd is unknown. */
6531 abfd = section->get_bfd_owner ();
6532
6533 /* We don't use cutu_reader here because we don't need to read
6534 any dies: the signature is in the header. */
6535
6536 end_ptr = info_ptr + section->size;
6537 while (info_ptr < end_ptr)
6538 {
6539 struct signatured_type *sig_type;
6540 struct dwo_unit *dwo_tu;
6541 void **slot;
6542 const gdb_byte *ptr = info_ptr;
6543 struct comp_unit_head header;
6544 unsigned int length;
6545
6546 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6547
6548 /* Initialize it due to a false compiler warning. */
6549 header.signature = -1;
6550 header.type_cu_offset_in_tu = (cu_offset) -1;
6551
6552 /* We need to read the type's signature in order to build the hash
6553 table, but we don't need anything else just yet. */
6554
6555 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
6556 abbrev_section, ptr, section_kind);
6557
6558 length = header.get_length ();
6559
6560 /* Skip dummy type units. */
6561 if (ptr >= info_ptr + length
6562 || peek_abbrev_code (abfd, ptr) == 0
6563 || (header.unit_type != DW_UT_type
6564 && header.unit_type != DW_UT_split_type))
6565 {
6566 info_ptr += length;
6567 continue;
6568 }
6569
6570 if (types_htab == NULL)
6571 {
6572 if (dwo_file)
6573 types_htab = allocate_dwo_unit_table ();
6574 else
6575 types_htab = allocate_signatured_type_table ();
6576 }
6577
6578 if (dwo_file)
6579 {
6580 sig_type = NULL;
6581 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
6582 dwo_tu->dwo_file = dwo_file;
6583 dwo_tu->signature = header.signature;
6584 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6585 dwo_tu->section = section;
6586 dwo_tu->sect_off = sect_off;
6587 dwo_tu->length = length;
6588 }
6589 else
6590 {
6591 /* N.B.: type_offset is not usable if this type uses a DWO file.
6592 The real type_offset is in the DWO file. */
6593 dwo_tu = NULL;
6594 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6595 sig_type->signature = header.signature;
6596 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6597 sig_type->per_cu.is_debug_types = 1;
6598 sig_type->per_cu.section = section;
6599 sig_type->per_cu.sect_off = sect_off;
6600 sig_type->per_cu.length = length;
6601 }
6602
6603 slot = htab_find_slot (types_htab.get (),
6604 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6605 INSERT);
6606 gdb_assert (slot != NULL);
6607 if (*slot != NULL)
6608 {
6609 sect_offset dup_sect_off;
6610
6611 if (dwo_file)
6612 {
6613 const struct dwo_unit *dup_tu
6614 = (const struct dwo_unit *) *slot;
6615
6616 dup_sect_off = dup_tu->sect_off;
6617 }
6618 else
6619 {
6620 const struct signatured_type *dup_tu
6621 = (const struct signatured_type *) *slot;
6622
6623 dup_sect_off = dup_tu->per_cu.sect_off;
6624 }
6625
6626 complaint (_("debug type entry at offset %s is duplicate to"
6627 " the entry at offset %s, signature %s"),
6628 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6629 hex_string (header.signature));
6630 }
6631 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6632
6633 dwarf_read_debug_printf_v (" offset %s, signature %s",
6634 sect_offset_str (sect_off),
6635 hex_string (header.signature));
6636
6637 info_ptr += length;
6638 }
6639 }
6640
6641 /* Create the hash table of all entries in the .debug_types
6642 (or .debug_types.dwo) section(s).
6643 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6644 otherwise it is NULL.
6645
6646 The result is a pointer to the hash table or NULL if there are no types.
6647
6648 Note: This function processes DWO files only, not DWP files. */
6649
6650 static void
6651 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
6652 struct dwo_file *dwo_file,
6653 gdb::array_view<dwarf2_section_info> type_sections,
6654 htab_up &types_htab)
6655 {
6656 for (dwarf2_section_info &section : type_sections)
6657 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6658 rcuh_kind::TYPE);
6659 }
6660
6661 /* Create the hash table of all entries in the .debug_types section,
6662 and initialize all_type_units.
6663 The result is zero if there is an error (e.g. missing .debug_types section),
6664 otherwise non-zero. */
6665
6666 static int
6667 create_all_type_units (dwarf2_per_objfile *per_objfile)
6668 {
6669 htab_up types_htab;
6670
6671 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6672 types_htab, rcuh_kind::COMPILE);
6673 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6674 types_htab);
6675 if (types_htab == NULL)
6676 {
6677 per_objfile->per_bfd->signatured_types = NULL;
6678 return 0;
6679 }
6680
6681 per_objfile->per_bfd->signatured_types = std::move (types_htab);
6682
6683 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6684 per_objfile->per_bfd->all_type_units.reserve
6685 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
6686
6687 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
6688 add_signatured_type_cu_to_table,
6689 &per_objfile->per_bfd->all_type_units);
6690
6691 return 1;
6692 }
6693
6694 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6695 If SLOT is non-NULL, it is the entry to use in the hash table.
6696 Otherwise we find one. */
6697
6698 static struct signatured_type *
6699 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6700 {
6701 if (per_objfile->per_bfd->all_type_units.size ()
6702 == per_objfile->per_bfd->all_type_units.capacity ())
6703 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6704
6705 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6706
6707 per_objfile->resize_symtabs ();
6708
6709 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6710 sig_type->signature = sig;
6711 sig_type->per_cu.is_debug_types = 1;
6712 if (per_objfile->per_bfd->using_index)
6713 {
6714 sig_type->per_cu.v.quick =
6715 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6716 struct dwarf2_per_cu_quick_data);
6717 }
6718
6719 if (slot == NULL)
6720 {
6721 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6722 sig_type, INSERT);
6723 }
6724 gdb_assert (*slot == NULL);
6725 *slot = sig_type;
6726 /* The rest of sig_type must be filled in by the caller. */
6727 return sig_type;
6728 }
6729
6730 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6731 Fill in SIG_ENTRY with DWO_ENTRY. */
6732
6733 static void
6734 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
6735 struct signatured_type *sig_entry,
6736 struct dwo_unit *dwo_entry)
6737 {
6738 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6739
6740 /* Make sure we're not clobbering something we don't expect to. */
6741 gdb_assert (! sig_entry->per_cu.queued);
6742 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
6743 if (per_bfd->using_index)
6744 {
6745 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6746 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6747 }
6748 else
6749 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6750 gdb_assert (sig_entry->signature == dwo_entry->signature);
6751 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6752 gdb_assert (sig_entry->type_unit_group == NULL);
6753 gdb_assert (sig_entry->dwo_unit == NULL);
6754
6755 sig_entry->per_cu.section = dwo_entry->section;
6756 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6757 sig_entry->per_cu.length = dwo_entry->length;
6758 sig_entry->per_cu.reading_dwo_directly = 1;
6759 sig_entry->per_cu.per_bfd = per_bfd;
6760 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6761 sig_entry->dwo_unit = dwo_entry;
6762 }
6763
6764 /* Subroutine of lookup_signatured_type.
6765 If we haven't read the TU yet, create the signatured_type data structure
6766 for a TU to be read in directly from a DWO file, bypassing the stub.
6767 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6768 using .gdb_index, then when reading a CU we want to stay in the DWO file
6769 containing that CU. Otherwise we could end up reading several other DWO
6770 files (due to comdat folding) to process the transitive closure of all the
6771 mentioned TUs, and that can be slow. The current DWO file will have every
6772 type signature that it needs.
6773 We only do this for .gdb_index because in the psymtab case we already have
6774 to read all the DWOs to build the type unit groups. */
6775
6776 static struct signatured_type *
6777 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6778 {
6779 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6780 struct dwo_file *dwo_file;
6781 struct dwo_unit find_dwo_entry, *dwo_entry;
6782 struct signatured_type find_sig_entry, *sig_entry;
6783 void **slot;
6784
6785 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6786
6787 /* If TU skeletons have been removed then we may not have read in any
6788 TUs yet. */
6789 if (per_objfile->per_bfd->signatured_types == NULL)
6790 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6791
6792 /* We only ever need to read in one copy of a signatured type.
6793 Use the global signatured_types array to do our own comdat-folding
6794 of types. If this is the first time we're reading this TU, and
6795 the TU has an entry in .gdb_index, replace the recorded data from
6796 .gdb_index with this TU. */
6797
6798 find_sig_entry.signature = sig;
6799 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6800 &find_sig_entry, INSERT);
6801 sig_entry = (struct signatured_type *) *slot;
6802
6803 /* We can get here with the TU already read, *or* in the process of being
6804 read. Don't reassign the global entry to point to this DWO if that's
6805 the case. Also note that if the TU is already being read, it may not
6806 have come from a DWO, the program may be a mix of Fission-compiled
6807 code and non-Fission-compiled code. */
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 && sig_entry->per_cu.tu_read)
6813 return sig_entry;
6814
6815 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6816 dwo_unit of the TU itself. */
6817 dwo_file = cu->dwo_unit->dwo_file;
6818
6819 /* Ok, this is the first time we're reading this TU. */
6820 if (dwo_file->tus == NULL)
6821 return NULL;
6822 find_dwo_entry.signature = sig;
6823 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6824 &find_dwo_entry);
6825 if (dwo_entry == NULL)
6826 return NULL;
6827
6828 /* If the global table doesn't have an entry for this TU, add one. */
6829 if (sig_entry == NULL)
6830 sig_entry = add_type_unit (per_objfile, sig, slot);
6831
6832 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6833 sig_entry->per_cu.tu_read = 1;
6834 return sig_entry;
6835 }
6836
6837 /* Subroutine of lookup_signatured_type.
6838 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6839 then try the DWP file. If the TU stub (skeleton) has been removed then
6840 it won't be in .gdb_index. */
6841
6842 static struct signatured_type *
6843 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6844 {
6845 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6846 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6847 struct dwo_unit *dwo_entry;
6848 struct signatured_type find_sig_entry, *sig_entry;
6849 void **slot;
6850
6851 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6852 gdb_assert (dwp_file != NULL);
6853
6854 /* If TU skeletons have been removed then we may not have read in any
6855 TUs yet. */
6856 if (per_objfile->per_bfd->signatured_types == NULL)
6857 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6858
6859 find_sig_entry.signature = sig;
6860 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6861 &find_sig_entry, INSERT);
6862 sig_entry = (struct signatured_type *) *slot;
6863
6864 /* Have we already tried to read this TU?
6865 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6866 needn't exist in the global table yet). */
6867 if (sig_entry != NULL)
6868 return sig_entry;
6869
6870 if (dwp_file->tus == NULL)
6871 return NULL;
6872 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6873 1 /* is_debug_types */);
6874 if (dwo_entry == NULL)
6875 return NULL;
6876
6877 sig_entry = add_type_unit (per_objfile, sig, slot);
6878 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6879
6880 return sig_entry;
6881 }
6882
6883 /* Lookup a signature based type for DW_FORM_ref_sig8.
6884 Returns NULL if signature SIG is not present in the table.
6885 It is up to the caller to complain about this. */
6886
6887 static struct signatured_type *
6888 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6889 {
6890 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6891
6892 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6893 {
6894 /* We're in a DWO/DWP file, and we're using .gdb_index.
6895 These cases require special processing. */
6896 if (get_dwp_file (per_objfile) == NULL)
6897 return lookup_dwo_signatured_type (cu, sig);
6898 else
6899 return lookup_dwp_signatured_type (cu, sig);
6900 }
6901 else
6902 {
6903 struct signatured_type find_entry, *entry;
6904
6905 if (per_objfile->per_bfd->signatured_types == NULL)
6906 return NULL;
6907 find_entry.signature = sig;
6908 entry = ((struct signatured_type *)
6909 htab_find (per_objfile->per_bfd->signatured_types.get (),
6910 &find_entry));
6911 return entry;
6912 }
6913 }
6914
6915 /* Low level DIE reading support. */
6916
6917 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6918
6919 static void
6920 init_cu_die_reader (struct die_reader_specs *reader,
6921 struct dwarf2_cu *cu,
6922 struct dwarf2_section_info *section,
6923 struct dwo_file *dwo_file,
6924 struct abbrev_table *abbrev_table)
6925 {
6926 gdb_assert (section->readin && section->buffer != NULL);
6927 reader->abfd = section->get_bfd_owner ();
6928 reader->cu = cu;
6929 reader->dwo_file = dwo_file;
6930 reader->die_section = section;
6931 reader->buffer = section->buffer;
6932 reader->buffer_end = section->buffer + section->size;
6933 reader->abbrev_table = abbrev_table;
6934 }
6935
6936 /* Subroutine of cutu_reader to simplify it.
6937 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6938 There's just a lot of work to do, and cutu_reader is big enough
6939 already.
6940
6941 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6942 from it to the DIE in the DWO. If NULL we are skipping the stub.
6943 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6944 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6945 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6946 STUB_COMP_DIR may be non-NULL.
6947 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6948 are filled in with the info of the DIE from the DWO file.
6949 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6950 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6951 kept around for at least as long as *RESULT_READER.
6952
6953 The result is non-zero if a valid (non-dummy) DIE was found. */
6954
6955 static int
6956 read_cutu_die_from_dwo (dwarf2_cu *cu,
6957 struct dwo_unit *dwo_unit,
6958 struct die_info *stub_comp_unit_die,
6959 const char *stub_comp_dir,
6960 struct die_reader_specs *result_reader,
6961 const gdb_byte **result_info_ptr,
6962 struct die_info **result_comp_unit_die,
6963 abbrev_table_up *result_dwo_abbrev_table)
6964 {
6965 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6966 dwarf2_per_cu_data *per_cu = cu->per_cu;
6967 struct objfile *objfile = per_objfile->objfile;
6968 bfd *abfd;
6969 const gdb_byte *begin_info_ptr, *info_ptr;
6970 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6971 int i,num_extra_attrs;
6972 struct dwarf2_section_info *dwo_abbrev_section;
6973 struct die_info *comp_unit_die;
6974
6975 /* At most one of these may be provided. */
6976 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6977
6978 /* These attributes aren't processed until later:
6979 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6980 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6981 referenced later. However, these attributes are found in the stub
6982 which we won't have later. In order to not impose this complication
6983 on the rest of the code, we read them here and copy them to the
6984 DWO CU/TU die. */
6985
6986 stmt_list = NULL;
6987 low_pc = NULL;
6988 high_pc = NULL;
6989 ranges = NULL;
6990 comp_dir = NULL;
6991
6992 if (stub_comp_unit_die != NULL)
6993 {
6994 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6995 DWO file. */
6996 if (!per_cu->is_debug_types)
6997 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6998 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6999 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7000 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7001 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7002
7003 cu->addr_base = stub_comp_unit_die->addr_base ();
7004
7005 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
7006 We need the value before we can process DW_AT_ranges values from the
7007 DWO. */
7008 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
7009
7010 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
7011 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
7012 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
7013 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
7014 section. */
7015 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
7016 }
7017 else if (stub_comp_dir != NULL)
7018 {
7019 /* Reconstruct the comp_dir attribute to simplify the code below. */
7020 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
7021 comp_dir->name = DW_AT_comp_dir;
7022 comp_dir->form = DW_FORM_string;
7023 comp_dir->set_string_noncanonical (stub_comp_dir);
7024 }
7025
7026 /* Set up for reading the DWO CU/TU. */
7027 cu->dwo_unit = dwo_unit;
7028 dwarf2_section_info *section = dwo_unit->section;
7029 section->read (objfile);
7030 abfd = section->get_bfd_owner ();
7031 begin_info_ptr = info_ptr = (section->buffer
7032 + to_underlying (dwo_unit->sect_off));
7033 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7034
7035 if (per_cu->is_debug_types)
7036 {
7037 signatured_type *sig_type = (struct signatured_type *) per_cu;
7038
7039 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7040 section, dwo_abbrev_section,
7041 info_ptr, rcuh_kind::TYPE);
7042 /* This is not an assert because it can be caused by bad debug info. */
7043 if (sig_type->signature != cu->header.signature)
7044 {
7045 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7046 " TU at offset %s [in module %s]"),
7047 hex_string (sig_type->signature),
7048 hex_string (cu->header.signature),
7049 sect_offset_str (dwo_unit->sect_off),
7050 bfd_get_filename (abfd));
7051 }
7052 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7053 /* For DWOs coming from DWP files, we don't know the CU length
7054 nor the type's offset in the TU until now. */
7055 dwo_unit->length = cu->header.get_length ();
7056 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7057
7058 /* Establish the type offset that can be used to lookup the type.
7059 For DWO files, we don't know it until now. */
7060 sig_type->type_offset_in_section
7061 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7062 }
7063 else
7064 {
7065 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7066 section, dwo_abbrev_section,
7067 info_ptr, rcuh_kind::COMPILE);
7068 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7069 /* For DWOs coming from DWP files, we don't know the CU length
7070 until now. */
7071 dwo_unit->length = cu->header.get_length ();
7072 }
7073
7074 dwo_abbrev_section->read (objfile);
7075 *result_dwo_abbrev_table
7076 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
7077 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7078 result_dwo_abbrev_table->get ());
7079
7080 /* Read in the die, but leave space to copy over the attributes
7081 from the stub. This has the benefit of simplifying the rest of
7082 the code - all the work to maintain the illusion of a single
7083 DW_TAG_{compile,type}_unit DIE is done here. */
7084 num_extra_attrs = ((stmt_list != NULL)
7085 + (low_pc != NULL)
7086 + (high_pc != NULL)
7087 + (ranges != NULL)
7088 + (comp_dir != NULL));
7089 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7090 num_extra_attrs);
7091
7092 /* Copy over the attributes from the stub to the DIE we just read in. */
7093 comp_unit_die = *result_comp_unit_die;
7094 i = comp_unit_die->num_attrs;
7095 if (stmt_list != NULL)
7096 comp_unit_die->attrs[i++] = *stmt_list;
7097 if (low_pc != NULL)
7098 comp_unit_die->attrs[i++] = *low_pc;
7099 if (high_pc != NULL)
7100 comp_unit_die->attrs[i++] = *high_pc;
7101 if (ranges != NULL)
7102 comp_unit_die->attrs[i++] = *ranges;
7103 if (comp_dir != NULL)
7104 comp_unit_die->attrs[i++] = *comp_dir;
7105 comp_unit_die->num_attrs += num_extra_attrs;
7106
7107 if (dwarf_die_debug)
7108 {
7109 fprintf_unfiltered (gdb_stdlog,
7110 "Read die from %s@0x%x of %s:\n",
7111 section->get_name (),
7112 (unsigned) (begin_info_ptr - section->buffer),
7113 bfd_get_filename (abfd));
7114 dump_die (comp_unit_die, dwarf_die_debug);
7115 }
7116
7117 /* Skip dummy compilation units. */
7118 if (info_ptr >= begin_info_ptr + dwo_unit->length
7119 || peek_abbrev_code (abfd, info_ptr) == 0)
7120 return 0;
7121
7122 *result_info_ptr = info_ptr;
7123 return 1;
7124 }
7125
7126 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7127 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7128 signature is part of the header. */
7129 static gdb::optional<ULONGEST>
7130 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7131 {
7132 if (cu->header.version >= 5)
7133 return cu->header.signature;
7134 struct attribute *attr;
7135 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7136 if (attr == nullptr || !attr->form_is_unsigned ())
7137 return gdb::optional<ULONGEST> ();
7138 return attr->as_unsigned ();
7139 }
7140
7141 /* Subroutine of cutu_reader to simplify it.
7142 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7143 Returns NULL if the specified DWO unit cannot be found. */
7144
7145 static struct dwo_unit *
7146 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
7147 {
7148 dwarf2_per_cu_data *per_cu = cu->per_cu;
7149 struct dwo_unit *dwo_unit;
7150 const char *comp_dir;
7151
7152 gdb_assert (cu != NULL);
7153
7154 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7155 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7156 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7157
7158 if (per_cu->is_debug_types)
7159 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
7160 else
7161 {
7162 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7163
7164 if (!signature.has_value ())
7165 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7166 " [in module %s]"),
7167 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
7168
7169 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
7170 }
7171
7172 return dwo_unit;
7173 }
7174
7175 /* Subroutine of cutu_reader to simplify it.
7176 See it for a description of the parameters.
7177 Read a TU directly from a DWO file, bypassing the stub. */
7178
7179 void
7180 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
7181 dwarf2_per_objfile *per_objfile,
7182 dwarf2_cu *existing_cu)
7183 {
7184 struct signatured_type *sig_type;
7185
7186 /* Verify we can do the following downcast, and that we have the
7187 data we need. */
7188 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7189 sig_type = (struct signatured_type *) this_cu;
7190 gdb_assert (sig_type->dwo_unit != NULL);
7191
7192 dwarf2_cu *cu;
7193
7194 if (existing_cu != nullptr)
7195 {
7196 cu = existing_cu;
7197 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
7198 /* There's no need to do the rereading_dwo_cu handling that
7199 cutu_reader does since we don't read the stub. */
7200 }
7201 else
7202 {
7203 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7204 in per_objfile yet. */
7205 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7206 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7207 cu = m_new_cu.get ();
7208 }
7209
7210 /* A future optimization, if needed, would be to use an existing
7211 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7212 could share abbrev tables. */
7213
7214 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
7215 NULL /* stub_comp_unit_die */,
7216 sig_type->dwo_unit->dwo_file->comp_dir,
7217 this, &info_ptr,
7218 &comp_unit_die,
7219 &m_dwo_abbrev_table) == 0)
7220 {
7221 /* Dummy die. */
7222 dummy_p = true;
7223 }
7224 }
7225
7226 /* Initialize a CU (or TU) and read its DIEs.
7227 If the CU defers to a DWO file, read the DWO file as well.
7228
7229 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7230 Otherwise the table specified in the comp unit header is read in and used.
7231 This is an optimization for when we already have the abbrev table.
7232
7233 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7234 allocated. */
7235
7236 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7237 dwarf2_per_objfile *per_objfile,
7238 struct abbrev_table *abbrev_table,
7239 dwarf2_cu *existing_cu,
7240 bool skip_partial)
7241 : die_reader_specs {},
7242 m_this_cu (this_cu)
7243 {
7244 struct objfile *objfile = per_objfile->objfile;
7245 struct dwarf2_section_info *section = this_cu->section;
7246 bfd *abfd = section->get_bfd_owner ();
7247 const gdb_byte *begin_info_ptr;
7248 struct signatured_type *sig_type = NULL;
7249 struct dwarf2_section_info *abbrev_section;
7250 /* Non-zero if CU currently points to a DWO file and we need to
7251 reread it. When this happens we need to reread the skeleton die
7252 before we can reread the DWO file (this only applies to CUs, not TUs). */
7253 int rereading_dwo_cu = 0;
7254
7255 if (dwarf_die_debug)
7256 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7257 this_cu->is_debug_types ? "type" : "comp",
7258 sect_offset_str (this_cu->sect_off));
7259
7260 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7261 file (instead of going through the stub), short-circuit all of this. */
7262 if (this_cu->reading_dwo_directly)
7263 {
7264 /* Narrow down the scope of possibilities to have to understand. */
7265 gdb_assert (this_cu->is_debug_types);
7266 gdb_assert (abbrev_table == NULL);
7267 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
7268 return;
7269 }
7270
7271 /* This is cheap if the section is already read in. */
7272 section->read (objfile);
7273
7274 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7275
7276 abbrev_section = get_abbrev_section_for_cu (this_cu);
7277
7278 dwarf2_cu *cu;
7279
7280 if (existing_cu != nullptr)
7281 {
7282 cu = existing_cu;
7283 /* If this CU is from a DWO file we need to start over, we need to
7284 refetch the attributes from the skeleton CU.
7285 This could be optimized by retrieving those attributes from when we
7286 were here the first time: the previous comp_unit_die was stored in
7287 comp_unit_obstack. But there's no data yet that we need this
7288 optimization. */
7289 if (cu->dwo_unit != NULL)
7290 rereading_dwo_cu = 1;
7291 }
7292 else
7293 {
7294 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7295 in per_objfile yet. */
7296 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7297 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7298 cu = m_new_cu.get ();
7299 }
7300
7301 /* Get the header. */
7302 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7303 {
7304 /* We already have the header, there's no need to read it in again. */
7305 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7306 }
7307 else
7308 {
7309 if (this_cu->is_debug_types)
7310 {
7311 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7312 section, abbrev_section,
7313 info_ptr, rcuh_kind::TYPE);
7314
7315 /* Since per_cu is the first member of struct signatured_type,
7316 we can go from a pointer to one to a pointer to the other. */
7317 sig_type = (struct signatured_type *) this_cu;
7318 gdb_assert (sig_type->signature == cu->header.signature);
7319 gdb_assert (sig_type->type_offset_in_tu
7320 == cu->header.type_cu_offset_in_tu);
7321 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7322
7323 /* LENGTH has not been set yet for type units if we're
7324 using .gdb_index. */
7325 this_cu->length = cu->header.get_length ();
7326
7327 /* Establish the type offset that can be used to lookup the type. */
7328 sig_type->type_offset_in_section =
7329 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7330
7331 this_cu->dwarf_version = cu->header.version;
7332 }
7333 else
7334 {
7335 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7336 section, abbrev_section,
7337 info_ptr,
7338 rcuh_kind::COMPILE);
7339
7340 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7341 if (this_cu->length == 0)
7342 this_cu->length = cu->header.get_length ();
7343 else
7344 gdb_assert (this_cu->length == cu->header.get_length ());
7345 this_cu->dwarf_version = cu->header.version;
7346 }
7347 }
7348
7349 /* Skip dummy compilation units. */
7350 if (info_ptr >= begin_info_ptr + this_cu->length
7351 || peek_abbrev_code (abfd, info_ptr) == 0)
7352 {
7353 dummy_p = true;
7354 return;
7355 }
7356
7357 /* If we don't have them yet, read the abbrevs for this compilation unit.
7358 And if we need to read them now, make sure they're freed when we're
7359 done. */
7360 if (abbrev_table != NULL)
7361 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7362 else
7363 {
7364 abbrev_section->read (objfile);
7365 m_abbrev_table_holder
7366 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
7367 abbrev_table = m_abbrev_table_holder.get ();
7368 }
7369
7370 /* Read the top level CU/TU die. */
7371 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7372 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7373
7374 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7375 {
7376 dummy_p = true;
7377 return;
7378 }
7379
7380 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7381 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7382 table from the DWO file and pass the ownership over to us. It will be
7383 referenced from READER, so we must make sure to free it after we're done
7384 with READER.
7385
7386 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7387 DWO CU, that this test will fail (the attribute will not be present). */
7388 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7389 if (dwo_name != nullptr)
7390 {
7391 struct dwo_unit *dwo_unit;
7392 struct die_info *dwo_comp_unit_die;
7393
7394 if (comp_unit_die->has_children)
7395 {
7396 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7397 " has children (offset %s) [in module %s]"),
7398 sect_offset_str (this_cu->sect_off),
7399 bfd_get_filename (abfd));
7400 }
7401 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
7402 if (dwo_unit != NULL)
7403 {
7404 if (read_cutu_die_from_dwo (cu, dwo_unit,
7405 comp_unit_die, NULL,
7406 this, &info_ptr,
7407 &dwo_comp_unit_die,
7408 &m_dwo_abbrev_table) == 0)
7409 {
7410 /* Dummy die. */
7411 dummy_p = true;
7412 return;
7413 }
7414 comp_unit_die = dwo_comp_unit_die;
7415 }
7416 else
7417 {
7418 /* Yikes, we couldn't find the rest of the DIE, we only have
7419 the stub. A complaint has already been logged. There's
7420 not much more we can do except pass on the stub DIE to
7421 die_reader_func. We don't want to throw an error on bad
7422 debug info. */
7423 }
7424 }
7425 }
7426
7427 void
7428 cutu_reader::keep ()
7429 {
7430 /* Done, clean up. */
7431 gdb_assert (!dummy_p);
7432 if (m_new_cu != NULL)
7433 {
7434 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7435 now. */
7436 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7437 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
7438 }
7439 }
7440
7441 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7442 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7443 assumed to have already done the lookup to find the DWO file).
7444
7445 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7446 THIS_CU->is_debug_types, but nothing else.
7447
7448 We fill in THIS_CU->length.
7449
7450 THIS_CU->cu is always freed when done.
7451 This is done in order to not leave THIS_CU->cu in a state where we have
7452 to care whether it refers to the "main" CU or the DWO CU.
7453
7454 When parent_cu is passed, it is used to provide a default value for
7455 str_offsets_base and addr_base from the parent. */
7456
7457 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7458 dwarf2_per_objfile *per_objfile,
7459 struct dwarf2_cu *parent_cu,
7460 struct dwo_file *dwo_file)
7461 : die_reader_specs {},
7462 m_this_cu (this_cu)
7463 {
7464 struct objfile *objfile = per_objfile->objfile;
7465 struct dwarf2_section_info *section = this_cu->section;
7466 bfd *abfd = section->get_bfd_owner ();
7467 struct dwarf2_section_info *abbrev_section;
7468 const gdb_byte *begin_info_ptr, *info_ptr;
7469
7470 if (dwarf_die_debug)
7471 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7472 this_cu->is_debug_types ? "type" : "comp",
7473 sect_offset_str (this_cu->sect_off));
7474
7475 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7476
7477 abbrev_section = (dwo_file != NULL
7478 ? &dwo_file->sections.abbrev
7479 : get_abbrev_section_for_cu (this_cu));
7480
7481 /* This is cheap if the section is already read in. */
7482 section->read (objfile);
7483
7484 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7485
7486 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7487 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7488 section, abbrev_section, info_ptr,
7489 (this_cu->is_debug_types
7490 ? rcuh_kind::TYPE
7491 : rcuh_kind::COMPILE));
7492
7493 if (parent_cu != nullptr)
7494 {
7495 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7496 m_new_cu->addr_base = parent_cu->addr_base;
7497 }
7498 this_cu->length = m_new_cu->header.get_length ();
7499
7500 /* Skip dummy compilation units. */
7501 if (info_ptr >= begin_info_ptr + this_cu->length
7502 || peek_abbrev_code (abfd, info_ptr) == 0)
7503 {
7504 dummy_p = true;
7505 return;
7506 }
7507
7508 abbrev_section->read (objfile);
7509 m_abbrev_table_holder
7510 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
7511
7512 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7513 m_abbrev_table_holder.get ());
7514 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7515 }
7516
7517 \f
7518 /* Type Unit Groups.
7519
7520 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7521 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7522 so that all types coming from the same compilation (.o file) are grouped
7523 together. A future step could be to put the types in the same symtab as
7524 the CU the types ultimately came from. */
7525
7526 static hashval_t
7527 hash_type_unit_group (const void *item)
7528 {
7529 const struct type_unit_group *tu_group
7530 = (const struct type_unit_group *) item;
7531
7532 return hash_stmt_list_entry (&tu_group->hash);
7533 }
7534
7535 static int
7536 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7537 {
7538 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7539 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7540
7541 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7542 }
7543
7544 /* Allocate a hash table for type unit groups. */
7545
7546 static htab_up
7547 allocate_type_unit_groups_table ()
7548 {
7549 return htab_up (htab_create_alloc (3,
7550 hash_type_unit_group,
7551 eq_type_unit_group,
7552 NULL, xcalloc, xfree));
7553 }
7554
7555 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7556 partial symtabs. We combine several TUs per psymtab to not let the size
7557 of any one psymtab grow too big. */
7558 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7559 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7560
7561 /* Helper routine for get_type_unit_group.
7562 Create the type_unit_group object used to hold one or more TUs. */
7563
7564 static struct type_unit_group *
7565 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7566 {
7567 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7568 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7569 struct dwarf2_per_cu_data *per_cu;
7570 struct type_unit_group *tu_group;
7571
7572 tu_group = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, type_unit_group);
7573 per_cu = &tu_group->per_cu;
7574 per_cu->per_bfd = per_bfd;
7575
7576 if (per_bfd->using_index)
7577 {
7578 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7579 struct dwarf2_per_cu_quick_data);
7580 }
7581 else
7582 {
7583 unsigned int line_offset = to_underlying (line_offset_struct);
7584 dwarf2_psymtab *pst;
7585 std::string name;
7586
7587 /* Give the symtab a useful name for debug purposes. */
7588 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7589 name = string_printf ("<type_units_%d>",
7590 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7591 else
7592 name = string_printf ("<type_units_at_0x%x>", line_offset);
7593
7594 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
7595 pst->anonymous = true;
7596 }
7597
7598 tu_group->hash.dwo_unit = cu->dwo_unit;
7599 tu_group->hash.line_sect_off = line_offset_struct;
7600
7601 return tu_group;
7602 }
7603
7604 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7605 STMT_LIST is a DW_AT_stmt_list attribute. */
7606
7607 static struct type_unit_group *
7608 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7609 {
7610 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7611 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7612 struct type_unit_group *tu_group;
7613 void **slot;
7614 unsigned int line_offset;
7615 struct type_unit_group type_unit_group_for_lookup;
7616
7617 if (per_objfile->per_bfd->type_unit_groups == NULL)
7618 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7619
7620 /* Do we need to create a new group, or can we use an existing one? */
7621
7622 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
7623 {
7624 line_offset = stmt_list->as_unsigned ();
7625 ++tu_stats->nr_symtab_sharers;
7626 }
7627 else
7628 {
7629 /* Ugh, no stmt_list. Rare, but we have to handle it.
7630 We can do various things here like create one group per TU or
7631 spread them over multiple groups to split up the expansion work.
7632 To avoid worst case scenarios (too many groups or too large groups)
7633 we, umm, group them in bunches. */
7634 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7635 | (tu_stats->nr_stmt_less_type_units
7636 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7637 ++tu_stats->nr_stmt_less_type_units;
7638 }
7639
7640 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7641 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7642 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
7643 &type_unit_group_for_lookup, INSERT);
7644 if (*slot != NULL)
7645 {
7646 tu_group = (struct type_unit_group *) *slot;
7647 gdb_assert (tu_group != NULL);
7648 }
7649 else
7650 {
7651 sect_offset line_offset_struct = (sect_offset) line_offset;
7652 tu_group = create_type_unit_group (cu, line_offset_struct);
7653 *slot = tu_group;
7654 ++tu_stats->nr_symtabs;
7655 }
7656
7657 return tu_group;
7658 }
7659 \f
7660 /* Partial symbol tables. */
7661
7662 /* Create a psymtab named NAME and assign it to PER_CU.
7663
7664 The caller must fill in the following details:
7665 dirname, textlow, texthigh. */
7666
7667 static dwarf2_psymtab *
7668 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7669 dwarf2_per_objfile *per_objfile,
7670 const char *name)
7671 {
7672 struct objfile *objfile = per_objfile->objfile;
7673 dwarf2_psymtab *pst;
7674
7675 pst = new dwarf2_psymtab (name, objfile, per_cu);
7676
7677 pst->psymtabs_addrmap_supported = true;
7678
7679 /* This is the glue that links PST into GDB's symbol API. */
7680 per_cu->v.psymtab = pst;
7681
7682 return pst;
7683 }
7684
7685 /* DIE reader function for process_psymtab_comp_unit. */
7686
7687 static void
7688 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7689 const gdb_byte *info_ptr,
7690 struct die_info *comp_unit_die,
7691 enum language pretend_language)
7692 {
7693 struct dwarf2_cu *cu = reader->cu;
7694 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7695 struct objfile *objfile = per_objfile->objfile;
7696 struct gdbarch *gdbarch = objfile->arch ();
7697 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7698 CORE_ADDR baseaddr;
7699 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7700 dwarf2_psymtab *pst;
7701 enum pc_bounds_kind cu_bounds_kind;
7702 const char *filename;
7703
7704 gdb_assert (! per_cu->is_debug_types);
7705
7706 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7707
7708 /* Allocate a new partial symbol table structure. */
7709 gdb::unique_xmalloc_ptr<char> debug_filename;
7710 static const char artificial[] = "<artificial>";
7711 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7712 if (filename == NULL)
7713 filename = "";
7714 else if (strcmp (filename, artificial) == 0)
7715 {
7716 debug_filename.reset (concat (artificial, "@",
7717 sect_offset_str (per_cu->sect_off),
7718 (char *) NULL));
7719 filename = debug_filename.get ();
7720 }
7721
7722 pst = create_partial_symtab (per_cu, per_objfile, filename);
7723
7724 /* This must be done before calling dwarf2_build_include_psymtabs. */
7725 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7726
7727 baseaddr = objfile->text_section_offset ();
7728
7729 dwarf2_find_base_address (comp_unit_die, cu);
7730
7731 /* Possibly set the default values of LOWPC and HIGHPC from
7732 `DW_AT_ranges'. */
7733 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7734 &best_highpc, cu, pst);
7735 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7736 {
7737 CORE_ADDR low
7738 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7739 - baseaddr);
7740 CORE_ADDR high
7741 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7742 - baseaddr - 1);
7743 /* Store the contiguous range if it is not empty; it can be
7744 empty for CUs with no code. */
7745 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7746 low, high, pst);
7747 }
7748
7749 /* Check if comp unit has_children.
7750 If so, read the rest of the partial symbols from this comp unit.
7751 If not, there's no more debug_info for this comp unit. */
7752 if (comp_unit_die->has_children)
7753 {
7754 struct partial_die_info *first_die;
7755 CORE_ADDR lowpc, highpc;
7756
7757 lowpc = ((CORE_ADDR) -1);
7758 highpc = ((CORE_ADDR) 0);
7759
7760 first_die = load_partial_dies (reader, info_ptr, 1);
7761
7762 scan_partial_symbols (first_die, &lowpc, &highpc,
7763 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7764
7765 /* If we didn't find a lowpc, set it to highpc to avoid
7766 complaints from `maint check'. */
7767 if (lowpc == ((CORE_ADDR) -1))
7768 lowpc = highpc;
7769
7770 /* If the compilation unit didn't have an explicit address range,
7771 then use the information extracted from its child dies. */
7772 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7773 {
7774 best_lowpc = lowpc;
7775 best_highpc = highpc;
7776 }
7777 }
7778 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7779 best_lowpc + baseaddr)
7780 - baseaddr);
7781 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7782 best_highpc + baseaddr)
7783 - baseaddr);
7784
7785 pst->end ();
7786
7787 if (!cu->per_cu->imported_symtabs_empty ())
7788 {
7789 int i;
7790 int len = cu->per_cu->imported_symtabs_size ();
7791
7792 /* Fill in 'dependencies' here; we fill in 'users' in a
7793 post-pass. */
7794 pst->number_of_dependencies = len;
7795 pst->dependencies
7796 = objfile->partial_symtabs->allocate_dependencies (len);
7797 for (i = 0; i < len; ++i)
7798 {
7799 pst->dependencies[i]
7800 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7801 }
7802
7803 cu->per_cu->imported_symtabs_free ();
7804 }
7805
7806 /* Get the list of files included in the current compilation unit,
7807 and build a psymtab for each of them. */
7808 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7809
7810 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7811 ", %d global, %d static syms",
7812 per_cu->is_debug_types ? "type" : "comp",
7813 sect_offset_str (per_cu->sect_off),
7814 paddress (gdbarch, pst->text_low (objfile)),
7815 paddress (gdbarch, pst->text_high (objfile)),
7816 (int) pst->global_psymbols.size (),
7817 (int) pst->static_psymbols.size ());
7818 }
7819
7820 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7821 Process compilation unit THIS_CU for a psymtab. */
7822
7823 static void
7824 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7825 dwarf2_per_objfile *per_objfile,
7826 bool want_partial_unit,
7827 enum language pretend_language)
7828 {
7829 /* If this compilation unit was already read in, free the
7830 cached copy in order to read it in again. This is
7831 necessary because we skipped some symbols when we first
7832 read in the compilation unit (see load_partial_dies).
7833 This problem could be avoided, but the benefit is unclear. */
7834 per_objfile->remove_cu (this_cu);
7835
7836 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7837
7838 switch (reader.comp_unit_die->tag)
7839 {
7840 case DW_TAG_compile_unit:
7841 this_cu->unit_type = DW_UT_compile;
7842 break;
7843 case DW_TAG_partial_unit:
7844 this_cu->unit_type = DW_UT_partial;
7845 break;
7846 case DW_TAG_type_unit:
7847 this_cu->unit_type = DW_UT_type;
7848 break;
7849 default:
7850 abort ();
7851 }
7852
7853 if (reader.dummy_p)
7854 {
7855 /* Nothing. */
7856 }
7857 else if (this_cu->is_debug_types)
7858 build_type_psymtabs_reader (&reader, reader.info_ptr,
7859 reader.comp_unit_die);
7860 else if (want_partial_unit
7861 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7862 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7863 reader.comp_unit_die,
7864 pretend_language);
7865
7866 this_cu->lang = reader.cu->language;
7867
7868 /* Age out any secondary CUs. */
7869 per_objfile->age_comp_units ();
7870 }
7871
7872 /* Reader function for build_type_psymtabs. */
7873
7874 static void
7875 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7876 const gdb_byte *info_ptr,
7877 struct die_info *type_unit_die)
7878 {
7879 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7880 struct dwarf2_cu *cu = reader->cu;
7881 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7882 struct signatured_type *sig_type;
7883 struct type_unit_group *tu_group;
7884 struct attribute *attr;
7885 struct partial_die_info *first_die;
7886 CORE_ADDR lowpc, highpc;
7887 dwarf2_psymtab *pst;
7888
7889 gdb_assert (per_cu->is_debug_types);
7890 sig_type = (struct signatured_type *) per_cu;
7891
7892 if (! type_unit_die->has_children)
7893 return;
7894
7895 attr = type_unit_die->attr (DW_AT_stmt_list);
7896 tu_group = get_type_unit_group (cu, attr);
7897
7898 if (tu_group->tus == nullptr)
7899 tu_group->tus = new std::vector<signatured_type *>;
7900 tu_group->tus->push_back (sig_type);
7901
7902 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7903 pst = create_partial_symtab (per_cu, per_objfile, "");
7904 pst->anonymous = true;
7905
7906 first_die = load_partial_dies (reader, info_ptr, 1);
7907
7908 lowpc = (CORE_ADDR) -1;
7909 highpc = (CORE_ADDR) 0;
7910 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7911
7912 pst->end ();
7913 }
7914
7915 /* Struct used to sort TUs by their abbreviation table offset. */
7916
7917 struct tu_abbrev_offset
7918 {
7919 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7920 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7921 {}
7922
7923 signatured_type *sig_type;
7924 sect_offset abbrev_offset;
7925 };
7926
7927 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7928
7929 static bool
7930 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7931 const struct tu_abbrev_offset &b)
7932 {
7933 return a.abbrev_offset < b.abbrev_offset;
7934 }
7935
7936 /* Efficiently read all the type units.
7937 This does the bulk of the work for build_type_psymtabs.
7938
7939 The efficiency is because we sort TUs by the abbrev table they use and
7940 only read each abbrev table once. In one program there are 200K TUs
7941 sharing 8K abbrev tables.
7942
7943 The main purpose of this function is to support building the
7944 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7945 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7946 can collapse the search space by grouping them by stmt_list.
7947 The savings can be significant, in the same program from above the 200K TUs
7948 share 8K stmt_list tables.
7949
7950 FUNC is expected to call get_type_unit_group, which will create the
7951 struct type_unit_group if necessary and add it to
7952 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7953
7954 static void
7955 build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
7956 {
7957 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7958 abbrev_table_up abbrev_table;
7959 sect_offset abbrev_offset;
7960
7961 /* It's up to the caller to not call us multiple times. */
7962 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7963
7964 if (per_objfile->per_bfd->all_type_units.empty ())
7965 return;
7966
7967 /* TUs typically share abbrev tables, and there can be way more TUs than
7968 abbrev tables. Sort by abbrev table to reduce the number of times we
7969 read each abbrev table in.
7970 Alternatives are to punt or to maintain a cache of abbrev tables.
7971 This is simpler and efficient enough for now.
7972
7973 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7974 symtab to use). Typically TUs with the same abbrev offset have the same
7975 stmt_list value too so in practice this should work well.
7976
7977 The basic algorithm here is:
7978
7979 sort TUs by abbrev table
7980 for each TU with same abbrev table:
7981 read abbrev table if first user
7982 read TU top level DIE
7983 [IWBN if DWO skeletons had DW_AT_stmt_list]
7984 call FUNC */
7985
7986 dwarf_read_debug_printf ("Building type unit groups ...");
7987
7988 /* Sort in a separate table to maintain the order of all_type_units
7989 for .gdb_index: TU indices directly index all_type_units. */
7990 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7991 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
7992
7993 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
7994 sorted_by_abbrev.emplace_back
7995 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
7996 sig_type->per_cu.sect_off));
7997
7998 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7999 sort_tu_by_abbrev_offset);
8000
8001 abbrev_offset = (sect_offset) ~(unsigned) 0;
8002
8003 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
8004 {
8005 /* Switch to the next abbrev table if necessary. */
8006 if (abbrev_table == NULL
8007 || tu.abbrev_offset != abbrev_offset)
8008 {
8009 abbrev_offset = tu.abbrev_offset;
8010 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
8011 abbrev_table =
8012 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
8013 ++tu_stats->nr_uniq_abbrev_tables;
8014 }
8015
8016 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
8017 abbrev_table.get (), nullptr, false);
8018 if (!reader.dummy_p)
8019 build_type_psymtabs_reader (&reader, reader.info_ptr,
8020 reader.comp_unit_die);
8021 }
8022 }
8023
8024 /* Print collected type unit statistics. */
8025
8026 static void
8027 print_tu_stats (dwarf2_per_objfile *per_objfile)
8028 {
8029 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
8030
8031 dwarf_read_debug_printf ("Type unit statistics:");
8032 dwarf_read_debug_printf (" %zu TUs",
8033 per_objfile->per_bfd->all_type_units.size ());
8034 dwarf_read_debug_printf (" %d uniq abbrev tables",
8035 tu_stats->nr_uniq_abbrev_tables);
8036 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
8037 tu_stats->nr_symtabs);
8038 dwarf_read_debug_printf (" %d symtab sharers",
8039 tu_stats->nr_symtab_sharers);
8040 dwarf_read_debug_printf (" %d type units without a stmt_list",
8041 tu_stats->nr_stmt_less_type_units);
8042 dwarf_read_debug_printf (" %d all_type_units reallocs",
8043 tu_stats->nr_all_type_units_reallocs);
8044 }
8045
8046 /* Traversal function for build_type_psymtabs. */
8047
8048 static int
8049 build_type_psymtab_dependencies (void **slot, void *info)
8050 {
8051 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
8052 struct objfile *objfile = per_objfile->objfile;
8053 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8054 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8055 dwarf2_psymtab *pst = per_cu->v.psymtab;
8056 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
8057 int i;
8058
8059 gdb_assert (len > 0);
8060 gdb_assert (per_cu->type_unit_group_p ());
8061
8062 pst->number_of_dependencies = len;
8063 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
8064 for (i = 0; i < len; ++i)
8065 {
8066 struct signatured_type *iter = tu_group->tus->at (i);
8067 gdb_assert (iter->per_cu.is_debug_types);
8068 pst->dependencies[i] = iter->per_cu.v.psymtab;
8069 iter->type_unit_group = tu_group;
8070 }
8071
8072 delete tu_group->tus;
8073 tu_group->tus = nullptr;
8074
8075 return 1;
8076 }
8077
8078 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8079 Build partial symbol tables for the .debug_types comp-units. */
8080
8081 static void
8082 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
8083 {
8084 if (! create_all_type_units (per_objfile))
8085 return;
8086
8087 build_type_psymtabs_1 (per_objfile);
8088 }
8089
8090 /* Traversal function for process_skeletonless_type_unit.
8091 Read a TU in a DWO file and build partial symbols for it. */
8092
8093 static int
8094 process_skeletonless_type_unit (void **slot, void *info)
8095 {
8096 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8097 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
8098 struct signatured_type find_entry, *entry;
8099
8100 /* If this TU doesn't exist in the global table, add it and read it in. */
8101
8102 if (per_objfile->per_bfd->signatured_types == NULL)
8103 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
8104
8105 find_entry.signature = dwo_unit->signature;
8106 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
8107 &find_entry, INSERT);
8108 /* If we've already seen this type there's nothing to do. What's happening
8109 is we're doing our own version of comdat-folding here. */
8110 if (*slot != NULL)
8111 return 1;
8112
8113 /* This does the job that create_all_type_units would have done for
8114 this TU. */
8115 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
8116 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
8117 *slot = entry;
8118
8119 /* This does the job that build_type_psymtabs_1 would have done. */
8120 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
8121 if (!reader.dummy_p)
8122 build_type_psymtabs_reader (&reader, reader.info_ptr,
8123 reader.comp_unit_die);
8124
8125 return 1;
8126 }
8127
8128 /* Traversal function for process_skeletonless_type_units. */
8129
8130 static int
8131 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8132 {
8133 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8134
8135 if (dwo_file->tus != NULL)
8136 htab_traverse_noresize (dwo_file->tus.get (),
8137 process_skeletonless_type_unit, info);
8138
8139 return 1;
8140 }
8141
8142 /* Scan all TUs of DWO files, verifying we've processed them.
8143 This is needed in case a TU was emitted without its skeleton.
8144 Note: This can't be done until we know what all the DWO files are. */
8145
8146 static void
8147 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
8148 {
8149 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8150 if (get_dwp_file (per_objfile) == NULL
8151 && per_objfile->per_bfd->dwo_files != NULL)
8152 {
8153 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
8154 process_dwo_file_for_skeletonless_type_units,
8155 per_objfile);
8156 }
8157 }
8158
8159 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8160
8161 static void
8162 set_partial_user (dwarf2_per_objfile *per_objfile)
8163 {
8164 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
8165 {
8166 dwarf2_psymtab *pst = per_cu->v.psymtab;
8167
8168 if (pst == NULL)
8169 continue;
8170
8171 for (int j = 0; j < pst->number_of_dependencies; ++j)
8172 {
8173 /* Set the 'user' field only if it is not already set. */
8174 if (pst->dependencies[j]->user == NULL)
8175 pst->dependencies[j]->user = pst;
8176 }
8177 }
8178 }
8179
8180 /* Build the partial symbol table by doing a quick pass through the
8181 .debug_info and .debug_abbrev sections. */
8182
8183 static void
8184 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
8185 {
8186 struct objfile *objfile = per_objfile->objfile;
8187
8188 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
8189 objfile_name (objfile));
8190
8191 scoped_restore restore_reading_psyms
8192 = make_scoped_restore (&per_objfile->per_bfd->reading_partial_symbols,
8193 true);
8194
8195 per_objfile->per_bfd->info.read (objfile);
8196
8197 /* Any cached compilation units will be linked by the per-objfile
8198 read_in_chain. Make sure to free them when we're done. */
8199 free_cached_comp_units freer (per_objfile);
8200
8201 build_type_psymtabs (per_objfile);
8202
8203 create_all_comp_units (per_objfile);
8204
8205 /* Create a temporary address map on a temporary obstack. We later
8206 copy this to the final obstack. */
8207 auto_obstack temp_obstack;
8208
8209 scoped_restore save_psymtabs_addrmap
8210 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8211 addrmap_create_mutable (&temp_obstack));
8212
8213 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
8214 {
8215 if (per_cu->v.psymtab != NULL)
8216 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8217 continue;
8218 process_psymtab_comp_unit (per_cu, per_objfile, false,
8219 language_minimal);
8220 }
8221
8222 /* This has to wait until we read the CUs, we need the list of DWOs. */
8223 process_skeletonless_type_units (per_objfile);
8224
8225 /* Now that all TUs have been processed we can fill in the dependencies. */
8226 if (per_objfile->per_bfd->type_unit_groups != NULL)
8227 {
8228 htab_traverse_noresize (per_objfile->per_bfd->type_unit_groups.get (),
8229 build_type_psymtab_dependencies, per_objfile);
8230 }
8231
8232 if (dwarf_read_debug > 0)
8233 print_tu_stats (per_objfile);
8234
8235 set_partial_user (per_objfile);
8236
8237 objfile->partial_symtabs->psymtabs_addrmap
8238 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8239 objfile->partial_symtabs->obstack ());
8240 /* At this point we want to keep the address map. */
8241 save_psymtabs_addrmap.release ();
8242
8243 dwarf_read_debug_printf ("Done building psymtabs of %s",
8244 objfile_name (objfile));
8245 }
8246
8247 /* Load the partial DIEs for a secondary CU into memory.
8248 This is also used when rereading a primary CU with load_all_dies. */
8249
8250 static void
8251 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
8252 dwarf2_per_objfile *per_objfile,
8253 dwarf2_cu *existing_cu)
8254 {
8255 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
8256
8257 if (!reader.dummy_p)
8258 {
8259 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8260 language_minimal);
8261
8262 /* Check if comp unit has_children.
8263 If so, read the rest of the partial symbols from this comp unit.
8264 If not, there's no more debug_info for this comp unit. */
8265 if (reader.comp_unit_die->has_children)
8266 load_partial_dies (&reader, reader.info_ptr, 0);
8267
8268 reader.keep ();
8269 }
8270 }
8271
8272 static void
8273 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
8274 struct dwarf2_section_info *section,
8275 struct dwarf2_section_info *abbrev_section,
8276 unsigned int is_dwz)
8277 {
8278 const gdb_byte *info_ptr;
8279 struct objfile *objfile = per_objfile->objfile;
8280
8281 dwarf_read_debug_printf ("Reading %s for %s",
8282 section->get_name (),
8283 section->get_file_name ());
8284
8285 section->read (objfile);
8286
8287 info_ptr = section->buffer;
8288
8289 while (info_ptr < section->buffer + section->size)
8290 {
8291 struct dwarf2_per_cu_data *this_cu;
8292
8293 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8294
8295 comp_unit_head cu_header;
8296 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
8297 abbrev_section, info_ptr,
8298 rcuh_kind::COMPILE);
8299
8300 /* Save the compilation unit for later lookup. */
8301 if (cu_header.unit_type != DW_UT_type)
8302 this_cu = per_objfile->per_bfd->allocate_per_cu ();
8303 else
8304 {
8305 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
8306 sig_type->signature = cu_header.signature;
8307 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8308 this_cu = &sig_type->per_cu;
8309 }
8310 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8311 this_cu->sect_off = sect_off;
8312 this_cu->length = cu_header.length + cu_header.initial_length_size;
8313 this_cu->is_dwz = is_dwz;
8314 this_cu->section = section;
8315
8316 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8317
8318 info_ptr = info_ptr + this_cu->length;
8319 }
8320 }
8321
8322 /* Create a list of all compilation units in OBJFILE.
8323 This is only done for -readnow and building partial symtabs. */
8324
8325 static void
8326 create_all_comp_units (dwarf2_per_objfile *per_objfile)
8327 {
8328 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8329 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8330 &per_objfile->per_bfd->abbrev, 0);
8331
8332 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8333 if (dwz != NULL)
8334 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
8335 }
8336
8337 /* Process all loaded DIEs for compilation unit CU, starting at
8338 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8339 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8340 DW_AT_ranges). See the comments of add_partial_subprogram on how
8341 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8342
8343 static void
8344 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8345 CORE_ADDR *highpc, int set_addrmap,
8346 struct dwarf2_cu *cu)
8347 {
8348 struct partial_die_info *pdi;
8349
8350 /* Now, march along the PDI's, descending into ones which have
8351 interesting children but skipping the children of the other ones,
8352 until we reach the end of the compilation unit. */
8353
8354 pdi = first_die;
8355
8356 while (pdi != NULL)
8357 {
8358 pdi->fixup (cu);
8359
8360 /* Anonymous namespaces or modules have no name but have interesting
8361 children, so we need to look at them. Ditto for anonymous
8362 enums. */
8363
8364 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
8365 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8366 || pdi->tag == DW_TAG_imported_unit
8367 || pdi->tag == DW_TAG_inlined_subroutine)
8368 {
8369 switch (pdi->tag)
8370 {
8371 case DW_TAG_subprogram:
8372 case DW_TAG_inlined_subroutine:
8373 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8374 if (cu->language == language_cplus)
8375 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8376 set_addrmap, cu);
8377 break;
8378 case DW_TAG_constant:
8379 case DW_TAG_variable:
8380 case DW_TAG_typedef:
8381 case DW_TAG_union_type:
8382 if (!pdi->is_declaration
8383 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8384 {
8385 add_partial_symbol (pdi, cu);
8386 }
8387 break;
8388 case DW_TAG_class_type:
8389 case DW_TAG_interface_type:
8390 case DW_TAG_structure_type:
8391 if (!pdi->is_declaration)
8392 {
8393 add_partial_symbol (pdi, cu);
8394 }
8395 if ((cu->language == language_rust
8396 || cu->language == language_cplus) && pdi->has_children)
8397 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8398 set_addrmap, cu);
8399 break;
8400 case DW_TAG_enumeration_type:
8401 if (!pdi->is_declaration)
8402 add_partial_enumeration (pdi, cu);
8403 break;
8404 case DW_TAG_base_type:
8405 case DW_TAG_subrange_type:
8406 /* File scope base type definitions are added to the partial
8407 symbol table. */
8408 add_partial_symbol (pdi, cu);
8409 break;
8410 case DW_TAG_namespace:
8411 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8412 break;
8413 case DW_TAG_module:
8414 if (!pdi->is_declaration)
8415 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8416 break;
8417 case DW_TAG_imported_unit:
8418 {
8419 struct dwarf2_per_cu_data *per_cu;
8420
8421 /* For now we don't handle imported units in type units. */
8422 if (cu->per_cu->is_debug_types)
8423 {
8424 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8425 " supported in type units [in module %s]"),
8426 objfile_name (cu->per_objfile->objfile));
8427 }
8428
8429 per_cu = dwarf2_find_containing_comp_unit
8430 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8431
8432 /* Go read the partial unit, if needed. */
8433 if (per_cu->v.psymtab == NULL)
8434 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8435 cu->language);
8436
8437 cu->per_cu->imported_symtabs_push (per_cu);
8438 }
8439 break;
8440 case DW_TAG_imported_declaration:
8441 add_partial_symbol (pdi, cu);
8442 break;
8443 default:
8444 break;
8445 }
8446 }
8447
8448 /* If the die has a sibling, skip to the sibling. */
8449
8450 pdi = pdi->die_sibling;
8451 }
8452 }
8453
8454 /* Functions used to compute the fully scoped name of a partial DIE.
8455
8456 Normally, this is simple. For C++, the parent DIE's fully scoped
8457 name is concatenated with "::" and the partial DIE's name.
8458 Enumerators are an exception; they use the scope of their parent
8459 enumeration type, i.e. the name of the enumeration type is not
8460 prepended to the enumerator.
8461
8462 There are two complexities. One is DW_AT_specification; in this
8463 case "parent" means the parent of the target of the specification,
8464 instead of the direct parent of the DIE. The other is compilers
8465 which do not emit DW_TAG_namespace; in this case we try to guess
8466 the fully qualified name of structure types from their members'
8467 linkage names. This must be done using the DIE's children rather
8468 than the children of any DW_AT_specification target. We only need
8469 to do this for structures at the top level, i.e. if the target of
8470 any DW_AT_specification (if any; otherwise the DIE itself) does not
8471 have a parent. */
8472
8473 /* Compute the scope prefix associated with PDI's parent, in
8474 compilation unit CU. The result will be allocated on CU's
8475 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8476 field. NULL is returned if no prefix is necessary. */
8477 static const char *
8478 partial_die_parent_scope (struct partial_die_info *pdi,
8479 struct dwarf2_cu *cu)
8480 {
8481 const char *grandparent_scope;
8482 struct partial_die_info *parent, *real_pdi;
8483
8484 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8485 then this means the parent of the specification DIE. */
8486
8487 real_pdi = pdi;
8488 while (real_pdi->has_specification)
8489 {
8490 auto res = find_partial_die (real_pdi->spec_offset,
8491 real_pdi->spec_is_dwz, cu);
8492 real_pdi = res.pdi;
8493 cu = res.cu;
8494 }
8495
8496 parent = real_pdi->die_parent;
8497 if (parent == NULL)
8498 return NULL;
8499
8500 if (parent->scope_set)
8501 return parent->scope;
8502
8503 parent->fixup (cu);
8504
8505 grandparent_scope = partial_die_parent_scope (parent, cu);
8506
8507 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8508 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8509 Work around this problem here. */
8510 if (cu->language == language_cplus
8511 && parent->tag == DW_TAG_namespace
8512 && strcmp (parent->name (cu), "::") == 0
8513 && grandparent_scope == NULL)
8514 {
8515 parent->scope = NULL;
8516 parent->scope_set = 1;
8517 return NULL;
8518 }
8519
8520 /* Nested subroutines in Fortran get a prefix. */
8521 if (pdi->tag == DW_TAG_enumerator)
8522 /* Enumerators should not get the name of the enumeration as a prefix. */
8523 parent->scope = grandparent_scope;
8524 else if (parent->tag == DW_TAG_namespace
8525 || parent->tag == DW_TAG_module
8526 || parent->tag == DW_TAG_structure_type
8527 || parent->tag == DW_TAG_class_type
8528 || parent->tag == DW_TAG_interface_type
8529 || parent->tag == DW_TAG_union_type
8530 || parent->tag == DW_TAG_enumeration_type
8531 || (cu->language == language_fortran
8532 && parent->tag == DW_TAG_subprogram
8533 && pdi->tag == DW_TAG_subprogram))
8534 {
8535 if (grandparent_scope == NULL)
8536 parent->scope = parent->name (cu);
8537 else
8538 parent->scope = typename_concat (&cu->comp_unit_obstack,
8539 grandparent_scope,
8540 parent->name (cu), 0, cu);
8541 }
8542 else
8543 {
8544 /* FIXME drow/2004-04-01: What should we be doing with
8545 function-local names? For partial symbols, we should probably be
8546 ignoring them. */
8547 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8548 dwarf_tag_name (parent->tag),
8549 sect_offset_str (pdi->sect_off));
8550 parent->scope = grandparent_scope;
8551 }
8552
8553 parent->scope_set = 1;
8554 return parent->scope;
8555 }
8556
8557 /* Return the fully scoped name associated with PDI, from compilation unit
8558 CU. The result will be allocated with malloc. */
8559
8560 static gdb::unique_xmalloc_ptr<char>
8561 partial_die_full_name (struct partial_die_info *pdi,
8562 struct dwarf2_cu *cu)
8563 {
8564 const char *parent_scope;
8565
8566 /* If this is a template instantiation, we can not work out the
8567 template arguments from partial DIEs. So, unfortunately, we have
8568 to go through the full DIEs. At least any work we do building
8569 types here will be reused if full symbols are loaded later. */
8570 if (pdi->has_template_arguments)
8571 {
8572 pdi->fixup (cu);
8573
8574 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
8575 {
8576 struct die_info *die;
8577 struct attribute attr;
8578 struct dwarf2_cu *ref_cu = cu;
8579
8580 /* DW_FORM_ref_addr is using section offset. */
8581 attr.name = (enum dwarf_attribute) 0;
8582 attr.form = DW_FORM_ref_addr;
8583 attr.u.unsnd = to_underlying (pdi->sect_off);
8584 die = follow_die_ref (NULL, &attr, &ref_cu);
8585
8586 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8587 }
8588 }
8589
8590 parent_scope = partial_die_parent_scope (pdi, cu);
8591 if (parent_scope == NULL)
8592 return NULL;
8593 else
8594 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8595 pdi->name (cu),
8596 0, cu));
8597 }
8598
8599 static void
8600 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8601 {
8602 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8603 struct objfile *objfile = per_objfile->objfile;
8604 struct gdbarch *gdbarch = objfile->arch ();
8605 CORE_ADDR addr = 0;
8606 const char *actual_name = NULL;
8607 CORE_ADDR baseaddr;
8608
8609 baseaddr = objfile->text_section_offset ();
8610
8611 gdb::unique_xmalloc_ptr<char> built_actual_name
8612 = partial_die_full_name (pdi, cu);
8613 if (built_actual_name != NULL)
8614 actual_name = built_actual_name.get ();
8615
8616 if (actual_name == NULL)
8617 actual_name = pdi->name (cu);
8618
8619 partial_symbol psymbol;
8620 memset (&psymbol, 0, sizeof (psymbol));
8621 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8622 psymbol.ginfo.set_section_index (-1);
8623
8624 /* The code below indicates that the psymbol should be installed by
8625 setting this. */
8626 gdb::optional<psymbol_placement> where;
8627
8628 switch (pdi->tag)
8629 {
8630 case DW_TAG_inlined_subroutine:
8631 case DW_TAG_subprogram:
8632 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8633 - baseaddr);
8634 if (pdi->is_external
8635 || cu->language == language_ada
8636 || (cu->language == language_fortran
8637 && pdi->die_parent != NULL
8638 && pdi->die_parent->tag == DW_TAG_subprogram))
8639 {
8640 /* Normally, only "external" DIEs are part of the global scope.
8641 But in Ada and Fortran, we want to be able to access nested
8642 procedures globally. So all Ada and Fortran subprograms are
8643 stored in the global scope. */
8644 where = psymbol_placement::GLOBAL;
8645 }
8646 else
8647 where = psymbol_placement::STATIC;
8648
8649 psymbol.domain = VAR_DOMAIN;
8650 psymbol.aclass = LOC_BLOCK;
8651 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8652 psymbol.ginfo.value.address = addr;
8653
8654 if (pdi->main_subprogram && actual_name != NULL)
8655 set_objfile_main_name (objfile, actual_name, cu->language);
8656 break;
8657 case DW_TAG_constant:
8658 psymbol.domain = VAR_DOMAIN;
8659 psymbol.aclass = LOC_STATIC;
8660 where = (pdi->is_external
8661 ? psymbol_placement::GLOBAL
8662 : psymbol_placement::STATIC);
8663 break;
8664 case DW_TAG_variable:
8665 if (pdi->d.locdesc)
8666 addr = decode_locdesc (pdi->d.locdesc, cu);
8667
8668 if (pdi->d.locdesc
8669 && addr == 0
8670 && !per_objfile->per_bfd->has_section_at_zero)
8671 {
8672 /* A global or static variable may also have been stripped
8673 out by the linker if unused, in which case its address
8674 will be nullified; do not add such variables into partial
8675 symbol table then. */
8676 }
8677 else if (pdi->is_external)
8678 {
8679 /* Global Variable.
8680 Don't enter into the minimal symbol tables as there is
8681 a minimal symbol table entry from the ELF symbols already.
8682 Enter into partial symbol table if it has a location
8683 descriptor or a type.
8684 If the location descriptor is missing, new_symbol will create
8685 a LOC_UNRESOLVED symbol, the address of the variable will then
8686 be determined from the minimal symbol table whenever the variable
8687 is referenced.
8688 The address for the partial symbol table entry is not
8689 used by GDB, but it comes in handy for debugging partial symbol
8690 table building. */
8691
8692 if (pdi->d.locdesc || pdi->has_type)
8693 {
8694 psymbol.domain = VAR_DOMAIN;
8695 psymbol.aclass = LOC_STATIC;
8696 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8697 psymbol.ginfo.value.address = addr;
8698 where = psymbol_placement::GLOBAL;
8699 }
8700 }
8701 else
8702 {
8703 int has_loc = pdi->d.locdesc != NULL;
8704
8705 /* Static Variable. Skip symbols whose value we cannot know (those
8706 without location descriptors or constant values). */
8707 if (!has_loc && !pdi->has_const_value)
8708 return;
8709
8710 psymbol.domain = VAR_DOMAIN;
8711 psymbol.aclass = LOC_STATIC;
8712 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8713 if (has_loc)
8714 psymbol.ginfo.value.address = addr;
8715 where = psymbol_placement::STATIC;
8716 }
8717 break;
8718 case DW_TAG_array_type:
8719 case DW_TAG_typedef:
8720 case DW_TAG_base_type:
8721 case DW_TAG_subrange_type:
8722 psymbol.domain = VAR_DOMAIN;
8723 psymbol.aclass = LOC_TYPEDEF;
8724 where = psymbol_placement::STATIC;
8725 break;
8726 case DW_TAG_imported_declaration:
8727 case DW_TAG_namespace:
8728 psymbol.domain = VAR_DOMAIN;
8729 psymbol.aclass = LOC_TYPEDEF;
8730 where = psymbol_placement::GLOBAL;
8731 break;
8732 case DW_TAG_module:
8733 /* With Fortran 77 there might be a "BLOCK DATA" module
8734 available without any name. If so, we skip the module as it
8735 doesn't bring any value. */
8736 if (actual_name != nullptr)
8737 {
8738 psymbol.domain = MODULE_DOMAIN;
8739 psymbol.aclass = LOC_TYPEDEF;
8740 where = psymbol_placement::GLOBAL;
8741 }
8742 break;
8743 case DW_TAG_class_type:
8744 case DW_TAG_interface_type:
8745 case DW_TAG_structure_type:
8746 case DW_TAG_union_type:
8747 case DW_TAG_enumeration_type:
8748 /* Skip external references. The DWARF standard says in the section
8749 about "Structure, Union, and Class Type Entries": "An incomplete
8750 structure, union or class type is represented by a structure,
8751 union or class entry that does not have a byte size attribute
8752 and that has a DW_AT_declaration attribute." */
8753 if (!pdi->has_byte_size && pdi->is_declaration)
8754 return;
8755
8756 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8757 static vs. global. */
8758 psymbol.domain = STRUCT_DOMAIN;
8759 psymbol.aclass = LOC_TYPEDEF;
8760 where = (cu->language == language_cplus
8761 ? psymbol_placement::GLOBAL
8762 : psymbol_placement::STATIC);
8763 break;
8764 case DW_TAG_enumerator:
8765 psymbol.domain = VAR_DOMAIN;
8766 psymbol.aclass = LOC_CONST;
8767 where = (cu->language == language_cplus
8768 ? psymbol_placement::GLOBAL
8769 : psymbol_placement::STATIC);
8770 break;
8771 default:
8772 break;
8773 }
8774
8775 if (where.has_value ())
8776 {
8777 if (built_actual_name != nullptr)
8778 actual_name = objfile->intern (actual_name);
8779 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8780 psymbol.ginfo.set_linkage_name (actual_name);
8781 else
8782 {
8783 psymbol.ginfo.set_demangled_name (actual_name,
8784 &objfile->objfile_obstack);
8785 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8786 }
8787 cu->per_cu->v.psymtab->add_psymbol (psymbol, *where, objfile);
8788 }
8789 }
8790
8791 /* Read a partial die corresponding to a namespace; also, add a symbol
8792 corresponding to that namespace to the symbol table. NAMESPACE is
8793 the name of the enclosing namespace. */
8794
8795 static void
8796 add_partial_namespace (struct partial_die_info *pdi,
8797 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8798 int set_addrmap, struct dwarf2_cu *cu)
8799 {
8800 /* Add a symbol for the namespace. */
8801
8802 add_partial_symbol (pdi, cu);
8803
8804 /* Now scan partial symbols in that namespace. */
8805
8806 if (pdi->has_children)
8807 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8808 }
8809
8810 /* Read a partial die corresponding to a Fortran module. */
8811
8812 static void
8813 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8814 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8815 {
8816 /* Add a symbol for the namespace. */
8817
8818 add_partial_symbol (pdi, cu);
8819
8820 /* Now scan partial symbols in that module. */
8821
8822 if (pdi->has_children)
8823 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8824 }
8825
8826 /* Read a partial die corresponding to a subprogram or an inlined
8827 subprogram and create a partial symbol for that subprogram.
8828 When the CU language allows it, this routine also defines a partial
8829 symbol for each nested subprogram that this subprogram contains.
8830 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8831 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8832
8833 PDI may also be a lexical block, in which case we simply search
8834 recursively for subprograms defined inside that lexical block.
8835 Again, this is only performed when the CU language allows this
8836 type of definitions. */
8837
8838 static void
8839 add_partial_subprogram (struct partial_die_info *pdi,
8840 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8841 int set_addrmap, struct dwarf2_cu *cu)
8842 {
8843 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8844 {
8845 if (pdi->has_pc_info)
8846 {
8847 if (pdi->lowpc < *lowpc)
8848 *lowpc = pdi->lowpc;
8849 if (pdi->highpc > *highpc)
8850 *highpc = pdi->highpc;
8851 if (set_addrmap)
8852 {
8853 struct objfile *objfile = cu->per_objfile->objfile;
8854 struct gdbarch *gdbarch = objfile->arch ();
8855 CORE_ADDR baseaddr;
8856 CORE_ADDR this_highpc;
8857 CORE_ADDR this_lowpc;
8858
8859 baseaddr = objfile->text_section_offset ();
8860 this_lowpc
8861 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8862 pdi->lowpc + baseaddr)
8863 - baseaddr);
8864 this_highpc
8865 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8866 pdi->highpc + baseaddr)
8867 - baseaddr);
8868 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8869 this_lowpc, this_highpc - 1,
8870 cu->per_cu->v.psymtab);
8871 }
8872 }
8873
8874 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8875 {
8876 if (!pdi->is_declaration)
8877 /* Ignore subprogram DIEs that do not have a name, they are
8878 illegal. Do not emit a complaint at this point, we will
8879 do so when we convert this psymtab into a symtab. */
8880 if (pdi->name (cu))
8881 add_partial_symbol (pdi, cu);
8882 }
8883 }
8884
8885 if (! pdi->has_children)
8886 return;
8887
8888 if (cu->language == language_ada || cu->language == language_fortran)
8889 {
8890 pdi = pdi->die_child;
8891 while (pdi != NULL)
8892 {
8893 pdi->fixup (cu);
8894 if (pdi->tag == DW_TAG_subprogram
8895 || pdi->tag == DW_TAG_inlined_subroutine
8896 || pdi->tag == DW_TAG_lexical_block)
8897 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8898 pdi = pdi->die_sibling;
8899 }
8900 }
8901 }
8902
8903 /* Read a partial die corresponding to an enumeration type. */
8904
8905 static void
8906 add_partial_enumeration (struct partial_die_info *enum_pdi,
8907 struct dwarf2_cu *cu)
8908 {
8909 struct partial_die_info *pdi;
8910
8911 if (enum_pdi->name (cu) != NULL)
8912 add_partial_symbol (enum_pdi, cu);
8913
8914 pdi = enum_pdi->die_child;
8915 while (pdi)
8916 {
8917 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8918 complaint (_("malformed enumerator DIE ignored"));
8919 else
8920 add_partial_symbol (pdi, cu);
8921 pdi = pdi->die_sibling;
8922 }
8923 }
8924
8925 /* Return the initial uleb128 in the die at INFO_PTR. */
8926
8927 static unsigned int
8928 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8929 {
8930 unsigned int bytes_read;
8931
8932 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8933 }
8934
8935 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8936 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8937
8938 Return the corresponding abbrev, or NULL if the number is zero (indicating
8939 an empty DIE). In either case *BYTES_READ will be set to the length of
8940 the initial number. */
8941
8942 static struct abbrev_info *
8943 peek_die_abbrev (const die_reader_specs &reader,
8944 const gdb_byte *info_ptr, unsigned int *bytes_read)
8945 {
8946 dwarf2_cu *cu = reader.cu;
8947 bfd *abfd = cu->per_objfile->objfile->obfd;
8948 unsigned int abbrev_number
8949 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8950
8951 if (abbrev_number == 0)
8952 return NULL;
8953
8954 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8955 if (!abbrev)
8956 {
8957 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8958 " at offset %s [in module %s]"),
8959 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8960 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8961 }
8962
8963 return abbrev;
8964 }
8965
8966 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8967 Returns a pointer to the end of a series of DIEs, terminated by an empty
8968 DIE. Any children of the skipped DIEs will also be skipped. */
8969
8970 static const gdb_byte *
8971 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8972 {
8973 while (1)
8974 {
8975 unsigned int bytes_read;
8976 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8977
8978 if (abbrev == NULL)
8979 return info_ptr + bytes_read;
8980 else
8981 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8982 }
8983 }
8984
8985 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8986 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8987 abbrev corresponding to that skipped uleb128 should be passed in
8988 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8989 children. */
8990
8991 static const gdb_byte *
8992 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8993 struct abbrev_info *abbrev)
8994 {
8995 unsigned int bytes_read;
8996 struct attribute attr;
8997 bfd *abfd = reader->abfd;
8998 struct dwarf2_cu *cu = reader->cu;
8999 const gdb_byte *buffer = reader->buffer;
9000 const gdb_byte *buffer_end = reader->buffer_end;
9001 unsigned int form, i;
9002
9003 for (i = 0; i < abbrev->num_attrs; i++)
9004 {
9005 /* The only abbrev we care about is DW_AT_sibling. */
9006 if (abbrev->attrs[i].name == DW_AT_sibling)
9007 {
9008 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9009 if (attr.form == DW_FORM_ref_addr)
9010 complaint (_("ignoring absolute DW_AT_sibling"));
9011 else
9012 {
9013 sect_offset off = attr.get_ref_die_offset ();
9014 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9015
9016 if (sibling_ptr < info_ptr)
9017 complaint (_("DW_AT_sibling points backwards"));
9018 else if (sibling_ptr > reader->buffer_end)
9019 reader->die_section->overflow_complaint ();
9020 else
9021 return sibling_ptr;
9022 }
9023 }
9024
9025 /* If it isn't DW_AT_sibling, skip this attribute. */
9026 form = abbrev->attrs[i].form;
9027 skip_attribute:
9028 switch (form)
9029 {
9030 case DW_FORM_ref_addr:
9031 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9032 and later it is offset sized. */
9033 if (cu->header.version == 2)
9034 info_ptr += cu->header.addr_size;
9035 else
9036 info_ptr += cu->header.offset_size;
9037 break;
9038 case DW_FORM_GNU_ref_alt:
9039 info_ptr += cu->header.offset_size;
9040 break;
9041 case DW_FORM_addr:
9042 info_ptr += cu->header.addr_size;
9043 break;
9044 case DW_FORM_data1:
9045 case DW_FORM_ref1:
9046 case DW_FORM_flag:
9047 case DW_FORM_strx1:
9048 info_ptr += 1;
9049 break;
9050 case DW_FORM_flag_present:
9051 case DW_FORM_implicit_const:
9052 break;
9053 case DW_FORM_data2:
9054 case DW_FORM_ref2:
9055 case DW_FORM_strx2:
9056 info_ptr += 2;
9057 break;
9058 case DW_FORM_strx3:
9059 info_ptr += 3;
9060 break;
9061 case DW_FORM_data4:
9062 case DW_FORM_ref4:
9063 case DW_FORM_strx4:
9064 info_ptr += 4;
9065 break;
9066 case DW_FORM_data8:
9067 case DW_FORM_ref8:
9068 case DW_FORM_ref_sig8:
9069 info_ptr += 8;
9070 break;
9071 case DW_FORM_data16:
9072 info_ptr += 16;
9073 break;
9074 case DW_FORM_string:
9075 read_direct_string (abfd, info_ptr, &bytes_read);
9076 info_ptr += bytes_read;
9077 break;
9078 case DW_FORM_sec_offset:
9079 case DW_FORM_strp:
9080 case DW_FORM_GNU_strp_alt:
9081 info_ptr += cu->header.offset_size;
9082 break;
9083 case DW_FORM_exprloc:
9084 case DW_FORM_block:
9085 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9086 info_ptr += bytes_read;
9087 break;
9088 case DW_FORM_block1:
9089 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9090 break;
9091 case DW_FORM_block2:
9092 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9093 break;
9094 case DW_FORM_block4:
9095 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9096 break;
9097 case DW_FORM_addrx:
9098 case DW_FORM_strx:
9099 case DW_FORM_sdata:
9100 case DW_FORM_udata:
9101 case DW_FORM_ref_udata:
9102 case DW_FORM_GNU_addr_index:
9103 case DW_FORM_GNU_str_index:
9104 case DW_FORM_rnglistx:
9105 case DW_FORM_loclistx:
9106 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9107 break;
9108 case DW_FORM_indirect:
9109 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9110 info_ptr += bytes_read;
9111 /* We need to continue parsing from here, so just go back to
9112 the top. */
9113 goto skip_attribute;
9114
9115 default:
9116 error (_("Dwarf Error: Cannot handle %s "
9117 "in DWARF reader [in module %s]"),
9118 dwarf_form_name (form),
9119 bfd_get_filename (abfd));
9120 }
9121 }
9122
9123 if (abbrev->has_children)
9124 return skip_children (reader, info_ptr);
9125 else
9126 return info_ptr;
9127 }
9128
9129 /* Locate ORIG_PDI's sibling.
9130 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9131
9132 static const gdb_byte *
9133 locate_pdi_sibling (const struct die_reader_specs *reader,
9134 struct partial_die_info *orig_pdi,
9135 const gdb_byte *info_ptr)
9136 {
9137 /* Do we know the sibling already? */
9138
9139 if (orig_pdi->sibling)
9140 return orig_pdi->sibling;
9141
9142 /* Are there any children to deal with? */
9143
9144 if (!orig_pdi->has_children)
9145 return info_ptr;
9146
9147 /* Skip the children the long way. */
9148
9149 return skip_children (reader, info_ptr);
9150 }
9151
9152 /* Expand this partial symbol table into a full symbol table. SELF is
9153 not NULL. */
9154
9155 void
9156 dwarf2_psymtab::read_symtab (struct objfile *objfile)
9157 {
9158 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9159
9160 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
9161
9162 /* If this psymtab is constructed from a debug-only objfile, the
9163 has_section_at_zero flag will not necessarily be correct. We
9164 can get the correct value for this flag by looking at the data
9165 associated with the (presumably stripped) associated objfile. */
9166 if (objfile->separate_debug_objfile_backlink)
9167 {
9168 dwarf2_per_objfile *per_objfile_backlink
9169 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9170
9171 per_objfile->per_bfd->has_section_at_zero
9172 = per_objfile_backlink->per_bfd->has_section_at_zero;
9173 }
9174
9175 expand_psymtab (objfile);
9176
9177 process_cu_includes (per_objfile);
9178 }
9179 \f
9180 /* Reading in full CUs. */
9181
9182 /* Add PER_CU to the queue. */
9183
9184 static void
9185 queue_comp_unit (dwarf2_per_cu_data *per_cu,
9186 dwarf2_per_objfile *per_objfile,
9187 enum language pretend_language)
9188 {
9189 per_cu->queued = 1;
9190
9191 gdb_assert (per_objfile->per_bfd->queue.has_value ());
9192 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
9193 }
9194
9195 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
9196
9197 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9198 dependency.
9199
9200 Return true if maybe_queue_comp_unit requires the caller to load the CU's
9201 DIEs, false otherwise.
9202
9203 Explanation: there is an invariant that if a CU is queued for expansion
9204 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
9205 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
9206 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
9207 are not yet loaded, the the caller must load the CU's DIEs to ensure the
9208 invariant is respected.
9209
9210 The caller is therefore not required to load the CU's DIEs (we return false)
9211 if:
9212
9213 - the CU is already expanded, and therefore does not get enqueued
9214 - the CU gets enqueued for expansion, but its DIEs are already loaded
9215
9216 Note that the caller should not use this function's return value as an
9217 indicator of whether the CU's DIEs are loaded right now, it should check
9218 that by calling `dwarf2_per_objfile::get_cu` instead. */
9219
9220 static int
9221 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9222 dwarf2_per_cu_data *per_cu,
9223 dwarf2_per_objfile *per_objfile,
9224 enum language pretend_language)
9225 {
9226 /* We may arrive here during partial symbol reading, if we need full
9227 DIEs to process an unusual case (e.g. template arguments). Do
9228 not queue PER_CU, just tell our caller to load its DIEs. */
9229 if (per_cu->per_bfd->reading_partial_symbols)
9230 {
9231 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9232
9233 if (cu == NULL || cu->dies == NULL)
9234 return 1;
9235 return 0;
9236 }
9237
9238 /* Mark the dependence relation so that we don't flush PER_CU
9239 too early. */
9240 if (dependent_cu != NULL)
9241 dwarf2_add_dependence (dependent_cu, per_cu);
9242
9243 /* If it's already on the queue, we have nothing to do. */
9244 if (per_cu->queued)
9245 {
9246 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
9247 loaded. */
9248 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
9249
9250 /* If the CU is queued for expansion, it should not already be
9251 expanded. */
9252 gdb_assert (!per_objfile->symtab_set_p (per_cu));
9253
9254 /* The DIEs are already loaded, the caller doesn't need to do it. */
9255 return 0;
9256 }
9257
9258 bool queued = false;
9259 if (!per_objfile->symtab_set_p (per_cu))
9260 {
9261 /* Add it to the queue. */
9262 queue_comp_unit (per_cu, per_objfile, pretend_language);
9263 queued = true;
9264 }
9265
9266 /* If the compilation unit is already loaded, just mark it as
9267 used. */
9268 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9269 if (cu != nullptr)
9270 cu->last_used = 0;
9271
9272 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
9273 and the DIEs are not already loaded. */
9274 return queued && cu == nullptr;
9275 }
9276
9277 /* Process the queue. */
9278
9279 static void
9280 process_queue (dwarf2_per_objfile *per_objfile)
9281 {
9282 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
9283 objfile_name (per_objfile->objfile));
9284
9285 /* The queue starts out with one item, but following a DIE reference
9286 may load a new CU, adding it to the end of the queue. */
9287 while (!per_objfile->per_bfd->queue->empty ())
9288 {
9289 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
9290 dwarf2_per_cu_data *per_cu = item.per_cu;
9291
9292 if (!per_objfile->symtab_set_p (per_cu))
9293 {
9294 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9295
9296 /* Skip dummy CUs. */
9297 if (cu != nullptr)
9298 {
9299 unsigned int debug_print_threshold;
9300 char buf[100];
9301
9302 if (per_cu->is_debug_types)
9303 {
9304 struct signatured_type *sig_type =
9305 (struct signatured_type *) per_cu;
9306
9307 sprintf (buf, "TU %s at offset %s",
9308 hex_string (sig_type->signature),
9309 sect_offset_str (per_cu->sect_off));
9310 /* There can be 100s of TUs.
9311 Only print them in verbose mode. */
9312 debug_print_threshold = 2;
9313 }
9314 else
9315 {
9316 sprintf (buf, "CU at offset %s",
9317 sect_offset_str (per_cu->sect_off));
9318 debug_print_threshold = 1;
9319 }
9320
9321 if (dwarf_read_debug >= debug_print_threshold)
9322 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
9323
9324 if (per_cu->is_debug_types)
9325 process_full_type_unit (cu, item.pretend_language);
9326 else
9327 process_full_comp_unit (cu, item.pretend_language);
9328
9329 if (dwarf_read_debug >= debug_print_threshold)
9330 dwarf_read_debug_printf ("Done expanding %s", buf);
9331 }
9332 }
9333
9334 per_cu->queued = 0;
9335 per_objfile->per_bfd->queue->pop ();
9336 }
9337
9338 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
9339 objfile_name (per_objfile->objfile));
9340 }
9341
9342 /* Read in full symbols for PST, and anything it depends on. */
9343
9344 void
9345 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9346 {
9347 gdb_assert (!readin_p (objfile));
9348
9349 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9350 free_cached_comp_units freer (per_objfile);
9351 expand_dependencies (objfile);
9352
9353 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9354 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9355 }
9356
9357 /* See psympriv.h. */
9358
9359 bool
9360 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9361 {
9362 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9363 return per_objfile->symtab_set_p (per_cu_data);
9364 }
9365
9366 /* See psympriv.h. */
9367
9368 compunit_symtab *
9369 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9370 {
9371 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9372 return per_objfile->get_symtab (per_cu_data);
9373 }
9374
9375 /* Trivial hash function for die_info: the hash value of a DIE
9376 is its offset in .debug_info for this objfile. */
9377
9378 static hashval_t
9379 die_hash (const void *item)
9380 {
9381 const struct die_info *die = (const struct die_info *) item;
9382
9383 return to_underlying (die->sect_off);
9384 }
9385
9386 /* Trivial comparison function for die_info structures: two DIEs
9387 are equal if they have the same offset. */
9388
9389 static int
9390 die_eq (const void *item_lhs, const void *item_rhs)
9391 {
9392 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9393 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9394
9395 return die_lhs->sect_off == die_rhs->sect_off;
9396 }
9397
9398 /* Load the DIEs associated with PER_CU into memory.
9399
9400 In some cases, the caller, while reading partial symbols, will need to load
9401 the full symbols for the CU for some reason. It will already have a
9402 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
9403 rather than creating a new one. */
9404
9405 static void
9406 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9407 dwarf2_per_objfile *per_objfile,
9408 dwarf2_cu *existing_cu,
9409 bool skip_partial,
9410 enum language pretend_language)
9411 {
9412 gdb_assert (! this_cu->is_debug_types);
9413
9414 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
9415 if (reader.dummy_p)
9416 return;
9417
9418 struct dwarf2_cu *cu = reader.cu;
9419 const gdb_byte *info_ptr = reader.info_ptr;
9420
9421 gdb_assert (cu->die_hash == NULL);
9422 cu->die_hash =
9423 htab_create_alloc_ex (cu->header.length / 12,
9424 die_hash,
9425 die_eq,
9426 NULL,
9427 &cu->comp_unit_obstack,
9428 hashtab_obstack_allocate,
9429 dummy_obstack_deallocate);
9430
9431 if (reader.comp_unit_die->has_children)
9432 reader.comp_unit_die->child
9433 = read_die_and_siblings (&reader, reader.info_ptr,
9434 &info_ptr, reader.comp_unit_die);
9435 cu->dies = reader.comp_unit_die;
9436 /* comp_unit_die is not stored in die_hash, no need. */
9437
9438 /* We try not to read any attributes in this function, because not
9439 all CUs needed for references have been loaded yet, and symbol
9440 table processing isn't initialized. But we have to set the CU language,
9441 or we won't be able to build types correctly.
9442 Similarly, if we do not read the producer, we can not apply
9443 producer-specific interpretation. */
9444 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9445
9446 reader.keep ();
9447 }
9448
9449 /* Add a DIE to the delayed physname list. */
9450
9451 static void
9452 add_to_method_list (struct type *type, int fnfield_index, int index,
9453 const char *name, struct die_info *die,
9454 struct dwarf2_cu *cu)
9455 {
9456 struct delayed_method_info mi;
9457 mi.type = type;
9458 mi.fnfield_index = fnfield_index;
9459 mi.index = index;
9460 mi.name = name;
9461 mi.die = die;
9462 cu->method_list.push_back (mi);
9463 }
9464
9465 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9466 "const" / "volatile". If so, decrements LEN by the length of the
9467 modifier and return true. Otherwise return false. */
9468
9469 template<size_t N>
9470 static bool
9471 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9472 {
9473 size_t mod_len = sizeof (mod) - 1;
9474 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9475 {
9476 len -= mod_len;
9477 return true;
9478 }
9479 return false;
9480 }
9481
9482 /* Compute the physnames of any methods on the CU's method list.
9483
9484 The computation of method physnames is delayed in order to avoid the
9485 (bad) condition that one of the method's formal parameters is of an as yet
9486 incomplete type. */
9487
9488 static void
9489 compute_delayed_physnames (struct dwarf2_cu *cu)
9490 {
9491 /* Only C++ delays computing physnames. */
9492 if (cu->method_list.empty ())
9493 return;
9494 gdb_assert (cu->language == language_cplus);
9495
9496 for (const delayed_method_info &mi : cu->method_list)
9497 {
9498 const char *physname;
9499 struct fn_fieldlist *fn_flp
9500 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9501 physname = dwarf2_physname (mi.name, mi.die, cu);
9502 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9503 = physname ? physname : "";
9504
9505 /* Since there's no tag to indicate whether a method is a
9506 const/volatile overload, extract that information out of the
9507 demangled name. */
9508 if (physname != NULL)
9509 {
9510 size_t len = strlen (physname);
9511
9512 while (1)
9513 {
9514 if (physname[len] == ')') /* shortcut */
9515 break;
9516 else if (check_modifier (physname, len, " const"))
9517 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9518 else if (check_modifier (physname, len, " volatile"))
9519 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9520 else
9521 break;
9522 }
9523 }
9524 }
9525
9526 /* The list is no longer needed. */
9527 cu->method_list.clear ();
9528 }
9529
9530 /* Go objects should be embedded in a DW_TAG_module DIE,
9531 and it's not clear if/how imported objects will appear.
9532 To keep Go support simple until that's worked out,
9533 go back through what we've read and create something usable.
9534 We could do this while processing each DIE, and feels kinda cleaner,
9535 but that way is more invasive.
9536 This is to, for example, allow the user to type "p var" or "b main"
9537 without having to specify the package name, and allow lookups
9538 of module.object to work in contexts that use the expression
9539 parser. */
9540
9541 static void
9542 fixup_go_packaging (struct dwarf2_cu *cu)
9543 {
9544 gdb::unique_xmalloc_ptr<char> package_name;
9545 struct pending *list;
9546 int i;
9547
9548 for (list = *cu->get_builder ()->get_global_symbols ();
9549 list != NULL;
9550 list = list->next)
9551 {
9552 for (i = 0; i < list->nsyms; ++i)
9553 {
9554 struct symbol *sym = list->symbol[i];
9555
9556 if (sym->language () == language_go
9557 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9558 {
9559 gdb::unique_xmalloc_ptr<char> this_package_name
9560 (go_symbol_package_name (sym));
9561
9562 if (this_package_name == NULL)
9563 continue;
9564 if (package_name == NULL)
9565 package_name = std::move (this_package_name);
9566 else
9567 {
9568 struct objfile *objfile = cu->per_objfile->objfile;
9569 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9570 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9571 (symbol_symtab (sym) != NULL
9572 ? symtab_to_filename_for_display
9573 (symbol_symtab (sym))
9574 : objfile_name (objfile)),
9575 this_package_name.get (), package_name.get ());
9576 }
9577 }
9578 }
9579 }
9580
9581 if (package_name != NULL)
9582 {
9583 struct objfile *objfile = cu->per_objfile->objfile;
9584 const char *saved_package_name = objfile->intern (package_name.get ());
9585 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9586 saved_package_name);
9587 struct symbol *sym;
9588
9589 sym = new (&objfile->objfile_obstack) symbol;
9590 sym->set_language (language_go, &objfile->objfile_obstack);
9591 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9592 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9593 e.g., "main" finds the "main" module and not C's main(). */
9594 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9595 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9596 SYMBOL_TYPE (sym) = type;
9597
9598 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9599 }
9600 }
9601
9602 /* Allocate a fully-qualified name consisting of the two parts on the
9603 obstack. */
9604
9605 static const char *
9606 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9607 {
9608 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9609 }
9610
9611 /* A helper that allocates a variant part to attach to a Rust enum
9612 type. OBSTACK is where the results should be allocated. TYPE is
9613 the type we're processing. DISCRIMINANT_INDEX is the index of the
9614 discriminant. It must be the index of one of the fields of TYPE,
9615 or -1 to mean there is no discriminant (univariant enum).
9616 DEFAULT_INDEX is the index of the default field; or -1 if there is
9617 no default. RANGES is indexed by "effective" field number (the
9618 field index, but omitting the discriminant and default fields) and
9619 must hold the discriminant values used by the variants. Note that
9620 RANGES must have a lifetime at least as long as OBSTACK -- either
9621 already allocated on it, or static. */
9622
9623 static void
9624 alloc_rust_variant (struct obstack *obstack, struct type *type,
9625 int discriminant_index, int default_index,
9626 gdb::array_view<discriminant_range> ranges)
9627 {
9628 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9629 gdb_assert (discriminant_index == -1
9630 || (discriminant_index >= 0
9631 && discriminant_index < type->num_fields ()));
9632 gdb_assert (default_index == -1
9633 || (default_index >= 0 && default_index < type->num_fields ()));
9634
9635 /* We have one variant for each non-discriminant field. */
9636 int n_variants = type->num_fields ();
9637 if (discriminant_index != -1)
9638 --n_variants;
9639
9640 variant *variants = new (obstack) variant[n_variants];
9641 int var_idx = 0;
9642 int range_idx = 0;
9643 for (int i = 0; i < type->num_fields (); ++i)
9644 {
9645 if (i == discriminant_index)
9646 continue;
9647
9648 variants[var_idx].first_field = i;
9649 variants[var_idx].last_field = i + 1;
9650
9651 /* The default field does not need a range, but other fields do.
9652 We skipped the discriminant above. */
9653 if (i != default_index)
9654 {
9655 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9656 ++range_idx;
9657 }
9658
9659 ++var_idx;
9660 }
9661
9662 gdb_assert (range_idx == ranges.size ());
9663 gdb_assert (var_idx == n_variants);
9664
9665 variant_part *part = new (obstack) variant_part;
9666 part->discriminant_index = discriminant_index;
9667 /* If there is no discriminant, then whether it is signed is of no
9668 consequence. */
9669 part->is_unsigned
9670 = (discriminant_index == -1
9671 ? false
9672 : type->field (discriminant_index).type ()->is_unsigned ());
9673 part->variants = gdb::array_view<variant> (variants, n_variants);
9674
9675 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9676 gdb::array_view<variant_part> *prop_value
9677 = new (storage) gdb::array_view<variant_part> (part, 1);
9678
9679 struct dynamic_prop prop;
9680 prop.set_variant_parts (prop_value);
9681
9682 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9683 }
9684
9685 /* Some versions of rustc emitted enums in an unusual way.
9686
9687 Ordinary enums were emitted as unions. The first element of each
9688 structure in the union was named "RUST$ENUM$DISR". This element
9689 held the discriminant.
9690
9691 These versions of Rust also implemented the "non-zero"
9692 optimization. When the enum had two values, and one is empty and
9693 the other holds a pointer that cannot be zero, the pointer is used
9694 as the discriminant, with a zero value meaning the empty variant.
9695 Here, the union's first member is of the form
9696 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9697 where the fieldnos are the indices of the fields that should be
9698 traversed in order to find the field (which may be several fields deep)
9699 and the variantname is the name of the variant of the case when the
9700 field is zero.
9701
9702 This function recognizes whether TYPE is of one of these forms,
9703 and, if so, smashes it to be a variant type. */
9704
9705 static void
9706 quirk_rust_enum (struct type *type, struct objfile *objfile)
9707 {
9708 gdb_assert (type->code () == TYPE_CODE_UNION);
9709
9710 /* We don't need to deal with empty enums. */
9711 if (type->num_fields () == 0)
9712 return;
9713
9714 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9715 if (type->num_fields () == 1
9716 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9717 {
9718 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9719
9720 /* Decode the field name to find the offset of the
9721 discriminant. */
9722 ULONGEST bit_offset = 0;
9723 struct type *field_type = type->field (0).type ();
9724 while (name[0] >= '0' && name[0] <= '9')
9725 {
9726 char *tail;
9727 unsigned long index = strtoul (name, &tail, 10);
9728 name = tail;
9729 if (*name != '$'
9730 || index >= field_type->num_fields ()
9731 || (TYPE_FIELD_LOC_KIND (field_type, index)
9732 != FIELD_LOC_KIND_BITPOS))
9733 {
9734 complaint (_("Could not parse Rust enum encoding string \"%s\""
9735 "[in module %s]"),
9736 TYPE_FIELD_NAME (type, 0),
9737 objfile_name (objfile));
9738 return;
9739 }
9740 ++name;
9741
9742 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9743 field_type = field_type->field (index).type ();
9744 }
9745
9746 /* Smash this type to be a structure type. We have to do this
9747 because the type has already been recorded. */
9748 type->set_code (TYPE_CODE_STRUCT);
9749 type->set_num_fields (3);
9750 /* Save the field we care about. */
9751 struct field saved_field = type->field (0);
9752 type->set_fields
9753 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9754
9755 /* Put the discriminant at index 0. */
9756 type->field (0).set_type (field_type);
9757 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9758 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9759 SET_FIELD_BITPOS (type->field (0), bit_offset);
9760
9761 /* The order of fields doesn't really matter, so put the real
9762 field at index 1 and the data-less field at index 2. */
9763 type->field (1) = saved_field;
9764 TYPE_FIELD_NAME (type, 1)
9765 = rust_last_path_segment (type->field (1).type ()->name ());
9766 type->field (1).type ()->set_name
9767 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9768 TYPE_FIELD_NAME (type, 1)));
9769
9770 const char *dataless_name
9771 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9772 name);
9773 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9774 dataless_name);
9775 type->field (2).set_type (dataless_type);
9776 /* NAME points into the original discriminant name, which
9777 already has the correct lifetime. */
9778 TYPE_FIELD_NAME (type, 2) = name;
9779 SET_FIELD_BITPOS (type->field (2), 0);
9780
9781 /* Indicate that this is a variant type. */
9782 static discriminant_range ranges[1] = { { 0, 0 } };
9783 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9784 }
9785 /* A union with a single anonymous field is probably an old-style
9786 univariant enum. */
9787 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9788 {
9789 /* Smash this type to be a structure type. We have to do this
9790 because the type has already been recorded. */
9791 type->set_code (TYPE_CODE_STRUCT);
9792
9793 struct type *field_type = type->field (0).type ();
9794 const char *variant_name
9795 = rust_last_path_segment (field_type->name ());
9796 TYPE_FIELD_NAME (type, 0) = variant_name;
9797 field_type->set_name
9798 (rust_fully_qualify (&objfile->objfile_obstack,
9799 type->name (), variant_name));
9800
9801 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
9802 }
9803 else
9804 {
9805 struct type *disr_type = nullptr;
9806 for (int i = 0; i < type->num_fields (); ++i)
9807 {
9808 disr_type = type->field (i).type ();
9809
9810 if (disr_type->code () != TYPE_CODE_STRUCT)
9811 {
9812 /* All fields of a true enum will be structs. */
9813 return;
9814 }
9815 else if (disr_type->num_fields () == 0)
9816 {
9817 /* Could be data-less variant, so keep going. */
9818 disr_type = nullptr;
9819 }
9820 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9821 "RUST$ENUM$DISR") != 0)
9822 {
9823 /* Not a Rust enum. */
9824 return;
9825 }
9826 else
9827 {
9828 /* Found one. */
9829 break;
9830 }
9831 }
9832
9833 /* If we got here without a discriminant, then it's probably
9834 just a union. */
9835 if (disr_type == nullptr)
9836 return;
9837
9838 /* Smash this type to be a structure type. We have to do this
9839 because the type has already been recorded. */
9840 type->set_code (TYPE_CODE_STRUCT);
9841
9842 /* Make space for the discriminant field. */
9843 struct field *disr_field = &disr_type->field (0);
9844 field *new_fields
9845 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9846 * sizeof (struct field)));
9847 memcpy (new_fields + 1, type->fields (),
9848 type->num_fields () * sizeof (struct field));
9849 type->set_fields (new_fields);
9850 type->set_num_fields (type->num_fields () + 1);
9851
9852 /* Install the discriminant at index 0 in the union. */
9853 type->field (0) = *disr_field;
9854 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9855 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9856
9857 /* We need a way to find the correct discriminant given a
9858 variant name. For convenience we build a map here. */
9859 struct type *enum_type = disr_field->type ();
9860 std::unordered_map<std::string, ULONGEST> discriminant_map;
9861 for (int i = 0; i < enum_type->num_fields (); ++i)
9862 {
9863 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9864 {
9865 const char *name
9866 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9867 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9868 }
9869 }
9870
9871 int n_fields = type->num_fields ();
9872 /* We don't need a range entry for the discriminant, but we do
9873 need one for every other field, as there is no default
9874 variant. */
9875 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9876 discriminant_range,
9877 n_fields - 1);
9878 /* Skip the discriminant here. */
9879 for (int i = 1; i < n_fields; ++i)
9880 {
9881 /* Find the final word in the name of this variant's type.
9882 That name can be used to look up the correct
9883 discriminant. */
9884 const char *variant_name
9885 = rust_last_path_segment (type->field (i).type ()->name ());
9886
9887 auto iter = discriminant_map.find (variant_name);
9888 if (iter != discriminant_map.end ())
9889 {
9890 ranges[i - 1].low = iter->second;
9891 ranges[i - 1].high = iter->second;
9892 }
9893
9894 /* In Rust, each element should have the size of the
9895 enclosing enum. */
9896 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9897
9898 /* Remove the discriminant field, if it exists. */
9899 struct type *sub_type = type->field (i).type ();
9900 if (sub_type->num_fields () > 0)
9901 {
9902 sub_type->set_num_fields (sub_type->num_fields () - 1);
9903 sub_type->set_fields (sub_type->fields () + 1);
9904 }
9905 TYPE_FIELD_NAME (type, i) = variant_name;
9906 sub_type->set_name
9907 (rust_fully_qualify (&objfile->objfile_obstack,
9908 type->name (), variant_name));
9909 }
9910
9911 /* Indicate that this is a variant type. */
9912 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9913 gdb::array_view<discriminant_range> (ranges,
9914 n_fields - 1));
9915 }
9916 }
9917
9918 /* Rewrite some Rust unions to be structures with variants parts. */
9919
9920 static void
9921 rust_union_quirks (struct dwarf2_cu *cu)
9922 {
9923 gdb_assert (cu->language == language_rust);
9924 for (type *type_ : cu->rust_unions)
9925 quirk_rust_enum (type_, cu->per_objfile->objfile);
9926 /* We don't need this any more. */
9927 cu->rust_unions.clear ();
9928 }
9929
9930 /* See read.h. */
9931
9932 type_unit_group_unshareable *
9933 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9934 {
9935 auto iter = this->m_type_units.find (tu_group);
9936 if (iter != this->m_type_units.end ())
9937 return iter->second.get ();
9938
9939 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9940 type_unit_group_unshareable *result = uniq.get ();
9941 this->m_type_units[tu_group] = std::move (uniq);
9942 return result;
9943 }
9944
9945 struct type *
9946 dwarf2_per_objfile::get_type_for_signatured_type
9947 (signatured_type *sig_type) const
9948 {
9949 auto iter = this->m_type_map.find (sig_type);
9950 if (iter == this->m_type_map.end ())
9951 return nullptr;
9952
9953 return iter->second;
9954 }
9955
9956 void dwarf2_per_objfile::set_type_for_signatured_type
9957 (signatured_type *sig_type, struct type *type)
9958 {
9959 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9960
9961 this->m_type_map[sig_type] = type;
9962 }
9963
9964 /* A helper function for computing the list of all symbol tables
9965 included by PER_CU. */
9966
9967 static void
9968 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9969 htab_t all_children, htab_t all_type_symtabs,
9970 dwarf2_per_cu_data *per_cu,
9971 dwarf2_per_objfile *per_objfile,
9972 struct compunit_symtab *immediate_parent)
9973 {
9974 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9975 if (*slot != NULL)
9976 {
9977 /* This inclusion and its children have been processed. */
9978 return;
9979 }
9980
9981 *slot = per_cu;
9982
9983 /* Only add a CU if it has a symbol table. */
9984 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9985 if (cust != NULL)
9986 {
9987 /* If this is a type unit only add its symbol table if we haven't
9988 seen it yet (type unit per_cu's can share symtabs). */
9989 if (per_cu->is_debug_types)
9990 {
9991 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9992 if (*slot == NULL)
9993 {
9994 *slot = cust;
9995 result->push_back (cust);
9996 if (cust->user == NULL)
9997 cust->user = immediate_parent;
9998 }
9999 }
10000 else
10001 {
10002 result->push_back (cust);
10003 if (cust->user == NULL)
10004 cust->user = immediate_parent;
10005 }
10006 }
10007
10008 if (!per_cu->imported_symtabs_empty ())
10009 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10010 {
10011 recursively_compute_inclusions (result, all_children,
10012 all_type_symtabs, ptr, per_objfile,
10013 cust);
10014 }
10015 }
10016
10017 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10018 PER_CU. */
10019
10020 static void
10021 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
10022 dwarf2_per_objfile *per_objfile)
10023 {
10024 gdb_assert (! per_cu->is_debug_types);
10025
10026 if (!per_cu->imported_symtabs_empty ())
10027 {
10028 int len;
10029 std::vector<compunit_symtab *> result_symtabs;
10030 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
10031
10032 /* If we don't have a symtab, we can just skip this case. */
10033 if (cust == NULL)
10034 return;
10035
10036 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
10037 htab_eq_pointer,
10038 NULL, xcalloc, xfree));
10039 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
10040 htab_eq_pointer,
10041 NULL, xcalloc, xfree));
10042
10043 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10044 {
10045 recursively_compute_inclusions (&result_symtabs, all_children.get (),
10046 all_type_symtabs.get (), ptr,
10047 per_objfile, cust);
10048 }
10049
10050 /* Now we have a transitive closure of all the included symtabs. */
10051 len = result_symtabs.size ();
10052 cust->includes
10053 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
10054 struct compunit_symtab *, len + 1);
10055 memcpy (cust->includes, result_symtabs.data (),
10056 len * sizeof (compunit_symtab *));
10057 cust->includes[len] = NULL;
10058 }
10059 }
10060
10061 /* Compute the 'includes' field for the symtabs of all the CUs we just
10062 read. */
10063
10064 static void
10065 process_cu_includes (dwarf2_per_objfile *per_objfile)
10066 {
10067 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
10068 {
10069 if (! iter->is_debug_types)
10070 compute_compunit_symtab_includes (iter, per_objfile);
10071 }
10072
10073 per_objfile->per_bfd->just_read_cus.clear ();
10074 }
10075
10076 /* Generate full symbol information for CU, whose DIEs have
10077 already been loaded into memory. */
10078
10079 static void
10080 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10081 {
10082 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10083 struct objfile *objfile = per_objfile->objfile;
10084 struct gdbarch *gdbarch = objfile->arch ();
10085 CORE_ADDR lowpc, highpc;
10086 struct compunit_symtab *cust;
10087 CORE_ADDR baseaddr;
10088 struct block *static_block;
10089 CORE_ADDR addr;
10090
10091 baseaddr = objfile->text_section_offset ();
10092
10093 /* Clear the list here in case something was left over. */
10094 cu->method_list.clear ();
10095
10096 cu->language = pretend_language;
10097 cu->language_defn = language_def (cu->language);
10098
10099 dwarf2_find_base_address (cu->dies, cu);
10100
10101 /* Do line number decoding in read_file_scope () */
10102 process_die (cu->dies, cu);
10103
10104 /* For now fudge the Go package. */
10105 if (cu->language == language_go)
10106 fixup_go_packaging (cu);
10107
10108 /* Now that we have processed all the DIEs in the CU, all the types
10109 should be complete, and it should now be safe to compute all of the
10110 physnames. */
10111 compute_delayed_physnames (cu);
10112
10113 if (cu->language == language_rust)
10114 rust_union_quirks (cu);
10115
10116 /* Some compilers don't define a DW_AT_high_pc attribute for the
10117 compilation unit. If the DW_AT_high_pc is missing, synthesize
10118 it, by scanning the DIE's below the compilation unit. */
10119 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10120
10121 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10122 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10123
10124 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10125 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10126 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10127 addrmap to help ensure it has an accurate map of pc values belonging to
10128 this comp unit. */
10129 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10130
10131 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10132 SECT_OFF_TEXT (objfile),
10133 0);
10134
10135 if (cust != NULL)
10136 {
10137 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10138
10139 /* Set symtab language to language from DW_AT_language. If the
10140 compilation is from a C file generated by language preprocessors, do
10141 not set the language if it was already deduced by start_subfile. */
10142 if (!(cu->language == language_c
10143 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10144 COMPUNIT_FILETABS (cust)->language = cu->language;
10145
10146 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10147 produce DW_AT_location with location lists but it can be possibly
10148 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10149 there were bugs in prologue debug info, fixed later in GCC-4.5
10150 by "unwind info for epilogues" patch (which is not directly related).
10151
10152 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10153 needed, it would be wrong due to missing DW_AT_producer there.
10154
10155 Still one can confuse GDB by using non-standard GCC compilation
10156 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10157 */
10158 if (cu->has_loclist && gcc_4_minor >= 5)
10159 cust->locations_valid = 1;
10160
10161 if (gcc_4_minor >= 5)
10162 cust->epilogue_unwind_valid = 1;
10163
10164 cust->call_site_htab = cu->call_site_htab;
10165 }
10166
10167 per_objfile->set_symtab (cu->per_cu, cust);
10168
10169 /* Push it for inclusion processing later. */
10170 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
10171
10172 /* Not needed any more. */
10173 cu->reset_builder ();
10174 }
10175
10176 /* Generate full symbol information for type unit CU, whose DIEs have
10177 already been loaded into memory. */
10178
10179 static void
10180 process_full_type_unit (dwarf2_cu *cu,
10181 enum language pretend_language)
10182 {
10183 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10184 struct objfile *objfile = per_objfile->objfile;
10185 struct compunit_symtab *cust;
10186 struct signatured_type *sig_type;
10187
10188 gdb_assert (cu->per_cu->is_debug_types);
10189 sig_type = (struct signatured_type *) cu->per_cu;
10190
10191 /* Clear the list here in case something was left over. */
10192 cu->method_list.clear ();
10193
10194 cu->language = pretend_language;
10195 cu->language_defn = language_def (cu->language);
10196
10197 /* The symbol tables are set up in read_type_unit_scope. */
10198 process_die (cu->dies, cu);
10199
10200 /* For now fudge the Go package. */
10201 if (cu->language == language_go)
10202 fixup_go_packaging (cu);
10203
10204 /* Now that we have processed all the DIEs in the CU, all the types
10205 should be complete, and it should now be safe to compute all of the
10206 physnames. */
10207 compute_delayed_physnames (cu);
10208
10209 if (cu->language == language_rust)
10210 rust_union_quirks (cu);
10211
10212 /* TUs share symbol tables.
10213 If this is the first TU to use this symtab, complete the construction
10214 of it with end_expandable_symtab. Otherwise, complete the addition of
10215 this TU's symbols to the existing symtab. */
10216 type_unit_group_unshareable *tug_unshare =
10217 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
10218 if (tug_unshare->compunit_symtab == NULL)
10219 {
10220 buildsym_compunit *builder = cu->get_builder ();
10221 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10222 tug_unshare->compunit_symtab = cust;
10223
10224 if (cust != NULL)
10225 {
10226 /* Set symtab language to language from DW_AT_language. If the
10227 compilation is from a C file generated by language preprocessors,
10228 do not set the language if it was already deduced by
10229 start_subfile. */
10230 if (!(cu->language == language_c
10231 && COMPUNIT_FILETABS (cust)->language != language_c))
10232 COMPUNIT_FILETABS (cust)->language = cu->language;
10233 }
10234 }
10235 else
10236 {
10237 cu->get_builder ()->augment_type_symtab ();
10238 cust = tug_unshare->compunit_symtab;
10239 }
10240
10241 per_objfile->set_symtab (cu->per_cu, cust);
10242
10243 /* Not needed any more. */
10244 cu->reset_builder ();
10245 }
10246
10247 /* Process an imported unit DIE. */
10248
10249 static void
10250 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10251 {
10252 struct attribute *attr;
10253
10254 /* For now we don't handle imported units in type units. */
10255 if (cu->per_cu->is_debug_types)
10256 {
10257 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10258 " supported in type units [in module %s]"),
10259 objfile_name (cu->per_objfile->objfile));
10260 }
10261
10262 attr = dwarf2_attr (die, DW_AT_import, cu);
10263 if (attr != NULL)
10264 {
10265 sect_offset sect_off = attr->get_ref_die_offset ();
10266 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10267 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10268 dwarf2_per_cu_data *per_cu
10269 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
10270
10271 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10272 into another compilation unit, at root level. Regard this as a hint,
10273 and ignore it. */
10274 if (die->parent && die->parent->parent == NULL
10275 && per_cu->unit_type == DW_UT_compile
10276 && per_cu->lang == language_cplus)
10277 return;
10278
10279 /* If necessary, add it to the queue and load its DIEs. */
10280 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
10281 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
10282 false, cu->language);
10283
10284 cu->per_cu->imported_symtabs_push (per_cu);
10285 }
10286 }
10287
10288 /* RAII object that represents a process_die scope: i.e.,
10289 starts/finishes processing a DIE. */
10290 class process_die_scope
10291 {
10292 public:
10293 process_die_scope (die_info *die, dwarf2_cu *cu)
10294 : m_die (die), m_cu (cu)
10295 {
10296 /* We should only be processing DIEs not already in process. */
10297 gdb_assert (!m_die->in_process);
10298 m_die->in_process = true;
10299 }
10300
10301 ~process_die_scope ()
10302 {
10303 m_die->in_process = false;
10304
10305 /* If we're done processing the DIE for the CU that owns the line
10306 header, we don't need the line header anymore. */
10307 if (m_cu->line_header_die_owner == m_die)
10308 {
10309 delete m_cu->line_header;
10310 m_cu->line_header = NULL;
10311 m_cu->line_header_die_owner = NULL;
10312 }
10313 }
10314
10315 private:
10316 die_info *m_die;
10317 dwarf2_cu *m_cu;
10318 };
10319
10320 /* Process a die and its children. */
10321
10322 static void
10323 process_die (struct die_info *die, struct dwarf2_cu *cu)
10324 {
10325 process_die_scope scope (die, cu);
10326
10327 switch (die->tag)
10328 {
10329 case DW_TAG_padding:
10330 break;
10331 case DW_TAG_compile_unit:
10332 case DW_TAG_partial_unit:
10333 read_file_scope (die, cu);
10334 break;
10335 case DW_TAG_type_unit:
10336 read_type_unit_scope (die, cu);
10337 break;
10338 case DW_TAG_subprogram:
10339 /* Nested subprograms in Fortran get a prefix. */
10340 if (cu->language == language_fortran
10341 && die->parent != NULL
10342 && die->parent->tag == DW_TAG_subprogram)
10343 cu->processing_has_namespace_info = true;
10344 /* Fall through. */
10345 case DW_TAG_inlined_subroutine:
10346 read_func_scope (die, cu);
10347 break;
10348 case DW_TAG_lexical_block:
10349 case DW_TAG_try_block:
10350 case DW_TAG_catch_block:
10351 read_lexical_block_scope (die, cu);
10352 break;
10353 case DW_TAG_call_site:
10354 case DW_TAG_GNU_call_site:
10355 read_call_site_scope (die, cu);
10356 break;
10357 case DW_TAG_class_type:
10358 case DW_TAG_interface_type:
10359 case DW_TAG_structure_type:
10360 case DW_TAG_union_type:
10361 process_structure_scope (die, cu);
10362 break;
10363 case DW_TAG_enumeration_type:
10364 process_enumeration_scope (die, cu);
10365 break;
10366
10367 /* These dies have a type, but processing them does not create
10368 a symbol or recurse to process the children. Therefore we can
10369 read them on-demand through read_type_die. */
10370 case DW_TAG_subroutine_type:
10371 case DW_TAG_set_type:
10372 case DW_TAG_pointer_type:
10373 case DW_TAG_ptr_to_member_type:
10374 case DW_TAG_reference_type:
10375 case DW_TAG_rvalue_reference_type:
10376 case DW_TAG_string_type:
10377 break;
10378
10379 case DW_TAG_array_type:
10380 /* We only need to handle this case for Ada -- in other
10381 languages, it's normal for the compiler to emit a typedef
10382 instead. */
10383 if (cu->language != language_ada)
10384 break;
10385 /* FALLTHROUGH */
10386 case DW_TAG_base_type:
10387 case DW_TAG_subrange_type:
10388 case DW_TAG_typedef:
10389 /* Add a typedef symbol for the type definition, if it has a
10390 DW_AT_name. */
10391 new_symbol (die, read_type_die (die, cu), cu);
10392 break;
10393 case DW_TAG_common_block:
10394 read_common_block (die, cu);
10395 break;
10396 case DW_TAG_common_inclusion:
10397 break;
10398 case DW_TAG_namespace:
10399 cu->processing_has_namespace_info = true;
10400 read_namespace (die, cu);
10401 break;
10402 case DW_TAG_module:
10403 cu->processing_has_namespace_info = true;
10404 read_module (die, cu);
10405 break;
10406 case DW_TAG_imported_declaration:
10407 cu->processing_has_namespace_info = true;
10408 if (read_namespace_alias (die, cu))
10409 break;
10410 /* The declaration is not a global namespace alias. */
10411 /* Fall through. */
10412 case DW_TAG_imported_module:
10413 cu->processing_has_namespace_info = true;
10414 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10415 || cu->language != language_fortran))
10416 complaint (_("Tag '%s' has unexpected children"),
10417 dwarf_tag_name (die->tag));
10418 read_import_statement (die, cu);
10419 break;
10420
10421 case DW_TAG_imported_unit:
10422 process_imported_unit_die (die, cu);
10423 break;
10424
10425 case DW_TAG_variable:
10426 read_variable (die, cu);
10427 break;
10428
10429 default:
10430 new_symbol (die, NULL, cu);
10431 break;
10432 }
10433 }
10434 \f
10435 /* DWARF name computation. */
10436
10437 /* A helper function for dwarf2_compute_name which determines whether DIE
10438 needs to have the name of the scope prepended to the name listed in the
10439 die. */
10440
10441 static int
10442 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10443 {
10444 struct attribute *attr;
10445
10446 switch (die->tag)
10447 {
10448 case DW_TAG_namespace:
10449 case DW_TAG_typedef:
10450 case DW_TAG_class_type:
10451 case DW_TAG_interface_type:
10452 case DW_TAG_structure_type:
10453 case DW_TAG_union_type:
10454 case DW_TAG_enumeration_type:
10455 case DW_TAG_enumerator:
10456 case DW_TAG_subprogram:
10457 case DW_TAG_inlined_subroutine:
10458 case DW_TAG_member:
10459 case DW_TAG_imported_declaration:
10460 return 1;
10461
10462 case DW_TAG_variable:
10463 case DW_TAG_constant:
10464 /* We only need to prefix "globally" visible variables. These include
10465 any variable marked with DW_AT_external or any variable that
10466 lives in a namespace. [Variables in anonymous namespaces
10467 require prefixing, but they are not DW_AT_external.] */
10468
10469 if (dwarf2_attr (die, DW_AT_specification, cu))
10470 {
10471 struct dwarf2_cu *spec_cu = cu;
10472
10473 return die_needs_namespace (die_specification (die, &spec_cu),
10474 spec_cu);
10475 }
10476
10477 attr = dwarf2_attr (die, DW_AT_external, cu);
10478 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10479 && die->parent->tag != DW_TAG_module)
10480 return 0;
10481 /* A variable in a lexical block of some kind does not need a
10482 namespace, even though in C++ such variables may be external
10483 and have a mangled name. */
10484 if (die->parent->tag == DW_TAG_lexical_block
10485 || die->parent->tag == DW_TAG_try_block
10486 || die->parent->tag == DW_TAG_catch_block
10487 || die->parent->tag == DW_TAG_subprogram)
10488 return 0;
10489 return 1;
10490
10491 default:
10492 return 0;
10493 }
10494 }
10495
10496 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10497 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10498 defined for the given DIE. */
10499
10500 static struct attribute *
10501 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10502 {
10503 struct attribute *attr;
10504
10505 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10506 if (attr == NULL)
10507 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10508
10509 return attr;
10510 }
10511
10512 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10513 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10514 defined for the given DIE. */
10515
10516 static const char *
10517 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10518 {
10519 const char *linkage_name;
10520
10521 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10522 if (linkage_name == NULL)
10523 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10524
10525 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10526 See https://github.com/rust-lang/rust/issues/32925. */
10527 if (cu->language == language_rust && linkage_name != NULL
10528 && strchr (linkage_name, '{') != NULL)
10529 linkage_name = NULL;
10530
10531 return linkage_name;
10532 }
10533
10534 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10535 compute the physname for the object, which include a method's:
10536 - formal parameters (C++),
10537 - receiver type (Go),
10538
10539 The term "physname" is a bit confusing.
10540 For C++, for example, it is the demangled name.
10541 For Go, for example, it's the mangled name.
10542
10543 For Ada, return the DIE's linkage name rather than the fully qualified
10544 name. PHYSNAME is ignored..
10545
10546 The result is allocated on the objfile->per_bfd's obstack and
10547 canonicalized. */
10548
10549 static const char *
10550 dwarf2_compute_name (const char *name,
10551 struct die_info *die, struct dwarf2_cu *cu,
10552 int physname)
10553 {
10554 struct objfile *objfile = cu->per_objfile->objfile;
10555
10556 if (name == NULL)
10557 name = dwarf2_name (die, cu);
10558
10559 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10560 but otherwise compute it by typename_concat inside GDB.
10561 FIXME: Actually this is not really true, or at least not always true.
10562 It's all very confusing. compute_and_set_names doesn't try to demangle
10563 Fortran names because there is no mangling standard. So new_symbol
10564 will set the demangled name to the result of dwarf2_full_name, and it is
10565 the demangled name that GDB uses if it exists. */
10566 if (cu->language == language_ada
10567 || (cu->language == language_fortran && physname))
10568 {
10569 /* For Ada unit, we prefer the linkage name over the name, as
10570 the former contains the exported name, which the user expects
10571 to be able to reference. Ideally, we want the user to be able
10572 to reference this entity using either natural or linkage name,
10573 but we haven't started looking at this enhancement yet. */
10574 const char *linkage_name = dw2_linkage_name (die, cu);
10575
10576 if (linkage_name != NULL)
10577 return linkage_name;
10578 }
10579
10580 /* These are the only languages we know how to qualify names in. */
10581 if (name != NULL
10582 && (cu->language == language_cplus
10583 || cu->language == language_fortran || cu->language == language_d
10584 || cu->language == language_rust))
10585 {
10586 if (die_needs_namespace (die, cu))
10587 {
10588 const char *prefix;
10589 const char *canonical_name = NULL;
10590
10591 string_file buf;
10592
10593 prefix = determine_prefix (die, cu);
10594 if (*prefix != '\0')
10595 {
10596 gdb::unique_xmalloc_ptr<char> prefixed_name
10597 (typename_concat (NULL, prefix, name, physname, cu));
10598
10599 buf.puts (prefixed_name.get ());
10600 }
10601 else
10602 buf.puts (name);
10603
10604 /* Template parameters may be specified in the DIE's DW_AT_name, or
10605 as children with DW_TAG_template_type_param or
10606 DW_TAG_value_type_param. If the latter, add them to the name
10607 here. If the name already has template parameters, then
10608 skip this step; some versions of GCC emit both, and
10609 it is more efficient to use the pre-computed name.
10610
10611 Something to keep in mind about this process: it is very
10612 unlikely, or in some cases downright impossible, to produce
10613 something that will match the mangled name of a function.
10614 If the definition of the function has the same debug info,
10615 we should be able to match up with it anyway. But fallbacks
10616 using the minimal symbol, for instance to find a method
10617 implemented in a stripped copy of libstdc++, will not work.
10618 If we do not have debug info for the definition, we will have to
10619 match them up some other way.
10620
10621 When we do name matching there is a related problem with function
10622 templates; two instantiated function templates are allowed to
10623 differ only by their return types, which we do not add here. */
10624
10625 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10626 {
10627 struct attribute *attr;
10628 struct die_info *child;
10629 int first = 1;
10630 const language_defn *cplus_lang = language_def (cu->language);
10631
10632 die->building_fullname = 1;
10633
10634 for (child = die->child; child != NULL; child = child->sibling)
10635 {
10636 struct type *type;
10637 LONGEST value;
10638 const gdb_byte *bytes;
10639 struct dwarf2_locexpr_baton *baton;
10640 struct value *v;
10641
10642 if (child->tag != DW_TAG_template_type_param
10643 && child->tag != DW_TAG_template_value_param)
10644 continue;
10645
10646 if (first)
10647 {
10648 buf.puts ("<");
10649 first = 0;
10650 }
10651 else
10652 buf.puts (", ");
10653
10654 attr = dwarf2_attr (child, DW_AT_type, cu);
10655 if (attr == NULL)
10656 {
10657 complaint (_("template parameter missing DW_AT_type"));
10658 buf.puts ("UNKNOWN_TYPE");
10659 continue;
10660 }
10661 type = die_type (child, cu);
10662
10663 if (child->tag == DW_TAG_template_type_param)
10664 {
10665 cplus_lang->print_type (type, "", &buf, -1, 0,
10666 &type_print_raw_options);
10667 continue;
10668 }
10669
10670 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10671 if (attr == NULL)
10672 {
10673 complaint (_("template parameter missing "
10674 "DW_AT_const_value"));
10675 buf.puts ("UNKNOWN_VALUE");
10676 continue;
10677 }
10678
10679 dwarf2_const_value_attr (attr, type, name,
10680 &cu->comp_unit_obstack, cu,
10681 &value, &bytes, &baton);
10682
10683 if (type->has_no_signedness ())
10684 /* GDB prints characters as NUMBER 'CHAR'. If that's
10685 changed, this can use value_print instead. */
10686 cplus_lang->printchar (value, type, &buf);
10687 else
10688 {
10689 struct value_print_options opts;
10690
10691 if (baton != NULL)
10692 v = dwarf2_evaluate_loc_desc (type, NULL,
10693 baton->data,
10694 baton->size,
10695 baton->per_cu,
10696 baton->per_objfile);
10697 else if (bytes != NULL)
10698 {
10699 v = allocate_value (type);
10700 memcpy (value_contents_writeable (v), bytes,
10701 TYPE_LENGTH (type));
10702 }
10703 else
10704 v = value_from_longest (type, value);
10705
10706 /* Specify decimal so that we do not depend on
10707 the radix. */
10708 get_formatted_print_options (&opts, 'd');
10709 opts.raw = 1;
10710 value_print (v, &buf, &opts);
10711 release_value (v);
10712 }
10713 }
10714
10715 die->building_fullname = 0;
10716
10717 if (!first)
10718 {
10719 /* Close the argument list, with a space if necessary
10720 (nested templates). */
10721 if (!buf.empty () && buf.string ().back () == '>')
10722 buf.puts (" >");
10723 else
10724 buf.puts (">");
10725 }
10726 }
10727
10728 /* For C++ methods, append formal parameter type
10729 information, if PHYSNAME. */
10730
10731 if (physname && die->tag == DW_TAG_subprogram
10732 && cu->language == language_cplus)
10733 {
10734 struct type *type = read_type_die (die, cu);
10735
10736 c_type_print_args (type, &buf, 1, cu->language,
10737 &type_print_raw_options);
10738
10739 if (cu->language == language_cplus)
10740 {
10741 /* Assume that an artificial first parameter is
10742 "this", but do not crash if it is not. RealView
10743 marks unnamed (and thus unused) parameters as
10744 artificial; there is no way to differentiate
10745 the two cases. */
10746 if (type->num_fields () > 0
10747 && TYPE_FIELD_ARTIFICIAL (type, 0)
10748 && type->field (0).type ()->code () == TYPE_CODE_PTR
10749 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
10750 buf.puts (" const");
10751 }
10752 }
10753
10754 const std::string &intermediate_name = buf.string ();
10755
10756 if (cu->language == language_cplus)
10757 canonical_name
10758 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10759 objfile);
10760
10761 /* If we only computed INTERMEDIATE_NAME, or if
10762 INTERMEDIATE_NAME is already canonical, then we need to
10763 intern it. */
10764 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10765 name = objfile->intern (intermediate_name);
10766 else
10767 name = canonical_name;
10768 }
10769 }
10770
10771 return name;
10772 }
10773
10774 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10775 If scope qualifiers are appropriate they will be added. The result
10776 will be allocated on the storage_obstack, or NULL if the DIE does
10777 not have a name. NAME may either be from a previous call to
10778 dwarf2_name or NULL.
10779
10780 The output string will be canonicalized (if C++). */
10781
10782 static const char *
10783 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10784 {
10785 return dwarf2_compute_name (name, die, cu, 0);
10786 }
10787
10788 /* Construct a physname for the given DIE in CU. NAME may either be
10789 from a previous call to dwarf2_name or NULL. The result will be
10790 allocated on the objfile_objstack or NULL if the DIE does not have a
10791 name.
10792
10793 The output string will be canonicalized (if C++). */
10794
10795 static const char *
10796 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10797 {
10798 struct objfile *objfile = cu->per_objfile->objfile;
10799 const char *retval, *mangled = NULL, *canon = NULL;
10800 int need_copy = 1;
10801
10802 /* In this case dwarf2_compute_name is just a shortcut not building anything
10803 on its own. */
10804 if (!die_needs_namespace (die, cu))
10805 return dwarf2_compute_name (name, die, cu, 1);
10806
10807 if (cu->language != language_rust)
10808 mangled = dw2_linkage_name (die, cu);
10809
10810 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10811 has computed. */
10812 gdb::unique_xmalloc_ptr<char> demangled;
10813 if (mangled != NULL)
10814 {
10815
10816 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
10817 {
10818 /* Do nothing (do not demangle the symbol name). */
10819 }
10820 else
10821 {
10822 /* Use DMGL_RET_DROP for C++ template functions to suppress
10823 their return type. It is easier for GDB users to search
10824 for such functions as `name(params)' than `long name(params)'.
10825 In such case the minimal symbol names do not match the full
10826 symbol names but for template functions there is never a need
10827 to look up their definition from their declaration so
10828 the only disadvantage remains the minimal symbol variant
10829 `long name(params)' does not have the proper inferior type. */
10830 demangled.reset (gdb_demangle (mangled,
10831 (DMGL_PARAMS | DMGL_ANSI
10832 | DMGL_RET_DROP)));
10833 }
10834 if (demangled)
10835 canon = demangled.get ();
10836 else
10837 {
10838 canon = mangled;
10839 need_copy = 0;
10840 }
10841 }
10842
10843 if (canon == NULL || check_physname)
10844 {
10845 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10846
10847 if (canon != NULL && strcmp (physname, canon) != 0)
10848 {
10849 /* It may not mean a bug in GDB. The compiler could also
10850 compute DW_AT_linkage_name incorrectly. But in such case
10851 GDB would need to be bug-to-bug compatible. */
10852
10853 complaint (_("Computed physname <%s> does not match demangled <%s> "
10854 "(from linkage <%s>) - DIE at %s [in module %s]"),
10855 physname, canon, mangled, sect_offset_str (die->sect_off),
10856 objfile_name (objfile));
10857
10858 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10859 is available here - over computed PHYSNAME. It is safer
10860 against both buggy GDB and buggy compilers. */
10861
10862 retval = canon;
10863 }
10864 else
10865 {
10866 retval = physname;
10867 need_copy = 0;
10868 }
10869 }
10870 else
10871 retval = canon;
10872
10873 if (need_copy)
10874 retval = objfile->intern (retval);
10875
10876 return retval;
10877 }
10878
10879 /* Inspect DIE in CU for a namespace alias. If one exists, record
10880 a new symbol for it.
10881
10882 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10883
10884 static int
10885 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10886 {
10887 struct attribute *attr;
10888
10889 /* If the die does not have a name, this is not a namespace
10890 alias. */
10891 attr = dwarf2_attr (die, DW_AT_name, cu);
10892 if (attr != NULL)
10893 {
10894 int num;
10895 struct die_info *d = die;
10896 struct dwarf2_cu *imported_cu = cu;
10897
10898 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10899 keep inspecting DIEs until we hit the underlying import. */
10900 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10901 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10902 {
10903 attr = dwarf2_attr (d, DW_AT_import, cu);
10904 if (attr == NULL)
10905 break;
10906
10907 d = follow_die_ref (d, attr, &imported_cu);
10908 if (d->tag != DW_TAG_imported_declaration)
10909 break;
10910 }
10911
10912 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10913 {
10914 complaint (_("DIE at %s has too many recursively imported "
10915 "declarations"), sect_offset_str (d->sect_off));
10916 return 0;
10917 }
10918
10919 if (attr != NULL)
10920 {
10921 struct type *type;
10922 sect_offset sect_off = attr->get_ref_die_offset ();
10923
10924 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10925 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10926 {
10927 /* This declaration is a global namespace alias. Add
10928 a symbol for it whose type is the aliased namespace. */
10929 new_symbol (die, type, cu);
10930 return 1;
10931 }
10932 }
10933 }
10934
10935 return 0;
10936 }
10937
10938 /* Return the using directives repository (global or local?) to use in the
10939 current context for CU.
10940
10941 For Ada, imported declarations can materialize renamings, which *may* be
10942 global. However it is impossible (for now?) in DWARF to distinguish
10943 "external" imported declarations and "static" ones. As all imported
10944 declarations seem to be static in all other languages, make them all CU-wide
10945 global only in Ada. */
10946
10947 static struct using_direct **
10948 using_directives (struct dwarf2_cu *cu)
10949 {
10950 if (cu->language == language_ada
10951 && cu->get_builder ()->outermost_context_p ())
10952 return cu->get_builder ()->get_global_using_directives ();
10953 else
10954 return cu->get_builder ()->get_local_using_directives ();
10955 }
10956
10957 /* Read the import statement specified by the given die and record it. */
10958
10959 static void
10960 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10961 {
10962 struct objfile *objfile = cu->per_objfile->objfile;
10963 struct attribute *import_attr;
10964 struct die_info *imported_die, *child_die;
10965 struct dwarf2_cu *imported_cu;
10966 const char *imported_name;
10967 const char *imported_name_prefix;
10968 const char *canonical_name;
10969 const char *import_alias;
10970 const char *imported_declaration = NULL;
10971 const char *import_prefix;
10972 std::vector<const char *> excludes;
10973
10974 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10975 if (import_attr == NULL)
10976 {
10977 complaint (_("Tag '%s' has no DW_AT_import"),
10978 dwarf_tag_name (die->tag));
10979 return;
10980 }
10981
10982 imported_cu = cu;
10983 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10984 imported_name = dwarf2_name (imported_die, imported_cu);
10985 if (imported_name == NULL)
10986 {
10987 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10988
10989 The import in the following code:
10990 namespace A
10991 {
10992 typedef int B;
10993 }
10994
10995 int main ()
10996 {
10997 using A::B;
10998 B b;
10999 return b;
11000 }
11001
11002 ...
11003 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11004 <52> DW_AT_decl_file : 1
11005 <53> DW_AT_decl_line : 6
11006 <54> DW_AT_import : <0x75>
11007 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11008 <59> DW_AT_name : B
11009 <5b> DW_AT_decl_file : 1
11010 <5c> DW_AT_decl_line : 2
11011 <5d> DW_AT_type : <0x6e>
11012 ...
11013 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11014 <76> DW_AT_byte_size : 4
11015 <77> DW_AT_encoding : 5 (signed)
11016
11017 imports the wrong die ( 0x75 instead of 0x58 ).
11018 This case will be ignored until the gcc bug is fixed. */
11019 return;
11020 }
11021
11022 /* Figure out the local name after import. */
11023 import_alias = dwarf2_name (die, cu);
11024
11025 /* Figure out where the statement is being imported to. */
11026 import_prefix = determine_prefix (die, cu);
11027
11028 /* Figure out what the scope of the imported die is and prepend it
11029 to the name of the imported die. */
11030 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11031
11032 if (imported_die->tag != DW_TAG_namespace
11033 && imported_die->tag != DW_TAG_module)
11034 {
11035 imported_declaration = imported_name;
11036 canonical_name = imported_name_prefix;
11037 }
11038 else if (strlen (imported_name_prefix) > 0)
11039 canonical_name = obconcat (&objfile->objfile_obstack,
11040 imported_name_prefix,
11041 (cu->language == language_d ? "." : "::"),
11042 imported_name, (char *) NULL);
11043 else
11044 canonical_name = imported_name;
11045
11046 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11047 for (child_die = die->child; child_die && child_die->tag;
11048 child_die = child_die->sibling)
11049 {
11050 /* DWARF-4: A Fortran use statement with a “rename list” may be
11051 represented by an imported module entry with an import attribute
11052 referring to the module and owned entries corresponding to those
11053 entities that are renamed as part of being imported. */
11054
11055 if (child_die->tag != DW_TAG_imported_declaration)
11056 {
11057 complaint (_("child DW_TAG_imported_declaration expected "
11058 "- DIE at %s [in module %s]"),
11059 sect_offset_str (child_die->sect_off),
11060 objfile_name (objfile));
11061 continue;
11062 }
11063
11064 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11065 if (import_attr == NULL)
11066 {
11067 complaint (_("Tag '%s' has no DW_AT_import"),
11068 dwarf_tag_name (child_die->tag));
11069 continue;
11070 }
11071
11072 imported_cu = cu;
11073 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11074 &imported_cu);
11075 imported_name = dwarf2_name (imported_die, imported_cu);
11076 if (imported_name == NULL)
11077 {
11078 complaint (_("child DW_TAG_imported_declaration has unknown "
11079 "imported name - DIE at %s [in module %s]"),
11080 sect_offset_str (child_die->sect_off),
11081 objfile_name (objfile));
11082 continue;
11083 }
11084
11085 excludes.push_back (imported_name);
11086
11087 process_die (child_die, cu);
11088 }
11089
11090 add_using_directive (using_directives (cu),
11091 import_prefix,
11092 canonical_name,
11093 import_alias,
11094 imported_declaration,
11095 excludes,
11096 0,
11097 &objfile->objfile_obstack);
11098 }
11099
11100 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11101 types, but gives them a size of zero. Starting with version 14,
11102 ICC is compatible with GCC. */
11103
11104 static bool
11105 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11106 {
11107 if (!cu->checked_producer)
11108 check_producer (cu);
11109
11110 return cu->producer_is_icc_lt_14;
11111 }
11112
11113 /* ICC generates a DW_AT_type for C void functions. This was observed on
11114 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11115 which says that void functions should not have a DW_AT_type. */
11116
11117 static bool
11118 producer_is_icc (struct dwarf2_cu *cu)
11119 {
11120 if (!cu->checked_producer)
11121 check_producer (cu);
11122
11123 return cu->producer_is_icc;
11124 }
11125
11126 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11127 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11128 this, it was first present in GCC release 4.3.0. */
11129
11130 static bool
11131 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11132 {
11133 if (!cu->checked_producer)
11134 check_producer (cu);
11135
11136 return cu->producer_is_gcc_lt_4_3;
11137 }
11138
11139 static file_and_directory
11140 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11141 {
11142 file_and_directory res;
11143
11144 /* Find the filename. Do not use dwarf2_name here, since the filename
11145 is not a source language identifier. */
11146 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11147 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11148
11149 if (res.comp_dir == NULL
11150 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11151 && IS_ABSOLUTE_PATH (res.name))
11152 {
11153 res.comp_dir_storage = ldirname (res.name);
11154 if (!res.comp_dir_storage.empty ())
11155 res.comp_dir = res.comp_dir_storage.c_str ();
11156 }
11157 if (res.comp_dir != NULL)
11158 {
11159 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11160 directory, get rid of it. */
11161 const char *cp = strchr (res.comp_dir, ':');
11162
11163 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11164 res.comp_dir = cp + 1;
11165 }
11166
11167 if (res.name == NULL)
11168 res.name = "<unknown>";
11169
11170 return res;
11171 }
11172
11173 /* Handle DW_AT_stmt_list for a compilation unit.
11174 DIE is the DW_TAG_compile_unit die for CU.
11175 COMP_DIR is the compilation directory. LOWPC is passed to
11176 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11177
11178 static void
11179 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11180 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11181 {
11182 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11183 struct attribute *attr;
11184 struct line_header line_header_local;
11185 hashval_t line_header_local_hash;
11186 void **slot;
11187 int decode_mapping;
11188
11189 gdb_assert (! cu->per_cu->is_debug_types);
11190
11191 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11192 if (attr == NULL || !attr->form_is_unsigned ())
11193 return;
11194
11195 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
11196
11197 /* The line header hash table is only created if needed (it exists to
11198 prevent redundant reading of the line table for partial_units).
11199 If we're given a partial_unit, we'll need it. If we're given a
11200 compile_unit, then use the line header hash table if it's already
11201 created, but don't create one just yet. */
11202
11203 if (per_objfile->line_header_hash == NULL
11204 && die->tag == DW_TAG_partial_unit)
11205 {
11206 per_objfile->line_header_hash
11207 .reset (htab_create_alloc (127, line_header_hash_voidp,
11208 line_header_eq_voidp,
11209 free_line_header_voidp,
11210 xcalloc, xfree));
11211 }
11212
11213 line_header_local.sect_off = line_offset;
11214 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11215 line_header_local_hash = line_header_hash (&line_header_local);
11216 if (per_objfile->line_header_hash != NULL)
11217 {
11218 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11219 &line_header_local,
11220 line_header_local_hash, NO_INSERT);
11221
11222 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11223 is not present in *SLOT (since if there is something in *SLOT then
11224 it will be for a partial_unit). */
11225 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11226 {
11227 gdb_assert (*slot != NULL);
11228 cu->line_header = (struct line_header *) *slot;
11229 return;
11230 }
11231 }
11232
11233 /* dwarf_decode_line_header does not yet provide sufficient information.
11234 We always have to call also dwarf_decode_lines for it. */
11235 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11236 if (lh == NULL)
11237 return;
11238
11239 cu->line_header = lh.release ();
11240 cu->line_header_die_owner = die;
11241
11242 if (per_objfile->line_header_hash == NULL)
11243 slot = NULL;
11244 else
11245 {
11246 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11247 &line_header_local,
11248 line_header_local_hash, INSERT);
11249 gdb_assert (slot != NULL);
11250 }
11251 if (slot != NULL && *slot == NULL)
11252 {
11253 /* This newly decoded line number information unit will be owned
11254 by line_header_hash hash table. */
11255 *slot = cu->line_header;
11256 cu->line_header_die_owner = NULL;
11257 }
11258 else
11259 {
11260 /* We cannot free any current entry in (*slot) as that struct line_header
11261 may be already used by multiple CUs. Create only temporary decoded
11262 line_header for this CU - it may happen at most once for each line
11263 number information unit. And if we're not using line_header_hash
11264 then this is what we want as well. */
11265 gdb_assert (die->tag != DW_TAG_partial_unit);
11266 }
11267 decode_mapping = (die->tag != DW_TAG_partial_unit);
11268 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11269 decode_mapping);
11270
11271 }
11272
11273 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11274
11275 static void
11276 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11277 {
11278 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11279 struct objfile *objfile = per_objfile->objfile;
11280 struct gdbarch *gdbarch = objfile->arch ();
11281 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11282 CORE_ADDR highpc = ((CORE_ADDR) 0);
11283 struct attribute *attr;
11284 struct die_info *child_die;
11285 CORE_ADDR baseaddr;
11286
11287 prepare_one_comp_unit (cu, die, cu->language);
11288 baseaddr = objfile->text_section_offset ();
11289
11290 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11291
11292 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11293 from finish_block. */
11294 if (lowpc == ((CORE_ADDR) -1))
11295 lowpc = highpc;
11296 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11297
11298 file_and_directory fnd = find_file_and_directory (die, cu);
11299
11300 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11301 standardised yet. As a workaround for the language detection we fall
11302 back to the DW_AT_producer string. */
11303 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11304 cu->language = language_opencl;
11305
11306 /* Similar hack for Go. */
11307 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11308 set_cu_language (DW_LANG_Go, cu);
11309
11310 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11311
11312 /* Decode line number information if present. We do this before
11313 processing child DIEs, so that the line header table is available
11314 for DW_AT_decl_file. */
11315 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11316
11317 /* Process all dies in compilation unit. */
11318 if (die->child != NULL)
11319 {
11320 child_die = die->child;
11321 while (child_die && child_die->tag)
11322 {
11323 process_die (child_die, cu);
11324 child_die = child_die->sibling;
11325 }
11326 }
11327
11328 /* Decode macro information, if present. Dwarf 2 macro information
11329 refers to information in the line number info statement program
11330 header, so we can only read it if we've read the header
11331 successfully. */
11332 attr = dwarf2_attr (die, DW_AT_macros, cu);
11333 if (attr == NULL)
11334 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11335 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
11336 {
11337 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11338 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11339
11340 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
11341 }
11342 else
11343 {
11344 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11345 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
11346 {
11347 unsigned int macro_offset = attr->as_unsigned ();
11348
11349 dwarf_decode_macros (cu, macro_offset, 0);
11350 }
11351 }
11352 }
11353
11354 void
11355 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11356 {
11357 struct type_unit_group *tu_group;
11358 int first_time;
11359 struct attribute *attr;
11360 unsigned int i;
11361 struct signatured_type *sig_type;
11362
11363 gdb_assert (per_cu->is_debug_types);
11364 sig_type = (struct signatured_type *) per_cu;
11365
11366 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11367
11368 /* If we're using .gdb_index (includes -readnow) then
11369 per_cu->type_unit_group may not have been set up yet. */
11370 if (sig_type->type_unit_group == NULL)
11371 sig_type->type_unit_group = get_type_unit_group (this, attr);
11372 tu_group = sig_type->type_unit_group;
11373
11374 /* If we've already processed this stmt_list there's no real need to
11375 do it again, we could fake it and just recreate the part we need
11376 (file name,index -> symtab mapping). If data shows this optimization
11377 is useful we can do it then. */
11378 type_unit_group_unshareable *tug_unshare
11379 = per_objfile->get_type_unit_group_unshareable (tu_group);
11380 first_time = tug_unshare->compunit_symtab == NULL;
11381
11382 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11383 debug info. */
11384 line_header_up lh;
11385 if (attr != NULL && attr->form_is_unsigned ())
11386 {
11387 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
11388 lh = dwarf_decode_line_header (line_offset, this);
11389 }
11390 if (lh == NULL)
11391 {
11392 if (first_time)
11393 start_symtab ("", NULL, 0);
11394 else
11395 {
11396 gdb_assert (tug_unshare->symtabs == NULL);
11397 gdb_assert (m_builder == nullptr);
11398 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11399 m_builder.reset (new struct buildsym_compunit
11400 (COMPUNIT_OBJFILE (cust), "",
11401 COMPUNIT_DIRNAME (cust),
11402 compunit_language (cust),
11403 0, cust));
11404 list_in_scope = get_builder ()->get_file_symbols ();
11405 }
11406 return;
11407 }
11408
11409 line_header = lh.release ();
11410 line_header_die_owner = die;
11411
11412 if (first_time)
11413 {
11414 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11415
11416 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11417 still initializing it, and our caller (a few levels up)
11418 process_full_type_unit still needs to know if this is the first
11419 time. */
11420
11421 tug_unshare->symtabs
11422 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11423 struct symtab *, line_header->file_names_size ());
11424
11425 auto &file_names = line_header->file_names ();
11426 for (i = 0; i < file_names.size (); ++i)
11427 {
11428 file_entry &fe = file_names[i];
11429 dwarf2_start_subfile (this, fe.name,
11430 fe.include_dir (line_header));
11431 buildsym_compunit *b = get_builder ();
11432 if (b->get_current_subfile ()->symtab == NULL)
11433 {
11434 /* NOTE: start_subfile will recognize when it's been
11435 passed a file it has already seen. So we can't
11436 assume there's a simple mapping from
11437 cu->line_header->file_names to subfiles, plus
11438 cu->line_header->file_names may contain dups. */
11439 b->get_current_subfile ()->symtab
11440 = allocate_symtab (cust, b->get_current_subfile ()->name);
11441 }
11442
11443 fe.symtab = b->get_current_subfile ()->symtab;
11444 tug_unshare->symtabs[i] = fe.symtab;
11445 }
11446 }
11447 else
11448 {
11449 gdb_assert (m_builder == nullptr);
11450 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11451 m_builder.reset (new struct buildsym_compunit
11452 (COMPUNIT_OBJFILE (cust), "",
11453 COMPUNIT_DIRNAME (cust),
11454 compunit_language (cust),
11455 0, cust));
11456 list_in_scope = get_builder ()->get_file_symbols ();
11457
11458 auto &file_names = line_header->file_names ();
11459 for (i = 0; i < file_names.size (); ++i)
11460 {
11461 file_entry &fe = file_names[i];
11462 fe.symtab = tug_unshare->symtabs[i];
11463 }
11464 }
11465
11466 /* The main symtab is allocated last. Type units don't have DW_AT_name
11467 so they don't have a "real" (so to speak) symtab anyway.
11468 There is later code that will assign the main symtab to all symbols
11469 that don't have one. We need to handle the case of a symbol with a
11470 missing symtab (DW_AT_decl_file) anyway. */
11471 }
11472
11473 /* Process DW_TAG_type_unit.
11474 For TUs we want to skip the first top level sibling if it's not the
11475 actual type being defined by this TU. In this case the first top
11476 level sibling is there to provide context only. */
11477
11478 static void
11479 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11480 {
11481 struct die_info *child_die;
11482
11483 prepare_one_comp_unit (cu, die, language_minimal);
11484
11485 /* Initialize (or reinitialize) the machinery for building symtabs.
11486 We do this before processing child DIEs, so that the line header table
11487 is available for DW_AT_decl_file. */
11488 cu->setup_type_unit_groups (die);
11489
11490 if (die->child != NULL)
11491 {
11492 child_die = die->child;
11493 while (child_die && child_die->tag)
11494 {
11495 process_die (child_die, cu);
11496 child_die = child_die->sibling;
11497 }
11498 }
11499 }
11500 \f
11501 /* DWO/DWP files.
11502
11503 http://gcc.gnu.org/wiki/DebugFission
11504 http://gcc.gnu.org/wiki/DebugFissionDWP
11505
11506 To simplify handling of both DWO files ("object" files with the DWARF info)
11507 and DWP files (a file with the DWOs packaged up into one file), we treat
11508 DWP files as having a collection of virtual DWO files. */
11509
11510 static hashval_t
11511 hash_dwo_file (const void *item)
11512 {
11513 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11514 hashval_t hash;
11515
11516 hash = htab_hash_string (dwo_file->dwo_name);
11517 if (dwo_file->comp_dir != NULL)
11518 hash += htab_hash_string (dwo_file->comp_dir);
11519 return hash;
11520 }
11521
11522 static int
11523 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11524 {
11525 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11526 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11527
11528 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11529 return 0;
11530 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11531 return lhs->comp_dir == rhs->comp_dir;
11532 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11533 }
11534
11535 /* Allocate a hash table for DWO files. */
11536
11537 static htab_up
11538 allocate_dwo_file_hash_table ()
11539 {
11540 auto delete_dwo_file = [] (void *item)
11541 {
11542 struct dwo_file *dwo_file = (struct dwo_file *) item;
11543
11544 delete dwo_file;
11545 };
11546
11547 return htab_up (htab_create_alloc (41,
11548 hash_dwo_file,
11549 eq_dwo_file,
11550 delete_dwo_file,
11551 xcalloc, xfree));
11552 }
11553
11554 /* Lookup DWO file DWO_NAME. */
11555
11556 static void **
11557 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
11558 const char *dwo_name,
11559 const char *comp_dir)
11560 {
11561 struct dwo_file find_entry;
11562 void **slot;
11563
11564 if (per_objfile->per_bfd->dwo_files == NULL)
11565 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11566
11567 find_entry.dwo_name = dwo_name;
11568 find_entry.comp_dir = comp_dir;
11569 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
11570 INSERT);
11571
11572 return slot;
11573 }
11574
11575 static hashval_t
11576 hash_dwo_unit (const void *item)
11577 {
11578 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11579
11580 /* This drops the top 32 bits of the id, but is ok for a hash. */
11581 return dwo_unit->signature;
11582 }
11583
11584 static int
11585 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11586 {
11587 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11588 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11589
11590 /* The signature is assumed to be unique within the DWO file.
11591 So while object file CU dwo_id's always have the value zero,
11592 that's OK, assuming each object file DWO file has only one CU,
11593 and that's the rule for now. */
11594 return lhs->signature == rhs->signature;
11595 }
11596
11597 /* Allocate a hash table for DWO CUs,TUs.
11598 There is one of these tables for each of CUs,TUs for each DWO file. */
11599
11600 static htab_up
11601 allocate_dwo_unit_table ()
11602 {
11603 /* Start out with a pretty small number.
11604 Generally DWO files contain only one CU and maybe some TUs. */
11605 return htab_up (htab_create_alloc (3,
11606 hash_dwo_unit,
11607 eq_dwo_unit,
11608 NULL, xcalloc, xfree));
11609 }
11610
11611 /* die_reader_func for create_dwo_cu. */
11612
11613 static void
11614 create_dwo_cu_reader (const struct die_reader_specs *reader,
11615 const gdb_byte *info_ptr,
11616 struct die_info *comp_unit_die,
11617 struct dwo_file *dwo_file,
11618 struct dwo_unit *dwo_unit)
11619 {
11620 struct dwarf2_cu *cu = reader->cu;
11621 sect_offset sect_off = cu->per_cu->sect_off;
11622 struct dwarf2_section_info *section = cu->per_cu->section;
11623
11624 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11625 if (!signature.has_value ())
11626 {
11627 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11628 " its dwo_id [in module %s]"),
11629 sect_offset_str (sect_off), dwo_file->dwo_name);
11630 return;
11631 }
11632
11633 dwo_unit->dwo_file = dwo_file;
11634 dwo_unit->signature = *signature;
11635 dwo_unit->section = section;
11636 dwo_unit->sect_off = sect_off;
11637 dwo_unit->length = cu->per_cu->length;
11638
11639 dwarf_read_debug_printf (" offset %s, dwo_id %s",
11640 sect_offset_str (sect_off),
11641 hex_string (dwo_unit->signature));
11642 }
11643
11644 /* Create the dwo_units for the CUs in a DWO_FILE.
11645 Note: This function processes DWO files only, not DWP files. */
11646
11647 static void
11648 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
11649 dwarf2_cu *cu, struct dwo_file &dwo_file,
11650 dwarf2_section_info &section, htab_up &cus_htab)
11651 {
11652 struct objfile *objfile = per_objfile->objfile;
11653 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
11654 const gdb_byte *info_ptr, *end_ptr;
11655
11656 section.read (objfile);
11657 info_ptr = section.buffer;
11658
11659 if (info_ptr == NULL)
11660 return;
11661
11662 dwarf_read_debug_printf ("Reading %s for %s:",
11663 section.get_name (),
11664 section.get_file_name ());
11665
11666 end_ptr = info_ptr + section.size;
11667 while (info_ptr < end_ptr)
11668 {
11669 struct dwarf2_per_cu_data per_cu;
11670 struct dwo_unit read_unit {};
11671 struct dwo_unit *dwo_unit;
11672 void **slot;
11673 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11674
11675 memset (&per_cu, 0, sizeof (per_cu));
11676 per_cu.per_bfd = per_bfd;
11677 per_cu.is_debug_types = 0;
11678 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11679 per_cu.section = &section;
11680
11681 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
11682 if (!reader.dummy_p)
11683 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11684 &dwo_file, &read_unit);
11685 info_ptr += per_cu.length;
11686
11687 // If the unit could not be parsed, skip it.
11688 if (read_unit.dwo_file == NULL)
11689 continue;
11690
11691 if (cus_htab == NULL)
11692 cus_htab = allocate_dwo_unit_table ();
11693
11694 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11695 struct dwo_unit);
11696 *dwo_unit = read_unit;
11697 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11698 gdb_assert (slot != NULL);
11699 if (*slot != NULL)
11700 {
11701 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11702 sect_offset dup_sect_off = dup_cu->sect_off;
11703
11704 complaint (_("debug cu entry at offset %s is duplicate to"
11705 " the entry at offset %s, signature %s"),
11706 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11707 hex_string (dwo_unit->signature));
11708 }
11709 *slot = (void *)dwo_unit;
11710 }
11711 }
11712
11713 /* DWP file .debug_{cu,tu}_index section format:
11714 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11715 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11716
11717 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11718 officially standard DWP format was published with DWARF v5 and is called
11719 Version 5. There are no versions 3 or 4.
11720
11721 DWP Version 1:
11722
11723 Both index sections have the same format, and serve to map a 64-bit
11724 signature to a set of section numbers. Each section begins with a header,
11725 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11726 indexes, and a pool of 32-bit section numbers. The index sections will be
11727 aligned at 8-byte boundaries in the file.
11728
11729 The index section header consists of:
11730
11731 V, 32 bit version number
11732 -, 32 bits unused
11733 N, 32 bit number of compilation units or type units in the index
11734 M, 32 bit number of slots in the hash table
11735
11736 Numbers are recorded using the byte order of the application binary.
11737
11738 The hash table begins at offset 16 in the section, and consists of an array
11739 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11740 order of the application binary). Unused slots in the hash table are 0.
11741 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11742
11743 The parallel table begins immediately after the hash table
11744 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11745 array of 32-bit indexes (using the byte order of the application binary),
11746 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11747 table contains a 32-bit index into the pool of section numbers. For unused
11748 hash table slots, the corresponding entry in the parallel table will be 0.
11749
11750 The pool of section numbers begins immediately following the hash table
11751 (at offset 16 + 12 * M from the beginning of the section). The pool of
11752 section numbers consists of an array of 32-bit words (using the byte order
11753 of the application binary). Each item in the array is indexed starting
11754 from 0. The hash table entry provides the index of the first section
11755 number in the set. Additional section numbers in the set follow, and the
11756 set is terminated by a 0 entry (section number 0 is not used in ELF).
11757
11758 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11759 section must be the first entry in the set, and the .debug_abbrev.dwo must
11760 be the second entry. Other members of the set may follow in any order.
11761
11762 ---
11763
11764 DWP Versions 2 and 5:
11765
11766 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
11767 and the entries in the index tables are now offsets into these sections.
11768 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11769 section.
11770
11771 Index Section Contents:
11772 Header
11773 Hash Table of Signatures dwp_hash_table.hash_table
11774 Parallel Table of Indices dwp_hash_table.unit_table
11775 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11776 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
11777
11778 The index section header consists of:
11779
11780 V, 32 bit version number
11781 L, 32 bit number of columns in the table of section offsets
11782 N, 32 bit number of compilation units or type units in the index
11783 M, 32 bit number of slots in the hash table
11784
11785 Numbers are recorded using the byte order of the application binary.
11786
11787 The hash table has the same format as version 1.
11788 The parallel table of indices has the same format as version 1,
11789 except that the entries are origin-1 indices into the table of sections
11790 offsets and the table of section sizes.
11791
11792 The table of offsets begins immediately following the parallel table
11793 (at offset 16 + 12 * M from the beginning of the section). The table is
11794 a two-dimensional array of 32-bit words (using the byte order of the
11795 application binary), with L columns and N+1 rows, in row-major order.
11796 Each row in the array is indexed starting from 0. The first row provides
11797 a key to the remaining rows: each column in this row provides an identifier
11798 for a debug section, and the offsets in the same column of subsequent rows
11799 refer to that section. The section identifiers for Version 2 are:
11800
11801 DW_SECT_INFO 1 .debug_info.dwo
11802 DW_SECT_TYPES 2 .debug_types.dwo
11803 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11804 DW_SECT_LINE 4 .debug_line.dwo
11805 DW_SECT_LOC 5 .debug_loc.dwo
11806 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11807 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11808 DW_SECT_MACRO 8 .debug_macro.dwo
11809
11810 The section identifiers for Version 5 are:
11811
11812 DW_SECT_INFO_V5 1 .debug_info.dwo
11813 DW_SECT_RESERVED_V5 2 --
11814 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11815 DW_SECT_LINE_V5 4 .debug_line.dwo
11816 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11817 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11818 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11819 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11820
11821 The offsets provided by the CU and TU index sections are the base offsets
11822 for the contributions made by each CU or TU to the corresponding section
11823 in the package file. Each CU and TU header contains an abbrev_offset
11824 field, used to find the abbreviations table for that CU or TU within the
11825 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11826 be interpreted as relative to the base offset given in the index section.
11827 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11828 should be interpreted as relative to the base offset for .debug_line.dwo,
11829 and offsets into other debug sections obtained from DWARF attributes should
11830 also be interpreted as relative to the corresponding base offset.
11831
11832 The table of sizes begins immediately following the table of offsets.
11833 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11834 with L columns and N rows, in row-major order. Each row in the array is
11835 indexed starting from 1 (row 0 is shared by the two tables).
11836
11837 ---
11838
11839 Hash table lookup is handled the same in version 1 and 2:
11840
11841 We assume that N and M will not exceed 2^32 - 1.
11842 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11843
11844 Given a 64-bit compilation unit signature or a type signature S, an entry
11845 in the hash table is located as follows:
11846
11847 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11848 the low-order k bits all set to 1.
11849
11850 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11851
11852 3) If the hash table entry at index H matches the signature, use that
11853 entry. If the hash table entry at index H is unused (all zeroes),
11854 terminate the search: the signature is not present in the table.
11855
11856 4) Let H = (H + H') modulo M. Repeat at Step 3.
11857
11858 Because M > N and H' and M are relatively prime, the search is guaranteed
11859 to stop at an unused slot or find the match. */
11860
11861 /* Create a hash table to map DWO IDs to their CU/TU entry in
11862 .debug_{info,types}.dwo in DWP_FILE.
11863 Returns NULL if there isn't one.
11864 Note: This function processes DWP files only, not DWO files. */
11865
11866 static struct dwp_hash_table *
11867 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11868 struct dwp_file *dwp_file, int is_debug_types)
11869 {
11870 struct objfile *objfile = per_objfile->objfile;
11871 bfd *dbfd = dwp_file->dbfd.get ();
11872 const gdb_byte *index_ptr, *index_end;
11873 struct dwarf2_section_info *index;
11874 uint32_t version, nr_columns, nr_units, nr_slots;
11875 struct dwp_hash_table *htab;
11876
11877 if (is_debug_types)
11878 index = &dwp_file->sections.tu_index;
11879 else
11880 index = &dwp_file->sections.cu_index;
11881
11882 if (index->empty ())
11883 return NULL;
11884 index->read (objfile);
11885
11886 index_ptr = index->buffer;
11887 index_end = index_ptr + index->size;
11888
11889 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11890 For now it's safe to just read 4 bytes (particularly as it's difficult to
11891 tell if you're dealing with Version 5 before you've read the version). */
11892 version = read_4_bytes (dbfd, index_ptr);
11893 index_ptr += 4;
11894 if (version == 2 || version == 5)
11895 nr_columns = read_4_bytes (dbfd, index_ptr);
11896 else
11897 nr_columns = 0;
11898 index_ptr += 4;
11899 nr_units = read_4_bytes (dbfd, index_ptr);
11900 index_ptr += 4;
11901 nr_slots = read_4_bytes (dbfd, index_ptr);
11902 index_ptr += 4;
11903
11904 if (version != 1 && version != 2 && version != 5)
11905 {
11906 error (_("Dwarf Error: unsupported DWP file version (%s)"
11907 " [in module %s]"),
11908 pulongest (version), dwp_file->name);
11909 }
11910 if (nr_slots != (nr_slots & -nr_slots))
11911 {
11912 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11913 " is not power of 2 [in module %s]"),
11914 pulongest (nr_slots), dwp_file->name);
11915 }
11916
11917 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11918 htab->version = version;
11919 htab->nr_columns = nr_columns;
11920 htab->nr_units = nr_units;
11921 htab->nr_slots = nr_slots;
11922 htab->hash_table = index_ptr;
11923 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11924
11925 /* Exit early if the table is empty. */
11926 if (nr_slots == 0 || nr_units == 0
11927 || (version == 2 && nr_columns == 0)
11928 || (version == 5 && nr_columns == 0))
11929 {
11930 /* All must be zero. */
11931 if (nr_slots != 0 || nr_units != 0
11932 || (version == 2 && nr_columns != 0)
11933 || (version == 5 && nr_columns != 0))
11934 {
11935 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11936 " all zero [in modules %s]"),
11937 dwp_file->name);
11938 }
11939 return htab;
11940 }
11941
11942 if (version == 1)
11943 {
11944 htab->section_pool.v1.indices =
11945 htab->unit_table + sizeof (uint32_t) * nr_slots;
11946 /* It's harder to decide whether the section is too small in v1.
11947 V1 is deprecated anyway so we punt. */
11948 }
11949 else if (version == 2)
11950 {
11951 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11952 int *ids = htab->section_pool.v2.section_ids;
11953 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11954 /* Reverse map for error checking. */
11955 int ids_seen[DW_SECT_MAX + 1];
11956 int i;
11957
11958 if (nr_columns < 2)
11959 {
11960 error (_("Dwarf Error: bad DWP hash table, too few columns"
11961 " in section table [in module %s]"),
11962 dwp_file->name);
11963 }
11964 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11965 {
11966 error (_("Dwarf Error: bad DWP hash table, too many columns"
11967 " in section table [in module %s]"),
11968 dwp_file->name);
11969 }
11970 memset (ids, 255, sizeof_ids);
11971 memset (ids_seen, 255, sizeof (ids_seen));
11972 for (i = 0; i < nr_columns; ++i)
11973 {
11974 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11975
11976 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11977 {
11978 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11979 " in section table [in module %s]"),
11980 id, dwp_file->name);
11981 }
11982 if (ids_seen[id] != -1)
11983 {
11984 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11985 " id %d in section table [in module %s]"),
11986 id, dwp_file->name);
11987 }
11988 ids_seen[id] = i;
11989 ids[i] = id;
11990 }
11991 /* Must have exactly one info or types section. */
11992 if (((ids_seen[DW_SECT_INFO] != -1)
11993 + (ids_seen[DW_SECT_TYPES] != -1))
11994 != 1)
11995 {
11996 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11997 " DWO info/types section [in module %s]"),
11998 dwp_file->name);
11999 }
12000 /* Must have an abbrev section. */
12001 if (ids_seen[DW_SECT_ABBREV] == -1)
12002 {
12003 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12004 " section [in module %s]"),
12005 dwp_file->name);
12006 }
12007 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12008 htab->section_pool.v2.sizes =
12009 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12010 * nr_units * nr_columns);
12011 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12012 * nr_units * nr_columns))
12013 > index_end)
12014 {
12015 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12016 " [in module %s]"),
12017 dwp_file->name);
12018 }
12019 }
12020 else /* version == 5 */
12021 {
12022 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12023 int *ids = htab->section_pool.v5.section_ids;
12024 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
12025 /* Reverse map for error checking. */
12026 int ids_seen[DW_SECT_MAX_V5 + 1];
12027
12028 if (nr_columns < 2)
12029 {
12030 error (_("Dwarf Error: bad DWP hash table, too few columns"
12031 " in section table [in module %s]"),
12032 dwp_file->name);
12033 }
12034 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
12035 {
12036 error (_("Dwarf Error: bad DWP hash table, too many columns"
12037 " in section table [in module %s]"),
12038 dwp_file->name);
12039 }
12040 memset (ids, 255, sizeof_ids);
12041 memset (ids_seen, 255, sizeof (ids_seen));
12042 for (int i = 0; i < nr_columns; ++i)
12043 {
12044 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12045
12046 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
12047 {
12048 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12049 " in section table [in module %s]"),
12050 id, dwp_file->name);
12051 }
12052 if (ids_seen[id] != -1)
12053 {
12054 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12055 " id %d in section table [in module %s]"),
12056 id, dwp_file->name);
12057 }
12058 ids_seen[id] = i;
12059 ids[i] = id;
12060 }
12061 /* Must have seen an info section. */
12062 if (ids_seen[DW_SECT_INFO_V5] == -1)
12063 {
12064 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12065 " DWO info/types section [in module %s]"),
12066 dwp_file->name);
12067 }
12068 /* Must have an abbrev section. */
12069 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
12070 {
12071 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12072 " section [in module %s]"),
12073 dwp_file->name);
12074 }
12075 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12076 htab->section_pool.v5.sizes
12077 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
12078 * nr_units * nr_columns);
12079 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
12080 * nr_units * nr_columns))
12081 > index_end)
12082 {
12083 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12084 " [in module %s]"),
12085 dwp_file->name);
12086 }
12087 }
12088
12089 return htab;
12090 }
12091
12092 /* Update SECTIONS with the data from SECTP.
12093
12094 This function is like the other "locate" section routines, but in
12095 this context the sections to read comes from the DWP V1 hash table,
12096 not the full ELF section table.
12097
12098 The result is non-zero for success, or zero if an error was found. */
12099
12100 static int
12101 locate_v1_virtual_dwo_sections (asection *sectp,
12102 struct virtual_v1_dwo_sections *sections)
12103 {
12104 const struct dwop_section_names *names = &dwop_section_names;
12105
12106 if (section_is_p (sectp->name, &names->abbrev_dwo))
12107 {
12108 /* There can be only one. */
12109 if (sections->abbrev.s.section != NULL)
12110 return 0;
12111 sections->abbrev.s.section = sectp;
12112 sections->abbrev.size = bfd_section_size (sectp);
12113 }
12114 else if (section_is_p (sectp->name, &names->info_dwo)
12115 || section_is_p (sectp->name, &names->types_dwo))
12116 {
12117 /* There can be only one. */
12118 if (sections->info_or_types.s.section != NULL)
12119 return 0;
12120 sections->info_or_types.s.section = sectp;
12121 sections->info_or_types.size = bfd_section_size (sectp);
12122 }
12123 else if (section_is_p (sectp->name, &names->line_dwo))
12124 {
12125 /* There can be only one. */
12126 if (sections->line.s.section != NULL)
12127 return 0;
12128 sections->line.s.section = sectp;
12129 sections->line.size = bfd_section_size (sectp);
12130 }
12131 else if (section_is_p (sectp->name, &names->loc_dwo))
12132 {
12133 /* There can be only one. */
12134 if (sections->loc.s.section != NULL)
12135 return 0;
12136 sections->loc.s.section = sectp;
12137 sections->loc.size = bfd_section_size (sectp);
12138 }
12139 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12140 {
12141 /* There can be only one. */
12142 if (sections->macinfo.s.section != NULL)
12143 return 0;
12144 sections->macinfo.s.section = sectp;
12145 sections->macinfo.size = bfd_section_size (sectp);
12146 }
12147 else if (section_is_p (sectp->name, &names->macro_dwo))
12148 {
12149 /* There can be only one. */
12150 if (sections->macro.s.section != NULL)
12151 return 0;
12152 sections->macro.s.section = sectp;
12153 sections->macro.size = bfd_section_size (sectp);
12154 }
12155 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12156 {
12157 /* There can be only one. */
12158 if (sections->str_offsets.s.section != NULL)
12159 return 0;
12160 sections->str_offsets.s.section = sectp;
12161 sections->str_offsets.size = bfd_section_size (sectp);
12162 }
12163 else
12164 {
12165 /* No other kind of section is valid. */
12166 return 0;
12167 }
12168
12169 return 1;
12170 }
12171
12172 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12173 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12174 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12175 This is for DWP version 1 files. */
12176
12177 static struct dwo_unit *
12178 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
12179 struct dwp_file *dwp_file,
12180 uint32_t unit_index,
12181 const char *comp_dir,
12182 ULONGEST signature, int is_debug_types)
12183 {
12184 const struct dwp_hash_table *dwp_htab =
12185 is_debug_types ? dwp_file->tus : dwp_file->cus;
12186 bfd *dbfd = dwp_file->dbfd.get ();
12187 const char *kind = is_debug_types ? "TU" : "CU";
12188 struct dwo_file *dwo_file;
12189 struct dwo_unit *dwo_unit;
12190 struct virtual_v1_dwo_sections sections;
12191 void **dwo_file_slot;
12192 int i;
12193
12194 gdb_assert (dwp_file->version == 1);
12195
12196 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
12197 kind, pulongest (unit_index), hex_string (signature),
12198 dwp_file->name);
12199
12200 /* Fetch the sections of this DWO unit.
12201 Put a limit on the number of sections we look for so that bad data
12202 doesn't cause us to loop forever. */
12203
12204 #define MAX_NR_V1_DWO_SECTIONS \
12205 (1 /* .debug_info or .debug_types */ \
12206 + 1 /* .debug_abbrev */ \
12207 + 1 /* .debug_line */ \
12208 + 1 /* .debug_loc */ \
12209 + 1 /* .debug_str_offsets */ \
12210 + 1 /* .debug_macro or .debug_macinfo */ \
12211 + 1 /* trailing zero */)
12212
12213 memset (&sections, 0, sizeof (sections));
12214
12215 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12216 {
12217 asection *sectp;
12218 uint32_t section_nr =
12219 read_4_bytes (dbfd,
12220 dwp_htab->section_pool.v1.indices
12221 + (unit_index + i) * sizeof (uint32_t));
12222
12223 if (section_nr == 0)
12224 break;
12225 if (section_nr >= dwp_file->num_sections)
12226 {
12227 error (_("Dwarf Error: bad DWP hash table, section number too large"
12228 " [in module %s]"),
12229 dwp_file->name);
12230 }
12231
12232 sectp = dwp_file->elf_sections[section_nr];
12233 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12234 {
12235 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12236 " [in module %s]"),
12237 dwp_file->name);
12238 }
12239 }
12240
12241 if (i < 2
12242 || sections.info_or_types.empty ()
12243 || sections.abbrev.empty ())
12244 {
12245 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12246 " [in module %s]"),
12247 dwp_file->name);
12248 }
12249 if (i == MAX_NR_V1_DWO_SECTIONS)
12250 {
12251 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12252 " [in module %s]"),
12253 dwp_file->name);
12254 }
12255
12256 /* It's easier for the rest of the code if we fake a struct dwo_file and
12257 have dwo_unit "live" in that. At least for now.
12258
12259 The DWP file can be made up of a random collection of CUs and TUs.
12260 However, for each CU + set of TUs that came from the same original DWO
12261 file, we can combine them back into a virtual DWO file to save space
12262 (fewer struct dwo_file objects to allocate). Remember that for really
12263 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12264
12265 std::string virtual_dwo_name =
12266 string_printf ("virtual-dwo/%d-%d-%d-%d",
12267 sections.abbrev.get_id (),
12268 sections.line.get_id (),
12269 sections.loc.get_id (),
12270 sections.str_offsets.get_id ());
12271 /* Can we use an existing virtual DWO file? */
12272 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12273 comp_dir);
12274 /* Create one if necessary. */
12275 if (*dwo_file_slot == NULL)
12276 {
12277 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12278 virtual_dwo_name.c_str ());
12279
12280 dwo_file = new struct dwo_file;
12281 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12282 dwo_file->comp_dir = comp_dir;
12283 dwo_file->sections.abbrev = sections.abbrev;
12284 dwo_file->sections.line = sections.line;
12285 dwo_file->sections.loc = sections.loc;
12286 dwo_file->sections.macinfo = sections.macinfo;
12287 dwo_file->sections.macro = sections.macro;
12288 dwo_file->sections.str_offsets = sections.str_offsets;
12289 /* The "str" section is global to the entire DWP file. */
12290 dwo_file->sections.str = dwp_file->sections.str;
12291 /* The info or types section is assigned below to dwo_unit,
12292 there's no need to record it in dwo_file.
12293 Also, we can't simply record type sections in dwo_file because
12294 we record a pointer into the vector in dwo_unit. As we collect more
12295 types we'll grow the vector and eventually have to reallocate space
12296 for it, invalidating all copies of pointers into the previous
12297 contents. */
12298 *dwo_file_slot = dwo_file;
12299 }
12300 else
12301 {
12302 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12303 virtual_dwo_name.c_str ());
12304
12305 dwo_file = (struct dwo_file *) *dwo_file_slot;
12306 }
12307
12308 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12309 dwo_unit->dwo_file = dwo_file;
12310 dwo_unit->signature = signature;
12311 dwo_unit->section =
12312 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12313 *dwo_unit->section = sections.info_or_types;
12314 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12315
12316 return dwo_unit;
12317 }
12318
12319 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
12320 simplify them. Given a pointer to the containing section SECTION, and
12321 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
12322 virtual section of just that piece. */
12323
12324 static struct dwarf2_section_info
12325 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
12326 struct dwarf2_section_info *section,
12327 bfd_size_type offset, bfd_size_type size)
12328 {
12329 struct dwarf2_section_info result;
12330 asection *sectp;
12331
12332 gdb_assert (section != NULL);
12333 gdb_assert (!section->is_virtual);
12334
12335 memset (&result, 0, sizeof (result));
12336 result.s.containing_section = section;
12337 result.is_virtual = true;
12338
12339 if (size == 0)
12340 return result;
12341
12342 sectp = section->get_bfd_section ();
12343
12344 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12345 bounds of the real section. This is a pretty-rare event, so just
12346 flag an error (easier) instead of a warning and trying to cope. */
12347 if (sectp == NULL
12348 || offset + size > bfd_section_size (sectp))
12349 {
12350 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
12351 " in section %s [in module %s]"),
12352 sectp ? bfd_section_name (sectp) : "<unknown>",
12353 objfile_name (per_objfile->objfile));
12354 }
12355
12356 result.virtual_offset = offset;
12357 result.size = size;
12358 return result;
12359 }
12360
12361 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12362 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12363 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12364 This is for DWP version 2 files. */
12365
12366 static struct dwo_unit *
12367 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
12368 struct dwp_file *dwp_file,
12369 uint32_t unit_index,
12370 const char *comp_dir,
12371 ULONGEST signature, int is_debug_types)
12372 {
12373 const struct dwp_hash_table *dwp_htab =
12374 is_debug_types ? dwp_file->tus : dwp_file->cus;
12375 bfd *dbfd = dwp_file->dbfd.get ();
12376 const char *kind = is_debug_types ? "TU" : "CU";
12377 struct dwo_file *dwo_file;
12378 struct dwo_unit *dwo_unit;
12379 struct virtual_v2_or_v5_dwo_sections sections;
12380 void **dwo_file_slot;
12381 int i;
12382
12383 gdb_assert (dwp_file->version == 2);
12384
12385 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
12386 kind, pulongest (unit_index), hex_string (signature),
12387 dwp_file->name);
12388
12389 /* Fetch the section offsets of this DWO unit. */
12390
12391 memset (&sections, 0, sizeof (sections));
12392
12393 for (i = 0; i < dwp_htab->nr_columns; ++i)
12394 {
12395 uint32_t offset = read_4_bytes (dbfd,
12396 dwp_htab->section_pool.v2.offsets
12397 + (((unit_index - 1) * dwp_htab->nr_columns
12398 + i)
12399 * sizeof (uint32_t)));
12400 uint32_t size = read_4_bytes (dbfd,
12401 dwp_htab->section_pool.v2.sizes
12402 + (((unit_index - 1) * dwp_htab->nr_columns
12403 + i)
12404 * sizeof (uint32_t)));
12405
12406 switch (dwp_htab->section_pool.v2.section_ids[i])
12407 {
12408 case DW_SECT_INFO:
12409 case DW_SECT_TYPES:
12410 sections.info_or_types_offset = offset;
12411 sections.info_or_types_size = size;
12412 break;
12413 case DW_SECT_ABBREV:
12414 sections.abbrev_offset = offset;
12415 sections.abbrev_size = size;
12416 break;
12417 case DW_SECT_LINE:
12418 sections.line_offset = offset;
12419 sections.line_size = size;
12420 break;
12421 case DW_SECT_LOC:
12422 sections.loc_offset = offset;
12423 sections.loc_size = size;
12424 break;
12425 case DW_SECT_STR_OFFSETS:
12426 sections.str_offsets_offset = offset;
12427 sections.str_offsets_size = size;
12428 break;
12429 case DW_SECT_MACINFO:
12430 sections.macinfo_offset = offset;
12431 sections.macinfo_size = size;
12432 break;
12433 case DW_SECT_MACRO:
12434 sections.macro_offset = offset;
12435 sections.macro_size = size;
12436 break;
12437 }
12438 }
12439
12440 /* It's easier for the rest of the code if we fake a struct dwo_file and
12441 have dwo_unit "live" in that. At least for now.
12442
12443 The DWP file can be made up of a random collection of CUs and TUs.
12444 However, for each CU + set of TUs that came from the same original DWO
12445 file, we can combine them back into a virtual DWO file to save space
12446 (fewer struct dwo_file objects to allocate). Remember that for really
12447 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12448
12449 std::string virtual_dwo_name =
12450 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12451 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12452 (long) (sections.line_size ? sections.line_offset : 0),
12453 (long) (sections.loc_size ? sections.loc_offset : 0),
12454 (long) (sections.str_offsets_size
12455 ? sections.str_offsets_offset : 0));
12456 /* Can we use an existing virtual DWO file? */
12457 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12458 comp_dir);
12459 /* Create one if necessary. */
12460 if (*dwo_file_slot == NULL)
12461 {
12462 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12463 virtual_dwo_name.c_str ());
12464
12465 dwo_file = new struct dwo_file;
12466 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12467 dwo_file->comp_dir = comp_dir;
12468 dwo_file->sections.abbrev =
12469 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12470 sections.abbrev_offset,
12471 sections.abbrev_size);
12472 dwo_file->sections.line =
12473 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12474 sections.line_offset,
12475 sections.line_size);
12476 dwo_file->sections.loc =
12477 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12478 sections.loc_offset, sections.loc_size);
12479 dwo_file->sections.macinfo =
12480 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12481 sections.macinfo_offset,
12482 sections.macinfo_size);
12483 dwo_file->sections.macro =
12484 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12485 sections.macro_offset,
12486 sections.macro_size);
12487 dwo_file->sections.str_offsets =
12488 create_dwp_v2_or_v5_section (per_objfile,
12489 &dwp_file->sections.str_offsets,
12490 sections.str_offsets_offset,
12491 sections.str_offsets_size);
12492 /* The "str" section is global to the entire DWP file. */
12493 dwo_file->sections.str = dwp_file->sections.str;
12494 /* The info or types section is assigned below to dwo_unit,
12495 there's no need to record it in dwo_file.
12496 Also, we can't simply record type sections in dwo_file because
12497 we record a pointer into the vector in dwo_unit. As we collect more
12498 types we'll grow the vector and eventually have to reallocate space
12499 for it, invalidating all copies of pointers into the previous
12500 contents. */
12501 *dwo_file_slot = dwo_file;
12502 }
12503 else
12504 {
12505 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12506 virtual_dwo_name.c_str ());
12507
12508 dwo_file = (struct dwo_file *) *dwo_file_slot;
12509 }
12510
12511 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12512 dwo_unit->dwo_file = dwo_file;
12513 dwo_unit->signature = signature;
12514 dwo_unit->section =
12515 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12516 *dwo_unit->section = create_dwp_v2_or_v5_section
12517 (per_objfile,
12518 is_debug_types
12519 ? &dwp_file->sections.types
12520 : &dwp_file->sections.info,
12521 sections.info_or_types_offset,
12522 sections.info_or_types_size);
12523 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12524
12525 return dwo_unit;
12526 }
12527
12528 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12529 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12530 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12531 This is for DWP version 5 files. */
12532
12533 static struct dwo_unit *
12534 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12535 struct dwp_file *dwp_file,
12536 uint32_t unit_index,
12537 const char *comp_dir,
12538 ULONGEST signature, int is_debug_types)
12539 {
12540 const struct dwp_hash_table *dwp_htab
12541 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12542 bfd *dbfd = dwp_file->dbfd.get ();
12543 const char *kind = is_debug_types ? "TU" : "CU";
12544 struct dwo_file *dwo_file;
12545 struct dwo_unit *dwo_unit;
12546 struct virtual_v2_or_v5_dwo_sections sections {};
12547 void **dwo_file_slot;
12548
12549 gdb_assert (dwp_file->version == 5);
12550
12551 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
12552 kind, pulongest (unit_index), hex_string (signature),
12553 dwp_file->name);
12554
12555 /* Fetch the section offsets of this DWO unit. */
12556
12557 /* memset (&sections, 0, sizeof (sections)); */
12558
12559 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12560 {
12561 uint32_t offset = read_4_bytes (dbfd,
12562 dwp_htab->section_pool.v5.offsets
12563 + (((unit_index - 1)
12564 * dwp_htab->nr_columns
12565 + i)
12566 * sizeof (uint32_t)));
12567 uint32_t size = read_4_bytes (dbfd,
12568 dwp_htab->section_pool.v5.sizes
12569 + (((unit_index - 1) * dwp_htab->nr_columns
12570 + i)
12571 * sizeof (uint32_t)));
12572
12573 switch (dwp_htab->section_pool.v5.section_ids[i])
12574 {
12575 case DW_SECT_ABBREV_V5:
12576 sections.abbrev_offset = offset;
12577 sections.abbrev_size = size;
12578 break;
12579 case DW_SECT_INFO_V5:
12580 sections.info_or_types_offset = offset;
12581 sections.info_or_types_size = size;
12582 break;
12583 case DW_SECT_LINE_V5:
12584 sections.line_offset = offset;
12585 sections.line_size = size;
12586 break;
12587 case DW_SECT_LOCLISTS_V5:
12588 sections.loclists_offset = offset;
12589 sections.loclists_size = size;
12590 break;
12591 case DW_SECT_MACRO_V5:
12592 sections.macro_offset = offset;
12593 sections.macro_size = size;
12594 break;
12595 case DW_SECT_RNGLISTS_V5:
12596 sections.rnglists_offset = offset;
12597 sections.rnglists_size = size;
12598 break;
12599 case DW_SECT_STR_OFFSETS_V5:
12600 sections.str_offsets_offset = offset;
12601 sections.str_offsets_size = size;
12602 break;
12603 case DW_SECT_RESERVED_V5:
12604 default:
12605 break;
12606 }
12607 }
12608
12609 /* It's easier for the rest of the code if we fake a struct dwo_file and
12610 have dwo_unit "live" in that. At least for now.
12611
12612 The DWP file can be made up of a random collection of CUs and TUs.
12613 However, for each CU + set of TUs that came from the same original DWO
12614 file, we can combine them back into a virtual DWO file to save space
12615 (fewer struct dwo_file objects to allocate). Remember that for really
12616 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12617
12618 std::string virtual_dwo_name =
12619 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
12620 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12621 (long) (sections.line_size ? sections.line_offset : 0),
12622 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12623 (long) (sections.str_offsets_size
12624 ? sections.str_offsets_offset : 0),
12625 (long) (sections.macro_size ? sections.macro_offset : 0),
12626 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
12627 /* Can we use an existing virtual DWO file? */
12628 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
12629 virtual_dwo_name.c_str (),
12630 comp_dir);
12631 /* Create one if necessary. */
12632 if (*dwo_file_slot == NULL)
12633 {
12634 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12635 virtual_dwo_name.c_str ());
12636
12637 dwo_file = new struct dwo_file;
12638 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12639 dwo_file->comp_dir = comp_dir;
12640 dwo_file->sections.abbrev =
12641 create_dwp_v2_or_v5_section (per_objfile,
12642 &dwp_file->sections.abbrev,
12643 sections.abbrev_offset,
12644 sections.abbrev_size);
12645 dwo_file->sections.line =
12646 create_dwp_v2_or_v5_section (per_objfile,
12647 &dwp_file->sections.line,
12648 sections.line_offset, sections.line_size);
12649 dwo_file->sections.macro =
12650 create_dwp_v2_or_v5_section (per_objfile,
12651 &dwp_file->sections.macro,
12652 sections.macro_offset,
12653 sections.macro_size);
12654 dwo_file->sections.loclists =
12655 create_dwp_v2_or_v5_section (per_objfile,
12656 &dwp_file->sections.loclists,
12657 sections.loclists_offset,
12658 sections.loclists_size);
12659 dwo_file->sections.rnglists =
12660 create_dwp_v2_or_v5_section (per_objfile,
12661 &dwp_file->sections.rnglists,
12662 sections.rnglists_offset,
12663 sections.rnglists_size);
12664 dwo_file->sections.str_offsets =
12665 create_dwp_v2_or_v5_section (per_objfile,
12666 &dwp_file->sections.str_offsets,
12667 sections.str_offsets_offset,
12668 sections.str_offsets_size);
12669 /* The "str" section is global to the entire DWP file. */
12670 dwo_file->sections.str = dwp_file->sections.str;
12671 /* The info or types section is assigned below to dwo_unit,
12672 there's no need to record it in dwo_file.
12673 Also, we can't simply record type sections in dwo_file because
12674 we record a pointer into the vector in dwo_unit. As we collect more
12675 types we'll grow the vector and eventually have to reallocate space
12676 for it, invalidating all copies of pointers into the previous
12677 contents. */
12678 *dwo_file_slot = dwo_file;
12679 }
12680 else
12681 {
12682 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12683 virtual_dwo_name.c_str ());
12684
12685 dwo_file = (struct dwo_file *) *dwo_file_slot;
12686 }
12687
12688 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12689 dwo_unit->dwo_file = dwo_file;
12690 dwo_unit->signature = signature;
12691 dwo_unit->section
12692 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12693 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
12694 &dwp_file->sections.info,
12695 sections.info_or_types_offset,
12696 sections.info_or_types_size);
12697 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12698
12699 return dwo_unit;
12700 }
12701
12702 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12703 Returns NULL if the signature isn't found. */
12704
12705 static struct dwo_unit *
12706 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12707 struct dwp_file *dwp_file, const char *comp_dir,
12708 ULONGEST signature, int is_debug_types)
12709 {
12710 const struct dwp_hash_table *dwp_htab =
12711 is_debug_types ? dwp_file->tus : dwp_file->cus;
12712 bfd *dbfd = dwp_file->dbfd.get ();
12713 uint32_t mask = dwp_htab->nr_slots - 1;
12714 uint32_t hash = signature & mask;
12715 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12716 unsigned int i;
12717 void **slot;
12718 struct dwo_unit find_dwo_cu;
12719
12720 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12721 find_dwo_cu.signature = signature;
12722 slot = htab_find_slot (is_debug_types
12723 ? dwp_file->loaded_tus.get ()
12724 : dwp_file->loaded_cus.get (),
12725 &find_dwo_cu, INSERT);
12726
12727 if (*slot != NULL)
12728 return (struct dwo_unit *) *slot;
12729
12730 /* Use a for loop so that we don't loop forever on bad debug info. */
12731 for (i = 0; i < dwp_htab->nr_slots; ++i)
12732 {
12733 ULONGEST signature_in_table;
12734
12735 signature_in_table =
12736 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12737 if (signature_in_table == signature)
12738 {
12739 uint32_t unit_index =
12740 read_4_bytes (dbfd,
12741 dwp_htab->unit_table + hash * sizeof (uint32_t));
12742
12743 if (dwp_file->version == 1)
12744 {
12745 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12746 unit_index, comp_dir,
12747 signature, is_debug_types);
12748 }
12749 else if (dwp_file->version == 2)
12750 {
12751 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12752 unit_index, comp_dir,
12753 signature, is_debug_types);
12754 }
12755 else /* version == 5 */
12756 {
12757 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12758 unit_index, comp_dir,
12759 signature, is_debug_types);
12760 }
12761 return (struct dwo_unit *) *slot;
12762 }
12763 if (signature_in_table == 0)
12764 return NULL;
12765 hash = (hash + hash2) & mask;
12766 }
12767
12768 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12769 " [in module %s]"),
12770 dwp_file->name);
12771 }
12772
12773 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12774 Open the file specified by FILE_NAME and hand it off to BFD for
12775 preliminary analysis. Return a newly initialized bfd *, which
12776 includes a canonicalized copy of FILE_NAME.
12777 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12778 SEARCH_CWD is true if the current directory is to be searched.
12779 It will be searched before debug-file-directory.
12780 If successful, the file is added to the bfd include table of the
12781 objfile's bfd (see gdb_bfd_record_inclusion).
12782 If unable to find/open the file, return NULL.
12783 NOTE: This function is derived from symfile_bfd_open. */
12784
12785 static gdb_bfd_ref_ptr
12786 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12787 const char *file_name, int is_dwp, int search_cwd)
12788 {
12789 int desc;
12790 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12791 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12792 to debug_file_directory. */
12793 const char *search_path;
12794 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12795
12796 gdb::unique_xmalloc_ptr<char> search_path_holder;
12797 if (search_cwd)
12798 {
12799 if (*debug_file_directory != '\0')
12800 {
12801 search_path_holder.reset (concat (".", dirname_separator_string,
12802 debug_file_directory,
12803 (char *) NULL));
12804 search_path = search_path_holder.get ();
12805 }
12806 else
12807 search_path = ".";
12808 }
12809 else
12810 search_path = debug_file_directory;
12811
12812 openp_flags flags = OPF_RETURN_REALPATH;
12813 if (is_dwp)
12814 flags |= OPF_SEARCH_IN_PATH;
12815
12816 gdb::unique_xmalloc_ptr<char> absolute_name;
12817 desc = openp (search_path, flags, file_name,
12818 O_RDONLY | O_BINARY, &absolute_name);
12819 if (desc < 0)
12820 return NULL;
12821
12822 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12823 gnutarget, desc));
12824 if (sym_bfd == NULL)
12825 return NULL;
12826 bfd_set_cacheable (sym_bfd.get (), 1);
12827
12828 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12829 return NULL;
12830
12831 /* Success. Record the bfd as having been included by the objfile's bfd.
12832 This is important because things like demangled_names_hash lives in the
12833 objfile's per_bfd space and may have references to things like symbol
12834 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12835 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12836
12837 return sym_bfd;
12838 }
12839
12840 /* Try to open DWO file FILE_NAME.
12841 COMP_DIR is the DW_AT_comp_dir attribute.
12842 The result is the bfd handle of the file.
12843 If there is a problem finding or opening the file, return NULL.
12844 Upon success, the canonicalized path of the file is stored in the bfd,
12845 same as symfile_bfd_open. */
12846
12847 static gdb_bfd_ref_ptr
12848 open_dwo_file (dwarf2_per_objfile *per_objfile,
12849 const char *file_name, const char *comp_dir)
12850 {
12851 if (IS_ABSOLUTE_PATH (file_name))
12852 return try_open_dwop_file (per_objfile, file_name,
12853 0 /*is_dwp*/, 0 /*search_cwd*/);
12854
12855 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12856
12857 if (comp_dir != NULL)
12858 {
12859 gdb::unique_xmalloc_ptr<char> path_to_try
12860 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12861
12862 /* NOTE: If comp_dir is a relative path, this will also try the
12863 search path, which seems useful. */
12864 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12865 0 /*is_dwp*/,
12866 1 /*search_cwd*/));
12867 if (abfd != NULL)
12868 return abfd;
12869 }
12870
12871 /* That didn't work, try debug-file-directory, which, despite its name,
12872 is a list of paths. */
12873
12874 if (*debug_file_directory == '\0')
12875 return NULL;
12876
12877 return try_open_dwop_file (per_objfile, file_name,
12878 0 /*is_dwp*/, 1 /*search_cwd*/);
12879 }
12880
12881 /* This function is mapped across the sections and remembers the offset and
12882 size of each of the DWO debugging sections we are interested in. */
12883
12884 static void
12885 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12886 dwo_sections *dwo_sections)
12887 {
12888 const struct dwop_section_names *names = &dwop_section_names;
12889
12890 if (section_is_p (sectp->name, &names->abbrev_dwo))
12891 {
12892 dwo_sections->abbrev.s.section = sectp;
12893 dwo_sections->abbrev.size = bfd_section_size (sectp);
12894 }
12895 else if (section_is_p (sectp->name, &names->info_dwo))
12896 {
12897 dwo_sections->info.s.section = sectp;
12898 dwo_sections->info.size = bfd_section_size (sectp);
12899 }
12900 else if (section_is_p (sectp->name, &names->line_dwo))
12901 {
12902 dwo_sections->line.s.section = sectp;
12903 dwo_sections->line.size = bfd_section_size (sectp);
12904 }
12905 else if (section_is_p (sectp->name, &names->loc_dwo))
12906 {
12907 dwo_sections->loc.s.section = sectp;
12908 dwo_sections->loc.size = bfd_section_size (sectp);
12909 }
12910 else if (section_is_p (sectp->name, &names->loclists_dwo))
12911 {
12912 dwo_sections->loclists.s.section = sectp;
12913 dwo_sections->loclists.size = bfd_section_size (sectp);
12914 }
12915 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12916 {
12917 dwo_sections->macinfo.s.section = sectp;
12918 dwo_sections->macinfo.size = bfd_section_size (sectp);
12919 }
12920 else if (section_is_p (sectp->name, &names->macro_dwo))
12921 {
12922 dwo_sections->macro.s.section = sectp;
12923 dwo_sections->macro.size = bfd_section_size (sectp);
12924 }
12925 else if (section_is_p (sectp->name, &names->rnglists_dwo))
12926 {
12927 dwo_sections->rnglists.s.section = sectp;
12928 dwo_sections->rnglists.size = bfd_section_size (sectp);
12929 }
12930 else if (section_is_p (sectp->name, &names->str_dwo))
12931 {
12932 dwo_sections->str.s.section = sectp;
12933 dwo_sections->str.size = bfd_section_size (sectp);
12934 }
12935 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12936 {
12937 dwo_sections->str_offsets.s.section = sectp;
12938 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12939 }
12940 else if (section_is_p (sectp->name, &names->types_dwo))
12941 {
12942 struct dwarf2_section_info type_section;
12943
12944 memset (&type_section, 0, sizeof (type_section));
12945 type_section.s.section = sectp;
12946 type_section.size = bfd_section_size (sectp);
12947 dwo_sections->types.push_back (type_section);
12948 }
12949 }
12950
12951 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12952 by PER_CU. This is for the non-DWP case.
12953 The result is NULL if DWO_NAME can't be found. */
12954
12955 static struct dwo_file *
12956 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12957 const char *comp_dir)
12958 {
12959 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12960
12961 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12962 if (dbfd == NULL)
12963 {
12964 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12965
12966 return NULL;
12967 }
12968
12969 dwo_file_up dwo_file (new struct dwo_file);
12970 dwo_file->dwo_name = dwo_name;
12971 dwo_file->comp_dir = comp_dir;
12972 dwo_file->dbfd = std::move (dbfd);
12973
12974 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12975 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12976 &dwo_file->sections);
12977
12978 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12979 dwo_file->cus);
12980
12981 if (cu->per_cu->dwarf_version < 5)
12982 {
12983 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12984 dwo_file->sections.types, dwo_file->tus);
12985 }
12986 else
12987 {
12988 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12989 &dwo_file->sections.info, dwo_file->tus,
12990 rcuh_kind::COMPILE);
12991 }
12992
12993 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
12994
12995 return dwo_file.release ();
12996 }
12997
12998 /* This function is mapped across the sections and remembers the offset and
12999 size of each of the DWP debugging sections common to version 1 and 2 that
13000 we are interested in. */
13001
13002 static void
13003 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13004 dwp_file *dwp_file)
13005 {
13006 const struct dwop_section_names *names = &dwop_section_names;
13007 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13008
13009 /* Record the ELF section number for later lookup: this is what the
13010 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13011 gdb_assert (elf_section_nr < dwp_file->num_sections);
13012 dwp_file->elf_sections[elf_section_nr] = sectp;
13013
13014 /* Look for specific sections that we need. */
13015 if (section_is_p (sectp->name, &names->str_dwo))
13016 {
13017 dwp_file->sections.str.s.section = sectp;
13018 dwp_file->sections.str.size = bfd_section_size (sectp);
13019 }
13020 else if (section_is_p (sectp->name, &names->cu_index))
13021 {
13022 dwp_file->sections.cu_index.s.section = sectp;
13023 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
13024 }
13025 else if (section_is_p (sectp->name, &names->tu_index))
13026 {
13027 dwp_file->sections.tu_index.s.section = sectp;
13028 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
13029 }
13030 }
13031
13032 /* This function is mapped across the sections and remembers the offset and
13033 size of each of the DWP version 2 debugging sections that we are interested
13034 in. This is split into a separate function because we don't know if we
13035 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
13036
13037 static void
13038 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13039 {
13040 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13041 const struct dwop_section_names *names = &dwop_section_names;
13042 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13043
13044 /* Record the ELF section number for later lookup: this is what the
13045 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13046 gdb_assert (elf_section_nr < dwp_file->num_sections);
13047 dwp_file->elf_sections[elf_section_nr] = sectp;
13048
13049 /* Look for specific sections that we need. */
13050 if (section_is_p (sectp->name, &names->abbrev_dwo))
13051 {
13052 dwp_file->sections.abbrev.s.section = sectp;
13053 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13054 }
13055 else if (section_is_p (sectp->name, &names->info_dwo))
13056 {
13057 dwp_file->sections.info.s.section = sectp;
13058 dwp_file->sections.info.size = bfd_section_size (sectp);
13059 }
13060 else if (section_is_p (sectp->name, &names->line_dwo))
13061 {
13062 dwp_file->sections.line.s.section = sectp;
13063 dwp_file->sections.line.size = bfd_section_size (sectp);
13064 }
13065 else if (section_is_p (sectp->name, &names->loc_dwo))
13066 {
13067 dwp_file->sections.loc.s.section = sectp;
13068 dwp_file->sections.loc.size = bfd_section_size (sectp);
13069 }
13070 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13071 {
13072 dwp_file->sections.macinfo.s.section = sectp;
13073 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
13074 }
13075 else if (section_is_p (sectp->name, &names->macro_dwo))
13076 {
13077 dwp_file->sections.macro.s.section = sectp;
13078 dwp_file->sections.macro.size = bfd_section_size (sectp);
13079 }
13080 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13081 {
13082 dwp_file->sections.str_offsets.s.section = sectp;
13083 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13084 }
13085 else if (section_is_p (sectp->name, &names->types_dwo))
13086 {
13087 dwp_file->sections.types.s.section = sectp;
13088 dwp_file->sections.types.size = bfd_section_size (sectp);
13089 }
13090 }
13091
13092 /* This function is mapped across the sections and remembers the offset and
13093 size of each of the DWP version 5 debugging sections that we are interested
13094 in. This is split into a separate function because we don't know if we
13095 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
13096
13097 static void
13098 dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13099 {
13100 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13101 const struct dwop_section_names *names = &dwop_section_names;
13102 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13103
13104 /* Record the ELF section number for later lookup: this is what the
13105 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13106 gdb_assert (elf_section_nr < dwp_file->num_sections);
13107 dwp_file->elf_sections[elf_section_nr] = sectp;
13108
13109 /* Look for specific sections that we need. */
13110 if (section_is_p (sectp->name, &names->abbrev_dwo))
13111 {
13112 dwp_file->sections.abbrev.s.section = sectp;
13113 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13114 }
13115 else if (section_is_p (sectp->name, &names->info_dwo))
13116 {
13117 dwp_file->sections.info.s.section = sectp;
13118 dwp_file->sections.info.size = bfd_section_size (sectp);
13119 }
13120 else if (section_is_p (sectp->name, &names->line_dwo))
13121 {
13122 dwp_file->sections.line.s.section = sectp;
13123 dwp_file->sections.line.size = bfd_section_size (sectp);
13124 }
13125 else if (section_is_p (sectp->name, &names->loclists_dwo))
13126 {
13127 dwp_file->sections.loclists.s.section = sectp;
13128 dwp_file->sections.loclists.size = bfd_section_size (sectp);
13129 }
13130 else if (section_is_p (sectp->name, &names->macro_dwo))
13131 {
13132 dwp_file->sections.macro.s.section = sectp;
13133 dwp_file->sections.macro.size = bfd_section_size (sectp);
13134 }
13135 else if (section_is_p (sectp->name, &names->rnglists_dwo))
13136 {
13137 dwp_file->sections.rnglists.s.section = sectp;
13138 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
13139 }
13140 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13141 {
13142 dwp_file->sections.str_offsets.s.section = sectp;
13143 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13144 }
13145 }
13146
13147 /* Hash function for dwp_file loaded CUs/TUs. */
13148
13149 static hashval_t
13150 hash_dwp_loaded_cutus (const void *item)
13151 {
13152 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13153
13154 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13155 return dwo_unit->signature;
13156 }
13157
13158 /* Equality function for dwp_file loaded CUs/TUs. */
13159
13160 static int
13161 eq_dwp_loaded_cutus (const void *a, const void *b)
13162 {
13163 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13164 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13165
13166 return dua->signature == dub->signature;
13167 }
13168
13169 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13170
13171 static htab_up
13172 allocate_dwp_loaded_cutus_table ()
13173 {
13174 return htab_up (htab_create_alloc (3,
13175 hash_dwp_loaded_cutus,
13176 eq_dwp_loaded_cutus,
13177 NULL, xcalloc, xfree));
13178 }
13179
13180 /* Try to open DWP file FILE_NAME.
13181 The result is the bfd handle of the file.
13182 If there is a problem finding or opening the file, return NULL.
13183 Upon success, the canonicalized path of the file is stored in the bfd,
13184 same as symfile_bfd_open. */
13185
13186 static gdb_bfd_ref_ptr
13187 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
13188 {
13189 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
13190 1 /*is_dwp*/,
13191 1 /*search_cwd*/));
13192 if (abfd != NULL)
13193 return abfd;
13194
13195 /* Work around upstream bug 15652.
13196 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13197 [Whether that's a "bug" is debatable, but it is getting in our way.]
13198 We have no real idea where the dwp file is, because gdb's realpath-ing
13199 of the executable's path may have discarded the needed info.
13200 [IWBN if the dwp file name was recorded in the executable, akin to
13201 .gnu_debuglink, but that doesn't exist yet.]
13202 Strip the directory from FILE_NAME and search again. */
13203 if (*debug_file_directory != '\0')
13204 {
13205 /* Don't implicitly search the current directory here.
13206 If the user wants to search "." to handle this case,
13207 it must be added to debug-file-directory. */
13208 return try_open_dwop_file (per_objfile, lbasename (file_name),
13209 1 /*is_dwp*/,
13210 0 /*search_cwd*/);
13211 }
13212
13213 return NULL;
13214 }
13215
13216 /* Initialize the use of the DWP file for the current objfile.
13217 By convention the name of the DWP file is ${objfile}.dwp.
13218 The result is NULL if it can't be found. */
13219
13220 static std::unique_ptr<struct dwp_file>
13221 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
13222 {
13223 struct objfile *objfile = per_objfile->objfile;
13224
13225 /* Try to find first .dwp for the binary file before any symbolic links
13226 resolving. */
13227
13228 /* If the objfile is a debug file, find the name of the real binary
13229 file and get the name of dwp file from there. */
13230 std::string dwp_name;
13231 if (objfile->separate_debug_objfile_backlink != NULL)
13232 {
13233 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13234 const char *backlink_basename = lbasename (backlink->original_name);
13235
13236 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13237 }
13238 else
13239 dwp_name = objfile->original_name;
13240
13241 dwp_name += ".dwp";
13242
13243 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
13244 if (dbfd == NULL
13245 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13246 {
13247 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13248 dwp_name = objfile_name (objfile);
13249 dwp_name += ".dwp";
13250 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
13251 }
13252
13253 if (dbfd == NULL)
13254 {
13255 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
13256
13257 return std::unique_ptr<dwp_file> ();
13258 }
13259
13260 const char *name = bfd_get_filename (dbfd.get ());
13261 std::unique_ptr<struct dwp_file> dwp_file
13262 (new struct dwp_file (name, std::move (dbfd)));
13263
13264 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13265 dwp_file->elf_sections =
13266 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
13267 dwp_file->num_sections, asection *);
13268
13269 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13270 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
13271 dwp_file.get ());
13272
13273 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
13274
13275 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
13276
13277 /* The DWP file version is stored in the hash table. Oh well. */
13278 if (dwp_file->cus && dwp_file->tus
13279 && dwp_file->cus->version != dwp_file->tus->version)
13280 {
13281 /* Technically speaking, we should try to limp along, but this is
13282 pretty bizarre. We use pulongest here because that's the established
13283 portability solution (e.g, we cannot use %u for uint32_t). */
13284 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13285 " TU version %s [in DWP file %s]"),
13286 pulongest (dwp_file->cus->version),
13287 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13288 }
13289
13290 if (dwp_file->cus)
13291 dwp_file->version = dwp_file->cus->version;
13292 else if (dwp_file->tus)
13293 dwp_file->version = dwp_file->tus->version;
13294 else
13295 dwp_file->version = 2;
13296
13297 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13298 {
13299 if (dwp_file->version == 2)
13300 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
13301 dwp_file.get ());
13302 else
13303 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
13304 dwp_file.get ());
13305 }
13306
13307 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
13308 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
13309
13310 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
13311 dwarf_read_debug_printf (" %s CUs, %s TUs",
13312 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13313 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13314
13315 return dwp_file;
13316 }
13317
13318 /* Wrapper around open_and_init_dwp_file, only open it once. */
13319
13320 static struct dwp_file *
13321 get_dwp_file (dwarf2_per_objfile *per_objfile)
13322 {
13323 if (!per_objfile->per_bfd->dwp_checked)
13324 {
13325 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
13326 per_objfile->per_bfd->dwp_checked = 1;
13327 }
13328 return per_objfile->per_bfd->dwp_file.get ();
13329 }
13330
13331 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13332 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13333 or in the DWP file for the objfile, referenced by THIS_UNIT.
13334 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13335 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13336
13337 This is called, for example, when wanting to read a variable with a
13338 complex location. Therefore we don't want to do file i/o for every call.
13339 Therefore we don't want to look for a DWO file on every call.
13340 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13341 then we check if we've already seen DWO_NAME, and only THEN do we check
13342 for a DWO file.
13343
13344 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13345 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13346
13347 static struct dwo_unit *
13348 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13349 ULONGEST signature, int is_debug_types)
13350 {
13351 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13352 struct objfile *objfile = per_objfile->objfile;
13353 const char *kind = is_debug_types ? "TU" : "CU";
13354 void **dwo_file_slot;
13355 struct dwo_file *dwo_file;
13356 struct dwp_file *dwp_file;
13357
13358 /* First see if there's a DWP file.
13359 If we have a DWP file but didn't find the DWO inside it, don't
13360 look for the original DWO file. It makes gdb behave differently
13361 depending on whether one is debugging in the build tree. */
13362
13363 dwp_file = get_dwp_file (per_objfile);
13364 if (dwp_file != NULL)
13365 {
13366 const struct dwp_hash_table *dwp_htab =
13367 is_debug_types ? dwp_file->tus : dwp_file->cus;
13368
13369 if (dwp_htab != NULL)
13370 {
13371 struct dwo_unit *dwo_cutu =
13372 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
13373 is_debug_types);
13374
13375 if (dwo_cutu != NULL)
13376 {
13377 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
13378 kind, hex_string (signature),
13379 host_address_to_string (dwo_cutu));
13380
13381 return dwo_cutu;
13382 }
13383 }
13384 }
13385 else
13386 {
13387 /* No DWP file, look for the DWO file. */
13388
13389 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
13390 if (*dwo_file_slot == NULL)
13391 {
13392 /* Read in the file and build a table of the CUs/TUs it contains. */
13393 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
13394 }
13395 /* NOTE: This will be NULL if unable to open the file. */
13396 dwo_file = (struct dwo_file *) *dwo_file_slot;
13397
13398 if (dwo_file != NULL)
13399 {
13400 struct dwo_unit *dwo_cutu = NULL;
13401
13402 if (is_debug_types && dwo_file->tus)
13403 {
13404 struct dwo_unit find_dwo_cutu;
13405
13406 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13407 find_dwo_cutu.signature = signature;
13408 dwo_cutu
13409 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
13410 &find_dwo_cutu);
13411 }
13412 else if (!is_debug_types && dwo_file->cus)
13413 {
13414 struct dwo_unit find_dwo_cutu;
13415
13416 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13417 find_dwo_cutu.signature = signature;
13418 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
13419 &find_dwo_cutu);
13420 }
13421
13422 if (dwo_cutu != NULL)
13423 {
13424 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
13425 kind, dwo_name, hex_string (signature),
13426 host_address_to_string (dwo_cutu));
13427
13428 return dwo_cutu;
13429 }
13430 }
13431 }
13432
13433 /* We didn't find it. This could mean a dwo_id mismatch, or
13434 someone deleted the DWO/DWP file, or the search path isn't set up
13435 correctly to find the file. */
13436
13437 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
13438 kind, dwo_name, hex_string (signature));
13439
13440 /* This is a warning and not a complaint because it can be caused by
13441 pilot error (e.g., user accidentally deleting the DWO). */
13442 {
13443 /* Print the name of the DWP file if we looked there, helps the user
13444 better diagnose the problem. */
13445 std::string dwp_text;
13446
13447 if (dwp_file != NULL)
13448 dwp_text = string_printf (" [in DWP file %s]",
13449 lbasename (dwp_file->name));
13450
13451 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13452 " [in module %s]"),
13453 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13454 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
13455 }
13456 return NULL;
13457 }
13458
13459 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13460 See lookup_dwo_cutu_unit for details. */
13461
13462 static struct dwo_unit *
13463 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13464 ULONGEST signature)
13465 {
13466 gdb_assert (!cu->per_cu->is_debug_types);
13467
13468 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
13469 }
13470
13471 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13472 See lookup_dwo_cutu_unit for details. */
13473
13474 static struct dwo_unit *
13475 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
13476 {
13477 gdb_assert (cu->per_cu->is_debug_types);
13478
13479 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13480
13481 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
13482 }
13483
13484 /* Traversal function for queue_and_load_all_dwo_tus. */
13485
13486 static int
13487 queue_and_load_dwo_tu (void **slot, void *info)
13488 {
13489 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13490 dwarf2_cu *cu = (dwarf2_cu *) info;
13491 ULONGEST signature = dwo_unit->signature;
13492 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
13493
13494 if (sig_type != NULL)
13495 {
13496 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13497
13498 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13499 a real dependency of PER_CU on SIG_TYPE. That is detected later
13500 while processing PER_CU. */
13501 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
13502 load_full_type_unit (sig_cu, cu->per_objfile);
13503 cu->per_cu->imported_symtabs_push (sig_cu);
13504 }
13505
13506 return 1;
13507 }
13508
13509 /* Queue all TUs contained in the DWO of CU to be read in.
13510 The DWO may have the only definition of the type, though it may not be
13511 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13512 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13513
13514 static void
13515 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
13516 {
13517 struct dwo_unit *dwo_unit;
13518 struct dwo_file *dwo_file;
13519
13520 gdb_assert (cu != nullptr);
13521 gdb_assert (!cu->per_cu->is_debug_types);
13522 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
13523
13524 dwo_unit = cu->dwo_unit;
13525 gdb_assert (dwo_unit != NULL);
13526
13527 dwo_file = dwo_unit->dwo_file;
13528 if (dwo_file->tus != NULL)
13529 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
13530 }
13531
13532 /* Read in various DIEs. */
13533
13534 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13535 Inherit only the children of the DW_AT_abstract_origin DIE not being
13536 already referenced by DW_AT_abstract_origin from the children of the
13537 current DIE. */
13538
13539 static void
13540 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13541 {
13542 struct die_info *child_die;
13543 sect_offset *offsetp;
13544 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13545 struct die_info *origin_die;
13546 /* Iterator of the ORIGIN_DIE children. */
13547 struct die_info *origin_child_die;
13548 struct attribute *attr;
13549 struct dwarf2_cu *origin_cu;
13550 struct pending **origin_previous_list_in_scope;
13551
13552 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13553 if (!attr)
13554 return;
13555
13556 /* Note that following die references may follow to a die in a
13557 different cu. */
13558
13559 origin_cu = cu;
13560 origin_die = follow_die_ref (die, attr, &origin_cu);
13561
13562 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13563 symbols in. */
13564 origin_previous_list_in_scope = origin_cu->list_in_scope;
13565 origin_cu->list_in_scope = cu->list_in_scope;
13566
13567 if (die->tag != origin_die->tag
13568 && !(die->tag == DW_TAG_inlined_subroutine
13569 && origin_die->tag == DW_TAG_subprogram))
13570 complaint (_("DIE %s and its abstract origin %s have different tags"),
13571 sect_offset_str (die->sect_off),
13572 sect_offset_str (origin_die->sect_off));
13573
13574 std::vector<sect_offset> offsets;
13575
13576 for (child_die = die->child;
13577 child_die && child_die->tag;
13578 child_die = child_die->sibling)
13579 {
13580 struct die_info *child_origin_die;
13581 struct dwarf2_cu *child_origin_cu;
13582
13583 /* We are trying to process concrete instance entries:
13584 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13585 it's not relevant to our analysis here. i.e. detecting DIEs that are
13586 present in the abstract instance but not referenced in the concrete
13587 one. */
13588 if (child_die->tag == DW_TAG_call_site
13589 || child_die->tag == DW_TAG_GNU_call_site)
13590 continue;
13591
13592 /* For each CHILD_DIE, find the corresponding child of
13593 ORIGIN_DIE. If there is more than one layer of
13594 DW_AT_abstract_origin, follow them all; there shouldn't be,
13595 but GCC versions at least through 4.4 generate this (GCC PR
13596 40573). */
13597 child_origin_die = child_die;
13598 child_origin_cu = cu;
13599 while (1)
13600 {
13601 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13602 child_origin_cu);
13603 if (attr == NULL)
13604 break;
13605 child_origin_die = follow_die_ref (child_origin_die, attr,
13606 &child_origin_cu);
13607 }
13608
13609 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13610 counterpart may exist. */
13611 if (child_origin_die != child_die)
13612 {
13613 if (child_die->tag != child_origin_die->tag
13614 && !(child_die->tag == DW_TAG_inlined_subroutine
13615 && child_origin_die->tag == DW_TAG_subprogram))
13616 complaint (_("Child DIE %s and its abstract origin %s have "
13617 "different tags"),
13618 sect_offset_str (child_die->sect_off),
13619 sect_offset_str (child_origin_die->sect_off));
13620 if (child_origin_die->parent != origin_die)
13621 complaint (_("Child DIE %s and its abstract origin %s have "
13622 "different parents"),
13623 sect_offset_str (child_die->sect_off),
13624 sect_offset_str (child_origin_die->sect_off));
13625 else
13626 offsets.push_back (child_origin_die->sect_off);
13627 }
13628 }
13629 std::sort (offsets.begin (), offsets.end ());
13630 sect_offset *offsets_end = offsets.data () + offsets.size ();
13631 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13632 if (offsetp[-1] == *offsetp)
13633 complaint (_("Multiple children of DIE %s refer "
13634 "to DIE %s as their abstract origin"),
13635 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13636
13637 offsetp = offsets.data ();
13638 origin_child_die = origin_die->child;
13639 while (origin_child_die && origin_child_die->tag)
13640 {
13641 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13642 while (offsetp < offsets_end
13643 && *offsetp < origin_child_die->sect_off)
13644 offsetp++;
13645 if (offsetp >= offsets_end
13646 || *offsetp > origin_child_die->sect_off)
13647 {
13648 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13649 Check whether we're already processing ORIGIN_CHILD_DIE.
13650 This can happen with mutually referenced abstract_origins.
13651 PR 16581. */
13652 if (!origin_child_die->in_process)
13653 process_die (origin_child_die, origin_cu);
13654 }
13655 origin_child_die = origin_child_die->sibling;
13656 }
13657 origin_cu->list_in_scope = origin_previous_list_in_scope;
13658
13659 if (cu != origin_cu)
13660 compute_delayed_physnames (origin_cu);
13661 }
13662
13663 static void
13664 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13665 {
13666 struct objfile *objfile = cu->per_objfile->objfile;
13667 struct gdbarch *gdbarch = objfile->arch ();
13668 struct context_stack *newobj;
13669 CORE_ADDR lowpc;
13670 CORE_ADDR highpc;
13671 struct die_info *child_die;
13672 struct attribute *attr, *call_line, *call_file;
13673 const char *name;
13674 CORE_ADDR baseaddr;
13675 struct block *block;
13676 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13677 std::vector<struct symbol *> template_args;
13678 struct template_symbol *templ_func = NULL;
13679
13680 if (inlined_func)
13681 {
13682 /* If we do not have call site information, we can't show the
13683 caller of this inlined function. That's too confusing, so
13684 only use the scope for local variables. */
13685 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13686 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13687 if (call_line == NULL || call_file == NULL)
13688 {
13689 read_lexical_block_scope (die, cu);
13690 return;
13691 }
13692 }
13693
13694 baseaddr = objfile->text_section_offset ();
13695
13696 name = dwarf2_name (die, cu);
13697
13698 /* Ignore functions with missing or empty names. These are actually
13699 illegal according to the DWARF standard. */
13700 if (name == NULL)
13701 {
13702 complaint (_("missing name for subprogram DIE at %s"),
13703 sect_offset_str (die->sect_off));
13704 return;
13705 }
13706
13707 /* Ignore functions with missing or invalid low and high pc attributes. */
13708 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13709 <= PC_BOUNDS_INVALID)
13710 {
13711 attr = dwarf2_attr (die, DW_AT_external, cu);
13712 if (attr == nullptr || !attr->as_boolean ())
13713 complaint (_("cannot get low and high bounds "
13714 "for subprogram DIE at %s"),
13715 sect_offset_str (die->sect_off));
13716 return;
13717 }
13718
13719 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13720 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13721
13722 /* If we have any template arguments, then we must allocate a
13723 different sort of symbol. */
13724 for (child_die = die->child; child_die; child_die = child_die->sibling)
13725 {
13726 if (child_die->tag == DW_TAG_template_type_param
13727 || child_die->tag == DW_TAG_template_value_param)
13728 {
13729 templ_func = new (&objfile->objfile_obstack) template_symbol;
13730 templ_func->subclass = SYMBOL_TEMPLATE;
13731 break;
13732 }
13733 }
13734
13735 newobj = cu->get_builder ()->push_context (0, lowpc);
13736 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13737 (struct symbol *) templ_func);
13738
13739 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13740 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13741 cu->language);
13742
13743 /* If there is a location expression for DW_AT_frame_base, record
13744 it. */
13745 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13746 if (attr != nullptr)
13747 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13748
13749 /* If there is a location for the static link, record it. */
13750 newobj->static_link = NULL;
13751 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13752 if (attr != nullptr)
13753 {
13754 newobj->static_link
13755 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13756 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13757 cu->addr_type ());
13758 }
13759
13760 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13761
13762 if (die->child != NULL)
13763 {
13764 child_die = die->child;
13765 while (child_die && child_die->tag)
13766 {
13767 if (child_die->tag == DW_TAG_template_type_param
13768 || child_die->tag == DW_TAG_template_value_param)
13769 {
13770 struct symbol *arg = new_symbol (child_die, NULL, cu);
13771
13772 if (arg != NULL)
13773 template_args.push_back (arg);
13774 }
13775 else
13776 process_die (child_die, cu);
13777 child_die = child_die->sibling;
13778 }
13779 }
13780
13781 inherit_abstract_dies (die, cu);
13782
13783 /* If we have a DW_AT_specification, we might need to import using
13784 directives from the context of the specification DIE. See the
13785 comment in determine_prefix. */
13786 if (cu->language == language_cplus
13787 && dwarf2_attr (die, DW_AT_specification, cu))
13788 {
13789 struct dwarf2_cu *spec_cu = cu;
13790 struct die_info *spec_die = die_specification (die, &spec_cu);
13791
13792 while (spec_die)
13793 {
13794 child_die = spec_die->child;
13795 while (child_die && child_die->tag)
13796 {
13797 if (child_die->tag == DW_TAG_imported_module)
13798 process_die (child_die, spec_cu);
13799 child_die = child_die->sibling;
13800 }
13801
13802 /* In some cases, GCC generates specification DIEs that
13803 themselves contain DW_AT_specification attributes. */
13804 spec_die = die_specification (spec_die, &spec_cu);
13805 }
13806 }
13807
13808 struct context_stack cstk = cu->get_builder ()->pop_context ();
13809 /* Make a block for the local symbols within. */
13810 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13811 cstk.static_link, lowpc, highpc);
13812
13813 /* For C++, set the block's scope. */
13814 if ((cu->language == language_cplus
13815 || cu->language == language_fortran
13816 || cu->language == language_d
13817 || cu->language == language_rust)
13818 && cu->processing_has_namespace_info)
13819 block_set_scope (block, determine_prefix (die, cu),
13820 &objfile->objfile_obstack);
13821
13822 /* If we have address ranges, record them. */
13823 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13824
13825 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13826
13827 /* Attach template arguments to function. */
13828 if (!template_args.empty ())
13829 {
13830 gdb_assert (templ_func != NULL);
13831
13832 templ_func->n_template_arguments = template_args.size ();
13833 templ_func->template_arguments
13834 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13835 templ_func->n_template_arguments);
13836 memcpy (templ_func->template_arguments,
13837 template_args.data (),
13838 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13839
13840 /* Make sure that the symtab is set on the new symbols. Even
13841 though they don't appear in this symtab directly, other parts
13842 of gdb assume that symbols do, and this is reasonably
13843 true. */
13844 for (symbol *sym : template_args)
13845 symbol_set_symtab (sym, symbol_symtab (templ_func));
13846 }
13847
13848 /* In C++, we can have functions nested inside functions (e.g., when
13849 a function declares a class that has methods). This means that
13850 when we finish processing a function scope, we may need to go
13851 back to building a containing block's symbol lists. */
13852 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13853 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13854
13855 /* If we've finished processing a top-level function, subsequent
13856 symbols go in the file symbol list. */
13857 if (cu->get_builder ()->outermost_context_p ())
13858 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13859 }
13860
13861 /* Process all the DIES contained within a lexical block scope. Start
13862 a new scope, process the dies, and then close the scope. */
13863
13864 static void
13865 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13866 {
13867 struct objfile *objfile = cu->per_objfile->objfile;
13868 struct gdbarch *gdbarch = objfile->arch ();
13869 CORE_ADDR lowpc, highpc;
13870 struct die_info *child_die;
13871 CORE_ADDR baseaddr;
13872
13873 baseaddr = objfile->text_section_offset ();
13874
13875 /* Ignore blocks with missing or invalid low and high pc attributes. */
13876 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13877 as multiple lexical blocks? Handling children in a sane way would
13878 be nasty. Might be easier to properly extend generic blocks to
13879 describe ranges. */
13880 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13881 {
13882 case PC_BOUNDS_NOT_PRESENT:
13883 /* DW_TAG_lexical_block has no attributes, process its children as if
13884 there was no wrapping by that DW_TAG_lexical_block.
13885 GCC does no longer produces such DWARF since GCC r224161. */
13886 for (child_die = die->child;
13887 child_die != NULL && child_die->tag;
13888 child_die = child_die->sibling)
13889 {
13890 /* We might already be processing this DIE. This can happen
13891 in an unusual circumstance -- where a subroutine A
13892 appears lexically in another subroutine B, but A actually
13893 inlines B. The recursion is broken here, rather than in
13894 inherit_abstract_dies, because it seems better to simply
13895 drop concrete children here. */
13896 if (!child_die->in_process)
13897 process_die (child_die, cu);
13898 }
13899 return;
13900 case PC_BOUNDS_INVALID:
13901 return;
13902 }
13903 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13904 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13905
13906 cu->get_builder ()->push_context (0, lowpc);
13907 if (die->child != NULL)
13908 {
13909 child_die = die->child;
13910 while (child_die && child_die->tag)
13911 {
13912 process_die (child_die, cu);
13913 child_die = child_die->sibling;
13914 }
13915 }
13916 inherit_abstract_dies (die, cu);
13917 struct context_stack cstk = cu->get_builder ()->pop_context ();
13918
13919 if (*cu->get_builder ()->get_local_symbols () != NULL
13920 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13921 {
13922 struct block *block
13923 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13924 cstk.start_addr, highpc);
13925
13926 /* Note that recording ranges after traversing children, as we
13927 do here, means that recording a parent's ranges entails
13928 walking across all its children's ranges as they appear in
13929 the address map, which is quadratic behavior.
13930
13931 It would be nicer to record the parent's ranges before
13932 traversing its children, simply overriding whatever you find
13933 there. But since we don't even decide whether to create a
13934 block until after we've traversed its children, that's hard
13935 to do. */
13936 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13937 }
13938 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13939 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13940 }
13941
13942 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13943
13944 static void
13945 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13946 {
13947 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13948 struct objfile *objfile = per_objfile->objfile;
13949 struct gdbarch *gdbarch = objfile->arch ();
13950 CORE_ADDR pc, baseaddr;
13951 struct attribute *attr;
13952 struct call_site *call_site, call_site_local;
13953 void **slot;
13954 int nparams;
13955 struct die_info *child_die;
13956
13957 baseaddr = objfile->text_section_offset ();
13958
13959 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13960 if (attr == NULL)
13961 {
13962 /* This was a pre-DWARF-5 GNU extension alias
13963 for DW_AT_call_return_pc. */
13964 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13965 }
13966 if (!attr)
13967 {
13968 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13969 "DIE %s [in module %s]"),
13970 sect_offset_str (die->sect_off), objfile_name (objfile));
13971 return;
13972 }
13973 pc = attr->as_address () + baseaddr;
13974 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13975
13976 if (cu->call_site_htab == NULL)
13977 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13978 NULL, &objfile->objfile_obstack,
13979 hashtab_obstack_allocate, NULL);
13980 call_site_local.pc = pc;
13981 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13982 if (*slot != NULL)
13983 {
13984 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13985 "DIE %s [in module %s]"),
13986 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13987 objfile_name (objfile));
13988 return;
13989 }
13990
13991 /* Count parameters at the caller. */
13992
13993 nparams = 0;
13994 for (child_die = die->child; child_die && child_die->tag;
13995 child_die = child_die->sibling)
13996 {
13997 if (child_die->tag != DW_TAG_call_site_parameter
13998 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13999 {
14000 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
14001 "DW_TAG_call_site child DIE %s [in module %s]"),
14002 child_die->tag, sect_offset_str (child_die->sect_off),
14003 objfile_name (objfile));
14004 continue;
14005 }
14006
14007 nparams++;
14008 }
14009
14010 call_site
14011 = ((struct call_site *)
14012 obstack_alloc (&objfile->objfile_obstack,
14013 sizeof (*call_site)
14014 + (sizeof (*call_site->parameter) * (nparams - 1))));
14015 *slot = call_site;
14016 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14017 call_site->pc = pc;
14018
14019 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14020 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14021 {
14022 struct die_info *func_die;
14023
14024 /* Skip also over DW_TAG_inlined_subroutine. */
14025 for (func_die = die->parent;
14026 func_die && func_die->tag != DW_TAG_subprogram
14027 && func_die->tag != DW_TAG_subroutine_type;
14028 func_die = func_die->parent);
14029
14030 /* DW_AT_call_all_calls is a superset
14031 of DW_AT_call_all_tail_calls. */
14032 if (func_die
14033 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14034 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14035 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14036 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14037 {
14038 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14039 not complete. But keep CALL_SITE for look ups via call_site_htab,
14040 both the initial caller containing the real return address PC and
14041 the final callee containing the current PC of a chain of tail
14042 calls do not need to have the tail call list complete. But any
14043 function candidate for a virtual tail call frame searched via
14044 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14045 determined unambiguously. */
14046 }
14047 else
14048 {
14049 struct type *func_type = NULL;
14050
14051 if (func_die)
14052 func_type = get_die_type (func_die, cu);
14053 if (func_type != NULL)
14054 {
14055 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
14056
14057 /* Enlist this call site to the function. */
14058 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14059 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14060 }
14061 else
14062 complaint (_("Cannot find function owning DW_TAG_call_site "
14063 "DIE %s [in module %s]"),
14064 sect_offset_str (die->sect_off), objfile_name (objfile));
14065 }
14066 }
14067
14068 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14069 if (attr == NULL)
14070 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14071 if (attr == NULL)
14072 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14073 if (attr == NULL)
14074 {
14075 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14076 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14077 }
14078 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14079 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
14080 /* Keep NULL DWARF_BLOCK. */;
14081 else if (attr->form_is_block ())
14082 {
14083 struct dwarf2_locexpr_baton *dlbaton;
14084 struct dwarf_block *block = attr->as_block ();
14085
14086 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14087 dlbaton->data = block->data;
14088 dlbaton->size = block->size;
14089 dlbaton->per_objfile = per_objfile;
14090 dlbaton->per_cu = cu->per_cu;
14091
14092 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14093 }
14094 else if (attr->form_is_ref ())
14095 {
14096 struct dwarf2_cu *target_cu = cu;
14097 struct die_info *target_die;
14098
14099 target_die = follow_die_ref (die, attr, &target_cu);
14100 gdb_assert (target_cu->per_objfile->objfile == objfile);
14101 if (die_is_declaration (target_die, target_cu))
14102 {
14103 const char *target_physname;
14104
14105 /* Prefer the mangled name; otherwise compute the demangled one. */
14106 target_physname = dw2_linkage_name (target_die, target_cu);
14107 if (target_physname == NULL)
14108 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14109 if (target_physname == NULL)
14110 complaint (_("DW_AT_call_target target DIE has invalid "
14111 "physname, for referencing DIE %s [in module %s]"),
14112 sect_offset_str (die->sect_off), objfile_name (objfile));
14113 else
14114 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14115 }
14116 else
14117 {
14118 CORE_ADDR lowpc;
14119
14120 /* DW_AT_entry_pc should be preferred. */
14121 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14122 <= PC_BOUNDS_INVALID)
14123 complaint (_("DW_AT_call_target target DIE has invalid "
14124 "low pc, for referencing DIE %s [in module %s]"),
14125 sect_offset_str (die->sect_off), objfile_name (objfile));
14126 else
14127 {
14128 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14129 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14130 }
14131 }
14132 }
14133 else
14134 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14135 "block nor reference, for DIE %s [in module %s]"),
14136 sect_offset_str (die->sect_off), objfile_name (objfile));
14137
14138 call_site->per_cu = cu->per_cu;
14139 call_site->per_objfile = per_objfile;
14140
14141 for (child_die = die->child;
14142 child_die && child_die->tag;
14143 child_die = child_die->sibling)
14144 {
14145 struct call_site_parameter *parameter;
14146 struct attribute *loc, *origin;
14147
14148 if (child_die->tag != DW_TAG_call_site_parameter
14149 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14150 {
14151 /* Already printed the complaint above. */
14152 continue;
14153 }
14154
14155 gdb_assert (call_site->parameter_count < nparams);
14156 parameter = &call_site->parameter[call_site->parameter_count];
14157
14158 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14159 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14160 register is contained in DW_AT_call_value. */
14161
14162 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14163 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14164 if (origin == NULL)
14165 {
14166 /* This was a pre-DWARF-5 GNU extension alias
14167 for DW_AT_call_parameter. */
14168 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14169 }
14170 if (loc == NULL && origin != NULL && origin->form_is_ref ())
14171 {
14172 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14173
14174 sect_offset sect_off = origin->get_ref_die_offset ();
14175 if (!cu->header.offset_in_cu_p (sect_off))
14176 {
14177 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14178 binding can be done only inside one CU. Such referenced DIE
14179 therefore cannot be even moved to DW_TAG_partial_unit. */
14180 complaint (_("DW_AT_call_parameter offset is not in CU for "
14181 "DW_TAG_call_site child DIE %s [in module %s]"),
14182 sect_offset_str (child_die->sect_off),
14183 objfile_name (objfile));
14184 continue;
14185 }
14186 parameter->u.param_cu_off
14187 = (cu_offset) (sect_off - cu->header.sect_off);
14188 }
14189 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
14190 {
14191 complaint (_("No DW_FORM_block* DW_AT_location for "
14192 "DW_TAG_call_site child DIE %s [in module %s]"),
14193 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14194 continue;
14195 }
14196 else
14197 {
14198 struct dwarf_block *block = loc->as_block ();
14199
14200 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14201 (block->data, &block->data[block->size]);
14202 if (parameter->u.dwarf_reg != -1)
14203 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14204 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
14205 &block->data[block->size],
14206 &parameter->u.fb_offset))
14207 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14208 else
14209 {
14210 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14211 "for DW_FORM_block* DW_AT_location is supported for "
14212 "DW_TAG_call_site child DIE %s "
14213 "[in module %s]"),
14214 sect_offset_str (child_die->sect_off),
14215 objfile_name (objfile));
14216 continue;
14217 }
14218 }
14219
14220 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14221 if (attr == NULL)
14222 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14223 if (attr == NULL || !attr->form_is_block ())
14224 {
14225 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14226 "DW_TAG_call_site child DIE %s [in module %s]"),
14227 sect_offset_str (child_die->sect_off),
14228 objfile_name (objfile));
14229 continue;
14230 }
14231
14232 struct dwarf_block *block = attr->as_block ();
14233 parameter->value = block->data;
14234 parameter->value_size = block->size;
14235
14236 /* Parameters are not pre-cleared by memset above. */
14237 parameter->data_value = NULL;
14238 parameter->data_value_size = 0;
14239 call_site->parameter_count++;
14240
14241 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14242 if (attr == NULL)
14243 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14244 if (attr != nullptr)
14245 {
14246 if (!attr->form_is_block ())
14247 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14248 "DW_TAG_call_site child DIE %s [in module %s]"),
14249 sect_offset_str (child_die->sect_off),
14250 objfile_name (objfile));
14251 else
14252 {
14253 block = attr->as_block ();
14254 parameter->data_value = block->data;
14255 parameter->data_value_size = block->size;
14256 }
14257 }
14258 }
14259 }
14260
14261 /* Helper function for read_variable. If DIE represents a virtual
14262 table, then return the type of the concrete object that is
14263 associated with the virtual table. Otherwise, return NULL. */
14264
14265 static struct type *
14266 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14267 {
14268 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14269 if (attr == NULL)
14270 return NULL;
14271
14272 /* Find the type DIE. */
14273 struct die_info *type_die = NULL;
14274 struct dwarf2_cu *type_cu = cu;
14275
14276 if (attr->form_is_ref ())
14277 type_die = follow_die_ref (die, attr, &type_cu);
14278 if (type_die == NULL)
14279 return NULL;
14280
14281 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14282 return NULL;
14283 return die_containing_type (type_die, type_cu);
14284 }
14285
14286 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14287
14288 static void
14289 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14290 {
14291 struct rust_vtable_symbol *storage = NULL;
14292
14293 if (cu->language == language_rust)
14294 {
14295 struct type *containing_type = rust_containing_type (die, cu);
14296
14297 if (containing_type != NULL)
14298 {
14299 struct objfile *objfile = cu->per_objfile->objfile;
14300
14301 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
14302 storage->concrete_type = containing_type;
14303 storage->subclass = SYMBOL_RUST_VTABLE;
14304 }
14305 }
14306
14307 struct symbol *res = new_symbol (die, NULL, cu, storage);
14308 struct attribute *abstract_origin
14309 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14310 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14311 if (res == NULL && loc && abstract_origin)
14312 {
14313 /* We have a variable without a name, but with a location and an abstract
14314 origin. This may be a concrete instance of an abstract variable
14315 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14316 later. */
14317 struct dwarf2_cu *origin_cu = cu;
14318 struct die_info *origin_die
14319 = follow_die_ref (die, abstract_origin, &origin_cu);
14320 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14321 per_objfile->per_bfd->abstract_to_concrete
14322 [origin_die->sect_off].push_back (die->sect_off);
14323 }
14324 }
14325
14326 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14327 reading .debug_rnglists.
14328 Callback's type should be:
14329 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14330 Return true if the attributes are present and valid, otherwise,
14331 return false. */
14332
14333 template <typename Callback>
14334 static bool
14335 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14336 dwarf_tag tag, Callback &&callback)
14337 {
14338 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14339 struct objfile *objfile = per_objfile->objfile;
14340 bfd *obfd = objfile->obfd;
14341 /* Base address selection entry. */
14342 gdb::optional<CORE_ADDR> base;
14343 const gdb_byte *buffer;
14344 CORE_ADDR baseaddr;
14345 bool overflow = false;
14346 ULONGEST addr_index;
14347 struct dwarf2_section_info *rnglists_section;
14348
14349 base = cu->base_address;
14350 rnglists_section = cu_debug_rnglists_section (cu, tag);
14351 rnglists_section->read (objfile);
14352
14353 if (offset >= rnglists_section->size)
14354 {
14355 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14356 offset);
14357 return false;
14358 }
14359 buffer = rnglists_section->buffer + offset;
14360
14361 baseaddr = objfile->text_section_offset ();
14362
14363 while (1)
14364 {
14365 /* Initialize it due to a false compiler warning. */
14366 CORE_ADDR range_beginning = 0, range_end = 0;
14367 const gdb_byte *buf_end = (rnglists_section->buffer
14368 + rnglists_section->size);
14369 unsigned int bytes_read;
14370
14371 if (buffer == buf_end)
14372 {
14373 overflow = true;
14374 break;
14375 }
14376 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14377 switch (rlet)
14378 {
14379 case DW_RLE_end_of_list:
14380 break;
14381 case DW_RLE_base_address:
14382 if (buffer + cu->header.addr_size > buf_end)
14383 {
14384 overflow = true;
14385 break;
14386 }
14387 base = cu->header.read_address (obfd, buffer, &bytes_read);
14388 buffer += bytes_read;
14389 break;
14390 case DW_RLE_base_addressx:
14391 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14392 buffer += bytes_read;
14393 base = read_addr_index (cu, addr_index);
14394 break;
14395 case DW_RLE_start_length:
14396 if (buffer + cu->header.addr_size > buf_end)
14397 {
14398 overflow = true;
14399 break;
14400 }
14401 range_beginning = cu->header.read_address (obfd, buffer,
14402 &bytes_read);
14403 buffer += bytes_read;
14404 range_end = (range_beginning
14405 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14406 buffer += bytes_read;
14407 if (buffer > buf_end)
14408 {
14409 overflow = true;
14410 break;
14411 }
14412 break;
14413 case DW_RLE_startx_length:
14414 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14415 buffer += bytes_read;
14416 range_beginning = read_addr_index (cu, addr_index);
14417 if (buffer > buf_end)
14418 {
14419 overflow = true;
14420 break;
14421 }
14422 range_end = (range_beginning
14423 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14424 buffer += bytes_read;
14425 break;
14426 case DW_RLE_offset_pair:
14427 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14428 buffer += bytes_read;
14429 if (buffer > buf_end)
14430 {
14431 overflow = true;
14432 break;
14433 }
14434 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14435 buffer += bytes_read;
14436 if (buffer > buf_end)
14437 {
14438 overflow = true;
14439 break;
14440 }
14441 break;
14442 case DW_RLE_start_end:
14443 if (buffer + 2 * cu->header.addr_size > buf_end)
14444 {
14445 overflow = true;
14446 break;
14447 }
14448 range_beginning = cu->header.read_address (obfd, buffer,
14449 &bytes_read);
14450 buffer += bytes_read;
14451 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
14452 buffer += bytes_read;
14453 break;
14454 case DW_RLE_startx_endx:
14455 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14456 buffer += bytes_read;
14457 range_beginning = read_addr_index (cu, addr_index);
14458 if (buffer > buf_end)
14459 {
14460 overflow = true;
14461 break;
14462 }
14463 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14464 buffer += bytes_read;
14465 range_end = read_addr_index (cu, addr_index);
14466 break;
14467 default:
14468 complaint (_("Invalid .debug_rnglists data (no base address)"));
14469 return false;
14470 }
14471 if (rlet == DW_RLE_end_of_list || overflow)
14472 break;
14473 if (rlet == DW_RLE_base_address)
14474 continue;
14475
14476 if (range_beginning > range_end)
14477 {
14478 /* Inverted range entries are invalid. */
14479 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14480 return false;
14481 }
14482
14483 /* Empty range entries have no effect. */
14484 if (range_beginning == range_end)
14485 continue;
14486
14487 /* Only DW_RLE_offset_pair needs the base address added. */
14488 if (rlet == DW_RLE_offset_pair)
14489 {
14490 if (!base.has_value ())
14491 {
14492 /* We have no valid base address for the DW_RLE_offset_pair. */
14493 complaint (_("Invalid .debug_rnglists data (no base address for "
14494 "DW_RLE_offset_pair)"));
14495 return false;
14496 }
14497
14498 range_beginning += *base;
14499 range_end += *base;
14500 }
14501
14502 /* A not-uncommon case of bad debug info.
14503 Don't pollute the addrmap with bad data. */
14504 if (range_beginning + baseaddr == 0
14505 && !per_objfile->per_bfd->has_section_at_zero)
14506 {
14507 complaint (_(".debug_rnglists entry has start address of zero"
14508 " [in module %s]"), objfile_name (objfile));
14509 continue;
14510 }
14511
14512 callback (range_beginning, range_end);
14513 }
14514
14515 if (overflow)
14516 {
14517 complaint (_("Offset %d is not terminated "
14518 "for DW_AT_ranges attribute"),
14519 offset);
14520 return false;
14521 }
14522
14523 return true;
14524 }
14525
14526 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14527 Callback's type should be:
14528 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14529 Return 1 if the attributes are present and valid, otherwise, return 0. */
14530
14531 template <typename Callback>
14532 static int
14533 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
14534 Callback &&callback)
14535 {
14536 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14537 struct objfile *objfile = per_objfile->objfile;
14538 struct comp_unit_head *cu_header = &cu->header;
14539 bfd *obfd = objfile->obfd;
14540 unsigned int addr_size = cu_header->addr_size;
14541 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14542 /* Base address selection entry. */
14543 gdb::optional<CORE_ADDR> base;
14544 unsigned int dummy;
14545 const gdb_byte *buffer;
14546 CORE_ADDR baseaddr;
14547
14548 if (cu_header->version >= 5)
14549 return dwarf2_rnglists_process (offset, cu, tag, callback);
14550
14551 base = cu->base_address;
14552
14553 per_objfile->per_bfd->ranges.read (objfile);
14554 if (offset >= per_objfile->per_bfd->ranges.size)
14555 {
14556 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14557 offset);
14558 return 0;
14559 }
14560 buffer = per_objfile->per_bfd->ranges.buffer + offset;
14561
14562 baseaddr = objfile->text_section_offset ();
14563
14564 while (1)
14565 {
14566 CORE_ADDR range_beginning, range_end;
14567
14568 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
14569 buffer += addr_size;
14570 range_end = cu->header.read_address (obfd, buffer, &dummy);
14571 buffer += addr_size;
14572 offset += 2 * addr_size;
14573
14574 /* An end of list marker is a pair of zero addresses. */
14575 if (range_beginning == 0 && range_end == 0)
14576 /* Found the end of list entry. */
14577 break;
14578
14579 /* Each base address selection entry is a pair of 2 values.
14580 The first is the largest possible address, the second is
14581 the base address. Check for a base address here. */
14582 if ((range_beginning & mask) == mask)
14583 {
14584 /* If we found the largest possible address, then we already
14585 have the base address in range_end. */
14586 base = range_end;
14587 continue;
14588 }
14589
14590 if (!base.has_value ())
14591 {
14592 /* We have no valid base address for the ranges
14593 data. */
14594 complaint (_("Invalid .debug_ranges data (no base address)"));
14595 return 0;
14596 }
14597
14598 if (range_beginning > range_end)
14599 {
14600 /* Inverted range entries are invalid. */
14601 complaint (_("Invalid .debug_ranges data (inverted range)"));
14602 return 0;
14603 }
14604
14605 /* Empty range entries have no effect. */
14606 if (range_beginning == range_end)
14607 continue;
14608
14609 range_beginning += *base;
14610 range_end += *base;
14611
14612 /* A not-uncommon case of bad debug info.
14613 Don't pollute the addrmap with bad data. */
14614 if (range_beginning + baseaddr == 0
14615 && !per_objfile->per_bfd->has_section_at_zero)
14616 {
14617 complaint (_(".debug_ranges entry has start address of zero"
14618 " [in module %s]"), objfile_name (objfile));
14619 continue;
14620 }
14621
14622 callback (range_beginning, range_end);
14623 }
14624
14625 return 1;
14626 }
14627
14628 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14629 Return 1 if the attributes are present and valid, otherwise, return 0.
14630 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14631
14632 static int
14633 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14634 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14635 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
14636 {
14637 struct objfile *objfile = cu->per_objfile->objfile;
14638 struct gdbarch *gdbarch = objfile->arch ();
14639 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14640 int low_set = 0;
14641 CORE_ADDR low = 0;
14642 CORE_ADDR high = 0;
14643 int retval;
14644
14645 retval = dwarf2_ranges_process (offset, cu, tag,
14646 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14647 {
14648 if (ranges_pst != NULL)
14649 {
14650 CORE_ADDR lowpc;
14651 CORE_ADDR highpc;
14652
14653 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14654 range_beginning + baseaddr)
14655 - baseaddr);
14656 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14657 range_end + baseaddr)
14658 - baseaddr);
14659 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14660 lowpc, highpc - 1, ranges_pst);
14661 }
14662
14663 /* FIXME: This is recording everything as a low-high
14664 segment of consecutive addresses. We should have a
14665 data structure for discontiguous block ranges
14666 instead. */
14667 if (! low_set)
14668 {
14669 low = range_beginning;
14670 high = range_end;
14671 low_set = 1;
14672 }
14673 else
14674 {
14675 if (range_beginning < low)
14676 low = range_beginning;
14677 if (range_end > high)
14678 high = range_end;
14679 }
14680 });
14681 if (!retval)
14682 return 0;
14683
14684 if (! low_set)
14685 /* If the first entry is an end-of-list marker, the range
14686 describes an empty scope, i.e. no instructions. */
14687 return 0;
14688
14689 if (low_return)
14690 *low_return = low;
14691 if (high_return)
14692 *high_return = high;
14693 return 1;
14694 }
14695
14696 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14697 definition for the return value. *LOWPC and *HIGHPC are set iff
14698 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14699
14700 static enum pc_bounds_kind
14701 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14702 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14703 dwarf2_psymtab *pst)
14704 {
14705 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14706 struct attribute *attr;
14707 struct attribute *attr_high;
14708 CORE_ADDR low = 0;
14709 CORE_ADDR high = 0;
14710 enum pc_bounds_kind ret;
14711
14712 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14713 if (attr_high)
14714 {
14715 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14716 if (attr != nullptr)
14717 {
14718 low = attr->as_address ();
14719 high = attr_high->as_address ();
14720 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14721 high += low;
14722 }
14723 else
14724 /* Found high w/o low attribute. */
14725 return PC_BOUNDS_INVALID;
14726
14727 /* Found consecutive range of addresses. */
14728 ret = PC_BOUNDS_HIGH_LOW;
14729 }
14730 else
14731 {
14732 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14733 if (attr != nullptr && attr->form_is_unsigned ())
14734 {
14735 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14736 on DWARF version). */
14737 ULONGEST ranges_offset = attr->as_unsigned ();
14738
14739 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14740 this value. */
14741 if (die->tag != DW_TAG_compile_unit)
14742 ranges_offset += cu->gnu_ranges_base;
14743
14744 /* Value of the DW_AT_ranges attribute is the offset in the
14745 .debug_ranges section. */
14746 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14747 die->tag))
14748 return PC_BOUNDS_INVALID;
14749 /* Found discontinuous range of addresses. */
14750 ret = PC_BOUNDS_RANGES;
14751 }
14752 else
14753 return PC_BOUNDS_NOT_PRESENT;
14754 }
14755
14756 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14757 if (high <= low)
14758 return PC_BOUNDS_INVALID;
14759
14760 /* When using the GNU linker, .gnu.linkonce. sections are used to
14761 eliminate duplicate copies of functions and vtables and such.
14762 The linker will arbitrarily choose one and discard the others.
14763 The AT_*_pc values for such functions refer to local labels in
14764 these sections. If the section from that file was discarded, the
14765 labels are not in the output, so the relocs get a value of 0.
14766 If this is a discarded function, mark the pc bounds as invalid,
14767 so that GDB will ignore it. */
14768 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14769 return PC_BOUNDS_INVALID;
14770
14771 *lowpc = low;
14772 if (highpc)
14773 *highpc = high;
14774 return ret;
14775 }
14776
14777 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14778 its low and high PC addresses. Do nothing if these addresses could not
14779 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14780 and HIGHPC to the high address if greater than HIGHPC. */
14781
14782 static void
14783 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14784 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14785 struct dwarf2_cu *cu)
14786 {
14787 CORE_ADDR low, high;
14788 struct die_info *child = die->child;
14789
14790 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14791 {
14792 *lowpc = std::min (*lowpc, low);
14793 *highpc = std::max (*highpc, high);
14794 }
14795
14796 /* If the language does not allow nested subprograms (either inside
14797 subprograms or lexical blocks), we're done. */
14798 if (cu->language != language_ada)
14799 return;
14800
14801 /* Check all the children of the given DIE. If it contains nested
14802 subprograms, then check their pc bounds. Likewise, we need to
14803 check lexical blocks as well, as they may also contain subprogram
14804 definitions. */
14805 while (child && child->tag)
14806 {
14807 if (child->tag == DW_TAG_subprogram
14808 || child->tag == DW_TAG_lexical_block)
14809 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14810 child = child->sibling;
14811 }
14812 }
14813
14814 /* Get the low and high pc's represented by the scope DIE, and store
14815 them in *LOWPC and *HIGHPC. If the correct values can't be
14816 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14817
14818 static void
14819 get_scope_pc_bounds (struct die_info *die,
14820 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14821 struct dwarf2_cu *cu)
14822 {
14823 CORE_ADDR best_low = (CORE_ADDR) -1;
14824 CORE_ADDR best_high = (CORE_ADDR) 0;
14825 CORE_ADDR current_low, current_high;
14826
14827 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14828 >= PC_BOUNDS_RANGES)
14829 {
14830 best_low = current_low;
14831 best_high = current_high;
14832 }
14833 else
14834 {
14835 struct die_info *child = die->child;
14836
14837 while (child && child->tag)
14838 {
14839 switch (child->tag) {
14840 case DW_TAG_subprogram:
14841 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14842 break;
14843 case DW_TAG_namespace:
14844 case DW_TAG_module:
14845 /* FIXME: carlton/2004-01-16: Should we do this for
14846 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14847 that current GCC's always emit the DIEs corresponding
14848 to definitions of methods of classes as children of a
14849 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14850 the DIEs giving the declarations, which could be
14851 anywhere). But I don't see any reason why the
14852 standards says that they have to be there. */
14853 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14854
14855 if (current_low != ((CORE_ADDR) -1))
14856 {
14857 best_low = std::min (best_low, current_low);
14858 best_high = std::max (best_high, current_high);
14859 }
14860 break;
14861 default:
14862 /* Ignore. */
14863 break;
14864 }
14865
14866 child = child->sibling;
14867 }
14868 }
14869
14870 *lowpc = best_low;
14871 *highpc = best_high;
14872 }
14873
14874 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14875 in DIE. */
14876
14877 static void
14878 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14879 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14880 {
14881 struct objfile *objfile = cu->per_objfile->objfile;
14882 struct gdbarch *gdbarch = objfile->arch ();
14883 struct attribute *attr;
14884 struct attribute *attr_high;
14885
14886 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14887 if (attr_high)
14888 {
14889 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14890 if (attr != nullptr)
14891 {
14892 CORE_ADDR low = attr->as_address ();
14893 CORE_ADDR high = attr_high->as_address ();
14894
14895 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14896 high += low;
14897
14898 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14899 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14900 cu->get_builder ()->record_block_range (block, low, high - 1);
14901 }
14902 }
14903
14904 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14905 if (attr != nullptr && attr->form_is_unsigned ())
14906 {
14907 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14908 on DWARF version). */
14909 ULONGEST ranges_offset = attr->as_unsigned ();
14910
14911 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14912 this value. */
14913 if (die->tag != DW_TAG_compile_unit)
14914 ranges_offset += cu->gnu_ranges_base;
14915
14916 std::vector<blockrange> blockvec;
14917 dwarf2_ranges_process (ranges_offset, cu, die->tag,
14918 [&] (CORE_ADDR start, CORE_ADDR end)
14919 {
14920 start += baseaddr;
14921 end += baseaddr;
14922 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14923 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14924 cu->get_builder ()->record_block_range (block, start, end - 1);
14925 blockvec.emplace_back (start, end);
14926 });
14927
14928 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14929 }
14930 }
14931
14932 /* Check whether the producer field indicates either of GCC < 4.6, or the
14933 Intel C/C++ compiler, and cache the result in CU. */
14934
14935 static void
14936 check_producer (struct dwarf2_cu *cu)
14937 {
14938 int major, minor;
14939
14940 if (cu->producer == NULL)
14941 {
14942 /* For unknown compilers expect their behavior is DWARF version
14943 compliant.
14944
14945 GCC started to support .debug_types sections by -gdwarf-4 since
14946 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14947 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14948 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14949 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14950 }
14951 else if (producer_is_gcc (cu->producer, &major, &minor))
14952 {
14953 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14954 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14955 }
14956 else if (producer_is_icc (cu->producer, &major, &minor))
14957 {
14958 cu->producer_is_icc = true;
14959 cu->producer_is_icc_lt_14 = major < 14;
14960 }
14961 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14962 cu->producer_is_codewarrior = true;
14963 else
14964 {
14965 /* For other non-GCC compilers, expect their behavior is DWARF version
14966 compliant. */
14967 }
14968
14969 cu->checked_producer = true;
14970 }
14971
14972 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14973 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14974 during 4.6.0 experimental. */
14975
14976 static bool
14977 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14978 {
14979 if (!cu->checked_producer)
14980 check_producer (cu);
14981
14982 return cu->producer_is_gxx_lt_4_6;
14983 }
14984
14985
14986 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14987 with incorrect is_stmt attributes. */
14988
14989 static bool
14990 producer_is_codewarrior (struct dwarf2_cu *cu)
14991 {
14992 if (!cu->checked_producer)
14993 check_producer (cu);
14994
14995 return cu->producer_is_codewarrior;
14996 }
14997
14998 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
14999 If that attribute is not available, return the appropriate
15000 default. */
15001
15002 static enum dwarf_access_attribute
15003 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
15004 {
15005 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15006 if (attr != nullptr)
15007 {
15008 LONGEST value = attr->constant_value (-1);
15009 if (value == DW_ACCESS_public
15010 || value == DW_ACCESS_protected
15011 || value == DW_ACCESS_private)
15012 return (dwarf_access_attribute) value;
15013 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
15014 plongest (value));
15015 }
15016
15017 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
15018 {
15019 /* The default DWARF 2 accessibility for members is public, the default
15020 accessibility for inheritance is private. */
15021
15022 if (die->tag != DW_TAG_inheritance)
15023 return DW_ACCESS_public;
15024 else
15025 return DW_ACCESS_private;
15026 }
15027 else
15028 {
15029 /* DWARF 3+ defines the default accessibility a different way. The same
15030 rules apply now for DW_TAG_inheritance as for the members and it only
15031 depends on the container kind. */
15032
15033 if (die->parent->tag == DW_TAG_class_type)
15034 return DW_ACCESS_private;
15035 else
15036 return DW_ACCESS_public;
15037 }
15038 }
15039
15040 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15041 offset. If the attribute was not found return 0, otherwise return
15042 1. If it was found but could not properly be handled, set *OFFSET
15043 to 0. */
15044
15045 static int
15046 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15047 LONGEST *offset)
15048 {
15049 struct attribute *attr;
15050
15051 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15052 if (attr != NULL)
15053 {
15054 *offset = 0;
15055
15056 /* Note that we do not check for a section offset first here.
15057 This is because DW_AT_data_member_location is new in DWARF 4,
15058 so if we see it, we can assume that a constant form is really
15059 a constant and not a section offset. */
15060 if (attr->form_is_constant ())
15061 *offset = attr->constant_value (0);
15062 else if (attr->form_is_section_offset ())
15063 dwarf2_complex_location_expr_complaint ();
15064 else if (attr->form_is_block ())
15065 *offset = decode_locdesc (attr->as_block (), cu);
15066 else
15067 dwarf2_complex_location_expr_complaint ();
15068
15069 return 1;
15070 }
15071
15072 return 0;
15073 }
15074
15075 /* Look for DW_AT_data_member_location and store the results in FIELD. */
15076
15077 static void
15078 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15079 struct field *field)
15080 {
15081 struct attribute *attr;
15082
15083 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15084 if (attr != NULL)
15085 {
15086 if (attr->form_is_constant ())
15087 {
15088 LONGEST offset = attr->constant_value (0);
15089 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
15090 }
15091 else if (attr->form_is_section_offset ())
15092 dwarf2_complex_location_expr_complaint ();
15093 else if (attr->form_is_block ())
15094 {
15095 bool handled;
15096 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
15097 if (handled)
15098 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
15099 else
15100 {
15101 dwarf2_per_objfile *per_objfile = cu->per_objfile;
15102 struct objfile *objfile = per_objfile->objfile;
15103 struct dwarf2_locexpr_baton *dlbaton
15104 = XOBNEW (&objfile->objfile_obstack,
15105 struct dwarf2_locexpr_baton);
15106 dlbaton->data = attr->as_block ()->data;
15107 dlbaton->size = attr->as_block ()->size;
15108 /* When using this baton, we want to compute the address
15109 of the field, not the value. This is why
15110 is_reference is set to false here. */
15111 dlbaton->is_reference = false;
15112 dlbaton->per_objfile = per_objfile;
15113 dlbaton->per_cu = cu->per_cu;
15114
15115 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
15116 }
15117 }
15118 else
15119 dwarf2_complex_location_expr_complaint ();
15120 }
15121 }
15122
15123 /* Add an aggregate field to the field list. */
15124
15125 static void
15126 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15127 struct dwarf2_cu *cu)
15128 {
15129 struct objfile *objfile = cu->per_objfile->objfile;
15130 struct gdbarch *gdbarch = objfile->arch ();
15131 struct nextfield *new_field;
15132 struct attribute *attr;
15133 struct field *fp;
15134 const char *fieldname = "";
15135
15136 if (die->tag == DW_TAG_inheritance)
15137 {
15138 fip->baseclasses.emplace_back ();
15139 new_field = &fip->baseclasses.back ();
15140 }
15141 else
15142 {
15143 fip->fields.emplace_back ();
15144 new_field = &fip->fields.back ();
15145 }
15146
15147 new_field->offset = die->sect_off;
15148
15149 new_field->accessibility = dwarf2_access_attribute (die, cu);
15150 if (new_field->accessibility != DW_ACCESS_public)
15151 fip->non_public_fields = true;
15152
15153 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15154 if (attr != nullptr)
15155 new_field->virtuality = attr->as_virtuality ();
15156 else
15157 new_field->virtuality = DW_VIRTUALITY_none;
15158
15159 fp = &new_field->field;
15160
15161 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15162 {
15163 /* Data member other than a C++ static data member. */
15164
15165 /* Get type of field. */
15166 fp->set_type (die_type (die, cu));
15167
15168 SET_FIELD_BITPOS (*fp, 0);
15169
15170 /* Get bit size of field (zero if none). */
15171 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15172 if (attr != nullptr)
15173 {
15174 FIELD_BITSIZE (*fp) = attr->constant_value (0);
15175 }
15176 else
15177 {
15178 FIELD_BITSIZE (*fp) = 0;
15179 }
15180
15181 /* Get bit offset of field. */
15182 handle_data_member_location (die, cu, fp);
15183 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15184 if (attr != nullptr && attr->form_is_constant ())
15185 {
15186 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
15187 {
15188 /* For big endian bits, the DW_AT_bit_offset gives the
15189 additional bit offset from the MSB of the containing
15190 anonymous object to the MSB of the field. We don't
15191 have to do anything special since we don't need to
15192 know the size of the anonymous object. */
15193 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15194 + attr->constant_value (0)));
15195 }
15196 else
15197 {
15198 /* For little endian bits, compute the bit offset to the
15199 MSB of the anonymous object, subtract off the number of
15200 bits from the MSB of the field to the MSB of the
15201 object, and then subtract off the number of bits of
15202 the field itself. The result is the bit offset of
15203 the LSB of the field. */
15204 int anonymous_size;
15205 int bit_offset = attr->constant_value (0);
15206
15207 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15208 if (attr != nullptr && attr->form_is_constant ())
15209 {
15210 /* The size of the anonymous object containing
15211 the bit field is explicit, so use the
15212 indicated size (in bytes). */
15213 anonymous_size = attr->constant_value (0);
15214 }
15215 else
15216 {
15217 /* The size of the anonymous object containing
15218 the bit field must be inferred from the type
15219 attribute of the data member containing the
15220 bit field. */
15221 anonymous_size = TYPE_LENGTH (fp->type ());
15222 }
15223 SET_FIELD_BITPOS (*fp,
15224 (FIELD_BITPOS (*fp)
15225 + anonymous_size * bits_per_byte
15226 - bit_offset - FIELD_BITSIZE (*fp)));
15227 }
15228 }
15229 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15230 if (attr != NULL)
15231 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15232 + attr->constant_value (0)));
15233
15234 /* Get name of field. */
15235 fieldname = dwarf2_name (die, cu);
15236 if (fieldname == NULL)
15237 fieldname = "";
15238
15239 /* The name is already allocated along with this objfile, so we don't
15240 need to duplicate it for the type. */
15241 fp->name = fieldname;
15242
15243 /* Change accessibility for artificial fields (e.g. virtual table
15244 pointer or virtual base class pointer) to private. */
15245 if (dwarf2_attr (die, DW_AT_artificial, cu))
15246 {
15247 FIELD_ARTIFICIAL (*fp) = 1;
15248 new_field->accessibility = DW_ACCESS_private;
15249 fip->non_public_fields = true;
15250 }
15251 }
15252 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15253 {
15254 /* C++ static member. */
15255
15256 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15257 is a declaration, but all versions of G++ as of this writing
15258 (so through at least 3.2.1) incorrectly generate
15259 DW_TAG_variable tags. */
15260
15261 const char *physname;
15262
15263 /* Get name of field. */
15264 fieldname = dwarf2_name (die, cu);
15265 if (fieldname == NULL)
15266 return;
15267
15268 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15269 if (attr
15270 /* Only create a symbol if this is an external value.
15271 new_symbol checks this and puts the value in the global symbol
15272 table, which we want. If it is not external, new_symbol
15273 will try to put the value in cu->list_in_scope which is wrong. */
15274 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15275 {
15276 /* A static const member, not much different than an enum as far as
15277 we're concerned, except that we can support more types. */
15278 new_symbol (die, NULL, cu);
15279 }
15280
15281 /* Get physical name. */
15282 physname = dwarf2_physname (fieldname, die, cu);
15283
15284 /* The name is already allocated along with this objfile, so we don't
15285 need to duplicate it for the type. */
15286 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15287 fp->set_type (die_type (die, cu));
15288 FIELD_NAME (*fp) = fieldname;
15289 }
15290 else if (die->tag == DW_TAG_inheritance)
15291 {
15292 /* C++ base class field. */
15293 handle_data_member_location (die, cu, fp);
15294 FIELD_BITSIZE (*fp) = 0;
15295 fp->set_type (die_type (die, cu));
15296 FIELD_NAME (*fp) = fp->type ()->name ();
15297 }
15298 else
15299 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15300 }
15301
15302 /* Can the type given by DIE define another type? */
15303
15304 static bool
15305 type_can_define_types (const struct die_info *die)
15306 {
15307 switch (die->tag)
15308 {
15309 case DW_TAG_typedef:
15310 case DW_TAG_class_type:
15311 case DW_TAG_structure_type:
15312 case DW_TAG_union_type:
15313 case DW_TAG_enumeration_type:
15314 return true;
15315
15316 default:
15317 return false;
15318 }
15319 }
15320
15321 /* Add a type definition defined in the scope of the FIP's class. */
15322
15323 static void
15324 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15325 struct dwarf2_cu *cu)
15326 {
15327 struct decl_field fp;
15328 memset (&fp, 0, sizeof (fp));
15329
15330 gdb_assert (type_can_define_types (die));
15331
15332 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15333 fp.name = dwarf2_name (die, cu);
15334 fp.type = read_type_die (die, cu);
15335
15336 /* Save accessibility. */
15337 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15338 switch (accessibility)
15339 {
15340 case DW_ACCESS_public:
15341 /* The assumed value if neither private nor protected. */
15342 break;
15343 case DW_ACCESS_private:
15344 fp.is_private = 1;
15345 break;
15346 case DW_ACCESS_protected:
15347 fp.is_protected = 1;
15348 break;
15349 }
15350
15351 if (die->tag == DW_TAG_typedef)
15352 fip->typedef_field_list.push_back (fp);
15353 else
15354 fip->nested_types_list.push_back (fp);
15355 }
15356
15357 /* A convenience typedef that's used when finding the discriminant
15358 field for a variant part. */
15359 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
15360 offset_map_type;
15361
15362 /* Compute the discriminant range for a given variant. OBSTACK is
15363 where the results will be stored. VARIANT is the variant to
15364 process. IS_UNSIGNED indicates whether the discriminant is signed
15365 or unsigned. */
15366
15367 static const gdb::array_view<discriminant_range>
15368 convert_variant_range (struct obstack *obstack, const variant_field &variant,
15369 bool is_unsigned)
15370 {
15371 std::vector<discriminant_range> ranges;
15372
15373 if (variant.default_branch)
15374 return {};
15375
15376 if (variant.discr_list_data == nullptr)
15377 {
15378 discriminant_range r
15379 = {variant.discriminant_value, variant.discriminant_value};
15380 ranges.push_back (r);
15381 }
15382 else
15383 {
15384 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15385 variant.discr_list_data->size);
15386 while (!data.empty ())
15387 {
15388 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15389 {
15390 complaint (_("invalid discriminant marker: %d"), data[0]);
15391 break;
15392 }
15393 bool is_range = data[0] == DW_DSC_range;
15394 data = data.slice (1);
15395
15396 ULONGEST low, high;
15397 unsigned int bytes_read;
15398
15399 if (data.empty ())
15400 {
15401 complaint (_("DW_AT_discr_list missing low value"));
15402 break;
15403 }
15404 if (is_unsigned)
15405 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15406 else
15407 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15408 &bytes_read);
15409 data = data.slice (bytes_read);
15410
15411 if (is_range)
15412 {
15413 if (data.empty ())
15414 {
15415 complaint (_("DW_AT_discr_list missing high value"));
15416 break;
15417 }
15418 if (is_unsigned)
15419 high = read_unsigned_leb128 (nullptr, data.data (),
15420 &bytes_read);
15421 else
15422 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15423 &bytes_read);
15424 data = data.slice (bytes_read);
15425 }
15426 else
15427 high = low;
15428
15429 ranges.push_back ({ low, high });
15430 }
15431 }
15432
15433 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15434 ranges.size ());
15435 std::copy (ranges.begin (), ranges.end (), result);
15436 return gdb::array_view<discriminant_range> (result, ranges.size ());
15437 }
15438
15439 static const gdb::array_view<variant_part> create_variant_parts
15440 (struct obstack *obstack,
15441 const offset_map_type &offset_map,
15442 struct field_info *fi,
15443 const std::vector<variant_part_builder> &variant_parts);
15444
15445 /* Fill in a "struct variant" for a given variant field. RESULT is
15446 the variant to fill in. OBSTACK is where any needed allocations
15447 will be done. OFFSET_MAP holds the mapping from section offsets to
15448 fields for the type. FI describes the fields of the type we're
15449 processing. FIELD is the variant field we're converting. */
15450
15451 static void
15452 create_one_variant (variant &result, struct obstack *obstack,
15453 const offset_map_type &offset_map,
15454 struct field_info *fi, const variant_field &field)
15455 {
15456 result.discriminants = convert_variant_range (obstack, field, false);
15457 result.first_field = field.first_field + fi->baseclasses.size ();
15458 result.last_field = field.last_field + fi->baseclasses.size ();
15459 result.parts = create_variant_parts (obstack, offset_map, fi,
15460 field.variant_parts);
15461 }
15462
15463 /* Fill in a "struct variant_part" for a given variant part. RESULT
15464 is the variant part to fill in. OBSTACK is where any needed
15465 allocations will be done. OFFSET_MAP holds the mapping from
15466 section offsets to fields for the type. FI describes the fields of
15467 the type we're processing. BUILDER is the variant part to be
15468 converted. */
15469
15470 static void
15471 create_one_variant_part (variant_part &result,
15472 struct obstack *obstack,
15473 const offset_map_type &offset_map,
15474 struct field_info *fi,
15475 const variant_part_builder &builder)
15476 {
15477 auto iter = offset_map.find (builder.discriminant_offset);
15478 if (iter == offset_map.end ())
15479 {
15480 result.discriminant_index = -1;
15481 /* Doesn't matter. */
15482 result.is_unsigned = false;
15483 }
15484 else
15485 {
15486 result.discriminant_index = iter->second;
15487 result.is_unsigned
15488 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
15489 }
15490
15491 size_t n = builder.variants.size ();
15492 variant *output = new (obstack) variant[n];
15493 for (size_t i = 0; i < n; ++i)
15494 create_one_variant (output[i], obstack, offset_map, fi,
15495 builder.variants[i]);
15496
15497 result.variants = gdb::array_view<variant> (output, n);
15498 }
15499
15500 /* Create a vector of variant parts that can be attached to a type.
15501 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15502 holds the mapping from section offsets to fields for the type. FI
15503 describes the fields of the type we're processing. VARIANT_PARTS
15504 is the vector to convert. */
15505
15506 static const gdb::array_view<variant_part>
15507 create_variant_parts (struct obstack *obstack,
15508 const offset_map_type &offset_map,
15509 struct field_info *fi,
15510 const std::vector<variant_part_builder> &variant_parts)
15511 {
15512 if (variant_parts.empty ())
15513 return {};
15514
15515 size_t n = variant_parts.size ();
15516 variant_part *result = new (obstack) variant_part[n];
15517 for (size_t i = 0; i < n; ++i)
15518 create_one_variant_part (result[i], obstack, offset_map, fi,
15519 variant_parts[i]);
15520
15521 return gdb::array_view<variant_part> (result, n);
15522 }
15523
15524 /* Compute the variant part vector for FIP, attaching it to TYPE when
15525 done. */
15526
15527 static void
15528 add_variant_property (struct field_info *fip, struct type *type,
15529 struct dwarf2_cu *cu)
15530 {
15531 /* Map section offsets of fields to their field index. Note the
15532 field index here does not take the number of baseclasses into
15533 account. */
15534 offset_map_type offset_map;
15535 for (int i = 0; i < fip->fields.size (); ++i)
15536 offset_map[fip->fields[i].offset] = i;
15537
15538 struct objfile *objfile = cu->per_objfile->objfile;
15539 gdb::array_view<variant_part> parts
15540 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15541 fip->variant_parts);
15542
15543 struct dynamic_prop prop;
15544 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15545 obstack_copy (&objfile->objfile_obstack, &parts,
15546 sizeof (parts)));
15547
15548 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
15549 }
15550
15551 /* Create the vector of fields, and attach it to the type. */
15552
15553 static void
15554 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15555 struct dwarf2_cu *cu)
15556 {
15557 int nfields = fip->nfields ();
15558
15559 /* Record the field count, allocate space for the array of fields,
15560 and create blank accessibility bitfields if necessary. */
15561 type->set_num_fields (nfields);
15562 type->set_fields
15563 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
15564
15565 if (fip->non_public_fields && cu->language != language_ada)
15566 {
15567 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15568
15569 TYPE_FIELD_PRIVATE_BITS (type) =
15570 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15571 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15572
15573 TYPE_FIELD_PROTECTED_BITS (type) =
15574 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15575 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15576
15577 TYPE_FIELD_IGNORE_BITS (type) =
15578 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15579 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15580 }
15581
15582 /* If the type has baseclasses, allocate and clear a bit vector for
15583 TYPE_FIELD_VIRTUAL_BITS. */
15584 if (!fip->baseclasses.empty () && cu->language != language_ada)
15585 {
15586 int num_bytes = B_BYTES (fip->baseclasses.size ());
15587 unsigned char *pointer;
15588
15589 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15590 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15591 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15592 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15593 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15594 }
15595
15596 if (!fip->variant_parts.empty ())
15597 add_variant_property (fip, type, cu);
15598
15599 /* Copy the saved-up fields into the field vector. */
15600 for (int i = 0; i < nfields; ++i)
15601 {
15602 struct nextfield &field
15603 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15604 : fip->fields[i - fip->baseclasses.size ()]);
15605
15606 type->field (i) = field.field;
15607 switch (field.accessibility)
15608 {
15609 case DW_ACCESS_private:
15610 if (cu->language != language_ada)
15611 SET_TYPE_FIELD_PRIVATE (type, i);
15612 break;
15613
15614 case DW_ACCESS_protected:
15615 if (cu->language != language_ada)
15616 SET_TYPE_FIELD_PROTECTED (type, i);
15617 break;
15618
15619 case DW_ACCESS_public:
15620 break;
15621
15622 default:
15623 /* Unknown accessibility. Complain and treat it as public. */
15624 {
15625 complaint (_("unsupported accessibility %d"),
15626 field.accessibility);
15627 }
15628 break;
15629 }
15630 if (i < fip->baseclasses.size ())
15631 {
15632 switch (field.virtuality)
15633 {
15634 case DW_VIRTUALITY_virtual:
15635 case DW_VIRTUALITY_pure_virtual:
15636 if (cu->language == language_ada)
15637 error (_("unexpected virtuality in component of Ada type"));
15638 SET_TYPE_FIELD_VIRTUAL (type, i);
15639 break;
15640 }
15641 }
15642 }
15643 }
15644
15645 /* Return true if this member function is a constructor, false
15646 otherwise. */
15647
15648 static int
15649 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15650 {
15651 const char *fieldname;
15652 const char *type_name;
15653 int len;
15654
15655 if (die->parent == NULL)
15656 return 0;
15657
15658 if (die->parent->tag != DW_TAG_structure_type
15659 && die->parent->tag != DW_TAG_union_type
15660 && die->parent->tag != DW_TAG_class_type)
15661 return 0;
15662
15663 fieldname = dwarf2_name (die, cu);
15664 type_name = dwarf2_name (die->parent, cu);
15665 if (fieldname == NULL || type_name == NULL)
15666 return 0;
15667
15668 len = strlen (fieldname);
15669 return (strncmp (fieldname, type_name, len) == 0
15670 && (type_name[len] == '\0' || type_name[len] == '<'));
15671 }
15672
15673 /* Add a member function to the proper fieldlist. */
15674
15675 static void
15676 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15677 struct type *type, struct dwarf2_cu *cu)
15678 {
15679 struct objfile *objfile = cu->per_objfile->objfile;
15680 struct attribute *attr;
15681 int i;
15682 struct fnfieldlist *flp = nullptr;
15683 struct fn_field *fnp;
15684 const char *fieldname;
15685 struct type *this_type;
15686
15687 if (cu->language == language_ada)
15688 error (_("unexpected member function in Ada type"));
15689
15690 /* Get name of member function. */
15691 fieldname = dwarf2_name (die, cu);
15692 if (fieldname == NULL)
15693 return;
15694
15695 /* Look up member function name in fieldlist. */
15696 for (i = 0; i < fip->fnfieldlists.size (); i++)
15697 {
15698 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15699 {
15700 flp = &fip->fnfieldlists[i];
15701 break;
15702 }
15703 }
15704
15705 /* Create a new fnfieldlist if necessary. */
15706 if (flp == nullptr)
15707 {
15708 fip->fnfieldlists.emplace_back ();
15709 flp = &fip->fnfieldlists.back ();
15710 flp->name = fieldname;
15711 i = fip->fnfieldlists.size () - 1;
15712 }
15713
15714 /* Create a new member function field and add it to the vector of
15715 fnfieldlists. */
15716 flp->fnfields.emplace_back ();
15717 fnp = &flp->fnfields.back ();
15718
15719 /* Delay processing of the physname until later. */
15720 if (cu->language == language_cplus)
15721 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15722 die, cu);
15723 else
15724 {
15725 const char *physname = dwarf2_physname (fieldname, die, cu);
15726 fnp->physname = physname ? physname : "";
15727 }
15728
15729 fnp->type = alloc_type (objfile);
15730 this_type = read_type_die (die, cu);
15731 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15732 {
15733 int nparams = this_type->num_fields ();
15734
15735 /* TYPE is the domain of this method, and THIS_TYPE is the type
15736 of the method itself (TYPE_CODE_METHOD). */
15737 smash_to_method_type (fnp->type, type,
15738 TYPE_TARGET_TYPE (this_type),
15739 this_type->fields (),
15740 this_type->num_fields (),
15741 this_type->has_varargs ());
15742
15743 /* Handle static member functions.
15744 Dwarf2 has no clean way to discern C++ static and non-static
15745 member functions. G++ helps GDB by marking the first
15746 parameter for non-static member functions (which is the this
15747 pointer) as artificial. We obtain this information from
15748 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15749 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15750 fnp->voffset = VOFFSET_STATIC;
15751 }
15752 else
15753 complaint (_("member function type missing for '%s'"),
15754 dwarf2_full_name (fieldname, die, cu));
15755
15756 /* Get fcontext from DW_AT_containing_type if present. */
15757 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15758 fnp->fcontext = die_containing_type (die, cu);
15759
15760 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15761 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15762
15763 /* Get accessibility. */
15764 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15765 switch (accessibility)
15766 {
15767 case DW_ACCESS_private:
15768 fnp->is_private = 1;
15769 break;
15770 case DW_ACCESS_protected:
15771 fnp->is_protected = 1;
15772 break;
15773 }
15774
15775 /* Check for artificial methods. */
15776 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15777 if (attr && attr->as_boolean ())
15778 fnp->is_artificial = 1;
15779
15780 /* Check for defaulted methods. */
15781 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15782 if (attr != nullptr)
15783 fnp->defaulted = attr->defaulted ();
15784
15785 /* Check for deleted methods. */
15786 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15787 if (attr != nullptr && attr->as_boolean ())
15788 fnp->is_deleted = 1;
15789
15790 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15791
15792 /* Get index in virtual function table if it is a virtual member
15793 function. For older versions of GCC, this is an offset in the
15794 appropriate virtual table, as specified by DW_AT_containing_type.
15795 For everyone else, it is an expression to be evaluated relative
15796 to the object address. */
15797
15798 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15799 if (attr != nullptr)
15800 {
15801 if (attr->form_is_block () && attr->as_block ()->size > 0)
15802 {
15803 struct dwarf_block *block = attr->as_block ();
15804
15805 if (block->data[0] == DW_OP_constu)
15806 {
15807 /* Old-style GCC. */
15808 fnp->voffset = decode_locdesc (block, cu) + 2;
15809 }
15810 else if (block->data[0] == DW_OP_deref
15811 || (block->size > 1
15812 && block->data[0] == DW_OP_deref_size
15813 && block->data[1] == cu->header.addr_size))
15814 {
15815 fnp->voffset = decode_locdesc (block, cu);
15816 if ((fnp->voffset % cu->header.addr_size) != 0)
15817 dwarf2_complex_location_expr_complaint ();
15818 else
15819 fnp->voffset /= cu->header.addr_size;
15820 fnp->voffset += 2;
15821 }
15822 else
15823 dwarf2_complex_location_expr_complaint ();
15824
15825 if (!fnp->fcontext)
15826 {
15827 /* If there is no `this' field and no DW_AT_containing_type,
15828 we cannot actually find a base class context for the
15829 vtable! */
15830 if (this_type->num_fields () == 0
15831 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15832 {
15833 complaint (_("cannot determine context for virtual member "
15834 "function \"%s\" (offset %s)"),
15835 fieldname, sect_offset_str (die->sect_off));
15836 }
15837 else
15838 {
15839 fnp->fcontext
15840 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15841 }
15842 }
15843 }
15844 else if (attr->form_is_section_offset ())
15845 {
15846 dwarf2_complex_location_expr_complaint ();
15847 }
15848 else
15849 {
15850 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15851 fieldname);
15852 }
15853 }
15854 else
15855 {
15856 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15857 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
15858 {
15859 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15860 complaint (_("Member function \"%s\" (offset %s) is virtual "
15861 "but the vtable offset is not specified"),
15862 fieldname, sect_offset_str (die->sect_off));
15863 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15864 TYPE_CPLUS_DYNAMIC (type) = 1;
15865 }
15866 }
15867 }
15868
15869 /* Create the vector of member function fields, and attach it to the type. */
15870
15871 static void
15872 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15873 struct dwarf2_cu *cu)
15874 {
15875 if (cu->language == language_ada)
15876 error (_("unexpected member functions in Ada type"));
15877
15878 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15879 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15880 TYPE_ALLOC (type,
15881 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15882
15883 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15884 {
15885 struct fnfieldlist &nf = fip->fnfieldlists[i];
15886 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15887
15888 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15889 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15890 fn_flp->fn_fields = (struct fn_field *)
15891 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15892
15893 for (int k = 0; k < nf.fnfields.size (); ++k)
15894 fn_flp->fn_fields[k] = nf.fnfields[k];
15895 }
15896
15897 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15898 }
15899
15900 /* Returns non-zero if NAME is the name of a vtable member in CU's
15901 language, zero otherwise. */
15902 static int
15903 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15904 {
15905 static const char vptr[] = "_vptr";
15906
15907 /* Look for the C++ form of the vtable. */
15908 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15909 return 1;
15910
15911 return 0;
15912 }
15913
15914 /* GCC outputs unnamed structures that are really pointers to member
15915 functions, with the ABI-specified layout. If TYPE describes
15916 such a structure, smash it into a member function type.
15917
15918 GCC shouldn't do this; it should just output pointer to member DIEs.
15919 This is GCC PR debug/28767. */
15920
15921 static void
15922 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15923 {
15924 struct type *pfn_type, *self_type, *new_type;
15925
15926 /* Check for a structure with no name and two children. */
15927 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15928 return;
15929
15930 /* Check for __pfn and __delta members. */
15931 if (TYPE_FIELD_NAME (type, 0) == NULL
15932 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15933 || TYPE_FIELD_NAME (type, 1) == NULL
15934 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15935 return;
15936
15937 /* Find the type of the method. */
15938 pfn_type = type->field (0).type ();
15939 if (pfn_type == NULL
15940 || pfn_type->code () != TYPE_CODE_PTR
15941 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15942 return;
15943
15944 /* Look for the "this" argument. */
15945 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15946 if (pfn_type->num_fields () == 0
15947 /* || pfn_type->field (0).type () == NULL */
15948 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15949 return;
15950
15951 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15952 new_type = alloc_type (objfile);
15953 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15954 pfn_type->fields (), pfn_type->num_fields (),
15955 pfn_type->has_varargs ());
15956 smash_to_methodptr_type (type, new_type);
15957 }
15958
15959 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15960 requires rewriting, then copy it and return the updated copy.
15961 Otherwise return nullptr. */
15962
15963 static struct type *
15964 rewrite_array_type (struct type *type)
15965 {
15966 if (type->code () != TYPE_CODE_ARRAY)
15967 return nullptr;
15968
15969 struct type *index_type = type->index_type ();
15970 range_bounds *current_bounds = index_type->bounds ();
15971
15972 /* Handle multi-dimensional arrays. */
15973 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15974 if (new_target == nullptr)
15975 {
15976 /* Maybe we don't need to rewrite this array. */
15977 if (current_bounds->low.kind () == PROP_CONST
15978 && current_bounds->high.kind () == PROP_CONST)
15979 return nullptr;
15980 }
15981
15982 /* Either the target type was rewritten, or the bounds have to be
15983 updated. Either way we want to copy the type and update
15984 everything. */
15985 struct type *copy = copy_type (type);
15986 int nfields = copy->num_fields ();
15987 field *new_fields
15988 = ((struct field *) TYPE_ZALLOC (copy,
15989 nfields * sizeof (struct field)));
15990 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15991 copy->set_fields (new_fields);
15992 if (new_target != nullptr)
15993 TYPE_TARGET_TYPE (copy) = new_target;
15994
15995 struct type *index_copy = copy_type (index_type);
15996 range_bounds *bounds
15997 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15998 sizeof (range_bounds));
15999 *bounds = *current_bounds;
16000 bounds->low.set_const_val (1);
16001 bounds->high.set_const_val (0);
16002 index_copy->set_bounds (bounds);
16003 copy->set_index_type (index_copy);
16004
16005 return copy;
16006 }
16007
16008 /* While some versions of GCC will generate complicated DWARF for an
16009 array (see quirk_ada_thick_pointer), more recent versions were
16010 modified to emit an explicit thick pointer structure. However, in
16011 this case, the array still has DWARF expressions for its ranges,
16012 and these must be ignored. */
16013
16014 static void
16015 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
16016 struct type *type)
16017 {
16018 gdb_assert (cu->language == language_ada);
16019
16020 /* Check for a structure with two children. */
16021 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
16022 return;
16023
16024 /* Check for P_ARRAY and P_BOUNDS members. */
16025 if (TYPE_FIELD_NAME (type, 0) == NULL
16026 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
16027 || TYPE_FIELD_NAME (type, 1) == NULL
16028 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
16029 return;
16030
16031 /* Make sure we're looking at a pointer to an array. */
16032 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
16033 return;
16034
16035 /* The Ada code already knows how to handle these types, so all that
16036 we need to do is turn the bounds into static bounds. However, we
16037 don't want to rewrite existing array or index types in-place,
16038 because those may be referenced in other contexts where this
16039 rewriting is undesirable. */
16040 struct type *new_ary_type
16041 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
16042 if (new_ary_type != nullptr)
16043 type->field (0).set_type (lookup_pointer_type (new_ary_type));
16044 }
16045
16046 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
16047 appropriate error checking and issuing complaints if there is a
16048 problem. */
16049
16050 static ULONGEST
16051 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
16052 {
16053 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
16054
16055 if (attr == nullptr)
16056 return 0;
16057
16058 if (!attr->form_is_constant ())
16059 {
16060 complaint (_("DW_AT_alignment must have constant form"
16061 " - DIE at %s [in module %s]"),
16062 sect_offset_str (die->sect_off),
16063 objfile_name (cu->per_objfile->objfile));
16064 return 0;
16065 }
16066
16067 LONGEST val = attr->constant_value (0);
16068 if (val < 0)
16069 {
16070 complaint (_("DW_AT_alignment value must not be negative"
16071 " - DIE at %s [in module %s]"),
16072 sect_offset_str (die->sect_off),
16073 objfile_name (cu->per_objfile->objfile));
16074 return 0;
16075 }
16076 ULONGEST align = val;
16077
16078 if (align == 0)
16079 {
16080 complaint (_("DW_AT_alignment value must not be zero"
16081 " - DIE at %s [in module %s]"),
16082 sect_offset_str (die->sect_off),
16083 objfile_name (cu->per_objfile->objfile));
16084 return 0;
16085 }
16086 if ((align & (align - 1)) != 0)
16087 {
16088 complaint (_("DW_AT_alignment value must be a power of 2"
16089 " - DIE at %s [in module %s]"),
16090 sect_offset_str (die->sect_off),
16091 objfile_name (cu->per_objfile->objfile));
16092 return 0;
16093 }
16094
16095 return align;
16096 }
16097
16098 /* If the DIE has a DW_AT_alignment attribute, use its value to set
16099 the alignment for TYPE. */
16100
16101 static void
16102 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
16103 struct type *type)
16104 {
16105 if (!set_type_align (type, get_alignment (cu, die)))
16106 complaint (_("DW_AT_alignment value too large"
16107 " - DIE at %s [in module %s]"),
16108 sect_offset_str (die->sect_off),
16109 objfile_name (cu->per_objfile->objfile));
16110 }
16111
16112 /* Check if the given VALUE is a valid enum dwarf_calling_convention
16113 constant for a type, according to DWARF5 spec, Table 5.5. */
16114
16115 static bool
16116 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
16117 {
16118 switch (value)
16119 {
16120 case DW_CC_normal:
16121 case DW_CC_pass_by_reference:
16122 case DW_CC_pass_by_value:
16123 return true;
16124
16125 default:
16126 complaint (_("unrecognized DW_AT_calling_convention value "
16127 "(%s) for a type"), pulongest (value));
16128 return false;
16129 }
16130 }
16131
16132 /* Check if the given VALUE is a valid enum dwarf_calling_convention
16133 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
16134 also according to GNU-specific values (see include/dwarf2.h). */
16135
16136 static bool
16137 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
16138 {
16139 switch (value)
16140 {
16141 case DW_CC_normal:
16142 case DW_CC_program:
16143 case DW_CC_nocall:
16144 return true;
16145
16146 case DW_CC_GNU_renesas_sh:
16147 case DW_CC_GNU_borland_fastcall_i386:
16148 case DW_CC_GDB_IBM_OpenCL:
16149 return true;
16150
16151 default:
16152 complaint (_("unrecognized DW_AT_calling_convention value "
16153 "(%s) for a subroutine"), pulongest (value));
16154 return false;
16155 }
16156 }
16157
16158 /* Called when we find the DIE that starts a structure or union scope
16159 (definition) to create a type for the structure or union. Fill in
16160 the type's name and general properties; the members will not be
16161 processed until process_structure_scope. A symbol table entry for
16162 the type will also not be done until process_structure_scope (assuming
16163 the type has a name).
16164
16165 NOTE: we need to call these functions regardless of whether or not the
16166 DIE has a DW_AT_name attribute, since it might be an anonymous
16167 structure or union. This gets the type entered into our set of
16168 user defined types. */
16169
16170 static struct type *
16171 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
16172 {
16173 struct objfile *objfile = cu->per_objfile->objfile;
16174 struct type *type;
16175 struct attribute *attr;
16176 const char *name;
16177
16178 /* If the definition of this type lives in .debug_types, read that type.
16179 Don't follow DW_AT_specification though, that will take us back up
16180 the chain and we want to go down. */
16181 attr = die->attr (DW_AT_signature);
16182 if (attr != nullptr)
16183 {
16184 type = get_DW_AT_signature_type (die, attr, cu);
16185
16186 /* The type's CU may not be the same as CU.
16187 Ensure TYPE is recorded with CU in die_type_hash. */
16188 return set_die_type (die, type, cu);
16189 }
16190
16191 type = alloc_type (objfile);
16192 INIT_CPLUS_SPECIFIC (type);
16193
16194 name = dwarf2_name (die, cu);
16195 if (name != NULL)
16196 {
16197 if (cu->language == language_cplus
16198 || cu->language == language_d
16199 || cu->language == language_rust)
16200 {
16201 const char *full_name = dwarf2_full_name (name, die, cu);
16202
16203 /* dwarf2_full_name might have already finished building the DIE's
16204 type. If so, there is no need to continue. */
16205 if (get_die_type (die, cu) != NULL)
16206 return get_die_type (die, cu);
16207
16208 type->set_name (full_name);
16209 }
16210 else
16211 {
16212 /* The name is already allocated along with this objfile, so
16213 we don't need to duplicate it for the type. */
16214 type->set_name (name);
16215 }
16216 }
16217
16218 if (die->tag == DW_TAG_structure_type)
16219 {
16220 type->set_code (TYPE_CODE_STRUCT);
16221 }
16222 else if (die->tag == DW_TAG_union_type)
16223 {
16224 type->set_code (TYPE_CODE_UNION);
16225 }
16226 else
16227 {
16228 type->set_code (TYPE_CODE_STRUCT);
16229 }
16230
16231 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16232 TYPE_DECLARED_CLASS (type) = 1;
16233
16234 /* Store the calling convention in the type if it's available in
16235 the die. Otherwise the calling convention remains set to
16236 the default value DW_CC_normal. */
16237 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16238 if (attr != nullptr
16239 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
16240 {
16241 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16242 TYPE_CPLUS_CALLING_CONVENTION (type)
16243 = (enum dwarf_calling_convention) (attr->constant_value (0));
16244 }
16245
16246 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16247 if (attr != nullptr)
16248 {
16249 if (attr->form_is_constant ())
16250 TYPE_LENGTH (type) = attr->constant_value (0);
16251 else
16252 {
16253 struct dynamic_prop prop;
16254 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
16255 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
16256 TYPE_LENGTH (type) = 0;
16257 }
16258 }
16259 else
16260 {
16261 TYPE_LENGTH (type) = 0;
16262 }
16263
16264 maybe_set_alignment (cu, die, type);
16265
16266 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
16267 {
16268 /* ICC<14 does not output the required DW_AT_declaration on
16269 incomplete types, but gives them a size of zero. */
16270 type->set_is_stub (true);
16271 }
16272 else
16273 type->set_stub_is_supported (true);
16274
16275 if (die_is_declaration (die, cu))
16276 type->set_is_stub (true);
16277 else if (attr == NULL && die->child == NULL
16278 && producer_is_realview (cu->producer))
16279 /* RealView does not output the required DW_AT_declaration
16280 on incomplete types. */
16281 type->set_is_stub (true);
16282
16283 /* We need to add the type field to the die immediately so we don't
16284 infinitely recurse when dealing with pointers to the structure
16285 type within the structure itself. */
16286 set_die_type (die, type, cu);
16287
16288 /* set_die_type should be already done. */
16289 set_descriptive_type (type, die, cu);
16290
16291 return type;
16292 }
16293
16294 static void handle_struct_member_die
16295 (struct die_info *child_die,
16296 struct type *type,
16297 struct field_info *fi,
16298 std::vector<struct symbol *> *template_args,
16299 struct dwarf2_cu *cu);
16300
16301 /* A helper for handle_struct_member_die that handles
16302 DW_TAG_variant_part. */
16303
16304 static void
16305 handle_variant_part (struct die_info *die, struct type *type,
16306 struct field_info *fi,
16307 std::vector<struct symbol *> *template_args,
16308 struct dwarf2_cu *cu)
16309 {
16310 variant_part_builder *new_part;
16311 if (fi->current_variant_part == nullptr)
16312 {
16313 fi->variant_parts.emplace_back ();
16314 new_part = &fi->variant_parts.back ();
16315 }
16316 else if (!fi->current_variant_part->processing_variant)
16317 {
16318 complaint (_("nested DW_TAG_variant_part seen "
16319 "- DIE at %s [in module %s]"),
16320 sect_offset_str (die->sect_off),
16321 objfile_name (cu->per_objfile->objfile));
16322 return;
16323 }
16324 else
16325 {
16326 variant_field &current = fi->current_variant_part->variants.back ();
16327 current.variant_parts.emplace_back ();
16328 new_part = &current.variant_parts.back ();
16329 }
16330
16331 /* When we recurse, we want callees to add to this new variant
16332 part. */
16333 scoped_restore save_current_variant_part
16334 = make_scoped_restore (&fi->current_variant_part, new_part);
16335
16336 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16337 if (discr == NULL)
16338 {
16339 /* It's a univariant form, an extension we support. */
16340 }
16341 else if (discr->form_is_ref ())
16342 {
16343 struct dwarf2_cu *target_cu = cu;
16344 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16345
16346 new_part->discriminant_offset = target_die->sect_off;
16347 }
16348 else
16349 {
16350 complaint (_("DW_AT_discr does not have DIE reference form"
16351 " - DIE at %s [in module %s]"),
16352 sect_offset_str (die->sect_off),
16353 objfile_name (cu->per_objfile->objfile));
16354 }
16355
16356 for (die_info *child_die = die->child;
16357 child_die != NULL;
16358 child_die = child_die->sibling)
16359 handle_struct_member_die (child_die, type, fi, template_args, cu);
16360 }
16361
16362 /* A helper for handle_struct_member_die that handles
16363 DW_TAG_variant. */
16364
16365 static void
16366 handle_variant (struct die_info *die, struct type *type,
16367 struct field_info *fi,
16368 std::vector<struct symbol *> *template_args,
16369 struct dwarf2_cu *cu)
16370 {
16371 if (fi->current_variant_part == nullptr)
16372 {
16373 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
16374 "- DIE at %s [in module %s]"),
16375 sect_offset_str (die->sect_off),
16376 objfile_name (cu->per_objfile->objfile));
16377 return;
16378 }
16379 if (fi->current_variant_part->processing_variant)
16380 {
16381 complaint (_("nested DW_TAG_variant seen "
16382 "- DIE at %s [in module %s]"),
16383 sect_offset_str (die->sect_off),
16384 objfile_name (cu->per_objfile->objfile));
16385 return;
16386 }
16387
16388 scoped_restore save_processing_variant
16389 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16390 true);
16391
16392 fi->current_variant_part->variants.emplace_back ();
16393 variant_field &variant = fi->current_variant_part->variants.back ();
16394 variant.first_field = fi->fields.size ();
16395
16396 /* In a variant we want to get the discriminant and also add a
16397 field for our sole member child. */
16398 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
16399 if (discr == nullptr || !discr->form_is_constant ())
16400 {
16401 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
16402 if (discr == nullptr || discr->as_block ()->size == 0)
16403 variant.default_branch = true;
16404 else
16405 variant.discr_list_data = discr->as_block ();
16406 }
16407 else
16408 variant.discriminant_value = discr->constant_value (0);
16409
16410 for (die_info *variant_child = die->child;
16411 variant_child != NULL;
16412 variant_child = variant_child->sibling)
16413 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16414
16415 variant.last_field = fi->fields.size ();
16416 }
16417
16418 /* A helper for process_structure_scope that handles a single member
16419 DIE. */
16420
16421 static void
16422 handle_struct_member_die (struct die_info *child_die, struct type *type,
16423 struct field_info *fi,
16424 std::vector<struct symbol *> *template_args,
16425 struct dwarf2_cu *cu)
16426 {
16427 if (child_die->tag == DW_TAG_member
16428 || child_die->tag == DW_TAG_variable)
16429 {
16430 /* NOTE: carlton/2002-11-05: A C++ static data member
16431 should be a DW_TAG_member that is a declaration, but
16432 all versions of G++ as of this writing (so through at
16433 least 3.2.1) incorrectly generate DW_TAG_variable
16434 tags for them instead. */
16435 dwarf2_add_field (fi, child_die, cu);
16436 }
16437 else if (child_die->tag == DW_TAG_subprogram)
16438 {
16439 /* Rust doesn't have member functions in the C++ sense.
16440 However, it does emit ordinary functions as children
16441 of a struct DIE. */
16442 if (cu->language == language_rust)
16443 read_func_scope (child_die, cu);
16444 else
16445 {
16446 /* C++ member function. */
16447 dwarf2_add_member_fn (fi, child_die, type, cu);
16448 }
16449 }
16450 else if (child_die->tag == DW_TAG_inheritance)
16451 {
16452 /* C++ base class field. */
16453 dwarf2_add_field (fi, child_die, cu);
16454 }
16455 else if (type_can_define_types (child_die))
16456 dwarf2_add_type_defn (fi, child_die, cu);
16457 else if (child_die->tag == DW_TAG_template_type_param
16458 || child_die->tag == DW_TAG_template_value_param)
16459 {
16460 struct symbol *arg = new_symbol (child_die, NULL, cu);
16461
16462 if (arg != NULL)
16463 template_args->push_back (arg);
16464 }
16465 else if (child_die->tag == DW_TAG_variant_part)
16466 handle_variant_part (child_die, type, fi, template_args, cu);
16467 else if (child_die->tag == DW_TAG_variant)
16468 handle_variant (child_die, type, fi, template_args, cu);
16469 }
16470
16471 /* Finish creating a structure or union type, including filling in
16472 its members and creating a symbol for it. */
16473
16474 static void
16475 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16476 {
16477 struct objfile *objfile = cu->per_objfile->objfile;
16478 struct die_info *child_die;
16479 struct type *type;
16480
16481 type = get_die_type (die, cu);
16482 if (type == NULL)
16483 type = read_structure_type (die, cu);
16484
16485 bool has_template_parameters = false;
16486 if (die->child != NULL && ! die_is_declaration (die, cu))
16487 {
16488 struct field_info fi;
16489 std::vector<struct symbol *> template_args;
16490
16491 child_die = die->child;
16492
16493 while (child_die && child_die->tag)
16494 {
16495 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16496 child_die = child_die->sibling;
16497 }
16498
16499 /* Attach template arguments to type. */
16500 if (!template_args.empty ())
16501 {
16502 has_template_parameters = true;
16503 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16504 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16505 TYPE_TEMPLATE_ARGUMENTS (type)
16506 = XOBNEWVEC (&objfile->objfile_obstack,
16507 struct symbol *,
16508 TYPE_N_TEMPLATE_ARGUMENTS (type));
16509 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16510 template_args.data (),
16511 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16512 * sizeof (struct symbol *)));
16513 }
16514
16515 /* Attach fields and member functions to the type. */
16516 if (fi.nfields () > 0)
16517 dwarf2_attach_fields_to_type (&fi, type, cu);
16518 if (!fi.fnfieldlists.empty ())
16519 {
16520 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16521
16522 /* Get the type which refers to the base class (possibly this
16523 class itself) which contains the vtable pointer for the current
16524 class from the DW_AT_containing_type attribute. This use of
16525 DW_AT_containing_type is a GNU extension. */
16526
16527 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16528 {
16529 struct type *t = die_containing_type (die, cu);
16530
16531 set_type_vptr_basetype (type, t);
16532 if (type == t)
16533 {
16534 int i;
16535
16536 /* Our own class provides vtbl ptr. */
16537 for (i = t->num_fields () - 1;
16538 i >= TYPE_N_BASECLASSES (t);
16539 --i)
16540 {
16541 const char *fieldname = TYPE_FIELD_NAME (t, i);
16542
16543 if (is_vtable_name (fieldname, cu))
16544 {
16545 set_type_vptr_fieldno (type, i);
16546 break;
16547 }
16548 }
16549
16550 /* Complain if virtual function table field not found. */
16551 if (i < TYPE_N_BASECLASSES (t))
16552 complaint (_("virtual function table pointer "
16553 "not found when defining class '%s'"),
16554 type->name () ? type->name () : "");
16555 }
16556 else
16557 {
16558 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16559 }
16560 }
16561 else if (cu->producer
16562 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16563 {
16564 /* The IBM XLC compiler does not provide direct indication
16565 of the containing type, but the vtable pointer is
16566 always named __vfp. */
16567
16568 int i;
16569
16570 for (i = type->num_fields () - 1;
16571 i >= TYPE_N_BASECLASSES (type);
16572 --i)
16573 {
16574 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16575 {
16576 set_type_vptr_fieldno (type, i);
16577 set_type_vptr_basetype (type, type);
16578 break;
16579 }
16580 }
16581 }
16582 }
16583
16584 /* Copy fi.typedef_field_list linked list elements content into the
16585 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16586 if (!fi.typedef_field_list.empty ())
16587 {
16588 int count = fi.typedef_field_list.size ();
16589
16590 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16591 TYPE_TYPEDEF_FIELD_ARRAY (type)
16592 = ((struct decl_field *)
16593 TYPE_ALLOC (type,
16594 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16595 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16596
16597 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16598 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16599 }
16600
16601 /* Copy fi.nested_types_list linked list elements content into the
16602 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16603 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16604 {
16605 int count = fi.nested_types_list.size ();
16606
16607 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16608 TYPE_NESTED_TYPES_ARRAY (type)
16609 = ((struct decl_field *)
16610 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16611 TYPE_NESTED_TYPES_COUNT (type) = count;
16612
16613 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16614 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16615 }
16616 }
16617
16618 quirk_gcc_member_function_pointer (type, objfile);
16619 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16620 cu->rust_unions.push_back (type);
16621 else if (cu->language == language_ada)
16622 quirk_ada_thick_pointer_struct (die, cu, type);
16623
16624 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16625 snapshots) has been known to create a die giving a declaration
16626 for a class that has, as a child, a die giving a definition for a
16627 nested class. So we have to process our children even if the
16628 current die is a declaration. Normally, of course, a declaration
16629 won't have any children at all. */
16630
16631 child_die = die->child;
16632
16633 while (child_die != NULL && child_die->tag)
16634 {
16635 if (child_die->tag == DW_TAG_member
16636 || child_die->tag == DW_TAG_variable
16637 || child_die->tag == DW_TAG_inheritance
16638 || child_die->tag == DW_TAG_template_value_param
16639 || child_die->tag == DW_TAG_template_type_param)
16640 {
16641 /* Do nothing. */
16642 }
16643 else
16644 process_die (child_die, cu);
16645
16646 child_die = child_die->sibling;
16647 }
16648
16649 /* Do not consider external references. According to the DWARF standard,
16650 these DIEs are identified by the fact that they have no byte_size
16651 attribute, and a declaration attribute. */
16652 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16653 || !die_is_declaration (die, cu)
16654 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16655 {
16656 struct symbol *sym = new_symbol (die, type, cu);
16657
16658 if (has_template_parameters)
16659 {
16660 struct symtab *symtab;
16661 if (sym != nullptr)
16662 symtab = symbol_symtab (sym);
16663 else if (cu->line_header != nullptr)
16664 {
16665 /* Any related symtab will do. */
16666 symtab
16667 = cu->line_header->file_names ()[0].symtab;
16668 }
16669 else
16670 {
16671 symtab = nullptr;
16672 complaint (_("could not find suitable "
16673 "symtab for template parameter"
16674 " - DIE at %s [in module %s]"),
16675 sect_offset_str (die->sect_off),
16676 objfile_name (objfile));
16677 }
16678
16679 if (symtab != nullptr)
16680 {
16681 /* Make sure that the symtab is set on the new symbols.
16682 Even though they don't appear in this symtab directly,
16683 other parts of gdb assume that symbols do, and this is
16684 reasonably true. */
16685 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16686 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16687 }
16688 }
16689 }
16690 }
16691
16692 /* Assuming DIE is an enumeration type, and TYPE is its associated
16693 type, update TYPE using some information only available in DIE's
16694 children. In particular, the fields are computed. */
16695
16696 static void
16697 update_enumeration_type_from_children (struct die_info *die,
16698 struct type *type,
16699 struct dwarf2_cu *cu)
16700 {
16701 struct die_info *child_die;
16702 int unsigned_enum = 1;
16703 int flag_enum = 1;
16704
16705 auto_obstack obstack;
16706 std::vector<struct field> fields;
16707
16708 for (child_die = die->child;
16709 child_die != NULL && child_die->tag;
16710 child_die = child_die->sibling)
16711 {
16712 struct attribute *attr;
16713 LONGEST value;
16714 const gdb_byte *bytes;
16715 struct dwarf2_locexpr_baton *baton;
16716 const char *name;
16717
16718 if (child_die->tag != DW_TAG_enumerator)
16719 continue;
16720
16721 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16722 if (attr == NULL)
16723 continue;
16724
16725 name = dwarf2_name (child_die, cu);
16726 if (name == NULL)
16727 name = "<anonymous enumerator>";
16728
16729 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16730 &value, &bytes, &baton);
16731 if (value < 0)
16732 {
16733 unsigned_enum = 0;
16734 flag_enum = 0;
16735 }
16736 else
16737 {
16738 if (count_one_bits_ll (value) >= 2)
16739 flag_enum = 0;
16740 }
16741
16742 fields.emplace_back ();
16743 struct field &field = fields.back ();
16744 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16745 SET_FIELD_ENUMVAL (field, value);
16746 }
16747
16748 if (!fields.empty ())
16749 {
16750 type->set_num_fields (fields.size ());
16751 type->set_fields
16752 ((struct field *)
16753 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16754 memcpy (type->fields (), fields.data (),
16755 sizeof (struct field) * fields.size ());
16756 }
16757
16758 if (unsigned_enum)
16759 type->set_is_unsigned (true);
16760
16761 if (flag_enum)
16762 TYPE_FLAG_ENUM (type) = 1;
16763 }
16764
16765 /* Given a DW_AT_enumeration_type die, set its type. We do not
16766 complete the type's fields yet, or create any symbols. */
16767
16768 static struct type *
16769 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16770 {
16771 struct objfile *objfile = cu->per_objfile->objfile;
16772 struct type *type;
16773 struct attribute *attr;
16774 const char *name;
16775
16776 /* If the definition of this type lives in .debug_types, read that type.
16777 Don't follow DW_AT_specification though, that will take us back up
16778 the chain and we want to go down. */
16779 attr = die->attr (DW_AT_signature);
16780 if (attr != nullptr)
16781 {
16782 type = get_DW_AT_signature_type (die, attr, cu);
16783
16784 /* The type's CU may not be the same as CU.
16785 Ensure TYPE is recorded with CU in die_type_hash. */
16786 return set_die_type (die, type, cu);
16787 }
16788
16789 type = alloc_type (objfile);
16790
16791 type->set_code (TYPE_CODE_ENUM);
16792 name = dwarf2_full_name (NULL, die, cu);
16793 if (name != NULL)
16794 type->set_name (name);
16795
16796 attr = dwarf2_attr (die, DW_AT_type, cu);
16797 if (attr != NULL)
16798 {
16799 struct type *underlying_type = die_type (die, cu);
16800
16801 TYPE_TARGET_TYPE (type) = underlying_type;
16802 }
16803
16804 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16805 if (attr != nullptr)
16806 {
16807 TYPE_LENGTH (type) = attr->constant_value (0);
16808 }
16809 else
16810 {
16811 TYPE_LENGTH (type) = 0;
16812 }
16813
16814 maybe_set_alignment (cu, die, type);
16815
16816 /* The enumeration DIE can be incomplete. In Ada, any type can be
16817 declared as private in the package spec, and then defined only
16818 inside the package body. Such types are known as Taft Amendment
16819 Types. When another package uses such a type, an incomplete DIE
16820 may be generated by the compiler. */
16821 if (die_is_declaration (die, cu))
16822 type->set_is_stub (true);
16823
16824 /* If this type has an underlying type that is not a stub, then we
16825 may use its attributes. We always use the "unsigned" attribute
16826 in this situation, because ordinarily we guess whether the type
16827 is unsigned -- but the guess can be wrong and the underlying type
16828 can tell us the reality. However, we defer to a local size
16829 attribute if one exists, because this lets the compiler override
16830 the underlying type if needed. */
16831 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16832 {
16833 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16834 underlying_type = check_typedef (underlying_type);
16835
16836 type->set_is_unsigned (underlying_type->is_unsigned ());
16837
16838 if (TYPE_LENGTH (type) == 0)
16839 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16840
16841 if (TYPE_RAW_ALIGN (type) == 0
16842 && TYPE_RAW_ALIGN (underlying_type) != 0)
16843 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16844 }
16845
16846 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16847
16848 set_die_type (die, type, cu);
16849
16850 /* Finish the creation of this type by using the enum's children.
16851 Note that, as usual, this must come after set_die_type to avoid
16852 infinite recursion when trying to compute the names of the
16853 enumerators. */
16854 update_enumeration_type_from_children (die, type, cu);
16855
16856 return type;
16857 }
16858
16859 /* Given a pointer to a die which begins an enumeration, process all
16860 the dies that define the members of the enumeration, and create the
16861 symbol for the enumeration type.
16862
16863 NOTE: We reverse the order of the element list. */
16864
16865 static void
16866 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16867 {
16868 struct type *this_type;
16869
16870 this_type = get_die_type (die, cu);
16871 if (this_type == NULL)
16872 this_type = read_enumeration_type (die, cu);
16873
16874 if (die->child != NULL)
16875 {
16876 struct die_info *child_die;
16877 const char *name;
16878
16879 child_die = die->child;
16880 while (child_die && child_die->tag)
16881 {
16882 if (child_die->tag != DW_TAG_enumerator)
16883 {
16884 process_die (child_die, cu);
16885 }
16886 else
16887 {
16888 name = dwarf2_name (child_die, cu);
16889 if (name)
16890 new_symbol (child_die, this_type, cu);
16891 }
16892
16893 child_die = child_die->sibling;
16894 }
16895 }
16896
16897 /* If we are reading an enum from a .debug_types unit, and the enum
16898 is a declaration, and the enum is not the signatured type in the
16899 unit, then we do not want to add a symbol for it. Adding a
16900 symbol would in some cases obscure the true definition of the
16901 enum, giving users an incomplete type when the definition is
16902 actually available. Note that we do not want to do this for all
16903 enums which are just declarations, because C++0x allows forward
16904 enum declarations. */
16905 if (cu->per_cu->is_debug_types
16906 && die_is_declaration (die, cu))
16907 {
16908 struct signatured_type *sig_type;
16909
16910 sig_type = (struct signatured_type *) cu->per_cu;
16911 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16912 if (sig_type->type_offset_in_section != die->sect_off)
16913 return;
16914 }
16915
16916 new_symbol (die, this_type, cu);
16917 }
16918
16919 /* Helper function for quirk_ada_thick_pointer that examines a bounds
16920 expression for an index type and finds the corresponding field
16921 offset in the hidden "P_BOUNDS" structure. Returns true on success
16922 and updates *FIELD, false if it fails to recognize an
16923 expression. */
16924
16925 static bool
16926 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16927 int *bounds_offset, struct field *field,
16928 struct dwarf2_cu *cu)
16929 {
16930 struct attribute *attr = dwarf2_attr (die, name, cu);
16931 if (attr == nullptr || !attr->form_is_block ())
16932 return false;
16933
16934 const struct dwarf_block *block = attr->as_block ();
16935 const gdb_byte *start = block->data;
16936 const gdb_byte *end = block->data + block->size;
16937
16938 /* The expression to recognize generally looks like:
16939
16940 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16941 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16942
16943 However, the second "plus_uconst" may be missing:
16944
16945 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16946 DW_OP_deref_size: 4)
16947
16948 This happens when the field is at the start of the structure.
16949
16950 Also, the final deref may not be sized:
16951
16952 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16953 DW_OP_deref)
16954
16955 This happens when the size of the index type happens to be the
16956 same as the architecture's word size. This can occur with or
16957 without the second plus_uconst. */
16958
16959 if (end - start < 2)
16960 return false;
16961 if (*start++ != DW_OP_push_object_address)
16962 return false;
16963 if (*start++ != DW_OP_plus_uconst)
16964 return false;
16965
16966 uint64_t this_bound_off;
16967 start = gdb_read_uleb128 (start, end, &this_bound_off);
16968 if (start == nullptr || (int) this_bound_off != this_bound_off)
16969 return false;
16970 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16971 is consistent among all bounds. */
16972 if (*bounds_offset == -1)
16973 *bounds_offset = this_bound_off;
16974 else if (*bounds_offset != this_bound_off)
16975 return false;
16976
16977 if (start == end || *start++ != DW_OP_deref)
16978 return false;
16979
16980 int offset = 0;
16981 if (start ==end)
16982 return false;
16983 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16984 {
16985 /* This means an offset of 0. */
16986 }
16987 else if (*start++ != DW_OP_plus_uconst)
16988 return false;
16989 else
16990 {
16991 /* The size is the parameter to DW_OP_plus_uconst. */
16992 uint64_t val;
16993 start = gdb_read_uleb128 (start, end, &val);
16994 if (start == nullptr)
16995 return false;
16996 if ((int) val != val)
16997 return false;
16998 offset = val;
16999 }
17000
17001 if (start == end)
17002 return false;
17003
17004 uint64_t size;
17005 if (*start == DW_OP_deref_size)
17006 {
17007 start = gdb_read_uleb128 (start + 1, end, &size);
17008 if (start == nullptr)
17009 return false;
17010 }
17011 else if (*start == DW_OP_deref)
17012 {
17013 size = cu->header.addr_size;
17014 ++start;
17015 }
17016 else
17017 return false;
17018
17019 SET_FIELD_BITPOS (*field, 8 * offset);
17020 if (size != TYPE_LENGTH (field->type ()))
17021 FIELD_BITSIZE (*field) = 8 * size;
17022
17023 return true;
17024 }
17025
17026 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
17027 some kinds of Ada arrays:
17028
17029 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
17030 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
17031 <11e0> DW_AT_data_location: 2 byte block: 97 6
17032 (DW_OP_push_object_address; DW_OP_deref)
17033 <11e3> DW_AT_type : <0x1173>
17034 <11e7> DW_AT_sibling : <0x1201>
17035 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
17036 <11ec> DW_AT_type : <0x1206>
17037 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
17038 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
17039 DW_OP_deref_size: 4)
17040 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
17041 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
17042 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
17043
17044 This actually represents a "thick pointer", which is a structure
17045 with two elements: one that is a pointer to the array data, and one
17046 that is a pointer to another structure; this second structure holds
17047 the array bounds.
17048
17049 This returns a new type on success, or nullptr if this didn't
17050 recognize the type. */
17051
17052 static struct type *
17053 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
17054 struct type *type)
17055 {
17056 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
17057 /* So far we've only seen this with block form. */
17058 if (attr == nullptr || !attr->form_is_block ())
17059 return nullptr;
17060
17061 /* Note that this will fail if the structure layout is changed by
17062 the compiler. However, we have no good way to recognize some
17063 other layout, because we don't know what expression the compiler
17064 might choose to emit should this happen. */
17065 struct dwarf_block *blk = attr->as_block ();
17066 if (blk->size != 2
17067 || blk->data[0] != DW_OP_push_object_address
17068 || blk->data[1] != DW_OP_deref)
17069 return nullptr;
17070
17071 int bounds_offset = -1;
17072 int max_align = -1;
17073 std::vector<struct field> range_fields;
17074 for (struct die_info *child_die = die->child;
17075 child_die;
17076 child_die = child_die->sibling)
17077 {
17078 if (child_die->tag == DW_TAG_subrange_type)
17079 {
17080 struct type *underlying = read_subrange_index_type (child_die, cu);
17081
17082 int this_align = type_align (underlying);
17083 if (this_align > max_align)
17084 max_align = this_align;
17085
17086 range_fields.emplace_back ();
17087 range_fields.emplace_back ();
17088
17089 struct field &lower = range_fields[range_fields.size () - 2];
17090 struct field &upper = range_fields[range_fields.size () - 1];
17091
17092 lower.set_type (underlying);
17093 FIELD_ARTIFICIAL (lower) = 1;
17094
17095 upper.set_type (underlying);
17096 FIELD_ARTIFICIAL (upper) = 1;
17097
17098 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
17099 &bounds_offset, &lower, cu)
17100 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
17101 &bounds_offset, &upper, cu))
17102 return nullptr;
17103 }
17104 }
17105
17106 /* This shouldn't really happen, but double-check that we found
17107 where the bounds are stored. */
17108 if (bounds_offset == -1)
17109 return nullptr;
17110
17111 struct objfile *objfile = cu->per_objfile->objfile;
17112 for (int i = 0; i < range_fields.size (); i += 2)
17113 {
17114 char name[20];
17115
17116 /* Set the name of each field in the bounds. */
17117 xsnprintf (name, sizeof (name), "LB%d", i / 2);
17118 FIELD_NAME (range_fields[i]) = objfile->intern (name);
17119 xsnprintf (name, sizeof (name), "UB%d", i / 2);
17120 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
17121 }
17122
17123 struct type *bounds = alloc_type (objfile);
17124 bounds->set_code (TYPE_CODE_STRUCT);
17125
17126 bounds->set_num_fields (range_fields.size ());
17127 bounds->set_fields
17128 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
17129 * sizeof (struct field))));
17130 memcpy (bounds->fields (), range_fields.data (),
17131 bounds->num_fields () * sizeof (struct field));
17132
17133 int last_fieldno = range_fields.size () - 1;
17134 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
17135 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
17136 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
17137
17138 /* Rewrite the existing array type in place. Specifically, we
17139 remove any dynamic properties we might have read, and we replace
17140 the index types. */
17141 struct type *iter = type;
17142 for (int i = 0; i < range_fields.size (); i += 2)
17143 {
17144 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
17145 iter->main_type->dyn_prop_list = nullptr;
17146 iter->set_index_type
17147 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
17148 iter = TYPE_TARGET_TYPE (iter);
17149 }
17150
17151 struct type *result = alloc_type (objfile);
17152 result->set_code (TYPE_CODE_STRUCT);
17153
17154 result->set_num_fields (2);
17155 result->set_fields
17156 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
17157 * sizeof (struct field))));
17158
17159 /* The names are chosen to coincide with what the compiler does with
17160 -fgnat-encodings=all, which the Ada code in gdb already
17161 understands. */
17162 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
17163 result->field (0).set_type (lookup_pointer_type (type));
17164
17165 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
17166 result->field (1).set_type (lookup_pointer_type (bounds));
17167 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
17168
17169 result->set_name (type->name ());
17170 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
17171 + TYPE_LENGTH (result->field (1).type ()));
17172
17173 return result;
17174 }
17175
17176 /* Extract all information from a DW_TAG_array_type DIE and put it in
17177 the DIE's type field. For now, this only handles one dimensional
17178 arrays. */
17179
17180 static struct type *
17181 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
17182 {
17183 struct objfile *objfile = cu->per_objfile->objfile;
17184 struct die_info *child_die;
17185 struct type *type;
17186 struct type *element_type, *range_type, *index_type;
17187 struct attribute *attr;
17188 const char *name;
17189 struct dynamic_prop *byte_stride_prop = NULL;
17190 unsigned int bit_stride = 0;
17191
17192 element_type = die_type (die, cu);
17193
17194 /* The die_type call above may have already set the type for this DIE. */
17195 type = get_die_type (die, cu);
17196 if (type)
17197 return type;
17198
17199 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
17200 if (attr != NULL)
17201 {
17202 int stride_ok;
17203 struct type *prop_type = cu->addr_sized_int_type (false);
17204
17205 byte_stride_prop
17206 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
17207 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
17208 prop_type);
17209 if (!stride_ok)
17210 {
17211 complaint (_("unable to read array DW_AT_byte_stride "
17212 " - DIE at %s [in module %s]"),
17213 sect_offset_str (die->sect_off),
17214 objfile_name (cu->per_objfile->objfile));
17215 /* Ignore this attribute. We will likely not be able to print
17216 arrays of this type correctly, but there is little we can do
17217 to help if we cannot read the attribute's value. */
17218 byte_stride_prop = NULL;
17219 }
17220 }
17221
17222 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
17223 if (attr != NULL)
17224 bit_stride = attr->constant_value (0);
17225
17226 /* Irix 6.2 native cc creates array types without children for
17227 arrays with unspecified length. */
17228 if (die->child == NULL)
17229 {
17230 index_type = objfile_type (objfile)->builtin_int;
17231 range_type = create_static_range_type (NULL, index_type, 0, -1);
17232 type = create_array_type_with_stride (NULL, element_type, range_type,
17233 byte_stride_prop, bit_stride);
17234 return set_die_type (die, type, cu);
17235 }
17236
17237 std::vector<struct type *> range_types;
17238 child_die = die->child;
17239 while (child_die && child_die->tag)
17240 {
17241 if (child_die->tag == DW_TAG_subrange_type)
17242 {
17243 struct type *child_type = read_type_die (child_die, cu);
17244
17245 if (child_type != NULL)
17246 {
17247 /* The range type was succesfully read. Save it for the
17248 array type creation. */
17249 range_types.push_back (child_type);
17250 }
17251 }
17252 child_die = child_die->sibling;
17253 }
17254
17255 if (range_types.empty ())
17256 {
17257 complaint (_("unable to find array range - DIE at %s [in module %s]"),
17258 sect_offset_str (die->sect_off),
17259 objfile_name (cu->per_objfile->objfile));
17260 return NULL;
17261 }
17262
17263 /* Dwarf2 dimensions are output from left to right, create the
17264 necessary array types in backwards order. */
17265
17266 type = element_type;
17267
17268 if (read_array_order (die, cu) == DW_ORD_col_major)
17269 {
17270 int i = 0;
17271
17272 while (i < range_types.size ())
17273 {
17274 type = create_array_type_with_stride (NULL, type, range_types[i++],
17275 byte_stride_prop, bit_stride);
17276 bit_stride = 0;
17277 byte_stride_prop = nullptr;
17278 }
17279 }
17280 else
17281 {
17282 size_t ndim = range_types.size ();
17283 while (ndim-- > 0)
17284 {
17285 type = create_array_type_with_stride (NULL, type, range_types[ndim],
17286 byte_stride_prop, bit_stride);
17287 bit_stride = 0;
17288 byte_stride_prop = nullptr;
17289 }
17290 }
17291
17292 gdb_assert (type != element_type);
17293
17294 /* Understand Dwarf2 support for vector types (like they occur on
17295 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
17296 array type. This is not part of the Dwarf2/3 standard yet, but a
17297 custom vendor extension. The main difference between a regular
17298 array and the vector variant is that vectors are passed by value
17299 to functions. */
17300 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
17301 if (attr != nullptr)
17302 make_vector_type (type);
17303
17304 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
17305 implementation may choose to implement triple vectors using this
17306 attribute. */
17307 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17308 if (attr != nullptr && attr->form_is_unsigned ())
17309 {
17310 if (attr->as_unsigned () >= TYPE_LENGTH (type))
17311 TYPE_LENGTH (type) = attr->as_unsigned ();
17312 else
17313 complaint (_("DW_AT_byte_size for array type smaller "
17314 "than the total size of elements"));
17315 }
17316
17317 name = dwarf2_name (die, cu);
17318 if (name)
17319 type->set_name (name);
17320
17321 maybe_set_alignment (cu, die, type);
17322
17323 struct type *replacement_type = nullptr;
17324 if (cu->language == language_ada)
17325 {
17326 replacement_type = quirk_ada_thick_pointer (die, cu, type);
17327 if (replacement_type != nullptr)
17328 type = replacement_type;
17329 }
17330
17331 /* Install the type in the die. */
17332 set_die_type (die, type, cu, replacement_type != nullptr);
17333
17334 /* set_die_type should be already done. */
17335 set_descriptive_type (type, die, cu);
17336
17337 return type;
17338 }
17339
17340 static enum dwarf_array_dim_ordering
17341 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
17342 {
17343 struct attribute *attr;
17344
17345 attr = dwarf2_attr (die, DW_AT_ordering, cu);
17346
17347 if (attr != nullptr)
17348 {
17349 LONGEST val = attr->constant_value (-1);
17350 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
17351 return (enum dwarf_array_dim_ordering) val;
17352 }
17353
17354 /* GNU F77 is a special case, as at 08/2004 array type info is the
17355 opposite order to the dwarf2 specification, but data is still
17356 laid out as per normal fortran.
17357
17358 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
17359 version checking. */
17360
17361 if (cu->language == language_fortran
17362 && cu->producer && strstr (cu->producer, "GNU F77"))
17363 {
17364 return DW_ORD_row_major;
17365 }
17366
17367 switch (cu->language_defn->array_ordering ())
17368 {
17369 case array_column_major:
17370 return DW_ORD_col_major;
17371 case array_row_major:
17372 default:
17373 return DW_ORD_row_major;
17374 };
17375 }
17376
17377 /* Extract all information from a DW_TAG_set_type DIE and put it in
17378 the DIE's type field. */
17379
17380 static struct type *
17381 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
17382 {
17383 struct type *domain_type, *set_type;
17384 struct attribute *attr;
17385
17386 domain_type = die_type (die, cu);
17387
17388 /* The die_type call above may have already set the type for this DIE. */
17389 set_type = get_die_type (die, cu);
17390 if (set_type)
17391 return set_type;
17392
17393 set_type = create_set_type (NULL, domain_type);
17394
17395 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17396 if (attr != nullptr && attr->form_is_unsigned ())
17397 TYPE_LENGTH (set_type) = attr->as_unsigned ();
17398
17399 maybe_set_alignment (cu, die, set_type);
17400
17401 return set_die_type (die, set_type, cu);
17402 }
17403
17404 /* A helper for read_common_block that creates a locexpr baton.
17405 SYM is the symbol which we are marking as computed.
17406 COMMON_DIE is the DIE for the common block.
17407 COMMON_LOC is the location expression attribute for the common
17408 block itself.
17409 MEMBER_LOC is the location expression attribute for the particular
17410 member of the common block that we are processing.
17411 CU is the CU from which the above come. */
17412
17413 static void
17414 mark_common_block_symbol_computed (struct symbol *sym,
17415 struct die_info *common_die,
17416 struct attribute *common_loc,
17417 struct attribute *member_loc,
17418 struct dwarf2_cu *cu)
17419 {
17420 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17421 struct objfile *objfile = per_objfile->objfile;
17422 struct dwarf2_locexpr_baton *baton;
17423 gdb_byte *ptr;
17424 unsigned int cu_off;
17425 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
17426 LONGEST offset = 0;
17427
17428 gdb_assert (common_loc && member_loc);
17429 gdb_assert (common_loc->form_is_block ());
17430 gdb_assert (member_loc->form_is_block ()
17431 || member_loc->form_is_constant ());
17432
17433 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
17434 baton->per_objfile = per_objfile;
17435 baton->per_cu = cu->per_cu;
17436 gdb_assert (baton->per_cu);
17437
17438 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17439
17440 if (member_loc->form_is_constant ())
17441 {
17442 offset = member_loc->constant_value (0);
17443 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17444 }
17445 else
17446 baton->size += member_loc->as_block ()->size;
17447
17448 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
17449 baton->data = ptr;
17450
17451 *ptr++ = DW_OP_call4;
17452 cu_off = common_die->sect_off - cu->per_cu->sect_off;
17453 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17454 ptr += 4;
17455
17456 if (member_loc->form_is_constant ())
17457 {
17458 *ptr++ = DW_OP_addr;
17459 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17460 ptr += cu->header.addr_size;
17461 }
17462 else
17463 {
17464 /* We have to copy the data here, because DW_OP_call4 will only
17465 use a DW_AT_location attribute. */
17466 struct dwarf_block *block = member_loc->as_block ();
17467 memcpy (ptr, block->data, block->size);
17468 ptr += block->size;
17469 }
17470
17471 *ptr++ = DW_OP_plus;
17472 gdb_assert (ptr - baton->data == baton->size);
17473
17474 SYMBOL_LOCATION_BATON (sym) = baton;
17475 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
17476 }
17477
17478 /* Create appropriate locally-scoped variables for all the
17479 DW_TAG_common_block entries. Also create a struct common_block
17480 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
17481 is used to separate the common blocks name namespace from regular
17482 variable names. */
17483
17484 static void
17485 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
17486 {
17487 struct attribute *attr;
17488
17489 attr = dwarf2_attr (die, DW_AT_location, cu);
17490 if (attr != nullptr)
17491 {
17492 /* Support the .debug_loc offsets. */
17493 if (attr->form_is_block ())
17494 {
17495 /* Ok. */
17496 }
17497 else if (attr->form_is_section_offset ())
17498 {
17499 dwarf2_complex_location_expr_complaint ();
17500 attr = NULL;
17501 }
17502 else
17503 {
17504 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17505 "common block member");
17506 attr = NULL;
17507 }
17508 }
17509
17510 if (die->child != NULL)
17511 {
17512 struct objfile *objfile = cu->per_objfile->objfile;
17513 struct die_info *child_die;
17514 size_t n_entries = 0, size;
17515 struct common_block *common_block;
17516 struct symbol *sym;
17517
17518 for (child_die = die->child;
17519 child_die && child_die->tag;
17520 child_die = child_die->sibling)
17521 ++n_entries;
17522
17523 size = (sizeof (struct common_block)
17524 + (n_entries - 1) * sizeof (struct symbol *));
17525 common_block
17526 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17527 size);
17528 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17529 common_block->n_entries = 0;
17530
17531 for (child_die = die->child;
17532 child_die && child_die->tag;
17533 child_die = child_die->sibling)
17534 {
17535 /* Create the symbol in the DW_TAG_common_block block in the current
17536 symbol scope. */
17537 sym = new_symbol (child_die, NULL, cu);
17538 if (sym != NULL)
17539 {
17540 struct attribute *member_loc;
17541
17542 common_block->contents[common_block->n_entries++] = sym;
17543
17544 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17545 cu);
17546 if (member_loc)
17547 {
17548 /* GDB has handled this for a long time, but it is
17549 not specified by DWARF. It seems to have been
17550 emitted by gfortran at least as recently as:
17551 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17552 complaint (_("Variable in common block has "
17553 "DW_AT_data_member_location "
17554 "- DIE at %s [in module %s]"),
17555 sect_offset_str (child_die->sect_off),
17556 objfile_name (objfile));
17557
17558 if (member_loc->form_is_section_offset ())
17559 dwarf2_complex_location_expr_complaint ();
17560 else if (member_loc->form_is_constant ()
17561 || member_loc->form_is_block ())
17562 {
17563 if (attr != nullptr)
17564 mark_common_block_symbol_computed (sym, die, attr,
17565 member_loc, cu);
17566 }
17567 else
17568 dwarf2_complex_location_expr_complaint ();
17569 }
17570 }
17571 }
17572
17573 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17574 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
17575 }
17576 }
17577
17578 /* Create a type for a C++ namespace. */
17579
17580 static struct type *
17581 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
17582 {
17583 struct objfile *objfile = cu->per_objfile->objfile;
17584 const char *previous_prefix, *name;
17585 int is_anonymous;
17586 struct type *type;
17587
17588 /* For extensions, reuse the type of the original namespace. */
17589 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17590 {
17591 struct die_info *ext_die;
17592 struct dwarf2_cu *ext_cu = cu;
17593
17594 ext_die = dwarf2_extension (die, &ext_cu);
17595 type = read_type_die (ext_die, ext_cu);
17596
17597 /* EXT_CU may not be the same as CU.
17598 Ensure TYPE is recorded with CU in die_type_hash. */
17599 return set_die_type (die, type, cu);
17600 }
17601
17602 name = namespace_name (die, &is_anonymous, cu);
17603
17604 /* Now build the name of the current namespace. */
17605
17606 previous_prefix = determine_prefix (die, cu);
17607 if (previous_prefix[0] != '\0')
17608 name = typename_concat (&objfile->objfile_obstack,
17609 previous_prefix, name, 0, cu);
17610
17611 /* Create the type. */
17612 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17613
17614 return set_die_type (die, type, cu);
17615 }
17616
17617 /* Read a namespace scope. */
17618
17619 static void
17620 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17621 {
17622 struct objfile *objfile = cu->per_objfile->objfile;
17623 int is_anonymous;
17624
17625 /* Add a symbol associated to this if we haven't seen the namespace
17626 before. Also, add a using directive if it's an anonymous
17627 namespace. */
17628
17629 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17630 {
17631 struct type *type;
17632
17633 type = read_type_die (die, cu);
17634 new_symbol (die, type, cu);
17635
17636 namespace_name (die, &is_anonymous, cu);
17637 if (is_anonymous)
17638 {
17639 const char *previous_prefix = determine_prefix (die, cu);
17640
17641 std::vector<const char *> excludes;
17642 add_using_directive (using_directives (cu),
17643 previous_prefix, type->name (), NULL,
17644 NULL, excludes, 0, &objfile->objfile_obstack);
17645 }
17646 }
17647
17648 if (die->child != NULL)
17649 {
17650 struct die_info *child_die = die->child;
17651
17652 while (child_die && child_die->tag)
17653 {
17654 process_die (child_die, cu);
17655 child_die = child_die->sibling;
17656 }
17657 }
17658 }
17659
17660 /* Read a Fortran module as type. This DIE can be only a declaration used for
17661 imported module. Still we need that type as local Fortran "use ... only"
17662 declaration imports depend on the created type in determine_prefix. */
17663
17664 static struct type *
17665 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17666 {
17667 struct objfile *objfile = cu->per_objfile->objfile;
17668 const char *module_name;
17669 struct type *type;
17670
17671 module_name = dwarf2_name (die, cu);
17672 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17673
17674 return set_die_type (die, type, cu);
17675 }
17676
17677 /* Read a Fortran module. */
17678
17679 static void
17680 read_module (struct die_info *die, struct dwarf2_cu *cu)
17681 {
17682 struct die_info *child_die = die->child;
17683 struct type *type;
17684
17685 type = read_type_die (die, cu);
17686 new_symbol (die, type, cu);
17687
17688 while (child_die && child_die->tag)
17689 {
17690 process_die (child_die, cu);
17691 child_die = child_die->sibling;
17692 }
17693 }
17694
17695 /* Return the name of the namespace represented by DIE. Set
17696 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17697 namespace. */
17698
17699 static const char *
17700 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17701 {
17702 struct die_info *current_die;
17703 const char *name = NULL;
17704
17705 /* Loop through the extensions until we find a name. */
17706
17707 for (current_die = die;
17708 current_die != NULL;
17709 current_die = dwarf2_extension (die, &cu))
17710 {
17711 /* We don't use dwarf2_name here so that we can detect the absence
17712 of a name -> anonymous namespace. */
17713 name = dwarf2_string_attr (die, DW_AT_name, cu);
17714
17715 if (name != NULL)
17716 break;
17717 }
17718
17719 /* Is it an anonymous namespace? */
17720
17721 *is_anonymous = (name == NULL);
17722 if (*is_anonymous)
17723 name = CP_ANONYMOUS_NAMESPACE_STR;
17724
17725 return name;
17726 }
17727
17728 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17729 the user defined type vector. */
17730
17731 static struct type *
17732 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17733 {
17734 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
17735 struct comp_unit_head *cu_header = &cu->header;
17736 struct type *type;
17737 struct attribute *attr_byte_size;
17738 struct attribute *attr_address_class;
17739 int byte_size, addr_class;
17740 struct type *target_type;
17741
17742 target_type = die_type (die, cu);
17743
17744 /* The die_type call above may have already set the type for this DIE. */
17745 type = get_die_type (die, cu);
17746 if (type)
17747 return type;
17748
17749 type = lookup_pointer_type (target_type);
17750
17751 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17752 if (attr_byte_size)
17753 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
17754 else
17755 byte_size = cu_header->addr_size;
17756
17757 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17758 if (attr_address_class)
17759 addr_class = attr_address_class->constant_value (DW_ADDR_none);
17760 else
17761 addr_class = DW_ADDR_none;
17762
17763 ULONGEST alignment = get_alignment (cu, die);
17764
17765 /* If the pointer size, alignment, or address class is different
17766 than the default, create a type variant marked as such and set
17767 the length accordingly. */
17768 if (TYPE_LENGTH (type) != byte_size
17769 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17770 && alignment != TYPE_RAW_ALIGN (type))
17771 || addr_class != DW_ADDR_none)
17772 {
17773 if (gdbarch_address_class_type_flags_p (gdbarch))
17774 {
17775 type_instance_flags type_flags
17776 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17777 addr_class);
17778 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17779 == 0);
17780 type = make_type_with_address_space (type, type_flags);
17781 }
17782 else if (TYPE_LENGTH (type) != byte_size)
17783 {
17784 complaint (_("invalid pointer size %d"), byte_size);
17785 }
17786 else if (TYPE_RAW_ALIGN (type) != alignment)
17787 {
17788 complaint (_("Invalid DW_AT_alignment"
17789 " - DIE at %s [in module %s]"),
17790 sect_offset_str (die->sect_off),
17791 objfile_name (cu->per_objfile->objfile));
17792 }
17793 else
17794 {
17795 /* Should we also complain about unhandled address classes? */
17796 }
17797 }
17798
17799 TYPE_LENGTH (type) = byte_size;
17800 set_type_align (type, alignment);
17801 return set_die_type (die, type, cu);
17802 }
17803
17804 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17805 the user defined type vector. */
17806
17807 static struct type *
17808 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17809 {
17810 struct type *type;
17811 struct type *to_type;
17812 struct type *domain;
17813
17814 to_type = die_type (die, cu);
17815 domain = die_containing_type (die, cu);
17816
17817 /* The calls above may have already set the type for this DIE. */
17818 type = get_die_type (die, cu);
17819 if (type)
17820 return type;
17821
17822 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17823 type = lookup_methodptr_type (to_type);
17824 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17825 {
17826 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17827
17828 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17829 to_type->fields (), to_type->num_fields (),
17830 to_type->has_varargs ());
17831 type = lookup_methodptr_type (new_type);
17832 }
17833 else
17834 type = lookup_memberptr_type (to_type, domain);
17835
17836 return set_die_type (die, type, cu);
17837 }
17838
17839 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17840 the user defined type vector. */
17841
17842 static struct type *
17843 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17844 enum type_code refcode)
17845 {
17846 struct comp_unit_head *cu_header = &cu->header;
17847 struct type *type, *target_type;
17848 struct attribute *attr;
17849
17850 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17851
17852 target_type = die_type (die, cu);
17853
17854 /* The die_type call above may have already set the type for this DIE. */
17855 type = get_die_type (die, cu);
17856 if (type)
17857 return type;
17858
17859 type = lookup_reference_type (target_type, refcode);
17860 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17861 if (attr != nullptr)
17862 {
17863 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
17864 }
17865 else
17866 {
17867 TYPE_LENGTH (type) = cu_header->addr_size;
17868 }
17869 maybe_set_alignment (cu, die, type);
17870 return set_die_type (die, type, cu);
17871 }
17872
17873 /* Add the given cv-qualifiers to the element type of the array. GCC
17874 outputs DWARF type qualifiers that apply to an array, not the
17875 element type. But GDB relies on the array element type to carry
17876 the cv-qualifiers. This mimics section 6.7.3 of the C99
17877 specification. */
17878
17879 static struct type *
17880 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17881 struct type *base_type, int cnst, int voltl)
17882 {
17883 struct type *el_type, *inner_array;
17884
17885 base_type = copy_type (base_type);
17886 inner_array = base_type;
17887
17888 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17889 {
17890 TYPE_TARGET_TYPE (inner_array) =
17891 copy_type (TYPE_TARGET_TYPE (inner_array));
17892 inner_array = TYPE_TARGET_TYPE (inner_array);
17893 }
17894
17895 el_type = TYPE_TARGET_TYPE (inner_array);
17896 cnst |= TYPE_CONST (el_type);
17897 voltl |= TYPE_VOLATILE (el_type);
17898 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17899
17900 return set_die_type (die, base_type, cu);
17901 }
17902
17903 static struct type *
17904 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17905 {
17906 struct type *base_type, *cv_type;
17907
17908 base_type = die_type (die, cu);
17909
17910 /* The die_type call above may have already set the type for this DIE. */
17911 cv_type = get_die_type (die, cu);
17912 if (cv_type)
17913 return cv_type;
17914
17915 /* In case the const qualifier is applied to an array type, the element type
17916 is so qualified, not the array type (section 6.7.3 of C99). */
17917 if (base_type->code () == TYPE_CODE_ARRAY)
17918 return add_array_cv_type (die, cu, base_type, 1, 0);
17919
17920 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17921 return set_die_type (die, cv_type, cu);
17922 }
17923
17924 static struct type *
17925 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17926 {
17927 struct type *base_type, *cv_type;
17928
17929 base_type = die_type (die, cu);
17930
17931 /* The die_type call above may have already set the type for this DIE. */
17932 cv_type = get_die_type (die, cu);
17933 if (cv_type)
17934 return cv_type;
17935
17936 /* In case the volatile qualifier is applied to an array type, the
17937 element type is so qualified, not the array type (section 6.7.3
17938 of C99). */
17939 if (base_type->code () == TYPE_CODE_ARRAY)
17940 return add_array_cv_type (die, cu, base_type, 0, 1);
17941
17942 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17943 return set_die_type (die, cv_type, cu);
17944 }
17945
17946 /* Handle DW_TAG_restrict_type. */
17947
17948 static struct type *
17949 read_tag_restrict_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 cv_type = make_restrict_type (base_type);
17961 return set_die_type (die, cv_type, cu);
17962 }
17963
17964 /* Handle DW_TAG_atomic_type. */
17965
17966 static struct type *
17967 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17968 {
17969 struct type *base_type, *cv_type;
17970
17971 base_type = die_type (die, cu);
17972
17973 /* The die_type call above may have already set the type for this DIE. */
17974 cv_type = get_die_type (die, cu);
17975 if (cv_type)
17976 return cv_type;
17977
17978 cv_type = make_atomic_type (base_type);
17979 return set_die_type (die, cv_type, cu);
17980 }
17981
17982 /* Extract all information from a DW_TAG_string_type DIE and add to
17983 the user defined type vector. It isn't really a user defined type,
17984 but it behaves like one, with other DIE's using an AT_user_def_type
17985 attribute to reference it. */
17986
17987 static struct type *
17988 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17989 {
17990 struct objfile *objfile = cu->per_objfile->objfile;
17991 struct gdbarch *gdbarch = objfile->arch ();
17992 struct type *type, *range_type, *index_type, *char_type;
17993 struct attribute *attr;
17994 struct dynamic_prop prop;
17995 bool length_is_constant = true;
17996 LONGEST length;
17997
17998 /* There are a couple of places where bit sizes might be made use of
17999 when parsing a DW_TAG_string_type, however, no producer that we know
18000 of make use of these. Handling bit sizes that are a multiple of the
18001 byte size is easy enough, but what about other bit sizes? Lets deal
18002 with that problem when we have to. Warn about these attributes being
18003 unsupported, then parse the type and ignore them like we always
18004 have. */
18005 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
18006 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
18007 {
18008 static bool warning_printed = false;
18009 if (!warning_printed)
18010 {
18011 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
18012 "currently supported on DW_TAG_string_type."));
18013 warning_printed = true;
18014 }
18015 }
18016
18017 attr = dwarf2_attr (die, DW_AT_string_length, cu);
18018 if (attr != nullptr && !attr->form_is_constant ())
18019 {
18020 /* The string length describes the location at which the length of
18021 the string can be found. The size of the length field can be
18022 specified with one of the attributes below. */
18023 struct type *prop_type;
18024 struct attribute *len
18025 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
18026 if (len == nullptr)
18027 len = dwarf2_attr (die, DW_AT_byte_size, cu);
18028 if (len != nullptr && len->form_is_constant ())
18029 {
18030 /* Pass 0 as the default as we know this attribute is constant
18031 and the default value will not be returned. */
18032 LONGEST sz = len->constant_value (0);
18033 prop_type = cu->per_objfile->int_type (sz, true);
18034 }
18035 else
18036 {
18037 /* If the size is not specified then we assume it is the size of
18038 an address on this target. */
18039 prop_type = cu->addr_sized_int_type (true);
18040 }
18041
18042 /* Convert the attribute into a dynamic property. */
18043 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
18044 length = 1;
18045 else
18046 length_is_constant = false;
18047 }
18048 else if (attr != nullptr)
18049 {
18050 /* This DW_AT_string_length just contains the length with no
18051 indirection. There's no need to create a dynamic property in this
18052 case. Pass 0 for the default value as we know it will not be
18053 returned in this case. */
18054 length = attr->constant_value (0);
18055 }
18056 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
18057 {
18058 /* We don't currently support non-constant byte sizes for strings. */
18059 length = attr->constant_value (1);
18060 }
18061 else
18062 {
18063 /* Use 1 as a fallback length if we have nothing else. */
18064 length = 1;
18065 }
18066
18067 index_type = objfile_type (objfile)->builtin_int;
18068 if (length_is_constant)
18069 range_type = create_static_range_type (NULL, index_type, 1, length);
18070 else
18071 {
18072 struct dynamic_prop low_bound;
18073
18074 low_bound.set_const_val (1);
18075 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
18076 }
18077 char_type = language_string_char_type (cu->language_defn, gdbarch);
18078 type = create_string_type (NULL, char_type, range_type);
18079
18080 return set_die_type (die, type, cu);
18081 }
18082
18083 /* Assuming that DIE corresponds to a function, returns nonzero
18084 if the function is prototyped. */
18085
18086 static int
18087 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
18088 {
18089 struct attribute *attr;
18090
18091 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
18092 if (attr && attr->as_boolean ())
18093 return 1;
18094
18095 /* The DWARF standard implies that the DW_AT_prototyped attribute
18096 is only meaningful for C, but the concept also extends to other
18097 languages that allow unprototyped functions (Eg: Objective C).
18098 For all other languages, assume that functions are always
18099 prototyped. */
18100 if (cu->language != language_c
18101 && cu->language != language_objc
18102 && cu->language != language_opencl)
18103 return 1;
18104
18105 /* RealView does not emit DW_AT_prototyped. We can not distinguish
18106 prototyped and unprototyped functions; default to prototyped,
18107 since that is more common in modern code (and RealView warns
18108 about unprototyped functions). */
18109 if (producer_is_realview (cu->producer))
18110 return 1;
18111
18112 return 0;
18113 }
18114
18115 /* Handle DIES due to C code like:
18116
18117 struct foo
18118 {
18119 int (*funcp)(int a, long l);
18120 int b;
18121 };
18122
18123 ('funcp' generates a DW_TAG_subroutine_type DIE). */
18124
18125 static struct type *
18126 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
18127 {
18128 struct objfile *objfile = cu->per_objfile->objfile;
18129 struct type *type; /* Type that this function returns. */
18130 struct type *ftype; /* Function that returns above type. */
18131 struct attribute *attr;
18132
18133 type = die_type (die, cu);
18134
18135 /* The die_type call above may have already set the type for this DIE. */
18136 ftype = get_die_type (die, cu);
18137 if (ftype)
18138 return ftype;
18139
18140 ftype = lookup_function_type (type);
18141
18142 if (prototyped_function_p (die, cu))
18143 ftype->set_is_prototyped (true);
18144
18145 /* Store the calling convention in the type if it's available in
18146 the subroutine die. Otherwise set the calling convention to
18147 the default value DW_CC_normal. */
18148 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
18149 if (attr != nullptr
18150 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
18151 TYPE_CALLING_CONVENTION (ftype)
18152 = (enum dwarf_calling_convention) attr->constant_value (0);
18153 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
18154 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
18155 else
18156 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
18157
18158 /* Record whether the function returns normally to its caller or not
18159 if the DWARF producer set that information. */
18160 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
18161 if (attr && attr->as_boolean ())
18162 TYPE_NO_RETURN (ftype) = 1;
18163
18164 /* We need to add the subroutine type to the die immediately so
18165 we don't infinitely recurse when dealing with parameters
18166 declared as the same subroutine type. */
18167 set_die_type (die, ftype, cu);
18168
18169 if (die->child != NULL)
18170 {
18171 struct type *void_type = objfile_type (objfile)->builtin_void;
18172 struct die_info *child_die;
18173 int nparams, iparams;
18174
18175 /* Count the number of parameters.
18176 FIXME: GDB currently ignores vararg functions, but knows about
18177 vararg member functions. */
18178 nparams = 0;
18179 child_die = die->child;
18180 while (child_die && child_die->tag)
18181 {
18182 if (child_die->tag == DW_TAG_formal_parameter)
18183 nparams++;
18184 else if (child_die->tag == DW_TAG_unspecified_parameters)
18185 ftype->set_has_varargs (true);
18186
18187 child_die = child_die->sibling;
18188 }
18189
18190 /* Allocate storage for parameters and fill them in. */
18191 ftype->set_num_fields (nparams);
18192 ftype->set_fields
18193 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
18194
18195 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
18196 even if we error out during the parameters reading below. */
18197 for (iparams = 0; iparams < nparams; iparams++)
18198 ftype->field (iparams).set_type (void_type);
18199
18200 iparams = 0;
18201 child_die = die->child;
18202 while (child_die && child_die->tag)
18203 {
18204 if (child_die->tag == DW_TAG_formal_parameter)
18205 {
18206 struct type *arg_type;
18207
18208 /* DWARF version 2 has no clean way to discern C++
18209 static and non-static member functions. G++ helps
18210 GDB by marking the first parameter for non-static
18211 member functions (which is the this pointer) as
18212 artificial. We pass this information to
18213 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
18214
18215 DWARF version 3 added DW_AT_object_pointer, which GCC
18216 4.5 does not yet generate. */
18217 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
18218 if (attr != nullptr)
18219 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
18220 else
18221 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
18222 arg_type = die_type (child_die, cu);
18223
18224 /* RealView does not mark THIS as const, which the testsuite
18225 expects. GCC marks THIS as const in method definitions,
18226 but not in the class specifications (GCC PR 43053). */
18227 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
18228 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
18229 {
18230 int is_this = 0;
18231 struct dwarf2_cu *arg_cu = cu;
18232 const char *name = dwarf2_name (child_die, cu);
18233
18234 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
18235 if (attr != nullptr)
18236 {
18237 /* If the compiler emits this, use it. */
18238 if (follow_die_ref (die, attr, &arg_cu) == child_die)
18239 is_this = 1;
18240 }
18241 else if (name && strcmp (name, "this") == 0)
18242 /* Function definitions will have the argument names. */
18243 is_this = 1;
18244 else if (name == NULL && iparams == 0)
18245 /* Declarations may not have the names, so like
18246 elsewhere in GDB, assume an artificial first
18247 argument is "this". */
18248 is_this = 1;
18249
18250 if (is_this)
18251 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
18252 arg_type, 0);
18253 }
18254
18255 ftype->field (iparams).set_type (arg_type);
18256 iparams++;
18257 }
18258 child_die = child_die->sibling;
18259 }
18260 }
18261
18262 return ftype;
18263 }
18264
18265 static struct type *
18266 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
18267 {
18268 struct objfile *objfile = cu->per_objfile->objfile;
18269 const char *name = NULL;
18270 struct type *this_type, *target_type;
18271
18272 name = dwarf2_full_name (NULL, die, cu);
18273 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
18274 this_type->set_target_is_stub (true);
18275 set_die_type (die, this_type, cu);
18276 target_type = die_type (die, cu);
18277 if (target_type != this_type)
18278 TYPE_TARGET_TYPE (this_type) = target_type;
18279 else
18280 {
18281 /* Self-referential typedefs are, it seems, not allowed by the DWARF
18282 spec and cause infinite loops in GDB. */
18283 complaint (_("Self-referential DW_TAG_typedef "
18284 "- DIE at %s [in module %s]"),
18285 sect_offset_str (die->sect_off), objfile_name (objfile));
18286 TYPE_TARGET_TYPE (this_type) = NULL;
18287 }
18288 if (name == NULL)
18289 {
18290 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
18291 anonymous typedefs, which is, strictly speaking, invalid DWARF.
18292 Handle these by just returning the target type, rather than
18293 constructing an anonymous typedef type and trying to handle this
18294 elsewhere. */
18295 set_die_type (die, target_type, cu);
18296 return target_type;
18297 }
18298 return this_type;
18299 }
18300
18301 /* Helper for get_dwarf2_rational_constant that computes the value of
18302 a given gmp_mpz given an attribute. */
18303
18304 static void
18305 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
18306 {
18307 /* GCC will sometimes emit a 16-byte constant value as a DWARF
18308 location expression that pushes an implicit value. */
18309 if (attr->form == DW_FORM_exprloc)
18310 {
18311 dwarf_block *blk = attr->as_block ();
18312 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
18313 {
18314 uint64_t len;
18315 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
18316 blk->data + blk->size,
18317 &len);
18318 if (ptr - blk->data + len <= blk->size)
18319 {
18320 mpz_import (value->val, len,
18321 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18322 1, 0, 0, ptr);
18323 return;
18324 }
18325 }
18326
18327 /* On failure set it to 1. */
18328 *value = gdb_mpz (1);
18329 }
18330 else if (attr->form_is_block ())
18331 {
18332 dwarf_block *blk = attr->as_block ();
18333 mpz_import (value->val, blk->size,
18334 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18335 1, 0, 0, blk->data);
18336 }
18337 else
18338 *value = gdb_mpz (attr->constant_value (1));
18339 }
18340
18341 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
18342 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
18343
18344 If the numerator and/or numerator attribute is missing,
18345 a complaint is filed, and NUMERATOR and DENOMINATOR are left
18346 untouched. */
18347
18348 static void
18349 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
18350 gdb_mpz *numerator, gdb_mpz *denominator)
18351 {
18352 struct attribute *num_attr, *denom_attr;
18353
18354 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
18355 if (num_attr == nullptr)
18356 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
18357 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18358
18359 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
18360 if (denom_attr == nullptr)
18361 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
18362 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18363
18364 if (num_attr == nullptr || denom_attr == nullptr)
18365 return;
18366
18367 get_mpz (cu, numerator, num_attr);
18368 get_mpz (cu, denominator, denom_attr);
18369 }
18370
18371 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
18372 rational constant, rather than a signed one.
18373
18374 If the rational constant has a negative value, a complaint
18375 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
18376
18377 static void
18378 get_dwarf2_unsigned_rational_constant (struct die_info *die,
18379 struct dwarf2_cu *cu,
18380 gdb_mpz *numerator,
18381 gdb_mpz *denominator)
18382 {
18383 gdb_mpz num (1);
18384 gdb_mpz denom (1);
18385
18386 get_dwarf2_rational_constant (die, cu, &num, &denom);
18387 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
18388 {
18389 mpz_neg (num.val, num.val);
18390 mpz_neg (denom.val, denom.val);
18391 }
18392 else if (mpz_sgn (num.val) == -1)
18393 {
18394 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
18395 " in DIE at %s"),
18396 sect_offset_str (die->sect_off));
18397 return;
18398 }
18399 else if (mpz_sgn (denom.val) == -1)
18400 {
18401 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
18402 " in DIE at %s"),
18403 sect_offset_str (die->sect_off));
18404 return;
18405 }
18406
18407 *numerator = std::move (num);
18408 *denominator = std::move (denom);
18409 }
18410
18411 /* Assuming DIE corresponds to a fixed point type, finish the creation
18412 of the corresponding TYPE by setting its type-specific data.
18413 CU is the DIE's CU. */
18414
18415 static void
18416 finish_fixed_point_type (struct type *type, struct die_info *die,
18417 struct dwarf2_cu *cu)
18418 {
18419 struct attribute *attr;
18420
18421 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
18422 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
18423
18424 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
18425 if (!attr)
18426 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
18427 if (!attr)
18428 attr = dwarf2_attr (die, DW_AT_small, cu);
18429
18430 /* Numerator and denominator of our fixed-point type's scaling factor.
18431 The default is a scaling factor of 1, which we use as a fallback
18432 when we are not able to decode it (problem with the debugging info,
18433 unsupported forms, bug in GDB, etc...). Using that as the default
18434 allows us to at least print the unscaled value, which might still
18435 be useful to a user. */
18436 gdb_mpz scale_num (1);
18437 gdb_mpz scale_denom (1);
18438
18439 if (attr == nullptr)
18440 {
18441 /* Scaling factor not found. Assume a scaling factor of 1,
18442 and hope for the best. At least the user will be able to see
18443 the encoded value. */
18444 complaint (_("no scale found for fixed-point type (DIE at %s)"),
18445 sect_offset_str (die->sect_off));
18446 }
18447 else if (attr->name == DW_AT_binary_scale)
18448 {
18449 LONGEST scale_exp = attr->constant_value (0);
18450 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18451
18452 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
18453 }
18454 else if (attr->name == DW_AT_decimal_scale)
18455 {
18456 LONGEST scale_exp = attr->constant_value (0);
18457 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18458
18459 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
18460 }
18461 else if (attr->name == DW_AT_small)
18462 {
18463 struct die_info *scale_die;
18464 struct dwarf2_cu *scale_cu = cu;
18465
18466 scale_die = follow_die_ref (die, attr, &scale_cu);
18467 if (scale_die->tag == DW_TAG_constant)
18468 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18469 &scale_num, &scale_denom);
18470 else
18471 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18472 " (DIE at %s)"),
18473 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18474 }
18475 else
18476 {
18477 complaint (_("unsupported scale attribute %s for fixed-point type"
18478 " (DIE at %s)"),
18479 dwarf_attr_name (attr->name),
18480 sect_offset_str (die->sect_off));
18481 }
18482
18483 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
18484 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
18485 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
18486 mpq_canonicalize (scaling_factor.val);
18487 }
18488
18489 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18490 (which may be different from NAME) to the architecture back-end to allow
18491 it to guess the correct format if necessary. */
18492
18493 static struct type *
18494 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
18495 const char *name_hint, enum bfd_endian byte_order)
18496 {
18497 struct gdbarch *gdbarch = objfile->arch ();
18498 const struct floatformat **format;
18499 struct type *type;
18500
18501 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18502 if (format)
18503 type = init_float_type (objfile, bits, name, format, byte_order);
18504 else
18505 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18506
18507 return type;
18508 }
18509
18510 /* Allocate an integer type of size BITS and name NAME. */
18511
18512 static struct type *
18513 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18514 int bits, int unsigned_p, const char *name)
18515 {
18516 struct type *type;
18517
18518 /* Versions of Intel's C Compiler generate an integer type called "void"
18519 instead of using DW_TAG_unspecified_type. This has been seen on
18520 at least versions 14, 17, and 18. */
18521 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18522 && strcmp (name, "void") == 0)
18523 type = objfile_type (objfile)->builtin_void;
18524 else
18525 type = init_integer_type (objfile, bits, unsigned_p, name);
18526
18527 return type;
18528 }
18529
18530 /* Return true if DIE has a DW_AT_small attribute whose value is
18531 a constant rational, where both the numerator and denominator
18532 are equal to zero.
18533
18534 CU is the DIE's Compilation Unit. */
18535
18536 static bool
18537 has_zero_over_zero_small_attribute (struct die_info *die,
18538 struct dwarf2_cu *cu)
18539 {
18540 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18541 if (attr == nullptr)
18542 return false;
18543
18544 struct dwarf2_cu *scale_cu = cu;
18545 struct die_info *scale_die
18546 = follow_die_ref (die, attr, &scale_cu);
18547
18548 if (scale_die->tag != DW_TAG_constant)
18549 return false;
18550
18551 gdb_mpz num (1), denom (1);
18552 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
18553 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
18554 }
18555
18556 /* Initialise and return a floating point type of size BITS suitable for
18557 use as a component of a complex number. The NAME_HINT is passed through
18558 when initialising the floating point type and is the name of the complex
18559 type.
18560
18561 As DWARF doesn't currently provide an explicit name for the components
18562 of a complex number, but it can be helpful to have these components
18563 named, we try to select a suitable name based on the size of the
18564 component. */
18565 static struct type *
18566 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18567 struct objfile *objfile,
18568 int bits, const char *name_hint,
18569 enum bfd_endian byte_order)
18570 {
18571 gdbarch *gdbarch = objfile->arch ();
18572 struct type *tt = nullptr;
18573
18574 /* Try to find a suitable floating point builtin type of size BITS.
18575 We're going to use the name of this type as the name for the complex
18576 target type that we are about to create. */
18577 switch (cu->language)
18578 {
18579 case language_fortran:
18580 switch (bits)
18581 {
18582 case 32:
18583 tt = builtin_f_type (gdbarch)->builtin_real;
18584 break;
18585 case 64:
18586 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18587 break;
18588 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18589 case 128:
18590 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18591 break;
18592 }
18593 break;
18594 default:
18595 switch (bits)
18596 {
18597 case 32:
18598 tt = builtin_type (gdbarch)->builtin_float;
18599 break;
18600 case 64:
18601 tt = builtin_type (gdbarch)->builtin_double;
18602 break;
18603 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18604 case 128:
18605 tt = builtin_type (gdbarch)->builtin_long_double;
18606 break;
18607 }
18608 break;
18609 }
18610
18611 /* If the type we found doesn't match the size we were looking for, then
18612 pretend we didn't find a type at all, the complex target type we
18613 create will then be nameless. */
18614 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
18615 tt = nullptr;
18616
18617 const char *name = (tt == nullptr) ? nullptr : tt->name ();
18618 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
18619 }
18620
18621 /* Find a representation of a given base type and install
18622 it in the TYPE field of the die. */
18623
18624 static struct type *
18625 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
18626 {
18627 struct objfile *objfile = cu->per_objfile->objfile;
18628 struct type *type;
18629 struct attribute *attr;
18630 int encoding = 0, bits = 0;
18631 const char *name;
18632 gdbarch *arch;
18633
18634 attr = dwarf2_attr (die, DW_AT_encoding, cu);
18635 if (attr != nullptr && attr->form_is_constant ())
18636 encoding = attr->constant_value (0);
18637 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18638 if (attr != nullptr)
18639 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
18640 name = dwarf2_name (die, cu);
18641 if (!name)
18642 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
18643
18644 arch = objfile->arch ();
18645 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18646
18647 attr = dwarf2_attr (die, DW_AT_endianity, cu);
18648 if (attr != nullptr && attr->form_is_constant ())
18649 {
18650 int endianity = attr->constant_value (0);
18651
18652 switch (endianity)
18653 {
18654 case DW_END_big:
18655 byte_order = BFD_ENDIAN_BIG;
18656 break;
18657 case DW_END_little:
18658 byte_order = BFD_ENDIAN_LITTLE;
18659 break;
18660 default:
18661 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18662 break;
18663 }
18664 }
18665
18666 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18667 && cu->language == language_ada
18668 && has_zero_over_zero_small_attribute (die, cu))
18669 {
18670 /* brobecker/2018-02-24: This is a fixed point type for which
18671 the scaling factor is represented as fraction whose value
18672 does not make sense (zero divided by zero), so we should
18673 normally never see these. However, there is a small category
18674 of fixed point types for which GNAT is unable to provide
18675 the scaling factor via the standard DWARF mechanisms, and
18676 for which the info is provided via the GNAT encodings instead.
18677 This is likely what this DIE is about.
18678
18679 Ideally, GNAT should be declaring this type the same way
18680 it declares other fixed point types when using the legacy
18681 GNAT encoding, which is to use a simple signed or unsigned
18682 base type. A report to the GNAT team has been created to
18683 look into it. In the meantime, pretend this type is a simple
18684 signed or unsigned integral, rather than a fixed point type,
18685 to avoid any confusion later on as to how to process this type. */
18686 encoding = (encoding == DW_ATE_signed_fixed
18687 ? DW_ATE_signed
18688 : DW_ATE_unsigned);
18689 }
18690
18691 switch (encoding)
18692 {
18693 case DW_ATE_address:
18694 /* Turn DW_ATE_address into a void * pointer. */
18695 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
18696 type = init_pointer_type (objfile, bits, name, type);
18697 break;
18698 case DW_ATE_boolean:
18699 type = init_boolean_type (objfile, bits, 1, name);
18700 break;
18701 case DW_ATE_complex_float:
18702 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18703 byte_order);
18704 if (type->code () == TYPE_CODE_ERROR)
18705 {
18706 if (name == nullptr)
18707 {
18708 struct obstack *obstack
18709 = &cu->per_objfile->objfile->objfile_obstack;
18710 name = obconcat (obstack, "_Complex ", type->name (),
18711 nullptr);
18712 }
18713 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18714 }
18715 else
18716 type = init_complex_type (name, type);
18717 break;
18718 case DW_ATE_decimal_float:
18719 type = init_decfloat_type (objfile, bits, name);
18720 break;
18721 case DW_ATE_float:
18722 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
18723 break;
18724 case DW_ATE_signed:
18725 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18726 break;
18727 case DW_ATE_unsigned:
18728 if (cu->language == language_fortran
18729 && name
18730 && startswith (name, "character("))
18731 type = init_character_type (objfile, bits, 1, name);
18732 else
18733 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18734 break;
18735 case DW_ATE_signed_char:
18736 if (cu->language == language_ada || cu->language == language_m2
18737 || cu->language == language_pascal
18738 || cu->language == language_fortran)
18739 type = init_character_type (objfile, bits, 0, name);
18740 else
18741 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18742 break;
18743 case DW_ATE_unsigned_char:
18744 if (cu->language == language_ada || cu->language == language_m2
18745 || cu->language == language_pascal
18746 || cu->language == language_fortran
18747 || cu->language == language_rust)
18748 type = init_character_type (objfile, bits, 1, name);
18749 else
18750 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18751 break;
18752 case DW_ATE_UTF:
18753 {
18754 if (bits == 16)
18755 type = builtin_type (arch)->builtin_char16;
18756 else if (bits == 32)
18757 type = builtin_type (arch)->builtin_char32;
18758 else
18759 {
18760 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
18761 bits);
18762 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18763 }
18764 return set_die_type (die, type, cu);
18765 }
18766 break;
18767 case DW_ATE_signed_fixed:
18768 type = init_fixed_point_type (objfile, bits, 0, name);
18769 finish_fixed_point_type (type, die, cu);
18770 break;
18771 case DW_ATE_unsigned_fixed:
18772 type = init_fixed_point_type (objfile, bits, 1, name);
18773 finish_fixed_point_type (type, die, cu);
18774 break;
18775
18776 default:
18777 complaint (_("unsupported DW_AT_encoding: '%s'"),
18778 dwarf_type_encoding_name (encoding));
18779 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18780 break;
18781 }
18782
18783 if (name && strcmp (name, "char") == 0)
18784 type->set_has_no_signedness (true);
18785
18786 maybe_set_alignment (cu, die, type);
18787
18788 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18789
18790 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18791 {
18792 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18793 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
18794 {
18795 unsigned real_bit_size = attr->as_unsigned ();
18796 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18797 /* Only use the attributes if they make sense together. */
18798 if (attr == nullptr
18799 || (attr->as_unsigned () + real_bit_size
18800 <= 8 * TYPE_LENGTH (type)))
18801 {
18802 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18803 = real_bit_size;
18804 if (attr != nullptr)
18805 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18806 = attr->as_unsigned ();
18807 }
18808 }
18809 }
18810
18811 return set_die_type (die, type, cu);
18812 }
18813
18814 /* Parse dwarf attribute if it's a block, reference or constant and put the
18815 resulting value of the attribute into struct bound_prop.
18816 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18817
18818 static int
18819 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18820 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18821 struct type *default_type)
18822 {
18823 struct dwarf2_property_baton *baton;
18824 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18825 struct objfile *objfile = per_objfile->objfile;
18826 struct obstack *obstack = &objfile->objfile_obstack;
18827
18828 gdb_assert (default_type != NULL);
18829
18830 if (attr == NULL || prop == NULL)
18831 return 0;
18832
18833 if (attr->form_is_block ())
18834 {
18835 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18836 baton->property_type = default_type;
18837 baton->locexpr.per_cu = cu->per_cu;
18838 baton->locexpr.per_objfile = per_objfile;
18839
18840 struct dwarf_block *block = attr->as_block ();
18841 baton->locexpr.size = block->size;
18842 baton->locexpr.data = block->data;
18843 switch (attr->name)
18844 {
18845 case DW_AT_string_length:
18846 baton->locexpr.is_reference = true;
18847 break;
18848 default:
18849 baton->locexpr.is_reference = false;
18850 break;
18851 }
18852
18853 prop->set_locexpr (baton);
18854 gdb_assert (prop->baton () != NULL);
18855 }
18856 else if (attr->form_is_ref ())
18857 {
18858 struct dwarf2_cu *target_cu = cu;
18859 struct die_info *target_die;
18860 struct attribute *target_attr;
18861
18862 target_die = follow_die_ref (die, attr, &target_cu);
18863 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18864 if (target_attr == NULL)
18865 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18866 target_cu);
18867 if (target_attr == NULL)
18868 return 0;
18869
18870 switch (target_attr->name)
18871 {
18872 case DW_AT_location:
18873 if (target_attr->form_is_section_offset ())
18874 {
18875 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18876 baton->property_type = die_type (target_die, target_cu);
18877 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18878 prop->set_loclist (baton);
18879 gdb_assert (prop->baton () != NULL);
18880 }
18881 else if (target_attr->form_is_block ())
18882 {
18883 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18884 baton->property_type = die_type (target_die, target_cu);
18885 baton->locexpr.per_cu = cu->per_cu;
18886 baton->locexpr.per_objfile = per_objfile;
18887 struct dwarf_block *block = target_attr->as_block ();
18888 baton->locexpr.size = block->size;
18889 baton->locexpr.data = block->data;
18890 baton->locexpr.is_reference = true;
18891 prop->set_locexpr (baton);
18892 gdb_assert (prop->baton () != NULL);
18893 }
18894 else
18895 {
18896 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18897 "dynamic property");
18898 return 0;
18899 }
18900 break;
18901 case DW_AT_data_member_location:
18902 {
18903 LONGEST offset;
18904
18905 if (!handle_data_member_location (target_die, target_cu,
18906 &offset))
18907 return 0;
18908
18909 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18910 baton->property_type = read_type_die (target_die->parent,
18911 target_cu);
18912 baton->offset_info.offset = offset;
18913 baton->offset_info.type = die_type (target_die, target_cu);
18914 prop->set_addr_offset (baton);
18915 break;
18916 }
18917 }
18918 }
18919 else if (attr->form_is_constant ())
18920 prop->set_const_val (attr->constant_value (0));
18921 else
18922 {
18923 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18924 dwarf2_name (die, cu));
18925 return 0;
18926 }
18927
18928 return 1;
18929 }
18930
18931 /* See read.h. */
18932
18933 struct type *
18934 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
18935 {
18936 struct type *int_type;
18937
18938 /* Helper macro to examine the various builtin types. */
18939 #define TRY_TYPE(F) \
18940 int_type = (unsigned_p \
18941 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18942 : objfile_type (objfile)->builtin_ ## F); \
18943 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
18944 return int_type
18945
18946 TRY_TYPE (char);
18947 TRY_TYPE (short);
18948 TRY_TYPE (int);
18949 TRY_TYPE (long);
18950 TRY_TYPE (long_long);
18951
18952 #undef TRY_TYPE
18953
18954 gdb_assert_not_reached ("unable to find suitable integer type");
18955 }
18956
18957 /* See read.h. */
18958
18959 struct type *
18960 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
18961 {
18962 int addr_size = this->per_cu->addr_size ();
18963 return this->per_objfile->int_type (addr_size, unsigned_p);
18964 }
18965
18966 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18967 present (which is valid) then compute the default type based on the
18968 compilation units address size. */
18969
18970 static struct type *
18971 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18972 {
18973 struct type *index_type = die_type (die, cu);
18974
18975 /* Dwarf-2 specifications explicitly allows to create subrange types
18976 without specifying a base type.
18977 In that case, the base type must be set to the type of
18978 the lower bound, upper bound or count, in that order, if any of these
18979 three attributes references an object that has a type.
18980 If no base type is found, the Dwarf-2 specifications say that
18981 a signed integer type of size equal to the size of an address should
18982 be used.
18983 For the following C code: `extern char gdb_int [];'
18984 GCC produces an empty range DIE.
18985 FIXME: muller/2010-05-28: Possible references to object for low bound,
18986 high bound or count are not yet handled by this code. */
18987 if (index_type->code () == TYPE_CODE_VOID)
18988 index_type = cu->addr_sized_int_type (false);
18989
18990 return index_type;
18991 }
18992
18993 /* Read the given DW_AT_subrange DIE. */
18994
18995 static struct type *
18996 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18997 {
18998 struct type *base_type, *orig_base_type;
18999 struct type *range_type;
19000 struct attribute *attr;
19001 struct dynamic_prop low, high;
19002 int low_default_is_valid;
19003 int high_bound_is_count = 0;
19004 const char *name;
19005 ULONGEST negative_mask;
19006
19007 orig_base_type = read_subrange_index_type (die, cu);
19008
19009 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
19010 whereas the real type might be. So, we use ORIG_BASE_TYPE when
19011 creating the range type, but we use the result of check_typedef
19012 when examining properties of the type. */
19013 base_type = check_typedef (orig_base_type);
19014
19015 /* The die_type call above may have already set the type for this DIE. */
19016 range_type = get_die_type (die, cu);
19017 if (range_type)
19018 return range_type;
19019
19020 high.set_const_val (0);
19021
19022 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
19023 omitting DW_AT_lower_bound. */
19024 switch (cu->language)
19025 {
19026 case language_c:
19027 case language_cplus:
19028 low.set_const_val (0);
19029 low_default_is_valid = 1;
19030 break;
19031 case language_fortran:
19032 low.set_const_val (1);
19033 low_default_is_valid = 1;
19034 break;
19035 case language_d:
19036 case language_objc:
19037 case language_rust:
19038 low.set_const_val (0);
19039 low_default_is_valid = (cu->header.version >= 4);
19040 break;
19041 case language_ada:
19042 case language_m2:
19043 case language_pascal:
19044 low.set_const_val (1);
19045 low_default_is_valid = (cu->header.version >= 4);
19046 break;
19047 default:
19048 low.set_const_val (0);
19049 low_default_is_valid = 0;
19050 break;
19051 }
19052
19053 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
19054 if (attr != nullptr)
19055 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
19056 else if (!low_default_is_valid)
19057 complaint (_("Missing DW_AT_lower_bound "
19058 "- DIE at %s [in module %s]"),
19059 sect_offset_str (die->sect_off),
19060 objfile_name (cu->per_objfile->objfile));
19061
19062 struct attribute *attr_ub, *attr_count;
19063 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
19064 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
19065 {
19066 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
19067 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
19068 {
19069 /* If bounds are constant do the final calculation here. */
19070 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
19071 high.set_const_val (low.const_val () + high.const_val () - 1);
19072 else
19073 high_bound_is_count = 1;
19074 }
19075 else
19076 {
19077 if (attr_ub != NULL)
19078 complaint (_("Unresolved DW_AT_upper_bound "
19079 "- DIE at %s [in module %s]"),
19080 sect_offset_str (die->sect_off),
19081 objfile_name (cu->per_objfile->objfile));
19082 if (attr_count != NULL)
19083 complaint (_("Unresolved DW_AT_count "
19084 "- DIE at %s [in module %s]"),
19085 sect_offset_str (die->sect_off),
19086 objfile_name (cu->per_objfile->objfile));
19087 }
19088 }
19089
19090 LONGEST bias = 0;
19091 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
19092 if (bias_attr != nullptr && bias_attr->form_is_constant ())
19093 bias = bias_attr->constant_value (0);
19094
19095 /* Normally, the DWARF producers are expected to use a signed
19096 constant form (Eg. DW_FORM_sdata) to express negative bounds.
19097 But this is unfortunately not always the case, as witnessed
19098 with GCC, for instance, where the ambiguous DW_FORM_dataN form
19099 is used instead. To work around that ambiguity, we treat
19100 the bounds as signed, and thus sign-extend their values, when
19101 the base type is signed. */
19102 negative_mask =
19103 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
19104 if (low.kind () == PROP_CONST
19105 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
19106 low.set_const_val (low.const_val () | negative_mask);
19107 if (high.kind () == PROP_CONST
19108 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
19109 high.set_const_val (high.const_val () | negative_mask);
19110
19111 /* Check for bit and byte strides. */
19112 struct dynamic_prop byte_stride_prop;
19113 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
19114 if (attr_byte_stride != nullptr)
19115 {
19116 struct type *prop_type = cu->addr_sized_int_type (false);
19117 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
19118 prop_type);
19119 }
19120
19121 struct dynamic_prop bit_stride_prop;
19122 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
19123 if (attr_bit_stride != nullptr)
19124 {
19125 /* It only makes sense to have either a bit or byte stride. */
19126 if (attr_byte_stride != nullptr)
19127 {
19128 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
19129 "- DIE at %s [in module %s]"),
19130 sect_offset_str (die->sect_off),
19131 objfile_name (cu->per_objfile->objfile));
19132 attr_bit_stride = nullptr;
19133 }
19134 else
19135 {
19136 struct type *prop_type = cu->addr_sized_int_type (false);
19137 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
19138 prop_type);
19139 }
19140 }
19141
19142 if (attr_byte_stride != nullptr
19143 || attr_bit_stride != nullptr)
19144 {
19145 bool byte_stride_p = (attr_byte_stride != nullptr);
19146 struct dynamic_prop *stride
19147 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
19148
19149 range_type
19150 = create_range_type_with_stride (NULL, orig_base_type, &low,
19151 &high, bias, stride, byte_stride_p);
19152 }
19153 else
19154 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
19155
19156 if (high_bound_is_count)
19157 range_type->bounds ()->flag_upper_bound_is_count = 1;
19158
19159 /* Ada expects an empty array on no boundary attributes. */
19160 if (attr == NULL && cu->language != language_ada)
19161 range_type->bounds ()->high.set_undefined ();
19162
19163 name = dwarf2_name (die, cu);
19164 if (name)
19165 range_type->set_name (name);
19166
19167 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
19168 if (attr != nullptr)
19169 TYPE_LENGTH (range_type) = attr->constant_value (0);
19170
19171 maybe_set_alignment (cu, die, range_type);
19172
19173 set_die_type (die, range_type, cu);
19174
19175 /* set_die_type should be already done. */
19176 set_descriptive_type (range_type, die, cu);
19177
19178 return range_type;
19179 }
19180
19181 static struct type *
19182 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
19183 {
19184 struct type *type;
19185
19186 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
19187 type->set_name (dwarf2_name (die, cu));
19188
19189 /* In Ada, an unspecified type is typically used when the description
19190 of the type is deferred to a different unit. When encountering
19191 such a type, we treat it as a stub, and try to resolve it later on,
19192 when needed. */
19193 if (cu->language == language_ada)
19194 type->set_is_stub (true);
19195
19196 return set_die_type (die, type, cu);
19197 }
19198
19199 /* Read a single die and all its descendents. Set the die's sibling
19200 field to NULL; set other fields in the die correctly, and set all
19201 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
19202 location of the info_ptr after reading all of those dies. PARENT
19203 is the parent of the die in question. */
19204
19205 static struct die_info *
19206 read_die_and_children (const struct die_reader_specs *reader,
19207 const gdb_byte *info_ptr,
19208 const gdb_byte **new_info_ptr,
19209 struct die_info *parent)
19210 {
19211 struct die_info *die;
19212 const gdb_byte *cur_ptr;
19213
19214 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
19215 if (die == NULL)
19216 {
19217 *new_info_ptr = cur_ptr;
19218 return NULL;
19219 }
19220 store_in_ref_table (die, reader->cu);
19221
19222 if (die->has_children)
19223 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
19224 else
19225 {
19226 die->child = NULL;
19227 *new_info_ptr = cur_ptr;
19228 }
19229
19230 die->sibling = NULL;
19231 die->parent = parent;
19232 return die;
19233 }
19234
19235 /* Read a die, all of its descendents, and all of its siblings; set
19236 all of the fields of all of the dies correctly. Arguments are as
19237 in read_die_and_children. */
19238
19239 static struct die_info *
19240 read_die_and_siblings_1 (const struct die_reader_specs *reader,
19241 const gdb_byte *info_ptr,
19242 const gdb_byte **new_info_ptr,
19243 struct die_info *parent)
19244 {
19245 struct die_info *first_die, *last_sibling;
19246 const gdb_byte *cur_ptr;
19247
19248 cur_ptr = info_ptr;
19249 first_die = last_sibling = NULL;
19250
19251 while (1)
19252 {
19253 struct die_info *die
19254 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
19255
19256 if (die == NULL)
19257 {
19258 *new_info_ptr = cur_ptr;
19259 return first_die;
19260 }
19261
19262 if (!first_die)
19263 first_die = die;
19264 else
19265 last_sibling->sibling = die;
19266
19267 last_sibling = die;
19268 }
19269 }
19270
19271 /* Read a die, all of its descendents, and all of its siblings; set
19272 all of the fields of all of the dies correctly. Arguments are as
19273 in read_die_and_children.
19274 This the main entry point for reading a DIE and all its children. */
19275
19276 static struct die_info *
19277 read_die_and_siblings (const struct die_reader_specs *reader,
19278 const gdb_byte *info_ptr,
19279 const gdb_byte **new_info_ptr,
19280 struct die_info *parent)
19281 {
19282 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
19283 new_info_ptr, parent);
19284
19285 if (dwarf_die_debug)
19286 {
19287 fprintf_unfiltered (gdb_stdlog,
19288 "Read die from %s@0x%x of %s:\n",
19289 reader->die_section->get_name (),
19290 (unsigned) (info_ptr - reader->die_section->buffer),
19291 bfd_get_filename (reader->abfd));
19292 dump_die (die, dwarf_die_debug);
19293 }
19294
19295 return die;
19296 }
19297
19298 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
19299 attributes.
19300 The caller is responsible for filling in the extra attributes
19301 and updating (*DIEP)->num_attrs.
19302 Set DIEP to point to a newly allocated die with its information,
19303 except for its child, sibling, and parent fields. */
19304
19305 static const gdb_byte *
19306 read_full_die_1 (const struct die_reader_specs *reader,
19307 struct die_info **diep, const gdb_byte *info_ptr,
19308 int num_extra_attrs)
19309 {
19310 unsigned int abbrev_number, bytes_read, i;
19311 struct abbrev_info *abbrev;
19312 struct die_info *die;
19313 struct dwarf2_cu *cu = reader->cu;
19314 bfd *abfd = reader->abfd;
19315
19316 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
19317 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19318 info_ptr += bytes_read;
19319 if (!abbrev_number)
19320 {
19321 *diep = NULL;
19322 return info_ptr;
19323 }
19324
19325 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
19326 if (!abbrev)
19327 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
19328 abbrev_number,
19329 bfd_get_filename (abfd));
19330
19331 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
19332 die->sect_off = sect_off;
19333 die->tag = abbrev->tag;
19334 die->abbrev = abbrev_number;
19335 die->has_children = abbrev->has_children;
19336
19337 /* Make the result usable.
19338 The caller needs to update num_attrs after adding the extra
19339 attributes. */
19340 die->num_attrs = abbrev->num_attrs;
19341
19342 bool any_need_reprocess = false;
19343 for (i = 0; i < abbrev->num_attrs; ++i)
19344 {
19345 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
19346 info_ptr);
19347 if (die->attrs[i].requires_reprocessing_p ())
19348 any_need_reprocess = true;
19349 }
19350
19351 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
19352 if (attr != nullptr && attr->form_is_unsigned ())
19353 cu->str_offsets_base = attr->as_unsigned ();
19354
19355 attr = die->attr (DW_AT_loclists_base);
19356 if (attr != nullptr)
19357 cu->loclist_base = attr->as_unsigned ();
19358
19359 auto maybe_addr_base = die->addr_base ();
19360 if (maybe_addr_base.has_value ())
19361 cu->addr_base = *maybe_addr_base;
19362
19363 attr = die->attr (DW_AT_rnglists_base);
19364 if (attr != nullptr)
19365 cu->rnglists_base = attr->as_unsigned ();
19366
19367 if (any_need_reprocess)
19368 {
19369 for (i = 0; i < abbrev->num_attrs; ++i)
19370 {
19371 if (die->attrs[i].requires_reprocessing_p ())
19372 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
19373 }
19374 }
19375 *diep = die;
19376 return info_ptr;
19377 }
19378
19379 /* Read a die and all its attributes.
19380 Set DIEP to point to a newly allocated die with its information,
19381 except for its child, sibling, and parent fields. */
19382
19383 static const gdb_byte *
19384 read_full_die (const struct die_reader_specs *reader,
19385 struct die_info **diep, const gdb_byte *info_ptr)
19386 {
19387 const gdb_byte *result;
19388
19389 result = read_full_die_1 (reader, diep, info_ptr, 0);
19390
19391 if (dwarf_die_debug)
19392 {
19393 fprintf_unfiltered (gdb_stdlog,
19394 "Read die from %s@0x%x of %s:\n",
19395 reader->die_section->get_name (),
19396 (unsigned) (info_ptr - reader->die_section->buffer),
19397 bfd_get_filename (reader->abfd));
19398 dump_die (*diep, dwarf_die_debug);
19399 }
19400
19401 return result;
19402 }
19403 \f
19404
19405 /* Returns nonzero if TAG represents a type that we might generate a partial
19406 symbol for. */
19407
19408 static int
19409 is_type_tag_for_partial (int tag, enum language lang)
19410 {
19411 switch (tag)
19412 {
19413 #if 0
19414 /* Some types that would be reasonable to generate partial symbols for,
19415 that we don't at present. Note that normally this does not
19416 matter, mainly because C compilers don't give names to these
19417 types, but instead emit DW_TAG_typedef. */
19418 case DW_TAG_file_type:
19419 case DW_TAG_ptr_to_member_type:
19420 case DW_TAG_set_type:
19421 case DW_TAG_string_type:
19422 case DW_TAG_subroutine_type:
19423 #endif
19424
19425 /* GNAT may emit an array with a name, but no typedef, so we
19426 need to make a symbol in this case. */
19427 case DW_TAG_array_type:
19428 return lang == language_ada;
19429
19430 case DW_TAG_base_type:
19431 case DW_TAG_class_type:
19432 case DW_TAG_interface_type:
19433 case DW_TAG_enumeration_type:
19434 case DW_TAG_structure_type:
19435 case DW_TAG_subrange_type:
19436 case DW_TAG_typedef:
19437 case DW_TAG_union_type:
19438 return 1;
19439 default:
19440 return 0;
19441 }
19442 }
19443
19444 /* Load all DIEs that are interesting for partial symbols into memory. */
19445
19446 static struct partial_die_info *
19447 load_partial_dies (const struct die_reader_specs *reader,
19448 const gdb_byte *info_ptr, int building_psymtab)
19449 {
19450 struct dwarf2_cu *cu = reader->cu;
19451 struct objfile *objfile = cu->per_objfile->objfile;
19452 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
19453 unsigned int bytes_read;
19454 unsigned int load_all = 0;
19455 int nesting_level = 1;
19456
19457 parent_die = NULL;
19458 last_die = NULL;
19459
19460 gdb_assert (cu->per_cu != NULL);
19461 if (cu->per_cu->load_all_dies)
19462 load_all = 1;
19463
19464 cu->partial_dies
19465 = htab_create_alloc_ex (cu->header.length / 12,
19466 partial_die_hash,
19467 partial_die_eq,
19468 NULL,
19469 &cu->comp_unit_obstack,
19470 hashtab_obstack_allocate,
19471 dummy_obstack_deallocate);
19472
19473 while (1)
19474 {
19475 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
19476
19477 /* A NULL abbrev means the end of a series of children. */
19478 if (abbrev == NULL)
19479 {
19480 if (--nesting_level == 0)
19481 return first_die;
19482
19483 info_ptr += bytes_read;
19484 last_die = parent_die;
19485 parent_die = parent_die->die_parent;
19486 continue;
19487 }
19488
19489 /* Check for template arguments. We never save these; if
19490 they're seen, we just mark the parent, and go on our way. */
19491 if (parent_die != NULL
19492 && cu->language == language_cplus
19493 && (abbrev->tag == DW_TAG_template_type_param
19494 || abbrev->tag == DW_TAG_template_value_param))
19495 {
19496 parent_die->has_template_arguments = 1;
19497
19498 if (!load_all)
19499 {
19500 /* We don't need a partial DIE for the template argument. */
19501 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19502 continue;
19503 }
19504 }
19505
19506 /* We only recurse into c++ subprograms looking for template arguments.
19507 Skip their other children. */
19508 if (!load_all
19509 && cu->language == language_cplus
19510 && parent_die != NULL
19511 && parent_die->tag == DW_TAG_subprogram
19512 && abbrev->tag != DW_TAG_inlined_subroutine)
19513 {
19514 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19515 continue;
19516 }
19517
19518 /* Check whether this DIE is interesting enough to save. Normally
19519 we would not be interested in members here, but there may be
19520 later variables referencing them via DW_AT_specification (for
19521 static members). */
19522 if (!load_all
19523 && !is_type_tag_for_partial (abbrev->tag, cu->language)
19524 && abbrev->tag != DW_TAG_constant
19525 && abbrev->tag != DW_TAG_enumerator
19526 && abbrev->tag != DW_TAG_subprogram
19527 && abbrev->tag != DW_TAG_inlined_subroutine
19528 && abbrev->tag != DW_TAG_lexical_block
19529 && abbrev->tag != DW_TAG_variable
19530 && abbrev->tag != DW_TAG_namespace
19531 && abbrev->tag != DW_TAG_module
19532 && abbrev->tag != DW_TAG_member
19533 && abbrev->tag != DW_TAG_imported_unit
19534 && abbrev->tag != DW_TAG_imported_declaration)
19535 {
19536 /* Otherwise we skip to the next sibling, if any. */
19537 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19538 continue;
19539 }
19540
19541 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19542 abbrev);
19543
19544 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
19545
19546 /* This two-pass algorithm for processing partial symbols has a
19547 high cost in cache pressure. Thus, handle some simple cases
19548 here which cover the majority of C partial symbols. DIEs
19549 which neither have specification tags in them, nor could have
19550 specification tags elsewhere pointing at them, can simply be
19551 processed and discarded.
19552
19553 This segment is also optional; scan_partial_symbols and
19554 add_partial_symbol will handle these DIEs if we chain
19555 them in normally. When compilers which do not emit large
19556 quantities of duplicate debug information are more common,
19557 this code can probably be removed. */
19558
19559 /* Any complete simple types at the top level (pretty much all
19560 of them, for a language without namespaces), can be processed
19561 directly. */
19562 if (parent_die == NULL
19563 && pdi.has_specification == 0
19564 && pdi.is_declaration == 0
19565 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19566 || pdi.tag == DW_TAG_base_type
19567 || pdi.tag == DW_TAG_array_type
19568 || pdi.tag == DW_TAG_subrange_type))
19569 {
19570 if (building_psymtab && pdi.raw_name != NULL)
19571 add_partial_symbol (&pdi, cu);
19572
19573 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19574 continue;
19575 }
19576
19577 /* The exception for DW_TAG_typedef with has_children above is
19578 a workaround of GCC PR debug/47510. In the case of this complaint
19579 type_name_or_error will error on such types later.
19580
19581 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19582 it could not find the child DIEs referenced later, this is checked
19583 above. In correct DWARF DW_TAG_typedef should have no children. */
19584
19585 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
19586 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
19587 "- DIE at %s [in module %s]"),
19588 sect_offset_str (pdi.sect_off), objfile_name (objfile));
19589
19590 /* If we're at the second level, and we're an enumerator, and
19591 our parent has no specification (meaning possibly lives in a
19592 namespace elsewhere), then we can add the partial symbol now
19593 instead of queueing it. */
19594 if (pdi.tag == DW_TAG_enumerator
19595 && parent_die != NULL
19596 && parent_die->die_parent == NULL
19597 && parent_die->tag == DW_TAG_enumeration_type
19598 && parent_die->has_specification == 0)
19599 {
19600 if (pdi.raw_name == NULL)
19601 complaint (_("malformed enumerator DIE ignored"));
19602 else if (building_psymtab)
19603 add_partial_symbol (&pdi, cu);
19604
19605 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19606 continue;
19607 }
19608
19609 struct partial_die_info *part_die
19610 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
19611
19612 /* We'll save this DIE so link it in. */
19613 part_die->die_parent = parent_die;
19614 part_die->die_sibling = NULL;
19615 part_die->die_child = NULL;
19616
19617 if (last_die && last_die == parent_die)
19618 last_die->die_child = part_die;
19619 else if (last_die)
19620 last_die->die_sibling = part_die;
19621
19622 last_die = part_die;
19623
19624 if (first_die == NULL)
19625 first_die = part_die;
19626
19627 /* Maybe add the DIE to the hash table. Not all DIEs that we
19628 find interesting need to be in the hash table, because we
19629 also have the parent/sibling/child chains; only those that we
19630 might refer to by offset later during partial symbol reading.
19631
19632 For now this means things that might have be the target of a
19633 DW_AT_specification, DW_AT_abstract_origin, or
19634 DW_AT_extension. DW_AT_extension will refer only to
19635 namespaces; DW_AT_abstract_origin refers to functions (and
19636 many things under the function DIE, but we do not recurse
19637 into function DIEs during partial symbol reading) and
19638 possibly variables as well; DW_AT_specification refers to
19639 declarations. Declarations ought to have the DW_AT_declaration
19640 flag. It happens that GCC forgets to put it in sometimes, but
19641 only for functions, not for types.
19642
19643 Adding more things than necessary to the hash table is harmless
19644 except for the performance cost. Adding too few will result in
19645 wasted time in find_partial_die, when we reread the compilation
19646 unit with load_all_dies set. */
19647
19648 if (load_all
19649 || abbrev->tag == DW_TAG_constant
19650 || abbrev->tag == DW_TAG_subprogram
19651 || abbrev->tag == DW_TAG_variable
19652 || abbrev->tag == DW_TAG_namespace
19653 || part_die->is_declaration)
19654 {
19655 void **slot;
19656
19657 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
19658 to_underlying (part_die->sect_off),
19659 INSERT);
19660 *slot = part_die;
19661 }
19662
19663 /* For some DIEs we want to follow their children (if any). For C
19664 we have no reason to follow the children of structures; for other
19665 languages we have to, so that we can get at method physnames
19666 to infer fully qualified class names, for DW_AT_specification,
19667 and for C++ template arguments. For C++, we also look one level
19668 inside functions to find template arguments (if the name of the
19669 function does not already contain the template arguments).
19670
19671 For Ada and Fortran, we need to scan the children of subprograms
19672 and lexical blocks as well because these languages allow the
19673 definition of nested entities that could be interesting for the
19674 debugger, such as nested subprograms for instance. */
19675 if (last_die->has_children
19676 && (load_all
19677 || last_die->tag == DW_TAG_namespace
19678 || last_die->tag == DW_TAG_module
19679 || last_die->tag == DW_TAG_enumeration_type
19680 || (cu->language == language_cplus
19681 && last_die->tag == DW_TAG_subprogram
19682 && (last_die->raw_name == NULL
19683 || strchr (last_die->raw_name, '<') == NULL))
19684 || (cu->language != language_c
19685 && (last_die->tag == DW_TAG_class_type
19686 || last_die->tag == DW_TAG_interface_type
19687 || last_die->tag == DW_TAG_structure_type
19688 || last_die->tag == DW_TAG_union_type))
19689 || ((cu->language == language_ada
19690 || cu->language == language_fortran)
19691 && (last_die->tag == DW_TAG_subprogram
19692 || last_die->tag == DW_TAG_lexical_block))))
19693 {
19694 nesting_level++;
19695 parent_die = last_die;
19696 continue;
19697 }
19698
19699 /* Otherwise we skip to the next sibling, if any. */
19700 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
19701
19702 /* Back to the top, do it again. */
19703 }
19704 }
19705
19706 partial_die_info::partial_die_info (sect_offset sect_off_,
19707 struct abbrev_info *abbrev)
19708 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19709 {
19710 }
19711
19712 /* See class definition. */
19713
19714 const char *
19715 partial_die_info::name (dwarf2_cu *cu)
19716 {
19717 if (!canonical_name && raw_name != nullptr)
19718 {
19719 struct objfile *objfile = cu->per_objfile->objfile;
19720 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19721 canonical_name = 1;
19722 }
19723
19724 return raw_name;
19725 }
19726
19727 /* Read a minimal amount of information into the minimal die structure.
19728 INFO_PTR should point just after the initial uleb128 of a DIE. */
19729
19730 const gdb_byte *
19731 partial_die_info::read (const struct die_reader_specs *reader,
19732 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
19733 {
19734 struct dwarf2_cu *cu = reader->cu;
19735 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19736 unsigned int i;
19737 int has_low_pc_attr = 0;
19738 int has_high_pc_attr = 0;
19739 int high_pc_relative = 0;
19740
19741 for (i = 0; i < abbrev.num_attrs; ++i)
19742 {
19743 attribute attr;
19744 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19745 /* String and address offsets that need to do the reprocessing have
19746 already been read at this point, so there is no need to wait until
19747 the loop terminates to do the reprocessing. */
19748 if (attr.requires_reprocessing_p ())
19749 read_attribute_reprocess (reader, &attr, tag);
19750 /* Store the data if it is of an attribute we want to keep in a
19751 partial symbol table. */
19752 switch (attr.name)
19753 {
19754 case DW_AT_name:
19755 switch (tag)
19756 {
19757 case DW_TAG_compile_unit:
19758 case DW_TAG_partial_unit:
19759 case DW_TAG_type_unit:
19760 /* Compilation units have a DW_AT_name that is a filename, not
19761 a source language identifier. */
19762 case DW_TAG_enumeration_type:
19763 case DW_TAG_enumerator:
19764 /* These tags always have simple identifiers already; no need
19765 to canonicalize them. */
19766 canonical_name = 1;
19767 raw_name = attr.as_string ();
19768 break;
19769 default:
19770 canonical_name = 0;
19771 raw_name = attr.as_string ();
19772 break;
19773 }
19774 break;
19775 case DW_AT_linkage_name:
19776 case DW_AT_MIPS_linkage_name:
19777 /* Note that both forms of linkage name might appear. We
19778 assume they will be the same, and we only store the last
19779 one we see. */
19780 linkage_name = attr.as_string ();
19781 break;
19782 case DW_AT_low_pc:
19783 has_low_pc_attr = 1;
19784 lowpc = attr.as_address ();
19785 break;
19786 case DW_AT_high_pc:
19787 has_high_pc_attr = 1;
19788 highpc = attr.as_address ();
19789 if (cu->header.version >= 4 && attr.form_is_constant ())
19790 high_pc_relative = 1;
19791 break;
19792 case DW_AT_location:
19793 /* Support the .debug_loc offsets. */
19794 if (attr.form_is_block ())
19795 {
19796 d.locdesc = attr.as_block ();
19797 }
19798 else if (attr.form_is_section_offset ())
19799 {
19800 dwarf2_complex_location_expr_complaint ();
19801 }
19802 else
19803 {
19804 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19805 "partial symbol information");
19806 }
19807 break;
19808 case DW_AT_external:
19809 is_external = attr.as_boolean ();
19810 break;
19811 case DW_AT_declaration:
19812 is_declaration = attr.as_boolean ();
19813 break;
19814 case DW_AT_type:
19815 has_type = 1;
19816 break;
19817 case DW_AT_abstract_origin:
19818 case DW_AT_specification:
19819 case DW_AT_extension:
19820 has_specification = 1;
19821 spec_offset = attr.get_ref_die_offset ();
19822 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19823 || cu->per_cu->is_dwz);
19824 break;
19825 case DW_AT_sibling:
19826 /* Ignore absolute siblings, they might point outside of
19827 the current compile unit. */
19828 if (attr.form == DW_FORM_ref_addr)
19829 complaint (_("ignoring absolute DW_AT_sibling"));
19830 else
19831 {
19832 const gdb_byte *buffer = reader->buffer;
19833 sect_offset off = attr.get_ref_die_offset ();
19834 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19835
19836 if (sibling_ptr < info_ptr)
19837 complaint (_("DW_AT_sibling points backwards"));
19838 else if (sibling_ptr > reader->buffer_end)
19839 reader->die_section->overflow_complaint ();
19840 else
19841 sibling = sibling_ptr;
19842 }
19843 break;
19844 case DW_AT_byte_size:
19845 has_byte_size = 1;
19846 break;
19847 case DW_AT_const_value:
19848 has_const_value = 1;
19849 break;
19850 case DW_AT_calling_convention:
19851 /* DWARF doesn't provide a way to identify a program's source-level
19852 entry point. DW_AT_calling_convention attributes are only meant
19853 to describe functions' calling conventions.
19854
19855 However, because it's a necessary piece of information in
19856 Fortran, and before DWARF 4 DW_CC_program was the only
19857 piece of debugging information whose definition refers to
19858 a 'main program' at all, several compilers marked Fortran
19859 main programs with DW_CC_program --- even when those
19860 functions use the standard calling conventions.
19861
19862 Although DWARF now specifies a way to provide this
19863 information, we support this practice for backward
19864 compatibility. */
19865 if (attr.constant_value (0) == DW_CC_program
19866 && cu->language == language_fortran)
19867 main_subprogram = 1;
19868 break;
19869 case DW_AT_inline:
19870 {
19871 LONGEST value = attr.constant_value (-1);
19872 if (value == DW_INL_inlined
19873 || value == DW_INL_declared_inlined)
19874 may_be_inlined = 1;
19875 }
19876 break;
19877
19878 case DW_AT_import:
19879 if (tag == DW_TAG_imported_unit)
19880 {
19881 d.sect_off = attr.get_ref_die_offset ();
19882 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19883 || cu->per_cu->is_dwz);
19884 }
19885 break;
19886
19887 case DW_AT_main_subprogram:
19888 main_subprogram = attr.as_boolean ();
19889 break;
19890
19891 case DW_AT_ranges:
19892 {
19893 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19894 on DWARF version). */
19895 ULONGEST ranges_offset = attr.as_unsigned ();
19896
19897 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19898 this value. */
19899 if (tag != DW_TAG_compile_unit)
19900 ranges_offset += cu->gnu_ranges_base;
19901
19902 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19903 nullptr, tag))
19904 has_pc_info = 1;
19905 }
19906 break;
19907
19908 default:
19909 break;
19910 }
19911 }
19912
19913 /* For Ada, if both the name and the linkage name appear, we prefer
19914 the latter. This lets "catch exception" work better, regardless
19915 of the order in which the name and linkage name were emitted.
19916 Really, though, this is just a workaround for the fact that gdb
19917 doesn't store both the name and the linkage name. */
19918 if (cu->language == language_ada && linkage_name != nullptr)
19919 raw_name = linkage_name;
19920
19921 if (high_pc_relative)
19922 highpc += lowpc;
19923
19924 if (has_low_pc_attr && has_high_pc_attr)
19925 {
19926 /* When using the GNU linker, .gnu.linkonce. sections are used to
19927 eliminate duplicate copies of functions and vtables and such.
19928 The linker will arbitrarily choose one and discard the others.
19929 The AT_*_pc values for such functions refer to local labels in
19930 these sections. If the section from that file was discarded, the
19931 labels are not in the output, so the relocs get a value of 0.
19932 If this is a discarded function, mark the pc bounds as invalid,
19933 so that GDB will ignore it. */
19934 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
19935 {
19936 struct objfile *objfile = per_objfile->objfile;
19937 struct gdbarch *gdbarch = objfile->arch ();
19938
19939 complaint (_("DW_AT_low_pc %s is zero "
19940 "for DIE at %s [in module %s]"),
19941 paddress (gdbarch, lowpc),
19942 sect_offset_str (sect_off),
19943 objfile_name (objfile));
19944 }
19945 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19946 else if (lowpc >= highpc)
19947 {
19948 struct objfile *objfile = per_objfile->objfile;
19949 struct gdbarch *gdbarch = objfile->arch ();
19950
19951 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19952 "for DIE at %s [in module %s]"),
19953 paddress (gdbarch, lowpc),
19954 paddress (gdbarch, highpc),
19955 sect_offset_str (sect_off),
19956 objfile_name (objfile));
19957 }
19958 else
19959 has_pc_info = 1;
19960 }
19961
19962 return info_ptr;
19963 }
19964
19965 /* Find a cached partial DIE at OFFSET in CU. */
19966
19967 struct partial_die_info *
19968 dwarf2_cu::find_partial_die (sect_offset sect_off)
19969 {
19970 struct partial_die_info *lookup_die = NULL;
19971 struct partial_die_info part_die (sect_off);
19972
19973 lookup_die = ((struct partial_die_info *)
19974 htab_find_with_hash (partial_dies, &part_die,
19975 to_underlying (sect_off)));
19976
19977 return lookup_die;
19978 }
19979
19980 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19981 except in the case of .debug_types DIEs which do not reference
19982 outside their CU (they do however referencing other types via
19983 DW_FORM_ref_sig8). */
19984
19985 static const struct cu_partial_die_info
19986 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19987 {
19988 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19989 struct objfile *objfile = per_objfile->objfile;
19990 struct partial_die_info *pd = NULL;
19991
19992 if (offset_in_dwz == cu->per_cu->is_dwz
19993 && cu->header.offset_in_cu_p (sect_off))
19994 {
19995 pd = cu->find_partial_die (sect_off);
19996 if (pd != NULL)
19997 return { cu, pd };
19998 /* We missed recording what we needed.
19999 Load all dies and try again. */
20000 }
20001 else
20002 {
20003 /* TUs don't reference other CUs/TUs (except via type signatures). */
20004 if (cu->per_cu->is_debug_types)
20005 {
20006 error (_("Dwarf Error: Type Unit at offset %s contains"
20007 " external reference to offset %s [in module %s].\n"),
20008 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
20009 bfd_get_filename (objfile->obfd));
20010 }
20011 dwarf2_per_cu_data *per_cu
20012 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
20013 per_objfile);
20014
20015 cu = per_objfile->get_cu (per_cu);
20016 if (cu == NULL || cu->partial_dies == NULL)
20017 load_partial_comp_unit (per_cu, per_objfile, nullptr);
20018
20019 cu = per_objfile->get_cu (per_cu);
20020
20021 cu->last_used = 0;
20022 pd = cu->find_partial_die (sect_off);
20023 }
20024
20025 /* If we didn't find it, and not all dies have been loaded,
20026 load them all and try again. */
20027
20028 if (pd == NULL && cu->per_cu->load_all_dies == 0)
20029 {
20030 cu->per_cu->load_all_dies = 1;
20031
20032 /* This is nasty. When we reread the DIEs, somewhere up the call chain
20033 THIS_CU->cu may already be in use. So we can't just free it and
20034 replace its DIEs with the ones we read in. Instead, we leave those
20035 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
20036 and clobber THIS_CU->cu->partial_dies with the hash table for the new
20037 set. */
20038 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
20039
20040 pd = cu->find_partial_die (sect_off);
20041 }
20042
20043 if (pd == NULL)
20044 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
20045 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
20046 return { cu, pd };
20047 }
20048
20049 /* See if we can figure out if the class lives in a namespace. We do
20050 this by looking for a member function; its demangled name will
20051 contain namespace info, if there is any. */
20052
20053 static void
20054 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
20055 struct dwarf2_cu *cu)
20056 {
20057 /* NOTE: carlton/2003-10-07: Getting the info this way changes
20058 what template types look like, because the demangler
20059 frequently doesn't give the same name as the debug info. We
20060 could fix this by only using the demangled name to get the
20061 prefix (but see comment in read_structure_type). */
20062
20063 struct partial_die_info *real_pdi;
20064 struct partial_die_info *child_pdi;
20065
20066 /* If this DIE (this DIE's specification, if any) has a parent, then
20067 we should not do this. We'll prepend the parent's fully qualified
20068 name when we create the partial symbol. */
20069
20070 real_pdi = struct_pdi;
20071 while (real_pdi->has_specification)
20072 {
20073 auto res = find_partial_die (real_pdi->spec_offset,
20074 real_pdi->spec_is_dwz, cu);
20075 real_pdi = res.pdi;
20076 cu = res.cu;
20077 }
20078
20079 if (real_pdi->die_parent != NULL)
20080 return;
20081
20082 for (child_pdi = struct_pdi->die_child;
20083 child_pdi != NULL;
20084 child_pdi = child_pdi->die_sibling)
20085 {
20086 if (child_pdi->tag == DW_TAG_subprogram
20087 && child_pdi->linkage_name != NULL)
20088 {
20089 gdb::unique_xmalloc_ptr<char> actual_class_name
20090 (cu->language_defn->class_name_from_physname
20091 (child_pdi->linkage_name));
20092 if (actual_class_name != NULL)
20093 {
20094 struct objfile *objfile = cu->per_objfile->objfile;
20095 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
20096 struct_pdi->canonical_name = 1;
20097 }
20098 break;
20099 }
20100 }
20101 }
20102
20103 /* Return true if a DIE with TAG may have the DW_AT_const_value
20104 attribute. */
20105
20106 static bool
20107 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
20108 {
20109 switch (tag)
20110 {
20111 case DW_TAG_constant:
20112 case DW_TAG_enumerator:
20113 case DW_TAG_formal_parameter:
20114 case DW_TAG_template_value_param:
20115 case DW_TAG_variable:
20116 return true;
20117 }
20118
20119 return false;
20120 }
20121
20122 void
20123 partial_die_info::fixup (struct dwarf2_cu *cu)
20124 {
20125 /* Once we've fixed up a die, there's no point in doing so again.
20126 This also avoids a memory leak if we were to call
20127 guess_partial_die_structure_name multiple times. */
20128 if (fixup_called)
20129 return;
20130
20131 /* If we found a reference attribute and the DIE has no name, try
20132 to find a name in the referred to DIE. */
20133
20134 if (raw_name == NULL && has_specification)
20135 {
20136 struct partial_die_info *spec_die;
20137
20138 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20139 spec_die = res.pdi;
20140 cu = res.cu;
20141
20142 spec_die->fixup (cu);
20143
20144 if (spec_die->raw_name)
20145 {
20146 raw_name = spec_die->raw_name;
20147 canonical_name = spec_die->canonical_name;
20148
20149 /* Copy DW_AT_external attribute if it is set. */
20150 if (spec_die->is_external)
20151 is_external = spec_die->is_external;
20152 }
20153 }
20154
20155 if (!has_const_value && has_specification
20156 && can_have_DW_AT_const_value_p (tag))
20157 {
20158 struct partial_die_info *spec_die;
20159
20160 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20161 spec_die = res.pdi;
20162 cu = res.cu;
20163
20164 spec_die->fixup (cu);
20165
20166 if (spec_die->has_const_value)
20167 {
20168 /* Copy DW_AT_const_value attribute if it is set. */
20169 has_const_value = spec_die->has_const_value;
20170 }
20171 }
20172
20173 /* Set default names for some unnamed DIEs. */
20174
20175 if (raw_name == NULL && tag == DW_TAG_namespace)
20176 {
20177 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
20178 canonical_name = 1;
20179 }
20180
20181 /* If there is no parent die to provide a namespace, and there are
20182 children, see if we can determine the namespace from their linkage
20183 name. */
20184 if (cu->language == language_cplus
20185 && !cu->per_objfile->per_bfd->types.empty ()
20186 && die_parent == NULL
20187 && has_children
20188 && (tag == DW_TAG_class_type
20189 || tag == DW_TAG_structure_type
20190 || tag == DW_TAG_union_type))
20191 guess_partial_die_structure_name (this, cu);
20192
20193 /* GCC might emit a nameless struct or union that has a linkage
20194 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20195 if (raw_name == NULL
20196 && (tag == DW_TAG_class_type
20197 || tag == DW_TAG_interface_type
20198 || tag == DW_TAG_structure_type
20199 || tag == DW_TAG_union_type)
20200 && linkage_name != NULL)
20201 {
20202 gdb::unique_xmalloc_ptr<char> demangled
20203 (gdb_demangle (linkage_name, DMGL_TYPES));
20204 if (demangled != nullptr)
20205 {
20206 const char *base;
20207
20208 /* Strip any leading namespaces/classes, keep only the base name.
20209 DW_AT_name for named DIEs does not contain the prefixes. */
20210 base = strrchr (demangled.get (), ':');
20211 if (base && base > demangled.get () && base[-1] == ':')
20212 base++;
20213 else
20214 base = demangled.get ();
20215
20216 struct objfile *objfile = cu->per_objfile->objfile;
20217 raw_name = objfile->intern (base);
20218 canonical_name = 1;
20219 }
20220 }
20221
20222 fixup_called = 1;
20223 }
20224
20225 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
20226 contents from the given SECTION in the HEADER.
20227
20228 HEADER_OFFSET is the offset of the header in the section. */
20229 static void
20230 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
20231 struct dwarf2_section_info *section,
20232 sect_offset header_offset)
20233 {
20234 unsigned int bytes_read;
20235 bfd *abfd = section->get_bfd_owner ();
20236 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
20237
20238 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
20239 info_ptr += bytes_read;
20240
20241 header->version = read_2_bytes (abfd, info_ptr);
20242 info_ptr += 2;
20243
20244 header->addr_size = read_1_byte (abfd, info_ptr);
20245 info_ptr += 1;
20246
20247 header->segment_collector_size = read_1_byte (abfd, info_ptr);
20248 info_ptr += 1;
20249
20250 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
20251 }
20252
20253 /* Return the DW_AT_loclists_base value for the CU. */
20254 static ULONGEST
20255 lookup_loclist_base (struct dwarf2_cu *cu)
20256 {
20257 /* For the .dwo unit, the loclist_base points to the first offset following
20258 the header. The header consists of the following entities-
20259 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
20260 bit format)
20261 2. version (2 bytes)
20262 3. address size (1 byte)
20263 4. segment selector size (1 byte)
20264 5. offset entry count (4 bytes)
20265 These sizes are derived as per the DWARFv5 standard. */
20266 if (cu->dwo_unit != nullptr)
20267 {
20268 if (cu->header.initial_length_size == 4)
20269 return LOCLIST_HEADER_SIZE32;
20270 return LOCLIST_HEADER_SIZE64;
20271 }
20272 return cu->loclist_base;
20273 }
20274
20275 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
20276 array of offsets in the .debug_loclists section. */
20277
20278 static sect_offset
20279 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
20280 {
20281 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20282 struct objfile *objfile = per_objfile->objfile;
20283 bfd *abfd = objfile->obfd;
20284 ULONGEST loclist_header_size =
20285 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
20286 : LOCLIST_HEADER_SIZE64);
20287 ULONGEST loclist_base = lookup_loclist_base (cu);
20288
20289 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
20290 ULONGEST start_offset =
20291 loclist_base + loclist_index * cu->header.offset_size;
20292
20293 /* Get loclists section. */
20294 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20295
20296 /* Read the loclists section content. */
20297 section->read (objfile);
20298 if (section->buffer == NULL)
20299 error (_("DW_FORM_loclistx used without .debug_loclists "
20300 "section [in module %s]"), objfile_name (objfile));
20301
20302 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
20303 so if loclist_base is smaller than the header size, we have a problem. */
20304 if (loclist_base < loclist_header_size)
20305 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
20306 objfile_name (objfile));
20307
20308 /* Read the header of the loclists contribution. */
20309 struct loclists_rnglists_header header;
20310 read_loclists_rnglists_header (&header, section,
20311 (sect_offset) (loclist_base - loclist_header_size));
20312
20313 /* Verify the loclist index is valid. */
20314 if (loclist_index >= header.offset_entry_count)
20315 error (_("DW_FORM_loclistx pointing outside of "
20316 ".debug_loclists offset array [in module %s]"),
20317 objfile_name (objfile));
20318
20319 /* Validate that reading won't go beyond the end of the section. */
20320 if (start_offset + cu->header.offset_size > section->size)
20321 error (_("Reading DW_FORM_loclistx index beyond end of"
20322 ".debug_loclists section [in module %s]"),
20323 objfile_name (objfile));
20324
20325 const gdb_byte *info_ptr = section->buffer + start_offset;
20326
20327 if (cu->header.offset_size == 4)
20328 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
20329 else
20330 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
20331 }
20332
20333 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
20334 array of offsets in the .debug_rnglists section. */
20335
20336 static sect_offset
20337 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
20338 dwarf_tag tag)
20339 {
20340 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20341 struct objfile *objfile = dwarf2_per_objfile->objfile;
20342 bfd *abfd = objfile->obfd;
20343 ULONGEST rnglist_header_size =
20344 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
20345 : RNGLIST_HEADER_SIZE64);
20346
20347 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
20348 .debug_rnglists.dwo section. The rnglists base given in the skeleton
20349 doesn't apply. */
20350 ULONGEST rnglist_base =
20351 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
20352
20353 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
20354 ULONGEST start_offset =
20355 rnglist_base + rnglist_index * cu->header.offset_size;
20356
20357 /* Get rnglists section. */
20358 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
20359
20360 /* Read the rnglists section content. */
20361 section->read (objfile);
20362 if (section->buffer == nullptr)
20363 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
20364 "[in module %s]"),
20365 objfile_name (objfile));
20366
20367 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
20368 so if rnglist_base is smaller than the header size, we have a problem. */
20369 if (rnglist_base < rnglist_header_size)
20370 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
20371 objfile_name (objfile));
20372
20373 /* Read the header of the rnglists contribution. */
20374 struct loclists_rnglists_header header;
20375 read_loclists_rnglists_header (&header, section,
20376 (sect_offset) (rnglist_base - rnglist_header_size));
20377
20378 /* Verify the rnglist index is valid. */
20379 if (rnglist_index >= header.offset_entry_count)
20380 error (_("DW_FORM_rnglistx index pointing outside of "
20381 ".debug_rnglists offset array [in module %s]"),
20382 objfile_name (objfile));
20383
20384 /* Validate that reading won't go beyond the end of the section. */
20385 if (start_offset + cu->header.offset_size > section->size)
20386 error (_("Reading DW_FORM_rnglistx index beyond end of"
20387 ".debug_rnglists section [in module %s]"),
20388 objfile_name (objfile));
20389
20390 const gdb_byte *info_ptr = section->buffer + start_offset;
20391
20392 if (cu->header.offset_size == 4)
20393 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
20394 else
20395 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
20396 }
20397
20398 /* Process the attributes that had to be skipped in the first round. These
20399 attributes are the ones that need str_offsets_base or addr_base attributes.
20400 They could not have been processed in the first round, because at the time
20401 the values of str_offsets_base or addr_base may not have been known. */
20402 static void
20403 read_attribute_reprocess (const struct die_reader_specs *reader,
20404 struct attribute *attr, dwarf_tag tag)
20405 {
20406 struct dwarf2_cu *cu = reader->cu;
20407 switch (attr->form)
20408 {
20409 case DW_FORM_addrx:
20410 case DW_FORM_GNU_addr_index:
20411 attr->set_address (read_addr_index (cu,
20412 attr->as_unsigned_reprocess ()));
20413 break;
20414 case DW_FORM_loclistx:
20415 {
20416 sect_offset loclists_sect_off
20417 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
20418
20419 attr->set_unsigned (to_underlying (loclists_sect_off));
20420 }
20421 break;
20422 case DW_FORM_rnglistx:
20423 {
20424 sect_offset rnglists_sect_off
20425 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
20426
20427 attr->set_unsigned (to_underlying (rnglists_sect_off));
20428 }
20429 break;
20430 case DW_FORM_strx:
20431 case DW_FORM_strx1:
20432 case DW_FORM_strx2:
20433 case DW_FORM_strx3:
20434 case DW_FORM_strx4:
20435 case DW_FORM_GNU_str_index:
20436 {
20437 unsigned int str_index = attr->as_unsigned_reprocess ();
20438 gdb_assert (!attr->canonical_string_p ());
20439 if (reader->dwo_file != NULL)
20440 attr->set_string_noncanonical (read_dwo_str_index (reader,
20441 str_index));
20442 else
20443 attr->set_string_noncanonical (read_stub_str_index (cu,
20444 str_index));
20445 break;
20446 }
20447 default:
20448 gdb_assert_not_reached (_("Unexpected DWARF form."));
20449 }
20450 }
20451
20452 /* Read an attribute value described by an attribute form. */
20453
20454 static const gdb_byte *
20455 read_attribute_value (const struct die_reader_specs *reader,
20456 struct attribute *attr, unsigned form,
20457 LONGEST implicit_const, const gdb_byte *info_ptr)
20458 {
20459 struct dwarf2_cu *cu = reader->cu;
20460 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20461 struct objfile *objfile = per_objfile->objfile;
20462 bfd *abfd = reader->abfd;
20463 struct comp_unit_head *cu_header = &cu->header;
20464 unsigned int bytes_read;
20465 struct dwarf_block *blk;
20466
20467 attr->form = (enum dwarf_form) form;
20468 switch (form)
20469 {
20470 case DW_FORM_ref_addr:
20471 if (cu->header.version == 2)
20472 attr->set_unsigned (cu->header.read_address (abfd, info_ptr,
20473 &bytes_read));
20474 else
20475 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20476 &bytes_read));
20477 info_ptr += bytes_read;
20478 break;
20479 case DW_FORM_GNU_ref_alt:
20480 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20481 &bytes_read));
20482 info_ptr += bytes_read;
20483 break;
20484 case DW_FORM_addr:
20485 {
20486 struct gdbarch *gdbarch = objfile->arch ();
20487 CORE_ADDR addr = cu->header.read_address (abfd, info_ptr, &bytes_read);
20488 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20489 attr->set_address (addr);
20490 info_ptr += bytes_read;
20491 }
20492 break;
20493 case DW_FORM_block2:
20494 blk = dwarf_alloc_block (cu);
20495 blk->size = read_2_bytes (abfd, info_ptr);
20496 info_ptr += 2;
20497 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20498 info_ptr += blk->size;
20499 attr->set_block (blk);
20500 break;
20501 case DW_FORM_block4:
20502 blk = dwarf_alloc_block (cu);
20503 blk->size = read_4_bytes (abfd, info_ptr);
20504 info_ptr += 4;
20505 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20506 info_ptr += blk->size;
20507 attr->set_block (blk);
20508 break;
20509 case DW_FORM_data2:
20510 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
20511 info_ptr += 2;
20512 break;
20513 case DW_FORM_data4:
20514 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
20515 info_ptr += 4;
20516 break;
20517 case DW_FORM_data8:
20518 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
20519 info_ptr += 8;
20520 break;
20521 case DW_FORM_data16:
20522 blk = dwarf_alloc_block (cu);
20523 blk->size = 16;
20524 blk->data = read_n_bytes (abfd, info_ptr, 16);
20525 info_ptr += 16;
20526 attr->set_block (blk);
20527 break;
20528 case DW_FORM_sec_offset:
20529 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20530 &bytes_read));
20531 info_ptr += bytes_read;
20532 break;
20533 case DW_FORM_loclistx:
20534 {
20535 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20536 &bytes_read));
20537 info_ptr += bytes_read;
20538 }
20539 break;
20540 case DW_FORM_string:
20541 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20542 &bytes_read));
20543 info_ptr += bytes_read;
20544 break;
20545 case DW_FORM_strp:
20546 if (!cu->per_cu->is_dwz)
20547 {
20548 attr->set_string_noncanonical
20549 (read_indirect_string (per_objfile,
20550 abfd, info_ptr, cu_header,
20551 &bytes_read));
20552 info_ptr += bytes_read;
20553 break;
20554 }
20555 /* FALLTHROUGH */
20556 case DW_FORM_line_strp:
20557 if (!cu->per_cu->is_dwz)
20558 {
20559 attr->set_string_noncanonical
20560 (per_objfile->read_line_string (info_ptr, cu_header,
20561 &bytes_read));
20562 info_ptr += bytes_read;
20563 break;
20564 }
20565 /* FALLTHROUGH */
20566 case DW_FORM_GNU_strp_alt:
20567 {
20568 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
20569 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20570 &bytes_read);
20571
20572 attr->set_string_noncanonical
20573 (dwz->read_string (objfile, str_offset));
20574 info_ptr += bytes_read;
20575 }
20576 break;
20577 case DW_FORM_exprloc:
20578 case DW_FORM_block:
20579 blk = dwarf_alloc_block (cu);
20580 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20581 info_ptr += bytes_read;
20582 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20583 info_ptr += blk->size;
20584 attr->set_block (blk);
20585 break;
20586 case DW_FORM_block1:
20587 blk = dwarf_alloc_block (cu);
20588 blk->size = read_1_byte (abfd, info_ptr);
20589 info_ptr += 1;
20590 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20591 info_ptr += blk->size;
20592 attr->set_block (blk);
20593 break;
20594 case DW_FORM_data1:
20595 case DW_FORM_flag:
20596 attr->set_unsigned (read_1_byte (abfd, info_ptr));
20597 info_ptr += 1;
20598 break;
20599 case DW_FORM_flag_present:
20600 attr->set_unsigned (1);
20601 break;
20602 case DW_FORM_sdata:
20603 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
20604 info_ptr += bytes_read;
20605 break;
20606 case DW_FORM_rnglistx:
20607 {
20608 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20609 &bytes_read));
20610 info_ptr += bytes_read;
20611 }
20612 break;
20613 case DW_FORM_udata:
20614 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
20615 info_ptr += bytes_read;
20616 break;
20617 case DW_FORM_ref1:
20618 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20619 + read_1_byte (abfd, info_ptr)));
20620 info_ptr += 1;
20621 break;
20622 case DW_FORM_ref2:
20623 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20624 + read_2_bytes (abfd, info_ptr)));
20625 info_ptr += 2;
20626 break;
20627 case DW_FORM_ref4:
20628 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20629 + read_4_bytes (abfd, info_ptr)));
20630 info_ptr += 4;
20631 break;
20632 case DW_FORM_ref8:
20633 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20634 + read_8_bytes (abfd, info_ptr)));
20635 info_ptr += 8;
20636 break;
20637 case DW_FORM_ref_sig8:
20638 attr->set_signature (read_8_bytes (abfd, info_ptr));
20639 info_ptr += 8;
20640 break;
20641 case DW_FORM_ref_udata:
20642 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20643 + read_unsigned_leb128 (abfd, info_ptr,
20644 &bytes_read)));
20645 info_ptr += bytes_read;
20646 break;
20647 case DW_FORM_indirect:
20648 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20649 info_ptr += bytes_read;
20650 if (form == DW_FORM_implicit_const)
20651 {
20652 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20653 info_ptr += bytes_read;
20654 }
20655 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
20656 info_ptr);
20657 break;
20658 case DW_FORM_implicit_const:
20659 attr->set_signed (implicit_const);
20660 break;
20661 case DW_FORM_addrx:
20662 case DW_FORM_GNU_addr_index:
20663 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20664 &bytes_read));
20665 info_ptr += bytes_read;
20666 break;
20667 case DW_FORM_strx:
20668 case DW_FORM_strx1:
20669 case DW_FORM_strx2:
20670 case DW_FORM_strx3:
20671 case DW_FORM_strx4:
20672 case DW_FORM_GNU_str_index:
20673 {
20674 ULONGEST str_index;
20675 if (form == DW_FORM_strx1)
20676 {
20677 str_index = read_1_byte (abfd, info_ptr);
20678 info_ptr += 1;
20679 }
20680 else if (form == DW_FORM_strx2)
20681 {
20682 str_index = read_2_bytes (abfd, info_ptr);
20683 info_ptr += 2;
20684 }
20685 else if (form == DW_FORM_strx3)
20686 {
20687 str_index = read_3_bytes (abfd, info_ptr);
20688 info_ptr += 3;
20689 }
20690 else if (form == DW_FORM_strx4)
20691 {
20692 str_index = read_4_bytes (abfd, info_ptr);
20693 info_ptr += 4;
20694 }
20695 else
20696 {
20697 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20698 info_ptr += bytes_read;
20699 }
20700 attr->set_unsigned_reprocess (str_index);
20701 }
20702 break;
20703 default:
20704 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
20705 dwarf_form_name (form),
20706 bfd_get_filename (abfd));
20707 }
20708
20709 /* Super hack. */
20710 if (cu->per_cu->is_dwz && attr->form_is_ref ())
20711 attr->form = DW_FORM_GNU_ref_alt;
20712
20713 /* We have seen instances where the compiler tried to emit a byte
20714 size attribute of -1 which ended up being encoded as an unsigned
20715 0xffffffff. Although 0xffffffff is technically a valid size value,
20716 an object of this size seems pretty unlikely so we can relatively
20717 safely treat these cases as if the size attribute was invalid and
20718 treat them as zero by default. */
20719 if (attr->name == DW_AT_byte_size
20720 && form == DW_FORM_data4
20721 && attr->as_unsigned () >= 0xffffffff)
20722 {
20723 complaint
20724 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20725 hex_string (attr->as_unsigned ()));
20726 attr->set_unsigned (0);
20727 }
20728
20729 return info_ptr;
20730 }
20731
20732 /* Read an attribute described by an abbreviated attribute. */
20733
20734 static const gdb_byte *
20735 read_attribute (const struct die_reader_specs *reader,
20736 struct attribute *attr, struct attr_abbrev *abbrev,
20737 const gdb_byte *info_ptr)
20738 {
20739 attr->name = abbrev->name;
20740 attr->string_is_canonical = 0;
20741 attr->requires_reprocessing = 0;
20742 return read_attribute_value (reader, attr, abbrev->form,
20743 abbrev->implicit_const, info_ptr);
20744 }
20745
20746 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20747
20748 static const char *
20749 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
20750 LONGEST str_offset)
20751 {
20752 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20753 str_offset, "DW_FORM_strp");
20754 }
20755
20756 /* Return pointer to string at .debug_str offset as read from BUF.
20757 BUF is assumed to be in a compilation unit described by CU_HEADER.
20758 Return *BYTES_READ_PTR count of bytes read from BUF. */
20759
20760 static const char *
20761 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
20762 const gdb_byte *buf,
20763 const struct comp_unit_head *cu_header,
20764 unsigned int *bytes_read_ptr)
20765 {
20766 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20767
20768 return read_indirect_string_at_offset (per_objfile, str_offset);
20769 }
20770
20771 /* See read.h. */
20772
20773 const char *
20774 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20775 const struct comp_unit_head *cu_header,
20776 unsigned int *bytes_read_ptr)
20777 {
20778 bfd *abfd = objfile->obfd;
20779 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20780
20781 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20782 }
20783
20784 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20785 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20786 ADDR_SIZE is the size of addresses from the CU header. */
20787
20788 static CORE_ADDR
20789 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20790 gdb::optional<ULONGEST> addr_base, int addr_size)
20791 {
20792 struct objfile *objfile = per_objfile->objfile;
20793 bfd *abfd = objfile->obfd;
20794 const gdb_byte *info_ptr;
20795 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20796
20797 per_objfile->per_bfd->addr.read (objfile);
20798 if (per_objfile->per_bfd->addr.buffer == NULL)
20799 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20800 objfile_name (objfile));
20801 if (addr_base_or_zero + addr_index * addr_size
20802 >= per_objfile->per_bfd->addr.size)
20803 error (_("DW_FORM_addr_index pointing outside of "
20804 ".debug_addr section [in module %s]"),
20805 objfile_name (objfile));
20806 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20807 + addr_index * addr_size);
20808 if (addr_size == 4)
20809 return bfd_get_32 (abfd, info_ptr);
20810 else
20811 return bfd_get_64 (abfd, info_ptr);
20812 }
20813
20814 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20815
20816 static CORE_ADDR
20817 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20818 {
20819 return read_addr_index_1 (cu->per_objfile, addr_index,
20820 cu->addr_base, cu->header.addr_size);
20821 }
20822
20823 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20824
20825 static CORE_ADDR
20826 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20827 unsigned int *bytes_read)
20828 {
20829 bfd *abfd = cu->per_objfile->objfile->obfd;
20830 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20831
20832 return read_addr_index (cu, addr_index);
20833 }
20834
20835 /* See read.h. */
20836
20837 CORE_ADDR
20838 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20839 dwarf2_per_objfile *per_objfile,
20840 unsigned int addr_index)
20841 {
20842 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20843 gdb::optional<ULONGEST> addr_base;
20844 int addr_size;
20845
20846 /* We need addr_base and addr_size.
20847 If we don't have PER_CU->cu, we have to get it.
20848 Nasty, but the alternative is storing the needed info in PER_CU,
20849 which at this point doesn't seem justified: it's not clear how frequently
20850 it would get used and it would increase the size of every PER_CU.
20851 Entry points like dwarf2_per_cu_addr_size do a similar thing
20852 so we're not in uncharted territory here.
20853 Alas we need to be a bit more complicated as addr_base is contained
20854 in the DIE.
20855
20856 We don't need to read the entire CU(/TU).
20857 We just need the header and top level die.
20858
20859 IWBN to use the aging mechanism to let us lazily later discard the CU.
20860 For now we skip this optimization. */
20861
20862 if (cu != NULL)
20863 {
20864 addr_base = cu->addr_base;
20865 addr_size = cu->header.addr_size;
20866 }
20867 else
20868 {
20869 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
20870 addr_base = reader.cu->addr_base;
20871 addr_size = reader.cu->header.addr_size;
20872 }
20873
20874 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
20875 }
20876
20877 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20878 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20879 DWO file. */
20880
20881 static const char *
20882 read_str_index (struct dwarf2_cu *cu,
20883 struct dwarf2_section_info *str_section,
20884 struct dwarf2_section_info *str_offsets_section,
20885 ULONGEST str_offsets_base, ULONGEST str_index)
20886 {
20887 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20888 struct objfile *objfile = per_objfile->objfile;
20889 const char *objf_name = objfile_name (objfile);
20890 bfd *abfd = objfile->obfd;
20891 const gdb_byte *info_ptr;
20892 ULONGEST str_offset;
20893 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20894
20895 str_section->read (objfile);
20896 str_offsets_section->read (objfile);
20897 if (str_section->buffer == NULL)
20898 error (_("%s used without %s section"
20899 " in CU at offset %s [in module %s]"),
20900 form_name, str_section->get_name (),
20901 sect_offset_str (cu->header.sect_off), objf_name);
20902 if (str_offsets_section->buffer == NULL)
20903 error (_("%s used without %s section"
20904 " in CU at offset %s [in module %s]"),
20905 form_name, str_section->get_name (),
20906 sect_offset_str (cu->header.sect_off), objf_name);
20907 info_ptr = (str_offsets_section->buffer
20908 + str_offsets_base
20909 + str_index * cu->header.offset_size);
20910 if (cu->header.offset_size == 4)
20911 str_offset = bfd_get_32 (abfd, info_ptr);
20912 else
20913 str_offset = bfd_get_64 (abfd, info_ptr);
20914 if (str_offset >= str_section->size)
20915 error (_("Offset from %s pointing outside of"
20916 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20917 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20918 return (const char *) (str_section->buffer + str_offset);
20919 }
20920
20921 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20922
20923 static const char *
20924 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20925 {
20926 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20927 ? reader->cu->header.addr_size : 0;
20928 return read_str_index (reader->cu,
20929 &reader->dwo_file->sections.str,
20930 &reader->dwo_file->sections.str_offsets,
20931 str_offsets_base, str_index);
20932 }
20933
20934 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20935
20936 static const char *
20937 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20938 {
20939 struct objfile *objfile = cu->per_objfile->objfile;
20940 const char *objf_name = objfile_name (objfile);
20941 static const char form_name[] = "DW_FORM_GNU_str_index";
20942 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20943
20944 if (!cu->str_offsets_base.has_value ())
20945 error (_("%s used in Fission stub without %s"
20946 " in CU at offset 0x%lx [in module %s]"),
20947 form_name, str_offsets_attr_name,
20948 (long) cu->header.offset_size, objf_name);
20949
20950 return read_str_index (cu,
20951 &cu->per_objfile->per_bfd->str,
20952 &cu->per_objfile->per_bfd->str_offsets,
20953 *cu->str_offsets_base, str_index);
20954 }
20955
20956 /* Return the length of an LEB128 number in BUF. */
20957
20958 static int
20959 leb128_size (const gdb_byte *buf)
20960 {
20961 const gdb_byte *begin = buf;
20962 gdb_byte byte;
20963
20964 while (1)
20965 {
20966 byte = *buf++;
20967 if ((byte & 128) == 0)
20968 return buf - begin;
20969 }
20970 }
20971
20972 static void
20973 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20974 {
20975 switch (lang)
20976 {
20977 case DW_LANG_C89:
20978 case DW_LANG_C99:
20979 case DW_LANG_C11:
20980 case DW_LANG_C:
20981 case DW_LANG_UPC:
20982 cu->language = language_c;
20983 break;
20984 case DW_LANG_Java:
20985 case DW_LANG_C_plus_plus:
20986 case DW_LANG_C_plus_plus_11:
20987 case DW_LANG_C_plus_plus_14:
20988 cu->language = language_cplus;
20989 break;
20990 case DW_LANG_D:
20991 cu->language = language_d;
20992 break;
20993 case DW_LANG_Fortran77:
20994 case DW_LANG_Fortran90:
20995 case DW_LANG_Fortran95:
20996 case DW_LANG_Fortran03:
20997 case DW_LANG_Fortran08:
20998 cu->language = language_fortran;
20999 break;
21000 case DW_LANG_Go:
21001 cu->language = language_go;
21002 break;
21003 case DW_LANG_Mips_Assembler:
21004 cu->language = language_asm;
21005 break;
21006 case DW_LANG_Ada83:
21007 case DW_LANG_Ada95:
21008 cu->language = language_ada;
21009 break;
21010 case DW_LANG_Modula2:
21011 cu->language = language_m2;
21012 break;
21013 case DW_LANG_Pascal83:
21014 cu->language = language_pascal;
21015 break;
21016 case DW_LANG_ObjC:
21017 cu->language = language_objc;
21018 break;
21019 case DW_LANG_Rust:
21020 case DW_LANG_Rust_old:
21021 cu->language = language_rust;
21022 break;
21023 case DW_LANG_Cobol74:
21024 case DW_LANG_Cobol85:
21025 default:
21026 cu->language = language_minimal;
21027 break;
21028 }
21029 cu->language_defn = language_def (cu->language);
21030 }
21031
21032 /* Return the named attribute or NULL if not there. */
21033
21034 static struct attribute *
21035 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
21036 {
21037 for (;;)
21038 {
21039 unsigned int i;
21040 struct attribute *spec = NULL;
21041
21042 for (i = 0; i < die->num_attrs; ++i)
21043 {
21044 if (die->attrs[i].name == name)
21045 return &die->attrs[i];
21046 if (die->attrs[i].name == DW_AT_specification
21047 || die->attrs[i].name == DW_AT_abstract_origin)
21048 spec = &die->attrs[i];
21049 }
21050
21051 if (!spec)
21052 break;
21053
21054 die = follow_die_ref (die, spec, &cu);
21055 }
21056
21057 return NULL;
21058 }
21059
21060 /* Return the string associated with a string-typed attribute, or NULL if it
21061 is either not found or is of an incorrect type. */
21062
21063 static const char *
21064 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
21065 {
21066 struct attribute *attr;
21067 const char *str = NULL;
21068
21069 attr = dwarf2_attr (die, name, cu);
21070
21071 if (attr != NULL)
21072 {
21073 str = attr->as_string ();
21074 if (str == nullptr)
21075 complaint (_("string type expected for attribute %s for "
21076 "DIE at %s in module %s"),
21077 dwarf_attr_name (name), sect_offset_str (die->sect_off),
21078 objfile_name (cu->per_objfile->objfile));
21079 }
21080
21081 return str;
21082 }
21083
21084 /* Return the dwo name or NULL if not present. If present, it is in either
21085 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
21086 static const char *
21087 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
21088 {
21089 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
21090 if (dwo_name == nullptr)
21091 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
21092 return dwo_name;
21093 }
21094
21095 /* Return non-zero iff the attribute NAME is defined for the given DIE,
21096 and holds a non-zero value. This function should only be used for
21097 DW_FORM_flag or DW_FORM_flag_present attributes. */
21098
21099 static int
21100 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
21101 {
21102 struct attribute *attr = dwarf2_attr (die, name, cu);
21103
21104 return attr != nullptr && attr->as_boolean ();
21105 }
21106
21107 static int
21108 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
21109 {
21110 /* A DIE is a declaration if it has a DW_AT_declaration attribute
21111 which value is non-zero. However, we have to be careful with
21112 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
21113 (via dwarf2_flag_true_p) follows this attribute. So we may
21114 end up accidently finding a declaration attribute that belongs
21115 to a different DIE referenced by the specification attribute,
21116 even though the given DIE does not have a declaration attribute. */
21117 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
21118 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
21119 }
21120
21121 /* Return the die giving the specification for DIE, if there is
21122 one. *SPEC_CU is the CU containing DIE on input, and the CU
21123 containing the return value on output. If there is no
21124 specification, but there is an abstract origin, that is
21125 returned. */
21126
21127 static struct die_info *
21128 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
21129 {
21130 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
21131 *spec_cu);
21132
21133 if (spec_attr == NULL)
21134 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
21135
21136 if (spec_attr == NULL)
21137 return NULL;
21138 else
21139 return follow_die_ref (die, spec_attr, spec_cu);
21140 }
21141
21142 /* Stub for free_line_header to match void * callback types. */
21143
21144 static void
21145 free_line_header_voidp (void *arg)
21146 {
21147 struct line_header *lh = (struct line_header *) arg;
21148
21149 delete lh;
21150 }
21151
21152 /* A convenience function to find the proper .debug_line section for a CU. */
21153
21154 static struct dwarf2_section_info *
21155 get_debug_line_section (struct dwarf2_cu *cu)
21156 {
21157 struct dwarf2_section_info *section;
21158 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21159
21160 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
21161 DWO file. */
21162 if (cu->dwo_unit && cu->per_cu->is_debug_types)
21163 section = &cu->dwo_unit->dwo_file->sections.line;
21164 else if (cu->per_cu->is_dwz)
21165 {
21166 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
21167
21168 section = &dwz->line;
21169 }
21170 else
21171 section = &per_objfile->per_bfd->line;
21172
21173 return section;
21174 }
21175
21176 /* Read the statement program header starting at OFFSET in
21177 .debug_line, or .debug_line.dwo. Return a pointer
21178 to a struct line_header, allocated using xmalloc.
21179 Returns NULL if there is a problem reading the header, e.g., if it
21180 has a version we don't understand.
21181
21182 NOTE: the strings in the include directory and file name tables of
21183 the returned object point into the dwarf line section buffer,
21184 and must not be freed. */
21185
21186 static line_header_up
21187 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
21188 {
21189 struct dwarf2_section_info *section;
21190 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21191
21192 section = get_debug_line_section (cu);
21193 section->read (per_objfile->objfile);
21194 if (section->buffer == NULL)
21195 {
21196 if (cu->dwo_unit && cu->per_cu->is_debug_types)
21197 complaint (_("missing .debug_line.dwo section"));
21198 else
21199 complaint (_("missing .debug_line section"));
21200 return 0;
21201 }
21202
21203 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
21204 per_objfile, section, &cu->header);
21205 }
21206
21207 /* Subroutine of dwarf_decode_lines to simplify it.
21208 Return the file name of the psymtab for the given file_entry.
21209 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21210 If space for the result is malloc'd, *NAME_HOLDER will be set.
21211 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
21212
21213 static const char *
21214 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
21215 const dwarf2_psymtab *pst,
21216 const char *comp_dir,
21217 gdb::unique_xmalloc_ptr<char> *name_holder)
21218 {
21219 const char *include_name = fe.name;
21220 const char *include_name_to_compare = include_name;
21221 const char *pst_filename;
21222 int file_is_pst;
21223
21224 const char *dir_name = fe.include_dir (lh);
21225
21226 gdb::unique_xmalloc_ptr<char> hold_compare;
21227 if (!IS_ABSOLUTE_PATH (include_name)
21228 && (dir_name != NULL || comp_dir != NULL))
21229 {
21230 /* Avoid creating a duplicate psymtab for PST.
21231 We do this by comparing INCLUDE_NAME and PST_FILENAME.
21232 Before we do the comparison, however, we need to account
21233 for DIR_NAME and COMP_DIR.
21234 First prepend dir_name (if non-NULL). If we still don't
21235 have an absolute path prepend comp_dir (if non-NULL).
21236 However, the directory we record in the include-file's
21237 psymtab does not contain COMP_DIR (to match the
21238 corresponding symtab(s)).
21239
21240 Example:
21241
21242 bash$ cd /tmp
21243 bash$ gcc -g ./hello.c
21244 include_name = "hello.c"
21245 dir_name = "."
21246 DW_AT_comp_dir = comp_dir = "/tmp"
21247 DW_AT_name = "./hello.c"
21248
21249 */
21250
21251 if (dir_name != NULL)
21252 {
21253 name_holder->reset (concat (dir_name, SLASH_STRING,
21254 include_name, (char *) NULL));
21255 include_name = name_holder->get ();
21256 include_name_to_compare = include_name;
21257 }
21258 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
21259 {
21260 hold_compare.reset (concat (comp_dir, SLASH_STRING,
21261 include_name, (char *) NULL));
21262 include_name_to_compare = hold_compare.get ();
21263 }
21264 }
21265
21266 pst_filename = pst->filename;
21267 gdb::unique_xmalloc_ptr<char> copied_name;
21268 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
21269 {
21270 copied_name.reset (concat (pst->dirname, SLASH_STRING,
21271 pst_filename, (char *) NULL));
21272 pst_filename = copied_name.get ();
21273 }
21274
21275 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
21276
21277 if (file_is_pst)
21278 return NULL;
21279 return include_name;
21280 }
21281
21282 /* State machine to track the state of the line number program. */
21283
21284 class lnp_state_machine
21285 {
21286 public:
21287 /* Initialize a machine state for the start of a line number
21288 program. */
21289 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
21290 bool record_lines_p);
21291
21292 file_entry *current_file ()
21293 {
21294 /* lh->file_names is 0-based, but the file name numbers in the
21295 statement program are 1-based. */
21296 return m_line_header->file_name_at (m_file);
21297 }
21298
21299 /* Record the line in the state machine. END_SEQUENCE is true if
21300 we're processing the end of a sequence. */
21301 void record_line (bool end_sequence);
21302
21303 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
21304 nop-out rest of the lines in this sequence. */
21305 void check_line_address (struct dwarf2_cu *cu,
21306 const gdb_byte *line_ptr,
21307 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
21308
21309 void handle_set_discriminator (unsigned int discriminator)
21310 {
21311 m_discriminator = discriminator;
21312 m_line_has_non_zero_discriminator |= discriminator != 0;
21313 }
21314
21315 /* Handle DW_LNE_set_address. */
21316 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21317 {
21318 m_op_index = 0;
21319 address += baseaddr;
21320 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21321 }
21322
21323 /* Handle DW_LNS_advance_pc. */
21324 void handle_advance_pc (CORE_ADDR adjust);
21325
21326 /* Handle a special opcode. */
21327 void handle_special_opcode (unsigned char op_code);
21328
21329 /* Handle DW_LNS_advance_line. */
21330 void handle_advance_line (int line_delta)
21331 {
21332 advance_line (line_delta);
21333 }
21334
21335 /* Handle DW_LNS_set_file. */
21336 void handle_set_file (file_name_index file);
21337
21338 /* Handle DW_LNS_negate_stmt. */
21339 void handle_negate_stmt ()
21340 {
21341 m_is_stmt = !m_is_stmt;
21342 }
21343
21344 /* Handle DW_LNS_const_add_pc. */
21345 void handle_const_add_pc ();
21346
21347 /* Handle DW_LNS_fixed_advance_pc. */
21348 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21349 {
21350 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21351 m_op_index = 0;
21352 }
21353
21354 /* Handle DW_LNS_copy. */
21355 void handle_copy ()
21356 {
21357 record_line (false);
21358 m_discriminator = 0;
21359 }
21360
21361 /* Handle DW_LNE_end_sequence. */
21362 void handle_end_sequence ()
21363 {
21364 m_currently_recording_lines = true;
21365 }
21366
21367 private:
21368 /* Advance the line by LINE_DELTA. */
21369 void advance_line (int line_delta)
21370 {
21371 m_line += line_delta;
21372
21373 if (line_delta != 0)
21374 m_line_has_non_zero_discriminator = m_discriminator != 0;
21375 }
21376
21377 struct dwarf2_cu *m_cu;
21378
21379 gdbarch *m_gdbarch;
21380
21381 /* True if we're recording lines.
21382 Otherwise we're building partial symtabs and are just interested in
21383 finding include files mentioned by the line number program. */
21384 bool m_record_lines_p;
21385
21386 /* The line number header. */
21387 line_header *m_line_header;
21388
21389 /* These are part of the standard DWARF line number state machine,
21390 and initialized according to the DWARF spec. */
21391
21392 unsigned char m_op_index = 0;
21393 /* The line table index of the current file. */
21394 file_name_index m_file = 1;
21395 unsigned int m_line = 1;
21396
21397 /* These are initialized in the constructor. */
21398
21399 CORE_ADDR m_address;
21400 bool m_is_stmt;
21401 unsigned int m_discriminator;
21402
21403 /* Additional bits of state we need to track. */
21404
21405 /* The last file that we called dwarf2_start_subfile for.
21406 This is only used for TLLs. */
21407 unsigned int m_last_file = 0;
21408 /* The last file a line number was recorded for. */
21409 struct subfile *m_last_subfile = NULL;
21410
21411 /* The address of the last line entry. */
21412 CORE_ADDR m_last_address;
21413
21414 /* Set to true when a previous line at the same address (using
21415 m_last_address) had m_is_stmt true. This is reset to false when a
21416 line entry at a new address (m_address different to m_last_address) is
21417 processed. */
21418 bool m_stmt_at_address = false;
21419
21420 /* When true, record the lines we decode. */
21421 bool m_currently_recording_lines = false;
21422
21423 /* The last line number that was recorded, used to coalesce
21424 consecutive entries for the same line. This can happen, for
21425 example, when discriminators are present. PR 17276. */
21426 unsigned int m_last_line = 0;
21427 bool m_line_has_non_zero_discriminator = false;
21428 };
21429
21430 void
21431 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21432 {
21433 CORE_ADDR addr_adj = (((m_op_index + adjust)
21434 / m_line_header->maximum_ops_per_instruction)
21435 * m_line_header->minimum_instruction_length);
21436 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21437 m_op_index = ((m_op_index + adjust)
21438 % m_line_header->maximum_ops_per_instruction);
21439 }
21440
21441 void
21442 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21443 {
21444 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21445 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21446 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21447 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
21448 / m_line_header->maximum_ops_per_instruction)
21449 * m_line_header->minimum_instruction_length);
21450 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21451 m_op_index = ((m_op_index + adj_opcode_d)
21452 % m_line_header->maximum_ops_per_instruction);
21453
21454 int line_delta = m_line_header->line_base + adj_opcode_r;
21455 advance_line (line_delta);
21456 record_line (false);
21457 m_discriminator = 0;
21458 }
21459
21460 void
21461 lnp_state_machine::handle_set_file (file_name_index file)
21462 {
21463 m_file = file;
21464
21465 const file_entry *fe = current_file ();
21466 if (fe == NULL)
21467 dwarf2_debug_line_missing_file_complaint ();
21468 else if (m_record_lines_p)
21469 {
21470 const char *dir = fe->include_dir (m_line_header);
21471
21472 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21473 m_line_has_non_zero_discriminator = m_discriminator != 0;
21474 dwarf2_start_subfile (m_cu, fe->name, dir);
21475 }
21476 }
21477
21478 void
21479 lnp_state_machine::handle_const_add_pc ()
21480 {
21481 CORE_ADDR adjust
21482 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21483
21484 CORE_ADDR addr_adj
21485 = (((m_op_index + adjust)
21486 / m_line_header->maximum_ops_per_instruction)
21487 * m_line_header->minimum_instruction_length);
21488
21489 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21490 m_op_index = ((m_op_index + adjust)
21491 % m_line_header->maximum_ops_per_instruction);
21492 }
21493
21494 /* Return non-zero if we should add LINE to the line number table.
21495 LINE is the line to add, LAST_LINE is the last line that was added,
21496 LAST_SUBFILE is the subfile for LAST_LINE.
21497 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21498 had a non-zero discriminator.
21499
21500 We have to be careful in the presence of discriminators.
21501 E.g., for this line:
21502
21503 for (i = 0; i < 100000; i++);
21504
21505 clang can emit four line number entries for that one line,
21506 each with a different discriminator.
21507 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21508
21509 However, we want gdb to coalesce all four entries into one.
21510 Otherwise the user could stepi into the middle of the line and
21511 gdb would get confused about whether the pc really was in the
21512 middle of the line.
21513
21514 Things are further complicated by the fact that two consecutive
21515 line number entries for the same line is a heuristic used by gcc
21516 to denote the end of the prologue. So we can't just discard duplicate
21517 entries, we have to be selective about it. The heuristic we use is
21518 that we only collapse consecutive entries for the same line if at least
21519 one of those entries has a non-zero discriminator. PR 17276.
21520
21521 Note: Addresses in the line number state machine can never go backwards
21522 within one sequence, thus this coalescing is ok. */
21523
21524 static int
21525 dwarf_record_line_p (struct dwarf2_cu *cu,
21526 unsigned int line, unsigned int last_line,
21527 int line_has_non_zero_discriminator,
21528 struct subfile *last_subfile)
21529 {
21530 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21531 return 1;
21532 if (line != last_line)
21533 return 1;
21534 /* Same line for the same file that we've seen already.
21535 As a last check, for pr 17276, only record the line if the line
21536 has never had a non-zero discriminator. */
21537 if (!line_has_non_zero_discriminator)
21538 return 1;
21539 return 0;
21540 }
21541
21542 /* Use the CU's builder to record line number LINE beginning at
21543 address ADDRESS in the line table of subfile SUBFILE. */
21544
21545 static void
21546 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21547 unsigned int line, CORE_ADDR address, bool is_stmt,
21548 struct dwarf2_cu *cu)
21549 {
21550 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21551
21552 if (dwarf_line_debug)
21553 {
21554 fprintf_unfiltered (gdb_stdlog,
21555 "Recording line %u, file %s, address %s\n",
21556 line, lbasename (subfile->name),
21557 paddress (gdbarch, address));
21558 }
21559
21560 if (cu != nullptr)
21561 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
21562 }
21563
21564 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21565 Mark the end of a set of line number records.
21566 The arguments are the same as for dwarf_record_line_1.
21567 If SUBFILE is NULL the request is ignored. */
21568
21569 static void
21570 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21571 CORE_ADDR address, struct dwarf2_cu *cu)
21572 {
21573 if (subfile == NULL)
21574 return;
21575
21576 if (dwarf_line_debug)
21577 {
21578 fprintf_unfiltered (gdb_stdlog,
21579 "Finishing current line, file %s, address %s\n",
21580 lbasename (subfile->name),
21581 paddress (gdbarch, address));
21582 }
21583
21584 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
21585 }
21586
21587 void
21588 lnp_state_machine::record_line (bool end_sequence)
21589 {
21590 if (dwarf_line_debug)
21591 {
21592 fprintf_unfiltered (gdb_stdlog,
21593 "Processing actual line %u: file %u,"
21594 " address %s, is_stmt %u, discrim %u%s\n",
21595 m_line, m_file,
21596 paddress (m_gdbarch, m_address),
21597 m_is_stmt, m_discriminator,
21598 (end_sequence ? "\t(end sequence)" : ""));
21599 }
21600
21601 file_entry *fe = current_file ();
21602
21603 if (fe == NULL)
21604 dwarf2_debug_line_missing_file_complaint ();
21605 /* For now we ignore lines not starting on an instruction boundary.
21606 But not when processing end_sequence for compatibility with the
21607 previous version of the code. */
21608 else if (m_op_index == 0 || end_sequence)
21609 {
21610 fe->included_p = 1;
21611 if (m_record_lines_p)
21612 {
21613 /* When we switch files we insert an end maker in the first file,
21614 switch to the second file and add a new line entry. The
21615 problem is that the end marker inserted in the first file will
21616 discard any previous line entries at the same address. If the
21617 line entries in the first file are marked as is-stmt, while
21618 the new line in the second file is non-stmt, then this means
21619 the end marker will discard is-stmt lines so we can have a
21620 non-stmt line. This means that there are less addresses at
21621 which the user can insert a breakpoint.
21622
21623 To improve this we track the last address in m_last_address,
21624 and whether we have seen an is-stmt at this address. Then
21625 when switching files, if we have seen a stmt at the current
21626 address, and we are switching to create a non-stmt line, then
21627 discard the new line. */
21628 bool file_changed
21629 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21630 bool ignore_this_line
21631 = ((file_changed && !end_sequence && m_last_address == m_address
21632 && !m_is_stmt && m_stmt_at_address)
21633 || (!end_sequence && m_line == 0));
21634
21635 if ((file_changed && !ignore_this_line) || end_sequence)
21636 {
21637 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21638 m_currently_recording_lines ? m_cu : nullptr);
21639 }
21640
21641 if (!end_sequence && !ignore_this_line)
21642 {
21643 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21644
21645 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21646 m_line_has_non_zero_discriminator,
21647 m_last_subfile))
21648 {
21649 buildsym_compunit *builder = m_cu->get_builder ();
21650 dwarf_record_line_1 (m_gdbarch,
21651 builder->get_current_subfile (),
21652 m_line, m_address, is_stmt,
21653 m_currently_recording_lines ? m_cu : nullptr);
21654 }
21655 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21656 m_last_line = m_line;
21657 }
21658 }
21659 }
21660
21661 /* Track whether we have seen any m_is_stmt true at m_address in case we
21662 have multiple line table entries all at m_address. */
21663 if (m_last_address != m_address)
21664 {
21665 m_stmt_at_address = false;
21666 m_last_address = m_address;
21667 }
21668 m_stmt_at_address |= m_is_stmt;
21669 }
21670
21671 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21672 line_header *lh, bool record_lines_p)
21673 {
21674 m_cu = cu;
21675 m_gdbarch = arch;
21676 m_record_lines_p = record_lines_p;
21677 m_line_header = lh;
21678
21679 m_currently_recording_lines = true;
21680
21681 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21682 was a line entry for it so that the backend has a chance to adjust it
21683 and also record it in case it needs it. This is currently used by MIPS
21684 code, cf. `mips_adjust_dwarf2_line'. */
21685 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21686 m_is_stmt = lh->default_is_stmt;
21687 m_discriminator = 0;
21688
21689 m_last_address = m_address;
21690 m_stmt_at_address = false;
21691 }
21692
21693 void
21694 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21695 const gdb_byte *line_ptr,
21696 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21697 {
21698 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21699 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21700 located at 0x0. In this case, additionally check that if
21701 ADDRESS < UNRELOCATED_LOWPC. */
21702
21703 if ((address == 0 && address < unrelocated_lowpc)
21704 || address == (CORE_ADDR) -1)
21705 {
21706 /* This line table is for a function which has been
21707 GCd by the linker. Ignore it. PR gdb/12528 */
21708
21709 struct objfile *objfile = cu->per_objfile->objfile;
21710 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21711
21712 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21713 line_offset, objfile_name (objfile));
21714 m_currently_recording_lines = false;
21715 /* Note: m_currently_recording_lines is left as false until we see
21716 DW_LNE_end_sequence. */
21717 }
21718 }
21719
21720 /* Subroutine of dwarf_decode_lines to simplify it.
21721 Process the line number information in LH.
21722 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21723 program in order to set included_p for every referenced header. */
21724
21725 static void
21726 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21727 const int decode_for_pst_p, CORE_ADDR lowpc)
21728 {
21729 const gdb_byte *line_ptr, *extended_end;
21730 const gdb_byte *line_end;
21731 unsigned int bytes_read, extended_len;
21732 unsigned char op_code, extended_op;
21733 CORE_ADDR baseaddr;
21734 struct objfile *objfile = cu->per_objfile->objfile;
21735 bfd *abfd = objfile->obfd;
21736 struct gdbarch *gdbarch = objfile->arch ();
21737 /* True if we're recording line info (as opposed to building partial
21738 symtabs and just interested in finding include files mentioned by
21739 the line number program). */
21740 bool record_lines_p = !decode_for_pst_p;
21741
21742 baseaddr = objfile->text_section_offset ();
21743
21744 line_ptr = lh->statement_program_start;
21745 line_end = lh->statement_program_end;
21746
21747 /* Read the statement sequences until there's nothing left. */
21748 while (line_ptr < line_end)
21749 {
21750 /* The DWARF line number program state machine. Reset the state
21751 machine at the start of each sequence. */
21752 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21753 bool end_sequence = false;
21754
21755 if (record_lines_p)
21756 {
21757 /* Start a subfile for the current file of the state
21758 machine. */
21759 const file_entry *fe = state_machine.current_file ();
21760
21761 if (fe != NULL)
21762 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21763 }
21764
21765 /* Decode the table. */
21766 while (line_ptr < line_end && !end_sequence)
21767 {
21768 op_code = read_1_byte (abfd, line_ptr);
21769 line_ptr += 1;
21770
21771 if (op_code >= lh->opcode_base)
21772 {
21773 /* Special opcode. */
21774 state_machine.handle_special_opcode (op_code);
21775 }
21776 else switch (op_code)
21777 {
21778 case DW_LNS_extended_op:
21779 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21780 &bytes_read);
21781 line_ptr += bytes_read;
21782 extended_end = line_ptr + extended_len;
21783 extended_op = read_1_byte (abfd, line_ptr);
21784 line_ptr += 1;
21785 if (DW_LNE_lo_user <= extended_op
21786 && extended_op <= DW_LNE_hi_user)
21787 {
21788 /* Vendor extension, ignore. */
21789 line_ptr = extended_end;
21790 break;
21791 }
21792 switch (extended_op)
21793 {
21794 case DW_LNE_end_sequence:
21795 state_machine.handle_end_sequence ();
21796 end_sequence = true;
21797 break;
21798 case DW_LNE_set_address:
21799 {
21800 CORE_ADDR address
21801 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21802 line_ptr += bytes_read;
21803
21804 state_machine.check_line_address (cu, line_ptr,
21805 lowpc - baseaddr, address);
21806 state_machine.handle_set_address (baseaddr, address);
21807 }
21808 break;
21809 case DW_LNE_define_file:
21810 {
21811 const char *cur_file;
21812 unsigned int mod_time, length;
21813 dir_index dindex;
21814
21815 cur_file = read_direct_string (abfd, line_ptr,
21816 &bytes_read);
21817 line_ptr += bytes_read;
21818 dindex = (dir_index)
21819 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21820 line_ptr += bytes_read;
21821 mod_time =
21822 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21823 line_ptr += bytes_read;
21824 length =
21825 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21826 line_ptr += bytes_read;
21827 lh->add_file_name (cur_file, dindex, mod_time, length);
21828 }
21829 break;
21830 case DW_LNE_set_discriminator:
21831 {
21832 /* The discriminator is not interesting to the
21833 debugger; just ignore it. We still need to
21834 check its value though:
21835 if there are consecutive entries for the same
21836 (non-prologue) line we want to coalesce them.
21837 PR 17276. */
21838 unsigned int discr
21839 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21840 line_ptr += bytes_read;
21841
21842 state_machine.handle_set_discriminator (discr);
21843 }
21844 break;
21845 default:
21846 complaint (_("mangled .debug_line section"));
21847 return;
21848 }
21849 /* Make sure that we parsed the extended op correctly. If e.g.
21850 we expected a different address size than the producer used,
21851 we may have read the wrong number of bytes. */
21852 if (line_ptr != extended_end)
21853 {
21854 complaint (_("mangled .debug_line section"));
21855 return;
21856 }
21857 break;
21858 case DW_LNS_copy:
21859 state_machine.handle_copy ();
21860 break;
21861 case DW_LNS_advance_pc:
21862 {
21863 CORE_ADDR adjust
21864 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21865 line_ptr += bytes_read;
21866
21867 state_machine.handle_advance_pc (adjust);
21868 }
21869 break;
21870 case DW_LNS_advance_line:
21871 {
21872 int line_delta
21873 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21874 line_ptr += bytes_read;
21875
21876 state_machine.handle_advance_line (line_delta);
21877 }
21878 break;
21879 case DW_LNS_set_file:
21880 {
21881 file_name_index file
21882 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21883 &bytes_read);
21884 line_ptr += bytes_read;
21885
21886 state_machine.handle_set_file (file);
21887 }
21888 break;
21889 case DW_LNS_set_column:
21890 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21891 line_ptr += bytes_read;
21892 break;
21893 case DW_LNS_negate_stmt:
21894 state_machine.handle_negate_stmt ();
21895 break;
21896 case DW_LNS_set_basic_block:
21897 break;
21898 /* Add to the address register of the state machine the
21899 address increment value corresponding to special opcode
21900 255. I.e., this value is scaled by the minimum
21901 instruction length since special opcode 255 would have
21902 scaled the increment. */
21903 case DW_LNS_const_add_pc:
21904 state_machine.handle_const_add_pc ();
21905 break;
21906 case DW_LNS_fixed_advance_pc:
21907 {
21908 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21909 line_ptr += 2;
21910
21911 state_machine.handle_fixed_advance_pc (addr_adj);
21912 }
21913 break;
21914 default:
21915 {
21916 /* Unknown standard opcode, ignore it. */
21917 int i;
21918
21919 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21920 {
21921 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21922 line_ptr += bytes_read;
21923 }
21924 }
21925 }
21926 }
21927
21928 if (!end_sequence)
21929 dwarf2_debug_line_missing_end_sequence_complaint ();
21930
21931 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21932 in which case we still finish recording the last line). */
21933 state_machine.record_line (true);
21934 }
21935 }
21936
21937 /* Decode the Line Number Program (LNP) for the given line_header
21938 structure and CU. The actual information extracted and the type
21939 of structures created from the LNP depends on the value of PST.
21940
21941 1. If PST is NULL, then this procedure uses the data from the program
21942 to create all necessary symbol tables, and their linetables.
21943
21944 2. If PST is not NULL, this procedure reads the program to determine
21945 the list of files included by the unit represented by PST, and
21946 builds all the associated partial symbol tables.
21947
21948 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21949 It is used for relative paths in the line table.
21950 NOTE: When processing partial symtabs (pst != NULL),
21951 comp_dir == pst->dirname.
21952
21953 NOTE: It is important that psymtabs have the same file name (via strcmp)
21954 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21955 symtab we don't use it in the name of the psymtabs we create.
21956 E.g. expand_line_sal requires this when finding psymtabs to expand.
21957 A good testcase for this is mb-inline.exp.
21958
21959 LOWPC is the lowest address in CU (or 0 if not known).
21960
21961 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21962 for its PC<->lines mapping information. Otherwise only the filename
21963 table is read in. */
21964
21965 static void
21966 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21967 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21968 CORE_ADDR lowpc, int decode_mapping)
21969 {
21970 struct objfile *objfile = cu->per_objfile->objfile;
21971 const int decode_for_pst_p = (pst != NULL);
21972
21973 if (decode_mapping)
21974 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21975
21976 if (decode_for_pst_p)
21977 {
21978 /* Now that we're done scanning the Line Header Program, we can
21979 create the psymtab of each included file. */
21980 for (auto &file_entry : lh->file_names ())
21981 if (file_entry.included_p == 1)
21982 {
21983 gdb::unique_xmalloc_ptr<char> name_holder;
21984 const char *include_name =
21985 psymtab_include_file_name (lh, file_entry, pst,
21986 comp_dir, &name_holder);
21987 if (include_name != NULL)
21988 dwarf2_create_include_psymtab (include_name, pst, objfile);
21989 }
21990 }
21991 else
21992 {
21993 /* Make sure a symtab is created for every file, even files
21994 which contain only variables (i.e. no code with associated
21995 line numbers). */
21996 buildsym_compunit *builder = cu->get_builder ();
21997 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21998
21999 for (auto &fe : lh->file_names ())
22000 {
22001 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
22002 if (builder->get_current_subfile ()->symtab == NULL)
22003 {
22004 builder->get_current_subfile ()->symtab
22005 = allocate_symtab (cust,
22006 builder->get_current_subfile ()->name);
22007 }
22008 fe.symtab = builder->get_current_subfile ()->symtab;
22009 }
22010 }
22011 }
22012
22013 /* Start a subfile for DWARF. FILENAME is the name of the file and
22014 DIRNAME the name of the source directory which contains FILENAME
22015 or NULL if not known.
22016 This routine tries to keep line numbers from identical absolute and
22017 relative file names in a common subfile.
22018
22019 Using the `list' example from the GDB testsuite, which resides in
22020 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
22021 of /srcdir/list0.c yields the following debugging information for list0.c:
22022
22023 DW_AT_name: /srcdir/list0.c
22024 DW_AT_comp_dir: /compdir
22025 files.files[0].name: list0.h
22026 files.files[0].dir: /srcdir
22027 files.files[1].name: list0.c
22028 files.files[1].dir: /srcdir
22029
22030 The line number information for list0.c has to end up in a single
22031 subfile, so that `break /srcdir/list0.c:1' works as expected.
22032 start_subfile will ensure that this happens provided that we pass the
22033 concatenation of files.files[1].dir and files.files[1].name as the
22034 subfile's name. */
22035
22036 static void
22037 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
22038 const char *dirname)
22039 {
22040 gdb::unique_xmalloc_ptr<char> copy;
22041
22042 /* In order not to lose the line information directory,
22043 we concatenate it to the filename when it makes sense.
22044 Note that the Dwarf3 standard says (speaking of filenames in line
22045 information): ``The directory index is ignored for file names
22046 that represent full path names''. Thus ignoring dirname in the
22047 `else' branch below isn't an issue. */
22048
22049 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
22050 {
22051 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
22052 filename = copy.get ();
22053 }
22054
22055 cu->get_builder ()->start_subfile (filename);
22056 }
22057
22058 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
22059 buildsym_compunit constructor. */
22060
22061 struct compunit_symtab *
22062 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
22063 CORE_ADDR low_pc)
22064 {
22065 gdb_assert (m_builder == nullptr);
22066
22067 m_builder.reset (new struct buildsym_compunit
22068 (this->per_objfile->objfile,
22069 name, comp_dir, language, low_pc));
22070
22071 list_in_scope = get_builder ()->get_file_symbols ();
22072
22073 get_builder ()->record_debugformat ("DWARF 2");
22074 get_builder ()->record_producer (producer);
22075
22076 processing_has_namespace_info = false;
22077
22078 return get_builder ()->get_compunit_symtab ();
22079 }
22080
22081 static void
22082 var_decode_location (struct attribute *attr, struct symbol *sym,
22083 struct dwarf2_cu *cu)
22084 {
22085 struct objfile *objfile = cu->per_objfile->objfile;
22086 struct comp_unit_head *cu_header = &cu->header;
22087
22088 /* NOTE drow/2003-01-30: There used to be a comment and some special
22089 code here to turn a symbol with DW_AT_external and a
22090 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
22091 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
22092 with some versions of binutils) where shared libraries could have
22093 relocations against symbols in their debug information - the
22094 minimal symbol would have the right address, but the debug info
22095 would not. It's no longer necessary, because we will explicitly
22096 apply relocations when we read in the debug information now. */
22097
22098 /* A DW_AT_location attribute with no contents indicates that a
22099 variable has been optimized away. */
22100 if (attr->form_is_block () && attr->as_block ()->size == 0)
22101 {
22102 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22103 return;
22104 }
22105
22106 /* Handle one degenerate form of location expression specially, to
22107 preserve GDB's previous behavior when section offsets are
22108 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
22109 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
22110
22111 if (attr->form_is_block ())
22112 {
22113 struct dwarf_block *block = attr->as_block ();
22114
22115 if ((block->data[0] == DW_OP_addr
22116 && block->size == 1 + cu_header->addr_size)
22117 || ((block->data[0] == DW_OP_GNU_addr_index
22118 || block->data[0] == DW_OP_addrx)
22119 && (block->size
22120 == 1 + leb128_size (&block->data[1]))))
22121 {
22122 unsigned int dummy;
22123
22124 if (block->data[0] == DW_OP_addr)
22125 SET_SYMBOL_VALUE_ADDRESS
22126 (sym, cu->header.read_address (objfile->obfd,
22127 block->data + 1,
22128 &dummy));
22129 else
22130 SET_SYMBOL_VALUE_ADDRESS
22131 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
22132 &dummy));
22133 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
22134 fixup_symbol_section (sym, objfile);
22135 SET_SYMBOL_VALUE_ADDRESS
22136 (sym,
22137 SYMBOL_VALUE_ADDRESS (sym)
22138 + objfile->section_offsets[sym->section_index ()]);
22139 return;
22140 }
22141 }
22142
22143 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
22144 expression evaluator, and use LOC_COMPUTED only when necessary
22145 (i.e. when the value of a register or memory location is
22146 referenced, or a thread-local block, etc.). Then again, it might
22147 not be worthwhile. I'm assuming that it isn't unless performance
22148 or memory numbers show me otherwise. */
22149
22150 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
22151
22152 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
22153 cu->has_loclist = true;
22154 }
22155
22156 /* Given a pointer to a DWARF information entry, figure out if we need
22157 to make a symbol table entry for it, and if so, create a new entry
22158 and return a pointer to it.
22159 If TYPE is NULL, determine symbol type from the die, otherwise
22160 used the passed type.
22161 If SPACE is not NULL, use it to hold the new symbol. If it is
22162 NULL, allocate a new symbol on the objfile's obstack. */
22163
22164 static struct symbol *
22165 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
22166 struct symbol *space)
22167 {
22168 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22169 struct objfile *objfile = per_objfile->objfile;
22170 struct gdbarch *gdbarch = objfile->arch ();
22171 struct symbol *sym = NULL;
22172 const char *name;
22173 struct attribute *attr = NULL;
22174 struct attribute *attr2 = NULL;
22175 CORE_ADDR baseaddr;
22176 struct pending **list_to_add = NULL;
22177
22178 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
22179
22180 baseaddr = objfile->text_section_offset ();
22181
22182 name = dwarf2_name (die, cu);
22183 if (name)
22184 {
22185 int suppress_add = 0;
22186
22187 if (space)
22188 sym = space;
22189 else
22190 sym = new (&objfile->objfile_obstack) symbol;
22191 OBJSTAT (objfile, n_syms++);
22192
22193 /* Cache this symbol's name and the name's demangled form (if any). */
22194 sym->set_language (cu->language, &objfile->objfile_obstack);
22195 /* Fortran does not have mangling standard and the mangling does differ
22196 between gfortran, iFort etc. */
22197 const char *physname
22198 = (cu->language == language_fortran
22199 ? dwarf2_full_name (name, die, cu)
22200 : dwarf2_physname (name, die, cu));
22201 const char *linkagename = dw2_linkage_name (die, cu);
22202
22203 if (linkagename == nullptr || cu->language == language_ada)
22204 sym->set_linkage_name (physname);
22205 else
22206 {
22207 sym->set_demangled_name (physname, &objfile->objfile_obstack);
22208 sym->set_linkage_name (linkagename);
22209 }
22210
22211 /* Default assumptions.
22212 Use the passed type or decode it from the die. */
22213 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22214 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22215 if (type != NULL)
22216 SYMBOL_TYPE (sym) = type;
22217 else
22218 SYMBOL_TYPE (sym) = die_type (die, cu);
22219 attr = dwarf2_attr (die,
22220 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
22221 cu);
22222 if (attr != nullptr)
22223 SYMBOL_LINE (sym) = attr->constant_value (0);
22224
22225 attr = dwarf2_attr (die,
22226 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
22227 cu);
22228 if (attr != nullptr && attr->is_nonnegative ())
22229 {
22230 file_name_index file_index
22231 = (file_name_index) attr->as_nonnegative ();
22232 struct file_entry *fe;
22233
22234 if (cu->line_header != NULL)
22235 fe = cu->line_header->file_name_at (file_index);
22236 else
22237 fe = NULL;
22238
22239 if (fe == NULL)
22240 complaint (_("file index out of range"));
22241 else
22242 symbol_set_symtab (sym, fe->symtab);
22243 }
22244
22245 switch (die->tag)
22246 {
22247 case DW_TAG_label:
22248 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
22249 if (attr != nullptr)
22250 {
22251 CORE_ADDR addr;
22252
22253 addr = attr->as_address ();
22254 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
22255 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
22256 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
22257 }
22258 else
22259 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22260 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
22261 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
22262 add_symbol_to_list (sym, cu->list_in_scope);
22263 break;
22264 case DW_TAG_subprogram:
22265 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22266 finish_block. */
22267 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
22268 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22269 if ((attr2 != nullptr && attr2->as_boolean ())
22270 || cu->language == language_ada
22271 || cu->language == language_fortran)
22272 {
22273 /* Subprograms marked external are stored as a global symbol.
22274 Ada and Fortran subprograms, whether marked external or
22275 not, are always stored as a global symbol, because we want
22276 to be able to access them globally. For instance, we want
22277 to be able to break on a nested subprogram without having
22278 to specify the context. */
22279 list_to_add = cu->get_builder ()->get_global_symbols ();
22280 }
22281 else
22282 {
22283 list_to_add = cu->list_in_scope;
22284 }
22285 break;
22286 case DW_TAG_inlined_subroutine:
22287 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22288 finish_block. */
22289 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
22290 SYMBOL_INLINED (sym) = 1;
22291 list_to_add = cu->list_in_scope;
22292 break;
22293 case DW_TAG_template_value_param:
22294 suppress_add = 1;
22295 /* Fall through. */
22296 case DW_TAG_constant:
22297 case DW_TAG_variable:
22298 case DW_TAG_member:
22299 /* Compilation with minimal debug info may result in
22300 variables with missing type entries. Change the
22301 misleading `void' type to something sensible. */
22302 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
22303 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
22304
22305 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22306 /* In the case of DW_TAG_member, we should only be called for
22307 static const members. */
22308 if (die->tag == DW_TAG_member)
22309 {
22310 /* dwarf2_add_field uses die_is_declaration,
22311 so we do the same. */
22312 gdb_assert (die_is_declaration (die, cu));
22313 gdb_assert (attr);
22314 }
22315 if (attr != nullptr)
22316 {
22317 dwarf2_const_value (attr, sym, cu);
22318 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22319 if (!suppress_add)
22320 {
22321 if (attr2 != nullptr && attr2->as_boolean ())
22322 list_to_add = cu->get_builder ()->get_global_symbols ();
22323 else
22324 list_to_add = cu->list_in_scope;
22325 }
22326 break;
22327 }
22328 attr = dwarf2_attr (die, DW_AT_location, cu);
22329 if (attr != nullptr)
22330 {
22331 var_decode_location (attr, sym, cu);
22332 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22333
22334 /* Fortran explicitly imports any global symbols to the local
22335 scope by DW_TAG_common_block. */
22336 if (cu->language == language_fortran && die->parent
22337 && die->parent->tag == DW_TAG_common_block)
22338 attr2 = NULL;
22339
22340 if (SYMBOL_CLASS (sym) == LOC_STATIC
22341 && SYMBOL_VALUE_ADDRESS (sym) == 0
22342 && !per_objfile->per_bfd->has_section_at_zero)
22343 {
22344 /* When a static variable is eliminated by the linker,
22345 the corresponding debug information is not stripped
22346 out, but the variable address is set to null;
22347 do not add such variables into symbol table. */
22348 }
22349 else if (attr2 != nullptr && attr2->as_boolean ())
22350 {
22351 if (SYMBOL_CLASS (sym) == LOC_STATIC
22352 && (objfile->flags & OBJF_MAINLINE) == 0
22353 && per_objfile->per_bfd->can_copy)
22354 {
22355 /* A global static variable might be subject to
22356 copy relocation. We first check for a local
22357 minsym, though, because maybe the symbol was
22358 marked hidden, in which case this would not
22359 apply. */
22360 bound_minimal_symbol found
22361 = (lookup_minimal_symbol_linkage
22362 (sym->linkage_name (), objfile));
22363 if (found.minsym != nullptr)
22364 sym->maybe_copied = 1;
22365 }
22366
22367 /* A variable with DW_AT_external is never static,
22368 but it may be block-scoped. */
22369 list_to_add
22370 = ((cu->list_in_scope
22371 == cu->get_builder ()->get_file_symbols ())
22372 ? cu->get_builder ()->get_global_symbols ()
22373 : cu->list_in_scope);
22374 }
22375 else
22376 list_to_add = cu->list_in_scope;
22377 }
22378 else
22379 {
22380 /* We do not know the address of this symbol.
22381 If it is an external symbol and we have type information
22382 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22383 The address of the variable will then be determined from
22384 the minimal symbol table whenever the variable is
22385 referenced. */
22386 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22387
22388 /* Fortran explicitly imports any global symbols to the local
22389 scope by DW_TAG_common_block. */
22390 if (cu->language == language_fortran && die->parent
22391 && die->parent->tag == DW_TAG_common_block)
22392 {
22393 /* SYMBOL_CLASS doesn't matter here because
22394 read_common_block is going to reset it. */
22395 if (!suppress_add)
22396 list_to_add = cu->list_in_scope;
22397 }
22398 else if (attr2 != nullptr && attr2->as_boolean ()
22399 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
22400 {
22401 /* A variable with DW_AT_external is never static, but it
22402 may be block-scoped. */
22403 list_to_add
22404 = ((cu->list_in_scope
22405 == cu->get_builder ()->get_file_symbols ())
22406 ? cu->get_builder ()->get_global_symbols ()
22407 : cu->list_in_scope);
22408
22409 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
22410 }
22411 else if (!die_is_declaration (die, cu))
22412 {
22413 /* Use the default LOC_OPTIMIZED_OUT class. */
22414 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
22415 if (!suppress_add)
22416 list_to_add = cu->list_in_scope;
22417 }
22418 }
22419 break;
22420 case DW_TAG_formal_parameter:
22421 {
22422 /* If we are inside a function, mark this as an argument. If
22423 not, we might be looking at an argument to an inlined function
22424 when we do not have enough information to show inlined frames;
22425 pretend it's a local variable in that case so that the user can
22426 still see it. */
22427 struct context_stack *curr
22428 = cu->get_builder ()->get_current_context_stack ();
22429 if (curr != nullptr && curr->name != nullptr)
22430 SYMBOL_IS_ARGUMENT (sym) = 1;
22431 attr = dwarf2_attr (die, DW_AT_location, cu);
22432 if (attr != nullptr)
22433 {
22434 var_decode_location (attr, sym, cu);
22435 }
22436 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22437 if (attr != nullptr)
22438 {
22439 dwarf2_const_value (attr, sym, cu);
22440 }
22441
22442 list_to_add = cu->list_in_scope;
22443 }
22444 break;
22445 case DW_TAG_unspecified_parameters:
22446 /* From varargs functions; gdb doesn't seem to have any
22447 interest in this information, so just ignore it for now.
22448 (FIXME?) */
22449 break;
22450 case DW_TAG_template_type_param:
22451 suppress_add = 1;
22452 /* Fall through. */
22453 case DW_TAG_class_type:
22454 case DW_TAG_interface_type:
22455 case DW_TAG_structure_type:
22456 case DW_TAG_union_type:
22457 case DW_TAG_set_type:
22458 case DW_TAG_enumeration_type:
22459 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22460 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
22461
22462 {
22463 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22464 really ever be static objects: otherwise, if you try
22465 to, say, break of a class's method and you're in a file
22466 which doesn't mention that class, it won't work unless
22467 the check for all static symbols in lookup_symbol_aux
22468 saves you. See the OtherFileClass tests in
22469 gdb.c++/namespace.exp. */
22470
22471 if (!suppress_add)
22472 {
22473 buildsym_compunit *builder = cu->get_builder ();
22474 list_to_add
22475 = (cu->list_in_scope == builder->get_file_symbols ()
22476 && cu->language == language_cplus
22477 ? builder->get_global_symbols ()
22478 : cu->list_in_scope);
22479
22480 /* The semantics of C++ state that "struct foo {
22481 ... }" also defines a typedef for "foo". */
22482 if (cu->language == language_cplus
22483 || cu->language == language_ada
22484 || cu->language == language_d
22485 || cu->language == language_rust)
22486 {
22487 /* The symbol's name is already allocated along
22488 with this objfile, so we don't need to
22489 duplicate it for the type. */
22490 if (SYMBOL_TYPE (sym)->name () == 0)
22491 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
22492 }
22493 }
22494 }
22495 break;
22496 case DW_TAG_typedef:
22497 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22498 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22499 list_to_add = cu->list_in_scope;
22500 break;
22501 case DW_TAG_array_type:
22502 case DW_TAG_base_type:
22503 case DW_TAG_subrange_type:
22504 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22505 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22506 list_to_add = cu->list_in_scope;
22507 break;
22508 case DW_TAG_enumerator:
22509 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22510 if (attr != nullptr)
22511 {
22512 dwarf2_const_value (attr, sym, cu);
22513 }
22514 {
22515 /* NOTE: carlton/2003-11-10: See comment above in the
22516 DW_TAG_class_type, etc. block. */
22517
22518 list_to_add
22519 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22520 && cu->language == language_cplus
22521 ? cu->get_builder ()->get_global_symbols ()
22522 : cu->list_in_scope);
22523 }
22524 break;
22525 case DW_TAG_imported_declaration:
22526 case DW_TAG_namespace:
22527 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22528 list_to_add = cu->get_builder ()->get_global_symbols ();
22529 break;
22530 case DW_TAG_module:
22531 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22532 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22533 list_to_add = cu->get_builder ()->get_global_symbols ();
22534 break;
22535 case DW_TAG_common_block:
22536 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22537 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22538 add_symbol_to_list (sym, cu->list_in_scope);
22539 break;
22540 default:
22541 /* Not a tag we recognize. Hopefully we aren't processing
22542 trash data, but since we must specifically ignore things
22543 we don't recognize, there is nothing else we should do at
22544 this point. */
22545 complaint (_("unsupported tag: '%s'"),
22546 dwarf_tag_name (die->tag));
22547 break;
22548 }
22549
22550 if (suppress_add)
22551 {
22552 sym->hash_next = objfile->template_symbols;
22553 objfile->template_symbols = sym;
22554 list_to_add = NULL;
22555 }
22556
22557 if (list_to_add != NULL)
22558 add_symbol_to_list (sym, list_to_add);
22559
22560 /* For the benefit of old versions of GCC, check for anonymous
22561 namespaces based on the demangled name. */
22562 if (!cu->processing_has_namespace_info
22563 && cu->language == language_cplus)
22564 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22565 }
22566 return (sym);
22567 }
22568
22569 /* Given an attr with a DW_FORM_dataN value in host byte order,
22570 zero-extend it as appropriate for the symbol's type. The DWARF
22571 standard (v4) is not entirely clear about the meaning of using
22572 DW_FORM_dataN for a constant with a signed type, where the type is
22573 wider than the data. The conclusion of a discussion on the DWARF
22574 list was that this is unspecified. We choose to always zero-extend
22575 because that is the interpretation long in use by GCC. */
22576
22577 static gdb_byte *
22578 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22579 struct dwarf2_cu *cu, LONGEST *value, int bits)
22580 {
22581 struct objfile *objfile = cu->per_objfile->objfile;
22582 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22583 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22584 LONGEST l = attr->constant_value (0);
22585
22586 if (bits < sizeof (*value) * 8)
22587 {
22588 l &= ((LONGEST) 1 << bits) - 1;
22589 *value = l;
22590 }
22591 else if (bits == sizeof (*value) * 8)
22592 *value = l;
22593 else
22594 {
22595 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22596 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22597 return bytes;
22598 }
22599
22600 return NULL;
22601 }
22602
22603 /* Read a constant value from an attribute. Either set *VALUE, or if
22604 the value does not fit in *VALUE, set *BYTES - either already
22605 allocated on the objfile obstack, or newly allocated on OBSTACK,
22606 or, set *BATON, if we translated the constant to a location
22607 expression. */
22608
22609 static void
22610 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22611 const char *name, struct obstack *obstack,
22612 struct dwarf2_cu *cu,
22613 LONGEST *value, const gdb_byte **bytes,
22614 struct dwarf2_locexpr_baton **baton)
22615 {
22616 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22617 struct objfile *objfile = per_objfile->objfile;
22618 struct comp_unit_head *cu_header = &cu->header;
22619 struct dwarf_block *blk;
22620 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22621 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22622
22623 *value = 0;
22624 *bytes = NULL;
22625 *baton = NULL;
22626
22627 switch (attr->form)
22628 {
22629 case DW_FORM_addr:
22630 case DW_FORM_addrx:
22631 case DW_FORM_GNU_addr_index:
22632 {
22633 gdb_byte *data;
22634
22635 if (TYPE_LENGTH (type) != cu_header->addr_size)
22636 dwarf2_const_value_length_mismatch_complaint (name,
22637 cu_header->addr_size,
22638 TYPE_LENGTH (type));
22639 /* Symbols of this form are reasonably rare, so we just
22640 piggyback on the existing location code rather than writing
22641 a new implementation of symbol_computed_ops. */
22642 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22643 (*baton)->per_objfile = per_objfile;
22644 (*baton)->per_cu = cu->per_cu;
22645 gdb_assert ((*baton)->per_cu);
22646
22647 (*baton)->size = 2 + cu_header->addr_size;
22648 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22649 (*baton)->data = data;
22650
22651 data[0] = DW_OP_addr;
22652 store_unsigned_integer (&data[1], cu_header->addr_size,
22653 byte_order, attr->as_address ());
22654 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22655 }
22656 break;
22657 case DW_FORM_string:
22658 case DW_FORM_strp:
22659 case DW_FORM_strx:
22660 case DW_FORM_GNU_str_index:
22661 case DW_FORM_GNU_strp_alt:
22662 /* The string is already allocated on the objfile obstack, point
22663 directly to it. */
22664 *bytes = (const gdb_byte *) attr->as_string ();
22665 break;
22666 case DW_FORM_block1:
22667 case DW_FORM_block2:
22668 case DW_FORM_block4:
22669 case DW_FORM_block:
22670 case DW_FORM_exprloc:
22671 case DW_FORM_data16:
22672 blk = attr->as_block ();
22673 if (TYPE_LENGTH (type) != blk->size)
22674 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22675 TYPE_LENGTH (type));
22676 *bytes = blk->data;
22677 break;
22678
22679 /* The DW_AT_const_value attributes are supposed to carry the
22680 symbol's value "represented as it would be on the target
22681 architecture." By the time we get here, it's already been
22682 converted to host endianness, so we just need to sign- or
22683 zero-extend it as appropriate. */
22684 case DW_FORM_data1:
22685 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22686 break;
22687 case DW_FORM_data2:
22688 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22689 break;
22690 case DW_FORM_data4:
22691 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22692 break;
22693 case DW_FORM_data8:
22694 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22695 break;
22696
22697 case DW_FORM_sdata:
22698 case DW_FORM_implicit_const:
22699 *value = attr->as_signed ();
22700 break;
22701
22702 case DW_FORM_udata:
22703 *value = attr->as_unsigned ();
22704 break;
22705
22706 default:
22707 complaint (_("unsupported const value attribute form: '%s'"),
22708 dwarf_form_name (attr->form));
22709 *value = 0;
22710 break;
22711 }
22712 }
22713
22714
22715 /* Copy constant value from an attribute to a symbol. */
22716
22717 static void
22718 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22719 struct dwarf2_cu *cu)
22720 {
22721 struct objfile *objfile = cu->per_objfile->objfile;
22722 LONGEST value;
22723 const gdb_byte *bytes;
22724 struct dwarf2_locexpr_baton *baton;
22725
22726 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22727 sym->print_name (),
22728 &objfile->objfile_obstack, cu,
22729 &value, &bytes, &baton);
22730
22731 if (baton != NULL)
22732 {
22733 SYMBOL_LOCATION_BATON (sym) = baton;
22734 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22735 }
22736 else if (bytes != NULL)
22737 {
22738 SYMBOL_VALUE_BYTES (sym) = bytes;
22739 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22740 }
22741 else
22742 {
22743 SYMBOL_VALUE (sym) = value;
22744 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22745 }
22746 }
22747
22748 /* Return the type of the die in question using its DW_AT_type attribute. */
22749
22750 static struct type *
22751 die_type (struct die_info *die, struct dwarf2_cu *cu)
22752 {
22753 struct attribute *type_attr;
22754
22755 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22756 if (!type_attr)
22757 {
22758 struct objfile *objfile = cu->per_objfile->objfile;
22759 /* A missing DW_AT_type represents a void type. */
22760 return objfile_type (objfile)->builtin_void;
22761 }
22762
22763 return lookup_die_type (die, type_attr, cu);
22764 }
22765
22766 /* True iff CU's producer generates GNAT Ada auxiliary information
22767 that allows to find parallel types through that information instead
22768 of having to do expensive parallel lookups by type name. */
22769
22770 static int
22771 need_gnat_info (struct dwarf2_cu *cu)
22772 {
22773 /* Assume that the Ada compiler was GNAT, which always produces
22774 the auxiliary information. */
22775 return (cu->language == language_ada);
22776 }
22777
22778 /* Return the auxiliary type of the die in question using its
22779 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22780 attribute is not present. */
22781
22782 static struct type *
22783 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22784 {
22785 struct attribute *type_attr;
22786
22787 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22788 if (!type_attr)
22789 return NULL;
22790
22791 return lookup_die_type (die, type_attr, cu);
22792 }
22793
22794 /* If DIE has a descriptive_type attribute, then set the TYPE's
22795 descriptive type accordingly. */
22796
22797 static void
22798 set_descriptive_type (struct type *type, struct die_info *die,
22799 struct dwarf2_cu *cu)
22800 {
22801 struct type *descriptive_type = die_descriptive_type (die, cu);
22802
22803 if (descriptive_type)
22804 {
22805 ALLOCATE_GNAT_AUX_TYPE (type);
22806 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22807 }
22808 }
22809
22810 /* Return the containing type of the die in question using its
22811 DW_AT_containing_type attribute. */
22812
22813 static struct type *
22814 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22815 {
22816 struct attribute *type_attr;
22817 struct objfile *objfile = cu->per_objfile->objfile;
22818
22819 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22820 if (!type_attr)
22821 error (_("Dwarf Error: Problem turning containing type into gdb type "
22822 "[in module %s]"), objfile_name (objfile));
22823
22824 return lookup_die_type (die, type_attr, cu);
22825 }
22826
22827 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22828
22829 static struct type *
22830 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22831 {
22832 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22833 struct objfile *objfile = per_objfile->objfile;
22834 char *saved;
22835
22836 std::string message
22837 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22838 objfile_name (objfile),
22839 sect_offset_str (cu->header.sect_off),
22840 sect_offset_str (die->sect_off));
22841 saved = obstack_strdup (&objfile->objfile_obstack, message);
22842
22843 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22844 }
22845
22846 /* Look up the type of DIE in CU using its type attribute ATTR.
22847 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22848 DW_AT_containing_type.
22849 If there is no type substitute an error marker. */
22850
22851 static struct type *
22852 lookup_die_type (struct die_info *die, const struct attribute *attr,
22853 struct dwarf2_cu *cu)
22854 {
22855 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22856 struct objfile *objfile = per_objfile->objfile;
22857 struct type *this_type;
22858
22859 gdb_assert (attr->name == DW_AT_type
22860 || attr->name == DW_AT_GNAT_descriptive_type
22861 || attr->name == DW_AT_containing_type);
22862
22863 /* First see if we have it cached. */
22864
22865 if (attr->form == DW_FORM_GNU_ref_alt)
22866 {
22867 struct dwarf2_per_cu_data *per_cu;
22868 sect_offset sect_off = attr->get_ref_die_offset ();
22869
22870 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22871 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
22872 }
22873 else if (attr->form_is_ref ())
22874 {
22875 sect_offset sect_off = attr->get_ref_die_offset ();
22876
22877 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
22878 }
22879 else if (attr->form == DW_FORM_ref_sig8)
22880 {
22881 ULONGEST signature = attr->as_signature ();
22882
22883 return get_signatured_type (die, signature, cu);
22884 }
22885 else
22886 {
22887 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22888 " at %s [in module %s]"),
22889 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22890 objfile_name (objfile));
22891 return build_error_marker_type (cu, die);
22892 }
22893
22894 /* If not cached we need to read it in. */
22895
22896 if (this_type == NULL)
22897 {
22898 struct die_info *type_die = NULL;
22899 struct dwarf2_cu *type_cu = cu;
22900
22901 if (attr->form_is_ref ())
22902 type_die = follow_die_ref (die, attr, &type_cu);
22903 if (type_die == NULL)
22904 return build_error_marker_type (cu, die);
22905 /* If we find the type now, it's probably because the type came
22906 from an inter-CU reference and the type's CU got expanded before
22907 ours. */
22908 this_type = read_type_die (type_die, type_cu);
22909 }
22910
22911 /* If we still don't have a type use an error marker. */
22912
22913 if (this_type == NULL)
22914 return build_error_marker_type (cu, die);
22915
22916 return this_type;
22917 }
22918
22919 /* Return the type in DIE, CU.
22920 Returns NULL for invalid types.
22921
22922 This first does a lookup in die_type_hash,
22923 and only reads the die in if necessary.
22924
22925 NOTE: This can be called when reading in partial or full symbols. */
22926
22927 static struct type *
22928 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22929 {
22930 struct type *this_type;
22931
22932 this_type = get_die_type (die, cu);
22933 if (this_type)
22934 return this_type;
22935
22936 return read_type_die_1 (die, cu);
22937 }
22938
22939 /* Read the type in DIE, CU.
22940 Returns NULL for invalid types. */
22941
22942 static struct type *
22943 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22944 {
22945 struct type *this_type = NULL;
22946
22947 switch (die->tag)
22948 {
22949 case DW_TAG_class_type:
22950 case DW_TAG_interface_type:
22951 case DW_TAG_structure_type:
22952 case DW_TAG_union_type:
22953 this_type = read_structure_type (die, cu);
22954 break;
22955 case DW_TAG_enumeration_type:
22956 this_type = read_enumeration_type (die, cu);
22957 break;
22958 case DW_TAG_subprogram:
22959 case DW_TAG_subroutine_type:
22960 case DW_TAG_inlined_subroutine:
22961 this_type = read_subroutine_type (die, cu);
22962 break;
22963 case DW_TAG_array_type:
22964 this_type = read_array_type (die, cu);
22965 break;
22966 case DW_TAG_set_type:
22967 this_type = read_set_type (die, cu);
22968 break;
22969 case DW_TAG_pointer_type:
22970 this_type = read_tag_pointer_type (die, cu);
22971 break;
22972 case DW_TAG_ptr_to_member_type:
22973 this_type = read_tag_ptr_to_member_type (die, cu);
22974 break;
22975 case DW_TAG_reference_type:
22976 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22977 break;
22978 case DW_TAG_rvalue_reference_type:
22979 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22980 break;
22981 case DW_TAG_const_type:
22982 this_type = read_tag_const_type (die, cu);
22983 break;
22984 case DW_TAG_volatile_type:
22985 this_type = read_tag_volatile_type (die, cu);
22986 break;
22987 case DW_TAG_restrict_type:
22988 this_type = read_tag_restrict_type (die, cu);
22989 break;
22990 case DW_TAG_string_type:
22991 this_type = read_tag_string_type (die, cu);
22992 break;
22993 case DW_TAG_typedef:
22994 this_type = read_typedef (die, cu);
22995 break;
22996 case DW_TAG_subrange_type:
22997 this_type = read_subrange_type (die, cu);
22998 break;
22999 case DW_TAG_base_type:
23000 this_type = read_base_type (die, cu);
23001 break;
23002 case DW_TAG_unspecified_type:
23003 this_type = read_unspecified_type (die, cu);
23004 break;
23005 case DW_TAG_namespace:
23006 this_type = read_namespace_type (die, cu);
23007 break;
23008 case DW_TAG_module:
23009 this_type = read_module_type (die, cu);
23010 break;
23011 case DW_TAG_atomic_type:
23012 this_type = read_tag_atomic_type (die, cu);
23013 break;
23014 default:
23015 complaint (_("unexpected tag in read_type_die: '%s'"),
23016 dwarf_tag_name (die->tag));
23017 break;
23018 }
23019
23020 return this_type;
23021 }
23022
23023 /* See if we can figure out if the class lives in a namespace. We do
23024 this by looking for a member function; its demangled name will
23025 contain namespace info, if there is any.
23026 Return the computed name or NULL.
23027 Space for the result is allocated on the objfile's obstack.
23028 This is the full-die version of guess_partial_die_structure_name.
23029 In this case we know DIE has no useful parent. */
23030
23031 static const char *
23032 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
23033 {
23034 struct die_info *spec_die;
23035 struct dwarf2_cu *spec_cu;
23036 struct die_info *child;
23037 struct objfile *objfile = cu->per_objfile->objfile;
23038
23039 spec_cu = cu;
23040 spec_die = die_specification (die, &spec_cu);
23041 if (spec_die != NULL)
23042 {
23043 die = spec_die;
23044 cu = spec_cu;
23045 }
23046
23047 for (child = die->child;
23048 child != NULL;
23049 child = child->sibling)
23050 {
23051 if (child->tag == DW_TAG_subprogram)
23052 {
23053 const char *linkage_name = dw2_linkage_name (child, cu);
23054
23055 if (linkage_name != NULL)
23056 {
23057 gdb::unique_xmalloc_ptr<char> actual_name
23058 (cu->language_defn->class_name_from_physname (linkage_name));
23059 const char *name = NULL;
23060
23061 if (actual_name != NULL)
23062 {
23063 const char *die_name = dwarf2_name (die, cu);
23064
23065 if (die_name != NULL
23066 && strcmp (die_name, actual_name.get ()) != 0)
23067 {
23068 /* Strip off the class name from the full name.
23069 We want the prefix. */
23070 int die_name_len = strlen (die_name);
23071 int actual_name_len = strlen (actual_name.get ());
23072 const char *ptr = actual_name.get ();
23073
23074 /* Test for '::' as a sanity check. */
23075 if (actual_name_len > die_name_len + 2
23076 && ptr[actual_name_len - die_name_len - 1] == ':')
23077 name = obstack_strndup (
23078 &objfile->per_bfd->storage_obstack,
23079 ptr, actual_name_len - die_name_len - 2);
23080 }
23081 }
23082 return name;
23083 }
23084 }
23085 }
23086
23087 return NULL;
23088 }
23089
23090 /* GCC might emit a nameless typedef that has a linkage name. Determine the
23091 prefix part in such case. See
23092 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23093
23094 static const char *
23095 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
23096 {
23097 struct attribute *attr;
23098 const char *base;
23099
23100 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
23101 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
23102 return NULL;
23103
23104 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
23105 return NULL;
23106
23107 attr = dw2_linkage_name_attr (die, cu);
23108 const char *attr_name = attr->as_string ();
23109 if (attr == NULL || attr_name == NULL)
23110 return NULL;
23111
23112 /* dwarf2_name had to be already called. */
23113 gdb_assert (attr->canonical_string_p ());
23114
23115 /* Strip the base name, keep any leading namespaces/classes. */
23116 base = strrchr (attr_name, ':');
23117 if (base == NULL || base == attr_name || base[-1] != ':')
23118 return "";
23119
23120 struct objfile *objfile = cu->per_objfile->objfile;
23121 return obstack_strndup (&objfile->per_bfd->storage_obstack,
23122 attr_name,
23123 &base[-1] - attr_name);
23124 }
23125
23126 /* Return the name of the namespace/class that DIE is defined within,
23127 or "" if we can't tell. The caller should not xfree the result.
23128
23129 For example, if we're within the method foo() in the following
23130 code:
23131
23132 namespace N {
23133 class C {
23134 void foo () {
23135 }
23136 };
23137 }
23138
23139 then determine_prefix on foo's die will return "N::C". */
23140
23141 static const char *
23142 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
23143 {
23144 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23145 struct die_info *parent, *spec_die;
23146 struct dwarf2_cu *spec_cu;
23147 struct type *parent_type;
23148 const char *retval;
23149
23150 if (cu->language != language_cplus
23151 && cu->language != language_fortran && cu->language != language_d
23152 && cu->language != language_rust)
23153 return "";
23154
23155 retval = anonymous_struct_prefix (die, cu);
23156 if (retval)
23157 return retval;
23158
23159 /* We have to be careful in the presence of DW_AT_specification.
23160 For example, with GCC 3.4, given the code
23161
23162 namespace N {
23163 void foo() {
23164 // Definition of N::foo.
23165 }
23166 }
23167
23168 then we'll have a tree of DIEs like this:
23169
23170 1: DW_TAG_compile_unit
23171 2: DW_TAG_namespace // N
23172 3: DW_TAG_subprogram // declaration of N::foo
23173 4: DW_TAG_subprogram // definition of N::foo
23174 DW_AT_specification // refers to die #3
23175
23176 Thus, when processing die #4, we have to pretend that we're in
23177 the context of its DW_AT_specification, namely the contex of die
23178 #3. */
23179 spec_cu = cu;
23180 spec_die = die_specification (die, &spec_cu);
23181 if (spec_die == NULL)
23182 parent = die->parent;
23183 else
23184 {
23185 parent = spec_die->parent;
23186 cu = spec_cu;
23187 }
23188
23189 if (parent == NULL)
23190 return "";
23191 else if (parent->building_fullname)
23192 {
23193 const char *name;
23194 const char *parent_name;
23195
23196 /* It has been seen on RealView 2.2 built binaries,
23197 DW_TAG_template_type_param types actually _defined_ as
23198 children of the parent class:
23199
23200 enum E {};
23201 template class <class Enum> Class{};
23202 Class<enum E> class_e;
23203
23204 1: DW_TAG_class_type (Class)
23205 2: DW_TAG_enumeration_type (E)
23206 3: DW_TAG_enumerator (enum1:0)
23207 3: DW_TAG_enumerator (enum2:1)
23208 ...
23209 2: DW_TAG_template_type_param
23210 DW_AT_type DW_FORM_ref_udata (E)
23211
23212 Besides being broken debug info, it can put GDB into an
23213 infinite loop. Consider:
23214
23215 When we're building the full name for Class<E>, we'll start
23216 at Class, and go look over its template type parameters,
23217 finding E. We'll then try to build the full name of E, and
23218 reach here. We're now trying to build the full name of E,
23219 and look over the parent DIE for containing scope. In the
23220 broken case, if we followed the parent DIE of E, we'd again
23221 find Class, and once again go look at its template type
23222 arguments, etc., etc. Simply don't consider such parent die
23223 as source-level parent of this die (it can't be, the language
23224 doesn't allow it), and break the loop here. */
23225 name = dwarf2_name (die, cu);
23226 parent_name = dwarf2_name (parent, cu);
23227 complaint (_("template param type '%s' defined within parent '%s'"),
23228 name ? name : "<unknown>",
23229 parent_name ? parent_name : "<unknown>");
23230 return "";
23231 }
23232 else
23233 switch (parent->tag)
23234 {
23235 case DW_TAG_namespace:
23236 parent_type = read_type_die (parent, cu);
23237 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
23238 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
23239 Work around this problem here. */
23240 if (cu->language == language_cplus
23241 && strcmp (parent_type->name (), "::") == 0)
23242 return "";
23243 /* We give a name to even anonymous namespaces. */
23244 return parent_type->name ();
23245 case DW_TAG_class_type:
23246 case DW_TAG_interface_type:
23247 case DW_TAG_structure_type:
23248 case DW_TAG_union_type:
23249 case DW_TAG_module:
23250 parent_type = read_type_die (parent, cu);
23251 if (parent_type->name () != NULL)
23252 return parent_type->name ();
23253 else
23254 /* An anonymous structure is only allowed non-static data
23255 members; no typedefs, no member functions, et cetera.
23256 So it does not need a prefix. */
23257 return "";
23258 case DW_TAG_compile_unit:
23259 case DW_TAG_partial_unit:
23260 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
23261 if (cu->language == language_cplus
23262 && !per_objfile->per_bfd->types.empty ()
23263 && die->child != NULL
23264 && (die->tag == DW_TAG_class_type
23265 || die->tag == DW_TAG_structure_type
23266 || die->tag == DW_TAG_union_type))
23267 {
23268 const char *name = guess_full_die_structure_name (die, cu);
23269 if (name != NULL)
23270 return name;
23271 }
23272 return "";
23273 case DW_TAG_subprogram:
23274 /* Nested subroutines in Fortran get a prefix with the name
23275 of the parent's subroutine. */
23276 if (cu->language == language_fortran)
23277 {
23278 if ((die->tag == DW_TAG_subprogram)
23279 && (dwarf2_name (parent, cu) != NULL))
23280 return dwarf2_name (parent, cu);
23281 }
23282 return determine_prefix (parent, cu);
23283 case DW_TAG_enumeration_type:
23284 parent_type = read_type_die (parent, cu);
23285 if (TYPE_DECLARED_CLASS (parent_type))
23286 {
23287 if (parent_type->name () != NULL)
23288 return parent_type->name ();
23289 return "";
23290 }
23291 /* Fall through. */
23292 default:
23293 return determine_prefix (parent, cu);
23294 }
23295 }
23296
23297 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
23298 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
23299 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
23300 an obconcat, otherwise allocate storage for the result. The CU argument is
23301 used to determine the language and hence, the appropriate separator. */
23302
23303 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
23304
23305 static char *
23306 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
23307 int physname, struct dwarf2_cu *cu)
23308 {
23309 const char *lead = "";
23310 const char *sep;
23311
23312 if (suffix == NULL || suffix[0] == '\0'
23313 || prefix == NULL || prefix[0] == '\0')
23314 sep = "";
23315 else if (cu->language == language_d)
23316 {
23317 /* For D, the 'main' function could be defined in any module, but it
23318 should never be prefixed. */
23319 if (strcmp (suffix, "D main") == 0)
23320 {
23321 prefix = "";
23322 sep = "";
23323 }
23324 else
23325 sep = ".";
23326 }
23327 else if (cu->language == language_fortran && physname)
23328 {
23329 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
23330 DW_AT_MIPS_linkage_name is preferred and used instead. */
23331
23332 lead = "__";
23333 sep = "_MOD_";
23334 }
23335 else
23336 sep = "::";
23337
23338 if (prefix == NULL)
23339 prefix = "";
23340 if (suffix == NULL)
23341 suffix = "";
23342
23343 if (obs == NULL)
23344 {
23345 char *retval
23346 = ((char *)
23347 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
23348
23349 strcpy (retval, lead);
23350 strcat (retval, prefix);
23351 strcat (retval, sep);
23352 strcat (retval, suffix);
23353 return retval;
23354 }
23355 else
23356 {
23357 /* We have an obstack. */
23358 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
23359 }
23360 }
23361
23362 /* Get name of a die, return NULL if not found. */
23363
23364 static const char *
23365 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
23366 struct objfile *objfile)
23367 {
23368 if (name && cu->language == language_cplus)
23369 {
23370 gdb::unique_xmalloc_ptr<char> canon_name
23371 = cp_canonicalize_string (name);
23372
23373 if (canon_name != nullptr)
23374 name = objfile->intern (canon_name.get ());
23375 }
23376
23377 return name;
23378 }
23379
23380 /* Get name of a die, return NULL if not found.
23381 Anonymous namespaces are converted to their magic string. */
23382
23383 static const char *
23384 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
23385 {
23386 struct attribute *attr;
23387 struct objfile *objfile = cu->per_objfile->objfile;
23388
23389 attr = dwarf2_attr (die, DW_AT_name, cu);
23390 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
23391 if (attr_name == nullptr
23392 && die->tag != DW_TAG_namespace
23393 && die->tag != DW_TAG_class_type
23394 && die->tag != DW_TAG_interface_type
23395 && die->tag != DW_TAG_structure_type
23396 && die->tag != DW_TAG_union_type)
23397 return NULL;
23398
23399 switch (die->tag)
23400 {
23401 case DW_TAG_compile_unit:
23402 case DW_TAG_partial_unit:
23403 /* Compilation units have a DW_AT_name that is a filename, not
23404 a source language identifier. */
23405 case DW_TAG_enumeration_type:
23406 case DW_TAG_enumerator:
23407 /* These tags always have simple identifiers already; no need
23408 to canonicalize them. */
23409 return attr_name;
23410
23411 case DW_TAG_namespace:
23412 if (attr_name != nullptr)
23413 return attr_name;
23414 return CP_ANONYMOUS_NAMESPACE_STR;
23415
23416 case DW_TAG_class_type:
23417 case DW_TAG_interface_type:
23418 case DW_TAG_structure_type:
23419 case DW_TAG_union_type:
23420 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23421 structures or unions. These were of the form "._%d" in GCC 4.1,
23422 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23423 and GCC 4.4. We work around this problem by ignoring these. */
23424 if (attr_name != nullptr
23425 && (startswith (attr_name, "._")
23426 || startswith (attr_name, "<anonymous")))
23427 return NULL;
23428
23429 /* GCC might emit a nameless typedef that has a linkage name. See
23430 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23431 if (!attr || attr_name == NULL)
23432 {
23433 attr = dw2_linkage_name_attr (die, cu);
23434 attr_name = attr == nullptr ? nullptr : attr->as_string ();
23435 if (attr == NULL || attr_name == NULL)
23436 return NULL;
23437
23438 /* Avoid demangling attr_name the second time on a second
23439 call for the same DIE. */
23440 if (!attr->canonical_string_p ())
23441 {
23442 gdb::unique_xmalloc_ptr<char> demangled
23443 (gdb_demangle (attr_name, DMGL_TYPES));
23444 if (demangled == nullptr)
23445 return nullptr;
23446
23447 attr->set_string_canonical (objfile->intern (demangled.get ()));
23448 attr_name = attr->as_string ();
23449 }
23450
23451 /* Strip any leading namespaces/classes, keep only the
23452 base name. DW_AT_name for named DIEs does not
23453 contain the prefixes. */
23454 const char *base = strrchr (attr_name, ':');
23455 if (base && base > attr_name && base[-1] == ':')
23456 return &base[1];
23457 else
23458 return attr_name;
23459 }
23460 break;
23461
23462 default:
23463 break;
23464 }
23465
23466 if (!attr->canonical_string_p ())
23467 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23468 objfile));
23469 return attr->as_string ();
23470 }
23471
23472 /* Return the die that this die in an extension of, or NULL if there
23473 is none. *EXT_CU is the CU containing DIE on input, and the CU
23474 containing the return value on output. */
23475
23476 static struct die_info *
23477 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23478 {
23479 struct attribute *attr;
23480
23481 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23482 if (attr == NULL)
23483 return NULL;
23484
23485 return follow_die_ref (die, attr, ext_cu);
23486 }
23487
23488 static void
23489 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23490 {
23491 unsigned int i;
23492
23493 print_spaces (indent, f);
23494 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23495 dwarf_tag_name (die->tag), die->abbrev,
23496 sect_offset_str (die->sect_off));
23497
23498 if (die->parent != NULL)
23499 {
23500 print_spaces (indent, f);
23501 fprintf_unfiltered (f, " parent at offset: %s\n",
23502 sect_offset_str (die->parent->sect_off));
23503 }
23504
23505 print_spaces (indent, f);
23506 fprintf_unfiltered (f, " has children: %s\n",
23507 dwarf_bool_name (die->child != NULL));
23508
23509 print_spaces (indent, f);
23510 fprintf_unfiltered (f, " attributes:\n");
23511
23512 for (i = 0; i < die->num_attrs; ++i)
23513 {
23514 print_spaces (indent, f);
23515 fprintf_unfiltered (f, " %s (%s) ",
23516 dwarf_attr_name (die->attrs[i].name),
23517 dwarf_form_name (die->attrs[i].form));
23518
23519 switch (die->attrs[i].form)
23520 {
23521 case DW_FORM_addr:
23522 case DW_FORM_addrx:
23523 case DW_FORM_GNU_addr_index:
23524 fprintf_unfiltered (f, "address: ");
23525 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
23526 break;
23527 case DW_FORM_block2:
23528 case DW_FORM_block4:
23529 case DW_FORM_block:
23530 case DW_FORM_block1:
23531 fprintf_unfiltered (f, "block: size %s",
23532 pulongest (die->attrs[i].as_block ()->size));
23533 break;
23534 case DW_FORM_exprloc:
23535 fprintf_unfiltered (f, "expression: size %s",
23536 pulongest (die->attrs[i].as_block ()->size));
23537 break;
23538 case DW_FORM_data16:
23539 fprintf_unfiltered (f, "constant of 16 bytes");
23540 break;
23541 case DW_FORM_ref_addr:
23542 fprintf_unfiltered (f, "ref address: ");
23543 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23544 break;
23545 case DW_FORM_GNU_ref_alt:
23546 fprintf_unfiltered (f, "alt ref address: ");
23547 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23548 break;
23549 case DW_FORM_ref1:
23550 case DW_FORM_ref2:
23551 case DW_FORM_ref4:
23552 case DW_FORM_ref8:
23553 case DW_FORM_ref_udata:
23554 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23555 (long) (die->attrs[i].as_unsigned ()));
23556 break;
23557 case DW_FORM_data1:
23558 case DW_FORM_data2:
23559 case DW_FORM_data4:
23560 case DW_FORM_data8:
23561 case DW_FORM_udata:
23562 fprintf_unfiltered (f, "constant: %s",
23563 pulongest (die->attrs[i].as_unsigned ()));
23564 break;
23565 case DW_FORM_sec_offset:
23566 fprintf_unfiltered (f, "section offset: %s",
23567 pulongest (die->attrs[i].as_unsigned ()));
23568 break;
23569 case DW_FORM_ref_sig8:
23570 fprintf_unfiltered (f, "signature: %s",
23571 hex_string (die->attrs[i].as_signature ()));
23572 break;
23573 case DW_FORM_string:
23574 case DW_FORM_strp:
23575 case DW_FORM_line_strp:
23576 case DW_FORM_strx:
23577 case DW_FORM_GNU_str_index:
23578 case DW_FORM_GNU_strp_alt:
23579 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23580 die->attrs[i].as_string ()
23581 ? die->attrs[i].as_string () : "",
23582 die->attrs[i].canonical_string_p () ? "is" : "not");
23583 break;
23584 case DW_FORM_flag:
23585 if (die->attrs[i].as_boolean ())
23586 fprintf_unfiltered (f, "flag: TRUE");
23587 else
23588 fprintf_unfiltered (f, "flag: FALSE");
23589 break;
23590 case DW_FORM_flag_present:
23591 fprintf_unfiltered (f, "flag: TRUE");
23592 break;
23593 case DW_FORM_indirect:
23594 /* The reader will have reduced the indirect form to
23595 the "base form" so this form should not occur. */
23596 fprintf_unfiltered (f,
23597 "unexpected attribute form: DW_FORM_indirect");
23598 break;
23599 case DW_FORM_sdata:
23600 case DW_FORM_implicit_const:
23601 fprintf_unfiltered (f, "constant: %s",
23602 plongest (die->attrs[i].as_signed ()));
23603 break;
23604 default:
23605 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23606 die->attrs[i].form);
23607 break;
23608 }
23609 fprintf_unfiltered (f, "\n");
23610 }
23611 }
23612
23613 static void
23614 dump_die_for_error (struct die_info *die)
23615 {
23616 dump_die_shallow (gdb_stderr, 0, die);
23617 }
23618
23619 static void
23620 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23621 {
23622 int indent = level * 4;
23623
23624 gdb_assert (die != NULL);
23625
23626 if (level >= max_level)
23627 return;
23628
23629 dump_die_shallow (f, indent, die);
23630
23631 if (die->child != NULL)
23632 {
23633 print_spaces (indent, f);
23634 fprintf_unfiltered (f, " Children:");
23635 if (level + 1 < max_level)
23636 {
23637 fprintf_unfiltered (f, "\n");
23638 dump_die_1 (f, level + 1, max_level, die->child);
23639 }
23640 else
23641 {
23642 fprintf_unfiltered (f,
23643 " [not printed, max nesting level reached]\n");
23644 }
23645 }
23646
23647 if (die->sibling != NULL && level > 0)
23648 {
23649 dump_die_1 (f, level, max_level, die->sibling);
23650 }
23651 }
23652
23653 /* This is called from the pdie macro in gdbinit.in.
23654 It's not static so gcc will keep a copy callable from gdb. */
23655
23656 void
23657 dump_die (struct die_info *die, int max_level)
23658 {
23659 dump_die_1 (gdb_stdlog, 0, max_level, die);
23660 }
23661
23662 static void
23663 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23664 {
23665 void **slot;
23666
23667 slot = htab_find_slot_with_hash (cu->die_hash, die,
23668 to_underlying (die->sect_off),
23669 INSERT);
23670
23671 *slot = die;
23672 }
23673
23674 /* Follow reference or signature attribute ATTR of SRC_DIE.
23675 On entry *REF_CU is the CU of SRC_DIE.
23676 On exit *REF_CU is the CU of the result. */
23677
23678 static struct die_info *
23679 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23680 struct dwarf2_cu **ref_cu)
23681 {
23682 struct die_info *die;
23683
23684 if (attr->form_is_ref ())
23685 die = follow_die_ref (src_die, attr, ref_cu);
23686 else if (attr->form == DW_FORM_ref_sig8)
23687 die = follow_die_sig (src_die, attr, ref_cu);
23688 else
23689 {
23690 dump_die_for_error (src_die);
23691 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23692 objfile_name ((*ref_cu)->per_objfile->objfile));
23693 }
23694
23695 return die;
23696 }
23697
23698 /* Follow reference OFFSET.
23699 On entry *REF_CU is the CU of the source die referencing OFFSET.
23700 On exit *REF_CU is the CU of the result.
23701 Returns NULL if OFFSET is invalid. */
23702
23703 static struct die_info *
23704 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23705 struct dwarf2_cu **ref_cu)
23706 {
23707 struct die_info temp_die;
23708 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23709 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23710
23711 gdb_assert (cu->per_cu != NULL);
23712
23713 target_cu = cu;
23714
23715 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23716 "source CU contains target offset: %d",
23717 sect_offset_str (cu->per_cu->sect_off),
23718 sect_offset_str (sect_off),
23719 cu->header.offset_in_cu_p (sect_off));
23720
23721 if (cu->per_cu->is_debug_types)
23722 {
23723 /* .debug_types CUs cannot reference anything outside their CU.
23724 If they need to, they have to reference a signatured type via
23725 DW_FORM_ref_sig8. */
23726 if (!cu->header.offset_in_cu_p (sect_off))
23727 return NULL;
23728 }
23729 else if (offset_in_dwz != cu->per_cu->is_dwz
23730 || !cu->header.offset_in_cu_p (sect_off))
23731 {
23732 struct dwarf2_per_cu_data *per_cu;
23733
23734 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23735 per_objfile);
23736
23737 dwarf_read_debug_printf_v ("target CU offset: %s, "
23738 "target CU DIEs loaded: %d",
23739 sect_offset_str (per_cu->sect_off),
23740 per_objfile->get_cu (per_cu) != nullptr);
23741
23742 /* If necessary, add it to the queue and load its DIEs.
23743
23744 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23745 it doesn't mean they are currently loaded. Since we require them
23746 to be loaded, we must check for ourselves. */
23747 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language)
23748 || per_objfile->get_cu (per_cu) == nullptr)
23749 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23750 false, cu->language);
23751
23752 target_cu = per_objfile->get_cu (per_cu);
23753 gdb_assert (target_cu != nullptr);
23754 }
23755 else if (cu->dies == NULL)
23756 {
23757 /* We're loading full DIEs during partial symbol reading. */
23758 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
23759 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23760 language_minimal);
23761 }
23762
23763 *ref_cu = target_cu;
23764 temp_die.sect_off = sect_off;
23765
23766 if (target_cu != cu)
23767 target_cu->ancestor = cu;
23768
23769 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23770 &temp_die,
23771 to_underlying (sect_off));
23772 }
23773
23774 /* Follow reference attribute ATTR of SRC_DIE.
23775 On entry *REF_CU is the CU of SRC_DIE.
23776 On exit *REF_CU is the CU of the result. */
23777
23778 static struct die_info *
23779 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23780 struct dwarf2_cu **ref_cu)
23781 {
23782 sect_offset sect_off = attr->get_ref_die_offset ();
23783 struct dwarf2_cu *cu = *ref_cu;
23784 struct die_info *die;
23785
23786 die = follow_die_offset (sect_off,
23787 (attr->form == DW_FORM_GNU_ref_alt
23788 || cu->per_cu->is_dwz),
23789 ref_cu);
23790 if (!die)
23791 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23792 "at %s [in module %s]"),
23793 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23794 objfile_name (cu->per_objfile->objfile));
23795
23796 return die;
23797 }
23798
23799 /* See read.h. */
23800
23801 struct dwarf2_locexpr_baton
23802 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23803 dwarf2_per_cu_data *per_cu,
23804 dwarf2_per_objfile *per_objfile,
23805 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23806 bool resolve_abstract_p)
23807 {
23808 struct die_info *die;
23809 struct attribute *attr;
23810 struct dwarf2_locexpr_baton retval;
23811 struct objfile *objfile = per_objfile->objfile;
23812
23813 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23814 if (cu == nullptr)
23815 cu = load_cu (per_cu, per_objfile, false);
23816
23817 if (cu == nullptr)
23818 {
23819 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23820 Instead just throw an error, not much else we can do. */
23821 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23822 sect_offset_str (sect_off), objfile_name (objfile));
23823 }
23824
23825 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23826 if (!die)
23827 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23828 sect_offset_str (sect_off), objfile_name (objfile));
23829
23830 attr = dwarf2_attr (die, DW_AT_location, cu);
23831 if (!attr && resolve_abstract_p
23832 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23833 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23834 {
23835 CORE_ADDR pc = get_frame_pc ();
23836 CORE_ADDR baseaddr = objfile->text_section_offset ();
23837 struct gdbarch *gdbarch = objfile->arch ();
23838
23839 for (const auto &cand_off
23840 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23841 {
23842 struct dwarf2_cu *cand_cu = cu;
23843 struct die_info *cand
23844 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23845 if (!cand
23846 || !cand->parent
23847 || cand->parent->tag != DW_TAG_subprogram)
23848 continue;
23849
23850 CORE_ADDR pc_low, pc_high;
23851 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23852 if (pc_low == ((CORE_ADDR) -1))
23853 continue;
23854 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23855 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23856 if (!(pc_low <= pc && pc < pc_high))
23857 continue;
23858
23859 die = cand;
23860 attr = dwarf2_attr (die, DW_AT_location, cu);
23861 break;
23862 }
23863 }
23864
23865 if (!attr)
23866 {
23867 /* DWARF: "If there is no such attribute, then there is no effect.".
23868 DATA is ignored if SIZE is 0. */
23869
23870 retval.data = NULL;
23871 retval.size = 0;
23872 }
23873 else if (attr->form_is_section_offset ())
23874 {
23875 struct dwarf2_loclist_baton loclist_baton;
23876 CORE_ADDR pc = get_frame_pc ();
23877 size_t size;
23878
23879 fill_in_loclist_baton (cu, &loclist_baton, attr);
23880
23881 retval.data = dwarf2_find_location_expression (&loclist_baton,
23882 &size, pc);
23883 retval.size = size;
23884 }
23885 else
23886 {
23887 if (!attr->form_is_block ())
23888 error (_("Dwarf Error: DIE at %s referenced in module %s "
23889 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23890 sect_offset_str (sect_off), objfile_name (objfile));
23891
23892 struct dwarf_block *block = attr->as_block ();
23893 retval.data = block->data;
23894 retval.size = block->size;
23895 }
23896 retval.per_objfile = per_objfile;
23897 retval.per_cu = cu->per_cu;
23898
23899 per_objfile->age_comp_units ();
23900
23901 return retval;
23902 }
23903
23904 /* See read.h. */
23905
23906 struct dwarf2_locexpr_baton
23907 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23908 dwarf2_per_cu_data *per_cu,
23909 dwarf2_per_objfile *per_objfile,
23910 gdb::function_view<CORE_ADDR ()> get_frame_pc)
23911 {
23912 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23913
23914 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
23915 get_frame_pc);
23916 }
23917
23918 /* Write a constant of a given type as target-ordered bytes into
23919 OBSTACK. */
23920
23921 static const gdb_byte *
23922 write_constant_as_bytes (struct obstack *obstack,
23923 enum bfd_endian byte_order,
23924 struct type *type,
23925 ULONGEST value,
23926 LONGEST *len)
23927 {
23928 gdb_byte *result;
23929
23930 *len = TYPE_LENGTH (type);
23931 result = (gdb_byte *) obstack_alloc (obstack, *len);
23932 store_unsigned_integer (result, *len, byte_order, value);
23933
23934 return result;
23935 }
23936
23937 /* See read.h. */
23938
23939 const gdb_byte *
23940 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23941 dwarf2_per_cu_data *per_cu,
23942 dwarf2_per_objfile *per_objfile,
23943 obstack *obstack,
23944 LONGEST *len)
23945 {
23946 struct die_info *die;
23947 struct attribute *attr;
23948 const gdb_byte *result = NULL;
23949 struct type *type;
23950 LONGEST value;
23951 enum bfd_endian byte_order;
23952 struct objfile *objfile = per_objfile->objfile;
23953
23954 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23955 if (cu == nullptr)
23956 cu = load_cu (per_cu, per_objfile, false);
23957
23958 if (cu == nullptr)
23959 {
23960 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23961 Instead just throw an error, not much else we can do. */
23962 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23963 sect_offset_str (sect_off), objfile_name (objfile));
23964 }
23965
23966 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23967 if (!die)
23968 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23969 sect_offset_str (sect_off), objfile_name (objfile));
23970
23971 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23972 if (attr == NULL)
23973 return NULL;
23974
23975 byte_order = (bfd_big_endian (objfile->obfd)
23976 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23977
23978 switch (attr->form)
23979 {
23980 case DW_FORM_addr:
23981 case DW_FORM_addrx:
23982 case DW_FORM_GNU_addr_index:
23983 {
23984 gdb_byte *tem;
23985
23986 *len = cu->header.addr_size;
23987 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23988 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
23989 result = tem;
23990 }
23991 break;
23992 case DW_FORM_string:
23993 case DW_FORM_strp:
23994 case DW_FORM_strx:
23995 case DW_FORM_GNU_str_index:
23996 case DW_FORM_GNU_strp_alt:
23997 /* The string is already allocated on the objfile obstack, point
23998 directly to it. */
23999 {
24000 const char *attr_name = attr->as_string ();
24001 result = (const gdb_byte *) attr_name;
24002 *len = strlen (attr_name);
24003 }
24004 break;
24005 case DW_FORM_block1:
24006 case DW_FORM_block2:
24007 case DW_FORM_block4:
24008 case DW_FORM_block:
24009 case DW_FORM_exprloc:
24010 case DW_FORM_data16:
24011 {
24012 struct dwarf_block *block = attr->as_block ();
24013 result = block->data;
24014 *len = block->size;
24015 }
24016 break;
24017
24018 /* The DW_AT_const_value attributes are supposed to carry the
24019 symbol's value "represented as it would be on the target
24020 architecture." By the time we get here, it's already been
24021 converted to host endianness, so we just need to sign- or
24022 zero-extend it as appropriate. */
24023 case DW_FORM_data1:
24024 type = die_type (die, cu);
24025 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
24026 if (result == NULL)
24027 result = write_constant_as_bytes (obstack, byte_order,
24028 type, value, len);
24029 break;
24030 case DW_FORM_data2:
24031 type = die_type (die, cu);
24032 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
24033 if (result == NULL)
24034 result = write_constant_as_bytes (obstack, byte_order,
24035 type, value, len);
24036 break;
24037 case DW_FORM_data4:
24038 type = die_type (die, cu);
24039 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
24040 if (result == NULL)
24041 result = write_constant_as_bytes (obstack, byte_order,
24042 type, value, len);
24043 break;
24044 case DW_FORM_data8:
24045 type = die_type (die, cu);
24046 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
24047 if (result == NULL)
24048 result = write_constant_as_bytes (obstack, byte_order,
24049 type, value, len);
24050 break;
24051
24052 case DW_FORM_sdata:
24053 case DW_FORM_implicit_const:
24054 type = die_type (die, cu);
24055 result = write_constant_as_bytes (obstack, byte_order,
24056 type, attr->as_signed (), len);
24057 break;
24058
24059 case DW_FORM_udata:
24060 type = die_type (die, cu);
24061 result = write_constant_as_bytes (obstack, byte_order,
24062 type, attr->as_unsigned (), len);
24063 break;
24064
24065 default:
24066 complaint (_("unsupported const value attribute form: '%s'"),
24067 dwarf_form_name (attr->form));
24068 break;
24069 }
24070
24071 return result;
24072 }
24073
24074 /* See read.h. */
24075
24076 struct type *
24077 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
24078 dwarf2_per_cu_data *per_cu,
24079 dwarf2_per_objfile *per_objfile)
24080 {
24081 struct die_info *die;
24082
24083 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
24084 if (cu == nullptr)
24085 cu = load_cu (per_cu, per_objfile, false);
24086
24087 if (cu == nullptr)
24088 return nullptr;
24089
24090 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
24091 if (!die)
24092 return NULL;
24093
24094 return die_type (die, cu);
24095 }
24096
24097 /* See read.h. */
24098
24099 struct type *
24100 dwarf2_get_die_type (cu_offset die_offset,
24101 dwarf2_per_cu_data *per_cu,
24102 dwarf2_per_objfile *per_objfile)
24103 {
24104 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
24105 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
24106 }
24107
24108 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
24109 On entry *REF_CU is the CU of SRC_DIE.
24110 On exit *REF_CU is the CU of the result.
24111 Returns NULL if the referenced DIE isn't found. */
24112
24113 static struct die_info *
24114 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
24115 struct dwarf2_cu **ref_cu)
24116 {
24117 struct die_info temp_die;
24118 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
24119 struct die_info *die;
24120 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
24121
24122
24123 /* While it might be nice to assert sig_type->type == NULL here,
24124 we can get here for DW_AT_imported_declaration where we need
24125 the DIE not the type. */
24126
24127 /* If necessary, add it to the queue and load its DIEs.
24128
24129 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
24130 it doesn't mean they are currently loaded. Since we require them
24131 to be loaded, we must check for ourselves. */
24132 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
24133 language_minimal)
24134 || per_objfile->get_cu (&sig_type->per_cu) == nullptr)
24135 read_signatured_type (sig_type, per_objfile);
24136
24137 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
24138 gdb_assert (sig_cu != NULL);
24139 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
24140 temp_die.sect_off = sig_type->type_offset_in_section;
24141 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
24142 to_underlying (temp_die.sect_off));
24143 if (die)
24144 {
24145 /* For .gdb_index version 7 keep track of included TUs.
24146 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
24147 if (per_objfile->per_bfd->index_table != NULL
24148 && per_objfile->per_bfd->index_table->version <= 7)
24149 {
24150 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
24151 }
24152
24153 *ref_cu = sig_cu;
24154 if (sig_cu != cu)
24155 sig_cu->ancestor = cu;
24156
24157 return die;
24158 }
24159
24160 return NULL;
24161 }
24162
24163 /* Follow signatured type referenced by ATTR in SRC_DIE.
24164 On entry *REF_CU is the CU of SRC_DIE.
24165 On exit *REF_CU is the CU of the result.
24166 The result is the DIE of the type.
24167 If the referenced type cannot be found an error is thrown. */
24168
24169 static struct die_info *
24170 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
24171 struct dwarf2_cu **ref_cu)
24172 {
24173 ULONGEST signature = attr->as_signature ();
24174 struct signatured_type *sig_type;
24175 struct die_info *die;
24176
24177 gdb_assert (attr->form == DW_FORM_ref_sig8);
24178
24179 sig_type = lookup_signatured_type (*ref_cu, signature);
24180 /* sig_type will be NULL if the signatured type is missing from
24181 the debug info. */
24182 if (sig_type == NULL)
24183 {
24184 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24185 " from DIE at %s [in module %s]"),
24186 hex_string (signature), sect_offset_str (src_die->sect_off),
24187 objfile_name ((*ref_cu)->per_objfile->objfile));
24188 }
24189
24190 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
24191 if (die == NULL)
24192 {
24193 dump_die_for_error (src_die);
24194 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24195 " from DIE at %s [in module %s]"),
24196 hex_string (signature), sect_offset_str (src_die->sect_off),
24197 objfile_name ((*ref_cu)->per_objfile->objfile));
24198 }
24199
24200 return die;
24201 }
24202
24203 /* Get the type specified by SIGNATURE referenced in DIE/CU,
24204 reading in and processing the type unit if necessary. */
24205
24206 static struct type *
24207 get_signatured_type (struct die_info *die, ULONGEST signature,
24208 struct dwarf2_cu *cu)
24209 {
24210 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24211 struct signatured_type *sig_type;
24212 struct dwarf2_cu *type_cu;
24213 struct die_info *type_die;
24214 struct type *type;
24215
24216 sig_type = lookup_signatured_type (cu, signature);
24217 /* sig_type will be NULL if the signatured type is missing from
24218 the debug info. */
24219 if (sig_type == NULL)
24220 {
24221 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24222 " from DIE at %s [in module %s]"),
24223 hex_string (signature), sect_offset_str (die->sect_off),
24224 objfile_name (per_objfile->objfile));
24225 return build_error_marker_type (cu, die);
24226 }
24227
24228 /* If we already know the type we're done. */
24229 type = per_objfile->get_type_for_signatured_type (sig_type);
24230 if (type != nullptr)
24231 return type;
24232
24233 type_cu = cu;
24234 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24235 if (type_die != NULL)
24236 {
24237 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24238 is created. This is important, for example, because for c++ classes
24239 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24240 type = read_type_die (type_die, type_cu);
24241 if (type == NULL)
24242 {
24243 complaint (_("Dwarf Error: Cannot build signatured type %s"
24244 " referenced from DIE at %s [in module %s]"),
24245 hex_string (signature), sect_offset_str (die->sect_off),
24246 objfile_name (per_objfile->objfile));
24247 type = build_error_marker_type (cu, die);
24248 }
24249 }
24250 else
24251 {
24252 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24253 " from DIE at %s [in module %s]"),
24254 hex_string (signature), sect_offset_str (die->sect_off),
24255 objfile_name (per_objfile->objfile));
24256 type = build_error_marker_type (cu, die);
24257 }
24258
24259 per_objfile->set_type_for_signatured_type (sig_type, type);
24260
24261 return type;
24262 }
24263
24264 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24265 reading in and processing the type unit if necessary. */
24266
24267 static struct type *
24268 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
24269 struct dwarf2_cu *cu) /* ARI: editCase function */
24270 {
24271 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
24272 if (attr->form_is_ref ())
24273 {
24274 struct dwarf2_cu *type_cu = cu;
24275 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24276
24277 return read_type_die (type_die, type_cu);
24278 }
24279 else if (attr->form == DW_FORM_ref_sig8)
24280 {
24281 return get_signatured_type (die, attr->as_signature (), cu);
24282 }
24283 else
24284 {
24285 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24286
24287 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
24288 " at %s [in module %s]"),
24289 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
24290 objfile_name (per_objfile->objfile));
24291 return build_error_marker_type (cu, die);
24292 }
24293 }
24294
24295 /* Load the DIEs associated with type unit PER_CU into memory. */
24296
24297 static void
24298 load_full_type_unit (dwarf2_per_cu_data *per_cu,
24299 dwarf2_per_objfile *per_objfile)
24300 {
24301 struct signatured_type *sig_type;
24302
24303 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24304 gdb_assert (! per_cu->type_unit_group_p ());
24305
24306 /* We have the per_cu, but we need the signatured_type.
24307 Fortunately this is an easy translation. */
24308 gdb_assert (per_cu->is_debug_types);
24309 sig_type = (struct signatured_type *) per_cu;
24310
24311 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
24312
24313 read_signatured_type (sig_type, per_objfile);
24314
24315 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
24316 }
24317
24318 /* Read in a signatured type and build its CU and DIEs.
24319 If the type is a stub for the real type in a DWO file,
24320 read in the real type from the DWO file as well. */
24321
24322 static void
24323 read_signatured_type (signatured_type *sig_type,
24324 dwarf2_per_objfile *per_objfile)
24325 {
24326 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
24327
24328 gdb_assert (per_cu->is_debug_types);
24329 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
24330
24331 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
24332
24333 if (!reader.dummy_p)
24334 {
24335 struct dwarf2_cu *cu = reader.cu;
24336 const gdb_byte *info_ptr = reader.info_ptr;
24337
24338 gdb_assert (cu->die_hash == NULL);
24339 cu->die_hash =
24340 htab_create_alloc_ex (cu->header.length / 12,
24341 die_hash,
24342 die_eq,
24343 NULL,
24344 &cu->comp_unit_obstack,
24345 hashtab_obstack_allocate,
24346 dummy_obstack_deallocate);
24347
24348 if (reader.comp_unit_die->has_children)
24349 reader.comp_unit_die->child
24350 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24351 reader.comp_unit_die);
24352 cu->dies = reader.comp_unit_die;
24353 /* comp_unit_die is not stored in die_hash, no need. */
24354
24355 /* We try not to read any attributes in this function, because
24356 not all CUs needed for references have been loaded yet, and
24357 symbol table processing isn't initialized. But we have to
24358 set the CU language, or we won't be able to build types
24359 correctly. Similarly, if we do not read the producer, we can
24360 not apply producer-specific interpretation. */
24361 prepare_one_comp_unit (cu, cu->dies, language_minimal);
24362
24363 reader.keep ();
24364 }
24365
24366 sig_type->per_cu.tu_read = 1;
24367 }
24368
24369 /* Decode simple location descriptions.
24370 Given a pointer to a dwarf block that defines a location, compute
24371 the location and return the value. If COMPUTED is non-null, it is
24372 set to true to indicate that decoding was successful, and false
24373 otherwise. If COMPUTED is null, then this function may emit a
24374 complaint. */
24375
24376 static CORE_ADDR
24377 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
24378 {
24379 struct objfile *objfile = cu->per_objfile->objfile;
24380 size_t i;
24381 size_t size = blk->size;
24382 const gdb_byte *data = blk->data;
24383 CORE_ADDR stack[64];
24384 int stacki;
24385 unsigned int bytes_read, unsnd;
24386 gdb_byte op;
24387
24388 if (computed != nullptr)
24389 *computed = false;
24390
24391 i = 0;
24392 stacki = 0;
24393 stack[stacki] = 0;
24394 stack[++stacki] = 0;
24395
24396 while (i < size)
24397 {
24398 op = data[i++];
24399 switch (op)
24400 {
24401 case DW_OP_lit0:
24402 case DW_OP_lit1:
24403 case DW_OP_lit2:
24404 case DW_OP_lit3:
24405 case DW_OP_lit4:
24406 case DW_OP_lit5:
24407 case DW_OP_lit6:
24408 case DW_OP_lit7:
24409 case DW_OP_lit8:
24410 case DW_OP_lit9:
24411 case DW_OP_lit10:
24412 case DW_OP_lit11:
24413 case DW_OP_lit12:
24414 case DW_OP_lit13:
24415 case DW_OP_lit14:
24416 case DW_OP_lit15:
24417 case DW_OP_lit16:
24418 case DW_OP_lit17:
24419 case DW_OP_lit18:
24420 case DW_OP_lit19:
24421 case DW_OP_lit20:
24422 case DW_OP_lit21:
24423 case DW_OP_lit22:
24424 case DW_OP_lit23:
24425 case DW_OP_lit24:
24426 case DW_OP_lit25:
24427 case DW_OP_lit26:
24428 case DW_OP_lit27:
24429 case DW_OP_lit28:
24430 case DW_OP_lit29:
24431 case DW_OP_lit30:
24432 case DW_OP_lit31:
24433 stack[++stacki] = op - DW_OP_lit0;
24434 break;
24435
24436 case DW_OP_reg0:
24437 case DW_OP_reg1:
24438 case DW_OP_reg2:
24439 case DW_OP_reg3:
24440 case DW_OP_reg4:
24441 case DW_OP_reg5:
24442 case DW_OP_reg6:
24443 case DW_OP_reg7:
24444 case DW_OP_reg8:
24445 case DW_OP_reg9:
24446 case DW_OP_reg10:
24447 case DW_OP_reg11:
24448 case DW_OP_reg12:
24449 case DW_OP_reg13:
24450 case DW_OP_reg14:
24451 case DW_OP_reg15:
24452 case DW_OP_reg16:
24453 case DW_OP_reg17:
24454 case DW_OP_reg18:
24455 case DW_OP_reg19:
24456 case DW_OP_reg20:
24457 case DW_OP_reg21:
24458 case DW_OP_reg22:
24459 case DW_OP_reg23:
24460 case DW_OP_reg24:
24461 case DW_OP_reg25:
24462 case DW_OP_reg26:
24463 case DW_OP_reg27:
24464 case DW_OP_reg28:
24465 case DW_OP_reg29:
24466 case DW_OP_reg30:
24467 case DW_OP_reg31:
24468 stack[++stacki] = op - DW_OP_reg0;
24469 if (i < size)
24470 {
24471 if (computed == nullptr)
24472 dwarf2_complex_location_expr_complaint ();
24473 else
24474 return 0;
24475 }
24476 break;
24477
24478 case DW_OP_regx:
24479 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24480 i += bytes_read;
24481 stack[++stacki] = unsnd;
24482 if (i < size)
24483 {
24484 if (computed == nullptr)
24485 dwarf2_complex_location_expr_complaint ();
24486 else
24487 return 0;
24488 }
24489 break;
24490
24491 case DW_OP_addr:
24492 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24493 &bytes_read);
24494 i += bytes_read;
24495 break;
24496
24497 case DW_OP_const1u:
24498 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24499 i += 1;
24500 break;
24501
24502 case DW_OP_const1s:
24503 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24504 i += 1;
24505 break;
24506
24507 case DW_OP_const2u:
24508 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24509 i += 2;
24510 break;
24511
24512 case DW_OP_const2s:
24513 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24514 i += 2;
24515 break;
24516
24517 case DW_OP_const4u:
24518 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24519 i += 4;
24520 break;
24521
24522 case DW_OP_const4s:
24523 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24524 i += 4;
24525 break;
24526
24527 case DW_OP_const8u:
24528 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24529 i += 8;
24530 break;
24531
24532 case DW_OP_constu:
24533 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24534 &bytes_read);
24535 i += bytes_read;
24536 break;
24537
24538 case DW_OP_consts:
24539 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24540 i += bytes_read;
24541 break;
24542
24543 case DW_OP_dup:
24544 stack[stacki + 1] = stack[stacki];
24545 stacki++;
24546 break;
24547
24548 case DW_OP_plus:
24549 stack[stacki - 1] += stack[stacki];
24550 stacki--;
24551 break;
24552
24553 case DW_OP_plus_uconst:
24554 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24555 &bytes_read);
24556 i += bytes_read;
24557 break;
24558
24559 case DW_OP_minus:
24560 stack[stacki - 1] -= stack[stacki];
24561 stacki--;
24562 break;
24563
24564 case DW_OP_deref:
24565 /* If we're not the last op, then we definitely can't encode
24566 this using GDB's address_class enum. This is valid for partial
24567 global symbols, although the variable's address will be bogus
24568 in the psymtab. */
24569 if (i < size)
24570 {
24571 if (computed == nullptr)
24572 dwarf2_complex_location_expr_complaint ();
24573 else
24574 return 0;
24575 }
24576 break;
24577
24578 case DW_OP_GNU_push_tls_address:
24579 case DW_OP_form_tls_address:
24580 /* The top of the stack has the offset from the beginning
24581 of the thread control block at which the variable is located. */
24582 /* Nothing should follow this operator, so the top of stack would
24583 be returned. */
24584 /* This is valid for partial global symbols, but the variable's
24585 address will be bogus in the psymtab. Make it always at least
24586 non-zero to not look as a variable garbage collected by linker
24587 which have DW_OP_addr 0. */
24588 if (i < size)
24589 {
24590 if (computed == nullptr)
24591 dwarf2_complex_location_expr_complaint ();
24592 else
24593 return 0;
24594 }
24595 stack[stacki]++;
24596 break;
24597
24598 case DW_OP_GNU_uninit:
24599 if (computed != nullptr)
24600 return 0;
24601 break;
24602
24603 case DW_OP_addrx:
24604 case DW_OP_GNU_addr_index:
24605 case DW_OP_GNU_const_index:
24606 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24607 &bytes_read);
24608 i += bytes_read;
24609 break;
24610
24611 default:
24612 if (computed == nullptr)
24613 {
24614 const char *name = get_DW_OP_name (op);
24615
24616 if (name)
24617 complaint (_("unsupported stack op: '%s'"),
24618 name);
24619 else
24620 complaint (_("unsupported stack op: '%02x'"),
24621 op);
24622 }
24623
24624 return (stack[stacki]);
24625 }
24626
24627 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24628 outside of the allocated space. Also enforce minimum>0. */
24629 if (stacki >= ARRAY_SIZE (stack) - 1)
24630 {
24631 if (computed == nullptr)
24632 complaint (_("location description stack overflow"));
24633 return 0;
24634 }
24635
24636 if (stacki <= 0)
24637 {
24638 if (computed == nullptr)
24639 complaint (_("location description stack underflow"));
24640 return 0;
24641 }
24642 }
24643
24644 if (computed != nullptr)
24645 *computed = true;
24646 return (stack[stacki]);
24647 }
24648
24649 /* memory allocation interface */
24650
24651 static struct dwarf_block *
24652 dwarf_alloc_block (struct dwarf2_cu *cu)
24653 {
24654 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24655 }
24656
24657 static struct die_info *
24658 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24659 {
24660 struct die_info *die;
24661 size_t size = sizeof (struct die_info);
24662
24663 if (num_attrs > 1)
24664 size += (num_attrs - 1) * sizeof (struct attribute);
24665
24666 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24667 memset (die, 0, sizeof (struct die_info));
24668 return (die);
24669 }
24670
24671 \f
24672
24673 /* Macro support. */
24674
24675 /* An overload of dwarf_decode_macros that finds the correct section
24676 and ensures it is read in before calling the other overload. */
24677
24678 static void
24679 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24680 int section_is_gnu)
24681 {
24682 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24683 struct objfile *objfile = per_objfile->objfile;
24684 const struct line_header *lh = cu->line_header;
24685 unsigned int offset_size = cu->header.offset_size;
24686 struct dwarf2_section_info *section;
24687 const char *section_name;
24688
24689 if (cu->dwo_unit != nullptr)
24690 {
24691 if (section_is_gnu)
24692 {
24693 section = &cu->dwo_unit->dwo_file->sections.macro;
24694 section_name = ".debug_macro.dwo";
24695 }
24696 else
24697 {
24698 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24699 section_name = ".debug_macinfo.dwo";
24700 }
24701 }
24702 else
24703 {
24704 if (section_is_gnu)
24705 {
24706 section = &per_objfile->per_bfd->macro;
24707 section_name = ".debug_macro";
24708 }
24709 else
24710 {
24711 section = &per_objfile->per_bfd->macinfo;
24712 section_name = ".debug_macinfo";
24713 }
24714 }
24715
24716 section->read (objfile);
24717 if (section->buffer == nullptr)
24718 {
24719 complaint (_("missing %s section"), section_name);
24720 return;
24721 }
24722
24723 buildsym_compunit *builder = cu->get_builder ();
24724
24725 struct dwarf2_section_info *str_offsets_section;
24726 struct dwarf2_section_info *str_section;
24727 ULONGEST str_offsets_base;
24728
24729 if (cu->dwo_unit != nullptr)
24730 {
24731 str_offsets_section = &cu->dwo_unit->dwo_file
24732 ->sections.str_offsets;
24733 str_section = &cu->dwo_unit->dwo_file->sections.str;
24734 str_offsets_base = cu->header.addr_size;
24735 }
24736 else
24737 {
24738 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24739 str_section = &per_objfile->per_bfd->str;
24740 str_offsets_base = *cu->str_offsets_base;
24741 }
24742
24743 dwarf_decode_macros (per_objfile, builder, section, lh,
24744 offset_size, offset, str_section, str_offsets_section,
24745 str_offsets_base, section_is_gnu);
24746 }
24747
24748 /* Return the .debug_loc section to use for CU.
24749 For DWO files use .debug_loc.dwo. */
24750
24751 static struct dwarf2_section_info *
24752 cu_debug_loc_section (struct dwarf2_cu *cu)
24753 {
24754 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24755
24756 if (cu->dwo_unit)
24757 {
24758 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24759
24760 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24761 }
24762 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24763 : &per_objfile->per_bfd->loc);
24764 }
24765
24766 /* Return the .debug_rnglists section to use for CU. */
24767 static struct dwarf2_section_info *
24768 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24769 {
24770 if (cu->header.version < 5)
24771 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24772 cu->header.version);
24773 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24774
24775 /* Make sure we read the .debug_rnglists section from the file that
24776 contains the DW_AT_ranges attribute we are reading. Normally that
24777 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24778 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24779 program. */
24780 if (cu->dwo_unit != nullptr
24781 && tag != DW_TAG_compile_unit
24782 && tag != DW_TAG_skeleton_unit)
24783 {
24784 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24785
24786 if (sections->rnglists.size > 0)
24787 return &sections->rnglists;
24788 else
24789 error (_(".debug_rnglists section is missing from .dwo file."));
24790 }
24791 return &dwarf2_per_objfile->per_bfd->rnglists;
24792 }
24793
24794 /* A helper function that fills in a dwarf2_loclist_baton. */
24795
24796 static void
24797 fill_in_loclist_baton (struct dwarf2_cu *cu,
24798 struct dwarf2_loclist_baton *baton,
24799 const struct attribute *attr)
24800 {
24801 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24802 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24803
24804 section->read (per_objfile->objfile);
24805
24806 baton->per_objfile = per_objfile;
24807 baton->per_cu = cu->per_cu;
24808 gdb_assert (baton->per_cu);
24809 /* We don't know how long the location list is, but make sure we
24810 don't run off the edge of the section. */
24811 baton->size = section->size - attr->as_unsigned ();
24812 baton->data = section->buffer + attr->as_unsigned ();
24813 if (cu->base_address.has_value ())
24814 baton->base_address = *cu->base_address;
24815 else
24816 baton->base_address = 0;
24817 baton->from_dwo = cu->dwo_unit != NULL;
24818 }
24819
24820 static void
24821 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24822 struct dwarf2_cu *cu, int is_block)
24823 {
24824 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24825 struct objfile *objfile = per_objfile->objfile;
24826 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24827
24828 if (attr->form_is_section_offset ()
24829 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24830 the section. If so, fall through to the complaint in the
24831 other branch. */
24832 && attr->as_unsigned () < section->get_size (objfile))
24833 {
24834 struct dwarf2_loclist_baton *baton;
24835
24836 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24837
24838 fill_in_loclist_baton (cu, baton, attr);
24839
24840 if (!cu->base_address.has_value ())
24841 complaint (_("Location list used without "
24842 "specifying the CU base address."));
24843
24844 SYMBOL_ACLASS_INDEX (sym) = (is_block
24845 ? dwarf2_loclist_block_index
24846 : dwarf2_loclist_index);
24847 SYMBOL_LOCATION_BATON (sym) = baton;
24848 }
24849 else
24850 {
24851 struct dwarf2_locexpr_baton *baton;
24852
24853 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24854 baton->per_objfile = per_objfile;
24855 baton->per_cu = cu->per_cu;
24856 gdb_assert (baton->per_cu);
24857
24858 if (attr->form_is_block ())
24859 {
24860 /* Note that we're just copying the block's data pointer
24861 here, not the actual data. We're still pointing into the
24862 info_buffer for SYM's objfile; right now we never release
24863 that buffer, but when we do clean up properly this may
24864 need to change. */
24865 struct dwarf_block *block = attr->as_block ();
24866 baton->size = block->size;
24867 baton->data = block->data;
24868 }
24869 else
24870 {
24871 dwarf2_invalid_attrib_class_complaint ("location description",
24872 sym->natural_name ());
24873 baton->size = 0;
24874 }
24875
24876 SYMBOL_ACLASS_INDEX (sym) = (is_block
24877 ? dwarf2_locexpr_block_index
24878 : dwarf2_locexpr_index);
24879 SYMBOL_LOCATION_BATON (sym) = baton;
24880 }
24881 }
24882
24883 /* See read.h. */
24884
24885 const comp_unit_head *
24886 dwarf2_per_cu_data::get_header () const
24887 {
24888 if (!m_header_read_in)
24889 {
24890 const gdb_byte *info_ptr
24891 = this->section->buffer + to_underlying (this->sect_off);
24892
24893 memset (&m_header, 0, sizeof (m_header));
24894
24895 read_comp_unit_head (&m_header, info_ptr, this->section,
24896 rcuh_kind::COMPILE);
24897 }
24898
24899 return &m_header;
24900 }
24901
24902 /* See read.h. */
24903
24904 int
24905 dwarf2_per_cu_data::addr_size () const
24906 {
24907 return this->get_header ()->addr_size;
24908 }
24909
24910 /* See read.h. */
24911
24912 int
24913 dwarf2_per_cu_data::offset_size () const
24914 {
24915 return this->get_header ()->offset_size;
24916 }
24917
24918 /* See read.h. */
24919
24920 int
24921 dwarf2_per_cu_data::ref_addr_size () const
24922 {
24923 const comp_unit_head *header = this->get_header ();
24924
24925 if (header->version == 2)
24926 return header->addr_size;
24927 else
24928 return header->offset_size;
24929 }
24930
24931 /* See read.h. */
24932
24933 struct type *
24934 dwarf2_cu::addr_type () const
24935 {
24936 struct objfile *objfile = this->per_objfile->objfile;
24937 struct type *void_type = objfile_type (objfile)->builtin_void;
24938 struct type *addr_type = lookup_pointer_type (void_type);
24939 int addr_size = this->per_cu->addr_size ();
24940
24941 if (TYPE_LENGTH (addr_type) == addr_size)
24942 return addr_type;
24943
24944 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
24945 return addr_type;
24946 }
24947
24948 /* A helper function for dwarf2_find_containing_comp_unit that returns
24949 the index of the result, and that searches a vector. It will
24950 return a result even if the offset in question does not actually
24951 occur in any CU. This is separate so that it can be unit
24952 tested. */
24953
24954 static int
24955 dwarf2_find_containing_comp_unit
24956 (sect_offset sect_off,
24957 unsigned int offset_in_dwz,
24958 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
24959 {
24960 int low, high;
24961
24962 low = 0;
24963 high = all_comp_units.size () - 1;
24964 while (high > low)
24965 {
24966 struct dwarf2_per_cu_data *mid_cu;
24967 int mid = low + (high - low) / 2;
24968
24969 mid_cu = all_comp_units[mid];
24970 if (mid_cu->is_dwz > offset_in_dwz
24971 || (mid_cu->is_dwz == offset_in_dwz
24972 && mid_cu->sect_off + mid_cu->length > sect_off))
24973 high = mid;
24974 else
24975 low = mid + 1;
24976 }
24977 gdb_assert (low == high);
24978 return low;
24979 }
24980
24981 /* Locate the .debug_info compilation unit from CU's objfile which contains
24982 the DIE at OFFSET. Raises an error on failure. */
24983
24984 static struct dwarf2_per_cu_data *
24985 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24986 unsigned int offset_in_dwz,
24987 dwarf2_per_objfile *per_objfile)
24988 {
24989 int low = dwarf2_find_containing_comp_unit
24990 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24991 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
24992
24993 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24994 {
24995 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24996 error (_("Dwarf Error: could not find partial DIE containing "
24997 "offset %s [in module %s]"),
24998 sect_offset_str (sect_off),
24999 bfd_get_filename (per_objfile->objfile->obfd));
25000
25001 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
25002 <= sect_off);
25003 return per_objfile->per_bfd->all_comp_units[low-1];
25004 }
25005 else
25006 {
25007 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
25008 && sect_off >= this_cu->sect_off + this_cu->length)
25009 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25010 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25011 return this_cu;
25012 }
25013 }
25014
25015 #if GDB_SELF_TEST
25016
25017 namespace selftests {
25018 namespace find_containing_comp_unit {
25019
25020 static void
25021 run_test ()
25022 {
25023 struct dwarf2_per_cu_data one {};
25024 struct dwarf2_per_cu_data two {};
25025 struct dwarf2_per_cu_data three {};
25026 struct dwarf2_per_cu_data four {};
25027
25028 one.length = 5;
25029 two.sect_off = sect_offset (one.length);
25030 two.length = 7;
25031
25032 three.length = 5;
25033 three.is_dwz = 1;
25034 four.sect_off = sect_offset (three.length);
25035 four.length = 7;
25036 four.is_dwz = 1;
25037
25038 std::vector<dwarf2_per_cu_data *> units;
25039 units.push_back (&one);
25040 units.push_back (&two);
25041 units.push_back (&three);
25042 units.push_back (&four);
25043
25044 int result;
25045
25046 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
25047 SELF_CHECK (units[result] == &one);
25048 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
25049 SELF_CHECK (units[result] == &one);
25050 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
25051 SELF_CHECK (units[result] == &two);
25052
25053 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
25054 SELF_CHECK (units[result] == &three);
25055 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
25056 SELF_CHECK (units[result] == &three);
25057 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
25058 SELF_CHECK (units[result] == &four);
25059 }
25060
25061 }
25062 }
25063
25064 #endif /* GDB_SELF_TEST */
25065
25066 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
25067
25068 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
25069 dwarf2_per_objfile *per_objfile)
25070 : per_cu (per_cu),
25071 per_objfile (per_objfile),
25072 mark (false),
25073 has_loclist (false),
25074 checked_producer (false),
25075 producer_is_gxx_lt_4_6 (false),
25076 producer_is_gcc_lt_4_3 (false),
25077 producer_is_icc (false),
25078 producer_is_icc_lt_14 (false),
25079 producer_is_codewarrior (false),
25080 processing_has_namespace_info (false)
25081 {
25082 }
25083
25084 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25085
25086 static void
25087 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25088 enum language pretend_language)
25089 {
25090 struct attribute *attr;
25091
25092 /* Set the language we're debugging. */
25093 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25094 if (attr != nullptr)
25095 set_cu_language (attr->constant_value (0), cu);
25096 else
25097 {
25098 cu->language = pretend_language;
25099 cu->language_defn = language_def (cu->language);
25100 }
25101
25102 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25103 }
25104
25105 /* See read.h. */
25106
25107 dwarf2_cu *
25108 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
25109 {
25110 auto it = m_dwarf2_cus.find (per_cu);
25111 if (it == m_dwarf2_cus.end ())
25112 return nullptr;
25113
25114 return it->second;
25115 }
25116
25117 /* See read.h. */
25118
25119 void
25120 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
25121 {
25122 gdb_assert (this->get_cu (per_cu) == nullptr);
25123
25124 m_dwarf2_cus[per_cu] = cu;
25125 }
25126
25127 /* See read.h. */
25128
25129 void
25130 dwarf2_per_objfile::age_comp_units ()
25131 {
25132 dwarf_read_debug_printf_v ("running");
25133
25134 /* This is not expected to be called in the middle of CU expansion. There is
25135 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
25136 loaded in memory. Calling age_comp_units while the queue is in use could
25137 make us free the DIEs for a CU that is in the queue and therefore break
25138 that invariant. */
25139 gdb_assert (!this->per_bfd->queue.has_value ());
25140
25141 /* Start by clearing all marks. */
25142 for (auto pair : m_dwarf2_cus)
25143 pair.second->mark = false;
25144
25145 /* Traverse all CUs, mark them and their dependencies if used recently
25146 enough. */
25147 for (auto pair : m_dwarf2_cus)
25148 {
25149 dwarf2_cu *cu = pair.second;
25150
25151 cu->last_used++;
25152 if (cu->last_used <= dwarf_max_cache_age)
25153 dwarf2_mark (cu);
25154 }
25155
25156 /* Delete all CUs still not marked. */
25157 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
25158 {
25159 dwarf2_cu *cu = it->second;
25160
25161 if (!cu->mark)
25162 {
25163 dwarf_read_debug_printf_v ("deleting old CU %s",
25164 sect_offset_str (cu->per_cu->sect_off));
25165 delete cu;
25166 it = m_dwarf2_cus.erase (it);
25167 }
25168 else
25169 it++;
25170 }
25171 }
25172
25173 /* See read.h. */
25174
25175 void
25176 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
25177 {
25178 auto it = m_dwarf2_cus.find (per_cu);
25179 if (it == m_dwarf2_cus.end ())
25180 return;
25181
25182 delete it->second;
25183
25184 m_dwarf2_cus.erase (it);
25185 }
25186
25187 dwarf2_per_objfile::~dwarf2_per_objfile ()
25188 {
25189 remove_all_cus ();
25190 }
25191
25192 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25193 We store these in a hash table separate from the DIEs, and preserve them
25194 when the DIEs are flushed out of cache.
25195
25196 The CU "per_cu" pointer is needed because offset alone is not enough to
25197 uniquely identify the type. A file may have multiple .debug_types sections,
25198 or the type may come from a DWO file. Furthermore, while it's more logical
25199 to use per_cu->section+offset, with Fission the section with the data is in
25200 the DWO file but we don't know that section at the point we need it.
25201 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25202 because we can enter the lookup routine, get_die_type_at_offset, from
25203 outside this file, and thus won't necessarily have PER_CU->cu.
25204 Fortunately, PER_CU is stable for the life of the objfile. */
25205
25206 struct dwarf2_per_cu_offset_and_type
25207 {
25208 const struct dwarf2_per_cu_data *per_cu;
25209 sect_offset sect_off;
25210 struct type *type;
25211 };
25212
25213 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25214
25215 static hashval_t
25216 per_cu_offset_and_type_hash (const void *item)
25217 {
25218 const struct dwarf2_per_cu_offset_and_type *ofs
25219 = (const struct dwarf2_per_cu_offset_and_type *) item;
25220
25221 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25222 }
25223
25224 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25225
25226 static int
25227 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25228 {
25229 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25230 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25231 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25232 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25233
25234 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25235 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25236 }
25237
25238 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25239 table if necessary. For convenience, return TYPE.
25240
25241 The DIEs reading must have careful ordering to:
25242 * Not cause infinite loops trying to read in DIEs as a prerequisite for
25243 reading current DIE.
25244 * Not trying to dereference contents of still incompletely read in types
25245 while reading in other DIEs.
25246 * Enable referencing still incompletely read in types just by a pointer to
25247 the type without accessing its fields.
25248
25249 Therefore caller should follow these rules:
25250 * Try to fetch any prerequisite types we may need to build this DIE type
25251 before building the type and calling set_die_type.
25252 * After building type call set_die_type for current DIE as soon as
25253 possible before fetching more types to complete the current type.
25254 * Make the type as complete as possible before fetching more types. */
25255
25256 static struct type *
25257 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
25258 bool skip_data_location)
25259 {
25260 dwarf2_per_objfile *per_objfile = cu->per_objfile;
25261 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25262 struct objfile *objfile = per_objfile->objfile;
25263 struct attribute *attr;
25264 struct dynamic_prop prop;
25265
25266 /* For Ada types, make sure that the gnat-specific data is always
25267 initialized (if not already set). There are a few types where
25268 we should not be doing so, because the type-specific area is
25269 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25270 where the type-specific area is used to store the floatformat).
25271 But this is not a problem, because the gnat-specific information
25272 is actually not needed for these types. */
25273 if (need_gnat_info (cu)
25274 && type->code () != TYPE_CODE_FUNC
25275 && type->code () != TYPE_CODE_FLT
25276 && type->code () != TYPE_CODE_METHODPTR
25277 && type->code () != TYPE_CODE_MEMBERPTR
25278 && type->code () != TYPE_CODE_METHOD
25279 && type->code () != TYPE_CODE_FIXED_POINT
25280 && !HAVE_GNAT_AUX_INFO (type))
25281 INIT_GNAT_SPECIFIC (type);
25282
25283 /* Read DW_AT_allocated and set in type. */
25284 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25285 if (attr != NULL)
25286 {
25287 struct type *prop_type = cu->addr_sized_int_type (false);
25288 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25289 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
25290 }
25291
25292 /* Read DW_AT_associated and set in type. */
25293 attr = dwarf2_attr (die, DW_AT_associated, cu);
25294 if (attr != NULL)
25295 {
25296 struct type *prop_type = cu->addr_sized_int_type (false);
25297 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25298 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
25299 }
25300
25301 /* Read DW_AT_data_location and set in type. */
25302 if (!skip_data_location)
25303 {
25304 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25305 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
25306 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
25307 }
25308
25309 if (per_objfile->die_type_hash == NULL)
25310 per_objfile->die_type_hash
25311 = htab_up (htab_create_alloc (127,
25312 per_cu_offset_and_type_hash,
25313 per_cu_offset_and_type_eq,
25314 NULL, xcalloc, xfree));
25315
25316 ofs.per_cu = cu->per_cu;
25317 ofs.sect_off = die->sect_off;
25318 ofs.type = type;
25319 slot = (struct dwarf2_per_cu_offset_and_type **)
25320 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
25321 if (*slot)
25322 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25323 sect_offset_str (die->sect_off));
25324 *slot = XOBNEW (&objfile->objfile_obstack,
25325 struct dwarf2_per_cu_offset_and_type);
25326 **slot = ofs;
25327 return type;
25328 }
25329
25330 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25331 or return NULL if the die does not have a saved type. */
25332
25333 static struct type *
25334 get_die_type_at_offset (sect_offset sect_off,
25335 dwarf2_per_cu_data *per_cu,
25336 dwarf2_per_objfile *per_objfile)
25337 {
25338 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25339
25340 if (per_objfile->die_type_hash == NULL)
25341 return NULL;
25342
25343 ofs.per_cu = per_cu;
25344 ofs.sect_off = sect_off;
25345 slot = ((struct dwarf2_per_cu_offset_and_type *)
25346 htab_find (per_objfile->die_type_hash.get (), &ofs));
25347 if (slot)
25348 return slot->type;
25349 else
25350 return NULL;
25351 }
25352
25353 /* Look up the type for DIE in CU in die_type_hash,
25354 or return NULL if DIE does not have a saved type. */
25355
25356 static struct type *
25357 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25358 {
25359 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
25360 }
25361
25362 /* Add a dependence relationship from CU to REF_PER_CU. */
25363
25364 static void
25365 dwarf2_add_dependence (struct dwarf2_cu *cu,
25366 struct dwarf2_per_cu_data *ref_per_cu)
25367 {
25368 void **slot;
25369
25370 if (cu->dependencies == NULL)
25371 cu->dependencies
25372 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25373 NULL, &cu->comp_unit_obstack,
25374 hashtab_obstack_allocate,
25375 dummy_obstack_deallocate);
25376
25377 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25378 if (*slot == NULL)
25379 *slot = ref_per_cu;
25380 }
25381
25382 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25383 Set the mark field in every compilation unit in the
25384 cache that we must keep because we are keeping CU.
25385
25386 DATA is the dwarf2_per_objfile object in which to look up CUs. */
25387
25388 static int
25389 dwarf2_mark_helper (void **slot, void *data)
25390 {
25391 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
25392 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
25393 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
25394
25395 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25396 reading of the chain. As such dependencies remain valid it is not much
25397 useful to track and undo them during QUIT cleanups. */
25398 if (cu == nullptr)
25399 return 1;
25400
25401 if (cu->mark)
25402 return 1;
25403
25404 cu->mark = true;
25405
25406 if (cu->dependencies != nullptr)
25407 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
25408
25409 return 1;
25410 }
25411
25412 /* Set the mark field in CU and in every other compilation unit in the
25413 cache that we must keep because we are keeping CU. */
25414
25415 static void
25416 dwarf2_mark (struct dwarf2_cu *cu)
25417 {
25418 if (cu->mark)
25419 return;
25420
25421 cu->mark = true;
25422
25423 if (cu->dependencies != nullptr)
25424 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
25425 }
25426
25427 /* Trivial hash function for partial_die_info: the hash value of a DIE
25428 is its offset in .debug_info for this objfile. */
25429
25430 static hashval_t
25431 partial_die_hash (const void *item)
25432 {
25433 const struct partial_die_info *part_die
25434 = (const struct partial_die_info *) item;
25435
25436 return to_underlying (part_die->sect_off);
25437 }
25438
25439 /* Trivial comparison function for partial_die_info structures: two DIEs
25440 are equal if they have the same offset. */
25441
25442 static int
25443 partial_die_eq (const void *item_lhs, const void *item_rhs)
25444 {
25445 const struct partial_die_info *part_die_lhs
25446 = (const struct partial_die_info *) item_lhs;
25447 const struct partial_die_info *part_die_rhs
25448 = (const struct partial_die_info *) item_rhs;
25449
25450 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25451 }
25452
25453 struct cmd_list_element *set_dwarf_cmdlist;
25454 struct cmd_list_element *show_dwarf_cmdlist;
25455
25456 static void
25457 show_check_physname (struct ui_file *file, int from_tty,
25458 struct cmd_list_element *c, const char *value)
25459 {
25460 fprintf_filtered (file,
25461 _("Whether to check \"physname\" is %s.\n"),
25462 value);
25463 }
25464
25465 void _initialize_dwarf2_read ();
25466 void
25467 _initialize_dwarf2_read ()
25468 {
25469 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
25470 Set DWARF specific variables.\n\
25471 Configure DWARF variables such as the cache size."),
25472 &set_dwarf_cmdlist, "maintenance set dwarf ",
25473 0/*allow-unknown*/, &maintenance_set_cmdlist);
25474
25475 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
25476 Show DWARF specific variables.\n\
25477 Show DWARF variables such as the cache size."),
25478 &show_dwarf_cmdlist, "maintenance show dwarf ",
25479 0/*allow-unknown*/, &maintenance_show_cmdlist);
25480
25481 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25482 &dwarf_max_cache_age, _("\
25483 Set the upper bound on the age of cached DWARF compilation units."), _("\
25484 Show the upper bound on the age of cached DWARF compilation units."), _("\
25485 A higher limit means that cached compilation units will be stored\n\
25486 in memory longer, and more total memory will be used. Zero disables\n\
25487 caching, which can slow down startup."),
25488 NULL,
25489 show_dwarf_max_cache_age,
25490 &set_dwarf_cmdlist,
25491 &show_dwarf_cmdlist);
25492
25493 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25494 Set debugging of the DWARF reader."), _("\
25495 Show debugging of the DWARF reader."), _("\
25496 When enabled (non-zero), debugging messages are printed during DWARF\n\
25497 reading and symtab expansion. A value of 1 (one) provides basic\n\
25498 information. A value greater than 1 provides more verbose information."),
25499 NULL,
25500 NULL,
25501 &setdebuglist, &showdebuglist);
25502
25503 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25504 Set debugging of the DWARF DIE reader."), _("\
25505 Show debugging of the DWARF DIE reader."), _("\
25506 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25507 The value is the maximum depth to print."),
25508 NULL,
25509 NULL,
25510 &setdebuglist, &showdebuglist);
25511
25512 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25513 Set debugging of the dwarf line reader."), _("\
25514 Show debugging of the dwarf line reader."), _("\
25515 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25516 A value of 1 (one) provides basic information.\n\
25517 A value greater than 1 provides more verbose information."),
25518 NULL,
25519 NULL,
25520 &setdebuglist, &showdebuglist);
25521
25522 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25523 Set cross-checking of \"physname\" code against demangler."), _("\
25524 Show cross-checking of \"physname\" code against demangler."), _("\
25525 When enabled, GDB's internal \"physname\" code is checked against\n\
25526 the demangler."),
25527 NULL, show_check_physname,
25528 &setdebuglist, &showdebuglist);
25529
25530 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25531 no_class, &use_deprecated_index_sections, _("\
25532 Set whether to use deprecated gdb_index sections."), _("\
25533 Show whether to use deprecated gdb_index sections."), _("\
25534 When enabled, deprecated .gdb_index sections are used anyway.\n\
25535 Normally they are ignored either because of a missing feature or\n\
25536 performance issue.\n\
25537 Warning: This option must be enabled before gdb reads the file."),
25538 NULL,
25539 NULL,
25540 &setlist, &showlist);
25541
25542 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25543 &dwarf2_locexpr_funcs);
25544 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25545 &dwarf2_loclist_funcs);
25546
25547 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25548 &dwarf2_block_frame_base_locexpr_funcs);
25549 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25550 &dwarf2_block_frame_base_loclist_funcs);
25551
25552 #if GDB_SELF_TEST
25553 selftests::register_test ("dw2_expand_symtabs_matching",
25554 selftests::dw2_expand_symtabs_matching::run_test);
25555 selftests::register_test ("dwarf2_find_containing_comp_unit",
25556 selftests::find_containing_comp_unit::run_test);
25557 #endif
25558 }