Add 'domain' parameter to expand_symtabs_matching
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2021 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit.h"
36 #include "dwarf2/index-cache.h"
37 #include "dwarf2/index-common.h"
38 #include "dwarf2/leb.h"
39 #include "dwarf2/line-header.h"
40 #include "dwarf2/dwz.h"
41 #include "dwarf2/macro.h"
42 #include "dwarf2/die.h"
43 #include "dwarf2/sect-names.h"
44 #include "dwarf2/stringify.h"
45 #include "dwarf2/public.h"
46 #include "bfd.h"
47 #include "elf-bfd.h"
48 #include "symtab.h"
49 #include "gdbtypes.h"
50 #include "objfiles.h"
51 #include "dwarf2.h"
52 #include "buildsym.h"
53 #include "demangle.h"
54 #include "gdb-demangle.h"
55 #include "filenames.h" /* for DOSish file names */
56 #include "language.h"
57 #include "complaints.h"
58 #include "dwarf2/expr.h"
59 #include "dwarf2/loc.h"
60 #include "cp-support.h"
61 #include "hashtab.h"
62 #include "command.h"
63 #include "gdbcmd.h"
64 #include "block.h"
65 #include "addrmap.h"
66 #include "typeprint.h"
67 #include "psympriv.h"
68 #include "c-lang.h"
69 #include "go-lang.h"
70 #include "valprint.h"
71 #include "gdbcore.h" /* for gnutarget */
72 #include "gdb/gdb-index.h"
73 #include "gdb_bfd.h"
74 #include "f-lang.h"
75 #include "source.h"
76 #include "build-id.h"
77 #include "namespace.h"
78 #include "gdbsupport/function-view.h"
79 #include "gdbsupport/gdb_optional.h"
80 #include "gdbsupport/underlying.h"
81 #include "gdbsupport/hash_enum.h"
82 #include "filename-seen-cache.h"
83 #include "producer.h"
84 #include <fcntl.h>
85 #include <algorithm>
86 #include <unordered_map>
87 #include "gdbsupport/selftest.h"
88 #include "rust-lang.h"
89 #include "gdbsupport/pathstuff.h"
90 #include "count-one-bits.h"
91
92 /* When == 1, print basic high level tracing messages.
93 When > 1, be more verbose.
94 This is in contrast to the low level DIE reading of dwarf_die_debug. */
95 static unsigned int dwarf_read_debug = 0;
96
97 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
98
99 #define dwarf_read_debug_printf(fmt, ...) \
100 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
101 ##__VA_ARGS__)
102
103 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
104
105 #define dwarf_read_debug_printf_v(fmt, ...) \
106 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
107 ##__VA_ARGS__)
108
109 /* When non-zero, dump DIEs after they are read in. */
110 static unsigned int dwarf_die_debug = 0;
111
112 /* When non-zero, dump line number entries as they are read in. */
113 unsigned int dwarf_line_debug = 0;
114
115 /* When true, cross-check physname against demangler. */
116 static bool check_physname = false;
117
118 /* When true, do not reject deprecated .gdb_index sections. */
119 static bool use_deprecated_index_sections = false;
120
121 /* This is used to store the data that is always per objfile. */
122 static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
123
124 /* These are used to store the dwarf2_per_bfd objects.
125
126 objfiles having the same BFD, which doesn't require relocations, are going to
127 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
128
129 Other objfiles are not going to share a dwarf2_per_bfd with any other
130 objfiles, so they'll have their own version kept in the _objfile_data_key
131 version. */
132 static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
133 static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
134
135 /* The "aclass" indices for various kinds of computed DWARF symbols. */
136
137 static int dwarf2_locexpr_index;
138 static int dwarf2_loclist_index;
139 static int dwarf2_locexpr_block_index;
140 static int dwarf2_loclist_block_index;
141
142 /* Size of .debug_loclists section header for 32-bit DWARF format. */
143 #define LOCLIST_HEADER_SIZE32 12
144
145 /* Size of .debug_loclists section header for 64-bit DWARF format. */
146 #define LOCLIST_HEADER_SIZE64 20
147
148 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
149 #define RNGLIST_HEADER_SIZE32 12
150
151 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
152 #define RNGLIST_HEADER_SIZE64 20
153
154 /* An index into a (C++) symbol name component in a symbol name as
155 recorded in the mapped_index's symbol table. For each C++ symbol
156 in the symbol table, we record one entry for the start of each
157 component in the symbol in a table of name components, and then
158 sort the table, in order to be able to binary search symbol names,
159 ignoring leading namespaces, both completion and regular look up.
160 For example, for symbol "A::B::C", we'll have an entry that points
161 to "A::B::C", another that points to "B::C", and another for "C".
162 Note that function symbols in GDB index have no parameter
163 information, just the function/method names. You can convert a
164 name_component to a "const char *" using the
165 'mapped_index::symbol_name_at(offset_type)' method. */
166
167 struct name_component
168 {
169 /* Offset in the symbol name where the component starts. Stored as
170 a (32-bit) offset instead of a pointer to save memory and improve
171 locality on 64-bit architectures. */
172 offset_type name_offset;
173
174 /* The symbol's index in the symbol and constant pool tables of a
175 mapped_index. */
176 offset_type idx;
177 };
178
179 /* Base class containing bits shared by both .gdb_index and
180 .debug_name indexes. */
181
182 struct mapped_index_base
183 {
184 mapped_index_base () = default;
185 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
186
187 /* The name_component table (a sorted vector). See name_component's
188 description above. */
189 std::vector<name_component> name_components;
190
191 /* How NAME_COMPONENTS is sorted. */
192 enum case_sensitivity name_components_casing;
193
194 /* Return the number of names in the symbol table. */
195 virtual size_t symbol_name_count () const = 0;
196
197 /* Get the name of the symbol at IDX in the symbol table. */
198 virtual const char *symbol_name_at
199 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
200
201 /* Return whether the name at IDX in the symbol table should be
202 ignored. */
203 virtual bool symbol_name_slot_invalid (offset_type idx) const
204 {
205 return false;
206 }
207
208 /* Build the symbol name component sorted vector, if we haven't
209 yet. */
210 void build_name_components (dwarf2_per_objfile *per_objfile);
211
212 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
213 possible matches for LN_NO_PARAMS in the name component
214 vector. */
215 std::pair<std::vector<name_component>::const_iterator,
216 std::vector<name_component>::const_iterator>
217 find_name_components_bounds (const lookup_name_info &ln_no_params,
218 enum language lang,
219 dwarf2_per_objfile *per_objfile) const;
220
221 /* Prevent deleting/destroying via a base class pointer. */
222 protected:
223 ~mapped_index_base() = default;
224 };
225
226 /* A description of the mapped index. The file format is described in
227 a comment by the code that writes the index. */
228 struct mapped_index final : public mapped_index_base
229 {
230 /* A slot/bucket in the symbol table hash. */
231 struct symbol_table_slot
232 {
233 const offset_type name;
234 const offset_type vec;
235 };
236
237 /* Index data format version. */
238 int version = 0;
239
240 /* The address table data. */
241 gdb::array_view<const gdb_byte> address_table;
242
243 /* The symbol table, implemented as a hash table. */
244 gdb::array_view<symbol_table_slot> symbol_table;
245
246 /* A pointer to the constant pool. */
247 const char *constant_pool = nullptr;
248
249 bool symbol_name_slot_invalid (offset_type idx) const override
250 {
251 const auto &bucket = this->symbol_table[idx];
252 return bucket.name == 0 && bucket.vec == 0;
253 }
254
255 /* Convenience method to get at the name of the symbol at IDX in the
256 symbol table. */
257 const char *symbol_name_at
258 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
259 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
260
261 size_t symbol_name_count () const override
262 { return this->symbol_table.size (); }
263 };
264
265 /* A description of the mapped .debug_names.
266 Uninitialized map has CU_COUNT 0. */
267 struct mapped_debug_names final : public mapped_index_base
268 {
269 bfd_endian dwarf5_byte_order;
270 bool dwarf5_is_dwarf64;
271 bool augmentation_is_gdb;
272 uint8_t offset_size;
273 uint32_t cu_count = 0;
274 uint32_t tu_count, bucket_count, name_count;
275 const gdb_byte *cu_table_reordered, *tu_table_reordered;
276 const uint32_t *bucket_table_reordered, *hash_table_reordered;
277 const gdb_byte *name_table_string_offs_reordered;
278 const gdb_byte *name_table_entry_offs_reordered;
279 const gdb_byte *entry_pool;
280
281 struct index_val
282 {
283 ULONGEST dwarf_tag;
284 struct attr
285 {
286 /* Attribute name DW_IDX_*. */
287 ULONGEST dw_idx;
288
289 /* Attribute form DW_FORM_*. */
290 ULONGEST form;
291
292 /* Value if FORM is DW_FORM_implicit_const. */
293 LONGEST implicit_const;
294 };
295 std::vector<attr> attr_vec;
296 };
297
298 std::unordered_map<ULONGEST, index_val> abbrev_map;
299
300 const char *namei_to_name
301 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
302
303 /* Implementation of the mapped_index_base virtual interface, for
304 the name_components cache. */
305
306 const char *symbol_name_at
307 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
308 { return namei_to_name (idx, per_objfile); }
309
310 size_t symbol_name_count () const override
311 { return this->name_count; }
312 };
313
314 /* See dwarf2read.h. */
315
316 dwarf2_per_objfile *
317 get_dwarf2_per_objfile (struct objfile *objfile)
318 {
319 return dwarf2_objfile_data_key.get (objfile);
320 }
321
322 /* Default names of the debugging sections. */
323
324 /* Note that if the debugging section has been compressed, it might
325 have a name like .zdebug_info. */
326
327 const struct dwarf2_debug_sections dwarf2_elf_names =
328 {
329 { ".debug_info", ".zdebug_info" },
330 { ".debug_abbrev", ".zdebug_abbrev" },
331 { ".debug_line", ".zdebug_line" },
332 { ".debug_loc", ".zdebug_loc" },
333 { ".debug_loclists", ".zdebug_loclists" },
334 { ".debug_macinfo", ".zdebug_macinfo" },
335 { ".debug_macro", ".zdebug_macro" },
336 { ".debug_str", ".zdebug_str" },
337 { ".debug_str_offsets", ".zdebug_str_offsets" },
338 { ".debug_line_str", ".zdebug_line_str" },
339 { ".debug_ranges", ".zdebug_ranges" },
340 { ".debug_rnglists", ".zdebug_rnglists" },
341 { ".debug_types", ".zdebug_types" },
342 { ".debug_addr", ".zdebug_addr" },
343 { ".debug_frame", ".zdebug_frame" },
344 { ".eh_frame", NULL },
345 { ".gdb_index", ".zgdb_index" },
346 { ".debug_names", ".zdebug_names" },
347 { ".debug_aranges", ".zdebug_aranges" },
348 23
349 };
350
351 /* List of DWO/DWP sections. */
352
353 static const struct dwop_section_names
354 {
355 struct dwarf2_section_names abbrev_dwo;
356 struct dwarf2_section_names info_dwo;
357 struct dwarf2_section_names line_dwo;
358 struct dwarf2_section_names loc_dwo;
359 struct dwarf2_section_names loclists_dwo;
360 struct dwarf2_section_names macinfo_dwo;
361 struct dwarf2_section_names macro_dwo;
362 struct dwarf2_section_names rnglists_dwo;
363 struct dwarf2_section_names str_dwo;
364 struct dwarf2_section_names str_offsets_dwo;
365 struct dwarf2_section_names types_dwo;
366 struct dwarf2_section_names cu_index;
367 struct dwarf2_section_names tu_index;
368 }
369 dwop_section_names =
370 {
371 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
372 { ".debug_info.dwo", ".zdebug_info.dwo" },
373 { ".debug_line.dwo", ".zdebug_line.dwo" },
374 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
375 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
376 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
377 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
378 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
379 { ".debug_str.dwo", ".zdebug_str.dwo" },
380 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
381 { ".debug_types.dwo", ".zdebug_types.dwo" },
382 { ".debug_cu_index", ".zdebug_cu_index" },
383 { ".debug_tu_index", ".zdebug_tu_index" },
384 };
385
386 /* local data types */
387
388 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
389 begin with a header, which contains the following information. */
390 struct loclists_rnglists_header
391 {
392 /* A 4-byte or 12-byte length containing the length of the
393 set of entries for this compilation unit, not including the
394 length field itself. */
395 unsigned int length;
396
397 /* A 2-byte version identifier. */
398 short version;
399
400 /* A 1-byte unsigned integer containing the size in bytes of an address on
401 the target system. */
402 unsigned char addr_size;
403
404 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
405 on the target system. */
406 unsigned char segment_collector_size;
407
408 /* A 4-byte count of the number of offsets that follow the header. */
409 unsigned int offset_entry_count;
410 };
411
412 /* Type used for delaying computation of method physnames.
413 See comments for compute_delayed_physnames. */
414 struct delayed_method_info
415 {
416 /* The type to which the method is attached, i.e., its parent class. */
417 struct type *type;
418
419 /* The index of the method in the type's function fieldlists. */
420 int fnfield_index;
421
422 /* The index of the method in the fieldlist. */
423 int index;
424
425 /* The name of the DIE. */
426 const char *name;
427
428 /* The DIE associated with this method. */
429 struct die_info *die;
430 };
431
432 /* Internal state when decoding a particular compilation unit. */
433 struct dwarf2_cu
434 {
435 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
436 dwarf2_per_objfile *per_objfile);
437
438 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
439
440 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
441 Create the set of symtabs used by this TU, or if this TU is sharing
442 symtabs with another TU and the symtabs have already been created
443 then restore those symtabs in the line header.
444 We don't need the pc/line-number mapping for type units. */
445 void setup_type_unit_groups (struct die_info *die);
446
447 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
448 buildsym_compunit constructor. */
449 struct compunit_symtab *start_symtab (const char *name,
450 const char *comp_dir,
451 CORE_ADDR low_pc);
452
453 /* Reset the builder. */
454 void reset_builder () { m_builder.reset (); }
455
456 /* Return a type that is a generic pointer type, the size of which
457 matches the address size given in the compilation unit header for
458 this CU. */
459 struct type *addr_type () const;
460
461 /* Find an integer type the same size as the address size given in
462 the compilation unit header for this CU. UNSIGNED_P controls if
463 the integer is unsigned or not. */
464 struct type *addr_sized_int_type (bool unsigned_p) const;
465
466 /* The header of the compilation unit. */
467 struct comp_unit_head header {};
468
469 /* Base address of this compilation unit. */
470 gdb::optional<CORE_ADDR> base_address;
471
472 /* The language we are debugging. */
473 enum language language = language_unknown;
474 const struct language_defn *language_defn = nullptr;
475
476 const char *producer = nullptr;
477
478 private:
479 /* The symtab builder for this CU. This is only non-NULL when full
480 symbols are being read. */
481 std::unique_ptr<buildsym_compunit> m_builder;
482
483 public:
484 /* The generic symbol table building routines have separate lists for
485 file scope symbols and all all other scopes (local scopes). So
486 we need to select the right one to pass to add_symbol_to_list().
487 We do it by keeping a pointer to the correct list in list_in_scope.
488
489 FIXME: The original dwarf code just treated the file scope as the
490 first local scope, and all other local scopes as nested local
491 scopes, and worked fine. Check to see if we really need to
492 distinguish these in buildsym.c. */
493 struct pending **list_in_scope = nullptr;
494
495 /* Hash table holding all the loaded partial DIEs
496 with partial_die->offset.SECT_OFF as hash. */
497 htab_t partial_dies = nullptr;
498
499 /* Storage for things with the same lifetime as this read-in compilation
500 unit, including partial DIEs. */
501 auto_obstack comp_unit_obstack;
502
503 /* Backlink to our per_cu entry. */
504 struct dwarf2_per_cu_data *per_cu;
505
506 /* The dwarf2_per_objfile that owns this. */
507 dwarf2_per_objfile *per_objfile;
508
509 /* How many compilation units ago was this CU last referenced? */
510 int last_used = 0;
511
512 /* A hash table of DIE cu_offset for following references with
513 die_info->offset.sect_off as hash. */
514 htab_t die_hash = nullptr;
515
516 /* Full DIEs if read in. */
517 struct die_info *dies = nullptr;
518
519 /* A set of pointers to dwarf2_per_cu_data objects for compilation
520 units referenced by this one. Only set during full symbol processing;
521 partial symbol tables do not have dependencies. */
522 htab_t dependencies = nullptr;
523
524 /* Header data from the line table, during full symbol processing. */
525 struct line_header *line_header = nullptr;
526 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
527 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
528 this is the DW_TAG_compile_unit die for this CU. We'll hold on
529 to the line header as long as this DIE is being processed. See
530 process_die_scope. */
531 die_info *line_header_die_owner = nullptr;
532
533 /* A list of methods which need to have physnames computed
534 after all type information has been read. */
535 std::vector<delayed_method_info> method_list;
536
537 /* To be copied to symtab->call_site_htab. */
538 htab_t call_site_htab = nullptr;
539
540 /* Non-NULL if this CU came from a DWO file.
541 There is an invariant here that is important to remember:
542 Except for attributes copied from the top level DIE in the "main"
543 (or "stub") file in preparation for reading the DWO file
544 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
545 Either there isn't a DWO file (in which case this is NULL and the point
546 is moot), or there is and either we're not going to read it (in which
547 case this is NULL) or there is and we are reading it (in which case this
548 is non-NULL). */
549 struct dwo_unit *dwo_unit = nullptr;
550
551 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
552 Note this value comes from the Fission stub CU/TU's DIE. */
553 gdb::optional<ULONGEST> addr_base;
554
555 /* The DW_AT_GNU_ranges_base attribute, if present.
556
557 This is only relevant in the context of pre-DWARF 5 split units. In this
558 context, there is a .debug_ranges section in the linked executable,
559 containing all the ranges data for all the compilation units. Each
560 skeleton/stub unit has (if needed) a DW_AT_GNU_ranges_base attribute that
561 indicates the base of its contribution to that section. The DW_AT_ranges
562 attributes in the split-unit are of the form DW_FORM_sec_offset and point
563 into the .debug_ranges section of the linked file. However, they are not
564 "true" DW_FORM_sec_offset, because they are relative to the base of their
565 compilation unit's contribution, rather than relative to the beginning of
566 the section. The DW_AT_GNU_ranges_base value must be added to it to make
567 it relative to the beginning of the section.
568
569 Note that the value is zero when we are not in a pre-DWARF 5 split-unit
570 case, so this value can be added without needing to know whether we are in
571 this case or not.
572
573 N.B. If a DW_AT_ranges attribute is found on the DW_TAG_compile_unit in the
574 skeleton/stub, it must not have the base added, as it already points to the
575 right place. And since the DW_TAG_compile_unit DIE in the split-unit can't
576 have a DW_AT_ranges attribute, we can use the
577
578 die->tag != DW_AT_compile_unit
579
580 to determine whether the base should be added or not. */
581 ULONGEST gnu_ranges_base = 0;
582
583 /* The DW_AT_rnglists_base attribute, if present.
584
585 This is used when processing attributes of form DW_FORM_rnglistx in
586 non-split units. Attributes of this form found in a split unit don't
587 use it, as split-unit files have their own non-shared .debug_rnglists.dwo
588 section. */
589 ULONGEST rnglists_base = 0;
590
591 /* The DW_AT_loclists_base attribute if present. */
592 ULONGEST loclist_base = 0;
593
594 /* When reading debug info generated by older versions of rustc, we
595 have to rewrite some union types to be struct types with a
596 variant part. This rewriting must be done after the CU is fully
597 read in, because otherwise at the point of rewriting some struct
598 type might not have been fully processed. So, we keep a list of
599 all such types here and process them after expansion. */
600 std::vector<struct type *> rust_unions;
601
602 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
603 files, the value is implicitly zero. For DWARF 5 version DWO files, the
604 value is often implicit and is the size of the header of
605 .debug_str_offsets section (8 or 4, depending on the address size). */
606 gdb::optional<ULONGEST> str_offsets_base;
607
608 /* Mark used when releasing cached dies. */
609 bool mark : 1;
610
611 /* This CU references .debug_loc. See the symtab->locations_valid field.
612 This test is imperfect as there may exist optimized debug code not using
613 any location list and still facing inlining issues if handled as
614 unoptimized code. For a future better test see GCC PR other/32998. */
615 bool has_loclist : 1;
616
617 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
618 if all the producer_is_* fields are valid. This information is cached
619 because profiling CU expansion showed excessive time spent in
620 producer_is_gxx_lt_4_6. */
621 bool checked_producer : 1;
622 bool producer_is_gxx_lt_4_6 : 1;
623 bool producer_is_gcc_lt_4_3 : 1;
624 bool producer_is_icc : 1;
625 bool producer_is_icc_lt_14 : 1;
626 bool producer_is_codewarrior : 1;
627
628 /* When true, the file that we're processing is known to have
629 debugging info for C++ namespaces. GCC 3.3.x did not produce
630 this information, but later versions do. */
631
632 bool processing_has_namespace_info : 1;
633
634 struct partial_die_info *find_partial_die (sect_offset sect_off);
635
636 /* If this CU was inherited by another CU (via specification,
637 abstract_origin, etc), this is the ancestor CU. */
638 dwarf2_cu *ancestor;
639
640 /* Get the buildsym_compunit for this CU. */
641 buildsym_compunit *get_builder ()
642 {
643 /* If this CU has a builder associated with it, use that. */
644 if (m_builder != nullptr)
645 return m_builder.get ();
646
647 /* Otherwise, search ancestors for a valid builder. */
648 if (ancestor != nullptr)
649 return ancestor->get_builder ();
650
651 return nullptr;
652 }
653 };
654
655 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
656 This includes type_unit_group and quick_file_names. */
657
658 struct stmt_list_hash
659 {
660 /* The DWO unit this table is from or NULL if there is none. */
661 struct dwo_unit *dwo_unit;
662
663 /* Offset in .debug_line or .debug_line.dwo. */
664 sect_offset line_sect_off;
665 };
666
667 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
668 an object of this type. This contains elements of type unit groups
669 that can be shared across objfiles. The non-shareable parts are in
670 type_unit_group_unshareable. */
671
672 struct type_unit_group
673 {
674 /* dwarf2read.c's main "handle" on a TU symtab.
675 To simplify things we create an artificial CU that "includes" all the
676 type units using this stmt_list so that the rest of the code still has
677 a "per_cu" handle on the symtab. */
678 struct dwarf2_per_cu_data per_cu;
679
680 /* The TUs that share this DW_AT_stmt_list entry.
681 This is added to while parsing type units to build partial symtabs,
682 and is deleted afterwards and not used again. */
683 std::vector<signatured_type *> *tus;
684
685 /* The data used to construct the hash key. */
686 struct stmt_list_hash hash;
687 };
688
689 /* These sections are what may appear in a (real or virtual) DWO file. */
690
691 struct dwo_sections
692 {
693 struct dwarf2_section_info abbrev;
694 struct dwarf2_section_info line;
695 struct dwarf2_section_info loc;
696 struct dwarf2_section_info loclists;
697 struct dwarf2_section_info macinfo;
698 struct dwarf2_section_info macro;
699 struct dwarf2_section_info rnglists;
700 struct dwarf2_section_info str;
701 struct dwarf2_section_info str_offsets;
702 /* In the case of a virtual DWO file, these two are unused. */
703 struct dwarf2_section_info info;
704 std::vector<dwarf2_section_info> types;
705 };
706
707 /* CUs/TUs in DWP/DWO files. */
708
709 struct dwo_unit
710 {
711 /* Backlink to the containing struct dwo_file. */
712 struct dwo_file *dwo_file;
713
714 /* The "id" that distinguishes this CU/TU.
715 .debug_info calls this "dwo_id", .debug_types calls this "signature".
716 Since signatures came first, we stick with it for consistency. */
717 ULONGEST signature;
718
719 /* The section this CU/TU lives in, in the DWO file. */
720 struct dwarf2_section_info *section;
721
722 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
723 sect_offset sect_off;
724 unsigned int length;
725
726 /* For types, offset in the type's DIE of the type defined by this TU. */
727 cu_offset type_offset_in_tu;
728 };
729
730 /* include/dwarf2.h defines the DWP section codes.
731 It defines a max value but it doesn't define a min value, which we
732 use for error checking, so provide one. */
733
734 enum dwp_v2_section_ids
735 {
736 DW_SECT_MIN = 1
737 };
738
739 /* Data for one DWO file.
740
741 This includes virtual DWO files (a virtual DWO file is a DWO file as it
742 appears in a DWP file). DWP files don't really have DWO files per se -
743 comdat folding of types "loses" the DWO file they came from, and from
744 a high level view DWP files appear to contain a mass of random types.
745 However, to maintain consistency with the non-DWP case we pretend DWP
746 files contain virtual DWO files, and we assign each TU with one virtual
747 DWO file (generally based on the line and abbrev section offsets -
748 a heuristic that seems to work in practice). */
749
750 struct dwo_file
751 {
752 dwo_file () = default;
753 DISABLE_COPY_AND_ASSIGN (dwo_file);
754
755 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
756 For virtual DWO files the name is constructed from the section offsets
757 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
758 from related CU+TUs. */
759 const char *dwo_name = nullptr;
760
761 /* The DW_AT_comp_dir attribute. */
762 const char *comp_dir = nullptr;
763
764 /* The bfd, when the file is open. Otherwise this is NULL.
765 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
766 gdb_bfd_ref_ptr dbfd;
767
768 /* The sections that make up this DWO file.
769 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
770 sections (for lack of a better name). */
771 struct dwo_sections sections {};
772
773 /* The CUs in the file.
774 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
775 an extension to handle LLVM's Link Time Optimization output (where
776 multiple source files may be compiled into a single object/dwo pair). */
777 htab_up cus;
778
779 /* Table of TUs in the file.
780 Each element is a struct dwo_unit. */
781 htab_up tus;
782 };
783
784 /* These sections are what may appear in a DWP file. */
785
786 struct dwp_sections
787 {
788 /* These are used by all DWP versions (1, 2 and 5). */
789 struct dwarf2_section_info str;
790 struct dwarf2_section_info cu_index;
791 struct dwarf2_section_info tu_index;
792
793 /* These are only used by DWP version 2 and version 5 files.
794 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
795 sections are referenced by section number, and are not recorded here.
796 In DWP version 2 or 5 there is at most one copy of all these sections,
797 each section being (effectively) comprised of the concatenation of all of
798 the individual sections that exist in the version 1 format.
799 To keep the code simple we treat each of these concatenated pieces as a
800 section itself (a virtual section?). */
801 struct dwarf2_section_info abbrev;
802 struct dwarf2_section_info info;
803 struct dwarf2_section_info line;
804 struct dwarf2_section_info loc;
805 struct dwarf2_section_info loclists;
806 struct dwarf2_section_info macinfo;
807 struct dwarf2_section_info macro;
808 struct dwarf2_section_info rnglists;
809 struct dwarf2_section_info str_offsets;
810 struct dwarf2_section_info types;
811 };
812
813 /* These sections are what may appear in a virtual DWO file in DWP version 1.
814 A virtual DWO file is a DWO file as it appears in a DWP file. */
815
816 struct virtual_v1_dwo_sections
817 {
818 struct dwarf2_section_info abbrev;
819 struct dwarf2_section_info line;
820 struct dwarf2_section_info loc;
821 struct dwarf2_section_info macinfo;
822 struct dwarf2_section_info macro;
823 struct dwarf2_section_info str_offsets;
824 /* Each DWP hash table entry records one CU or one TU.
825 That is recorded here, and copied to dwo_unit.section. */
826 struct dwarf2_section_info info_or_types;
827 };
828
829 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
830 In version 2, the sections of the DWO files are concatenated together
831 and stored in one section of that name. Thus each ELF section contains
832 several "virtual" sections. */
833
834 struct virtual_v2_or_v5_dwo_sections
835 {
836 bfd_size_type abbrev_offset;
837 bfd_size_type abbrev_size;
838
839 bfd_size_type line_offset;
840 bfd_size_type line_size;
841
842 bfd_size_type loc_offset;
843 bfd_size_type loc_size;
844
845 bfd_size_type loclists_offset;
846 bfd_size_type loclists_size;
847
848 bfd_size_type macinfo_offset;
849 bfd_size_type macinfo_size;
850
851 bfd_size_type macro_offset;
852 bfd_size_type macro_size;
853
854 bfd_size_type rnglists_offset;
855 bfd_size_type rnglists_size;
856
857 bfd_size_type str_offsets_offset;
858 bfd_size_type str_offsets_size;
859
860 /* Each DWP hash table entry records one CU or one TU.
861 That is recorded here, and copied to dwo_unit.section. */
862 bfd_size_type info_or_types_offset;
863 bfd_size_type info_or_types_size;
864 };
865
866 /* Contents of DWP hash tables. */
867
868 struct dwp_hash_table
869 {
870 uint32_t version, nr_columns;
871 uint32_t nr_units, nr_slots;
872 const gdb_byte *hash_table, *unit_table;
873 union
874 {
875 struct
876 {
877 const gdb_byte *indices;
878 } v1;
879 struct
880 {
881 /* This is indexed by column number and gives the id of the section
882 in that column. */
883 #define MAX_NR_V2_DWO_SECTIONS \
884 (1 /* .debug_info or .debug_types */ \
885 + 1 /* .debug_abbrev */ \
886 + 1 /* .debug_line */ \
887 + 1 /* .debug_loc */ \
888 + 1 /* .debug_str_offsets */ \
889 + 1 /* .debug_macro or .debug_macinfo */)
890 int section_ids[MAX_NR_V2_DWO_SECTIONS];
891 const gdb_byte *offsets;
892 const gdb_byte *sizes;
893 } v2;
894 struct
895 {
896 /* This is indexed by column number and gives the id of the section
897 in that column. */
898 #define MAX_NR_V5_DWO_SECTIONS \
899 (1 /* .debug_info */ \
900 + 1 /* .debug_abbrev */ \
901 + 1 /* .debug_line */ \
902 + 1 /* .debug_loclists */ \
903 + 1 /* .debug_str_offsets */ \
904 + 1 /* .debug_macro */ \
905 + 1 /* .debug_rnglists */)
906 int section_ids[MAX_NR_V5_DWO_SECTIONS];
907 const gdb_byte *offsets;
908 const gdb_byte *sizes;
909 } v5;
910 } section_pool;
911 };
912
913 /* Data for one DWP file. */
914
915 struct dwp_file
916 {
917 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
918 : name (name_),
919 dbfd (std::move (abfd))
920 {
921 }
922
923 /* Name of the file. */
924 const char *name;
925
926 /* File format version. */
927 int version = 0;
928
929 /* The bfd. */
930 gdb_bfd_ref_ptr dbfd;
931
932 /* Section info for this file. */
933 struct dwp_sections sections {};
934
935 /* Table of CUs in the file. */
936 const struct dwp_hash_table *cus = nullptr;
937
938 /* Table of TUs in the file. */
939 const struct dwp_hash_table *tus = nullptr;
940
941 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
942 htab_up loaded_cus;
943 htab_up loaded_tus;
944
945 /* Table to map ELF section numbers to their sections.
946 This is only needed for the DWP V1 file format. */
947 unsigned int num_sections = 0;
948 asection **elf_sections = nullptr;
949 };
950
951 /* Struct used to pass misc. parameters to read_die_and_children, et
952 al. which are used for both .debug_info and .debug_types dies.
953 All parameters here are unchanging for the life of the call. This
954 struct exists to abstract away the constant parameters of die reading. */
955
956 struct die_reader_specs
957 {
958 /* The bfd of die_section. */
959 bfd *abfd;
960
961 /* The CU of the DIE we are parsing. */
962 struct dwarf2_cu *cu;
963
964 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
965 struct dwo_file *dwo_file;
966
967 /* The section the die comes from.
968 This is either .debug_info or .debug_types, or the .dwo variants. */
969 struct dwarf2_section_info *die_section;
970
971 /* die_section->buffer. */
972 const gdb_byte *buffer;
973
974 /* The end of the buffer. */
975 const gdb_byte *buffer_end;
976
977 /* The abbreviation table to use when reading the DIEs. */
978 struct abbrev_table *abbrev_table;
979 };
980
981 /* A subclass of die_reader_specs that holds storage and has complex
982 constructor and destructor behavior. */
983
984 class cutu_reader : public die_reader_specs
985 {
986 public:
987
988 cutu_reader (dwarf2_per_cu_data *this_cu,
989 dwarf2_per_objfile *per_objfile,
990 struct abbrev_table *abbrev_table,
991 dwarf2_cu *existing_cu,
992 bool skip_partial);
993
994 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
995 dwarf2_per_objfile *per_objfile,
996 struct dwarf2_cu *parent_cu = nullptr,
997 struct dwo_file *dwo_file = nullptr);
998
999 DISABLE_COPY_AND_ASSIGN (cutu_reader);
1000
1001 const gdb_byte *info_ptr = nullptr;
1002 struct die_info *comp_unit_die = nullptr;
1003 bool dummy_p = false;
1004
1005 /* Release the new CU, putting it on the chain. This cannot be done
1006 for dummy CUs. */
1007 void keep ();
1008
1009 private:
1010 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
1011 dwarf2_per_objfile *per_objfile,
1012 dwarf2_cu *existing_cu);
1013
1014 struct dwarf2_per_cu_data *m_this_cu;
1015 std::unique_ptr<dwarf2_cu> m_new_cu;
1016
1017 /* The ordinary abbreviation table. */
1018 abbrev_table_up m_abbrev_table_holder;
1019
1020 /* The DWO abbreviation table. */
1021 abbrev_table_up m_dwo_abbrev_table;
1022 };
1023
1024 /* When we construct a partial symbol table entry we only
1025 need this much information. */
1026 struct partial_die_info : public allocate_on_obstack
1027 {
1028 partial_die_info (sect_offset sect_off, const struct abbrev_info *abbrev);
1029
1030 /* Disable assign but still keep copy ctor, which is needed
1031 load_partial_dies. */
1032 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1033
1034 /* Adjust the partial die before generating a symbol for it. This
1035 function may set the is_external flag or change the DIE's
1036 name. */
1037 void fixup (struct dwarf2_cu *cu);
1038
1039 /* Read a minimal amount of information into the minimal die
1040 structure. */
1041 const gdb_byte *read (const struct die_reader_specs *reader,
1042 const struct abbrev_info &abbrev,
1043 const gdb_byte *info_ptr);
1044
1045 /* Compute the name of this partial DIE. This memoizes the
1046 result, so it is safe to call multiple times. */
1047 const char *name (dwarf2_cu *cu);
1048
1049 /* Offset of this DIE. */
1050 const sect_offset sect_off;
1051
1052 /* DWARF-2 tag for this DIE. */
1053 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1054
1055 /* Assorted flags describing the data found in this DIE. */
1056 const unsigned int has_children : 1;
1057
1058 unsigned int is_external : 1;
1059 unsigned int is_declaration : 1;
1060 unsigned int has_type : 1;
1061 unsigned int has_specification : 1;
1062 unsigned int has_pc_info : 1;
1063 unsigned int may_be_inlined : 1;
1064
1065 /* This DIE has been marked DW_AT_main_subprogram. */
1066 unsigned int main_subprogram : 1;
1067
1068 /* Flag set if the SCOPE field of this structure has been
1069 computed. */
1070 unsigned int scope_set : 1;
1071
1072 /* Flag set if the DIE has a byte_size attribute. */
1073 unsigned int has_byte_size : 1;
1074
1075 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1076 unsigned int has_const_value : 1;
1077
1078 /* Flag set if any of the DIE's children are template arguments. */
1079 unsigned int has_template_arguments : 1;
1080
1081 /* Flag set if fixup has been called on this die. */
1082 unsigned int fixup_called : 1;
1083
1084 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1085 unsigned int is_dwz : 1;
1086
1087 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1088 unsigned int spec_is_dwz : 1;
1089
1090 unsigned int canonical_name : 1;
1091
1092 /* The name of this DIE. Normally the value of DW_AT_name, but
1093 sometimes a default name for unnamed DIEs. */
1094 const char *raw_name = nullptr;
1095
1096 /* The linkage name, if present. */
1097 const char *linkage_name = nullptr;
1098
1099 /* The scope to prepend to our children. This is generally
1100 allocated on the comp_unit_obstack, so will disappear
1101 when this compilation unit leaves the cache. */
1102 const char *scope = nullptr;
1103
1104 /* Some data associated with the partial DIE. The tag determines
1105 which field is live. */
1106 union
1107 {
1108 /* The location description associated with this DIE, if any. */
1109 struct dwarf_block *locdesc;
1110 /* The offset of an import, for DW_TAG_imported_unit. */
1111 sect_offset sect_off;
1112 } d {};
1113
1114 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1115 CORE_ADDR lowpc = 0;
1116 CORE_ADDR highpc = 0;
1117
1118 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1119 DW_AT_sibling, if any. */
1120 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1121 could return DW_AT_sibling values to its caller load_partial_dies. */
1122 const gdb_byte *sibling = nullptr;
1123
1124 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1125 DW_AT_specification (or DW_AT_abstract_origin or
1126 DW_AT_extension). */
1127 sect_offset spec_offset {};
1128
1129 /* Pointers to this DIE's parent, first child, and next sibling,
1130 if any. */
1131 struct partial_die_info *die_parent = nullptr;
1132 struct partial_die_info *die_child = nullptr;
1133 struct partial_die_info *die_sibling = nullptr;
1134
1135 friend struct partial_die_info *
1136 dwarf2_cu::find_partial_die (sect_offset sect_off);
1137
1138 private:
1139 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1140 partial_die_info (sect_offset sect_off)
1141 : partial_die_info (sect_off, DW_TAG_padding, 0)
1142 {
1143 }
1144
1145 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1146 int has_children_)
1147 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1148 {
1149 is_external = 0;
1150 is_declaration = 0;
1151 has_type = 0;
1152 has_specification = 0;
1153 has_pc_info = 0;
1154 may_be_inlined = 0;
1155 main_subprogram = 0;
1156 scope_set = 0;
1157 has_byte_size = 0;
1158 has_const_value = 0;
1159 has_template_arguments = 0;
1160 fixup_called = 0;
1161 is_dwz = 0;
1162 spec_is_dwz = 0;
1163 canonical_name = 0;
1164 }
1165 };
1166
1167 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1168 but this would require a corresponding change in unpack_field_as_long
1169 and friends. */
1170 static int bits_per_byte = 8;
1171
1172 struct variant_part_builder;
1173
1174 /* When reading a variant, we track a bit more information about the
1175 field, and store it in an object of this type. */
1176
1177 struct variant_field
1178 {
1179 int first_field = -1;
1180 int last_field = -1;
1181
1182 /* A variant can contain other variant parts. */
1183 std::vector<variant_part_builder> variant_parts;
1184
1185 /* If we see a DW_TAG_variant, then this will be set if this is the
1186 default branch. */
1187 bool default_branch = false;
1188 /* If we see a DW_AT_discr_value, then this will be the discriminant
1189 value. */
1190 ULONGEST discriminant_value = 0;
1191 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1192 data. */
1193 struct dwarf_block *discr_list_data = nullptr;
1194 };
1195
1196 /* This represents a DW_TAG_variant_part. */
1197
1198 struct variant_part_builder
1199 {
1200 /* The offset of the discriminant field. */
1201 sect_offset discriminant_offset {};
1202
1203 /* Variants that are direct children of this variant part. */
1204 std::vector<variant_field> variants;
1205
1206 /* True if we're currently reading a variant. */
1207 bool processing_variant = false;
1208 };
1209
1210 struct nextfield
1211 {
1212 int accessibility = 0;
1213 int virtuality = 0;
1214 /* Variant parts need to find the discriminant, which is a DIE
1215 reference. We track the section offset of each field to make
1216 this link. */
1217 sect_offset offset;
1218 struct field field {};
1219 };
1220
1221 struct fnfieldlist
1222 {
1223 const char *name = nullptr;
1224 std::vector<struct fn_field> fnfields;
1225 };
1226
1227 /* The routines that read and process dies for a C struct or C++ class
1228 pass lists of data member fields and lists of member function fields
1229 in an instance of a field_info structure, as defined below. */
1230 struct field_info
1231 {
1232 /* List of data member and baseclasses fields. */
1233 std::vector<struct nextfield> fields;
1234 std::vector<struct nextfield> baseclasses;
1235
1236 /* Set if the accessibility of one of the fields is not public. */
1237 bool non_public_fields = false;
1238
1239 /* Member function fieldlist array, contains name of possibly overloaded
1240 member function, number of overloaded member functions and a pointer
1241 to the head of the member function field chain. */
1242 std::vector<struct fnfieldlist> fnfieldlists;
1243
1244 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1245 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1246 std::vector<struct decl_field> typedef_field_list;
1247
1248 /* Nested types defined by this class and the number of elements in this
1249 list. */
1250 std::vector<struct decl_field> nested_types_list;
1251
1252 /* If non-null, this is the variant part we are currently
1253 reading. */
1254 variant_part_builder *current_variant_part = nullptr;
1255 /* This holds all the top-level variant parts attached to the type
1256 we're reading. */
1257 std::vector<variant_part_builder> variant_parts;
1258
1259 /* Return the total number of fields (including baseclasses). */
1260 int nfields () const
1261 {
1262 return fields.size () + baseclasses.size ();
1263 }
1264 };
1265
1266 /* Loaded secondary compilation units are kept in memory until they
1267 have not been referenced for the processing of this many
1268 compilation units. Set this to zero to disable caching. Cache
1269 sizes of up to at least twenty will improve startup time for
1270 typical inter-CU-reference binaries, at an obvious memory cost. */
1271 static int dwarf_max_cache_age = 5;
1272 static void
1273 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1274 struct cmd_list_element *c, const char *value)
1275 {
1276 fprintf_filtered (file, _("The upper bound on the age of cached "
1277 "DWARF compilation units is %s.\n"),
1278 value);
1279 }
1280 \f
1281 /* local function prototypes */
1282
1283 static void dwarf2_find_base_address (struct die_info *die,
1284 struct dwarf2_cu *cu);
1285
1286 static dwarf2_psymtab *create_partial_symtab
1287 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1288 const char *name);
1289
1290 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1291 const gdb_byte *info_ptr,
1292 struct die_info *type_unit_die);
1293
1294 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
1295
1296 static void scan_partial_symbols (struct partial_die_info *,
1297 CORE_ADDR *, CORE_ADDR *,
1298 int, struct dwarf2_cu *);
1299
1300 static void add_partial_symbol (struct partial_die_info *,
1301 struct dwarf2_cu *);
1302
1303 static void add_partial_namespace (struct partial_die_info *pdi,
1304 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1305 int set_addrmap, struct dwarf2_cu *cu);
1306
1307 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1308 CORE_ADDR *highpc, int set_addrmap,
1309 struct dwarf2_cu *cu);
1310
1311 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1312 struct dwarf2_cu *cu);
1313
1314 static void add_partial_subprogram (struct partial_die_info *pdi,
1315 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1316 int need_pc, struct dwarf2_cu *cu);
1317
1318 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1319
1320 static struct partial_die_info *load_partial_dies
1321 (const struct die_reader_specs *, const gdb_byte *, int);
1322
1323 /* A pair of partial_die_info and compilation unit. */
1324 struct cu_partial_die_info
1325 {
1326 /* The compilation unit of the partial_die_info. */
1327 struct dwarf2_cu *cu;
1328 /* A partial_die_info. */
1329 struct partial_die_info *pdi;
1330
1331 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1332 : cu (cu),
1333 pdi (pdi)
1334 { /* Nothing. */ }
1335
1336 private:
1337 cu_partial_die_info () = delete;
1338 };
1339
1340 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1341 struct dwarf2_cu *);
1342
1343 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1344 struct attribute *,
1345 const struct attr_abbrev *,
1346 const gdb_byte *);
1347
1348 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1349 struct attribute *attr, dwarf_tag tag);
1350
1351 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1352
1353 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1354 dwarf2_section_info *, sect_offset);
1355
1356 static const char *read_indirect_string
1357 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
1358 const struct comp_unit_head *, unsigned int *);
1359
1360 static const char *read_indirect_string_at_offset
1361 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
1362
1363 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1364 const gdb_byte *,
1365 unsigned int *);
1366
1367 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1368 ULONGEST str_index);
1369
1370 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1371 ULONGEST str_index);
1372
1373 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1374
1375 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1376 struct dwarf2_cu *);
1377
1378 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1379 struct dwarf2_cu *cu);
1380
1381 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1382
1383 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1384 struct dwarf2_cu *cu);
1385
1386 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1387
1388 static struct die_info *die_specification (struct die_info *die,
1389 struct dwarf2_cu **);
1390
1391 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1392 struct dwarf2_cu *cu);
1393
1394 static void dwarf_decode_lines (struct line_header *, const char *,
1395 struct dwarf2_cu *, dwarf2_psymtab *,
1396 CORE_ADDR, int decode_mapping);
1397
1398 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1399 const char *);
1400
1401 static struct symbol *new_symbol (struct die_info *, struct type *,
1402 struct dwarf2_cu *, struct symbol * = NULL);
1403
1404 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1405 struct dwarf2_cu *);
1406
1407 static void dwarf2_const_value_attr (const struct attribute *attr,
1408 struct type *type,
1409 const char *name,
1410 struct obstack *obstack,
1411 struct dwarf2_cu *cu, LONGEST *value,
1412 const gdb_byte **bytes,
1413 struct dwarf2_locexpr_baton **baton);
1414
1415 static struct type *read_subrange_index_type (struct die_info *die,
1416 struct dwarf2_cu *cu);
1417
1418 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1419
1420 static int need_gnat_info (struct dwarf2_cu *);
1421
1422 static struct type *die_descriptive_type (struct die_info *,
1423 struct dwarf2_cu *);
1424
1425 static void set_descriptive_type (struct type *, struct die_info *,
1426 struct dwarf2_cu *);
1427
1428 static struct type *die_containing_type (struct die_info *,
1429 struct dwarf2_cu *);
1430
1431 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1432 struct dwarf2_cu *);
1433
1434 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1435
1436 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1437
1438 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1439
1440 static char *typename_concat (struct obstack *obs, const char *prefix,
1441 const char *suffix, int physname,
1442 struct dwarf2_cu *cu);
1443
1444 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1445
1446 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1447
1448 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1449
1450 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1451
1452 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1453
1454 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1455
1456 /* Return the .debug_loclists section to use for cu. */
1457 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1458
1459 /* Return the .debug_rnglists section to use for cu. */
1460 static struct dwarf2_section_info *cu_debug_rnglists_section
1461 (struct dwarf2_cu *cu, dwarf_tag tag);
1462
1463 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1464 values. Keep the items ordered with increasing constraints compliance. */
1465 enum pc_bounds_kind
1466 {
1467 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1468 PC_BOUNDS_NOT_PRESENT,
1469
1470 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1471 were present but they do not form a valid range of PC addresses. */
1472 PC_BOUNDS_INVALID,
1473
1474 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1475 PC_BOUNDS_RANGES,
1476
1477 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1478 PC_BOUNDS_HIGH_LOW,
1479 };
1480
1481 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1482 CORE_ADDR *, CORE_ADDR *,
1483 struct dwarf2_cu *,
1484 dwarf2_psymtab *);
1485
1486 static void get_scope_pc_bounds (struct die_info *,
1487 CORE_ADDR *, CORE_ADDR *,
1488 struct dwarf2_cu *);
1489
1490 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1491 CORE_ADDR, struct dwarf2_cu *);
1492
1493 static void dwarf2_add_field (struct field_info *, struct die_info *,
1494 struct dwarf2_cu *);
1495
1496 static void dwarf2_attach_fields_to_type (struct field_info *,
1497 struct type *, struct dwarf2_cu *);
1498
1499 static void dwarf2_add_member_fn (struct field_info *,
1500 struct die_info *, struct type *,
1501 struct dwarf2_cu *);
1502
1503 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1504 struct type *,
1505 struct dwarf2_cu *);
1506
1507 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1508
1509 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1510
1511 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1512
1513 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1514
1515 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1516
1517 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1518
1519 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1520
1521 static struct type *read_module_type (struct die_info *die,
1522 struct dwarf2_cu *cu);
1523
1524 static const char *namespace_name (struct die_info *die,
1525 int *is_anonymous, struct dwarf2_cu *);
1526
1527 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1528
1529 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1530 bool * = nullptr);
1531
1532 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1533 struct dwarf2_cu *);
1534
1535 static struct die_info *read_die_and_siblings_1
1536 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1537 struct die_info *);
1538
1539 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1540 const gdb_byte *info_ptr,
1541 const gdb_byte **new_info_ptr,
1542 struct die_info *parent);
1543
1544 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1545 struct die_info **, const gdb_byte *,
1546 int);
1547
1548 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1549 struct die_info **, const gdb_byte *);
1550
1551 static void process_die (struct die_info *, struct dwarf2_cu *);
1552
1553 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1554 struct objfile *);
1555
1556 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1557
1558 static const char *dwarf2_full_name (const char *name,
1559 struct die_info *die,
1560 struct dwarf2_cu *cu);
1561
1562 static const char *dwarf2_physname (const char *name, struct die_info *die,
1563 struct dwarf2_cu *cu);
1564
1565 static struct die_info *dwarf2_extension (struct die_info *die,
1566 struct dwarf2_cu **);
1567
1568 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1569
1570 static void dump_die_for_error (struct die_info *);
1571
1572 static void dump_die_1 (struct ui_file *, int level, int max_level,
1573 struct die_info *);
1574
1575 /*static*/ void dump_die (struct die_info *, int max_level);
1576
1577 static void store_in_ref_table (struct die_info *,
1578 struct dwarf2_cu *);
1579
1580 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1581 const struct attribute *,
1582 struct dwarf2_cu **);
1583
1584 static struct die_info *follow_die_ref (struct die_info *,
1585 const struct attribute *,
1586 struct dwarf2_cu **);
1587
1588 static struct die_info *follow_die_sig (struct die_info *,
1589 const struct attribute *,
1590 struct dwarf2_cu **);
1591
1592 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1593 struct dwarf2_cu *);
1594
1595 static struct type *get_DW_AT_signature_type (struct die_info *,
1596 const struct attribute *,
1597 struct dwarf2_cu *);
1598
1599 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1600 dwarf2_per_objfile *per_objfile);
1601
1602 static void read_signatured_type (signatured_type *sig_type,
1603 dwarf2_per_objfile *per_objfile);
1604
1605 static int attr_to_dynamic_prop (const struct attribute *attr,
1606 struct die_info *die, struct dwarf2_cu *cu,
1607 struct dynamic_prop *prop, struct type *type);
1608
1609 /* memory allocation interface */
1610
1611 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1612
1613 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1614
1615 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1616
1617 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1618 struct dwarf2_loclist_baton *baton,
1619 const struct attribute *attr);
1620
1621 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1622 struct symbol *sym,
1623 struct dwarf2_cu *cu,
1624 int is_block);
1625
1626 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1627 const gdb_byte *info_ptr,
1628 const struct abbrev_info *abbrev);
1629
1630 static hashval_t partial_die_hash (const void *item);
1631
1632 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1633
1634 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1635 (sect_offset sect_off, unsigned int offset_in_dwz,
1636 dwarf2_per_objfile *per_objfile);
1637
1638 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1639 struct die_info *comp_unit_die,
1640 enum language pretend_language);
1641
1642 static struct type *set_die_type (struct die_info *, struct type *,
1643 struct dwarf2_cu *, bool = false);
1644
1645 static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
1646
1647 static int create_all_type_units (dwarf2_per_objfile *per_objfile);
1648
1649 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1650 dwarf2_per_objfile *per_objfile,
1651 dwarf2_cu *existing_cu,
1652 bool skip_partial,
1653 enum language pretend_language);
1654
1655 static void process_full_comp_unit (dwarf2_cu *cu,
1656 enum language pretend_language);
1657
1658 static void process_full_type_unit (dwarf2_cu *cu,
1659 enum language pretend_language);
1660
1661 static void dwarf2_add_dependence (struct dwarf2_cu *,
1662 struct dwarf2_per_cu_data *);
1663
1664 static void dwarf2_mark (struct dwarf2_cu *);
1665
1666 static struct type *get_die_type_at_offset (sect_offset,
1667 dwarf2_per_cu_data *per_cu,
1668 dwarf2_per_objfile *per_objfile);
1669
1670 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1671
1672 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1673 dwarf2_per_objfile *per_objfile,
1674 enum language pretend_language);
1675
1676 static void process_queue (dwarf2_per_objfile *per_objfile);
1677
1678 /* Class, the destructor of which frees all allocated queue entries. This
1679 will only have work to do if an error was thrown while processing the
1680 dwarf. If no error was thrown then the queue entries should have all
1681 been processed, and freed, as we went along. */
1682
1683 class dwarf2_queue_guard
1684 {
1685 public:
1686 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1687 : m_per_objfile (per_objfile)
1688 {
1689 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1690
1691 m_per_objfile->per_bfd->queue.emplace ();
1692 }
1693
1694 /* Free any entries remaining on the queue. There should only be
1695 entries left if we hit an error while processing the dwarf. */
1696 ~dwarf2_queue_guard ()
1697 {
1698 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1699
1700 m_per_objfile->per_bfd->queue.reset ();
1701 }
1702
1703 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1704
1705 private:
1706 dwarf2_per_objfile *m_per_objfile;
1707 };
1708
1709 dwarf2_queue_item::~dwarf2_queue_item ()
1710 {
1711 /* Anything still marked queued is likely to be in an
1712 inconsistent state, so discard it. */
1713 if (per_cu->queued)
1714 {
1715 per_objfile->remove_cu (per_cu);
1716 per_cu->queued = 0;
1717 }
1718 }
1719
1720 /* The return type of find_file_and_directory. Note, the enclosed
1721 string pointers are only valid while this object is valid. */
1722
1723 struct file_and_directory
1724 {
1725 /* The filename. This is never NULL. */
1726 const char *name;
1727
1728 /* The compilation directory. NULL if not known. If we needed to
1729 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1730 points directly to the DW_AT_comp_dir string attribute owned by
1731 the obstack that owns the DIE. */
1732 const char *comp_dir;
1733
1734 /* If we needed to build a new string for comp_dir, this is what
1735 owns the storage. */
1736 std::string comp_dir_storage;
1737 };
1738
1739 static file_and_directory find_file_and_directory (struct die_info *die,
1740 struct dwarf2_cu *cu);
1741
1742 static htab_up allocate_signatured_type_table ();
1743
1744 static htab_up allocate_dwo_unit_table ();
1745
1746 static struct dwo_unit *lookup_dwo_unit_in_dwp
1747 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1748 const char *comp_dir, ULONGEST signature, int is_debug_types);
1749
1750 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1751
1752 static struct dwo_unit *lookup_dwo_comp_unit
1753 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1754 ULONGEST signature);
1755
1756 static struct dwo_unit *lookup_dwo_type_unit
1757 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1758
1759 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1760
1761 /* A unique pointer to a dwo_file. */
1762
1763 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1764
1765 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1766
1767 static void check_producer (struct dwarf2_cu *cu);
1768
1769 static void free_line_header_voidp (void *arg);
1770 \f
1771 /* Various complaints about symbol reading that don't abort the process. */
1772
1773 static void
1774 dwarf2_debug_line_missing_file_complaint (void)
1775 {
1776 complaint (_(".debug_line section has line data without a file"));
1777 }
1778
1779 static void
1780 dwarf2_debug_line_missing_end_sequence_complaint (void)
1781 {
1782 complaint (_(".debug_line section has line "
1783 "program sequence without an end"));
1784 }
1785
1786 static void
1787 dwarf2_complex_location_expr_complaint (void)
1788 {
1789 complaint (_("location expression too complex"));
1790 }
1791
1792 static void
1793 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1794 int arg3)
1795 {
1796 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1797 arg1, arg2, arg3);
1798 }
1799
1800 static void
1801 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1802 {
1803 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1804 arg1, arg2);
1805 }
1806
1807 /* Hash function for line_header_hash. */
1808
1809 static hashval_t
1810 line_header_hash (const struct line_header *ofs)
1811 {
1812 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1813 }
1814
1815 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1816
1817 static hashval_t
1818 line_header_hash_voidp (const void *item)
1819 {
1820 const struct line_header *ofs = (const struct line_header *) item;
1821
1822 return line_header_hash (ofs);
1823 }
1824
1825 /* Equality function for line_header_hash. */
1826
1827 static int
1828 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1829 {
1830 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1831 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1832
1833 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1834 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1835 }
1836
1837 \f
1838
1839 /* See declaration. */
1840
1841 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1842 bool can_copy_)
1843 : obfd (obfd),
1844 can_copy (can_copy_)
1845 {
1846 if (names == NULL)
1847 names = &dwarf2_elf_names;
1848
1849 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1850 locate_sections (obfd, sec, *names);
1851 }
1852
1853 dwarf2_per_bfd::~dwarf2_per_bfd ()
1854 {
1855 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1856 per_cu->imported_symtabs_free ();
1857
1858 for (signatured_type *sig_type : all_type_units)
1859 sig_type->per_cu.imported_symtabs_free ();
1860
1861 /* Everything else should be on this->obstack. */
1862 }
1863
1864 /* See read.h. */
1865
1866 void
1867 dwarf2_per_objfile::remove_all_cus ()
1868 {
1869 gdb_assert (!this->per_bfd->queue.has_value ());
1870
1871 for (auto pair : m_dwarf2_cus)
1872 delete pair.second;
1873
1874 m_dwarf2_cus.clear ();
1875 }
1876
1877 /* A helper class that calls free_cached_comp_units on
1878 destruction. */
1879
1880 class free_cached_comp_units
1881 {
1882 public:
1883
1884 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1885 : m_per_objfile (per_objfile)
1886 {
1887 }
1888
1889 ~free_cached_comp_units ()
1890 {
1891 m_per_objfile->remove_all_cus ();
1892 }
1893
1894 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1895
1896 private:
1897
1898 dwarf2_per_objfile *m_per_objfile;
1899 };
1900
1901 /* See read.h. */
1902
1903 bool
1904 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1905 {
1906 gdb_assert (per_cu->index < this->m_symtabs.size ());
1907
1908 return this->m_symtabs[per_cu->index] != nullptr;
1909 }
1910
1911 /* See read.h. */
1912
1913 compunit_symtab *
1914 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1915 {
1916 gdb_assert (per_cu->index < this->m_symtabs.size ());
1917
1918 return this->m_symtabs[per_cu->index];
1919 }
1920
1921 /* See read.h. */
1922
1923 void
1924 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1925 compunit_symtab *symtab)
1926 {
1927 gdb_assert (per_cu->index < this->m_symtabs.size ());
1928 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1929
1930 this->m_symtabs[per_cu->index] = symtab;
1931 }
1932
1933 /* Try to locate the sections we need for DWARF 2 debugging
1934 information and return true if we have enough to do something.
1935 NAMES points to the dwarf2 section names, or is NULL if the standard
1936 ELF names are used. CAN_COPY is true for formats where symbol
1937 interposition is possible and so symbol values must follow copy
1938 relocation rules. */
1939
1940 int
1941 dwarf2_has_info (struct objfile *objfile,
1942 const struct dwarf2_debug_sections *names,
1943 bool can_copy)
1944 {
1945 if (objfile->flags & OBJF_READNEVER)
1946 return 0;
1947
1948 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1949
1950 if (per_objfile == NULL)
1951 {
1952 dwarf2_per_bfd *per_bfd;
1953
1954 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1955 BFD doesn't require relocations.
1956
1957 We don't share with objfiles for which -readnow was requested,
1958 because it would complicate things when loading the same BFD with
1959 -readnow and then without -readnow. */
1960 if (!gdb_bfd_requires_relocations (objfile->obfd)
1961 && (objfile->flags & OBJF_READNOW) == 0)
1962 {
1963 /* See if one has been created for this BFD yet. */
1964 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1965
1966 if (per_bfd == nullptr)
1967 {
1968 /* No, create it now. */
1969 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1970 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1971 }
1972 }
1973 else
1974 {
1975 /* No sharing possible, create one specifically for this objfile. */
1976 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1977 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1978 }
1979
1980 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1981 }
1982
1983 return (!per_objfile->per_bfd->info.is_virtual
1984 && per_objfile->per_bfd->info.s.section != NULL
1985 && !per_objfile->per_bfd->abbrev.is_virtual
1986 && per_objfile->per_bfd->abbrev.s.section != NULL);
1987 }
1988
1989 /* See declaration. */
1990
1991 void
1992 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1993 const dwarf2_debug_sections &names)
1994 {
1995 flagword aflag = bfd_section_flags (sectp);
1996
1997 if ((aflag & SEC_HAS_CONTENTS) == 0)
1998 {
1999 }
2000 else if (elf_section_data (sectp)->this_hdr.sh_size
2001 > bfd_get_file_size (abfd))
2002 {
2003 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2004 warning (_("Discarding section %s which has a section size (%s"
2005 ") larger than the file size [in module %s]"),
2006 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2007 bfd_get_filename (abfd));
2008 }
2009 else if (names.info.matches (sectp->name))
2010 {
2011 this->info.s.section = sectp;
2012 this->info.size = bfd_section_size (sectp);
2013 }
2014 else if (names.abbrev.matches (sectp->name))
2015 {
2016 this->abbrev.s.section = sectp;
2017 this->abbrev.size = bfd_section_size (sectp);
2018 }
2019 else if (names.line.matches (sectp->name))
2020 {
2021 this->line.s.section = sectp;
2022 this->line.size = bfd_section_size (sectp);
2023 }
2024 else if (names.loc.matches (sectp->name))
2025 {
2026 this->loc.s.section = sectp;
2027 this->loc.size = bfd_section_size (sectp);
2028 }
2029 else if (names.loclists.matches (sectp->name))
2030 {
2031 this->loclists.s.section = sectp;
2032 this->loclists.size = bfd_section_size (sectp);
2033 }
2034 else if (names.macinfo.matches (sectp->name))
2035 {
2036 this->macinfo.s.section = sectp;
2037 this->macinfo.size = bfd_section_size (sectp);
2038 }
2039 else if (names.macro.matches (sectp->name))
2040 {
2041 this->macro.s.section = sectp;
2042 this->macro.size = bfd_section_size (sectp);
2043 }
2044 else if (names.str.matches (sectp->name))
2045 {
2046 this->str.s.section = sectp;
2047 this->str.size = bfd_section_size (sectp);
2048 }
2049 else if (names.str_offsets.matches (sectp->name))
2050 {
2051 this->str_offsets.s.section = sectp;
2052 this->str_offsets.size = bfd_section_size (sectp);
2053 }
2054 else if (names.line_str.matches (sectp->name))
2055 {
2056 this->line_str.s.section = sectp;
2057 this->line_str.size = bfd_section_size (sectp);
2058 }
2059 else if (names.addr.matches (sectp->name))
2060 {
2061 this->addr.s.section = sectp;
2062 this->addr.size = bfd_section_size (sectp);
2063 }
2064 else if (names.frame.matches (sectp->name))
2065 {
2066 this->frame.s.section = sectp;
2067 this->frame.size = bfd_section_size (sectp);
2068 }
2069 else if (names.eh_frame.matches (sectp->name))
2070 {
2071 this->eh_frame.s.section = sectp;
2072 this->eh_frame.size = bfd_section_size (sectp);
2073 }
2074 else if (names.ranges.matches (sectp->name))
2075 {
2076 this->ranges.s.section = sectp;
2077 this->ranges.size = bfd_section_size (sectp);
2078 }
2079 else if (names.rnglists.matches (sectp->name))
2080 {
2081 this->rnglists.s.section = sectp;
2082 this->rnglists.size = bfd_section_size (sectp);
2083 }
2084 else if (names.types.matches (sectp->name))
2085 {
2086 struct dwarf2_section_info type_section;
2087
2088 memset (&type_section, 0, sizeof (type_section));
2089 type_section.s.section = sectp;
2090 type_section.size = bfd_section_size (sectp);
2091
2092 this->types.push_back (type_section);
2093 }
2094 else if (names.gdb_index.matches (sectp->name))
2095 {
2096 this->gdb_index.s.section = sectp;
2097 this->gdb_index.size = bfd_section_size (sectp);
2098 }
2099 else if (names.debug_names.matches (sectp->name))
2100 {
2101 this->debug_names.s.section = sectp;
2102 this->debug_names.size = bfd_section_size (sectp);
2103 }
2104 else if (names.debug_aranges.matches (sectp->name))
2105 {
2106 this->debug_aranges.s.section = sectp;
2107 this->debug_aranges.size = bfd_section_size (sectp);
2108 }
2109
2110 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2111 && bfd_section_vma (sectp) == 0)
2112 this->has_section_at_zero = true;
2113 }
2114
2115 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2116 SECTION_NAME. */
2117
2118 void
2119 dwarf2_get_section_info (struct objfile *objfile,
2120 enum dwarf2_section_enum sect,
2121 asection **sectp, const gdb_byte **bufp,
2122 bfd_size_type *sizep)
2123 {
2124 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2125 struct dwarf2_section_info *info;
2126
2127 /* We may see an objfile without any DWARF, in which case we just
2128 return nothing. */
2129 if (per_objfile == NULL)
2130 {
2131 *sectp = NULL;
2132 *bufp = NULL;
2133 *sizep = 0;
2134 return;
2135 }
2136 switch (sect)
2137 {
2138 case DWARF2_DEBUG_FRAME:
2139 info = &per_objfile->per_bfd->frame;
2140 break;
2141 case DWARF2_EH_FRAME:
2142 info = &per_objfile->per_bfd->eh_frame;
2143 break;
2144 default:
2145 gdb_assert_not_reached ("unexpected section");
2146 }
2147
2148 info->read (objfile);
2149
2150 *sectp = info->get_bfd_section ();
2151 *bufp = info->buffer;
2152 *sizep = info->size;
2153 }
2154
2155 \f
2156 /* DWARF quick_symbol_functions support. */
2157
2158 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2159 unique line tables, so we maintain a separate table of all .debug_line
2160 derived entries to support the sharing.
2161 All the quick functions need is the list of file names. We discard the
2162 line_header when we're done and don't need to record it here. */
2163 struct quick_file_names
2164 {
2165 /* The data used to construct the hash key. */
2166 struct stmt_list_hash hash;
2167
2168 /* The number of entries in file_names, real_names. */
2169 unsigned int num_file_names;
2170
2171 /* The file names from the line table, after being run through
2172 file_full_name. */
2173 const char **file_names;
2174
2175 /* The file names from the line table after being run through
2176 gdb_realpath. These are computed lazily. */
2177 const char **real_names;
2178 };
2179
2180 /* When using the index (and thus not using psymtabs), each CU has an
2181 object of this type. This is used to hold information needed by
2182 the various "quick" methods. */
2183 struct dwarf2_per_cu_quick_data
2184 {
2185 /* The file table. This can be NULL if there was no file table
2186 or it's currently not read in.
2187 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2188 struct quick_file_names *file_names;
2189
2190 /* A temporary mark bit used when iterating over all CUs in
2191 expand_symtabs_matching. */
2192 unsigned int mark : 1;
2193
2194 /* True if we've tried to read the file table and found there isn't one.
2195 There will be no point in trying to read it again next time. */
2196 unsigned int no_file_data : 1;
2197 };
2198
2199 /* A subclass of psymbol_functions that arranges to read the DWARF
2200 partial symbols when needed. */
2201 struct lazy_dwarf_reader : public psymbol_functions
2202 {
2203 using psymbol_functions::psymbol_functions;
2204
2205 bool can_lazily_read_symbols () override
2206 {
2207 return true;
2208 }
2209
2210 void read_partial_symbols (struct objfile *objfile) override
2211 {
2212 if (dwarf2_has_info (objfile, nullptr))
2213 dwarf2_build_psymtabs (objfile, this);
2214 }
2215 };
2216
2217 static quick_symbol_functions_up
2218 make_lazy_dwarf_reader ()
2219 {
2220 return quick_symbol_functions_up (new lazy_dwarf_reader);
2221 }
2222
2223 struct dwarf2_base_index_functions : public quick_symbol_functions
2224 {
2225 bool has_symbols (struct objfile *objfile) override;
2226
2227 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
2228
2229 void forget_cached_source_info (struct objfile *objfile) override;
2230
2231 bool map_symtabs_matching_filename
2232 (struct objfile *objfile, const char *name, const char *real_path,
2233 gdb::function_view<bool (symtab *)> callback) override;
2234
2235 enum language lookup_global_symbol_language (struct objfile *objfile,
2236 const char *name,
2237 domain_enum domain,
2238 bool *symbol_found_p) override
2239 {
2240 *symbol_found_p = false;
2241 return language_unknown;
2242 }
2243
2244 void print_stats (struct objfile *objfile, bool print_bcache) override;
2245
2246 void expand_all_symtabs (struct objfile *objfile) override;
2247
2248 void expand_symtabs_with_fullname (struct objfile *objfile,
2249 const char *fullname) override;
2250
2251 struct compunit_symtab *find_pc_sect_compunit_symtab
2252 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
2253 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
2254
2255 struct compunit_symtab *find_compunit_symtab_by_address
2256 (struct objfile *objfile, CORE_ADDR address) override
2257 {
2258 return nullptr;
2259 }
2260
2261 void map_symbol_filenames (struct objfile *objfile,
2262 gdb::function_view<symbol_filename_ftype> fun,
2263 bool need_fullname) override;
2264 };
2265
2266 struct dwarf2_gdb_index : public dwarf2_base_index_functions
2267 {
2268 struct compunit_symtab *lookup_symbol (struct objfile *objfile,
2269 block_enum block_index,
2270 const char *name,
2271 domain_enum domain) override;
2272
2273 void dump (struct objfile *objfile) override;
2274
2275 void expand_symtabs_for_function (struct objfile *objfile,
2276 const char *func_name) override;
2277
2278 void map_matching_symbols
2279 (struct objfile *,
2280 const lookup_name_info &lookup_name,
2281 domain_enum domain,
2282 int global,
2283 gdb::function_view<symbol_found_callback_ftype> callback,
2284 symbol_compare_ftype *ordered_compare) override;
2285
2286 bool expand_symtabs_matching
2287 (struct objfile *objfile,
2288 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2289 const lookup_name_info *lookup_name,
2290 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2291 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2292 block_search_flags search_flags,
2293 domain_enum domain,
2294 enum search_domain kind) override;
2295 };
2296
2297 struct dwarf2_debug_names_index : public dwarf2_base_index_functions
2298 {
2299 struct compunit_symtab *lookup_symbol (struct objfile *objfile,
2300 block_enum block_index,
2301 const char *name,
2302 domain_enum domain) override;
2303
2304 void dump (struct objfile *objfile) override;
2305
2306 void expand_symtabs_for_function (struct objfile *objfile,
2307 const char *func_name) override;
2308
2309 void map_matching_symbols
2310 (struct objfile *,
2311 const lookup_name_info &lookup_name,
2312 domain_enum domain,
2313 int global,
2314 gdb::function_view<symbol_found_callback_ftype> callback,
2315 symbol_compare_ftype *ordered_compare) override;
2316
2317 bool expand_symtabs_matching
2318 (struct objfile *objfile,
2319 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2320 const lookup_name_info *lookup_name,
2321 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2322 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2323 block_search_flags search_flags,
2324 domain_enum domain,
2325 enum search_domain kind) override;
2326 };
2327
2328 static quick_symbol_functions_up
2329 make_dwarf_gdb_index ()
2330 {
2331 return quick_symbol_functions_up (new dwarf2_gdb_index);
2332 }
2333
2334 static quick_symbol_functions_up
2335 make_dwarf_debug_names ()
2336 {
2337 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2338 }
2339
2340 /* Utility hash function for a stmt_list_hash. */
2341
2342 static hashval_t
2343 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2344 {
2345 hashval_t v = 0;
2346
2347 if (stmt_list_hash->dwo_unit != NULL)
2348 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2349 v += to_underlying (stmt_list_hash->line_sect_off);
2350 return v;
2351 }
2352
2353 /* Utility equality function for a stmt_list_hash. */
2354
2355 static int
2356 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2357 const struct stmt_list_hash *rhs)
2358 {
2359 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2360 return 0;
2361 if (lhs->dwo_unit != NULL
2362 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2363 return 0;
2364
2365 return lhs->line_sect_off == rhs->line_sect_off;
2366 }
2367
2368 /* Hash function for a quick_file_names. */
2369
2370 static hashval_t
2371 hash_file_name_entry (const void *e)
2372 {
2373 const struct quick_file_names *file_data
2374 = (const struct quick_file_names *) e;
2375
2376 return hash_stmt_list_entry (&file_data->hash);
2377 }
2378
2379 /* Equality function for a quick_file_names. */
2380
2381 static int
2382 eq_file_name_entry (const void *a, const void *b)
2383 {
2384 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2385 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2386
2387 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2388 }
2389
2390 /* Delete function for a quick_file_names. */
2391
2392 static void
2393 delete_file_name_entry (void *e)
2394 {
2395 struct quick_file_names *file_data = (struct quick_file_names *) e;
2396 int i;
2397
2398 for (i = 0; i < file_data->num_file_names; ++i)
2399 {
2400 xfree ((void*) file_data->file_names[i]);
2401 if (file_data->real_names)
2402 xfree ((void*) file_data->real_names[i]);
2403 }
2404
2405 /* The space for the struct itself lives on the obstack, so we don't
2406 free it here. */
2407 }
2408
2409 /* Create a quick_file_names hash table. */
2410
2411 static htab_up
2412 create_quick_file_names_table (unsigned int nr_initial_entries)
2413 {
2414 return htab_up (htab_create_alloc (nr_initial_entries,
2415 hash_file_name_entry, eq_file_name_entry,
2416 delete_file_name_entry, xcalloc, xfree));
2417 }
2418
2419 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2420 function is unrelated to symtabs, symtab would have to be created afterwards.
2421 You should call age_cached_comp_units after processing the CU. */
2422
2423 static dwarf2_cu *
2424 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2425 bool skip_partial)
2426 {
2427 if (per_cu->is_debug_types)
2428 load_full_type_unit (per_cu, per_objfile);
2429 else
2430 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2431 skip_partial, language_minimal);
2432
2433 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2434 if (cu == nullptr)
2435 return nullptr; /* Dummy CU. */
2436
2437 dwarf2_find_base_address (cu->dies, cu);
2438
2439 return cu;
2440 }
2441
2442 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2443
2444 static void
2445 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2446 dwarf2_per_objfile *per_objfile, bool skip_partial)
2447 {
2448 /* Skip type_unit_groups, reading the type units they contain
2449 is handled elsewhere. */
2450 if (per_cu->type_unit_group_p ())
2451 return;
2452
2453 {
2454 /* The destructor of dwarf2_queue_guard frees any entries left on
2455 the queue. After this point we're guaranteed to leave this function
2456 with the dwarf queue empty. */
2457 dwarf2_queue_guard q_guard (per_objfile);
2458
2459 if (!per_objfile->symtab_set_p (per_cu))
2460 {
2461 queue_comp_unit (per_cu, per_objfile, language_minimal);
2462 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2463
2464 /* If we just loaded a CU from a DWO, and we're working with an index
2465 that may badly handle TUs, load all the TUs in that DWO as well.
2466 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2467 if (!per_cu->is_debug_types
2468 && cu != NULL
2469 && cu->dwo_unit != NULL
2470 && per_objfile->per_bfd->index_table != NULL
2471 && per_objfile->per_bfd->index_table->version <= 7
2472 /* DWP files aren't supported yet. */
2473 && get_dwp_file (per_objfile) == NULL)
2474 queue_and_load_all_dwo_tus (cu);
2475 }
2476
2477 process_queue (per_objfile);
2478 }
2479
2480 /* Age the cache, releasing compilation units that have not
2481 been used recently. */
2482 per_objfile->age_comp_units ();
2483 }
2484
2485 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2486 the per-objfile for which this symtab is instantiated.
2487
2488 Returns the resulting symbol table. */
2489
2490 static struct compunit_symtab *
2491 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2492 dwarf2_per_objfile *per_objfile,
2493 bool skip_partial)
2494 {
2495 gdb_assert (per_objfile->per_bfd->using_index);
2496
2497 if (!per_objfile->symtab_set_p (per_cu))
2498 {
2499 free_cached_comp_units freer (per_objfile);
2500 scoped_restore decrementer = increment_reading_symtab ();
2501 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2502 process_cu_includes (per_objfile);
2503 }
2504
2505 return per_objfile->get_symtab (per_cu);
2506 }
2507
2508 /* See declaration. */
2509
2510 dwarf2_per_cu_data *
2511 dwarf2_per_bfd::get_cutu (int index)
2512 {
2513 if (index >= this->all_comp_units.size ())
2514 {
2515 index -= this->all_comp_units.size ();
2516 gdb_assert (index < this->all_type_units.size ());
2517 return &this->all_type_units[index]->per_cu;
2518 }
2519
2520 return this->all_comp_units[index];
2521 }
2522
2523 /* See declaration. */
2524
2525 dwarf2_per_cu_data *
2526 dwarf2_per_bfd::get_cu (int index)
2527 {
2528 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2529
2530 return this->all_comp_units[index];
2531 }
2532
2533 /* See declaration. */
2534
2535 signatured_type *
2536 dwarf2_per_bfd::get_tu (int index)
2537 {
2538 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2539
2540 return this->all_type_units[index];
2541 }
2542
2543 /* See read.h. */
2544
2545 dwarf2_per_cu_data *
2546 dwarf2_per_bfd::allocate_per_cu ()
2547 {
2548 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2549 result->per_bfd = this;
2550 result->index = m_num_psymtabs++;
2551 return result;
2552 }
2553
2554 /* See read.h. */
2555
2556 signatured_type *
2557 dwarf2_per_bfd::allocate_signatured_type ()
2558 {
2559 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2560 result->per_cu.per_bfd = this;
2561 result->per_cu.index = m_num_psymtabs++;
2562 return result;
2563 }
2564
2565 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2566 obstack, and constructed with the specified field values. */
2567
2568 static dwarf2_per_cu_data *
2569 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2570 struct dwarf2_section_info *section,
2571 int is_dwz,
2572 sect_offset sect_off, ULONGEST length)
2573 {
2574 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
2575 the_cu->sect_off = sect_off;
2576 the_cu->length = length;
2577 the_cu->section = section;
2578 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2579 struct dwarf2_per_cu_quick_data);
2580 the_cu->is_dwz = is_dwz;
2581 return the_cu;
2582 }
2583
2584 /* A helper for create_cus_from_index that handles a given list of
2585 CUs. */
2586
2587 static void
2588 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2589 const gdb_byte *cu_list, offset_type n_elements,
2590 struct dwarf2_section_info *section,
2591 int is_dwz)
2592 {
2593 for (offset_type i = 0; i < n_elements; i += 2)
2594 {
2595 gdb_static_assert (sizeof (ULONGEST) >= 8);
2596
2597 sect_offset sect_off
2598 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2599 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2600 cu_list += 2 * 8;
2601
2602 dwarf2_per_cu_data *per_cu
2603 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2604 length);
2605 per_bfd->all_comp_units.push_back (per_cu);
2606 }
2607 }
2608
2609 /* Read the CU list from the mapped index, and use it to create all
2610 the CU objects for PER_BFD. */
2611
2612 static void
2613 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2614 const gdb_byte *cu_list, offset_type cu_list_elements,
2615 const gdb_byte *dwz_list, offset_type dwz_elements)
2616 {
2617 gdb_assert (per_bfd->all_comp_units.empty ());
2618 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2619
2620 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2621 &per_bfd->info, 0);
2622
2623 if (dwz_elements == 0)
2624 return;
2625
2626 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2627 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2628 &dwz->info, 1);
2629 }
2630
2631 /* Create the signatured type hash table from the index. */
2632
2633 static void
2634 create_signatured_type_table_from_index
2635 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2636 const gdb_byte *bytes, offset_type elements)
2637 {
2638 gdb_assert (per_bfd->all_type_units.empty ());
2639 per_bfd->all_type_units.reserve (elements / 3);
2640
2641 htab_up sig_types_hash = allocate_signatured_type_table ();
2642
2643 for (offset_type i = 0; i < elements; i += 3)
2644 {
2645 struct signatured_type *sig_type;
2646 ULONGEST signature;
2647 void **slot;
2648 cu_offset type_offset_in_tu;
2649
2650 gdb_static_assert (sizeof (ULONGEST) >= 8);
2651 sect_offset sect_off
2652 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2653 type_offset_in_tu
2654 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2655 BFD_ENDIAN_LITTLE);
2656 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2657 bytes += 3 * 8;
2658
2659 sig_type = per_bfd->allocate_signatured_type ();
2660 sig_type->signature = signature;
2661 sig_type->type_offset_in_tu = type_offset_in_tu;
2662 sig_type->per_cu.is_debug_types = 1;
2663 sig_type->per_cu.section = section;
2664 sig_type->per_cu.sect_off = sect_off;
2665 sig_type->per_cu.v.quick
2666 = OBSTACK_ZALLOC (&per_bfd->obstack,
2667 struct dwarf2_per_cu_quick_data);
2668
2669 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2670 *slot = sig_type;
2671
2672 per_bfd->all_type_units.push_back (sig_type);
2673 }
2674
2675 per_bfd->signatured_types = std::move (sig_types_hash);
2676 }
2677
2678 /* Create the signatured type hash table from .debug_names. */
2679
2680 static void
2681 create_signatured_type_table_from_debug_names
2682 (dwarf2_per_objfile *per_objfile,
2683 const mapped_debug_names &map,
2684 struct dwarf2_section_info *section,
2685 struct dwarf2_section_info *abbrev_section)
2686 {
2687 struct objfile *objfile = per_objfile->objfile;
2688
2689 section->read (objfile);
2690 abbrev_section->read (objfile);
2691
2692 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2693 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2694
2695 htab_up sig_types_hash = allocate_signatured_type_table ();
2696
2697 for (uint32_t i = 0; i < map.tu_count; ++i)
2698 {
2699 struct signatured_type *sig_type;
2700 void **slot;
2701
2702 sect_offset sect_off
2703 = (sect_offset) (extract_unsigned_integer
2704 (map.tu_table_reordered + i * map.offset_size,
2705 map.offset_size,
2706 map.dwarf5_byte_order));
2707
2708 comp_unit_head cu_header;
2709 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2710 abbrev_section,
2711 section->buffer + to_underlying (sect_off),
2712 rcuh_kind::TYPE);
2713
2714 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
2715 sig_type->signature = cu_header.signature;
2716 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2717 sig_type->per_cu.is_debug_types = 1;
2718 sig_type->per_cu.section = section;
2719 sig_type->per_cu.sect_off = sect_off;
2720 sig_type->per_cu.v.quick
2721 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
2722 struct dwarf2_per_cu_quick_data);
2723
2724 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2725 *slot = sig_type;
2726
2727 per_objfile->per_bfd->all_type_units.push_back (sig_type);
2728 }
2729
2730 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2731 }
2732
2733 /* Read the address map data from the mapped index, and use it to
2734 populate the psymtabs_addrmap. */
2735
2736 static void
2737 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2738 struct mapped_index *index)
2739 {
2740 struct objfile *objfile = per_objfile->objfile;
2741 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2742 struct gdbarch *gdbarch = objfile->arch ();
2743 const gdb_byte *iter, *end;
2744 struct addrmap *mutable_map;
2745 CORE_ADDR baseaddr;
2746
2747 auto_obstack temp_obstack;
2748
2749 mutable_map = addrmap_create_mutable (&temp_obstack);
2750
2751 iter = index->address_table.data ();
2752 end = iter + index->address_table.size ();
2753
2754 baseaddr = objfile->text_section_offset ();
2755
2756 while (iter < end)
2757 {
2758 ULONGEST hi, lo, cu_index;
2759 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2760 iter += 8;
2761 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2762 iter += 8;
2763 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2764 iter += 4;
2765
2766 if (lo > hi)
2767 {
2768 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2769 hex_string (lo), hex_string (hi));
2770 continue;
2771 }
2772
2773 if (cu_index >= per_bfd->all_comp_units.size ())
2774 {
2775 complaint (_(".gdb_index address table has invalid CU number %u"),
2776 (unsigned) cu_index);
2777 continue;
2778 }
2779
2780 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2781 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2782 addrmap_set_empty (mutable_map, lo, hi - 1,
2783 per_bfd->get_cu (cu_index));
2784 }
2785
2786 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2787 &per_bfd->obstack);
2788 }
2789
2790 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2791 populate the psymtabs_addrmap. */
2792
2793 static void
2794 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2795 struct dwarf2_section_info *section)
2796 {
2797 struct objfile *objfile = per_objfile->objfile;
2798 bfd *abfd = objfile->obfd;
2799 struct gdbarch *gdbarch = objfile->arch ();
2800 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2801 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2802
2803 auto_obstack temp_obstack;
2804 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2805
2806 std::unordered_map<sect_offset,
2807 dwarf2_per_cu_data *,
2808 gdb::hash_enum<sect_offset>>
2809 debug_info_offset_to_per_cu;
2810 for (dwarf2_per_cu_data *per_cu : per_bfd->all_comp_units)
2811 {
2812 const auto insertpair
2813 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2814 if (!insertpair.second)
2815 {
2816 warning (_("Section .debug_aranges in %s has duplicate "
2817 "debug_info_offset %s, ignoring .debug_aranges."),
2818 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2819 return;
2820 }
2821 }
2822
2823 section->read (objfile);
2824
2825 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2826
2827 const gdb_byte *addr = section->buffer;
2828
2829 while (addr < section->buffer + section->size)
2830 {
2831 const gdb_byte *const entry_addr = addr;
2832 unsigned int bytes_read;
2833
2834 const LONGEST entry_length = read_initial_length (abfd, addr,
2835 &bytes_read);
2836 addr += bytes_read;
2837
2838 const gdb_byte *const entry_end = addr + entry_length;
2839 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2840 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2841 if (addr + entry_length > section->buffer + section->size)
2842 {
2843 warning (_("Section .debug_aranges in %s entry at offset %s "
2844 "length %s exceeds section length %s, "
2845 "ignoring .debug_aranges."),
2846 objfile_name (objfile),
2847 plongest (entry_addr - section->buffer),
2848 plongest (bytes_read + entry_length),
2849 pulongest (section->size));
2850 return;
2851 }
2852
2853 /* The version number. */
2854 const uint16_t version = read_2_bytes (abfd, addr);
2855 addr += 2;
2856 if (version != 2)
2857 {
2858 warning (_("Section .debug_aranges in %s entry at offset %s "
2859 "has unsupported version %d, ignoring .debug_aranges."),
2860 objfile_name (objfile),
2861 plongest (entry_addr - section->buffer), version);
2862 return;
2863 }
2864
2865 const uint64_t debug_info_offset
2866 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2867 addr += offset_size;
2868 const auto per_cu_it
2869 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2870 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2871 {
2872 warning (_("Section .debug_aranges in %s entry at offset %s "
2873 "debug_info_offset %s does not exists, "
2874 "ignoring .debug_aranges."),
2875 objfile_name (objfile),
2876 plongest (entry_addr - section->buffer),
2877 pulongest (debug_info_offset));
2878 return;
2879 }
2880 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2881
2882 const uint8_t address_size = *addr++;
2883 if (address_size < 1 || address_size > 8)
2884 {
2885 warning (_("Section .debug_aranges in %s entry at offset %s "
2886 "address_size %u is invalid, ignoring .debug_aranges."),
2887 objfile_name (objfile),
2888 plongest (entry_addr - section->buffer), address_size);
2889 return;
2890 }
2891
2892 const uint8_t segment_selector_size = *addr++;
2893 if (segment_selector_size != 0)
2894 {
2895 warning (_("Section .debug_aranges in %s entry at offset %s "
2896 "segment_selector_size %u is not supported, "
2897 "ignoring .debug_aranges."),
2898 objfile_name (objfile),
2899 plongest (entry_addr - section->buffer),
2900 segment_selector_size);
2901 return;
2902 }
2903
2904 /* Must pad to an alignment boundary that is twice the address
2905 size. It is undocumented by the DWARF standard but GCC does
2906 use it. */
2907 for (size_t padding = ((-(addr - section->buffer))
2908 & (2 * address_size - 1));
2909 padding > 0; padding--)
2910 if (*addr++ != 0)
2911 {
2912 warning (_("Section .debug_aranges in %s entry at offset %s "
2913 "padding is not zero, ignoring .debug_aranges."),
2914 objfile_name (objfile),
2915 plongest (entry_addr - section->buffer));
2916 return;
2917 }
2918
2919 for (;;)
2920 {
2921 if (addr + 2 * address_size > entry_end)
2922 {
2923 warning (_("Section .debug_aranges in %s entry at offset %s "
2924 "address list is not properly terminated, "
2925 "ignoring .debug_aranges."),
2926 objfile_name (objfile),
2927 plongest (entry_addr - section->buffer));
2928 return;
2929 }
2930 ULONGEST start = extract_unsigned_integer (addr, address_size,
2931 dwarf5_byte_order);
2932 addr += address_size;
2933 ULONGEST length = extract_unsigned_integer (addr, address_size,
2934 dwarf5_byte_order);
2935 addr += address_size;
2936 if (start == 0 && length == 0)
2937 break;
2938 if (start == 0 && !per_bfd->has_section_at_zero)
2939 {
2940 /* Symbol was eliminated due to a COMDAT group. */
2941 continue;
2942 }
2943 ULONGEST end = start + length;
2944 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2945 - baseaddr);
2946 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2947 - baseaddr);
2948 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2949 }
2950 }
2951
2952 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2953 &per_bfd->obstack);
2954 }
2955
2956 /* Find a slot in the mapped index INDEX for the object named NAME.
2957 If NAME is found, set *VEC_OUT to point to the CU vector in the
2958 constant pool and return true. If NAME cannot be found, return
2959 false. */
2960
2961 static bool
2962 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2963 offset_type **vec_out)
2964 {
2965 offset_type hash;
2966 offset_type slot, step;
2967 int (*cmp) (const char *, const char *);
2968
2969 gdb::unique_xmalloc_ptr<char> without_params;
2970 if (current_language->la_language == language_cplus
2971 || current_language->la_language == language_fortran
2972 || current_language->la_language == language_d)
2973 {
2974 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2975 not contain any. */
2976
2977 if (strchr (name, '(') != NULL)
2978 {
2979 without_params = cp_remove_params (name);
2980
2981 if (without_params != NULL)
2982 name = without_params.get ();
2983 }
2984 }
2985
2986 /* Index version 4 did not support case insensitive searches. But the
2987 indices for case insensitive languages are built in lowercase, therefore
2988 simulate our NAME being searched is also lowercased. */
2989 hash = mapped_index_string_hash ((index->version == 4
2990 && case_sensitivity == case_sensitive_off
2991 ? 5 : index->version),
2992 name);
2993
2994 slot = hash & (index->symbol_table.size () - 1);
2995 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2996 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2997
2998 for (;;)
2999 {
3000 const char *str;
3001
3002 const auto &bucket = index->symbol_table[slot];
3003 if (bucket.name == 0 && bucket.vec == 0)
3004 return false;
3005
3006 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3007 if (!cmp (name, str))
3008 {
3009 *vec_out = (offset_type *) (index->constant_pool
3010 + MAYBE_SWAP (bucket.vec));
3011 return true;
3012 }
3013
3014 slot = (slot + step) & (index->symbol_table.size () - 1);
3015 }
3016 }
3017
3018 /* A helper function that reads the .gdb_index from BUFFER and fills
3019 in MAP. FILENAME is the name of the file containing the data;
3020 it is used for error reporting. DEPRECATED_OK is true if it is
3021 ok to use deprecated sections.
3022
3023 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3024 out parameters that are filled in with information about the CU and
3025 TU lists in the section.
3026
3027 Returns true if all went well, false otherwise. */
3028
3029 static bool
3030 read_gdb_index_from_buffer (const char *filename,
3031 bool deprecated_ok,
3032 gdb::array_view<const gdb_byte> buffer,
3033 struct mapped_index *map,
3034 const gdb_byte **cu_list,
3035 offset_type *cu_list_elements,
3036 const gdb_byte **types_list,
3037 offset_type *types_list_elements)
3038 {
3039 const gdb_byte *addr = &buffer[0];
3040
3041 /* Version check. */
3042 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3043 /* Versions earlier than 3 emitted every copy of a psymbol. This
3044 causes the index to behave very poorly for certain requests. Version 3
3045 contained incomplete addrmap. So, it seems better to just ignore such
3046 indices. */
3047 if (version < 4)
3048 {
3049 static int warning_printed = 0;
3050 if (!warning_printed)
3051 {
3052 warning (_("Skipping obsolete .gdb_index section in %s."),
3053 filename);
3054 warning_printed = 1;
3055 }
3056 return 0;
3057 }
3058 /* Index version 4 uses a different hash function than index version
3059 5 and later.
3060
3061 Versions earlier than 6 did not emit psymbols for inlined
3062 functions. Using these files will cause GDB not to be able to
3063 set breakpoints on inlined functions by name, so we ignore these
3064 indices unless the user has done
3065 "set use-deprecated-index-sections on". */
3066 if (version < 6 && !deprecated_ok)
3067 {
3068 static int warning_printed = 0;
3069 if (!warning_printed)
3070 {
3071 warning (_("\
3072 Skipping deprecated .gdb_index section in %s.\n\
3073 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3074 to use the section anyway."),
3075 filename);
3076 warning_printed = 1;
3077 }
3078 return 0;
3079 }
3080 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3081 of the TU (for symbols coming from TUs),
3082 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3083 Plus gold-generated indices can have duplicate entries for global symbols,
3084 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3085 These are just performance bugs, and we can't distinguish gdb-generated
3086 indices from gold-generated ones, so issue no warning here. */
3087
3088 /* Indexes with higher version than the one supported by GDB may be no
3089 longer backward compatible. */
3090 if (version > 8)
3091 return 0;
3092
3093 map->version = version;
3094
3095 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3096
3097 int i = 0;
3098 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3099 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3100 / 8);
3101 ++i;
3102
3103 *types_list = addr + MAYBE_SWAP (metadata[i]);
3104 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3105 - MAYBE_SWAP (metadata[i]))
3106 / 8);
3107 ++i;
3108
3109 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3110 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3111 map->address_table
3112 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3113 ++i;
3114
3115 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3116 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3117 map->symbol_table
3118 = gdb::array_view<mapped_index::symbol_table_slot>
3119 ((mapped_index::symbol_table_slot *) symbol_table,
3120 (mapped_index::symbol_table_slot *) symbol_table_end);
3121
3122 ++i;
3123 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3124
3125 return 1;
3126 }
3127
3128 /* Callback types for dwarf2_read_gdb_index. */
3129
3130 typedef gdb::function_view
3131 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3132 get_gdb_index_contents_ftype;
3133 typedef gdb::function_view
3134 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3135 get_gdb_index_contents_dwz_ftype;
3136
3137 /* Read .gdb_index. If everything went ok, initialize the "quick"
3138 elements of all the CUs and return 1. Otherwise, return 0. */
3139
3140 static int
3141 dwarf2_read_gdb_index
3142 (dwarf2_per_objfile *per_objfile,
3143 get_gdb_index_contents_ftype get_gdb_index_contents,
3144 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3145 {
3146 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3147 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3148 struct dwz_file *dwz;
3149 struct objfile *objfile = per_objfile->objfile;
3150 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
3151
3152 gdb::array_view<const gdb_byte> main_index_contents
3153 = get_gdb_index_contents (objfile, per_bfd);
3154
3155 if (main_index_contents.empty ())
3156 return 0;
3157
3158 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3159 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3160 use_deprecated_index_sections,
3161 main_index_contents, map.get (), &cu_list,
3162 &cu_list_elements, &types_list,
3163 &types_list_elements))
3164 return 0;
3165
3166 /* Don't use the index if it's empty. */
3167 if (map->symbol_table.empty ())
3168 return 0;
3169
3170 /* If there is a .dwz file, read it so we can get its CU list as
3171 well. */
3172 dwz = dwarf2_get_dwz_file (per_bfd);
3173 if (dwz != NULL)
3174 {
3175 struct mapped_index dwz_map;
3176 const gdb_byte *dwz_types_ignore;
3177 offset_type dwz_types_elements_ignore;
3178
3179 gdb::array_view<const gdb_byte> dwz_index_content
3180 = get_gdb_index_contents_dwz (objfile, dwz);
3181
3182 if (dwz_index_content.empty ())
3183 return 0;
3184
3185 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3186 1, dwz_index_content, &dwz_map,
3187 &dwz_list, &dwz_list_elements,
3188 &dwz_types_ignore,
3189 &dwz_types_elements_ignore))
3190 {
3191 warning (_("could not read '.gdb_index' section from %s; skipping"),
3192 bfd_get_filename (dwz->dwz_bfd.get ()));
3193 return 0;
3194 }
3195 }
3196
3197 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3198 dwz_list_elements);
3199
3200 if (types_list_elements)
3201 {
3202 /* We can only handle a single .debug_types when we have an
3203 index. */
3204 if (per_bfd->types.size () != 1)
3205 return 0;
3206
3207 dwarf2_section_info *section = &per_bfd->types[0];
3208
3209 create_signatured_type_table_from_index (per_bfd, section, types_list,
3210 types_list_elements);
3211 }
3212
3213 create_addrmap_from_index (per_objfile, map.get ());
3214
3215 per_bfd->index_table = std::move (map);
3216 per_bfd->using_index = 1;
3217 per_bfd->quick_file_names_table =
3218 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3219
3220 return 1;
3221 }
3222
3223 /* die_reader_func for dw2_get_file_names. */
3224
3225 static void
3226 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3227 struct die_info *comp_unit_die)
3228 {
3229 struct dwarf2_cu *cu = reader->cu;
3230 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3231 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3232 struct dwarf2_per_cu_data *lh_cu;
3233 struct attribute *attr;
3234 void **slot;
3235 struct quick_file_names *qfn;
3236
3237 gdb_assert (! this_cu->is_debug_types);
3238
3239 /* Our callers never want to match partial units -- instead they
3240 will match the enclosing full CU. */
3241 if (comp_unit_die->tag == DW_TAG_partial_unit)
3242 {
3243 this_cu->v.quick->no_file_data = 1;
3244 return;
3245 }
3246
3247 lh_cu = this_cu;
3248 slot = NULL;
3249
3250 line_header_up lh;
3251 sect_offset line_offset {};
3252
3253 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3254 if (attr != nullptr && attr->form_is_unsigned ())
3255 {
3256 struct quick_file_names find_entry;
3257
3258 line_offset = (sect_offset) attr->as_unsigned ();
3259
3260 /* We may have already read in this line header (TU line header sharing).
3261 If we have we're done. */
3262 find_entry.hash.dwo_unit = cu->dwo_unit;
3263 find_entry.hash.line_sect_off = line_offset;
3264 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
3265 &find_entry, INSERT);
3266 if (*slot != NULL)
3267 {
3268 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3269 return;
3270 }
3271
3272 lh = dwarf_decode_line_header (line_offset, cu);
3273 }
3274 if (lh == NULL)
3275 {
3276 lh_cu->v.quick->no_file_data = 1;
3277 return;
3278 }
3279
3280 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
3281 qfn->hash.dwo_unit = cu->dwo_unit;
3282 qfn->hash.line_sect_off = line_offset;
3283 gdb_assert (slot != NULL);
3284 *slot = qfn;
3285
3286 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3287
3288 int offset = 0;
3289 if (strcmp (fnd.name, "<unknown>") != 0)
3290 ++offset;
3291
3292 qfn->num_file_names = offset + lh->file_names_size ();
3293 qfn->file_names =
3294 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
3295 qfn->num_file_names);
3296 if (offset != 0)
3297 qfn->file_names[0] = xstrdup (fnd.name);
3298 for (int i = 0; i < lh->file_names_size (); ++i)
3299 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3300 fnd.comp_dir).release ();
3301 qfn->real_names = NULL;
3302
3303 lh_cu->v.quick->file_names = qfn;
3304 }
3305
3306 /* A helper for the "quick" functions which attempts to read the line
3307 table for THIS_CU. */
3308
3309 static struct quick_file_names *
3310 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3311 dwarf2_per_objfile *per_objfile)
3312 {
3313 /* This should never be called for TUs. */
3314 gdb_assert (! this_cu->is_debug_types);
3315 /* Nor type unit groups. */
3316 gdb_assert (! this_cu->type_unit_group_p ());
3317
3318 if (this_cu->v.quick->file_names != NULL)
3319 return this_cu->v.quick->file_names;
3320 /* If we know there is no line data, no point in looking again. */
3321 if (this_cu->v.quick->no_file_data)
3322 return NULL;
3323
3324 cutu_reader reader (this_cu, per_objfile);
3325 if (!reader.dummy_p)
3326 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
3327
3328 if (this_cu->v.quick->no_file_data)
3329 return NULL;
3330 return this_cu->v.quick->file_names;
3331 }
3332
3333 /* A helper for the "quick" functions which computes and caches the
3334 real path for a given file name from the line table. */
3335
3336 static const char *
3337 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3338 struct quick_file_names *qfn, int index)
3339 {
3340 if (qfn->real_names == NULL)
3341 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3342 qfn->num_file_names, const char *);
3343
3344 if (qfn->real_names[index] == NULL)
3345 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3346
3347 return qfn->real_names[index];
3348 }
3349
3350 struct symtab *
3351 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
3352 {
3353 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3354 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3355 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3356
3357 if (cust == NULL)
3358 return NULL;
3359
3360 return compunit_primary_filetab (cust);
3361 }
3362
3363 /* Traversal function for dw2_forget_cached_source_info. */
3364
3365 static int
3366 dw2_free_cached_file_names (void **slot, void *info)
3367 {
3368 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3369
3370 if (file_data->real_names)
3371 {
3372 int i;
3373
3374 for (i = 0; i < file_data->num_file_names; ++i)
3375 {
3376 xfree ((void*) file_data->real_names[i]);
3377 file_data->real_names[i] = NULL;
3378 }
3379 }
3380
3381 return 1;
3382 }
3383
3384 void
3385 dwarf2_base_index_functions::forget_cached_source_info
3386 (struct objfile *objfile)
3387 {
3388 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3389
3390 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
3391 dw2_free_cached_file_names, NULL);
3392 }
3393
3394 /* Helper function for dw2_map_symtabs_matching_filename that expands
3395 the symtabs and calls the iterator. */
3396
3397 static int
3398 dw2_map_expand_apply (struct objfile *objfile,
3399 struct dwarf2_per_cu_data *per_cu,
3400 const char *name, const char *real_path,
3401 gdb::function_view<bool (symtab *)> callback)
3402 {
3403 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3404
3405 /* Don't visit already-expanded CUs. */
3406 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3407 if (per_objfile->symtab_set_p (per_cu))
3408 return 0;
3409
3410 /* This may expand more than one symtab, and we want to iterate over
3411 all of them. */
3412 dw2_instantiate_symtab (per_cu, per_objfile, false);
3413
3414 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3415 last_made, callback);
3416 }
3417
3418 /* Implementation of the map_symtabs_matching_filename method. */
3419
3420 bool
3421 dwarf2_base_index_functions::map_symtabs_matching_filename
3422 (struct objfile *objfile, const char *name, const char *real_path,
3423 gdb::function_view<bool (symtab *)> callback)
3424 {
3425 const char *name_basename = lbasename (name);
3426 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3427
3428 /* The rule is CUs specify all the files, including those used by
3429 any TU, so there's no need to scan TUs here. */
3430
3431 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3432 {
3433 /* We only need to look at symtabs not already expanded. */
3434 if (per_objfile->symtab_set_p (per_cu))
3435 continue;
3436
3437 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3438 if (file_data == NULL)
3439 continue;
3440
3441 for (int j = 0; j < file_data->num_file_names; ++j)
3442 {
3443 const char *this_name = file_data->file_names[j];
3444 const char *this_real_name;
3445
3446 if (compare_filenames_for_search (this_name, name))
3447 {
3448 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3449 callback))
3450 return true;
3451 continue;
3452 }
3453
3454 /* Before we invoke realpath, which can get expensive when many
3455 files are involved, do a quick comparison of the basenames. */
3456 if (! basenames_may_differ
3457 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3458 continue;
3459
3460 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
3461 if (compare_filenames_for_search (this_real_name, name))
3462 {
3463 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3464 callback))
3465 return true;
3466 continue;
3467 }
3468
3469 if (real_path != NULL)
3470 {
3471 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3472 gdb_assert (IS_ABSOLUTE_PATH (name));
3473 if (this_real_name != NULL
3474 && FILENAME_CMP (real_path, this_real_name) == 0)
3475 {
3476 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3477 callback))
3478 return true;
3479 continue;
3480 }
3481 }
3482 }
3483 }
3484
3485 return false;
3486 }
3487
3488 /* Struct used to manage iterating over all CUs looking for a symbol. */
3489
3490 struct dw2_symtab_iterator
3491 {
3492 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3493 dwarf2_per_objfile *per_objfile;
3494 /* If set, only look for symbols that match that block. Valid values are
3495 GLOBAL_BLOCK and STATIC_BLOCK. */
3496 gdb::optional<block_enum> block_index;
3497 /* The kind of symbol we're looking for. */
3498 domain_enum domain;
3499 /* The list of CUs from the index entry of the symbol,
3500 or NULL if not found. */
3501 offset_type *vec;
3502 /* The next element in VEC to look at. */
3503 int next;
3504 /* The number of elements in VEC, or zero if there is no match. */
3505 int length;
3506 /* Have we seen a global version of the symbol?
3507 If so we can ignore all further global instances.
3508 This is to work around gold/15646, inefficient gold-generated
3509 indices. */
3510 int global_seen;
3511 };
3512
3513 /* Initialize the index symtab iterator ITER, common part. */
3514
3515 static void
3516 dw2_symtab_iter_init_common (struct dw2_symtab_iterator *iter,
3517 dwarf2_per_objfile *per_objfile,
3518 gdb::optional<block_enum> block_index,
3519 domain_enum domain)
3520 {
3521 iter->per_objfile = per_objfile;
3522 iter->block_index = block_index;
3523 iter->domain = domain;
3524 iter->next = 0;
3525 iter->global_seen = 0;
3526 iter->vec = NULL;
3527 iter->length = 0;
3528 }
3529
3530 /* Initialize the index symtab iterator ITER, const char *NAME variant. */
3531
3532 static void
3533 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3534 dwarf2_per_objfile *per_objfile,
3535 gdb::optional<block_enum> block_index,
3536 domain_enum domain,
3537 const char *name)
3538 {
3539 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3540
3541 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3542 /* index is NULL if OBJF_READNOW. */
3543 if (index == NULL)
3544 return;
3545
3546 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3547 iter->length = MAYBE_SWAP (*iter->vec);
3548 }
3549
3550 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3551
3552 static void
3553 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3554 dwarf2_per_objfile *per_objfile,
3555 gdb::optional<block_enum> block_index,
3556 domain_enum domain, offset_type namei)
3557 {
3558 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3559
3560 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3561 /* index is NULL if OBJF_READNOW. */
3562 if (index == NULL)
3563 return;
3564
3565 gdb_assert (!index->symbol_name_slot_invalid (namei));
3566 const auto &bucket = index->symbol_table[namei];
3567
3568 iter->vec = (offset_type *) (index->constant_pool
3569 + MAYBE_SWAP (bucket.vec));
3570 iter->length = MAYBE_SWAP (*iter->vec);
3571 }
3572
3573 /* Return the next matching CU or NULL if there are no more. */
3574
3575 static struct dwarf2_per_cu_data *
3576 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3577 {
3578 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3579
3580 for ( ; iter->next < iter->length; ++iter->next)
3581 {
3582 offset_type cu_index_and_attrs =
3583 MAYBE_SWAP (iter->vec[iter->next + 1]);
3584 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3585 gdb_index_symbol_kind symbol_kind =
3586 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3587 /* Only check the symbol attributes if they're present.
3588 Indices prior to version 7 don't record them,
3589 and indices >= 7 may elide them for certain symbols
3590 (gold does this). */
3591 int attrs_valid =
3592 (per_objfile->per_bfd->index_table->version >= 7
3593 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3594
3595 /* Don't crash on bad data. */
3596 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3597 + per_objfile->per_bfd->all_type_units.size ()))
3598 {
3599 complaint (_(".gdb_index entry has bad CU index"
3600 " [in module %s]"), objfile_name (per_objfile->objfile));
3601 continue;
3602 }
3603
3604 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3605
3606 /* Skip if already read in. */
3607 if (per_objfile->symtab_set_p (per_cu))
3608 continue;
3609
3610 /* Check static vs global. */
3611 if (attrs_valid)
3612 {
3613 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3614
3615 if (iter->block_index.has_value ())
3616 {
3617 bool want_static = *iter->block_index == STATIC_BLOCK;
3618
3619 if (is_static != want_static)
3620 continue;
3621 }
3622
3623 /* Work around gold/15646. */
3624 if (!is_static
3625 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3626 {
3627 if (iter->global_seen)
3628 continue;
3629
3630 iter->global_seen = 1;
3631 }
3632 }
3633
3634 /* Only check the symbol's kind if it has one. */
3635 if (attrs_valid)
3636 {
3637 switch (iter->domain)
3638 {
3639 case VAR_DOMAIN:
3640 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3641 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3642 /* Some types are also in VAR_DOMAIN. */
3643 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3644 continue;
3645 break;
3646 case STRUCT_DOMAIN:
3647 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3648 continue;
3649 break;
3650 case LABEL_DOMAIN:
3651 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3652 continue;
3653 break;
3654 case MODULE_DOMAIN:
3655 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3656 continue;
3657 break;
3658 default:
3659 break;
3660 }
3661 }
3662
3663 ++iter->next;
3664 return per_cu;
3665 }
3666
3667 return NULL;
3668 }
3669
3670 struct compunit_symtab *
3671 dwarf2_gdb_index::lookup_symbol (struct objfile *objfile,
3672 block_enum block_index,
3673 const char *name, domain_enum domain)
3674 {
3675 struct compunit_symtab *stab_best = NULL;
3676 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3677
3678 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3679
3680 struct dw2_symtab_iterator iter;
3681 struct dwarf2_per_cu_data *per_cu;
3682
3683 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
3684
3685 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3686 {
3687 struct symbol *sym, *with_opaque = NULL;
3688 struct compunit_symtab *stab
3689 = dw2_instantiate_symtab (per_cu, per_objfile, false);
3690 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3691 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3692
3693 sym = block_find_symbol (block, name, domain,
3694 block_find_non_opaque_type_preferred,
3695 &with_opaque);
3696
3697 /* Some caution must be observed with overloaded functions
3698 and methods, since the index will not contain any overload
3699 information (but NAME might contain it). */
3700
3701 if (sym != NULL
3702 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3703 return stab;
3704 if (with_opaque != NULL
3705 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3706 stab_best = stab;
3707
3708 /* Keep looking through other CUs. */
3709 }
3710
3711 return stab_best;
3712 }
3713
3714 void
3715 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3716 bool print_bcache)
3717 {
3718 if (print_bcache)
3719 return;
3720
3721 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3722 int total = (per_objfile->per_bfd->all_comp_units.size ()
3723 + per_objfile->per_bfd->all_type_units.size ());
3724 int count = 0;
3725
3726 for (int i = 0; i < total; ++i)
3727 {
3728 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3729
3730 if (!per_objfile->symtab_set_p (per_cu))
3731 ++count;
3732 }
3733 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3734 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3735 }
3736
3737 /* This dumps minimal information about the index.
3738 It is called via "mt print objfiles".
3739 One use is to verify .gdb_index has been loaded by the
3740 gdb.dwarf2/gdb-index.exp testcase. */
3741
3742 void
3743 dwarf2_gdb_index::dump (struct objfile *objfile)
3744 {
3745 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3746
3747 gdb_assert (per_objfile->per_bfd->using_index);
3748 printf_filtered (".gdb_index:");
3749 if (per_objfile->per_bfd->index_table != NULL)
3750 {
3751 printf_filtered (" version %d\n",
3752 per_objfile->per_bfd->index_table->version);
3753 }
3754 else
3755 printf_filtered (" faked for \"readnow\"\n");
3756 printf_filtered ("\n");
3757 }
3758
3759 void
3760 dwarf2_gdb_index::expand_symtabs_for_function (struct objfile *objfile,
3761 const char *func_name)
3762 {
3763 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3764
3765 struct dw2_symtab_iterator iter;
3766 struct dwarf2_per_cu_data *per_cu;
3767
3768 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
3769
3770 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3771 dw2_instantiate_symtab (per_cu, per_objfile, false);
3772
3773 }
3774
3775 void
3776 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
3777 {
3778 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3779 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3780 + per_objfile->per_bfd->all_type_units.size ());
3781
3782 for (int i = 0; i < total_units; ++i)
3783 {
3784 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3785
3786 /* We don't want to directly expand a partial CU, because if we
3787 read it with the wrong language, then assertion failures can
3788 be triggered later on. See PR symtab/23010. So, tell
3789 dw2_instantiate_symtab to skip partial CUs -- any important
3790 partial CU will be read via DW_TAG_imported_unit anyway. */
3791 dw2_instantiate_symtab (per_cu, per_objfile, true);
3792 }
3793 }
3794
3795 void
3796 dwarf2_base_index_functions::expand_symtabs_with_fullname
3797 (struct objfile *objfile, const char *fullname)
3798 {
3799 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3800
3801 /* We don't need to consider type units here.
3802 This is only called for examining code, e.g. expand_line_sal.
3803 There can be an order of magnitude (or more) more type units
3804 than comp units, and we avoid them if we can. */
3805
3806 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3807 {
3808 /* We only need to look at symtabs not already expanded. */
3809 if (per_objfile->symtab_set_p (per_cu))
3810 continue;
3811
3812 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3813 if (file_data == NULL)
3814 continue;
3815
3816 for (int j = 0; j < file_data->num_file_names; ++j)
3817 {
3818 const char *this_fullname = file_data->file_names[j];
3819
3820 if (filename_cmp (this_fullname, fullname) == 0)
3821 {
3822 dw2_instantiate_symtab (per_cu, per_objfile, false);
3823 break;
3824 }
3825 }
3826 }
3827 }
3828
3829 static bool
3830 dw2_expand_symtabs_matching_symbol
3831 (mapped_index_base &index,
3832 const lookup_name_info &lookup_name_in,
3833 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3834 gdb::function_view<bool (offset_type)> match_callback,
3835 dwarf2_per_objfile *per_objfile);
3836
3837 static bool
3838 dw2_expand_symtabs_matching_one
3839 (dwarf2_per_cu_data *per_cu,
3840 dwarf2_per_objfile *per_objfile,
3841 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3842 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3843
3844 void
3845 dwarf2_gdb_index::map_matching_symbols
3846 (struct objfile *objfile,
3847 const lookup_name_info &name, domain_enum domain,
3848 int global,
3849 gdb::function_view<symbol_found_callback_ftype> callback,
3850 symbol_compare_ftype *ordered_compare)
3851 {
3852 /* Used for Ada. */
3853 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3854
3855 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3856
3857 if (per_objfile->per_bfd->index_table != nullptr)
3858 {
3859 mapped_index &index = *per_objfile->per_bfd->index_table;
3860
3861 const char *match_name = name.ada ().lookup_name ().c_str ();
3862 auto matcher = [&] (const char *symname)
3863 {
3864 if (ordered_compare == nullptr)
3865 return true;
3866 return ordered_compare (symname, match_name) == 0;
3867 };
3868
3869 dw2_expand_symtabs_matching_symbol (index, name, matcher,
3870 [&] (offset_type namei)
3871 {
3872 struct dw2_symtab_iterator iter;
3873 struct dwarf2_per_cu_data *per_cu;
3874
3875 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3876 namei);
3877 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3878 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3879 nullptr);
3880 return true;
3881 }, per_objfile);
3882 }
3883 else
3884 {
3885 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3886 proceed assuming all symtabs have been read in. */
3887 }
3888
3889 for (compunit_symtab *cust : objfile->compunits ())
3890 {
3891 const struct block *block;
3892
3893 if (cust == NULL)
3894 continue;
3895 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3896 if (!iterate_over_symbols_terminated (block, name,
3897 domain, callback))
3898 return;
3899 }
3900 }
3901
3902 /* Starting from a search name, return the string that finds the upper
3903 bound of all strings that start with SEARCH_NAME in a sorted name
3904 list. Returns the empty string to indicate that the upper bound is
3905 the end of the list. */
3906
3907 static std::string
3908 make_sort_after_prefix_name (const char *search_name)
3909 {
3910 /* When looking to complete "func", we find the upper bound of all
3911 symbols that start with "func" by looking for where we'd insert
3912 the closest string that would follow "func" in lexicographical
3913 order. Usually, that's "func"-with-last-character-incremented,
3914 i.e. "fund". Mind non-ASCII characters, though. Usually those
3915 will be UTF-8 multi-byte sequences, but we can't be certain.
3916 Especially mind the 0xff character, which is a valid character in
3917 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3918 rule out compilers allowing it in identifiers. Note that
3919 conveniently, strcmp/strcasecmp are specified to compare
3920 characters interpreted as unsigned char. So what we do is treat
3921 the whole string as a base 256 number composed of a sequence of
3922 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3923 to 0, and carries 1 to the following more-significant position.
3924 If the very first character in SEARCH_NAME ends up incremented
3925 and carries/overflows, then the upper bound is the end of the
3926 list. The string after the empty string is also the empty
3927 string.
3928
3929 Some examples of this operation:
3930
3931 SEARCH_NAME => "+1" RESULT
3932
3933 "abc" => "abd"
3934 "ab\xff" => "ac"
3935 "\xff" "a" "\xff" => "\xff" "b"
3936 "\xff" => ""
3937 "\xff\xff" => ""
3938 "" => ""
3939
3940 Then, with these symbols for example:
3941
3942 func
3943 func1
3944 fund
3945
3946 completing "func" looks for symbols between "func" and
3947 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3948 which finds "func" and "func1", but not "fund".
3949
3950 And with:
3951
3952 funcÿ (Latin1 'ÿ' [0xff])
3953 funcÿ1
3954 fund
3955
3956 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3957 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3958
3959 And with:
3960
3961 ÿÿ (Latin1 'ÿ' [0xff])
3962 ÿÿ1
3963
3964 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3965 the end of the list.
3966 */
3967 std::string after = search_name;
3968 while (!after.empty () && (unsigned char) after.back () == 0xff)
3969 after.pop_back ();
3970 if (!after.empty ())
3971 after.back () = (unsigned char) after.back () + 1;
3972 return after;
3973 }
3974
3975 /* See declaration. */
3976
3977 std::pair<std::vector<name_component>::const_iterator,
3978 std::vector<name_component>::const_iterator>
3979 mapped_index_base::find_name_components_bounds
3980 (const lookup_name_info &lookup_name_without_params, language lang,
3981 dwarf2_per_objfile *per_objfile) const
3982 {
3983 auto *name_cmp
3984 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3985
3986 const char *lang_name
3987 = lookup_name_without_params.language_lookup_name (lang);
3988
3989 /* Comparison function object for lower_bound that matches against a
3990 given symbol name. */
3991 auto lookup_compare_lower = [&] (const name_component &elem,
3992 const char *name)
3993 {
3994 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3995 const char *elem_name = elem_qualified + elem.name_offset;
3996 return name_cmp (elem_name, name) < 0;
3997 };
3998
3999 /* Comparison function object for upper_bound that matches against a
4000 given symbol name. */
4001 auto lookup_compare_upper = [&] (const char *name,
4002 const name_component &elem)
4003 {
4004 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
4005 const char *elem_name = elem_qualified + elem.name_offset;
4006 return name_cmp (name, elem_name) < 0;
4007 };
4008
4009 auto begin = this->name_components.begin ();
4010 auto end = this->name_components.end ();
4011
4012 /* Find the lower bound. */
4013 auto lower = [&] ()
4014 {
4015 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
4016 return begin;
4017 else
4018 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
4019 } ();
4020
4021 /* Find the upper bound. */
4022 auto upper = [&] ()
4023 {
4024 if (lookup_name_without_params.completion_mode ())
4025 {
4026 /* In completion mode, we want UPPER to point past all
4027 symbols names that have the same prefix. I.e., with
4028 these symbols, and completing "func":
4029
4030 function << lower bound
4031 function1
4032 other_function << upper bound
4033
4034 We find the upper bound by looking for the insertion
4035 point of "func"-with-last-character-incremented,
4036 i.e. "fund". */
4037 std::string after = make_sort_after_prefix_name (lang_name);
4038 if (after.empty ())
4039 return end;
4040 return std::lower_bound (lower, end, after.c_str (),
4041 lookup_compare_lower);
4042 }
4043 else
4044 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4045 } ();
4046
4047 return {lower, upper};
4048 }
4049
4050 /* See declaration. */
4051
4052 void
4053 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
4054 {
4055 if (!this->name_components.empty ())
4056 return;
4057
4058 this->name_components_casing = case_sensitivity;
4059 auto *name_cmp
4060 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4061
4062 /* The code below only knows how to break apart components of C++
4063 symbol names (and other languages that use '::' as
4064 namespace/module separator) and Ada symbol names. */
4065 auto count = this->symbol_name_count ();
4066 for (offset_type idx = 0; idx < count; idx++)
4067 {
4068 if (this->symbol_name_slot_invalid (idx))
4069 continue;
4070
4071 const char *name = this->symbol_name_at (idx, per_objfile);
4072
4073 /* Add each name component to the name component table. */
4074 unsigned int previous_len = 0;
4075
4076 if (strstr (name, "::") != nullptr)
4077 {
4078 for (unsigned int current_len = cp_find_first_component (name);
4079 name[current_len] != '\0';
4080 current_len += cp_find_first_component (name + current_len))
4081 {
4082 gdb_assert (name[current_len] == ':');
4083 this->name_components.push_back ({previous_len, idx});
4084 /* Skip the '::'. */
4085 current_len += 2;
4086 previous_len = current_len;
4087 }
4088 }
4089 else
4090 {
4091 /* Handle the Ada encoded (aka mangled) form here. */
4092 for (const char *iter = strstr (name, "__");
4093 iter != nullptr;
4094 iter = strstr (iter, "__"))
4095 {
4096 this->name_components.push_back ({previous_len, idx});
4097 iter += 2;
4098 previous_len = iter - name;
4099 }
4100 }
4101
4102 this->name_components.push_back ({previous_len, idx});
4103 }
4104
4105 /* Sort name_components elements by name. */
4106 auto name_comp_compare = [&] (const name_component &left,
4107 const name_component &right)
4108 {
4109 const char *left_qualified
4110 = this->symbol_name_at (left.idx, per_objfile);
4111 const char *right_qualified
4112 = this->symbol_name_at (right.idx, per_objfile);
4113
4114 const char *left_name = left_qualified + left.name_offset;
4115 const char *right_name = right_qualified + right.name_offset;
4116
4117 return name_cmp (left_name, right_name) < 0;
4118 };
4119
4120 std::sort (this->name_components.begin (),
4121 this->name_components.end (),
4122 name_comp_compare);
4123 }
4124
4125 /* Helper for dw2_expand_symtabs_matching that works with a
4126 mapped_index_base instead of the containing objfile. This is split
4127 to a separate function in order to be able to unit test the
4128 name_components matching using a mock mapped_index_base. For each
4129 symbol name that matches, calls MATCH_CALLBACK, passing it the
4130 symbol's index in the mapped_index_base symbol table. */
4131
4132 static bool
4133 dw2_expand_symtabs_matching_symbol
4134 (mapped_index_base &index,
4135 const lookup_name_info &lookup_name_in,
4136 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4137 gdb::function_view<bool (offset_type)> match_callback,
4138 dwarf2_per_objfile *per_objfile)
4139 {
4140 lookup_name_info lookup_name_without_params
4141 = lookup_name_in.make_ignore_params ();
4142
4143 /* Build the symbol name component sorted vector, if we haven't
4144 yet. */
4145 index.build_name_components (per_objfile);
4146
4147 /* The same symbol may appear more than once in the range though.
4148 E.g., if we're looking for symbols that complete "w", and we have
4149 a symbol named "w1::w2", we'll find the two name components for
4150 that same symbol in the range. To be sure we only call the
4151 callback once per symbol, we first collect the symbol name
4152 indexes that matched in a temporary vector and ignore
4153 duplicates. */
4154 std::vector<offset_type> matches;
4155
4156 struct name_and_matcher
4157 {
4158 symbol_name_matcher_ftype *matcher;
4159 const char *name;
4160
4161 bool operator== (const name_and_matcher &other) const
4162 {
4163 return matcher == other.matcher && strcmp (name, other.name) == 0;
4164 }
4165 };
4166
4167 /* A vector holding all the different symbol name matchers, for all
4168 languages. */
4169 std::vector<name_and_matcher> matchers;
4170
4171 for (int i = 0; i < nr_languages; i++)
4172 {
4173 enum language lang_e = (enum language) i;
4174
4175 const language_defn *lang = language_def (lang_e);
4176 symbol_name_matcher_ftype *name_matcher
4177 = lang->get_symbol_name_matcher (lookup_name_without_params);
4178
4179 name_and_matcher key {
4180 name_matcher,
4181 lookup_name_without_params.language_lookup_name (lang_e)
4182 };
4183
4184 /* Don't insert the same comparison routine more than once.
4185 Note that we do this linear walk. This is not a problem in
4186 practice because the number of supported languages is
4187 low. */
4188 if (std::find (matchers.begin (), matchers.end (), key)
4189 != matchers.end ())
4190 continue;
4191 matchers.push_back (std::move (key));
4192
4193 auto bounds
4194 = index.find_name_components_bounds (lookup_name_without_params,
4195 lang_e, per_objfile);
4196
4197 /* Now for each symbol name in range, check to see if we have a name
4198 match, and if so, call the MATCH_CALLBACK callback. */
4199
4200 for (; bounds.first != bounds.second; ++bounds.first)
4201 {
4202 const char *qualified
4203 = index.symbol_name_at (bounds.first->idx, per_objfile);
4204
4205 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4206 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4207 continue;
4208
4209 matches.push_back (bounds.first->idx);
4210 }
4211 }
4212
4213 std::sort (matches.begin (), matches.end ());
4214
4215 /* Finally call the callback, once per match. */
4216 ULONGEST prev = -1;
4217 bool result = true;
4218 for (offset_type idx : matches)
4219 {
4220 if (prev != idx)
4221 {
4222 if (!match_callback (idx))
4223 {
4224 result = false;
4225 break;
4226 }
4227 prev = idx;
4228 }
4229 }
4230
4231 /* Above we use a type wider than idx's for 'prev', since 0 and
4232 (offset_type)-1 are both possible values. */
4233 static_assert (sizeof (prev) > sizeof (offset_type), "");
4234
4235 return result;
4236 }
4237
4238 #if GDB_SELF_TEST
4239
4240 namespace selftests { namespace dw2_expand_symtabs_matching {
4241
4242 /* A mock .gdb_index/.debug_names-like name index table, enough to
4243 exercise dw2_expand_symtabs_matching_symbol, which works with the
4244 mapped_index_base interface. Builds an index from the symbol list
4245 passed as parameter to the constructor. */
4246 class mock_mapped_index : public mapped_index_base
4247 {
4248 public:
4249 mock_mapped_index (gdb::array_view<const char *> symbols)
4250 : m_symbol_table (symbols)
4251 {}
4252
4253 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4254
4255 /* Return the number of names in the symbol table. */
4256 size_t symbol_name_count () const override
4257 {
4258 return m_symbol_table.size ();
4259 }
4260
4261 /* Get the name of the symbol at IDX in the symbol table. */
4262 const char *symbol_name_at
4263 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
4264 {
4265 return m_symbol_table[idx];
4266 }
4267
4268 private:
4269 gdb::array_view<const char *> m_symbol_table;
4270 };
4271
4272 /* Convenience function that converts a NULL pointer to a "<null>"
4273 string, to pass to print routines. */
4274
4275 static const char *
4276 string_or_null (const char *str)
4277 {
4278 return str != NULL ? str : "<null>";
4279 }
4280
4281 /* Check if a lookup_name_info built from
4282 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4283 index. EXPECTED_LIST is the list of expected matches, in expected
4284 matching order. If no match expected, then an empty list is
4285 specified. Returns true on success. On failure prints a warning
4286 indicating the file:line that failed, and returns false. */
4287
4288 static bool
4289 check_match (const char *file, int line,
4290 mock_mapped_index &mock_index,
4291 const char *name, symbol_name_match_type match_type,
4292 bool completion_mode,
4293 std::initializer_list<const char *> expected_list,
4294 dwarf2_per_objfile *per_objfile)
4295 {
4296 lookup_name_info lookup_name (name, match_type, completion_mode);
4297
4298 bool matched = true;
4299
4300 auto mismatch = [&] (const char *expected_str,
4301 const char *got)
4302 {
4303 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4304 "expected=\"%s\", got=\"%s\"\n"),
4305 file, line,
4306 (match_type == symbol_name_match_type::FULL
4307 ? "FULL" : "WILD"),
4308 name, string_or_null (expected_str), string_or_null (got));
4309 matched = false;
4310 };
4311
4312 auto expected_it = expected_list.begin ();
4313 auto expected_end = expected_list.end ();
4314
4315 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4316 nullptr,
4317 [&] (offset_type idx)
4318 {
4319 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
4320 const char *expected_str
4321 = expected_it == expected_end ? NULL : *expected_it++;
4322
4323 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4324 mismatch (expected_str, matched_name);
4325 return true;
4326 }, per_objfile);
4327
4328 const char *expected_str
4329 = expected_it == expected_end ? NULL : *expected_it++;
4330 if (expected_str != NULL)
4331 mismatch (expected_str, NULL);
4332
4333 return matched;
4334 }
4335
4336 /* The symbols added to the mock mapped_index for testing (in
4337 canonical form). */
4338 static const char *test_symbols[] = {
4339 "function",
4340 "std::bar",
4341 "std::zfunction",
4342 "std::zfunction2",
4343 "w1::w2",
4344 "ns::foo<char*>",
4345 "ns::foo<int>",
4346 "ns::foo<long>",
4347 "ns2::tmpl<int>::foo2",
4348 "(anonymous namespace)::A::B::C",
4349
4350 /* These are used to check that the increment-last-char in the
4351 matching algorithm for completion doesn't match "t1_fund" when
4352 completing "t1_func". */
4353 "t1_func",
4354 "t1_func1",
4355 "t1_fund",
4356 "t1_fund1",
4357
4358 /* A UTF-8 name with multi-byte sequences to make sure that
4359 cp-name-parser understands this as a single identifier ("função"
4360 is "function" in PT). */
4361 u8"u8função",
4362
4363 /* \377 (0xff) is Latin1 'ÿ'. */
4364 "yfunc\377",
4365
4366 /* \377 (0xff) is Latin1 'ÿ'. */
4367 "\377",
4368 "\377\377123",
4369
4370 /* A name with all sorts of complications. Starts with "z" to make
4371 it easier for the completion tests below. */
4372 #define Z_SYM_NAME \
4373 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4374 "::tuple<(anonymous namespace)::ui*, " \
4375 "std::default_delete<(anonymous namespace)::ui>, void>"
4376
4377 Z_SYM_NAME
4378 };
4379
4380 /* Returns true if the mapped_index_base::find_name_component_bounds
4381 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4382 in completion mode. */
4383
4384 static bool
4385 check_find_bounds_finds (mapped_index_base &index,
4386 const char *search_name,
4387 gdb::array_view<const char *> expected_syms,
4388 dwarf2_per_objfile *per_objfile)
4389 {
4390 lookup_name_info lookup_name (search_name,
4391 symbol_name_match_type::FULL, true);
4392
4393 auto bounds = index.find_name_components_bounds (lookup_name,
4394 language_cplus,
4395 per_objfile);
4396
4397 size_t distance = std::distance (bounds.first, bounds.second);
4398 if (distance != expected_syms.size ())
4399 return false;
4400
4401 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4402 {
4403 auto nc_elem = bounds.first + exp_elem;
4404 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
4405 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4406 return false;
4407 }
4408
4409 return true;
4410 }
4411
4412 /* Test the lower-level mapped_index::find_name_component_bounds
4413 method. */
4414
4415 static void
4416 test_mapped_index_find_name_component_bounds ()
4417 {
4418 mock_mapped_index mock_index (test_symbols);
4419
4420 mock_index.build_name_components (NULL /* per_objfile */);
4421
4422 /* Test the lower-level mapped_index::find_name_component_bounds
4423 method in completion mode. */
4424 {
4425 static const char *expected_syms[] = {
4426 "t1_func",
4427 "t1_func1",
4428 };
4429
4430 SELF_CHECK (check_find_bounds_finds
4431 (mock_index, "t1_func", expected_syms,
4432 NULL /* per_objfile */));
4433 }
4434
4435 /* Check that the increment-last-char in the name matching algorithm
4436 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4437 {
4438 static const char *expected_syms1[] = {
4439 "\377",
4440 "\377\377123",
4441 };
4442 SELF_CHECK (check_find_bounds_finds
4443 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
4444
4445 static const char *expected_syms2[] = {
4446 "\377\377123",
4447 };
4448 SELF_CHECK (check_find_bounds_finds
4449 (mock_index, "\377\377", expected_syms2,
4450 NULL /* per_objfile */));
4451 }
4452 }
4453
4454 /* Test dw2_expand_symtabs_matching_symbol. */
4455
4456 static void
4457 test_dw2_expand_symtabs_matching_symbol ()
4458 {
4459 mock_mapped_index mock_index (test_symbols);
4460
4461 /* We let all tests run until the end even if some fails, for debug
4462 convenience. */
4463 bool any_mismatch = false;
4464
4465 /* Create the expected symbols list (an initializer_list). Needed
4466 because lists have commas, and we need to pass them to CHECK,
4467 which is a macro. */
4468 #define EXPECT(...) { __VA_ARGS__ }
4469
4470 /* Wrapper for check_match that passes down the current
4471 __FILE__/__LINE__. */
4472 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4473 any_mismatch |= !check_match (__FILE__, __LINE__, \
4474 mock_index, \
4475 NAME, MATCH_TYPE, COMPLETION_MODE, \
4476 EXPECTED_LIST, NULL)
4477
4478 /* Identity checks. */
4479 for (const char *sym : test_symbols)
4480 {
4481 /* Should be able to match all existing symbols. */
4482 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4483 EXPECT (sym));
4484
4485 /* Should be able to match all existing symbols with
4486 parameters. */
4487 std::string with_params = std::string (sym) + "(int)";
4488 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4489 EXPECT (sym));
4490
4491 /* Should be able to match all existing symbols with
4492 parameters and qualifiers. */
4493 with_params = std::string (sym) + " ( int ) const";
4494 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4495 EXPECT (sym));
4496
4497 /* This should really find sym, but cp-name-parser.y doesn't
4498 know about lvalue/rvalue qualifiers yet. */
4499 with_params = std::string (sym) + " ( int ) &&";
4500 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4501 {});
4502 }
4503
4504 /* Check that the name matching algorithm for completion doesn't get
4505 confused with Latin1 'ÿ' / 0xff. */
4506 {
4507 static const char str[] = "\377";
4508 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4509 EXPECT ("\377", "\377\377123"));
4510 }
4511
4512 /* Check that the increment-last-char in the matching algorithm for
4513 completion doesn't match "t1_fund" when completing "t1_func". */
4514 {
4515 static const char str[] = "t1_func";
4516 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4517 EXPECT ("t1_func", "t1_func1"));
4518 }
4519
4520 /* Check that completion mode works at each prefix of the expected
4521 symbol name. */
4522 {
4523 static const char str[] = "function(int)";
4524 size_t len = strlen (str);
4525 std::string lookup;
4526
4527 for (size_t i = 1; i < len; i++)
4528 {
4529 lookup.assign (str, i);
4530 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4531 EXPECT ("function"));
4532 }
4533 }
4534
4535 /* While "w" is a prefix of both components, the match function
4536 should still only be called once. */
4537 {
4538 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4539 EXPECT ("w1::w2"));
4540 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4541 EXPECT ("w1::w2"));
4542 }
4543
4544 /* Same, with a "complicated" symbol. */
4545 {
4546 static const char str[] = Z_SYM_NAME;
4547 size_t len = strlen (str);
4548 std::string lookup;
4549
4550 for (size_t i = 1; i < len; i++)
4551 {
4552 lookup.assign (str, i);
4553 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4554 EXPECT (Z_SYM_NAME));
4555 }
4556 }
4557
4558 /* In FULL mode, an incomplete symbol doesn't match. */
4559 {
4560 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4561 {});
4562 }
4563
4564 /* A complete symbol with parameters matches any overload, since the
4565 index has no overload info. */
4566 {
4567 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4568 EXPECT ("std::zfunction", "std::zfunction2"));
4569 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4570 EXPECT ("std::zfunction", "std::zfunction2"));
4571 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4572 EXPECT ("std::zfunction", "std::zfunction2"));
4573 }
4574
4575 /* Check that whitespace is ignored appropriately. A symbol with a
4576 template argument list. */
4577 {
4578 static const char expected[] = "ns::foo<int>";
4579 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4580 EXPECT (expected));
4581 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4582 EXPECT (expected));
4583 }
4584
4585 /* Check that whitespace is ignored appropriately. A symbol with a
4586 template argument list that includes a pointer. */
4587 {
4588 static const char expected[] = "ns::foo<char*>";
4589 /* Try both completion and non-completion modes. */
4590 static const bool completion_mode[2] = {false, true};
4591 for (size_t i = 0; i < 2; i++)
4592 {
4593 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4594 completion_mode[i], EXPECT (expected));
4595 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4596 completion_mode[i], EXPECT (expected));
4597
4598 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4599 completion_mode[i], EXPECT (expected));
4600 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4601 completion_mode[i], EXPECT (expected));
4602 }
4603 }
4604
4605 {
4606 /* Check method qualifiers are ignored. */
4607 static const char expected[] = "ns::foo<char*>";
4608 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4609 symbol_name_match_type::FULL, true, EXPECT (expected));
4610 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4611 symbol_name_match_type::FULL, true, EXPECT (expected));
4612 CHECK_MATCH ("foo < char * > ( int ) const",
4613 symbol_name_match_type::WILD, true, EXPECT (expected));
4614 CHECK_MATCH ("foo < char * > ( int ) &&",
4615 symbol_name_match_type::WILD, true, EXPECT (expected));
4616 }
4617
4618 /* Test lookup names that don't match anything. */
4619 {
4620 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4621 {});
4622
4623 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4624 {});
4625 }
4626
4627 /* Some wild matching tests, exercising "(anonymous namespace)",
4628 which should not be confused with a parameter list. */
4629 {
4630 static const char *syms[] = {
4631 "A::B::C",
4632 "B::C",
4633 "C",
4634 "A :: B :: C ( int )",
4635 "B :: C ( int )",
4636 "C ( int )",
4637 };
4638
4639 for (const char *s : syms)
4640 {
4641 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4642 EXPECT ("(anonymous namespace)::A::B::C"));
4643 }
4644 }
4645
4646 {
4647 static const char expected[] = "ns2::tmpl<int>::foo2";
4648 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4649 EXPECT (expected));
4650 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4651 EXPECT (expected));
4652 }
4653
4654 SELF_CHECK (!any_mismatch);
4655
4656 #undef EXPECT
4657 #undef CHECK_MATCH
4658 }
4659
4660 static void
4661 run_test ()
4662 {
4663 test_mapped_index_find_name_component_bounds ();
4664 test_dw2_expand_symtabs_matching_symbol ();
4665 }
4666
4667 }} // namespace selftests::dw2_expand_symtabs_matching
4668
4669 #endif /* GDB_SELF_TEST */
4670
4671 /* If FILE_MATCHER is NULL or if PER_CU has
4672 dwarf2_per_cu_quick_data::MARK set (see
4673 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4674 EXPANSION_NOTIFY on it. */
4675
4676 static bool
4677 dw2_expand_symtabs_matching_one
4678 (dwarf2_per_cu_data *per_cu,
4679 dwarf2_per_objfile *per_objfile,
4680 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4681 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4682 {
4683 if (file_matcher == NULL || per_cu->v.quick->mark)
4684 {
4685 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4686
4687 compunit_symtab *symtab
4688 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4689 gdb_assert (symtab != nullptr);
4690
4691 if (expansion_notify != NULL && symtab_was_null)
4692 return expansion_notify (symtab);
4693 }
4694 return true;
4695 }
4696
4697 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4698 matched, to expand corresponding CUs that were marked. IDX is the
4699 index of the symbol name that matched. */
4700
4701 static bool
4702 dw2_expand_marked_cus
4703 (dwarf2_per_objfile *per_objfile, offset_type idx,
4704 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4705 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4706 block_search_flags search_flags,
4707 search_domain kind)
4708 {
4709 offset_type *vec, vec_len, vec_idx;
4710 bool global_seen = false;
4711 mapped_index &index = *per_objfile->per_bfd->index_table;
4712
4713 vec = (offset_type *) (index.constant_pool
4714 + MAYBE_SWAP (index.symbol_table[idx].vec));
4715 vec_len = MAYBE_SWAP (vec[0]);
4716 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4717 {
4718 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4719 /* This value is only valid for index versions >= 7. */
4720 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4721 gdb_index_symbol_kind symbol_kind =
4722 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4723 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4724 /* Only check the symbol attributes if they're present.
4725 Indices prior to version 7 don't record them,
4726 and indices >= 7 may elide them for certain symbols
4727 (gold does this). */
4728 int attrs_valid =
4729 (index.version >= 7
4730 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4731
4732 /* Work around gold/15646. */
4733 if (attrs_valid
4734 && !is_static
4735 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4736 {
4737 if (global_seen)
4738 continue;
4739
4740 global_seen = true;
4741 }
4742
4743 /* Only check the symbol's kind if it has one. */
4744 if (attrs_valid)
4745 {
4746 if (is_static)
4747 {
4748 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4749 continue;
4750 }
4751 else
4752 {
4753 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4754 continue;
4755 }
4756
4757 switch (kind)
4758 {
4759 case VARIABLES_DOMAIN:
4760 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4761 continue;
4762 break;
4763 case FUNCTIONS_DOMAIN:
4764 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4765 continue;
4766 break;
4767 case TYPES_DOMAIN:
4768 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4769 continue;
4770 break;
4771 case MODULES_DOMAIN:
4772 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4773 continue;
4774 break;
4775 default:
4776 break;
4777 }
4778 }
4779
4780 /* Don't crash on bad data. */
4781 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4782 + per_objfile->per_bfd->all_type_units.size ()))
4783 {
4784 complaint (_(".gdb_index entry has bad CU index"
4785 " [in module %s]"), objfile_name (per_objfile->objfile));
4786 continue;
4787 }
4788
4789 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4790 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4791 expansion_notify))
4792 return false;
4793 }
4794
4795 return true;
4796 }
4797
4798 /* If FILE_MATCHER is non-NULL, set all the
4799 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4800 that match FILE_MATCHER. */
4801
4802 static void
4803 dw_expand_symtabs_matching_file_matcher
4804 (dwarf2_per_objfile *per_objfile,
4805 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4806 {
4807 if (file_matcher == NULL)
4808 return;
4809
4810 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4811 htab_eq_pointer,
4812 NULL, xcalloc, xfree));
4813 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4814 htab_eq_pointer,
4815 NULL, xcalloc, xfree));
4816
4817 /* The rule is CUs specify all the files, including those used by
4818 any TU, so there's no need to scan TUs here. */
4819
4820 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4821 {
4822 QUIT;
4823
4824 per_cu->v.quick->mark = 0;
4825
4826 /* We only need to look at symtabs not already expanded. */
4827 if (per_objfile->symtab_set_p (per_cu))
4828 continue;
4829
4830 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4831 if (file_data == NULL)
4832 continue;
4833
4834 if (htab_find (visited_not_found.get (), file_data) != NULL)
4835 continue;
4836 else if (htab_find (visited_found.get (), file_data) != NULL)
4837 {
4838 per_cu->v.quick->mark = 1;
4839 continue;
4840 }
4841
4842 for (int j = 0; j < file_data->num_file_names; ++j)
4843 {
4844 const char *this_real_name;
4845
4846 if (file_matcher (file_data->file_names[j], false))
4847 {
4848 per_cu->v.quick->mark = 1;
4849 break;
4850 }
4851
4852 /* Before we invoke realpath, which can get expensive when many
4853 files are involved, do a quick comparison of the basenames. */
4854 if (!basenames_may_differ
4855 && !file_matcher (lbasename (file_data->file_names[j]),
4856 true))
4857 continue;
4858
4859 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4860 if (file_matcher (this_real_name, false))
4861 {
4862 per_cu->v.quick->mark = 1;
4863 break;
4864 }
4865 }
4866
4867 void **slot = htab_find_slot (per_cu->v.quick->mark
4868 ? visited_found.get ()
4869 : visited_not_found.get (),
4870 file_data, INSERT);
4871 *slot = file_data;
4872 }
4873 }
4874
4875 bool
4876 dwarf2_gdb_index::expand_symtabs_matching
4877 (struct objfile *objfile,
4878 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4879 const lookup_name_info *lookup_name,
4880 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4881 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4882 block_search_flags search_flags,
4883 domain_enum domain,
4884 enum search_domain kind)
4885 {
4886 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4887
4888 /* index_table is NULL if OBJF_READNOW. */
4889 if (!per_objfile->per_bfd->index_table)
4890 return true;
4891
4892 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4893
4894 if (symbol_matcher == NULL && lookup_name == NULL)
4895 {
4896 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4897 {
4898 QUIT;
4899
4900 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4901 file_matcher,
4902 expansion_notify))
4903 return false;
4904 }
4905 return true;
4906 }
4907
4908 mapped_index &index = *per_objfile->per_bfd->index_table;
4909
4910 bool result
4911 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4912 symbol_matcher,
4913 [&] (offset_type idx)
4914 {
4915 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
4916 expansion_notify, search_flags, kind))
4917 return false;
4918 return true;
4919 }, per_objfile);
4920
4921 return result;
4922 }
4923
4924 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4925 symtab. */
4926
4927 static struct compunit_symtab *
4928 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4929 CORE_ADDR pc)
4930 {
4931 int i;
4932
4933 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4934 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4935 return cust;
4936
4937 if (cust->includes == NULL)
4938 return NULL;
4939
4940 for (i = 0; cust->includes[i]; ++i)
4941 {
4942 struct compunit_symtab *s = cust->includes[i];
4943
4944 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4945 if (s != NULL)
4946 return s;
4947 }
4948
4949 return NULL;
4950 }
4951
4952 struct compunit_symtab *
4953 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4954 (struct objfile *objfile,
4955 struct bound_minimal_symbol msymbol,
4956 CORE_ADDR pc,
4957 struct obj_section *section,
4958 int warn_if_readin)
4959 {
4960 struct dwarf2_per_cu_data *data;
4961 struct compunit_symtab *result;
4962
4963 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4964 if (per_objfile->per_bfd->index_addrmap == nullptr)
4965 return NULL;
4966
4967 CORE_ADDR baseaddr = objfile->text_section_offset ();
4968 data = ((struct dwarf2_per_cu_data *)
4969 addrmap_find (per_objfile->per_bfd->index_addrmap,
4970 pc - baseaddr));
4971 if (!data)
4972 return NULL;
4973
4974 if (warn_if_readin && per_objfile->symtab_set_p (data))
4975 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4976 paddress (objfile->arch (), pc));
4977
4978 result = recursively_find_pc_sect_compunit_symtab
4979 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4980
4981 gdb_assert (result != NULL);
4982 return result;
4983 }
4984
4985 void
4986 dwarf2_base_index_functions::map_symbol_filenames
4987 (struct objfile *objfile,
4988 gdb::function_view<symbol_filename_ftype> fun,
4989 bool need_fullname)
4990 {
4991 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4992
4993 if (!per_objfile->per_bfd->filenames_cache)
4994 {
4995 per_objfile->per_bfd->filenames_cache.emplace ();
4996
4997 htab_up visited (htab_create_alloc (10,
4998 htab_hash_pointer, htab_eq_pointer,
4999 NULL, xcalloc, xfree));
5000
5001 /* The rule is CUs specify all the files, including those used
5002 by any TU, so there's no need to scan TUs here. We can
5003 ignore file names coming from already-expanded CUs. */
5004
5005 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5006 {
5007 if (per_objfile->symtab_set_p (per_cu))
5008 {
5009 void **slot = htab_find_slot (visited.get (),
5010 per_cu->v.quick->file_names,
5011 INSERT);
5012
5013 *slot = per_cu->v.quick->file_names;
5014 }
5015 }
5016
5017 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5018 {
5019 /* We only need to look at symtabs not already expanded. */
5020 if (per_objfile->symtab_set_p (per_cu))
5021 continue;
5022
5023 quick_file_names *file_data
5024 = dw2_get_file_names (per_cu, per_objfile);
5025 if (file_data == NULL)
5026 continue;
5027
5028 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
5029 if (*slot)
5030 {
5031 /* Already visited. */
5032 continue;
5033 }
5034 *slot = file_data;
5035
5036 for (int j = 0; j < file_data->num_file_names; ++j)
5037 {
5038 const char *filename = file_data->file_names[j];
5039 per_objfile->per_bfd->filenames_cache->seen (filename);
5040 }
5041 }
5042 }
5043
5044 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
5045 {
5046 gdb::unique_xmalloc_ptr<char> this_real_name;
5047
5048 if (need_fullname)
5049 this_real_name = gdb_realpath (filename);
5050 fun (filename, this_real_name.get ());
5051 });
5052 }
5053
5054 bool
5055 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
5056 {
5057 return true;
5058 }
5059
5060 /* DWARF-5 debug_names reader. */
5061
5062 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5063 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5064
5065 /* A helper function that reads the .debug_names section in SECTION
5066 and fills in MAP. FILENAME is the name of the file containing the
5067 section; it is used for error reporting.
5068
5069 Returns true if all went well, false otherwise. */
5070
5071 static bool
5072 read_debug_names_from_section (struct objfile *objfile,
5073 const char *filename,
5074 struct dwarf2_section_info *section,
5075 mapped_debug_names &map)
5076 {
5077 if (section->empty ())
5078 return false;
5079
5080 /* Older elfutils strip versions could keep the section in the main
5081 executable while splitting it for the separate debug info file. */
5082 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5083 return false;
5084
5085 section->read (objfile);
5086
5087 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
5088
5089 const gdb_byte *addr = section->buffer;
5090
5091 bfd *const abfd = section->get_bfd_owner ();
5092
5093 unsigned int bytes_read;
5094 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5095 addr += bytes_read;
5096
5097 map.dwarf5_is_dwarf64 = bytes_read != 4;
5098 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5099 if (bytes_read + length != section->size)
5100 {
5101 /* There may be multiple per-CU indices. */
5102 warning (_("Section .debug_names in %s length %s does not match "
5103 "section length %s, ignoring .debug_names."),
5104 filename, plongest (bytes_read + length),
5105 pulongest (section->size));
5106 return false;
5107 }
5108
5109 /* The version number. */
5110 uint16_t version = read_2_bytes (abfd, addr);
5111 addr += 2;
5112 if (version != 5)
5113 {
5114 warning (_("Section .debug_names in %s has unsupported version %d, "
5115 "ignoring .debug_names."),
5116 filename, version);
5117 return false;
5118 }
5119
5120 /* Padding. */
5121 uint16_t padding = read_2_bytes (abfd, addr);
5122 addr += 2;
5123 if (padding != 0)
5124 {
5125 warning (_("Section .debug_names in %s has unsupported padding %d, "
5126 "ignoring .debug_names."),
5127 filename, padding);
5128 return false;
5129 }
5130
5131 /* comp_unit_count - The number of CUs in the CU list. */
5132 map.cu_count = read_4_bytes (abfd, addr);
5133 addr += 4;
5134
5135 /* local_type_unit_count - The number of TUs in the local TU
5136 list. */
5137 map.tu_count = read_4_bytes (abfd, addr);
5138 addr += 4;
5139
5140 /* foreign_type_unit_count - The number of TUs in the foreign TU
5141 list. */
5142 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5143 addr += 4;
5144 if (foreign_tu_count != 0)
5145 {
5146 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5147 "ignoring .debug_names."),
5148 filename, static_cast<unsigned long> (foreign_tu_count));
5149 return false;
5150 }
5151
5152 /* bucket_count - The number of hash buckets in the hash lookup
5153 table. */
5154 map.bucket_count = read_4_bytes (abfd, addr);
5155 addr += 4;
5156
5157 /* name_count - The number of unique names in the index. */
5158 map.name_count = read_4_bytes (abfd, addr);
5159 addr += 4;
5160
5161 /* abbrev_table_size - The size in bytes of the abbreviations
5162 table. */
5163 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5164 addr += 4;
5165
5166 /* augmentation_string_size - The size in bytes of the augmentation
5167 string. This value is rounded up to a multiple of 4. */
5168 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5169 addr += 4;
5170 map.augmentation_is_gdb = ((augmentation_string_size
5171 == sizeof (dwarf5_augmentation))
5172 && memcmp (addr, dwarf5_augmentation,
5173 sizeof (dwarf5_augmentation)) == 0);
5174 augmentation_string_size += (-augmentation_string_size) & 3;
5175 addr += augmentation_string_size;
5176
5177 /* List of CUs */
5178 map.cu_table_reordered = addr;
5179 addr += map.cu_count * map.offset_size;
5180
5181 /* List of Local TUs */
5182 map.tu_table_reordered = addr;
5183 addr += map.tu_count * map.offset_size;
5184
5185 /* Hash Lookup Table */
5186 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5187 addr += map.bucket_count * 4;
5188 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5189 addr += map.name_count * 4;
5190
5191 /* Name Table */
5192 map.name_table_string_offs_reordered = addr;
5193 addr += map.name_count * map.offset_size;
5194 map.name_table_entry_offs_reordered = addr;
5195 addr += map.name_count * map.offset_size;
5196
5197 const gdb_byte *abbrev_table_start = addr;
5198 for (;;)
5199 {
5200 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5201 addr += bytes_read;
5202 if (index_num == 0)
5203 break;
5204
5205 const auto insertpair
5206 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5207 if (!insertpair.second)
5208 {
5209 warning (_("Section .debug_names in %s has duplicate index %s, "
5210 "ignoring .debug_names."),
5211 filename, pulongest (index_num));
5212 return false;
5213 }
5214 mapped_debug_names::index_val &indexval = insertpair.first->second;
5215 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5216 addr += bytes_read;
5217
5218 for (;;)
5219 {
5220 mapped_debug_names::index_val::attr attr;
5221 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5222 addr += bytes_read;
5223 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5224 addr += bytes_read;
5225 if (attr.form == DW_FORM_implicit_const)
5226 {
5227 attr.implicit_const = read_signed_leb128 (abfd, addr,
5228 &bytes_read);
5229 addr += bytes_read;
5230 }
5231 if (attr.dw_idx == 0 && attr.form == 0)
5232 break;
5233 indexval.attr_vec.push_back (std::move (attr));
5234 }
5235 }
5236 if (addr != abbrev_table_start + abbrev_table_size)
5237 {
5238 warning (_("Section .debug_names in %s has abbreviation_table "
5239 "of size %s vs. written as %u, ignoring .debug_names."),
5240 filename, plongest (addr - abbrev_table_start),
5241 abbrev_table_size);
5242 return false;
5243 }
5244 map.entry_pool = addr;
5245
5246 return true;
5247 }
5248
5249 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5250 list. */
5251
5252 static void
5253 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
5254 const mapped_debug_names &map,
5255 dwarf2_section_info &section,
5256 bool is_dwz)
5257 {
5258 if (!map.augmentation_is_gdb)
5259 {
5260 for (uint32_t i = 0; i < map.cu_count; ++i)
5261 {
5262 sect_offset sect_off
5263 = (sect_offset) (extract_unsigned_integer
5264 (map.cu_table_reordered + i * map.offset_size,
5265 map.offset_size,
5266 map.dwarf5_byte_order));
5267 /* We don't know the length of the CU, because the CU list in a
5268 .debug_names index can be incomplete, so we can't use the start
5269 of the next CU as end of this CU. We create the CUs here with
5270 length 0, and in cutu_reader::cutu_reader we'll fill in the
5271 actual length. */
5272 dwarf2_per_cu_data *per_cu
5273 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5274 sect_off, 0);
5275 per_bfd->all_comp_units.push_back (per_cu);
5276 }
5277 return;
5278 }
5279
5280 sect_offset sect_off_prev;
5281 for (uint32_t i = 0; i <= map.cu_count; ++i)
5282 {
5283 sect_offset sect_off_next;
5284 if (i < map.cu_count)
5285 {
5286 sect_off_next
5287 = (sect_offset) (extract_unsigned_integer
5288 (map.cu_table_reordered + i * map.offset_size,
5289 map.offset_size,
5290 map.dwarf5_byte_order));
5291 }
5292 else
5293 sect_off_next = (sect_offset) section.size;
5294 if (i >= 1)
5295 {
5296 const ULONGEST length = sect_off_next - sect_off_prev;
5297 dwarf2_per_cu_data *per_cu
5298 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5299 sect_off_prev, length);
5300 per_bfd->all_comp_units.push_back (per_cu);
5301 }
5302 sect_off_prev = sect_off_next;
5303 }
5304 }
5305
5306 /* Read the CU list from the mapped index, and use it to create all
5307 the CU objects for this dwarf2_per_objfile. */
5308
5309 static void
5310 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
5311 const mapped_debug_names &map,
5312 const mapped_debug_names &dwz_map)
5313 {
5314 gdb_assert (per_bfd->all_comp_units.empty ());
5315 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5316
5317 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
5318 false /* is_dwz */);
5319
5320 if (dwz_map.cu_count == 0)
5321 return;
5322
5323 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5324 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
5325 true /* is_dwz */);
5326 }
5327
5328 /* Read .debug_names. If everything went ok, initialize the "quick"
5329 elements of all the CUs and return true. Otherwise, return false. */
5330
5331 static bool
5332 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
5333 {
5334 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5335 mapped_debug_names dwz_map;
5336 struct objfile *objfile = per_objfile->objfile;
5337 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5338
5339 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5340 &per_bfd->debug_names, *map))
5341 return false;
5342
5343 /* Don't use the index if it's empty. */
5344 if (map->name_count == 0)
5345 return false;
5346
5347 /* If there is a .dwz file, read it so we can get its CU list as
5348 well. */
5349 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5350 if (dwz != NULL)
5351 {
5352 if (!read_debug_names_from_section (objfile,
5353 bfd_get_filename (dwz->dwz_bfd.get ()),
5354 &dwz->debug_names, dwz_map))
5355 {
5356 warning (_("could not read '.debug_names' section from %s; skipping"),
5357 bfd_get_filename (dwz->dwz_bfd.get ()));
5358 return false;
5359 }
5360 }
5361
5362 create_cus_from_debug_names (per_bfd, *map, dwz_map);
5363
5364 if (map->tu_count != 0)
5365 {
5366 /* We can only handle a single .debug_types when we have an
5367 index. */
5368 if (per_bfd->types.size () != 1)
5369 return false;
5370
5371 dwarf2_section_info *section = &per_bfd->types[0];
5372
5373 create_signatured_type_table_from_debug_names
5374 (per_objfile, *map, section, &per_bfd->abbrev);
5375 }
5376
5377 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
5378
5379 per_bfd->debug_names_table = std::move (map);
5380 per_bfd->using_index = 1;
5381 per_bfd->quick_file_names_table =
5382 create_quick_file_names_table (per_bfd->all_comp_units.size ());
5383
5384 return true;
5385 }
5386
5387 /* Type used to manage iterating over all CUs looking for a symbol for
5388 .debug_names. */
5389
5390 class dw2_debug_names_iterator
5391 {
5392 public:
5393 dw2_debug_names_iterator (const mapped_debug_names &map,
5394 block_search_flags block_index,
5395 domain_enum domain,
5396 const char *name, dwarf2_per_objfile *per_objfile)
5397 : m_map (map), m_block_index (block_index), m_domain (domain),
5398 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5399 m_per_objfile (per_objfile)
5400 {}
5401
5402 dw2_debug_names_iterator (const mapped_debug_names &map,
5403 search_domain search, uint32_t namei,
5404 dwarf2_per_objfile *per_objfile,
5405 domain_enum domain = UNDEF_DOMAIN)
5406 : m_map (map),
5407 m_domain (domain),
5408 m_search (search),
5409 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5410 m_per_objfile (per_objfile)
5411 {}
5412
5413 dw2_debug_names_iterator (const mapped_debug_names &map,
5414 block_search_flags block_index, domain_enum domain,
5415 uint32_t namei, dwarf2_per_objfile *per_objfile)
5416 : m_map (map), m_block_index (block_index), m_domain (domain),
5417 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5418 m_per_objfile (per_objfile)
5419 {}
5420
5421 /* Return the next matching CU or NULL if there are no more. */
5422 dwarf2_per_cu_data *next ();
5423
5424 private:
5425 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5426 const char *name,
5427 dwarf2_per_objfile *per_objfile);
5428 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5429 uint32_t namei,
5430 dwarf2_per_objfile *per_objfile);
5431
5432 /* The internalized form of .debug_names. */
5433 const mapped_debug_names &m_map;
5434
5435 /* Restrict the search to these blocks. */
5436 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
5437 | SEARCH_STATIC_BLOCK);
5438
5439 /* The kind of symbol we're looking for. */
5440 const domain_enum m_domain = UNDEF_DOMAIN;
5441 const search_domain m_search = ALL_DOMAIN;
5442
5443 /* The list of CUs from the index entry of the symbol, or NULL if
5444 not found. */
5445 const gdb_byte *m_addr;
5446
5447 dwarf2_per_objfile *m_per_objfile;
5448 };
5449
5450 const char *
5451 mapped_debug_names::namei_to_name
5452 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
5453 {
5454 const ULONGEST namei_string_offs
5455 = extract_unsigned_integer ((name_table_string_offs_reordered
5456 + namei * offset_size),
5457 offset_size,
5458 dwarf5_byte_order);
5459 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
5460 }
5461
5462 /* Find a slot in .debug_names for the object named NAME. If NAME is
5463 found, return pointer to its pool data. If NAME cannot be found,
5464 return NULL. */
5465
5466 const gdb_byte *
5467 dw2_debug_names_iterator::find_vec_in_debug_names
5468 (const mapped_debug_names &map, const char *name,
5469 dwarf2_per_objfile *per_objfile)
5470 {
5471 int (*cmp) (const char *, const char *);
5472
5473 gdb::unique_xmalloc_ptr<char> without_params;
5474 if (current_language->la_language == language_cplus
5475 || current_language->la_language == language_fortran
5476 || current_language->la_language == language_d)
5477 {
5478 /* NAME is already canonical. Drop any qualifiers as
5479 .debug_names does not contain any. */
5480
5481 if (strchr (name, '(') != NULL)
5482 {
5483 without_params = cp_remove_params (name);
5484 if (without_params != NULL)
5485 name = without_params.get ();
5486 }
5487 }
5488
5489 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5490
5491 const uint32_t full_hash = dwarf5_djb_hash (name);
5492 uint32_t namei
5493 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5494 (map.bucket_table_reordered
5495 + (full_hash % map.bucket_count)), 4,
5496 map.dwarf5_byte_order);
5497 if (namei == 0)
5498 return NULL;
5499 --namei;
5500 if (namei >= map.name_count)
5501 {
5502 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5503 "[in module %s]"),
5504 namei, map.name_count,
5505 objfile_name (per_objfile->objfile));
5506 return NULL;
5507 }
5508
5509 for (;;)
5510 {
5511 const uint32_t namei_full_hash
5512 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5513 (map.hash_table_reordered + namei), 4,
5514 map.dwarf5_byte_order);
5515 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5516 return NULL;
5517
5518 if (full_hash == namei_full_hash)
5519 {
5520 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5521
5522 #if 0 /* An expensive sanity check. */
5523 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5524 {
5525 complaint (_("Wrong .debug_names hash for string at index %u "
5526 "[in module %s]"),
5527 namei, objfile_name (dwarf2_per_objfile->objfile));
5528 return NULL;
5529 }
5530 #endif
5531
5532 if (cmp (namei_string, name) == 0)
5533 {
5534 const ULONGEST namei_entry_offs
5535 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5536 + namei * map.offset_size),
5537 map.offset_size, map.dwarf5_byte_order);
5538 return map.entry_pool + namei_entry_offs;
5539 }
5540 }
5541
5542 ++namei;
5543 if (namei >= map.name_count)
5544 return NULL;
5545 }
5546 }
5547
5548 const gdb_byte *
5549 dw2_debug_names_iterator::find_vec_in_debug_names
5550 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5551 {
5552 if (namei >= map.name_count)
5553 {
5554 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5555 "[in module %s]"),
5556 namei, map.name_count,
5557 objfile_name (per_objfile->objfile));
5558 return NULL;
5559 }
5560
5561 const ULONGEST namei_entry_offs
5562 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5563 + namei * map.offset_size),
5564 map.offset_size, map.dwarf5_byte_order);
5565 return map.entry_pool + namei_entry_offs;
5566 }
5567
5568 /* See dw2_debug_names_iterator. */
5569
5570 dwarf2_per_cu_data *
5571 dw2_debug_names_iterator::next ()
5572 {
5573 if (m_addr == NULL)
5574 return NULL;
5575
5576 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5577 struct objfile *objfile = m_per_objfile->objfile;
5578 bfd *const abfd = objfile->obfd;
5579
5580 again:
5581
5582 unsigned int bytes_read;
5583 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5584 m_addr += bytes_read;
5585 if (abbrev == 0)
5586 return NULL;
5587
5588 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5589 if (indexval_it == m_map.abbrev_map.cend ())
5590 {
5591 complaint (_("Wrong .debug_names undefined abbrev code %s "
5592 "[in module %s]"),
5593 pulongest (abbrev), objfile_name (objfile));
5594 return NULL;
5595 }
5596 const mapped_debug_names::index_val &indexval = indexval_it->second;
5597 enum class symbol_linkage {
5598 unknown,
5599 static_,
5600 extern_,
5601 } symbol_linkage_ = symbol_linkage::unknown;
5602 dwarf2_per_cu_data *per_cu = NULL;
5603 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5604 {
5605 ULONGEST ull;
5606 switch (attr.form)
5607 {
5608 case DW_FORM_implicit_const:
5609 ull = attr.implicit_const;
5610 break;
5611 case DW_FORM_flag_present:
5612 ull = 1;
5613 break;
5614 case DW_FORM_udata:
5615 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5616 m_addr += bytes_read;
5617 break;
5618 case DW_FORM_ref4:
5619 ull = read_4_bytes (abfd, m_addr);
5620 m_addr += 4;
5621 break;
5622 case DW_FORM_ref8:
5623 ull = read_8_bytes (abfd, m_addr);
5624 m_addr += 8;
5625 break;
5626 case DW_FORM_ref_sig8:
5627 ull = read_8_bytes (abfd, m_addr);
5628 m_addr += 8;
5629 break;
5630 default:
5631 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5632 dwarf_form_name (attr.form),
5633 objfile_name (objfile));
5634 return NULL;
5635 }
5636 switch (attr.dw_idx)
5637 {
5638 case DW_IDX_compile_unit:
5639 /* Don't crash on bad data. */
5640 if (ull >= per_bfd->all_comp_units.size ())
5641 {
5642 complaint (_(".debug_names entry has bad CU index %s"
5643 " [in module %s]"),
5644 pulongest (ull),
5645 objfile_name (objfile));
5646 continue;
5647 }
5648 per_cu = per_bfd->get_cutu (ull);
5649 break;
5650 case DW_IDX_type_unit:
5651 /* Don't crash on bad data. */
5652 if (ull >= per_bfd->all_type_units.size ())
5653 {
5654 complaint (_(".debug_names entry has bad TU index %s"
5655 " [in module %s]"),
5656 pulongest (ull),
5657 objfile_name (objfile));
5658 continue;
5659 }
5660 per_cu = &per_bfd->get_tu (ull)->per_cu;
5661 break;
5662 case DW_IDX_die_offset:
5663 /* In a per-CU index (as opposed to a per-module index), index
5664 entries without CU attribute implicitly refer to the single CU. */
5665 if (per_cu == NULL)
5666 per_cu = per_bfd->get_cu (0);
5667 break;
5668 case DW_IDX_GNU_internal:
5669 if (!m_map.augmentation_is_gdb)
5670 break;
5671 symbol_linkage_ = symbol_linkage::static_;
5672 break;
5673 case DW_IDX_GNU_external:
5674 if (!m_map.augmentation_is_gdb)
5675 break;
5676 symbol_linkage_ = symbol_linkage::extern_;
5677 break;
5678 }
5679 }
5680
5681 /* Skip if already read in. */
5682 if (m_per_objfile->symtab_set_p (per_cu))
5683 goto again;
5684
5685 /* Check static vs global. */
5686 if (symbol_linkage_ != symbol_linkage::unknown)
5687 {
5688 if (symbol_linkage_ == symbol_linkage::static_)
5689 {
5690 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5691 goto again;
5692 }
5693 else
5694 {
5695 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5696 goto again;
5697 }
5698 }
5699
5700 /* Match dw2_symtab_iter_next, symbol_kind
5701 and debug_names::psymbol_tag. */
5702 switch (m_domain)
5703 {
5704 case VAR_DOMAIN:
5705 switch (indexval.dwarf_tag)
5706 {
5707 case DW_TAG_variable:
5708 case DW_TAG_subprogram:
5709 /* Some types are also in VAR_DOMAIN. */
5710 case DW_TAG_typedef:
5711 case DW_TAG_structure_type:
5712 break;
5713 default:
5714 goto again;
5715 }
5716 break;
5717 case STRUCT_DOMAIN:
5718 switch (indexval.dwarf_tag)
5719 {
5720 case DW_TAG_typedef:
5721 case DW_TAG_structure_type:
5722 break;
5723 default:
5724 goto again;
5725 }
5726 break;
5727 case LABEL_DOMAIN:
5728 switch (indexval.dwarf_tag)
5729 {
5730 case 0:
5731 case DW_TAG_variable:
5732 break;
5733 default:
5734 goto again;
5735 }
5736 break;
5737 case MODULE_DOMAIN:
5738 switch (indexval.dwarf_tag)
5739 {
5740 case DW_TAG_module:
5741 break;
5742 default:
5743 goto again;
5744 }
5745 break;
5746 default:
5747 break;
5748 }
5749
5750 /* Match dw2_expand_symtabs_matching, symbol_kind and
5751 debug_names::psymbol_tag. */
5752 switch (m_search)
5753 {
5754 case VARIABLES_DOMAIN:
5755 switch (indexval.dwarf_tag)
5756 {
5757 case DW_TAG_variable:
5758 break;
5759 default:
5760 goto again;
5761 }
5762 break;
5763 case FUNCTIONS_DOMAIN:
5764 switch (indexval.dwarf_tag)
5765 {
5766 case DW_TAG_subprogram:
5767 break;
5768 default:
5769 goto again;
5770 }
5771 break;
5772 case TYPES_DOMAIN:
5773 switch (indexval.dwarf_tag)
5774 {
5775 case DW_TAG_typedef:
5776 case DW_TAG_structure_type:
5777 break;
5778 default:
5779 goto again;
5780 }
5781 break;
5782 case MODULES_DOMAIN:
5783 switch (indexval.dwarf_tag)
5784 {
5785 case DW_TAG_module:
5786 break;
5787 default:
5788 goto again;
5789 }
5790 default:
5791 break;
5792 }
5793
5794 return per_cu;
5795 }
5796
5797 struct compunit_symtab *
5798 dwarf2_debug_names_index::lookup_symbol
5799 (struct objfile *objfile, block_enum block_index,
5800 const char *name, domain_enum domain)
5801 {
5802 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5803
5804 const auto &mapp = per_objfile->per_bfd->debug_names_table;
5805 if (!mapp)
5806 {
5807 /* index is NULL if OBJF_READNOW. */
5808 return NULL;
5809 }
5810 const auto &map = *mapp;
5811
5812 dw2_debug_names_iterator iter (map,
5813 block_index == GLOBAL_BLOCK
5814 ? SEARCH_GLOBAL_BLOCK
5815 : SEARCH_STATIC_BLOCK,
5816 domain, name, per_objfile);
5817
5818 struct compunit_symtab *stab_best = NULL;
5819 struct dwarf2_per_cu_data *per_cu;
5820 while ((per_cu = iter.next ()) != NULL)
5821 {
5822 struct symbol *sym, *with_opaque = NULL;
5823 compunit_symtab *stab
5824 = dw2_instantiate_symtab (per_cu, per_objfile, false);
5825 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5826 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5827
5828 sym = block_find_symbol (block, name, domain,
5829 block_find_non_opaque_type_preferred,
5830 &with_opaque);
5831
5832 /* Some caution must be observed with overloaded functions and
5833 methods, since the index will not contain any overload
5834 information (but NAME might contain it). */
5835
5836 if (sym != NULL
5837 && strcmp_iw (sym->search_name (), name) == 0)
5838 return stab;
5839 if (with_opaque != NULL
5840 && strcmp_iw (with_opaque->search_name (), name) == 0)
5841 stab_best = stab;
5842
5843 /* Keep looking through other CUs. */
5844 }
5845
5846 return stab_best;
5847 }
5848
5849 /* This dumps minimal information about .debug_names. It is called
5850 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5851 uses this to verify that .debug_names has been loaded. */
5852
5853 void
5854 dwarf2_debug_names_index::dump (struct objfile *objfile)
5855 {
5856 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5857
5858 gdb_assert (per_objfile->per_bfd->using_index);
5859 printf_filtered (".debug_names:");
5860 if (per_objfile->per_bfd->debug_names_table)
5861 printf_filtered (" exists\n");
5862 else
5863 printf_filtered (" faked for \"readnow\"\n");
5864 printf_filtered ("\n");
5865 }
5866
5867 void
5868 dwarf2_debug_names_index::expand_symtabs_for_function
5869 (struct objfile *objfile, const char *func_name)
5870 {
5871 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5872
5873 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5874 if (per_objfile->per_bfd->debug_names_table)
5875 {
5876 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5877
5878 dw2_debug_names_iterator iter (map,
5879 (SEARCH_GLOBAL_BLOCK
5880 | SEARCH_STATIC_BLOCK),
5881 VAR_DOMAIN, func_name,
5882 per_objfile);
5883
5884 struct dwarf2_per_cu_data *per_cu;
5885 while ((per_cu = iter.next ()) != NULL)
5886 dw2_instantiate_symtab (per_cu, per_objfile, false);
5887 }
5888 }
5889
5890 void
5891 dwarf2_debug_names_index::map_matching_symbols
5892 (struct objfile *objfile,
5893 const lookup_name_info &name, domain_enum domain,
5894 int global,
5895 gdb::function_view<symbol_found_callback_ftype> callback,
5896 symbol_compare_ftype *ordered_compare)
5897 {
5898 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5899
5900 /* debug_names_table is NULL if OBJF_READNOW. */
5901 if (!per_objfile->per_bfd->debug_names_table)
5902 return;
5903
5904 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5905 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5906 const block_search_flags block_flags
5907 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
5908
5909 const char *match_name = name.ada ().lookup_name ().c_str ();
5910 auto matcher = [&] (const char *symname)
5911 {
5912 if (ordered_compare == nullptr)
5913 return true;
5914 return ordered_compare (symname, match_name) == 0;
5915 };
5916
5917 dw2_expand_symtabs_matching_symbol (map, name, matcher,
5918 [&] (offset_type namei)
5919 {
5920 /* The name was matched, now expand corresponding CUs that were
5921 marked. */
5922 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
5923 per_objfile);
5924
5925 struct dwarf2_per_cu_data *per_cu;
5926 while ((per_cu = iter.next ()) != NULL)
5927 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5928 nullptr);
5929 return true;
5930 }, per_objfile);
5931
5932 /* It's a shame we couldn't do this inside the
5933 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5934 that have already been expanded. Instead, this loop matches what
5935 the psymtab code does. */
5936 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5937 {
5938 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
5939 if (symtab != nullptr)
5940 {
5941 const struct block *block
5942 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5943 if (!iterate_over_symbols_terminated (block, name,
5944 domain, callback))
5945 break;
5946 }
5947 }
5948 }
5949
5950 bool
5951 dwarf2_debug_names_index::expand_symtabs_matching
5952 (struct objfile *objfile,
5953 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5954 const lookup_name_info *lookup_name,
5955 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5956 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5957 block_search_flags search_flags,
5958 domain_enum domain,
5959 enum search_domain kind)
5960 {
5961 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5962
5963 /* debug_names_table is NULL if OBJF_READNOW. */
5964 if (!per_objfile->per_bfd->debug_names_table)
5965 return true;
5966
5967 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5968
5969 if (symbol_matcher == NULL && lookup_name == NULL)
5970 {
5971 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5972 {
5973 QUIT;
5974
5975 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5976 file_matcher,
5977 expansion_notify))
5978 return false;
5979 }
5980 return true;
5981 }
5982
5983 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5984
5985 bool result
5986 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5987 symbol_matcher,
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, kind, namei, per_objfile, domain);
5993
5994 struct dwarf2_per_cu_data *per_cu;
5995 while ((per_cu = iter.next ()) != NULL)
5996 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5997 file_matcher,
5998 expansion_notify))
5999 return false;
6000 return true;
6001 }, per_objfile);
6002
6003 return result;
6004 }
6005
6006 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6007 to either a dwarf2_per_bfd or dwz_file object. */
6008
6009 template <typename T>
6010 static gdb::array_view<const gdb_byte>
6011 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6012 {
6013 dwarf2_section_info *section = &section_owner->gdb_index;
6014
6015 if (section->empty ())
6016 return {};
6017
6018 /* Older elfutils strip versions could keep the section in the main
6019 executable while splitting it for the separate debug info file. */
6020 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
6021 return {};
6022
6023 section->read (obj);
6024
6025 /* dwarf2_section_info::size is a bfd_size_type, while
6026 gdb::array_view works with size_t. On 32-bit hosts, with
6027 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6028 is 32-bit. So we need an explicit narrowing conversion here.
6029 This is fine, because it's impossible to allocate or mmap an
6030 array/buffer larger than what size_t can represent. */
6031 return gdb::make_array_view (section->buffer, section->size);
6032 }
6033
6034 /* Lookup the index cache for the contents of the index associated to
6035 DWARF2_OBJ. */
6036
6037 static gdb::array_view<const gdb_byte>
6038 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
6039 {
6040 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6041 if (build_id == nullptr)
6042 return {};
6043
6044 return global_index_cache.lookup_gdb_index (build_id,
6045 &dwarf2_per_bfd->index_cache_res);
6046 }
6047
6048 /* Same as the above, but for DWZ. */
6049
6050 static gdb::array_view<const gdb_byte>
6051 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6052 {
6053 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6054 if (build_id == nullptr)
6055 return {};
6056
6057 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6058 }
6059
6060 /* See dwarf2/public.h. */
6061
6062 void
6063 dwarf2_initialize_objfile (struct objfile *objfile)
6064 {
6065 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6066 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6067
6068 dwarf_read_debug_printf ("called");
6069
6070 /* If we're about to read full symbols, don't bother with the
6071 indices. In this case we also don't care if some other debug
6072 format is making psymtabs, because they are all about to be
6073 expanded anyway. */
6074 if ((objfile->flags & OBJF_READNOW))
6075 {
6076 dwarf_read_debug_printf ("readnow requested");
6077
6078 /* When using READNOW, the using_index flag (set below) indicates that
6079 PER_BFD was already initialized, when we loaded some other objfile. */
6080 if (per_bfd->using_index)
6081 {
6082 dwarf_read_debug_printf ("using_index already set");
6083 per_objfile->resize_symtabs ();
6084 objfile->qf.push_front (make_dwarf_gdb_index ());
6085 return;
6086 }
6087
6088 per_bfd->using_index = 1;
6089 create_all_comp_units (per_objfile);
6090 create_all_type_units (per_objfile);
6091 per_bfd->quick_file_names_table
6092 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
6093 per_objfile->resize_symtabs ();
6094
6095 for (int i = 0; i < (per_bfd->all_comp_units.size ()
6096 + per_bfd->all_type_units.size ()); ++i)
6097 {
6098 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
6099
6100 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6101 struct dwarf2_per_cu_quick_data);
6102 }
6103
6104 /* Arrange for gdb to see the "quick" functions. However, these
6105 functions will be no-ops because we will have expanded all
6106 symtabs. */
6107 objfile->qf.push_front (make_dwarf_gdb_index ());
6108 return;
6109 }
6110
6111 /* Was a debug names index already read when we processed an objfile sharing
6112 PER_BFD? */
6113 if (per_bfd->debug_names_table != nullptr)
6114 {
6115 dwarf_read_debug_printf ("re-using shared debug names table");
6116 per_objfile->resize_symtabs ();
6117 objfile->qf.push_front (make_dwarf_debug_names ());
6118 return;
6119 }
6120
6121 /* Was a GDB index already read when we processed an objfile sharing
6122 PER_BFD? */
6123 if (per_bfd->index_table != nullptr)
6124 {
6125 dwarf_read_debug_printf ("re-using shared index table");
6126 per_objfile->resize_symtabs ();
6127 objfile->qf.push_front (make_dwarf_gdb_index ());
6128 return;
6129 }
6130
6131 /* There might already be partial symtabs built for this BFD. This happens
6132 when loading the same binary twice with the index-cache enabled. If so,
6133 don't try to read an index. The objfile / per_objfile initialization will
6134 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
6135 code path. */
6136 if (per_bfd->partial_symtabs != nullptr)
6137 {
6138 dwarf_read_debug_printf ("re-using shared partial symtabs");
6139 objfile->qf.push_front (make_lazy_dwarf_reader ());
6140 return;
6141 }
6142
6143 if (dwarf2_read_debug_names (per_objfile))
6144 {
6145 dwarf_read_debug_printf ("found debug names");
6146 per_objfile->resize_symtabs ();
6147 objfile->qf.push_front (make_dwarf_debug_names ());
6148 return;
6149 }
6150
6151 if (dwarf2_read_gdb_index (per_objfile,
6152 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
6153 get_gdb_index_contents_from_section<dwz_file>))
6154 {
6155 dwarf_read_debug_printf ("found gdb index from file");
6156 per_objfile->resize_symtabs ();
6157 objfile->qf.push_front (make_dwarf_gdb_index ());
6158 return;
6159 }
6160
6161 /* ... otherwise, try to find the index in the index cache. */
6162 if (dwarf2_read_gdb_index (per_objfile,
6163 get_gdb_index_contents_from_cache,
6164 get_gdb_index_contents_from_cache_dwz))
6165 {
6166 dwarf_read_debug_printf ("found gdb index from cache");
6167 global_index_cache.hit ();
6168 per_objfile->resize_symtabs ();
6169 objfile->qf.push_front (make_dwarf_gdb_index ());
6170 return;
6171 }
6172
6173 global_index_cache.miss ();
6174 objfile->qf.push_front (make_lazy_dwarf_reader ());
6175 }
6176
6177 \f
6178
6179 /* Build a partial symbol table. */
6180
6181 void
6182 dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf)
6183 {
6184 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6185 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6186
6187 if (per_bfd->partial_symtabs != nullptr)
6188 {
6189 /* Partial symbols were already read, so now we can simply
6190 attach them. */
6191 if (psf == nullptr)
6192 {
6193 psf = new psymbol_functions (per_bfd->partial_symtabs);
6194 objfile->qf.emplace_front (psf);
6195 }
6196 else
6197 psf->set_partial_symtabs (per_bfd->partial_symtabs);
6198 per_objfile->resize_symtabs ();
6199 return;
6200 }
6201
6202 if (psf == nullptr)
6203 {
6204 psf = new psymbol_functions;
6205 objfile->qf.emplace_front (psf);
6206 }
6207 const std::shared_ptr<psymtab_storage> &partial_symtabs
6208 = psf->get_partial_symtabs ();
6209
6210 /* Set the local reference to partial symtabs, so that we don't try
6211 to read them again if reading another objfile with the same BFD.
6212 If we can't in fact share, this won't make a difference anyway as
6213 the dwarf2_per_bfd object won't be shared. */
6214 per_bfd->partial_symtabs = partial_symtabs;
6215
6216 try
6217 {
6218 /* This isn't really ideal: all the data we allocate on the
6219 objfile's obstack is still uselessly kept around. However,
6220 freeing it seems unsafe. */
6221 psymtab_discarder psymtabs (partial_symtabs.get ());
6222 dwarf2_build_psymtabs_hard (per_objfile);
6223 psymtabs.keep ();
6224
6225 per_objfile->resize_symtabs ();
6226
6227 /* (maybe) store an index in the cache. */
6228 global_index_cache.store (per_objfile);
6229 }
6230 catch (const gdb_exception_error &except)
6231 {
6232 exception_print (gdb_stderr, except);
6233 }
6234 }
6235
6236 /* Find the base address of the compilation unit for range lists and
6237 location lists. It will normally be specified by DW_AT_low_pc.
6238 In DWARF-3 draft 4, the base address could be overridden by
6239 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6240 compilation units with discontinuous ranges. */
6241
6242 static void
6243 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6244 {
6245 struct attribute *attr;
6246
6247 cu->base_address.reset ();
6248
6249 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6250 if (attr != nullptr)
6251 cu->base_address = attr->as_address ();
6252 else
6253 {
6254 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6255 if (attr != nullptr)
6256 cu->base_address = attr->as_address ();
6257 }
6258 }
6259
6260 /* Helper function that returns the proper abbrev section for
6261 THIS_CU. */
6262
6263 static struct dwarf2_section_info *
6264 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6265 {
6266 struct dwarf2_section_info *abbrev;
6267 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6268
6269 if (this_cu->is_dwz)
6270 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
6271 else
6272 abbrev = &per_bfd->abbrev;
6273
6274 return abbrev;
6275 }
6276
6277 /* Fetch the abbreviation table offset from a comp or type unit header. */
6278
6279 static sect_offset
6280 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
6281 struct dwarf2_section_info *section,
6282 sect_offset sect_off)
6283 {
6284 bfd *abfd = section->get_bfd_owner ();
6285 const gdb_byte *info_ptr;
6286 unsigned int initial_length_size, offset_size;
6287 uint16_t version;
6288
6289 section->read (per_objfile->objfile);
6290 info_ptr = section->buffer + to_underlying (sect_off);
6291 read_initial_length (abfd, info_ptr, &initial_length_size);
6292 offset_size = initial_length_size == 4 ? 4 : 8;
6293 info_ptr += initial_length_size;
6294
6295 version = read_2_bytes (abfd, info_ptr);
6296 info_ptr += 2;
6297 if (version >= 5)
6298 {
6299 /* Skip unit type and address size. */
6300 info_ptr += 2;
6301 }
6302
6303 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6304 }
6305
6306 /* A partial symtab that is used only for include files. */
6307 struct dwarf2_include_psymtab : public partial_symtab
6308 {
6309 dwarf2_include_psymtab (const char *filename,
6310 psymtab_storage *partial_symtabs,
6311 objfile_per_bfd_storage *objfile_per_bfd)
6312 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
6313 {
6314 }
6315
6316 void read_symtab (struct objfile *objfile) override
6317 {
6318 /* It's an include file, no symbols to read for it.
6319 Everything is in the includer symtab. */
6320
6321 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6322 expansion of the includer psymtab. We use the dependencies[0] field to
6323 model the includer. But if we go the regular route of calling
6324 expand_psymtab here, and having expand_psymtab call expand_dependencies
6325 to expand the includer, we'll only use expand_psymtab on the includer
6326 (making it a non-toplevel psymtab), while if we expand the includer via
6327 another path, we'll use read_symtab (making it a toplevel psymtab).
6328 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6329 psymtab, and trigger read_symtab on the includer here directly. */
6330 includer ()->read_symtab (objfile);
6331 }
6332
6333 void expand_psymtab (struct objfile *objfile) override
6334 {
6335 /* This is not called by read_symtab, and should not be called by any
6336 expand_dependencies. */
6337 gdb_assert (false);
6338 }
6339
6340 bool readin_p (struct objfile *objfile) const override
6341 {
6342 return includer ()->readin_p (objfile);
6343 }
6344
6345 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6346 {
6347 return nullptr;
6348 }
6349
6350 private:
6351 partial_symtab *includer () const
6352 {
6353 /* An include psymtab has exactly one dependency: the psymtab that
6354 includes it. */
6355 gdb_assert (this->number_of_dependencies == 1);
6356 return this->dependencies[0];
6357 }
6358 };
6359
6360 /* Allocate a new partial symtab for file named NAME and mark this new
6361 partial symtab as being an include of PST. */
6362
6363 static void
6364 dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
6365 const char *name,
6366 dwarf2_psymtab *pst,
6367 psymtab_storage *partial_symtabs,
6368 objfile_per_bfd_storage *objfile_per_bfd)
6369 {
6370 dwarf2_include_psymtab *subpst
6371 = new dwarf2_include_psymtab (name, partial_symtabs, objfile_per_bfd);
6372
6373 if (!IS_ABSOLUTE_PATH (subpst->filename))
6374 subpst->dirname = pst->dirname;
6375
6376 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
6377 subpst->dependencies[0] = pst;
6378 subpst->number_of_dependencies = 1;
6379 }
6380
6381 /* Read the Line Number Program data and extract the list of files
6382 included by the source file represented by PST. Build an include
6383 partial symtab for each of these included files. */
6384
6385 static void
6386 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6387 struct die_info *die,
6388 dwarf2_psymtab *pst)
6389 {
6390 line_header_up lh;
6391 struct attribute *attr;
6392
6393 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6394 if (attr != nullptr && attr->form_is_unsigned ())
6395 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
6396 if (lh == NULL)
6397 return; /* No linetable, so no includes. */
6398
6399 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6400 that we pass in the raw text_low here; that is ok because we're
6401 only decoding the line table to make include partial symtabs, and
6402 so the addresses aren't really used. */
6403 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6404 pst->raw_text_low (), 1);
6405 }
6406
6407 static hashval_t
6408 hash_signatured_type (const void *item)
6409 {
6410 const struct signatured_type *sig_type
6411 = (const struct signatured_type *) item;
6412
6413 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6414 return sig_type->signature;
6415 }
6416
6417 static int
6418 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6419 {
6420 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6421 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6422
6423 return lhs->signature == rhs->signature;
6424 }
6425
6426 /* Allocate a hash table for signatured types. */
6427
6428 static htab_up
6429 allocate_signatured_type_table ()
6430 {
6431 return htab_up (htab_create_alloc (41,
6432 hash_signatured_type,
6433 eq_signatured_type,
6434 NULL, xcalloc, xfree));
6435 }
6436
6437 /* A helper function to add a signatured type CU to a table. */
6438
6439 static int
6440 add_signatured_type_cu_to_table (void **slot, void *datum)
6441 {
6442 struct signatured_type *sigt = (struct signatured_type *) *slot;
6443 std::vector<signatured_type *> *all_type_units
6444 = (std::vector<signatured_type *> *) datum;
6445
6446 all_type_units->push_back (sigt);
6447
6448 return 1;
6449 }
6450
6451 /* A helper for create_debug_types_hash_table. Read types from SECTION
6452 and fill them into TYPES_HTAB. It will process only type units,
6453 therefore DW_UT_type. */
6454
6455 static void
6456 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
6457 struct dwo_file *dwo_file,
6458 dwarf2_section_info *section, htab_up &types_htab,
6459 rcuh_kind section_kind)
6460 {
6461 struct objfile *objfile = per_objfile->objfile;
6462 struct dwarf2_section_info *abbrev_section;
6463 bfd *abfd;
6464 const gdb_byte *info_ptr, *end_ptr;
6465
6466 abbrev_section = (dwo_file != NULL
6467 ? &dwo_file->sections.abbrev
6468 : &per_objfile->per_bfd->abbrev);
6469
6470 dwarf_read_debug_printf ("Reading %s for %s",
6471 section->get_name (),
6472 abbrev_section->get_file_name ());
6473
6474 section->read (objfile);
6475 info_ptr = section->buffer;
6476
6477 if (info_ptr == NULL)
6478 return;
6479
6480 /* We can't set abfd until now because the section may be empty or
6481 not present, in which case the bfd is unknown. */
6482 abfd = section->get_bfd_owner ();
6483
6484 /* We don't use cutu_reader here because we don't need to read
6485 any dies: the signature is in the header. */
6486
6487 end_ptr = info_ptr + section->size;
6488 while (info_ptr < end_ptr)
6489 {
6490 struct signatured_type *sig_type;
6491 struct dwo_unit *dwo_tu;
6492 void **slot;
6493 const gdb_byte *ptr = info_ptr;
6494 struct comp_unit_head header;
6495 unsigned int length;
6496
6497 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6498
6499 /* Initialize it due to a false compiler warning. */
6500 header.signature = -1;
6501 header.type_cu_offset_in_tu = (cu_offset) -1;
6502
6503 /* We need to read the type's signature in order to build the hash
6504 table, but we don't need anything else just yet. */
6505
6506 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
6507 abbrev_section, ptr, section_kind);
6508
6509 length = header.get_length ();
6510
6511 /* Skip dummy type units. */
6512 if (ptr >= info_ptr + length
6513 || peek_abbrev_code (abfd, ptr) == 0
6514 || (header.unit_type != DW_UT_type
6515 && header.unit_type != DW_UT_split_type))
6516 {
6517 info_ptr += length;
6518 continue;
6519 }
6520
6521 if (types_htab == NULL)
6522 {
6523 if (dwo_file)
6524 types_htab = allocate_dwo_unit_table ();
6525 else
6526 types_htab = allocate_signatured_type_table ();
6527 }
6528
6529 if (dwo_file)
6530 {
6531 sig_type = NULL;
6532 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
6533 dwo_tu->dwo_file = dwo_file;
6534 dwo_tu->signature = header.signature;
6535 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6536 dwo_tu->section = section;
6537 dwo_tu->sect_off = sect_off;
6538 dwo_tu->length = length;
6539 }
6540 else
6541 {
6542 /* N.B.: type_offset is not usable if this type uses a DWO file.
6543 The real type_offset is in the DWO file. */
6544 dwo_tu = NULL;
6545 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6546 sig_type->signature = header.signature;
6547 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6548 sig_type->per_cu.is_debug_types = 1;
6549 sig_type->per_cu.section = section;
6550 sig_type->per_cu.sect_off = sect_off;
6551 sig_type->per_cu.length = length;
6552 }
6553
6554 slot = htab_find_slot (types_htab.get (),
6555 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6556 INSERT);
6557 gdb_assert (slot != NULL);
6558 if (*slot != NULL)
6559 {
6560 sect_offset dup_sect_off;
6561
6562 if (dwo_file)
6563 {
6564 const struct dwo_unit *dup_tu
6565 = (const struct dwo_unit *) *slot;
6566
6567 dup_sect_off = dup_tu->sect_off;
6568 }
6569 else
6570 {
6571 const struct signatured_type *dup_tu
6572 = (const struct signatured_type *) *slot;
6573
6574 dup_sect_off = dup_tu->per_cu.sect_off;
6575 }
6576
6577 complaint (_("debug type entry at offset %s is duplicate to"
6578 " the entry at offset %s, signature %s"),
6579 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6580 hex_string (header.signature));
6581 }
6582 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6583
6584 dwarf_read_debug_printf_v (" offset %s, signature %s",
6585 sect_offset_str (sect_off),
6586 hex_string (header.signature));
6587
6588 info_ptr += length;
6589 }
6590 }
6591
6592 /* Create the hash table of all entries in the .debug_types
6593 (or .debug_types.dwo) section(s).
6594 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6595 otherwise it is NULL.
6596
6597 The result is a pointer to the hash table or NULL if there are no types.
6598
6599 Note: This function processes DWO files only, not DWP files. */
6600
6601 static void
6602 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
6603 struct dwo_file *dwo_file,
6604 gdb::array_view<dwarf2_section_info> type_sections,
6605 htab_up &types_htab)
6606 {
6607 for (dwarf2_section_info &section : type_sections)
6608 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6609 rcuh_kind::TYPE);
6610 }
6611
6612 /* Create the hash table of all entries in the .debug_types section,
6613 and initialize all_type_units.
6614 The result is zero if there is an error (e.g. missing .debug_types section),
6615 otherwise non-zero. */
6616
6617 static int
6618 create_all_type_units (dwarf2_per_objfile *per_objfile)
6619 {
6620 htab_up types_htab;
6621
6622 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6623 types_htab, rcuh_kind::COMPILE);
6624 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6625 types_htab);
6626 if (types_htab == NULL)
6627 {
6628 per_objfile->per_bfd->signatured_types = NULL;
6629 return 0;
6630 }
6631
6632 per_objfile->per_bfd->signatured_types = std::move (types_htab);
6633
6634 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6635 per_objfile->per_bfd->all_type_units.reserve
6636 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
6637
6638 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
6639 add_signatured_type_cu_to_table,
6640 &per_objfile->per_bfd->all_type_units);
6641
6642 return 1;
6643 }
6644
6645 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6646 If SLOT is non-NULL, it is the entry to use in the hash table.
6647 Otherwise we find one. */
6648
6649 static struct signatured_type *
6650 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6651 {
6652 if (per_objfile->per_bfd->all_type_units.size ()
6653 == per_objfile->per_bfd->all_type_units.capacity ())
6654 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6655
6656 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6657
6658 per_objfile->resize_symtabs ();
6659
6660 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6661 sig_type->signature = sig;
6662 sig_type->per_cu.is_debug_types = 1;
6663 if (per_objfile->per_bfd->using_index)
6664 {
6665 sig_type->per_cu.v.quick =
6666 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6667 struct dwarf2_per_cu_quick_data);
6668 }
6669
6670 if (slot == NULL)
6671 {
6672 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6673 sig_type, INSERT);
6674 }
6675 gdb_assert (*slot == NULL);
6676 *slot = sig_type;
6677 /* The rest of sig_type must be filled in by the caller. */
6678 return sig_type;
6679 }
6680
6681 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6682 Fill in SIG_ENTRY with DWO_ENTRY. */
6683
6684 static void
6685 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
6686 struct signatured_type *sig_entry,
6687 struct dwo_unit *dwo_entry)
6688 {
6689 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6690
6691 /* Make sure we're not clobbering something we don't expect to. */
6692 gdb_assert (! sig_entry->per_cu.queued);
6693 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
6694 if (per_bfd->using_index)
6695 {
6696 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6697 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6698 }
6699 else
6700 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6701 gdb_assert (sig_entry->signature == dwo_entry->signature);
6702 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6703 gdb_assert (sig_entry->type_unit_group == NULL);
6704 gdb_assert (sig_entry->dwo_unit == NULL);
6705
6706 sig_entry->per_cu.section = dwo_entry->section;
6707 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6708 sig_entry->per_cu.length = dwo_entry->length;
6709 sig_entry->per_cu.reading_dwo_directly = 1;
6710 sig_entry->per_cu.per_bfd = per_bfd;
6711 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6712 sig_entry->dwo_unit = dwo_entry;
6713 }
6714
6715 /* Subroutine of lookup_signatured_type.
6716 If we haven't read the TU yet, create the signatured_type data structure
6717 for a TU to be read in directly from a DWO file, bypassing the stub.
6718 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6719 using .gdb_index, then when reading a CU we want to stay in the DWO file
6720 containing that CU. Otherwise we could end up reading several other DWO
6721 files (due to comdat folding) to process the transitive closure of all the
6722 mentioned TUs, and that can be slow. The current DWO file will have every
6723 type signature that it needs.
6724 We only do this for .gdb_index because in the psymtab case we already have
6725 to read all the DWOs to build the type unit groups. */
6726
6727 static struct signatured_type *
6728 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6729 {
6730 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6731 struct dwo_file *dwo_file;
6732 struct dwo_unit find_dwo_entry, *dwo_entry;
6733 struct signatured_type find_sig_entry, *sig_entry;
6734 void **slot;
6735
6736 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6737
6738 /* If TU skeletons have been removed then we may not have read in any
6739 TUs yet. */
6740 if (per_objfile->per_bfd->signatured_types == NULL)
6741 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6742
6743 /* We only ever need to read in one copy of a signatured type.
6744 Use the global signatured_types array to do our own comdat-folding
6745 of types. If this is the first time we're reading this TU, and
6746 the TU has an entry in .gdb_index, replace the recorded data from
6747 .gdb_index with this TU. */
6748
6749 find_sig_entry.signature = sig;
6750 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6751 &find_sig_entry, INSERT);
6752 sig_entry = (struct signatured_type *) *slot;
6753
6754 /* We can get here with the TU already read, *or* in the process of being
6755 read. Don't reassign the global entry to point to this DWO if that's
6756 the case. Also note that if the TU is already being read, it may not
6757 have come from a DWO, the program may be a mix of Fission-compiled
6758 code and non-Fission-compiled code. */
6759
6760 /* Have we already tried to read this TU?
6761 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6762 needn't exist in the global table yet). */
6763 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6764 return sig_entry;
6765
6766 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6767 dwo_unit of the TU itself. */
6768 dwo_file = cu->dwo_unit->dwo_file;
6769
6770 /* Ok, this is the first time we're reading this TU. */
6771 if (dwo_file->tus == NULL)
6772 return NULL;
6773 find_dwo_entry.signature = sig;
6774 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6775 &find_dwo_entry);
6776 if (dwo_entry == NULL)
6777 return NULL;
6778
6779 /* If the global table doesn't have an entry for this TU, add one. */
6780 if (sig_entry == NULL)
6781 sig_entry = add_type_unit (per_objfile, sig, slot);
6782
6783 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6784 sig_entry->per_cu.tu_read = 1;
6785 return sig_entry;
6786 }
6787
6788 /* Subroutine of lookup_signatured_type.
6789 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6790 then try the DWP file. If the TU stub (skeleton) has been removed then
6791 it won't be in .gdb_index. */
6792
6793 static struct signatured_type *
6794 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6795 {
6796 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6797 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6798 struct dwo_unit *dwo_entry;
6799 struct signatured_type find_sig_entry, *sig_entry;
6800 void **slot;
6801
6802 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6803 gdb_assert (dwp_file != NULL);
6804
6805 /* If TU skeletons have been removed then we may not have read in any
6806 TUs yet. */
6807 if (per_objfile->per_bfd->signatured_types == NULL)
6808 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6809
6810 find_sig_entry.signature = sig;
6811 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6812 &find_sig_entry, INSERT);
6813 sig_entry = (struct signatured_type *) *slot;
6814
6815 /* Have we already tried to read this TU?
6816 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6817 needn't exist in the global table yet). */
6818 if (sig_entry != NULL)
6819 return sig_entry;
6820
6821 if (dwp_file->tus == NULL)
6822 return NULL;
6823 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6824 1 /* is_debug_types */);
6825 if (dwo_entry == NULL)
6826 return NULL;
6827
6828 sig_entry = add_type_unit (per_objfile, sig, slot);
6829 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6830
6831 return sig_entry;
6832 }
6833
6834 /* Lookup a signature based type for DW_FORM_ref_sig8.
6835 Returns NULL if signature SIG is not present in the table.
6836 It is up to the caller to complain about this. */
6837
6838 static struct signatured_type *
6839 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6840 {
6841 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6842
6843 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6844 {
6845 /* We're in a DWO/DWP file, and we're using .gdb_index.
6846 These cases require special processing. */
6847 if (get_dwp_file (per_objfile) == NULL)
6848 return lookup_dwo_signatured_type (cu, sig);
6849 else
6850 return lookup_dwp_signatured_type (cu, sig);
6851 }
6852 else
6853 {
6854 struct signatured_type find_entry, *entry;
6855
6856 if (per_objfile->per_bfd->signatured_types == NULL)
6857 return NULL;
6858 find_entry.signature = sig;
6859 entry = ((struct signatured_type *)
6860 htab_find (per_objfile->per_bfd->signatured_types.get (),
6861 &find_entry));
6862 return entry;
6863 }
6864 }
6865
6866 /* Low level DIE reading support. */
6867
6868 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6869
6870 static void
6871 init_cu_die_reader (struct die_reader_specs *reader,
6872 struct dwarf2_cu *cu,
6873 struct dwarf2_section_info *section,
6874 struct dwo_file *dwo_file,
6875 struct abbrev_table *abbrev_table)
6876 {
6877 gdb_assert (section->readin && section->buffer != NULL);
6878 reader->abfd = section->get_bfd_owner ();
6879 reader->cu = cu;
6880 reader->dwo_file = dwo_file;
6881 reader->die_section = section;
6882 reader->buffer = section->buffer;
6883 reader->buffer_end = section->buffer + section->size;
6884 reader->abbrev_table = abbrev_table;
6885 }
6886
6887 /* Subroutine of cutu_reader to simplify it.
6888 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6889 There's just a lot of work to do, and cutu_reader is big enough
6890 already.
6891
6892 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6893 from it to the DIE in the DWO. If NULL we are skipping the stub.
6894 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6895 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6896 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6897 STUB_COMP_DIR may be non-NULL.
6898 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6899 are filled in with the info of the DIE from the DWO file.
6900 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6901 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6902 kept around for at least as long as *RESULT_READER.
6903
6904 The result is non-zero if a valid (non-dummy) DIE was found. */
6905
6906 static int
6907 read_cutu_die_from_dwo (dwarf2_cu *cu,
6908 struct dwo_unit *dwo_unit,
6909 struct die_info *stub_comp_unit_die,
6910 const char *stub_comp_dir,
6911 struct die_reader_specs *result_reader,
6912 const gdb_byte **result_info_ptr,
6913 struct die_info **result_comp_unit_die,
6914 abbrev_table_up *result_dwo_abbrev_table)
6915 {
6916 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6917 dwarf2_per_cu_data *per_cu = cu->per_cu;
6918 struct objfile *objfile = per_objfile->objfile;
6919 bfd *abfd;
6920 const gdb_byte *begin_info_ptr, *info_ptr;
6921 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6922 int i,num_extra_attrs;
6923 struct dwarf2_section_info *dwo_abbrev_section;
6924 struct die_info *comp_unit_die;
6925
6926 /* At most one of these may be provided. */
6927 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6928
6929 /* These attributes aren't processed until later:
6930 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6931 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6932 referenced later. However, these attributes are found in the stub
6933 which we won't have later. In order to not impose this complication
6934 on the rest of the code, we read them here and copy them to the
6935 DWO CU/TU die. */
6936
6937 stmt_list = NULL;
6938 low_pc = NULL;
6939 high_pc = NULL;
6940 ranges = NULL;
6941 comp_dir = NULL;
6942
6943 if (stub_comp_unit_die != NULL)
6944 {
6945 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6946 DWO file. */
6947 if (!per_cu->is_debug_types)
6948 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6949 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6950 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6951 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6952 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6953
6954 cu->addr_base = stub_comp_unit_die->addr_base ();
6955
6956 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6957 We need the value before we can process DW_AT_ranges values from the
6958 DWO. */
6959 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6960
6961 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6962 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6963 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6964 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6965 section. */
6966 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
6967 }
6968 else if (stub_comp_dir != NULL)
6969 {
6970 /* Reconstruct the comp_dir attribute to simplify the code below. */
6971 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
6972 comp_dir->name = DW_AT_comp_dir;
6973 comp_dir->form = DW_FORM_string;
6974 comp_dir->set_string_noncanonical (stub_comp_dir);
6975 }
6976
6977 /* Set up for reading the DWO CU/TU. */
6978 cu->dwo_unit = dwo_unit;
6979 dwarf2_section_info *section = dwo_unit->section;
6980 section->read (objfile);
6981 abfd = section->get_bfd_owner ();
6982 begin_info_ptr = info_ptr = (section->buffer
6983 + to_underlying (dwo_unit->sect_off));
6984 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6985
6986 if (per_cu->is_debug_types)
6987 {
6988 signatured_type *sig_type = (struct signatured_type *) per_cu;
6989
6990 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6991 section, dwo_abbrev_section,
6992 info_ptr, rcuh_kind::TYPE);
6993 /* This is not an assert because it can be caused by bad debug info. */
6994 if (sig_type->signature != cu->header.signature)
6995 {
6996 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6997 " TU at offset %s [in module %s]"),
6998 hex_string (sig_type->signature),
6999 hex_string (cu->header.signature),
7000 sect_offset_str (dwo_unit->sect_off),
7001 bfd_get_filename (abfd));
7002 }
7003 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7004 /* For DWOs coming from DWP files, we don't know the CU length
7005 nor the type's offset in the TU until now. */
7006 dwo_unit->length = cu->header.get_length ();
7007 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7008
7009 /* Establish the type offset that can be used to lookup the type.
7010 For DWO files, we don't know it until now. */
7011 sig_type->type_offset_in_section
7012 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7013 }
7014 else
7015 {
7016 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7017 section, dwo_abbrev_section,
7018 info_ptr, rcuh_kind::COMPILE);
7019 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7020 /* For DWOs coming from DWP files, we don't know the CU length
7021 until now. */
7022 dwo_unit->length = cu->header.get_length ();
7023 }
7024
7025 dwo_abbrev_section->read (objfile);
7026 *result_dwo_abbrev_table
7027 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
7028 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7029 result_dwo_abbrev_table->get ());
7030
7031 /* Read in the die, but leave space to copy over the attributes
7032 from the stub. This has the benefit of simplifying the rest of
7033 the code - all the work to maintain the illusion of a single
7034 DW_TAG_{compile,type}_unit DIE is done here. */
7035 num_extra_attrs = ((stmt_list != NULL)
7036 + (low_pc != NULL)
7037 + (high_pc != NULL)
7038 + (ranges != NULL)
7039 + (comp_dir != NULL));
7040 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7041 num_extra_attrs);
7042
7043 /* Copy over the attributes from the stub to the DIE we just read in. */
7044 comp_unit_die = *result_comp_unit_die;
7045 i = comp_unit_die->num_attrs;
7046 if (stmt_list != NULL)
7047 comp_unit_die->attrs[i++] = *stmt_list;
7048 if (low_pc != NULL)
7049 comp_unit_die->attrs[i++] = *low_pc;
7050 if (high_pc != NULL)
7051 comp_unit_die->attrs[i++] = *high_pc;
7052 if (ranges != NULL)
7053 comp_unit_die->attrs[i++] = *ranges;
7054 if (comp_dir != NULL)
7055 comp_unit_die->attrs[i++] = *comp_dir;
7056 comp_unit_die->num_attrs += num_extra_attrs;
7057
7058 if (dwarf_die_debug)
7059 {
7060 fprintf_unfiltered (gdb_stdlog,
7061 "Read die from %s@0x%x of %s:\n",
7062 section->get_name (),
7063 (unsigned) (begin_info_ptr - section->buffer),
7064 bfd_get_filename (abfd));
7065 dump_die (comp_unit_die, dwarf_die_debug);
7066 }
7067
7068 /* Skip dummy compilation units. */
7069 if (info_ptr >= begin_info_ptr + dwo_unit->length
7070 || peek_abbrev_code (abfd, info_ptr) == 0)
7071 return 0;
7072
7073 *result_info_ptr = info_ptr;
7074 return 1;
7075 }
7076
7077 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7078 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7079 signature is part of the header. */
7080 static gdb::optional<ULONGEST>
7081 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7082 {
7083 if (cu->header.version >= 5)
7084 return cu->header.signature;
7085 struct attribute *attr;
7086 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7087 if (attr == nullptr || !attr->form_is_unsigned ())
7088 return gdb::optional<ULONGEST> ();
7089 return attr->as_unsigned ();
7090 }
7091
7092 /* Subroutine of cutu_reader to simplify it.
7093 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7094 Returns NULL if the specified DWO unit cannot be found. */
7095
7096 static struct dwo_unit *
7097 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
7098 {
7099 dwarf2_per_cu_data *per_cu = cu->per_cu;
7100 struct dwo_unit *dwo_unit;
7101 const char *comp_dir;
7102
7103 gdb_assert (cu != NULL);
7104
7105 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7106 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7107 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7108
7109 if (per_cu->is_debug_types)
7110 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
7111 else
7112 {
7113 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7114
7115 if (!signature.has_value ())
7116 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7117 " [in module %s]"),
7118 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
7119
7120 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
7121 }
7122
7123 return dwo_unit;
7124 }
7125
7126 /* Subroutine of cutu_reader to simplify it.
7127 See it for a description of the parameters.
7128 Read a TU directly from a DWO file, bypassing the stub. */
7129
7130 void
7131 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
7132 dwarf2_per_objfile *per_objfile,
7133 dwarf2_cu *existing_cu)
7134 {
7135 struct signatured_type *sig_type;
7136
7137 /* Verify we can do the following downcast, and that we have the
7138 data we need. */
7139 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7140 sig_type = (struct signatured_type *) this_cu;
7141 gdb_assert (sig_type->dwo_unit != NULL);
7142
7143 dwarf2_cu *cu;
7144
7145 if (existing_cu != nullptr)
7146 {
7147 cu = existing_cu;
7148 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
7149 /* There's no need to do the rereading_dwo_cu handling that
7150 cutu_reader does since we don't read the stub. */
7151 }
7152 else
7153 {
7154 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7155 in per_objfile yet. */
7156 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7157 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7158 cu = m_new_cu.get ();
7159 }
7160
7161 /* A future optimization, if needed, would be to use an existing
7162 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7163 could share abbrev tables. */
7164
7165 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
7166 NULL /* stub_comp_unit_die */,
7167 sig_type->dwo_unit->dwo_file->comp_dir,
7168 this, &info_ptr,
7169 &comp_unit_die,
7170 &m_dwo_abbrev_table) == 0)
7171 {
7172 /* Dummy die. */
7173 dummy_p = true;
7174 }
7175 }
7176
7177 /* Initialize a CU (or TU) and read its DIEs.
7178 If the CU defers to a DWO file, read the DWO file as well.
7179
7180 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7181 Otherwise the table specified in the comp unit header is read in and used.
7182 This is an optimization for when we already have the abbrev table.
7183
7184 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7185 allocated. */
7186
7187 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7188 dwarf2_per_objfile *per_objfile,
7189 struct abbrev_table *abbrev_table,
7190 dwarf2_cu *existing_cu,
7191 bool skip_partial)
7192 : die_reader_specs {},
7193 m_this_cu (this_cu)
7194 {
7195 struct objfile *objfile = per_objfile->objfile;
7196 struct dwarf2_section_info *section = this_cu->section;
7197 bfd *abfd = section->get_bfd_owner ();
7198 const gdb_byte *begin_info_ptr;
7199 struct signatured_type *sig_type = NULL;
7200 struct dwarf2_section_info *abbrev_section;
7201 /* Non-zero if CU currently points to a DWO file and we need to
7202 reread it. When this happens we need to reread the skeleton die
7203 before we can reread the DWO file (this only applies to CUs, not TUs). */
7204 int rereading_dwo_cu = 0;
7205
7206 if (dwarf_die_debug)
7207 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7208 this_cu->is_debug_types ? "type" : "comp",
7209 sect_offset_str (this_cu->sect_off));
7210
7211 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7212 file (instead of going through the stub), short-circuit all of this. */
7213 if (this_cu->reading_dwo_directly)
7214 {
7215 /* Narrow down the scope of possibilities to have to understand. */
7216 gdb_assert (this_cu->is_debug_types);
7217 gdb_assert (abbrev_table == NULL);
7218 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
7219 return;
7220 }
7221
7222 /* This is cheap if the section is already read in. */
7223 section->read (objfile);
7224
7225 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7226
7227 abbrev_section = get_abbrev_section_for_cu (this_cu);
7228
7229 dwarf2_cu *cu;
7230
7231 if (existing_cu != nullptr)
7232 {
7233 cu = existing_cu;
7234 /* If this CU is from a DWO file we need to start over, we need to
7235 refetch the attributes from the skeleton CU.
7236 This could be optimized by retrieving those attributes from when we
7237 were here the first time: the previous comp_unit_die was stored in
7238 comp_unit_obstack. But there's no data yet that we need this
7239 optimization. */
7240 if (cu->dwo_unit != NULL)
7241 rereading_dwo_cu = 1;
7242 }
7243 else
7244 {
7245 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7246 in per_objfile yet. */
7247 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7248 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7249 cu = m_new_cu.get ();
7250 }
7251
7252 /* Get the header. */
7253 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7254 {
7255 /* We already have the header, there's no need to read it in again. */
7256 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7257 }
7258 else
7259 {
7260 if (this_cu->is_debug_types)
7261 {
7262 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7263 section, abbrev_section,
7264 info_ptr, rcuh_kind::TYPE);
7265
7266 /* Since per_cu is the first member of struct signatured_type,
7267 we can go from a pointer to one to a pointer to the other. */
7268 sig_type = (struct signatured_type *) this_cu;
7269 gdb_assert (sig_type->signature == cu->header.signature);
7270 gdb_assert (sig_type->type_offset_in_tu
7271 == cu->header.type_cu_offset_in_tu);
7272 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7273
7274 /* LENGTH has not been set yet for type units if we're
7275 using .gdb_index. */
7276 this_cu->length = cu->header.get_length ();
7277
7278 /* Establish the type offset that can be used to lookup the type. */
7279 sig_type->type_offset_in_section =
7280 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7281
7282 this_cu->dwarf_version = cu->header.version;
7283 }
7284 else
7285 {
7286 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7287 section, abbrev_section,
7288 info_ptr,
7289 rcuh_kind::COMPILE);
7290
7291 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7292 if (this_cu->length == 0)
7293 this_cu->length = cu->header.get_length ();
7294 else
7295 gdb_assert (this_cu->length == cu->header.get_length ());
7296 this_cu->dwarf_version = cu->header.version;
7297 }
7298 }
7299
7300 /* Skip dummy compilation units. */
7301 if (info_ptr >= begin_info_ptr + this_cu->length
7302 || peek_abbrev_code (abfd, info_ptr) == 0)
7303 {
7304 dummy_p = true;
7305 return;
7306 }
7307
7308 /* If we don't have them yet, read the abbrevs for this compilation unit.
7309 And if we need to read them now, make sure they're freed when we're
7310 done. */
7311 if (abbrev_table != NULL)
7312 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7313 else
7314 {
7315 abbrev_section->read (objfile);
7316 m_abbrev_table_holder
7317 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
7318 abbrev_table = m_abbrev_table_holder.get ();
7319 }
7320
7321 /* Read the top level CU/TU die. */
7322 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7323 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7324
7325 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7326 {
7327 dummy_p = true;
7328 return;
7329 }
7330
7331 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7332 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7333 table from the DWO file and pass the ownership over to us. It will be
7334 referenced from READER, so we must make sure to free it after we're done
7335 with READER.
7336
7337 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7338 DWO CU, that this test will fail (the attribute will not be present). */
7339 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7340 if (dwo_name != nullptr)
7341 {
7342 struct dwo_unit *dwo_unit;
7343 struct die_info *dwo_comp_unit_die;
7344
7345 if (comp_unit_die->has_children)
7346 {
7347 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7348 " has children (offset %s) [in module %s]"),
7349 sect_offset_str (this_cu->sect_off),
7350 bfd_get_filename (abfd));
7351 }
7352 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
7353 if (dwo_unit != NULL)
7354 {
7355 if (read_cutu_die_from_dwo (cu, dwo_unit,
7356 comp_unit_die, NULL,
7357 this, &info_ptr,
7358 &dwo_comp_unit_die,
7359 &m_dwo_abbrev_table) == 0)
7360 {
7361 /* Dummy die. */
7362 dummy_p = true;
7363 return;
7364 }
7365 comp_unit_die = dwo_comp_unit_die;
7366 }
7367 else
7368 {
7369 /* Yikes, we couldn't find the rest of the DIE, we only have
7370 the stub. A complaint has already been logged. There's
7371 not much more we can do except pass on the stub DIE to
7372 die_reader_func. We don't want to throw an error on bad
7373 debug info. */
7374 }
7375 }
7376 }
7377
7378 void
7379 cutu_reader::keep ()
7380 {
7381 /* Done, clean up. */
7382 gdb_assert (!dummy_p);
7383 if (m_new_cu != NULL)
7384 {
7385 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7386 now. */
7387 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7388 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
7389 }
7390 }
7391
7392 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7393 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7394 assumed to have already done the lookup to find the DWO file).
7395
7396 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7397 THIS_CU->is_debug_types, but nothing else.
7398
7399 We fill in THIS_CU->length.
7400
7401 THIS_CU->cu is always freed when done.
7402 This is done in order to not leave THIS_CU->cu in a state where we have
7403 to care whether it refers to the "main" CU or the DWO CU.
7404
7405 When parent_cu is passed, it is used to provide a default value for
7406 str_offsets_base and addr_base from the parent. */
7407
7408 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7409 dwarf2_per_objfile *per_objfile,
7410 struct dwarf2_cu *parent_cu,
7411 struct dwo_file *dwo_file)
7412 : die_reader_specs {},
7413 m_this_cu (this_cu)
7414 {
7415 struct objfile *objfile = per_objfile->objfile;
7416 struct dwarf2_section_info *section = this_cu->section;
7417 bfd *abfd = section->get_bfd_owner ();
7418 struct dwarf2_section_info *abbrev_section;
7419 const gdb_byte *begin_info_ptr, *info_ptr;
7420
7421 if (dwarf_die_debug)
7422 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7423 this_cu->is_debug_types ? "type" : "comp",
7424 sect_offset_str (this_cu->sect_off));
7425
7426 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7427
7428 abbrev_section = (dwo_file != NULL
7429 ? &dwo_file->sections.abbrev
7430 : get_abbrev_section_for_cu (this_cu));
7431
7432 /* This is cheap if the section is already read in. */
7433 section->read (objfile);
7434
7435 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7436
7437 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7438 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7439 section, abbrev_section, info_ptr,
7440 (this_cu->is_debug_types
7441 ? rcuh_kind::TYPE
7442 : rcuh_kind::COMPILE));
7443
7444 if (parent_cu != nullptr)
7445 {
7446 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7447 m_new_cu->addr_base = parent_cu->addr_base;
7448 }
7449 this_cu->length = m_new_cu->header.get_length ();
7450
7451 /* Skip dummy compilation units. */
7452 if (info_ptr >= begin_info_ptr + this_cu->length
7453 || peek_abbrev_code (abfd, info_ptr) == 0)
7454 {
7455 dummy_p = true;
7456 return;
7457 }
7458
7459 abbrev_section->read (objfile);
7460 m_abbrev_table_holder
7461 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
7462
7463 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7464 m_abbrev_table_holder.get ());
7465 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7466 }
7467
7468 \f
7469 /* Type Unit Groups.
7470
7471 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7472 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7473 so that all types coming from the same compilation (.o file) are grouped
7474 together. A future step could be to put the types in the same symtab as
7475 the CU the types ultimately came from. */
7476
7477 static hashval_t
7478 hash_type_unit_group (const void *item)
7479 {
7480 const struct type_unit_group *tu_group
7481 = (const struct type_unit_group *) item;
7482
7483 return hash_stmt_list_entry (&tu_group->hash);
7484 }
7485
7486 static int
7487 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7488 {
7489 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7490 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7491
7492 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7493 }
7494
7495 /* Allocate a hash table for type unit groups. */
7496
7497 static htab_up
7498 allocate_type_unit_groups_table ()
7499 {
7500 return htab_up (htab_create_alloc (3,
7501 hash_type_unit_group,
7502 eq_type_unit_group,
7503 NULL, xcalloc, xfree));
7504 }
7505
7506 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7507 partial symtabs. We combine several TUs per psymtab to not let the size
7508 of any one psymtab grow too big. */
7509 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7510 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7511
7512 /* Helper routine for get_type_unit_group.
7513 Create the type_unit_group object used to hold one or more TUs. */
7514
7515 static struct type_unit_group *
7516 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7517 {
7518 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7519 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7520 struct dwarf2_per_cu_data *per_cu;
7521 struct type_unit_group *tu_group;
7522
7523 tu_group = OBSTACK_ZALLOC (&per_bfd->obstack, type_unit_group);
7524 per_cu = &tu_group->per_cu;
7525 per_cu->per_bfd = per_bfd;
7526
7527 if (per_bfd->using_index)
7528 {
7529 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7530 struct dwarf2_per_cu_quick_data);
7531 }
7532 else
7533 {
7534 unsigned int line_offset = to_underlying (line_offset_struct);
7535 dwarf2_psymtab *pst;
7536 std::string name;
7537
7538 /* Give the symtab a useful name for debug purposes. */
7539 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7540 name = string_printf ("<type_units_%d>",
7541 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7542 else
7543 name = string_printf ("<type_units_at_0x%x>", line_offset);
7544
7545 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
7546 pst->anonymous = true;
7547 }
7548
7549 tu_group->hash.dwo_unit = cu->dwo_unit;
7550 tu_group->hash.line_sect_off = line_offset_struct;
7551
7552 return tu_group;
7553 }
7554
7555 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7556 STMT_LIST is a DW_AT_stmt_list attribute. */
7557
7558 static struct type_unit_group *
7559 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7560 {
7561 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7562 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7563 struct type_unit_group *tu_group;
7564 void **slot;
7565 unsigned int line_offset;
7566 struct type_unit_group type_unit_group_for_lookup;
7567
7568 if (per_objfile->per_bfd->type_unit_groups == NULL)
7569 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7570
7571 /* Do we need to create a new group, or can we use an existing one? */
7572
7573 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
7574 {
7575 line_offset = stmt_list->as_unsigned ();
7576 ++tu_stats->nr_symtab_sharers;
7577 }
7578 else
7579 {
7580 /* Ugh, no stmt_list. Rare, but we have to handle it.
7581 We can do various things here like create one group per TU or
7582 spread them over multiple groups to split up the expansion work.
7583 To avoid worst case scenarios (too many groups or too large groups)
7584 we, umm, group them in bunches. */
7585 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7586 | (tu_stats->nr_stmt_less_type_units
7587 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7588 ++tu_stats->nr_stmt_less_type_units;
7589 }
7590
7591 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7592 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7593 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
7594 &type_unit_group_for_lookup, INSERT);
7595 if (*slot != NULL)
7596 {
7597 tu_group = (struct type_unit_group *) *slot;
7598 gdb_assert (tu_group != NULL);
7599 }
7600 else
7601 {
7602 sect_offset line_offset_struct = (sect_offset) line_offset;
7603 tu_group = create_type_unit_group (cu, line_offset_struct);
7604 *slot = tu_group;
7605 ++tu_stats->nr_symtabs;
7606 }
7607
7608 return tu_group;
7609 }
7610 \f
7611 /* Partial symbol tables. */
7612
7613 /* Create a psymtab named NAME and assign it to PER_CU.
7614
7615 The caller must fill in the following details:
7616 dirname, textlow, texthigh. */
7617
7618 static dwarf2_psymtab *
7619 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7620 dwarf2_per_objfile *per_objfile,
7621 const char *name)
7622 {
7623 dwarf2_psymtab *pst
7624 = new dwarf2_psymtab (name, per_objfile->per_bfd->partial_symtabs.get (),
7625 per_objfile->objfile->per_bfd, per_cu);
7626
7627 pst->psymtabs_addrmap_supported = true;
7628
7629 /* This is the glue that links PST into GDB's symbol API. */
7630 per_cu->v.psymtab = pst;
7631
7632 return pst;
7633 }
7634
7635 /* DIE reader function for process_psymtab_comp_unit. */
7636
7637 static void
7638 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7639 const gdb_byte *info_ptr,
7640 struct die_info *comp_unit_die,
7641 enum language pretend_language)
7642 {
7643 struct dwarf2_cu *cu = reader->cu;
7644 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7645 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7646 struct objfile *objfile = per_objfile->objfile;
7647 struct gdbarch *gdbarch = objfile->arch ();
7648 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7649 CORE_ADDR baseaddr;
7650 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7651 dwarf2_psymtab *pst;
7652 enum pc_bounds_kind cu_bounds_kind;
7653 const char *filename;
7654
7655 gdb_assert (! per_cu->is_debug_types);
7656
7657 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7658
7659 /* Allocate a new partial symbol table structure. */
7660 gdb::unique_xmalloc_ptr<char> debug_filename;
7661 static const char artificial[] = "<artificial>";
7662 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7663 if (filename == NULL)
7664 filename = "";
7665 else if (strcmp (filename, artificial) == 0)
7666 {
7667 debug_filename.reset (concat (artificial, "@",
7668 sect_offset_str (per_cu->sect_off),
7669 (char *) NULL));
7670 filename = debug_filename.get ();
7671 }
7672
7673 pst = create_partial_symtab (per_cu, per_objfile, filename);
7674
7675 /* This must be done before calling dwarf2_build_include_psymtabs. */
7676 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7677
7678 baseaddr = objfile->text_section_offset ();
7679
7680 dwarf2_find_base_address (comp_unit_die, cu);
7681
7682 /* Possibly set the default values of LOWPC and HIGHPC from
7683 `DW_AT_ranges'. */
7684 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7685 &best_highpc, cu, pst);
7686 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7687 {
7688 CORE_ADDR low
7689 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7690 - baseaddr);
7691 CORE_ADDR high
7692 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7693 - baseaddr - 1);
7694 /* Store the contiguous range if it is not empty; it can be
7695 empty for CUs with no code. */
7696 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
7697 low, high, pst);
7698 }
7699
7700 /* Check if comp unit has_children.
7701 If so, read the rest of the partial symbols from this comp unit.
7702 If not, there's no more debug_info for this comp unit. */
7703 if (comp_unit_die->has_children)
7704 {
7705 struct partial_die_info *first_die;
7706 CORE_ADDR lowpc, highpc;
7707
7708 lowpc = ((CORE_ADDR) -1);
7709 highpc = ((CORE_ADDR) 0);
7710
7711 first_die = load_partial_dies (reader, info_ptr, 1);
7712
7713 scan_partial_symbols (first_die, &lowpc, &highpc,
7714 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7715
7716 /* If we didn't find a lowpc, set it to highpc to avoid
7717 complaints from `maint check'. */
7718 if (lowpc == ((CORE_ADDR) -1))
7719 lowpc = highpc;
7720
7721 /* If the compilation unit didn't have an explicit address range,
7722 then use the information extracted from its child dies. */
7723 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7724 {
7725 best_lowpc = lowpc;
7726 best_highpc = highpc;
7727 }
7728 }
7729 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7730 best_lowpc + baseaddr)
7731 - baseaddr);
7732 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7733 best_highpc + baseaddr)
7734 - baseaddr);
7735
7736 pst->end ();
7737
7738 if (!cu->per_cu->imported_symtabs_empty ())
7739 {
7740 int i;
7741 int len = cu->per_cu->imported_symtabs_size ();
7742
7743 /* Fill in 'dependencies' here; we fill in 'users' in a
7744 post-pass. */
7745 pst->number_of_dependencies = len;
7746 pst->dependencies
7747 = per_bfd->partial_symtabs->allocate_dependencies (len);
7748 for (i = 0; i < len; ++i)
7749 {
7750 pst->dependencies[i]
7751 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7752 }
7753
7754 cu->per_cu->imported_symtabs_free ();
7755 }
7756
7757 /* Get the list of files included in the current compilation unit,
7758 and build a psymtab for each of them. */
7759 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7760
7761 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7762 ", %d global, %d static syms",
7763 per_cu->is_debug_types ? "type" : "comp",
7764 sect_offset_str (per_cu->sect_off),
7765 paddress (gdbarch, pst->text_low (objfile)),
7766 paddress (gdbarch, pst->text_high (objfile)),
7767 (int) pst->global_psymbols.size (),
7768 (int) pst->static_psymbols.size ());
7769 }
7770
7771 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7772 Process compilation unit THIS_CU for a psymtab. */
7773
7774 static void
7775 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7776 dwarf2_per_objfile *per_objfile,
7777 bool want_partial_unit,
7778 enum language pretend_language)
7779 {
7780 /* If this compilation unit was already read in, free the
7781 cached copy in order to read it in again. This is
7782 necessary because we skipped some symbols when we first
7783 read in the compilation unit (see load_partial_dies).
7784 This problem could be avoided, but the benefit is unclear. */
7785 per_objfile->remove_cu (this_cu);
7786
7787 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7788
7789 switch (reader.comp_unit_die->tag)
7790 {
7791 case DW_TAG_compile_unit:
7792 this_cu->unit_type = DW_UT_compile;
7793 break;
7794 case DW_TAG_partial_unit:
7795 this_cu->unit_type = DW_UT_partial;
7796 break;
7797 case DW_TAG_type_unit:
7798 this_cu->unit_type = DW_UT_type;
7799 break;
7800 default:
7801 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
7802 dwarf_tag_name (reader.comp_unit_die->tag),
7803 sect_offset_str (reader.cu->per_cu->sect_off),
7804 objfile_name (per_objfile->objfile));
7805 }
7806
7807 if (reader.dummy_p)
7808 {
7809 /* Nothing. */
7810 }
7811 else if (this_cu->is_debug_types)
7812 build_type_psymtabs_reader (&reader, reader.info_ptr,
7813 reader.comp_unit_die);
7814 else if (want_partial_unit
7815 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7816 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7817 reader.comp_unit_die,
7818 pretend_language);
7819
7820 this_cu->lang = reader.cu->language;
7821
7822 /* Age out any secondary CUs. */
7823 per_objfile->age_comp_units ();
7824 }
7825
7826 /* Reader function for build_type_psymtabs. */
7827
7828 static void
7829 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7830 const gdb_byte *info_ptr,
7831 struct die_info *type_unit_die)
7832 {
7833 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7834 struct dwarf2_cu *cu = reader->cu;
7835 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7836 struct signatured_type *sig_type;
7837 struct type_unit_group *tu_group;
7838 struct attribute *attr;
7839 struct partial_die_info *first_die;
7840 CORE_ADDR lowpc, highpc;
7841 dwarf2_psymtab *pst;
7842
7843 gdb_assert (per_cu->is_debug_types);
7844 sig_type = (struct signatured_type *) per_cu;
7845
7846 if (! type_unit_die->has_children)
7847 return;
7848
7849 attr = type_unit_die->attr (DW_AT_stmt_list);
7850 tu_group = get_type_unit_group (cu, attr);
7851
7852 if (tu_group->tus == nullptr)
7853 tu_group->tus = new std::vector<signatured_type *>;
7854 tu_group->tus->push_back (sig_type);
7855
7856 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7857 pst = create_partial_symtab (per_cu, per_objfile, "");
7858 pst->anonymous = true;
7859
7860 first_die = load_partial_dies (reader, info_ptr, 1);
7861
7862 lowpc = (CORE_ADDR) -1;
7863 highpc = (CORE_ADDR) 0;
7864 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7865
7866 pst->end ();
7867 }
7868
7869 /* Struct used to sort TUs by their abbreviation table offset. */
7870
7871 struct tu_abbrev_offset
7872 {
7873 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7874 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7875 {}
7876
7877 signatured_type *sig_type;
7878 sect_offset abbrev_offset;
7879 };
7880
7881 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7882
7883 static bool
7884 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7885 const struct tu_abbrev_offset &b)
7886 {
7887 return a.abbrev_offset < b.abbrev_offset;
7888 }
7889
7890 /* Efficiently read all the type units.
7891 This does the bulk of the work for build_type_psymtabs.
7892
7893 The efficiency is because we sort TUs by the abbrev table they use and
7894 only read each abbrev table once. In one program there are 200K TUs
7895 sharing 8K abbrev tables.
7896
7897 The main purpose of this function is to support building the
7898 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7899 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7900 can collapse the search space by grouping them by stmt_list.
7901 The savings can be significant, in the same program from above the 200K TUs
7902 share 8K stmt_list tables.
7903
7904 FUNC is expected to call get_type_unit_group, which will create the
7905 struct type_unit_group if necessary and add it to
7906 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7907
7908 static void
7909 build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
7910 {
7911 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7912 abbrev_table_up abbrev_table;
7913 sect_offset abbrev_offset;
7914
7915 /* It's up to the caller to not call us multiple times. */
7916 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7917
7918 if (per_objfile->per_bfd->all_type_units.empty ())
7919 return;
7920
7921 /* TUs typically share abbrev tables, and there can be way more TUs than
7922 abbrev tables. Sort by abbrev table to reduce the number of times we
7923 read each abbrev table in.
7924 Alternatives are to punt or to maintain a cache of abbrev tables.
7925 This is simpler and efficient enough for now.
7926
7927 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7928 symtab to use). Typically TUs with the same abbrev offset have the same
7929 stmt_list value too so in practice this should work well.
7930
7931 The basic algorithm here is:
7932
7933 sort TUs by abbrev table
7934 for each TU with same abbrev table:
7935 read abbrev table if first user
7936 read TU top level DIE
7937 [IWBN if DWO skeletons had DW_AT_stmt_list]
7938 call FUNC */
7939
7940 dwarf_read_debug_printf ("Building type unit groups ...");
7941
7942 /* Sort in a separate table to maintain the order of all_type_units
7943 for .gdb_index: TU indices directly index all_type_units. */
7944 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7945 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
7946
7947 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
7948 sorted_by_abbrev.emplace_back
7949 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
7950 sig_type->per_cu.sect_off));
7951
7952 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7953 sort_tu_by_abbrev_offset);
7954
7955 abbrev_offset = (sect_offset) ~(unsigned) 0;
7956
7957 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7958 {
7959 /* Switch to the next abbrev table if necessary. */
7960 if (abbrev_table == NULL
7961 || tu.abbrev_offset != abbrev_offset)
7962 {
7963 abbrev_offset = tu.abbrev_offset;
7964 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
7965 abbrev_table =
7966 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
7967 ++tu_stats->nr_uniq_abbrev_tables;
7968 }
7969
7970 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
7971 abbrev_table.get (), nullptr, false);
7972 if (!reader.dummy_p)
7973 build_type_psymtabs_reader (&reader, reader.info_ptr,
7974 reader.comp_unit_die);
7975 }
7976 }
7977
7978 /* Print collected type unit statistics. */
7979
7980 static void
7981 print_tu_stats (dwarf2_per_objfile *per_objfile)
7982 {
7983 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7984
7985 dwarf_read_debug_printf ("Type unit statistics:");
7986 dwarf_read_debug_printf (" %zu TUs",
7987 per_objfile->per_bfd->all_type_units.size ());
7988 dwarf_read_debug_printf (" %d uniq abbrev tables",
7989 tu_stats->nr_uniq_abbrev_tables);
7990 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7991 tu_stats->nr_symtabs);
7992 dwarf_read_debug_printf (" %d symtab sharers",
7993 tu_stats->nr_symtab_sharers);
7994 dwarf_read_debug_printf (" %d type units without a stmt_list",
7995 tu_stats->nr_stmt_less_type_units);
7996 dwarf_read_debug_printf (" %d all_type_units reallocs",
7997 tu_stats->nr_all_type_units_reallocs);
7998 }
7999
8000 /* Traversal function for build_type_psymtabs. */
8001
8002 static int
8003 build_type_psymtab_dependencies (void **slot, void *info)
8004 {
8005 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
8006 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
8007 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8008 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8009 dwarf2_psymtab *pst = per_cu->v.psymtab;
8010 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
8011 int i;
8012
8013 gdb_assert (len > 0);
8014 gdb_assert (per_cu->type_unit_group_p ());
8015
8016 pst->number_of_dependencies = len;
8017 pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
8018 for (i = 0; i < len; ++i)
8019 {
8020 struct signatured_type *iter = tu_group->tus->at (i);
8021 gdb_assert (iter->per_cu.is_debug_types);
8022 pst->dependencies[i] = iter->per_cu.v.psymtab;
8023 iter->type_unit_group = tu_group;
8024 }
8025
8026 delete tu_group->tus;
8027 tu_group->tus = nullptr;
8028
8029 return 1;
8030 }
8031
8032 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8033 Build partial symbol tables for the .debug_types comp-units. */
8034
8035 static void
8036 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
8037 {
8038 if (! create_all_type_units (per_objfile))
8039 return;
8040
8041 build_type_psymtabs_1 (per_objfile);
8042 }
8043
8044 /* Traversal function for process_skeletonless_type_unit.
8045 Read a TU in a DWO file and build partial symbols for it. */
8046
8047 static int
8048 process_skeletonless_type_unit (void **slot, void *info)
8049 {
8050 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8051 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
8052 struct signatured_type find_entry, *entry;
8053
8054 /* If this TU doesn't exist in the global table, add it and read it in. */
8055
8056 if (per_objfile->per_bfd->signatured_types == NULL)
8057 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
8058
8059 find_entry.signature = dwo_unit->signature;
8060 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
8061 &find_entry, INSERT);
8062 /* If we've already seen this type there's nothing to do. What's happening
8063 is we're doing our own version of comdat-folding here. */
8064 if (*slot != NULL)
8065 return 1;
8066
8067 /* This does the job that create_all_type_units would have done for
8068 this TU. */
8069 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
8070 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
8071 *slot = entry;
8072
8073 /* This does the job that build_type_psymtabs_1 would have done. */
8074 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
8075 if (!reader.dummy_p)
8076 build_type_psymtabs_reader (&reader, reader.info_ptr,
8077 reader.comp_unit_die);
8078
8079 return 1;
8080 }
8081
8082 /* Traversal function for process_skeletonless_type_units. */
8083
8084 static int
8085 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8086 {
8087 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8088
8089 if (dwo_file->tus != NULL)
8090 htab_traverse_noresize (dwo_file->tus.get (),
8091 process_skeletonless_type_unit, info);
8092
8093 return 1;
8094 }
8095
8096 /* Scan all TUs of DWO files, verifying we've processed them.
8097 This is needed in case a TU was emitted without its skeleton.
8098 Note: This can't be done until we know what all the DWO files are. */
8099
8100 static void
8101 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
8102 {
8103 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8104 if (get_dwp_file (per_objfile) == NULL
8105 && per_objfile->per_bfd->dwo_files != NULL)
8106 {
8107 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
8108 process_dwo_file_for_skeletonless_type_units,
8109 per_objfile);
8110 }
8111 }
8112
8113 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8114
8115 static void
8116 set_partial_user (dwarf2_per_objfile *per_objfile)
8117 {
8118 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
8119 {
8120 dwarf2_psymtab *pst = per_cu->v.psymtab;
8121
8122 if (pst == NULL)
8123 continue;
8124
8125 for (int j = 0; j < pst->number_of_dependencies; ++j)
8126 {
8127 /* Set the 'user' field only if it is not already set. */
8128 if (pst->dependencies[j]->user == NULL)
8129 pst->dependencies[j]->user = pst;
8130 }
8131 }
8132 }
8133
8134 /* Build the partial symbol table by doing a quick pass through the
8135 .debug_info and .debug_abbrev sections. */
8136
8137 static void
8138 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
8139 {
8140 struct objfile *objfile = per_objfile->objfile;
8141 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
8142
8143 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
8144 objfile_name (objfile));
8145
8146 scoped_restore restore_reading_psyms
8147 = make_scoped_restore (&per_bfd->reading_partial_symbols, true);
8148
8149 per_bfd->info.read (objfile);
8150
8151 /* Any cached compilation units will be linked by the per-objfile
8152 read_in_chain. Make sure to free them when we're done. */
8153 free_cached_comp_units freer (per_objfile);
8154
8155 build_type_psymtabs (per_objfile);
8156
8157 create_all_comp_units (per_objfile);
8158
8159 /* Create a temporary address map on a temporary obstack. We later
8160 copy this to the final obstack. */
8161 auto_obstack temp_obstack;
8162
8163 scoped_restore save_psymtabs_addrmap
8164 = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
8165 addrmap_create_mutable (&temp_obstack));
8166
8167 for (dwarf2_per_cu_data *per_cu : per_bfd->all_comp_units)
8168 {
8169 if (per_cu->v.psymtab != NULL)
8170 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8171 continue;
8172 process_psymtab_comp_unit (per_cu, per_objfile, false,
8173 language_minimal);
8174 }
8175
8176 /* This has to wait until we read the CUs, we need the list of DWOs. */
8177 process_skeletonless_type_units (per_objfile);
8178
8179 /* Now that all TUs have been processed we can fill in the dependencies. */
8180 if (per_bfd->type_unit_groups != NULL)
8181 {
8182 htab_traverse_noresize (per_bfd->type_unit_groups.get (),
8183 build_type_psymtab_dependencies, per_objfile);
8184 }
8185
8186 if (dwarf_read_debug > 0)
8187 print_tu_stats (per_objfile);
8188
8189 set_partial_user (per_objfile);
8190
8191 per_bfd->partial_symtabs->psymtabs_addrmap
8192 = addrmap_create_fixed (per_bfd->partial_symtabs->psymtabs_addrmap,
8193 per_bfd->partial_symtabs->obstack ());
8194 /* At this point we want to keep the address map. */
8195 save_psymtabs_addrmap.release ();
8196
8197 dwarf_read_debug_printf ("Done building psymtabs of %s",
8198 objfile_name (objfile));
8199 }
8200
8201 /* Load the partial DIEs for a secondary CU into memory.
8202 This is also used when rereading a primary CU with load_all_dies. */
8203
8204 static void
8205 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
8206 dwarf2_per_objfile *per_objfile,
8207 dwarf2_cu *existing_cu)
8208 {
8209 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
8210
8211 if (!reader.dummy_p)
8212 {
8213 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8214 language_minimal);
8215
8216 /* Check if comp unit has_children.
8217 If so, read the rest of the partial symbols from this comp unit.
8218 If not, there's no more debug_info for this comp unit. */
8219 if (reader.comp_unit_die->has_children)
8220 load_partial_dies (&reader, reader.info_ptr, 0);
8221
8222 reader.keep ();
8223 }
8224 }
8225
8226 static void
8227 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
8228 struct dwarf2_section_info *section,
8229 struct dwarf2_section_info *abbrev_section,
8230 unsigned int is_dwz)
8231 {
8232 const gdb_byte *info_ptr;
8233 struct objfile *objfile = per_objfile->objfile;
8234
8235 dwarf_read_debug_printf ("Reading %s for %s",
8236 section->get_name (),
8237 section->get_file_name ());
8238
8239 section->read (objfile);
8240
8241 info_ptr = section->buffer;
8242
8243 while (info_ptr < section->buffer + section->size)
8244 {
8245 struct dwarf2_per_cu_data *this_cu;
8246
8247 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8248
8249 comp_unit_head cu_header;
8250 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
8251 abbrev_section, info_ptr,
8252 rcuh_kind::COMPILE);
8253
8254 /* Save the compilation unit for later lookup. */
8255 if (cu_header.unit_type != DW_UT_type)
8256 this_cu = per_objfile->per_bfd->allocate_per_cu ();
8257 else
8258 {
8259 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
8260 sig_type->signature = cu_header.signature;
8261 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8262 this_cu = &sig_type->per_cu;
8263 }
8264 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8265 this_cu->sect_off = sect_off;
8266 this_cu->length = cu_header.length + cu_header.initial_length_size;
8267 this_cu->is_dwz = is_dwz;
8268 this_cu->section = section;
8269
8270 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8271
8272 info_ptr = info_ptr + this_cu->length;
8273 }
8274 }
8275
8276 /* Create a list of all compilation units in OBJFILE.
8277 This is only done for -readnow and building partial symtabs. */
8278
8279 static void
8280 create_all_comp_units (dwarf2_per_objfile *per_objfile)
8281 {
8282 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8283 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8284 &per_objfile->per_bfd->abbrev, 0);
8285
8286 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8287 if (dwz != NULL)
8288 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
8289 }
8290
8291 /* Process all loaded DIEs for compilation unit CU, starting at
8292 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8293 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8294 DW_AT_ranges). See the comments of add_partial_subprogram on how
8295 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8296
8297 static void
8298 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8299 CORE_ADDR *highpc, int set_addrmap,
8300 struct dwarf2_cu *cu)
8301 {
8302 struct partial_die_info *pdi;
8303
8304 /* Now, march along the PDI's, descending into ones which have
8305 interesting children but skipping the children of the other ones,
8306 until we reach the end of the compilation unit. */
8307
8308 pdi = first_die;
8309
8310 while (pdi != NULL)
8311 {
8312 pdi->fixup (cu);
8313
8314 /* Anonymous namespaces or modules have no name but have interesting
8315 children, so we need to look at them. Ditto for anonymous
8316 enums. */
8317
8318 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
8319 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8320 || pdi->tag == DW_TAG_imported_unit
8321 || pdi->tag == DW_TAG_inlined_subroutine)
8322 {
8323 switch (pdi->tag)
8324 {
8325 case DW_TAG_subprogram:
8326 case DW_TAG_inlined_subroutine:
8327 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8328 if (cu->language == language_cplus)
8329 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8330 set_addrmap, cu);
8331 break;
8332 case DW_TAG_constant:
8333 case DW_TAG_variable:
8334 case DW_TAG_typedef:
8335 case DW_TAG_union_type:
8336 if (!pdi->is_declaration
8337 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8338 {
8339 add_partial_symbol (pdi, cu);
8340 }
8341 break;
8342 case DW_TAG_class_type:
8343 case DW_TAG_interface_type:
8344 case DW_TAG_structure_type:
8345 if (!pdi->is_declaration)
8346 {
8347 add_partial_symbol (pdi, cu);
8348 }
8349 if ((cu->language == language_rust
8350 || cu->language == language_cplus) && pdi->has_children)
8351 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8352 set_addrmap, cu);
8353 break;
8354 case DW_TAG_enumeration_type:
8355 if (!pdi->is_declaration)
8356 add_partial_enumeration (pdi, cu);
8357 break;
8358 case DW_TAG_base_type:
8359 case DW_TAG_subrange_type:
8360 /* File scope base type definitions are added to the partial
8361 symbol table. */
8362 add_partial_symbol (pdi, cu);
8363 break;
8364 case DW_TAG_namespace:
8365 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8366 break;
8367 case DW_TAG_module:
8368 if (!pdi->is_declaration)
8369 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8370 break;
8371 case DW_TAG_imported_unit:
8372 {
8373 struct dwarf2_per_cu_data *per_cu;
8374
8375 /* For now we don't handle imported units in type units. */
8376 if (cu->per_cu->is_debug_types)
8377 {
8378 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8379 " supported in type units [in module %s]"),
8380 objfile_name (cu->per_objfile->objfile));
8381 }
8382
8383 per_cu = dwarf2_find_containing_comp_unit
8384 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8385
8386 /* Go read the partial unit, if needed. */
8387 if (per_cu->v.psymtab == NULL)
8388 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8389 cu->language);
8390
8391 cu->per_cu->imported_symtabs_push (per_cu);
8392 }
8393 break;
8394 case DW_TAG_imported_declaration:
8395 add_partial_symbol (pdi, cu);
8396 break;
8397 default:
8398 break;
8399 }
8400 }
8401
8402 /* If the die has a sibling, skip to the sibling. */
8403
8404 pdi = pdi->die_sibling;
8405 }
8406 }
8407
8408 /* Functions used to compute the fully scoped name of a partial DIE.
8409
8410 Normally, this is simple. For C++, the parent DIE's fully scoped
8411 name is concatenated with "::" and the partial DIE's name.
8412 Enumerators are an exception; they use the scope of their parent
8413 enumeration type, i.e. the name of the enumeration type is not
8414 prepended to the enumerator.
8415
8416 There are two complexities. One is DW_AT_specification; in this
8417 case "parent" means the parent of the target of the specification,
8418 instead of the direct parent of the DIE. The other is compilers
8419 which do not emit DW_TAG_namespace; in this case we try to guess
8420 the fully qualified name of structure types from their members'
8421 linkage names. This must be done using the DIE's children rather
8422 than the children of any DW_AT_specification target. We only need
8423 to do this for structures at the top level, i.e. if the target of
8424 any DW_AT_specification (if any; otherwise the DIE itself) does not
8425 have a parent. */
8426
8427 /* Compute the scope prefix associated with PDI's parent, in
8428 compilation unit CU. The result will be allocated on CU's
8429 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8430 field. NULL is returned if no prefix is necessary. */
8431 static const char *
8432 partial_die_parent_scope (struct partial_die_info *pdi,
8433 struct dwarf2_cu *cu)
8434 {
8435 const char *grandparent_scope;
8436 struct partial_die_info *parent, *real_pdi;
8437
8438 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8439 then this means the parent of the specification DIE. */
8440
8441 real_pdi = pdi;
8442 while (real_pdi->has_specification)
8443 {
8444 auto res = find_partial_die (real_pdi->spec_offset,
8445 real_pdi->spec_is_dwz, cu);
8446 real_pdi = res.pdi;
8447 cu = res.cu;
8448 }
8449
8450 parent = real_pdi->die_parent;
8451 if (parent == NULL)
8452 return NULL;
8453
8454 if (parent->scope_set)
8455 return parent->scope;
8456
8457 parent->fixup (cu);
8458
8459 grandparent_scope = partial_die_parent_scope (parent, cu);
8460
8461 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8462 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8463 Work around this problem here. */
8464 if (cu->language == language_cplus
8465 && parent->tag == DW_TAG_namespace
8466 && strcmp (parent->name (cu), "::") == 0
8467 && grandparent_scope == NULL)
8468 {
8469 parent->scope = NULL;
8470 parent->scope_set = 1;
8471 return NULL;
8472 }
8473
8474 /* Nested subroutines in Fortran get a prefix. */
8475 if (pdi->tag == DW_TAG_enumerator)
8476 /* Enumerators should not get the name of the enumeration as a prefix. */
8477 parent->scope = grandparent_scope;
8478 else if (parent->tag == DW_TAG_namespace
8479 || parent->tag == DW_TAG_module
8480 || parent->tag == DW_TAG_structure_type
8481 || parent->tag == DW_TAG_class_type
8482 || parent->tag == DW_TAG_interface_type
8483 || parent->tag == DW_TAG_union_type
8484 || parent->tag == DW_TAG_enumeration_type
8485 || (cu->language == language_fortran
8486 && parent->tag == DW_TAG_subprogram
8487 && pdi->tag == DW_TAG_subprogram))
8488 {
8489 if (grandparent_scope == NULL)
8490 parent->scope = parent->name (cu);
8491 else
8492 parent->scope = typename_concat (&cu->comp_unit_obstack,
8493 grandparent_scope,
8494 parent->name (cu), 0, cu);
8495 }
8496 else
8497 {
8498 /* FIXME drow/2004-04-01: What should we be doing with
8499 function-local names? For partial symbols, we should probably be
8500 ignoring them. */
8501 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8502 dwarf_tag_name (parent->tag),
8503 sect_offset_str (pdi->sect_off));
8504 parent->scope = grandparent_scope;
8505 }
8506
8507 parent->scope_set = 1;
8508 return parent->scope;
8509 }
8510
8511 /* Return the fully scoped name associated with PDI, from compilation unit
8512 CU. The result will be allocated with malloc. */
8513
8514 static gdb::unique_xmalloc_ptr<char>
8515 partial_die_full_name (struct partial_die_info *pdi,
8516 struct dwarf2_cu *cu)
8517 {
8518 const char *parent_scope;
8519
8520 /* If this is a template instantiation, we can not work out the
8521 template arguments from partial DIEs. So, unfortunately, we have
8522 to go through the full DIEs. At least any work we do building
8523 types here will be reused if full symbols are loaded later. */
8524 if (pdi->has_template_arguments)
8525 {
8526 pdi->fixup (cu);
8527
8528 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
8529 {
8530 struct die_info *die;
8531 struct attribute attr;
8532 struct dwarf2_cu *ref_cu = cu;
8533
8534 /* DW_FORM_ref_addr is using section offset. */
8535 attr.name = (enum dwarf_attribute) 0;
8536 attr.form = DW_FORM_ref_addr;
8537 attr.u.unsnd = to_underlying (pdi->sect_off);
8538 die = follow_die_ref (NULL, &attr, &ref_cu);
8539
8540 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8541 }
8542 }
8543
8544 parent_scope = partial_die_parent_scope (pdi, cu);
8545 if (parent_scope == NULL)
8546 return NULL;
8547 else
8548 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8549 pdi->name (cu),
8550 0, cu));
8551 }
8552
8553 static void
8554 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8555 {
8556 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8557 struct objfile *objfile = per_objfile->objfile;
8558 struct gdbarch *gdbarch = objfile->arch ();
8559 CORE_ADDR addr = 0;
8560 const char *actual_name = NULL;
8561 CORE_ADDR baseaddr;
8562
8563 baseaddr = objfile->text_section_offset ();
8564
8565 gdb::unique_xmalloc_ptr<char> built_actual_name
8566 = partial_die_full_name (pdi, cu);
8567 if (built_actual_name != NULL)
8568 actual_name = built_actual_name.get ();
8569
8570 if (actual_name == NULL)
8571 actual_name = pdi->name (cu);
8572
8573 partial_symbol psymbol;
8574 memset (&psymbol, 0, sizeof (psymbol));
8575 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8576 psymbol.ginfo.set_section_index (-1);
8577
8578 /* The code below indicates that the psymbol should be installed by
8579 setting this. */
8580 gdb::optional<psymbol_placement> where;
8581
8582 switch (pdi->tag)
8583 {
8584 case DW_TAG_inlined_subroutine:
8585 case DW_TAG_subprogram:
8586 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8587 - baseaddr);
8588 if (pdi->is_external
8589 || cu->language == language_ada
8590 || (cu->language == language_fortran
8591 && pdi->die_parent != NULL
8592 && pdi->die_parent->tag == DW_TAG_subprogram))
8593 {
8594 /* Normally, only "external" DIEs are part of the global scope.
8595 But in Ada and Fortran, we want to be able to access nested
8596 procedures globally. So all Ada and Fortran subprograms are
8597 stored in the global scope. */
8598 where = psymbol_placement::GLOBAL;
8599 }
8600 else
8601 where = psymbol_placement::STATIC;
8602
8603 psymbol.domain = VAR_DOMAIN;
8604 psymbol.aclass = LOC_BLOCK;
8605 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8606 psymbol.ginfo.value.address = addr;
8607
8608 if (pdi->main_subprogram && actual_name != NULL)
8609 set_objfile_main_name (objfile, actual_name, cu->language);
8610 break;
8611 case DW_TAG_constant:
8612 psymbol.domain = VAR_DOMAIN;
8613 psymbol.aclass = LOC_STATIC;
8614 where = (pdi->is_external
8615 ? psymbol_placement::GLOBAL
8616 : psymbol_placement::STATIC);
8617 break;
8618 case DW_TAG_variable:
8619 if (pdi->d.locdesc)
8620 addr = decode_locdesc (pdi->d.locdesc, cu);
8621
8622 if (pdi->d.locdesc
8623 && addr == 0
8624 && !per_objfile->per_bfd->has_section_at_zero)
8625 {
8626 /* A global or static variable may also have been stripped
8627 out by the linker if unused, in which case its address
8628 will be nullified; do not add such variables into partial
8629 symbol table then. */
8630 }
8631 else if (pdi->is_external)
8632 {
8633 /* Global Variable.
8634 Don't enter into the minimal symbol tables as there is
8635 a minimal symbol table entry from the ELF symbols already.
8636 Enter into partial symbol table if it has a location
8637 descriptor or a type.
8638 If the location descriptor is missing, new_symbol will create
8639 a LOC_UNRESOLVED symbol, the address of the variable will then
8640 be determined from the minimal symbol table whenever the variable
8641 is referenced.
8642 The address for the partial symbol table entry is not
8643 used by GDB, but it comes in handy for debugging partial symbol
8644 table building. */
8645
8646 if (pdi->d.locdesc || pdi->has_type)
8647 {
8648 psymbol.domain = VAR_DOMAIN;
8649 psymbol.aclass = LOC_STATIC;
8650 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8651 psymbol.ginfo.value.address = addr;
8652 where = psymbol_placement::GLOBAL;
8653 }
8654 }
8655 else
8656 {
8657 int has_loc = pdi->d.locdesc != NULL;
8658
8659 /* Static Variable. Skip symbols whose value we cannot know (those
8660 without location descriptors or constant values). */
8661 if (!has_loc && !pdi->has_const_value)
8662 return;
8663
8664 psymbol.domain = VAR_DOMAIN;
8665 psymbol.aclass = LOC_STATIC;
8666 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8667 if (has_loc)
8668 psymbol.ginfo.value.address = addr;
8669 where = psymbol_placement::STATIC;
8670 }
8671 break;
8672 case DW_TAG_array_type:
8673 case DW_TAG_typedef:
8674 case DW_TAG_base_type:
8675 case DW_TAG_subrange_type:
8676 psymbol.domain = VAR_DOMAIN;
8677 psymbol.aclass = LOC_TYPEDEF;
8678 where = psymbol_placement::STATIC;
8679 break;
8680 case DW_TAG_imported_declaration:
8681 case DW_TAG_namespace:
8682 psymbol.domain = VAR_DOMAIN;
8683 psymbol.aclass = LOC_TYPEDEF;
8684 where = psymbol_placement::GLOBAL;
8685 break;
8686 case DW_TAG_module:
8687 /* With Fortran 77 there might be a "BLOCK DATA" module
8688 available without any name. If so, we skip the module as it
8689 doesn't bring any value. */
8690 if (actual_name != nullptr)
8691 {
8692 psymbol.domain = MODULE_DOMAIN;
8693 psymbol.aclass = LOC_TYPEDEF;
8694 where = psymbol_placement::GLOBAL;
8695 }
8696 break;
8697 case DW_TAG_class_type:
8698 case DW_TAG_interface_type:
8699 case DW_TAG_structure_type:
8700 case DW_TAG_union_type:
8701 case DW_TAG_enumeration_type:
8702 /* Skip external references. The DWARF standard says in the section
8703 about "Structure, Union, and Class Type Entries": "An incomplete
8704 structure, union or class type is represented by a structure,
8705 union or class entry that does not have a byte size attribute
8706 and that has a DW_AT_declaration attribute." */
8707 if (!pdi->has_byte_size && pdi->is_declaration)
8708 return;
8709
8710 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8711 static vs. global. */
8712 psymbol.domain = STRUCT_DOMAIN;
8713 psymbol.aclass = LOC_TYPEDEF;
8714 where = (cu->language == language_cplus
8715 ? psymbol_placement::GLOBAL
8716 : psymbol_placement::STATIC);
8717 break;
8718 case DW_TAG_enumerator:
8719 psymbol.domain = VAR_DOMAIN;
8720 psymbol.aclass = LOC_CONST;
8721 where = (cu->language == language_cplus
8722 ? psymbol_placement::GLOBAL
8723 : psymbol_placement::STATIC);
8724 break;
8725 default:
8726 break;
8727 }
8728
8729 if (where.has_value ())
8730 {
8731 if (built_actual_name != nullptr)
8732 actual_name = objfile->intern (actual_name);
8733 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8734 psymbol.ginfo.set_linkage_name (actual_name);
8735 else
8736 {
8737 psymbol.ginfo.set_demangled_name (actual_name,
8738 &objfile->objfile_obstack);
8739 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8740 }
8741 cu->per_cu->v.psymtab->add_psymbol
8742 (psymbol, *where, per_objfile->per_bfd->partial_symtabs.get (),
8743 objfile);
8744 }
8745 }
8746
8747 /* Read a partial die corresponding to a namespace; also, add a symbol
8748 corresponding to that namespace to the symbol table. NAMESPACE is
8749 the name of the enclosing namespace. */
8750
8751 static void
8752 add_partial_namespace (struct partial_die_info *pdi,
8753 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8754 int set_addrmap, struct dwarf2_cu *cu)
8755 {
8756 /* Add a symbol for the namespace. */
8757
8758 add_partial_symbol (pdi, cu);
8759
8760 /* Now scan partial symbols in that namespace. */
8761
8762 if (pdi->has_children)
8763 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8764 }
8765
8766 /* Read a partial die corresponding to a Fortran module. */
8767
8768 static void
8769 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8770 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8771 {
8772 /* Add a symbol for the namespace. */
8773
8774 add_partial_symbol (pdi, cu);
8775
8776 /* Now scan partial symbols in that module. */
8777
8778 if (pdi->has_children)
8779 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8780 }
8781
8782 /* Read a partial die corresponding to a subprogram or an inlined
8783 subprogram and create a partial symbol for that subprogram.
8784 When the CU language allows it, this routine also defines a partial
8785 symbol for each nested subprogram that this subprogram contains.
8786 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8787 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8788
8789 PDI may also be a lexical block, in which case we simply search
8790 recursively for subprograms defined inside that lexical block.
8791 Again, this is only performed when the CU language allows this
8792 type of definitions. */
8793
8794 static void
8795 add_partial_subprogram (struct partial_die_info *pdi,
8796 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8797 int set_addrmap, struct dwarf2_cu *cu)
8798 {
8799 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8800 {
8801 if (pdi->has_pc_info)
8802 {
8803 if (pdi->lowpc < *lowpc)
8804 *lowpc = pdi->lowpc;
8805 if (pdi->highpc > *highpc)
8806 *highpc = pdi->highpc;
8807 if (set_addrmap)
8808 {
8809 struct objfile *objfile = cu->per_objfile->objfile;
8810 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
8811 struct gdbarch *gdbarch = objfile->arch ();
8812 CORE_ADDR baseaddr;
8813 CORE_ADDR this_highpc;
8814 CORE_ADDR this_lowpc;
8815
8816 baseaddr = objfile->text_section_offset ();
8817 this_lowpc
8818 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8819 pdi->lowpc + baseaddr)
8820 - baseaddr);
8821 this_highpc
8822 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8823 pdi->highpc + baseaddr)
8824 - baseaddr);
8825 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
8826 this_lowpc, this_highpc - 1,
8827 cu->per_cu->v.psymtab);
8828 }
8829 }
8830
8831 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8832 {
8833 if (!pdi->is_declaration)
8834 /* Ignore subprogram DIEs that do not have a name, they are
8835 illegal. Do not emit a complaint at this point, we will
8836 do so when we convert this psymtab into a symtab. */
8837 if (pdi->name (cu))
8838 add_partial_symbol (pdi, cu);
8839 }
8840 }
8841
8842 if (! pdi->has_children)
8843 return;
8844
8845 if (cu->language == language_ada || cu->language == language_fortran)
8846 {
8847 pdi = pdi->die_child;
8848 while (pdi != NULL)
8849 {
8850 pdi->fixup (cu);
8851 if (pdi->tag == DW_TAG_subprogram
8852 || pdi->tag == DW_TAG_inlined_subroutine
8853 || pdi->tag == DW_TAG_lexical_block)
8854 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8855 pdi = pdi->die_sibling;
8856 }
8857 }
8858 }
8859
8860 /* Read a partial die corresponding to an enumeration type. */
8861
8862 static void
8863 add_partial_enumeration (struct partial_die_info *enum_pdi,
8864 struct dwarf2_cu *cu)
8865 {
8866 struct partial_die_info *pdi;
8867
8868 if (enum_pdi->name (cu) != NULL)
8869 add_partial_symbol (enum_pdi, cu);
8870
8871 pdi = enum_pdi->die_child;
8872 while (pdi)
8873 {
8874 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8875 complaint (_("malformed enumerator DIE ignored"));
8876 else
8877 add_partial_symbol (pdi, cu);
8878 pdi = pdi->die_sibling;
8879 }
8880 }
8881
8882 /* Return the initial uleb128 in the die at INFO_PTR. */
8883
8884 static unsigned int
8885 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8886 {
8887 unsigned int bytes_read;
8888
8889 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8890 }
8891
8892 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8893 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8894
8895 Return the corresponding abbrev, or NULL if the number is zero (indicating
8896 an empty DIE). In either case *BYTES_READ will be set to the length of
8897 the initial number. */
8898
8899 static const struct abbrev_info *
8900 peek_die_abbrev (const die_reader_specs &reader,
8901 const gdb_byte *info_ptr, unsigned int *bytes_read)
8902 {
8903 dwarf2_cu *cu = reader.cu;
8904 bfd *abfd = reader.abfd;
8905 unsigned int abbrev_number
8906 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8907
8908 if (abbrev_number == 0)
8909 return NULL;
8910
8911 const abbrev_info *abbrev
8912 = reader.abbrev_table->lookup_abbrev (abbrev_number);
8913 if (!abbrev)
8914 {
8915 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8916 " at offset %s [in module %s]"),
8917 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8918 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8919 }
8920
8921 return abbrev;
8922 }
8923
8924 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8925 Returns a pointer to the end of a series of DIEs, terminated by an empty
8926 DIE. Any children of the skipped DIEs will also be skipped. */
8927
8928 static const gdb_byte *
8929 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8930 {
8931 while (1)
8932 {
8933 unsigned int bytes_read;
8934 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8935 &bytes_read);
8936
8937 if (abbrev == NULL)
8938 return info_ptr + bytes_read;
8939 else
8940 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8941 }
8942 }
8943
8944 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8945 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8946 abbrev corresponding to that skipped uleb128 should be passed in
8947 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8948 children. */
8949
8950 static const gdb_byte *
8951 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8952 const struct abbrev_info *abbrev)
8953 {
8954 unsigned int bytes_read;
8955 struct attribute attr;
8956 bfd *abfd = reader->abfd;
8957 struct dwarf2_cu *cu = reader->cu;
8958 const gdb_byte *buffer = reader->buffer;
8959 const gdb_byte *buffer_end = reader->buffer_end;
8960 unsigned int form, i;
8961
8962 for (i = 0; i < abbrev->num_attrs; i++)
8963 {
8964 /* The only abbrev we care about is DW_AT_sibling. */
8965 if (abbrev->attrs[i].name == DW_AT_sibling)
8966 {
8967 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8968 if (attr.form == DW_FORM_ref_addr)
8969 complaint (_("ignoring absolute DW_AT_sibling"));
8970 else
8971 {
8972 sect_offset off = attr.get_ref_die_offset ();
8973 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8974
8975 if (sibling_ptr < info_ptr)
8976 complaint (_("DW_AT_sibling points backwards"));
8977 else if (sibling_ptr > reader->buffer_end)
8978 reader->die_section->overflow_complaint ();
8979 else
8980 return sibling_ptr;
8981 }
8982 }
8983
8984 /* If it isn't DW_AT_sibling, skip this attribute. */
8985 form = abbrev->attrs[i].form;
8986 skip_attribute:
8987 switch (form)
8988 {
8989 case DW_FORM_ref_addr:
8990 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8991 and later it is offset sized. */
8992 if (cu->header.version == 2)
8993 info_ptr += cu->header.addr_size;
8994 else
8995 info_ptr += cu->header.offset_size;
8996 break;
8997 case DW_FORM_GNU_ref_alt:
8998 info_ptr += cu->header.offset_size;
8999 break;
9000 case DW_FORM_addr:
9001 info_ptr += cu->header.addr_size;
9002 break;
9003 case DW_FORM_data1:
9004 case DW_FORM_ref1:
9005 case DW_FORM_flag:
9006 case DW_FORM_strx1:
9007 info_ptr += 1;
9008 break;
9009 case DW_FORM_flag_present:
9010 case DW_FORM_implicit_const:
9011 break;
9012 case DW_FORM_data2:
9013 case DW_FORM_ref2:
9014 case DW_FORM_strx2:
9015 info_ptr += 2;
9016 break;
9017 case DW_FORM_strx3:
9018 info_ptr += 3;
9019 break;
9020 case DW_FORM_data4:
9021 case DW_FORM_ref4:
9022 case DW_FORM_strx4:
9023 info_ptr += 4;
9024 break;
9025 case DW_FORM_data8:
9026 case DW_FORM_ref8:
9027 case DW_FORM_ref_sig8:
9028 info_ptr += 8;
9029 break;
9030 case DW_FORM_data16:
9031 info_ptr += 16;
9032 break;
9033 case DW_FORM_string:
9034 read_direct_string (abfd, info_ptr, &bytes_read);
9035 info_ptr += bytes_read;
9036 break;
9037 case DW_FORM_sec_offset:
9038 case DW_FORM_strp:
9039 case DW_FORM_GNU_strp_alt:
9040 info_ptr += cu->header.offset_size;
9041 break;
9042 case DW_FORM_exprloc:
9043 case DW_FORM_block:
9044 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9045 info_ptr += bytes_read;
9046 break;
9047 case DW_FORM_block1:
9048 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9049 break;
9050 case DW_FORM_block2:
9051 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9052 break;
9053 case DW_FORM_block4:
9054 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9055 break;
9056 case DW_FORM_addrx:
9057 case DW_FORM_strx:
9058 case DW_FORM_sdata:
9059 case DW_FORM_udata:
9060 case DW_FORM_ref_udata:
9061 case DW_FORM_GNU_addr_index:
9062 case DW_FORM_GNU_str_index:
9063 case DW_FORM_rnglistx:
9064 case DW_FORM_loclistx:
9065 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9066 break;
9067 case DW_FORM_indirect:
9068 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9069 info_ptr += bytes_read;
9070 /* We need to continue parsing from here, so just go back to
9071 the top. */
9072 goto skip_attribute;
9073
9074 default:
9075 error (_("Dwarf Error: Cannot handle %s "
9076 "in DWARF reader [in module %s]"),
9077 dwarf_form_name (form),
9078 bfd_get_filename (abfd));
9079 }
9080 }
9081
9082 if (abbrev->has_children)
9083 return skip_children (reader, info_ptr);
9084 else
9085 return info_ptr;
9086 }
9087
9088 /* Locate ORIG_PDI's sibling.
9089 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9090
9091 static const gdb_byte *
9092 locate_pdi_sibling (const struct die_reader_specs *reader,
9093 struct partial_die_info *orig_pdi,
9094 const gdb_byte *info_ptr)
9095 {
9096 /* Do we know the sibling already? */
9097
9098 if (orig_pdi->sibling)
9099 return orig_pdi->sibling;
9100
9101 /* Are there any children to deal with? */
9102
9103 if (!orig_pdi->has_children)
9104 return info_ptr;
9105
9106 /* Skip the children the long way. */
9107
9108 return skip_children (reader, info_ptr);
9109 }
9110
9111 /* Expand this partial symbol table into a full symbol table. SELF is
9112 not NULL. */
9113
9114 void
9115 dwarf2_psymtab::read_symtab (struct objfile *objfile)
9116 {
9117 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9118
9119 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
9120
9121 /* If this psymtab is constructed from a debug-only objfile, the
9122 has_section_at_zero flag will not necessarily be correct. We
9123 can get the correct value for this flag by looking at the data
9124 associated with the (presumably stripped) associated objfile. */
9125 if (objfile->separate_debug_objfile_backlink)
9126 {
9127 dwarf2_per_objfile *per_objfile_backlink
9128 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9129
9130 per_objfile->per_bfd->has_section_at_zero
9131 = per_objfile_backlink->per_bfd->has_section_at_zero;
9132 }
9133
9134 expand_psymtab (objfile);
9135
9136 process_cu_includes (per_objfile);
9137 }
9138 \f
9139 /* Reading in full CUs. */
9140
9141 /* Add PER_CU to the queue. */
9142
9143 static void
9144 queue_comp_unit (dwarf2_per_cu_data *per_cu,
9145 dwarf2_per_objfile *per_objfile,
9146 enum language pretend_language)
9147 {
9148 per_cu->queued = 1;
9149
9150 gdb_assert (per_objfile->per_bfd->queue.has_value ());
9151 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
9152 }
9153
9154 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
9155
9156 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9157 dependency.
9158
9159 Return true if maybe_queue_comp_unit requires the caller to load the CU's
9160 DIEs, false otherwise.
9161
9162 Explanation: there is an invariant that if a CU is queued for expansion
9163 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
9164 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
9165 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
9166 are not yet loaded, the the caller must load the CU's DIEs to ensure the
9167 invariant is respected.
9168
9169 The caller is therefore not required to load the CU's DIEs (we return false)
9170 if:
9171
9172 - the CU is already expanded, and therefore does not get enqueued
9173 - the CU gets enqueued for expansion, but its DIEs are already loaded
9174
9175 Note that the caller should not use this function's return value as an
9176 indicator of whether the CU's DIEs are loaded right now, it should check
9177 that by calling `dwarf2_per_objfile::get_cu` instead. */
9178
9179 static int
9180 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9181 dwarf2_per_cu_data *per_cu,
9182 dwarf2_per_objfile *per_objfile,
9183 enum language pretend_language)
9184 {
9185 /* We may arrive here during partial symbol reading, if we need full
9186 DIEs to process an unusual case (e.g. template arguments). Do
9187 not queue PER_CU, just tell our caller to load its DIEs. */
9188 if (per_cu->per_bfd->reading_partial_symbols)
9189 {
9190 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9191
9192 if (cu == NULL || cu->dies == NULL)
9193 return 1;
9194 return 0;
9195 }
9196
9197 /* Mark the dependence relation so that we don't flush PER_CU
9198 too early. */
9199 if (dependent_cu != NULL)
9200 dwarf2_add_dependence (dependent_cu, per_cu);
9201
9202 /* If it's already on the queue, we have nothing to do. */
9203 if (per_cu->queued)
9204 {
9205 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
9206 loaded. */
9207 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
9208
9209 /* If the CU is queued for expansion, it should not already be
9210 expanded. */
9211 gdb_assert (!per_objfile->symtab_set_p (per_cu));
9212
9213 /* The DIEs are already loaded, the caller doesn't need to do it. */
9214 return 0;
9215 }
9216
9217 bool queued = false;
9218 if (!per_objfile->symtab_set_p (per_cu))
9219 {
9220 /* Add it to the queue. */
9221 queue_comp_unit (per_cu, per_objfile, pretend_language);
9222 queued = true;
9223 }
9224
9225 /* If the compilation unit is already loaded, just mark it as
9226 used. */
9227 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9228 if (cu != nullptr)
9229 cu->last_used = 0;
9230
9231 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
9232 and the DIEs are not already loaded. */
9233 return queued && cu == nullptr;
9234 }
9235
9236 /* Process the queue. */
9237
9238 static void
9239 process_queue (dwarf2_per_objfile *per_objfile)
9240 {
9241 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
9242 objfile_name (per_objfile->objfile));
9243
9244 /* The queue starts out with one item, but following a DIE reference
9245 may load a new CU, adding it to the end of the queue. */
9246 while (!per_objfile->per_bfd->queue->empty ())
9247 {
9248 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
9249 dwarf2_per_cu_data *per_cu = item.per_cu;
9250
9251 if (!per_objfile->symtab_set_p (per_cu))
9252 {
9253 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9254
9255 /* Skip dummy CUs. */
9256 if (cu != nullptr)
9257 {
9258 unsigned int debug_print_threshold;
9259 char buf[100];
9260
9261 if (per_cu->is_debug_types)
9262 {
9263 struct signatured_type *sig_type =
9264 (struct signatured_type *) per_cu;
9265
9266 sprintf (buf, "TU %s at offset %s",
9267 hex_string (sig_type->signature),
9268 sect_offset_str (per_cu->sect_off));
9269 /* There can be 100s of TUs.
9270 Only print them in verbose mode. */
9271 debug_print_threshold = 2;
9272 }
9273 else
9274 {
9275 sprintf (buf, "CU at offset %s",
9276 sect_offset_str (per_cu->sect_off));
9277 debug_print_threshold = 1;
9278 }
9279
9280 if (dwarf_read_debug >= debug_print_threshold)
9281 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
9282
9283 if (per_cu->is_debug_types)
9284 process_full_type_unit (cu, item.pretend_language);
9285 else
9286 process_full_comp_unit (cu, item.pretend_language);
9287
9288 if (dwarf_read_debug >= debug_print_threshold)
9289 dwarf_read_debug_printf ("Done expanding %s", buf);
9290 }
9291 }
9292
9293 per_cu->queued = 0;
9294 per_objfile->per_bfd->queue->pop ();
9295 }
9296
9297 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
9298 objfile_name (per_objfile->objfile));
9299 }
9300
9301 /* Read in full symbols for PST, and anything it depends on. */
9302
9303 void
9304 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9305 {
9306 gdb_assert (!readin_p (objfile));
9307
9308 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9309 free_cached_comp_units freer (per_objfile);
9310 expand_dependencies (objfile);
9311
9312 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9313 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9314 }
9315
9316 /* See psympriv.h. */
9317
9318 bool
9319 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9320 {
9321 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9322 return per_objfile->symtab_set_p (per_cu_data);
9323 }
9324
9325 /* See psympriv.h. */
9326
9327 compunit_symtab *
9328 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9329 {
9330 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9331 return per_objfile->get_symtab (per_cu_data);
9332 }
9333
9334 /* Trivial hash function for die_info: the hash value of a DIE
9335 is its offset in .debug_info for this objfile. */
9336
9337 static hashval_t
9338 die_hash (const void *item)
9339 {
9340 const struct die_info *die = (const struct die_info *) item;
9341
9342 return to_underlying (die->sect_off);
9343 }
9344
9345 /* Trivial comparison function for die_info structures: two DIEs
9346 are equal if they have the same offset. */
9347
9348 static int
9349 die_eq (const void *item_lhs, const void *item_rhs)
9350 {
9351 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9352 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9353
9354 return die_lhs->sect_off == die_rhs->sect_off;
9355 }
9356
9357 /* Load the DIEs associated with PER_CU into memory.
9358
9359 In some cases, the caller, while reading partial symbols, will need to load
9360 the full symbols for the CU for some reason. It will already have a
9361 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
9362 rather than creating a new one. */
9363
9364 static void
9365 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9366 dwarf2_per_objfile *per_objfile,
9367 dwarf2_cu *existing_cu,
9368 bool skip_partial,
9369 enum language pretend_language)
9370 {
9371 gdb_assert (! this_cu->is_debug_types);
9372
9373 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
9374 if (reader.dummy_p)
9375 return;
9376
9377 struct dwarf2_cu *cu = reader.cu;
9378 const gdb_byte *info_ptr = reader.info_ptr;
9379
9380 gdb_assert (cu->die_hash == NULL);
9381 cu->die_hash =
9382 htab_create_alloc_ex (cu->header.length / 12,
9383 die_hash,
9384 die_eq,
9385 NULL,
9386 &cu->comp_unit_obstack,
9387 hashtab_obstack_allocate,
9388 dummy_obstack_deallocate);
9389
9390 if (reader.comp_unit_die->has_children)
9391 reader.comp_unit_die->child
9392 = read_die_and_siblings (&reader, reader.info_ptr,
9393 &info_ptr, reader.comp_unit_die);
9394 cu->dies = reader.comp_unit_die;
9395 /* comp_unit_die is not stored in die_hash, no need. */
9396
9397 /* We try not to read any attributes in this function, because not
9398 all CUs needed for references have been loaded yet, and symbol
9399 table processing isn't initialized. But we have to set the CU language,
9400 or we won't be able to build types correctly.
9401 Similarly, if we do not read the producer, we can not apply
9402 producer-specific interpretation. */
9403 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9404
9405 reader.keep ();
9406 }
9407
9408 /* Add a DIE to the delayed physname list. */
9409
9410 static void
9411 add_to_method_list (struct type *type, int fnfield_index, int index,
9412 const char *name, struct die_info *die,
9413 struct dwarf2_cu *cu)
9414 {
9415 struct delayed_method_info mi;
9416 mi.type = type;
9417 mi.fnfield_index = fnfield_index;
9418 mi.index = index;
9419 mi.name = name;
9420 mi.die = die;
9421 cu->method_list.push_back (mi);
9422 }
9423
9424 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9425 "const" / "volatile". If so, decrements LEN by the length of the
9426 modifier and return true. Otherwise return false. */
9427
9428 template<size_t N>
9429 static bool
9430 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9431 {
9432 size_t mod_len = sizeof (mod) - 1;
9433 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9434 {
9435 len -= mod_len;
9436 return true;
9437 }
9438 return false;
9439 }
9440
9441 /* Compute the physnames of any methods on the CU's method list.
9442
9443 The computation of method physnames is delayed in order to avoid the
9444 (bad) condition that one of the method's formal parameters is of an as yet
9445 incomplete type. */
9446
9447 static void
9448 compute_delayed_physnames (struct dwarf2_cu *cu)
9449 {
9450 /* Only C++ delays computing physnames. */
9451 if (cu->method_list.empty ())
9452 return;
9453 gdb_assert (cu->language == language_cplus);
9454
9455 for (const delayed_method_info &mi : cu->method_list)
9456 {
9457 const char *physname;
9458 struct fn_fieldlist *fn_flp
9459 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9460 physname = dwarf2_physname (mi.name, mi.die, cu);
9461 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9462 = physname ? physname : "";
9463
9464 /* Since there's no tag to indicate whether a method is a
9465 const/volatile overload, extract that information out of the
9466 demangled name. */
9467 if (physname != NULL)
9468 {
9469 size_t len = strlen (physname);
9470
9471 while (1)
9472 {
9473 if (physname[len] == ')') /* shortcut */
9474 break;
9475 else if (check_modifier (physname, len, " const"))
9476 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9477 else if (check_modifier (physname, len, " volatile"))
9478 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9479 else
9480 break;
9481 }
9482 }
9483 }
9484
9485 /* The list is no longer needed. */
9486 cu->method_list.clear ();
9487 }
9488
9489 /* Go objects should be embedded in a DW_TAG_module DIE,
9490 and it's not clear if/how imported objects will appear.
9491 To keep Go support simple until that's worked out,
9492 go back through what we've read and create something usable.
9493 We could do this while processing each DIE, and feels kinda cleaner,
9494 but that way is more invasive.
9495 This is to, for example, allow the user to type "p var" or "b main"
9496 without having to specify the package name, and allow lookups
9497 of module.object to work in contexts that use the expression
9498 parser. */
9499
9500 static void
9501 fixup_go_packaging (struct dwarf2_cu *cu)
9502 {
9503 gdb::unique_xmalloc_ptr<char> package_name;
9504 struct pending *list;
9505 int i;
9506
9507 for (list = *cu->get_builder ()->get_global_symbols ();
9508 list != NULL;
9509 list = list->next)
9510 {
9511 for (i = 0; i < list->nsyms; ++i)
9512 {
9513 struct symbol *sym = list->symbol[i];
9514
9515 if (sym->language () == language_go
9516 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9517 {
9518 gdb::unique_xmalloc_ptr<char> this_package_name
9519 (go_symbol_package_name (sym));
9520
9521 if (this_package_name == NULL)
9522 continue;
9523 if (package_name == NULL)
9524 package_name = std::move (this_package_name);
9525 else
9526 {
9527 struct objfile *objfile = cu->per_objfile->objfile;
9528 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9529 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9530 (symbol_symtab (sym) != NULL
9531 ? symtab_to_filename_for_display
9532 (symbol_symtab (sym))
9533 : objfile_name (objfile)),
9534 this_package_name.get (), package_name.get ());
9535 }
9536 }
9537 }
9538 }
9539
9540 if (package_name != NULL)
9541 {
9542 struct objfile *objfile = cu->per_objfile->objfile;
9543 const char *saved_package_name = objfile->intern (package_name.get ());
9544 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9545 saved_package_name);
9546 struct symbol *sym;
9547
9548 sym = new (&objfile->objfile_obstack) symbol;
9549 sym->set_language (language_go, &objfile->objfile_obstack);
9550 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9551 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9552 e.g., "main" finds the "main" module and not C's main(). */
9553 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9554 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9555 SYMBOL_TYPE (sym) = type;
9556
9557 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9558 }
9559 }
9560
9561 /* Allocate a fully-qualified name consisting of the two parts on the
9562 obstack. */
9563
9564 static const char *
9565 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9566 {
9567 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9568 }
9569
9570 /* A helper that allocates a variant part to attach to a Rust enum
9571 type. OBSTACK is where the results should be allocated. TYPE is
9572 the type we're processing. DISCRIMINANT_INDEX is the index of the
9573 discriminant. It must be the index of one of the fields of TYPE,
9574 or -1 to mean there is no discriminant (univariant enum).
9575 DEFAULT_INDEX is the index of the default field; or -1 if there is
9576 no default. RANGES is indexed by "effective" field number (the
9577 field index, but omitting the discriminant and default fields) and
9578 must hold the discriminant values used by the variants. Note that
9579 RANGES must have a lifetime at least as long as OBSTACK -- either
9580 already allocated on it, or static. */
9581
9582 static void
9583 alloc_rust_variant (struct obstack *obstack, struct type *type,
9584 int discriminant_index, int default_index,
9585 gdb::array_view<discriminant_range> ranges)
9586 {
9587 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9588 gdb_assert (discriminant_index == -1
9589 || (discriminant_index >= 0
9590 && discriminant_index < type->num_fields ()));
9591 gdb_assert (default_index == -1
9592 || (default_index >= 0 && default_index < type->num_fields ()));
9593
9594 /* We have one variant for each non-discriminant field. */
9595 int n_variants = type->num_fields ();
9596 if (discriminant_index != -1)
9597 --n_variants;
9598
9599 variant *variants = new (obstack) variant[n_variants];
9600 int var_idx = 0;
9601 int range_idx = 0;
9602 for (int i = 0; i < type->num_fields (); ++i)
9603 {
9604 if (i == discriminant_index)
9605 continue;
9606
9607 variants[var_idx].first_field = i;
9608 variants[var_idx].last_field = i + 1;
9609
9610 /* The default field does not need a range, but other fields do.
9611 We skipped the discriminant above. */
9612 if (i != default_index)
9613 {
9614 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9615 ++range_idx;
9616 }
9617
9618 ++var_idx;
9619 }
9620
9621 gdb_assert (range_idx == ranges.size ());
9622 gdb_assert (var_idx == n_variants);
9623
9624 variant_part *part = new (obstack) variant_part;
9625 part->discriminant_index = discriminant_index;
9626 /* If there is no discriminant, then whether it is signed is of no
9627 consequence. */
9628 part->is_unsigned
9629 = (discriminant_index == -1
9630 ? false
9631 : type->field (discriminant_index).type ()->is_unsigned ());
9632 part->variants = gdb::array_view<variant> (variants, n_variants);
9633
9634 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9635 gdb::array_view<variant_part> *prop_value
9636 = new (storage) gdb::array_view<variant_part> (part, 1);
9637
9638 struct dynamic_prop prop;
9639 prop.set_variant_parts (prop_value);
9640
9641 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9642 }
9643
9644 /* Some versions of rustc emitted enums in an unusual way.
9645
9646 Ordinary enums were emitted as unions. The first element of each
9647 structure in the union was named "RUST$ENUM$DISR". This element
9648 held the discriminant.
9649
9650 These versions of Rust also implemented the "non-zero"
9651 optimization. When the enum had two values, and one is empty and
9652 the other holds a pointer that cannot be zero, the pointer is used
9653 as the discriminant, with a zero value meaning the empty variant.
9654 Here, the union's first member is of the form
9655 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9656 where the fieldnos are the indices of the fields that should be
9657 traversed in order to find the field (which may be several fields deep)
9658 and the variantname is the name of the variant of the case when the
9659 field is zero.
9660
9661 This function recognizes whether TYPE is of one of these forms,
9662 and, if so, smashes it to be a variant type. */
9663
9664 static void
9665 quirk_rust_enum (struct type *type, struct objfile *objfile)
9666 {
9667 gdb_assert (type->code () == TYPE_CODE_UNION);
9668
9669 /* We don't need to deal with empty enums. */
9670 if (type->num_fields () == 0)
9671 return;
9672
9673 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9674 if (type->num_fields () == 1
9675 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9676 {
9677 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9678
9679 /* Decode the field name to find the offset of the
9680 discriminant. */
9681 ULONGEST bit_offset = 0;
9682 struct type *field_type = type->field (0).type ();
9683 while (name[0] >= '0' && name[0] <= '9')
9684 {
9685 char *tail;
9686 unsigned long index = strtoul (name, &tail, 10);
9687 name = tail;
9688 if (*name != '$'
9689 || index >= field_type->num_fields ()
9690 || (TYPE_FIELD_LOC_KIND (field_type, index)
9691 != FIELD_LOC_KIND_BITPOS))
9692 {
9693 complaint (_("Could not parse Rust enum encoding string \"%s\""
9694 "[in module %s]"),
9695 TYPE_FIELD_NAME (type, 0),
9696 objfile_name (objfile));
9697 return;
9698 }
9699 ++name;
9700
9701 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9702 field_type = field_type->field (index).type ();
9703 }
9704
9705 /* Smash this type to be a structure type. We have to do this
9706 because the type has already been recorded. */
9707 type->set_code (TYPE_CODE_STRUCT);
9708 type->set_num_fields (3);
9709 /* Save the field we care about. */
9710 struct field saved_field = type->field (0);
9711 type->set_fields
9712 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9713
9714 /* Put the discriminant at index 0. */
9715 type->field (0).set_type (field_type);
9716 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9717 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9718 SET_FIELD_BITPOS (type->field (0), bit_offset);
9719
9720 /* The order of fields doesn't really matter, so put the real
9721 field at index 1 and the data-less field at index 2. */
9722 type->field (1) = saved_field;
9723 TYPE_FIELD_NAME (type, 1)
9724 = rust_last_path_segment (type->field (1).type ()->name ());
9725 type->field (1).type ()->set_name
9726 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9727 TYPE_FIELD_NAME (type, 1)));
9728
9729 const char *dataless_name
9730 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9731 name);
9732 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9733 dataless_name);
9734 type->field (2).set_type (dataless_type);
9735 /* NAME points into the original discriminant name, which
9736 already has the correct lifetime. */
9737 TYPE_FIELD_NAME (type, 2) = name;
9738 SET_FIELD_BITPOS (type->field (2), 0);
9739
9740 /* Indicate that this is a variant type. */
9741 static discriminant_range ranges[1] = { { 0, 0 } };
9742 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9743 }
9744 /* A union with a single anonymous field is probably an old-style
9745 univariant enum. */
9746 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9747 {
9748 /* Smash this type to be a structure type. We have to do this
9749 because the type has already been recorded. */
9750 type->set_code (TYPE_CODE_STRUCT);
9751
9752 struct type *field_type = type->field (0).type ();
9753 const char *variant_name
9754 = rust_last_path_segment (field_type->name ());
9755 TYPE_FIELD_NAME (type, 0) = variant_name;
9756 field_type->set_name
9757 (rust_fully_qualify (&objfile->objfile_obstack,
9758 type->name (), variant_name));
9759
9760 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
9761 }
9762 else
9763 {
9764 struct type *disr_type = nullptr;
9765 for (int i = 0; i < type->num_fields (); ++i)
9766 {
9767 disr_type = type->field (i).type ();
9768
9769 if (disr_type->code () != TYPE_CODE_STRUCT)
9770 {
9771 /* All fields of a true enum will be structs. */
9772 return;
9773 }
9774 else if (disr_type->num_fields () == 0)
9775 {
9776 /* Could be data-less variant, so keep going. */
9777 disr_type = nullptr;
9778 }
9779 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9780 "RUST$ENUM$DISR") != 0)
9781 {
9782 /* Not a Rust enum. */
9783 return;
9784 }
9785 else
9786 {
9787 /* Found one. */
9788 break;
9789 }
9790 }
9791
9792 /* If we got here without a discriminant, then it's probably
9793 just a union. */
9794 if (disr_type == nullptr)
9795 return;
9796
9797 /* Smash this type to be a structure type. We have to do this
9798 because the type has already been recorded. */
9799 type->set_code (TYPE_CODE_STRUCT);
9800
9801 /* Make space for the discriminant field. */
9802 struct field *disr_field = &disr_type->field (0);
9803 field *new_fields
9804 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9805 * sizeof (struct field)));
9806 memcpy (new_fields + 1, type->fields (),
9807 type->num_fields () * sizeof (struct field));
9808 type->set_fields (new_fields);
9809 type->set_num_fields (type->num_fields () + 1);
9810
9811 /* Install the discriminant at index 0 in the union. */
9812 type->field (0) = *disr_field;
9813 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9814 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9815
9816 /* We need a way to find the correct discriminant given a
9817 variant name. For convenience we build a map here. */
9818 struct type *enum_type = disr_field->type ();
9819 std::unordered_map<std::string, ULONGEST> discriminant_map;
9820 for (int i = 0; i < enum_type->num_fields (); ++i)
9821 {
9822 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9823 {
9824 const char *name
9825 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9826 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9827 }
9828 }
9829
9830 int n_fields = type->num_fields ();
9831 /* We don't need a range entry for the discriminant, but we do
9832 need one for every other field, as there is no default
9833 variant. */
9834 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9835 discriminant_range,
9836 n_fields - 1);
9837 /* Skip the discriminant here. */
9838 for (int i = 1; i < n_fields; ++i)
9839 {
9840 /* Find the final word in the name of this variant's type.
9841 That name can be used to look up the correct
9842 discriminant. */
9843 const char *variant_name
9844 = rust_last_path_segment (type->field (i).type ()->name ());
9845
9846 auto iter = discriminant_map.find (variant_name);
9847 if (iter != discriminant_map.end ())
9848 {
9849 ranges[i - 1].low = iter->second;
9850 ranges[i - 1].high = iter->second;
9851 }
9852
9853 /* In Rust, each element should have the size of the
9854 enclosing enum. */
9855 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9856
9857 /* Remove the discriminant field, if it exists. */
9858 struct type *sub_type = type->field (i).type ();
9859 if (sub_type->num_fields () > 0)
9860 {
9861 sub_type->set_num_fields (sub_type->num_fields () - 1);
9862 sub_type->set_fields (sub_type->fields () + 1);
9863 }
9864 TYPE_FIELD_NAME (type, i) = variant_name;
9865 sub_type->set_name
9866 (rust_fully_qualify (&objfile->objfile_obstack,
9867 type->name (), variant_name));
9868 }
9869
9870 /* Indicate that this is a variant type. */
9871 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9872 gdb::array_view<discriminant_range> (ranges,
9873 n_fields - 1));
9874 }
9875 }
9876
9877 /* Rewrite some Rust unions to be structures with variants parts. */
9878
9879 static void
9880 rust_union_quirks (struct dwarf2_cu *cu)
9881 {
9882 gdb_assert (cu->language == language_rust);
9883 for (type *type_ : cu->rust_unions)
9884 quirk_rust_enum (type_, cu->per_objfile->objfile);
9885 /* We don't need this any more. */
9886 cu->rust_unions.clear ();
9887 }
9888
9889 /* See read.h. */
9890
9891 type_unit_group_unshareable *
9892 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9893 {
9894 auto iter = this->m_type_units.find (tu_group);
9895 if (iter != this->m_type_units.end ())
9896 return iter->second.get ();
9897
9898 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9899 type_unit_group_unshareable *result = uniq.get ();
9900 this->m_type_units[tu_group] = std::move (uniq);
9901 return result;
9902 }
9903
9904 struct type *
9905 dwarf2_per_objfile::get_type_for_signatured_type
9906 (signatured_type *sig_type) const
9907 {
9908 auto iter = this->m_type_map.find (sig_type);
9909 if (iter == this->m_type_map.end ())
9910 return nullptr;
9911
9912 return iter->second;
9913 }
9914
9915 void dwarf2_per_objfile::set_type_for_signatured_type
9916 (signatured_type *sig_type, struct type *type)
9917 {
9918 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9919
9920 this->m_type_map[sig_type] = type;
9921 }
9922
9923 /* A helper function for computing the list of all symbol tables
9924 included by PER_CU. */
9925
9926 static void
9927 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9928 htab_t all_children, htab_t all_type_symtabs,
9929 dwarf2_per_cu_data *per_cu,
9930 dwarf2_per_objfile *per_objfile,
9931 struct compunit_symtab *immediate_parent)
9932 {
9933 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9934 if (*slot != NULL)
9935 {
9936 /* This inclusion and its children have been processed. */
9937 return;
9938 }
9939
9940 *slot = per_cu;
9941
9942 /* Only add a CU if it has a symbol table. */
9943 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9944 if (cust != NULL)
9945 {
9946 /* If this is a type unit only add its symbol table if we haven't
9947 seen it yet (type unit per_cu's can share symtabs). */
9948 if (per_cu->is_debug_types)
9949 {
9950 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9951 if (*slot == NULL)
9952 {
9953 *slot = cust;
9954 result->push_back (cust);
9955 if (cust->user == NULL)
9956 cust->user = immediate_parent;
9957 }
9958 }
9959 else
9960 {
9961 result->push_back (cust);
9962 if (cust->user == NULL)
9963 cust->user = immediate_parent;
9964 }
9965 }
9966
9967 if (!per_cu->imported_symtabs_empty ())
9968 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9969 {
9970 recursively_compute_inclusions (result, all_children,
9971 all_type_symtabs, ptr, per_objfile,
9972 cust);
9973 }
9974 }
9975
9976 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9977 PER_CU. */
9978
9979 static void
9980 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9981 dwarf2_per_objfile *per_objfile)
9982 {
9983 gdb_assert (! per_cu->is_debug_types);
9984
9985 if (!per_cu->imported_symtabs_empty ())
9986 {
9987 int len;
9988 std::vector<compunit_symtab *> result_symtabs;
9989 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9990
9991 /* If we don't have a symtab, we can just skip this case. */
9992 if (cust == NULL)
9993 return;
9994
9995 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9996 htab_eq_pointer,
9997 NULL, xcalloc, xfree));
9998 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9999 htab_eq_pointer,
10000 NULL, xcalloc, xfree));
10001
10002 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10003 {
10004 recursively_compute_inclusions (&result_symtabs, all_children.get (),
10005 all_type_symtabs.get (), ptr,
10006 per_objfile, cust);
10007 }
10008
10009 /* Now we have a transitive closure of all the included symtabs. */
10010 len = result_symtabs.size ();
10011 cust->includes
10012 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
10013 struct compunit_symtab *, len + 1);
10014 memcpy (cust->includes, result_symtabs.data (),
10015 len * sizeof (compunit_symtab *));
10016 cust->includes[len] = NULL;
10017 }
10018 }
10019
10020 /* Compute the 'includes' field for the symtabs of all the CUs we just
10021 read. */
10022
10023 static void
10024 process_cu_includes (dwarf2_per_objfile *per_objfile)
10025 {
10026 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
10027 {
10028 if (! iter->is_debug_types)
10029 compute_compunit_symtab_includes (iter, per_objfile);
10030 }
10031
10032 per_objfile->per_bfd->just_read_cus.clear ();
10033 }
10034
10035 /* Generate full symbol information for CU, whose DIEs have
10036 already been loaded into memory. */
10037
10038 static void
10039 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10040 {
10041 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10042 struct objfile *objfile = per_objfile->objfile;
10043 struct gdbarch *gdbarch = objfile->arch ();
10044 CORE_ADDR lowpc, highpc;
10045 struct compunit_symtab *cust;
10046 CORE_ADDR baseaddr;
10047 struct block *static_block;
10048 CORE_ADDR addr;
10049
10050 baseaddr = objfile->text_section_offset ();
10051
10052 /* Clear the list here in case something was left over. */
10053 cu->method_list.clear ();
10054
10055 cu->language = pretend_language;
10056 cu->language_defn = language_def (cu->language);
10057
10058 dwarf2_find_base_address (cu->dies, cu);
10059
10060 /* Before we start reading the top-level DIE, ensure it has a valid tag
10061 type. */
10062 switch (cu->dies->tag)
10063 {
10064 case DW_TAG_compile_unit:
10065 case DW_TAG_partial_unit:
10066 case DW_TAG_type_unit:
10067 break;
10068 default:
10069 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
10070 dwarf_tag_name (cu->dies->tag),
10071 sect_offset_str (cu->per_cu->sect_off),
10072 objfile_name (per_objfile->objfile));
10073 }
10074
10075 /* Do line number decoding in read_file_scope () */
10076 process_die (cu->dies, cu);
10077
10078 /* For now fudge the Go package. */
10079 if (cu->language == language_go)
10080 fixup_go_packaging (cu);
10081
10082 /* Now that we have processed all the DIEs in the CU, all the types
10083 should be complete, and it should now be safe to compute all of the
10084 physnames. */
10085 compute_delayed_physnames (cu);
10086
10087 if (cu->language == language_rust)
10088 rust_union_quirks (cu);
10089
10090 /* Some compilers don't define a DW_AT_high_pc attribute for the
10091 compilation unit. If the DW_AT_high_pc is missing, synthesize
10092 it, by scanning the DIE's below the compilation unit. */
10093 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10094
10095 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10096 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10097
10098 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10099 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10100 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10101 addrmap to help ensure it has an accurate map of pc values belonging to
10102 this comp unit. */
10103 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10104
10105 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10106 SECT_OFF_TEXT (objfile),
10107 0);
10108
10109 if (cust != NULL)
10110 {
10111 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10112
10113 /* Set symtab language to language from DW_AT_language. If the
10114 compilation is from a C file generated by language preprocessors, do
10115 not set the language if it was already deduced by start_subfile. */
10116 if (!(cu->language == language_c
10117 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10118 COMPUNIT_FILETABS (cust)->language = cu->language;
10119
10120 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10121 produce DW_AT_location with location lists but it can be possibly
10122 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10123 there were bugs in prologue debug info, fixed later in GCC-4.5
10124 by "unwind info for epilogues" patch (which is not directly related).
10125
10126 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10127 needed, it would be wrong due to missing DW_AT_producer there.
10128
10129 Still one can confuse GDB by using non-standard GCC compilation
10130 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10131 */
10132 if (cu->has_loclist && gcc_4_minor >= 5)
10133 cust->locations_valid = 1;
10134
10135 if (gcc_4_minor >= 5)
10136 cust->epilogue_unwind_valid = 1;
10137
10138 cust->call_site_htab = cu->call_site_htab;
10139 }
10140
10141 per_objfile->set_symtab (cu->per_cu, cust);
10142
10143 /* Push it for inclusion processing later. */
10144 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
10145
10146 /* Not needed any more. */
10147 cu->reset_builder ();
10148 }
10149
10150 /* Generate full symbol information for type unit CU, whose DIEs have
10151 already been loaded into memory. */
10152
10153 static void
10154 process_full_type_unit (dwarf2_cu *cu,
10155 enum language pretend_language)
10156 {
10157 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10158 struct objfile *objfile = per_objfile->objfile;
10159 struct compunit_symtab *cust;
10160 struct signatured_type *sig_type;
10161
10162 gdb_assert (cu->per_cu->is_debug_types);
10163 sig_type = (struct signatured_type *) cu->per_cu;
10164
10165 /* Clear the list here in case something was left over. */
10166 cu->method_list.clear ();
10167
10168 cu->language = pretend_language;
10169 cu->language_defn = language_def (cu->language);
10170
10171 /* The symbol tables are set up in read_type_unit_scope. */
10172 process_die (cu->dies, cu);
10173
10174 /* For now fudge the Go package. */
10175 if (cu->language == language_go)
10176 fixup_go_packaging (cu);
10177
10178 /* Now that we have processed all the DIEs in the CU, all the types
10179 should be complete, and it should now be safe to compute all of the
10180 physnames. */
10181 compute_delayed_physnames (cu);
10182
10183 if (cu->language == language_rust)
10184 rust_union_quirks (cu);
10185
10186 /* TUs share symbol tables.
10187 If this is the first TU to use this symtab, complete the construction
10188 of it with end_expandable_symtab. Otherwise, complete the addition of
10189 this TU's symbols to the existing symtab. */
10190 type_unit_group_unshareable *tug_unshare =
10191 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
10192 if (tug_unshare->compunit_symtab == NULL)
10193 {
10194 buildsym_compunit *builder = cu->get_builder ();
10195 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10196 tug_unshare->compunit_symtab = cust;
10197
10198 if (cust != NULL)
10199 {
10200 /* Set symtab language to language from DW_AT_language. If the
10201 compilation is from a C file generated by language preprocessors,
10202 do not set the language if it was already deduced by
10203 start_subfile. */
10204 if (!(cu->language == language_c
10205 && COMPUNIT_FILETABS (cust)->language != language_c))
10206 COMPUNIT_FILETABS (cust)->language = cu->language;
10207 }
10208 }
10209 else
10210 {
10211 cu->get_builder ()->augment_type_symtab ();
10212 cust = tug_unshare->compunit_symtab;
10213 }
10214
10215 per_objfile->set_symtab (cu->per_cu, cust);
10216
10217 /* Not needed any more. */
10218 cu->reset_builder ();
10219 }
10220
10221 /* Process an imported unit DIE. */
10222
10223 static void
10224 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10225 {
10226 struct attribute *attr;
10227
10228 /* For now we don't handle imported units in type units. */
10229 if (cu->per_cu->is_debug_types)
10230 {
10231 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10232 " supported in type units [in module %s]"),
10233 objfile_name (cu->per_objfile->objfile));
10234 }
10235
10236 attr = dwarf2_attr (die, DW_AT_import, cu);
10237 if (attr != NULL)
10238 {
10239 sect_offset sect_off = attr->get_ref_die_offset ();
10240 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10241 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10242 dwarf2_per_cu_data *per_cu
10243 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
10244
10245 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10246 into another compilation unit, at root level. Regard this as a hint,
10247 and ignore it. */
10248 if (die->parent && die->parent->parent == NULL
10249 && per_cu->unit_type == DW_UT_compile
10250 && per_cu->lang == language_cplus)
10251 return;
10252
10253 /* If necessary, add it to the queue and load its DIEs. */
10254 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
10255 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
10256 false, cu->language);
10257
10258 cu->per_cu->imported_symtabs_push (per_cu);
10259 }
10260 }
10261
10262 /* RAII object that represents a process_die scope: i.e.,
10263 starts/finishes processing a DIE. */
10264 class process_die_scope
10265 {
10266 public:
10267 process_die_scope (die_info *die, dwarf2_cu *cu)
10268 : m_die (die), m_cu (cu)
10269 {
10270 /* We should only be processing DIEs not already in process. */
10271 gdb_assert (!m_die->in_process);
10272 m_die->in_process = true;
10273 }
10274
10275 ~process_die_scope ()
10276 {
10277 m_die->in_process = false;
10278
10279 /* If we're done processing the DIE for the CU that owns the line
10280 header, we don't need the line header anymore. */
10281 if (m_cu->line_header_die_owner == m_die)
10282 {
10283 delete m_cu->line_header;
10284 m_cu->line_header = NULL;
10285 m_cu->line_header_die_owner = NULL;
10286 }
10287 }
10288
10289 private:
10290 die_info *m_die;
10291 dwarf2_cu *m_cu;
10292 };
10293
10294 /* Process a die and its children. */
10295
10296 static void
10297 process_die (struct die_info *die, struct dwarf2_cu *cu)
10298 {
10299 process_die_scope scope (die, cu);
10300
10301 switch (die->tag)
10302 {
10303 case DW_TAG_padding:
10304 break;
10305 case DW_TAG_compile_unit:
10306 case DW_TAG_partial_unit:
10307 read_file_scope (die, cu);
10308 break;
10309 case DW_TAG_type_unit:
10310 read_type_unit_scope (die, cu);
10311 break;
10312 case DW_TAG_subprogram:
10313 /* Nested subprograms in Fortran get a prefix. */
10314 if (cu->language == language_fortran
10315 && die->parent != NULL
10316 && die->parent->tag == DW_TAG_subprogram)
10317 cu->processing_has_namespace_info = true;
10318 /* Fall through. */
10319 case DW_TAG_inlined_subroutine:
10320 read_func_scope (die, cu);
10321 break;
10322 case DW_TAG_lexical_block:
10323 case DW_TAG_try_block:
10324 case DW_TAG_catch_block:
10325 read_lexical_block_scope (die, cu);
10326 break;
10327 case DW_TAG_call_site:
10328 case DW_TAG_GNU_call_site:
10329 read_call_site_scope (die, cu);
10330 break;
10331 case DW_TAG_class_type:
10332 case DW_TAG_interface_type:
10333 case DW_TAG_structure_type:
10334 case DW_TAG_union_type:
10335 process_structure_scope (die, cu);
10336 break;
10337 case DW_TAG_enumeration_type:
10338 process_enumeration_scope (die, cu);
10339 break;
10340
10341 /* These dies have a type, but processing them does not create
10342 a symbol or recurse to process the children. Therefore we can
10343 read them on-demand through read_type_die. */
10344 case DW_TAG_subroutine_type:
10345 case DW_TAG_set_type:
10346 case DW_TAG_pointer_type:
10347 case DW_TAG_ptr_to_member_type:
10348 case DW_TAG_reference_type:
10349 case DW_TAG_rvalue_reference_type:
10350 case DW_TAG_string_type:
10351 break;
10352
10353 case DW_TAG_array_type:
10354 /* We only need to handle this case for Ada -- in other
10355 languages, it's normal for the compiler to emit a typedef
10356 instead. */
10357 if (cu->language != language_ada)
10358 break;
10359 /* FALLTHROUGH */
10360 case DW_TAG_base_type:
10361 case DW_TAG_subrange_type:
10362 case DW_TAG_typedef:
10363 /* Add a typedef symbol for the type definition, if it has a
10364 DW_AT_name. */
10365 new_symbol (die, read_type_die (die, cu), cu);
10366 break;
10367 case DW_TAG_common_block:
10368 read_common_block (die, cu);
10369 break;
10370 case DW_TAG_common_inclusion:
10371 break;
10372 case DW_TAG_namespace:
10373 cu->processing_has_namespace_info = true;
10374 read_namespace (die, cu);
10375 break;
10376 case DW_TAG_module:
10377 cu->processing_has_namespace_info = true;
10378 read_module (die, cu);
10379 break;
10380 case DW_TAG_imported_declaration:
10381 cu->processing_has_namespace_info = true;
10382 if (read_namespace_alias (die, cu))
10383 break;
10384 /* The declaration is not a global namespace alias. */
10385 /* Fall through. */
10386 case DW_TAG_imported_module:
10387 cu->processing_has_namespace_info = true;
10388 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10389 || cu->language != language_fortran))
10390 complaint (_("Tag '%s' has unexpected children"),
10391 dwarf_tag_name (die->tag));
10392 read_import_statement (die, cu);
10393 break;
10394
10395 case DW_TAG_imported_unit:
10396 process_imported_unit_die (die, cu);
10397 break;
10398
10399 case DW_TAG_variable:
10400 read_variable (die, cu);
10401 break;
10402
10403 default:
10404 new_symbol (die, NULL, cu);
10405 break;
10406 }
10407 }
10408 \f
10409 /* DWARF name computation. */
10410
10411 /* A helper function for dwarf2_compute_name which determines whether DIE
10412 needs to have the name of the scope prepended to the name listed in the
10413 die. */
10414
10415 static int
10416 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10417 {
10418 struct attribute *attr;
10419
10420 switch (die->tag)
10421 {
10422 case DW_TAG_namespace:
10423 case DW_TAG_typedef:
10424 case DW_TAG_class_type:
10425 case DW_TAG_interface_type:
10426 case DW_TAG_structure_type:
10427 case DW_TAG_union_type:
10428 case DW_TAG_enumeration_type:
10429 case DW_TAG_enumerator:
10430 case DW_TAG_subprogram:
10431 case DW_TAG_inlined_subroutine:
10432 case DW_TAG_member:
10433 case DW_TAG_imported_declaration:
10434 return 1;
10435
10436 case DW_TAG_variable:
10437 case DW_TAG_constant:
10438 /* We only need to prefix "globally" visible variables. These include
10439 any variable marked with DW_AT_external or any variable that
10440 lives in a namespace. [Variables in anonymous namespaces
10441 require prefixing, but they are not DW_AT_external.] */
10442
10443 if (dwarf2_attr (die, DW_AT_specification, cu))
10444 {
10445 struct dwarf2_cu *spec_cu = cu;
10446
10447 return die_needs_namespace (die_specification (die, &spec_cu),
10448 spec_cu);
10449 }
10450
10451 attr = dwarf2_attr (die, DW_AT_external, cu);
10452 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10453 && die->parent->tag != DW_TAG_module)
10454 return 0;
10455 /* A variable in a lexical block of some kind does not need a
10456 namespace, even though in C++ such variables may be external
10457 and have a mangled name. */
10458 if (die->parent->tag == DW_TAG_lexical_block
10459 || die->parent->tag == DW_TAG_try_block
10460 || die->parent->tag == DW_TAG_catch_block
10461 || die->parent->tag == DW_TAG_subprogram)
10462 return 0;
10463 return 1;
10464
10465 default:
10466 return 0;
10467 }
10468 }
10469
10470 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10471 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10472 defined for the given DIE. */
10473
10474 static struct attribute *
10475 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10476 {
10477 struct attribute *attr;
10478
10479 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10480 if (attr == NULL)
10481 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10482
10483 return attr;
10484 }
10485
10486 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10487 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10488 defined for the given DIE. */
10489
10490 static const char *
10491 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10492 {
10493 const char *linkage_name;
10494
10495 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10496 if (linkage_name == NULL)
10497 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10498
10499 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10500 See https://github.com/rust-lang/rust/issues/32925. */
10501 if (cu->language == language_rust && linkage_name != NULL
10502 && strchr (linkage_name, '{') != NULL)
10503 linkage_name = NULL;
10504
10505 return linkage_name;
10506 }
10507
10508 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10509 compute the physname for the object, which include a method's:
10510 - formal parameters (C++),
10511 - receiver type (Go),
10512
10513 The term "physname" is a bit confusing.
10514 For C++, for example, it is the demangled name.
10515 For Go, for example, it's the mangled name.
10516
10517 For Ada, return the DIE's linkage name rather than the fully qualified
10518 name. PHYSNAME is ignored..
10519
10520 The result is allocated on the objfile->per_bfd's obstack and
10521 canonicalized. */
10522
10523 static const char *
10524 dwarf2_compute_name (const char *name,
10525 struct die_info *die, struct dwarf2_cu *cu,
10526 int physname)
10527 {
10528 struct objfile *objfile = cu->per_objfile->objfile;
10529
10530 if (name == NULL)
10531 name = dwarf2_name (die, cu);
10532
10533 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10534 but otherwise compute it by typename_concat inside GDB.
10535 FIXME: Actually this is not really true, or at least not always true.
10536 It's all very confusing. compute_and_set_names doesn't try to demangle
10537 Fortran names because there is no mangling standard. So new_symbol
10538 will set the demangled name to the result of dwarf2_full_name, and it is
10539 the demangled name that GDB uses if it exists. */
10540 if (cu->language == language_ada
10541 || (cu->language == language_fortran && physname))
10542 {
10543 /* For Ada unit, we prefer the linkage name over the name, as
10544 the former contains the exported name, which the user expects
10545 to be able to reference. Ideally, we want the user to be able
10546 to reference this entity using either natural or linkage name,
10547 but we haven't started looking at this enhancement yet. */
10548 const char *linkage_name = dw2_linkage_name (die, cu);
10549
10550 if (linkage_name != NULL)
10551 return linkage_name;
10552 }
10553
10554 /* These are the only languages we know how to qualify names in. */
10555 if (name != NULL
10556 && (cu->language == language_cplus
10557 || cu->language == language_fortran || cu->language == language_d
10558 || cu->language == language_rust))
10559 {
10560 if (die_needs_namespace (die, cu))
10561 {
10562 const char *prefix;
10563 const char *canonical_name = NULL;
10564
10565 string_file buf;
10566
10567 prefix = determine_prefix (die, cu);
10568 if (*prefix != '\0')
10569 {
10570 gdb::unique_xmalloc_ptr<char> prefixed_name
10571 (typename_concat (NULL, prefix, name, physname, cu));
10572
10573 buf.puts (prefixed_name.get ());
10574 }
10575 else
10576 buf.puts (name);
10577
10578 /* Template parameters may be specified in the DIE's DW_AT_name, or
10579 as children with DW_TAG_template_type_param or
10580 DW_TAG_value_type_param. If the latter, add them to the name
10581 here. If the name already has template parameters, then
10582 skip this step; some versions of GCC emit both, and
10583 it is more efficient to use the pre-computed name.
10584
10585 Something to keep in mind about this process: it is very
10586 unlikely, or in some cases downright impossible, to produce
10587 something that will match the mangled name of a function.
10588 If the definition of the function has the same debug info,
10589 we should be able to match up with it anyway. But fallbacks
10590 using the minimal symbol, for instance to find a method
10591 implemented in a stripped copy of libstdc++, will not work.
10592 If we do not have debug info for the definition, we will have to
10593 match them up some other way.
10594
10595 When we do name matching there is a related problem with function
10596 templates; two instantiated function templates are allowed to
10597 differ only by their return types, which we do not add here. */
10598
10599 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10600 {
10601 struct attribute *attr;
10602 struct die_info *child;
10603 int first = 1;
10604 const language_defn *cplus_lang = language_def (cu->language);
10605
10606 die->building_fullname = 1;
10607
10608 for (child = die->child; child != NULL; child = child->sibling)
10609 {
10610 struct type *type;
10611 LONGEST value;
10612 const gdb_byte *bytes;
10613 struct dwarf2_locexpr_baton *baton;
10614 struct value *v;
10615
10616 if (child->tag != DW_TAG_template_type_param
10617 && child->tag != DW_TAG_template_value_param)
10618 continue;
10619
10620 if (first)
10621 {
10622 buf.puts ("<");
10623 first = 0;
10624 }
10625 else
10626 buf.puts (", ");
10627
10628 attr = dwarf2_attr (child, DW_AT_type, cu);
10629 if (attr == NULL)
10630 {
10631 complaint (_("template parameter missing DW_AT_type"));
10632 buf.puts ("UNKNOWN_TYPE");
10633 continue;
10634 }
10635 type = die_type (child, cu);
10636
10637 if (child->tag == DW_TAG_template_type_param)
10638 {
10639 cplus_lang->print_type (type, "", &buf, -1, 0,
10640 &type_print_raw_options);
10641 continue;
10642 }
10643
10644 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10645 if (attr == NULL)
10646 {
10647 complaint (_("template parameter missing "
10648 "DW_AT_const_value"));
10649 buf.puts ("UNKNOWN_VALUE");
10650 continue;
10651 }
10652
10653 dwarf2_const_value_attr (attr, type, name,
10654 &cu->comp_unit_obstack, cu,
10655 &value, &bytes, &baton);
10656
10657 if (type->has_no_signedness ())
10658 /* GDB prints characters as NUMBER 'CHAR'. If that's
10659 changed, this can use value_print instead. */
10660 cplus_lang->printchar (value, type, &buf);
10661 else
10662 {
10663 struct value_print_options opts;
10664
10665 if (baton != NULL)
10666 v = dwarf2_evaluate_loc_desc (type, NULL,
10667 baton->data,
10668 baton->size,
10669 baton->per_cu,
10670 baton->per_objfile);
10671 else if (bytes != NULL)
10672 {
10673 v = allocate_value (type);
10674 memcpy (value_contents_writeable (v), bytes,
10675 TYPE_LENGTH (type));
10676 }
10677 else
10678 v = value_from_longest (type, value);
10679
10680 /* Specify decimal so that we do not depend on
10681 the radix. */
10682 get_formatted_print_options (&opts, 'd');
10683 opts.raw = 1;
10684 value_print (v, &buf, &opts);
10685 release_value (v);
10686 }
10687 }
10688
10689 die->building_fullname = 0;
10690
10691 if (!first)
10692 {
10693 /* Close the argument list, with a space if necessary
10694 (nested templates). */
10695 if (!buf.empty () && buf.string ().back () == '>')
10696 buf.puts (" >");
10697 else
10698 buf.puts (">");
10699 }
10700 }
10701
10702 /* For C++ methods, append formal parameter type
10703 information, if PHYSNAME. */
10704
10705 if (physname && die->tag == DW_TAG_subprogram
10706 && cu->language == language_cplus)
10707 {
10708 struct type *type = read_type_die (die, cu);
10709
10710 c_type_print_args (type, &buf, 1, cu->language,
10711 &type_print_raw_options);
10712
10713 if (cu->language == language_cplus)
10714 {
10715 /* Assume that an artificial first parameter is
10716 "this", but do not crash if it is not. RealView
10717 marks unnamed (and thus unused) parameters as
10718 artificial; there is no way to differentiate
10719 the two cases. */
10720 if (type->num_fields () > 0
10721 && TYPE_FIELD_ARTIFICIAL (type, 0)
10722 && type->field (0).type ()->code () == TYPE_CODE_PTR
10723 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
10724 buf.puts (" const");
10725 }
10726 }
10727
10728 const std::string &intermediate_name = buf.string ();
10729
10730 if (cu->language == language_cplus)
10731 canonical_name
10732 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10733 objfile);
10734
10735 /* If we only computed INTERMEDIATE_NAME, or if
10736 INTERMEDIATE_NAME is already canonical, then we need to
10737 intern it. */
10738 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10739 name = objfile->intern (intermediate_name);
10740 else
10741 name = canonical_name;
10742 }
10743 }
10744
10745 return name;
10746 }
10747
10748 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10749 If scope qualifiers are appropriate they will be added. The result
10750 will be allocated on the storage_obstack, or NULL if the DIE does
10751 not have a name. NAME may either be from a previous call to
10752 dwarf2_name or NULL.
10753
10754 The output string will be canonicalized (if C++). */
10755
10756 static const char *
10757 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10758 {
10759 return dwarf2_compute_name (name, die, cu, 0);
10760 }
10761
10762 /* Construct a physname for the given DIE in CU. NAME may either be
10763 from a previous call to dwarf2_name or NULL. The result will be
10764 allocated on the objfile_objstack or NULL if the DIE does not have a
10765 name.
10766
10767 The output string will be canonicalized (if C++). */
10768
10769 static const char *
10770 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10771 {
10772 struct objfile *objfile = cu->per_objfile->objfile;
10773 const char *retval, *mangled = NULL, *canon = NULL;
10774 int need_copy = 1;
10775
10776 /* In this case dwarf2_compute_name is just a shortcut not building anything
10777 on its own. */
10778 if (!die_needs_namespace (die, cu))
10779 return dwarf2_compute_name (name, die, cu, 1);
10780
10781 if (cu->language != language_rust)
10782 mangled = dw2_linkage_name (die, cu);
10783
10784 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10785 has computed. */
10786 gdb::unique_xmalloc_ptr<char> demangled;
10787 if (mangled != NULL)
10788 {
10789
10790 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
10791 {
10792 /* Do nothing (do not demangle the symbol name). */
10793 }
10794 else
10795 {
10796 /* Use DMGL_RET_DROP for C++ template functions to suppress
10797 their return type. It is easier for GDB users to search
10798 for such functions as `name(params)' than `long name(params)'.
10799 In such case the minimal symbol names do not match the full
10800 symbol names but for template functions there is never a need
10801 to look up their definition from their declaration so
10802 the only disadvantage remains the minimal symbol variant
10803 `long name(params)' does not have the proper inferior type. */
10804 demangled.reset (gdb_demangle (mangled,
10805 (DMGL_PARAMS | DMGL_ANSI
10806 | DMGL_RET_DROP)));
10807 }
10808 if (demangled)
10809 canon = demangled.get ();
10810 else
10811 {
10812 canon = mangled;
10813 need_copy = 0;
10814 }
10815 }
10816
10817 if (canon == NULL || check_physname)
10818 {
10819 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10820
10821 if (canon != NULL && strcmp (physname, canon) != 0)
10822 {
10823 /* It may not mean a bug in GDB. The compiler could also
10824 compute DW_AT_linkage_name incorrectly. But in such case
10825 GDB would need to be bug-to-bug compatible. */
10826
10827 complaint (_("Computed physname <%s> does not match demangled <%s> "
10828 "(from linkage <%s>) - DIE at %s [in module %s]"),
10829 physname, canon, mangled, sect_offset_str (die->sect_off),
10830 objfile_name (objfile));
10831
10832 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10833 is available here - over computed PHYSNAME. It is safer
10834 against both buggy GDB and buggy compilers. */
10835
10836 retval = canon;
10837 }
10838 else
10839 {
10840 retval = physname;
10841 need_copy = 0;
10842 }
10843 }
10844 else
10845 retval = canon;
10846
10847 if (need_copy)
10848 retval = objfile->intern (retval);
10849
10850 return retval;
10851 }
10852
10853 /* Inspect DIE in CU for a namespace alias. If one exists, record
10854 a new symbol for it.
10855
10856 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10857
10858 static int
10859 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10860 {
10861 struct attribute *attr;
10862
10863 /* If the die does not have a name, this is not a namespace
10864 alias. */
10865 attr = dwarf2_attr (die, DW_AT_name, cu);
10866 if (attr != NULL)
10867 {
10868 int num;
10869 struct die_info *d = die;
10870 struct dwarf2_cu *imported_cu = cu;
10871
10872 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10873 keep inspecting DIEs until we hit the underlying import. */
10874 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10875 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10876 {
10877 attr = dwarf2_attr (d, DW_AT_import, cu);
10878 if (attr == NULL)
10879 break;
10880
10881 d = follow_die_ref (d, attr, &imported_cu);
10882 if (d->tag != DW_TAG_imported_declaration)
10883 break;
10884 }
10885
10886 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10887 {
10888 complaint (_("DIE at %s has too many recursively imported "
10889 "declarations"), sect_offset_str (d->sect_off));
10890 return 0;
10891 }
10892
10893 if (attr != NULL)
10894 {
10895 struct type *type;
10896 sect_offset sect_off = attr->get_ref_die_offset ();
10897
10898 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10899 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10900 {
10901 /* This declaration is a global namespace alias. Add
10902 a symbol for it whose type is the aliased namespace. */
10903 new_symbol (die, type, cu);
10904 return 1;
10905 }
10906 }
10907 }
10908
10909 return 0;
10910 }
10911
10912 /* Return the using directives repository (global or local?) to use in the
10913 current context for CU.
10914
10915 For Ada, imported declarations can materialize renamings, which *may* be
10916 global. However it is impossible (for now?) in DWARF to distinguish
10917 "external" imported declarations and "static" ones. As all imported
10918 declarations seem to be static in all other languages, make them all CU-wide
10919 global only in Ada. */
10920
10921 static struct using_direct **
10922 using_directives (struct dwarf2_cu *cu)
10923 {
10924 if (cu->language == language_ada
10925 && cu->get_builder ()->outermost_context_p ())
10926 return cu->get_builder ()->get_global_using_directives ();
10927 else
10928 return cu->get_builder ()->get_local_using_directives ();
10929 }
10930
10931 /* Read the import statement specified by the given die and record it. */
10932
10933 static void
10934 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10935 {
10936 struct objfile *objfile = cu->per_objfile->objfile;
10937 struct attribute *import_attr;
10938 struct die_info *imported_die, *child_die;
10939 struct dwarf2_cu *imported_cu;
10940 const char *imported_name;
10941 const char *imported_name_prefix;
10942 const char *canonical_name;
10943 const char *import_alias;
10944 const char *imported_declaration = NULL;
10945 const char *import_prefix;
10946 std::vector<const char *> excludes;
10947
10948 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10949 if (import_attr == NULL)
10950 {
10951 complaint (_("Tag '%s' has no DW_AT_import"),
10952 dwarf_tag_name (die->tag));
10953 return;
10954 }
10955
10956 imported_cu = cu;
10957 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10958 imported_name = dwarf2_name (imported_die, imported_cu);
10959 if (imported_name == NULL)
10960 {
10961 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10962
10963 The import in the following code:
10964 namespace A
10965 {
10966 typedef int B;
10967 }
10968
10969 int main ()
10970 {
10971 using A::B;
10972 B b;
10973 return b;
10974 }
10975
10976 ...
10977 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10978 <52> DW_AT_decl_file : 1
10979 <53> DW_AT_decl_line : 6
10980 <54> DW_AT_import : <0x75>
10981 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10982 <59> DW_AT_name : B
10983 <5b> DW_AT_decl_file : 1
10984 <5c> DW_AT_decl_line : 2
10985 <5d> DW_AT_type : <0x6e>
10986 ...
10987 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10988 <76> DW_AT_byte_size : 4
10989 <77> DW_AT_encoding : 5 (signed)
10990
10991 imports the wrong die ( 0x75 instead of 0x58 ).
10992 This case will be ignored until the gcc bug is fixed. */
10993 return;
10994 }
10995
10996 /* Figure out the local name after import. */
10997 import_alias = dwarf2_name (die, cu);
10998
10999 /* Figure out where the statement is being imported to. */
11000 import_prefix = determine_prefix (die, cu);
11001
11002 /* Figure out what the scope of the imported die is and prepend it
11003 to the name of the imported die. */
11004 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11005
11006 if (imported_die->tag != DW_TAG_namespace
11007 && imported_die->tag != DW_TAG_module)
11008 {
11009 imported_declaration = imported_name;
11010 canonical_name = imported_name_prefix;
11011 }
11012 else if (strlen (imported_name_prefix) > 0)
11013 canonical_name = obconcat (&objfile->objfile_obstack,
11014 imported_name_prefix,
11015 (cu->language == language_d ? "." : "::"),
11016 imported_name, (char *) NULL);
11017 else
11018 canonical_name = imported_name;
11019
11020 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11021 for (child_die = die->child; child_die && child_die->tag;
11022 child_die = child_die->sibling)
11023 {
11024 /* DWARF-4: A Fortran use statement with a “rename list” may be
11025 represented by an imported module entry with an import attribute
11026 referring to the module and owned entries corresponding to those
11027 entities that are renamed as part of being imported. */
11028
11029 if (child_die->tag != DW_TAG_imported_declaration)
11030 {
11031 complaint (_("child DW_TAG_imported_declaration expected "
11032 "- DIE at %s [in module %s]"),
11033 sect_offset_str (child_die->sect_off),
11034 objfile_name (objfile));
11035 continue;
11036 }
11037
11038 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11039 if (import_attr == NULL)
11040 {
11041 complaint (_("Tag '%s' has no DW_AT_import"),
11042 dwarf_tag_name (child_die->tag));
11043 continue;
11044 }
11045
11046 imported_cu = cu;
11047 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11048 &imported_cu);
11049 imported_name = dwarf2_name (imported_die, imported_cu);
11050 if (imported_name == NULL)
11051 {
11052 complaint (_("child DW_TAG_imported_declaration has unknown "
11053 "imported name - DIE at %s [in module %s]"),
11054 sect_offset_str (child_die->sect_off),
11055 objfile_name (objfile));
11056 continue;
11057 }
11058
11059 excludes.push_back (imported_name);
11060
11061 process_die (child_die, cu);
11062 }
11063
11064 add_using_directive (using_directives (cu),
11065 import_prefix,
11066 canonical_name,
11067 import_alias,
11068 imported_declaration,
11069 excludes,
11070 0,
11071 &objfile->objfile_obstack);
11072 }
11073
11074 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11075 types, but gives them a size of zero. Starting with version 14,
11076 ICC is compatible with GCC. */
11077
11078 static bool
11079 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11080 {
11081 if (!cu->checked_producer)
11082 check_producer (cu);
11083
11084 return cu->producer_is_icc_lt_14;
11085 }
11086
11087 /* ICC generates a DW_AT_type for C void functions. This was observed on
11088 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11089 which says that void functions should not have a DW_AT_type. */
11090
11091 static bool
11092 producer_is_icc (struct dwarf2_cu *cu)
11093 {
11094 if (!cu->checked_producer)
11095 check_producer (cu);
11096
11097 return cu->producer_is_icc;
11098 }
11099
11100 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11101 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11102 this, it was first present in GCC release 4.3.0. */
11103
11104 static bool
11105 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11106 {
11107 if (!cu->checked_producer)
11108 check_producer (cu);
11109
11110 return cu->producer_is_gcc_lt_4_3;
11111 }
11112
11113 static file_and_directory
11114 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11115 {
11116 file_and_directory res;
11117
11118 /* Find the filename. Do not use dwarf2_name here, since the filename
11119 is not a source language identifier. */
11120 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11121 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11122
11123 if (res.comp_dir == NULL
11124 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11125 && IS_ABSOLUTE_PATH (res.name))
11126 {
11127 res.comp_dir_storage = ldirname (res.name);
11128 if (!res.comp_dir_storage.empty ())
11129 res.comp_dir = res.comp_dir_storage.c_str ();
11130 }
11131 if (res.comp_dir != NULL)
11132 {
11133 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11134 directory, get rid of it. */
11135 const char *cp = strchr (res.comp_dir, ':');
11136
11137 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11138 res.comp_dir = cp + 1;
11139 }
11140
11141 if (res.name == NULL)
11142 res.name = "<unknown>";
11143
11144 return res;
11145 }
11146
11147 /* Handle DW_AT_stmt_list for a compilation unit.
11148 DIE is the DW_TAG_compile_unit die for CU.
11149 COMP_DIR is the compilation directory. LOWPC is passed to
11150 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11151
11152 static void
11153 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11154 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11155 {
11156 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11157 struct attribute *attr;
11158 struct line_header line_header_local;
11159 hashval_t line_header_local_hash;
11160 void **slot;
11161 int decode_mapping;
11162
11163 gdb_assert (! cu->per_cu->is_debug_types);
11164
11165 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11166 if (attr == NULL || !attr->form_is_unsigned ())
11167 return;
11168
11169 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
11170
11171 /* The line header hash table is only created if needed (it exists to
11172 prevent redundant reading of the line table for partial_units).
11173 If we're given a partial_unit, we'll need it. If we're given a
11174 compile_unit, then use the line header hash table if it's already
11175 created, but don't create one just yet. */
11176
11177 if (per_objfile->line_header_hash == NULL
11178 && die->tag == DW_TAG_partial_unit)
11179 {
11180 per_objfile->line_header_hash
11181 .reset (htab_create_alloc (127, line_header_hash_voidp,
11182 line_header_eq_voidp,
11183 free_line_header_voidp,
11184 xcalloc, xfree));
11185 }
11186
11187 line_header_local.sect_off = line_offset;
11188 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11189 line_header_local_hash = line_header_hash (&line_header_local);
11190 if (per_objfile->line_header_hash != NULL)
11191 {
11192 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11193 &line_header_local,
11194 line_header_local_hash, NO_INSERT);
11195
11196 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11197 is not present in *SLOT (since if there is something in *SLOT then
11198 it will be for a partial_unit). */
11199 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11200 {
11201 gdb_assert (*slot != NULL);
11202 cu->line_header = (struct line_header *) *slot;
11203 return;
11204 }
11205 }
11206
11207 /* dwarf_decode_line_header does not yet provide sufficient information.
11208 We always have to call also dwarf_decode_lines for it. */
11209 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11210 if (lh == NULL)
11211 return;
11212
11213 cu->line_header = lh.release ();
11214 cu->line_header_die_owner = die;
11215
11216 if (per_objfile->line_header_hash == NULL)
11217 slot = NULL;
11218 else
11219 {
11220 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11221 &line_header_local,
11222 line_header_local_hash, INSERT);
11223 gdb_assert (slot != NULL);
11224 }
11225 if (slot != NULL && *slot == NULL)
11226 {
11227 /* This newly decoded line number information unit will be owned
11228 by line_header_hash hash table. */
11229 *slot = cu->line_header;
11230 cu->line_header_die_owner = NULL;
11231 }
11232 else
11233 {
11234 /* We cannot free any current entry in (*slot) as that struct line_header
11235 may be already used by multiple CUs. Create only temporary decoded
11236 line_header for this CU - it may happen at most once for each line
11237 number information unit. And if we're not using line_header_hash
11238 then this is what we want as well. */
11239 gdb_assert (die->tag != DW_TAG_partial_unit);
11240 }
11241 decode_mapping = (die->tag != DW_TAG_partial_unit);
11242 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11243 decode_mapping);
11244
11245 }
11246
11247 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11248
11249 static void
11250 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11251 {
11252 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11253 struct objfile *objfile = per_objfile->objfile;
11254 struct gdbarch *gdbarch = objfile->arch ();
11255 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11256 CORE_ADDR highpc = ((CORE_ADDR) 0);
11257 struct attribute *attr;
11258 struct die_info *child_die;
11259 CORE_ADDR baseaddr;
11260
11261 prepare_one_comp_unit (cu, die, cu->language);
11262 baseaddr = objfile->text_section_offset ();
11263
11264 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11265
11266 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11267 from finish_block. */
11268 if (lowpc == ((CORE_ADDR) -1))
11269 lowpc = highpc;
11270 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11271
11272 file_and_directory fnd = find_file_and_directory (die, cu);
11273
11274 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11275 standardised yet. As a workaround for the language detection we fall
11276 back to the DW_AT_producer string. */
11277 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11278 cu->language = language_opencl;
11279
11280 /* Similar hack for Go. */
11281 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11282 set_cu_language (DW_LANG_Go, cu);
11283
11284 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11285
11286 /* Decode line number information if present. We do this before
11287 processing child DIEs, so that the line header table is available
11288 for DW_AT_decl_file. */
11289 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11290
11291 /* Process all dies in compilation unit. */
11292 if (die->child != NULL)
11293 {
11294 child_die = die->child;
11295 while (child_die && child_die->tag)
11296 {
11297 process_die (child_die, cu);
11298 child_die = child_die->sibling;
11299 }
11300 }
11301
11302 /* Decode macro information, if present. Dwarf 2 macro information
11303 refers to information in the line number info statement program
11304 header, so we can only read it if we've read the header
11305 successfully. */
11306 attr = dwarf2_attr (die, DW_AT_macros, cu);
11307 if (attr == NULL)
11308 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11309 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
11310 {
11311 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11312 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11313
11314 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
11315 }
11316 else
11317 {
11318 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11319 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
11320 {
11321 unsigned int macro_offset = attr->as_unsigned ();
11322
11323 dwarf_decode_macros (cu, macro_offset, 0);
11324 }
11325 }
11326 }
11327
11328 void
11329 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11330 {
11331 struct type_unit_group *tu_group;
11332 int first_time;
11333 struct attribute *attr;
11334 unsigned int i;
11335 struct signatured_type *sig_type;
11336
11337 gdb_assert (per_cu->is_debug_types);
11338 sig_type = (struct signatured_type *) per_cu;
11339
11340 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11341
11342 /* If we're using .gdb_index (includes -readnow) then
11343 per_cu->type_unit_group may not have been set up yet. */
11344 if (sig_type->type_unit_group == NULL)
11345 sig_type->type_unit_group = get_type_unit_group (this, attr);
11346 tu_group = sig_type->type_unit_group;
11347
11348 /* If we've already processed this stmt_list there's no real need to
11349 do it again, we could fake it and just recreate the part we need
11350 (file name,index -> symtab mapping). If data shows this optimization
11351 is useful we can do it then. */
11352 type_unit_group_unshareable *tug_unshare
11353 = per_objfile->get_type_unit_group_unshareable (tu_group);
11354 first_time = tug_unshare->compunit_symtab == NULL;
11355
11356 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11357 debug info. */
11358 line_header_up lh;
11359 if (attr != NULL && attr->form_is_unsigned ())
11360 {
11361 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
11362 lh = dwarf_decode_line_header (line_offset, this);
11363 }
11364 if (lh == NULL)
11365 {
11366 if (first_time)
11367 start_symtab ("", NULL, 0);
11368 else
11369 {
11370 gdb_assert (tug_unshare->symtabs == NULL);
11371 gdb_assert (m_builder == nullptr);
11372 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11373 m_builder.reset (new struct buildsym_compunit
11374 (COMPUNIT_OBJFILE (cust), "",
11375 COMPUNIT_DIRNAME (cust),
11376 compunit_language (cust),
11377 0, cust));
11378 list_in_scope = get_builder ()->get_file_symbols ();
11379 }
11380 return;
11381 }
11382
11383 line_header = lh.release ();
11384 line_header_die_owner = die;
11385
11386 if (first_time)
11387 {
11388 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11389
11390 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11391 still initializing it, and our caller (a few levels up)
11392 process_full_type_unit still needs to know if this is the first
11393 time. */
11394
11395 tug_unshare->symtabs
11396 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11397 struct symtab *, line_header->file_names_size ());
11398
11399 auto &file_names = line_header->file_names ();
11400 for (i = 0; i < file_names.size (); ++i)
11401 {
11402 file_entry &fe = file_names[i];
11403 dwarf2_start_subfile (this, fe.name,
11404 fe.include_dir (line_header));
11405 buildsym_compunit *b = get_builder ();
11406 if (b->get_current_subfile ()->symtab == NULL)
11407 {
11408 /* NOTE: start_subfile will recognize when it's been
11409 passed a file it has already seen. So we can't
11410 assume there's a simple mapping from
11411 cu->line_header->file_names to subfiles, plus
11412 cu->line_header->file_names may contain dups. */
11413 b->get_current_subfile ()->symtab
11414 = allocate_symtab (cust, b->get_current_subfile ()->name);
11415 }
11416
11417 fe.symtab = b->get_current_subfile ()->symtab;
11418 tug_unshare->symtabs[i] = fe.symtab;
11419 }
11420 }
11421 else
11422 {
11423 gdb_assert (m_builder == nullptr);
11424 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11425 m_builder.reset (new struct buildsym_compunit
11426 (COMPUNIT_OBJFILE (cust), "",
11427 COMPUNIT_DIRNAME (cust),
11428 compunit_language (cust),
11429 0, cust));
11430 list_in_scope = get_builder ()->get_file_symbols ();
11431
11432 auto &file_names = line_header->file_names ();
11433 for (i = 0; i < file_names.size (); ++i)
11434 {
11435 file_entry &fe = file_names[i];
11436 fe.symtab = tug_unshare->symtabs[i];
11437 }
11438 }
11439
11440 /* The main symtab is allocated last. Type units don't have DW_AT_name
11441 so they don't have a "real" (so to speak) symtab anyway.
11442 There is later code that will assign the main symtab to all symbols
11443 that don't have one. We need to handle the case of a symbol with a
11444 missing symtab (DW_AT_decl_file) anyway. */
11445 }
11446
11447 /* Process DW_TAG_type_unit.
11448 For TUs we want to skip the first top level sibling if it's not the
11449 actual type being defined by this TU. In this case the first top
11450 level sibling is there to provide context only. */
11451
11452 static void
11453 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11454 {
11455 struct die_info *child_die;
11456
11457 prepare_one_comp_unit (cu, die, language_minimal);
11458
11459 /* Initialize (or reinitialize) the machinery for building symtabs.
11460 We do this before processing child DIEs, so that the line header table
11461 is available for DW_AT_decl_file. */
11462 cu->setup_type_unit_groups (die);
11463
11464 if (die->child != NULL)
11465 {
11466 child_die = die->child;
11467 while (child_die && child_die->tag)
11468 {
11469 process_die (child_die, cu);
11470 child_die = child_die->sibling;
11471 }
11472 }
11473 }
11474 \f
11475 /* DWO/DWP files.
11476
11477 http://gcc.gnu.org/wiki/DebugFission
11478 http://gcc.gnu.org/wiki/DebugFissionDWP
11479
11480 To simplify handling of both DWO files ("object" files with the DWARF info)
11481 and DWP files (a file with the DWOs packaged up into one file), we treat
11482 DWP files as having a collection of virtual DWO files. */
11483
11484 static hashval_t
11485 hash_dwo_file (const void *item)
11486 {
11487 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11488 hashval_t hash;
11489
11490 hash = htab_hash_string (dwo_file->dwo_name);
11491 if (dwo_file->comp_dir != NULL)
11492 hash += htab_hash_string (dwo_file->comp_dir);
11493 return hash;
11494 }
11495
11496 static int
11497 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11498 {
11499 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11500 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11501
11502 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11503 return 0;
11504 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11505 return lhs->comp_dir == rhs->comp_dir;
11506 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11507 }
11508
11509 /* Allocate a hash table for DWO files. */
11510
11511 static htab_up
11512 allocate_dwo_file_hash_table ()
11513 {
11514 auto delete_dwo_file = [] (void *item)
11515 {
11516 struct dwo_file *dwo_file = (struct dwo_file *) item;
11517
11518 delete dwo_file;
11519 };
11520
11521 return htab_up (htab_create_alloc (41,
11522 hash_dwo_file,
11523 eq_dwo_file,
11524 delete_dwo_file,
11525 xcalloc, xfree));
11526 }
11527
11528 /* Lookup DWO file DWO_NAME. */
11529
11530 static void **
11531 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
11532 const char *dwo_name,
11533 const char *comp_dir)
11534 {
11535 struct dwo_file find_entry;
11536 void **slot;
11537
11538 if (per_objfile->per_bfd->dwo_files == NULL)
11539 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11540
11541 find_entry.dwo_name = dwo_name;
11542 find_entry.comp_dir = comp_dir;
11543 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
11544 INSERT);
11545
11546 return slot;
11547 }
11548
11549 static hashval_t
11550 hash_dwo_unit (const void *item)
11551 {
11552 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11553
11554 /* This drops the top 32 bits of the id, but is ok for a hash. */
11555 return dwo_unit->signature;
11556 }
11557
11558 static int
11559 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11560 {
11561 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11562 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11563
11564 /* The signature is assumed to be unique within the DWO file.
11565 So while object file CU dwo_id's always have the value zero,
11566 that's OK, assuming each object file DWO file has only one CU,
11567 and that's the rule for now. */
11568 return lhs->signature == rhs->signature;
11569 }
11570
11571 /* Allocate a hash table for DWO CUs,TUs.
11572 There is one of these tables for each of CUs,TUs for each DWO file. */
11573
11574 static htab_up
11575 allocate_dwo_unit_table ()
11576 {
11577 /* Start out with a pretty small number.
11578 Generally DWO files contain only one CU and maybe some TUs. */
11579 return htab_up (htab_create_alloc (3,
11580 hash_dwo_unit,
11581 eq_dwo_unit,
11582 NULL, xcalloc, xfree));
11583 }
11584
11585 /* die_reader_func for create_dwo_cu. */
11586
11587 static void
11588 create_dwo_cu_reader (const struct die_reader_specs *reader,
11589 const gdb_byte *info_ptr,
11590 struct die_info *comp_unit_die,
11591 struct dwo_file *dwo_file,
11592 struct dwo_unit *dwo_unit)
11593 {
11594 struct dwarf2_cu *cu = reader->cu;
11595 sect_offset sect_off = cu->per_cu->sect_off;
11596 struct dwarf2_section_info *section = cu->per_cu->section;
11597
11598 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11599 if (!signature.has_value ())
11600 {
11601 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11602 " its dwo_id [in module %s]"),
11603 sect_offset_str (sect_off), dwo_file->dwo_name);
11604 return;
11605 }
11606
11607 dwo_unit->dwo_file = dwo_file;
11608 dwo_unit->signature = *signature;
11609 dwo_unit->section = section;
11610 dwo_unit->sect_off = sect_off;
11611 dwo_unit->length = cu->per_cu->length;
11612
11613 dwarf_read_debug_printf (" offset %s, dwo_id %s",
11614 sect_offset_str (sect_off),
11615 hex_string (dwo_unit->signature));
11616 }
11617
11618 /* Create the dwo_units for the CUs in a DWO_FILE.
11619 Note: This function processes DWO files only, not DWP files. */
11620
11621 static void
11622 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
11623 dwarf2_cu *cu, struct dwo_file &dwo_file,
11624 dwarf2_section_info &section, htab_up &cus_htab)
11625 {
11626 struct objfile *objfile = per_objfile->objfile;
11627 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
11628 const gdb_byte *info_ptr, *end_ptr;
11629
11630 section.read (objfile);
11631 info_ptr = section.buffer;
11632
11633 if (info_ptr == NULL)
11634 return;
11635
11636 dwarf_read_debug_printf ("Reading %s for %s:",
11637 section.get_name (),
11638 section.get_file_name ());
11639
11640 end_ptr = info_ptr + section.size;
11641 while (info_ptr < end_ptr)
11642 {
11643 struct dwarf2_per_cu_data per_cu;
11644 struct dwo_unit read_unit {};
11645 struct dwo_unit *dwo_unit;
11646 void **slot;
11647 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11648
11649 memset (&per_cu, 0, sizeof (per_cu));
11650 per_cu.per_bfd = per_bfd;
11651 per_cu.is_debug_types = 0;
11652 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11653 per_cu.section = &section;
11654
11655 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
11656 if (!reader.dummy_p)
11657 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11658 &dwo_file, &read_unit);
11659 info_ptr += per_cu.length;
11660
11661 // If the unit could not be parsed, skip it.
11662 if (read_unit.dwo_file == NULL)
11663 continue;
11664
11665 if (cus_htab == NULL)
11666 cus_htab = allocate_dwo_unit_table ();
11667
11668 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11669 struct dwo_unit);
11670 *dwo_unit = read_unit;
11671 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11672 gdb_assert (slot != NULL);
11673 if (*slot != NULL)
11674 {
11675 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11676 sect_offset dup_sect_off = dup_cu->sect_off;
11677
11678 complaint (_("debug cu entry at offset %s is duplicate to"
11679 " the entry at offset %s, signature %s"),
11680 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11681 hex_string (dwo_unit->signature));
11682 }
11683 *slot = (void *)dwo_unit;
11684 }
11685 }
11686
11687 /* DWP file .debug_{cu,tu}_index section format:
11688 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11689 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11690
11691 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11692 officially standard DWP format was published with DWARF v5 and is called
11693 Version 5. There are no versions 3 or 4.
11694
11695 DWP Version 1:
11696
11697 Both index sections have the same format, and serve to map a 64-bit
11698 signature to a set of section numbers. Each section begins with a header,
11699 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11700 indexes, and a pool of 32-bit section numbers. The index sections will be
11701 aligned at 8-byte boundaries in the file.
11702
11703 The index section header consists of:
11704
11705 V, 32 bit version number
11706 -, 32 bits unused
11707 N, 32 bit number of compilation units or type units in the index
11708 M, 32 bit number of slots in the hash table
11709
11710 Numbers are recorded using the byte order of the application binary.
11711
11712 The hash table begins at offset 16 in the section, and consists of an array
11713 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11714 order of the application binary). Unused slots in the hash table are 0.
11715 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11716
11717 The parallel table begins immediately after the hash table
11718 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11719 array of 32-bit indexes (using the byte order of the application binary),
11720 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11721 table contains a 32-bit index into the pool of section numbers. For unused
11722 hash table slots, the corresponding entry in the parallel table will be 0.
11723
11724 The pool of section numbers begins immediately following the hash table
11725 (at offset 16 + 12 * M from the beginning of the section). The pool of
11726 section numbers consists of an array of 32-bit words (using the byte order
11727 of the application binary). Each item in the array is indexed starting
11728 from 0. The hash table entry provides the index of the first section
11729 number in the set. Additional section numbers in the set follow, and the
11730 set is terminated by a 0 entry (section number 0 is not used in ELF).
11731
11732 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11733 section must be the first entry in the set, and the .debug_abbrev.dwo must
11734 be the second entry. Other members of the set may follow in any order.
11735
11736 ---
11737
11738 DWP Versions 2 and 5:
11739
11740 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
11741 and the entries in the index tables are now offsets into these sections.
11742 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11743 section.
11744
11745 Index Section Contents:
11746 Header
11747 Hash Table of Signatures dwp_hash_table.hash_table
11748 Parallel Table of Indices dwp_hash_table.unit_table
11749 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11750 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
11751
11752 The index section header consists of:
11753
11754 V, 32 bit version number
11755 L, 32 bit number of columns in the table of section offsets
11756 N, 32 bit number of compilation units or type units in the index
11757 M, 32 bit number of slots in the hash table
11758
11759 Numbers are recorded using the byte order of the application binary.
11760
11761 The hash table has the same format as version 1.
11762 The parallel table of indices has the same format as version 1,
11763 except that the entries are origin-1 indices into the table of sections
11764 offsets and the table of section sizes.
11765
11766 The table of offsets begins immediately following the parallel table
11767 (at offset 16 + 12 * M from the beginning of the section). The table is
11768 a two-dimensional array of 32-bit words (using the byte order of the
11769 application binary), with L columns and N+1 rows, in row-major order.
11770 Each row in the array is indexed starting from 0. The first row provides
11771 a key to the remaining rows: each column in this row provides an identifier
11772 for a debug section, and the offsets in the same column of subsequent rows
11773 refer to that section. The section identifiers for Version 2 are:
11774
11775 DW_SECT_INFO 1 .debug_info.dwo
11776 DW_SECT_TYPES 2 .debug_types.dwo
11777 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11778 DW_SECT_LINE 4 .debug_line.dwo
11779 DW_SECT_LOC 5 .debug_loc.dwo
11780 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11781 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11782 DW_SECT_MACRO 8 .debug_macro.dwo
11783
11784 The section identifiers for Version 5 are:
11785
11786 DW_SECT_INFO_V5 1 .debug_info.dwo
11787 DW_SECT_RESERVED_V5 2 --
11788 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11789 DW_SECT_LINE_V5 4 .debug_line.dwo
11790 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11791 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11792 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11793 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11794
11795 The offsets provided by the CU and TU index sections are the base offsets
11796 for the contributions made by each CU or TU to the corresponding section
11797 in the package file. Each CU and TU header contains an abbrev_offset
11798 field, used to find the abbreviations table for that CU or TU within the
11799 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11800 be interpreted as relative to the base offset given in the index section.
11801 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11802 should be interpreted as relative to the base offset for .debug_line.dwo,
11803 and offsets into other debug sections obtained from DWARF attributes should
11804 also be interpreted as relative to the corresponding base offset.
11805
11806 The table of sizes begins immediately following the table of offsets.
11807 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11808 with L columns and N rows, in row-major order. Each row in the array is
11809 indexed starting from 1 (row 0 is shared by the two tables).
11810
11811 ---
11812
11813 Hash table lookup is handled the same in version 1 and 2:
11814
11815 We assume that N and M will not exceed 2^32 - 1.
11816 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11817
11818 Given a 64-bit compilation unit signature or a type signature S, an entry
11819 in the hash table is located as follows:
11820
11821 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11822 the low-order k bits all set to 1.
11823
11824 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11825
11826 3) If the hash table entry at index H matches the signature, use that
11827 entry. If the hash table entry at index H is unused (all zeroes),
11828 terminate the search: the signature is not present in the table.
11829
11830 4) Let H = (H + H') modulo M. Repeat at Step 3.
11831
11832 Because M > N and H' and M are relatively prime, the search is guaranteed
11833 to stop at an unused slot or find the match. */
11834
11835 /* Create a hash table to map DWO IDs to their CU/TU entry in
11836 .debug_{info,types}.dwo in DWP_FILE.
11837 Returns NULL if there isn't one.
11838 Note: This function processes DWP files only, not DWO files. */
11839
11840 static struct dwp_hash_table *
11841 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11842 struct dwp_file *dwp_file, int is_debug_types)
11843 {
11844 struct objfile *objfile = per_objfile->objfile;
11845 bfd *dbfd = dwp_file->dbfd.get ();
11846 const gdb_byte *index_ptr, *index_end;
11847 struct dwarf2_section_info *index;
11848 uint32_t version, nr_columns, nr_units, nr_slots;
11849 struct dwp_hash_table *htab;
11850
11851 if (is_debug_types)
11852 index = &dwp_file->sections.tu_index;
11853 else
11854 index = &dwp_file->sections.cu_index;
11855
11856 if (index->empty ())
11857 return NULL;
11858 index->read (objfile);
11859
11860 index_ptr = index->buffer;
11861 index_end = index_ptr + index->size;
11862
11863 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11864 For now it's safe to just read 4 bytes (particularly as it's difficult to
11865 tell if you're dealing with Version 5 before you've read the version). */
11866 version = read_4_bytes (dbfd, index_ptr);
11867 index_ptr += 4;
11868 if (version == 2 || version == 5)
11869 nr_columns = read_4_bytes (dbfd, index_ptr);
11870 else
11871 nr_columns = 0;
11872 index_ptr += 4;
11873 nr_units = read_4_bytes (dbfd, index_ptr);
11874 index_ptr += 4;
11875 nr_slots = read_4_bytes (dbfd, index_ptr);
11876 index_ptr += 4;
11877
11878 if (version != 1 && version != 2 && version != 5)
11879 {
11880 error (_("Dwarf Error: unsupported DWP file version (%s)"
11881 " [in module %s]"),
11882 pulongest (version), dwp_file->name);
11883 }
11884 if (nr_slots != (nr_slots & -nr_slots))
11885 {
11886 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11887 " is not power of 2 [in module %s]"),
11888 pulongest (nr_slots), dwp_file->name);
11889 }
11890
11891 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11892 htab->version = version;
11893 htab->nr_columns = nr_columns;
11894 htab->nr_units = nr_units;
11895 htab->nr_slots = nr_slots;
11896 htab->hash_table = index_ptr;
11897 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11898
11899 /* Exit early if the table is empty. */
11900 if (nr_slots == 0 || nr_units == 0
11901 || (version == 2 && nr_columns == 0)
11902 || (version == 5 && nr_columns == 0))
11903 {
11904 /* All must be zero. */
11905 if (nr_slots != 0 || nr_units != 0
11906 || (version == 2 && nr_columns != 0)
11907 || (version == 5 && nr_columns != 0))
11908 {
11909 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11910 " all zero [in modules %s]"),
11911 dwp_file->name);
11912 }
11913 return htab;
11914 }
11915
11916 if (version == 1)
11917 {
11918 htab->section_pool.v1.indices =
11919 htab->unit_table + sizeof (uint32_t) * nr_slots;
11920 /* It's harder to decide whether the section is too small in v1.
11921 V1 is deprecated anyway so we punt. */
11922 }
11923 else if (version == 2)
11924 {
11925 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11926 int *ids = htab->section_pool.v2.section_ids;
11927 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11928 /* Reverse map for error checking. */
11929 int ids_seen[DW_SECT_MAX + 1];
11930 int i;
11931
11932 if (nr_columns < 2)
11933 {
11934 error (_("Dwarf Error: bad DWP hash table, too few columns"
11935 " in section table [in module %s]"),
11936 dwp_file->name);
11937 }
11938 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11939 {
11940 error (_("Dwarf Error: bad DWP hash table, too many columns"
11941 " in section table [in module %s]"),
11942 dwp_file->name);
11943 }
11944 memset (ids, 255, sizeof_ids);
11945 memset (ids_seen, 255, sizeof (ids_seen));
11946 for (i = 0; i < nr_columns; ++i)
11947 {
11948 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11949
11950 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11951 {
11952 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11953 " in section table [in module %s]"),
11954 id, dwp_file->name);
11955 }
11956 if (ids_seen[id] != -1)
11957 {
11958 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11959 " id %d in section table [in module %s]"),
11960 id, dwp_file->name);
11961 }
11962 ids_seen[id] = i;
11963 ids[i] = id;
11964 }
11965 /* Must have exactly one info or types section. */
11966 if (((ids_seen[DW_SECT_INFO] != -1)
11967 + (ids_seen[DW_SECT_TYPES] != -1))
11968 != 1)
11969 {
11970 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11971 " DWO info/types section [in module %s]"),
11972 dwp_file->name);
11973 }
11974 /* Must have an abbrev section. */
11975 if (ids_seen[DW_SECT_ABBREV] == -1)
11976 {
11977 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11978 " section [in module %s]"),
11979 dwp_file->name);
11980 }
11981 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11982 htab->section_pool.v2.sizes =
11983 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11984 * nr_units * nr_columns);
11985 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11986 * nr_units * nr_columns))
11987 > index_end)
11988 {
11989 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11990 " [in module %s]"),
11991 dwp_file->name);
11992 }
11993 }
11994 else /* version == 5 */
11995 {
11996 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11997 int *ids = htab->section_pool.v5.section_ids;
11998 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11999 /* Reverse map for error checking. */
12000 int ids_seen[DW_SECT_MAX_V5 + 1];
12001
12002 if (nr_columns < 2)
12003 {
12004 error (_("Dwarf Error: bad DWP hash table, too few columns"
12005 " in section table [in module %s]"),
12006 dwp_file->name);
12007 }
12008 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
12009 {
12010 error (_("Dwarf Error: bad DWP hash table, too many columns"
12011 " in section table [in module %s]"),
12012 dwp_file->name);
12013 }
12014 memset (ids, 255, sizeof_ids);
12015 memset (ids_seen, 255, sizeof (ids_seen));
12016 for (int i = 0; i < nr_columns; ++i)
12017 {
12018 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12019
12020 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
12021 {
12022 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12023 " in section table [in module %s]"),
12024 id, dwp_file->name);
12025 }
12026 if (ids_seen[id] != -1)
12027 {
12028 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12029 " id %d in section table [in module %s]"),
12030 id, dwp_file->name);
12031 }
12032 ids_seen[id] = i;
12033 ids[i] = id;
12034 }
12035 /* Must have seen an info section. */
12036 if (ids_seen[DW_SECT_INFO_V5] == -1)
12037 {
12038 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12039 " DWO info/types section [in module %s]"),
12040 dwp_file->name);
12041 }
12042 /* Must have an abbrev section. */
12043 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
12044 {
12045 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12046 " section [in module %s]"),
12047 dwp_file->name);
12048 }
12049 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12050 htab->section_pool.v5.sizes
12051 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
12052 * nr_units * nr_columns);
12053 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
12054 * nr_units * nr_columns))
12055 > index_end)
12056 {
12057 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12058 " [in module %s]"),
12059 dwp_file->name);
12060 }
12061 }
12062
12063 return htab;
12064 }
12065
12066 /* Update SECTIONS with the data from SECTP.
12067
12068 This function is like the other "locate" section routines, but in
12069 this context the sections to read comes from the DWP V1 hash table,
12070 not the full ELF section table.
12071
12072 The result is non-zero for success, or zero if an error was found. */
12073
12074 static int
12075 locate_v1_virtual_dwo_sections (asection *sectp,
12076 struct virtual_v1_dwo_sections *sections)
12077 {
12078 const struct dwop_section_names *names = &dwop_section_names;
12079
12080 if (names->abbrev_dwo.matches (sectp->name))
12081 {
12082 /* There can be only one. */
12083 if (sections->abbrev.s.section != NULL)
12084 return 0;
12085 sections->abbrev.s.section = sectp;
12086 sections->abbrev.size = bfd_section_size (sectp);
12087 }
12088 else if (names->info_dwo.matches (sectp->name)
12089 || names->types_dwo.matches (sectp->name))
12090 {
12091 /* There can be only one. */
12092 if (sections->info_or_types.s.section != NULL)
12093 return 0;
12094 sections->info_or_types.s.section = sectp;
12095 sections->info_or_types.size = bfd_section_size (sectp);
12096 }
12097 else if (names->line_dwo.matches (sectp->name))
12098 {
12099 /* There can be only one. */
12100 if (sections->line.s.section != NULL)
12101 return 0;
12102 sections->line.s.section = sectp;
12103 sections->line.size = bfd_section_size (sectp);
12104 }
12105 else if (names->loc_dwo.matches (sectp->name))
12106 {
12107 /* There can be only one. */
12108 if (sections->loc.s.section != NULL)
12109 return 0;
12110 sections->loc.s.section = sectp;
12111 sections->loc.size = bfd_section_size (sectp);
12112 }
12113 else if (names->macinfo_dwo.matches (sectp->name))
12114 {
12115 /* There can be only one. */
12116 if (sections->macinfo.s.section != NULL)
12117 return 0;
12118 sections->macinfo.s.section = sectp;
12119 sections->macinfo.size = bfd_section_size (sectp);
12120 }
12121 else if (names->macro_dwo.matches (sectp->name))
12122 {
12123 /* There can be only one. */
12124 if (sections->macro.s.section != NULL)
12125 return 0;
12126 sections->macro.s.section = sectp;
12127 sections->macro.size = bfd_section_size (sectp);
12128 }
12129 else if (names->str_offsets_dwo.matches (sectp->name))
12130 {
12131 /* There can be only one. */
12132 if (sections->str_offsets.s.section != NULL)
12133 return 0;
12134 sections->str_offsets.s.section = sectp;
12135 sections->str_offsets.size = bfd_section_size (sectp);
12136 }
12137 else
12138 {
12139 /* No other kind of section is valid. */
12140 return 0;
12141 }
12142
12143 return 1;
12144 }
12145
12146 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12147 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12148 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12149 This is for DWP version 1 files. */
12150
12151 static struct dwo_unit *
12152 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
12153 struct dwp_file *dwp_file,
12154 uint32_t unit_index,
12155 const char *comp_dir,
12156 ULONGEST signature, int is_debug_types)
12157 {
12158 const struct dwp_hash_table *dwp_htab =
12159 is_debug_types ? dwp_file->tus : dwp_file->cus;
12160 bfd *dbfd = dwp_file->dbfd.get ();
12161 const char *kind = is_debug_types ? "TU" : "CU";
12162 struct dwo_file *dwo_file;
12163 struct dwo_unit *dwo_unit;
12164 struct virtual_v1_dwo_sections sections;
12165 void **dwo_file_slot;
12166 int i;
12167
12168 gdb_assert (dwp_file->version == 1);
12169
12170 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
12171 kind, pulongest (unit_index), hex_string (signature),
12172 dwp_file->name);
12173
12174 /* Fetch the sections of this DWO unit.
12175 Put a limit on the number of sections we look for so that bad data
12176 doesn't cause us to loop forever. */
12177
12178 #define MAX_NR_V1_DWO_SECTIONS \
12179 (1 /* .debug_info or .debug_types */ \
12180 + 1 /* .debug_abbrev */ \
12181 + 1 /* .debug_line */ \
12182 + 1 /* .debug_loc */ \
12183 + 1 /* .debug_str_offsets */ \
12184 + 1 /* .debug_macro or .debug_macinfo */ \
12185 + 1 /* trailing zero */)
12186
12187 memset (&sections, 0, sizeof (sections));
12188
12189 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12190 {
12191 asection *sectp;
12192 uint32_t section_nr =
12193 read_4_bytes (dbfd,
12194 dwp_htab->section_pool.v1.indices
12195 + (unit_index + i) * sizeof (uint32_t));
12196
12197 if (section_nr == 0)
12198 break;
12199 if (section_nr >= dwp_file->num_sections)
12200 {
12201 error (_("Dwarf Error: bad DWP hash table, section number too large"
12202 " [in module %s]"),
12203 dwp_file->name);
12204 }
12205
12206 sectp = dwp_file->elf_sections[section_nr];
12207 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12208 {
12209 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12210 " [in module %s]"),
12211 dwp_file->name);
12212 }
12213 }
12214
12215 if (i < 2
12216 || sections.info_or_types.empty ()
12217 || sections.abbrev.empty ())
12218 {
12219 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12220 " [in module %s]"),
12221 dwp_file->name);
12222 }
12223 if (i == MAX_NR_V1_DWO_SECTIONS)
12224 {
12225 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12226 " [in module %s]"),
12227 dwp_file->name);
12228 }
12229
12230 /* It's easier for the rest of the code if we fake a struct dwo_file and
12231 have dwo_unit "live" in that. At least for now.
12232
12233 The DWP file can be made up of a random collection of CUs and TUs.
12234 However, for each CU + set of TUs that came from the same original DWO
12235 file, we can combine them back into a virtual DWO file to save space
12236 (fewer struct dwo_file objects to allocate). Remember that for really
12237 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12238
12239 std::string virtual_dwo_name =
12240 string_printf ("virtual-dwo/%d-%d-%d-%d",
12241 sections.abbrev.get_id (),
12242 sections.line.get_id (),
12243 sections.loc.get_id (),
12244 sections.str_offsets.get_id ());
12245 /* Can we use an existing virtual DWO file? */
12246 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12247 comp_dir);
12248 /* Create one if necessary. */
12249 if (*dwo_file_slot == NULL)
12250 {
12251 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12252 virtual_dwo_name.c_str ());
12253
12254 dwo_file = new struct dwo_file;
12255 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12256 dwo_file->comp_dir = comp_dir;
12257 dwo_file->sections.abbrev = sections.abbrev;
12258 dwo_file->sections.line = sections.line;
12259 dwo_file->sections.loc = sections.loc;
12260 dwo_file->sections.macinfo = sections.macinfo;
12261 dwo_file->sections.macro = sections.macro;
12262 dwo_file->sections.str_offsets = sections.str_offsets;
12263 /* The "str" section is global to the entire DWP file. */
12264 dwo_file->sections.str = dwp_file->sections.str;
12265 /* The info or types section is assigned below to dwo_unit,
12266 there's no need to record it in dwo_file.
12267 Also, we can't simply record type sections in dwo_file because
12268 we record a pointer into the vector in dwo_unit. As we collect more
12269 types we'll grow the vector and eventually have to reallocate space
12270 for it, invalidating all copies of pointers into the previous
12271 contents. */
12272 *dwo_file_slot = dwo_file;
12273 }
12274 else
12275 {
12276 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12277 virtual_dwo_name.c_str ());
12278
12279 dwo_file = (struct dwo_file *) *dwo_file_slot;
12280 }
12281
12282 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12283 dwo_unit->dwo_file = dwo_file;
12284 dwo_unit->signature = signature;
12285 dwo_unit->section =
12286 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12287 *dwo_unit->section = sections.info_or_types;
12288 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12289
12290 return dwo_unit;
12291 }
12292
12293 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
12294 simplify them. Given a pointer to the containing section SECTION, and
12295 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
12296 virtual section of just that piece. */
12297
12298 static struct dwarf2_section_info
12299 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
12300 struct dwarf2_section_info *section,
12301 bfd_size_type offset, bfd_size_type size)
12302 {
12303 struct dwarf2_section_info result;
12304 asection *sectp;
12305
12306 gdb_assert (section != NULL);
12307 gdb_assert (!section->is_virtual);
12308
12309 memset (&result, 0, sizeof (result));
12310 result.s.containing_section = section;
12311 result.is_virtual = true;
12312
12313 if (size == 0)
12314 return result;
12315
12316 sectp = section->get_bfd_section ();
12317
12318 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12319 bounds of the real section. This is a pretty-rare event, so just
12320 flag an error (easier) instead of a warning and trying to cope. */
12321 if (sectp == NULL
12322 || offset + size > bfd_section_size (sectp))
12323 {
12324 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
12325 " in section %s [in module %s]"),
12326 sectp ? bfd_section_name (sectp) : "<unknown>",
12327 objfile_name (per_objfile->objfile));
12328 }
12329
12330 result.virtual_offset = offset;
12331 result.size = size;
12332 return result;
12333 }
12334
12335 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12336 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12337 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12338 This is for DWP version 2 files. */
12339
12340 static struct dwo_unit *
12341 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
12342 struct dwp_file *dwp_file,
12343 uint32_t unit_index,
12344 const char *comp_dir,
12345 ULONGEST signature, int is_debug_types)
12346 {
12347 const struct dwp_hash_table *dwp_htab =
12348 is_debug_types ? dwp_file->tus : dwp_file->cus;
12349 bfd *dbfd = dwp_file->dbfd.get ();
12350 const char *kind = is_debug_types ? "TU" : "CU";
12351 struct dwo_file *dwo_file;
12352 struct dwo_unit *dwo_unit;
12353 struct virtual_v2_or_v5_dwo_sections sections;
12354 void **dwo_file_slot;
12355 int i;
12356
12357 gdb_assert (dwp_file->version == 2);
12358
12359 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
12360 kind, pulongest (unit_index), hex_string (signature),
12361 dwp_file->name);
12362
12363 /* Fetch the section offsets of this DWO unit. */
12364
12365 memset (&sections, 0, sizeof (sections));
12366
12367 for (i = 0; i < dwp_htab->nr_columns; ++i)
12368 {
12369 uint32_t offset = read_4_bytes (dbfd,
12370 dwp_htab->section_pool.v2.offsets
12371 + (((unit_index - 1) * dwp_htab->nr_columns
12372 + i)
12373 * sizeof (uint32_t)));
12374 uint32_t size = read_4_bytes (dbfd,
12375 dwp_htab->section_pool.v2.sizes
12376 + (((unit_index - 1) * dwp_htab->nr_columns
12377 + i)
12378 * sizeof (uint32_t)));
12379
12380 switch (dwp_htab->section_pool.v2.section_ids[i])
12381 {
12382 case DW_SECT_INFO:
12383 case DW_SECT_TYPES:
12384 sections.info_or_types_offset = offset;
12385 sections.info_or_types_size = size;
12386 break;
12387 case DW_SECT_ABBREV:
12388 sections.abbrev_offset = offset;
12389 sections.abbrev_size = size;
12390 break;
12391 case DW_SECT_LINE:
12392 sections.line_offset = offset;
12393 sections.line_size = size;
12394 break;
12395 case DW_SECT_LOC:
12396 sections.loc_offset = offset;
12397 sections.loc_size = size;
12398 break;
12399 case DW_SECT_STR_OFFSETS:
12400 sections.str_offsets_offset = offset;
12401 sections.str_offsets_size = size;
12402 break;
12403 case DW_SECT_MACINFO:
12404 sections.macinfo_offset = offset;
12405 sections.macinfo_size = size;
12406 break;
12407 case DW_SECT_MACRO:
12408 sections.macro_offset = offset;
12409 sections.macro_size = size;
12410 break;
12411 }
12412 }
12413
12414 /* It's easier for the rest of the code if we fake a struct dwo_file and
12415 have dwo_unit "live" in that. At least for now.
12416
12417 The DWP file can be made up of a random collection of CUs and TUs.
12418 However, for each CU + set of TUs that came from the same original DWO
12419 file, we can combine them back into a virtual DWO file to save space
12420 (fewer struct dwo_file objects to allocate). Remember that for really
12421 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12422
12423 std::string virtual_dwo_name =
12424 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12425 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12426 (long) (sections.line_size ? sections.line_offset : 0),
12427 (long) (sections.loc_size ? sections.loc_offset : 0),
12428 (long) (sections.str_offsets_size
12429 ? sections.str_offsets_offset : 0));
12430 /* Can we use an existing virtual DWO file? */
12431 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12432 comp_dir);
12433 /* Create one if necessary. */
12434 if (*dwo_file_slot == NULL)
12435 {
12436 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12437 virtual_dwo_name.c_str ());
12438
12439 dwo_file = new struct dwo_file;
12440 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12441 dwo_file->comp_dir = comp_dir;
12442 dwo_file->sections.abbrev =
12443 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12444 sections.abbrev_offset,
12445 sections.abbrev_size);
12446 dwo_file->sections.line =
12447 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12448 sections.line_offset,
12449 sections.line_size);
12450 dwo_file->sections.loc =
12451 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12452 sections.loc_offset, sections.loc_size);
12453 dwo_file->sections.macinfo =
12454 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12455 sections.macinfo_offset,
12456 sections.macinfo_size);
12457 dwo_file->sections.macro =
12458 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12459 sections.macro_offset,
12460 sections.macro_size);
12461 dwo_file->sections.str_offsets =
12462 create_dwp_v2_or_v5_section (per_objfile,
12463 &dwp_file->sections.str_offsets,
12464 sections.str_offsets_offset,
12465 sections.str_offsets_size);
12466 /* The "str" section is global to the entire DWP file. */
12467 dwo_file->sections.str = dwp_file->sections.str;
12468 /* The info or types section is assigned below to dwo_unit,
12469 there's no need to record it in dwo_file.
12470 Also, we can't simply record type sections in dwo_file because
12471 we record a pointer into the vector in dwo_unit. As we collect more
12472 types we'll grow the vector and eventually have to reallocate space
12473 for it, invalidating all copies of pointers into the previous
12474 contents. */
12475 *dwo_file_slot = dwo_file;
12476 }
12477 else
12478 {
12479 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12480 virtual_dwo_name.c_str ());
12481
12482 dwo_file = (struct dwo_file *) *dwo_file_slot;
12483 }
12484
12485 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12486 dwo_unit->dwo_file = dwo_file;
12487 dwo_unit->signature = signature;
12488 dwo_unit->section =
12489 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12490 *dwo_unit->section = create_dwp_v2_or_v5_section
12491 (per_objfile,
12492 is_debug_types
12493 ? &dwp_file->sections.types
12494 : &dwp_file->sections.info,
12495 sections.info_or_types_offset,
12496 sections.info_or_types_size);
12497 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12498
12499 return dwo_unit;
12500 }
12501
12502 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12503 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12504 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12505 This is for DWP version 5 files. */
12506
12507 static struct dwo_unit *
12508 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12509 struct dwp_file *dwp_file,
12510 uint32_t unit_index,
12511 const char *comp_dir,
12512 ULONGEST signature, int is_debug_types)
12513 {
12514 const struct dwp_hash_table *dwp_htab
12515 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12516 bfd *dbfd = dwp_file->dbfd.get ();
12517 const char *kind = is_debug_types ? "TU" : "CU";
12518 struct dwo_file *dwo_file;
12519 struct dwo_unit *dwo_unit;
12520 struct virtual_v2_or_v5_dwo_sections sections {};
12521 void **dwo_file_slot;
12522
12523 gdb_assert (dwp_file->version == 5);
12524
12525 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
12526 kind, pulongest (unit_index), hex_string (signature),
12527 dwp_file->name);
12528
12529 /* Fetch the section offsets of this DWO unit. */
12530
12531 /* memset (&sections, 0, sizeof (sections)); */
12532
12533 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12534 {
12535 uint32_t offset = read_4_bytes (dbfd,
12536 dwp_htab->section_pool.v5.offsets
12537 + (((unit_index - 1)
12538 * dwp_htab->nr_columns
12539 + i)
12540 * sizeof (uint32_t)));
12541 uint32_t size = read_4_bytes (dbfd,
12542 dwp_htab->section_pool.v5.sizes
12543 + (((unit_index - 1) * dwp_htab->nr_columns
12544 + i)
12545 * sizeof (uint32_t)));
12546
12547 switch (dwp_htab->section_pool.v5.section_ids[i])
12548 {
12549 case DW_SECT_ABBREV_V5:
12550 sections.abbrev_offset = offset;
12551 sections.abbrev_size = size;
12552 break;
12553 case DW_SECT_INFO_V5:
12554 sections.info_or_types_offset = offset;
12555 sections.info_or_types_size = size;
12556 break;
12557 case DW_SECT_LINE_V5:
12558 sections.line_offset = offset;
12559 sections.line_size = size;
12560 break;
12561 case DW_SECT_LOCLISTS_V5:
12562 sections.loclists_offset = offset;
12563 sections.loclists_size = size;
12564 break;
12565 case DW_SECT_MACRO_V5:
12566 sections.macro_offset = offset;
12567 sections.macro_size = size;
12568 break;
12569 case DW_SECT_RNGLISTS_V5:
12570 sections.rnglists_offset = offset;
12571 sections.rnglists_size = size;
12572 break;
12573 case DW_SECT_STR_OFFSETS_V5:
12574 sections.str_offsets_offset = offset;
12575 sections.str_offsets_size = size;
12576 break;
12577 case DW_SECT_RESERVED_V5:
12578 default:
12579 break;
12580 }
12581 }
12582
12583 /* It's easier for the rest of the code if we fake a struct dwo_file and
12584 have dwo_unit "live" in that. At least for now.
12585
12586 The DWP file can be made up of a random collection of CUs and TUs.
12587 However, for each CU + set of TUs that came from the same original DWO
12588 file, we can combine them back into a virtual DWO file to save space
12589 (fewer struct dwo_file objects to allocate). Remember that for really
12590 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12591
12592 std::string virtual_dwo_name =
12593 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
12594 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12595 (long) (sections.line_size ? sections.line_offset : 0),
12596 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12597 (long) (sections.str_offsets_size
12598 ? sections.str_offsets_offset : 0),
12599 (long) (sections.macro_size ? sections.macro_offset : 0),
12600 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
12601 /* Can we use an existing virtual DWO file? */
12602 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
12603 virtual_dwo_name.c_str (),
12604 comp_dir);
12605 /* Create one if necessary. */
12606 if (*dwo_file_slot == NULL)
12607 {
12608 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12609 virtual_dwo_name.c_str ());
12610
12611 dwo_file = new struct dwo_file;
12612 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12613 dwo_file->comp_dir = comp_dir;
12614 dwo_file->sections.abbrev =
12615 create_dwp_v2_or_v5_section (per_objfile,
12616 &dwp_file->sections.abbrev,
12617 sections.abbrev_offset,
12618 sections.abbrev_size);
12619 dwo_file->sections.line =
12620 create_dwp_v2_or_v5_section (per_objfile,
12621 &dwp_file->sections.line,
12622 sections.line_offset, sections.line_size);
12623 dwo_file->sections.macro =
12624 create_dwp_v2_or_v5_section (per_objfile,
12625 &dwp_file->sections.macro,
12626 sections.macro_offset,
12627 sections.macro_size);
12628 dwo_file->sections.loclists =
12629 create_dwp_v2_or_v5_section (per_objfile,
12630 &dwp_file->sections.loclists,
12631 sections.loclists_offset,
12632 sections.loclists_size);
12633 dwo_file->sections.rnglists =
12634 create_dwp_v2_or_v5_section (per_objfile,
12635 &dwp_file->sections.rnglists,
12636 sections.rnglists_offset,
12637 sections.rnglists_size);
12638 dwo_file->sections.str_offsets =
12639 create_dwp_v2_or_v5_section (per_objfile,
12640 &dwp_file->sections.str_offsets,
12641 sections.str_offsets_offset,
12642 sections.str_offsets_size);
12643 /* The "str" section is global to the entire DWP file. */
12644 dwo_file->sections.str = dwp_file->sections.str;
12645 /* The info or types section is assigned below to dwo_unit,
12646 there's no need to record it in dwo_file.
12647 Also, we can't simply record type sections in dwo_file because
12648 we record a pointer into the vector in dwo_unit. As we collect more
12649 types we'll grow the vector and eventually have to reallocate space
12650 for it, invalidating all copies of pointers into the previous
12651 contents. */
12652 *dwo_file_slot = dwo_file;
12653 }
12654 else
12655 {
12656 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12657 virtual_dwo_name.c_str ());
12658
12659 dwo_file = (struct dwo_file *) *dwo_file_slot;
12660 }
12661
12662 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12663 dwo_unit->dwo_file = dwo_file;
12664 dwo_unit->signature = signature;
12665 dwo_unit->section
12666 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12667 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
12668 &dwp_file->sections.info,
12669 sections.info_or_types_offset,
12670 sections.info_or_types_size);
12671 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12672
12673 return dwo_unit;
12674 }
12675
12676 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12677 Returns NULL if the signature isn't found. */
12678
12679 static struct dwo_unit *
12680 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12681 struct dwp_file *dwp_file, const char *comp_dir,
12682 ULONGEST signature, int is_debug_types)
12683 {
12684 const struct dwp_hash_table *dwp_htab =
12685 is_debug_types ? dwp_file->tus : dwp_file->cus;
12686 bfd *dbfd = dwp_file->dbfd.get ();
12687 uint32_t mask = dwp_htab->nr_slots - 1;
12688 uint32_t hash = signature & mask;
12689 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12690 unsigned int i;
12691 void **slot;
12692 struct dwo_unit find_dwo_cu;
12693
12694 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12695 find_dwo_cu.signature = signature;
12696 slot = htab_find_slot (is_debug_types
12697 ? dwp_file->loaded_tus.get ()
12698 : dwp_file->loaded_cus.get (),
12699 &find_dwo_cu, INSERT);
12700
12701 if (*slot != NULL)
12702 return (struct dwo_unit *) *slot;
12703
12704 /* Use a for loop so that we don't loop forever on bad debug info. */
12705 for (i = 0; i < dwp_htab->nr_slots; ++i)
12706 {
12707 ULONGEST signature_in_table;
12708
12709 signature_in_table =
12710 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12711 if (signature_in_table == signature)
12712 {
12713 uint32_t unit_index =
12714 read_4_bytes (dbfd,
12715 dwp_htab->unit_table + hash * sizeof (uint32_t));
12716
12717 if (dwp_file->version == 1)
12718 {
12719 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12720 unit_index, comp_dir,
12721 signature, is_debug_types);
12722 }
12723 else if (dwp_file->version == 2)
12724 {
12725 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12726 unit_index, comp_dir,
12727 signature, is_debug_types);
12728 }
12729 else /* version == 5 */
12730 {
12731 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12732 unit_index, comp_dir,
12733 signature, is_debug_types);
12734 }
12735 return (struct dwo_unit *) *slot;
12736 }
12737 if (signature_in_table == 0)
12738 return NULL;
12739 hash = (hash + hash2) & mask;
12740 }
12741
12742 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12743 " [in module %s]"),
12744 dwp_file->name);
12745 }
12746
12747 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12748 Open the file specified by FILE_NAME and hand it off to BFD for
12749 preliminary analysis. Return a newly initialized bfd *, which
12750 includes a canonicalized copy of FILE_NAME.
12751 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12752 SEARCH_CWD is true if the current directory is to be searched.
12753 It will be searched before debug-file-directory.
12754 If successful, the file is added to the bfd include table of the
12755 objfile's bfd (see gdb_bfd_record_inclusion).
12756 If unable to find/open the file, return NULL.
12757 NOTE: This function is derived from symfile_bfd_open. */
12758
12759 static gdb_bfd_ref_ptr
12760 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12761 const char *file_name, int is_dwp, int search_cwd)
12762 {
12763 int desc;
12764 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12765 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12766 to debug_file_directory. */
12767 const char *search_path;
12768 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12769
12770 gdb::unique_xmalloc_ptr<char> search_path_holder;
12771 if (search_cwd)
12772 {
12773 if (*debug_file_directory != '\0')
12774 {
12775 search_path_holder.reset (concat (".", dirname_separator_string,
12776 debug_file_directory,
12777 (char *) NULL));
12778 search_path = search_path_holder.get ();
12779 }
12780 else
12781 search_path = ".";
12782 }
12783 else
12784 search_path = debug_file_directory;
12785
12786 /* Add the path for the executable binary to the list of search paths. */
12787 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
12788 search_path_holder.reset (concat (objfile_dir.c_str (),
12789 dirname_separator_string,
12790 search_path, nullptr));
12791 search_path = search_path_holder.get ();
12792
12793 openp_flags flags = OPF_RETURN_REALPATH;
12794 if (is_dwp)
12795 flags |= OPF_SEARCH_IN_PATH;
12796
12797 gdb::unique_xmalloc_ptr<char> absolute_name;
12798 desc = openp (search_path, flags, file_name,
12799 O_RDONLY | O_BINARY, &absolute_name);
12800 if (desc < 0)
12801 return NULL;
12802
12803 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12804 gnutarget, desc));
12805 if (sym_bfd == NULL)
12806 return NULL;
12807 bfd_set_cacheable (sym_bfd.get (), 1);
12808
12809 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12810 return NULL;
12811
12812 /* Success. Record the bfd as having been included by the objfile's bfd.
12813 This is important because things like demangled_names_hash lives in the
12814 objfile's per_bfd space and may have references to things like symbol
12815 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12816 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12817
12818 return sym_bfd;
12819 }
12820
12821 /* Try to open DWO file FILE_NAME.
12822 COMP_DIR is the DW_AT_comp_dir attribute.
12823 The result is the bfd handle of the file.
12824 If there is a problem finding or opening the file, return NULL.
12825 Upon success, the canonicalized path of the file is stored in the bfd,
12826 same as symfile_bfd_open. */
12827
12828 static gdb_bfd_ref_ptr
12829 open_dwo_file (dwarf2_per_objfile *per_objfile,
12830 const char *file_name, const char *comp_dir)
12831 {
12832 if (IS_ABSOLUTE_PATH (file_name))
12833 return try_open_dwop_file (per_objfile, file_name,
12834 0 /*is_dwp*/, 0 /*search_cwd*/);
12835
12836 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12837
12838 if (comp_dir != NULL)
12839 {
12840 gdb::unique_xmalloc_ptr<char> path_to_try
12841 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12842
12843 /* NOTE: If comp_dir is a relative path, this will also try the
12844 search path, which seems useful. */
12845 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12846 0 /*is_dwp*/,
12847 1 /*search_cwd*/));
12848 if (abfd != NULL)
12849 return abfd;
12850 }
12851
12852 /* That didn't work, try debug-file-directory, which, despite its name,
12853 is a list of paths. */
12854
12855 if (*debug_file_directory == '\0')
12856 return NULL;
12857
12858 return try_open_dwop_file (per_objfile, file_name,
12859 0 /*is_dwp*/, 1 /*search_cwd*/);
12860 }
12861
12862 /* This function is mapped across the sections and remembers the offset and
12863 size of each of the DWO debugging sections we are interested in. */
12864
12865 static void
12866 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12867 dwo_sections *dwo_sections)
12868 {
12869 const struct dwop_section_names *names = &dwop_section_names;
12870
12871 if (names->abbrev_dwo.matches (sectp->name))
12872 {
12873 dwo_sections->abbrev.s.section = sectp;
12874 dwo_sections->abbrev.size = bfd_section_size (sectp);
12875 }
12876 else if (names->info_dwo.matches (sectp->name))
12877 {
12878 dwo_sections->info.s.section = sectp;
12879 dwo_sections->info.size = bfd_section_size (sectp);
12880 }
12881 else if (names->line_dwo.matches (sectp->name))
12882 {
12883 dwo_sections->line.s.section = sectp;
12884 dwo_sections->line.size = bfd_section_size (sectp);
12885 }
12886 else if (names->loc_dwo.matches (sectp->name))
12887 {
12888 dwo_sections->loc.s.section = sectp;
12889 dwo_sections->loc.size = bfd_section_size (sectp);
12890 }
12891 else if (names->loclists_dwo.matches (sectp->name))
12892 {
12893 dwo_sections->loclists.s.section = sectp;
12894 dwo_sections->loclists.size = bfd_section_size (sectp);
12895 }
12896 else if (names->macinfo_dwo.matches (sectp->name))
12897 {
12898 dwo_sections->macinfo.s.section = sectp;
12899 dwo_sections->macinfo.size = bfd_section_size (sectp);
12900 }
12901 else if (names->macro_dwo.matches (sectp->name))
12902 {
12903 dwo_sections->macro.s.section = sectp;
12904 dwo_sections->macro.size = bfd_section_size (sectp);
12905 }
12906 else if (names->rnglists_dwo.matches (sectp->name))
12907 {
12908 dwo_sections->rnglists.s.section = sectp;
12909 dwo_sections->rnglists.size = bfd_section_size (sectp);
12910 }
12911 else if (names->str_dwo.matches (sectp->name))
12912 {
12913 dwo_sections->str.s.section = sectp;
12914 dwo_sections->str.size = bfd_section_size (sectp);
12915 }
12916 else if (names->str_offsets_dwo.matches (sectp->name))
12917 {
12918 dwo_sections->str_offsets.s.section = sectp;
12919 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12920 }
12921 else if (names->types_dwo.matches (sectp->name))
12922 {
12923 struct dwarf2_section_info type_section;
12924
12925 memset (&type_section, 0, sizeof (type_section));
12926 type_section.s.section = sectp;
12927 type_section.size = bfd_section_size (sectp);
12928 dwo_sections->types.push_back (type_section);
12929 }
12930 }
12931
12932 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12933 by PER_CU. This is for the non-DWP case.
12934 The result is NULL if DWO_NAME can't be found. */
12935
12936 static struct dwo_file *
12937 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12938 const char *comp_dir)
12939 {
12940 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12941
12942 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12943 if (dbfd == NULL)
12944 {
12945 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12946
12947 return NULL;
12948 }
12949
12950 dwo_file_up dwo_file (new struct dwo_file);
12951 dwo_file->dwo_name = dwo_name;
12952 dwo_file->comp_dir = comp_dir;
12953 dwo_file->dbfd = std::move (dbfd);
12954
12955 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12956 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12957 &dwo_file->sections);
12958
12959 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12960 dwo_file->cus);
12961
12962 if (cu->per_cu->dwarf_version < 5)
12963 {
12964 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12965 dwo_file->sections.types, dwo_file->tus);
12966 }
12967 else
12968 {
12969 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12970 &dwo_file->sections.info, dwo_file->tus,
12971 rcuh_kind::COMPILE);
12972 }
12973
12974 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
12975
12976 return dwo_file.release ();
12977 }
12978
12979 /* This function is mapped across the sections and remembers the offset and
12980 size of each of the DWP debugging sections common to version 1 and 2 that
12981 we are interested in. */
12982
12983 static void
12984 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12985 dwp_file *dwp_file)
12986 {
12987 const struct dwop_section_names *names = &dwop_section_names;
12988 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12989
12990 /* Record the ELF section number for later lookup: this is what the
12991 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12992 gdb_assert (elf_section_nr < dwp_file->num_sections);
12993 dwp_file->elf_sections[elf_section_nr] = sectp;
12994
12995 /* Look for specific sections that we need. */
12996 if (names->str_dwo.matches (sectp->name))
12997 {
12998 dwp_file->sections.str.s.section = sectp;
12999 dwp_file->sections.str.size = bfd_section_size (sectp);
13000 }
13001 else if (names->cu_index.matches (sectp->name))
13002 {
13003 dwp_file->sections.cu_index.s.section = sectp;
13004 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
13005 }
13006 else if (names->tu_index.matches (sectp->name))
13007 {
13008 dwp_file->sections.tu_index.s.section = sectp;
13009 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
13010 }
13011 }
13012
13013 /* This function is mapped across the sections and remembers the offset and
13014 size of each of the DWP version 2 debugging sections that we are interested
13015 in. This is split into a separate function because we don't know if we
13016 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
13017
13018 static void
13019 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13020 {
13021 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13022 const struct dwop_section_names *names = &dwop_section_names;
13023 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13024
13025 /* Record the ELF section number for later lookup: this is what the
13026 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13027 gdb_assert (elf_section_nr < dwp_file->num_sections);
13028 dwp_file->elf_sections[elf_section_nr] = sectp;
13029
13030 /* Look for specific sections that we need. */
13031 if (names->abbrev_dwo.matches (sectp->name))
13032 {
13033 dwp_file->sections.abbrev.s.section = sectp;
13034 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13035 }
13036 else if (names->info_dwo.matches (sectp->name))
13037 {
13038 dwp_file->sections.info.s.section = sectp;
13039 dwp_file->sections.info.size = bfd_section_size (sectp);
13040 }
13041 else if (names->line_dwo.matches (sectp->name))
13042 {
13043 dwp_file->sections.line.s.section = sectp;
13044 dwp_file->sections.line.size = bfd_section_size (sectp);
13045 }
13046 else if (names->loc_dwo.matches (sectp->name))
13047 {
13048 dwp_file->sections.loc.s.section = sectp;
13049 dwp_file->sections.loc.size = bfd_section_size (sectp);
13050 }
13051 else if (names->macinfo_dwo.matches (sectp->name))
13052 {
13053 dwp_file->sections.macinfo.s.section = sectp;
13054 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
13055 }
13056 else if (names->macro_dwo.matches (sectp->name))
13057 {
13058 dwp_file->sections.macro.s.section = sectp;
13059 dwp_file->sections.macro.size = bfd_section_size (sectp);
13060 }
13061 else if (names->str_offsets_dwo.matches (sectp->name))
13062 {
13063 dwp_file->sections.str_offsets.s.section = sectp;
13064 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13065 }
13066 else if (names->types_dwo.matches (sectp->name))
13067 {
13068 dwp_file->sections.types.s.section = sectp;
13069 dwp_file->sections.types.size = bfd_section_size (sectp);
13070 }
13071 }
13072
13073 /* This function is mapped across the sections and remembers the offset and
13074 size of each of the DWP version 5 debugging sections that we are interested
13075 in. This is split into a separate function because we don't know if we
13076 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
13077
13078 static void
13079 dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13080 {
13081 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13082 const struct dwop_section_names *names = &dwop_section_names;
13083 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13084
13085 /* Record the ELF section number for later lookup: this is what the
13086 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13087 gdb_assert (elf_section_nr < dwp_file->num_sections);
13088 dwp_file->elf_sections[elf_section_nr] = sectp;
13089
13090 /* Look for specific sections that we need. */
13091 if (names->abbrev_dwo.matches (sectp->name))
13092 {
13093 dwp_file->sections.abbrev.s.section = sectp;
13094 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13095 }
13096 else if (names->info_dwo.matches (sectp->name))
13097 {
13098 dwp_file->sections.info.s.section = sectp;
13099 dwp_file->sections.info.size = bfd_section_size (sectp);
13100 }
13101 else if (names->line_dwo.matches (sectp->name))
13102 {
13103 dwp_file->sections.line.s.section = sectp;
13104 dwp_file->sections.line.size = bfd_section_size (sectp);
13105 }
13106 else if (names->loclists_dwo.matches (sectp->name))
13107 {
13108 dwp_file->sections.loclists.s.section = sectp;
13109 dwp_file->sections.loclists.size = bfd_section_size (sectp);
13110 }
13111 else if (names->macro_dwo.matches (sectp->name))
13112 {
13113 dwp_file->sections.macro.s.section = sectp;
13114 dwp_file->sections.macro.size = bfd_section_size (sectp);
13115 }
13116 else if (names->rnglists_dwo.matches (sectp->name))
13117 {
13118 dwp_file->sections.rnglists.s.section = sectp;
13119 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
13120 }
13121 else if (names->str_offsets_dwo.matches (sectp->name))
13122 {
13123 dwp_file->sections.str_offsets.s.section = sectp;
13124 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13125 }
13126 }
13127
13128 /* Hash function for dwp_file loaded CUs/TUs. */
13129
13130 static hashval_t
13131 hash_dwp_loaded_cutus (const void *item)
13132 {
13133 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13134
13135 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13136 return dwo_unit->signature;
13137 }
13138
13139 /* Equality function for dwp_file loaded CUs/TUs. */
13140
13141 static int
13142 eq_dwp_loaded_cutus (const void *a, const void *b)
13143 {
13144 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13145 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13146
13147 return dua->signature == dub->signature;
13148 }
13149
13150 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13151
13152 static htab_up
13153 allocate_dwp_loaded_cutus_table ()
13154 {
13155 return htab_up (htab_create_alloc (3,
13156 hash_dwp_loaded_cutus,
13157 eq_dwp_loaded_cutus,
13158 NULL, xcalloc, xfree));
13159 }
13160
13161 /* Try to open DWP file FILE_NAME.
13162 The result is the bfd handle of the file.
13163 If there is a problem finding or opening the file, return NULL.
13164 Upon success, the canonicalized path of the file is stored in the bfd,
13165 same as symfile_bfd_open. */
13166
13167 static gdb_bfd_ref_ptr
13168 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
13169 {
13170 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
13171 1 /*is_dwp*/,
13172 1 /*search_cwd*/));
13173 if (abfd != NULL)
13174 return abfd;
13175
13176 /* Work around upstream bug 15652.
13177 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13178 [Whether that's a "bug" is debatable, but it is getting in our way.]
13179 We have no real idea where the dwp file is, because gdb's realpath-ing
13180 of the executable's path may have discarded the needed info.
13181 [IWBN if the dwp file name was recorded in the executable, akin to
13182 .gnu_debuglink, but that doesn't exist yet.]
13183 Strip the directory from FILE_NAME and search again. */
13184 if (*debug_file_directory != '\0')
13185 {
13186 /* Don't implicitly search the current directory here.
13187 If the user wants to search "." to handle this case,
13188 it must be added to debug-file-directory. */
13189 return try_open_dwop_file (per_objfile, lbasename (file_name),
13190 1 /*is_dwp*/,
13191 0 /*search_cwd*/);
13192 }
13193
13194 return NULL;
13195 }
13196
13197 /* Initialize the use of the DWP file for the current objfile.
13198 By convention the name of the DWP file is ${objfile}.dwp.
13199 The result is NULL if it can't be found. */
13200
13201 static std::unique_ptr<struct dwp_file>
13202 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
13203 {
13204 struct objfile *objfile = per_objfile->objfile;
13205
13206 /* Try to find first .dwp for the binary file before any symbolic links
13207 resolving. */
13208
13209 /* If the objfile is a debug file, find the name of the real binary
13210 file and get the name of dwp file from there. */
13211 std::string dwp_name;
13212 if (objfile->separate_debug_objfile_backlink != NULL)
13213 {
13214 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13215 const char *backlink_basename = lbasename (backlink->original_name);
13216
13217 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13218 }
13219 else
13220 dwp_name = objfile->original_name;
13221
13222 dwp_name += ".dwp";
13223
13224 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
13225 if (dbfd == NULL
13226 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13227 {
13228 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13229 dwp_name = objfile_name (objfile);
13230 dwp_name += ".dwp";
13231 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
13232 }
13233
13234 if (dbfd == NULL)
13235 {
13236 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
13237
13238 return std::unique_ptr<dwp_file> ();
13239 }
13240
13241 const char *name = bfd_get_filename (dbfd.get ());
13242 std::unique_ptr<struct dwp_file> dwp_file
13243 (new struct dwp_file (name, std::move (dbfd)));
13244
13245 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13246 dwp_file->elf_sections =
13247 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
13248 dwp_file->num_sections, asection *);
13249
13250 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13251 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
13252 dwp_file.get ());
13253
13254 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
13255
13256 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
13257
13258 /* The DWP file version is stored in the hash table. Oh well. */
13259 if (dwp_file->cus && dwp_file->tus
13260 && dwp_file->cus->version != dwp_file->tus->version)
13261 {
13262 /* Technically speaking, we should try to limp along, but this is
13263 pretty bizarre. We use pulongest here because that's the established
13264 portability solution (e.g, we cannot use %u for uint32_t). */
13265 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13266 " TU version %s [in DWP file %s]"),
13267 pulongest (dwp_file->cus->version),
13268 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13269 }
13270
13271 if (dwp_file->cus)
13272 dwp_file->version = dwp_file->cus->version;
13273 else if (dwp_file->tus)
13274 dwp_file->version = dwp_file->tus->version;
13275 else
13276 dwp_file->version = 2;
13277
13278 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13279 {
13280 if (dwp_file->version == 2)
13281 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
13282 dwp_file.get ());
13283 else
13284 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
13285 dwp_file.get ());
13286 }
13287
13288 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
13289 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
13290
13291 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
13292 dwarf_read_debug_printf (" %s CUs, %s TUs",
13293 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13294 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13295
13296 return dwp_file;
13297 }
13298
13299 /* Wrapper around open_and_init_dwp_file, only open it once. */
13300
13301 static struct dwp_file *
13302 get_dwp_file (dwarf2_per_objfile *per_objfile)
13303 {
13304 if (!per_objfile->per_bfd->dwp_checked)
13305 {
13306 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
13307 per_objfile->per_bfd->dwp_checked = 1;
13308 }
13309 return per_objfile->per_bfd->dwp_file.get ();
13310 }
13311
13312 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13313 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13314 or in the DWP file for the objfile, referenced by THIS_UNIT.
13315 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13316 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13317
13318 This is called, for example, when wanting to read a variable with a
13319 complex location. Therefore we don't want to do file i/o for every call.
13320 Therefore we don't want to look for a DWO file on every call.
13321 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13322 then we check if we've already seen DWO_NAME, and only THEN do we check
13323 for a DWO file.
13324
13325 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13326 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13327
13328 static struct dwo_unit *
13329 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13330 ULONGEST signature, int is_debug_types)
13331 {
13332 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13333 struct objfile *objfile = per_objfile->objfile;
13334 const char *kind = is_debug_types ? "TU" : "CU";
13335 void **dwo_file_slot;
13336 struct dwo_file *dwo_file;
13337 struct dwp_file *dwp_file;
13338
13339 /* First see if there's a DWP file.
13340 If we have a DWP file but didn't find the DWO inside it, don't
13341 look for the original DWO file. It makes gdb behave differently
13342 depending on whether one is debugging in the build tree. */
13343
13344 dwp_file = get_dwp_file (per_objfile);
13345 if (dwp_file != NULL)
13346 {
13347 const struct dwp_hash_table *dwp_htab =
13348 is_debug_types ? dwp_file->tus : dwp_file->cus;
13349
13350 if (dwp_htab != NULL)
13351 {
13352 struct dwo_unit *dwo_cutu =
13353 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
13354 is_debug_types);
13355
13356 if (dwo_cutu != NULL)
13357 {
13358 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
13359 kind, hex_string (signature),
13360 host_address_to_string (dwo_cutu));
13361
13362 return dwo_cutu;
13363 }
13364 }
13365 }
13366 else
13367 {
13368 /* No DWP file, look for the DWO file. */
13369
13370 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
13371 if (*dwo_file_slot == NULL)
13372 {
13373 /* Read in the file and build a table of the CUs/TUs it contains. */
13374 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
13375 }
13376 /* NOTE: This will be NULL if unable to open the file. */
13377 dwo_file = (struct dwo_file *) *dwo_file_slot;
13378
13379 if (dwo_file != NULL)
13380 {
13381 struct dwo_unit *dwo_cutu = NULL;
13382
13383 if (is_debug_types && dwo_file->tus)
13384 {
13385 struct dwo_unit find_dwo_cutu;
13386
13387 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13388 find_dwo_cutu.signature = signature;
13389 dwo_cutu
13390 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
13391 &find_dwo_cutu);
13392 }
13393 else if (!is_debug_types && dwo_file->cus)
13394 {
13395 struct dwo_unit find_dwo_cutu;
13396
13397 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13398 find_dwo_cutu.signature = signature;
13399 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
13400 &find_dwo_cutu);
13401 }
13402
13403 if (dwo_cutu != NULL)
13404 {
13405 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
13406 kind, dwo_name, hex_string (signature),
13407 host_address_to_string (dwo_cutu));
13408
13409 return dwo_cutu;
13410 }
13411 }
13412 }
13413
13414 /* We didn't find it. This could mean a dwo_id mismatch, or
13415 someone deleted the DWO/DWP file, or the search path isn't set up
13416 correctly to find the file. */
13417
13418 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
13419 kind, dwo_name, hex_string (signature));
13420
13421 /* This is a warning and not a complaint because it can be caused by
13422 pilot error (e.g., user accidentally deleting the DWO). */
13423 {
13424 /* Print the name of the DWP file if we looked there, helps the user
13425 better diagnose the problem. */
13426 std::string dwp_text;
13427
13428 if (dwp_file != NULL)
13429 dwp_text = string_printf (" [in DWP file %s]",
13430 lbasename (dwp_file->name));
13431
13432 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13433 " [in module %s]"),
13434 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13435 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
13436 }
13437 return NULL;
13438 }
13439
13440 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13441 See lookup_dwo_cutu_unit for details. */
13442
13443 static struct dwo_unit *
13444 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13445 ULONGEST signature)
13446 {
13447 gdb_assert (!cu->per_cu->is_debug_types);
13448
13449 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
13450 }
13451
13452 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13453 See lookup_dwo_cutu_unit for details. */
13454
13455 static struct dwo_unit *
13456 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
13457 {
13458 gdb_assert (cu->per_cu->is_debug_types);
13459
13460 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13461
13462 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
13463 }
13464
13465 /* Traversal function for queue_and_load_all_dwo_tus. */
13466
13467 static int
13468 queue_and_load_dwo_tu (void **slot, void *info)
13469 {
13470 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13471 dwarf2_cu *cu = (dwarf2_cu *) info;
13472 ULONGEST signature = dwo_unit->signature;
13473 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
13474
13475 if (sig_type != NULL)
13476 {
13477 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13478
13479 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13480 a real dependency of PER_CU on SIG_TYPE. That is detected later
13481 while processing PER_CU. */
13482 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
13483 load_full_type_unit (sig_cu, cu->per_objfile);
13484 cu->per_cu->imported_symtabs_push (sig_cu);
13485 }
13486
13487 return 1;
13488 }
13489
13490 /* Queue all TUs contained in the DWO of CU to be read in.
13491 The DWO may have the only definition of the type, though it may not be
13492 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13493 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13494
13495 static void
13496 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
13497 {
13498 struct dwo_unit *dwo_unit;
13499 struct dwo_file *dwo_file;
13500
13501 gdb_assert (cu != nullptr);
13502 gdb_assert (!cu->per_cu->is_debug_types);
13503 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
13504
13505 dwo_unit = cu->dwo_unit;
13506 gdb_assert (dwo_unit != NULL);
13507
13508 dwo_file = dwo_unit->dwo_file;
13509 if (dwo_file->tus != NULL)
13510 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
13511 }
13512
13513 /* Read in various DIEs. */
13514
13515 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13516 Inherit only the children of the DW_AT_abstract_origin DIE not being
13517 already referenced by DW_AT_abstract_origin from the children of the
13518 current DIE. */
13519
13520 static void
13521 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13522 {
13523 struct die_info *child_die;
13524 sect_offset *offsetp;
13525 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13526 struct die_info *origin_die;
13527 /* Iterator of the ORIGIN_DIE children. */
13528 struct die_info *origin_child_die;
13529 struct attribute *attr;
13530 struct dwarf2_cu *origin_cu;
13531 struct pending **origin_previous_list_in_scope;
13532
13533 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13534 if (!attr)
13535 return;
13536
13537 /* Note that following die references may follow to a die in a
13538 different cu. */
13539
13540 origin_cu = cu;
13541 origin_die = follow_die_ref (die, attr, &origin_cu);
13542
13543 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13544 symbols in. */
13545 origin_previous_list_in_scope = origin_cu->list_in_scope;
13546 origin_cu->list_in_scope = cu->list_in_scope;
13547
13548 if (die->tag != origin_die->tag
13549 && !(die->tag == DW_TAG_inlined_subroutine
13550 && origin_die->tag == DW_TAG_subprogram))
13551 complaint (_("DIE %s and its abstract origin %s have different tags"),
13552 sect_offset_str (die->sect_off),
13553 sect_offset_str (origin_die->sect_off));
13554
13555 /* Find if the concrete and abstract trees are structurally the
13556 same. This is a shallow traversal and it is not bullet-proof;
13557 the compiler can trick the debugger into believing that the trees
13558 are isomorphic, whereas they actually are not. However, the
13559 likelyhood of this happening is pretty low, and a full-fledged
13560 check would be an overkill. */
13561 bool are_isomorphic = true;
13562 die_info *concrete_child = die->child;
13563 die_info *abstract_child = origin_die->child;
13564 while (concrete_child != nullptr || abstract_child != nullptr)
13565 {
13566 if (concrete_child == nullptr
13567 || abstract_child == nullptr
13568 || concrete_child->tag != abstract_child->tag)
13569 {
13570 are_isomorphic = false;
13571 break;
13572 }
13573
13574 concrete_child = concrete_child->sibling;
13575 abstract_child = abstract_child->sibling;
13576 }
13577
13578 /* Walk the origin's children in parallel to the concrete children.
13579 This helps match an origin child in case the debug info misses
13580 DW_AT_abstract_origin attributes. Keep in mind that the abstract
13581 origin tree may not have the same tree structure as the concrete
13582 DIE, though. */
13583 die_info *corresponding_abstract_child
13584 = are_isomorphic ? origin_die->child : nullptr;
13585
13586 std::vector<sect_offset> offsets;
13587
13588 for (child_die = die->child;
13589 child_die && child_die->tag;
13590 child_die = child_die->sibling)
13591 {
13592 struct die_info *child_origin_die;
13593 struct dwarf2_cu *child_origin_cu;
13594
13595 /* We are trying to process concrete instance entries:
13596 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13597 it's not relevant to our analysis here. i.e. detecting DIEs that are
13598 present in the abstract instance but not referenced in the concrete
13599 one. */
13600 if (child_die->tag == DW_TAG_call_site
13601 || child_die->tag == DW_TAG_GNU_call_site)
13602 {
13603 if (are_isomorphic)
13604 corresponding_abstract_child
13605 = corresponding_abstract_child->sibling;
13606 continue;
13607 }
13608
13609 /* For each CHILD_DIE, find the corresponding child of
13610 ORIGIN_DIE. If there is more than one layer of
13611 DW_AT_abstract_origin, follow them all; there shouldn't be,
13612 but GCC versions at least through 4.4 generate this (GCC PR
13613 40573). */
13614 child_origin_die = child_die;
13615 child_origin_cu = cu;
13616 while (1)
13617 {
13618 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13619 child_origin_cu);
13620 if (attr == NULL)
13621 break;
13622 child_origin_die = follow_die_ref (child_origin_die, attr,
13623 &child_origin_cu);
13624 }
13625
13626 /* If missing DW_AT_abstract_origin, try the corresponding child
13627 of the origin. Clang emits such lexical scopes. */
13628 if (child_origin_die == child_die
13629 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
13630 && are_isomorphic
13631 && child_die->tag == DW_TAG_lexical_block)
13632 child_origin_die = corresponding_abstract_child;
13633
13634 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13635 counterpart may exist. */
13636 if (child_origin_die != child_die)
13637 {
13638 if (child_die->tag != child_origin_die->tag
13639 && !(child_die->tag == DW_TAG_inlined_subroutine
13640 && child_origin_die->tag == DW_TAG_subprogram))
13641 complaint (_("Child DIE %s and its abstract origin %s have "
13642 "different tags"),
13643 sect_offset_str (child_die->sect_off),
13644 sect_offset_str (child_origin_die->sect_off));
13645 if (child_origin_die->parent != origin_die)
13646 complaint (_("Child DIE %s and its abstract origin %s have "
13647 "different parents"),
13648 sect_offset_str (child_die->sect_off),
13649 sect_offset_str (child_origin_die->sect_off));
13650 else
13651 offsets.push_back (child_origin_die->sect_off);
13652 }
13653
13654 if (are_isomorphic)
13655 corresponding_abstract_child = corresponding_abstract_child->sibling;
13656 }
13657 std::sort (offsets.begin (), offsets.end ());
13658 sect_offset *offsets_end = offsets.data () + offsets.size ();
13659 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13660 if (offsetp[-1] == *offsetp)
13661 complaint (_("Multiple children of DIE %s refer "
13662 "to DIE %s as their abstract origin"),
13663 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13664
13665 offsetp = offsets.data ();
13666 origin_child_die = origin_die->child;
13667 while (origin_child_die && origin_child_die->tag)
13668 {
13669 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13670 while (offsetp < offsets_end
13671 && *offsetp < origin_child_die->sect_off)
13672 offsetp++;
13673 if (offsetp >= offsets_end
13674 || *offsetp > origin_child_die->sect_off)
13675 {
13676 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13677 Check whether we're already processing ORIGIN_CHILD_DIE.
13678 This can happen with mutually referenced abstract_origins.
13679 PR 16581. */
13680 if (!origin_child_die->in_process)
13681 process_die (origin_child_die, origin_cu);
13682 }
13683 origin_child_die = origin_child_die->sibling;
13684 }
13685 origin_cu->list_in_scope = origin_previous_list_in_scope;
13686
13687 if (cu != origin_cu)
13688 compute_delayed_physnames (origin_cu);
13689 }
13690
13691 static void
13692 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13693 {
13694 struct objfile *objfile = cu->per_objfile->objfile;
13695 struct gdbarch *gdbarch = objfile->arch ();
13696 struct context_stack *newobj;
13697 CORE_ADDR lowpc;
13698 CORE_ADDR highpc;
13699 struct die_info *child_die;
13700 struct attribute *attr, *call_line, *call_file;
13701 const char *name;
13702 CORE_ADDR baseaddr;
13703 struct block *block;
13704 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13705 std::vector<struct symbol *> template_args;
13706 struct template_symbol *templ_func = NULL;
13707
13708 if (inlined_func)
13709 {
13710 /* If we do not have call site information, we can't show the
13711 caller of this inlined function. That's too confusing, so
13712 only use the scope for local variables. */
13713 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13714 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13715 if (call_line == NULL || call_file == NULL)
13716 {
13717 read_lexical_block_scope (die, cu);
13718 return;
13719 }
13720 }
13721
13722 baseaddr = objfile->text_section_offset ();
13723
13724 name = dwarf2_name (die, cu);
13725
13726 /* Ignore functions with missing or empty names. These are actually
13727 illegal according to the DWARF standard. */
13728 if (name == NULL)
13729 {
13730 complaint (_("missing name for subprogram DIE at %s"),
13731 sect_offset_str (die->sect_off));
13732 return;
13733 }
13734
13735 /* Ignore functions with missing or invalid low and high pc attributes. */
13736 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13737 <= PC_BOUNDS_INVALID)
13738 {
13739 attr = dwarf2_attr (die, DW_AT_external, cu);
13740 if (attr == nullptr || !attr->as_boolean ())
13741 complaint (_("cannot get low and high bounds "
13742 "for subprogram DIE at %s"),
13743 sect_offset_str (die->sect_off));
13744 return;
13745 }
13746
13747 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13748 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13749
13750 /* If we have any template arguments, then we must allocate a
13751 different sort of symbol. */
13752 for (child_die = die->child; child_die; child_die = child_die->sibling)
13753 {
13754 if (child_die->tag == DW_TAG_template_type_param
13755 || child_die->tag == DW_TAG_template_value_param)
13756 {
13757 templ_func = new (&objfile->objfile_obstack) template_symbol;
13758 templ_func->subclass = SYMBOL_TEMPLATE;
13759 break;
13760 }
13761 }
13762
13763 gdb_assert (cu->get_builder () != nullptr);
13764 newobj = cu->get_builder ()->push_context (0, lowpc);
13765 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13766 (struct symbol *) templ_func);
13767
13768 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13769 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13770 cu->language);
13771
13772 /* If there is a location expression for DW_AT_frame_base, record
13773 it. */
13774 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13775 if (attr != nullptr)
13776 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13777
13778 /* If there is a location for the static link, record it. */
13779 newobj->static_link = NULL;
13780 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13781 if (attr != nullptr)
13782 {
13783 newobj->static_link
13784 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13785 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13786 cu->addr_type ());
13787 }
13788
13789 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13790
13791 if (die->child != NULL)
13792 {
13793 child_die = die->child;
13794 while (child_die && child_die->tag)
13795 {
13796 if (child_die->tag == DW_TAG_template_type_param
13797 || child_die->tag == DW_TAG_template_value_param)
13798 {
13799 struct symbol *arg = new_symbol (child_die, NULL, cu);
13800
13801 if (arg != NULL)
13802 template_args.push_back (arg);
13803 }
13804 else
13805 process_die (child_die, cu);
13806 child_die = child_die->sibling;
13807 }
13808 }
13809
13810 inherit_abstract_dies (die, cu);
13811
13812 /* If we have a DW_AT_specification, we might need to import using
13813 directives from the context of the specification DIE. See the
13814 comment in determine_prefix. */
13815 if (cu->language == language_cplus
13816 && dwarf2_attr (die, DW_AT_specification, cu))
13817 {
13818 struct dwarf2_cu *spec_cu = cu;
13819 struct die_info *spec_die = die_specification (die, &spec_cu);
13820
13821 while (spec_die)
13822 {
13823 child_die = spec_die->child;
13824 while (child_die && child_die->tag)
13825 {
13826 if (child_die->tag == DW_TAG_imported_module)
13827 process_die (child_die, spec_cu);
13828 child_die = child_die->sibling;
13829 }
13830
13831 /* In some cases, GCC generates specification DIEs that
13832 themselves contain DW_AT_specification attributes. */
13833 spec_die = die_specification (spec_die, &spec_cu);
13834 }
13835 }
13836
13837 struct context_stack cstk = cu->get_builder ()->pop_context ();
13838 /* Make a block for the local symbols within. */
13839 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13840 cstk.static_link, lowpc, highpc);
13841
13842 /* For C++, set the block's scope. */
13843 if ((cu->language == language_cplus
13844 || cu->language == language_fortran
13845 || cu->language == language_d
13846 || cu->language == language_rust)
13847 && cu->processing_has_namespace_info)
13848 block_set_scope (block, determine_prefix (die, cu),
13849 &objfile->objfile_obstack);
13850
13851 /* If we have address ranges, record them. */
13852 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13853
13854 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13855
13856 /* Attach template arguments to function. */
13857 if (!template_args.empty ())
13858 {
13859 gdb_assert (templ_func != NULL);
13860
13861 templ_func->n_template_arguments = template_args.size ();
13862 templ_func->template_arguments
13863 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13864 templ_func->n_template_arguments);
13865 memcpy (templ_func->template_arguments,
13866 template_args.data (),
13867 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13868
13869 /* Make sure that the symtab is set on the new symbols. Even
13870 though they don't appear in this symtab directly, other parts
13871 of gdb assume that symbols do, and this is reasonably
13872 true. */
13873 for (symbol *sym : template_args)
13874 symbol_set_symtab (sym, symbol_symtab (templ_func));
13875 }
13876
13877 /* In C++, we can have functions nested inside functions (e.g., when
13878 a function declares a class that has methods). This means that
13879 when we finish processing a function scope, we may need to go
13880 back to building a containing block's symbol lists. */
13881 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13882 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13883
13884 /* If we've finished processing a top-level function, subsequent
13885 symbols go in the file symbol list. */
13886 if (cu->get_builder ()->outermost_context_p ())
13887 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13888 }
13889
13890 /* Process all the DIES contained within a lexical block scope. Start
13891 a new scope, process the dies, and then close the scope. */
13892
13893 static void
13894 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13895 {
13896 struct objfile *objfile = cu->per_objfile->objfile;
13897 struct gdbarch *gdbarch = objfile->arch ();
13898 CORE_ADDR lowpc, highpc;
13899 struct die_info *child_die;
13900 CORE_ADDR baseaddr;
13901
13902 baseaddr = objfile->text_section_offset ();
13903
13904 /* Ignore blocks with missing or invalid low and high pc attributes. */
13905 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13906 as multiple lexical blocks? Handling children in a sane way would
13907 be nasty. Might be easier to properly extend generic blocks to
13908 describe ranges. */
13909 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13910 {
13911 case PC_BOUNDS_NOT_PRESENT:
13912 /* DW_TAG_lexical_block has no attributes, process its children as if
13913 there was no wrapping by that DW_TAG_lexical_block.
13914 GCC does no longer produces such DWARF since GCC r224161. */
13915 for (child_die = die->child;
13916 child_die != NULL && child_die->tag;
13917 child_die = child_die->sibling)
13918 {
13919 /* We might already be processing this DIE. This can happen
13920 in an unusual circumstance -- where a subroutine A
13921 appears lexically in another subroutine B, but A actually
13922 inlines B. The recursion is broken here, rather than in
13923 inherit_abstract_dies, because it seems better to simply
13924 drop concrete children here. */
13925 if (!child_die->in_process)
13926 process_die (child_die, cu);
13927 }
13928 return;
13929 case PC_BOUNDS_INVALID:
13930 return;
13931 }
13932 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13933 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13934
13935 cu->get_builder ()->push_context (0, lowpc);
13936 if (die->child != NULL)
13937 {
13938 child_die = die->child;
13939 while (child_die && child_die->tag)
13940 {
13941 process_die (child_die, cu);
13942 child_die = child_die->sibling;
13943 }
13944 }
13945 inherit_abstract_dies (die, cu);
13946 struct context_stack cstk = cu->get_builder ()->pop_context ();
13947
13948 if (*cu->get_builder ()->get_local_symbols () != NULL
13949 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13950 {
13951 struct block *block
13952 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13953 cstk.start_addr, highpc);
13954
13955 /* Note that recording ranges after traversing children, as we
13956 do here, means that recording a parent's ranges entails
13957 walking across all its children's ranges as they appear in
13958 the address map, which is quadratic behavior.
13959
13960 It would be nicer to record the parent's ranges before
13961 traversing its children, simply overriding whatever you find
13962 there. But since we don't even decide whether to create a
13963 block until after we've traversed its children, that's hard
13964 to do. */
13965 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13966 }
13967 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13968 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13969 }
13970
13971 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13972
13973 static void
13974 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13975 {
13976 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13977 struct objfile *objfile = per_objfile->objfile;
13978 struct gdbarch *gdbarch = objfile->arch ();
13979 CORE_ADDR pc, baseaddr;
13980 struct attribute *attr;
13981 struct call_site *call_site, call_site_local;
13982 void **slot;
13983 int nparams;
13984 struct die_info *child_die;
13985
13986 baseaddr = objfile->text_section_offset ();
13987
13988 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13989 if (attr == NULL)
13990 {
13991 /* This was a pre-DWARF-5 GNU extension alias
13992 for DW_AT_call_return_pc. */
13993 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13994 }
13995 if (!attr)
13996 {
13997 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13998 "DIE %s [in module %s]"),
13999 sect_offset_str (die->sect_off), objfile_name (objfile));
14000 return;
14001 }
14002 pc = attr->as_address () + baseaddr;
14003 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
14004
14005 if (cu->call_site_htab == NULL)
14006 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
14007 NULL, &objfile->objfile_obstack,
14008 hashtab_obstack_allocate, NULL);
14009 call_site_local.pc = pc;
14010 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
14011 if (*slot != NULL)
14012 {
14013 complaint (_("Duplicate PC %s for DW_TAG_call_site "
14014 "DIE %s [in module %s]"),
14015 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
14016 objfile_name (objfile));
14017 return;
14018 }
14019
14020 /* Count parameters at the caller. */
14021
14022 nparams = 0;
14023 for (child_die = die->child; child_die && child_die->tag;
14024 child_die = child_die->sibling)
14025 {
14026 if (child_die->tag != DW_TAG_call_site_parameter
14027 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14028 {
14029 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
14030 "DW_TAG_call_site child DIE %s [in module %s]"),
14031 child_die->tag, sect_offset_str (child_die->sect_off),
14032 objfile_name (objfile));
14033 continue;
14034 }
14035
14036 nparams++;
14037 }
14038
14039 call_site
14040 = ((struct call_site *)
14041 obstack_alloc (&objfile->objfile_obstack,
14042 sizeof (*call_site)
14043 + (sizeof (*call_site->parameter) * (nparams - 1))));
14044 *slot = call_site;
14045 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14046 call_site->pc = pc;
14047
14048 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14049 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14050 {
14051 struct die_info *func_die;
14052
14053 /* Skip also over DW_TAG_inlined_subroutine. */
14054 for (func_die = die->parent;
14055 func_die && func_die->tag != DW_TAG_subprogram
14056 && func_die->tag != DW_TAG_subroutine_type;
14057 func_die = func_die->parent);
14058
14059 /* DW_AT_call_all_calls is a superset
14060 of DW_AT_call_all_tail_calls. */
14061 if (func_die
14062 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14063 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14064 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14065 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14066 {
14067 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14068 not complete. But keep CALL_SITE for look ups via call_site_htab,
14069 both the initial caller containing the real return address PC and
14070 the final callee containing the current PC of a chain of tail
14071 calls do not need to have the tail call list complete. But any
14072 function candidate for a virtual tail call frame searched via
14073 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14074 determined unambiguously. */
14075 }
14076 else
14077 {
14078 struct type *func_type = NULL;
14079
14080 if (func_die)
14081 func_type = get_die_type (func_die, cu);
14082 if (func_type != NULL)
14083 {
14084 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
14085
14086 /* Enlist this call site to the function. */
14087 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14088 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14089 }
14090 else
14091 complaint (_("Cannot find function owning DW_TAG_call_site "
14092 "DIE %s [in module %s]"),
14093 sect_offset_str (die->sect_off), objfile_name (objfile));
14094 }
14095 }
14096
14097 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14098 if (attr == NULL)
14099 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14100 if (attr == NULL)
14101 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14102 if (attr == NULL)
14103 {
14104 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14105 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14106 }
14107 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14108 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
14109 /* Keep NULL DWARF_BLOCK. */;
14110 else if (attr->form_is_block ())
14111 {
14112 struct dwarf2_locexpr_baton *dlbaton;
14113 struct dwarf_block *block = attr->as_block ();
14114
14115 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14116 dlbaton->data = block->data;
14117 dlbaton->size = block->size;
14118 dlbaton->per_objfile = per_objfile;
14119 dlbaton->per_cu = cu->per_cu;
14120
14121 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14122 }
14123 else if (attr->form_is_ref ())
14124 {
14125 struct dwarf2_cu *target_cu = cu;
14126 struct die_info *target_die;
14127
14128 target_die = follow_die_ref (die, attr, &target_cu);
14129 gdb_assert (target_cu->per_objfile->objfile == objfile);
14130 if (die_is_declaration (target_die, target_cu))
14131 {
14132 const char *target_physname;
14133
14134 /* Prefer the mangled name; otherwise compute the demangled one. */
14135 target_physname = dw2_linkage_name (target_die, target_cu);
14136 if (target_physname == NULL)
14137 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14138 if (target_physname == NULL)
14139 complaint (_("DW_AT_call_target target DIE has invalid "
14140 "physname, for referencing DIE %s [in module %s]"),
14141 sect_offset_str (die->sect_off), objfile_name (objfile));
14142 else
14143 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14144 }
14145 else
14146 {
14147 CORE_ADDR lowpc;
14148
14149 /* DW_AT_entry_pc should be preferred. */
14150 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14151 <= PC_BOUNDS_INVALID)
14152 complaint (_("DW_AT_call_target target DIE has invalid "
14153 "low pc, for referencing DIE %s [in module %s]"),
14154 sect_offset_str (die->sect_off), objfile_name (objfile));
14155 else
14156 {
14157 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14158 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14159 }
14160 }
14161 }
14162 else
14163 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14164 "block nor reference, for DIE %s [in module %s]"),
14165 sect_offset_str (die->sect_off), objfile_name (objfile));
14166
14167 call_site->per_cu = cu->per_cu;
14168 call_site->per_objfile = per_objfile;
14169
14170 for (child_die = die->child;
14171 child_die && child_die->tag;
14172 child_die = child_die->sibling)
14173 {
14174 struct call_site_parameter *parameter;
14175 struct attribute *loc, *origin;
14176
14177 if (child_die->tag != DW_TAG_call_site_parameter
14178 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14179 {
14180 /* Already printed the complaint above. */
14181 continue;
14182 }
14183
14184 gdb_assert (call_site->parameter_count < nparams);
14185 parameter = &call_site->parameter[call_site->parameter_count];
14186
14187 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14188 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14189 register is contained in DW_AT_call_value. */
14190
14191 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14192 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14193 if (origin == NULL)
14194 {
14195 /* This was a pre-DWARF-5 GNU extension alias
14196 for DW_AT_call_parameter. */
14197 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14198 }
14199 if (loc == NULL && origin != NULL && origin->form_is_ref ())
14200 {
14201 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14202
14203 sect_offset sect_off = origin->get_ref_die_offset ();
14204 if (!cu->header.offset_in_cu_p (sect_off))
14205 {
14206 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14207 binding can be done only inside one CU. Such referenced DIE
14208 therefore cannot be even moved to DW_TAG_partial_unit. */
14209 complaint (_("DW_AT_call_parameter offset is not in CU for "
14210 "DW_TAG_call_site child DIE %s [in module %s]"),
14211 sect_offset_str (child_die->sect_off),
14212 objfile_name (objfile));
14213 continue;
14214 }
14215 parameter->u.param_cu_off
14216 = (cu_offset) (sect_off - cu->header.sect_off);
14217 }
14218 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
14219 {
14220 complaint (_("No DW_FORM_block* DW_AT_location for "
14221 "DW_TAG_call_site child DIE %s [in module %s]"),
14222 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14223 continue;
14224 }
14225 else
14226 {
14227 struct dwarf_block *block = loc->as_block ();
14228
14229 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14230 (block->data, &block->data[block->size]);
14231 if (parameter->u.dwarf_reg != -1)
14232 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14233 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
14234 &block->data[block->size],
14235 &parameter->u.fb_offset))
14236 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14237 else
14238 {
14239 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14240 "for DW_FORM_block* DW_AT_location is supported for "
14241 "DW_TAG_call_site child DIE %s "
14242 "[in module %s]"),
14243 sect_offset_str (child_die->sect_off),
14244 objfile_name (objfile));
14245 continue;
14246 }
14247 }
14248
14249 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14250 if (attr == NULL)
14251 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14252 if (attr == NULL || !attr->form_is_block ())
14253 {
14254 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14255 "DW_TAG_call_site child DIE %s [in module %s]"),
14256 sect_offset_str (child_die->sect_off),
14257 objfile_name (objfile));
14258 continue;
14259 }
14260
14261 struct dwarf_block *block = attr->as_block ();
14262 parameter->value = block->data;
14263 parameter->value_size = block->size;
14264
14265 /* Parameters are not pre-cleared by memset above. */
14266 parameter->data_value = NULL;
14267 parameter->data_value_size = 0;
14268 call_site->parameter_count++;
14269
14270 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14271 if (attr == NULL)
14272 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14273 if (attr != nullptr)
14274 {
14275 if (!attr->form_is_block ())
14276 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14277 "DW_TAG_call_site child DIE %s [in module %s]"),
14278 sect_offset_str (child_die->sect_off),
14279 objfile_name (objfile));
14280 else
14281 {
14282 block = attr->as_block ();
14283 parameter->data_value = block->data;
14284 parameter->data_value_size = block->size;
14285 }
14286 }
14287 }
14288 }
14289
14290 /* Helper function for read_variable. If DIE represents a virtual
14291 table, then return the type of the concrete object that is
14292 associated with the virtual table. Otherwise, return NULL. */
14293
14294 static struct type *
14295 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14296 {
14297 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14298 if (attr == NULL)
14299 return NULL;
14300
14301 /* Find the type DIE. */
14302 struct die_info *type_die = NULL;
14303 struct dwarf2_cu *type_cu = cu;
14304
14305 if (attr->form_is_ref ())
14306 type_die = follow_die_ref (die, attr, &type_cu);
14307 if (type_die == NULL)
14308 return NULL;
14309
14310 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14311 return NULL;
14312 return die_containing_type (type_die, type_cu);
14313 }
14314
14315 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14316
14317 static void
14318 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14319 {
14320 struct rust_vtable_symbol *storage = NULL;
14321
14322 if (cu->language == language_rust)
14323 {
14324 struct type *containing_type = rust_containing_type (die, cu);
14325
14326 if (containing_type != NULL)
14327 {
14328 struct objfile *objfile = cu->per_objfile->objfile;
14329
14330 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
14331 storage->concrete_type = containing_type;
14332 storage->subclass = SYMBOL_RUST_VTABLE;
14333 }
14334 }
14335
14336 struct symbol *res = new_symbol (die, NULL, cu, storage);
14337 struct attribute *abstract_origin
14338 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14339 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14340 if (res == NULL && loc && abstract_origin)
14341 {
14342 /* We have a variable without a name, but with a location and an abstract
14343 origin. This may be a concrete instance of an abstract variable
14344 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14345 later. */
14346 struct dwarf2_cu *origin_cu = cu;
14347 struct die_info *origin_die
14348 = follow_die_ref (die, abstract_origin, &origin_cu);
14349 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14350 per_objfile->per_bfd->abstract_to_concrete
14351 [origin_die->sect_off].push_back (die->sect_off);
14352 }
14353 }
14354
14355 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14356 reading .debug_rnglists.
14357 Callback's type should be:
14358 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14359 Return true if the attributes are present and valid, otherwise,
14360 return false. */
14361
14362 template <typename Callback>
14363 static bool
14364 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14365 dwarf_tag tag, Callback &&callback)
14366 {
14367 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14368 struct objfile *objfile = per_objfile->objfile;
14369 bfd *obfd = objfile->obfd;
14370 /* Base address selection entry. */
14371 gdb::optional<CORE_ADDR> base;
14372 const gdb_byte *buffer;
14373 CORE_ADDR baseaddr;
14374 bool overflow = false;
14375 ULONGEST addr_index;
14376 struct dwarf2_section_info *rnglists_section;
14377
14378 base = cu->base_address;
14379 rnglists_section = cu_debug_rnglists_section (cu, tag);
14380 rnglists_section->read (objfile);
14381
14382 if (offset >= rnglists_section->size)
14383 {
14384 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14385 offset);
14386 return false;
14387 }
14388 buffer = rnglists_section->buffer + offset;
14389
14390 baseaddr = objfile->text_section_offset ();
14391
14392 while (1)
14393 {
14394 /* Initialize it due to a false compiler warning. */
14395 CORE_ADDR range_beginning = 0, range_end = 0;
14396 const gdb_byte *buf_end = (rnglists_section->buffer
14397 + rnglists_section->size);
14398 unsigned int bytes_read;
14399
14400 if (buffer == buf_end)
14401 {
14402 overflow = true;
14403 break;
14404 }
14405 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14406 switch (rlet)
14407 {
14408 case DW_RLE_end_of_list:
14409 break;
14410 case DW_RLE_base_address:
14411 if (buffer + cu->header.addr_size > buf_end)
14412 {
14413 overflow = true;
14414 break;
14415 }
14416 base = cu->header.read_address (obfd, buffer, &bytes_read);
14417 buffer += bytes_read;
14418 break;
14419 case DW_RLE_base_addressx:
14420 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14421 buffer += bytes_read;
14422 base = read_addr_index (cu, addr_index);
14423 break;
14424 case DW_RLE_start_length:
14425 if (buffer + cu->header.addr_size > buf_end)
14426 {
14427 overflow = true;
14428 break;
14429 }
14430 range_beginning = cu->header.read_address (obfd, buffer,
14431 &bytes_read);
14432 buffer += bytes_read;
14433 range_end = (range_beginning
14434 + 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_startx_length:
14443 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14444 buffer += bytes_read;
14445 range_beginning = read_addr_index (cu, addr_index);
14446 if (buffer > buf_end)
14447 {
14448 overflow = true;
14449 break;
14450 }
14451 range_end = (range_beginning
14452 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14453 buffer += bytes_read;
14454 break;
14455 case DW_RLE_offset_pair:
14456 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14457 buffer += bytes_read;
14458 if (buffer > buf_end)
14459 {
14460 overflow = true;
14461 break;
14462 }
14463 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14464 buffer += bytes_read;
14465 if (buffer > buf_end)
14466 {
14467 overflow = true;
14468 break;
14469 }
14470 break;
14471 case DW_RLE_start_end:
14472 if (buffer + 2 * cu->header.addr_size > buf_end)
14473 {
14474 overflow = true;
14475 break;
14476 }
14477 range_beginning = cu->header.read_address (obfd, buffer,
14478 &bytes_read);
14479 buffer += bytes_read;
14480 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
14481 buffer += bytes_read;
14482 break;
14483 case DW_RLE_startx_endx:
14484 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14485 buffer += bytes_read;
14486 range_beginning = read_addr_index (cu, addr_index);
14487 if (buffer > buf_end)
14488 {
14489 overflow = true;
14490 break;
14491 }
14492 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14493 buffer += bytes_read;
14494 range_end = read_addr_index (cu, addr_index);
14495 break;
14496 default:
14497 complaint (_("Invalid .debug_rnglists data (no base address)"));
14498 return false;
14499 }
14500 if (rlet == DW_RLE_end_of_list || overflow)
14501 break;
14502 if (rlet == DW_RLE_base_address)
14503 continue;
14504
14505 if (range_beginning > range_end)
14506 {
14507 /* Inverted range entries are invalid. */
14508 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14509 return false;
14510 }
14511
14512 /* Empty range entries have no effect. */
14513 if (range_beginning == range_end)
14514 continue;
14515
14516 /* Only DW_RLE_offset_pair needs the base address added. */
14517 if (rlet == DW_RLE_offset_pair)
14518 {
14519 if (!base.has_value ())
14520 {
14521 /* We have no valid base address for the DW_RLE_offset_pair. */
14522 complaint (_("Invalid .debug_rnglists data (no base address for "
14523 "DW_RLE_offset_pair)"));
14524 return false;
14525 }
14526
14527 range_beginning += *base;
14528 range_end += *base;
14529 }
14530
14531 /* A not-uncommon case of bad debug info.
14532 Don't pollute the addrmap with bad data. */
14533 if (range_beginning + baseaddr == 0
14534 && !per_objfile->per_bfd->has_section_at_zero)
14535 {
14536 complaint (_(".debug_rnglists entry has start address of zero"
14537 " [in module %s]"), objfile_name (objfile));
14538 continue;
14539 }
14540
14541 callback (range_beginning, range_end);
14542 }
14543
14544 if (overflow)
14545 {
14546 complaint (_("Offset %d is not terminated "
14547 "for DW_AT_ranges attribute"),
14548 offset);
14549 return false;
14550 }
14551
14552 return true;
14553 }
14554
14555 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14556 Callback's type should be:
14557 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14558 Return 1 if the attributes are present and valid, otherwise, return 0. */
14559
14560 template <typename Callback>
14561 static int
14562 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
14563 Callback &&callback)
14564 {
14565 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14566 struct objfile *objfile = per_objfile->objfile;
14567 struct comp_unit_head *cu_header = &cu->header;
14568 bfd *obfd = objfile->obfd;
14569 unsigned int addr_size = cu_header->addr_size;
14570 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14571 /* Base address selection entry. */
14572 gdb::optional<CORE_ADDR> base;
14573 unsigned int dummy;
14574 const gdb_byte *buffer;
14575 CORE_ADDR baseaddr;
14576
14577 if (cu_header->version >= 5)
14578 return dwarf2_rnglists_process (offset, cu, tag, callback);
14579
14580 base = cu->base_address;
14581
14582 per_objfile->per_bfd->ranges.read (objfile);
14583 if (offset >= per_objfile->per_bfd->ranges.size)
14584 {
14585 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14586 offset);
14587 return 0;
14588 }
14589 buffer = per_objfile->per_bfd->ranges.buffer + offset;
14590
14591 baseaddr = objfile->text_section_offset ();
14592
14593 while (1)
14594 {
14595 CORE_ADDR range_beginning, range_end;
14596
14597 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
14598 buffer += addr_size;
14599 range_end = cu->header.read_address (obfd, buffer, &dummy);
14600 buffer += addr_size;
14601 offset += 2 * addr_size;
14602
14603 /* An end of list marker is a pair of zero addresses. */
14604 if (range_beginning == 0 && range_end == 0)
14605 /* Found the end of list entry. */
14606 break;
14607
14608 /* Each base address selection entry is a pair of 2 values.
14609 The first is the largest possible address, the second is
14610 the base address. Check for a base address here. */
14611 if ((range_beginning & mask) == mask)
14612 {
14613 /* If we found the largest possible address, then we already
14614 have the base address in range_end. */
14615 base = range_end;
14616 continue;
14617 }
14618
14619 if (!base.has_value ())
14620 {
14621 /* We have no valid base address for the ranges
14622 data. */
14623 complaint (_("Invalid .debug_ranges data (no base address)"));
14624 return 0;
14625 }
14626
14627 if (range_beginning > range_end)
14628 {
14629 /* Inverted range entries are invalid. */
14630 complaint (_("Invalid .debug_ranges data (inverted range)"));
14631 return 0;
14632 }
14633
14634 /* Empty range entries have no effect. */
14635 if (range_beginning == range_end)
14636 continue;
14637
14638 range_beginning += *base;
14639 range_end += *base;
14640
14641 /* A not-uncommon case of bad debug info.
14642 Don't pollute the addrmap with bad data. */
14643 if (range_beginning + baseaddr == 0
14644 && !per_objfile->per_bfd->has_section_at_zero)
14645 {
14646 complaint (_(".debug_ranges entry has start address of zero"
14647 " [in module %s]"), objfile_name (objfile));
14648 continue;
14649 }
14650
14651 callback (range_beginning, range_end);
14652 }
14653
14654 return 1;
14655 }
14656
14657 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14658 Return 1 if the attributes are present and valid, otherwise, return 0.
14659 If RANGES_PST is not NULL we should set up the `psymtabs_addrmap'. */
14660
14661 static int
14662 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14663 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14664 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
14665 {
14666 struct objfile *objfile = cu->per_objfile->objfile;
14667 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
14668 struct gdbarch *gdbarch = objfile->arch ();
14669 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14670 int low_set = 0;
14671 CORE_ADDR low = 0;
14672 CORE_ADDR high = 0;
14673 int retval;
14674
14675 retval = dwarf2_ranges_process (offset, cu, tag,
14676 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14677 {
14678 if (ranges_pst != NULL)
14679 {
14680 CORE_ADDR lowpc;
14681 CORE_ADDR highpc;
14682
14683 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14684 range_beginning + baseaddr)
14685 - baseaddr);
14686 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14687 range_end + baseaddr)
14688 - baseaddr);
14689 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
14690 lowpc, highpc - 1, ranges_pst);
14691 }
14692
14693 /* FIXME: This is recording everything as a low-high
14694 segment of consecutive addresses. We should have a
14695 data structure for discontiguous block ranges
14696 instead. */
14697 if (! low_set)
14698 {
14699 low = range_beginning;
14700 high = range_end;
14701 low_set = 1;
14702 }
14703 else
14704 {
14705 if (range_beginning < low)
14706 low = range_beginning;
14707 if (range_end > high)
14708 high = range_end;
14709 }
14710 });
14711 if (!retval)
14712 return 0;
14713
14714 if (! low_set)
14715 /* If the first entry is an end-of-list marker, the range
14716 describes an empty scope, i.e. no instructions. */
14717 return 0;
14718
14719 if (low_return)
14720 *low_return = low;
14721 if (high_return)
14722 *high_return = high;
14723 return 1;
14724 }
14725
14726 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14727 definition for the return value. *LOWPC and *HIGHPC are set iff
14728 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14729
14730 static enum pc_bounds_kind
14731 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14732 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14733 dwarf2_psymtab *pst)
14734 {
14735 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14736 struct attribute *attr;
14737 struct attribute *attr_high;
14738 CORE_ADDR low = 0;
14739 CORE_ADDR high = 0;
14740 enum pc_bounds_kind ret;
14741
14742 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14743 if (attr_high)
14744 {
14745 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14746 if (attr != nullptr)
14747 {
14748 low = attr->as_address ();
14749 high = attr_high->as_address ();
14750 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14751 high += low;
14752 }
14753 else
14754 /* Found high w/o low attribute. */
14755 return PC_BOUNDS_INVALID;
14756
14757 /* Found consecutive range of addresses. */
14758 ret = PC_BOUNDS_HIGH_LOW;
14759 }
14760 else
14761 {
14762 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14763 if (attr != nullptr && attr->form_is_unsigned ())
14764 {
14765 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14766 on DWARF version). */
14767 ULONGEST ranges_offset = attr->as_unsigned ();
14768
14769 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14770 this value. */
14771 if (die->tag != DW_TAG_compile_unit)
14772 ranges_offset += cu->gnu_ranges_base;
14773
14774 /* Value of the DW_AT_ranges attribute is the offset in the
14775 .debug_ranges section. */
14776 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14777 die->tag))
14778 return PC_BOUNDS_INVALID;
14779 /* Found discontinuous range of addresses. */
14780 ret = PC_BOUNDS_RANGES;
14781 }
14782 else
14783 return PC_BOUNDS_NOT_PRESENT;
14784 }
14785
14786 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14787 if (high <= low)
14788 return PC_BOUNDS_INVALID;
14789
14790 /* When using the GNU linker, .gnu.linkonce. sections are used to
14791 eliminate duplicate copies of functions and vtables and such.
14792 The linker will arbitrarily choose one and discard the others.
14793 The AT_*_pc values for such functions refer to local labels in
14794 these sections. If the section from that file was discarded, the
14795 labels are not in the output, so the relocs get a value of 0.
14796 If this is a discarded function, mark the pc bounds as invalid,
14797 so that GDB will ignore it. */
14798 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14799 return PC_BOUNDS_INVALID;
14800
14801 *lowpc = low;
14802 if (highpc)
14803 *highpc = high;
14804 return ret;
14805 }
14806
14807 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14808 its low and high PC addresses. Do nothing if these addresses could not
14809 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14810 and HIGHPC to the high address if greater than HIGHPC. */
14811
14812 static void
14813 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14814 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14815 struct dwarf2_cu *cu)
14816 {
14817 CORE_ADDR low, high;
14818 struct die_info *child = die->child;
14819
14820 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14821 {
14822 *lowpc = std::min (*lowpc, low);
14823 *highpc = std::max (*highpc, high);
14824 }
14825
14826 /* If the language does not allow nested subprograms (either inside
14827 subprograms or lexical blocks), we're done. */
14828 if (cu->language != language_ada)
14829 return;
14830
14831 /* Check all the children of the given DIE. If it contains nested
14832 subprograms, then check their pc bounds. Likewise, we need to
14833 check lexical blocks as well, as they may also contain subprogram
14834 definitions. */
14835 while (child && child->tag)
14836 {
14837 if (child->tag == DW_TAG_subprogram
14838 || child->tag == DW_TAG_lexical_block)
14839 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14840 child = child->sibling;
14841 }
14842 }
14843
14844 /* Get the low and high pc's represented by the scope DIE, and store
14845 them in *LOWPC and *HIGHPC. If the correct values can't be
14846 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14847
14848 static void
14849 get_scope_pc_bounds (struct die_info *die,
14850 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14851 struct dwarf2_cu *cu)
14852 {
14853 CORE_ADDR best_low = (CORE_ADDR) -1;
14854 CORE_ADDR best_high = (CORE_ADDR) 0;
14855 CORE_ADDR current_low, current_high;
14856
14857 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14858 >= PC_BOUNDS_RANGES)
14859 {
14860 best_low = current_low;
14861 best_high = current_high;
14862 }
14863 else
14864 {
14865 struct die_info *child = die->child;
14866
14867 while (child && child->tag)
14868 {
14869 switch (child->tag) {
14870 case DW_TAG_subprogram:
14871 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14872 break;
14873 case DW_TAG_namespace:
14874 case DW_TAG_module:
14875 /* FIXME: carlton/2004-01-16: Should we do this for
14876 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14877 that current GCC's always emit the DIEs corresponding
14878 to definitions of methods of classes as children of a
14879 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14880 the DIEs giving the declarations, which could be
14881 anywhere). But I don't see any reason why the
14882 standards says that they have to be there. */
14883 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14884
14885 if (current_low != ((CORE_ADDR) -1))
14886 {
14887 best_low = std::min (best_low, current_low);
14888 best_high = std::max (best_high, current_high);
14889 }
14890 break;
14891 default:
14892 /* Ignore. */
14893 break;
14894 }
14895
14896 child = child->sibling;
14897 }
14898 }
14899
14900 *lowpc = best_low;
14901 *highpc = best_high;
14902 }
14903
14904 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14905 in DIE. */
14906
14907 static void
14908 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14909 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14910 {
14911 struct objfile *objfile = cu->per_objfile->objfile;
14912 struct gdbarch *gdbarch = objfile->arch ();
14913 struct attribute *attr;
14914 struct attribute *attr_high;
14915
14916 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14917 if (attr_high)
14918 {
14919 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14920 if (attr != nullptr)
14921 {
14922 CORE_ADDR low = attr->as_address ();
14923 CORE_ADDR high = attr_high->as_address ();
14924
14925 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14926 high += low;
14927
14928 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14929 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14930 cu->get_builder ()->record_block_range (block, low, high - 1);
14931 }
14932 }
14933
14934 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14935 if (attr != nullptr && attr->form_is_unsigned ())
14936 {
14937 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14938 on DWARF version). */
14939 ULONGEST ranges_offset = attr->as_unsigned ();
14940
14941 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14942 this value. */
14943 if (die->tag != DW_TAG_compile_unit)
14944 ranges_offset += cu->gnu_ranges_base;
14945
14946 std::vector<blockrange> blockvec;
14947 dwarf2_ranges_process (ranges_offset, cu, die->tag,
14948 [&] (CORE_ADDR start, CORE_ADDR end)
14949 {
14950 start += baseaddr;
14951 end += baseaddr;
14952 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14953 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14954 cu->get_builder ()->record_block_range (block, start, end - 1);
14955 blockvec.emplace_back (start, end);
14956 });
14957
14958 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14959 }
14960 }
14961
14962 /* Check whether the producer field indicates either of GCC < 4.6, or the
14963 Intel C/C++ compiler, and cache the result in CU. */
14964
14965 static void
14966 check_producer (struct dwarf2_cu *cu)
14967 {
14968 int major, minor;
14969
14970 if (cu->producer == NULL)
14971 {
14972 /* For unknown compilers expect their behavior is DWARF version
14973 compliant.
14974
14975 GCC started to support .debug_types sections by -gdwarf-4 since
14976 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14977 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14978 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14979 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14980 }
14981 else if (producer_is_gcc (cu->producer, &major, &minor))
14982 {
14983 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14984 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14985 }
14986 else if (producer_is_icc (cu->producer, &major, &minor))
14987 {
14988 cu->producer_is_icc = true;
14989 cu->producer_is_icc_lt_14 = major < 14;
14990 }
14991 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14992 cu->producer_is_codewarrior = true;
14993 else
14994 {
14995 /* For other non-GCC compilers, expect their behavior is DWARF version
14996 compliant. */
14997 }
14998
14999 cu->checked_producer = true;
15000 }
15001
15002 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
15003 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
15004 during 4.6.0 experimental. */
15005
15006 static bool
15007 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
15008 {
15009 if (!cu->checked_producer)
15010 check_producer (cu);
15011
15012 return cu->producer_is_gxx_lt_4_6;
15013 }
15014
15015
15016 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
15017 with incorrect is_stmt attributes. */
15018
15019 static bool
15020 producer_is_codewarrior (struct dwarf2_cu *cu)
15021 {
15022 if (!cu->checked_producer)
15023 check_producer (cu);
15024
15025 return cu->producer_is_codewarrior;
15026 }
15027
15028 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
15029 If that attribute is not available, return the appropriate
15030 default. */
15031
15032 static enum dwarf_access_attribute
15033 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
15034 {
15035 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15036 if (attr != nullptr)
15037 {
15038 LONGEST value = attr->constant_value (-1);
15039 if (value == DW_ACCESS_public
15040 || value == DW_ACCESS_protected
15041 || value == DW_ACCESS_private)
15042 return (dwarf_access_attribute) value;
15043 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
15044 plongest (value));
15045 }
15046
15047 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
15048 {
15049 /* The default DWARF 2 accessibility for members is public, the default
15050 accessibility for inheritance is private. */
15051
15052 if (die->tag != DW_TAG_inheritance)
15053 return DW_ACCESS_public;
15054 else
15055 return DW_ACCESS_private;
15056 }
15057 else
15058 {
15059 /* DWARF 3+ defines the default accessibility a different way. The same
15060 rules apply now for DW_TAG_inheritance as for the members and it only
15061 depends on the container kind. */
15062
15063 if (die->parent->tag == DW_TAG_class_type)
15064 return DW_ACCESS_private;
15065 else
15066 return DW_ACCESS_public;
15067 }
15068 }
15069
15070 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15071 offset. If the attribute was not found return 0, otherwise return
15072 1. If it was found but could not properly be handled, set *OFFSET
15073 to 0. */
15074
15075 static int
15076 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15077 LONGEST *offset)
15078 {
15079 struct attribute *attr;
15080
15081 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15082 if (attr != NULL)
15083 {
15084 *offset = 0;
15085
15086 /* Note that we do not check for a section offset first here.
15087 This is because DW_AT_data_member_location is new in DWARF 4,
15088 so if we see it, we can assume that a constant form is really
15089 a constant and not a section offset. */
15090 if (attr->form_is_constant ())
15091 *offset = attr->constant_value (0);
15092 else if (attr->form_is_section_offset ())
15093 dwarf2_complex_location_expr_complaint ();
15094 else if (attr->form_is_block ())
15095 *offset = decode_locdesc (attr->as_block (), cu);
15096 else
15097 dwarf2_complex_location_expr_complaint ();
15098
15099 return 1;
15100 }
15101
15102 return 0;
15103 }
15104
15105 /* Look for DW_AT_data_member_location and store the results in FIELD. */
15106
15107 static void
15108 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15109 struct field *field)
15110 {
15111 struct attribute *attr;
15112
15113 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15114 if (attr != NULL)
15115 {
15116 if (attr->form_is_constant ())
15117 {
15118 LONGEST offset = attr->constant_value (0);
15119 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
15120 }
15121 else if (attr->form_is_section_offset ())
15122 dwarf2_complex_location_expr_complaint ();
15123 else if (attr->form_is_block ())
15124 {
15125 bool handled;
15126 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
15127 if (handled)
15128 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
15129 else
15130 {
15131 dwarf2_per_objfile *per_objfile = cu->per_objfile;
15132 struct objfile *objfile = per_objfile->objfile;
15133 struct dwarf2_locexpr_baton *dlbaton
15134 = XOBNEW (&objfile->objfile_obstack,
15135 struct dwarf2_locexpr_baton);
15136 dlbaton->data = attr->as_block ()->data;
15137 dlbaton->size = attr->as_block ()->size;
15138 /* When using this baton, we want to compute the address
15139 of the field, not the value. This is why
15140 is_reference is set to false here. */
15141 dlbaton->is_reference = false;
15142 dlbaton->per_objfile = per_objfile;
15143 dlbaton->per_cu = cu->per_cu;
15144
15145 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
15146 }
15147 }
15148 else
15149 dwarf2_complex_location_expr_complaint ();
15150 }
15151 }
15152
15153 /* Add an aggregate field to the field list. */
15154
15155 static void
15156 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15157 struct dwarf2_cu *cu)
15158 {
15159 struct objfile *objfile = cu->per_objfile->objfile;
15160 struct gdbarch *gdbarch = objfile->arch ();
15161 struct nextfield *new_field;
15162 struct attribute *attr;
15163 struct field *fp;
15164 const char *fieldname = "";
15165
15166 if (die->tag == DW_TAG_inheritance)
15167 {
15168 fip->baseclasses.emplace_back ();
15169 new_field = &fip->baseclasses.back ();
15170 }
15171 else
15172 {
15173 fip->fields.emplace_back ();
15174 new_field = &fip->fields.back ();
15175 }
15176
15177 new_field->offset = die->sect_off;
15178
15179 new_field->accessibility = dwarf2_access_attribute (die, cu);
15180 if (new_field->accessibility != DW_ACCESS_public)
15181 fip->non_public_fields = true;
15182
15183 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15184 if (attr != nullptr)
15185 new_field->virtuality = attr->as_virtuality ();
15186 else
15187 new_field->virtuality = DW_VIRTUALITY_none;
15188
15189 fp = &new_field->field;
15190
15191 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15192 {
15193 /* Data member other than a C++ static data member. */
15194
15195 /* Get type of field. */
15196 fp->set_type (die_type (die, cu));
15197
15198 SET_FIELD_BITPOS (*fp, 0);
15199
15200 /* Get bit size of field (zero if none). */
15201 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15202 if (attr != nullptr)
15203 {
15204 FIELD_BITSIZE (*fp) = attr->constant_value (0);
15205 }
15206 else
15207 {
15208 FIELD_BITSIZE (*fp) = 0;
15209 }
15210
15211 /* Get bit offset of field. */
15212 handle_data_member_location (die, cu, fp);
15213 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15214 if (attr != nullptr && attr->form_is_constant ())
15215 {
15216 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
15217 {
15218 /* For big endian bits, the DW_AT_bit_offset gives the
15219 additional bit offset from the MSB of the containing
15220 anonymous object to the MSB of the field. We don't
15221 have to do anything special since we don't need to
15222 know the size of the anonymous object. */
15223 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15224 + attr->constant_value (0)));
15225 }
15226 else
15227 {
15228 /* For little endian bits, compute the bit offset to the
15229 MSB of the anonymous object, subtract off the number of
15230 bits from the MSB of the field to the MSB of the
15231 object, and then subtract off the number of bits of
15232 the field itself. The result is the bit offset of
15233 the LSB of the field. */
15234 int anonymous_size;
15235 int bit_offset = attr->constant_value (0);
15236
15237 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15238 if (attr != nullptr && attr->form_is_constant ())
15239 {
15240 /* The size of the anonymous object containing
15241 the bit field is explicit, so use the
15242 indicated size (in bytes). */
15243 anonymous_size = attr->constant_value (0);
15244 }
15245 else
15246 {
15247 /* The size of the anonymous object containing
15248 the bit field must be inferred from the type
15249 attribute of the data member containing the
15250 bit field. */
15251 anonymous_size = TYPE_LENGTH (fp->type ());
15252 }
15253 SET_FIELD_BITPOS (*fp,
15254 (FIELD_BITPOS (*fp)
15255 + anonymous_size * bits_per_byte
15256 - bit_offset - FIELD_BITSIZE (*fp)));
15257 }
15258 }
15259 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15260 if (attr != NULL)
15261 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15262 + attr->constant_value (0)));
15263
15264 /* Get name of field. */
15265 fieldname = dwarf2_name (die, cu);
15266 if (fieldname == NULL)
15267 fieldname = "";
15268
15269 /* The name is already allocated along with this objfile, so we don't
15270 need to duplicate it for the type. */
15271 fp->name = fieldname;
15272
15273 /* Change accessibility for artificial fields (e.g. virtual table
15274 pointer or virtual base class pointer) to private. */
15275 if (dwarf2_attr (die, DW_AT_artificial, cu))
15276 {
15277 FIELD_ARTIFICIAL (*fp) = 1;
15278 new_field->accessibility = DW_ACCESS_private;
15279 fip->non_public_fields = true;
15280 }
15281 }
15282 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15283 {
15284 /* C++ static member. */
15285
15286 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15287 is a declaration, but all versions of G++ as of this writing
15288 (so through at least 3.2.1) incorrectly generate
15289 DW_TAG_variable tags. */
15290
15291 const char *physname;
15292
15293 /* Get name of field. */
15294 fieldname = dwarf2_name (die, cu);
15295 if (fieldname == NULL)
15296 return;
15297
15298 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15299 if (attr
15300 /* Only create a symbol if this is an external value.
15301 new_symbol checks this and puts the value in the global symbol
15302 table, which we want. If it is not external, new_symbol
15303 will try to put the value in cu->list_in_scope which is wrong. */
15304 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15305 {
15306 /* A static const member, not much different than an enum as far as
15307 we're concerned, except that we can support more types. */
15308 new_symbol (die, NULL, cu);
15309 }
15310
15311 /* Get physical name. */
15312 physname = dwarf2_physname (fieldname, die, cu);
15313
15314 /* The name is already allocated along with this objfile, so we don't
15315 need to duplicate it for the type. */
15316 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15317 fp->set_type (die_type (die, cu));
15318 FIELD_NAME (*fp) = fieldname;
15319 }
15320 else if (die->tag == DW_TAG_inheritance)
15321 {
15322 /* C++ base class field. */
15323 handle_data_member_location (die, cu, fp);
15324 FIELD_BITSIZE (*fp) = 0;
15325 fp->set_type (die_type (die, cu));
15326 FIELD_NAME (*fp) = fp->type ()->name ();
15327 }
15328 else
15329 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15330 }
15331
15332 /* Can the type given by DIE define another type? */
15333
15334 static bool
15335 type_can_define_types (const struct die_info *die)
15336 {
15337 switch (die->tag)
15338 {
15339 case DW_TAG_typedef:
15340 case DW_TAG_class_type:
15341 case DW_TAG_structure_type:
15342 case DW_TAG_union_type:
15343 case DW_TAG_enumeration_type:
15344 return true;
15345
15346 default:
15347 return false;
15348 }
15349 }
15350
15351 /* Add a type definition defined in the scope of the FIP's class. */
15352
15353 static void
15354 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15355 struct dwarf2_cu *cu)
15356 {
15357 struct decl_field fp;
15358 memset (&fp, 0, sizeof (fp));
15359
15360 gdb_assert (type_can_define_types (die));
15361
15362 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15363 fp.name = dwarf2_name (die, cu);
15364 fp.type = read_type_die (die, cu);
15365
15366 /* Save accessibility. */
15367 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15368 switch (accessibility)
15369 {
15370 case DW_ACCESS_public:
15371 /* The assumed value if neither private nor protected. */
15372 break;
15373 case DW_ACCESS_private:
15374 fp.is_private = 1;
15375 break;
15376 case DW_ACCESS_protected:
15377 fp.is_protected = 1;
15378 break;
15379 }
15380
15381 if (die->tag == DW_TAG_typedef)
15382 fip->typedef_field_list.push_back (fp);
15383 else
15384 fip->nested_types_list.push_back (fp);
15385 }
15386
15387 /* A convenience typedef that's used when finding the discriminant
15388 field for a variant part. */
15389 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
15390 offset_map_type;
15391
15392 /* Compute the discriminant range for a given variant. OBSTACK is
15393 where the results will be stored. VARIANT is the variant to
15394 process. IS_UNSIGNED indicates whether the discriminant is signed
15395 or unsigned. */
15396
15397 static const gdb::array_view<discriminant_range>
15398 convert_variant_range (struct obstack *obstack, const variant_field &variant,
15399 bool is_unsigned)
15400 {
15401 std::vector<discriminant_range> ranges;
15402
15403 if (variant.default_branch)
15404 return {};
15405
15406 if (variant.discr_list_data == nullptr)
15407 {
15408 discriminant_range r
15409 = {variant.discriminant_value, variant.discriminant_value};
15410 ranges.push_back (r);
15411 }
15412 else
15413 {
15414 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15415 variant.discr_list_data->size);
15416 while (!data.empty ())
15417 {
15418 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15419 {
15420 complaint (_("invalid discriminant marker: %d"), data[0]);
15421 break;
15422 }
15423 bool is_range = data[0] == DW_DSC_range;
15424 data = data.slice (1);
15425
15426 ULONGEST low, high;
15427 unsigned int bytes_read;
15428
15429 if (data.empty ())
15430 {
15431 complaint (_("DW_AT_discr_list missing low value"));
15432 break;
15433 }
15434 if (is_unsigned)
15435 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15436 else
15437 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15438 &bytes_read);
15439 data = data.slice (bytes_read);
15440
15441 if (is_range)
15442 {
15443 if (data.empty ())
15444 {
15445 complaint (_("DW_AT_discr_list missing high value"));
15446 break;
15447 }
15448 if (is_unsigned)
15449 high = read_unsigned_leb128 (nullptr, data.data (),
15450 &bytes_read);
15451 else
15452 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15453 &bytes_read);
15454 data = data.slice (bytes_read);
15455 }
15456 else
15457 high = low;
15458
15459 ranges.push_back ({ low, high });
15460 }
15461 }
15462
15463 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15464 ranges.size ());
15465 std::copy (ranges.begin (), ranges.end (), result);
15466 return gdb::array_view<discriminant_range> (result, ranges.size ());
15467 }
15468
15469 static const gdb::array_view<variant_part> create_variant_parts
15470 (struct obstack *obstack,
15471 const offset_map_type &offset_map,
15472 struct field_info *fi,
15473 const std::vector<variant_part_builder> &variant_parts);
15474
15475 /* Fill in a "struct variant" for a given variant field. RESULT is
15476 the variant to fill in. OBSTACK is where any needed allocations
15477 will be done. OFFSET_MAP holds the mapping from section offsets to
15478 fields for the type. FI describes the fields of the type we're
15479 processing. FIELD is the variant field we're converting. */
15480
15481 static void
15482 create_one_variant (variant &result, struct obstack *obstack,
15483 const offset_map_type &offset_map,
15484 struct field_info *fi, const variant_field &field)
15485 {
15486 result.discriminants = convert_variant_range (obstack, field, false);
15487 result.first_field = field.first_field + fi->baseclasses.size ();
15488 result.last_field = field.last_field + fi->baseclasses.size ();
15489 result.parts = create_variant_parts (obstack, offset_map, fi,
15490 field.variant_parts);
15491 }
15492
15493 /* Fill in a "struct variant_part" for a given variant part. RESULT
15494 is the variant part to fill in. OBSTACK is where any needed
15495 allocations will be done. OFFSET_MAP holds the mapping from
15496 section offsets to fields for the type. FI describes the fields of
15497 the type we're processing. BUILDER is the variant part to be
15498 converted. */
15499
15500 static void
15501 create_one_variant_part (variant_part &result,
15502 struct obstack *obstack,
15503 const offset_map_type &offset_map,
15504 struct field_info *fi,
15505 const variant_part_builder &builder)
15506 {
15507 auto iter = offset_map.find (builder.discriminant_offset);
15508 if (iter == offset_map.end ())
15509 {
15510 result.discriminant_index = -1;
15511 /* Doesn't matter. */
15512 result.is_unsigned = false;
15513 }
15514 else
15515 {
15516 result.discriminant_index = iter->second;
15517 result.is_unsigned
15518 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
15519 }
15520
15521 size_t n = builder.variants.size ();
15522 variant *output = new (obstack) variant[n];
15523 for (size_t i = 0; i < n; ++i)
15524 create_one_variant (output[i], obstack, offset_map, fi,
15525 builder.variants[i]);
15526
15527 result.variants = gdb::array_view<variant> (output, n);
15528 }
15529
15530 /* Create a vector of variant parts that can be attached to a type.
15531 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15532 holds the mapping from section offsets to fields for the type. FI
15533 describes the fields of the type we're processing. VARIANT_PARTS
15534 is the vector to convert. */
15535
15536 static const gdb::array_view<variant_part>
15537 create_variant_parts (struct obstack *obstack,
15538 const offset_map_type &offset_map,
15539 struct field_info *fi,
15540 const std::vector<variant_part_builder> &variant_parts)
15541 {
15542 if (variant_parts.empty ())
15543 return {};
15544
15545 size_t n = variant_parts.size ();
15546 variant_part *result = new (obstack) variant_part[n];
15547 for (size_t i = 0; i < n; ++i)
15548 create_one_variant_part (result[i], obstack, offset_map, fi,
15549 variant_parts[i]);
15550
15551 return gdb::array_view<variant_part> (result, n);
15552 }
15553
15554 /* Compute the variant part vector for FIP, attaching it to TYPE when
15555 done. */
15556
15557 static void
15558 add_variant_property (struct field_info *fip, struct type *type,
15559 struct dwarf2_cu *cu)
15560 {
15561 /* Map section offsets of fields to their field index. Note the
15562 field index here does not take the number of baseclasses into
15563 account. */
15564 offset_map_type offset_map;
15565 for (int i = 0; i < fip->fields.size (); ++i)
15566 offset_map[fip->fields[i].offset] = i;
15567
15568 struct objfile *objfile = cu->per_objfile->objfile;
15569 gdb::array_view<variant_part> parts
15570 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15571 fip->variant_parts);
15572
15573 struct dynamic_prop prop;
15574 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15575 obstack_copy (&objfile->objfile_obstack, &parts,
15576 sizeof (parts)));
15577
15578 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
15579 }
15580
15581 /* Create the vector of fields, and attach it to the type. */
15582
15583 static void
15584 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15585 struct dwarf2_cu *cu)
15586 {
15587 int nfields = fip->nfields ();
15588
15589 /* Record the field count, allocate space for the array of fields,
15590 and create blank accessibility bitfields if necessary. */
15591 type->set_num_fields (nfields);
15592 type->set_fields
15593 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
15594
15595 if (fip->non_public_fields && cu->language != language_ada)
15596 {
15597 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15598
15599 TYPE_FIELD_PRIVATE_BITS (type) =
15600 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15601 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15602
15603 TYPE_FIELD_PROTECTED_BITS (type) =
15604 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15605 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15606
15607 TYPE_FIELD_IGNORE_BITS (type) =
15608 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15609 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15610 }
15611
15612 /* If the type has baseclasses, allocate and clear a bit vector for
15613 TYPE_FIELD_VIRTUAL_BITS. */
15614 if (!fip->baseclasses.empty () && cu->language != language_ada)
15615 {
15616 int num_bytes = B_BYTES (fip->baseclasses.size ());
15617 unsigned char *pointer;
15618
15619 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15620 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15621 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15622 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15623 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15624 }
15625
15626 if (!fip->variant_parts.empty ())
15627 add_variant_property (fip, type, cu);
15628
15629 /* Copy the saved-up fields into the field vector. */
15630 for (int i = 0; i < nfields; ++i)
15631 {
15632 struct nextfield &field
15633 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15634 : fip->fields[i - fip->baseclasses.size ()]);
15635
15636 type->field (i) = field.field;
15637 switch (field.accessibility)
15638 {
15639 case DW_ACCESS_private:
15640 if (cu->language != language_ada)
15641 SET_TYPE_FIELD_PRIVATE (type, i);
15642 break;
15643
15644 case DW_ACCESS_protected:
15645 if (cu->language != language_ada)
15646 SET_TYPE_FIELD_PROTECTED (type, i);
15647 break;
15648
15649 case DW_ACCESS_public:
15650 break;
15651
15652 default:
15653 /* Unknown accessibility. Complain and treat it as public. */
15654 {
15655 complaint (_("unsupported accessibility %d"),
15656 field.accessibility);
15657 }
15658 break;
15659 }
15660 if (i < fip->baseclasses.size ())
15661 {
15662 switch (field.virtuality)
15663 {
15664 case DW_VIRTUALITY_virtual:
15665 case DW_VIRTUALITY_pure_virtual:
15666 if (cu->language == language_ada)
15667 error (_("unexpected virtuality in component of Ada type"));
15668 SET_TYPE_FIELD_VIRTUAL (type, i);
15669 break;
15670 }
15671 }
15672 }
15673 }
15674
15675 /* Return true if this member function is a constructor, false
15676 otherwise. */
15677
15678 static int
15679 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15680 {
15681 const char *fieldname;
15682 const char *type_name;
15683 int len;
15684
15685 if (die->parent == NULL)
15686 return 0;
15687
15688 if (die->parent->tag != DW_TAG_structure_type
15689 && die->parent->tag != DW_TAG_union_type
15690 && die->parent->tag != DW_TAG_class_type)
15691 return 0;
15692
15693 fieldname = dwarf2_name (die, cu);
15694 type_name = dwarf2_name (die->parent, cu);
15695 if (fieldname == NULL || type_name == NULL)
15696 return 0;
15697
15698 len = strlen (fieldname);
15699 return (strncmp (fieldname, type_name, len) == 0
15700 && (type_name[len] == '\0' || type_name[len] == '<'));
15701 }
15702
15703 /* Add a member function to the proper fieldlist. */
15704
15705 static void
15706 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15707 struct type *type, struct dwarf2_cu *cu)
15708 {
15709 struct objfile *objfile = cu->per_objfile->objfile;
15710 struct attribute *attr;
15711 int i;
15712 struct fnfieldlist *flp = nullptr;
15713 struct fn_field *fnp;
15714 const char *fieldname;
15715 struct type *this_type;
15716
15717 if (cu->language == language_ada)
15718 error (_("unexpected member function in Ada type"));
15719
15720 /* Get name of member function. */
15721 fieldname = dwarf2_name (die, cu);
15722 if (fieldname == NULL)
15723 return;
15724
15725 /* Look up member function name in fieldlist. */
15726 for (i = 0; i < fip->fnfieldlists.size (); i++)
15727 {
15728 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15729 {
15730 flp = &fip->fnfieldlists[i];
15731 break;
15732 }
15733 }
15734
15735 /* Create a new fnfieldlist if necessary. */
15736 if (flp == nullptr)
15737 {
15738 fip->fnfieldlists.emplace_back ();
15739 flp = &fip->fnfieldlists.back ();
15740 flp->name = fieldname;
15741 i = fip->fnfieldlists.size () - 1;
15742 }
15743
15744 /* Create a new member function field and add it to the vector of
15745 fnfieldlists. */
15746 flp->fnfields.emplace_back ();
15747 fnp = &flp->fnfields.back ();
15748
15749 /* Delay processing of the physname until later. */
15750 if (cu->language == language_cplus)
15751 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15752 die, cu);
15753 else
15754 {
15755 const char *physname = dwarf2_physname (fieldname, die, cu);
15756 fnp->physname = physname ? physname : "";
15757 }
15758
15759 fnp->type = alloc_type (objfile);
15760 this_type = read_type_die (die, cu);
15761 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15762 {
15763 int nparams = this_type->num_fields ();
15764
15765 /* TYPE is the domain of this method, and THIS_TYPE is the type
15766 of the method itself (TYPE_CODE_METHOD). */
15767 smash_to_method_type (fnp->type, type,
15768 TYPE_TARGET_TYPE (this_type),
15769 this_type->fields (),
15770 this_type->num_fields (),
15771 this_type->has_varargs ());
15772
15773 /* Handle static member functions.
15774 Dwarf2 has no clean way to discern C++ static and non-static
15775 member functions. G++ helps GDB by marking the first
15776 parameter for non-static member functions (which is the this
15777 pointer) as artificial. We obtain this information from
15778 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15779 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15780 fnp->voffset = VOFFSET_STATIC;
15781 }
15782 else
15783 complaint (_("member function type missing for '%s'"),
15784 dwarf2_full_name (fieldname, die, cu));
15785
15786 /* Get fcontext from DW_AT_containing_type if present. */
15787 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15788 fnp->fcontext = die_containing_type (die, cu);
15789
15790 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15791 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15792
15793 /* Get accessibility. */
15794 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15795 switch (accessibility)
15796 {
15797 case DW_ACCESS_private:
15798 fnp->is_private = 1;
15799 break;
15800 case DW_ACCESS_protected:
15801 fnp->is_protected = 1;
15802 break;
15803 }
15804
15805 /* Check for artificial methods. */
15806 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15807 if (attr && attr->as_boolean ())
15808 fnp->is_artificial = 1;
15809
15810 /* Check for defaulted methods. */
15811 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15812 if (attr != nullptr)
15813 fnp->defaulted = attr->defaulted ();
15814
15815 /* Check for deleted methods. */
15816 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15817 if (attr != nullptr && attr->as_boolean ())
15818 fnp->is_deleted = 1;
15819
15820 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15821
15822 /* Get index in virtual function table if it is a virtual member
15823 function. For older versions of GCC, this is an offset in the
15824 appropriate virtual table, as specified by DW_AT_containing_type.
15825 For everyone else, it is an expression to be evaluated relative
15826 to the object address. */
15827
15828 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15829 if (attr != nullptr)
15830 {
15831 if (attr->form_is_block () && attr->as_block ()->size > 0)
15832 {
15833 struct dwarf_block *block = attr->as_block ();
15834
15835 if (block->data[0] == DW_OP_constu)
15836 {
15837 /* Old-style GCC. */
15838 fnp->voffset = decode_locdesc (block, cu) + 2;
15839 }
15840 else if (block->data[0] == DW_OP_deref
15841 || (block->size > 1
15842 && block->data[0] == DW_OP_deref_size
15843 && block->data[1] == cu->header.addr_size))
15844 {
15845 fnp->voffset = decode_locdesc (block, cu);
15846 if ((fnp->voffset % cu->header.addr_size) != 0)
15847 dwarf2_complex_location_expr_complaint ();
15848 else
15849 fnp->voffset /= cu->header.addr_size;
15850 fnp->voffset += 2;
15851 }
15852 else
15853 dwarf2_complex_location_expr_complaint ();
15854
15855 if (!fnp->fcontext)
15856 {
15857 /* If there is no `this' field and no DW_AT_containing_type,
15858 we cannot actually find a base class context for the
15859 vtable! */
15860 if (this_type->num_fields () == 0
15861 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15862 {
15863 complaint (_("cannot determine context for virtual member "
15864 "function \"%s\" (offset %s)"),
15865 fieldname, sect_offset_str (die->sect_off));
15866 }
15867 else
15868 {
15869 fnp->fcontext
15870 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15871 }
15872 }
15873 }
15874 else if (attr->form_is_section_offset ())
15875 {
15876 dwarf2_complex_location_expr_complaint ();
15877 }
15878 else
15879 {
15880 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15881 fieldname);
15882 }
15883 }
15884 else
15885 {
15886 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15887 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
15888 {
15889 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15890 complaint (_("Member function \"%s\" (offset %s) is virtual "
15891 "but the vtable offset is not specified"),
15892 fieldname, sect_offset_str (die->sect_off));
15893 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15894 TYPE_CPLUS_DYNAMIC (type) = 1;
15895 }
15896 }
15897 }
15898
15899 /* Create the vector of member function fields, and attach it to the type. */
15900
15901 static void
15902 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15903 struct dwarf2_cu *cu)
15904 {
15905 if (cu->language == language_ada)
15906 error (_("unexpected member functions in Ada type"));
15907
15908 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15909 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15910 TYPE_ALLOC (type,
15911 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15912
15913 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15914 {
15915 struct fnfieldlist &nf = fip->fnfieldlists[i];
15916 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15917
15918 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15919 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15920 fn_flp->fn_fields = (struct fn_field *)
15921 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15922
15923 for (int k = 0; k < nf.fnfields.size (); ++k)
15924 fn_flp->fn_fields[k] = nf.fnfields[k];
15925 }
15926
15927 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15928 }
15929
15930 /* Returns non-zero if NAME is the name of a vtable member in CU's
15931 language, zero otherwise. */
15932 static int
15933 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15934 {
15935 static const char vptr[] = "_vptr";
15936
15937 /* Look for the C++ form of the vtable. */
15938 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15939 return 1;
15940
15941 return 0;
15942 }
15943
15944 /* GCC outputs unnamed structures that are really pointers to member
15945 functions, with the ABI-specified layout. If TYPE describes
15946 such a structure, smash it into a member function type.
15947
15948 GCC shouldn't do this; it should just output pointer to member DIEs.
15949 This is GCC PR debug/28767. */
15950
15951 static void
15952 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15953 {
15954 struct type *pfn_type, *self_type, *new_type;
15955
15956 /* Check for a structure with no name and two children. */
15957 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15958 return;
15959
15960 /* Check for __pfn and __delta members. */
15961 if (TYPE_FIELD_NAME (type, 0) == NULL
15962 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15963 || TYPE_FIELD_NAME (type, 1) == NULL
15964 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15965 return;
15966
15967 /* Find the type of the method. */
15968 pfn_type = type->field (0).type ();
15969 if (pfn_type == NULL
15970 || pfn_type->code () != TYPE_CODE_PTR
15971 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15972 return;
15973
15974 /* Look for the "this" argument. */
15975 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15976 if (pfn_type->num_fields () == 0
15977 /* || pfn_type->field (0).type () == NULL */
15978 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15979 return;
15980
15981 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15982 new_type = alloc_type (objfile);
15983 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15984 pfn_type->fields (), pfn_type->num_fields (),
15985 pfn_type->has_varargs ());
15986 smash_to_methodptr_type (type, new_type);
15987 }
15988
15989 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15990 requires rewriting, then copy it and return the updated copy.
15991 Otherwise return nullptr. */
15992
15993 static struct type *
15994 rewrite_array_type (struct type *type)
15995 {
15996 if (type->code () != TYPE_CODE_ARRAY)
15997 return nullptr;
15998
15999 struct type *index_type = type->index_type ();
16000 range_bounds *current_bounds = index_type->bounds ();
16001
16002 /* Handle multi-dimensional arrays. */
16003 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
16004 if (new_target == nullptr)
16005 {
16006 /* Maybe we don't need to rewrite this array. */
16007 if (current_bounds->low.kind () == PROP_CONST
16008 && current_bounds->high.kind () == PROP_CONST)
16009 return nullptr;
16010 }
16011
16012 /* Either the target type was rewritten, or the bounds have to be
16013 updated. Either way we want to copy the type and update
16014 everything. */
16015 struct type *copy = copy_type (type);
16016 int nfields = copy->num_fields ();
16017 field *new_fields
16018 = ((struct field *) TYPE_ZALLOC (copy,
16019 nfields * sizeof (struct field)));
16020 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
16021 copy->set_fields (new_fields);
16022 if (new_target != nullptr)
16023 TYPE_TARGET_TYPE (copy) = new_target;
16024
16025 struct type *index_copy = copy_type (index_type);
16026 range_bounds *bounds
16027 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
16028 sizeof (range_bounds));
16029 *bounds = *current_bounds;
16030 bounds->low.set_const_val (1);
16031 bounds->high.set_const_val (0);
16032 index_copy->set_bounds (bounds);
16033 copy->set_index_type (index_copy);
16034
16035 return copy;
16036 }
16037
16038 /* While some versions of GCC will generate complicated DWARF for an
16039 array (see quirk_ada_thick_pointer), more recent versions were
16040 modified to emit an explicit thick pointer structure. However, in
16041 this case, the array still has DWARF expressions for its ranges,
16042 and these must be ignored. */
16043
16044 static void
16045 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
16046 struct type *type)
16047 {
16048 gdb_assert (cu->language == language_ada);
16049
16050 /* Check for a structure with two children. */
16051 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
16052 return;
16053
16054 /* Check for P_ARRAY and P_BOUNDS members. */
16055 if (TYPE_FIELD_NAME (type, 0) == NULL
16056 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
16057 || TYPE_FIELD_NAME (type, 1) == NULL
16058 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
16059 return;
16060
16061 /* Make sure we're looking at a pointer to an array. */
16062 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
16063 return;
16064
16065 /* The Ada code already knows how to handle these types, so all that
16066 we need to do is turn the bounds into static bounds. However, we
16067 don't want to rewrite existing array or index types in-place,
16068 because those may be referenced in other contexts where this
16069 rewriting is undesirable. */
16070 struct type *new_ary_type
16071 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
16072 if (new_ary_type != nullptr)
16073 type->field (0).set_type (lookup_pointer_type (new_ary_type));
16074 }
16075
16076 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
16077 appropriate error checking and issuing complaints if there is a
16078 problem. */
16079
16080 static ULONGEST
16081 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
16082 {
16083 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
16084
16085 if (attr == nullptr)
16086 return 0;
16087
16088 if (!attr->form_is_constant ())
16089 {
16090 complaint (_("DW_AT_alignment must have constant form"
16091 " - DIE at %s [in module %s]"),
16092 sect_offset_str (die->sect_off),
16093 objfile_name (cu->per_objfile->objfile));
16094 return 0;
16095 }
16096
16097 LONGEST val = attr->constant_value (0);
16098 if (val < 0)
16099 {
16100 complaint (_("DW_AT_alignment value must not be negative"
16101 " - DIE at %s [in module %s]"),
16102 sect_offset_str (die->sect_off),
16103 objfile_name (cu->per_objfile->objfile));
16104 return 0;
16105 }
16106 ULONGEST align = val;
16107
16108 if (align == 0)
16109 {
16110 complaint (_("DW_AT_alignment value must not be zero"
16111 " - DIE at %s [in module %s]"),
16112 sect_offset_str (die->sect_off),
16113 objfile_name (cu->per_objfile->objfile));
16114 return 0;
16115 }
16116 if ((align & (align - 1)) != 0)
16117 {
16118 complaint (_("DW_AT_alignment value must be a power of 2"
16119 " - DIE at %s [in module %s]"),
16120 sect_offset_str (die->sect_off),
16121 objfile_name (cu->per_objfile->objfile));
16122 return 0;
16123 }
16124
16125 return align;
16126 }
16127
16128 /* If the DIE has a DW_AT_alignment attribute, use its value to set
16129 the alignment for TYPE. */
16130
16131 static void
16132 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
16133 struct type *type)
16134 {
16135 if (!set_type_align (type, get_alignment (cu, die)))
16136 complaint (_("DW_AT_alignment value too large"
16137 " - DIE at %s [in module %s]"),
16138 sect_offset_str (die->sect_off),
16139 objfile_name (cu->per_objfile->objfile));
16140 }
16141
16142 /* Check if the given VALUE is a valid enum dwarf_calling_convention
16143 constant for a type, according to DWARF5 spec, Table 5.5. */
16144
16145 static bool
16146 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
16147 {
16148 switch (value)
16149 {
16150 case DW_CC_normal:
16151 case DW_CC_pass_by_reference:
16152 case DW_CC_pass_by_value:
16153 return true;
16154
16155 default:
16156 complaint (_("unrecognized DW_AT_calling_convention value "
16157 "(%s) for a type"), pulongest (value));
16158 return false;
16159 }
16160 }
16161
16162 /* Check if the given VALUE is a valid enum dwarf_calling_convention
16163 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
16164 also according to GNU-specific values (see include/dwarf2.h). */
16165
16166 static bool
16167 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
16168 {
16169 switch (value)
16170 {
16171 case DW_CC_normal:
16172 case DW_CC_program:
16173 case DW_CC_nocall:
16174 return true;
16175
16176 case DW_CC_GNU_renesas_sh:
16177 case DW_CC_GNU_borland_fastcall_i386:
16178 case DW_CC_GDB_IBM_OpenCL:
16179 return true;
16180
16181 default:
16182 complaint (_("unrecognized DW_AT_calling_convention value "
16183 "(%s) for a subroutine"), pulongest (value));
16184 return false;
16185 }
16186 }
16187
16188 /* Called when we find the DIE that starts a structure or union scope
16189 (definition) to create a type for the structure or union. Fill in
16190 the type's name and general properties; the members will not be
16191 processed until process_structure_scope. A symbol table entry for
16192 the type will also not be done until process_structure_scope (assuming
16193 the type has a name).
16194
16195 NOTE: we need to call these functions regardless of whether or not the
16196 DIE has a DW_AT_name attribute, since it might be an anonymous
16197 structure or union. This gets the type entered into our set of
16198 user defined types. */
16199
16200 static struct type *
16201 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
16202 {
16203 struct objfile *objfile = cu->per_objfile->objfile;
16204 struct type *type;
16205 struct attribute *attr;
16206 const char *name;
16207
16208 /* If the definition of this type lives in .debug_types, read that type.
16209 Don't follow DW_AT_specification though, that will take us back up
16210 the chain and we want to go down. */
16211 attr = die->attr (DW_AT_signature);
16212 if (attr != nullptr)
16213 {
16214 type = get_DW_AT_signature_type (die, attr, cu);
16215
16216 /* The type's CU may not be the same as CU.
16217 Ensure TYPE is recorded with CU in die_type_hash. */
16218 return set_die_type (die, type, cu);
16219 }
16220
16221 type = alloc_type (objfile);
16222 INIT_CPLUS_SPECIFIC (type);
16223
16224 name = dwarf2_name (die, cu);
16225 if (name != NULL)
16226 {
16227 if (cu->language == language_cplus
16228 || cu->language == language_d
16229 || cu->language == language_rust)
16230 {
16231 const char *full_name = dwarf2_full_name (name, die, cu);
16232
16233 /* dwarf2_full_name might have already finished building the DIE's
16234 type. If so, there is no need to continue. */
16235 if (get_die_type (die, cu) != NULL)
16236 return get_die_type (die, cu);
16237
16238 type->set_name (full_name);
16239 }
16240 else
16241 {
16242 /* The name is already allocated along with this objfile, so
16243 we don't need to duplicate it for the type. */
16244 type->set_name (name);
16245 }
16246 }
16247
16248 if (die->tag == DW_TAG_structure_type)
16249 {
16250 type->set_code (TYPE_CODE_STRUCT);
16251 }
16252 else if (die->tag == DW_TAG_union_type)
16253 {
16254 type->set_code (TYPE_CODE_UNION);
16255 }
16256 else
16257 {
16258 type->set_code (TYPE_CODE_STRUCT);
16259 }
16260
16261 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16262 type->set_is_declared_class (true);
16263
16264 /* Store the calling convention in the type if it's available in
16265 the die. Otherwise the calling convention remains set to
16266 the default value DW_CC_normal. */
16267 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16268 if (attr != nullptr
16269 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
16270 {
16271 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16272 TYPE_CPLUS_CALLING_CONVENTION (type)
16273 = (enum dwarf_calling_convention) (attr->constant_value (0));
16274 }
16275
16276 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16277 if (attr != nullptr)
16278 {
16279 if (attr->form_is_constant ())
16280 TYPE_LENGTH (type) = attr->constant_value (0);
16281 else
16282 {
16283 struct dynamic_prop prop;
16284 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
16285 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
16286 TYPE_LENGTH (type) = 0;
16287 }
16288 }
16289 else
16290 {
16291 TYPE_LENGTH (type) = 0;
16292 }
16293
16294 maybe_set_alignment (cu, die, type);
16295
16296 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
16297 {
16298 /* ICC<14 does not output the required DW_AT_declaration on
16299 incomplete types, but gives them a size of zero. */
16300 type->set_is_stub (true);
16301 }
16302 else
16303 type->set_stub_is_supported (true);
16304
16305 if (die_is_declaration (die, cu))
16306 type->set_is_stub (true);
16307 else if (attr == NULL && die->child == NULL
16308 && producer_is_realview (cu->producer))
16309 /* RealView does not output the required DW_AT_declaration
16310 on incomplete types. */
16311 type->set_is_stub (true);
16312
16313 /* We need to add the type field to the die immediately so we don't
16314 infinitely recurse when dealing with pointers to the structure
16315 type within the structure itself. */
16316 set_die_type (die, type, cu);
16317
16318 /* set_die_type should be already done. */
16319 set_descriptive_type (type, die, cu);
16320
16321 return type;
16322 }
16323
16324 static void handle_struct_member_die
16325 (struct die_info *child_die,
16326 struct type *type,
16327 struct field_info *fi,
16328 std::vector<struct symbol *> *template_args,
16329 struct dwarf2_cu *cu);
16330
16331 /* A helper for handle_struct_member_die that handles
16332 DW_TAG_variant_part. */
16333
16334 static void
16335 handle_variant_part (struct die_info *die, struct type *type,
16336 struct field_info *fi,
16337 std::vector<struct symbol *> *template_args,
16338 struct dwarf2_cu *cu)
16339 {
16340 variant_part_builder *new_part;
16341 if (fi->current_variant_part == nullptr)
16342 {
16343 fi->variant_parts.emplace_back ();
16344 new_part = &fi->variant_parts.back ();
16345 }
16346 else if (!fi->current_variant_part->processing_variant)
16347 {
16348 complaint (_("nested DW_TAG_variant_part seen "
16349 "- DIE at %s [in module %s]"),
16350 sect_offset_str (die->sect_off),
16351 objfile_name (cu->per_objfile->objfile));
16352 return;
16353 }
16354 else
16355 {
16356 variant_field &current = fi->current_variant_part->variants.back ();
16357 current.variant_parts.emplace_back ();
16358 new_part = &current.variant_parts.back ();
16359 }
16360
16361 /* When we recurse, we want callees to add to this new variant
16362 part. */
16363 scoped_restore save_current_variant_part
16364 = make_scoped_restore (&fi->current_variant_part, new_part);
16365
16366 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16367 if (discr == NULL)
16368 {
16369 /* It's a univariant form, an extension we support. */
16370 }
16371 else if (discr->form_is_ref ())
16372 {
16373 struct dwarf2_cu *target_cu = cu;
16374 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16375
16376 new_part->discriminant_offset = target_die->sect_off;
16377 }
16378 else
16379 {
16380 complaint (_("DW_AT_discr does not have DIE reference form"
16381 " - DIE at %s [in module %s]"),
16382 sect_offset_str (die->sect_off),
16383 objfile_name (cu->per_objfile->objfile));
16384 }
16385
16386 for (die_info *child_die = die->child;
16387 child_die != NULL;
16388 child_die = child_die->sibling)
16389 handle_struct_member_die (child_die, type, fi, template_args, cu);
16390 }
16391
16392 /* A helper for handle_struct_member_die that handles
16393 DW_TAG_variant. */
16394
16395 static void
16396 handle_variant (struct die_info *die, struct type *type,
16397 struct field_info *fi,
16398 std::vector<struct symbol *> *template_args,
16399 struct dwarf2_cu *cu)
16400 {
16401 if (fi->current_variant_part == nullptr)
16402 {
16403 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
16404 "- DIE at %s [in module %s]"),
16405 sect_offset_str (die->sect_off),
16406 objfile_name (cu->per_objfile->objfile));
16407 return;
16408 }
16409 if (fi->current_variant_part->processing_variant)
16410 {
16411 complaint (_("nested DW_TAG_variant seen "
16412 "- DIE at %s [in module %s]"),
16413 sect_offset_str (die->sect_off),
16414 objfile_name (cu->per_objfile->objfile));
16415 return;
16416 }
16417
16418 scoped_restore save_processing_variant
16419 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16420 true);
16421
16422 fi->current_variant_part->variants.emplace_back ();
16423 variant_field &variant = fi->current_variant_part->variants.back ();
16424 variant.first_field = fi->fields.size ();
16425
16426 /* In a variant we want to get the discriminant and also add a
16427 field for our sole member child. */
16428 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
16429 if (discr == nullptr || !discr->form_is_constant ())
16430 {
16431 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
16432 if (discr == nullptr || discr->as_block ()->size == 0)
16433 variant.default_branch = true;
16434 else
16435 variant.discr_list_data = discr->as_block ();
16436 }
16437 else
16438 variant.discriminant_value = discr->constant_value (0);
16439
16440 for (die_info *variant_child = die->child;
16441 variant_child != NULL;
16442 variant_child = variant_child->sibling)
16443 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16444
16445 variant.last_field = fi->fields.size ();
16446 }
16447
16448 /* A helper for process_structure_scope that handles a single member
16449 DIE. */
16450
16451 static void
16452 handle_struct_member_die (struct die_info *child_die, struct type *type,
16453 struct field_info *fi,
16454 std::vector<struct symbol *> *template_args,
16455 struct dwarf2_cu *cu)
16456 {
16457 if (child_die->tag == DW_TAG_member
16458 || child_die->tag == DW_TAG_variable)
16459 {
16460 /* NOTE: carlton/2002-11-05: A C++ static data member
16461 should be a DW_TAG_member that is a declaration, but
16462 all versions of G++ as of this writing (so through at
16463 least 3.2.1) incorrectly generate DW_TAG_variable
16464 tags for them instead. */
16465 dwarf2_add_field (fi, child_die, cu);
16466 }
16467 else if (child_die->tag == DW_TAG_subprogram)
16468 {
16469 /* Rust doesn't have member functions in the C++ sense.
16470 However, it does emit ordinary functions as children
16471 of a struct DIE. */
16472 if (cu->language == language_rust)
16473 read_func_scope (child_die, cu);
16474 else
16475 {
16476 /* C++ member function. */
16477 dwarf2_add_member_fn (fi, child_die, type, cu);
16478 }
16479 }
16480 else if (child_die->tag == DW_TAG_inheritance)
16481 {
16482 /* C++ base class field. */
16483 dwarf2_add_field (fi, child_die, cu);
16484 }
16485 else if (type_can_define_types (child_die))
16486 dwarf2_add_type_defn (fi, child_die, cu);
16487 else if (child_die->tag == DW_TAG_template_type_param
16488 || child_die->tag == DW_TAG_template_value_param)
16489 {
16490 struct symbol *arg = new_symbol (child_die, NULL, cu);
16491
16492 if (arg != NULL)
16493 template_args->push_back (arg);
16494 }
16495 else if (child_die->tag == DW_TAG_variant_part)
16496 handle_variant_part (child_die, type, fi, template_args, cu);
16497 else if (child_die->tag == DW_TAG_variant)
16498 handle_variant (child_die, type, fi, template_args, cu);
16499 }
16500
16501 /* Finish creating a structure or union type, including filling in
16502 its members and creating a symbol for it. */
16503
16504 static void
16505 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16506 {
16507 struct objfile *objfile = cu->per_objfile->objfile;
16508 struct die_info *child_die;
16509 struct type *type;
16510
16511 type = get_die_type (die, cu);
16512 if (type == NULL)
16513 type = read_structure_type (die, cu);
16514
16515 bool has_template_parameters = false;
16516 if (die->child != NULL && ! die_is_declaration (die, cu))
16517 {
16518 struct field_info fi;
16519 std::vector<struct symbol *> template_args;
16520
16521 child_die = die->child;
16522
16523 while (child_die && child_die->tag)
16524 {
16525 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16526 child_die = child_die->sibling;
16527 }
16528
16529 /* Attach template arguments to type. */
16530 if (!template_args.empty ())
16531 {
16532 has_template_parameters = true;
16533 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16534 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16535 TYPE_TEMPLATE_ARGUMENTS (type)
16536 = XOBNEWVEC (&objfile->objfile_obstack,
16537 struct symbol *,
16538 TYPE_N_TEMPLATE_ARGUMENTS (type));
16539 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16540 template_args.data (),
16541 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16542 * sizeof (struct symbol *)));
16543 }
16544
16545 /* Attach fields and member functions to the type. */
16546 if (fi.nfields () > 0)
16547 dwarf2_attach_fields_to_type (&fi, type, cu);
16548 if (!fi.fnfieldlists.empty ())
16549 {
16550 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16551
16552 /* Get the type which refers to the base class (possibly this
16553 class itself) which contains the vtable pointer for the current
16554 class from the DW_AT_containing_type attribute. This use of
16555 DW_AT_containing_type is a GNU extension. */
16556
16557 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16558 {
16559 struct type *t = die_containing_type (die, cu);
16560
16561 set_type_vptr_basetype (type, t);
16562 if (type == t)
16563 {
16564 int i;
16565
16566 /* Our own class provides vtbl ptr. */
16567 for (i = t->num_fields () - 1;
16568 i >= TYPE_N_BASECLASSES (t);
16569 --i)
16570 {
16571 const char *fieldname = TYPE_FIELD_NAME (t, i);
16572
16573 if (is_vtable_name (fieldname, cu))
16574 {
16575 set_type_vptr_fieldno (type, i);
16576 break;
16577 }
16578 }
16579
16580 /* Complain if virtual function table field not found. */
16581 if (i < TYPE_N_BASECLASSES (t))
16582 complaint (_("virtual function table pointer "
16583 "not found when defining class '%s'"),
16584 type->name () ? type->name () : "");
16585 }
16586 else
16587 {
16588 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16589 }
16590 }
16591 else if (cu->producer
16592 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16593 {
16594 /* The IBM XLC compiler does not provide direct indication
16595 of the containing type, but the vtable pointer is
16596 always named __vfp. */
16597
16598 int i;
16599
16600 for (i = type->num_fields () - 1;
16601 i >= TYPE_N_BASECLASSES (type);
16602 --i)
16603 {
16604 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16605 {
16606 set_type_vptr_fieldno (type, i);
16607 set_type_vptr_basetype (type, type);
16608 break;
16609 }
16610 }
16611 }
16612 }
16613
16614 /* Copy fi.typedef_field_list linked list elements content into the
16615 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16616 if (!fi.typedef_field_list.empty ())
16617 {
16618 int count = fi.typedef_field_list.size ();
16619
16620 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16621 TYPE_TYPEDEF_FIELD_ARRAY (type)
16622 = ((struct decl_field *)
16623 TYPE_ALLOC (type,
16624 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16625 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16626
16627 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16628 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16629 }
16630
16631 /* Copy fi.nested_types_list linked list elements content into the
16632 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16633 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16634 {
16635 int count = fi.nested_types_list.size ();
16636
16637 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16638 TYPE_NESTED_TYPES_ARRAY (type)
16639 = ((struct decl_field *)
16640 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16641 TYPE_NESTED_TYPES_COUNT (type) = count;
16642
16643 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16644 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16645 }
16646 }
16647
16648 quirk_gcc_member_function_pointer (type, objfile);
16649 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16650 cu->rust_unions.push_back (type);
16651 else if (cu->language == language_ada)
16652 quirk_ada_thick_pointer_struct (die, cu, type);
16653
16654 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16655 snapshots) has been known to create a die giving a declaration
16656 for a class that has, as a child, a die giving a definition for a
16657 nested class. So we have to process our children even if the
16658 current die is a declaration. Normally, of course, a declaration
16659 won't have any children at all. */
16660
16661 child_die = die->child;
16662
16663 while (child_die != NULL && child_die->tag)
16664 {
16665 if (child_die->tag == DW_TAG_member
16666 || child_die->tag == DW_TAG_variable
16667 || child_die->tag == DW_TAG_inheritance
16668 || child_die->tag == DW_TAG_template_value_param
16669 || child_die->tag == DW_TAG_template_type_param)
16670 {
16671 /* Do nothing. */
16672 }
16673 else
16674 process_die (child_die, cu);
16675
16676 child_die = child_die->sibling;
16677 }
16678
16679 /* Do not consider external references. According to the DWARF standard,
16680 these DIEs are identified by the fact that they have no byte_size
16681 attribute, and a declaration attribute. */
16682 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16683 || !die_is_declaration (die, cu)
16684 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16685 {
16686 struct symbol *sym = new_symbol (die, type, cu);
16687
16688 if (has_template_parameters)
16689 {
16690 struct symtab *symtab;
16691 if (sym != nullptr)
16692 symtab = symbol_symtab (sym);
16693 else if (cu->line_header != nullptr)
16694 {
16695 /* Any related symtab will do. */
16696 symtab
16697 = cu->line_header->file_names ()[0].symtab;
16698 }
16699 else
16700 {
16701 symtab = nullptr;
16702 complaint (_("could not find suitable "
16703 "symtab for template parameter"
16704 " - DIE at %s [in module %s]"),
16705 sect_offset_str (die->sect_off),
16706 objfile_name (objfile));
16707 }
16708
16709 if (symtab != nullptr)
16710 {
16711 /* Make sure that the symtab is set on the new symbols.
16712 Even though they don't appear in this symtab directly,
16713 other parts of gdb assume that symbols do, and this is
16714 reasonably true. */
16715 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16716 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16717 }
16718 }
16719 }
16720 }
16721
16722 /* Assuming DIE is an enumeration type, and TYPE is its associated
16723 type, update TYPE using some information only available in DIE's
16724 children. In particular, the fields are computed. */
16725
16726 static void
16727 update_enumeration_type_from_children (struct die_info *die,
16728 struct type *type,
16729 struct dwarf2_cu *cu)
16730 {
16731 struct die_info *child_die;
16732 int unsigned_enum = 1;
16733 int flag_enum = 1;
16734
16735 auto_obstack obstack;
16736 std::vector<struct field> fields;
16737
16738 for (child_die = die->child;
16739 child_die != NULL && child_die->tag;
16740 child_die = child_die->sibling)
16741 {
16742 struct attribute *attr;
16743 LONGEST value;
16744 const gdb_byte *bytes;
16745 struct dwarf2_locexpr_baton *baton;
16746 const char *name;
16747
16748 if (child_die->tag != DW_TAG_enumerator)
16749 continue;
16750
16751 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16752 if (attr == NULL)
16753 continue;
16754
16755 name = dwarf2_name (child_die, cu);
16756 if (name == NULL)
16757 name = "<anonymous enumerator>";
16758
16759 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16760 &value, &bytes, &baton);
16761 if (value < 0)
16762 {
16763 unsigned_enum = 0;
16764 flag_enum = 0;
16765 }
16766 else
16767 {
16768 if (count_one_bits_ll (value) >= 2)
16769 flag_enum = 0;
16770 }
16771
16772 fields.emplace_back ();
16773 struct field &field = fields.back ();
16774 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16775 SET_FIELD_ENUMVAL (field, value);
16776 }
16777
16778 if (!fields.empty ())
16779 {
16780 type->set_num_fields (fields.size ());
16781 type->set_fields
16782 ((struct field *)
16783 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16784 memcpy (type->fields (), fields.data (),
16785 sizeof (struct field) * fields.size ());
16786 }
16787
16788 if (unsigned_enum)
16789 type->set_is_unsigned (true);
16790
16791 if (flag_enum)
16792 type->set_is_flag_enum (true);
16793 }
16794
16795 /* Given a DW_AT_enumeration_type die, set its type. We do not
16796 complete the type's fields yet, or create any symbols. */
16797
16798 static struct type *
16799 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16800 {
16801 struct objfile *objfile = cu->per_objfile->objfile;
16802 struct type *type;
16803 struct attribute *attr;
16804 const char *name;
16805
16806 /* If the definition of this type lives in .debug_types, read that type.
16807 Don't follow DW_AT_specification though, that will take us back up
16808 the chain and we want to go down. */
16809 attr = die->attr (DW_AT_signature);
16810 if (attr != nullptr)
16811 {
16812 type = get_DW_AT_signature_type (die, attr, cu);
16813
16814 /* The type's CU may not be the same as CU.
16815 Ensure TYPE is recorded with CU in die_type_hash. */
16816 return set_die_type (die, type, cu);
16817 }
16818
16819 type = alloc_type (objfile);
16820
16821 type->set_code (TYPE_CODE_ENUM);
16822 name = dwarf2_full_name (NULL, die, cu);
16823 if (name != NULL)
16824 type->set_name (name);
16825
16826 attr = dwarf2_attr (die, DW_AT_type, cu);
16827 if (attr != NULL)
16828 {
16829 struct type *underlying_type = die_type (die, cu);
16830
16831 TYPE_TARGET_TYPE (type) = underlying_type;
16832 }
16833
16834 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16835 if (attr != nullptr)
16836 {
16837 TYPE_LENGTH (type) = attr->constant_value (0);
16838 }
16839 else
16840 {
16841 TYPE_LENGTH (type) = 0;
16842 }
16843
16844 maybe_set_alignment (cu, die, type);
16845
16846 /* The enumeration DIE can be incomplete. In Ada, any type can be
16847 declared as private in the package spec, and then defined only
16848 inside the package body. Such types are known as Taft Amendment
16849 Types. When another package uses such a type, an incomplete DIE
16850 may be generated by the compiler. */
16851 if (die_is_declaration (die, cu))
16852 type->set_is_stub (true);
16853
16854 /* If this type has an underlying type that is not a stub, then we
16855 may use its attributes. We always use the "unsigned" attribute
16856 in this situation, because ordinarily we guess whether the type
16857 is unsigned -- but the guess can be wrong and the underlying type
16858 can tell us the reality. However, we defer to a local size
16859 attribute if one exists, because this lets the compiler override
16860 the underlying type if needed. */
16861 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16862 {
16863 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16864 underlying_type = check_typedef (underlying_type);
16865
16866 type->set_is_unsigned (underlying_type->is_unsigned ());
16867
16868 if (TYPE_LENGTH (type) == 0)
16869 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16870
16871 if (TYPE_RAW_ALIGN (type) == 0
16872 && TYPE_RAW_ALIGN (underlying_type) != 0)
16873 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16874 }
16875
16876 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
16877
16878 set_die_type (die, type, cu);
16879
16880 /* Finish the creation of this type by using the enum's children.
16881 Note that, as usual, this must come after set_die_type to avoid
16882 infinite recursion when trying to compute the names of the
16883 enumerators. */
16884 update_enumeration_type_from_children (die, type, cu);
16885
16886 return type;
16887 }
16888
16889 /* Given a pointer to a die which begins an enumeration, process all
16890 the dies that define the members of the enumeration, and create the
16891 symbol for the enumeration type.
16892
16893 NOTE: We reverse the order of the element list. */
16894
16895 static void
16896 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16897 {
16898 struct type *this_type;
16899
16900 this_type = get_die_type (die, cu);
16901 if (this_type == NULL)
16902 this_type = read_enumeration_type (die, cu);
16903
16904 if (die->child != NULL)
16905 {
16906 struct die_info *child_die;
16907 const char *name;
16908
16909 child_die = die->child;
16910 while (child_die && child_die->tag)
16911 {
16912 if (child_die->tag != DW_TAG_enumerator)
16913 {
16914 process_die (child_die, cu);
16915 }
16916 else
16917 {
16918 name = dwarf2_name (child_die, cu);
16919 if (name)
16920 new_symbol (child_die, this_type, cu);
16921 }
16922
16923 child_die = child_die->sibling;
16924 }
16925 }
16926
16927 /* If we are reading an enum from a .debug_types unit, and the enum
16928 is a declaration, and the enum is not the signatured type in the
16929 unit, then we do not want to add a symbol for it. Adding a
16930 symbol would in some cases obscure the true definition of the
16931 enum, giving users an incomplete type when the definition is
16932 actually available. Note that we do not want to do this for all
16933 enums which are just declarations, because C++0x allows forward
16934 enum declarations. */
16935 if (cu->per_cu->is_debug_types
16936 && die_is_declaration (die, cu))
16937 {
16938 struct signatured_type *sig_type;
16939
16940 sig_type = (struct signatured_type *) cu->per_cu;
16941 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16942 if (sig_type->type_offset_in_section != die->sect_off)
16943 return;
16944 }
16945
16946 new_symbol (die, this_type, cu);
16947 }
16948
16949 /* Helper function for quirk_ada_thick_pointer that examines a bounds
16950 expression for an index type and finds the corresponding field
16951 offset in the hidden "P_BOUNDS" structure. Returns true on success
16952 and updates *FIELD, false if it fails to recognize an
16953 expression. */
16954
16955 static bool
16956 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16957 int *bounds_offset, struct field *field,
16958 struct dwarf2_cu *cu)
16959 {
16960 struct attribute *attr = dwarf2_attr (die, name, cu);
16961 if (attr == nullptr || !attr->form_is_block ())
16962 return false;
16963
16964 const struct dwarf_block *block = attr->as_block ();
16965 const gdb_byte *start = block->data;
16966 const gdb_byte *end = block->data + block->size;
16967
16968 /* The expression to recognize generally looks like:
16969
16970 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16971 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16972
16973 However, the second "plus_uconst" may be missing:
16974
16975 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16976 DW_OP_deref_size: 4)
16977
16978 This happens when the field is at the start of the structure.
16979
16980 Also, the final deref may not be sized:
16981
16982 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16983 DW_OP_deref)
16984
16985 This happens when the size of the index type happens to be the
16986 same as the architecture's word size. This can occur with or
16987 without the second plus_uconst. */
16988
16989 if (end - start < 2)
16990 return false;
16991 if (*start++ != DW_OP_push_object_address)
16992 return false;
16993 if (*start++ != DW_OP_plus_uconst)
16994 return false;
16995
16996 uint64_t this_bound_off;
16997 start = gdb_read_uleb128 (start, end, &this_bound_off);
16998 if (start == nullptr || (int) this_bound_off != this_bound_off)
16999 return false;
17000 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
17001 is consistent among all bounds. */
17002 if (*bounds_offset == -1)
17003 *bounds_offset = this_bound_off;
17004 else if (*bounds_offset != this_bound_off)
17005 return false;
17006
17007 if (start == end || *start++ != DW_OP_deref)
17008 return false;
17009
17010 int offset = 0;
17011 if (start ==end)
17012 return false;
17013 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
17014 {
17015 /* This means an offset of 0. */
17016 }
17017 else if (*start++ != DW_OP_plus_uconst)
17018 return false;
17019 else
17020 {
17021 /* The size is the parameter to DW_OP_plus_uconst. */
17022 uint64_t val;
17023 start = gdb_read_uleb128 (start, end, &val);
17024 if (start == nullptr)
17025 return false;
17026 if ((int) val != val)
17027 return false;
17028 offset = val;
17029 }
17030
17031 if (start == end)
17032 return false;
17033
17034 uint64_t size;
17035 if (*start == DW_OP_deref_size)
17036 {
17037 start = gdb_read_uleb128 (start + 1, end, &size);
17038 if (start == nullptr)
17039 return false;
17040 }
17041 else if (*start == DW_OP_deref)
17042 {
17043 size = cu->header.addr_size;
17044 ++start;
17045 }
17046 else
17047 return false;
17048
17049 SET_FIELD_BITPOS (*field, 8 * offset);
17050 if (size != TYPE_LENGTH (field->type ()))
17051 FIELD_BITSIZE (*field) = 8 * size;
17052
17053 return true;
17054 }
17055
17056 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
17057 some kinds of Ada arrays:
17058
17059 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
17060 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
17061 <11e0> DW_AT_data_location: 2 byte block: 97 6
17062 (DW_OP_push_object_address; DW_OP_deref)
17063 <11e3> DW_AT_type : <0x1173>
17064 <11e7> DW_AT_sibling : <0x1201>
17065 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
17066 <11ec> DW_AT_type : <0x1206>
17067 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
17068 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
17069 DW_OP_deref_size: 4)
17070 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
17071 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
17072 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
17073
17074 This actually represents a "thick pointer", which is a structure
17075 with two elements: one that is a pointer to the array data, and one
17076 that is a pointer to another structure; this second structure holds
17077 the array bounds.
17078
17079 This returns a new type on success, or nullptr if this didn't
17080 recognize the type. */
17081
17082 static struct type *
17083 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
17084 struct type *type)
17085 {
17086 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
17087 /* So far we've only seen this with block form. */
17088 if (attr == nullptr || !attr->form_is_block ())
17089 return nullptr;
17090
17091 /* Note that this will fail if the structure layout is changed by
17092 the compiler. However, we have no good way to recognize some
17093 other layout, because we don't know what expression the compiler
17094 might choose to emit should this happen. */
17095 struct dwarf_block *blk = attr->as_block ();
17096 if (blk->size != 2
17097 || blk->data[0] != DW_OP_push_object_address
17098 || blk->data[1] != DW_OP_deref)
17099 return nullptr;
17100
17101 int bounds_offset = -1;
17102 int max_align = -1;
17103 std::vector<struct field> range_fields;
17104 for (struct die_info *child_die = die->child;
17105 child_die;
17106 child_die = child_die->sibling)
17107 {
17108 if (child_die->tag == DW_TAG_subrange_type)
17109 {
17110 struct type *underlying = read_subrange_index_type (child_die, cu);
17111
17112 int this_align = type_align (underlying);
17113 if (this_align > max_align)
17114 max_align = this_align;
17115
17116 range_fields.emplace_back ();
17117 range_fields.emplace_back ();
17118
17119 struct field &lower = range_fields[range_fields.size () - 2];
17120 struct field &upper = range_fields[range_fields.size () - 1];
17121
17122 lower.set_type (underlying);
17123 FIELD_ARTIFICIAL (lower) = 1;
17124
17125 upper.set_type (underlying);
17126 FIELD_ARTIFICIAL (upper) = 1;
17127
17128 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
17129 &bounds_offset, &lower, cu)
17130 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
17131 &bounds_offset, &upper, cu))
17132 return nullptr;
17133 }
17134 }
17135
17136 /* This shouldn't really happen, but double-check that we found
17137 where the bounds are stored. */
17138 if (bounds_offset == -1)
17139 return nullptr;
17140
17141 struct objfile *objfile = cu->per_objfile->objfile;
17142 for (int i = 0; i < range_fields.size (); i += 2)
17143 {
17144 char name[20];
17145
17146 /* Set the name of each field in the bounds. */
17147 xsnprintf (name, sizeof (name), "LB%d", i / 2);
17148 FIELD_NAME (range_fields[i]) = objfile->intern (name);
17149 xsnprintf (name, sizeof (name), "UB%d", i / 2);
17150 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
17151 }
17152
17153 struct type *bounds = alloc_type (objfile);
17154 bounds->set_code (TYPE_CODE_STRUCT);
17155
17156 bounds->set_num_fields (range_fields.size ());
17157 bounds->set_fields
17158 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
17159 * sizeof (struct field))));
17160 memcpy (bounds->fields (), range_fields.data (),
17161 bounds->num_fields () * sizeof (struct field));
17162
17163 int last_fieldno = range_fields.size () - 1;
17164 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
17165 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
17166 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
17167
17168 /* Rewrite the existing array type in place. Specifically, we
17169 remove any dynamic properties we might have read, and we replace
17170 the index types. */
17171 struct type *iter = type;
17172 for (int i = 0; i < range_fields.size (); i += 2)
17173 {
17174 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
17175 iter->main_type->dyn_prop_list = nullptr;
17176 iter->set_index_type
17177 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
17178 iter = TYPE_TARGET_TYPE (iter);
17179 }
17180
17181 struct type *result = alloc_type (objfile);
17182 result->set_code (TYPE_CODE_STRUCT);
17183
17184 result->set_num_fields (2);
17185 result->set_fields
17186 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
17187 * sizeof (struct field))));
17188
17189 /* The names are chosen to coincide with what the compiler does with
17190 -fgnat-encodings=all, which the Ada code in gdb already
17191 understands. */
17192 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
17193 result->field (0).set_type (lookup_pointer_type (type));
17194
17195 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
17196 result->field (1).set_type (lookup_pointer_type (bounds));
17197 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
17198
17199 result->set_name (type->name ());
17200 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
17201 + TYPE_LENGTH (result->field (1).type ()));
17202
17203 return result;
17204 }
17205
17206 /* Extract all information from a DW_TAG_array_type DIE and put it in
17207 the DIE's type field. For now, this only handles one dimensional
17208 arrays. */
17209
17210 static struct type *
17211 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
17212 {
17213 struct objfile *objfile = cu->per_objfile->objfile;
17214 struct die_info *child_die;
17215 struct type *type;
17216 struct type *element_type, *range_type, *index_type;
17217 struct attribute *attr;
17218 const char *name;
17219 struct dynamic_prop *byte_stride_prop = NULL;
17220 unsigned int bit_stride = 0;
17221
17222 element_type = die_type (die, cu);
17223
17224 /* The die_type call above may have already set the type for this DIE. */
17225 type = get_die_type (die, cu);
17226 if (type)
17227 return type;
17228
17229 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
17230 if (attr != NULL)
17231 {
17232 int stride_ok;
17233 struct type *prop_type = cu->addr_sized_int_type (false);
17234
17235 byte_stride_prop
17236 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
17237 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
17238 prop_type);
17239 if (!stride_ok)
17240 {
17241 complaint (_("unable to read array DW_AT_byte_stride "
17242 " - DIE at %s [in module %s]"),
17243 sect_offset_str (die->sect_off),
17244 objfile_name (cu->per_objfile->objfile));
17245 /* Ignore this attribute. We will likely not be able to print
17246 arrays of this type correctly, but there is little we can do
17247 to help if we cannot read the attribute's value. */
17248 byte_stride_prop = NULL;
17249 }
17250 }
17251
17252 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
17253 if (attr != NULL)
17254 bit_stride = attr->constant_value (0);
17255
17256 /* Irix 6.2 native cc creates array types without children for
17257 arrays with unspecified length. */
17258 if (die->child == NULL)
17259 {
17260 index_type = objfile_type (objfile)->builtin_int;
17261 range_type = create_static_range_type (NULL, index_type, 0, -1);
17262 type = create_array_type_with_stride (NULL, element_type, range_type,
17263 byte_stride_prop, bit_stride);
17264 return set_die_type (die, type, cu);
17265 }
17266
17267 std::vector<struct type *> range_types;
17268 child_die = die->child;
17269 while (child_die && child_die->tag)
17270 {
17271 if (child_die->tag == DW_TAG_subrange_type)
17272 {
17273 struct type *child_type = read_type_die (child_die, cu);
17274
17275 if (child_type != NULL)
17276 {
17277 /* The range type was succesfully read. Save it for the
17278 array type creation. */
17279 range_types.push_back (child_type);
17280 }
17281 }
17282 child_die = child_die->sibling;
17283 }
17284
17285 if (range_types.empty ())
17286 {
17287 complaint (_("unable to find array range - DIE at %s [in module %s]"),
17288 sect_offset_str (die->sect_off),
17289 objfile_name (cu->per_objfile->objfile));
17290 return NULL;
17291 }
17292
17293 /* Dwarf2 dimensions are output from left to right, create the
17294 necessary array types in backwards order. */
17295
17296 type = element_type;
17297
17298 if (read_array_order (die, cu) == DW_ORD_col_major)
17299 {
17300 int i = 0;
17301
17302 while (i < range_types.size ())
17303 {
17304 type = create_array_type_with_stride (NULL, type, range_types[i++],
17305 byte_stride_prop, bit_stride);
17306 bit_stride = 0;
17307 byte_stride_prop = nullptr;
17308 }
17309 }
17310 else
17311 {
17312 size_t ndim = range_types.size ();
17313 while (ndim-- > 0)
17314 {
17315 type = create_array_type_with_stride (NULL, type, range_types[ndim],
17316 byte_stride_prop, bit_stride);
17317 bit_stride = 0;
17318 byte_stride_prop = nullptr;
17319 }
17320 }
17321
17322 gdb_assert (type != element_type);
17323
17324 /* Understand Dwarf2 support for vector types (like they occur on
17325 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
17326 array type. This is not part of the Dwarf2/3 standard yet, but a
17327 custom vendor extension. The main difference between a regular
17328 array and the vector variant is that vectors are passed by value
17329 to functions. */
17330 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
17331 if (attr != nullptr)
17332 make_vector_type (type);
17333
17334 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
17335 implementation may choose to implement triple vectors using this
17336 attribute. */
17337 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17338 if (attr != nullptr && attr->form_is_unsigned ())
17339 {
17340 if (attr->as_unsigned () >= TYPE_LENGTH (type))
17341 TYPE_LENGTH (type) = attr->as_unsigned ();
17342 else
17343 complaint (_("DW_AT_byte_size for array type smaller "
17344 "than the total size of elements"));
17345 }
17346
17347 name = dwarf2_name (die, cu);
17348 if (name)
17349 type->set_name (name);
17350
17351 maybe_set_alignment (cu, die, type);
17352
17353 struct type *replacement_type = nullptr;
17354 if (cu->language == language_ada)
17355 {
17356 replacement_type = quirk_ada_thick_pointer (die, cu, type);
17357 if (replacement_type != nullptr)
17358 type = replacement_type;
17359 }
17360
17361 /* Install the type in the die. */
17362 set_die_type (die, type, cu, replacement_type != nullptr);
17363
17364 /* set_die_type should be already done. */
17365 set_descriptive_type (type, die, cu);
17366
17367 return type;
17368 }
17369
17370 static enum dwarf_array_dim_ordering
17371 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
17372 {
17373 struct attribute *attr;
17374
17375 attr = dwarf2_attr (die, DW_AT_ordering, cu);
17376
17377 if (attr != nullptr)
17378 {
17379 LONGEST val = attr->constant_value (-1);
17380 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
17381 return (enum dwarf_array_dim_ordering) val;
17382 }
17383
17384 /* GNU F77 is a special case, as at 08/2004 array type info is the
17385 opposite order to the dwarf2 specification, but data is still
17386 laid out as per normal fortran.
17387
17388 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
17389 version checking. */
17390
17391 if (cu->language == language_fortran
17392 && cu->producer && strstr (cu->producer, "GNU F77"))
17393 {
17394 return DW_ORD_row_major;
17395 }
17396
17397 switch (cu->language_defn->array_ordering ())
17398 {
17399 case array_column_major:
17400 return DW_ORD_col_major;
17401 case array_row_major:
17402 default:
17403 return DW_ORD_row_major;
17404 };
17405 }
17406
17407 /* Extract all information from a DW_TAG_set_type DIE and put it in
17408 the DIE's type field. */
17409
17410 static struct type *
17411 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
17412 {
17413 struct type *domain_type, *set_type;
17414 struct attribute *attr;
17415
17416 domain_type = die_type (die, cu);
17417
17418 /* The die_type call above may have already set the type for this DIE. */
17419 set_type = get_die_type (die, cu);
17420 if (set_type)
17421 return set_type;
17422
17423 set_type = create_set_type (NULL, domain_type);
17424
17425 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17426 if (attr != nullptr && attr->form_is_unsigned ())
17427 TYPE_LENGTH (set_type) = attr->as_unsigned ();
17428
17429 maybe_set_alignment (cu, die, set_type);
17430
17431 return set_die_type (die, set_type, cu);
17432 }
17433
17434 /* A helper for read_common_block that creates a locexpr baton.
17435 SYM is the symbol which we are marking as computed.
17436 COMMON_DIE is the DIE for the common block.
17437 COMMON_LOC is the location expression attribute for the common
17438 block itself.
17439 MEMBER_LOC is the location expression attribute for the particular
17440 member of the common block that we are processing.
17441 CU is the CU from which the above come. */
17442
17443 static void
17444 mark_common_block_symbol_computed (struct symbol *sym,
17445 struct die_info *common_die,
17446 struct attribute *common_loc,
17447 struct attribute *member_loc,
17448 struct dwarf2_cu *cu)
17449 {
17450 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17451 struct objfile *objfile = per_objfile->objfile;
17452 struct dwarf2_locexpr_baton *baton;
17453 gdb_byte *ptr;
17454 unsigned int cu_off;
17455 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
17456 LONGEST offset = 0;
17457
17458 gdb_assert (common_loc && member_loc);
17459 gdb_assert (common_loc->form_is_block ());
17460 gdb_assert (member_loc->form_is_block ()
17461 || member_loc->form_is_constant ());
17462
17463 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
17464 baton->per_objfile = per_objfile;
17465 baton->per_cu = cu->per_cu;
17466 gdb_assert (baton->per_cu);
17467
17468 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17469
17470 if (member_loc->form_is_constant ())
17471 {
17472 offset = member_loc->constant_value (0);
17473 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17474 }
17475 else
17476 baton->size += member_loc->as_block ()->size;
17477
17478 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
17479 baton->data = ptr;
17480
17481 *ptr++ = DW_OP_call4;
17482 cu_off = common_die->sect_off - cu->per_cu->sect_off;
17483 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17484 ptr += 4;
17485
17486 if (member_loc->form_is_constant ())
17487 {
17488 *ptr++ = DW_OP_addr;
17489 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17490 ptr += cu->header.addr_size;
17491 }
17492 else
17493 {
17494 /* We have to copy the data here, because DW_OP_call4 will only
17495 use a DW_AT_location attribute. */
17496 struct dwarf_block *block = member_loc->as_block ();
17497 memcpy (ptr, block->data, block->size);
17498 ptr += block->size;
17499 }
17500
17501 *ptr++ = DW_OP_plus;
17502 gdb_assert (ptr - baton->data == baton->size);
17503
17504 SYMBOL_LOCATION_BATON (sym) = baton;
17505 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
17506 }
17507
17508 /* Create appropriate locally-scoped variables for all the
17509 DW_TAG_common_block entries. Also create a struct common_block
17510 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
17511 is used to separate the common blocks name namespace from regular
17512 variable names. */
17513
17514 static void
17515 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
17516 {
17517 struct attribute *attr;
17518
17519 attr = dwarf2_attr (die, DW_AT_location, cu);
17520 if (attr != nullptr)
17521 {
17522 /* Support the .debug_loc offsets. */
17523 if (attr->form_is_block ())
17524 {
17525 /* Ok. */
17526 }
17527 else if (attr->form_is_section_offset ())
17528 {
17529 dwarf2_complex_location_expr_complaint ();
17530 attr = NULL;
17531 }
17532 else
17533 {
17534 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17535 "common block member");
17536 attr = NULL;
17537 }
17538 }
17539
17540 if (die->child != NULL)
17541 {
17542 struct objfile *objfile = cu->per_objfile->objfile;
17543 struct die_info *child_die;
17544 size_t n_entries = 0, size;
17545 struct common_block *common_block;
17546 struct symbol *sym;
17547
17548 for (child_die = die->child;
17549 child_die && child_die->tag;
17550 child_die = child_die->sibling)
17551 ++n_entries;
17552
17553 size = (sizeof (struct common_block)
17554 + (n_entries - 1) * sizeof (struct symbol *));
17555 common_block
17556 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17557 size);
17558 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17559 common_block->n_entries = 0;
17560
17561 for (child_die = die->child;
17562 child_die && child_die->tag;
17563 child_die = child_die->sibling)
17564 {
17565 /* Create the symbol in the DW_TAG_common_block block in the current
17566 symbol scope. */
17567 sym = new_symbol (child_die, NULL, cu);
17568 if (sym != NULL)
17569 {
17570 struct attribute *member_loc;
17571
17572 common_block->contents[common_block->n_entries++] = sym;
17573
17574 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17575 cu);
17576 if (member_loc)
17577 {
17578 /* GDB has handled this for a long time, but it is
17579 not specified by DWARF. It seems to have been
17580 emitted by gfortran at least as recently as:
17581 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17582 complaint (_("Variable in common block has "
17583 "DW_AT_data_member_location "
17584 "- DIE at %s [in module %s]"),
17585 sect_offset_str (child_die->sect_off),
17586 objfile_name (objfile));
17587
17588 if (member_loc->form_is_section_offset ())
17589 dwarf2_complex_location_expr_complaint ();
17590 else if (member_loc->form_is_constant ()
17591 || member_loc->form_is_block ())
17592 {
17593 if (attr != nullptr)
17594 mark_common_block_symbol_computed (sym, die, attr,
17595 member_loc, cu);
17596 }
17597 else
17598 dwarf2_complex_location_expr_complaint ();
17599 }
17600 }
17601 }
17602
17603 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17604 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
17605 }
17606 }
17607
17608 /* Create a type for a C++ namespace. */
17609
17610 static struct type *
17611 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
17612 {
17613 struct objfile *objfile = cu->per_objfile->objfile;
17614 const char *previous_prefix, *name;
17615 int is_anonymous;
17616 struct type *type;
17617
17618 /* For extensions, reuse the type of the original namespace. */
17619 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17620 {
17621 struct die_info *ext_die;
17622 struct dwarf2_cu *ext_cu = cu;
17623
17624 ext_die = dwarf2_extension (die, &ext_cu);
17625 type = read_type_die (ext_die, ext_cu);
17626
17627 /* EXT_CU may not be the same as CU.
17628 Ensure TYPE is recorded with CU in die_type_hash. */
17629 return set_die_type (die, type, cu);
17630 }
17631
17632 name = namespace_name (die, &is_anonymous, cu);
17633
17634 /* Now build the name of the current namespace. */
17635
17636 previous_prefix = determine_prefix (die, cu);
17637 if (previous_prefix[0] != '\0')
17638 name = typename_concat (&objfile->objfile_obstack,
17639 previous_prefix, name, 0, cu);
17640
17641 /* Create the type. */
17642 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17643
17644 return set_die_type (die, type, cu);
17645 }
17646
17647 /* Read a namespace scope. */
17648
17649 static void
17650 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17651 {
17652 struct objfile *objfile = cu->per_objfile->objfile;
17653 int is_anonymous;
17654
17655 /* Add a symbol associated to this if we haven't seen the namespace
17656 before. Also, add a using directive if it's an anonymous
17657 namespace. */
17658
17659 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17660 {
17661 struct type *type;
17662
17663 type = read_type_die (die, cu);
17664 new_symbol (die, type, cu);
17665
17666 namespace_name (die, &is_anonymous, cu);
17667 if (is_anonymous)
17668 {
17669 const char *previous_prefix = determine_prefix (die, cu);
17670
17671 std::vector<const char *> excludes;
17672 add_using_directive (using_directives (cu),
17673 previous_prefix, type->name (), NULL,
17674 NULL, excludes, 0, &objfile->objfile_obstack);
17675 }
17676 }
17677
17678 if (die->child != NULL)
17679 {
17680 struct die_info *child_die = die->child;
17681
17682 while (child_die && child_die->tag)
17683 {
17684 process_die (child_die, cu);
17685 child_die = child_die->sibling;
17686 }
17687 }
17688 }
17689
17690 /* Read a Fortran module as type. This DIE can be only a declaration used for
17691 imported module. Still we need that type as local Fortran "use ... only"
17692 declaration imports depend on the created type in determine_prefix. */
17693
17694 static struct type *
17695 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17696 {
17697 struct objfile *objfile = cu->per_objfile->objfile;
17698 const char *module_name;
17699 struct type *type;
17700
17701 module_name = dwarf2_name (die, cu);
17702 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17703
17704 return set_die_type (die, type, cu);
17705 }
17706
17707 /* Read a Fortran module. */
17708
17709 static void
17710 read_module (struct die_info *die, struct dwarf2_cu *cu)
17711 {
17712 struct die_info *child_die = die->child;
17713 struct type *type;
17714
17715 type = read_type_die (die, cu);
17716 new_symbol (die, type, cu);
17717
17718 while (child_die && child_die->tag)
17719 {
17720 process_die (child_die, cu);
17721 child_die = child_die->sibling;
17722 }
17723 }
17724
17725 /* Return the name of the namespace represented by DIE. Set
17726 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17727 namespace. */
17728
17729 static const char *
17730 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17731 {
17732 struct die_info *current_die;
17733 const char *name = NULL;
17734
17735 /* Loop through the extensions until we find a name. */
17736
17737 for (current_die = die;
17738 current_die != NULL;
17739 current_die = dwarf2_extension (die, &cu))
17740 {
17741 /* We don't use dwarf2_name here so that we can detect the absence
17742 of a name -> anonymous namespace. */
17743 name = dwarf2_string_attr (die, DW_AT_name, cu);
17744
17745 if (name != NULL)
17746 break;
17747 }
17748
17749 /* Is it an anonymous namespace? */
17750
17751 *is_anonymous = (name == NULL);
17752 if (*is_anonymous)
17753 name = CP_ANONYMOUS_NAMESPACE_STR;
17754
17755 return name;
17756 }
17757
17758 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17759 the user defined type vector. */
17760
17761 static struct type *
17762 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17763 {
17764 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
17765 struct comp_unit_head *cu_header = &cu->header;
17766 struct type *type;
17767 struct attribute *attr_byte_size;
17768 struct attribute *attr_address_class;
17769 int byte_size, addr_class;
17770 struct type *target_type;
17771
17772 target_type = die_type (die, cu);
17773
17774 /* The die_type call above may have already set the type for this DIE. */
17775 type = get_die_type (die, cu);
17776 if (type)
17777 return type;
17778
17779 type = lookup_pointer_type (target_type);
17780
17781 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17782 if (attr_byte_size)
17783 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
17784 else
17785 byte_size = cu_header->addr_size;
17786
17787 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17788 if (attr_address_class)
17789 addr_class = attr_address_class->constant_value (DW_ADDR_none);
17790 else
17791 addr_class = DW_ADDR_none;
17792
17793 ULONGEST alignment = get_alignment (cu, die);
17794
17795 /* If the pointer size, alignment, or address class is different
17796 than the default, create a type variant marked as such and set
17797 the length accordingly. */
17798 if (TYPE_LENGTH (type) != byte_size
17799 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17800 && alignment != TYPE_RAW_ALIGN (type))
17801 || addr_class != DW_ADDR_none)
17802 {
17803 if (gdbarch_address_class_type_flags_p (gdbarch))
17804 {
17805 type_instance_flags type_flags
17806 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17807 addr_class);
17808 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17809 == 0);
17810 type = make_type_with_address_space (type, type_flags);
17811 }
17812 else if (TYPE_LENGTH (type) != byte_size)
17813 {
17814 complaint (_("invalid pointer size %d"), byte_size);
17815 }
17816 else if (TYPE_RAW_ALIGN (type) != alignment)
17817 {
17818 complaint (_("Invalid DW_AT_alignment"
17819 " - DIE at %s [in module %s]"),
17820 sect_offset_str (die->sect_off),
17821 objfile_name (cu->per_objfile->objfile));
17822 }
17823 else
17824 {
17825 /* Should we also complain about unhandled address classes? */
17826 }
17827 }
17828
17829 TYPE_LENGTH (type) = byte_size;
17830 set_type_align (type, alignment);
17831 return set_die_type (die, type, cu);
17832 }
17833
17834 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17835 the user defined type vector. */
17836
17837 static struct type *
17838 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17839 {
17840 struct type *type;
17841 struct type *to_type;
17842 struct type *domain;
17843
17844 to_type = die_type (die, cu);
17845 domain = die_containing_type (die, cu);
17846
17847 /* The calls above may have already set the type for this DIE. */
17848 type = get_die_type (die, cu);
17849 if (type)
17850 return type;
17851
17852 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17853 type = lookup_methodptr_type (to_type);
17854 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17855 {
17856 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17857
17858 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17859 to_type->fields (), to_type->num_fields (),
17860 to_type->has_varargs ());
17861 type = lookup_methodptr_type (new_type);
17862 }
17863 else
17864 type = lookup_memberptr_type (to_type, domain);
17865
17866 return set_die_type (die, type, cu);
17867 }
17868
17869 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17870 the user defined type vector. */
17871
17872 static struct type *
17873 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17874 enum type_code refcode)
17875 {
17876 struct comp_unit_head *cu_header = &cu->header;
17877 struct type *type, *target_type;
17878 struct attribute *attr;
17879
17880 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17881
17882 target_type = die_type (die, cu);
17883
17884 /* The die_type call above may have already set the type for this DIE. */
17885 type = get_die_type (die, cu);
17886 if (type)
17887 return type;
17888
17889 type = lookup_reference_type (target_type, refcode);
17890 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17891 if (attr != nullptr)
17892 {
17893 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
17894 }
17895 else
17896 {
17897 TYPE_LENGTH (type) = cu_header->addr_size;
17898 }
17899 maybe_set_alignment (cu, die, type);
17900 return set_die_type (die, type, cu);
17901 }
17902
17903 /* Add the given cv-qualifiers to the element type of the array. GCC
17904 outputs DWARF type qualifiers that apply to an array, not the
17905 element type. But GDB relies on the array element type to carry
17906 the cv-qualifiers. This mimics section 6.7.3 of the C99
17907 specification. */
17908
17909 static struct type *
17910 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17911 struct type *base_type, int cnst, int voltl)
17912 {
17913 struct type *el_type, *inner_array;
17914
17915 base_type = copy_type (base_type);
17916 inner_array = base_type;
17917
17918 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17919 {
17920 TYPE_TARGET_TYPE (inner_array) =
17921 copy_type (TYPE_TARGET_TYPE (inner_array));
17922 inner_array = TYPE_TARGET_TYPE (inner_array);
17923 }
17924
17925 el_type = TYPE_TARGET_TYPE (inner_array);
17926 cnst |= TYPE_CONST (el_type);
17927 voltl |= TYPE_VOLATILE (el_type);
17928 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17929
17930 return set_die_type (die, base_type, cu);
17931 }
17932
17933 static struct type *
17934 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17935 {
17936 struct type *base_type, *cv_type;
17937
17938 base_type = die_type (die, cu);
17939
17940 /* The die_type call above may have already set the type for this DIE. */
17941 cv_type = get_die_type (die, cu);
17942 if (cv_type)
17943 return cv_type;
17944
17945 /* In case the const qualifier is applied to an array type, the element type
17946 is so qualified, not the array type (section 6.7.3 of C99). */
17947 if (base_type->code () == TYPE_CODE_ARRAY)
17948 return add_array_cv_type (die, cu, base_type, 1, 0);
17949
17950 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17951 return set_die_type (die, cv_type, cu);
17952 }
17953
17954 static struct type *
17955 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17956 {
17957 struct type *base_type, *cv_type;
17958
17959 base_type = die_type (die, cu);
17960
17961 /* The die_type call above may have already set the type for this DIE. */
17962 cv_type = get_die_type (die, cu);
17963 if (cv_type)
17964 return cv_type;
17965
17966 /* In case the volatile qualifier is applied to an array type, the
17967 element type is so qualified, not the array type (section 6.7.3
17968 of C99). */
17969 if (base_type->code () == TYPE_CODE_ARRAY)
17970 return add_array_cv_type (die, cu, base_type, 0, 1);
17971
17972 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17973 return set_die_type (die, cv_type, cu);
17974 }
17975
17976 /* Handle DW_TAG_restrict_type. */
17977
17978 static struct type *
17979 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17980 {
17981 struct type *base_type, *cv_type;
17982
17983 base_type = die_type (die, cu);
17984
17985 /* The die_type call above may have already set the type for this DIE. */
17986 cv_type = get_die_type (die, cu);
17987 if (cv_type)
17988 return cv_type;
17989
17990 cv_type = make_restrict_type (base_type);
17991 return set_die_type (die, cv_type, cu);
17992 }
17993
17994 /* Handle DW_TAG_atomic_type. */
17995
17996 static struct type *
17997 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17998 {
17999 struct type *base_type, *cv_type;
18000
18001 base_type = die_type (die, cu);
18002
18003 /* The die_type call above may have already set the type for this DIE. */
18004 cv_type = get_die_type (die, cu);
18005 if (cv_type)
18006 return cv_type;
18007
18008 cv_type = make_atomic_type (base_type);
18009 return set_die_type (die, cv_type, cu);
18010 }
18011
18012 /* Extract all information from a DW_TAG_string_type DIE and add to
18013 the user defined type vector. It isn't really a user defined type,
18014 but it behaves like one, with other DIE's using an AT_user_def_type
18015 attribute to reference it. */
18016
18017 static struct type *
18018 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
18019 {
18020 struct objfile *objfile = cu->per_objfile->objfile;
18021 struct gdbarch *gdbarch = objfile->arch ();
18022 struct type *type, *range_type, *index_type, *char_type;
18023 struct attribute *attr;
18024 struct dynamic_prop prop;
18025 bool length_is_constant = true;
18026 LONGEST length;
18027
18028 /* There are a couple of places where bit sizes might be made use of
18029 when parsing a DW_TAG_string_type, however, no producer that we know
18030 of make use of these. Handling bit sizes that are a multiple of the
18031 byte size is easy enough, but what about other bit sizes? Lets deal
18032 with that problem when we have to. Warn about these attributes being
18033 unsupported, then parse the type and ignore them like we always
18034 have. */
18035 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
18036 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
18037 {
18038 static bool warning_printed = false;
18039 if (!warning_printed)
18040 {
18041 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
18042 "currently supported on DW_TAG_string_type."));
18043 warning_printed = true;
18044 }
18045 }
18046
18047 attr = dwarf2_attr (die, DW_AT_string_length, cu);
18048 if (attr != nullptr && !attr->form_is_constant ())
18049 {
18050 /* The string length describes the location at which the length of
18051 the string can be found. The size of the length field can be
18052 specified with one of the attributes below. */
18053 struct type *prop_type;
18054 struct attribute *len
18055 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
18056 if (len == nullptr)
18057 len = dwarf2_attr (die, DW_AT_byte_size, cu);
18058 if (len != nullptr && len->form_is_constant ())
18059 {
18060 /* Pass 0 as the default as we know this attribute is constant
18061 and the default value will not be returned. */
18062 LONGEST sz = len->constant_value (0);
18063 prop_type = cu->per_objfile->int_type (sz, true);
18064 }
18065 else
18066 {
18067 /* If the size is not specified then we assume it is the size of
18068 an address on this target. */
18069 prop_type = cu->addr_sized_int_type (true);
18070 }
18071
18072 /* Convert the attribute into a dynamic property. */
18073 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
18074 length = 1;
18075 else
18076 length_is_constant = false;
18077 }
18078 else if (attr != nullptr)
18079 {
18080 /* This DW_AT_string_length just contains the length with no
18081 indirection. There's no need to create a dynamic property in this
18082 case. Pass 0 for the default value as we know it will not be
18083 returned in this case. */
18084 length = attr->constant_value (0);
18085 }
18086 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
18087 {
18088 /* We don't currently support non-constant byte sizes for strings. */
18089 length = attr->constant_value (1);
18090 }
18091 else
18092 {
18093 /* Use 1 as a fallback length if we have nothing else. */
18094 length = 1;
18095 }
18096
18097 index_type = objfile_type (objfile)->builtin_int;
18098 if (length_is_constant)
18099 range_type = create_static_range_type (NULL, index_type, 1, length);
18100 else
18101 {
18102 struct dynamic_prop low_bound;
18103
18104 low_bound.set_const_val (1);
18105 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
18106 }
18107 char_type = language_string_char_type (cu->language_defn, gdbarch);
18108 type = create_string_type (NULL, char_type, range_type);
18109
18110 return set_die_type (die, type, cu);
18111 }
18112
18113 /* Assuming that DIE corresponds to a function, returns nonzero
18114 if the function is prototyped. */
18115
18116 static int
18117 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
18118 {
18119 struct attribute *attr;
18120
18121 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
18122 if (attr && attr->as_boolean ())
18123 return 1;
18124
18125 /* The DWARF standard implies that the DW_AT_prototyped attribute
18126 is only meaningful for C, but the concept also extends to other
18127 languages that allow unprototyped functions (Eg: Objective C).
18128 For all other languages, assume that functions are always
18129 prototyped. */
18130 if (cu->language != language_c
18131 && cu->language != language_objc
18132 && cu->language != language_opencl)
18133 return 1;
18134
18135 /* RealView does not emit DW_AT_prototyped. We can not distinguish
18136 prototyped and unprototyped functions; default to prototyped,
18137 since that is more common in modern code (and RealView warns
18138 about unprototyped functions). */
18139 if (producer_is_realview (cu->producer))
18140 return 1;
18141
18142 return 0;
18143 }
18144
18145 /* Handle DIES due to C code like:
18146
18147 struct foo
18148 {
18149 int (*funcp)(int a, long l);
18150 int b;
18151 };
18152
18153 ('funcp' generates a DW_TAG_subroutine_type DIE). */
18154
18155 static struct type *
18156 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
18157 {
18158 struct objfile *objfile = cu->per_objfile->objfile;
18159 struct type *type; /* Type that this function returns. */
18160 struct type *ftype; /* Function that returns above type. */
18161 struct attribute *attr;
18162
18163 type = die_type (die, cu);
18164
18165 /* The die_type call above may have already set the type for this DIE. */
18166 ftype = get_die_type (die, cu);
18167 if (ftype)
18168 return ftype;
18169
18170 ftype = lookup_function_type (type);
18171
18172 if (prototyped_function_p (die, cu))
18173 ftype->set_is_prototyped (true);
18174
18175 /* Store the calling convention in the type if it's available in
18176 the subroutine die. Otherwise set the calling convention to
18177 the default value DW_CC_normal. */
18178 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
18179 if (attr != nullptr
18180 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
18181 TYPE_CALLING_CONVENTION (ftype)
18182 = (enum dwarf_calling_convention) attr->constant_value (0);
18183 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
18184 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
18185 else
18186 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
18187
18188 /* Record whether the function returns normally to its caller or not
18189 if the DWARF producer set that information. */
18190 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
18191 if (attr && attr->as_boolean ())
18192 TYPE_NO_RETURN (ftype) = 1;
18193
18194 /* We need to add the subroutine type to the die immediately so
18195 we don't infinitely recurse when dealing with parameters
18196 declared as the same subroutine type. */
18197 set_die_type (die, ftype, cu);
18198
18199 if (die->child != NULL)
18200 {
18201 struct type *void_type = objfile_type (objfile)->builtin_void;
18202 struct die_info *child_die;
18203 int nparams, iparams;
18204
18205 /* Count the number of parameters.
18206 FIXME: GDB currently ignores vararg functions, but knows about
18207 vararg member functions. */
18208 nparams = 0;
18209 child_die = die->child;
18210 while (child_die && child_die->tag)
18211 {
18212 if (child_die->tag == DW_TAG_formal_parameter)
18213 nparams++;
18214 else if (child_die->tag == DW_TAG_unspecified_parameters)
18215 ftype->set_has_varargs (true);
18216
18217 child_die = child_die->sibling;
18218 }
18219
18220 /* Allocate storage for parameters and fill them in. */
18221 ftype->set_num_fields (nparams);
18222 ftype->set_fields
18223 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
18224
18225 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
18226 even if we error out during the parameters reading below. */
18227 for (iparams = 0; iparams < nparams; iparams++)
18228 ftype->field (iparams).set_type (void_type);
18229
18230 iparams = 0;
18231 child_die = die->child;
18232 while (child_die && child_die->tag)
18233 {
18234 if (child_die->tag == DW_TAG_formal_parameter)
18235 {
18236 struct type *arg_type;
18237
18238 /* DWARF version 2 has no clean way to discern C++
18239 static and non-static member functions. G++ helps
18240 GDB by marking the first parameter for non-static
18241 member functions (which is the this pointer) as
18242 artificial. We pass this information to
18243 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
18244
18245 DWARF version 3 added DW_AT_object_pointer, which GCC
18246 4.5 does not yet generate. */
18247 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
18248 if (attr != nullptr)
18249 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
18250 else
18251 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
18252 arg_type = die_type (child_die, cu);
18253
18254 /* RealView does not mark THIS as const, which the testsuite
18255 expects. GCC marks THIS as const in method definitions,
18256 but not in the class specifications (GCC PR 43053). */
18257 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
18258 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
18259 {
18260 int is_this = 0;
18261 struct dwarf2_cu *arg_cu = cu;
18262 const char *name = dwarf2_name (child_die, cu);
18263
18264 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
18265 if (attr != nullptr)
18266 {
18267 /* If the compiler emits this, use it. */
18268 if (follow_die_ref (die, attr, &arg_cu) == child_die)
18269 is_this = 1;
18270 }
18271 else if (name && strcmp (name, "this") == 0)
18272 /* Function definitions will have the argument names. */
18273 is_this = 1;
18274 else if (name == NULL && iparams == 0)
18275 /* Declarations may not have the names, so like
18276 elsewhere in GDB, assume an artificial first
18277 argument is "this". */
18278 is_this = 1;
18279
18280 if (is_this)
18281 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
18282 arg_type, 0);
18283 }
18284
18285 ftype->field (iparams).set_type (arg_type);
18286 iparams++;
18287 }
18288 child_die = child_die->sibling;
18289 }
18290 }
18291
18292 return ftype;
18293 }
18294
18295 static struct type *
18296 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
18297 {
18298 struct objfile *objfile = cu->per_objfile->objfile;
18299 const char *name = NULL;
18300 struct type *this_type, *target_type;
18301
18302 name = dwarf2_full_name (NULL, die, cu);
18303 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
18304 this_type->set_target_is_stub (true);
18305 set_die_type (die, this_type, cu);
18306 target_type = die_type (die, cu);
18307 if (target_type != this_type)
18308 TYPE_TARGET_TYPE (this_type) = target_type;
18309 else
18310 {
18311 /* Self-referential typedefs are, it seems, not allowed by the DWARF
18312 spec and cause infinite loops in GDB. */
18313 complaint (_("Self-referential DW_TAG_typedef "
18314 "- DIE at %s [in module %s]"),
18315 sect_offset_str (die->sect_off), objfile_name (objfile));
18316 TYPE_TARGET_TYPE (this_type) = NULL;
18317 }
18318 if (name == NULL)
18319 {
18320 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
18321 anonymous typedefs, which is, strictly speaking, invalid DWARF.
18322 Handle these by just returning the target type, rather than
18323 constructing an anonymous typedef type and trying to handle this
18324 elsewhere. */
18325 set_die_type (die, target_type, cu);
18326 return target_type;
18327 }
18328 return this_type;
18329 }
18330
18331 /* Helper for get_dwarf2_rational_constant that computes the value of
18332 a given gmp_mpz given an attribute. */
18333
18334 static void
18335 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
18336 {
18337 /* GCC will sometimes emit a 16-byte constant value as a DWARF
18338 location expression that pushes an implicit value. */
18339 if (attr->form == DW_FORM_exprloc)
18340 {
18341 dwarf_block *blk = attr->as_block ();
18342 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
18343 {
18344 uint64_t len;
18345 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
18346 blk->data + blk->size,
18347 &len);
18348 if (ptr - blk->data + len <= blk->size)
18349 {
18350 mpz_import (value->val, len,
18351 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18352 1, 0, 0, ptr);
18353 return;
18354 }
18355 }
18356
18357 /* On failure set it to 1. */
18358 *value = gdb_mpz (1);
18359 }
18360 else if (attr->form_is_block ())
18361 {
18362 dwarf_block *blk = attr->as_block ();
18363 mpz_import (value->val, blk->size,
18364 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18365 1, 0, 0, blk->data);
18366 }
18367 else
18368 *value = gdb_mpz (attr->constant_value (1));
18369 }
18370
18371 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
18372 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
18373
18374 If the numerator and/or numerator attribute is missing,
18375 a complaint is filed, and NUMERATOR and DENOMINATOR are left
18376 untouched. */
18377
18378 static void
18379 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
18380 gdb_mpz *numerator, gdb_mpz *denominator)
18381 {
18382 struct attribute *num_attr, *denom_attr;
18383
18384 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
18385 if (num_attr == nullptr)
18386 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
18387 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18388
18389 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
18390 if (denom_attr == nullptr)
18391 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
18392 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18393
18394 if (num_attr == nullptr || denom_attr == nullptr)
18395 return;
18396
18397 get_mpz (cu, numerator, num_attr);
18398 get_mpz (cu, denominator, denom_attr);
18399 }
18400
18401 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
18402 rational constant, rather than a signed one.
18403
18404 If the rational constant has a negative value, a complaint
18405 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
18406
18407 static void
18408 get_dwarf2_unsigned_rational_constant (struct die_info *die,
18409 struct dwarf2_cu *cu,
18410 gdb_mpz *numerator,
18411 gdb_mpz *denominator)
18412 {
18413 gdb_mpz num (1);
18414 gdb_mpz denom (1);
18415
18416 get_dwarf2_rational_constant (die, cu, &num, &denom);
18417 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
18418 {
18419 mpz_neg (num.val, num.val);
18420 mpz_neg (denom.val, denom.val);
18421 }
18422 else if (mpz_sgn (num.val) == -1)
18423 {
18424 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
18425 " in DIE at %s"),
18426 sect_offset_str (die->sect_off));
18427 return;
18428 }
18429 else if (mpz_sgn (denom.val) == -1)
18430 {
18431 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
18432 " in DIE at %s"),
18433 sect_offset_str (die->sect_off));
18434 return;
18435 }
18436
18437 *numerator = std::move (num);
18438 *denominator = std::move (denom);
18439 }
18440
18441 /* Assuming that ENCODING is a string whose contents starting at the
18442 K'th character is "_nn" where "nn" is a decimal number, scan that
18443 number and set RESULT to the value. K is updated to point to the
18444 character immediately following the number.
18445
18446 If the string does not conform to the format described above, false
18447 is returned, and K may or may not be changed. */
18448
18449 static bool
18450 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
18451 {
18452 /* The next character should be an underscore ('_') followed
18453 by a digit. */
18454 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
18455 return false;
18456
18457 /* Skip the underscore. */
18458 k++;
18459 int start = k;
18460
18461 /* Determine the number of digits for our number. */
18462 while (isdigit (encoding[k]))
18463 k++;
18464 if (k == start)
18465 return false;
18466
18467 std::string copy (&encoding[start], k - start);
18468 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
18469 return false;
18470
18471 return true;
18472 }
18473
18474 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
18475 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
18476 DENOM, update OFFSET, and return true on success. Return false on
18477 failure. */
18478
18479 static bool
18480 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
18481 gdb_mpz *num, gdb_mpz *denom)
18482 {
18483 if (!ada_get_gnat_encoded_number (encoding, offset, num))
18484 return false;
18485 return ada_get_gnat_encoded_number (encoding, offset, denom);
18486 }
18487
18488 /* Assuming DIE corresponds to a fixed point type, finish the creation
18489 of the corresponding TYPE by setting its type-specific data. CU is
18490 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
18491 encodings. It is nullptr if the GNAT encoding should be
18492 ignored. */
18493
18494 static void
18495 finish_fixed_point_type (struct type *type, const char *suffix,
18496 struct die_info *die, struct dwarf2_cu *cu)
18497 {
18498 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
18499 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
18500
18501 /* If GNAT encodings are preferred, don't examine the
18502 attributes. */
18503 struct attribute *attr = nullptr;
18504 if (suffix == nullptr)
18505 {
18506 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
18507 if (attr == nullptr)
18508 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
18509 if (attr == nullptr)
18510 attr = dwarf2_attr (die, DW_AT_small, cu);
18511 }
18512
18513 /* Numerator and denominator of our fixed-point type's scaling factor.
18514 The default is a scaling factor of 1, which we use as a fallback
18515 when we are not able to decode it (problem with the debugging info,
18516 unsupported forms, bug in GDB, etc...). Using that as the default
18517 allows us to at least print the unscaled value, which might still
18518 be useful to a user. */
18519 gdb_mpz scale_num (1);
18520 gdb_mpz scale_denom (1);
18521
18522 if (attr == nullptr)
18523 {
18524 int offset = 0;
18525 if (suffix != nullptr
18526 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18527 &scale_denom)
18528 /* The number might be encoded as _nn_dd_nn_dd, where the
18529 second ratio is the 'small value. In this situation, we
18530 want the second value. */
18531 && (suffix[offset] != '_'
18532 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18533 &scale_denom)))
18534 {
18535 /* Found it. */
18536 }
18537 else
18538 {
18539 /* Scaling factor not found. Assume a scaling factor of 1,
18540 and hope for the best. At least the user will be able to
18541 see the encoded value. */
18542 scale_num = 1;
18543 scale_denom = 1;
18544 complaint (_("no scale found for fixed-point type (DIE at %s)"),
18545 sect_offset_str (die->sect_off));
18546 }
18547 }
18548 else if (attr->name == DW_AT_binary_scale)
18549 {
18550 LONGEST scale_exp = attr->constant_value (0);
18551 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18552
18553 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
18554 }
18555 else if (attr->name == DW_AT_decimal_scale)
18556 {
18557 LONGEST scale_exp = attr->constant_value (0);
18558 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18559
18560 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
18561 }
18562 else if (attr->name == DW_AT_small)
18563 {
18564 struct die_info *scale_die;
18565 struct dwarf2_cu *scale_cu = cu;
18566
18567 scale_die = follow_die_ref (die, attr, &scale_cu);
18568 if (scale_die->tag == DW_TAG_constant)
18569 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18570 &scale_num, &scale_denom);
18571 else
18572 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18573 " (DIE at %s)"),
18574 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18575 }
18576 else
18577 {
18578 complaint (_("unsupported scale attribute %s for fixed-point type"
18579 " (DIE at %s)"),
18580 dwarf_attr_name (attr->name),
18581 sect_offset_str (die->sect_off));
18582 }
18583
18584 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
18585 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
18586 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
18587 mpq_canonicalize (scaling_factor.val);
18588 }
18589
18590 /* The gnat-encoding suffix for fixed point. */
18591
18592 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
18593
18594 /* If NAME encodes an Ada fixed-point type, return a pointer to the
18595 "XF" suffix of the name. The text after this is what encodes the
18596 'small and 'delta information. Otherwise, return nullptr. */
18597
18598 static const char *
18599 gnat_encoded_fixed_point_type_info (const char *name)
18600 {
18601 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
18602 }
18603
18604 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18605 (which may be different from NAME) to the architecture back-end to allow
18606 it to guess the correct format if necessary. */
18607
18608 static struct type *
18609 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
18610 const char *name_hint, enum bfd_endian byte_order)
18611 {
18612 struct gdbarch *gdbarch = objfile->arch ();
18613 const struct floatformat **format;
18614 struct type *type;
18615
18616 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18617 if (format)
18618 type = init_float_type (objfile, bits, name, format, byte_order);
18619 else
18620 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18621
18622 return type;
18623 }
18624
18625 /* Allocate an integer type of size BITS and name NAME. */
18626
18627 static struct type *
18628 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18629 int bits, int unsigned_p, const char *name)
18630 {
18631 struct type *type;
18632
18633 /* Versions of Intel's C Compiler generate an integer type called "void"
18634 instead of using DW_TAG_unspecified_type. This has been seen on
18635 at least versions 14, 17, and 18. */
18636 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18637 && strcmp (name, "void") == 0)
18638 type = objfile_type (objfile)->builtin_void;
18639 else
18640 type = init_integer_type (objfile, bits, unsigned_p, name);
18641
18642 return type;
18643 }
18644
18645 /* Return true if DIE has a DW_AT_small attribute whose value is
18646 a constant rational, where both the numerator and denominator
18647 are equal to zero.
18648
18649 CU is the DIE's Compilation Unit. */
18650
18651 static bool
18652 has_zero_over_zero_small_attribute (struct die_info *die,
18653 struct dwarf2_cu *cu)
18654 {
18655 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18656 if (attr == nullptr)
18657 return false;
18658
18659 struct dwarf2_cu *scale_cu = cu;
18660 struct die_info *scale_die
18661 = follow_die_ref (die, attr, &scale_cu);
18662
18663 if (scale_die->tag != DW_TAG_constant)
18664 return false;
18665
18666 gdb_mpz num (1), denom (1);
18667 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
18668 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
18669 }
18670
18671 /* Initialise and return a floating point type of size BITS suitable for
18672 use as a component of a complex number. The NAME_HINT is passed through
18673 when initialising the floating point type and is the name of the complex
18674 type.
18675
18676 As DWARF doesn't currently provide an explicit name for the components
18677 of a complex number, but it can be helpful to have these components
18678 named, we try to select a suitable name based on the size of the
18679 component. */
18680 static struct type *
18681 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18682 struct objfile *objfile,
18683 int bits, const char *name_hint,
18684 enum bfd_endian byte_order)
18685 {
18686 gdbarch *gdbarch = objfile->arch ();
18687 struct type *tt = nullptr;
18688
18689 /* Try to find a suitable floating point builtin type of size BITS.
18690 We're going to use the name of this type as the name for the complex
18691 target type that we are about to create. */
18692 switch (cu->language)
18693 {
18694 case language_fortran:
18695 switch (bits)
18696 {
18697 case 32:
18698 tt = builtin_f_type (gdbarch)->builtin_real;
18699 break;
18700 case 64:
18701 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18702 break;
18703 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18704 case 128:
18705 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18706 break;
18707 }
18708 break;
18709 default:
18710 switch (bits)
18711 {
18712 case 32:
18713 tt = builtin_type (gdbarch)->builtin_float;
18714 break;
18715 case 64:
18716 tt = builtin_type (gdbarch)->builtin_double;
18717 break;
18718 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18719 case 128:
18720 tt = builtin_type (gdbarch)->builtin_long_double;
18721 break;
18722 }
18723 break;
18724 }
18725
18726 /* If the type we found doesn't match the size we were looking for, then
18727 pretend we didn't find a type at all, the complex target type we
18728 create will then be nameless. */
18729 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
18730 tt = nullptr;
18731
18732 const char *name = (tt == nullptr) ? nullptr : tt->name ();
18733 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
18734 }
18735
18736 /* Find a representation of a given base type and install
18737 it in the TYPE field of the die. */
18738
18739 static struct type *
18740 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
18741 {
18742 struct objfile *objfile = cu->per_objfile->objfile;
18743 struct type *type;
18744 struct attribute *attr;
18745 int encoding = 0, bits = 0;
18746 const char *name;
18747 gdbarch *arch;
18748
18749 attr = dwarf2_attr (die, DW_AT_encoding, cu);
18750 if (attr != nullptr && attr->form_is_constant ())
18751 encoding = attr->constant_value (0);
18752 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18753 if (attr != nullptr)
18754 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
18755 name = dwarf2_name (die, cu);
18756 if (!name)
18757 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
18758
18759 arch = objfile->arch ();
18760 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18761
18762 attr = dwarf2_attr (die, DW_AT_endianity, cu);
18763 if (attr != nullptr && attr->form_is_constant ())
18764 {
18765 int endianity = attr->constant_value (0);
18766
18767 switch (endianity)
18768 {
18769 case DW_END_big:
18770 byte_order = BFD_ENDIAN_BIG;
18771 break;
18772 case DW_END_little:
18773 byte_order = BFD_ENDIAN_LITTLE;
18774 break;
18775 default:
18776 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18777 break;
18778 }
18779 }
18780
18781 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18782 && cu->language == language_ada
18783 && has_zero_over_zero_small_attribute (die, cu))
18784 {
18785 /* brobecker/2018-02-24: This is a fixed point type for which
18786 the scaling factor is represented as fraction whose value
18787 does not make sense (zero divided by zero), so we should
18788 normally never see these. However, there is a small category
18789 of fixed point types for which GNAT is unable to provide
18790 the scaling factor via the standard DWARF mechanisms, and
18791 for which the info is provided via the GNAT encodings instead.
18792 This is likely what this DIE is about. */
18793 encoding = (encoding == DW_ATE_signed_fixed
18794 ? DW_ATE_signed
18795 : DW_ATE_unsigned);
18796 }
18797
18798 /* With GNAT encodings, fixed-point information will be encoded in
18799 the type name. Note that this can also occur with the above
18800 zero-over-zero case, which is why this is a separate "if" rather
18801 than an "else if". */
18802 const char *gnat_encoding_suffix = nullptr;
18803 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18804 && cu->language == language_ada
18805 && name != nullptr)
18806 {
18807 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18808 if (gnat_encoding_suffix != nullptr)
18809 {
18810 gdb_assert (startswith (gnat_encoding_suffix,
18811 GNAT_FIXED_POINT_SUFFIX));
18812 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18813 name, gnat_encoding_suffix - name);
18814 /* Use -1 here so that SUFFIX points at the "_" after the
18815 "XF". */
18816 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18817
18818 encoding = (encoding == DW_ATE_signed
18819 ? DW_ATE_signed_fixed
18820 : DW_ATE_unsigned_fixed);
18821 }
18822 }
18823
18824 switch (encoding)
18825 {
18826 case DW_ATE_address:
18827 /* Turn DW_ATE_address into a void * pointer. */
18828 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
18829 type = init_pointer_type (objfile, bits, name, type);
18830 break;
18831 case DW_ATE_boolean:
18832 type = init_boolean_type (objfile, bits, 1, name);
18833 break;
18834 case DW_ATE_complex_float:
18835 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18836 byte_order);
18837 if (type->code () == TYPE_CODE_ERROR)
18838 {
18839 if (name == nullptr)
18840 {
18841 struct obstack *obstack
18842 = &cu->per_objfile->objfile->objfile_obstack;
18843 name = obconcat (obstack, "_Complex ", type->name (),
18844 nullptr);
18845 }
18846 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18847 }
18848 else
18849 type = init_complex_type (name, type);
18850 break;
18851 case DW_ATE_decimal_float:
18852 type = init_decfloat_type (objfile, bits, name);
18853 break;
18854 case DW_ATE_float:
18855 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
18856 break;
18857 case DW_ATE_signed:
18858 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18859 break;
18860 case DW_ATE_unsigned:
18861 if (cu->language == language_fortran
18862 && name
18863 && startswith (name, "character("))
18864 type = init_character_type (objfile, bits, 1, name);
18865 else
18866 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18867 break;
18868 case DW_ATE_signed_char:
18869 if (cu->language == language_ada || cu->language == language_m2
18870 || cu->language == language_pascal
18871 || cu->language == language_fortran)
18872 type = init_character_type (objfile, bits, 0, name);
18873 else
18874 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18875 break;
18876 case DW_ATE_unsigned_char:
18877 if (cu->language == language_ada || cu->language == language_m2
18878 || cu->language == language_pascal
18879 || cu->language == language_fortran
18880 || cu->language == language_rust)
18881 type = init_character_type (objfile, bits, 1, name);
18882 else
18883 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18884 break;
18885 case DW_ATE_UTF:
18886 {
18887 if (bits == 16)
18888 type = builtin_type (arch)->builtin_char16;
18889 else if (bits == 32)
18890 type = builtin_type (arch)->builtin_char32;
18891 else
18892 {
18893 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
18894 bits);
18895 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18896 }
18897 return set_die_type (die, type, cu);
18898 }
18899 break;
18900 case DW_ATE_signed_fixed:
18901 type = init_fixed_point_type (objfile, bits, 0, name);
18902 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18903 break;
18904 case DW_ATE_unsigned_fixed:
18905 type = init_fixed_point_type (objfile, bits, 1, name);
18906 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18907 break;
18908
18909 default:
18910 complaint (_("unsupported DW_AT_encoding: '%s'"),
18911 dwarf_type_encoding_name (encoding));
18912 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18913 break;
18914 }
18915
18916 if (name && strcmp (name, "char") == 0)
18917 type->set_has_no_signedness (true);
18918
18919 maybe_set_alignment (cu, die, type);
18920
18921 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18922
18923 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18924 {
18925 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18926 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
18927 {
18928 unsigned real_bit_size = attr->as_unsigned ();
18929 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18930 /* Only use the attributes if they make sense together. */
18931 if (attr == nullptr
18932 || (attr->as_unsigned () + real_bit_size
18933 <= 8 * TYPE_LENGTH (type)))
18934 {
18935 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18936 = real_bit_size;
18937 if (attr != nullptr)
18938 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18939 = attr->as_unsigned ();
18940 }
18941 }
18942 }
18943
18944 return set_die_type (die, type, cu);
18945 }
18946
18947 /* Parse dwarf attribute if it's a block, reference or constant and put the
18948 resulting value of the attribute into struct bound_prop.
18949 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18950
18951 static int
18952 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18953 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18954 struct type *default_type)
18955 {
18956 struct dwarf2_property_baton *baton;
18957 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18958 struct objfile *objfile = per_objfile->objfile;
18959 struct obstack *obstack = &objfile->objfile_obstack;
18960
18961 gdb_assert (default_type != NULL);
18962
18963 if (attr == NULL || prop == NULL)
18964 return 0;
18965
18966 if (attr->form_is_block ())
18967 {
18968 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18969 baton->property_type = default_type;
18970 baton->locexpr.per_cu = cu->per_cu;
18971 baton->locexpr.per_objfile = per_objfile;
18972
18973 struct dwarf_block *block = attr->as_block ();
18974 baton->locexpr.size = block->size;
18975 baton->locexpr.data = block->data;
18976 switch (attr->name)
18977 {
18978 case DW_AT_string_length:
18979 baton->locexpr.is_reference = true;
18980 break;
18981 default:
18982 baton->locexpr.is_reference = false;
18983 break;
18984 }
18985
18986 prop->set_locexpr (baton);
18987 gdb_assert (prop->baton () != NULL);
18988 }
18989 else if (attr->form_is_ref ())
18990 {
18991 struct dwarf2_cu *target_cu = cu;
18992 struct die_info *target_die;
18993 struct attribute *target_attr;
18994
18995 target_die = follow_die_ref (die, attr, &target_cu);
18996 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18997 if (target_attr == NULL)
18998 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18999 target_cu);
19000 if (target_attr == NULL)
19001 return 0;
19002
19003 switch (target_attr->name)
19004 {
19005 case DW_AT_location:
19006 if (target_attr->form_is_section_offset ())
19007 {
19008 baton = XOBNEW (obstack, struct dwarf2_property_baton);
19009 baton->property_type = die_type (target_die, target_cu);
19010 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
19011 prop->set_loclist (baton);
19012 gdb_assert (prop->baton () != NULL);
19013 }
19014 else if (target_attr->form_is_block ())
19015 {
19016 baton = XOBNEW (obstack, struct dwarf2_property_baton);
19017 baton->property_type = die_type (target_die, target_cu);
19018 baton->locexpr.per_cu = cu->per_cu;
19019 baton->locexpr.per_objfile = per_objfile;
19020 struct dwarf_block *block = target_attr->as_block ();
19021 baton->locexpr.size = block->size;
19022 baton->locexpr.data = block->data;
19023 baton->locexpr.is_reference = true;
19024 prop->set_locexpr (baton);
19025 gdb_assert (prop->baton () != NULL);
19026 }
19027 else
19028 {
19029 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19030 "dynamic property");
19031 return 0;
19032 }
19033 break;
19034 case DW_AT_data_member_location:
19035 {
19036 LONGEST offset;
19037
19038 if (!handle_data_member_location (target_die, target_cu,
19039 &offset))
19040 return 0;
19041
19042 baton = XOBNEW (obstack, struct dwarf2_property_baton);
19043 baton->property_type = read_type_die (target_die->parent,
19044 target_cu);
19045 baton->offset_info.offset = offset;
19046 baton->offset_info.type = die_type (target_die, target_cu);
19047 prop->set_addr_offset (baton);
19048 break;
19049 }
19050 }
19051 }
19052 else if (attr->form_is_constant ())
19053 prop->set_const_val (attr->constant_value (0));
19054 else
19055 {
19056 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
19057 dwarf2_name (die, cu));
19058 return 0;
19059 }
19060
19061 return 1;
19062 }
19063
19064 /* See read.h. */
19065
19066 struct type *
19067 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
19068 {
19069 struct type *int_type;
19070
19071 /* Helper macro to examine the various builtin types. */
19072 #define TRY_TYPE(F) \
19073 int_type = (unsigned_p \
19074 ? objfile_type (objfile)->builtin_unsigned_ ## F \
19075 : objfile_type (objfile)->builtin_ ## F); \
19076 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
19077 return int_type
19078
19079 TRY_TYPE (char);
19080 TRY_TYPE (short);
19081 TRY_TYPE (int);
19082 TRY_TYPE (long);
19083 TRY_TYPE (long_long);
19084
19085 #undef TRY_TYPE
19086
19087 gdb_assert_not_reached ("unable to find suitable integer type");
19088 }
19089
19090 /* See read.h. */
19091
19092 struct type *
19093 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
19094 {
19095 int addr_size = this->per_cu->addr_size ();
19096 return this->per_objfile->int_type (addr_size, unsigned_p);
19097 }
19098
19099 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
19100 present (which is valid) then compute the default type based on the
19101 compilation units address size. */
19102
19103 static struct type *
19104 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
19105 {
19106 struct type *index_type = die_type (die, cu);
19107
19108 /* Dwarf-2 specifications explicitly allows to create subrange types
19109 without specifying a base type.
19110 In that case, the base type must be set to the type of
19111 the lower bound, upper bound or count, in that order, if any of these
19112 three attributes references an object that has a type.
19113 If no base type is found, the Dwarf-2 specifications say that
19114 a signed integer type of size equal to the size of an address should
19115 be used.
19116 For the following C code: `extern char gdb_int [];'
19117 GCC produces an empty range DIE.
19118 FIXME: muller/2010-05-28: Possible references to object for low bound,
19119 high bound or count are not yet handled by this code. */
19120 if (index_type->code () == TYPE_CODE_VOID)
19121 index_type = cu->addr_sized_int_type (false);
19122
19123 return index_type;
19124 }
19125
19126 /* Read the given DW_AT_subrange DIE. */
19127
19128 static struct type *
19129 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
19130 {
19131 struct type *base_type, *orig_base_type;
19132 struct type *range_type;
19133 struct attribute *attr;
19134 struct dynamic_prop low, high;
19135 int low_default_is_valid;
19136 int high_bound_is_count = 0;
19137 const char *name;
19138 ULONGEST negative_mask;
19139
19140 orig_base_type = read_subrange_index_type (die, cu);
19141
19142 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
19143 whereas the real type might be. So, we use ORIG_BASE_TYPE when
19144 creating the range type, but we use the result of check_typedef
19145 when examining properties of the type. */
19146 base_type = check_typedef (orig_base_type);
19147
19148 /* The die_type call above may have already set the type for this DIE. */
19149 range_type = get_die_type (die, cu);
19150 if (range_type)
19151 return range_type;
19152
19153 high.set_const_val (0);
19154
19155 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
19156 omitting DW_AT_lower_bound. */
19157 switch (cu->language)
19158 {
19159 case language_c:
19160 case language_cplus:
19161 low.set_const_val (0);
19162 low_default_is_valid = 1;
19163 break;
19164 case language_fortran:
19165 low.set_const_val (1);
19166 low_default_is_valid = 1;
19167 break;
19168 case language_d:
19169 case language_objc:
19170 case language_rust:
19171 low.set_const_val (0);
19172 low_default_is_valid = (cu->header.version >= 4);
19173 break;
19174 case language_ada:
19175 case language_m2:
19176 case language_pascal:
19177 low.set_const_val (1);
19178 low_default_is_valid = (cu->header.version >= 4);
19179 break;
19180 default:
19181 low.set_const_val (0);
19182 low_default_is_valid = 0;
19183 break;
19184 }
19185
19186 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
19187 if (attr != nullptr)
19188 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
19189 else if (!low_default_is_valid)
19190 complaint (_("Missing DW_AT_lower_bound "
19191 "- DIE at %s [in module %s]"),
19192 sect_offset_str (die->sect_off),
19193 objfile_name (cu->per_objfile->objfile));
19194
19195 struct attribute *attr_ub, *attr_count;
19196 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
19197 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
19198 {
19199 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
19200 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
19201 {
19202 /* If bounds are constant do the final calculation here. */
19203 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
19204 high.set_const_val (low.const_val () + high.const_val () - 1);
19205 else
19206 high_bound_is_count = 1;
19207 }
19208 else
19209 {
19210 if (attr_ub != NULL)
19211 complaint (_("Unresolved DW_AT_upper_bound "
19212 "- DIE at %s [in module %s]"),
19213 sect_offset_str (die->sect_off),
19214 objfile_name (cu->per_objfile->objfile));
19215 if (attr_count != NULL)
19216 complaint (_("Unresolved DW_AT_count "
19217 "- DIE at %s [in module %s]"),
19218 sect_offset_str (die->sect_off),
19219 objfile_name (cu->per_objfile->objfile));
19220 }
19221 }
19222
19223 LONGEST bias = 0;
19224 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
19225 if (bias_attr != nullptr && bias_attr->form_is_constant ())
19226 bias = bias_attr->constant_value (0);
19227
19228 /* Normally, the DWARF producers are expected to use a signed
19229 constant form (Eg. DW_FORM_sdata) to express negative bounds.
19230 But this is unfortunately not always the case, as witnessed
19231 with GCC, for instance, where the ambiguous DW_FORM_dataN form
19232 is used instead. To work around that ambiguity, we treat
19233 the bounds as signed, and thus sign-extend their values, when
19234 the base type is signed. */
19235 negative_mask =
19236 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
19237 if (low.kind () == PROP_CONST
19238 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
19239 low.set_const_val (low.const_val () | negative_mask);
19240 if (high.kind () == PROP_CONST
19241 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
19242 high.set_const_val (high.const_val () | negative_mask);
19243
19244 /* Check for bit and byte strides. */
19245 struct dynamic_prop byte_stride_prop;
19246 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
19247 if (attr_byte_stride != nullptr)
19248 {
19249 struct type *prop_type = cu->addr_sized_int_type (false);
19250 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
19251 prop_type);
19252 }
19253
19254 struct dynamic_prop bit_stride_prop;
19255 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
19256 if (attr_bit_stride != nullptr)
19257 {
19258 /* It only makes sense to have either a bit or byte stride. */
19259 if (attr_byte_stride != nullptr)
19260 {
19261 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
19262 "- DIE at %s [in module %s]"),
19263 sect_offset_str (die->sect_off),
19264 objfile_name (cu->per_objfile->objfile));
19265 attr_bit_stride = nullptr;
19266 }
19267 else
19268 {
19269 struct type *prop_type = cu->addr_sized_int_type (false);
19270 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
19271 prop_type);
19272 }
19273 }
19274
19275 if (attr_byte_stride != nullptr
19276 || attr_bit_stride != nullptr)
19277 {
19278 bool byte_stride_p = (attr_byte_stride != nullptr);
19279 struct dynamic_prop *stride
19280 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
19281
19282 range_type
19283 = create_range_type_with_stride (NULL, orig_base_type, &low,
19284 &high, bias, stride, byte_stride_p);
19285 }
19286 else
19287 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
19288
19289 if (high_bound_is_count)
19290 range_type->bounds ()->flag_upper_bound_is_count = 1;
19291
19292 /* Ada expects an empty array on no boundary attributes. */
19293 if (attr == NULL && cu->language != language_ada)
19294 range_type->bounds ()->high.set_undefined ();
19295
19296 name = dwarf2_name (die, cu);
19297 if (name)
19298 range_type->set_name (name);
19299
19300 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
19301 if (attr != nullptr)
19302 TYPE_LENGTH (range_type) = attr->constant_value (0);
19303
19304 maybe_set_alignment (cu, die, range_type);
19305
19306 set_die_type (die, range_type, cu);
19307
19308 /* set_die_type should be already done. */
19309 set_descriptive_type (range_type, die, cu);
19310
19311 return range_type;
19312 }
19313
19314 static struct type *
19315 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
19316 {
19317 struct type *type;
19318
19319 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
19320 type->set_name (dwarf2_name (die, cu));
19321
19322 /* In Ada, an unspecified type is typically used when the description
19323 of the type is deferred to a different unit. When encountering
19324 such a type, we treat it as a stub, and try to resolve it later on,
19325 when needed. */
19326 if (cu->language == language_ada)
19327 type->set_is_stub (true);
19328
19329 return set_die_type (die, type, cu);
19330 }
19331
19332 /* Read a single die and all its descendents. Set the die's sibling
19333 field to NULL; set other fields in the die correctly, and set all
19334 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
19335 location of the info_ptr after reading all of those dies. PARENT
19336 is the parent of the die in question. */
19337
19338 static struct die_info *
19339 read_die_and_children (const struct die_reader_specs *reader,
19340 const gdb_byte *info_ptr,
19341 const gdb_byte **new_info_ptr,
19342 struct die_info *parent)
19343 {
19344 struct die_info *die;
19345 const gdb_byte *cur_ptr;
19346
19347 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
19348 if (die == NULL)
19349 {
19350 *new_info_ptr = cur_ptr;
19351 return NULL;
19352 }
19353 store_in_ref_table (die, reader->cu);
19354
19355 if (die->has_children)
19356 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
19357 else
19358 {
19359 die->child = NULL;
19360 *new_info_ptr = cur_ptr;
19361 }
19362
19363 die->sibling = NULL;
19364 die->parent = parent;
19365 return die;
19366 }
19367
19368 /* Read a die, all of its descendents, and all of its siblings; set
19369 all of the fields of all of the dies correctly. Arguments are as
19370 in read_die_and_children. */
19371
19372 static struct die_info *
19373 read_die_and_siblings_1 (const struct die_reader_specs *reader,
19374 const gdb_byte *info_ptr,
19375 const gdb_byte **new_info_ptr,
19376 struct die_info *parent)
19377 {
19378 struct die_info *first_die, *last_sibling;
19379 const gdb_byte *cur_ptr;
19380
19381 cur_ptr = info_ptr;
19382 first_die = last_sibling = NULL;
19383
19384 while (1)
19385 {
19386 struct die_info *die
19387 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
19388
19389 if (die == NULL)
19390 {
19391 *new_info_ptr = cur_ptr;
19392 return first_die;
19393 }
19394
19395 if (!first_die)
19396 first_die = die;
19397 else
19398 last_sibling->sibling = die;
19399
19400 last_sibling = die;
19401 }
19402 }
19403
19404 /* Read a die, all of its descendents, and all of its siblings; set
19405 all of the fields of all of the dies correctly. Arguments are as
19406 in read_die_and_children.
19407 This the main entry point for reading a DIE and all its children. */
19408
19409 static struct die_info *
19410 read_die_and_siblings (const struct die_reader_specs *reader,
19411 const gdb_byte *info_ptr,
19412 const gdb_byte **new_info_ptr,
19413 struct die_info *parent)
19414 {
19415 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
19416 new_info_ptr, parent);
19417
19418 if (dwarf_die_debug)
19419 {
19420 fprintf_unfiltered (gdb_stdlog,
19421 "Read die from %s@0x%x of %s:\n",
19422 reader->die_section->get_name (),
19423 (unsigned) (info_ptr - reader->die_section->buffer),
19424 bfd_get_filename (reader->abfd));
19425 dump_die (die, dwarf_die_debug);
19426 }
19427
19428 return die;
19429 }
19430
19431 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
19432 attributes.
19433 The caller is responsible for filling in the extra attributes
19434 and updating (*DIEP)->num_attrs.
19435 Set DIEP to point to a newly allocated die with its information,
19436 except for its child, sibling, and parent fields. */
19437
19438 static const gdb_byte *
19439 read_full_die_1 (const struct die_reader_specs *reader,
19440 struct die_info **diep, const gdb_byte *info_ptr,
19441 int num_extra_attrs)
19442 {
19443 unsigned int abbrev_number, bytes_read, i;
19444 const struct abbrev_info *abbrev;
19445 struct die_info *die;
19446 struct dwarf2_cu *cu = reader->cu;
19447 bfd *abfd = reader->abfd;
19448
19449 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
19450 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19451 info_ptr += bytes_read;
19452 if (!abbrev_number)
19453 {
19454 *diep = NULL;
19455 return info_ptr;
19456 }
19457
19458 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
19459 if (!abbrev)
19460 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
19461 abbrev_number,
19462 bfd_get_filename (abfd));
19463
19464 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
19465 die->sect_off = sect_off;
19466 die->tag = abbrev->tag;
19467 die->abbrev = abbrev_number;
19468 die->has_children = abbrev->has_children;
19469
19470 /* Make the result usable.
19471 The caller needs to update num_attrs after adding the extra
19472 attributes. */
19473 die->num_attrs = abbrev->num_attrs;
19474
19475 bool any_need_reprocess = false;
19476 for (i = 0; i < abbrev->num_attrs; ++i)
19477 {
19478 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
19479 info_ptr);
19480 if (die->attrs[i].requires_reprocessing_p ())
19481 any_need_reprocess = true;
19482 }
19483
19484 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
19485 if (attr != nullptr && attr->form_is_unsigned ())
19486 cu->str_offsets_base = attr->as_unsigned ();
19487
19488 attr = die->attr (DW_AT_loclists_base);
19489 if (attr != nullptr)
19490 cu->loclist_base = attr->as_unsigned ();
19491
19492 auto maybe_addr_base = die->addr_base ();
19493 if (maybe_addr_base.has_value ())
19494 cu->addr_base = *maybe_addr_base;
19495
19496 attr = die->attr (DW_AT_rnglists_base);
19497 if (attr != nullptr)
19498 cu->rnglists_base = attr->as_unsigned ();
19499
19500 if (any_need_reprocess)
19501 {
19502 for (i = 0; i < abbrev->num_attrs; ++i)
19503 {
19504 if (die->attrs[i].requires_reprocessing_p ())
19505 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
19506 }
19507 }
19508 *diep = die;
19509 return info_ptr;
19510 }
19511
19512 /* Read a die and all its attributes.
19513 Set DIEP to point to a newly allocated die with its information,
19514 except for its child, sibling, and parent fields. */
19515
19516 static const gdb_byte *
19517 read_full_die (const struct die_reader_specs *reader,
19518 struct die_info **diep, const gdb_byte *info_ptr)
19519 {
19520 const gdb_byte *result;
19521
19522 result = read_full_die_1 (reader, diep, info_ptr, 0);
19523
19524 if (dwarf_die_debug)
19525 {
19526 fprintf_unfiltered (gdb_stdlog,
19527 "Read die from %s@0x%x of %s:\n",
19528 reader->die_section->get_name (),
19529 (unsigned) (info_ptr - reader->die_section->buffer),
19530 bfd_get_filename (reader->abfd));
19531 dump_die (*diep, dwarf_die_debug);
19532 }
19533
19534 return result;
19535 }
19536 \f
19537
19538 /* Returns nonzero if TAG represents a type that we might generate a partial
19539 symbol for. */
19540
19541 static int
19542 is_type_tag_for_partial (int tag, enum language lang)
19543 {
19544 switch (tag)
19545 {
19546 #if 0
19547 /* Some types that would be reasonable to generate partial symbols for,
19548 that we don't at present. Note that normally this does not
19549 matter, mainly because C compilers don't give names to these
19550 types, but instead emit DW_TAG_typedef. */
19551 case DW_TAG_file_type:
19552 case DW_TAG_ptr_to_member_type:
19553 case DW_TAG_set_type:
19554 case DW_TAG_string_type:
19555 case DW_TAG_subroutine_type:
19556 #endif
19557
19558 /* GNAT may emit an array with a name, but no typedef, so we
19559 need to make a symbol in this case. */
19560 case DW_TAG_array_type:
19561 return lang == language_ada;
19562
19563 case DW_TAG_base_type:
19564 case DW_TAG_class_type:
19565 case DW_TAG_interface_type:
19566 case DW_TAG_enumeration_type:
19567 case DW_TAG_structure_type:
19568 case DW_TAG_subrange_type:
19569 case DW_TAG_typedef:
19570 case DW_TAG_union_type:
19571 return 1;
19572 default:
19573 return 0;
19574 }
19575 }
19576
19577 /* Load all DIEs that are interesting for partial symbols into memory. */
19578
19579 static struct partial_die_info *
19580 load_partial_dies (const struct die_reader_specs *reader,
19581 const gdb_byte *info_ptr, int building_psymtab)
19582 {
19583 struct dwarf2_cu *cu = reader->cu;
19584 struct objfile *objfile = cu->per_objfile->objfile;
19585 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
19586 unsigned int bytes_read;
19587 unsigned int load_all = 0;
19588 int nesting_level = 1;
19589
19590 parent_die = NULL;
19591 last_die = NULL;
19592
19593 gdb_assert (cu->per_cu != NULL);
19594 if (cu->per_cu->load_all_dies)
19595 load_all = 1;
19596
19597 cu->partial_dies
19598 = htab_create_alloc_ex (cu->header.length / 12,
19599 partial_die_hash,
19600 partial_die_eq,
19601 NULL,
19602 &cu->comp_unit_obstack,
19603 hashtab_obstack_allocate,
19604 dummy_obstack_deallocate);
19605
19606 while (1)
19607 {
19608 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
19609 &bytes_read);
19610
19611 /* A NULL abbrev means the end of a series of children. */
19612 if (abbrev == NULL)
19613 {
19614 if (--nesting_level == 0)
19615 return first_die;
19616
19617 info_ptr += bytes_read;
19618 last_die = parent_die;
19619 parent_die = parent_die->die_parent;
19620 continue;
19621 }
19622
19623 /* Check for template arguments. We never save these; if
19624 they're seen, we just mark the parent, and go on our way. */
19625 if (parent_die != NULL
19626 && cu->language == language_cplus
19627 && (abbrev->tag == DW_TAG_template_type_param
19628 || abbrev->tag == DW_TAG_template_value_param))
19629 {
19630 parent_die->has_template_arguments = 1;
19631
19632 if (!load_all)
19633 {
19634 /* We don't need a partial DIE for the template argument. */
19635 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19636 continue;
19637 }
19638 }
19639
19640 /* We only recurse into c++ subprograms looking for template arguments.
19641 Skip their other children. */
19642 if (!load_all
19643 && cu->language == language_cplus
19644 && parent_die != NULL
19645 && parent_die->tag == DW_TAG_subprogram
19646 && abbrev->tag != DW_TAG_inlined_subroutine)
19647 {
19648 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19649 continue;
19650 }
19651
19652 /* Check whether this DIE is interesting enough to save. Normally
19653 we would not be interested in members here, but there may be
19654 later variables referencing them via DW_AT_specification (for
19655 static members). */
19656 if (!load_all
19657 && !is_type_tag_for_partial (abbrev->tag, cu->language)
19658 && abbrev->tag != DW_TAG_constant
19659 && abbrev->tag != DW_TAG_enumerator
19660 && abbrev->tag != DW_TAG_subprogram
19661 && abbrev->tag != DW_TAG_inlined_subroutine
19662 && abbrev->tag != DW_TAG_lexical_block
19663 && abbrev->tag != DW_TAG_variable
19664 && abbrev->tag != DW_TAG_namespace
19665 && abbrev->tag != DW_TAG_module
19666 && abbrev->tag != DW_TAG_member
19667 && abbrev->tag != DW_TAG_imported_unit
19668 && abbrev->tag != DW_TAG_imported_declaration)
19669 {
19670 /* Otherwise we skip to the next sibling, if any. */
19671 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19672 continue;
19673 }
19674
19675 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19676 abbrev);
19677
19678 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
19679
19680 /* This two-pass algorithm for processing partial symbols has a
19681 high cost in cache pressure. Thus, handle some simple cases
19682 here which cover the majority of C partial symbols. DIEs
19683 which neither have specification tags in them, nor could have
19684 specification tags elsewhere pointing at them, can simply be
19685 processed and discarded.
19686
19687 This segment is also optional; scan_partial_symbols and
19688 add_partial_symbol will handle these DIEs if we chain
19689 them in normally. When compilers which do not emit large
19690 quantities of duplicate debug information are more common,
19691 this code can probably be removed. */
19692
19693 /* Any complete simple types at the top level (pretty much all
19694 of them, for a language without namespaces), can be processed
19695 directly. */
19696 if (parent_die == NULL
19697 && pdi.has_specification == 0
19698 && pdi.is_declaration == 0
19699 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19700 || pdi.tag == DW_TAG_base_type
19701 || pdi.tag == DW_TAG_array_type
19702 || pdi.tag == DW_TAG_subrange_type))
19703 {
19704 if (building_psymtab && pdi.raw_name != NULL)
19705 add_partial_symbol (&pdi, cu);
19706
19707 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19708 continue;
19709 }
19710
19711 /* The exception for DW_TAG_typedef with has_children above is
19712 a workaround of GCC PR debug/47510. In the case of this complaint
19713 type_name_or_error will error on such types later.
19714
19715 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19716 it could not find the child DIEs referenced later, this is checked
19717 above. In correct DWARF DW_TAG_typedef should have no children. */
19718
19719 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
19720 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
19721 "- DIE at %s [in module %s]"),
19722 sect_offset_str (pdi.sect_off), objfile_name (objfile));
19723
19724 /* If we're at the second level, and we're an enumerator, and
19725 our parent has no specification (meaning possibly lives in a
19726 namespace elsewhere), then we can add the partial symbol now
19727 instead of queueing it. */
19728 if (pdi.tag == DW_TAG_enumerator
19729 && parent_die != NULL
19730 && parent_die->die_parent == NULL
19731 && parent_die->tag == DW_TAG_enumeration_type
19732 && parent_die->has_specification == 0)
19733 {
19734 if (pdi.raw_name == NULL)
19735 complaint (_("malformed enumerator DIE ignored"));
19736 else if (building_psymtab)
19737 add_partial_symbol (&pdi, cu);
19738
19739 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19740 continue;
19741 }
19742
19743 struct partial_die_info *part_die
19744 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
19745
19746 /* We'll save this DIE so link it in. */
19747 part_die->die_parent = parent_die;
19748 part_die->die_sibling = NULL;
19749 part_die->die_child = NULL;
19750
19751 if (last_die && last_die == parent_die)
19752 last_die->die_child = part_die;
19753 else if (last_die)
19754 last_die->die_sibling = part_die;
19755
19756 last_die = part_die;
19757
19758 if (first_die == NULL)
19759 first_die = part_die;
19760
19761 /* Maybe add the DIE to the hash table. Not all DIEs that we
19762 find interesting need to be in the hash table, because we
19763 also have the parent/sibling/child chains; only those that we
19764 might refer to by offset later during partial symbol reading.
19765
19766 For now this means things that might have be the target of a
19767 DW_AT_specification, DW_AT_abstract_origin, or
19768 DW_AT_extension. DW_AT_extension will refer only to
19769 namespaces; DW_AT_abstract_origin refers to functions (and
19770 many things under the function DIE, but we do not recurse
19771 into function DIEs during partial symbol reading) and
19772 possibly variables as well; DW_AT_specification refers to
19773 declarations. Declarations ought to have the DW_AT_declaration
19774 flag. It happens that GCC forgets to put it in sometimes, but
19775 only for functions, not for types.
19776
19777 Adding more things than necessary to the hash table is harmless
19778 except for the performance cost. Adding too few will result in
19779 wasted time in find_partial_die, when we reread the compilation
19780 unit with load_all_dies set. */
19781
19782 if (load_all
19783 || abbrev->tag == DW_TAG_constant
19784 || abbrev->tag == DW_TAG_subprogram
19785 || abbrev->tag == DW_TAG_variable
19786 || abbrev->tag == DW_TAG_namespace
19787 || part_die->is_declaration)
19788 {
19789 void **slot;
19790
19791 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
19792 to_underlying (part_die->sect_off),
19793 INSERT);
19794 *slot = part_die;
19795 }
19796
19797 /* For some DIEs we want to follow their children (if any). For C
19798 we have no reason to follow the children of structures; for other
19799 languages we have to, so that we can get at method physnames
19800 to infer fully qualified class names, for DW_AT_specification,
19801 and for C++ template arguments. For C++, we also look one level
19802 inside functions to find template arguments (if the name of the
19803 function does not already contain the template arguments).
19804
19805 For Ada and Fortran, we need to scan the children of subprograms
19806 and lexical blocks as well because these languages allow the
19807 definition of nested entities that could be interesting for the
19808 debugger, such as nested subprograms for instance. */
19809 if (last_die->has_children
19810 && (load_all
19811 || last_die->tag == DW_TAG_namespace
19812 || last_die->tag == DW_TAG_module
19813 || last_die->tag == DW_TAG_enumeration_type
19814 || (cu->language == language_cplus
19815 && last_die->tag == DW_TAG_subprogram
19816 && (last_die->raw_name == NULL
19817 || strchr (last_die->raw_name, '<') == NULL))
19818 || (cu->language != language_c
19819 && (last_die->tag == DW_TAG_class_type
19820 || last_die->tag == DW_TAG_interface_type
19821 || last_die->tag == DW_TAG_structure_type
19822 || last_die->tag == DW_TAG_union_type))
19823 || ((cu->language == language_ada
19824 || cu->language == language_fortran)
19825 && (last_die->tag == DW_TAG_subprogram
19826 || last_die->tag == DW_TAG_lexical_block))))
19827 {
19828 nesting_level++;
19829 parent_die = last_die;
19830 continue;
19831 }
19832
19833 /* Otherwise we skip to the next sibling, if any. */
19834 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
19835
19836 /* Back to the top, do it again. */
19837 }
19838 }
19839
19840 partial_die_info::partial_die_info (sect_offset sect_off_,
19841 const struct abbrev_info *abbrev)
19842 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19843 {
19844 }
19845
19846 /* See class definition. */
19847
19848 const char *
19849 partial_die_info::name (dwarf2_cu *cu)
19850 {
19851 if (!canonical_name && raw_name != nullptr)
19852 {
19853 struct objfile *objfile = cu->per_objfile->objfile;
19854 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19855 canonical_name = 1;
19856 }
19857
19858 return raw_name;
19859 }
19860
19861 /* Read a minimal amount of information into the minimal die structure.
19862 INFO_PTR should point just after the initial uleb128 of a DIE. */
19863
19864 const gdb_byte *
19865 partial_die_info::read (const struct die_reader_specs *reader,
19866 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
19867 {
19868 struct dwarf2_cu *cu = reader->cu;
19869 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19870 unsigned int i;
19871 int has_low_pc_attr = 0;
19872 int has_high_pc_attr = 0;
19873 int high_pc_relative = 0;
19874
19875 for (i = 0; i < abbrev.num_attrs; ++i)
19876 {
19877 attribute attr;
19878 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19879 /* String and address offsets that need to do the reprocessing have
19880 already been read at this point, so there is no need to wait until
19881 the loop terminates to do the reprocessing. */
19882 if (attr.requires_reprocessing_p ())
19883 read_attribute_reprocess (reader, &attr, tag);
19884 /* Store the data if it is of an attribute we want to keep in a
19885 partial symbol table. */
19886 switch (attr.name)
19887 {
19888 case DW_AT_name:
19889 switch (tag)
19890 {
19891 case DW_TAG_compile_unit:
19892 case DW_TAG_partial_unit:
19893 case DW_TAG_type_unit:
19894 /* Compilation units have a DW_AT_name that is a filename, not
19895 a source language identifier. */
19896 case DW_TAG_enumeration_type:
19897 case DW_TAG_enumerator:
19898 /* These tags always have simple identifiers already; no need
19899 to canonicalize them. */
19900 canonical_name = 1;
19901 raw_name = attr.as_string ();
19902 break;
19903 default:
19904 canonical_name = 0;
19905 raw_name = attr.as_string ();
19906 break;
19907 }
19908 break;
19909 case DW_AT_linkage_name:
19910 case DW_AT_MIPS_linkage_name:
19911 /* Note that both forms of linkage name might appear. We
19912 assume they will be the same, and we only store the last
19913 one we see. */
19914 linkage_name = attr.as_string ();
19915 break;
19916 case DW_AT_low_pc:
19917 has_low_pc_attr = 1;
19918 lowpc = attr.as_address ();
19919 break;
19920 case DW_AT_high_pc:
19921 has_high_pc_attr = 1;
19922 highpc = attr.as_address ();
19923 if (cu->header.version >= 4 && attr.form_is_constant ())
19924 high_pc_relative = 1;
19925 break;
19926 case DW_AT_location:
19927 /* Support the .debug_loc offsets. */
19928 if (attr.form_is_block ())
19929 {
19930 d.locdesc = attr.as_block ();
19931 }
19932 else if (attr.form_is_section_offset ())
19933 {
19934 dwarf2_complex_location_expr_complaint ();
19935 }
19936 else
19937 {
19938 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19939 "partial symbol information");
19940 }
19941 break;
19942 case DW_AT_external:
19943 is_external = attr.as_boolean ();
19944 break;
19945 case DW_AT_declaration:
19946 is_declaration = attr.as_boolean ();
19947 break;
19948 case DW_AT_type:
19949 has_type = 1;
19950 break;
19951 case DW_AT_abstract_origin:
19952 case DW_AT_specification:
19953 case DW_AT_extension:
19954 has_specification = 1;
19955 spec_offset = attr.get_ref_die_offset ();
19956 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19957 || cu->per_cu->is_dwz);
19958 break;
19959 case DW_AT_sibling:
19960 /* Ignore absolute siblings, they might point outside of
19961 the current compile unit. */
19962 if (attr.form == DW_FORM_ref_addr)
19963 complaint (_("ignoring absolute DW_AT_sibling"));
19964 else
19965 {
19966 const gdb_byte *buffer = reader->buffer;
19967 sect_offset off = attr.get_ref_die_offset ();
19968 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19969
19970 if (sibling_ptr < info_ptr)
19971 complaint (_("DW_AT_sibling points backwards"));
19972 else if (sibling_ptr > reader->buffer_end)
19973 reader->die_section->overflow_complaint ();
19974 else
19975 sibling = sibling_ptr;
19976 }
19977 break;
19978 case DW_AT_byte_size:
19979 has_byte_size = 1;
19980 break;
19981 case DW_AT_const_value:
19982 has_const_value = 1;
19983 break;
19984 case DW_AT_calling_convention:
19985 /* DWARF doesn't provide a way to identify a program's source-level
19986 entry point. DW_AT_calling_convention attributes are only meant
19987 to describe functions' calling conventions.
19988
19989 However, because it's a necessary piece of information in
19990 Fortran, and before DWARF 4 DW_CC_program was the only
19991 piece of debugging information whose definition refers to
19992 a 'main program' at all, several compilers marked Fortran
19993 main programs with DW_CC_program --- even when those
19994 functions use the standard calling conventions.
19995
19996 Although DWARF now specifies a way to provide this
19997 information, we support this practice for backward
19998 compatibility. */
19999 if (attr.constant_value (0) == DW_CC_program
20000 && cu->language == language_fortran)
20001 main_subprogram = 1;
20002 break;
20003 case DW_AT_inline:
20004 {
20005 LONGEST value = attr.constant_value (-1);
20006 if (value == DW_INL_inlined
20007 || value == DW_INL_declared_inlined)
20008 may_be_inlined = 1;
20009 }
20010 break;
20011
20012 case DW_AT_import:
20013 if (tag == DW_TAG_imported_unit)
20014 {
20015 d.sect_off = attr.get_ref_die_offset ();
20016 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
20017 || cu->per_cu->is_dwz);
20018 }
20019 break;
20020
20021 case DW_AT_main_subprogram:
20022 main_subprogram = attr.as_boolean ();
20023 break;
20024
20025 case DW_AT_ranges:
20026 {
20027 /* Offset in the .debug_ranges or .debug_rnglist section (depending
20028 on DWARF version). */
20029 ULONGEST ranges_offset = attr.as_unsigned ();
20030
20031 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
20032 this value. */
20033 if (tag != DW_TAG_compile_unit)
20034 ranges_offset += cu->gnu_ranges_base;
20035
20036 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
20037 nullptr, tag))
20038 has_pc_info = 1;
20039 }
20040 break;
20041
20042 default:
20043 break;
20044 }
20045 }
20046
20047 /* For Ada, if both the name and the linkage name appear, we prefer
20048 the latter. This lets "catch exception" work better, regardless
20049 of the order in which the name and linkage name were emitted.
20050 Really, though, this is just a workaround for the fact that gdb
20051 doesn't store both the name and the linkage name. */
20052 if (cu->language == language_ada && linkage_name != nullptr)
20053 raw_name = linkage_name;
20054
20055 if (high_pc_relative)
20056 highpc += lowpc;
20057
20058 if (has_low_pc_attr && has_high_pc_attr)
20059 {
20060 /* When using the GNU linker, .gnu.linkonce. sections are used to
20061 eliminate duplicate copies of functions and vtables and such.
20062 The linker will arbitrarily choose one and discard the others.
20063 The AT_*_pc values for such functions refer to local labels in
20064 these sections. If the section from that file was discarded, the
20065 labels are not in the output, so the relocs get a value of 0.
20066 If this is a discarded function, mark the pc bounds as invalid,
20067 so that GDB will ignore it. */
20068 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
20069 {
20070 struct objfile *objfile = per_objfile->objfile;
20071 struct gdbarch *gdbarch = objfile->arch ();
20072
20073 complaint (_("DW_AT_low_pc %s is zero "
20074 "for DIE at %s [in module %s]"),
20075 paddress (gdbarch, lowpc),
20076 sect_offset_str (sect_off),
20077 objfile_name (objfile));
20078 }
20079 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
20080 else if (lowpc >= highpc)
20081 {
20082 struct objfile *objfile = per_objfile->objfile;
20083 struct gdbarch *gdbarch = objfile->arch ();
20084
20085 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
20086 "for DIE at %s [in module %s]"),
20087 paddress (gdbarch, lowpc),
20088 paddress (gdbarch, highpc),
20089 sect_offset_str (sect_off),
20090 objfile_name (objfile));
20091 }
20092 else
20093 has_pc_info = 1;
20094 }
20095
20096 return info_ptr;
20097 }
20098
20099 /* Find a cached partial DIE at OFFSET in CU. */
20100
20101 struct partial_die_info *
20102 dwarf2_cu::find_partial_die (sect_offset sect_off)
20103 {
20104 struct partial_die_info *lookup_die = NULL;
20105 struct partial_die_info part_die (sect_off);
20106
20107 lookup_die = ((struct partial_die_info *)
20108 htab_find_with_hash (partial_dies, &part_die,
20109 to_underlying (sect_off)));
20110
20111 return lookup_die;
20112 }
20113
20114 /* Find a partial DIE at OFFSET, which may or may not be in CU,
20115 except in the case of .debug_types DIEs which do not reference
20116 outside their CU (they do however referencing other types via
20117 DW_FORM_ref_sig8). */
20118
20119 static const struct cu_partial_die_info
20120 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
20121 {
20122 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20123 struct objfile *objfile = per_objfile->objfile;
20124 struct partial_die_info *pd = NULL;
20125
20126 if (offset_in_dwz == cu->per_cu->is_dwz
20127 && cu->header.offset_in_cu_p (sect_off))
20128 {
20129 pd = cu->find_partial_die (sect_off);
20130 if (pd != NULL)
20131 return { cu, pd };
20132 /* We missed recording what we needed.
20133 Load all dies and try again. */
20134 }
20135 else
20136 {
20137 /* TUs don't reference other CUs/TUs (except via type signatures). */
20138 if (cu->per_cu->is_debug_types)
20139 {
20140 error (_("Dwarf Error: Type Unit at offset %s contains"
20141 " external reference to offset %s [in module %s].\n"),
20142 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
20143 bfd_get_filename (objfile->obfd));
20144 }
20145 dwarf2_per_cu_data *per_cu
20146 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
20147 per_objfile);
20148
20149 cu = per_objfile->get_cu (per_cu);
20150 if (cu == NULL || cu->partial_dies == NULL)
20151 load_partial_comp_unit (per_cu, per_objfile, nullptr);
20152
20153 cu = per_objfile->get_cu (per_cu);
20154
20155 cu->last_used = 0;
20156 pd = cu->find_partial_die (sect_off);
20157 }
20158
20159 /* If we didn't find it, and not all dies have been loaded,
20160 load them all and try again. */
20161
20162 if (pd == NULL && cu->per_cu->load_all_dies == 0)
20163 {
20164 cu->per_cu->load_all_dies = 1;
20165
20166 /* This is nasty. When we reread the DIEs, somewhere up the call chain
20167 THIS_CU->cu may already be in use. So we can't just free it and
20168 replace its DIEs with the ones we read in. Instead, we leave those
20169 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
20170 and clobber THIS_CU->cu->partial_dies with the hash table for the new
20171 set. */
20172 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
20173
20174 pd = cu->find_partial_die (sect_off);
20175 }
20176
20177 if (pd == NULL)
20178 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
20179 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
20180 return { cu, pd };
20181 }
20182
20183 /* See if we can figure out if the class lives in a namespace. We do
20184 this by looking for a member function; its demangled name will
20185 contain namespace info, if there is any. */
20186
20187 static void
20188 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
20189 struct dwarf2_cu *cu)
20190 {
20191 /* NOTE: carlton/2003-10-07: Getting the info this way changes
20192 what template types look like, because the demangler
20193 frequently doesn't give the same name as the debug info. We
20194 could fix this by only using the demangled name to get the
20195 prefix (but see comment in read_structure_type). */
20196
20197 struct partial_die_info *real_pdi;
20198 struct partial_die_info *child_pdi;
20199
20200 /* If this DIE (this DIE's specification, if any) has a parent, then
20201 we should not do this. We'll prepend the parent's fully qualified
20202 name when we create the partial symbol. */
20203
20204 real_pdi = struct_pdi;
20205 while (real_pdi->has_specification)
20206 {
20207 auto res = find_partial_die (real_pdi->spec_offset,
20208 real_pdi->spec_is_dwz, cu);
20209 real_pdi = res.pdi;
20210 cu = res.cu;
20211 }
20212
20213 if (real_pdi->die_parent != NULL)
20214 return;
20215
20216 for (child_pdi = struct_pdi->die_child;
20217 child_pdi != NULL;
20218 child_pdi = child_pdi->die_sibling)
20219 {
20220 if (child_pdi->tag == DW_TAG_subprogram
20221 && child_pdi->linkage_name != NULL)
20222 {
20223 gdb::unique_xmalloc_ptr<char> actual_class_name
20224 (cu->language_defn->class_name_from_physname
20225 (child_pdi->linkage_name));
20226 if (actual_class_name != NULL)
20227 {
20228 struct objfile *objfile = cu->per_objfile->objfile;
20229 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
20230 struct_pdi->canonical_name = 1;
20231 }
20232 break;
20233 }
20234 }
20235 }
20236
20237 /* Return true if a DIE with TAG may have the DW_AT_const_value
20238 attribute. */
20239
20240 static bool
20241 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
20242 {
20243 switch (tag)
20244 {
20245 case DW_TAG_constant:
20246 case DW_TAG_enumerator:
20247 case DW_TAG_formal_parameter:
20248 case DW_TAG_template_value_param:
20249 case DW_TAG_variable:
20250 return true;
20251 }
20252
20253 return false;
20254 }
20255
20256 void
20257 partial_die_info::fixup (struct dwarf2_cu *cu)
20258 {
20259 /* Once we've fixed up a die, there's no point in doing so again.
20260 This also avoids a memory leak if we were to call
20261 guess_partial_die_structure_name multiple times. */
20262 if (fixup_called)
20263 return;
20264
20265 /* If we found a reference attribute and the DIE has no name, try
20266 to find a name in the referred to DIE. */
20267
20268 if (raw_name == NULL && has_specification)
20269 {
20270 struct partial_die_info *spec_die;
20271
20272 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20273 spec_die = res.pdi;
20274 cu = res.cu;
20275
20276 spec_die->fixup (cu);
20277
20278 if (spec_die->raw_name)
20279 {
20280 raw_name = spec_die->raw_name;
20281 canonical_name = spec_die->canonical_name;
20282
20283 /* Copy DW_AT_external attribute if it is set. */
20284 if (spec_die->is_external)
20285 is_external = spec_die->is_external;
20286 }
20287 }
20288
20289 if (!has_const_value && has_specification
20290 && can_have_DW_AT_const_value_p (tag))
20291 {
20292 struct partial_die_info *spec_die;
20293
20294 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20295 spec_die = res.pdi;
20296 cu = res.cu;
20297
20298 spec_die->fixup (cu);
20299
20300 if (spec_die->has_const_value)
20301 {
20302 /* Copy DW_AT_const_value attribute if it is set. */
20303 has_const_value = spec_die->has_const_value;
20304 }
20305 }
20306
20307 /* Set default names for some unnamed DIEs. */
20308
20309 if (raw_name == NULL && tag == DW_TAG_namespace)
20310 {
20311 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
20312 canonical_name = 1;
20313 }
20314
20315 /* If there is no parent die to provide a namespace, and there are
20316 children, see if we can determine the namespace from their linkage
20317 name. */
20318 if (cu->language == language_cplus
20319 && !cu->per_objfile->per_bfd->types.empty ()
20320 && die_parent == NULL
20321 && has_children
20322 && (tag == DW_TAG_class_type
20323 || tag == DW_TAG_structure_type
20324 || tag == DW_TAG_union_type))
20325 guess_partial_die_structure_name (this, cu);
20326
20327 /* GCC might emit a nameless struct or union that has a linkage
20328 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20329 if (raw_name == NULL
20330 && (tag == DW_TAG_class_type
20331 || tag == DW_TAG_interface_type
20332 || tag == DW_TAG_structure_type
20333 || tag == DW_TAG_union_type)
20334 && linkage_name != NULL)
20335 {
20336 gdb::unique_xmalloc_ptr<char> demangled
20337 (gdb_demangle (linkage_name, DMGL_TYPES));
20338 if (demangled != nullptr)
20339 {
20340 const char *base;
20341
20342 /* Strip any leading namespaces/classes, keep only the base name.
20343 DW_AT_name for named DIEs does not contain the prefixes. */
20344 base = strrchr (demangled.get (), ':');
20345 if (base && base > demangled.get () && base[-1] == ':')
20346 base++;
20347 else
20348 base = demangled.get ();
20349
20350 struct objfile *objfile = cu->per_objfile->objfile;
20351 raw_name = objfile->intern (base);
20352 canonical_name = 1;
20353 }
20354 }
20355
20356 fixup_called = 1;
20357 }
20358
20359 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
20360 contents from the given SECTION in the HEADER.
20361
20362 HEADER_OFFSET is the offset of the header in the section. */
20363 static void
20364 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
20365 struct dwarf2_section_info *section,
20366 sect_offset header_offset)
20367 {
20368 unsigned int bytes_read;
20369 bfd *abfd = section->get_bfd_owner ();
20370 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
20371
20372 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
20373 info_ptr += bytes_read;
20374
20375 header->version = read_2_bytes (abfd, info_ptr);
20376 info_ptr += 2;
20377
20378 header->addr_size = read_1_byte (abfd, info_ptr);
20379 info_ptr += 1;
20380
20381 header->segment_collector_size = read_1_byte (abfd, info_ptr);
20382 info_ptr += 1;
20383
20384 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
20385 }
20386
20387 /* Return the DW_AT_loclists_base value for the CU. */
20388 static ULONGEST
20389 lookup_loclist_base (struct dwarf2_cu *cu)
20390 {
20391 /* For the .dwo unit, the loclist_base points to the first offset following
20392 the header. The header consists of the following entities-
20393 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
20394 bit format)
20395 2. version (2 bytes)
20396 3. address size (1 byte)
20397 4. segment selector size (1 byte)
20398 5. offset entry count (4 bytes)
20399 These sizes are derived as per the DWARFv5 standard. */
20400 if (cu->dwo_unit != nullptr)
20401 {
20402 if (cu->header.initial_length_size == 4)
20403 return LOCLIST_HEADER_SIZE32;
20404 return LOCLIST_HEADER_SIZE64;
20405 }
20406 return cu->loclist_base;
20407 }
20408
20409 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
20410 array of offsets in the .debug_loclists section. */
20411
20412 static sect_offset
20413 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
20414 {
20415 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20416 struct objfile *objfile = per_objfile->objfile;
20417 bfd *abfd = objfile->obfd;
20418 ULONGEST loclist_header_size =
20419 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
20420 : LOCLIST_HEADER_SIZE64);
20421 ULONGEST loclist_base = lookup_loclist_base (cu);
20422
20423 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
20424 ULONGEST start_offset =
20425 loclist_base + loclist_index * cu->header.offset_size;
20426
20427 /* Get loclists section. */
20428 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20429
20430 /* Read the loclists section content. */
20431 section->read (objfile);
20432 if (section->buffer == NULL)
20433 error (_("DW_FORM_loclistx used without .debug_loclists "
20434 "section [in module %s]"), objfile_name (objfile));
20435
20436 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
20437 so if loclist_base is smaller than the header size, we have a problem. */
20438 if (loclist_base < loclist_header_size)
20439 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
20440 objfile_name (objfile));
20441
20442 /* Read the header of the loclists contribution. */
20443 struct loclists_rnglists_header header;
20444 read_loclists_rnglists_header (&header, section,
20445 (sect_offset) (loclist_base - loclist_header_size));
20446
20447 /* Verify the loclist index is valid. */
20448 if (loclist_index >= header.offset_entry_count)
20449 error (_("DW_FORM_loclistx pointing outside of "
20450 ".debug_loclists offset array [in module %s]"),
20451 objfile_name (objfile));
20452
20453 /* Validate that reading won't go beyond the end of the section. */
20454 if (start_offset + cu->header.offset_size > section->size)
20455 error (_("Reading DW_FORM_loclistx index beyond end of"
20456 ".debug_loclists section [in module %s]"),
20457 objfile_name (objfile));
20458
20459 const gdb_byte *info_ptr = section->buffer + start_offset;
20460
20461 if (cu->header.offset_size == 4)
20462 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
20463 else
20464 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
20465 }
20466
20467 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
20468 array of offsets in the .debug_rnglists section. */
20469
20470 static sect_offset
20471 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
20472 dwarf_tag tag)
20473 {
20474 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20475 struct objfile *objfile = dwarf2_per_objfile->objfile;
20476 bfd *abfd = objfile->obfd;
20477 ULONGEST rnglist_header_size =
20478 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
20479 : RNGLIST_HEADER_SIZE64);
20480
20481 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
20482 .debug_rnglists.dwo section. The rnglists base given in the skeleton
20483 doesn't apply. */
20484 ULONGEST rnglist_base =
20485 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
20486
20487 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
20488 ULONGEST start_offset =
20489 rnglist_base + rnglist_index * cu->header.offset_size;
20490
20491 /* Get rnglists section. */
20492 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
20493
20494 /* Read the rnglists section content. */
20495 section->read (objfile);
20496 if (section->buffer == nullptr)
20497 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
20498 "[in module %s]"),
20499 objfile_name (objfile));
20500
20501 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
20502 so if rnglist_base is smaller than the header size, we have a problem. */
20503 if (rnglist_base < rnglist_header_size)
20504 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
20505 objfile_name (objfile));
20506
20507 /* Read the header of the rnglists contribution. */
20508 struct loclists_rnglists_header header;
20509 read_loclists_rnglists_header (&header, section,
20510 (sect_offset) (rnglist_base - rnglist_header_size));
20511
20512 /* Verify the rnglist index is valid. */
20513 if (rnglist_index >= header.offset_entry_count)
20514 error (_("DW_FORM_rnglistx index pointing outside of "
20515 ".debug_rnglists offset array [in module %s]"),
20516 objfile_name (objfile));
20517
20518 /* Validate that reading won't go beyond the end of the section. */
20519 if (start_offset + cu->header.offset_size > section->size)
20520 error (_("Reading DW_FORM_rnglistx index beyond end of"
20521 ".debug_rnglists section [in module %s]"),
20522 objfile_name (objfile));
20523
20524 const gdb_byte *info_ptr = section->buffer + start_offset;
20525
20526 if (cu->header.offset_size == 4)
20527 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
20528 else
20529 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
20530 }
20531
20532 /* Process the attributes that had to be skipped in the first round. These
20533 attributes are the ones that need str_offsets_base or addr_base attributes.
20534 They could not have been processed in the first round, because at the time
20535 the values of str_offsets_base or addr_base may not have been known. */
20536 static void
20537 read_attribute_reprocess (const struct die_reader_specs *reader,
20538 struct attribute *attr, dwarf_tag tag)
20539 {
20540 struct dwarf2_cu *cu = reader->cu;
20541 switch (attr->form)
20542 {
20543 case DW_FORM_addrx:
20544 case DW_FORM_GNU_addr_index:
20545 attr->set_address (read_addr_index (cu,
20546 attr->as_unsigned_reprocess ()));
20547 break;
20548 case DW_FORM_loclistx:
20549 {
20550 sect_offset loclists_sect_off
20551 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
20552
20553 attr->set_unsigned (to_underlying (loclists_sect_off));
20554 }
20555 break;
20556 case DW_FORM_rnglistx:
20557 {
20558 sect_offset rnglists_sect_off
20559 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
20560
20561 attr->set_unsigned (to_underlying (rnglists_sect_off));
20562 }
20563 break;
20564 case DW_FORM_strx:
20565 case DW_FORM_strx1:
20566 case DW_FORM_strx2:
20567 case DW_FORM_strx3:
20568 case DW_FORM_strx4:
20569 case DW_FORM_GNU_str_index:
20570 {
20571 unsigned int str_index = attr->as_unsigned_reprocess ();
20572 gdb_assert (!attr->canonical_string_p ());
20573 if (reader->dwo_file != NULL)
20574 attr->set_string_noncanonical (read_dwo_str_index (reader,
20575 str_index));
20576 else
20577 attr->set_string_noncanonical (read_stub_str_index (cu,
20578 str_index));
20579 break;
20580 }
20581 default:
20582 gdb_assert_not_reached (_("Unexpected DWARF form."));
20583 }
20584 }
20585
20586 /* Read an attribute value described by an attribute form. */
20587
20588 static const gdb_byte *
20589 read_attribute_value (const struct die_reader_specs *reader,
20590 struct attribute *attr, unsigned form,
20591 LONGEST implicit_const, const gdb_byte *info_ptr)
20592 {
20593 struct dwarf2_cu *cu = reader->cu;
20594 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20595 struct objfile *objfile = per_objfile->objfile;
20596 bfd *abfd = reader->abfd;
20597 struct comp_unit_head *cu_header = &cu->header;
20598 unsigned int bytes_read;
20599 struct dwarf_block *blk;
20600
20601 attr->form = (enum dwarf_form) form;
20602 switch (form)
20603 {
20604 case DW_FORM_ref_addr:
20605 if (cu_header->version == 2)
20606 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
20607 &bytes_read));
20608 else
20609 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20610 &bytes_read));
20611 info_ptr += bytes_read;
20612 break;
20613 case DW_FORM_GNU_ref_alt:
20614 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20615 &bytes_read));
20616 info_ptr += bytes_read;
20617 break;
20618 case DW_FORM_addr:
20619 {
20620 struct gdbarch *gdbarch = objfile->arch ();
20621 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
20622 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20623 attr->set_address (addr);
20624 info_ptr += bytes_read;
20625 }
20626 break;
20627 case DW_FORM_block2:
20628 blk = dwarf_alloc_block (cu);
20629 blk->size = read_2_bytes (abfd, info_ptr);
20630 info_ptr += 2;
20631 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20632 info_ptr += blk->size;
20633 attr->set_block (blk);
20634 break;
20635 case DW_FORM_block4:
20636 blk = dwarf_alloc_block (cu);
20637 blk->size = read_4_bytes (abfd, info_ptr);
20638 info_ptr += 4;
20639 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20640 info_ptr += blk->size;
20641 attr->set_block (blk);
20642 break;
20643 case DW_FORM_data2:
20644 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
20645 info_ptr += 2;
20646 break;
20647 case DW_FORM_data4:
20648 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
20649 info_ptr += 4;
20650 break;
20651 case DW_FORM_data8:
20652 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
20653 info_ptr += 8;
20654 break;
20655 case DW_FORM_data16:
20656 blk = dwarf_alloc_block (cu);
20657 blk->size = 16;
20658 blk->data = read_n_bytes (abfd, info_ptr, 16);
20659 info_ptr += 16;
20660 attr->set_block (blk);
20661 break;
20662 case DW_FORM_sec_offset:
20663 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20664 &bytes_read));
20665 info_ptr += bytes_read;
20666 break;
20667 case DW_FORM_loclistx:
20668 {
20669 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20670 &bytes_read));
20671 info_ptr += bytes_read;
20672 }
20673 break;
20674 case DW_FORM_string:
20675 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20676 &bytes_read));
20677 info_ptr += bytes_read;
20678 break;
20679 case DW_FORM_strp:
20680 if (!cu->per_cu->is_dwz)
20681 {
20682 attr->set_string_noncanonical
20683 (read_indirect_string (per_objfile,
20684 abfd, info_ptr, cu_header,
20685 &bytes_read));
20686 info_ptr += bytes_read;
20687 break;
20688 }
20689 /* FALLTHROUGH */
20690 case DW_FORM_line_strp:
20691 if (!cu->per_cu->is_dwz)
20692 {
20693 attr->set_string_noncanonical
20694 (per_objfile->read_line_string (info_ptr, cu_header,
20695 &bytes_read));
20696 info_ptr += bytes_read;
20697 break;
20698 }
20699 /* FALLTHROUGH */
20700 case DW_FORM_GNU_strp_alt:
20701 {
20702 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20703 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20704 &bytes_read);
20705
20706 attr->set_string_noncanonical
20707 (dwz->read_string (objfile, str_offset));
20708 info_ptr += bytes_read;
20709 }
20710 break;
20711 case DW_FORM_exprloc:
20712 case DW_FORM_block:
20713 blk = dwarf_alloc_block (cu);
20714 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20715 info_ptr += bytes_read;
20716 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20717 info_ptr += blk->size;
20718 attr->set_block (blk);
20719 break;
20720 case DW_FORM_block1:
20721 blk = dwarf_alloc_block (cu);
20722 blk->size = read_1_byte (abfd, info_ptr);
20723 info_ptr += 1;
20724 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20725 info_ptr += blk->size;
20726 attr->set_block (blk);
20727 break;
20728 case DW_FORM_data1:
20729 case DW_FORM_flag:
20730 attr->set_unsigned (read_1_byte (abfd, info_ptr));
20731 info_ptr += 1;
20732 break;
20733 case DW_FORM_flag_present:
20734 attr->set_unsigned (1);
20735 break;
20736 case DW_FORM_sdata:
20737 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
20738 info_ptr += bytes_read;
20739 break;
20740 case DW_FORM_rnglistx:
20741 {
20742 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20743 &bytes_read));
20744 info_ptr += bytes_read;
20745 }
20746 break;
20747 case DW_FORM_udata:
20748 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
20749 info_ptr += bytes_read;
20750 break;
20751 case DW_FORM_ref1:
20752 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20753 + read_1_byte (abfd, info_ptr)));
20754 info_ptr += 1;
20755 break;
20756 case DW_FORM_ref2:
20757 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20758 + read_2_bytes (abfd, info_ptr)));
20759 info_ptr += 2;
20760 break;
20761 case DW_FORM_ref4:
20762 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20763 + read_4_bytes (abfd, info_ptr)));
20764 info_ptr += 4;
20765 break;
20766 case DW_FORM_ref8:
20767 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20768 + read_8_bytes (abfd, info_ptr)));
20769 info_ptr += 8;
20770 break;
20771 case DW_FORM_ref_sig8:
20772 attr->set_signature (read_8_bytes (abfd, info_ptr));
20773 info_ptr += 8;
20774 break;
20775 case DW_FORM_ref_udata:
20776 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20777 + read_unsigned_leb128 (abfd, info_ptr,
20778 &bytes_read)));
20779 info_ptr += bytes_read;
20780 break;
20781 case DW_FORM_indirect:
20782 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20783 info_ptr += bytes_read;
20784 if (form == DW_FORM_implicit_const)
20785 {
20786 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20787 info_ptr += bytes_read;
20788 }
20789 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
20790 info_ptr);
20791 break;
20792 case DW_FORM_implicit_const:
20793 attr->set_signed (implicit_const);
20794 break;
20795 case DW_FORM_addrx:
20796 case DW_FORM_GNU_addr_index:
20797 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20798 &bytes_read));
20799 info_ptr += bytes_read;
20800 break;
20801 case DW_FORM_strx:
20802 case DW_FORM_strx1:
20803 case DW_FORM_strx2:
20804 case DW_FORM_strx3:
20805 case DW_FORM_strx4:
20806 case DW_FORM_GNU_str_index:
20807 {
20808 ULONGEST str_index;
20809 if (form == DW_FORM_strx1)
20810 {
20811 str_index = read_1_byte (abfd, info_ptr);
20812 info_ptr += 1;
20813 }
20814 else if (form == DW_FORM_strx2)
20815 {
20816 str_index = read_2_bytes (abfd, info_ptr);
20817 info_ptr += 2;
20818 }
20819 else if (form == DW_FORM_strx3)
20820 {
20821 str_index = read_3_bytes (abfd, info_ptr);
20822 info_ptr += 3;
20823 }
20824 else if (form == DW_FORM_strx4)
20825 {
20826 str_index = read_4_bytes (abfd, info_ptr);
20827 info_ptr += 4;
20828 }
20829 else
20830 {
20831 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20832 info_ptr += bytes_read;
20833 }
20834 attr->set_unsigned_reprocess (str_index);
20835 }
20836 break;
20837 default:
20838 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
20839 dwarf_form_name (form),
20840 bfd_get_filename (abfd));
20841 }
20842
20843 /* Super hack. */
20844 if (cu->per_cu->is_dwz && attr->form_is_ref ())
20845 attr->form = DW_FORM_GNU_ref_alt;
20846
20847 /* We have seen instances where the compiler tried to emit a byte
20848 size attribute of -1 which ended up being encoded as an unsigned
20849 0xffffffff. Although 0xffffffff is technically a valid size value,
20850 an object of this size seems pretty unlikely so we can relatively
20851 safely treat these cases as if the size attribute was invalid and
20852 treat them as zero by default. */
20853 if (attr->name == DW_AT_byte_size
20854 && form == DW_FORM_data4
20855 && attr->as_unsigned () >= 0xffffffff)
20856 {
20857 complaint
20858 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20859 hex_string (attr->as_unsigned ()));
20860 attr->set_unsigned (0);
20861 }
20862
20863 return info_ptr;
20864 }
20865
20866 /* Read an attribute described by an abbreviated attribute. */
20867
20868 static const gdb_byte *
20869 read_attribute (const struct die_reader_specs *reader,
20870 struct attribute *attr, const struct attr_abbrev *abbrev,
20871 const gdb_byte *info_ptr)
20872 {
20873 attr->name = abbrev->name;
20874 attr->string_is_canonical = 0;
20875 attr->requires_reprocessing = 0;
20876 return read_attribute_value (reader, attr, abbrev->form,
20877 abbrev->implicit_const, info_ptr);
20878 }
20879
20880 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20881
20882 static const char *
20883 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
20884 LONGEST str_offset)
20885 {
20886 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20887 str_offset, "DW_FORM_strp");
20888 }
20889
20890 /* Return pointer to string at .debug_str offset as read from BUF.
20891 BUF is assumed to be in a compilation unit described by CU_HEADER.
20892 Return *BYTES_READ_PTR count of bytes read from BUF. */
20893
20894 static const char *
20895 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
20896 const gdb_byte *buf,
20897 const struct comp_unit_head *cu_header,
20898 unsigned int *bytes_read_ptr)
20899 {
20900 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20901
20902 return read_indirect_string_at_offset (per_objfile, str_offset);
20903 }
20904
20905 /* See read.h. */
20906
20907 const char *
20908 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20909 const struct comp_unit_head *cu_header,
20910 unsigned int *bytes_read_ptr)
20911 {
20912 bfd *abfd = objfile->obfd;
20913 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20914
20915 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20916 }
20917
20918 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20919 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20920 ADDR_SIZE is the size of addresses from the CU header. */
20921
20922 static CORE_ADDR
20923 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20924 gdb::optional<ULONGEST> addr_base, int addr_size)
20925 {
20926 struct objfile *objfile = per_objfile->objfile;
20927 bfd *abfd = objfile->obfd;
20928 const gdb_byte *info_ptr;
20929 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20930
20931 per_objfile->per_bfd->addr.read (objfile);
20932 if (per_objfile->per_bfd->addr.buffer == NULL)
20933 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20934 objfile_name (objfile));
20935 if (addr_base_or_zero + addr_index * addr_size
20936 >= per_objfile->per_bfd->addr.size)
20937 error (_("DW_FORM_addr_index pointing outside of "
20938 ".debug_addr section [in module %s]"),
20939 objfile_name (objfile));
20940 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20941 + addr_index * addr_size);
20942 if (addr_size == 4)
20943 return bfd_get_32 (abfd, info_ptr);
20944 else
20945 return bfd_get_64 (abfd, info_ptr);
20946 }
20947
20948 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20949
20950 static CORE_ADDR
20951 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20952 {
20953 return read_addr_index_1 (cu->per_objfile, addr_index,
20954 cu->addr_base, cu->header.addr_size);
20955 }
20956
20957 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20958
20959 static CORE_ADDR
20960 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20961 unsigned int *bytes_read)
20962 {
20963 bfd *abfd = cu->per_objfile->objfile->obfd;
20964 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20965
20966 return read_addr_index (cu, addr_index);
20967 }
20968
20969 /* See read.h. */
20970
20971 CORE_ADDR
20972 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20973 dwarf2_per_objfile *per_objfile,
20974 unsigned int addr_index)
20975 {
20976 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20977 gdb::optional<ULONGEST> addr_base;
20978 int addr_size;
20979
20980 /* We need addr_base and addr_size.
20981 If we don't have PER_CU->cu, we have to get it.
20982 Nasty, but the alternative is storing the needed info in PER_CU,
20983 which at this point doesn't seem justified: it's not clear how frequently
20984 it would get used and it would increase the size of every PER_CU.
20985 Entry points like dwarf2_per_cu_addr_size do a similar thing
20986 so we're not in uncharted territory here.
20987 Alas we need to be a bit more complicated as addr_base is contained
20988 in the DIE.
20989
20990 We don't need to read the entire CU(/TU).
20991 We just need the header and top level die.
20992
20993 IWBN to use the aging mechanism to let us lazily later discard the CU.
20994 For now we skip this optimization. */
20995
20996 if (cu != NULL)
20997 {
20998 addr_base = cu->addr_base;
20999 addr_size = cu->header.addr_size;
21000 }
21001 else
21002 {
21003 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
21004 addr_base = reader.cu->addr_base;
21005 addr_size = reader.cu->header.addr_size;
21006 }
21007
21008 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
21009 }
21010
21011 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
21012 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
21013 DWO file. */
21014
21015 static const char *
21016 read_str_index (struct dwarf2_cu *cu,
21017 struct dwarf2_section_info *str_section,
21018 struct dwarf2_section_info *str_offsets_section,
21019 ULONGEST str_offsets_base, ULONGEST str_index)
21020 {
21021 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21022 struct objfile *objfile = per_objfile->objfile;
21023 const char *objf_name = objfile_name (objfile);
21024 bfd *abfd = objfile->obfd;
21025 const gdb_byte *info_ptr;
21026 ULONGEST str_offset;
21027 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
21028
21029 str_section->read (objfile);
21030 str_offsets_section->read (objfile);
21031 if (str_section->buffer == NULL)
21032 error (_("%s used without %s section"
21033 " in CU at offset %s [in module %s]"),
21034 form_name, str_section->get_name (),
21035 sect_offset_str (cu->header.sect_off), objf_name);
21036 if (str_offsets_section->buffer == NULL)
21037 error (_("%s used without %s section"
21038 " in CU at offset %s [in module %s]"),
21039 form_name, str_section->get_name (),
21040 sect_offset_str (cu->header.sect_off), objf_name);
21041 info_ptr = (str_offsets_section->buffer
21042 + str_offsets_base
21043 + str_index * cu->header.offset_size);
21044 if (cu->header.offset_size == 4)
21045 str_offset = bfd_get_32 (abfd, info_ptr);
21046 else
21047 str_offset = bfd_get_64 (abfd, info_ptr);
21048 if (str_offset >= str_section->size)
21049 error (_("Offset from %s pointing outside of"
21050 " .debug_str.dwo section in CU at offset %s [in module %s]"),
21051 form_name, sect_offset_str (cu->header.sect_off), objf_name);
21052 return (const char *) (str_section->buffer + str_offset);
21053 }
21054
21055 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
21056
21057 static const char *
21058 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
21059 {
21060 ULONGEST str_offsets_base = reader->cu->header.version >= 5
21061 ? reader->cu->header.addr_size : 0;
21062 return read_str_index (reader->cu,
21063 &reader->dwo_file->sections.str,
21064 &reader->dwo_file->sections.str_offsets,
21065 str_offsets_base, str_index);
21066 }
21067
21068 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
21069
21070 static const char *
21071 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
21072 {
21073 struct objfile *objfile = cu->per_objfile->objfile;
21074 const char *objf_name = objfile_name (objfile);
21075 static const char form_name[] = "DW_FORM_GNU_str_index";
21076 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
21077
21078 if (!cu->str_offsets_base.has_value ())
21079 error (_("%s used in Fission stub without %s"
21080 " in CU at offset 0x%lx [in module %s]"),
21081 form_name, str_offsets_attr_name,
21082 (long) cu->header.offset_size, objf_name);
21083
21084 return read_str_index (cu,
21085 &cu->per_objfile->per_bfd->str,
21086 &cu->per_objfile->per_bfd->str_offsets,
21087 *cu->str_offsets_base, str_index);
21088 }
21089
21090 /* Return the length of an LEB128 number in BUF. */
21091
21092 static int
21093 leb128_size (const gdb_byte *buf)
21094 {
21095 const gdb_byte *begin = buf;
21096 gdb_byte byte;
21097
21098 while (1)
21099 {
21100 byte = *buf++;
21101 if ((byte & 128) == 0)
21102 return buf - begin;
21103 }
21104 }
21105
21106 static void
21107 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
21108 {
21109 switch (lang)
21110 {
21111 case DW_LANG_C89:
21112 case DW_LANG_C99:
21113 case DW_LANG_C11:
21114 case DW_LANG_C:
21115 case DW_LANG_UPC:
21116 cu->language = language_c;
21117 break;
21118 case DW_LANG_Java:
21119 case DW_LANG_C_plus_plus:
21120 case DW_LANG_C_plus_plus_11:
21121 case DW_LANG_C_plus_plus_14:
21122 cu->language = language_cplus;
21123 break;
21124 case DW_LANG_D:
21125 cu->language = language_d;
21126 break;
21127 case DW_LANG_Fortran77:
21128 case DW_LANG_Fortran90:
21129 case DW_LANG_Fortran95:
21130 case DW_LANG_Fortran03:
21131 case DW_LANG_Fortran08:
21132 cu->language = language_fortran;
21133 break;
21134 case DW_LANG_Go:
21135 cu->language = language_go;
21136 break;
21137 case DW_LANG_Mips_Assembler:
21138 cu->language = language_asm;
21139 break;
21140 case DW_LANG_Ada83:
21141 case DW_LANG_Ada95:
21142 cu->language = language_ada;
21143 break;
21144 case DW_LANG_Modula2:
21145 cu->language = language_m2;
21146 break;
21147 case DW_LANG_Pascal83:
21148 cu->language = language_pascal;
21149 break;
21150 case DW_LANG_ObjC:
21151 cu->language = language_objc;
21152 break;
21153 case DW_LANG_Rust:
21154 case DW_LANG_Rust_old:
21155 cu->language = language_rust;
21156 break;
21157 case DW_LANG_Cobol74:
21158 case DW_LANG_Cobol85:
21159 default:
21160 cu->language = language_minimal;
21161 break;
21162 }
21163 cu->language_defn = language_def (cu->language);
21164 }
21165
21166 /* Return the named attribute or NULL if not there. */
21167
21168 static struct attribute *
21169 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
21170 {
21171 for (;;)
21172 {
21173 unsigned int i;
21174 struct attribute *spec = NULL;
21175
21176 for (i = 0; i < die->num_attrs; ++i)
21177 {
21178 if (die->attrs[i].name == name)
21179 return &die->attrs[i];
21180 if (die->attrs[i].name == DW_AT_specification
21181 || die->attrs[i].name == DW_AT_abstract_origin)
21182 spec = &die->attrs[i];
21183 }
21184
21185 if (!spec)
21186 break;
21187
21188 die = follow_die_ref (die, spec, &cu);
21189 }
21190
21191 return NULL;
21192 }
21193
21194 /* Return the string associated with a string-typed attribute, or NULL if it
21195 is either not found or is of an incorrect type. */
21196
21197 static const char *
21198 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
21199 {
21200 struct attribute *attr;
21201 const char *str = NULL;
21202
21203 attr = dwarf2_attr (die, name, cu);
21204
21205 if (attr != NULL)
21206 {
21207 str = attr->as_string ();
21208 if (str == nullptr)
21209 complaint (_("string type expected for attribute %s for "
21210 "DIE at %s in module %s"),
21211 dwarf_attr_name (name), sect_offset_str (die->sect_off),
21212 objfile_name (cu->per_objfile->objfile));
21213 }
21214
21215 return str;
21216 }
21217
21218 /* Return the dwo name or NULL if not present. If present, it is in either
21219 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
21220 static const char *
21221 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
21222 {
21223 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
21224 if (dwo_name == nullptr)
21225 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
21226 return dwo_name;
21227 }
21228
21229 /* Return non-zero iff the attribute NAME is defined for the given DIE,
21230 and holds a non-zero value. This function should only be used for
21231 DW_FORM_flag or DW_FORM_flag_present attributes. */
21232
21233 static int
21234 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
21235 {
21236 struct attribute *attr = dwarf2_attr (die, name, cu);
21237
21238 return attr != nullptr && attr->as_boolean ();
21239 }
21240
21241 static int
21242 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
21243 {
21244 /* A DIE is a declaration if it has a DW_AT_declaration attribute
21245 which value is non-zero. However, we have to be careful with
21246 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
21247 (via dwarf2_flag_true_p) follows this attribute. So we may
21248 end up accidently finding a declaration attribute that belongs
21249 to a different DIE referenced by the specification attribute,
21250 even though the given DIE does not have a declaration attribute. */
21251 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
21252 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
21253 }
21254
21255 /* Return the die giving the specification for DIE, if there is
21256 one. *SPEC_CU is the CU containing DIE on input, and the CU
21257 containing the return value on output. If there is no
21258 specification, but there is an abstract origin, that is
21259 returned. */
21260
21261 static struct die_info *
21262 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
21263 {
21264 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
21265 *spec_cu);
21266
21267 if (spec_attr == NULL)
21268 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
21269
21270 if (spec_attr == NULL)
21271 return NULL;
21272 else
21273 return follow_die_ref (die, spec_attr, spec_cu);
21274 }
21275
21276 /* Stub for free_line_header to match void * callback types. */
21277
21278 static void
21279 free_line_header_voidp (void *arg)
21280 {
21281 struct line_header *lh = (struct line_header *) arg;
21282
21283 delete lh;
21284 }
21285
21286 /* A convenience function to find the proper .debug_line section for a CU. */
21287
21288 static struct dwarf2_section_info *
21289 get_debug_line_section (struct dwarf2_cu *cu)
21290 {
21291 struct dwarf2_section_info *section;
21292 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21293
21294 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
21295 DWO file. */
21296 if (cu->dwo_unit && cu->per_cu->is_debug_types)
21297 section = &cu->dwo_unit->dwo_file->sections.line;
21298 else if (cu->per_cu->is_dwz)
21299 {
21300 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
21301
21302 section = &dwz->line;
21303 }
21304 else
21305 section = &per_objfile->per_bfd->line;
21306
21307 return section;
21308 }
21309
21310 /* Read the statement program header starting at OFFSET in
21311 .debug_line, or .debug_line.dwo. Return a pointer
21312 to a struct line_header, allocated using xmalloc.
21313 Returns NULL if there is a problem reading the header, e.g., if it
21314 has a version we don't understand.
21315
21316 NOTE: the strings in the include directory and file name tables of
21317 the returned object point into the dwarf line section buffer,
21318 and must not be freed. */
21319
21320 static line_header_up
21321 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
21322 {
21323 struct dwarf2_section_info *section;
21324 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21325
21326 section = get_debug_line_section (cu);
21327 section->read (per_objfile->objfile);
21328 if (section->buffer == NULL)
21329 {
21330 if (cu->dwo_unit && cu->per_cu->is_debug_types)
21331 complaint (_("missing .debug_line.dwo section"));
21332 else
21333 complaint (_("missing .debug_line section"));
21334 return 0;
21335 }
21336
21337 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
21338 per_objfile, section, &cu->header);
21339 }
21340
21341 /* Subroutine of dwarf_decode_lines to simplify it.
21342 Return the file name of the psymtab for the given file_entry.
21343 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21344 If space for the result is malloc'd, *NAME_HOLDER will be set.
21345 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
21346
21347 static const char *
21348 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
21349 const dwarf2_psymtab *pst,
21350 const char *comp_dir,
21351 gdb::unique_xmalloc_ptr<char> *name_holder)
21352 {
21353 const char *include_name = fe.name;
21354 const char *include_name_to_compare = include_name;
21355 const char *pst_filename;
21356 int file_is_pst;
21357
21358 const char *dir_name = fe.include_dir (lh);
21359
21360 gdb::unique_xmalloc_ptr<char> hold_compare;
21361 if (!IS_ABSOLUTE_PATH (include_name)
21362 && (dir_name != NULL || comp_dir != NULL))
21363 {
21364 /* Avoid creating a duplicate psymtab for PST.
21365 We do this by comparing INCLUDE_NAME and PST_FILENAME.
21366 Before we do the comparison, however, we need to account
21367 for DIR_NAME and COMP_DIR.
21368 First prepend dir_name (if non-NULL). If we still don't
21369 have an absolute path prepend comp_dir (if non-NULL).
21370 However, the directory we record in the include-file's
21371 psymtab does not contain COMP_DIR (to match the
21372 corresponding symtab(s)).
21373
21374 Example:
21375
21376 bash$ cd /tmp
21377 bash$ gcc -g ./hello.c
21378 include_name = "hello.c"
21379 dir_name = "."
21380 DW_AT_comp_dir = comp_dir = "/tmp"
21381 DW_AT_name = "./hello.c"
21382
21383 */
21384
21385 if (dir_name != NULL)
21386 {
21387 name_holder->reset (concat (dir_name, SLASH_STRING,
21388 include_name, (char *) NULL));
21389 include_name = name_holder->get ();
21390 include_name_to_compare = include_name;
21391 }
21392 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
21393 {
21394 hold_compare.reset (concat (comp_dir, SLASH_STRING,
21395 include_name, (char *) NULL));
21396 include_name_to_compare = hold_compare.get ();
21397 }
21398 }
21399
21400 pst_filename = pst->filename;
21401 gdb::unique_xmalloc_ptr<char> copied_name;
21402 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
21403 {
21404 copied_name.reset (concat (pst->dirname, SLASH_STRING,
21405 pst_filename, (char *) NULL));
21406 pst_filename = copied_name.get ();
21407 }
21408
21409 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
21410
21411 if (file_is_pst)
21412 return NULL;
21413 return include_name;
21414 }
21415
21416 /* State machine to track the state of the line number program. */
21417
21418 class lnp_state_machine
21419 {
21420 public:
21421 /* Initialize a machine state for the start of a line number
21422 program. */
21423 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
21424 bool record_lines_p);
21425
21426 file_entry *current_file ()
21427 {
21428 /* lh->file_names is 0-based, but the file name numbers in the
21429 statement program are 1-based. */
21430 return m_line_header->file_name_at (m_file);
21431 }
21432
21433 /* Record the line in the state machine. END_SEQUENCE is true if
21434 we're processing the end of a sequence. */
21435 void record_line (bool end_sequence);
21436
21437 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
21438 nop-out rest of the lines in this sequence. */
21439 void check_line_address (struct dwarf2_cu *cu,
21440 const gdb_byte *line_ptr,
21441 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
21442
21443 void handle_set_discriminator (unsigned int discriminator)
21444 {
21445 m_discriminator = discriminator;
21446 m_line_has_non_zero_discriminator |= discriminator != 0;
21447 }
21448
21449 /* Handle DW_LNE_set_address. */
21450 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21451 {
21452 m_op_index = 0;
21453 address += baseaddr;
21454 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21455 }
21456
21457 /* Handle DW_LNS_advance_pc. */
21458 void handle_advance_pc (CORE_ADDR adjust);
21459
21460 /* Handle a special opcode. */
21461 void handle_special_opcode (unsigned char op_code);
21462
21463 /* Handle DW_LNS_advance_line. */
21464 void handle_advance_line (int line_delta)
21465 {
21466 advance_line (line_delta);
21467 }
21468
21469 /* Handle DW_LNS_set_file. */
21470 void handle_set_file (file_name_index file);
21471
21472 /* Handle DW_LNS_negate_stmt. */
21473 void handle_negate_stmt ()
21474 {
21475 m_is_stmt = !m_is_stmt;
21476 }
21477
21478 /* Handle DW_LNS_const_add_pc. */
21479 void handle_const_add_pc ();
21480
21481 /* Handle DW_LNS_fixed_advance_pc. */
21482 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21483 {
21484 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21485 m_op_index = 0;
21486 }
21487
21488 /* Handle DW_LNS_copy. */
21489 void handle_copy ()
21490 {
21491 record_line (false);
21492 m_discriminator = 0;
21493 }
21494
21495 /* Handle DW_LNE_end_sequence. */
21496 void handle_end_sequence ()
21497 {
21498 m_currently_recording_lines = true;
21499 }
21500
21501 private:
21502 /* Advance the line by LINE_DELTA. */
21503 void advance_line (int line_delta)
21504 {
21505 m_line += line_delta;
21506
21507 if (line_delta != 0)
21508 m_line_has_non_zero_discriminator = m_discriminator != 0;
21509 }
21510
21511 struct dwarf2_cu *m_cu;
21512
21513 gdbarch *m_gdbarch;
21514
21515 /* True if we're recording lines.
21516 Otherwise we're building partial symtabs and are just interested in
21517 finding include files mentioned by the line number program. */
21518 bool m_record_lines_p;
21519
21520 /* The line number header. */
21521 line_header *m_line_header;
21522
21523 /* These are part of the standard DWARF line number state machine,
21524 and initialized according to the DWARF spec. */
21525
21526 unsigned char m_op_index = 0;
21527 /* The line table index of the current file. */
21528 file_name_index m_file = 1;
21529 unsigned int m_line = 1;
21530
21531 /* These are initialized in the constructor. */
21532
21533 CORE_ADDR m_address;
21534 bool m_is_stmt;
21535 unsigned int m_discriminator;
21536
21537 /* Additional bits of state we need to track. */
21538
21539 /* The last file that we called dwarf2_start_subfile for.
21540 This is only used for TLLs. */
21541 unsigned int m_last_file = 0;
21542 /* The last file a line number was recorded for. */
21543 struct subfile *m_last_subfile = NULL;
21544
21545 /* The address of the last line entry. */
21546 CORE_ADDR m_last_address;
21547
21548 /* Set to true when a previous line at the same address (using
21549 m_last_address) had m_is_stmt true. This is reset to false when a
21550 line entry at a new address (m_address different to m_last_address) is
21551 processed. */
21552 bool m_stmt_at_address = false;
21553
21554 /* When true, record the lines we decode. */
21555 bool m_currently_recording_lines = false;
21556
21557 /* The last line number that was recorded, used to coalesce
21558 consecutive entries for the same line. This can happen, for
21559 example, when discriminators are present. PR 17276. */
21560 unsigned int m_last_line = 0;
21561 bool m_line_has_non_zero_discriminator = false;
21562 };
21563
21564 void
21565 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21566 {
21567 CORE_ADDR addr_adj = (((m_op_index + adjust)
21568 / m_line_header->maximum_ops_per_instruction)
21569 * m_line_header->minimum_instruction_length);
21570 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21571 m_op_index = ((m_op_index + adjust)
21572 % m_line_header->maximum_ops_per_instruction);
21573 }
21574
21575 void
21576 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21577 {
21578 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21579 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21580 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21581 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
21582 / m_line_header->maximum_ops_per_instruction)
21583 * m_line_header->minimum_instruction_length);
21584 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21585 m_op_index = ((m_op_index + adj_opcode_d)
21586 % m_line_header->maximum_ops_per_instruction);
21587
21588 int line_delta = m_line_header->line_base + adj_opcode_r;
21589 advance_line (line_delta);
21590 record_line (false);
21591 m_discriminator = 0;
21592 }
21593
21594 void
21595 lnp_state_machine::handle_set_file (file_name_index file)
21596 {
21597 m_file = file;
21598
21599 const file_entry *fe = current_file ();
21600 if (fe == NULL)
21601 dwarf2_debug_line_missing_file_complaint ();
21602 else if (m_record_lines_p)
21603 {
21604 const char *dir = fe->include_dir (m_line_header);
21605
21606 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21607 m_line_has_non_zero_discriminator = m_discriminator != 0;
21608 dwarf2_start_subfile (m_cu, fe->name, dir);
21609 }
21610 }
21611
21612 void
21613 lnp_state_machine::handle_const_add_pc ()
21614 {
21615 CORE_ADDR adjust
21616 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21617
21618 CORE_ADDR addr_adj
21619 = (((m_op_index + adjust)
21620 / m_line_header->maximum_ops_per_instruction)
21621 * m_line_header->minimum_instruction_length);
21622
21623 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21624 m_op_index = ((m_op_index + adjust)
21625 % m_line_header->maximum_ops_per_instruction);
21626 }
21627
21628 /* Return non-zero if we should add LINE to the line number table.
21629 LINE is the line to add, LAST_LINE is the last line that was added,
21630 LAST_SUBFILE is the subfile for LAST_LINE.
21631 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21632 had a non-zero discriminator.
21633
21634 We have to be careful in the presence of discriminators.
21635 E.g., for this line:
21636
21637 for (i = 0; i < 100000; i++);
21638
21639 clang can emit four line number entries for that one line,
21640 each with a different discriminator.
21641 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21642
21643 However, we want gdb to coalesce all four entries into one.
21644 Otherwise the user could stepi into the middle of the line and
21645 gdb would get confused about whether the pc really was in the
21646 middle of the line.
21647
21648 Things are further complicated by the fact that two consecutive
21649 line number entries for the same line is a heuristic used by gcc
21650 to denote the end of the prologue. So we can't just discard duplicate
21651 entries, we have to be selective about it. The heuristic we use is
21652 that we only collapse consecutive entries for the same line if at least
21653 one of those entries has a non-zero discriminator. PR 17276.
21654
21655 Note: Addresses in the line number state machine can never go backwards
21656 within one sequence, thus this coalescing is ok. */
21657
21658 static int
21659 dwarf_record_line_p (struct dwarf2_cu *cu,
21660 unsigned int line, unsigned int last_line,
21661 int line_has_non_zero_discriminator,
21662 struct subfile *last_subfile)
21663 {
21664 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21665 return 1;
21666 if (line != last_line)
21667 return 1;
21668 /* Same line for the same file that we've seen already.
21669 As a last check, for pr 17276, only record the line if the line
21670 has never had a non-zero discriminator. */
21671 if (!line_has_non_zero_discriminator)
21672 return 1;
21673 return 0;
21674 }
21675
21676 /* Use the CU's builder to record line number LINE beginning at
21677 address ADDRESS in the line table of subfile SUBFILE. */
21678
21679 static void
21680 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21681 unsigned int line, CORE_ADDR address, bool is_stmt,
21682 struct dwarf2_cu *cu)
21683 {
21684 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21685
21686 if (dwarf_line_debug)
21687 {
21688 fprintf_unfiltered (gdb_stdlog,
21689 "Recording line %u, file %s, address %s\n",
21690 line, lbasename (subfile->name),
21691 paddress (gdbarch, address));
21692 }
21693
21694 if (cu != nullptr)
21695 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
21696 }
21697
21698 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21699 Mark the end of a set of line number records.
21700 The arguments are the same as for dwarf_record_line_1.
21701 If SUBFILE is NULL the request is ignored. */
21702
21703 static void
21704 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21705 CORE_ADDR address, struct dwarf2_cu *cu)
21706 {
21707 if (subfile == NULL)
21708 return;
21709
21710 if (dwarf_line_debug)
21711 {
21712 fprintf_unfiltered (gdb_stdlog,
21713 "Finishing current line, file %s, address %s\n",
21714 lbasename (subfile->name),
21715 paddress (gdbarch, address));
21716 }
21717
21718 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
21719 }
21720
21721 void
21722 lnp_state_machine::record_line (bool end_sequence)
21723 {
21724 if (dwarf_line_debug)
21725 {
21726 fprintf_unfiltered (gdb_stdlog,
21727 "Processing actual line %u: file %u,"
21728 " address %s, is_stmt %u, discrim %u%s\n",
21729 m_line, m_file,
21730 paddress (m_gdbarch, m_address),
21731 m_is_stmt, m_discriminator,
21732 (end_sequence ? "\t(end sequence)" : ""));
21733 }
21734
21735 file_entry *fe = current_file ();
21736
21737 if (fe == NULL)
21738 dwarf2_debug_line_missing_file_complaint ();
21739 /* For now we ignore lines not starting on an instruction boundary.
21740 But not when processing end_sequence for compatibility with the
21741 previous version of the code. */
21742 else if (m_op_index == 0 || end_sequence)
21743 {
21744 fe->included_p = 1;
21745 if (m_record_lines_p)
21746 {
21747 /* When we switch files we insert an end maker in the first file,
21748 switch to the second file and add a new line entry. The
21749 problem is that the end marker inserted in the first file will
21750 discard any previous line entries at the same address. If the
21751 line entries in the first file are marked as is-stmt, while
21752 the new line in the second file is non-stmt, then this means
21753 the end marker will discard is-stmt lines so we can have a
21754 non-stmt line. This means that there are less addresses at
21755 which the user can insert a breakpoint.
21756
21757 To improve this we track the last address in m_last_address,
21758 and whether we have seen an is-stmt at this address. Then
21759 when switching files, if we have seen a stmt at the current
21760 address, and we are switching to create a non-stmt line, then
21761 discard the new line. */
21762 bool file_changed
21763 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21764 bool ignore_this_line
21765 = ((file_changed && !end_sequence && m_last_address == m_address
21766 && !m_is_stmt && m_stmt_at_address)
21767 || (!end_sequence && m_line == 0));
21768
21769 if ((file_changed && !ignore_this_line) || end_sequence)
21770 {
21771 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21772 m_currently_recording_lines ? m_cu : nullptr);
21773 }
21774
21775 if (!end_sequence && !ignore_this_line)
21776 {
21777 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21778
21779 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21780 m_line_has_non_zero_discriminator,
21781 m_last_subfile))
21782 {
21783 buildsym_compunit *builder = m_cu->get_builder ();
21784 dwarf_record_line_1 (m_gdbarch,
21785 builder->get_current_subfile (),
21786 m_line, m_address, is_stmt,
21787 m_currently_recording_lines ? m_cu : nullptr);
21788 }
21789 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21790 m_last_line = m_line;
21791 }
21792 }
21793 }
21794
21795 /* Track whether we have seen any m_is_stmt true at m_address in case we
21796 have multiple line table entries all at m_address. */
21797 if (m_last_address != m_address)
21798 {
21799 m_stmt_at_address = false;
21800 m_last_address = m_address;
21801 }
21802 m_stmt_at_address |= m_is_stmt;
21803 }
21804
21805 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21806 line_header *lh, bool record_lines_p)
21807 {
21808 m_cu = cu;
21809 m_gdbarch = arch;
21810 m_record_lines_p = record_lines_p;
21811 m_line_header = lh;
21812
21813 m_currently_recording_lines = true;
21814
21815 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21816 was a line entry for it so that the backend has a chance to adjust it
21817 and also record it in case it needs it. This is currently used by MIPS
21818 code, cf. `mips_adjust_dwarf2_line'. */
21819 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21820 m_is_stmt = lh->default_is_stmt;
21821 m_discriminator = 0;
21822
21823 m_last_address = m_address;
21824 m_stmt_at_address = false;
21825 }
21826
21827 void
21828 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21829 const gdb_byte *line_ptr,
21830 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21831 {
21832 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21833 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21834 located at 0x0. In this case, additionally check that if
21835 ADDRESS < UNRELOCATED_LOWPC. */
21836
21837 if ((address == 0 && address < unrelocated_lowpc)
21838 || address == (CORE_ADDR) -1)
21839 {
21840 /* This line table is for a function which has been
21841 GCd by the linker. Ignore it. PR gdb/12528 */
21842
21843 struct objfile *objfile = cu->per_objfile->objfile;
21844 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21845
21846 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21847 line_offset, objfile_name (objfile));
21848 m_currently_recording_lines = false;
21849 /* Note: m_currently_recording_lines is left as false until we see
21850 DW_LNE_end_sequence. */
21851 }
21852 }
21853
21854 /* Subroutine of dwarf_decode_lines to simplify it.
21855 Process the line number information in LH.
21856 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21857 program in order to set included_p for every referenced header. */
21858
21859 static void
21860 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21861 const int decode_for_pst_p, CORE_ADDR lowpc)
21862 {
21863 const gdb_byte *line_ptr, *extended_end;
21864 const gdb_byte *line_end;
21865 unsigned int bytes_read, extended_len;
21866 unsigned char op_code, extended_op;
21867 CORE_ADDR baseaddr;
21868 struct objfile *objfile = cu->per_objfile->objfile;
21869 bfd *abfd = objfile->obfd;
21870 struct gdbarch *gdbarch = objfile->arch ();
21871 /* True if we're recording line info (as opposed to building partial
21872 symtabs and just interested in finding include files mentioned by
21873 the line number program). */
21874 bool record_lines_p = !decode_for_pst_p;
21875
21876 baseaddr = objfile->text_section_offset ();
21877
21878 line_ptr = lh->statement_program_start;
21879 line_end = lh->statement_program_end;
21880
21881 /* Read the statement sequences until there's nothing left. */
21882 while (line_ptr < line_end)
21883 {
21884 /* The DWARF line number program state machine. Reset the state
21885 machine at the start of each sequence. */
21886 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21887 bool end_sequence = false;
21888
21889 if (record_lines_p)
21890 {
21891 /* Start a subfile for the current file of the state
21892 machine. */
21893 const file_entry *fe = state_machine.current_file ();
21894
21895 if (fe != NULL)
21896 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21897 }
21898
21899 /* Decode the table. */
21900 while (line_ptr < line_end && !end_sequence)
21901 {
21902 op_code = read_1_byte (abfd, line_ptr);
21903 line_ptr += 1;
21904
21905 if (op_code >= lh->opcode_base)
21906 {
21907 /* Special opcode. */
21908 state_machine.handle_special_opcode (op_code);
21909 }
21910 else switch (op_code)
21911 {
21912 case DW_LNS_extended_op:
21913 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21914 &bytes_read);
21915 line_ptr += bytes_read;
21916 extended_end = line_ptr + extended_len;
21917 extended_op = read_1_byte (abfd, line_ptr);
21918 line_ptr += 1;
21919 if (DW_LNE_lo_user <= extended_op
21920 && extended_op <= DW_LNE_hi_user)
21921 {
21922 /* Vendor extension, ignore. */
21923 line_ptr = extended_end;
21924 break;
21925 }
21926 switch (extended_op)
21927 {
21928 case DW_LNE_end_sequence:
21929 state_machine.handle_end_sequence ();
21930 end_sequence = true;
21931 break;
21932 case DW_LNE_set_address:
21933 {
21934 CORE_ADDR address
21935 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21936 line_ptr += bytes_read;
21937
21938 state_machine.check_line_address (cu, line_ptr,
21939 lowpc - baseaddr, address);
21940 state_machine.handle_set_address (baseaddr, address);
21941 }
21942 break;
21943 case DW_LNE_define_file:
21944 {
21945 const char *cur_file;
21946 unsigned int mod_time, length;
21947 dir_index dindex;
21948
21949 cur_file = read_direct_string (abfd, line_ptr,
21950 &bytes_read);
21951 line_ptr += bytes_read;
21952 dindex = (dir_index)
21953 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21954 line_ptr += bytes_read;
21955 mod_time =
21956 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21957 line_ptr += bytes_read;
21958 length =
21959 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21960 line_ptr += bytes_read;
21961 lh->add_file_name (cur_file, dindex, mod_time, length);
21962 }
21963 break;
21964 case DW_LNE_set_discriminator:
21965 {
21966 /* The discriminator is not interesting to the
21967 debugger; just ignore it. We still need to
21968 check its value though:
21969 if there are consecutive entries for the same
21970 (non-prologue) line we want to coalesce them.
21971 PR 17276. */
21972 unsigned int discr
21973 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21974 line_ptr += bytes_read;
21975
21976 state_machine.handle_set_discriminator (discr);
21977 }
21978 break;
21979 default:
21980 complaint (_("mangled .debug_line section"));
21981 return;
21982 }
21983 /* Make sure that we parsed the extended op correctly. If e.g.
21984 we expected a different address size than the producer used,
21985 we may have read the wrong number of bytes. */
21986 if (line_ptr != extended_end)
21987 {
21988 complaint (_("mangled .debug_line section"));
21989 return;
21990 }
21991 break;
21992 case DW_LNS_copy:
21993 state_machine.handle_copy ();
21994 break;
21995 case DW_LNS_advance_pc:
21996 {
21997 CORE_ADDR adjust
21998 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21999 line_ptr += bytes_read;
22000
22001 state_machine.handle_advance_pc (adjust);
22002 }
22003 break;
22004 case DW_LNS_advance_line:
22005 {
22006 int line_delta
22007 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
22008 line_ptr += bytes_read;
22009
22010 state_machine.handle_advance_line (line_delta);
22011 }
22012 break;
22013 case DW_LNS_set_file:
22014 {
22015 file_name_index file
22016 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
22017 &bytes_read);
22018 line_ptr += bytes_read;
22019
22020 state_machine.handle_set_file (file);
22021 }
22022 break;
22023 case DW_LNS_set_column:
22024 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
22025 line_ptr += bytes_read;
22026 break;
22027 case DW_LNS_negate_stmt:
22028 state_machine.handle_negate_stmt ();
22029 break;
22030 case DW_LNS_set_basic_block:
22031 break;
22032 /* Add to the address register of the state machine the
22033 address increment value corresponding to special opcode
22034 255. I.e., this value is scaled by the minimum
22035 instruction length since special opcode 255 would have
22036 scaled the increment. */
22037 case DW_LNS_const_add_pc:
22038 state_machine.handle_const_add_pc ();
22039 break;
22040 case DW_LNS_fixed_advance_pc:
22041 {
22042 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
22043 line_ptr += 2;
22044
22045 state_machine.handle_fixed_advance_pc (addr_adj);
22046 }
22047 break;
22048 default:
22049 {
22050 /* Unknown standard opcode, ignore it. */
22051 int i;
22052
22053 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
22054 {
22055 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
22056 line_ptr += bytes_read;
22057 }
22058 }
22059 }
22060 }
22061
22062 if (!end_sequence)
22063 dwarf2_debug_line_missing_end_sequence_complaint ();
22064
22065 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
22066 in which case we still finish recording the last line). */
22067 state_machine.record_line (true);
22068 }
22069 }
22070
22071 /* Decode the Line Number Program (LNP) for the given line_header
22072 structure and CU. The actual information extracted and the type
22073 of structures created from the LNP depends on the value of PST.
22074
22075 1. If PST is NULL, then this procedure uses the data from the program
22076 to create all necessary symbol tables, and their linetables.
22077
22078 2. If PST is not NULL, this procedure reads the program to determine
22079 the list of files included by the unit represented by PST, and
22080 builds all the associated partial symbol tables.
22081
22082 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
22083 It is used for relative paths in the line table.
22084 NOTE: When processing partial symtabs (pst != NULL),
22085 comp_dir == pst->dirname.
22086
22087 NOTE: It is important that psymtabs have the same file name (via strcmp)
22088 as the corresponding symtab. Since COMP_DIR is not used in the name of the
22089 symtab we don't use it in the name of the psymtabs we create.
22090 E.g. expand_line_sal requires this when finding psymtabs to expand.
22091 A good testcase for this is mb-inline.exp.
22092
22093 LOWPC is the lowest address in CU (or 0 if not known).
22094
22095 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
22096 for its PC<->lines mapping information. Otherwise only the filename
22097 table is read in. */
22098
22099 static void
22100 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
22101 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
22102 CORE_ADDR lowpc, int decode_mapping)
22103 {
22104 struct objfile *objfile = cu->per_objfile->objfile;
22105 const int decode_for_pst_p = (pst != NULL);
22106
22107 if (decode_mapping)
22108 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
22109
22110 if (decode_for_pst_p)
22111 {
22112 /* Now that we're done scanning the Line Header Program, we can
22113 create the psymtab of each included file. */
22114 for (auto &file_entry : lh->file_names ())
22115 if (file_entry.included_p == 1)
22116 {
22117 gdb::unique_xmalloc_ptr<char> name_holder;
22118 const char *include_name =
22119 psymtab_include_file_name (lh, file_entry, pst,
22120 comp_dir, &name_holder);
22121 if (include_name != NULL)
22122 dwarf2_create_include_psymtab
22123 (cu->per_objfile->per_bfd, include_name, pst,
22124 cu->per_objfile->per_bfd->partial_symtabs.get (),
22125 objfile->per_bfd);
22126 }
22127 }
22128 else
22129 {
22130 /* Make sure a symtab is created for every file, even files
22131 which contain only variables (i.e. no code with associated
22132 line numbers). */
22133 buildsym_compunit *builder = cu->get_builder ();
22134 struct compunit_symtab *cust = builder->get_compunit_symtab ();
22135
22136 for (auto &fe : lh->file_names ())
22137 {
22138 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
22139 if (builder->get_current_subfile ()->symtab == NULL)
22140 {
22141 builder->get_current_subfile ()->symtab
22142 = allocate_symtab (cust,
22143 builder->get_current_subfile ()->name);
22144 }
22145 fe.symtab = builder->get_current_subfile ()->symtab;
22146 }
22147 }
22148 }
22149
22150 /* Start a subfile for DWARF. FILENAME is the name of the file and
22151 DIRNAME the name of the source directory which contains FILENAME
22152 or NULL if not known.
22153 This routine tries to keep line numbers from identical absolute and
22154 relative file names in a common subfile.
22155
22156 Using the `list' example from the GDB testsuite, which resides in
22157 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
22158 of /srcdir/list0.c yields the following debugging information for list0.c:
22159
22160 DW_AT_name: /srcdir/list0.c
22161 DW_AT_comp_dir: /compdir
22162 files.files[0].name: list0.h
22163 files.files[0].dir: /srcdir
22164 files.files[1].name: list0.c
22165 files.files[1].dir: /srcdir
22166
22167 The line number information for list0.c has to end up in a single
22168 subfile, so that `break /srcdir/list0.c:1' works as expected.
22169 start_subfile will ensure that this happens provided that we pass the
22170 concatenation of files.files[1].dir and files.files[1].name as the
22171 subfile's name. */
22172
22173 static void
22174 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
22175 const char *dirname)
22176 {
22177 gdb::unique_xmalloc_ptr<char> copy;
22178
22179 /* In order not to lose the line information directory,
22180 we concatenate it to the filename when it makes sense.
22181 Note that the Dwarf3 standard says (speaking of filenames in line
22182 information): ``The directory index is ignored for file names
22183 that represent full path names''. Thus ignoring dirname in the
22184 `else' branch below isn't an issue. */
22185
22186 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
22187 {
22188 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
22189 filename = copy.get ();
22190 }
22191
22192 cu->get_builder ()->start_subfile (filename);
22193 }
22194
22195 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
22196 buildsym_compunit constructor. */
22197
22198 struct compunit_symtab *
22199 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
22200 CORE_ADDR low_pc)
22201 {
22202 gdb_assert (m_builder == nullptr);
22203
22204 m_builder.reset (new struct buildsym_compunit
22205 (this->per_objfile->objfile,
22206 name, comp_dir, language, low_pc));
22207
22208 list_in_scope = get_builder ()->get_file_symbols ();
22209
22210 get_builder ()->record_debugformat ("DWARF 2");
22211 get_builder ()->record_producer (producer);
22212
22213 processing_has_namespace_info = false;
22214
22215 return get_builder ()->get_compunit_symtab ();
22216 }
22217
22218 static void
22219 var_decode_location (struct attribute *attr, struct symbol *sym,
22220 struct dwarf2_cu *cu)
22221 {
22222 struct objfile *objfile = cu->per_objfile->objfile;
22223 struct comp_unit_head *cu_header = &cu->header;
22224
22225 /* NOTE drow/2003-01-30: There used to be a comment and some special
22226 code here to turn a symbol with DW_AT_external and a
22227 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
22228 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
22229 with some versions of binutils) where shared libraries could have
22230 relocations against symbols in their debug information - the
22231 minimal symbol would have the right address, but the debug info
22232 would not. It's no longer necessary, because we will explicitly
22233 apply relocations when we read in the debug information now. */
22234
22235 /* A DW_AT_location attribute with no contents indicates that a
22236 variable has been optimized away. */
22237 if (attr->form_is_block () && attr->as_block ()->size == 0)
22238 {
22239 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22240 return;
22241 }
22242
22243 /* Handle one degenerate form of location expression specially, to
22244 preserve GDB's previous behavior when section offsets are
22245 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
22246 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
22247
22248 if (attr->form_is_block ())
22249 {
22250 struct dwarf_block *block = attr->as_block ();
22251
22252 if ((block->data[0] == DW_OP_addr
22253 && block->size == 1 + cu_header->addr_size)
22254 || ((block->data[0] == DW_OP_GNU_addr_index
22255 || block->data[0] == DW_OP_addrx)
22256 && (block->size
22257 == 1 + leb128_size (&block->data[1]))))
22258 {
22259 unsigned int dummy;
22260
22261 if (block->data[0] == DW_OP_addr)
22262 SET_SYMBOL_VALUE_ADDRESS
22263 (sym, cu->header.read_address (objfile->obfd,
22264 block->data + 1,
22265 &dummy));
22266 else
22267 SET_SYMBOL_VALUE_ADDRESS
22268 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
22269 &dummy));
22270 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
22271 fixup_symbol_section (sym, objfile);
22272 SET_SYMBOL_VALUE_ADDRESS
22273 (sym,
22274 SYMBOL_VALUE_ADDRESS (sym)
22275 + objfile->section_offsets[sym->section_index ()]);
22276 return;
22277 }
22278 }
22279
22280 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
22281 expression evaluator, and use LOC_COMPUTED only when necessary
22282 (i.e. when the value of a register or memory location is
22283 referenced, or a thread-local block, etc.). Then again, it might
22284 not be worthwhile. I'm assuming that it isn't unless performance
22285 or memory numbers show me otherwise. */
22286
22287 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
22288
22289 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
22290 cu->has_loclist = true;
22291 }
22292
22293 /* Given a pointer to a DWARF information entry, figure out if we need
22294 to make a symbol table entry for it, and if so, create a new entry
22295 and return a pointer to it.
22296 If TYPE is NULL, determine symbol type from the die, otherwise
22297 used the passed type.
22298 If SPACE is not NULL, use it to hold the new symbol. If it is
22299 NULL, allocate a new symbol on the objfile's obstack. */
22300
22301 static struct symbol *
22302 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
22303 struct symbol *space)
22304 {
22305 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22306 struct objfile *objfile = per_objfile->objfile;
22307 struct gdbarch *gdbarch = objfile->arch ();
22308 struct symbol *sym = NULL;
22309 const char *name;
22310 struct attribute *attr = NULL;
22311 struct attribute *attr2 = NULL;
22312 CORE_ADDR baseaddr;
22313 struct pending **list_to_add = NULL;
22314
22315 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
22316
22317 baseaddr = objfile->text_section_offset ();
22318
22319 name = dwarf2_name (die, cu);
22320 if (name)
22321 {
22322 int suppress_add = 0;
22323
22324 if (space)
22325 sym = space;
22326 else
22327 sym = new (&objfile->objfile_obstack) symbol;
22328 OBJSTAT (objfile, n_syms++);
22329
22330 /* Cache this symbol's name and the name's demangled form (if any). */
22331 sym->set_language (cu->language, &objfile->objfile_obstack);
22332 /* Fortran does not have mangling standard and the mangling does differ
22333 between gfortran, iFort etc. */
22334 const char *physname
22335 = (cu->language == language_fortran
22336 ? dwarf2_full_name (name, die, cu)
22337 : dwarf2_physname (name, die, cu));
22338 const char *linkagename = dw2_linkage_name (die, cu);
22339
22340 if (linkagename == nullptr || cu->language == language_ada)
22341 sym->set_linkage_name (physname);
22342 else
22343 {
22344 sym->set_demangled_name (physname, &objfile->objfile_obstack);
22345 sym->set_linkage_name (linkagename);
22346 }
22347
22348 /* Default assumptions.
22349 Use the passed type or decode it from the die. */
22350 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22351 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22352 if (type != NULL)
22353 SYMBOL_TYPE (sym) = type;
22354 else
22355 SYMBOL_TYPE (sym) = die_type (die, cu);
22356 attr = dwarf2_attr (die,
22357 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
22358 cu);
22359 if (attr != nullptr)
22360 SYMBOL_LINE (sym) = attr->constant_value (0);
22361
22362 attr = dwarf2_attr (die,
22363 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
22364 cu);
22365 if (attr != nullptr && attr->is_nonnegative ())
22366 {
22367 file_name_index file_index
22368 = (file_name_index) attr->as_nonnegative ();
22369 struct file_entry *fe;
22370
22371 if (cu->line_header != NULL)
22372 fe = cu->line_header->file_name_at (file_index);
22373 else
22374 fe = NULL;
22375
22376 if (fe == NULL)
22377 complaint (_("file index out of range"));
22378 else
22379 symbol_set_symtab (sym, fe->symtab);
22380 }
22381
22382 switch (die->tag)
22383 {
22384 case DW_TAG_label:
22385 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
22386 if (attr != nullptr)
22387 {
22388 CORE_ADDR addr;
22389
22390 addr = attr->as_address ();
22391 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
22392 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
22393 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
22394 }
22395 else
22396 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22397 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
22398 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
22399 add_symbol_to_list (sym, cu->list_in_scope);
22400 break;
22401 case DW_TAG_subprogram:
22402 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22403 finish_block. */
22404 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
22405 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22406 if ((attr2 != nullptr && attr2->as_boolean ())
22407 || cu->language == language_ada
22408 || cu->language == language_fortran)
22409 {
22410 /* Subprograms marked external are stored as a global symbol.
22411 Ada and Fortran subprograms, whether marked external or
22412 not, are always stored as a global symbol, because we want
22413 to be able to access them globally. For instance, we want
22414 to be able to break on a nested subprogram without having
22415 to specify the context. */
22416 list_to_add = cu->get_builder ()->get_global_symbols ();
22417 }
22418 else
22419 {
22420 list_to_add = cu->list_in_scope;
22421 }
22422 break;
22423 case DW_TAG_inlined_subroutine:
22424 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22425 finish_block. */
22426 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
22427 SYMBOL_INLINED (sym) = 1;
22428 list_to_add = cu->list_in_scope;
22429 break;
22430 case DW_TAG_template_value_param:
22431 suppress_add = 1;
22432 /* Fall through. */
22433 case DW_TAG_constant:
22434 case DW_TAG_variable:
22435 case DW_TAG_member:
22436 /* Compilation with minimal debug info may result in
22437 variables with missing type entries. Change the
22438 misleading `void' type to something sensible. */
22439 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
22440 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
22441
22442 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22443 /* In the case of DW_TAG_member, we should only be called for
22444 static const members. */
22445 if (die->tag == DW_TAG_member)
22446 {
22447 /* dwarf2_add_field uses die_is_declaration,
22448 so we do the same. */
22449 gdb_assert (die_is_declaration (die, cu));
22450 gdb_assert (attr);
22451 }
22452 if (attr != nullptr)
22453 {
22454 dwarf2_const_value (attr, sym, cu);
22455 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22456 if (!suppress_add)
22457 {
22458 if (attr2 != nullptr && attr2->as_boolean ())
22459 list_to_add = cu->get_builder ()->get_global_symbols ();
22460 else
22461 list_to_add = cu->list_in_scope;
22462 }
22463 break;
22464 }
22465 attr = dwarf2_attr (die, DW_AT_location, cu);
22466 if (attr != nullptr)
22467 {
22468 var_decode_location (attr, sym, cu);
22469 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22470
22471 /* Fortran explicitly imports any global symbols to the local
22472 scope by DW_TAG_common_block. */
22473 if (cu->language == language_fortran && die->parent
22474 && die->parent->tag == DW_TAG_common_block)
22475 attr2 = NULL;
22476
22477 if (SYMBOL_CLASS (sym) == LOC_STATIC
22478 && SYMBOL_VALUE_ADDRESS (sym) == 0
22479 && !per_objfile->per_bfd->has_section_at_zero)
22480 {
22481 /* When a static variable is eliminated by the linker,
22482 the corresponding debug information is not stripped
22483 out, but the variable address is set to null;
22484 do not add such variables into symbol table. */
22485 }
22486 else if (attr2 != nullptr && attr2->as_boolean ())
22487 {
22488 if (SYMBOL_CLASS (sym) == LOC_STATIC
22489 && (objfile->flags & OBJF_MAINLINE) == 0
22490 && per_objfile->per_bfd->can_copy)
22491 {
22492 /* A global static variable might be subject to
22493 copy relocation. We first check for a local
22494 minsym, though, because maybe the symbol was
22495 marked hidden, in which case this would not
22496 apply. */
22497 bound_minimal_symbol found
22498 = (lookup_minimal_symbol_linkage
22499 (sym->linkage_name (), objfile));
22500 if (found.minsym != nullptr)
22501 sym->maybe_copied = 1;
22502 }
22503
22504 /* A variable with DW_AT_external is never static,
22505 but it may be block-scoped. */
22506 list_to_add
22507 = ((cu->list_in_scope
22508 == cu->get_builder ()->get_file_symbols ())
22509 ? cu->get_builder ()->get_global_symbols ()
22510 : cu->list_in_scope);
22511 }
22512 else
22513 list_to_add = cu->list_in_scope;
22514 }
22515 else
22516 {
22517 /* We do not know the address of this symbol.
22518 If it is an external symbol and we have type information
22519 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22520 The address of the variable will then be determined from
22521 the minimal symbol table whenever the variable is
22522 referenced. */
22523 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22524
22525 /* Fortran explicitly imports any global symbols to the local
22526 scope by DW_TAG_common_block. */
22527 if (cu->language == language_fortran && die->parent
22528 && die->parent->tag == DW_TAG_common_block)
22529 {
22530 /* SYMBOL_CLASS doesn't matter here because
22531 read_common_block is going to reset it. */
22532 if (!suppress_add)
22533 list_to_add = cu->list_in_scope;
22534 }
22535 else if (attr2 != nullptr && attr2->as_boolean ()
22536 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
22537 {
22538 /* A variable with DW_AT_external is never static, but it
22539 may be block-scoped. */
22540 list_to_add
22541 = ((cu->list_in_scope
22542 == cu->get_builder ()->get_file_symbols ())
22543 ? cu->get_builder ()->get_global_symbols ()
22544 : cu->list_in_scope);
22545
22546 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
22547 }
22548 else if (!die_is_declaration (die, cu))
22549 {
22550 /* Use the default LOC_OPTIMIZED_OUT class. */
22551 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
22552 if (!suppress_add)
22553 list_to_add = cu->list_in_scope;
22554 }
22555 }
22556 break;
22557 case DW_TAG_formal_parameter:
22558 {
22559 /* If we are inside a function, mark this as an argument. If
22560 not, we might be looking at an argument to an inlined function
22561 when we do not have enough information to show inlined frames;
22562 pretend it's a local variable in that case so that the user can
22563 still see it. */
22564 struct context_stack *curr
22565 = cu->get_builder ()->get_current_context_stack ();
22566 if (curr != nullptr && curr->name != nullptr)
22567 SYMBOL_IS_ARGUMENT (sym) = 1;
22568 attr = dwarf2_attr (die, DW_AT_location, cu);
22569 if (attr != nullptr)
22570 {
22571 var_decode_location (attr, sym, cu);
22572 }
22573 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22574 if (attr != nullptr)
22575 {
22576 dwarf2_const_value (attr, sym, cu);
22577 }
22578
22579 list_to_add = cu->list_in_scope;
22580 }
22581 break;
22582 case DW_TAG_unspecified_parameters:
22583 /* From varargs functions; gdb doesn't seem to have any
22584 interest in this information, so just ignore it for now.
22585 (FIXME?) */
22586 break;
22587 case DW_TAG_template_type_param:
22588 suppress_add = 1;
22589 /* Fall through. */
22590 case DW_TAG_class_type:
22591 case DW_TAG_interface_type:
22592 case DW_TAG_structure_type:
22593 case DW_TAG_union_type:
22594 case DW_TAG_set_type:
22595 case DW_TAG_enumeration_type:
22596 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22597 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
22598
22599 {
22600 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22601 really ever be static objects: otherwise, if you try
22602 to, say, break of a class's method and you're in a file
22603 which doesn't mention that class, it won't work unless
22604 the check for all static symbols in lookup_symbol_aux
22605 saves you. See the OtherFileClass tests in
22606 gdb.c++/namespace.exp. */
22607
22608 if (!suppress_add)
22609 {
22610 buildsym_compunit *builder = cu->get_builder ();
22611 list_to_add
22612 = (cu->list_in_scope == builder->get_file_symbols ()
22613 && cu->language == language_cplus
22614 ? builder->get_global_symbols ()
22615 : cu->list_in_scope);
22616
22617 /* The semantics of C++ state that "struct foo {
22618 ... }" also defines a typedef for "foo". */
22619 if (cu->language == language_cplus
22620 || cu->language == language_ada
22621 || cu->language == language_d
22622 || cu->language == language_rust)
22623 {
22624 /* The symbol's name is already allocated along
22625 with this objfile, so we don't need to
22626 duplicate it for the type. */
22627 if (SYMBOL_TYPE (sym)->name () == 0)
22628 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
22629 }
22630 }
22631 }
22632 break;
22633 case DW_TAG_typedef:
22634 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22635 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22636 list_to_add = cu->list_in_scope;
22637 break;
22638 case DW_TAG_array_type:
22639 case DW_TAG_base_type:
22640 case DW_TAG_subrange_type:
22641 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22642 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22643 list_to_add = cu->list_in_scope;
22644 break;
22645 case DW_TAG_enumerator:
22646 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22647 if (attr != nullptr)
22648 {
22649 dwarf2_const_value (attr, sym, cu);
22650 }
22651 {
22652 /* NOTE: carlton/2003-11-10: See comment above in the
22653 DW_TAG_class_type, etc. block. */
22654
22655 list_to_add
22656 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22657 && cu->language == language_cplus
22658 ? cu->get_builder ()->get_global_symbols ()
22659 : cu->list_in_scope);
22660 }
22661 break;
22662 case DW_TAG_imported_declaration:
22663 case DW_TAG_namespace:
22664 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22665 list_to_add = cu->get_builder ()->get_global_symbols ();
22666 break;
22667 case DW_TAG_module:
22668 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22669 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22670 list_to_add = cu->get_builder ()->get_global_symbols ();
22671 break;
22672 case DW_TAG_common_block:
22673 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22674 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22675 add_symbol_to_list (sym, cu->list_in_scope);
22676 break;
22677 default:
22678 /* Not a tag we recognize. Hopefully we aren't processing
22679 trash data, but since we must specifically ignore things
22680 we don't recognize, there is nothing else we should do at
22681 this point. */
22682 complaint (_("unsupported tag: '%s'"),
22683 dwarf_tag_name (die->tag));
22684 break;
22685 }
22686
22687 if (suppress_add)
22688 {
22689 sym->hash_next = objfile->template_symbols;
22690 objfile->template_symbols = sym;
22691 list_to_add = NULL;
22692 }
22693
22694 if (list_to_add != NULL)
22695 add_symbol_to_list (sym, list_to_add);
22696
22697 /* For the benefit of old versions of GCC, check for anonymous
22698 namespaces based on the demangled name. */
22699 if (!cu->processing_has_namespace_info
22700 && cu->language == language_cplus)
22701 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22702 }
22703 return (sym);
22704 }
22705
22706 /* Given an attr with a DW_FORM_dataN value in host byte order,
22707 zero-extend it as appropriate for the symbol's type. The DWARF
22708 standard (v4) is not entirely clear about the meaning of using
22709 DW_FORM_dataN for a constant with a signed type, where the type is
22710 wider than the data. The conclusion of a discussion on the DWARF
22711 list was that this is unspecified. We choose to always zero-extend
22712 because that is the interpretation long in use by GCC. */
22713
22714 static gdb_byte *
22715 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22716 struct dwarf2_cu *cu, LONGEST *value, int bits)
22717 {
22718 struct objfile *objfile = cu->per_objfile->objfile;
22719 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22720 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22721 LONGEST l = attr->constant_value (0);
22722
22723 if (bits < sizeof (*value) * 8)
22724 {
22725 l &= ((LONGEST) 1 << bits) - 1;
22726 *value = l;
22727 }
22728 else if (bits == sizeof (*value) * 8)
22729 *value = l;
22730 else
22731 {
22732 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22733 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22734 return bytes;
22735 }
22736
22737 return NULL;
22738 }
22739
22740 /* Read a constant value from an attribute. Either set *VALUE, or if
22741 the value does not fit in *VALUE, set *BYTES - either already
22742 allocated on the objfile obstack, or newly allocated on OBSTACK,
22743 or, set *BATON, if we translated the constant to a location
22744 expression. */
22745
22746 static void
22747 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22748 const char *name, struct obstack *obstack,
22749 struct dwarf2_cu *cu,
22750 LONGEST *value, const gdb_byte **bytes,
22751 struct dwarf2_locexpr_baton **baton)
22752 {
22753 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22754 struct objfile *objfile = per_objfile->objfile;
22755 struct comp_unit_head *cu_header = &cu->header;
22756 struct dwarf_block *blk;
22757 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22758 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22759
22760 *value = 0;
22761 *bytes = NULL;
22762 *baton = NULL;
22763
22764 switch (attr->form)
22765 {
22766 case DW_FORM_addr:
22767 case DW_FORM_addrx:
22768 case DW_FORM_GNU_addr_index:
22769 {
22770 gdb_byte *data;
22771
22772 if (TYPE_LENGTH (type) != cu_header->addr_size)
22773 dwarf2_const_value_length_mismatch_complaint (name,
22774 cu_header->addr_size,
22775 TYPE_LENGTH (type));
22776 /* Symbols of this form are reasonably rare, so we just
22777 piggyback on the existing location code rather than writing
22778 a new implementation of symbol_computed_ops. */
22779 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22780 (*baton)->per_objfile = per_objfile;
22781 (*baton)->per_cu = cu->per_cu;
22782 gdb_assert ((*baton)->per_cu);
22783
22784 (*baton)->size = 2 + cu_header->addr_size;
22785 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22786 (*baton)->data = data;
22787
22788 data[0] = DW_OP_addr;
22789 store_unsigned_integer (&data[1], cu_header->addr_size,
22790 byte_order, attr->as_address ());
22791 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22792 }
22793 break;
22794 case DW_FORM_string:
22795 case DW_FORM_strp:
22796 case DW_FORM_strx:
22797 case DW_FORM_GNU_str_index:
22798 case DW_FORM_GNU_strp_alt:
22799 /* The string is already allocated on the objfile obstack, point
22800 directly to it. */
22801 *bytes = (const gdb_byte *) attr->as_string ();
22802 break;
22803 case DW_FORM_block1:
22804 case DW_FORM_block2:
22805 case DW_FORM_block4:
22806 case DW_FORM_block:
22807 case DW_FORM_exprloc:
22808 case DW_FORM_data16:
22809 blk = attr->as_block ();
22810 if (TYPE_LENGTH (type) != blk->size)
22811 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22812 TYPE_LENGTH (type));
22813 *bytes = blk->data;
22814 break;
22815
22816 /* The DW_AT_const_value attributes are supposed to carry the
22817 symbol's value "represented as it would be on the target
22818 architecture." By the time we get here, it's already been
22819 converted to host endianness, so we just need to sign- or
22820 zero-extend it as appropriate. */
22821 case DW_FORM_data1:
22822 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22823 break;
22824 case DW_FORM_data2:
22825 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22826 break;
22827 case DW_FORM_data4:
22828 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22829 break;
22830 case DW_FORM_data8:
22831 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22832 break;
22833
22834 case DW_FORM_sdata:
22835 case DW_FORM_implicit_const:
22836 *value = attr->as_signed ();
22837 break;
22838
22839 case DW_FORM_udata:
22840 *value = attr->as_unsigned ();
22841 break;
22842
22843 default:
22844 complaint (_("unsupported const value attribute form: '%s'"),
22845 dwarf_form_name (attr->form));
22846 *value = 0;
22847 break;
22848 }
22849 }
22850
22851
22852 /* Copy constant value from an attribute to a symbol. */
22853
22854 static void
22855 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22856 struct dwarf2_cu *cu)
22857 {
22858 struct objfile *objfile = cu->per_objfile->objfile;
22859 LONGEST value;
22860 const gdb_byte *bytes;
22861 struct dwarf2_locexpr_baton *baton;
22862
22863 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22864 sym->print_name (),
22865 &objfile->objfile_obstack, cu,
22866 &value, &bytes, &baton);
22867
22868 if (baton != NULL)
22869 {
22870 SYMBOL_LOCATION_BATON (sym) = baton;
22871 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22872 }
22873 else if (bytes != NULL)
22874 {
22875 SYMBOL_VALUE_BYTES (sym) = bytes;
22876 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22877 }
22878 else
22879 {
22880 SYMBOL_VALUE (sym) = value;
22881 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22882 }
22883 }
22884
22885 /* Return the type of the die in question using its DW_AT_type attribute. */
22886
22887 static struct type *
22888 die_type (struct die_info *die, struct dwarf2_cu *cu)
22889 {
22890 struct attribute *type_attr;
22891
22892 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22893 if (!type_attr)
22894 {
22895 struct objfile *objfile = cu->per_objfile->objfile;
22896 /* A missing DW_AT_type represents a void type. */
22897 return objfile_type (objfile)->builtin_void;
22898 }
22899
22900 return lookup_die_type (die, type_attr, cu);
22901 }
22902
22903 /* True iff CU's producer generates GNAT Ada auxiliary information
22904 that allows to find parallel types through that information instead
22905 of having to do expensive parallel lookups by type name. */
22906
22907 static int
22908 need_gnat_info (struct dwarf2_cu *cu)
22909 {
22910 /* Assume that the Ada compiler was GNAT, which always produces
22911 the auxiliary information. */
22912 return (cu->language == language_ada);
22913 }
22914
22915 /* Return the auxiliary type of the die in question using its
22916 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22917 attribute is not present. */
22918
22919 static struct type *
22920 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22921 {
22922 struct attribute *type_attr;
22923
22924 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22925 if (!type_attr)
22926 return NULL;
22927
22928 return lookup_die_type (die, type_attr, cu);
22929 }
22930
22931 /* If DIE has a descriptive_type attribute, then set the TYPE's
22932 descriptive type accordingly. */
22933
22934 static void
22935 set_descriptive_type (struct type *type, struct die_info *die,
22936 struct dwarf2_cu *cu)
22937 {
22938 struct type *descriptive_type = die_descriptive_type (die, cu);
22939
22940 if (descriptive_type)
22941 {
22942 ALLOCATE_GNAT_AUX_TYPE (type);
22943 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22944 }
22945 }
22946
22947 /* Return the containing type of the die in question using its
22948 DW_AT_containing_type attribute. */
22949
22950 static struct type *
22951 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22952 {
22953 struct attribute *type_attr;
22954 struct objfile *objfile = cu->per_objfile->objfile;
22955
22956 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22957 if (!type_attr)
22958 error (_("Dwarf Error: Problem turning containing type into gdb type "
22959 "[in module %s]"), objfile_name (objfile));
22960
22961 return lookup_die_type (die, type_attr, cu);
22962 }
22963
22964 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22965
22966 static struct type *
22967 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22968 {
22969 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22970 struct objfile *objfile = per_objfile->objfile;
22971 char *saved;
22972
22973 std::string message
22974 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22975 objfile_name (objfile),
22976 sect_offset_str (cu->header.sect_off),
22977 sect_offset_str (die->sect_off));
22978 saved = obstack_strdup (&objfile->objfile_obstack, message);
22979
22980 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22981 }
22982
22983 /* Look up the type of DIE in CU using its type attribute ATTR.
22984 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22985 DW_AT_containing_type.
22986 If there is no type substitute an error marker. */
22987
22988 static struct type *
22989 lookup_die_type (struct die_info *die, const struct attribute *attr,
22990 struct dwarf2_cu *cu)
22991 {
22992 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22993 struct objfile *objfile = per_objfile->objfile;
22994 struct type *this_type;
22995
22996 gdb_assert (attr->name == DW_AT_type
22997 || attr->name == DW_AT_GNAT_descriptive_type
22998 || attr->name == DW_AT_containing_type);
22999
23000 /* First see if we have it cached. */
23001
23002 if (attr->form == DW_FORM_GNU_ref_alt)
23003 {
23004 struct dwarf2_per_cu_data *per_cu;
23005 sect_offset sect_off = attr->get_ref_die_offset ();
23006
23007 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
23008 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
23009 }
23010 else if (attr->form_is_ref ())
23011 {
23012 sect_offset sect_off = attr->get_ref_die_offset ();
23013
23014 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
23015 }
23016 else if (attr->form == DW_FORM_ref_sig8)
23017 {
23018 ULONGEST signature = attr->as_signature ();
23019
23020 return get_signatured_type (die, signature, cu);
23021 }
23022 else
23023 {
23024 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
23025 " at %s [in module %s]"),
23026 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
23027 objfile_name (objfile));
23028 return build_error_marker_type (cu, die);
23029 }
23030
23031 /* If not cached we need to read it in. */
23032
23033 if (this_type == NULL)
23034 {
23035 struct die_info *type_die = NULL;
23036 struct dwarf2_cu *type_cu = cu;
23037
23038 if (attr->form_is_ref ())
23039 type_die = follow_die_ref (die, attr, &type_cu);
23040 if (type_die == NULL)
23041 return build_error_marker_type (cu, die);
23042 /* If we find the type now, it's probably because the type came
23043 from an inter-CU reference and the type's CU got expanded before
23044 ours. */
23045 this_type = read_type_die (type_die, type_cu);
23046 }
23047
23048 /* If we still don't have a type use an error marker. */
23049
23050 if (this_type == NULL)
23051 return build_error_marker_type (cu, die);
23052
23053 return this_type;
23054 }
23055
23056 /* Return the type in DIE, CU.
23057 Returns NULL for invalid types.
23058
23059 This first does a lookup in die_type_hash,
23060 and only reads the die in if necessary.
23061
23062 NOTE: This can be called when reading in partial or full symbols. */
23063
23064 static struct type *
23065 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
23066 {
23067 struct type *this_type;
23068
23069 this_type = get_die_type (die, cu);
23070 if (this_type)
23071 return this_type;
23072
23073 return read_type_die_1 (die, cu);
23074 }
23075
23076 /* Read the type in DIE, CU.
23077 Returns NULL for invalid types. */
23078
23079 static struct type *
23080 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
23081 {
23082 struct type *this_type = NULL;
23083
23084 switch (die->tag)
23085 {
23086 case DW_TAG_class_type:
23087 case DW_TAG_interface_type:
23088 case DW_TAG_structure_type:
23089 case DW_TAG_union_type:
23090 this_type = read_structure_type (die, cu);
23091 break;
23092 case DW_TAG_enumeration_type:
23093 this_type = read_enumeration_type (die, cu);
23094 break;
23095 case DW_TAG_subprogram:
23096 case DW_TAG_subroutine_type:
23097 case DW_TAG_inlined_subroutine:
23098 this_type = read_subroutine_type (die, cu);
23099 break;
23100 case DW_TAG_array_type:
23101 this_type = read_array_type (die, cu);
23102 break;
23103 case DW_TAG_set_type:
23104 this_type = read_set_type (die, cu);
23105 break;
23106 case DW_TAG_pointer_type:
23107 this_type = read_tag_pointer_type (die, cu);
23108 break;
23109 case DW_TAG_ptr_to_member_type:
23110 this_type = read_tag_ptr_to_member_type (die, cu);
23111 break;
23112 case DW_TAG_reference_type:
23113 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
23114 break;
23115 case DW_TAG_rvalue_reference_type:
23116 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
23117 break;
23118 case DW_TAG_const_type:
23119 this_type = read_tag_const_type (die, cu);
23120 break;
23121 case DW_TAG_volatile_type:
23122 this_type = read_tag_volatile_type (die, cu);
23123 break;
23124 case DW_TAG_restrict_type:
23125 this_type = read_tag_restrict_type (die, cu);
23126 break;
23127 case DW_TAG_string_type:
23128 this_type = read_tag_string_type (die, cu);
23129 break;
23130 case DW_TAG_typedef:
23131 this_type = read_typedef (die, cu);
23132 break;
23133 case DW_TAG_subrange_type:
23134 this_type = read_subrange_type (die, cu);
23135 break;
23136 case DW_TAG_base_type:
23137 this_type = read_base_type (die, cu);
23138 break;
23139 case DW_TAG_unspecified_type:
23140 this_type = read_unspecified_type (die, cu);
23141 break;
23142 case DW_TAG_namespace:
23143 this_type = read_namespace_type (die, cu);
23144 break;
23145 case DW_TAG_module:
23146 this_type = read_module_type (die, cu);
23147 break;
23148 case DW_TAG_atomic_type:
23149 this_type = read_tag_atomic_type (die, cu);
23150 break;
23151 default:
23152 complaint (_("unexpected tag in read_type_die: '%s'"),
23153 dwarf_tag_name (die->tag));
23154 break;
23155 }
23156
23157 return this_type;
23158 }
23159
23160 /* See if we can figure out if the class lives in a namespace. We do
23161 this by looking for a member function; its demangled name will
23162 contain namespace info, if there is any.
23163 Return the computed name or NULL.
23164 Space for the result is allocated on the objfile's obstack.
23165 This is the full-die version of guess_partial_die_structure_name.
23166 In this case we know DIE has no useful parent. */
23167
23168 static const char *
23169 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
23170 {
23171 struct die_info *spec_die;
23172 struct dwarf2_cu *spec_cu;
23173 struct die_info *child;
23174 struct objfile *objfile = cu->per_objfile->objfile;
23175
23176 spec_cu = cu;
23177 spec_die = die_specification (die, &spec_cu);
23178 if (spec_die != NULL)
23179 {
23180 die = spec_die;
23181 cu = spec_cu;
23182 }
23183
23184 for (child = die->child;
23185 child != NULL;
23186 child = child->sibling)
23187 {
23188 if (child->tag == DW_TAG_subprogram)
23189 {
23190 const char *linkage_name = dw2_linkage_name (child, cu);
23191
23192 if (linkage_name != NULL)
23193 {
23194 gdb::unique_xmalloc_ptr<char> actual_name
23195 (cu->language_defn->class_name_from_physname (linkage_name));
23196 const char *name = NULL;
23197
23198 if (actual_name != NULL)
23199 {
23200 const char *die_name = dwarf2_name (die, cu);
23201
23202 if (die_name != NULL
23203 && strcmp (die_name, actual_name.get ()) != 0)
23204 {
23205 /* Strip off the class name from the full name.
23206 We want the prefix. */
23207 int die_name_len = strlen (die_name);
23208 int actual_name_len = strlen (actual_name.get ());
23209 const char *ptr = actual_name.get ();
23210
23211 /* Test for '::' as a sanity check. */
23212 if (actual_name_len > die_name_len + 2
23213 && ptr[actual_name_len - die_name_len - 1] == ':')
23214 name = obstack_strndup (
23215 &objfile->per_bfd->storage_obstack,
23216 ptr, actual_name_len - die_name_len - 2);
23217 }
23218 }
23219 return name;
23220 }
23221 }
23222 }
23223
23224 return NULL;
23225 }
23226
23227 /* GCC might emit a nameless typedef that has a linkage name. Determine the
23228 prefix part in such case. See
23229 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23230
23231 static const char *
23232 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
23233 {
23234 struct attribute *attr;
23235 const char *base;
23236
23237 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
23238 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
23239 return NULL;
23240
23241 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
23242 return NULL;
23243
23244 attr = dw2_linkage_name_attr (die, cu);
23245 const char *attr_name = attr->as_string ();
23246 if (attr == NULL || attr_name == NULL)
23247 return NULL;
23248
23249 /* dwarf2_name had to be already called. */
23250 gdb_assert (attr->canonical_string_p ());
23251
23252 /* Strip the base name, keep any leading namespaces/classes. */
23253 base = strrchr (attr_name, ':');
23254 if (base == NULL || base == attr_name || base[-1] != ':')
23255 return "";
23256
23257 struct objfile *objfile = cu->per_objfile->objfile;
23258 return obstack_strndup (&objfile->per_bfd->storage_obstack,
23259 attr_name,
23260 &base[-1] - attr_name);
23261 }
23262
23263 /* Return the name of the namespace/class that DIE is defined within,
23264 or "" if we can't tell. The caller should not xfree the result.
23265
23266 For example, if we're within the method foo() in the following
23267 code:
23268
23269 namespace N {
23270 class C {
23271 void foo () {
23272 }
23273 };
23274 }
23275
23276 then determine_prefix on foo's die will return "N::C". */
23277
23278 static const char *
23279 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
23280 {
23281 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23282 struct die_info *parent, *spec_die;
23283 struct dwarf2_cu *spec_cu;
23284 struct type *parent_type;
23285 const char *retval;
23286
23287 if (cu->language != language_cplus
23288 && cu->language != language_fortran && cu->language != language_d
23289 && cu->language != language_rust)
23290 return "";
23291
23292 retval = anonymous_struct_prefix (die, cu);
23293 if (retval)
23294 return retval;
23295
23296 /* We have to be careful in the presence of DW_AT_specification.
23297 For example, with GCC 3.4, given the code
23298
23299 namespace N {
23300 void foo() {
23301 // Definition of N::foo.
23302 }
23303 }
23304
23305 then we'll have a tree of DIEs like this:
23306
23307 1: DW_TAG_compile_unit
23308 2: DW_TAG_namespace // N
23309 3: DW_TAG_subprogram // declaration of N::foo
23310 4: DW_TAG_subprogram // definition of N::foo
23311 DW_AT_specification // refers to die #3
23312
23313 Thus, when processing die #4, we have to pretend that we're in
23314 the context of its DW_AT_specification, namely the contex of die
23315 #3. */
23316 spec_cu = cu;
23317 spec_die = die_specification (die, &spec_cu);
23318 if (spec_die == NULL)
23319 parent = die->parent;
23320 else
23321 {
23322 parent = spec_die->parent;
23323 cu = spec_cu;
23324 }
23325
23326 if (parent == NULL)
23327 return "";
23328 else if (parent->building_fullname)
23329 {
23330 const char *name;
23331 const char *parent_name;
23332
23333 /* It has been seen on RealView 2.2 built binaries,
23334 DW_TAG_template_type_param types actually _defined_ as
23335 children of the parent class:
23336
23337 enum E {};
23338 template class <class Enum> Class{};
23339 Class<enum E> class_e;
23340
23341 1: DW_TAG_class_type (Class)
23342 2: DW_TAG_enumeration_type (E)
23343 3: DW_TAG_enumerator (enum1:0)
23344 3: DW_TAG_enumerator (enum2:1)
23345 ...
23346 2: DW_TAG_template_type_param
23347 DW_AT_type DW_FORM_ref_udata (E)
23348
23349 Besides being broken debug info, it can put GDB into an
23350 infinite loop. Consider:
23351
23352 When we're building the full name for Class<E>, we'll start
23353 at Class, and go look over its template type parameters,
23354 finding E. We'll then try to build the full name of E, and
23355 reach here. We're now trying to build the full name of E,
23356 and look over the parent DIE for containing scope. In the
23357 broken case, if we followed the parent DIE of E, we'd again
23358 find Class, and once again go look at its template type
23359 arguments, etc., etc. Simply don't consider such parent die
23360 as source-level parent of this die (it can't be, the language
23361 doesn't allow it), and break the loop here. */
23362 name = dwarf2_name (die, cu);
23363 parent_name = dwarf2_name (parent, cu);
23364 complaint (_("template param type '%s' defined within parent '%s'"),
23365 name ? name : "<unknown>",
23366 parent_name ? parent_name : "<unknown>");
23367 return "";
23368 }
23369 else
23370 switch (parent->tag)
23371 {
23372 case DW_TAG_namespace:
23373 parent_type = read_type_die (parent, cu);
23374 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
23375 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
23376 Work around this problem here. */
23377 if (cu->language == language_cplus
23378 && strcmp (parent_type->name (), "::") == 0)
23379 return "";
23380 /* We give a name to even anonymous namespaces. */
23381 return parent_type->name ();
23382 case DW_TAG_class_type:
23383 case DW_TAG_interface_type:
23384 case DW_TAG_structure_type:
23385 case DW_TAG_union_type:
23386 case DW_TAG_module:
23387 parent_type = read_type_die (parent, cu);
23388 if (parent_type->name () != NULL)
23389 return parent_type->name ();
23390 else
23391 /* An anonymous structure is only allowed non-static data
23392 members; no typedefs, no member functions, et cetera.
23393 So it does not need a prefix. */
23394 return "";
23395 case DW_TAG_compile_unit:
23396 case DW_TAG_partial_unit:
23397 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
23398 if (cu->language == language_cplus
23399 && !per_objfile->per_bfd->types.empty ()
23400 && die->child != NULL
23401 && (die->tag == DW_TAG_class_type
23402 || die->tag == DW_TAG_structure_type
23403 || die->tag == DW_TAG_union_type))
23404 {
23405 const char *name = guess_full_die_structure_name (die, cu);
23406 if (name != NULL)
23407 return name;
23408 }
23409 return "";
23410 case DW_TAG_subprogram:
23411 /* Nested subroutines in Fortran get a prefix with the name
23412 of the parent's subroutine. */
23413 if (cu->language == language_fortran)
23414 {
23415 if ((die->tag == DW_TAG_subprogram)
23416 && (dwarf2_name (parent, cu) != NULL))
23417 return dwarf2_name (parent, cu);
23418 }
23419 return determine_prefix (parent, cu);
23420 case DW_TAG_enumeration_type:
23421 parent_type = read_type_die (parent, cu);
23422 if (parent_type->is_declared_class ())
23423 {
23424 if (parent_type->name () != NULL)
23425 return parent_type->name ();
23426 return "";
23427 }
23428 /* Fall through. */
23429 default:
23430 return determine_prefix (parent, cu);
23431 }
23432 }
23433
23434 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
23435 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
23436 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
23437 an obconcat, otherwise allocate storage for the result. The CU argument is
23438 used to determine the language and hence, the appropriate separator. */
23439
23440 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
23441
23442 static char *
23443 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
23444 int physname, struct dwarf2_cu *cu)
23445 {
23446 const char *lead = "";
23447 const char *sep;
23448
23449 if (suffix == NULL || suffix[0] == '\0'
23450 || prefix == NULL || prefix[0] == '\0')
23451 sep = "";
23452 else if (cu->language == language_d)
23453 {
23454 /* For D, the 'main' function could be defined in any module, but it
23455 should never be prefixed. */
23456 if (strcmp (suffix, "D main") == 0)
23457 {
23458 prefix = "";
23459 sep = "";
23460 }
23461 else
23462 sep = ".";
23463 }
23464 else if (cu->language == language_fortran && physname)
23465 {
23466 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
23467 DW_AT_MIPS_linkage_name is preferred and used instead. */
23468
23469 lead = "__";
23470 sep = "_MOD_";
23471 }
23472 else
23473 sep = "::";
23474
23475 if (prefix == NULL)
23476 prefix = "";
23477 if (suffix == NULL)
23478 suffix = "";
23479
23480 if (obs == NULL)
23481 {
23482 char *retval
23483 = ((char *)
23484 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
23485
23486 strcpy (retval, lead);
23487 strcat (retval, prefix);
23488 strcat (retval, sep);
23489 strcat (retval, suffix);
23490 return retval;
23491 }
23492 else
23493 {
23494 /* We have an obstack. */
23495 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
23496 }
23497 }
23498
23499 /* Get name of a die, return NULL if not found. */
23500
23501 static const char *
23502 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
23503 struct objfile *objfile)
23504 {
23505 if (name && cu->language == language_cplus)
23506 {
23507 gdb::unique_xmalloc_ptr<char> canon_name
23508 = cp_canonicalize_string (name);
23509
23510 if (canon_name != nullptr)
23511 name = objfile->intern (canon_name.get ());
23512 }
23513
23514 return name;
23515 }
23516
23517 /* Get name of a die, return NULL if not found.
23518 Anonymous namespaces are converted to their magic string. */
23519
23520 static const char *
23521 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
23522 {
23523 struct attribute *attr;
23524 struct objfile *objfile = cu->per_objfile->objfile;
23525
23526 attr = dwarf2_attr (die, DW_AT_name, cu);
23527 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
23528 if (attr_name == nullptr
23529 && die->tag != DW_TAG_namespace
23530 && die->tag != DW_TAG_class_type
23531 && die->tag != DW_TAG_interface_type
23532 && die->tag != DW_TAG_structure_type
23533 && die->tag != DW_TAG_union_type)
23534 return NULL;
23535
23536 switch (die->tag)
23537 {
23538 case DW_TAG_compile_unit:
23539 case DW_TAG_partial_unit:
23540 /* Compilation units have a DW_AT_name that is a filename, not
23541 a source language identifier. */
23542 case DW_TAG_enumeration_type:
23543 case DW_TAG_enumerator:
23544 /* These tags always have simple identifiers already; no need
23545 to canonicalize them. */
23546 return attr_name;
23547
23548 case DW_TAG_namespace:
23549 if (attr_name != nullptr)
23550 return attr_name;
23551 return CP_ANONYMOUS_NAMESPACE_STR;
23552
23553 case DW_TAG_class_type:
23554 case DW_TAG_interface_type:
23555 case DW_TAG_structure_type:
23556 case DW_TAG_union_type:
23557 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23558 structures or unions. These were of the form "._%d" in GCC 4.1,
23559 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23560 and GCC 4.4. We work around this problem by ignoring these. */
23561 if (attr_name != nullptr
23562 && (startswith (attr_name, "._")
23563 || startswith (attr_name, "<anonymous")))
23564 return NULL;
23565
23566 /* GCC might emit a nameless typedef that has a linkage name. See
23567 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23568 if (!attr || attr_name == NULL)
23569 {
23570 attr = dw2_linkage_name_attr (die, cu);
23571 attr_name = attr == nullptr ? nullptr : attr->as_string ();
23572 if (attr == NULL || attr_name == NULL)
23573 return NULL;
23574
23575 /* Avoid demangling attr_name the second time on a second
23576 call for the same DIE. */
23577 if (!attr->canonical_string_p ())
23578 {
23579 gdb::unique_xmalloc_ptr<char> demangled
23580 (gdb_demangle (attr_name, DMGL_TYPES));
23581 if (demangled == nullptr)
23582 return nullptr;
23583
23584 attr->set_string_canonical (objfile->intern (demangled.get ()));
23585 attr_name = attr->as_string ();
23586 }
23587
23588 /* Strip any leading namespaces/classes, keep only the
23589 base name. DW_AT_name for named DIEs does not
23590 contain the prefixes. */
23591 const char *base = strrchr (attr_name, ':');
23592 if (base && base > attr_name && base[-1] == ':')
23593 return &base[1];
23594 else
23595 return attr_name;
23596 }
23597 break;
23598
23599 default:
23600 break;
23601 }
23602
23603 if (!attr->canonical_string_p ())
23604 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23605 objfile));
23606 return attr->as_string ();
23607 }
23608
23609 /* Return the die that this die in an extension of, or NULL if there
23610 is none. *EXT_CU is the CU containing DIE on input, and the CU
23611 containing the return value on output. */
23612
23613 static struct die_info *
23614 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23615 {
23616 struct attribute *attr;
23617
23618 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23619 if (attr == NULL)
23620 return NULL;
23621
23622 return follow_die_ref (die, attr, ext_cu);
23623 }
23624
23625 static void
23626 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23627 {
23628 unsigned int i;
23629
23630 print_spaces (indent, f);
23631 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23632 dwarf_tag_name (die->tag), die->abbrev,
23633 sect_offset_str (die->sect_off));
23634
23635 if (die->parent != NULL)
23636 {
23637 print_spaces (indent, f);
23638 fprintf_unfiltered (f, " parent at offset: %s\n",
23639 sect_offset_str (die->parent->sect_off));
23640 }
23641
23642 print_spaces (indent, f);
23643 fprintf_unfiltered (f, " has children: %s\n",
23644 dwarf_bool_name (die->child != NULL));
23645
23646 print_spaces (indent, f);
23647 fprintf_unfiltered (f, " attributes:\n");
23648
23649 for (i = 0; i < die->num_attrs; ++i)
23650 {
23651 print_spaces (indent, f);
23652 fprintf_unfiltered (f, " %s (%s) ",
23653 dwarf_attr_name (die->attrs[i].name),
23654 dwarf_form_name (die->attrs[i].form));
23655
23656 switch (die->attrs[i].form)
23657 {
23658 case DW_FORM_addr:
23659 case DW_FORM_addrx:
23660 case DW_FORM_GNU_addr_index:
23661 fprintf_unfiltered (f, "address: ");
23662 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
23663 break;
23664 case DW_FORM_block2:
23665 case DW_FORM_block4:
23666 case DW_FORM_block:
23667 case DW_FORM_block1:
23668 fprintf_unfiltered (f, "block: size %s",
23669 pulongest (die->attrs[i].as_block ()->size));
23670 break;
23671 case DW_FORM_exprloc:
23672 fprintf_unfiltered (f, "expression: size %s",
23673 pulongest (die->attrs[i].as_block ()->size));
23674 break;
23675 case DW_FORM_data16:
23676 fprintf_unfiltered (f, "constant of 16 bytes");
23677 break;
23678 case DW_FORM_ref_addr:
23679 fprintf_unfiltered (f, "ref address: ");
23680 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23681 break;
23682 case DW_FORM_GNU_ref_alt:
23683 fprintf_unfiltered (f, "alt ref address: ");
23684 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23685 break;
23686 case DW_FORM_ref1:
23687 case DW_FORM_ref2:
23688 case DW_FORM_ref4:
23689 case DW_FORM_ref8:
23690 case DW_FORM_ref_udata:
23691 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23692 (long) (die->attrs[i].as_unsigned ()));
23693 break;
23694 case DW_FORM_data1:
23695 case DW_FORM_data2:
23696 case DW_FORM_data4:
23697 case DW_FORM_data8:
23698 case DW_FORM_udata:
23699 fprintf_unfiltered (f, "constant: %s",
23700 pulongest (die->attrs[i].as_unsigned ()));
23701 break;
23702 case DW_FORM_sec_offset:
23703 fprintf_unfiltered (f, "section offset: %s",
23704 pulongest (die->attrs[i].as_unsigned ()));
23705 break;
23706 case DW_FORM_ref_sig8:
23707 fprintf_unfiltered (f, "signature: %s",
23708 hex_string (die->attrs[i].as_signature ()));
23709 break;
23710 case DW_FORM_string:
23711 case DW_FORM_strp:
23712 case DW_FORM_line_strp:
23713 case DW_FORM_strx:
23714 case DW_FORM_GNU_str_index:
23715 case DW_FORM_GNU_strp_alt:
23716 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23717 die->attrs[i].as_string ()
23718 ? die->attrs[i].as_string () : "",
23719 die->attrs[i].canonical_string_p () ? "is" : "not");
23720 break;
23721 case DW_FORM_flag:
23722 if (die->attrs[i].as_boolean ())
23723 fprintf_unfiltered (f, "flag: TRUE");
23724 else
23725 fprintf_unfiltered (f, "flag: FALSE");
23726 break;
23727 case DW_FORM_flag_present:
23728 fprintf_unfiltered (f, "flag: TRUE");
23729 break;
23730 case DW_FORM_indirect:
23731 /* The reader will have reduced the indirect form to
23732 the "base form" so this form should not occur. */
23733 fprintf_unfiltered (f,
23734 "unexpected attribute form: DW_FORM_indirect");
23735 break;
23736 case DW_FORM_sdata:
23737 case DW_FORM_implicit_const:
23738 fprintf_unfiltered (f, "constant: %s",
23739 plongest (die->attrs[i].as_signed ()));
23740 break;
23741 default:
23742 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23743 die->attrs[i].form);
23744 break;
23745 }
23746 fprintf_unfiltered (f, "\n");
23747 }
23748 }
23749
23750 static void
23751 dump_die_for_error (struct die_info *die)
23752 {
23753 dump_die_shallow (gdb_stderr, 0, die);
23754 }
23755
23756 static void
23757 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23758 {
23759 int indent = level * 4;
23760
23761 gdb_assert (die != NULL);
23762
23763 if (level >= max_level)
23764 return;
23765
23766 dump_die_shallow (f, indent, die);
23767
23768 if (die->child != NULL)
23769 {
23770 print_spaces (indent, f);
23771 fprintf_unfiltered (f, " Children:");
23772 if (level + 1 < max_level)
23773 {
23774 fprintf_unfiltered (f, "\n");
23775 dump_die_1 (f, level + 1, max_level, die->child);
23776 }
23777 else
23778 {
23779 fprintf_unfiltered (f,
23780 " [not printed, max nesting level reached]\n");
23781 }
23782 }
23783
23784 if (die->sibling != NULL && level > 0)
23785 {
23786 dump_die_1 (f, level, max_level, die->sibling);
23787 }
23788 }
23789
23790 /* This is called from the pdie macro in gdbinit.in.
23791 It's not static so gcc will keep a copy callable from gdb. */
23792
23793 void
23794 dump_die (struct die_info *die, int max_level)
23795 {
23796 dump_die_1 (gdb_stdlog, 0, max_level, die);
23797 }
23798
23799 static void
23800 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23801 {
23802 void **slot;
23803
23804 slot = htab_find_slot_with_hash (cu->die_hash, die,
23805 to_underlying (die->sect_off),
23806 INSERT);
23807
23808 *slot = die;
23809 }
23810
23811 /* Follow reference or signature attribute ATTR of SRC_DIE.
23812 On entry *REF_CU is the CU of SRC_DIE.
23813 On exit *REF_CU is the CU of the result. */
23814
23815 static struct die_info *
23816 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23817 struct dwarf2_cu **ref_cu)
23818 {
23819 struct die_info *die;
23820
23821 if (attr->form_is_ref ())
23822 die = follow_die_ref (src_die, attr, ref_cu);
23823 else if (attr->form == DW_FORM_ref_sig8)
23824 die = follow_die_sig (src_die, attr, ref_cu);
23825 else
23826 {
23827 dump_die_for_error (src_die);
23828 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23829 objfile_name ((*ref_cu)->per_objfile->objfile));
23830 }
23831
23832 return die;
23833 }
23834
23835 /* Follow reference OFFSET.
23836 On entry *REF_CU is the CU of the source die referencing OFFSET.
23837 On exit *REF_CU is the CU of the result.
23838 Returns NULL if OFFSET is invalid. */
23839
23840 static struct die_info *
23841 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23842 struct dwarf2_cu **ref_cu)
23843 {
23844 struct die_info temp_die;
23845 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23846 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23847
23848 gdb_assert (cu->per_cu != NULL);
23849
23850 target_cu = cu;
23851
23852 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23853 "source CU contains target offset: %d",
23854 sect_offset_str (cu->per_cu->sect_off),
23855 sect_offset_str (sect_off),
23856 cu->header.offset_in_cu_p (sect_off));
23857
23858 if (cu->per_cu->is_debug_types)
23859 {
23860 /* .debug_types CUs cannot reference anything outside their CU.
23861 If they need to, they have to reference a signatured type via
23862 DW_FORM_ref_sig8. */
23863 if (!cu->header.offset_in_cu_p (sect_off))
23864 return NULL;
23865 }
23866 else if (offset_in_dwz != cu->per_cu->is_dwz
23867 || !cu->header.offset_in_cu_p (sect_off))
23868 {
23869 struct dwarf2_per_cu_data *per_cu;
23870
23871 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23872 per_objfile);
23873
23874 dwarf_read_debug_printf_v ("target CU offset: %s, "
23875 "target CU DIEs loaded: %d",
23876 sect_offset_str (per_cu->sect_off),
23877 per_objfile->get_cu (per_cu) != nullptr);
23878
23879 /* If necessary, add it to the queue and load its DIEs.
23880
23881 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23882 it doesn't mean they are currently loaded. Since we require them
23883 to be loaded, we must check for ourselves. */
23884 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language)
23885 || per_objfile->get_cu (per_cu) == nullptr)
23886 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23887 false, cu->language);
23888
23889 target_cu = per_objfile->get_cu (per_cu);
23890 gdb_assert (target_cu != nullptr);
23891 }
23892 else if (cu->dies == NULL)
23893 {
23894 /* We're loading full DIEs during partial symbol reading. */
23895 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
23896 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23897 language_minimal);
23898 }
23899
23900 *ref_cu = target_cu;
23901 temp_die.sect_off = sect_off;
23902
23903 if (target_cu != cu)
23904 target_cu->ancestor = cu;
23905
23906 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23907 &temp_die,
23908 to_underlying (sect_off));
23909 }
23910
23911 /* Follow reference attribute ATTR of SRC_DIE.
23912 On entry *REF_CU is the CU of SRC_DIE.
23913 On exit *REF_CU is the CU of the result. */
23914
23915 static struct die_info *
23916 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23917 struct dwarf2_cu **ref_cu)
23918 {
23919 sect_offset sect_off = attr->get_ref_die_offset ();
23920 struct dwarf2_cu *cu = *ref_cu;
23921 struct die_info *die;
23922
23923 die = follow_die_offset (sect_off,
23924 (attr->form == DW_FORM_GNU_ref_alt
23925 || cu->per_cu->is_dwz),
23926 ref_cu);
23927 if (!die)
23928 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23929 "at %s [in module %s]"),
23930 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23931 objfile_name (cu->per_objfile->objfile));
23932
23933 return die;
23934 }
23935
23936 /* See read.h. */
23937
23938 struct dwarf2_locexpr_baton
23939 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23940 dwarf2_per_cu_data *per_cu,
23941 dwarf2_per_objfile *per_objfile,
23942 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23943 bool resolve_abstract_p)
23944 {
23945 struct die_info *die;
23946 struct attribute *attr;
23947 struct dwarf2_locexpr_baton retval;
23948 struct objfile *objfile = per_objfile->objfile;
23949
23950 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23951 if (cu == nullptr)
23952 cu = load_cu (per_cu, per_objfile, false);
23953
23954 if (cu == nullptr)
23955 {
23956 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23957 Instead just throw an error, not much else we can do. */
23958 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23959 sect_offset_str (sect_off), objfile_name (objfile));
23960 }
23961
23962 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23963 if (!die)
23964 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23965 sect_offset_str (sect_off), objfile_name (objfile));
23966
23967 attr = dwarf2_attr (die, DW_AT_location, cu);
23968 if (!attr && resolve_abstract_p
23969 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23970 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23971 {
23972 CORE_ADDR pc = get_frame_pc ();
23973 CORE_ADDR baseaddr = objfile->text_section_offset ();
23974 struct gdbarch *gdbarch = objfile->arch ();
23975
23976 for (const auto &cand_off
23977 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23978 {
23979 struct dwarf2_cu *cand_cu = cu;
23980 struct die_info *cand
23981 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23982 if (!cand
23983 || !cand->parent
23984 || cand->parent->tag != DW_TAG_subprogram)
23985 continue;
23986
23987 CORE_ADDR pc_low, pc_high;
23988 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23989 if (pc_low == ((CORE_ADDR) -1))
23990 continue;
23991 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23992 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23993 if (!(pc_low <= pc && pc < pc_high))
23994 continue;
23995
23996 die = cand;
23997 attr = dwarf2_attr (die, DW_AT_location, cu);
23998 break;
23999 }
24000 }
24001
24002 if (!attr)
24003 {
24004 /* DWARF: "If there is no such attribute, then there is no effect.".
24005 DATA is ignored if SIZE is 0. */
24006
24007 retval.data = NULL;
24008 retval.size = 0;
24009 }
24010 else if (attr->form_is_section_offset ())
24011 {
24012 struct dwarf2_loclist_baton loclist_baton;
24013 CORE_ADDR pc = get_frame_pc ();
24014 size_t size;
24015
24016 fill_in_loclist_baton (cu, &loclist_baton, attr);
24017
24018 retval.data = dwarf2_find_location_expression (&loclist_baton,
24019 &size, pc);
24020 retval.size = size;
24021 }
24022 else
24023 {
24024 if (!attr->form_is_block ())
24025 error (_("Dwarf Error: DIE at %s referenced in module %s "
24026 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
24027 sect_offset_str (sect_off), objfile_name (objfile));
24028
24029 struct dwarf_block *block = attr->as_block ();
24030 retval.data = block->data;
24031 retval.size = block->size;
24032 }
24033 retval.per_objfile = per_objfile;
24034 retval.per_cu = cu->per_cu;
24035
24036 per_objfile->age_comp_units ();
24037
24038 return retval;
24039 }
24040
24041 /* See read.h. */
24042
24043 struct dwarf2_locexpr_baton
24044 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
24045 dwarf2_per_cu_data *per_cu,
24046 dwarf2_per_objfile *per_objfile,
24047 gdb::function_view<CORE_ADDR ()> get_frame_pc)
24048 {
24049 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
24050
24051 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
24052 get_frame_pc);
24053 }
24054
24055 /* Write a constant of a given type as target-ordered bytes into
24056 OBSTACK. */
24057
24058 static const gdb_byte *
24059 write_constant_as_bytes (struct obstack *obstack,
24060 enum bfd_endian byte_order,
24061 struct type *type,
24062 ULONGEST value,
24063 LONGEST *len)
24064 {
24065 gdb_byte *result;
24066
24067 *len = TYPE_LENGTH (type);
24068 result = (gdb_byte *) obstack_alloc (obstack, *len);
24069 store_unsigned_integer (result, *len, byte_order, value);
24070
24071 return result;
24072 }
24073
24074 /* See read.h. */
24075
24076 const gdb_byte *
24077 dwarf2_fetch_constant_bytes (sect_offset sect_off,
24078 dwarf2_per_cu_data *per_cu,
24079 dwarf2_per_objfile *per_objfile,
24080 obstack *obstack,
24081 LONGEST *len)
24082 {
24083 struct die_info *die;
24084 struct attribute *attr;
24085 const gdb_byte *result = NULL;
24086 struct type *type;
24087 LONGEST value;
24088 enum bfd_endian byte_order;
24089 struct objfile *objfile = per_objfile->objfile;
24090
24091 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
24092 if (cu == nullptr)
24093 cu = load_cu (per_cu, per_objfile, false);
24094
24095 if (cu == nullptr)
24096 {
24097 /* We shouldn't get here for a dummy CU, but don't crash on the user.
24098 Instead just throw an error, not much else we can do. */
24099 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
24100 sect_offset_str (sect_off), objfile_name (objfile));
24101 }
24102
24103 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
24104 if (!die)
24105 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
24106 sect_offset_str (sect_off), objfile_name (objfile));
24107
24108 attr = dwarf2_attr (die, DW_AT_const_value, cu);
24109 if (attr == NULL)
24110 return NULL;
24111
24112 byte_order = (bfd_big_endian (objfile->obfd)
24113 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
24114
24115 switch (attr->form)
24116 {
24117 case DW_FORM_addr:
24118 case DW_FORM_addrx:
24119 case DW_FORM_GNU_addr_index:
24120 {
24121 gdb_byte *tem;
24122
24123 *len = cu->header.addr_size;
24124 tem = (gdb_byte *) obstack_alloc (obstack, *len);
24125 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
24126 result = tem;
24127 }
24128 break;
24129 case DW_FORM_string:
24130 case DW_FORM_strp:
24131 case DW_FORM_strx:
24132 case DW_FORM_GNU_str_index:
24133 case DW_FORM_GNU_strp_alt:
24134 /* The string is already allocated on the objfile obstack, point
24135 directly to it. */
24136 {
24137 const char *attr_name = attr->as_string ();
24138 result = (const gdb_byte *) attr_name;
24139 *len = strlen (attr_name);
24140 }
24141 break;
24142 case DW_FORM_block1:
24143 case DW_FORM_block2:
24144 case DW_FORM_block4:
24145 case DW_FORM_block:
24146 case DW_FORM_exprloc:
24147 case DW_FORM_data16:
24148 {
24149 struct dwarf_block *block = attr->as_block ();
24150 result = block->data;
24151 *len = block->size;
24152 }
24153 break;
24154
24155 /* The DW_AT_const_value attributes are supposed to carry the
24156 symbol's value "represented as it would be on the target
24157 architecture." By the time we get here, it's already been
24158 converted to host endianness, so we just need to sign- or
24159 zero-extend it as appropriate. */
24160 case DW_FORM_data1:
24161 type = die_type (die, cu);
24162 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
24163 if (result == NULL)
24164 result = write_constant_as_bytes (obstack, byte_order,
24165 type, value, len);
24166 break;
24167 case DW_FORM_data2:
24168 type = die_type (die, cu);
24169 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
24170 if (result == NULL)
24171 result = write_constant_as_bytes (obstack, byte_order,
24172 type, value, len);
24173 break;
24174 case DW_FORM_data4:
24175 type = die_type (die, cu);
24176 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
24177 if (result == NULL)
24178 result = write_constant_as_bytes (obstack, byte_order,
24179 type, value, len);
24180 break;
24181 case DW_FORM_data8:
24182 type = die_type (die, cu);
24183 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
24184 if (result == NULL)
24185 result = write_constant_as_bytes (obstack, byte_order,
24186 type, value, len);
24187 break;
24188
24189 case DW_FORM_sdata:
24190 case DW_FORM_implicit_const:
24191 type = die_type (die, cu);
24192 result = write_constant_as_bytes (obstack, byte_order,
24193 type, attr->as_signed (), len);
24194 break;
24195
24196 case DW_FORM_udata:
24197 type = die_type (die, cu);
24198 result = write_constant_as_bytes (obstack, byte_order,
24199 type, attr->as_unsigned (), len);
24200 break;
24201
24202 default:
24203 complaint (_("unsupported const value attribute form: '%s'"),
24204 dwarf_form_name (attr->form));
24205 break;
24206 }
24207
24208 return result;
24209 }
24210
24211 /* See read.h. */
24212
24213 struct type *
24214 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
24215 dwarf2_per_cu_data *per_cu,
24216 dwarf2_per_objfile *per_objfile)
24217 {
24218 struct die_info *die;
24219
24220 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
24221 if (cu == nullptr)
24222 cu = load_cu (per_cu, per_objfile, false);
24223
24224 if (cu == nullptr)
24225 return nullptr;
24226
24227 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
24228 if (!die)
24229 return NULL;
24230
24231 return die_type (die, cu);
24232 }
24233
24234 /* See read.h. */
24235
24236 struct type *
24237 dwarf2_get_die_type (cu_offset die_offset,
24238 dwarf2_per_cu_data *per_cu,
24239 dwarf2_per_objfile *per_objfile)
24240 {
24241 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
24242 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
24243 }
24244
24245 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
24246 On entry *REF_CU is the CU of SRC_DIE.
24247 On exit *REF_CU is the CU of the result.
24248 Returns NULL if the referenced DIE isn't found. */
24249
24250 static struct die_info *
24251 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
24252 struct dwarf2_cu **ref_cu)
24253 {
24254 struct die_info temp_die;
24255 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
24256 struct die_info *die;
24257 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
24258
24259
24260 /* While it might be nice to assert sig_type->type == NULL here,
24261 we can get here for DW_AT_imported_declaration where we need
24262 the DIE not the type. */
24263
24264 /* If necessary, add it to the queue and load its DIEs.
24265
24266 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
24267 it doesn't mean they are currently loaded. Since we require them
24268 to be loaded, we must check for ourselves. */
24269 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
24270 language_minimal)
24271 || per_objfile->get_cu (&sig_type->per_cu) == nullptr)
24272 read_signatured_type (sig_type, per_objfile);
24273
24274 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
24275 gdb_assert (sig_cu != NULL);
24276 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
24277 temp_die.sect_off = sig_type->type_offset_in_section;
24278 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
24279 to_underlying (temp_die.sect_off));
24280 if (die)
24281 {
24282 /* For .gdb_index version 7 keep track of included TUs.
24283 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
24284 if (per_objfile->per_bfd->index_table != NULL
24285 && per_objfile->per_bfd->index_table->version <= 7)
24286 {
24287 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
24288 }
24289
24290 *ref_cu = sig_cu;
24291 if (sig_cu != cu)
24292 sig_cu->ancestor = cu;
24293
24294 return die;
24295 }
24296
24297 return NULL;
24298 }
24299
24300 /* Follow signatured type referenced by ATTR in SRC_DIE.
24301 On entry *REF_CU is the CU of SRC_DIE.
24302 On exit *REF_CU is the CU of the result.
24303 The result is the DIE of the type.
24304 If the referenced type cannot be found an error is thrown. */
24305
24306 static struct die_info *
24307 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
24308 struct dwarf2_cu **ref_cu)
24309 {
24310 ULONGEST signature = attr->as_signature ();
24311 struct signatured_type *sig_type;
24312 struct die_info *die;
24313
24314 gdb_assert (attr->form == DW_FORM_ref_sig8);
24315
24316 sig_type = lookup_signatured_type (*ref_cu, signature);
24317 /* sig_type will be NULL if the signatured type is missing from
24318 the debug info. */
24319 if (sig_type == NULL)
24320 {
24321 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24322 " from DIE at %s [in module %s]"),
24323 hex_string (signature), sect_offset_str (src_die->sect_off),
24324 objfile_name ((*ref_cu)->per_objfile->objfile));
24325 }
24326
24327 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
24328 if (die == NULL)
24329 {
24330 dump_die_for_error (src_die);
24331 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24332 " from DIE at %s [in module %s]"),
24333 hex_string (signature), sect_offset_str (src_die->sect_off),
24334 objfile_name ((*ref_cu)->per_objfile->objfile));
24335 }
24336
24337 return die;
24338 }
24339
24340 /* Get the type specified by SIGNATURE referenced in DIE/CU,
24341 reading in and processing the type unit if necessary. */
24342
24343 static struct type *
24344 get_signatured_type (struct die_info *die, ULONGEST signature,
24345 struct dwarf2_cu *cu)
24346 {
24347 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24348 struct signatured_type *sig_type;
24349 struct dwarf2_cu *type_cu;
24350 struct die_info *type_die;
24351 struct type *type;
24352
24353 sig_type = lookup_signatured_type (cu, signature);
24354 /* sig_type will be NULL if the signatured type is missing from
24355 the debug info. */
24356 if (sig_type == NULL)
24357 {
24358 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24359 " from DIE at %s [in module %s]"),
24360 hex_string (signature), sect_offset_str (die->sect_off),
24361 objfile_name (per_objfile->objfile));
24362 return build_error_marker_type (cu, die);
24363 }
24364
24365 /* If we already know the type we're done. */
24366 type = per_objfile->get_type_for_signatured_type (sig_type);
24367 if (type != nullptr)
24368 return type;
24369
24370 type_cu = cu;
24371 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24372 if (type_die != NULL)
24373 {
24374 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24375 is created. This is important, for example, because for c++ classes
24376 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24377 type = read_type_die (type_die, type_cu);
24378 if (type == NULL)
24379 {
24380 complaint (_("Dwarf Error: Cannot build signatured type %s"
24381 " referenced from DIE at %s [in module %s]"),
24382 hex_string (signature), sect_offset_str (die->sect_off),
24383 objfile_name (per_objfile->objfile));
24384 type = build_error_marker_type (cu, die);
24385 }
24386 }
24387 else
24388 {
24389 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24390 " from DIE at %s [in module %s]"),
24391 hex_string (signature), sect_offset_str (die->sect_off),
24392 objfile_name (per_objfile->objfile));
24393 type = build_error_marker_type (cu, die);
24394 }
24395
24396 per_objfile->set_type_for_signatured_type (sig_type, type);
24397
24398 return type;
24399 }
24400
24401 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24402 reading in and processing the type unit if necessary. */
24403
24404 static struct type *
24405 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
24406 struct dwarf2_cu *cu) /* ARI: editCase function */
24407 {
24408 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
24409 if (attr->form_is_ref ())
24410 {
24411 struct dwarf2_cu *type_cu = cu;
24412 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24413
24414 return read_type_die (type_die, type_cu);
24415 }
24416 else if (attr->form == DW_FORM_ref_sig8)
24417 {
24418 return get_signatured_type (die, attr->as_signature (), cu);
24419 }
24420 else
24421 {
24422 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24423
24424 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
24425 " at %s [in module %s]"),
24426 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
24427 objfile_name (per_objfile->objfile));
24428 return build_error_marker_type (cu, die);
24429 }
24430 }
24431
24432 /* Load the DIEs associated with type unit PER_CU into memory. */
24433
24434 static void
24435 load_full_type_unit (dwarf2_per_cu_data *per_cu,
24436 dwarf2_per_objfile *per_objfile)
24437 {
24438 struct signatured_type *sig_type;
24439
24440 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24441 gdb_assert (! per_cu->type_unit_group_p ());
24442
24443 /* We have the per_cu, but we need the signatured_type.
24444 Fortunately this is an easy translation. */
24445 gdb_assert (per_cu->is_debug_types);
24446 sig_type = (struct signatured_type *) per_cu;
24447
24448 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
24449
24450 read_signatured_type (sig_type, per_objfile);
24451
24452 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
24453 }
24454
24455 /* Read in a signatured type and build its CU and DIEs.
24456 If the type is a stub for the real type in a DWO file,
24457 read in the real type from the DWO file as well. */
24458
24459 static void
24460 read_signatured_type (signatured_type *sig_type,
24461 dwarf2_per_objfile *per_objfile)
24462 {
24463 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
24464
24465 gdb_assert (per_cu->is_debug_types);
24466 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
24467
24468 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
24469
24470 if (!reader.dummy_p)
24471 {
24472 struct dwarf2_cu *cu = reader.cu;
24473 const gdb_byte *info_ptr = reader.info_ptr;
24474
24475 gdb_assert (cu->die_hash == NULL);
24476 cu->die_hash =
24477 htab_create_alloc_ex (cu->header.length / 12,
24478 die_hash,
24479 die_eq,
24480 NULL,
24481 &cu->comp_unit_obstack,
24482 hashtab_obstack_allocate,
24483 dummy_obstack_deallocate);
24484
24485 if (reader.comp_unit_die->has_children)
24486 reader.comp_unit_die->child
24487 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24488 reader.comp_unit_die);
24489 cu->dies = reader.comp_unit_die;
24490 /* comp_unit_die is not stored in die_hash, no need. */
24491
24492 /* We try not to read any attributes in this function, because
24493 not all CUs needed for references have been loaded yet, and
24494 symbol table processing isn't initialized. But we have to
24495 set the CU language, or we won't be able to build types
24496 correctly. Similarly, if we do not read the producer, we can
24497 not apply producer-specific interpretation. */
24498 prepare_one_comp_unit (cu, cu->dies, language_minimal);
24499
24500 reader.keep ();
24501 }
24502
24503 sig_type->per_cu.tu_read = 1;
24504 }
24505
24506 /* Decode simple location descriptions.
24507 Given a pointer to a dwarf block that defines a location, compute
24508 the location and return the value. If COMPUTED is non-null, it is
24509 set to true to indicate that decoding was successful, and false
24510 otherwise. If COMPUTED is null, then this function may emit a
24511 complaint. */
24512
24513 static CORE_ADDR
24514 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
24515 {
24516 struct objfile *objfile = cu->per_objfile->objfile;
24517 size_t i;
24518 size_t size = blk->size;
24519 const gdb_byte *data = blk->data;
24520 CORE_ADDR stack[64];
24521 int stacki;
24522 unsigned int bytes_read, unsnd;
24523 gdb_byte op;
24524
24525 if (computed != nullptr)
24526 *computed = false;
24527
24528 i = 0;
24529 stacki = 0;
24530 stack[stacki] = 0;
24531 stack[++stacki] = 0;
24532
24533 while (i < size)
24534 {
24535 op = data[i++];
24536 switch (op)
24537 {
24538 case DW_OP_lit0:
24539 case DW_OP_lit1:
24540 case DW_OP_lit2:
24541 case DW_OP_lit3:
24542 case DW_OP_lit4:
24543 case DW_OP_lit5:
24544 case DW_OP_lit6:
24545 case DW_OP_lit7:
24546 case DW_OP_lit8:
24547 case DW_OP_lit9:
24548 case DW_OP_lit10:
24549 case DW_OP_lit11:
24550 case DW_OP_lit12:
24551 case DW_OP_lit13:
24552 case DW_OP_lit14:
24553 case DW_OP_lit15:
24554 case DW_OP_lit16:
24555 case DW_OP_lit17:
24556 case DW_OP_lit18:
24557 case DW_OP_lit19:
24558 case DW_OP_lit20:
24559 case DW_OP_lit21:
24560 case DW_OP_lit22:
24561 case DW_OP_lit23:
24562 case DW_OP_lit24:
24563 case DW_OP_lit25:
24564 case DW_OP_lit26:
24565 case DW_OP_lit27:
24566 case DW_OP_lit28:
24567 case DW_OP_lit29:
24568 case DW_OP_lit30:
24569 case DW_OP_lit31:
24570 stack[++stacki] = op - DW_OP_lit0;
24571 break;
24572
24573 case DW_OP_reg0:
24574 case DW_OP_reg1:
24575 case DW_OP_reg2:
24576 case DW_OP_reg3:
24577 case DW_OP_reg4:
24578 case DW_OP_reg5:
24579 case DW_OP_reg6:
24580 case DW_OP_reg7:
24581 case DW_OP_reg8:
24582 case DW_OP_reg9:
24583 case DW_OP_reg10:
24584 case DW_OP_reg11:
24585 case DW_OP_reg12:
24586 case DW_OP_reg13:
24587 case DW_OP_reg14:
24588 case DW_OP_reg15:
24589 case DW_OP_reg16:
24590 case DW_OP_reg17:
24591 case DW_OP_reg18:
24592 case DW_OP_reg19:
24593 case DW_OP_reg20:
24594 case DW_OP_reg21:
24595 case DW_OP_reg22:
24596 case DW_OP_reg23:
24597 case DW_OP_reg24:
24598 case DW_OP_reg25:
24599 case DW_OP_reg26:
24600 case DW_OP_reg27:
24601 case DW_OP_reg28:
24602 case DW_OP_reg29:
24603 case DW_OP_reg30:
24604 case DW_OP_reg31:
24605 stack[++stacki] = op - DW_OP_reg0;
24606 if (i < size)
24607 {
24608 if (computed == nullptr)
24609 dwarf2_complex_location_expr_complaint ();
24610 else
24611 return 0;
24612 }
24613 break;
24614
24615 case DW_OP_regx:
24616 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24617 i += bytes_read;
24618 stack[++stacki] = unsnd;
24619 if (i < size)
24620 {
24621 if (computed == nullptr)
24622 dwarf2_complex_location_expr_complaint ();
24623 else
24624 return 0;
24625 }
24626 break;
24627
24628 case DW_OP_addr:
24629 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24630 &bytes_read);
24631 i += bytes_read;
24632 break;
24633
24634 case DW_OP_const1u:
24635 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24636 i += 1;
24637 break;
24638
24639 case DW_OP_const1s:
24640 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24641 i += 1;
24642 break;
24643
24644 case DW_OP_const2u:
24645 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24646 i += 2;
24647 break;
24648
24649 case DW_OP_const2s:
24650 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24651 i += 2;
24652 break;
24653
24654 case DW_OP_const4u:
24655 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24656 i += 4;
24657 break;
24658
24659 case DW_OP_const4s:
24660 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24661 i += 4;
24662 break;
24663
24664 case DW_OP_const8u:
24665 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24666 i += 8;
24667 break;
24668
24669 case DW_OP_constu:
24670 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24671 &bytes_read);
24672 i += bytes_read;
24673 break;
24674
24675 case DW_OP_consts:
24676 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24677 i += bytes_read;
24678 break;
24679
24680 case DW_OP_dup:
24681 stack[stacki + 1] = stack[stacki];
24682 stacki++;
24683 break;
24684
24685 case DW_OP_plus:
24686 stack[stacki - 1] += stack[stacki];
24687 stacki--;
24688 break;
24689
24690 case DW_OP_plus_uconst:
24691 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24692 &bytes_read);
24693 i += bytes_read;
24694 break;
24695
24696 case DW_OP_minus:
24697 stack[stacki - 1] -= stack[stacki];
24698 stacki--;
24699 break;
24700
24701 case DW_OP_deref:
24702 /* If we're not the last op, then we definitely can't encode
24703 this using GDB's address_class enum. This is valid for partial
24704 global symbols, although the variable's address will be bogus
24705 in the psymtab. */
24706 if (i < size)
24707 {
24708 if (computed == nullptr)
24709 dwarf2_complex_location_expr_complaint ();
24710 else
24711 return 0;
24712 }
24713 break;
24714
24715 case DW_OP_GNU_push_tls_address:
24716 case DW_OP_form_tls_address:
24717 /* The top of the stack has the offset from the beginning
24718 of the thread control block at which the variable is located. */
24719 /* Nothing should follow this operator, so the top of stack would
24720 be returned. */
24721 /* This is valid for partial global symbols, but the variable's
24722 address will be bogus in the psymtab. Make it always at least
24723 non-zero to not look as a variable garbage collected by linker
24724 which have DW_OP_addr 0. */
24725 if (i < size)
24726 {
24727 if (computed == nullptr)
24728 dwarf2_complex_location_expr_complaint ();
24729 else
24730 return 0;
24731 }
24732 stack[stacki]++;
24733 break;
24734
24735 case DW_OP_GNU_uninit:
24736 if (computed != nullptr)
24737 return 0;
24738 break;
24739
24740 case DW_OP_addrx:
24741 case DW_OP_GNU_addr_index:
24742 case DW_OP_GNU_const_index:
24743 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24744 &bytes_read);
24745 i += bytes_read;
24746 break;
24747
24748 default:
24749 if (computed == nullptr)
24750 {
24751 const char *name = get_DW_OP_name (op);
24752
24753 if (name)
24754 complaint (_("unsupported stack op: '%s'"),
24755 name);
24756 else
24757 complaint (_("unsupported stack op: '%02x'"),
24758 op);
24759 }
24760
24761 return (stack[stacki]);
24762 }
24763
24764 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24765 outside of the allocated space. Also enforce minimum>0. */
24766 if (stacki >= ARRAY_SIZE (stack) - 1)
24767 {
24768 if (computed == nullptr)
24769 complaint (_("location description stack overflow"));
24770 return 0;
24771 }
24772
24773 if (stacki <= 0)
24774 {
24775 if (computed == nullptr)
24776 complaint (_("location description stack underflow"));
24777 return 0;
24778 }
24779 }
24780
24781 if (computed != nullptr)
24782 *computed = true;
24783 return (stack[stacki]);
24784 }
24785
24786 /* memory allocation interface */
24787
24788 static struct dwarf_block *
24789 dwarf_alloc_block (struct dwarf2_cu *cu)
24790 {
24791 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24792 }
24793
24794 static struct die_info *
24795 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24796 {
24797 struct die_info *die;
24798 size_t size = sizeof (struct die_info);
24799
24800 if (num_attrs > 1)
24801 size += (num_attrs - 1) * sizeof (struct attribute);
24802
24803 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24804 memset (die, 0, sizeof (struct die_info));
24805 return (die);
24806 }
24807
24808 \f
24809
24810 /* Macro support. */
24811
24812 /* An overload of dwarf_decode_macros that finds the correct section
24813 and ensures it is read in before calling the other overload. */
24814
24815 static void
24816 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24817 int section_is_gnu)
24818 {
24819 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24820 struct objfile *objfile = per_objfile->objfile;
24821 const struct line_header *lh = cu->line_header;
24822 unsigned int offset_size = cu->header.offset_size;
24823 struct dwarf2_section_info *section;
24824 const char *section_name;
24825
24826 if (cu->dwo_unit != nullptr)
24827 {
24828 if (section_is_gnu)
24829 {
24830 section = &cu->dwo_unit->dwo_file->sections.macro;
24831 section_name = ".debug_macro.dwo";
24832 }
24833 else
24834 {
24835 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24836 section_name = ".debug_macinfo.dwo";
24837 }
24838 }
24839 else
24840 {
24841 if (section_is_gnu)
24842 {
24843 section = &per_objfile->per_bfd->macro;
24844 section_name = ".debug_macro";
24845 }
24846 else
24847 {
24848 section = &per_objfile->per_bfd->macinfo;
24849 section_name = ".debug_macinfo";
24850 }
24851 }
24852
24853 section->read (objfile);
24854 if (section->buffer == nullptr)
24855 {
24856 complaint (_("missing %s section"), section_name);
24857 return;
24858 }
24859
24860 buildsym_compunit *builder = cu->get_builder ();
24861
24862 struct dwarf2_section_info *str_offsets_section;
24863 struct dwarf2_section_info *str_section;
24864 ULONGEST str_offsets_base;
24865
24866 if (cu->dwo_unit != nullptr)
24867 {
24868 str_offsets_section = &cu->dwo_unit->dwo_file
24869 ->sections.str_offsets;
24870 str_section = &cu->dwo_unit->dwo_file->sections.str;
24871 str_offsets_base = cu->header.addr_size;
24872 }
24873 else
24874 {
24875 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24876 str_section = &per_objfile->per_bfd->str;
24877 str_offsets_base = *cu->str_offsets_base;
24878 }
24879
24880 dwarf_decode_macros (per_objfile, builder, section, lh,
24881 offset_size, offset, str_section, str_offsets_section,
24882 str_offsets_base, section_is_gnu);
24883 }
24884
24885 /* Return the .debug_loc section to use for CU.
24886 For DWO files use .debug_loc.dwo. */
24887
24888 static struct dwarf2_section_info *
24889 cu_debug_loc_section (struct dwarf2_cu *cu)
24890 {
24891 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24892
24893 if (cu->dwo_unit)
24894 {
24895 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24896
24897 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24898 }
24899 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24900 : &per_objfile->per_bfd->loc);
24901 }
24902
24903 /* Return the .debug_rnglists section to use for CU. */
24904 static struct dwarf2_section_info *
24905 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24906 {
24907 if (cu->header.version < 5)
24908 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24909 cu->header.version);
24910 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24911
24912 /* Make sure we read the .debug_rnglists section from the file that
24913 contains the DW_AT_ranges attribute we are reading. Normally that
24914 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24915 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24916 program. */
24917 if (cu->dwo_unit != nullptr
24918 && tag != DW_TAG_compile_unit
24919 && tag != DW_TAG_skeleton_unit)
24920 {
24921 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24922
24923 if (sections->rnglists.size > 0)
24924 return &sections->rnglists;
24925 else
24926 error (_(".debug_rnglists section is missing from .dwo file."));
24927 }
24928 return &dwarf2_per_objfile->per_bfd->rnglists;
24929 }
24930
24931 /* A helper function that fills in a dwarf2_loclist_baton. */
24932
24933 static void
24934 fill_in_loclist_baton (struct dwarf2_cu *cu,
24935 struct dwarf2_loclist_baton *baton,
24936 const struct attribute *attr)
24937 {
24938 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24939 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24940
24941 section->read (per_objfile->objfile);
24942
24943 baton->per_objfile = per_objfile;
24944 baton->per_cu = cu->per_cu;
24945 gdb_assert (baton->per_cu);
24946 /* We don't know how long the location list is, but make sure we
24947 don't run off the edge of the section. */
24948 baton->size = section->size - attr->as_unsigned ();
24949 baton->data = section->buffer + attr->as_unsigned ();
24950 if (cu->base_address.has_value ())
24951 baton->base_address = *cu->base_address;
24952 else
24953 baton->base_address = 0;
24954 baton->from_dwo = cu->dwo_unit != NULL;
24955 }
24956
24957 static void
24958 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24959 struct dwarf2_cu *cu, int is_block)
24960 {
24961 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24962 struct objfile *objfile = per_objfile->objfile;
24963 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24964
24965 if (attr->form_is_section_offset ()
24966 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24967 the section. If so, fall through to the complaint in the
24968 other branch. */
24969 && attr->as_unsigned () < section->get_size (objfile))
24970 {
24971 struct dwarf2_loclist_baton *baton;
24972
24973 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24974
24975 fill_in_loclist_baton (cu, baton, attr);
24976
24977 if (!cu->base_address.has_value ())
24978 complaint (_("Location list used without "
24979 "specifying the CU base address."));
24980
24981 SYMBOL_ACLASS_INDEX (sym) = (is_block
24982 ? dwarf2_loclist_block_index
24983 : dwarf2_loclist_index);
24984 SYMBOL_LOCATION_BATON (sym) = baton;
24985 }
24986 else
24987 {
24988 struct dwarf2_locexpr_baton *baton;
24989
24990 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24991 baton->per_objfile = per_objfile;
24992 baton->per_cu = cu->per_cu;
24993 gdb_assert (baton->per_cu);
24994
24995 if (attr->form_is_block ())
24996 {
24997 /* Note that we're just copying the block's data pointer
24998 here, not the actual data. We're still pointing into the
24999 info_buffer for SYM's objfile; right now we never release
25000 that buffer, but when we do clean up properly this may
25001 need to change. */
25002 struct dwarf_block *block = attr->as_block ();
25003 baton->size = block->size;
25004 baton->data = block->data;
25005 }
25006 else
25007 {
25008 dwarf2_invalid_attrib_class_complaint ("location description",
25009 sym->natural_name ());
25010 baton->size = 0;
25011 }
25012
25013 SYMBOL_ACLASS_INDEX (sym) = (is_block
25014 ? dwarf2_locexpr_block_index
25015 : dwarf2_locexpr_index);
25016 SYMBOL_LOCATION_BATON (sym) = baton;
25017 }
25018 }
25019
25020 /* See read.h. */
25021
25022 const comp_unit_head *
25023 dwarf2_per_cu_data::get_header () const
25024 {
25025 if (!m_header_read_in)
25026 {
25027 const gdb_byte *info_ptr
25028 = this->section->buffer + to_underlying (this->sect_off);
25029
25030 memset (&m_header, 0, sizeof (m_header));
25031
25032 read_comp_unit_head (&m_header, info_ptr, this->section,
25033 rcuh_kind::COMPILE);
25034
25035 m_header_read_in = true;
25036 }
25037
25038 return &m_header;
25039 }
25040
25041 /* See read.h. */
25042
25043 int
25044 dwarf2_per_cu_data::addr_size () const
25045 {
25046 return this->get_header ()->addr_size;
25047 }
25048
25049 /* See read.h. */
25050
25051 int
25052 dwarf2_per_cu_data::offset_size () const
25053 {
25054 return this->get_header ()->offset_size;
25055 }
25056
25057 /* See read.h. */
25058
25059 int
25060 dwarf2_per_cu_data::ref_addr_size () const
25061 {
25062 const comp_unit_head *header = this->get_header ();
25063
25064 if (header->version == 2)
25065 return header->addr_size;
25066 else
25067 return header->offset_size;
25068 }
25069
25070 /* See read.h. */
25071
25072 struct type *
25073 dwarf2_cu::addr_type () const
25074 {
25075 struct objfile *objfile = this->per_objfile->objfile;
25076 struct type *void_type = objfile_type (objfile)->builtin_void;
25077 struct type *addr_type = lookup_pointer_type (void_type);
25078 int addr_size = this->per_cu->addr_size ();
25079
25080 if (TYPE_LENGTH (addr_type) == addr_size)
25081 return addr_type;
25082
25083 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
25084 return addr_type;
25085 }
25086
25087 /* A helper function for dwarf2_find_containing_comp_unit that returns
25088 the index of the result, and that searches a vector. It will
25089 return a result even if the offset in question does not actually
25090 occur in any CU. This is separate so that it can be unit
25091 tested. */
25092
25093 static int
25094 dwarf2_find_containing_comp_unit
25095 (sect_offset sect_off,
25096 unsigned int offset_in_dwz,
25097 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
25098 {
25099 int low, high;
25100
25101 low = 0;
25102 high = all_comp_units.size () - 1;
25103 while (high > low)
25104 {
25105 struct dwarf2_per_cu_data *mid_cu;
25106 int mid = low + (high - low) / 2;
25107
25108 mid_cu = all_comp_units[mid];
25109 if (mid_cu->is_dwz > offset_in_dwz
25110 || (mid_cu->is_dwz == offset_in_dwz
25111 && mid_cu->sect_off + mid_cu->length > sect_off))
25112 high = mid;
25113 else
25114 low = mid + 1;
25115 }
25116 gdb_assert (low == high);
25117 return low;
25118 }
25119
25120 /* Locate the .debug_info compilation unit from CU's objfile which contains
25121 the DIE at OFFSET. Raises an error on failure. */
25122
25123 static struct dwarf2_per_cu_data *
25124 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25125 unsigned int offset_in_dwz,
25126 dwarf2_per_objfile *per_objfile)
25127 {
25128 int low = dwarf2_find_containing_comp_unit
25129 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
25130 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
25131
25132 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
25133 {
25134 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25135 error (_("Dwarf Error: could not find partial DIE containing "
25136 "offset %s [in module %s]"),
25137 sect_offset_str (sect_off),
25138 bfd_get_filename (per_objfile->objfile->obfd));
25139
25140 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
25141 <= sect_off);
25142 return per_objfile->per_bfd->all_comp_units[low-1];
25143 }
25144 else
25145 {
25146 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
25147 && sect_off >= this_cu->sect_off + this_cu->length)
25148 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25149 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25150 return this_cu;
25151 }
25152 }
25153
25154 #if GDB_SELF_TEST
25155
25156 namespace selftests {
25157 namespace find_containing_comp_unit {
25158
25159 static void
25160 run_test ()
25161 {
25162 struct dwarf2_per_cu_data one {};
25163 struct dwarf2_per_cu_data two {};
25164 struct dwarf2_per_cu_data three {};
25165 struct dwarf2_per_cu_data four {};
25166
25167 one.length = 5;
25168 two.sect_off = sect_offset (one.length);
25169 two.length = 7;
25170
25171 three.length = 5;
25172 three.is_dwz = 1;
25173 four.sect_off = sect_offset (three.length);
25174 four.length = 7;
25175 four.is_dwz = 1;
25176
25177 std::vector<dwarf2_per_cu_data *> units;
25178 units.push_back (&one);
25179 units.push_back (&two);
25180 units.push_back (&three);
25181 units.push_back (&four);
25182
25183 int result;
25184
25185 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
25186 SELF_CHECK (units[result] == &one);
25187 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
25188 SELF_CHECK (units[result] == &one);
25189 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
25190 SELF_CHECK (units[result] == &two);
25191
25192 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
25193 SELF_CHECK (units[result] == &three);
25194 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
25195 SELF_CHECK (units[result] == &three);
25196 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
25197 SELF_CHECK (units[result] == &four);
25198 }
25199
25200 }
25201 }
25202
25203 #endif /* GDB_SELF_TEST */
25204
25205 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
25206
25207 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
25208 dwarf2_per_objfile *per_objfile)
25209 : per_cu (per_cu),
25210 per_objfile (per_objfile),
25211 mark (false),
25212 has_loclist (false),
25213 checked_producer (false),
25214 producer_is_gxx_lt_4_6 (false),
25215 producer_is_gcc_lt_4_3 (false),
25216 producer_is_icc (false),
25217 producer_is_icc_lt_14 (false),
25218 producer_is_codewarrior (false),
25219 processing_has_namespace_info (false)
25220 {
25221 }
25222
25223 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25224
25225 static void
25226 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25227 enum language pretend_language)
25228 {
25229 struct attribute *attr;
25230
25231 /* Set the language we're debugging. */
25232 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25233 if (attr != nullptr)
25234 set_cu_language (attr->constant_value (0), cu);
25235 else
25236 {
25237 cu->language = pretend_language;
25238 cu->language_defn = language_def (cu->language);
25239 }
25240
25241 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25242 }
25243
25244 /* See read.h. */
25245
25246 dwarf2_cu *
25247 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
25248 {
25249 auto it = m_dwarf2_cus.find (per_cu);
25250 if (it == m_dwarf2_cus.end ())
25251 return nullptr;
25252
25253 return it->second;
25254 }
25255
25256 /* See read.h. */
25257
25258 void
25259 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
25260 {
25261 gdb_assert (this->get_cu (per_cu) == nullptr);
25262
25263 m_dwarf2_cus[per_cu] = cu;
25264 }
25265
25266 /* See read.h. */
25267
25268 void
25269 dwarf2_per_objfile::age_comp_units ()
25270 {
25271 dwarf_read_debug_printf_v ("running");
25272
25273 /* This is not expected to be called in the middle of CU expansion. There is
25274 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
25275 loaded in memory. Calling age_comp_units while the queue is in use could
25276 make us free the DIEs for a CU that is in the queue and therefore break
25277 that invariant. */
25278 gdb_assert (!this->per_bfd->queue.has_value ());
25279
25280 /* Start by clearing all marks. */
25281 for (auto pair : m_dwarf2_cus)
25282 pair.second->mark = false;
25283
25284 /* Traverse all CUs, mark them and their dependencies if used recently
25285 enough. */
25286 for (auto pair : m_dwarf2_cus)
25287 {
25288 dwarf2_cu *cu = pair.second;
25289
25290 cu->last_used++;
25291 if (cu->last_used <= dwarf_max_cache_age)
25292 dwarf2_mark (cu);
25293 }
25294
25295 /* Delete all CUs still not marked. */
25296 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
25297 {
25298 dwarf2_cu *cu = it->second;
25299
25300 if (!cu->mark)
25301 {
25302 dwarf_read_debug_printf_v ("deleting old CU %s",
25303 sect_offset_str (cu->per_cu->sect_off));
25304 delete cu;
25305 it = m_dwarf2_cus.erase (it);
25306 }
25307 else
25308 it++;
25309 }
25310 }
25311
25312 /* See read.h. */
25313
25314 void
25315 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
25316 {
25317 auto it = m_dwarf2_cus.find (per_cu);
25318 if (it == m_dwarf2_cus.end ())
25319 return;
25320
25321 delete it->second;
25322
25323 m_dwarf2_cus.erase (it);
25324 }
25325
25326 dwarf2_per_objfile::~dwarf2_per_objfile ()
25327 {
25328 remove_all_cus ();
25329 }
25330
25331 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25332 We store these in a hash table separate from the DIEs, and preserve them
25333 when the DIEs are flushed out of cache.
25334
25335 The CU "per_cu" pointer is needed because offset alone is not enough to
25336 uniquely identify the type. A file may have multiple .debug_types sections,
25337 or the type may come from a DWO file. Furthermore, while it's more logical
25338 to use per_cu->section+offset, with Fission the section with the data is in
25339 the DWO file but we don't know that section at the point we need it.
25340 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25341 because we can enter the lookup routine, get_die_type_at_offset, from
25342 outside this file, and thus won't necessarily have PER_CU->cu.
25343 Fortunately, PER_CU is stable for the life of the objfile. */
25344
25345 struct dwarf2_per_cu_offset_and_type
25346 {
25347 const struct dwarf2_per_cu_data *per_cu;
25348 sect_offset sect_off;
25349 struct type *type;
25350 };
25351
25352 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25353
25354 static hashval_t
25355 per_cu_offset_and_type_hash (const void *item)
25356 {
25357 const struct dwarf2_per_cu_offset_and_type *ofs
25358 = (const struct dwarf2_per_cu_offset_and_type *) item;
25359
25360 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25361 }
25362
25363 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25364
25365 static int
25366 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25367 {
25368 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25369 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25370 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25371 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25372
25373 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25374 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25375 }
25376
25377 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25378 table if necessary. For convenience, return TYPE.
25379
25380 The DIEs reading must have careful ordering to:
25381 * Not cause infinite loops trying to read in DIEs as a prerequisite for
25382 reading current DIE.
25383 * Not trying to dereference contents of still incompletely read in types
25384 while reading in other DIEs.
25385 * Enable referencing still incompletely read in types just by a pointer to
25386 the type without accessing its fields.
25387
25388 Therefore caller should follow these rules:
25389 * Try to fetch any prerequisite types we may need to build this DIE type
25390 before building the type and calling set_die_type.
25391 * After building type call set_die_type for current DIE as soon as
25392 possible before fetching more types to complete the current type.
25393 * Make the type as complete as possible before fetching more types. */
25394
25395 static struct type *
25396 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
25397 bool skip_data_location)
25398 {
25399 dwarf2_per_objfile *per_objfile = cu->per_objfile;
25400 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25401 struct objfile *objfile = per_objfile->objfile;
25402 struct attribute *attr;
25403 struct dynamic_prop prop;
25404
25405 /* For Ada types, make sure that the gnat-specific data is always
25406 initialized (if not already set). There are a few types where
25407 we should not be doing so, because the type-specific area is
25408 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25409 where the type-specific area is used to store the floatformat).
25410 But this is not a problem, because the gnat-specific information
25411 is actually not needed for these types. */
25412 if (need_gnat_info (cu)
25413 && type->code () != TYPE_CODE_FUNC
25414 && type->code () != TYPE_CODE_FLT
25415 && type->code () != TYPE_CODE_METHODPTR
25416 && type->code () != TYPE_CODE_MEMBERPTR
25417 && type->code () != TYPE_CODE_METHOD
25418 && type->code () != TYPE_CODE_FIXED_POINT
25419 && !HAVE_GNAT_AUX_INFO (type))
25420 INIT_GNAT_SPECIFIC (type);
25421
25422 /* Read DW_AT_allocated and set in type. */
25423 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25424 if (attr != NULL)
25425 {
25426 struct type *prop_type = cu->addr_sized_int_type (false);
25427 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25428 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
25429 }
25430
25431 /* Read DW_AT_associated and set in type. */
25432 attr = dwarf2_attr (die, DW_AT_associated, cu);
25433 if (attr != NULL)
25434 {
25435 struct type *prop_type = cu->addr_sized_int_type (false);
25436 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25437 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
25438 }
25439
25440 /* Read DW_AT_data_location and set in type. */
25441 if (!skip_data_location)
25442 {
25443 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25444 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
25445 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
25446 }
25447
25448 if (per_objfile->die_type_hash == NULL)
25449 per_objfile->die_type_hash
25450 = htab_up (htab_create_alloc (127,
25451 per_cu_offset_and_type_hash,
25452 per_cu_offset_and_type_eq,
25453 NULL, xcalloc, xfree));
25454
25455 ofs.per_cu = cu->per_cu;
25456 ofs.sect_off = die->sect_off;
25457 ofs.type = type;
25458 slot = (struct dwarf2_per_cu_offset_and_type **)
25459 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
25460 if (*slot)
25461 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25462 sect_offset_str (die->sect_off));
25463 *slot = XOBNEW (&objfile->objfile_obstack,
25464 struct dwarf2_per_cu_offset_and_type);
25465 **slot = ofs;
25466 return type;
25467 }
25468
25469 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25470 or return NULL if the die does not have a saved type. */
25471
25472 static struct type *
25473 get_die_type_at_offset (sect_offset sect_off,
25474 dwarf2_per_cu_data *per_cu,
25475 dwarf2_per_objfile *per_objfile)
25476 {
25477 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25478
25479 if (per_objfile->die_type_hash == NULL)
25480 return NULL;
25481
25482 ofs.per_cu = per_cu;
25483 ofs.sect_off = sect_off;
25484 slot = ((struct dwarf2_per_cu_offset_and_type *)
25485 htab_find (per_objfile->die_type_hash.get (), &ofs));
25486 if (slot)
25487 return slot->type;
25488 else
25489 return NULL;
25490 }
25491
25492 /* Look up the type for DIE in CU in die_type_hash,
25493 or return NULL if DIE does not have a saved type. */
25494
25495 static struct type *
25496 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25497 {
25498 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
25499 }
25500
25501 /* Add a dependence relationship from CU to REF_PER_CU. */
25502
25503 static void
25504 dwarf2_add_dependence (struct dwarf2_cu *cu,
25505 struct dwarf2_per_cu_data *ref_per_cu)
25506 {
25507 void **slot;
25508
25509 if (cu->dependencies == NULL)
25510 cu->dependencies
25511 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25512 NULL, &cu->comp_unit_obstack,
25513 hashtab_obstack_allocate,
25514 dummy_obstack_deallocate);
25515
25516 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25517 if (*slot == NULL)
25518 *slot = ref_per_cu;
25519 }
25520
25521 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25522 Set the mark field in every compilation unit in the
25523 cache that we must keep because we are keeping CU.
25524
25525 DATA is the dwarf2_per_objfile object in which to look up CUs. */
25526
25527 static int
25528 dwarf2_mark_helper (void **slot, void *data)
25529 {
25530 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
25531 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
25532 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
25533
25534 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25535 reading of the chain. As such dependencies remain valid it is not much
25536 useful to track and undo them during QUIT cleanups. */
25537 if (cu == nullptr)
25538 return 1;
25539
25540 if (cu->mark)
25541 return 1;
25542
25543 cu->mark = true;
25544
25545 if (cu->dependencies != nullptr)
25546 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
25547
25548 return 1;
25549 }
25550
25551 /* Set the mark field in CU and in every other compilation unit in the
25552 cache that we must keep because we are keeping CU. */
25553
25554 static void
25555 dwarf2_mark (struct dwarf2_cu *cu)
25556 {
25557 if (cu->mark)
25558 return;
25559
25560 cu->mark = true;
25561
25562 if (cu->dependencies != nullptr)
25563 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
25564 }
25565
25566 /* Trivial hash function for partial_die_info: the hash value of a DIE
25567 is its offset in .debug_info for this objfile. */
25568
25569 static hashval_t
25570 partial_die_hash (const void *item)
25571 {
25572 const struct partial_die_info *part_die
25573 = (const struct partial_die_info *) item;
25574
25575 return to_underlying (part_die->sect_off);
25576 }
25577
25578 /* Trivial comparison function for partial_die_info structures: two DIEs
25579 are equal if they have the same offset. */
25580
25581 static int
25582 partial_die_eq (const void *item_lhs, const void *item_rhs)
25583 {
25584 const struct partial_die_info *part_die_lhs
25585 = (const struct partial_die_info *) item_lhs;
25586 const struct partial_die_info *part_die_rhs
25587 = (const struct partial_die_info *) item_rhs;
25588
25589 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25590 }
25591
25592 struct cmd_list_element *set_dwarf_cmdlist;
25593 struct cmd_list_element *show_dwarf_cmdlist;
25594
25595 static void
25596 show_check_physname (struct ui_file *file, int from_tty,
25597 struct cmd_list_element *c, const char *value)
25598 {
25599 fprintf_filtered (file,
25600 _("Whether to check \"physname\" is %s.\n"),
25601 value);
25602 }
25603
25604 void _initialize_dwarf2_read ();
25605 void
25606 _initialize_dwarf2_read ()
25607 {
25608 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
25609 Set DWARF specific variables.\n\
25610 Configure DWARF variables such as the cache size."),
25611 &set_dwarf_cmdlist, "maintenance set dwarf ",
25612 0/*allow-unknown*/, &maintenance_set_cmdlist);
25613
25614 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
25615 Show DWARF specific variables.\n\
25616 Show DWARF variables such as the cache size."),
25617 &show_dwarf_cmdlist, "maintenance show dwarf ",
25618 0/*allow-unknown*/, &maintenance_show_cmdlist);
25619
25620 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25621 &dwarf_max_cache_age, _("\
25622 Set the upper bound on the age of cached DWARF compilation units."), _("\
25623 Show the upper bound on the age of cached DWARF compilation units."), _("\
25624 A higher limit means that cached compilation units will be stored\n\
25625 in memory longer, and more total memory will be used. Zero disables\n\
25626 caching, which can slow down startup."),
25627 NULL,
25628 show_dwarf_max_cache_age,
25629 &set_dwarf_cmdlist,
25630 &show_dwarf_cmdlist);
25631
25632 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25633 Set debugging of the DWARF reader."), _("\
25634 Show debugging of the DWARF reader."), _("\
25635 When enabled (non-zero), debugging messages are printed during DWARF\n\
25636 reading and symtab expansion. A value of 1 (one) provides basic\n\
25637 information. A value greater than 1 provides more verbose information."),
25638 NULL,
25639 NULL,
25640 &setdebuglist, &showdebuglist);
25641
25642 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25643 Set debugging of the DWARF DIE reader."), _("\
25644 Show debugging of the DWARF DIE reader."), _("\
25645 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25646 The value is the maximum depth to print."),
25647 NULL,
25648 NULL,
25649 &setdebuglist, &showdebuglist);
25650
25651 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25652 Set debugging of the dwarf line reader."), _("\
25653 Show debugging of the dwarf line reader."), _("\
25654 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25655 A value of 1 (one) provides basic information.\n\
25656 A value greater than 1 provides more verbose information."),
25657 NULL,
25658 NULL,
25659 &setdebuglist, &showdebuglist);
25660
25661 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25662 Set cross-checking of \"physname\" code against demangler."), _("\
25663 Show cross-checking of \"physname\" code against demangler."), _("\
25664 When enabled, GDB's internal \"physname\" code is checked against\n\
25665 the demangler."),
25666 NULL, show_check_physname,
25667 &setdebuglist, &showdebuglist);
25668
25669 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25670 no_class, &use_deprecated_index_sections, _("\
25671 Set whether to use deprecated gdb_index sections."), _("\
25672 Show whether to use deprecated gdb_index sections."), _("\
25673 When enabled, deprecated .gdb_index sections are used anyway.\n\
25674 Normally they are ignored either because of a missing feature or\n\
25675 performance issue.\n\
25676 Warning: This option must be enabled before gdb reads the file."),
25677 NULL,
25678 NULL,
25679 &setlist, &showlist);
25680
25681 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25682 &dwarf2_locexpr_funcs);
25683 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25684 &dwarf2_loclist_funcs);
25685
25686 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25687 &dwarf2_block_frame_base_locexpr_funcs);
25688 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25689 &dwarf2_block_frame_base_loclist_funcs);
25690
25691 #if GDB_SELF_TEST
25692 selftests::register_test ("dw2_expand_symtabs_matching",
25693 selftests::dw2_expand_symtabs_matching::run_test);
25694 selftests::register_test ("dwarf2_find_containing_comp_unit",
25695 selftests::find_containing_comp_unit::run_test);
25696 #endif
25697 }
This page took 0.575979 seconds and 4 git commands to generate.