Convert quick_symbol_functions to use methods
[binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2021 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit.h"
36 #include "dwarf2/index-cache.h"
37 #include "dwarf2/index-common.h"
38 #include "dwarf2/leb.h"
39 #include "dwarf2/line-header.h"
40 #include "dwarf2/dwz.h"
41 #include "dwarf2/macro.h"
42 #include "dwarf2/die.h"
43 #include "dwarf2/sect-names.h"
44 #include "dwarf2/stringify.h"
45 #include "dwarf2/public.h"
46 #include "bfd.h"
47 #include "elf-bfd.h"
48 #include "symtab.h"
49 #include "gdbtypes.h"
50 #include "objfiles.h"
51 #include "dwarf2.h"
52 #include "buildsym.h"
53 #include "demangle.h"
54 #include "gdb-demangle.h"
55 #include "filenames.h" /* for DOSish file names */
56 #include "language.h"
57 #include "complaints.h"
58 #include "dwarf2/expr.h"
59 #include "dwarf2/loc.h"
60 #include "cp-support.h"
61 #include "hashtab.h"
62 #include "command.h"
63 #include "gdbcmd.h"
64 #include "block.h"
65 #include "addrmap.h"
66 #include "typeprint.h"
67 #include "psympriv.h"
68 #include "c-lang.h"
69 #include "go-lang.h"
70 #include "valprint.h"
71 #include "gdbcore.h" /* for gnutarget */
72 #include "gdb/gdb-index.h"
73 #include "gdb_bfd.h"
74 #include "f-lang.h"
75 #include "source.h"
76 #include "build-id.h"
77 #include "namespace.h"
78 #include "gdbsupport/function-view.h"
79 #include "gdbsupport/gdb_optional.h"
80 #include "gdbsupport/underlying.h"
81 #include "gdbsupport/hash_enum.h"
82 #include "filename-seen-cache.h"
83 #include "producer.h"
84 #include <fcntl.h>
85 #include <algorithm>
86 #include <unordered_map>
87 #include "gdbsupport/selftest.h"
88 #include "rust-lang.h"
89 #include "gdbsupport/pathstuff.h"
90 #include "count-one-bits.h"
91
92 /* When == 1, print basic high level tracing messages.
93 When > 1, be more verbose.
94 This is in contrast to the low level DIE reading of dwarf_die_debug. */
95 static unsigned int dwarf_read_debug = 0;
96
97 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
98
99 #define dwarf_read_debug_printf(fmt, ...) \
100 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
101 ##__VA_ARGS__)
102
103 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
104
105 #define dwarf_read_debug_printf_v(fmt, ...) \
106 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
107 ##__VA_ARGS__)
108
109 /* When non-zero, dump DIEs after they are read in. */
110 static unsigned int dwarf_die_debug = 0;
111
112 /* When non-zero, dump line number entries as they are read in. */
113 unsigned int dwarf_line_debug = 0;
114
115 /* When true, cross-check physname against demangler. */
116 static bool check_physname = false;
117
118 /* When true, do not reject deprecated .gdb_index sections. */
119 static bool use_deprecated_index_sections = false;
120
121 /* This is used to store the data that is always per objfile. */
122 static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
123
124 /* These are used to store the dwarf2_per_bfd objects.
125
126 objfiles having the same BFD, which doesn't require relocations, are going to
127 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
128
129 Other objfiles are not going to share a dwarf2_per_bfd with any other
130 objfiles, so they'll have their own version kept in the _objfile_data_key
131 version. */
132 static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
133 static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
134
135 /* The "aclass" indices for various kinds of computed DWARF symbols. */
136
137 static int dwarf2_locexpr_index;
138 static int dwarf2_loclist_index;
139 static int dwarf2_locexpr_block_index;
140 static int dwarf2_loclist_block_index;
141
142 /* Size of .debug_loclists section header for 32-bit DWARF format. */
143 #define LOCLIST_HEADER_SIZE32 12
144
145 /* Size of .debug_loclists section header for 64-bit DWARF format. */
146 #define LOCLIST_HEADER_SIZE64 20
147
148 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
149 #define RNGLIST_HEADER_SIZE32 12
150
151 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
152 #define RNGLIST_HEADER_SIZE64 20
153
154 /* An index into a (C++) symbol name component in a symbol name as
155 recorded in the mapped_index's symbol table. For each C++ symbol
156 in the symbol table, we record one entry for the start of each
157 component in the symbol in a table of name components, and then
158 sort the table, in order to be able to binary search symbol names,
159 ignoring leading namespaces, both completion and regular look up.
160 For example, for symbol "A::B::C", we'll have an entry that points
161 to "A::B::C", another that points to "B::C", and another for "C".
162 Note that function symbols in GDB index have no parameter
163 information, just the function/method names. You can convert a
164 name_component to a "const char *" using the
165 'mapped_index::symbol_name_at(offset_type)' method. */
166
167 struct name_component
168 {
169 /* Offset in the symbol name where the component starts. Stored as
170 a (32-bit) offset instead of a pointer to save memory and improve
171 locality on 64-bit architectures. */
172 offset_type name_offset;
173
174 /* The symbol's index in the symbol and constant pool tables of a
175 mapped_index. */
176 offset_type idx;
177 };
178
179 /* Base class containing bits shared by both .gdb_index and
180 .debug_name indexes. */
181
182 struct mapped_index_base
183 {
184 mapped_index_base () = default;
185 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
186
187 /* The name_component table (a sorted vector). See name_component's
188 description above. */
189 std::vector<name_component> name_components;
190
191 /* How NAME_COMPONENTS is sorted. */
192 enum case_sensitivity name_components_casing;
193
194 /* Return the number of names in the symbol table. */
195 virtual size_t symbol_name_count () const = 0;
196
197 /* Get the name of the symbol at IDX in the symbol table. */
198 virtual const char *symbol_name_at
199 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
200
201 /* Return whether the name at IDX in the symbol table should be
202 ignored. */
203 virtual bool symbol_name_slot_invalid (offset_type idx) const
204 {
205 return false;
206 }
207
208 /* Build the symbol name component sorted vector, if we haven't
209 yet. */
210 void build_name_components (dwarf2_per_objfile *per_objfile);
211
212 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
213 possible matches for LN_NO_PARAMS in the name component
214 vector. */
215 std::pair<std::vector<name_component>::const_iterator,
216 std::vector<name_component>::const_iterator>
217 find_name_components_bounds (const lookup_name_info &ln_no_params,
218 enum language lang,
219 dwarf2_per_objfile *per_objfile) const;
220
221 /* Prevent deleting/destroying via a base class pointer. */
222 protected:
223 ~mapped_index_base() = default;
224 };
225
226 /* A description of the mapped index. The file format is described in
227 a comment by the code that writes the index. */
228 struct mapped_index final : public mapped_index_base
229 {
230 /* A slot/bucket in the symbol table hash. */
231 struct symbol_table_slot
232 {
233 const offset_type name;
234 const offset_type vec;
235 };
236
237 /* Index data format version. */
238 int version = 0;
239
240 /* The address table data. */
241 gdb::array_view<const gdb_byte> address_table;
242
243 /* The symbol table, implemented as a hash table. */
244 gdb::array_view<symbol_table_slot> symbol_table;
245
246 /* A pointer to the constant pool. */
247 const char *constant_pool = nullptr;
248
249 bool symbol_name_slot_invalid (offset_type idx) const override
250 {
251 const auto &bucket = this->symbol_table[idx];
252 return bucket.name == 0 && bucket.vec == 0;
253 }
254
255 /* Convenience method to get at the name of the symbol at IDX in the
256 symbol table. */
257 const char *symbol_name_at
258 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
259 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
260
261 size_t symbol_name_count () const override
262 { return this->symbol_table.size (); }
263 };
264
265 /* A description of the mapped .debug_names.
266 Uninitialized map has CU_COUNT 0. */
267 struct mapped_debug_names final : public mapped_index_base
268 {
269 bfd_endian dwarf5_byte_order;
270 bool dwarf5_is_dwarf64;
271 bool augmentation_is_gdb;
272 uint8_t offset_size;
273 uint32_t cu_count = 0;
274 uint32_t tu_count, bucket_count, name_count;
275 const gdb_byte *cu_table_reordered, *tu_table_reordered;
276 const uint32_t *bucket_table_reordered, *hash_table_reordered;
277 const gdb_byte *name_table_string_offs_reordered;
278 const gdb_byte *name_table_entry_offs_reordered;
279 const gdb_byte *entry_pool;
280
281 struct index_val
282 {
283 ULONGEST dwarf_tag;
284 struct attr
285 {
286 /* Attribute name DW_IDX_*. */
287 ULONGEST dw_idx;
288
289 /* Attribute form DW_FORM_*. */
290 ULONGEST form;
291
292 /* Value if FORM is DW_FORM_implicit_const. */
293 LONGEST implicit_const;
294 };
295 std::vector<attr> attr_vec;
296 };
297
298 std::unordered_map<ULONGEST, index_val> abbrev_map;
299
300 const char *namei_to_name
301 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
302
303 /* Implementation of the mapped_index_base virtual interface, for
304 the name_components cache. */
305
306 const char *symbol_name_at
307 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
308 { return namei_to_name (idx, per_objfile); }
309
310 size_t symbol_name_count () const override
311 { return this->name_count; }
312 };
313
314 /* See dwarf2read.h. */
315
316 dwarf2_per_objfile *
317 get_dwarf2_per_objfile (struct objfile *objfile)
318 {
319 return dwarf2_objfile_data_key.get (objfile);
320 }
321
322 /* Default names of the debugging sections. */
323
324 /* Note that if the debugging section has been compressed, it might
325 have a name like .zdebug_info. */
326
327 const struct dwarf2_debug_sections dwarf2_elf_names =
328 {
329 { ".debug_info", ".zdebug_info" },
330 { ".debug_abbrev", ".zdebug_abbrev" },
331 { ".debug_line", ".zdebug_line" },
332 { ".debug_loc", ".zdebug_loc" },
333 { ".debug_loclists", ".zdebug_loclists" },
334 { ".debug_macinfo", ".zdebug_macinfo" },
335 { ".debug_macro", ".zdebug_macro" },
336 { ".debug_str", ".zdebug_str" },
337 { ".debug_str_offsets", ".zdebug_str_offsets" },
338 { ".debug_line_str", ".zdebug_line_str" },
339 { ".debug_ranges", ".zdebug_ranges" },
340 { ".debug_rnglists", ".zdebug_rnglists" },
341 { ".debug_types", ".zdebug_types" },
342 { ".debug_addr", ".zdebug_addr" },
343 { ".debug_frame", ".zdebug_frame" },
344 { ".eh_frame", NULL },
345 { ".gdb_index", ".zgdb_index" },
346 { ".debug_names", ".zdebug_names" },
347 { ".debug_aranges", ".zdebug_aranges" },
348 23
349 };
350
351 /* List of DWO/DWP sections. */
352
353 static const struct dwop_section_names
354 {
355 struct dwarf2_section_names abbrev_dwo;
356 struct dwarf2_section_names info_dwo;
357 struct dwarf2_section_names line_dwo;
358 struct dwarf2_section_names loc_dwo;
359 struct dwarf2_section_names loclists_dwo;
360 struct dwarf2_section_names macinfo_dwo;
361 struct dwarf2_section_names macro_dwo;
362 struct dwarf2_section_names rnglists_dwo;
363 struct dwarf2_section_names str_dwo;
364 struct dwarf2_section_names str_offsets_dwo;
365 struct dwarf2_section_names types_dwo;
366 struct dwarf2_section_names cu_index;
367 struct dwarf2_section_names tu_index;
368 }
369 dwop_section_names =
370 {
371 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
372 { ".debug_info.dwo", ".zdebug_info.dwo" },
373 { ".debug_line.dwo", ".zdebug_line.dwo" },
374 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
375 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
376 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
377 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
378 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
379 { ".debug_str.dwo", ".zdebug_str.dwo" },
380 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
381 { ".debug_types.dwo", ".zdebug_types.dwo" },
382 { ".debug_cu_index", ".zdebug_cu_index" },
383 { ".debug_tu_index", ".zdebug_tu_index" },
384 };
385
386 /* local data types */
387
388 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
389 begin with a header, which contains the following information. */
390 struct loclists_rnglists_header
391 {
392 /* A 4-byte or 12-byte length containing the length of the
393 set of entries for this compilation unit, not including the
394 length field itself. */
395 unsigned int length;
396
397 /* A 2-byte version identifier. */
398 short version;
399
400 /* A 1-byte unsigned integer containing the size in bytes of an address on
401 the target system. */
402 unsigned char addr_size;
403
404 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
405 on the target system. */
406 unsigned char segment_collector_size;
407
408 /* A 4-byte count of the number of offsets that follow the header. */
409 unsigned int offset_entry_count;
410 };
411
412 /* Type used for delaying computation of method physnames.
413 See comments for compute_delayed_physnames. */
414 struct delayed_method_info
415 {
416 /* The type to which the method is attached, i.e., its parent class. */
417 struct type *type;
418
419 /* The index of the method in the type's function fieldlists. */
420 int fnfield_index;
421
422 /* The index of the method in the fieldlist. */
423 int index;
424
425 /* The name of the DIE. */
426 const char *name;
427
428 /* The DIE associated with this method. */
429 struct die_info *die;
430 };
431
432 /* Internal state when decoding a particular compilation unit. */
433 struct dwarf2_cu
434 {
435 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
436 dwarf2_per_objfile *per_objfile);
437
438 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
439
440 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
441 Create the set of symtabs used by this TU, or if this TU is sharing
442 symtabs with another TU and the symtabs have already been created
443 then restore those symtabs in the line header.
444 We don't need the pc/line-number mapping for type units. */
445 void setup_type_unit_groups (struct die_info *die);
446
447 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
448 buildsym_compunit constructor. */
449 struct compunit_symtab *start_symtab (const char *name,
450 const char *comp_dir,
451 CORE_ADDR low_pc);
452
453 /* Reset the builder. */
454 void reset_builder () { m_builder.reset (); }
455
456 /* Return a type that is a generic pointer type, the size of which
457 matches the address size given in the compilation unit header for
458 this CU. */
459 struct type *addr_type () const;
460
461 /* Find an integer type the same size as the address size given in
462 the compilation unit header for this CU. UNSIGNED_P controls if
463 the integer is unsigned or not. */
464 struct type *addr_sized_int_type (bool unsigned_p) const;
465
466 /* The header of the compilation unit. */
467 struct comp_unit_head header {};
468
469 /* Base address of this compilation unit. */
470 gdb::optional<CORE_ADDR> base_address;
471
472 /* The language we are debugging. */
473 enum language language = language_unknown;
474 const struct language_defn *language_defn = nullptr;
475
476 const char *producer = nullptr;
477
478 private:
479 /* The symtab builder for this CU. This is only non-NULL when full
480 symbols are being read. */
481 std::unique_ptr<buildsym_compunit> m_builder;
482
483 public:
484 /* The generic symbol table building routines have separate lists for
485 file scope symbols and all all other scopes (local scopes). So
486 we need to select the right one to pass to add_symbol_to_list().
487 We do it by keeping a pointer to the correct list in list_in_scope.
488
489 FIXME: The original dwarf code just treated the file scope as the
490 first local scope, and all other local scopes as nested local
491 scopes, and worked fine. Check to see if we really need to
492 distinguish these in buildsym.c. */
493 struct pending **list_in_scope = nullptr;
494
495 /* Hash table holding all the loaded partial DIEs
496 with partial_die->offset.SECT_OFF as hash. */
497 htab_t partial_dies = nullptr;
498
499 /* Storage for things with the same lifetime as this read-in compilation
500 unit, including partial DIEs. */
501 auto_obstack comp_unit_obstack;
502
503 /* Backlink to our per_cu entry. */
504 struct dwarf2_per_cu_data *per_cu;
505
506 /* The dwarf2_per_objfile that owns this. */
507 dwarf2_per_objfile *per_objfile;
508
509 /* How many compilation units ago was this CU last referenced? */
510 int last_used = 0;
511
512 /* A hash table of DIE cu_offset for following references with
513 die_info->offset.sect_off as hash. */
514 htab_t die_hash = nullptr;
515
516 /* Full DIEs if read in. */
517 struct die_info *dies = nullptr;
518
519 /* A set of pointers to dwarf2_per_cu_data objects for compilation
520 units referenced by this one. Only set during full symbol processing;
521 partial symbol tables do not have dependencies. */
522 htab_t dependencies = nullptr;
523
524 /* Header data from the line table, during full symbol processing. */
525 struct line_header *line_header = nullptr;
526 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
527 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
528 this is the DW_TAG_compile_unit die for this CU. We'll hold on
529 to the line header as long as this DIE is being processed. See
530 process_die_scope. */
531 die_info *line_header_die_owner = nullptr;
532
533 /* A list of methods which need to have physnames computed
534 after all type information has been read. */
535 std::vector<delayed_method_info> method_list;
536
537 /* To be copied to symtab->call_site_htab. */
538 htab_t call_site_htab = nullptr;
539
540 /* Non-NULL if this CU came from a DWO file.
541 There is an invariant here that is important to remember:
542 Except for attributes copied from the top level DIE in the "main"
543 (or "stub") file in preparation for reading the DWO file
544 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
545 Either there isn't a DWO file (in which case this is NULL and the point
546 is moot), or there is and either we're not going to read it (in which
547 case this is NULL) or there is and we are reading it (in which case this
548 is non-NULL). */
549 struct dwo_unit *dwo_unit = nullptr;
550
551 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
552 Note this value comes from the Fission stub CU/TU's DIE. */
553 gdb::optional<ULONGEST> addr_base;
554
555 /* The DW_AT_GNU_ranges_base attribute, if present.
556
557 This is only relevant in the context of pre-DWARF 5 split units. In this
558 context, there is a .debug_ranges section in the linked executable,
559 containing all the ranges data for all the compilation units. Each
560 skeleton/stub unit has (if needed) a DW_AT_GNU_ranges_base attribute that
561 indicates the base of its contribution to that section. The DW_AT_ranges
562 attributes in the split-unit are of the form DW_FORM_sec_offset and point
563 into the .debug_ranges section of the linked file. However, they are not
564 "true" DW_FORM_sec_offset, because they are relative to the base of their
565 compilation unit's contribution, rather than relative to the beginning of
566 the section. The DW_AT_GNU_ranges_base value must be added to it to make
567 it relative to the beginning of the section.
568
569 Note that the value is zero when we are not in a pre-DWARF 5 split-unit
570 case, so this value can be added without needing to know whether we are in
571 this case or not.
572
573 N.B. If a DW_AT_ranges attribute is found on the DW_TAG_compile_unit in the
574 skeleton/stub, it must not have the base added, as it already points to the
575 right place. And since the DW_TAG_compile_unit DIE in the split-unit can't
576 have a DW_AT_ranges attribute, we can use the
577
578 die->tag != DW_AT_compile_unit
579
580 to determine whether the base should be added or not. */
581 ULONGEST gnu_ranges_base = 0;
582
583 /* The DW_AT_rnglists_base attribute, if present.
584
585 This is used when processing attributes of form DW_FORM_rnglistx in
586 non-split units. Attributes of this form found in a split unit don't
587 use it, as split-unit files have their own non-shared .debug_rnglists.dwo
588 section. */
589 ULONGEST rnglists_base = 0;
590
591 /* The DW_AT_loclists_base attribute if present. */
592 ULONGEST loclist_base = 0;
593
594 /* When reading debug info generated by older versions of rustc, we
595 have to rewrite some union types to be struct types with a
596 variant part. This rewriting must be done after the CU is fully
597 read in, because otherwise at the point of rewriting some struct
598 type might not have been fully processed. So, we keep a list of
599 all such types here and process them after expansion. */
600 std::vector<struct type *> rust_unions;
601
602 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
603 files, the value is implicitly zero. For DWARF 5 version DWO files, the
604 value is often implicit and is the size of the header of
605 .debug_str_offsets section (8 or 4, depending on the address size). */
606 gdb::optional<ULONGEST> str_offsets_base;
607
608 /* Mark used when releasing cached dies. */
609 bool mark : 1;
610
611 /* This CU references .debug_loc. See the symtab->locations_valid field.
612 This test is imperfect as there may exist optimized debug code not using
613 any location list and still facing inlining issues if handled as
614 unoptimized code. For a future better test see GCC PR other/32998. */
615 bool has_loclist : 1;
616
617 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
618 if all the producer_is_* fields are valid. This information is cached
619 because profiling CU expansion showed excessive time spent in
620 producer_is_gxx_lt_4_6. */
621 bool checked_producer : 1;
622 bool producer_is_gxx_lt_4_6 : 1;
623 bool producer_is_gcc_lt_4_3 : 1;
624 bool producer_is_icc : 1;
625 bool producer_is_icc_lt_14 : 1;
626 bool producer_is_codewarrior : 1;
627
628 /* When true, the file that we're processing is known to have
629 debugging info for C++ namespaces. GCC 3.3.x did not produce
630 this information, but later versions do. */
631
632 bool processing_has_namespace_info : 1;
633
634 struct partial_die_info *find_partial_die (sect_offset sect_off);
635
636 /* If this CU was inherited by another CU (via specification,
637 abstract_origin, etc), this is the ancestor CU. */
638 dwarf2_cu *ancestor;
639
640 /* Get the buildsym_compunit for this CU. */
641 buildsym_compunit *get_builder ()
642 {
643 /* If this CU has a builder associated with it, use that. */
644 if (m_builder != nullptr)
645 return m_builder.get ();
646
647 /* Otherwise, search ancestors for a valid builder. */
648 if (ancestor != nullptr)
649 return ancestor->get_builder ();
650
651 return nullptr;
652 }
653 };
654
655 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
656 This includes type_unit_group and quick_file_names. */
657
658 struct stmt_list_hash
659 {
660 /* The DWO unit this table is from or NULL if there is none. */
661 struct dwo_unit *dwo_unit;
662
663 /* Offset in .debug_line or .debug_line.dwo. */
664 sect_offset line_sect_off;
665 };
666
667 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
668 an object of this type. This contains elements of type unit groups
669 that can be shared across objfiles. The non-shareable parts are in
670 type_unit_group_unshareable. */
671
672 struct type_unit_group
673 {
674 /* dwarf2read.c's main "handle" on a TU symtab.
675 To simplify things we create an artificial CU that "includes" all the
676 type units using this stmt_list so that the rest of the code still has
677 a "per_cu" handle on the symtab. */
678 struct dwarf2_per_cu_data per_cu;
679
680 /* The TUs that share this DW_AT_stmt_list entry.
681 This is added to while parsing type units to build partial symtabs,
682 and is deleted afterwards and not used again. */
683 std::vector<signatured_type *> *tus;
684
685 /* The data used to construct the hash key. */
686 struct stmt_list_hash hash;
687 };
688
689 /* These sections are what may appear in a (real or virtual) DWO file. */
690
691 struct dwo_sections
692 {
693 struct dwarf2_section_info abbrev;
694 struct dwarf2_section_info line;
695 struct dwarf2_section_info loc;
696 struct dwarf2_section_info loclists;
697 struct dwarf2_section_info macinfo;
698 struct dwarf2_section_info macro;
699 struct dwarf2_section_info rnglists;
700 struct dwarf2_section_info str;
701 struct dwarf2_section_info str_offsets;
702 /* In the case of a virtual DWO file, these two are unused. */
703 struct dwarf2_section_info info;
704 std::vector<dwarf2_section_info> types;
705 };
706
707 /* CUs/TUs in DWP/DWO files. */
708
709 struct dwo_unit
710 {
711 /* Backlink to the containing struct dwo_file. */
712 struct dwo_file *dwo_file;
713
714 /* The "id" that distinguishes this CU/TU.
715 .debug_info calls this "dwo_id", .debug_types calls this "signature".
716 Since signatures came first, we stick with it for consistency. */
717 ULONGEST signature;
718
719 /* The section this CU/TU lives in, in the DWO file. */
720 struct dwarf2_section_info *section;
721
722 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
723 sect_offset sect_off;
724 unsigned int length;
725
726 /* For types, offset in the type's DIE of the type defined by this TU. */
727 cu_offset type_offset_in_tu;
728 };
729
730 /* include/dwarf2.h defines the DWP section codes.
731 It defines a max value but it doesn't define a min value, which we
732 use for error checking, so provide one. */
733
734 enum dwp_v2_section_ids
735 {
736 DW_SECT_MIN = 1
737 };
738
739 /* Data for one DWO file.
740
741 This includes virtual DWO files (a virtual DWO file is a DWO file as it
742 appears in a DWP file). DWP files don't really have DWO files per se -
743 comdat folding of types "loses" the DWO file they came from, and from
744 a high level view DWP files appear to contain a mass of random types.
745 However, to maintain consistency with the non-DWP case we pretend DWP
746 files contain virtual DWO files, and we assign each TU with one virtual
747 DWO file (generally based on the line and abbrev section offsets -
748 a heuristic that seems to work in practice). */
749
750 struct dwo_file
751 {
752 dwo_file () = default;
753 DISABLE_COPY_AND_ASSIGN (dwo_file);
754
755 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
756 For virtual DWO files the name is constructed from the section offsets
757 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
758 from related CU+TUs. */
759 const char *dwo_name = nullptr;
760
761 /* The DW_AT_comp_dir attribute. */
762 const char *comp_dir = nullptr;
763
764 /* The bfd, when the file is open. Otherwise this is NULL.
765 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
766 gdb_bfd_ref_ptr dbfd;
767
768 /* The sections that make up this DWO file.
769 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
770 sections (for lack of a better name). */
771 struct dwo_sections sections {};
772
773 /* The CUs in the file.
774 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
775 an extension to handle LLVM's Link Time Optimization output (where
776 multiple source files may be compiled into a single object/dwo pair). */
777 htab_up cus;
778
779 /* Table of TUs in the file.
780 Each element is a struct dwo_unit. */
781 htab_up tus;
782 };
783
784 /* These sections are what may appear in a DWP file. */
785
786 struct dwp_sections
787 {
788 /* These are used by all DWP versions (1, 2 and 5). */
789 struct dwarf2_section_info str;
790 struct dwarf2_section_info cu_index;
791 struct dwarf2_section_info tu_index;
792
793 /* These are only used by DWP version 2 and version 5 files.
794 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
795 sections are referenced by section number, and are not recorded here.
796 In DWP version 2 or 5 there is at most one copy of all these sections,
797 each section being (effectively) comprised of the concatenation of all of
798 the individual sections that exist in the version 1 format.
799 To keep the code simple we treat each of these concatenated pieces as a
800 section itself (a virtual section?). */
801 struct dwarf2_section_info abbrev;
802 struct dwarf2_section_info info;
803 struct dwarf2_section_info line;
804 struct dwarf2_section_info loc;
805 struct dwarf2_section_info loclists;
806 struct dwarf2_section_info macinfo;
807 struct dwarf2_section_info macro;
808 struct dwarf2_section_info rnglists;
809 struct dwarf2_section_info str_offsets;
810 struct dwarf2_section_info types;
811 };
812
813 /* These sections are what may appear in a virtual DWO file in DWP version 1.
814 A virtual DWO file is a DWO file as it appears in a DWP file. */
815
816 struct virtual_v1_dwo_sections
817 {
818 struct dwarf2_section_info abbrev;
819 struct dwarf2_section_info line;
820 struct dwarf2_section_info loc;
821 struct dwarf2_section_info macinfo;
822 struct dwarf2_section_info macro;
823 struct dwarf2_section_info str_offsets;
824 /* Each DWP hash table entry records one CU or one TU.
825 That is recorded here, and copied to dwo_unit.section. */
826 struct dwarf2_section_info info_or_types;
827 };
828
829 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
830 In version 2, the sections of the DWO files are concatenated together
831 and stored in one section of that name. Thus each ELF section contains
832 several "virtual" sections. */
833
834 struct virtual_v2_or_v5_dwo_sections
835 {
836 bfd_size_type abbrev_offset;
837 bfd_size_type abbrev_size;
838
839 bfd_size_type line_offset;
840 bfd_size_type line_size;
841
842 bfd_size_type loc_offset;
843 bfd_size_type loc_size;
844
845 bfd_size_type loclists_offset;
846 bfd_size_type loclists_size;
847
848 bfd_size_type macinfo_offset;
849 bfd_size_type macinfo_size;
850
851 bfd_size_type macro_offset;
852 bfd_size_type macro_size;
853
854 bfd_size_type rnglists_offset;
855 bfd_size_type rnglists_size;
856
857 bfd_size_type str_offsets_offset;
858 bfd_size_type str_offsets_size;
859
860 /* Each DWP hash table entry records one CU or one TU.
861 That is recorded here, and copied to dwo_unit.section. */
862 bfd_size_type info_or_types_offset;
863 bfd_size_type info_or_types_size;
864 };
865
866 /* Contents of DWP hash tables. */
867
868 struct dwp_hash_table
869 {
870 uint32_t version, nr_columns;
871 uint32_t nr_units, nr_slots;
872 const gdb_byte *hash_table, *unit_table;
873 union
874 {
875 struct
876 {
877 const gdb_byte *indices;
878 } v1;
879 struct
880 {
881 /* This is indexed by column number and gives the id of the section
882 in that column. */
883 #define MAX_NR_V2_DWO_SECTIONS \
884 (1 /* .debug_info or .debug_types */ \
885 + 1 /* .debug_abbrev */ \
886 + 1 /* .debug_line */ \
887 + 1 /* .debug_loc */ \
888 + 1 /* .debug_str_offsets */ \
889 + 1 /* .debug_macro or .debug_macinfo */)
890 int section_ids[MAX_NR_V2_DWO_SECTIONS];
891 const gdb_byte *offsets;
892 const gdb_byte *sizes;
893 } v2;
894 struct
895 {
896 /* This is indexed by column number and gives the id of the section
897 in that column. */
898 #define MAX_NR_V5_DWO_SECTIONS \
899 (1 /* .debug_info */ \
900 + 1 /* .debug_abbrev */ \
901 + 1 /* .debug_line */ \
902 + 1 /* .debug_loclists */ \
903 + 1 /* .debug_str_offsets */ \
904 + 1 /* .debug_macro */ \
905 + 1 /* .debug_rnglists */)
906 int section_ids[MAX_NR_V5_DWO_SECTIONS];
907 const gdb_byte *offsets;
908 const gdb_byte *sizes;
909 } v5;
910 } section_pool;
911 };
912
913 /* Data for one DWP file. */
914
915 struct dwp_file
916 {
917 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
918 : name (name_),
919 dbfd (std::move (abfd))
920 {
921 }
922
923 /* Name of the file. */
924 const char *name;
925
926 /* File format version. */
927 int version = 0;
928
929 /* The bfd. */
930 gdb_bfd_ref_ptr dbfd;
931
932 /* Section info for this file. */
933 struct dwp_sections sections {};
934
935 /* Table of CUs in the file. */
936 const struct dwp_hash_table *cus = nullptr;
937
938 /* Table of TUs in the file. */
939 const struct dwp_hash_table *tus = nullptr;
940
941 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
942 htab_up loaded_cus;
943 htab_up loaded_tus;
944
945 /* Table to map ELF section numbers to their sections.
946 This is only needed for the DWP V1 file format. */
947 unsigned int num_sections = 0;
948 asection **elf_sections = nullptr;
949 };
950
951 /* Struct used to pass misc. parameters to read_die_and_children, et
952 al. which are used for both .debug_info and .debug_types dies.
953 All parameters here are unchanging for the life of the call. This
954 struct exists to abstract away the constant parameters of die reading. */
955
956 struct die_reader_specs
957 {
958 /* The bfd of die_section. */
959 bfd *abfd;
960
961 /* The CU of the DIE we are parsing. */
962 struct dwarf2_cu *cu;
963
964 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
965 struct dwo_file *dwo_file;
966
967 /* The section the die comes from.
968 This is either .debug_info or .debug_types, or the .dwo variants. */
969 struct dwarf2_section_info *die_section;
970
971 /* die_section->buffer. */
972 const gdb_byte *buffer;
973
974 /* The end of the buffer. */
975 const gdb_byte *buffer_end;
976
977 /* The abbreviation table to use when reading the DIEs. */
978 struct abbrev_table *abbrev_table;
979 };
980
981 /* A subclass of die_reader_specs that holds storage and has complex
982 constructor and destructor behavior. */
983
984 class cutu_reader : public die_reader_specs
985 {
986 public:
987
988 cutu_reader (dwarf2_per_cu_data *this_cu,
989 dwarf2_per_objfile *per_objfile,
990 struct abbrev_table *abbrev_table,
991 dwarf2_cu *existing_cu,
992 bool skip_partial);
993
994 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
995 dwarf2_per_objfile *per_objfile,
996 struct dwarf2_cu *parent_cu = nullptr,
997 struct dwo_file *dwo_file = nullptr);
998
999 DISABLE_COPY_AND_ASSIGN (cutu_reader);
1000
1001 const gdb_byte *info_ptr = nullptr;
1002 struct die_info *comp_unit_die = nullptr;
1003 bool dummy_p = false;
1004
1005 /* Release the new CU, putting it on the chain. This cannot be done
1006 for dummy CUs. */
1007 void keep ();
1008
1009 private:
1010 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
1011 dwarf2_per_objfile *per_objfile,
1012 dwarf2_cu *existing_cu);
1013
1014 struct dwarf2_per_cu_data *m_this_cu;
1015 std::unique_ptr<dwarf2_cu> m_new_cu;
1016
1017 /* The ordinary abbreviation table. */
1018 abbrev_table_up m_abbrev_table_holder;
1019
1020 /* The DWO abbreviation table. */
1021 abbrev_table_up m_dwo_abbrev_table;
1022 };
1023
1024 /* When we construct a partial symbol table entry we only
1025 need this much information. */
1026 struct partial_die_info : public allocate_on_obstack
1027 {
1028 partial_die_info (sect_offset sect_off, const struct abbrev_info *abbrev);
1029
1030 /* Disable assign but still keep copy ctor, which is needed
1031 load_partial_dies. */
1032 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1033
1034 /* Adjust the partial die before generating a symbol for it. This
1035 function may set the is_external flag or change the DIE's
1036 name. */
1037 void fixup (struct dwarf2_cu *cu);
1038
1039 /* Read a minimal amount of information into the minimal die
1040 structure. */
1041 const gdb_byte *read (const struct die_reader_specs *reader,
1042 const struct abbrev_info &abbrev,
1043 const gdb_byte *info_ptr);
1044
1045 /* Compute the name of this partial DIE. This memoizes the
1046 result, so it is safe to call multiple times. */
1047 const char *name (dwarf2_cu *cu);
1048
1049 /* Offset of this DIE. */
1050 const sect_offset sect_off;
1051
1052 /* DWARF-2 tag for this DIE. */
1053 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1054
1055 /* Assorted flags describing the data found in this DIE. */
1056 const unsigned int has_children : 1;
1057
1058 unsigned int is_external : 1;
1059 unsigned int is_declaration : 1;
1060 unsigned int has_type : 1;
1061 unsigned int has_specification : 1;
1062 unsigned int has_pc_info : 1;
1063 unsigned int may_be_inlined : 1;
1064
1065 /* This DIE has been marked DW_AT_main_subprogram. */
1066 unsigned int main_subprogram : 1;
1067
1068 /* Flag set if the SCOPE field of this structure has been
1069 computed. */
1070 unsigned int scope_set : 1;
1071
1072 /* Flag set if the DIE has a byte_size attribute. */
1073 unsigned int has_byte_size : 1;
1074
1075 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1076 unsigned int has_const_value : 1;
1077
1078 /* Flag set if any of the DIE's children are template arguments. */
1079 unsigned int has_template_arguments : 1;
1080
1081 /* Flag set if fixup has been called on this die. */
1082 unsigned int fixup_called : 1;
1083
1084 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1085 unsigned int is_dwz : 1;
1086
1087 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1088 unsigned int spec_is_dwz : 1;
1089
1090 unsigned int canonical_name : 1;
1091
1092 /* The name of this DIE. Normally the value of DW_AT_name, but
1093 sometimes a default name for unnamed DIEs. */
1094 const char *raw_name = nullptr;
1095
1096 /* The linkage name, if present. */
1097 const char *linkage_name = nullptr;
1098
1099 /* The scope to prepend to our children. This is generally
1100 allocated on the comp_unit_obstack, so will disappear
1101 when this compilation unit leaves the cache. */
1102 const char *scope = nullptr;
1103
1104 /* Some data associated with the partial DIE. The tag determines
1105 which field is live. */
1106 union
1107 {
1108 /* The location description associated with this DIE, if any. */
1109 struct dwarf_block *locdesc;
1110 /* The offset of an import, for DW_TAG_imported_unit. */
1111 sect_offset sect_off;
1112 } d {};
1113
1114 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1115 CORE_ADDR lowpc = 0;
1116 CORE_ADDR highpc = 0;
1117
1118 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1119 DW_AT_sibling, if any. */
1120 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1121 could return DW_AT_sibling values to its caller load_partial_dies. */
1122 const gdb_byte *sibling = nullptr;
1123
1124 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1125 DW_AT_specification (or DW_AT_abstract_origin or
1126 DW_AT_extension). */
1127 sect_offset spec_offset {};
1128
1129 /* Pointers to this DIE's parent, first child, and next sibling,
1130 if any. */
1131 struct partial_die_info *die_parent = nullptr;
1132 struct partial_die_info *die_child = nullptr;
1133 struct partial_die_info *die_sibling = nullptr;
1134
1135 friend struct partial_die_info *
1136 dwarf2_cu::find_partial_die (sect_offset sect_off);
1137
1138 private:
1139 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1140 partial_die_info (sect_offset sect_off)
1141 : partial_die_info (sect_off, DW_TAG_padding, 0)
1142 {
1143 }
1144
1145 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1146 int has_children_)
1147 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1148 {
1149 is_external = 0;
1150 is_declaration = 0;
1151 has_type = 0;
1152 has_specification = 0;
1153 has_pc_info = 0;
1154 may_be_inlined = 0;
1155 main_subprogram = 0;
1156 scope_set = 0;
1157 has_byte_size = 0;
1158 has_const_value = 0;
1159 has_template_arguments = 0;
1160 fixup_called = 0;
1161 is_dwz = 0;
1162 spec_is_dwz = 0;
1163 canonical_name = 0;
1164 }
1165 };
1166
1167 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1168 but this would require a corresponding change in unpack_field_as_long
1169 and friends. */
1170 static int bits_per_byte = 8;
1171
1172 struct variant_part_builder;
1173
1174 /* When reading a variant, we track a bit more information about the
1175 field, and store it in an object of this type. */
1176
1177 struct variant_field
1178 {
1179 int first_field = -1;
1180 int last_field = -1;
1181
1182 /* A variant can contain other variant parts. */
1183 std::vector<variant_part_builder> variant_parts;
1184
1185 /* If we see a DW_TAG_variant, then this will be set if this is the
1186 default branch. */
1187 bool default_branch = false;
1188 /* If we see a DW_AT_discr_value, then this will be the discriminant
1189 value. */
1190 ULONGEST discriminant_value = 0;
1191 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1192 data. */
1193 struct dwarf_block *discr_list_data = nullptr;
1194 };
1195
1196 /* This represents a DW_TAG_variant_part. */
1197
1198 struct variant_part_builder
1199 {
1200 /* The offset of the discriminant field. */
1201 sect_offset discriminant_offset {};
1202
1203 /* Variants that are direct children of this variant part. */
1204 std::vector<variant_field> variants;
1205
1206 /* True if we're currently reading a variant. */
1207 bool processing_variant = false;
1208 };
1209
1210 struct nextfield
1211 {
1212 int accessibility = 0;
1213 int virtuality = 0;
1214 /* Variant parts need to find the discriminant, which is a DIE
1215 reference. We track the section offset of each field to make
1216 this link. */
1217 sect_offset offset;
1218 struct field field {};
1219 };
1220
1221 struct fnfieldlist
1222 {
1223 const char *name = nullptr;
1224 std::vector<struct fn_field> fnfields;
1225 };
1226
1227 /* The routines that read and process dies for a C struct or C++ class
1228 pass lists of data member fields and lists of member function fields
1229 in an instance of a field_info structure, as defined below. */
1230 struct field_info
1231 {
1232 /* List of data member and baseclasses fields. */
1233 std::vector<struct nextfield> fields;
1234 std::vector<struct nextfield> baseclasses;
1235
1236 /* Set if the accessibility of one of the fields is not public. */
1237 bool non_public_fields = false;
1238
1239 /* Member function fieldlist array, contains name of possibly overloaded
1240 member function, number of overloaded member functions and a pointer
1241 to the head of the member function field chain. */
1242 std::vector<struct fnfieldlist> fnfieldlists;
1243
1244 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1245 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1246 std::vector<struct decl_field> typedef_field_list;
1247
1248 /* Nested types defined by this class and the number of elements in this
1249 list. */
1250 std::vector<struct decl_field> nested_types_list;
1251
1252 /* If non-null, this is the variant part we are currently
1253 reading. */
1254 variant_part_builder *current_variant_part = nullptr;
1255 /* This holds all the top-level variant parts attached to the type
1256 we're reading. */
1257 std::vector<variant_part_builder> variant_parts;
1258
1259 /* Return the total number of fields (including baseclasses). */
1260 int nfields () const
1261 {
1262 return fields.size () + baseclasses.size ();
1263 }
1264 };
1265
1266 /* Loaded secondary compilation units are kept in memory until they
1267 have not been referenced for the processing of this many
1268 compilation units. Set this to zero to disable caching. Cache
1269 sizes of up to at least twenty will improve startup time for
1270 typical inter-CU-reference binaries, at an obvious memory cost. */
1271 static int dwarf_max_cache_age = 5;
1272 static void
1273 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1274 struct cmd_list_element *c, const char *value)
1275 {
1276 fprintf_filtered (file, _("The upper bound on the age of cached "
1277 "DWARF compilation units is %s.\n"),
1278 value);
1279 }
1280 \f
1281 /* local function prototypes */
1282
1283 static void dwarf2_find_base_address (struct die_info *die,
1284 struct dwarf2_cu *cu);
1285
1286 static dwarf2_psymtab *create_partial_symtab
1287 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1288 const char *name);
1289
1290 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1291 const gdb_byte *info_ptr,
1292 struct die_info *type_unit_die);
1293
1294 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
1295
1296 static void scan_partial_symbols (struct partial_die_info *,
1297 CORE_ADDR *, CORE_ADDR *,
1298 int, struct dwarf2_cu *);
1299
1300 static void add_partial_symbol (struct partial_die_info *,
1301 struct dwarf2_cu *);
1302
1303 static void add_partial_namespace (struct partial_die_info *pdi,
1304 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1305 int set_addrmap, struct dwarf2_cu *cu);
1306
1307 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1308 CORE_ADDR *highpc, int set_addrmap,
1309 struct dwarf2_cu *cu);
1310
1311 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1312 struct dwarf2_cu *cu);
1313
1314 static void add_partial_subprogram (struct partial_die_info *pdi,
1315 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1316 int need_pc, struct dwarf2_cu *cu);
1317
1318 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1319
1320 static struct partial_die_info *load_partial_dies
1321 (const struct die_reader_specs *, const gdb_byte *, int);
1322
1323 /* A pair of partial_die_info and compilation unit. */
1324 struct cu_partial_die_info
1325 {
1326 /* The compilation unit of the partial_die_info. */
1327 struct dwarf2_cu *cu;
1328 /* A partial_die_info. */
1329 struct partial_die_info *pdi;
1330
1331 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1332 : cu (cu),
1333 pdi (pdi)
1334 { /* Nothing. */ }
1335
1336 private:
1337 cu_partial_die_info () = delete;
1338 };
1339
1340 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1341 struct dwarf2_cu *);
1342
1343 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1344 struct attribute *,
1345 const struct attr_abbrev *,
1346 const gdb_byte *);
1347
1348 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1349 struct attribute *attr, dwarf_tag tag);
1350
1351 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1352
1353 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1354 dwarf2_section_info *, sect_offset);
1355
1356 static const char *read_indirect_string
1357 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
1358 const struct comp_unit_head *, unsigned int *);
1359
1360 static const char *read_indirect_string_at_offset
1361 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
1362
1363 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1364 const gdb_byte *,
1365 unsigned int *);
1366
1367 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1368 ULONGEST str_index);
1369
1370 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1371 ULONGEST str_index);
1372
1373 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1374
1375 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1376 struct dwarf2_cu *);
1377
1378 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1379 struct dwarf2_cu *cu);
1380
1381 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1382
1383 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1384 struct dwarf2_cu *cu);
1385
1386 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1387
1388 static struct die_info *die_specification (struct die_info *die,
1389 struct dwarf2_cu **);
1390
1391 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1392 struct dwarf2_cu *cu);
1393
1394 static void dwarf_decode_lines (struct line_header *, const char *,
1395 struct dwarf2_cu *, dwarf2_psymtab *,
1396 CORE_ADDR, int decode_mapping);
1397
1398 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1399 const char *);
1400
1401 static struct symbol *new_symbol (struct die_info *, struct type *,
1402 struct dwarf2_cu *, struct symbol * = NULL);
1403
1404 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1405 struct dwarf2_cu *);
1406
1407 static void dwarf2_const_value_attr (const struct attribute *attr,
1408 struct type *type,
1409 const char *name,
1410 struct obstack *obstack,
1411 struct dwarf2_cu *cu, LONGEST *value,
1412 const gdb_byte **bytes,
1413 struct dwarf2_locexpr_baton **baton);
1414
1415 static struct type *read_subrange_index_type (struct die_info *die,
1416 struct dwarf2_cu *cu);
1417
1418 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1419
1420 static int need_gnat_info (struct dwarf2_cu *);
1421
1422 static struct type *die_descriptive_type (struct die_info *,
1423 struct dwarf2_cu *);
1424
1425 static void set_descriptive_type (struct type *, struct die_info *,
1426 struct dwarf2_cu *);
1427
1428 static struct type *die_containing_type (struct die_info *,
1429 struct dwarf2_cu *);
1430
1431 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1432 struct dwarf2_cu *);
1433
1434 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1435
1436 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1437
1438 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1439
1440 static char *typename_concat (struct obstack *obs, const char *prefix,
1441 const char *suffix, int physname,
1442 struct dwarf2_cu *cu);
1443
1444 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1445
1446 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1447
1448 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1449
1450 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1451
1452 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1453
1454 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1455
1456 /* Return the .debug_loclists section to use for cu. */
1457 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1458
1459 /* Return the .debug_rnglists section to use for cu. */
1460 static struct dwarf2_section_info *cu_debug_rnglists_section
1461 (struct dwarf2_cu *cu, dwarf_tag tag);
1462
1463 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1464 values. Keep the items ordered with increasing constraints compliance. */
1465 enum pc_bounds_kind
1466 {
1467 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1468 PC_BOUNDS_NOT_PRESENT,
1469
1470 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1471 were present but they do not form a valid range of PC addresses. */
1472 PC_BOUNDS_INVALID,
1473
1474 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1475 PC_BOUNDS_RANGES,
1476
1477 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1478 PC_BOUNDS_HIGH_LOW,
1479 };
1480
1481 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1482 CORE_ADDR *, CORE_ADDR *,
1483 struct dwarf2_cu *,
1484 dwarf2_psymtab *);
1485
1486 static void get_scope_pc_bounds (struct die_info *,
1487 CORE_ADDR *, CORE_ADDR *,
1488 struct dwarf2_cu *);
1489
1490 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1491 CORE_ADDR, struct dwarf2_cu *);
1492
1493 static void dwarf2_add_field (struct field_info *, struct die_info *,
1494 struct dwarf2_cu *);
1495
1496 static void dwarf2_attach_fields_to_type (struct field_info *,
1497 struct type *, struct dwarf2_cu *);
1498
1499 static void dwarf2_add_member_fn (struct field_info *,
1500 struct die_info *, struct type *,
1501 struct dwarf2_cu *);
1502
1503 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1504 struct type *,
1505 struct dwarf2_cu *);
1506
1507 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1508
1509 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1510
1511 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1512
1513 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1514
1515 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1516
1517 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1518
1519 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1520
1521 static struct type *read_module_type (struct die_info *die,
1522 struct dwarf2_cu *cu);
1523
1524 static const char *namespace_name (struct die_info *die,
1525 int *is_anonymous, struct dwarf2_cu *);
1526
1527 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1528
1529 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1530 bool * = nullptr);
1531
1532 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1533 struct dwarf2_cu *);
1534
1535 static struct die_info *read_die_and_siblings_1
1536 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1537 struct die_info *);
1538
1539 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1540 const gdb_byte *info_ptr,
1541 const gdb_byte **new_info_ptr,
1542 struct die_info *parent);
1543
1544 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1545 struct die_info **, const gdb_byte *,
1546 int);
1547
1548 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1549 struct die_info **, const gdb_byte *);
1550
1551 static void process_die (struct die_info *, struct dwarf2_cu *);
1552
1553 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1554 struct objfile *);
1555
1556 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1557
1558 static const char *dwarf2_full_name (const char *name,
1559 struct die_info *die,
1560 struct dwarf2_cu *cu);
1561
1562 static const char *dwarf2_physname (const char *name, struct die_info *die,
1563 struct dwarf2_cu *cu);
1564
1565 static struct die_info *dwarf2_extension (struct die_info *die,
1566 struct dwarf2_cu **);
1567
1568 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1569
1570 static void dump_die_for_error (struct die_info *);
1571
1572 static void dump_die_1 (struct ui_file *, int level, int max_level,
1573 struct die_info *);
1574
1575 /*static*/ void dump_die (struct die_info *, int max_level);
1576
1577 static void store_in_ref_table (struct die_info *,
1578 struct dwarf2_cu *);
1579
1580 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1581 const struct attribute *,
1582 struct dwarf2_cu **);
1583
1584 static struct die_info *follow_die_ref (struct die_info *,
1585 const struct attribute *,
1586 struct dwarf2_cu **);
1587
1588 static struct die_info *follow_die_sig (struct die_info *,
1589 const struct attribute *,
1590 struct dwarf2_cu **);
1591
1592 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1593 struct dwarf2_cu *);
1594
1595 static struct type *get_DW_AT_signature_type (struct die_info *,
1596 const struct attribute *,
1597 struct dwarf2_cu *);
1598
1599 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1600 dwarf2_per_objfile *per_objfile);
1601
1602 static void read_signatured_type (signatured_type *sig_type,
1603 dwarf2_per_objfile *per_objfile);
1604
1605 static int attr_to_dynamic_prop (const struct attribute *attr,
1606 struct die_info *die, struct dwarf2_cu *cu,
1607 struct dynamic_prop *prop, struct type *type);
1608
1609 /* memory allocation interface */
1610
1611 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1612
1613 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1614
1615 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1616
1617 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1618 struct dwarf2_loclist_baton *baton,
1619 const struct attribute *attr);
1620
1621 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1622 struct symbol *sym,
1623 struct dwarf2_cu *cu,
1624 int is_block);
1625
1626 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1627 const gdb_byte *info_ptr,
1628 const struct abbrev_info *abbrev);
1629
1630 static hashval_t partial_die_hash (const void *item);
1631
1632 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1633
1634 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1635 (sect_offset sect_off, unsigned int offset_in_dwz,
1636 dwarf2_per_objfile *per_objfile);
1637
1638 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1639 struct die_info *comp_unit_die,
1640 enum language pretend_language);
1641
1642 static struct type *set_die_type (struct die_info *, struct type *,
1643 struct dwarf2_cu *, bool = false);
1644
1645 static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
1646
1647 static int create_all_type_units (dwarf2_per_objfile *per_objfile);
1648
1649 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1650 dwarf2_per_objfile *per_objfile,
1651 dwarf2_cu *existing_cu,
1652 bool skip_partial,
1653 enum language pretend_language);
1654
1655 static void process_full_comp_unit (dwarf2_cu *cu,
1656 enum language pretend_language);
1657
1658 static void process_full_type_unit (dwarf2_cu *cu,
1659 enum language pretend_language);
1660
1661 static void dwarf2_add_dependence (struct dwarf2_cu *,
1662 struct dwarf2_per_cu_data *);
1663
1664 static void dwarf2_mark (struct dwarf2_cu *);
1665
1666 static struct type *get_die_type_at_offset (sect_offset,
1667 dwarf2_per_cu_data *per_cu,
1668 dwarf2_per_objfile *per_objfile);
1669
1670 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1671
1672 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1673 dwarf2_per_objfile *per_objfile,
1674 enum language pretend_language);
1675
1676 static void process_queue (dwarf2_per_objfile *per_objfile);
1677
1678 /* Class, the destructor of which frees all allocated queue entries. This
1679 will only have work to do if an error was thrown while processing the
1680 dwarf. If no error was thrown then the queue entries should have all
1681 been processed, and freed, as we went along. */
1682
1683 class dwarf2_queue_guard
1684 {
1685 public:
1686 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1687 : m_per_objfile (per_objfile)
1688 {
1689 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1690
1691 m_per_objfile->per_bfd->queue.emplace ();
1692 }
1693
1694 /* Free any entries remaining on the queue. There should only be
1695 entries left if we hit an error while processing the dwarf. */
1696 ~dwarf2_queue_guard ()
1697 {
1698 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1699
1700 m_per_objfile->per_bfd->queue.reset ();
1701 }
1702
1703 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1704
1705 private:
1706 dwarf2_per_objfile *m_per_objfile;
1707 };
1708
1709 dwarf2_queue_item::~dwarf2_queue_item ()
1710 {
1711 /* Anything still marked queued is likely to be in an
1712 inconsistent state, so discard it. */
1713 if (per_cu->queued)
1714 {
1715 per_objfile->remove_cu (per_cu);
1716 per_cu->queued = 0;
1717 }
1718 }
1719
1720 /* The return type of find_file_and_directory. Note, the enclosed
1721 string pointers are only valid while this object is valid. */
1722
1723 struct file_and_directory
1724 {
1725 /* The filename. This is never NULL. */
1726 const char *name;
1727
1728 /* The compilation directory. NULL if not known. If we needed to
1729 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1730 points directly to the DW_AT_comp_dir string attribute owned by
1731 the obstack that owns the DIE. */
1732 const char *comp_dir;
1733
1734 /* If we needed to build a new string for comp_dir, this is what
1735 owns the storage. */
1736 std::string comp_dir_storage;
1737 };
1738
1739 static file_and_directory find_file_and_directory (struct die_info *die,
1740 struct dwarf2_cu *cu);
1741
1742 static htab_up allocate_signatured_type_table ();
1743
1744 static htab_up allocate_dwo_unit_table ();
1745
1746 static struct dwo_unit *lookup_dwo_unit_in_dwp
1747 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1748 const char *comp_dir, ULONGEST signature, int is_debug_types);
1749
1750 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1751
1752 static struct dwo_unit *lookup_dwo_comp_unit
1753 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1754 ULONGEST signature);
1755
1756 static struct dwo_unit *lookup_dwo_type_unit
1757 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1758
1759 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1760
1761 /* A unique pointer to a dwo_file. */
1762
1763 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1764
1765 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1766
1767 static void check_producer (struct dwarf2_cu *cu);
1768
1769 static void free_line_header_voidp (void *arg);
1770 \f
1771 /* Various complaints about symbol reading that don't abort the process. */
1772
1773 static void
1774 dwarf2_debug_line_missing_file_complaint (void)
1775 {
1776 complaint (_(".debug_line section has line data without a file"));
1777 }
1778
1779 static void
1780 dwarf2_debug_line_missing_end_sequence_complaint (void)
1781 {
1782 complaint (_(".debug_line section has line "
1783 "program sequence without an end"));
1784 }
1785
1786 static void
1787 dwarf2_complex_location_expr_complaint (void)
1788 {
1789 complaint (_("location expression too complex"));
1790 }
1791
1792 static void
1793 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1794 int arg3)
1795 {
1796 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1797 arg1, arg2, arg3);
1798 }
1799
1800 static void
1801 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1802 {
1803 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1804 arg1, arg2);
1805 }
1806
1807 /* Hash function for line_header_hash. */
1808
1809 static hashval_t
1810 line_header_hash (const struct line_header *ofs)
1811 {
1812 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1813 }
1814
1815 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1816
1817 static hashval_t
1818 line_header_hash_voidp (const void *item)
1819 {
1820 const struct line_header *ofs = (const struct line_header *) item;
1821
1822 return line_header_hash (ofs);
1823 }
1824
1825 /* Equality function for line_header_hash. */
1826
1827 static int
1828 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1829 {
1830 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1831 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1832
1833 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1834 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1835 }
1836
1837 \f
1838
1839 /* See declaration. */
1840
1841 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1842 bool can_copy_)
1843 : obfd (obfd),
1844 can_copy (can_copy_)
1845 {
1846 if (names == NULL)
1847 names = &dwarf2_elf_names;
1848
1849 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1850 locate_sections (obfd, sec, *names);
1851 }
1852
1853 dwarf2_per_bfd::~dwarf2_per_bfd ()
1854 {
1855 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1856 per_cu->imported_symtabs_free ();
1857
1858 for (signatured_type *sig_type : all_type_units)
1859 sig_type->per_cu.imported_symtabs_free ();
1860
1861 /* Everything else should be on this->obstack. */
1862 }
1863
1864 /* See read.h. */
1865
1866 void
1867 dwarf2_per_objfile::remove_all_cus ()
1868 {
1869 gdb_assert (!this->per_bfd->queue.has_value ());
1870
1871 for (auto pair : m_dwarf2_cus)
1872 delete pair.second;
1873
1874 m_dwarf2_cus.clear ();
1875 }
1876
1877 /* A helper class that calls free_cached_comp_units on
1878 destruction. */
1879
1880 class free_cached_comp_units
1881 {
1882 public:
1883
1884 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1885 : m_per_objfile (per_objfile)
1886 {
1887 }
1888
1889 ~free_cached_comp_units ()
1890 {
1891 m_per_objfile->remove_all_cus ();
1892 }
1893
1894 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1895
1896 private:
1897
1898 dwarf2_per_objfile *m_per_objfile;
1899 };
1900
1901 /* See read.h. */
1902
1903 bool
1904 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1905 {
1906 gdb_assert (per_cu->index < this->m_symtabs.size ());
1907
1908 return this->m_symtabs[per_cu->index] != nullptr;
1909 }
1910
1911 /* See read.h. */
1912
1913 compunit_symtab *
1914 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1915 {
1916 gdb_assert (per_cu->index < this->m_symtabs.size ());
1917
1918 return this->m_symtabs[per_cu->index];
1919 }
1920
1921 /* See read.h. */
1922
1923 void
1924 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1925 compunit_symtab *symtab)
1926 {
1927 gdb_assert (per_cu->index < this->m_symtabs.size ());
1928 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1929
1930 this->m_symtabs[per_cu->index] = symtab;
1931 }
1932
1933 /* Try to locate the sections we need for DWARF 2 debugging
1934 information and return true if we have enough to do something.
1935 NAMES points to the dwarf2 section names, or is NULL if the standard
1936 ELF names are used. CAN_COPY is true for formats where symbol
1937 interposition is possible and so symbol values must follow copy
1938 relocation rules. */
1939
1940 int
1941 dwarf2_has_info (struct objfile *objfile,
1942 const struct dwarf2_debug_sections *names,
1943 bool can_copy)
1944 {
1945 if (objfile->flags & OBJF_READNEVER)
1946 return 0;
1947
1948 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1949
1950 if (per_objfile == NULL)
1951 {
1952 dwarf2_per_bfd *per_bfd;
1953
1954 /* We can share a "dwarf2_per_bfd" with other objfiles if the BFD
1955 doesn't require relocations and if there aren't partial symbols
1956 from some other reader. */
1957 if (!objfile->has_partial_symbols ()
1958 && !gdb_bfd_requires_relocations (objfile->obfd))
1959 {
1960 /* See if one has been created for this BFD yet. */
1961 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1962
1963 if (per_bfd == nullptr)
1964 {
1965 /* No, create it now. */
1966 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1967 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1968 }
1969 }
1970 else
1971 {
1972 /* No sharing possible, create one specifically for this objfile. */
1973 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1974 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1975 }
1976
1977 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1978 }
1979
1980 return (!per_objfile->per_bfd->info.is_virtual
1981 && per_objfile->per_bfd->info.s.section != NULL
1982 && !per_objfile->per_bfd->abbrev.is_virtual
1983 && per_objfile->per_bfd->abbrev.s.section != NULL);
1984 }
1985
1986 /* See declaration. */
1987
1988 void
1989 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1990 const dwarf2_debug_sections &names)
1991 {
1992 flagword aflag = bfd_section_flags (sectp);
1993
1994 if ((aflag & SEC_HAS_CONTENTS) == 0)
1995 {
1996 }
1997 else if (elf_section_data (sectp)->this_hdr.sh_size
1998 > bfd_get_file_size (abfd))
1999 {
2000 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2001 warning (_("Discarding section %s which has a section size (%s"
2002 ") larger than the file size [in module %s]"),
2003 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2004 bfd_get_filename (abfd));
2005 }
2006 else if (names.info.matches (sectp->name))
2007 {
2008 this->info.s.section = sectp;
2009 this->info.size = bfd_section_size (sectp);
2010 }
2011 else if (names.abbrev.matches (sectp->name))
2012 {
2013 this->abbrev.s.section = sectp;
2014 this->abbrev.size = bfd_section_size (sectp);
2015 }
2016 else if (names.line.matches (sectp->name))
2017 {
2018 this->line.s.section = sectp;
2019 this->line.size = bfd_section_size (sectp);
2020 }
2021 else if (names.loc.matches (sectp->name))
2022 {
2023 this->loc.s.section = sectp;
2024 this->loc.size = bfd_section_size (sectp);
2025 }
2026 else if (names.loclists.matches (sectp->name))
2027 {
2028 this->loclists.s.section = sectp;
2029 this->loclists.size = bfd_section_size (sectp);
2030 }
2031 else if (names.macinfo.matches (sectp->name))
2032 {
2033 this->macinfo.s.section = sectp;
2034 this->macinfo.size = bfd_section_size (sectp);
2035 }
2036 else if (names.macro.matches (sectp->name))
2037 {
2038 this->macro.s.section = sectp;
2039 this->macro.size = bfd_section_size (sectp);
2040 }
2041 else if (names.str.matches (sectp->name))
2042 {
2043 this->str.s.section = sectp;
2044 this->str.size = bfd_section_size (sectp);
2045 }
2046 else if (names.str_offsets.matches (sectp->name))
2047 {
2048 this->str_offsets.s.section = sectp;
2049 this->str_offsets.size = bfd_section_size (sectp);
2050 }
2051 else if (names.line_str.matches (sectp->name))
2052 {
2053 this->line_str.s.section = sectp;
2054 this->line_str.size = bfd_section_size (sectp);
2055 }
2056 else if (names.addr.matches (sectp->name))
2057 {
2058 this->addr.s.section = sectp;
2059 this->addr.size = bfd_section_size (sectp);
2060 }
2061 else if (names.frame.matches (sectp->name))
2062 {
2063 this->frame.s.section = sectp;
2064 this->frame.size = bfd_section_size (sectp);
2065 }
2066 else if (names.eh_frame.matches (sectp->name))
2067 {
2068 this->eh_frame.s.section = sectp;
2069 this->eh_frame.size = bfd_section_size (sectp);
2070 }
2071 else if (names.ranges.matches (sectp->name))
2072 {
2073 this->ranges.s.section = sectp;
2074 this->ranges.size = bfd_section_size (sectp);
2075 }
2076 else if (names.rnglists.matches (sectp->name))
2077 {
2078 this->rnglists.s.section = sectp;
2079 this->rnglists.size = bfd_section_size (sectp);
2080 }
2081 else if (names.types.matches (sectp->name))
2082 {
2083 struct dwarf2_section_info type_section;
2084
2085 memset (&type_section, 0, sizeof (type_section));
2086 type_section.s.section = sectp;
2087 type_section.size = bfd_section_size (sectp);
2088
2089 this->types.push_back (type_section);
2090 }
2091 else if (names.gdb_index.matches (sectp->name))
2092 {
2093 this->gdb_index.s.section = sectp;
2094 this->gdb_index.size = bfd_section_size (sectp);
2095 }
2096 else if (names.debug_names.matches (sectp->name))
2097 {
2098 this->debug_names.s.section = sectp;
2099 this->debug_names.size = bfd_section_size (sectp);
2100 }
2101 else if (names.debug_aranges.matches (sectp->name))
2102 {
2103 this->debug_aranges.s.section = sectp;
2104 this->debug_aranges.size = bfd_section_size (sectp);
2105 }
2106
2107 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2108 && bfd_section_vma (sectp) == 0)
2109 this->has_section_at_zero = true;
2110 }
2111
2112 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2113 SECTION_NAME. */
2114
2115 void
2116 dwarf2_get_section_info (struct objfile *objfile,
2117 enum dwarf2_section_enum sect,
2118 asection **sectp, const gdb_byte **bufp,
2119 bfd_size_type *sizep)
2120 {
2121 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2122 struct dwarf2_section_info *info;
2123
2124 /* We may see an objfile without any DWARF, in which case we just
2125 return nothing. */
2126 if (per_objfile == NULL)
2127 {
2128 *sectp = NULL;
2129 *bufp = NULL;
2130 *sizep = 0;
2131 return;
2132 }
2133 switch (sect)
2134 {
2135 case DWARF2_DEBUG_FRAME:
2136 info = &per_objfile->per_bfd->frame;
2137 break;
2138 case DWARF2_EH_FRAME:
2139 info = &per_objfile->per_bfd->eh_frame;
2140 break;
2141 default:
2142 gdb_assert_not_reached ("unexpected section");
2143 }
2144
2145 info->read (objfile);
2146
2147 *sectp = info->get_bfd_section ();
2148 *bufp = info->buffer;
2149 *sizep = info->size;
2150 }
2151
2152 \f
2153 /* DWARF quick_symbol_functions support. */
2154
2155 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2156 unique line tables, so we maintain a separate table of all .debug_line
2157 derived entries to support the sharing.
2158 All the quick functions need is the list of file names. We discard the
2159 line_header when we're done and don't need to record it here. */
2160 struct quick_file_names
2161 {
2162 /* The data used to construct the hash key. */
2163 struct stmt_list_hash hash;
2164
2165 /* The number of entries in file_names, real_names. */
2166 unsigned int num_file_names;
2167
2168 /* The file names from the line table, after being run through
2169 file_full_name. */
2170 const char **file_names;
2171
2172 /* The file names from the line table after being run through
2173 gdb_realpath. These are computed lazily. */
2174 const char **real_names;
2175 };
2176
2177 /* When using the index (and thus not using psymtabs), each CU has an
2178 object of this type. This is used to hold information needed by
2179 the various "quick" methods. */
2180 struct dwarf2_per_cu_quick_data
2181 {
2182 /* The file table. This can be NULL if there was no file table
2183 or it's currently not read in.
2184 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2185 struct quick_file_names *file_names;
2186
2187 /* A temporary mark bit used when iterating over all CUs in
2188 expand_symtabs_matching. */
2189 unsigned int mark : 1;
2190
2191 /* True if we've tried to read the file table and found there isn't one.
2192 There will be no point in trying to read it again next time. */
2193 unsigned int no_file_data : 1;
2194 };
2195
2196 struct dwarf2_base_index_functions : public quick_symbol_functions
2197 {
2198 bool has_symbols (struct objfile *objfile) override;
2199
2200 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
2201
2202 void forget_cached_source_info (struct objfile *objfile) override;
2203
2204 bool map_symtabs_matching_filename
2205 (struct objfile *objfile, const char *name, const char *real_path,
2206 gdb::function_view<bool (symtab *)> callback) override;
2207
2208 enum language lookup_global_symbol_language (struct objfile *objfile,
2209 const char *name,
2210 domain_enum domain,
2211 bool *symbol_found_p) override
2212 {
2213 *symbol_found_p = false;
2214 return language_unknown;
2215 }
2216
2217 void print_stats (struct objfile *objfile) override;
2218
2219 void expand_all_symtabs (struct objfile *objfile) override;
2220
2221 void expand_symtabs_with_fullname (struct objfile *objfile,
2222 const char *fullname) override;
2223
2224 struct compunit_symtab *find_pc_sect_compunit_symtab
2225 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
2226 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
2227
2228 struct compunit_symtab *find_compunit_symtab_by_address
2229 (struct objfile *objfile, CORE_ADDR address) override
2230 {
2231 return nullptr;
2232 }
2233
2234 void map_symbol_filenames (struct objfile *objfile,
2235 symbol_filename_ftype *fun, void *data,
2236 int need_fullname) override;
2237 };
2238
2239 struct dwarf2_gdb_index : public dwarf2_base_index_functions
2240 {
2241 struct compunit_symtab *lookup_symbol (struct objfile *objfile,
2242 block_enum block_index,
2243 const char *name,
2244 domain_enum domain) override;
2245
2246 void dump (struct objfile *objfile) override;
2247
2248 void expand_symtabs_for_function (struct objfile *objfile,
2249 const char *func_name) override;
2250
2251 void map_matching_symbols
2252 (struct objfile *,
2253 const lookup_name_info &lookup_name,
2254 domain_enum domain,
2255 int global,
2256 gdb::function_view<symbol_found_callback_ftype> callback,
2257 symbol_compare_ftype *ordered_compare) override;
2258
2259 void expand_symtabs_matching
2260 (struct objfile *objfile,
2261 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2262 const lookup_name_info *lookup_name,
2263 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2264 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2265 enum search_domain kind) override;
2266 };
2267
2268 struct dwarf2_debug_names_index : public dwarf2_base_index_functions
2269 {
2270 struct compunit_symtab *lookup_symbol (struct objfile *objfile,
2271 block_enum block_index,
2272 const char *name,
2273 domain_enum domain) override;
2274
2275 void dump (struct objfile *objfile) override;
2276
2277 void expand_symtabs_for_function (struct objfile *objfile,
2278 const char *func_name) override;
2279
2280 void map_matching_symbols
2281 (struct objfile *,
2282 const lookup_name_info &lookup_name,
2283 domain_enum domain,
2284 int global,
2285 gdb::function_view<symbol_found_callback_ftype> callback,
2286 symbol_compare_ftype *ordered_compare) override;
2287
2288 void expand_symtabs_matching
2289 (struct objfile *objfile,
2290 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2291 const lookup_name_info *lookup_name,
2292 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2293 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2294 enum search_domain kind) override;
2295 };
2296
2297 quick_symbol_functions_up
2298 make_dwarf_gdb_index ()
2299 {
2300 return quick_symbol_functions_up (new dwarf2_gdb_index);
2301 }
2302
2303 quick_symbol_functions_up
2304 make_dwarf_debug_names ()
2305 {
2306 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2307 }
2308
2309 /* Utility hash function for a stmt_list_hash. */
2310
2311 static hashval_t
2312 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2313 {
2314 hashval_t v = 0;
2315
2316 if (stmt_list_hash->dwo_unit != NULL)
2317 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2318 v += to_underlying (stmt_list_hash->line_sect_off);
2319 return v;
2320 }
2321
2322 /* Utility equality function for a stmt_list_hash. */
2323
2324 static int
2325 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2326 const struct stmt_list_hash *rhs)
2327 {
2328 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2329 return 0;
2330 if (lhs->dwo_unit != NULL
2331 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2332 return 0;
2333
2334 return lhs->line_sect_off == rhs->line_sect_off;
2335 }
2336
2337 /* Hash function for a quick_file_names. */
2338
2339 static hashval_t
2340 hash_file_name_entry (const void *e)
2341 {
2342 const struct quick_file_names *file_data
2343 = (const struct quick_file_names *) e;
2344
2345 return hash_stmt_list_entry (&file_data->hash);
2346 }
2347
2348 /* Equality function for a quick_file_names. */
2349
2350 static int
2351 eq_file_name_entry (const void *a, const void *b)
2352 {
2353 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2354 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2355
2356 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2357 }
2358
2359 /* Delete function for a quick_file_names. */
2360
2361 static void
2362 delete_file_name_entry (void *e)
2363 {
2364 struct quick_file_names *file_data = (struct quick_file_names *) e;
2365 int i;
2366
2367 for (i = 0; i < file_data->num_file_names; ++i)
2368 {
2369 xfree ((void*) file_data->file_names[i]);
2370 if (file_data->real_names)
2371 xfree ((void*) file_data->real_names[i]);
2372 }
2373
2374 /* The space for the struct itself lives on the obstack, so we don't
2375 free it here. */
2376 }
2377
2378 /* Create a quick_file_names hash table. */
2379
2380 static htab_up
2381 create_quick_file_names_table (unsigned int nr_initial_entries)
2382 {
2383 return htab_up (htab_create_alloc (nr_initial_entries,
2384 hash_file_name_entry, eq_file_name_entry,
2385 delete_file_name_entry, xcalloc, xfree));
2386 }
2387
2388 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2389 function is unrelated to symtabs, symtab would have to be created afterwards.
2390 You should call age_cached_comp_units after processing the CU. */
2391
2392 static dwarf2_cu *
2393 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2394 bool skip_partial)
2395 {
2396 if (per_cu->is_debug_types)
2397 load_full_type_unit (per_cu, per_objfile);
2398 else
2399 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2400 skip_partial, language_minimal);
2401
2402 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2403 if (cu == nullptr)
2404 return nullptr; /* Dummy CU. */
2405
2406 dwarf2_find_base_address (cu->dies, cu);
2407
2408 return cu;
2409 }
2410
2411 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2412
2413 static void
2414 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2415 dwarf2_per_objfile *per_objfile, bool skip_partial)
2416 {
2417 /* Skip type_unit_groups, reading the type units they contain
2418 is handled elsewhere. */
2419 if (per_cu->type_unit_group_p ())
2420 return;
2421
2422 {
2423 /* The destructor of dwarf2_queue_guard frees any entries left on
2424 the queue. After this point we're guaranteed to leave this function
2425 with the dwarf queue empty. */
2426 dwarf2_queue_guard q_guard (per_objfile);
2427
2428 if (!per_objfile->symtab_set_p (per_cu))
2429 {
2430 queue_comp_unit (per_cu, per_objfile, language_minimal);
2431 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2432
2433 /* If we just loaded a CU from a DWO, and we're working with an index
2434 that may badly handle TUs, load all the TUs in that DWO as well.
2435 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2436 if (!per_cu->is_debug_types
2437 && cu != NULL
2438 && cu->dwo_unit != NULL
2439 && per_objfile->per_bfd->index_table != NULL
2440 && per_objfile->per_bfd->index_table->version <= 7
2441 /* DWP files aren't supported yet. */
2442 && get_dwp_file (per_objfile) == NULL)
2443 queue_and_load_all_dwo_tus (cu);
2444 }
2445
2446 process_queue (per_objfile);
2447 }
2448
2449 /* Age the cache, releasing compilation units that have not
2450 been used recently. */
2451 per_objfile->age_comp_units ();
2452 }
2453
2454 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2455 the per-objfile for which this symtab is instantiated.
2456
2457 Returns the resulting symbol table. */
2458
2459 static struct compunit_symtab *
2460 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2461 dwarf2_per_objfile *per_objfile,
2462 bool skip_partial)
2463 {
2464 gdb_assert (per_objfile->per_bfd->using_index);
2465
2466 if (!per_objfile->symtab_set_p (per_cu))
2467 {
2468 free_cached_comp_units freer (per_objfile);
2469 scoped_restore decrementer = increment_reading_symtab ();
2470 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2471 process_cu_includes (per_objfile);
2472 }
2473
2474 return per_objfile->get_symtab (per_cu);
2475 }
2476
2477 /* See declaration. */
2478
2479 dwarf2_per_cu_data *
2480 dwarf2_per_bfd::get_cutu (int index)
2481 {
2482 if (index >= this->all_comp_units.size ())
2483 {
2484 index -= this->all_comp_units.size ();
2485 gdb_assert (index < this->all_type_units.size ());
2486 return &this->all_type_units[index]->per_cu;
2487 }
2488
2489 return this->all_comp_units[index];
2490 }
2491
2492 /* See declaration. */
2493
2494 dwarf2_per_cu_data *
2495 dwarf2_per_bfd::get_cu (int index)
2496 {
2497 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2498
2499 return this->all_comp_units[index];
2500 }
2501
2502 /* See declaration. */
2503
2504 signatured_type *
2505 dwarf2_per_bfd::get_tu (int index)
2506 {
2507 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2508
2509 return this->all_type_units[index];
2510 }
2511
2512 /* See read.h. */
2513
2514 dwarf2_per_cu_data *
2515 dwarf2_per_bfd::allocate_per_cu ()
2516 {
2517 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2518 result->per_bfd = this;
2519 result->index = m_num_psymtabs++;
2520 return result;
2521 }
2522
2523 /* See read.h. */
2524
2525 signatured_type *
2526 dwarf2_per_bfd::allocate_signatured_type ()
2527 {
2528 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2529 result->per_cu.per_bfd = this;
2530 result->per_cu.index = m_num_psymtabs++;
2531 return result;
2532 }
2533
2534 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2535 obstack, and constructed with the specified field values. */
2536
2537 static dwarf2_per_cu_data *
2538 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2539 struct dwarf2_section_info *section,
2540 int is_dwz,
2541 sect_offset sect_off, ULONGEST length)
2542 {
2543 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
2544 the_cu->sect_off = sect_off;
2545 the_cu->length = length;
2546 the_cu->section = section;
2547 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2548 struct dwarf2_per_cu_quick_data);
2549 the_cu->is_dwz = is_dwz;
2550 return the_cu;
2551 }
2552
2553 /* A helper for create_cus_from_index that handles a given list of
2554 CUs. */
2555
2556 static void
2557 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2558 const gdb_byte *cu_list, offset_type n_elements,
2559 struct dwarf2_section_info *section,
2560 int is_dwz)
2561 {
2562 for (offset_type i = 0; i < n_elements; i += 2)
2563 {
2564 gdb_static_assert (sizeof (ULONGEST) >= 8);
2565
2566 sect_offset sect_off
2567 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2568 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2569 cu_list += 2 * 8;
2570
2571 dwarf2_per_cu_data *per_cu
2572 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2573 length);
2574 per_bfd->all_comp_units.push_back (per_cu);
2575 }
2576 }
2577
2578 /* Read the CU list from the mapped index, and use it to create all
2579 the CU objects for PER_BFD. */
2580
2581 static void
2582 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2583 const gdb_byte *cu_list, offset_type cu_list_elements,
2584 const gdb_byte *dwz_list, offset_type dwz_elements)
2585 {
2586 gdb_assert (per_bfd->all_comp_units.empty ());
2587 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2588
2589 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2590 &per_bfd->info, 0);
2591
2592 if (dwz_elements == 0)
2593 return;
2594
2595 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2596 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2597 &dwz->info, 1);
2598 }
2599
2600 /* Create the signatured type hash table from the index. */
2601
2602 static void
2603 create_signatured_type_table_from_index
2604 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2605 const gdb_byte *bytes, offset_type elements)
2606 {
2607 gdb_assert (per_bfd->all_type_units.empty ());
2608 per_bfd->all_type_units.reserve (elements / 3);
2609
2610 htab_up sig_types_hash = allocate_signatured_type_table ();
2611
2612 for (offset_type i = 0; i < elements; i += 3)
2613 {
2614 struct signatured_type *sig_type;
2615 ULONGEST signature;
2616 void **slot;
2617 cu_offset type_offset_in_tu;
2618
2619 gdb_static_assert (sizeof (ULONGEST) >= 8);
2620 sect_offset sect_off
2621 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2622 type_offset_in_tu
2623 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2624 BFD_ENDIAN_LITTLE);
2625 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2626 bytes += 3 * 8;
2627
2628 sig_type = per_bfd->allocate_signatured_type ();
2629 sig_type->signature = signature;
2630 sig_type->type_offset_in_tu = type_offset_in_tu;
2631 sig_type->per_cu.is_debug_types = 1;
2632 sig_type->per_cu.section = section;
2633 sig_type->per_cu.sect_off = sect_off;
2634 sig_type->per_cu.v.quick
2635 = OBSTACK_ZALLOC (&per_bfd->obstack,
2636 struct dwarf2_per_cu_quick_data);
2637
2638 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2639 *slot = sig_type;
2640
2641 per_bfd->all_type_units.push_back (sig_type);
2642 }
2643
2644 per_bfd->signatured_types = std::move (sig_types_hash);
2645 }
2646
2647 /* Create the signatured type hash table from .debug_names. */
2648
2649 static void
2650 create_signatured_type_table_from_debug_names
2651 (dwarf2_per_objfile *per_objfile,
2652 const mapped_debug_names &map,
2653 struct dwarf2_section_info *section,
2654 struct dwarf2_section_info *abbrev_section)
2655 {
2656 struct objfile *objfile = per_objfile->objfile;
2657
2658 section->read (objfile);
2659 abbrev_section->read (objfile);
2660
2661 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2662 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2663
2664 htab_up sig_types_hash = allocate_signatured_type_table ();
2665
2666 for (uint32_t i = 0; i < map.tu_count; ++i)
2667 {
2668 struct signatured_type *sig_type;
2669 void **slot;
2670
2671 sect_offset sect_off
2672 = (sect_offset) (extract_unsigned_integer
2673 (map.tu_table_reordered + i * map.offset_size,
2674 map.offset_size,
2675 map.dwarf5_byte_order));
2676
2677 comp_unit_head cu_header;
2678 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2679 abbrev_section,
2680 section->buffer + to_underlying (sect_off),
2681 rcuh_kind::TYPE);
2682
2683 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
2684 sig_type->signature = cu_header.signature;
2685 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2686 sig_type->per_cu.is_debug_types = 1;
2687 sig_type->per_cu.section = section;
2688 sig_type->per_cu.sect_off = sect_off;
2689 sig_type->per_cu.v.quick
2690 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
2691 struct dwarf2_per_cu_quick_data);
2692
2693 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2694 *slot = sig_type;
2695
2696 per_objfile->per_bfd->all_type_units.push_back (sig_type);
2697 }
2698
2699 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2700 }
2701
2702 /* Read the address map data from the mapped index, and use it to
2703 populate the objfile's psymtabs_addrmap. */
2704
2705 static void
2706 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2707 struct mapped_index *index)
2708 {
2709 struct objfile *objfile = per_objfile->objfile;
2710 struct gdbarch *gdbarch = objfile->arch ();
2711 const gdb_byte *iter, *end;
2712 struct addrmap *mutable_map;
2713 CORE_ADDR baseaddr;
2714
2715 auto_obstack temp_obstack;
2716
2717 mutable_map = addrmap_create_mutable (&temp_obstack);
2718
2719 iter = index->address_table.data ();
2720 end = iter + index->address_table.size ();
2721
2722 baseaddr = objfile->text_section_offset ();
2723
2724 while (iter < end)
2725 {
2726 ULONGEST hi, lo, cu_index;
2727 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2728 iter += 8;
2729 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2730 iter += 8;
2731 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2732 iter += 4;
2733
2734 if (lo > hi)
2735 {
2736 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2737 hex_string (lo), hex_string (hi));
2738 continue;
2739 }
2740
2741 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
2742 {
2743 complaint (_(".gdb_index address table has invalid CU number %u"),
2744 (unsigned) cu_index);
2745 continue;
2746 }
2747
2748 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2749 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2750 addrmap_set_empty (mutable_map, lo, hi - 1,
2751 per_objfile->per_bfd->get_cu (cu_index));
2752 }
2753
2754 objfile->partial_symtabs->psymtabs_addrmap
2755 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2756 }
2757
2758 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2759 populate the objfile's psymtabs_addrmap. */
2760
2761 static void
2762 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2763 struct dwarf2_section_info *section)
2764 {
2765 struct objfile *objfile = per_objfile->objfile;
2766 bfd *abfd = objfile->obfd;
2767 struct gdbarch *gdbarch = objfile->arch ();
2768 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2769
2770 auto_obstack temp_obstack;
2771 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2772
2773 std::unordered_map<sect_offset,
2774 dwarf2_per_cu_data *,
2775 gdb::hash_enum<sect_offset>>
2776 debug_info_offset_to_per_cu;
2777 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
2778 {
2779 const auto insertpair
2780 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2781 if (!insertpair.second)
2782 {
2783 warning (_("Section .debug_aranges in %s has duplicate "
2784 "debug_info_offset %s, ignoring .debug_aranges."),
2785 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2786 return;
2787 }
2788 }
2789
2790 section->read (objfile);
2791
2792 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2793
2794 const gdb_byte *addr = section->buffer;
2795
2796 while (addr < section->buffer + section->size)
2797 {
2798 const gdb_byte *const entry_addr = addr;
2799 unsigned int bytes_read;
2800
2801 const LONGEST entry_length = read_initial_length (abfd, addr,
2802 &bytes_read);
2803 addr += bytes_read;
2804
2805 const gdb_byte *const entry_end = addr + entry_length;
2806 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2807 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2808 if (addr + entry_length > section->buffer + section->size)
2809 {
2810 warning (_("Section .debug_aranges in %s entry at offset %s "
2811 "length %s exceeds section length %s, "
2812 "ignoring .debug_aranges."),
2813 objfile_name (objfile),
2814 plongest (entry_addr - section->buffer),
2815 plongest (bytes_read + entry_length),
2816 pulongest (section->size));
2817 return;
2818 }
2819
2820 /* The version number. */
2821 const uint16_t version = read_2_bytes (abfd, addr);
2822 addr += 2;
2823 if (version != 2)
2824 {
2825 warning (_("Section .debug_aranges in %s entry at offset %s "
2826 "has unsupported version %d, ignoring .debug_aranges."),
2827 objfile_name (objfile),
2828 plongest (entry_addr - section->buffer), version);
2829 return;
2830 }
2831
2832 const uint64_t debug_info_offset
2833 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2834 addr += offset_size;
2835 const auto per_cu_it
2836 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2837 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2838 {
2839 warning (_("Section .debug_aranges in %s entry at offset %s "
2840 "debug_info_offset %s does not exists, "
2841 "ignoring .debug_aranges."),
2842 objfile_name (objfile),
2843 plongest (entry_addr - section->buffer),
2844 pulongest (debug_info_offset));
2845 return;
2846 }
2847 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2848
2849 const uint8_t address_size = *addr++;
2850 if (address_size < 1 || address_size > 8)
2851 {
2852 warning (_("Section .debug_aranges in %s entry at offset %s "
2853 "address_size %u is invalid, ignoring .debug_aranges."),
2854 objfile_name (objfile),
2855 plongest (entry_addr - section->buffer), address_size);
2856 return;
2857 }
2858
2859 const uint8_t segment_selector_size = *addr++;
2860 if (segment_selector_size != 0)
2861 {
2862 warning (_("Section .debug_aranges in %s entry at offset %s "
2863 "segment_selector_size %u is not supported, "
2864 "ignoring .debug_aranges."),
2865 objfile_name (objfile),
2866 plongest (entry_addr - section->buffer),
2867 segment_selector_size);
2868 return;
2869 }
2870
2871 /* Must pad to an alignment boundary that is twice the address
2872 size. It is undocumented by the DWARF standard but GCC does
2873 use it. */
2874 for (size_t padding = ((-(addr - section->buffer))
2875 & (2 * address_size - 1));
2876 padding > 0; padding--)
2877 if (*addr++ != 0)
2878 {
2879 warning (_("Section .debug_aranges in %s entry at offset %s "
2880 "padding is not zero, ignoring .debug_aranges."),
2881 objfile_name (objfile),
2882 plongest (entry_addr - section->buffer));
2883 return;
2884 }
2885
2886 for (;;)
2887 {
2888 if (addr + 2 * address_size > entry_end)
2889 {
2890 warning (_("Section .debug_aranges in %s entry at offset %s "
2891 "address list is not properly terminated, "
2892 "ignoring .debug_aranges."),
2893 objfile_name (objfile),
2894 plongest (entry_addr - section->buffer));
2895 return;
2896 }
2897 ULONGEST start = extract_unsigned_integer (addr, address_size,
2898 dwarf5_byte_order);
2899 addr += address_size;
2900 ULONGEST length = extract_unsigned_integer (addr, address_size,
2901 dwarf5_byte_order);
2902 addr += address_size;
2903 if (start == 0 && length == 0)
2904 break;
2905 if (start == 0 && !per_objfile->per_bfd->has_section_at_zero)
2906 {
2907 /* Symbol was eliminated due to a COMDAT group. */
2908 continue;
2909 }
2910 ULONGEST end = start + length;
2911 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2912 - baseaddr);
2913 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2914 - baseaddr);
2915 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2916 }
2917 }
2918
2919 objfile->partial_symtabs->psymtabs_addrmap
2920 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2921 }
2922
2923 /* Find a slot in the mapped index INDEX for the object named NAME.
2924 If NAME is found, set *VEC_OUT to point to the CU vector in the
2925 constant pool and return true. If NAME cannot be found, return
2926 false. */
2927
2928 static bool
2929 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2930 offset_type **vec_out)
2931 {
2932 offset_type hash;
2933 offset_type slot, step;
2934 int (*cmp) (const char *, const char *);
2935
2936 gdb::unique_xmalloc_ptr<char> without_params;
2937 if (current_language->la_language == language_cplus
2938 || current_language->la_language == language_fortran
2939 || current_language->la_language == language_d)
2940 {
2941 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2942 not contain any. */
2943
2944 if (strchr (name, '(') != NULL)
2945 {
2946 without_params = cp_remove_params (name);
2947
2948 if (without_params != NULL)
2949 name = without_params.get ();
2950 }
2951 }
2952
2953 /* Index version 4 did not support case insensitive searches. But the
2954 indices for case insensitive languages are built in lowercase, therefore
2955 simulate our NAME being searched is also lowercased. */
2956 hash = mapped_index_string_hash ((index->version == 4
2957 && case_sensitivity == case_sensitive_off
2958 ? 5 : index->version),
2959 name);
2960
2961 slot = hash & (index->symbol_table.size () - 1);
2962 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2963 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2964
2965 for (;;)
2966 {
2967 const char *str;
2968
2969 const auto &bucket = index->symbol_table[slot];
2970 if (bucket.name == 0 && bucket.vec == 0)
2971 return false;
2972
2973 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2974 if (!cmp (name, str))
2975 {
2976 *vec_out = (offset_type *) (index->constant_pool
2977 + MAYBE_SWAP (bucket.vec));
2978 return true;
2979 }
2980
2981 slot = (slot + step) & (index->symbol_table.size () - 1);
2982 }
2983 }
2984
2985 /* A helper function that reads the .gdb_index from BUFFER and fills
2986 in MAP. FILENAME is the name of the file containing the data;
2987 it is used for error reporting. DEPRECATED_OK is true if it is
2988 ok to use deprecated sections.
2989
2990 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2991 out parameters that are filled in with information about the CU and
2992 TU lists in the section.
2993
2994 Returns true if all went well, false otherwise. */
2995
2996 static bool
2997 read_gdb_index_from_buffer (const char *filename,
2998 bool deprecated_ok,
2999 gdb::array_view<const gdb_byte> buffer,
3000 struct mapped_index *map,
3001 const gdb_byte **cu_list,
3002 offset_type *cu_list_elements,
3003 const gdb_byte **types_list,
3004 offset_type *types_list_elements)
3005 {
3006 const gdb_byte *addr = &buffer[0];
3007
3008 /* Version check. */
3009 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3010 /* Versions earlier than 3 emitted every copy of a psymbol. This
3011 causes the index to behave very poorly for certain requests. Version 3
3012 contained incomplete addrmap. So, it seems better to just ignore such
3013 indices. */
3014 if (version < 4)
3015 {
3016 static int warning_printed = 0;
3017 if (!warning_printed)
3018 {
3019 warning (_("Skipping obsolete .gdb_index section in %s."),
3020 filename);
3021 warning_printed = 1;
3022 }
3023 return 0;
3024 }
3025 /* Index version 4 uses a different hash function than index version
3026 5 and later.
3027
3028 Versions earlier than 6 did not emit psymbols for inlined
3029 functions. Using these files will cause GDB not to be able to
3030 set breakpoints on inlined functions by name, so we ignore these
3031 indices unless the user has done
3032 "set use-deprecated-index-sections on". */
3033 if (version < 6 && !deprecated_ok)
3034 {
3035 static int warning_printed = 0;
3036 if (!warning_printed)
3037 {
3038 warning (_("\
3039 Skipping deprecated .gdb_index section in %s.\n\
3040 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3041 to use the section anyway."),
3042 filename);
3043 warning_printed = 1;
3044 }
3045 return 0;
3046 }
3047 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3048 of the TU (for symbols coming from TUs),
3049 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3050 Plus gold-generated indices can have duplicate entries for global symbols,
3051 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3052 These are just performance bugs, and we can't distinguish gdb-generated
3053 indices from gold-generated ones, so issue no warning here. */
3054
3055 /* Indexes with higher version than the one supported by GDB may be no
3056 longer backward compatible. */
3057 if (version > 8)
3058 return 0;
3059
3060 map->version = version;
3061
3062 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3063
3064 int i = 0;
3065 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3066 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3067 / 8);
3068 ++i;
3069
3070 *types_list = addr + MAYBE_SWAP (metadata[i]);
3071 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3072 - MAYBE_SWAP (metadata[i]))
3073 / 8);
3074 ++i;
3075
3076 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3077 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3078 map->address_table
3079 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3080 ++i;
3081
3082 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3083 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3084 map->symbol_table
3085 = gdb::array_view<mapped_index::symbol_table_slot>
3086 ((mapped_index::symbol_table_slot *) symbol_table,
3087 (mapped_index::symbol_table_slot *) symbol_table_end);
3088
3089 ++i;
3090 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3091
3092 return 1;
3093 }
3094
3095 /* Callback types for dwarf2_read_gdb_index. */
3096
3097 typedef gdb::function_view
3098 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3099 get_gdb_index_contents_ftype;
3100 typedef gdb::function_view
3101 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3102 get_gdb_index_contents_dwz_ftype;
3103
3104 /* Read .gdb_index. If everything went ok, initialize the "quick"
3105 elements of all the CUs and return 1. Otherwise, return 0. */
3106
3107 static int
3108 dwarf2_read_gdb_index
3109 (dwarf2_per_objfile *per_objfile,
3110 get_gdb_index_contents_ftype get_gdb_index_contents,
3111 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3112 {
3113 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3114 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3115 struct dwz_file *dwz;
3116 struct objfile *objfile = per_objfile->objfile;
3117 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
3118
3119 gdb::array_view<const gdb_byte> main_index_contents
3120 = get_gdb_index_contents (objfile, per_bfd);
3121
3122 if (main_index_contents.empty ())
3123 return 0;
3124
3125 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3126 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3127 use_deprecated_index_sections,
3128 main_index_contents, map.get (), &cu_list,
3129 &cu_list_elements, &types_list,
3130 &types_list_elements))
3131 return 0;
3132
3133 /* Don't use the index if it's empty. */
3134 if (map->symbol_table.empty ())
3135 return 0;
3136
3137 /* If there is a .dwz file, read it so we can get its CU list as
3138 well. */
3139 dwz = dwarf2_get_dwz_file (per_bfd);
3140 if (dwz != NULL)
3141 {
3142 struct mapped_index dwz_map;
3143 const gdb_byte *dwz_types_ignore;
3144 offset_type dwz_types_elements_ignore;
3145
3146 gdb::array_view<const gdb_byte> dwz_index_content
3147 = get_gdb_index_contents_dwz (objfile, dwz);
3148
3149 if (dwz_index_content.empty ())
3150 return 0;
3151
3152 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3153 1, dwz_index_content, &dwz_map,
3154 &dwz_list, &dwz_list_elements,
3155 &dwz_types_ignore,
3156 &dwz_types_elements_ignore))
3157 {
3158 warning (_("could not read '.gdb_index' section from %s; skipping"),
3159 bfd_get_filename (dwz->dwz_bfd.get ()));
3160 return 0;
3161 }
3162 }
3163
3164 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3165 dwz_list_elements);
3166
3167 if (types_list_elements)
3168 {
3169 /* We can only handle a single .debug_types when we have an
3170 index. */
3171 if (per_bfd->types.size () != 1)
3172 return 0;
3173
3174 dwarf2_section_info *section = &per_bfd->types[0];
3175
3176 create_signatured_type_table_from_index (per_bfd, section, types_list,
3177 types_list_elements);
3178 }
3179
3180 create_addrmap_from_index (per_objfile, map.get ());
3181
3182 per_bfd->index_table = std::move (map);
3183 per_bfd->using_index = 1;
3184 per_bfd->quick_file_names_table =
3185 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3186
3187 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
3188 objfiles using the same BFD. */
3189 gdb_assert (per_bfd->partial_symtabs == nullptr);
3190 per_bfd->partial_symtabs = objfile->partial_symtabs;
3191
3192 return 1;
3193 }
3194
3195 /* die_reader_func for dw2_get_file_names. */
3196
3197 static void
3198 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3199 struct die_info *comp_unit_die)
3200 {
3201 struct dwarf2_cu *cu = reader->cu;
3202 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3203 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3204 struct dwarf2_per_cu_data *lh_cu;
3205 struct attribute *attr;
3206 void **slot;
3207 struct quick_file_names *qfn;
3208
3209 gdb_assert (! this_cu->is_debug_types);
3210
3211 /* Our callers never want to match partial units -- instead they
3212 will match the enclosing full CU. */
3213 if (comp_unit_die->tag == DW_TAG_partial_unit)
3214 {
3215 this_cu->v.quick->no_file_data = 1;
3216 return;
3217 }
3218
3219 lh_cu = this_cu;
3220 slot = NULL;
3221
3222 line_header_up lh;
3223 sect_offset line_offset {};
3224
3225 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3226 if (attr != nullptr && attr->form_is_unsigned ())
3227 {
3228 struct quick_file_names find_entry;
3229
3230 line_offset = (sect_offset) attr->as_unsigned ();
3231
3232 /* We may have already read in this line header (TU line header sharing).
3233 If we have we're done. */
3234 find_entry.hash.dwo_unit = cu->dwo_unit;
3235 find_entry.hash.line_sect_off = line_offset;
3236 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
3237 &find_entry, INSERT);
3238 if (*slot != NULL)
3239 {
3240 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3241 return;
3242 }
3243
3244 lh = dwarf_decode_line_header (line_offset, cu);
3245 }
3246 if (lh == NULL)
3247 {
3248 lh_cu->v.quick->no_file_data = 1;
3249 return;
3250 }
3251
3252 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
3253 qfn->hash.dwo_unit = cu->dwo_unit;
3254 qfn->hash.line_sect_off = line_offset;
3255 gdb_assert (slot != NULL);
3256 *slot = qfn;
3257
3258 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3259
3260 int offset = 0;
3261 if (strcmp (fnd.name, "<unknown>") != 0)
3262 ++offset;
3263
3264 qfn->num_file_names = offset + lh->file_names_size ();
3265 qfn->file_names =
3266 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
3267 qfn->num_file_names);
3268 if (offset != 0)
3269 qfn->file_names[0] = xstrdup (fnd.name);
3270 for (int i = 0; i < lh->file_names_size (); ++i)
3271 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3272 fnd.comp_dir).release ();
3273 qfn->real_names = NULL;
3274
3275 lh_cu->v.quick->file_names = qfn;
3276 }
3277
3278 /* A helper for the "quick" functions which attempts to read the line
3279 table for THIS_CU. */
3280
3281 static struct quick_file_names *
3282 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3283 dwarf2_per_objfile *per_objfile)
3284 {
3285 /* This should never be called for TUs. */
3286 gdb_assert (! this_cu->is_debug_types);
3287 /* Nor type unit groups. */
3288 gdb_assert (! this_cu->type_unit_group_p ());
3289
3290 if (this_cu->v.quick->file_names != NULL)
3291 return this_cu->v.quick->file_names;
3292 /* If we know there is no line data, no point in looking again. */
3293 if (this_cu->v.quick->no_file_data)
3294 return NULL;
3295
3296 cutu_reader reader (this_cu, per_objfile);
3297 if (!reader.dummy_p)
3298 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
3299
3300 if (this_cu->v.quick->no_file_data)
3301 return NULL;
3302 return this_cu->v.quick->file_names;
3303 }
3304
3305 /* A helper for the "quick" functions which computes and caches the
3306 real path for a given file name from the line table. */
3307
3308 static const char *
3309 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3310 struct quick_file_names *qfn, int index)
3311 {
3312 if (qfn->real_names == NULL)
3313 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3314 qfn->num_file_names, const char *);
3315
3316 if (qfn->real_names[index] == NULL)
3317 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3318
3319 return qfn->real_names[index];
3320 }
3321
3322 struct symtab *
3323 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
3324 {
3325 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3326 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3327 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3328
3329 if (cust == NULL)
3330 return NULL;
3331
3332 return compunit_primary_filetab (cust);
3333 }
3334
3335 /* Traversal function for dw2_forget_cached_source_info. */
3336
3337 static int
3338 dw2_free_cached_file_names (void **slot, void *info)
3339 {
3340 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3341
3342 if (file_data->real_names)
3343 {
3344 int i;
3345
3346 for (i = 0; i < file_data->num_file_names; ++i)
3347 {
3348 xfree ((void*) file_data->real_names[i]);
3349 file_data->real_names[i] = NULL;
3350 }
3351 }
3352
3353 return 1;
3354 }
3355
3356 void
3357 dwarf2_base_index_functions::forget_cached_source_info
3358 (struct objfile *objfile)
3359 {
3360 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3361
3362 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
3363 dw2_free_cached_file_names, NULL);
3364 }
3365
3366 /* Helper function for dw2_map_symtabs_matching_filename that expands
3367 the symtabs and calls the iterator. */
3368
3369 static int
3370 dw2_map_expand_apply (struct objfile *objfile,
3371 struct dwarf2_per_cu_data *per_cu,
3372 const char *name, const char *real_path,
3373 gdb::function_view<bool (symtab *)> callback)
3374 {
3375 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3376
3377 /* Don't visit already-expanded CUs. */
3378 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3379 if (per_objfile->symtab_set_p (per_cu))
3380 return 0;
3381
3382 /* This may expand more than one symtab, and we want to iterate over
3383 all of them. */
3384 dw2_instantiate_symtab (per_cu, per_objfile, false);
3385
3386 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3387 last_made, callback);
3388 }
3389
3390 /* Implementation of the map_symtabs_matching_filename method. */
3391
3392 bool
3393 dwarf2_base_index_functions::map_symtabs_matching_filename
3394 (struct objfile *objfile, const char *name, const char *real_path,
3395 gdb::function_view<bool (symtab *)> callback)
3396 {
3397 const char *name_basename = lbasename (name);
3398 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3399
3400 /* The rule is CUs specify all the files, including those used by
3401 any TU, so there's no need to scan TUs here. */
3402
3403 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3404 {
3405 /* We only need to look at symtabs not already expanded. */
3406 if (per_objfile->symtab_set_p (per_cu))
3407 continue;
3408
3409 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3410 if (file_data == NULL)
3411 continue;
3412
3413 for (int j = 0; j < file_data->num_file_names; ++j)
3414 {
3415 const char *this_name = file_data->file_names[j];
3416 const char *this_real_name;
3417
3418 if (compare_filenames_for_search (this_name, name))
3419 {
3420 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3421 callback))
3422 return true;
3423 continue;
3424 }
3425
3426 /* Before we invoke realpath, which can get expensive when many
3427 files are involved, do a quick comparison of the basenames. */
3428 if (! basenames_may_differ
3429 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3430 continue;
3431
3432 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
3433 if (compare_filenames_for_search (this_real_name, name))
3434 {
3435 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3436 callback))
3437 return true;
3438 continue;
3439 }
3440
3441 if (real_path != NULL)
3442 {
3443 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3444 gdb_assert (IS_ABSOLUTE_PATH (name));
3445 if (this_real_name != NULL
3446 && FILENAME_CMP (real_path, this_real_name) == 0)
3447 {
3448 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3449 callback))
3450 return true;
3451 continue;
3452 }
3453 }
3454 }
3455 }
3456
3457 return false;
3458 }
3459
3460 /* Struct used to manage iterating over all CUs looking for a symbol. */
3461
3462 struct dw2_symtab_iterator
3463 {
3464 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3465 dwarf2_per_objfile *per_objfile;
3466 /* If set, only look for symbols that match that block. Valid values are
3467 GLOBAL_BLOCK and STATIC_BLOCK. */
3468 gdb::optional<block_enum> block_index;
3469 /* The kind of symbol we're looking for. */
3470 domain_enum domain;
3471 /* The list of CUs from the index entry of the symbol,
3472 or NULL if not found. */
3473 offset_type *vec;
3474 /* The next element in VEC to look at. */
3475 int next;
3476 /* The number of elements in VEC, or zero if there is no match. */
3477 int length;
3478 /* Have we seen a global version of the symbol?
3479 If so we can ignore all further global instances.
3480 This is to work around gold/15646, inefficient gold-generated
3481 indices. */
3482 int global_seen;
3483 };
3484
3485 /* Initialize the index symtab iterator ITER, common part. */
3486
3487 static void
3488 dw2_symtab_iter_init_common (struct dw2_symtab_iterator *iter,
3489 dwarf2_per_objfile *per_objfile,
3490 gdb::optional<block_enum> block_index,
3491 domain_enum domain)
3492 {
3493 iter->per_objfile = per_objfile;
3494 iter->block_index = block_index;
3495 iter->domain = domain;
3496 iter->next = 0;
3497 iter->global_seen = 0;
3498 iter->vec = NULL;
3499 iter->length = 0;
3500 }
3501
3502 /* Initialize the index symtab iterator ITER, const char *NAME variant. */
3503
3504 static void
3505 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3506 dwarf2_per_objfile *per_objfile,
3507 gdb::optional<block_enum> block_index,
3508 domain_enum domain,
3509 const char *name)
3510 {
3511 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3512
3513 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3514 /* index is NULL if OBJF_READNOW. */
3515 if (index == NULL)
3516 return;
3517
3518 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3519 iter->length = MAYBE_SWAP (*iter->vec);
3520 }
3521
3522 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3523
3524 static void
3525 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3526 dwarf2_per_objfile *per_objfile,
3527 gdb::optional<block_enum> block_index,
3528 domain_enum domain, offset_type namei)
3529 {
3530 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3531
3532 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3533 /* index is NULL if OBJF_READNOW. */
3534 if (index == NULL)
3535 return;
3536
3537 gdb_assert (!index->symbol_name_slot_invalid (namei));
3538 const auto &bucket = index->symbol_table[namei];
3539
3540 iter->vec = (offset_type *) (index->constant_pool
3541 + MAYBE_SWAP (bucket.vec));
3542 iter->length = MAYBE_SWAP (*iter->vec);
3543 }
3544
3545 /* Return the next matching CU or NULL if there are no more. */
3546
3547 static struct dwarf2_per_cu_data *
3548 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3549 {
3550 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3551
3552 for ( ; iter->next < iter->length; ++iter->next)
3553 {
3554 offset_type cu_index_and_attrs =
3555 MAYBE_SWAP (iter->vec[iter->next + 1]);
3556 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3557 gdb_index_symbol_kind symbol_kind =
3558 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3559 /* Only check the symbol attributes if they're present.
3560 Indices prior to version 7 don't record them,
3561 and indices >= 7 may elide them for certain symbols
3562 (gold does this). */
3563 int attrs_valid =
3564 (per_objfile->per_bfd->index_table->version >= 7
3565 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3566
3567 /* Don't crash on bad data. */
3568 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3569 + per_objfile->per_bfd->all_type_units.size ()))
3570 {
3571 complaint (_(".gdb_index entry has bad CU index"
3572 " [in module %s]"), objfile_name (per_objfile->objfile));
3573 continue;
3574 }
3575
3576 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3577
3578 /* Skip if already read in. */
3579 if (per_objfile->symtab_set_p (per_cu))
3580 continue;
3581
3582 /* Check static vs global. */
3583 if (attrs_valid)
3584 {
3585 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3586
3587 if (iter->block_index.has_value ())
3588 {
3589 bool want_static = *iter->block_index == STATIC_BLOCK;
3590
3591 if (is_static != want_static)
3592 continue;
3593 }
3594
3595 /* Work around gold/15646. */
3596 if (!is_static
3597 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3598 {
3599 if (iter->global_seen)
3600 continue;
3601
3602 iter->global_seen = 1;
3603 }
3604 }
3605
3606 /* Only check the symbol's kind if it has one. */
3607 if (attrs_valid)
3608 {
3609 switch (iter->domain)
3610 {
3611 case VAR_DOMAIN:
3612 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3613 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3614 /* Some types are also in VAR_DOMAIN. */
3615 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3616 continue;
3617 break;
3618 case STRUCT_DOMAIN:
3619 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3620 continue;
3621 break;
3622 case LABEL_DOMAIN:
3623 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3624 continue;
3625 break;
3626 case MODULE_DOMAIN:
3627 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3628 continue;
3629 break;
3630 default:
3631 break;
3632 }
3633 }
3634
3635 ++iter->next;
3636 return per_cu;
3637 }
3638
3639 return NULL;
3640 }
3641
3642 struct compunit_symtab *
3643 dwarf2_gdb_index::lookup_symbol (struct objfile *objfile,
3644 block_enum block_index,
3645 const char *name, domain_enum domain)
3646 {
3647 struct compunit_symtab *stab_best = NULL;
3648 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3649
3650 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3651
3652 struct dw2_symtab_iterator iter;
3653 struct dwarf2_per_cu_data *per_cu;
3654
3655 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
3656
3657 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3658 {
3659 struct symbol *sym, *with_opaque = NULL;
3660 struct compunit_symtab *stab
3661 = dw2_instantiate_symtab (per_cu, per_objfile, false);
3662 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3663 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3664
3665 sym = block_find_symbol (block, name, domain,
3666 block_find_non_opaque_type_preferred,
3667 &with_opaque);
3668
3669 /* Some caution must be observed with overloaded functions
3670 and methods, since the index will not contain any overload
3671 information (but NAME might contain it). */
3672
3673 if (sym != NULL
3674 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3675 return stab;
3676 if (with_opaque != NULL
3677 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3678 stab_best = stab;
3679
3680 /* Keep looking through other CUs. */
3681 }
3682
3683 return stab_best;
3684 }
3685
3686 void
3687 dwarf2_base_index_functions::print_stats (struct objfile *objfile)
3688 {
3689 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3690 int total = (per_objfile->per_bfd->all_comp_units.size ()
3691 + per_objfile->per_bfd->all_type_units.size ());
3692 int count = 0;
3693
3694 for (int i = 0; i < total; ++i)
3695 {
3696 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3697
3698 if (!per_objfile->symtab_set_p (per_cu))
3699 ++count;
3700 }
3701 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3702 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3703 }
3704
3705 /* This dumps minimal information about the index.
3706 It is called via "mt print objfiles".
3707 One use is to verify .gdb_index has been loaded by the
3708 gdb.dwarf2/gdb-index.exp testcase. */
3709
3710 void
3711 dwarf2_gdb_index::dump (struct objfile *objfile)
3712 {
3713 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3714
3715 gdb_assert (per_objfile->per_bfd->using_index);
3716 printf_filtered (".gdb_index:");
3717 if (per_objfile->per_bfd->index_table != NULL)
3718 {
3719 printf_filtered (" version %d\n",
3720 per_objfile->per_bfd->index_table->version);
3721 }
3722 else
3723 printf_filtered (" faked for \"readnow\"\n");
3724 printf_filtered ("\n");
3725 }
3726
3727 void
3728 dwarf2_gdb_index::expand_symtabs_for_function (struct objfile *objfile,
3729 const char *func_name)
3730 {
3731 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3732
3733 struct dw2_symtab_iterator iter;
3734 struct dwarf2_per_cu_data *per_cu;
3735
3736 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
3737
3738 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3739 dw2_instantiate_symtab (per_cu, per_objfile, false);
3740
3741 }
3742
3743 void
3744 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
3745 {
3746 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3747 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3748 + per_objfile->per_bfd->all_type_units.size ());
3749
3750 for (int i = 0; i < total_units; ++i)
3751 {
3752 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3753
3754 /* We don't want to directly expand a partial CU, because if we
3755 read it with the wrong language, then assertion failures can
3756 be triggered later on. See PR symtab/23010. So, tell
3757 dw2_instantiate_symtab to skip partial CUs -- any important
3758 partial CU will be read via DW_TAG_imported_unit anyway. */
3759 dw2_instantiate_symtab (per_cu, per_objfile, true);
3760 }
3761 }
3762
3763 void
3764 dwarf2_base_index_functions::expand_symtabs_with_fullname
3765 (struct objfile *objfile, const char *fullname)
3766 {
3767 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3768
3769 /* We don't need to consider type units here.
3770 This is only called for examining code, e.g. expand_line_sal.
3771 There can be an order of magnitude (or more) more type units
3772 than comp units, and we avoid them if we can. */
3773
3774 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3775 {
3776 /* We only need to look at symtabs not already expanded. */
3777 if (per_objfile->symtab_set_p (per_cu))
3778 continue;
3779
3780 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3781 if (file_data == NULL)
3782 continue;
3783
3784 for (int j = 0; j < file_data->num_file_names; ++j)
3785 {
3786 const char *this_fullname = file_data->file_names[j];
3787
3788 if (filename_cmp (this_fullname, fullname) == 0)
3789 {
3790 dw2_instantiate_symtab (per_cu, per_objfile, false);
3791 break;
3792 }
3793 }
3794 }
3795 }
3796
3797 static void
3798 dw2_expand_symtabs_matching_symbol
3799 (mapped_index_base &index,
3800 const lookup_name_info &lookup_name_in,
3801 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3802 enum search_domain kind,
3803 gdb::function_view<bool (offset_type)> match_callback,
3804 dwarf2_per_objfile *per_objfile);
3805
3806 static void
3807 dw2_expand_symtabs_matching_one
3808 (dwarf2_per_cu_data *per_cu,
3809 dwarf2_per_objfile *per_objfile,
3810 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3811 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3812
3813 static void
3814 dw2_map_matching_symbols
3815 (struct objfile *objfile,
3816 const lookup_name_info &name, domain_enum domain,
3817 int global,
3818 gdb::function_view<symbol_found_callback_ftype> callback,
3819 symbol_compare_ftype *ordered_compare)
3820 {
3821 /* Used for Ada. */
3822 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3823
3824 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3825
3826 if (per_objfile->per_bfd->index_table != nullptr)
3827 {
3828 mapped_index &index = *per_objfile->per_bfd->index_table;
3829
3830 const char *match_name = name.ada ().lookup_name ().c_str ();
3831 auto matcher = [&] (const char *symname)
3832 {
3833 if (ordered_compare == nullptr)
3834 return true;
3835 return ordered_compare (symname, match_name) == 0;
3836 };
3837
3838 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3839 [&] (offset_type namei)
3840 {
3841 struct dw2_symtab_iterator iter;
3842 struct dwarf2_per_cu_data *per_cu;
3843
3844 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3845 namei);
3846 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3847 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3848 nullptr);
3849 return true;
3850 }, per_objfile);
3851 }
3852 else
3853 {
3854 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3855 proceed assuming all symtabs have been read in. */
3856 }
3857
3858 for (compunit_symtab *cust : objfile->compunits ())
3859 {
3860 const struct block *block;
3861
3862 if (cust == NULL)
3863 continue;
3864 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3865 if (!iterate_over_symbols_terminated (block, name,
3866 domain, callback))
3867 return;
3868 }
3869 }
3870
3871 void
3872 dwarf2_gdb_index::map_matching_symbols
3873 (struct objfile *objfile,
3874 const lookup_name_info &name, domain_enum domain,
3875 int global,
3876 gdb::function_view<symbol_found_callback_ftype> callback,
3877 symbol_compare_ftype *ordered_compare)
3878 {
3879 dw2_map_matching_symbols (objfile, name, domain, global, callback,
3880 ordered_compare);
3881 }
3882
3883 /* Starting from a search name, return the string that finds the upper
3884 bound of all strings that start with SEARCH_NAME in a sorted name
3885 list. Returns the empty string to indicate that the upper bound is
3886 the end of the list. */
3887
3888 static std::string
3889 make_sort_after_prefix_name (const char *search_name)
3890 {
3891 /* When looking to complete "func", we find the upper bound of all
3892 symbols that start with "func" by looking for where we'd insert
3893 the closest string that would follow "func" in lexicographical
3894 order. Usually, that's "func"-with-last-character-incremented,
3895 i.e. "fund". Mind non-ASCII characters, though. Usually those
3896 will be UTF-8 multi-byte sequences, but we can't be certain.
3897 Especially mind the 0xff character, which is a valid character in
3898 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3899 rule out compilers allowing it in identifiers. Note that
3900 conveniently, strcmp/strcasecmp are specified to compare
3901 characters interpreted as unsigned char. So what we do is treat
3902 the whole string as a base 256 number composed of a sequence of
3903 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3904 to 0, and carries 1 to the following more-significant position.
3905 If the very first character in SEARCH_NAME ends up incremented
3906 and carries/overflows, then the upper bound is the end of the
3907 list. The string after the empty string is also the empty
3908 string.
3909
3910 Some examples of this operation:
3911
3912 SEARCH_NAME => "+1" RESULT
3913
3914 "abc" => "abd"
3915 "ab\xff" => "ac"
3916 "\xff" "a" "\xff" => "\xff" "b"
3917 "\xff" => ""
3918 "\xff\xff" => ""
3919 "" => ""
3920
3921 Then, with these symbols for example:
3922
3923 func
3924 func1
3925 fund
3926
3927 completing "func" looks for symbols between "func" and
3928 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3929 which finds "func" and "func1", but not "fund".
3930
3931 And with:
3932
3933 funcÿ (Latin1 'ÿ' [0xff])
3934 funcÿ1
3935 fund
3936
3937 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3938 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3939
3940 And with:
3941
3942 ÿÿ (Latin1 'ÿ' [0xff])
3943 ÿÿ1
3944
3945 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3946 the end of the list.
3947 */
3948 std::string after = search_name;
3949 while (!after.empty () && (unsigned char) after.back () == 0xff)
3950 after.pop_back ();
3951 if (!after.empty ())
3952 after.back () = (unsigned char) after.back () + 1;
3953 return after;
3954 }
3955
3956 /* See declaration. */
3957
3958 std::pair<std::vector<name_component>::const_iterator,
3959 std::vector<name_component>::const_iterator>
3960 mapped_index_base::find_name_components_bounds
3961 (const lookup_name_info &lookup_name_without_params, language lang,
3962 dwarf2_per_objfile *per_objfile) const
3963 {
3964 auto *name_cmp
3965 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3966
3967 const char *lang_name
3968 = lookup_name_without_params.language_lookup_name (lang);
3969
3970 /* Comparison function object for lower_bound that matches against a
3971 given symbol name. */
3972 auto lookup_compare_lower = [&] (const name_component &elem,
3973 const char *name)
3974 {
3975 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3976 const char *elem_name = elem_qualified + elem.name_offset;
3977 return name_cmp (elem_name, name) < 0;
3978 };
3979
3980 /* Comparison function object for upper_bound that matches against a
3981 given symbol name. */
3982 auto lookup_compare_upper = [&] (const char *name,
3983 const name_component &elem)
3984 {
3985 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3986 const char *elem_name = elem_qualified + elem.name_offset;
3987 return name_cmp (name, elem_name) < 0;
3988 };
3989
3990 auto begin = this->name_components.begin ();
3991 auto end = this->name_components.end ();
3992
3993 /* Find the lower bound. */
3994 auto lower = [&] ()
3995 {
3996 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3997 return begin;
3998 else
3999 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
4000 } ();
4001
4002 /* Find the upper bound. */
4003 auto upper = [&] ()
4004 {
4005 if (lookup_name_without_params.completion_mode ())
4006 {
4007 /* In completion mode, we want UPPER to point past all
4008 symbols names that have the same prefix. I.e., with
4009 these symbols, and completing "func":
4010
4011 function << lower bound
4012 function1
4013 other_function << upper bound
4014
4015 We find the upper bound by looking for the insertion
4016 point of "func"-with-last-character-incremented,
4017 i.e. "fund". */
4018 std::string after = make_sort_after_prefix_name (lang_name);
4019 if (after.empty ())
4020 return end;
4021 return std::lower_bound (lower, end, after.c_str (),
4022 lookup_compare_lower);
4023 }
4024 else
4025 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4026 } ();
4027
4028 return {lower, upper};
4029 }
4030
4031 /* See declaration. */
4032
4033 void
4034 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
4035 {
4036 if (!this->name_components.empty ())
4037 return;
4038
4039 this->name_components_casing = case_sensitivity;
4040 auto *name_cmp
4041 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4042
4043 /* The code below only knows how to break apart components of C++
4044 symbol names (and other languages that use '::' as
4045 namespace/module separator) and Ada symbol names. */
4046 auto count = this->symbol_name_count ();
4047 for (offset_type idx = 0; idx < count; idx++)
4048 {
4049 if (this->symbol_name_slot_invalid (idx))
4050 continue;
4051
4052 const char *name = this->symbol_name_at (idx, per_objfile);
4053
4054 /* Add each name component to the name component table. */
4055 unsigned int previous_len = 0;
4056
4057 if (strstr (name, "::") != nullptr)
4058 {
4059 for (unsigned int current_len = cp_find_first_component (name);
4060 name[current_len] != '\0';
4061 current_len += cp_find_first_component (name + current_len))
4062 {
4063 gdb_assert (name[current_len] == ':');
4064 this->name_components.push_back ({previous_len, idx});
4065 /* Skip the '::'. */
4066 current_len += 2;
4067 previous_len = current_len;
4068 }
4069 }
4070 else
4071 {
4072 /* Handle the Ada encoded (aka mangled) form here. */
4073 for (const char *iter = strstr (name, "__");
4074 iter != nullptr;
4075 iter = strstr (iter, "__"))
4076 {
4077 this->name_components.push_back ({previous_len, idx});
4078 iter += 2;
4079 previous_len = iter - name;
4080 }
4081 }
4082
4083 this->name_components.push_back ({previous_len, idx});
4084 }
4085
4086 /* Sort name_components elements by name. */
4087 auto name_comp_compare = [&] (const name_component &left,
4088 const name_component &right)
4089 {
4090 const char *left_qualified
4091 = this->symbol_name_at (left.idx, per_objfile);
4092 const char *right_qualified
4093 = this->symbol_name_at (right.idx, per_objfile);
4094
4095 const char *left_name = left_qualified + left.name_offset;
4096 const char *right_name = right_qualified + right.name_offset;
4097
4098 return name_cmp (left_name, right_name) < 0;
4099 };
4100
4101 std::sort (this->name_components.begin (),
4102 this->name_components.end (),
4103 name_comp_compare);
4104 }
4105
4106 /* Helper for dw2_expand_symtabs_matching that works with a
4107 mapped_index_base instead of the containing objfile. This is split
4108 to a separate function in order to be able to unit test the
4109 name_components matching using a mock mapped_index_base. For each
4110 symbol name that matches, calls MATCH_CALLBACK, passing it the
4111 symbol's index in the mapped_index_base symbol table. */
4112
4113 static void
4114 dw2_expand_symtabs_matching_symbol
4115 (mapped_index_base &index,
4116 const lookup_name_info &lookup_name_in,
4117 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4118 enum search_domain kind,
4119 gdb::function_view<bool (offset_type)> match_callback,
4120 dwarf2_per_objfile *per_objfile)
4121 {
4122 lookup_name_info lookup_name_without_params
4123 = lookup_name_in.make_ignore_params ();
4124
4125 /* Build the symbol name component sorted vector, if we haven't
4126 yet. */
4127 index.build_name_components (per_objfile);
4128
4129 /* The same symbol may appear more than once in the range though.
4130 E.g., if we're looking for symbols that complete "w", and we have
4131 a symbol named "w1::w2", we'll find the two name components for
4132 that same symbol in the range. To be sure we only call the
4133 callback once per symbol, we first collect the symbol name
4134 indexes that matched in a temporary vector and ignore
4135 duplicates. */
4136 std::vector<offset_type> matches;
4137
4138 struct name_and_matcher
4139 {
4140 symbol_name_matcher_ftype *matcher;
4141 const char *name;
4142
4143 bool operator== (const name_and_matcher &other) const
4144 {
4145 return matcher == other.matcher && strcmp (name, other.name) == 0;
4146 }
4147 };
4148
4149 /* A vector holding all the different symbol name matchers, for all
4150 languages. */
4151 std::vector<name_and_matcher> matchers;
4152
4153 for (int i = 0; i < nr_languages; i++)
4154 {
4155 enum language lang_e = (enum language) i;
4156
4157 const language_defn *lang = language_def (lang_e);
4158 symbol_name_matcher_ftype *name_matcher
4159 = lang->get_symbol_name_matcher (lookup_name_without_params);
4160
4161 name_and_matcher key {
4162 name_matcher,
4163 lookup_name_without_params.language_lookup_name (lang_e)
4164 };
4165
4166 /* Don't insert the same comparison routine more than once.
4167 Note that we do this linear walk. This is not a problem in
4168 practice because the number of supported languages is
4169 low. */
4170 if (std::find (matchers.begin (), matchers.end (), key)
4171 != matchers.end ())
4172 continue;
4173 matchers.push_back (std::move (key));
4174
4175 auto bounds
4176 = index.find_name_components_bounds (lookup_name_without_params,
4177 lang_e, per_objfile);
4178
4179 /* Now for each symbol name in range, check to see if we have a name
4180 match, and if so, call the MATCH_CALLBACK callback. */
4181
4182 for (; bounds.first != bounds.second; ++bounds.first)
4183 {
4184 const char *qualified
4185 = index.symbol_name_at (bounds.first->idx, per_objfile);
4186
4187 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4188 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4189 continue;
4190
4191 matches.push_back (bounds.first->idx);
4192 }
4193 }
4194
4195 std::sort (matches.begin (), matches.end ());
4196
4197 /* Finally call the callback, once per match. */
4198 ULONGEST prev = -1;
4199 for (offset_type idx : matches)
4200 {
4201 if (prev != idx)
4202 {
4203 if (!match_callback (idx))
4204 break;
4205 prev = idx;
4206 }
4207 }
4208
4209 /* Above we use a type wider than idx's for 'prev', since 0 and
4210 (offset_type)-1 are both possible values. */
4211 static_assert (sizeof (prev) > sizeof (offset_type), "");
4212 }
4213
4214 #if GDB_SELF_TEST
4215
4216 namespace selftests { namespace dw2_expand_symtabs_matching {
4217
4218 /* A mock .gdb_index/.debug_names-like name index table, enough to
4219 exercise dw2_expand_symtabs_matching_symbol, which works with the
4220 mapped_index_base interface. Builds an index from the symbol list
4221 passed as parameter to the constructor. */
4222 class mock_mapped_index : public mapped_index_base
4223 {
4224 public:
4225 mock_mapped_index (gdb::array_view<const char *> symbols)
4226 : m_symbol_table (symbols)
4227 {}
4228
4229 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4230
4231 /* Return the number of names in the symbol table. */
4232 size_t symbol_name_count () const override
4233 {
4234 return m_symbol_table.size ();
4235 }
4236
4237 /* Get the name of the symbol at IDX in the symbol table. */
4238 const char *symbol_name_at
4239 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
4240 {
4241 return m_symbol_table[idx];
4242 }
4243
4244 private:
4245 gdb::array_view<const char *> m_symbol_table;
4246 };
4247
4248 /* Convenience function that converts a NULL pointer to a "<null>"
4249 string, to pass to print routines. */
4250
4251 static const char *
4252 string_or_null (const char *str)
4253 {
4254 return str != NULL ? str : "<null>";
4255 }
4256
4257 /* Check if a lookup_name_info built from
4258 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4259 index. EXPECTED_LIST is the list of expected matches, in expected
4260 matching order. If no match expected, then an empty list is
4261 specified. Returns true on success. On failure prints a warning
4262 indicating the file:line that failed, and returns false. */
4263
4264 static bool
4265 check_match (const char *file, int line,
4266 mock_mapped_index &mock_index,
4267 const char *name, symbol_name_match_type match_type,
4268 bool completion_mode,
4269 std::initializer_list<const char *> expected_list,
4270 dwarf2_per_objfile *per_objfile)
4271 {
4272 lookup_name_info lookup_name (name, match_type, completion_mode);
4273
4274 bool matched = true;
4275
4276 auto mismatch = [&] (const char *expected_str,
4277 const char *got)
4278 {
4279 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4280 "expected=\"%s\", got=\"%s\"\n"),
4281 file, line,
4282 (match_type == symbol_name_match_type::FULL
4283 ? "FULL" : "WILD"),
4284 name, string_or_null (expected_str), string_or_null (got));
4285 matched = false;
4286 };
4287
4288 auto expected_it = expected_list.begin ();
4289 auto expected_end = expected_list.end ();
4290
4291 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4292 NULL, ALL_DOMAIN,
4293 [&] (offset_type idx)
4294 {
4295 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
4296 const char *expected_str
4297 = expected_it == expected_end ? NULL : *expected_it++;
4298
4299 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4300 mismatch (expected_str, matched_name);
4301 return true;
4302 }, per_objfile);
4303
4304 const char *expected_str
4305 = expected_it == expected_end ? NULL : *expected_it++;
4306 if (expected_str != NULL)
4307 mismatch (expected_str, NULL);
4308
4309 return matched;
4310 }
4311
4312 /* The symbols added to the mock mapped_index for testing (in
4313 canonical form). */
4314 static const char *test_symbols[] = {
4315 "function",
4316 "std::bar",
4317 "std::zfunction",
4318 "std::zfunction2",
4319 "w1::w2",
4320 "ns::foo<char*>",
4321 "ns::foo<int>",
4322 "ns::foo<long>",
4323 "ns2::tmpl<int>::foo2",
4324 "(anonymous namespace)::A::B::C",
4325
4326 /* These are used to check that the increment-last-char in the
4327 matching algorithm for completion doesn't match "t1_fund" when
4328 completing "t1_func". */
4329 "t1_func",
4330 "t1_func1",
4331 "t1_fund",
4332 "t1_fund1",
4333
4334 /* A UTF-8 name with multi-byte sequences to make sure that
4335 cp-name-parser understands this as a single identifier ("função"
4336 is "function" in PT). */
4337 u8"u8função",
4338
4339 /* \377 (0xff) is Latin1 'ÿ'. */
4340 "yfunc\377",
4341
4342 /* \377 (0xff) is Latin1 'ÿ'. */
4343 "\377",
4344 "\377\377123",
4345
4346 /* A name with all sorts of complications. Starts with "z" to make
4347 it easier for the completion tests below. */
4348 #define Z_SYM_NAME \
4349 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4350 "::tuple<(anonymous namespace)::ui*, " \
4351 "std::default_delete<(anonymous namespace)::ui>, void>"
4352
4353 Z_SYM_NAME
4354 };
4355
4356 /* Returns true if the mapped_index_base::find_name_component_bounds
4357 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4358 in completion mode. */
4359
4360 static bool
4361 check_find_bounds_finds (mapped_index_base &index,
4362 const char *search_name,
4363 gdb::array_view<const char *> expected_syms,
4364 dwarf2_per_objfile *per_objfile)
4365 {
4366 lookup_name_info lookup_name (search_name,
4367 symbol_name_match_type::FULL, true);
4368
4369 auto bounds = index.find_name_components_bounds (lookup_name,
4370 language_cplus,
4371 per_objfile);
4372
4373 size_t distance = std::distance (bounds.first, bounds.second);
4374 if (distance != expected_syms.size ())
4375 return false;
4376
4377 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4378 {
4379 auto nc_elem = bounds.first + exp_elem;
4380 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
4381 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4382 return false;
4383 }
4384
4385 return true;
4386 }
4387
4388 /* Test the lower-level mapped_index::find_name_component_bounds
4389 method. */
4390
4391 static void
4392 test_mapped_index_find_name_component_bounds ()
4393 {
4394 mock_mapped_index mock_index (test_symbols);
4395
4396 mock_index.build_name_components (NULL /* per_objfile */);
4397
4398 /* Test the lower-level mapped_index::find_name_component_bounds
4399 method in completion mode. */
4400 {
4401 static const char *expected_syms[] = {
4402 "t1_func",
4403 "t1_func1",
4404 };
4405
4406 SELF_CHECK (check_find_bounds_finds
4407 (mock_index, "t1_func", expected_syms,
4408 NULL /* per_objfile */));
4409 }
4410
4411 /* Check that the increment-last-char in the name matching algorithm
4412 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4413 {
4414 static const char *expected_syms1[] = {
4415 "\377",
4416 "\377\377123",
4417 };
4418 SELF_CHECK (check_find_bounds_finds
4419 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
4420
4421 static const char *expected_syms2[] = {
4422 "\377\377123",
4423 };
4424 SELF_CHECK (check_find_bounds_finds
4425 (mock_index, "\377\377", expected_syms2,
4426 NULL /* per_objfile */));
4427 }
4428 }
4429
4430 /* Test dw2_expand_symtabs_matching_symbol. */
4431
4432 static void
4433 test_dw2_expand_symtabs_matching_symbol ()
4434 {
4435 mock_mapped_index mock_index (test_symbols);
4436
4437 /* We let all tests run until the end even if some fails, for debug
4438 convenience. */
4439 bool any_mismatch = false;
4440
4441 /* Create the expected symbols list (an initializer_list). Needed
4442 because lists have commas, and we need to pass them to CHECK,
4443 which is a macro. */
4444 #define EXPECT(...) { __VA_ARGS__ }
4445
4446 /* Wrapper for check_match that passes down the current
4447 __FILE__/__LINE__. */
4448 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4449 any_mismatch |= !check_match (__FILE__, __LINE__, \
4450 mock_index, \
4451 NAME, MATCH_TYPE, COMPLETION_MODE, \
4452 EXPECTED_LIST, NULL)
4453
4454 /* Identity checks. */
4455 for (const char *sym : test_symbols)
4456 {
4457 /* Should be able to match all existing symbols. */
4458 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4459 EXPECT (sym));
4460
4461 /* Should be able to match all existing symbols with
4462 parameters. */
4463 std::string with_params = std::string (sym) + "(int)";
4464 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4465 EXPECT (sym));
4466
4467 /* Should be able to match all existing symbols with
4468 parameters and qualifiers. */
4469 with_params = std::string (sym) + " ( int ) const";
4470 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4471 EXPECT (sym));
4472
4473 /* This should really find sym, but cp-name-parser.y doesn't
4474 know about lvalue/rvalue qualifiers yet. */
4475 with_params = std::string (sym) + " ( int ) &&";
4476 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4477 {});
4478 }
4479
4480 /* Check that the name matching algorithm for completion doesn't get
4481 confused with Latin1 'ÿ' / 0xff. */
4482 {
4483 static const char str[] = "\377";
4484 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4485 EXPECT ("\377", "\377\377123"));
4486 }
4487
4488 /* Check that the increment-last-char in the matching algorithm for
4489 completion doesn't match "t1_fund" when completing "t1_func". */
4490 {
4491 static const char str[] = "t1_func";
4492 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4493 EXPECT ("t1_func", "t1_func1"));
4494 }
4495
4496 /* Check that completion mode works at each prefix of the expected
4497 symbol name. */
4498 {
4499 static const char str[] = "function(int)";
4500 size_t len = strlen (str);
4501 std::string lookup;
4502
4503 for (size_t i = 1; i < len; i++)
4504 {
4505 lookup.assign (str, i);
4506 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4507 EXPECT ("function"));
4508 }
4509 }
4510
4511 /* While "w" is a prefix of both components, the match function
4512 should still only be called once. */
4513 {
4514 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4515 EXPECT ("w1::w2"));
4516 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4517 EXPECT ("w1::w2"));
4518 }
4519
4520 /* Same, with a "complicated" symbol. */
4521 {
4522 static const char str[] = Z_SYM_NAME;
4523 size_t len = strlen (str);
4524 std::string lookup;
4525
4526 for (size_t i = 1; i < len; i++)
4527 {
4528 lookup.assign (str, i);
4529 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4530 EXPECT (Z_SYM_NAME));
4531 }
4532 }
4533
4534 /* In FULL mode, an incomplete symbol doesn't match. */
4535 {
4536 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4537 {});
4538 }
4539
4540 /* A complete symbol with parameters matches any overload, since the
4541 index has no overload info. */
4542 {
4543 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4544 EXPECT ("std::zfunction", "std::zfunction2"));
4545 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4546 EXPECT ("std::zfunction", "std::zfunction2"));
4547 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4548 EXPECT ("std::zfunction", "std::zfunction2"));
4549 }
4550
4551 /* Check that whitespace is ignored appropriately. A symbol with a
4552 template argument list. */
4553 {
4554 static const char expected[] = "ns::foo<int>";
4555 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4556 EXPECT (expected));
4557 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4558 EXPECT (expected));
4559 }
4560
4561 /* Check that whitespace is ignored appropriately. A symbol with a
4562 template argument list that includes a pointer. */
4563 {
4564 static const char expected[] = "ns::foo<char*>";
4565 /* Try both completion and non-completion modes. */
4566 static const bool completion_mode[2] = {false, true};
4567 for (size_t i = 0; i < 2; i++)
4568 {
4569 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4570 completion_mode[i], EXPECT (expected));
4571 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4572 completion_mode[i], EXPECT (expected));
4573
4574 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4575 completion_mode[i], EXPECT (expected));
4576 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4577 completion_mode[i], EXPECT (expected));
4578 }
4579 }
4580
4581 {
4582 /* Check method qualifiers are ignored. */
4583 static const char expected[] = "ns::foo<char*>";
4584 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4585 symbol_name_match_type::FULL, true, EXPECT (expected));
4586 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4587 symbol_name_match_type::FULL, true, EXPECT (expected));
4588 CHECK_MATCH ("foo < char * > ( int ) const",
4589 symbol_name_match_type::WILD, true, EXPECT (expected));
4590 CHECK_MATCH ("foo < char * > ( int ) &&",
4591 symbol_name_match_type::WILD, true, EXPECT (expected));
4592 }
4593
4594 /* Test lookup names that don't match anything. */
4595 {
4596 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4597 {});
4598
4599 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4600 {});
4601 }
4602
4603 /* Some wild matching tests, exercising "(anonymous namespace)",
4604 which should not be confused with a parameter list. */
4605 {
4606 static const char *syms[] = {
4607 "A::B::C",
4608 "B::C",
4609 "C",
4610 "A :: B :: C ( int )",
4611 "B :: C ( int )",
4612 "C ( int )",
4613 };
4614
4615 for (const char *s : syms)
4616 {
4617 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4618 EXPECT ("(anonymous namespace)::A::B::C"));
4619 }
4620 }
4621
4622 {
4623 static const char expected[] = "ns2::tmpl<int>::foo2";
4624 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4625 EXPECT (expected));
4626 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4627 EXPECT (expected));
4628 }
4629
4630 SELF_CHECK (!any_mismatch);
4631
4632 #undef EXPECT
4633 #undef CHECK_MATCH
4634 }
4635
4636 static void
4637 run_test ()
4638 {
4639 test_mapped_index_find_name_component_bounds ();
4640 test_dw2_expand_symtabs_matching_symbol ();
4641 }
4642
4643 }} // namespace selftests::dw2_expand_symtabs_matching
4644
4645 #endif /* GDB_SELF_TEST */
4646
4647 /* If FILE_MATCHER is NULL or if PER_CU has
4648 dwarf2_per_cu_quick_data::MARK set (see
4649 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4650 EXPANSION_NOTIFY on it. */
4651
4652 static void
4653 dw2_expand_symtabs_matching_one
4654 (dwarf2_per_cu_data *per_cu,
4655 dwarf2_per_objfile *per_objfile,
4656 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4657 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4658 {
4659 if (file_matcher == NULL || per_cu->v.quick->mark)
4660 {
4661 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4662
4663 compunit_symtab *symtab
4664 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4665 gdb_assert (symtab != nullptr);
4666
4667 if (expansion_notify != NULL && symtab_was_null)
4668 expansion_notify (symtab);
4669 }
4670 }
4671
4672 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4673 matched, to expand corresponding CUs that were marked. IDX is the
4674 index of the symbol name that matched. */
4675
4676 static void
4677 dw2_expand_marked_cus
4678 (dwarf2_per_objfile *per_objfile, offset_type idx,
4679 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4680 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4681 search_domain kind)
4682 {
4683 offset_type *vec, vec_len, vec_idx;
4684 bool global_seen = false;
4685 mapped_index &index = *per_objfile->per_bfd->index_table;
4686
4687 vec = (offset_type *) (index.constant_pool
4688 + MAYBE_SWAP (index.symbol_table[idx].vec));
4689 vec_len = MAYBE_SWAP (vec[0]);
4690 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4691 {
4692 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4693 /* This value is only valid for index versions >= 7. */
4694 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4695 gdb_index_symbol_kind symbol_kind =
4696 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4697 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4698 /* Only check the symbol attributes if they're present.
4699 Indices prior to version 7 don't record them,
4700 and indices >= 7 may elide them for certain symbols
4701 (gold does this). */
4702 int attrs_valid =
4703 (index.version >= 7
4704 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4705
4706 /* Work around gold/15646. */
4707 if (attrs_valid
4708 && !is_static
4709 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4710 {
4711 if (global_seen)
4712 continue;
4713
4714 global_seen = true;
4715 }
4716
4717 /* Only check the symbol's kind if it has one. */
4718 if (attrs_valid)
4719 {
4720 switch (kind)
4721 {
4722 case VARIABLES_DOMAIN:
4723 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4724 continue;
4725 break;
4726 case FUNCTIONS_DOMAIN:
4727 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4728 continue;
4729 break;
4730 case TYPES_DOMAIN:
4731 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4732 continue;
4733 break;
4734 case MODULES_DOMAIN:
4735 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4736 continue;
4737 break;
4738 default:
4739 break;
4740 }
4741 }
4742
4743 /* Don't crash on bad data. */
4744 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4745 + per_objfile->per_bfd->all_type_units.size ()))
4746 {
4747 complaint (_(".gdb_index entry has bad CU index"
4748 " [in module %s]"), objfile_name (per_objfile->objfile));
4749 continue;
4750 }
4751
4752 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4753 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4754 expansion_notify);
4755 }
4756 }
4757
4758 /* If FILE_MATCHER is non-NULL, set all the
4759 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4760 that match FILE_MATCHER. */
4761
4762 static void
4763 dw_expand_symtabs_matching_file_matcher
4764 (dwarf2_per_objfile *per_objfile,
4765 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4766 {
4767 if (file_matcher == NULL)
4768 return;
4769
4770 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4771 htab_eq_pointer,
4772 NULL, xcalloc, xfree));
4773 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4774 htab_eq_pointer,
4775 NULL, xcalloc, xfree));
4776
4777 /* The rule is CUs specify all the files, including those used by
4778 any TU, so there's no need to scan TUs here. */
4779
4780 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4781 {
4782 QUIT;
4783
4784 per_cu->v.quick->mark = 0;
4785
4786 /* We only need to look at symtabs not already expanded. */
4787 if (per_objfile->symtab_set_p (per_cu))
4788 continue;
4789
4790 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4791 if (file_data == NULL)
4792 continue;
4793
4794 if (htab_find (visited_not_found.get (), file_data) != NULL)
4795 continue;
4796 else if (htab_find (visited_found.get (), file_data) != NULL)
4797 {
4798 per_cu->v.quick->mark = 1;
4799 continue;
4800 }
4801
4802 for (int j = 0; j < file_data->num_file_names; ++j)
4803 {
4804 const char *this_real_name;
4805
4806 if (file_matcher (file_data->file_names[j], false))
4807 {
4808 per_cu->v.quick->mark = 1;
4809 break;
4810 }
4811
4812 /* Before we invoke realpath, which can get expensive when many
4813 files are involved, do a quick comparison of the basenames. */
4814 if (!basenames_may_differ
4815 && !file_matcher (lbasename (file_data->file_names[j]),
4816 true))
4817 continue;
4818
4819 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4820 if (file_matcher (this_real_name, false))
4821 {
4822 per_cu->v.quick->mark = 1;
4823 break;
4824 }
4825 }
4826
4827 void **slot = htab_find_slot (per_cu->v.quick->mark
4828 ? visited_found.get ()
4829 : visited_not_found.get (),
4830 file_data, INSERT);
4831 *slot = file_data;
4832 }
4833 }
4834
4835 static void
4836 dw2_expand_symtabs_matching
4837 (struct objfile *objfile,
4838 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4839 const lookup_name_info *lookup_name,
4840 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4841 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4842 enum search_domain kind)
4843 {
4844 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4845
4846 /* index_table is NULL if OBJF_READNOW. */
4847 if (!per_objfile->per_bfd->index_table)
4848 return;
4849
4850 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4851
4852 if (symbol_matcher == NULL && lookup_name == NULL)
4853 {
4854 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4855 {
4856 QUIT;
4857
4858 dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4859 file_matcher, expansion_notify);
4860 }
4861 return;
4862 }
4863
4864 mapped_index &index = *per_objfile->per_bfd->index_table;
4865
4866 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4867 symbol_matcher,
4868 kind, [&] (offset_type idx)
4869 {
4870 dw2_expand_marked_cus (per_objfile, idx, file_matcher, expansion_notify,
4871 kind);
4872 return true;
4873 }, per_objfile);
4874 }
4875
4876 void
4877 dwarf2_gdb_index::expand_symtabs_matching
4878 (struct objfile *objfile,
4879 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4880 const lookup_name_info *lookup_name,
4881 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4882 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4883 enum search_domain kind)
4884 {
4885 dw2_expand_symtabs_matching (objfile, file_matcher, lookup_name,
4886 symbol_matcher, expansion_notify, kind);
4887 }
4888
4889 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4890 symtab. */
4891
4892 static struct compunit_symtab *
4893 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4894 CORE_ADDR pc)
4895 {
4896 int i;
4897
4898 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4899 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4900 return cust;
4901
4902 if (cust->includes == NULL)
4903 return NULL;
4904
4905 for (i = 0; cust->includes[i]; ++i)
4906 {
4907 struct compunit_symtab *s = cust->includes[i];
4908
4909 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4910 if (s != NULL)
4911 return s;
4912 }
4913
4914 return NULL;
4915 }
4916
4917 struct compunit_symtab *
4918 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4919 (struct objfile *objfile,
4920 struct bound_minimal_symbol msymbol,
4921 CORE_ADDR pc,
4922 struct obj_section *section,
4923 int warn_if_readin)
4924 {
4925 struct dwarf2_per_cu_data *data;
4926 struct compunit_symtab *result;
4927
4928 if (!objfile->partial_symtabs->psymtabs_addrmap)
4929 return NULL;
4930
4931 CORE_ADDR baseaddr = objfile->text_section_offset ();
4932 data = (struct dwarf2_per_cu_data *) addrmap_find
4933 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4934 if (!data)
4935 return NULL;
4936
4937 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4938 if (warn_if_readin && per_objfile->symtab_set_p (data))
4939 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4940 paddress (objfile->arch (), pc));
4941
4942 result = recursively_find_pc_sect_compunit_symtab
4943 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4944
4945 gdb_assert (result != NULL);
4946 return result;
4947 }
4948
4949 void
4950 dwarf2_base_index_functions::map_symbol_filenames (struct objfile *objfile,
4951 symbol_filename_ftype *fun,
4952 void *data,
4953 int need_fullname)
4954 {
4955 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4956
4957 if (!per_objfile->per_bfd->filenames_cache)
4958 {
4959 per_objfile->per_bfd->filenames_cache.emplace ();
4960
4961 htab_up visited (htab_create_alloc (10,
4962 htab_hash_pointer, htab_eq_pointer,
4963 NULL, xcalloc, xfree));
4964
4965 /* The rule is CUs specify all the files, including those used
4966 by any TU, so there's no need to scan TUs here. We can
4967 ignore file names coming from already-expanded CUs. */
4968
4969 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4970 {
4971 if (per_objfile->symtab_set_p (per_cu))
4972 {
4973 void **slot = htab_find_slot (visited.get (),
4974 per_cu->v.quick->file_names,
4975 INSERT);
4976
4977 *slot = per_cu->v.quick->file_names;
4978 }
4979 }
4980
4981 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4982 {
4983 /* We only need to look at symtabs not already expanded. */
4984 if (per_objfile->symtab_set_p (per_cu))
4985 continue;
4986
4987 quick_file_names *file_data
4988 = dw2_get_file_names (per_cu, per_objfile);
4989 if (file_data == NULL)
4990 continue;
4991
4992 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4993 if (*slot)
4994 {
4995 /* Already visited. */
4996 continue;
4997 }
4998 *slot = file_data;
4999
5000 for (int j = 0; j < file_data->num_file_names; ++j)
5001 {
5002 const char *filename = file_data->file_names[j];
5003 per_objfile->per_bfd->filenames_cache->seen (filename);
5004 }
5005 }
5006 }
5007
5008 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
5009 {
5010 gdb::unique_xmalloc_ptr<char> this_real_name;
5011
5012 if (need_fullname)
5013 this_real_name = gdb_realpath (filename);
5014 (*fun) (filename, this_real_name.get (), data);
5015 });
5016 }
5017
5018 bool
5019 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
5020 {
5021 return true;
5022 }
5023
5024 /* DWARF-5 debug_names reader. */
5025
5026 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5027 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5028
5029 /* A helper function that reads the .debug_names section in SECTION
5030 and fills in MAP. FILENAME is the name of the file containing the
5031 section; it is used for error reporting.
5032
5033 Returns true if all went well, false otherwise. */
5034
5035 static bool
5036 read_debug_names_from_section (struct objfile *objfile,
5037 const char *filename,
5038 struct dwarf2_section_info *section,
5039 mapped_debug_names &map)
5040 {
5041 if (section->empty ())
5042 return false;
5043
5044 /* Older elfutils strip versions could keep the section in the main
5045 executable while splitting it for the separate debug info file. */
5046 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5047 return false;
5048
5049 section->read (objfile);
5050
5051 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
5052
5053 const gdb_byte *addr = section->buffer;
5054
5055 bfd *const abfd = section->get_bfd_owner ();
5056
5057 unsigned int bytes_read;
5058 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5059 addr += bytes_read;
5060
5061 map.dwarf5_is_dwarf64 = bytes_read != 4;
5062 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5063 if (bytes_read + length != section->size)
5064 {
5065 /* There may be multiple per-CU indices. */
5066 warning (_("Section .debug_names in %s length %s does not match "
5067 "section length %s, ignoring .debug_names."),
5068 filename, plongest (bytes_read + length),
5069 pulongest (section->size));
5070 return false;
5071 }
5072
5073 /* The version number. */
5074 uint16_t version = read_2_bytes (abfd, addr);
5075 addr += 2;
5076 if (version != 5)
5077 {
5078 warning (_("Section .debug_names in %s has unsupported version %d, "
5079 "ignoring .debug_names."),
5080 filename, version);
5081 return false;
5082 }
5083
5084 /* Padding. */
5085 uint16_t padding = read_2_bytes (abfd, addr);
5086 addr += 2;
5087 if (padding != 0)
5088 {
5089 warning (_("Section .debug_names in %s has unsupported padding %d, "
5090 "ignoring .debug_names."),
5091 filename, padding);
5092 return false;
5093 }
5094
5095 /* comp_unit_count - The number of CUs in the CU list. */
5096 map.cu_count = read_4_bytes (abfd, addr);
5097 addr += 4;
5098
5099 /* local_type_unit_count - The number of TUs in the local TU
5100 list. */
5101 map.tu_count = read_4_bytes (abfd, addr);
5102 addr += 4;
5103
5104 /* foreign_type_unit_count - The number of TUs in the foreign TU
5105 list. */
5106 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5107 addr += 4;
5108 if (foreign_tu_count != 0)
5109 {
5110 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5111 "ignoring .debug_names."),
5112 filename, static_cast<unsigned long> (foreign_tu_count));
5113 return false;
5114 }
5115
5116 /* bucket_count - The number of hash buckets in the hash lookup
5117 table. */
5118 map.bucket_count = read_4_bytes (abfd, addr);
5119 addr += 4;
5120
5121 /* name_count - The number of unique names in the index. */
5122 map.name_count = read_4_bytes (abfd, addr);
5123 addr += 4;
5124
5125 /* abbrev_table_size - The size in bytes of the abbreviations
5126 table. */
5127 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5128 addr += 4;
5129
5130 /* augmentation_string_size - The size in bytes of the augmentation
5131 string. This value is rounded up to a multiple of 4. */
5132 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5133 addr += 4;
5134 map.augmentation_is_gdb = ((augmentation_string_size
5135 == sizeof (dwarf5_augmentation))
5136 && memcmp (addr, dwarf5_augmentation,
5137 sizeof (dwarf5_augmentation)) == 0);
5138 augmentation_string_size += (-augmentation_string_size) & 3;
5139 addr += augmentation_string_size;
5140
5141 /* List of CUs */
5142 map.cu_table_reordered = addr;
5143 addr += map.cu_count * map.offset_size;
5144
5145 /* List of Local TUs */
5146 map.tu_table_reordered = addr;
5147 addr += map.tu_count * map.offset_size;
5148
5149 /* Hash Lookup Table */
5150 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5151 addr += map.bucket_count * 4;
5152 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5153 addr += map.name_count * 4;
5154
5155 /* Name Table */
5156 map.name_table_string_offs_reordered = addr;
5157 addr += map.name_count * map.offset_size;
5158 map.name_table_entry_offs_reordered = addr;
5159 addr += map.name_count * map.offset_size;
5160
5161 const gdb_byte *abbrev_table_start = addr;
5162 for (;;)
5163 {
5164 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5165 addr += bytes_read;
5166 if (index_num == 0)
5167 break;
5168
5169 const auto insertpair
5170 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5171 if (!insertpair.second)
5172 {
5173 warning (_("Section .debug_names in %s has duplicate index %s, "
5174 "ignoring .debug_names."),
5175 filename, pulongest (index_num));
5176 return false;
5177 }
5178 mapped_debug_names::index_val &indexval = insertpair.first->second;
5179 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5180 addr += bytes_read;
5181
5182 for (;;)
5183 {
5184 mapped_debug_names::index_val::attr attr;
5185 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5186 addr += bytes_read;
5187 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5188 addr += bytes_read;
5189 if (attr.form == DW_FORM_implicit_const)
5190 {
5191 attr.implicit_const = read_signed_leb128 (abfd, addr,
5192 &bytes_read);
5193 addr += bytes_read;
5194 }
5195 if (attr.dw_idx == 0 && attr.form == 0)
5196 break;
5197 indexval.attr_vec.push_back (std::move (attr));
5198 }
5199 }
5200 if (addr != abbrev_table_start + abbrev_table_size)
5201 {
5202 warning (_("Section .debug_names in %s has abbreviation_table "
5203 "of size %s vs. written as %u, ignoring .debug_names."),
5204 filename, plongest (addr - abbrev_table_start),
5205 abbrev_table_size);
5206 return false;
5207 }
5208 map.entry_pool = addr;
5209
5210 return true;
5211 }
5212
5213 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5214 list. */
5215
5216 static void
5217 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
5218 const mapped_debug_names &map,
5219 dwarf2_section_info &section,
5220 bool is_dwz)
5221 {
5222 if (!map.augmentation_is_gdb)
5223 {
5224 for (uint32_t i = 0; i < map.cu_count; ++i)
5225 {
5226 sect_offset sect_off
5227 = (sect_offset) (extract_unsigned_integer
5228 (map.cu_table_reordered + i * map.offset_size,
5229 map.offset_size,
5230 map.dwarf5_byte_order));
5231 /* We don't know the length of the CU, because the CU list in a
5232 .debug_names index can be incomplete, so we can't use the start
5233 of the next CU as end of this CU. We create the CUs here with
5234 length 0, and in cutu_reader::cutu_reader we'll fill in the
5235 actual length. */
5236 dwarf2_per_cu_data *per_cu
5237 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5238 sect_off, 0);
5239 per_bfd->all_comp_units.push_back (per_cu);
5240 }
5241 return;
5242 }
5243
5244 sect_offset sect_off_prev;
5245 for (uint32_t i = 0; i <= map.cu_count; ++i)
5246 {
5247 sect_offset sect_off_next;
5248 if (i < map.cu_count)
5249 {
5250 sect_off_next
5251 = (sect_offset) (extract_unsigned_integer
5252 (map.cu_table_reordered + i * map.offset_size,
5253 map.offset_size,
5254 map.dwarf5_byte_order));
5255 }
5256 else
5257 sect_off_next = (sect_offset) section.size;
5258 if (i >= 1)
5259 {
5260 const ULONGEST length = sect_off_next - sect_off_prev;
5261 dwarf2_per_cu_data *per_cu
5262 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5263 sect_off_prev, length);
5264 per_bfd->all_comp_units.push_back (per_cu);
5265 }
5266 sect_off_prev = sect_off_next;
5267 }
5268 }
5269
5270 /* Read the CU list from the mapped index, and use it to create all
5271 the CU objects for this dwarf2_per_objfile. */
5272
5273 static void
5274 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
5275 const mapped_debug_names &map,
5276 const mapped_debug_names &dwz_map)
5277 {
5278 gdb_assert (per_bfd->all_comp_units.empty ());
5279 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5280
5281 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
5282 false /* is_dwz */);
5283
5284 if (dwz_map.cu_count == 0)
5285 return;
5286
5287 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5288 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
5289 true /* is_dwz */);
5290 }
5291
5292 /* Read .debug_names. If everything went ok, initialize the "quick"
5293 elements of all the CUs and return true. Otherwise, return false. */
5294
5295 static bool
5296 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
5297 {
5298 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5299 mapped_debug_names dwz_map;
5300 struct objfile *objfile = per_objfile->objfile;
5301 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5302
5303 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5304 &per_objfile->per_bfd->debug_names, *map))
5305 return false;
5306
5307 /* Don't use the index if it's empty. */
5308 if (map->name_count == 0)
5309 return false;
5310
5311 /* If there is a .dwz file, read it so we can get its CU list as
5312 well. */
5313 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5314 if (dwz != NULL)
5315 {
5316 if (!read_debug_names_from_section (objfile,
5317 bfd_get_filename (dwz->dwz_bfd.get ()),
5318 &dwz->debug_names, dwz_map))
5319 {
5320 warning (_("could not read '.debug_names' section from %s; skipping"),
5321 bfd_get_filename (dwz->dwz_bfd.get ()));
5322 return false;
5323 }
5324 }
5325
5326 create_cus_from_debug_names (per_bfd, *map, dwz_map);
5327
5328 if (map->tu_count != 0)
5329 {
5330 /* We can only handle a single .debug_types when we have an
5331 index. */
5332 if (per_bfd->types.size () != 1)
5333 return false;
5334
5335 dwarf2_section_info *section = &per_bfd->types[0];
5336
5337 create_signatured_type_table_from_debug_names
5338 (per_objfile, *map, section, &per_bfd->abbrev);
5339 }
5340
5341 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
5342
5343 per_bfd->debug_names_table = std::move (map);
5344 per_bfd->using_index = 1;
5345 per_bfd->quick_file_names_table =
5346 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
5347
5348 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
5349 objfiles using the same BFD. */
5350 gdb_assert (per_bfd->partial_symtabs == nullptr);
5351 per_bfd->partial_symtabs = objfile->partial_symtabs;
5352
5353 return true;
5354 }
5355
5356 /* Type used to manage iterating over all CUs looking for a symbol for
5357 .debug_names. */
5358
5359 class dw2_debug_names_iterator
5360 {
5361 public:
5362 dw2_debug_names_iterator (const mapped_debug_names &map,
5363 gdb::optional<block_enum> block_index,
5364 domain_enum domain,
5365 const char *name, dwarf2_per_objfile *per_objfile)
5366 : m_map (map), m_block_index (block_index), m_domain (domain),
5367 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5368 m_per_objfile (per_objfile)
5369 {}
5370
5371 dw2_debug_names_iterator (const mapped_debug_names &map,
5372 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
5373 : m_map (map),
5374 m_search (search),
5375 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5376 m_per_objfile (per_objfile)
5377 {}
5378
5379 dw2_debug_names_iterator (const mapped_debug_names &map,
5380 block_enum block_index, domain_enum domain,
5381 uint32_t namei, dwarf2_per_objfile *per_objfile)
5382 : m_map (map), m_block_index (block_index), m_domain (domain),
5383 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5384 m_per_objfile (per_objfile)
5385 {}
5386
5387 /* Return the next matching CU or NULL if there are no more. */
5388 dwarf2_per_cu_data *next ();
5389
5390 private:
5391 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5392 const char *name,
5393 dwarf2_per_objfile *per_objfile);
5394 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5395 uint32_t namei,
5396 dwarf2_per_objfile *per_objfile);
5397
5398 /* The internalized form of .debug_names. */
5399 const mapped_debug_names &m_map;
5400
5401 /* If set, only look for symbols that match that block. Valid values are
5402 GLOBAL_BLOCK and STATIC_BLOCK. */
5403 const gdb::optional<block_enum> m_block_index;
5404
5405 /* The kind of symbol we're looking for. */
5406 const domain_enum m_domain = UNDEF_DOMAIN;
5407 const search_domain m_search = ALL_DOMAIN;
5408
5409 /* The list of CUs from the index entry of the symbol, or NULL if
5410 not found. */
5411 const gdb_byte *m_addr;
5412
5413 dwarf2_per_objfile *m_per_objfile;
5414 };
5415
5416 const char *
5417 mapped_debug_names::namei_to_name
5418 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
5419 {
5420 const ULONGEST namei_string_offs
5421 = extract_unsigned_integer ((name_table_string_offs_reordered
5422 + namei * offset_size),
5423 offset_size,
5424 dwarf5_byte_order);
5425 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
5426 }
5427
5428 /* Find a slot in .debug_names for the object named NAME. If NAME is
5429 found, return pointer to its pool data. If NAME cannot be found,
5430 return NULL. */
5431
5432 const gdb_byte *
5433 dw2_debug_names_iterator::find_vec_in_debug_names
5434 (const mapped_debug_names &map, const char *name,
5435 dwarf2_per_objfile *per_objfile)
5436 {
5437 int (*cmp) (const char *, const char *);
5438
5439 gdb::unique_xmalloc_ptr<char> without_params;
5440 if (current_language->la_language == language_cplus
5441 || current_language->la_language == language_fortran
5442 || current_language->la_language == language_d)
5443 {
5444 /* NAME is already canonical. Drop any qualifiers as
5445 .debug_names does not contain any. */
5446
5447 if (strchr (name, '(') != NULL)
5448 {
5449 without_params = cp_remove_params (name);
5450 if (without_params != NULL)
5451 name = without_params.get ();
5452 }
5453 }
5454
5455 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5456
5457 const uint32_t full_hash = dwarf5_djb_hash (name);
5458 uint32_t namei
5459 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5460 (map.bucket_table_reordered
5461 + (full_hash % map.bucket_count)), 4,
5462 map.dwarf5_byte_order);
5463 if (namei == 0)
5464 return NULL;
5465 --namei;
5466 if (namei >= map.name_count)
5467 {
5468 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5469 "[in module %s]"),
5470 namei, map.name_count,
5471 objfile_name (per_objfile->objfile));
5472 return NULL;
5473 }
5474
5475 for (;;)
5476 {
5477 const uint32_t namei_full_hash
5478 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5479 (map.hash_table_reordered + namei), 4,
5480 map.dwarf5_byte_order);
5481 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5482 return NULL;
5483
5484 if (full_hash == namei_full_hash)
5485 {
5486 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5487
5488 #if 0 /* An expensive sanity check. */
5489 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5490 {
5491 complaint (_("Wrong .debug_names hash for string at index %u "
5492 "[in module %s]"),
5493 namei, objfile_name (dwarf2_per_objfile->objfile));
5494 return NULL;
5495 }
5496 #endif
5497
5498 if (cmp (namei_string, name) == 0)
5499 {
5500 const ULONGEST namei_entry_offs
5501 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5502 + namei * map.offset_size),
5503 map.offset_size, map.dwarf5_byte_order);
5504 return map.entry_pool + namei_entry_offs;
5505 }
5506 }
5507
5508 ++namei;
5509 if (namei >= map.name_count)
5510 return NULL;
5511 }
5512 }
5513
5514 const gdb_byte *
5515 dw2_debug_names_iterator::find_vec_in_debug_names
5516 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5517 {
5518 if (namei >= map.name_count)
5519 {
5520 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5521 "[in module %s]"),
5522 namei, map.name_count,
5523 objfile_name (per_objfile->objfile));
5524 return NULL;
5525 }
5526
5527 const ULONGEST namei_entry_offs
5528 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5529 + namei * map.offset_size),
5530 map.offset_size, map.dwarf5_byte_order);
5531 return map.entry_pool + namei_entry_offs;
5532 }
5533
5534 /* See dw2_debug_names_iterator. */
5535
5536 dwarf2_per_cu_data *
5537 dw2_debug_names_iterator::next ()
5538 {
5539 if (m_addr == NULL)
5540 return NULL;
5541
5542 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5543 struct objfile *objfile = m_per_objfile->objfile;
5544 bfd *const abfd = objfile->obfd;
5545
5546 again:
5547
5548 unsigned int bytes_read;
5549 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5550 m_addr += bytes_read;
5551 if (abbrev == 0)
5552 return NULL;
5553
5554 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5555 if (indexval_it == m_map.abbrev_map.cend ())
5556 {
5557 complaint (_("Wrong .debug_names undefined abbrev code %s "
5558 "[in module %s]"),
5559 pulongest (abbrev), objfile_name (objfile));
5560 return NULL;
5561 }
5562 const mapped_debug_names::index_val &indexval = indexval_it->second;
5563 enum class symbol_linkage {
5564 unknown,
5565 static_,
5566 extern_,
5567 } symbol_linkage_ = symbol_linkage::unknown;
5568 dwarf2_per_cu_data *per_cu = NULL;
5569 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5570 {
5571 ULONGEST ull;
5572 switch (attr.form)
5573 {
5574 case DW_FORM_implicit_const:
5575 ull = attr.implicit_const;
5576 break;
5577 case DW_FORM_flag_present:
5578 ull = 1;
5579 break;
5580 case DW_FORM_udata:
5581 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5582 m_addr += bytes_read;
5583 break;
5584 case DW_FORM_ref4:
5585 ull = read_4_bytes (abfd, m_addr);
5586 m_addr += 4;
5587 break;
5588 case DW_FORM_ref8:
5589 ull = read_8_bytes (abfd, m_addr);
5590 m_addr += 8;
5591 break;
5592 case DW_FORM_ref_sig8:
5593 ull = read_8_bytes (abfd, m_addr);
5594 m_addr += 8;
5595 break;
5596 default:
5597 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5598 dwarf_form_name (attr.form),
5599 objfile_name (objfile));
5600 return NULL;
5601 }
5602 switch (attr.dw_idx)
5603 {
5604 case DW_IDX_compile_unit:
5605 /* Don't crash on bad data. */
5606 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
5607 {
5608 complaint (_(".debug_names entry has bad CU index %s"
5609 " [in module %s]"),
5610 pulongest (ull),
5611 objfile_name (objfile));
5612 continue;
5613 }
5614 per_cu = per_bfd->get_cutu (ull);
5615 break;
5616 case DW_IDX_type_unit:
5617 /* Don't crash on bad data. */
5618 if (ull >= per_bfd->all_type_units.size ())
5619 {
5620 complaint (_(".debug_names entry has bad TU index %s"
5621 " [in module %s]"),
5622 pulongest (ull),
5623 objfile_name (objfile));
5624 continue;
5625 }
5626 per_cu = &per_bfd->get_tu (ull)->per_cu;
5627 break;
5628 case DW_IDX_die_offset:
5629 /* In a per-CU index (as opposed to a per-module index), index
5630 entries without CU attribute implicitly refer to the single CU. */
5631 if (per_cu == NULL)
5632 per_cu = per_bfd->get_cu (0);
5633 break;
5634 case DW_IDX_GNU_internal:
5635 if (!m_map.augmentation_is_gdb)
5636 break;
5637 symbol_linkage_ = symbol_linkage::static_;
5638 break;
5639 case DW_IDX_GNU_external:
5640 if (!m_map.augmentation_is_gdb)
5641 break;
5642 symbol_linkage_ = symbol_linkage::extern_;
5643 break;
5644 }
5645 }
5646
5647 /* Skip if already read in. */
5648 if (m_per_objfile->symtab_set_p (per_cu))
5649 goto again;
5650
5651 /* Check static vs global. */
5652 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5653 {
5654 const bool want_static = *m_block_index == STATIC_BLOCK;
5655 const bool symbol_is_static =
5656 symbol_linkage_ == symbol_linkage::static_;
5657 if (want_static != symbol_is_static)
5658 goto again;
5659 }
5660
5661 /* Match dw2_symtab_iter_next, symbol_kind
5662 and debug_names::psymbol_tag. */
5663 switch (m_domain)
5664 {
5665 case VAR_DOMAIN:
5666 switch (indexval.dwarf_tag)
5667 {
5668 case DW_TAG_variable:
5669 case DW_TAG_subprogram:
5670 /* Some types are also in VAR_DOMAIN. */
5671 case DW_TAG_typedef:
5672 case DW_TAG_structure_type:
5673 break;
5674 default:
5675 goto again;
5676 }
5677 break;
5678 case STRUCT_DOMAIN:
5679 switch (indexval.dwarf_tag)
5680 {
5681 case DW_TAG_typedef:
5682 case DW_TAG_structure_type:
5683 break;
5684 default:
5685 goto again;
5686 }
5687 break;
5688 case LABEL_DOMAIN:
5689 switch (indexval.dwarf_tag)
5690 {
5691 case 0:
5692 case DW_TAG_variable:
5693 break;
5694 default:
5695 goto again;
5696 }
5697 break;
5698 case MODULE_DOMAIN:
5699 switch (indexval.dwarf_tag)
5700 {
5701 case DW_TAG_module:
5702 break;
5703 default:
5704 goto again;
5705 }
5706 break;
5707 default:
5708 break;
5709 }
5710
5711 /* Match dw2_expand_symtabs_matching, symbol_kind and
5712 debug_names::psymbol_tag. */
5713 switch (m_search)
5714 {
5715 case VARIABLES_DOMAIN:
5716 switch (indexval.dwarf_tag)
5717 {
5718 case DW_TAG_variable:
5719 break;
5720 default:
5721 goto again;
5722 }
5723 break;
5724 case FUNCTIONS_DOMAIN:
5725 switch (indexval.dwarf_tag)
5726 {
5727 case DW_TAG_subprogram:
5728 break;
5729 default:
5730 goto again;
5731 }
5732 break;
5733 case TYPES_DOMAIN:
5734 switch (indexval.dwarf_tag)
5735 {
5736 case DW_TAG_typedef:
5737 case DW_TAG_structure_type:
5738 break;
5739 default:
5740 goto again;
5741 }
5742 break;
5743 case MODULES_DOMAIN:
5744 switch (indexval.dwarf_tag)
5745 {
5746 case DW_TAG_module:
5747 break;
5748 default:
5749 goto again;
5750 }
5751 default:
5752 break;
5753 }
5754
5755 return per_cu;
5756 }
5757
5758 struct compunit_symtab *
5759 dwarf2_debug_names_index::lookup_symbol
5760 (struct objfile *objfile, block_enum block_index,
5761 const char *name, domain_enum domain)
5762 {
5763 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5764
5765 const auto &mapp = per_objfile->per_bfd->debug_names_table;
5766 if (!mapp)
5767 {
5768 /* index is NULL if OBJF_READNOW. */
5769 return NULL;
5770 }
5771 const auto &map = *mapp;
5772
5773 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
5774
5775 struct compunit_symtab *stab_best = NULL;
5776 struct dwarf2_per_cu_data *per_cu;
5777 while ((per_cu = iter.next ()) != NULL)
5778 {
5779 struct symbol *sym, *with_opaque = NULL;
5780 compunit_symtab *stab
5781 = dw2_instantiate_symtab (per_cu, per_objfile, false);
5782 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5783 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5784
5785 sym = block_find_symbol (block, name, domain,
5786 block_find_non_opaque_type_preferred,
5787 &with_opaque);
5788
5789 /* Some caution must be observed with overloaded functions and
5790 methods, since the index will not contain any overload
5791 information (but NAME might contain it). */
5792
5793 if (sym != NULL
5794 && strcmp_iw (sym->search_name (), name) == 0)
5795 return stab;
5796 if (with_opaque != NULL
5797 && strcmp_iw (with_opaque->search_name (), name) == 0)
5798 stab_best = stab;
5799
5800 /* Keep looking through other CUs. */
5801 }
5802
5803 return stab_best;
5804 }
5805
5806 /* This dumps minimal information about .debug_names. It is called
5807 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5808 uses this to verify that .debug_names has been loaded. */
5809
5810 void
5811 dwarf2_debug_names_index::dump (struct objfile *objfile)
5812 {
5813 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5814
5815 gdb_assert (per_objfile->per_bfd->using_index);
5816 printf_filtered (".debug_names:");
5817 if (per_objfile->per_bfd->debug_names_table)
5818 printf_filtered (" exists\n");
5819 else
5820 printf_filtered (" faked for \"readnow\"\n");
5821 printf_filtered ("\n");
5822 }
5823
5824 void
5825 dwarf2_debug_names_index::expand_symtabs_for_function
5826 (struct objfile *objfile, const char *func_name)
5827 {
5828 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5829
5830 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5831 if (per_objfile->per_bfd->debug_names_table)
5832 {
5833 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5834
5835 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
5836 per_objfile);
5837
5838 struct dwarf2_per_cu_data *per_cu;
5839 while ((per_cu = iter.next ()) != NULL)
5840 dw2_instantiate_symtab (per_cu, per_objfile, false);
5841 }
5842 }
5843
5844 void
5845 dwarf2_debug_names_index::map_matching_symbols
5846 (struct objfile *objfile,
5847 const lookup_name_info &name, domain_enum domain,
5848 int global,
5849 gdb::function_view<symbol_found_callback_ftype> callback,
5850 symbol_compare_ftype *ordered_compare)
5851 {
5852 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5853
5854 /* debug_names_table is NULL if OBJF_READNOW. */
5855 if (!per_objfile->per_bfd->debug_names_table)
5856 return;
5857
5858 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5859 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5860
5861 const char *match_name = name.ada ().lookup_name ().c_str ();
5862 auto matcher = [&] (const char *symname)
5863 {
5864 if (ordered_compare == nullptr)
5865 return true;
5866 return ordered_compare (symname, match_name) == 0;
5867 };
5868
5869 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5870 [&] (offset_type namei)
5871 {
5872 /* The name was matched, now expand corresponding CUs that were
5873 marked. */
5874 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
5875 per_objfile);
5876
5877 struct dwarf2_per_cu_data *per_cu;
5878 while ((per_cu = iter.next ()) != NULL)
5879 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5880 nullptr);
5881 return true;
5882 }, per_objfile);
5883
5884 /* It's a shame we couldn't do this inside the
5885 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5886 that have already been expanded. Instead, this loop matches what
5887 the psymtab code does. */
5888 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5889 {
5890 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
5891 if (symtab != nullptr)
5892 {
5893 const struct block *block
5894 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5895 if (!iterate_over_symbols_terminated (block, name,
5896 domain, callback))
5897 break;
5898 }
5899 }
5900 }
5901
5902 void
5903 dwarf2_debug_names_index::expand_symtabs_matching
5904 (struct objfile *objfile,
5905 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5906 const lookup_name_info *lookup_name,
5907 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5908 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5909 enum search_domain kind)
5910 {
5911 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5912
5913 /* debug_names_table is NULL if OBJF_READNOW. */
5914 if (!per_objfile->per_bfd->debug_names_table)
5915 return;
5916
5917 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5918
5919 if (symbol_matcher == NULL && lookup_name == NULL)
5920 {
5921 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5922 {
5923 QUIT;
5924
5925 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5926 expansion_notify);
5927 }
5928 return;
5929 }
5930
5931 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5932
5933 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5934 symbol_matcher,
5935 kind, [&] (offset_type namei)
5936 {
5937 /* The name was matched, now expand corresponding CUs that were
5938 marked. */
5939 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
5940
5941 struct dwarf2_per_cu_data *per_cu;
5942 while ((per_cu = iter.next ()) != NULL)
5943 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5944 expansion_notify);
5945 return true;
5946 }, per_objfile);
5947 }
5948
5949 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5950 to either a dwarf2_per_bfd or dwz_file object. */
5951
5952 template <typename T>
5953 static gdb::array_view<const gdb_byte>
5954 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5955 {
5956 dwarf2_section_info *section = &section_owner->gdb_index;
5957
5958 if (section->empty ())
5959 return {};
5960
5961 /* Older elfutils strip versions could keep the section in the main
5962 executable while splitting it for the separate debug info file. */
5963 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5964 return {};
5965
5966 section->read (obj);
5967
5968 /* dwarf2_section_info::size is a bfd_size_type, while
5969 gdb::array_view works with size_t. On 32-bit hosts, with
5970 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5971 is 32-bit. So we need an explicit narrowing conversion here.
5972 This is fine, because it's impossible to allocate or mmap an
5973 array/buffer larger than what size_t can represent. */
5974 return gdb::make_array_view (section->buffer, section->size);
5975 }
5976
5977 /* Lookup the index cache for the contents of the index associated to
5978 DWARF2_OBJ. */
5979
5980 static gdb::array_view<const gdb_byte>
5981 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5982 {
5983 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5984 if (build_id == nullptr)
5985 return {};
5986
5987 return global_index_cache.lookup_gdb_index (build_id,
5988 &dwarf2_per_bfd->index_cache_res);
5989 }
5990
5991 /* Same as the above, but for DWZ. */
5992
5993 static gdb::array_view<const gdb_byte>
5994 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5995 {
5996 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5997 if (build_id == nullptr)
5998 return {};
5999
6000 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6001 }
6002
6003 /* See symfile.h. */
6004
6005 bool
6006 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6007 {
6008 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6009 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6010
6011 dwarf_read_debug_printf ("called");
6012
6013 /* If we're about to read full symbols, don't bother with the
6014 indices. In this case we also don't care if some other debug
6015 format is making psymtabs, because they are all about to be
6016 expanded anyway. */
6017 if ((objfile->flags & OBJF_READNOW))
6018 {
6019 dwarf_read_debug_printf ("readnow requested");
6020
6021 /* When using READNOW, the using_index flag (set below) indicates that
6022 PER_BFD was already initialized, when we loaded some other objfile. */
6023 if (per_bfd->using_index)
6024 {
6025 dwarf_read_debug_printf ("using_index already set");
6026 *index_kind = dw_index_kind::GDB_INDEX;
6027 per_objfile->resize_symtabs ();
6028 return true;
6029 }
6030
6031 per_bfd->using_index = 1;
6032 create_all_comp_units (per_objfile);
6033 create_all_type_units (per_objfile);
6034 per_bfd->quick_file_names_table
6035 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
6036 per_objfile->resize_symtabs ();
6037
6038 for (int i = 0; i < (per_bfd->all_comp_units.size ()
6039 + per_bfd->all_type_units.size ()); ++i)
6040 {
6041 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
6042
6043 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6044 struct dwarf2_per_cu_quick_data);
6045 }
6046
6047 /* Return 1 so that gdb sees the "quick" functions. However,
6048 these functions will be no-ops because we will have expanded
6049 all symtabs. */
6050 *index_kind = dw_index_kind::GDB_INDEX;
6051 return true;
6052 }
6053
6054 /* Was a debug names index already read when we processed an objfile sharing
6055 PER_BFD? */
6056 if (per_bfd->debug_names_table != nullptr)
6057 {
6058 dwarf_read_debug_printf ("re-using shared debug names table");
6059 *index_kind = dw_index_kind::DEBUG_NAMES;
6060 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
6061 per_objfile->resize_symtabs ();
6062 return true;
6063 }
6064
6065 /* Was a GDB index already read when we processed an objfile sharing
6066 PER_BFD? */
6067 if (per_bfd->index_table != nullptr)
6068 {
6069 dwarf_read_debug_printf ("re-using shared index table");
6070 *index_kind = dw_index_kind::GDB_INDEX;
6071 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
6072 per_objfile->resize_symtabs ();
6073 return true;
6074 }
6075
6076 /* There might already be partial symtabs built for this BFD. This happens
6077 when loading the same binary twice with the index-cache enabled. If so,
6078 don't try to read an index. The objfile / per_objfile initialization will
6079 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
6080 code path. */
6081 if (per_bfd->partial_symtabs != nullptr)
6082 {
6083 dwarf_read_debug_printf ("re-using shared partial symtabs");
6084 return false;
6085 }
6086
6087 if (dwarf2_read_debug_names (per_objfile))
6088 {
6089 dwarf_read_debug_printf ("found debug names");
6090 *index_kind = dw_index_kind::DEBUG_NAMES;
6091 per_objfile->resize_symtabs ();
6092 return true;
6093 }
6094
6095 if (dwarf2_read_gdb_index (per_objfile,
6096 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
6097 get_gdb_index_contents_from_section<dwz_file>))
6098 {
6099 dwarf_read_debug_printf ("found gdb index from file");
6100 *index_kind = dw_index_kind::GDB_INDEX;
6101 per_objfile->resize_symtabs ();
6102 return true;
6103 }
6104
6105 /* ... otherwise, try to find the index in the index cache. */
6106 if (dwarf2_read_gdb_index (per_objfile,
6107 get_gdb_index_contents_from_cache,
6108 get_gdb_index_contents_from_cache_dwz))
6109 {
6110 dwarf_read_debug_printf ("found gdb index from cache");
6111 global_index_cache.hit ();
6112 *index_kind = dw_index_kind::GDB_INDEX;
6113 per_objfile->resize_symtabs ();
6114 return true;
6115 }
6116
6117 global_index_cache.miss ();
6118 return false;
6119 }
6120
6121 \f
6122
6123 /* Build a partial symbol table. */
6124
6125 void
6126 dwarf2_build_psymtabs (struct objfile *objfile)
6127 {
6128 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6129 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6130
6131 if (per_bfd->partial_symtabs != nullptr)
6132 {
6133 /* Partial symbols were already read, so now we can simply
6134 attach them. */
6135 objfile->partial_symtabs = per_bfd->partial_symtabs;
6136 per_objfile->resize_symtabs ();
6137 return;
6138 }
6139
6140 try
6141 {
6142 /* This isn't really ideal: all the data we allocate on the
6143 objfile's obstack is still uselessly kept around. However,
6144 freeing it seems unsafe. */
6145 psymtab_discarder psymtabs (objfile);
6146 dwarf2_build_psymtabs_hard (per_objfile);
6147 psymtabs.keep ();
6148
6149 per_objfile->resize_symtabs ();
6150
6151 /* (maybe) store an index in the cache. */
6152 global_index_cache.store (per_objfile);
6153 }
6154 catch (const gdb_exception_error &except)
6155 {
6156 exception_print (gdb_stderr, except);
6157 }
6158
6159 /* Finish by setting the local reference to partial symtabs, so that
6160 we don't try to read them again if reading another objfile with the same
6161 BFD. If we can't in fact share, this won't make a difference anyway as
6162 the dwarf2_per_bfd object won't be shared. */
6163 per_bfd->partial_symtabs = objfile->partial_symtabs;
6164 }
6165
6166 /* Find the base address of the compilation unit for range lists and
6167 location lists. It will normally be specified by DW_AT_low_pc.
6168 In DWARF-3 draft 4, the base address could be overridden by
6169 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6170 compilation units with discontinuous ranges. */
6171
6172 static void
6173 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6174 {
6175 struct attribute *attr;
6176
6177 cu->base_address.reset ();
6178
6179 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6180 if (attr != nullptr)
6181 cu->base_address = attr->as_address ();
6182 else
6183 {
6184 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6185 if (attr != nullptr)
6186 cu->base_address = attr->as_address ();
6187 }
6188 }
6189
6190 /* Helper function that returns the proper abbrev section for
6191 THIS_CU. */
6192
6193 static struct dwarf2_section_info *
6194 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6195 {
6196 struct dwarf2_section_info *abbrev;
6197 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6198
6199 if (this_cu->is_dwz)
6200 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
6201 else
6202 abbrev = &per_bfd->abbrev;
6203
6204 return abbrev;
6205 }
6206
6207 /* Fetch the abbreviation table offset from a comp or type unit header. */
6208
6209 static sect_offset
6210 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
6211 struct dwarf2_section_info *section,
6212 sect_offset sect_off)
6213 {
6214 bfd *abfd = section->get_bfd_owner ();
6215 const gdb_byte *info_ptr;
6216 unsigned int initial_length_size, offset_size;
6217 uint16_t version;
6218
6219 section->read (per_objfile->objfile);
6220 info_ptr = section->buffer + to_underlying (sect_off);
6221 read_initial_length (abfd, info_ptr, &initial_length_size);
6222 offset_size = initial_length_size == 4 ? 4 : 8;
6223 info_ptr += initial_length_size;
6224
6225 version = read_2_bytes (abfd, info_ptr);
6226 info_ptr += 2;
6227 if (version >= 5)
6228 {
6229 /* Skip unit type and address size. */
6230 info_ptr += 2;
6231 }
6232
6233 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6234 }
6235
6236 /* A partial symtab that is used only for include files. */
6237 struct dwarf2_include_psymtab : public partial_symtab
6238 {
6239 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6240 : partial_symtab (filename, objfile)
6241 {
6242 }
6243
6244 void read_symtab (struct objfile *objfile) override
6245 {
6246 /* It's an include file, no symbols to read for it.
6247 Everything is in the includer symtab. */
6248
6249 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6250 expansion of the includer psymtab. We use the dependencies[0] field to
6251 model the includer. But if we go the regular route of calling
6252 expand_psymtab here, and having expand_psymtab call expand_dependencies
6253 to expand the includer, we'll only use expand_psymtab on the includer
6254 (making it a non-toplevel psymtab), while if we expand the includer via
6255 another path, we'll use read_symtab (making it a toplevel psymtab).
6256 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6257 psymtab, and trigger read_symtab on the includer here directly. */
6258 includer ()->read_symtab (objfile);
6259 }
6260
6261 void expand_psymtab (struct objfile *objfile) override
6262 {
6263 /* This is not called by read_symtab, and should not be called by any
6264 expand_dependencies. */
6265 gdb_assert (false);
6266 }
6267
6268 bool readin_p (struct objfile *objfile) const override
6269 {
6270 return includer ()->readin_p (objfile);
6271 }
6272
6273 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6274 {
6275 return nullptr;
6276 }
6277
6278 private:
6279 partial_symtab *includer () const
6280 {
6281 /* An include psymtab has exactly one dependency: the psymtab that
6282 includes it. */
6283 gdb_assert (this->number_of_dependencies == 1);
6284 return this->dependencies[0];
6285 }
6286 };
6287
6288 /* Allocate a new partial symtab for file named NAME and mark this new
6289 partial symtab as being an include of PST. */
6290
6291 static void
6292 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6293 struct objfile *objfile)
6294 {
6295 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6296
6297 if (!IS_ABSOLUTE_PATH (subpst->filename))
6298 subpst->dirname = pst->dirname;
6299
6300 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6301 subpst->dependencies[0] = pst;
6302 subpst->number_of_dependencies = 1;
6303 }
6304
6305 /* Read the Line Number Program data and extract the list of files
6306 included by the source file represented by PST. Build an include
6307 partial symtab for each of these included files. */
6308
6309 static void
6310 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6311 struct die_info *die,
6312 dwarf2_psymtab *pst)
6313 {
6314 line_header_up lh;
6315 struct attribute *attr;
6316
6317 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6318 if (attr != nullptr && attr->form_is_unsigned ())
6319 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
6320 if (lh == NULL)
6321 return; /* No linetable, so no includes. */
6322
6323 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6324 that we pass in the raw text_low here; that is ok because we're
6325 only decoding the line table to make include partial symtabs, and
6326 so the addresses aren't really used. */
6327 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6328 pst->raw_text_low (), 1);
6329 }
6330
6331 static hashval_t
6332 hash_signatured_type (const void *item)
6333 {
6334 const struct signatured_type *sig_type
6335 = (const struct signatured_type *) item;
6336
6337 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6338 return sig_type->signature;
6339 }
6340
6341 static int
6342 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6343 {
6344 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6345 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6346
6347 return lhs->signature == rhs->signature;
6348 }
6349
6350 /* Allocate a hash table for signatured types. */
6351
6352 static htab_up
6353 allocate_signatured_type_table ()
6354 {
6355 return htab_up (htab_create_alloc (41,
6356 hash_signatured_type,
6357 eq_signatured_type,
6358 NULL, xcalloc, xfree));
6359 }
6360
6361 /* A helper function to add a signatured type CU to a table. */
6362
6363 static int
6364 add_signatured_type_cu_to_table (void **slot, void *datum)
6365 {
6366 struct signatured_type *sigt = (struct signatured_type *) *slot;
6367 std::vector<signatured_type *> *all_type_units
6368 = (std::vector<signatured_type *> *) datum;
6369
6370 all_type_units->push_back (sigt);
6371
6372 return 1;
6373 }
6374
6375 /* A helper for create_debug_types_hash_table. Read types from SECTION
6376 and fill them into TYPES_HTAB. It will process only type units,
6377 therefore DW_UT_type. */
6378
6379 static void
6380 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
6381 struct dwo_file *dwo_file,
6382 dwarf2_section_info *section, htab_up &types_htab,
6383 rcuh_kind section_kind)
6384 {
6385 struct objfile *objfile = per_objfile->objfile;
6386 struct dwarf2_section_info *abbrev_section;
6387 bfd *abfd;
6388 const gdb_byte *info_ptr, *end_ptr;
6389
6390 abbrev_section = (dwo_file != NULL
6391 ? &dwo_file->sections.abbrev
6392 : &per_objfile->per_bfd->abbrev);
6393
6394 dwarf_read_debug_printf ("Reading %s for %s",
6395 section->get_name (),
6396 abbrev_section->get_file_name ());
6397
6398 section->read (objfile);
6399 info_ptr = section->buffer;
6400
6401 if (info_ptr == NULL)
6402 return;
6403
6404 /* We can't set abfd until now because the section may be empty or
6405 not present, in which case the bfd is unknown. */
6406 abfd = section->get_bfd_owner ();
6407
6408 /* We don't use cutu_reader here because we don't need to read
6409 any dies: the signature is in the header. */
6410
6411 end_ptr = info_ptr + section->size;
6412 while (info_ptr < end_ptr)
6413 {
6414 struct signatured_type *sig_type;
6415 struct dwo_unit *dwo_tu;
6416 void **slot;
6417 const gdb_byte *ptr = info_ptr;
6418 struct comp_unit_head header;
6419 unsigned int length;
6420
6421 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6422
6423 /* Initialize it due to a false compiler warning. */
6424 header.signature = -1;
6425 header.type_cu_offset_in_tu = (cu_offset) -1;
6426
6427 /* We need to read the type's signature in order to build the hash
6428 table, but we don't need anything else just yet. */
6429
6430 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
6431 abbrev_section, ptr, section_kind);
6432
6433 length = header.get_length ();
6434
6435 /* Skip dummy type units. */
6436 if (ptr >= info_ptr + length
6437 || peek_abbrev_code (abfd, ptr) == 0
6438 || (header.unit_type != DW_UT_type
6439 && header.unit_type != DW_UT_split_type))
6440 {
6441 info_ptr += length;
6442 continue;
6443 }
6444
6445 if (types_htab == NULL)
6446 {
6447 if (dwo_file)
6448 types_htab = allocate_dwo_unit_table ();
6449 else
6450 types_htab = allocate_signatured_type_table ();
6451 }
6452
6453 if (dwo_file)
6454 {
6455 sig_type = NULL;
6456 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
6457 dwo_tu->dwo_file = dwo_file;
6458 dwo_tu->signature = header.signature;
6459 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6460 dwo_tu->section = section;
6461 dwo_tu->sect_off = sect_off;
6462 dwo_tu->length = length;
6463 }
6464 else
6465 {
6466 /* N.B.: type_offset is not usable if this type uses a DWO file.
6467 The real type_offset is in the DWO file. */
6468 dwo_tu = NULL;
6469 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6470 sig_type->signature = header.signature;
6471 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6472 sig_type->per_cu.is_debug_types = 1;
6473 sig_type->per_cu.section = section;
6474 sig_type->per_cu.sect_off = sect_off;
6475 sig_type->per_cu.length = length;
6476 }
6477
6478 slot = htab_find_slot (types_htab.get (),
6479 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6480 INSERT);
6481 gdb_assert (slot != NULL);
6482 if (*slot != NULL)
6483 {
6484 sect_offset dup_sect_off;
6485
6486 if (dwo_file)
6487 {
6488 const struct dwo_unit *dup_tu
6489 = (const struct dwo_unit *) *slot;
6490
6491 dup_sect_off = dup_tu->sect_off;
6492 }
6493 else
6494 {
6495 const struct signatured_type *dup_tu
6496 = (const struct signatured_type *) *slot;
6497
6498 dup_sect_off = dup_tu->per_cu.sect_off;
6499 }
6500
6501 complaint (_("debug type entry at offset %s is duplicate to"
6502 " the entry at offset %s, signature %s"),
6503 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6504 hex_string (header.signature));
6505 }
6506 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6507
6508 dwarf_read_debug_printf_v (" offset %s, signature %s",
6509 sect_offset_str (sect_off),
6510 hex_string (header.signature));
6511
6512 info_ptr += length;
6513 }
6514 }
6515
6516 /* Create the hash table of all entries in the .debug_types
6517 (or .debug_types.dwo) section(s).
6518 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6519 otherwise it is NULL.
6520
6521 The result is a pointer to the hash table or NULL if there are no types.
6522
6523 Note: This function processes DWO files only, not DWP files. */
6524
6525 static void
6526 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
6527 struct dwo_file *dwo_file,
6528 gdb::array_view<dwarf2_section_info> type_sections,
6529 htab_up &types_htab)
6530 {
6531 for (dwarf2_section_info &section : type_sections)
6532 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6533 rcuh_kind::TYPE);
6534 }
6535
6536 /* Create the hash table of all entries in the .debug_types section,
6537 and initialize all_type_units.
6538 The result is zero if there is an error (e.g. missing .debug_types section),
6539 otherwise non-zero. */
6540
6541 static int
6542 create_all_type_units (dwarf2_per_objfile *per_objfile)
6543 {
6544 htab_up types_htab;
6545
6546 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6547 types_htab, rcuh_kind::COMPILE);
6548 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6549 types_htab);
6550 if (types_htab == NULL)
6551 {
6552 per_objfile->per_bfd->signatured_types = NULL;
6553 return 0;
6554 }
6555
6556 per_objfile->per_bfd->signatured_types = std::move (types_htab);
6557
6558 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6559 per_objfile->per_bfd->all_type_units.reserve
6560 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
6561
6562 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
6563 add_signatured_type_cu_to_table,
6564 &per_objfile->per_bfd->all_type_units);
6565
6566 return 1;
6567 }
6568
6569 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6570 If SLOT is non-NULL, it is the entry to use in the hash table.
6571 Otherwise we find one. */
6572
6573 static struct signatured_type *
6574 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6575 {
6576 if (per_objfile->per_bfd->all_type_units.size ()
6577 == per_objfile->per_bfd->all_type_units.capacity ())
6578 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6579
6580 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6581
6582 per_objfile->resize_symtabs ();
6583
6584 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6585 sig_type->signature = sig;
6586 sig_type->per_cu.is_debug_types = 1;
6587 if (per_objfile->per_bfd->using_index)
6588 {
6589 sig_type->per_cu.v.quick =
6590 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6591 struct dwarf2_per_cu_quick_data);
6592 }
6593
6594 if (slot == NULL)
6595 {
6596 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6597 sig_type, INSERT);
6598 }
6599 gdb_assert (*slot == NULL);
6600 *slot = sig_type;
6601 /* The rest of sig_type must be filled in by the caller. */
6602 return sig_type;
6603 }
6604
6605 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6606 Fill in SIG_ENTRY with DWO_ENTRY. */
6607
6608 static void
6609 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
6610 struct signatured_type *sig_entry,
6611 struct dwo_unit *dwo_entry)
6612 {
6613 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6614
6615 /* Make sure we're not clobbering something we don't expect to. */
6616 gdb_assert (! sig_entry->per_cu.queued);
6617 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
6618 if (per_bfd->using_index)
6619 {
6620 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6621 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6622 }
6623 else
6624 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6625 gdb_assert (sig_entry->signature == dwo_entry->signature);
6626 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6627 gdb_assert (sig_entry->type_unit_group == NULL);
6628 gdb_assert (sig_entry->dwo_unit == NULL);
6629
6630 sig_entry->per_cu.section = dwo_entry->section;
6631 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6632 sig_entry->per_cu.length = dwo_entry->length;
6633 sig_entry->per_cu.reading_dwo_directly = 1;
6634 sig_entry->per_cu.per_bfd = per_bfd;
6635 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6636 sig_entry->dwo_unit = dwo_entry;
6637 }
6638
6639 /* Subroutine of lookup_signatured_type.
6640 If we haven't read the TU yet, create the signatured_type data structure
6641 for a TU to be read in directly from a DWO file, bypassing the stub.
6642 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6643 using .gdb_index, then when reading a CU we want to stay in the DWO file
6644 containing that CU. Otherwise we could end up reading several other DWO
6645 files (due to comdat folding) to process the transitive closure of all the
6646 mentioned TUs, and that can be slow. The current DWO file will have every
6647 type signature that it needs.
6648 We only do this for .gdb_index because in the psymtab case we already have
6649 to read all the DWOs to build the type unit groups. */
6650
6651 static struct signatured_type *
6652 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6653 {
6654 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6655 struct dwo_file *dwo_file;
6656 struct dwo_unit find_dwo_entry, *dwo_entry;
6657 struct signatured_type find_sig_entry, *sig_entry;
6658 void **slot;
6659
6660 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6661
6662 /* If TU skeletons have been removed then we may not have read in any
6663 TUs yet. */
6664 if (per_objfile->per_bfd->signatured_types == NULL)
6665 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6666
6667 /* We only ever need to read in one copy of a signatured type.
6668 Use the global signatured_types array to do our own comdat-folding
6669 of types. If this is the first time we're reading this TU, and
6670 the TU has an entry in .gdb_index, replace the recorded data from
6671 .gdb_index with this TU. */
6672
6673 find_sig_entry.signature = sig;
6674 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6675 &find_sig_entry, INSERT);
6676 sig_entry = (struct signatured_type *) *slot;
6677
6678 /* We can get here with the TU already read, *or* in the process of being
6679 read. Don't reassign the global entry to point to this DWO if that's
6680 the case. Also note that if the TU is already being read, it may not
6681 have come from a DWO, the program may be a mix of Fission-compiled
6682 code and non-Fission-compiled code. */
6683
6684 /* Have we already tried to read this TU?
6685 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6686 needn't exist in the global table yet). */
6687 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6688 return sig_entry;
6689
6690 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6691 dwo_unit of the TU itself. */
6692 dwo_file = cu->dwo_unit->dwo_file;
6693
6694 /* Ok, this is the first time we're reading this TU. */
6695 if (dwo_file->tus == NULL)
6696 return NULL;
6697 find_dwo_entry.signature = sig;
6698 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6699 &find_dwo_entry);
6700 if (dwo_entry == NULL)
6701 return NULL;
6702
6703 /* If the global table doesn't have an entry for this TU, add one. */
6704 if (sig_entry == NULL)
6705 sig_entry = add_type_unit (per_objfile, sig, slot);
6706
6707 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6708 sig_entry->per_cu.tu_read = 1;
6709 return sig_entry;
6710 }
6711
6712 /* Subroutine of lookup_signatured_type.
6713 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6714 then try the DWP file. If the TU stub (skeleton) has been removed then
6715 it won't be in .gdb_index. */
6716
6717 static struct signatured_type *
6718 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6719 {
6720 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6721 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6722 struct dwo_unit *dwo_entry;
6723 struct signatured_type find_sig_entry, *sig_entry;
6724 void **slot;
6725
6726 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6727 gdb_assert (dwp_file != NULL);
6728
6729 /* If TU skeletons have been removed then we may not have read in any
6730 TUs yet. */
6731 if (per_objfile->per_bfd->signatured_types == NULL)
6732 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6733
6734 find_sig_entry.signature = sig;
6735 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6736 &find_sig_entry, INSERT);
6737 sig_entry = (struct signatured_type *) *slot;
6738
6739 /* Have we already tried to read this TU?
6740 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6741 needn't exist in the global table yet). */
6742 if (sig_entry != NULL)
6743 return sig_entry;
6744
6745 if (dwp_file->tus == NULL)
6746 return NULL;
6747 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6748 1 /* is_debug_types */);
6749 if (dwo_entry == NULL)
6750 return NULL;
6751
6752 sig_entry = add_type_unit (per_objfile, sig, slot);
6753 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6754
6755 return sig_entry;
6756 }
6757
6758 /* Lookup a signature based type for DW_FORM_ref_sig8.
6759 Returns NULL if signature SIG is not present in the table.
6760 It is up to the caller to complain about this. */
6761
6762 static struct signatured_type *
6763 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6764 {
6765 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6766
6767 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6768 {
6769 /* We're in a DWO/DWP file, and we're using .gdb_index.
6770 These cases require special processing. */
6771 if (get_dwp_file (per_objfile) == NULL)
6772 return lookup_dwo_signatured_type (cu, sig);
6773 else
6774 return lookup_dwp_signatured_type (cu, sig);
6775 }
6776 else
6777 {
6778 struct signatured_type find_entry, *entry;
6779
6780 if (per_objfile->per_bfd->signatured_types == NULL)
6781 return NULL;
6782 find_entry.signature = sig;
6783 entry = ((struct signatured_type *)
6784 htab_find (per_objfile->per_bfd->signatured_types.get (),
6785 &find_entry));
6786 return entry;
6787 }
6788 }
6789
6790 /* Low level DIE reading support. */
6791
6792 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6793
6794 static void
6795 init_cu_die_reader (struct die_reader_specs *reader,
6796 struct dwarf2_cu *cu,
6797 struct dwarf2_section_info *section,
6798 struct dwo_file *dwo_file,
6799 struct abbrev_table *abbrev_table)
6800 {
6801 gdb_assert (section->readin && section->buffer != NULL);
6802 reader->abfd = section->get_bfd_owner ();
6803 reader->cu = cu;
6804 reader->dwo_file = dwo_file;
6805 reader->die_section = section;
6806 reader->buffer = section->buffer;
6807 reader->buffer_end = section->buffer + section->size;
6808 reader->abbrev_table = abbrev_table;
6809 }
6810
6811 /* Subroutine of cutu_reader to simplify it.
6812 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6813 There's just a lot of work to do, and cutu_reader is big enough
6814 already.
6815
6816 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6817 from it to the DIE in the DWO. If NULL we are skipping the stub.
6818 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6819 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6820 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6821 STUB_COMP_DIR may be non-NULL.
6822 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6823 are filled in with the info of the DIE from the DWO file.
6824 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6825 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6826 kept around for at least as long as *RESULT_READER.
6827
6828 The result is non-zero if a valid (non-dummy) DIE was found. */
6829
6830 static int
6831 read_cutu_die_from_dwo (dwarf2_cu *cu,
6832 struct dwo_unit *dwo_unit,
6833 struct die_info *stub_comp_unit_die,
6834 const char *stub_comp_dir,
6835 struct die_reader_specs *result_reader,
6836 const gdb_byte **result_info_ptr,
6837 struct die_info **result_comp_unit_die,
6838 abbrev_table_up *result_dwo_abbrev_table)
6839 {
6840 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6841 dwarf2_per_cu_data *per_cu = cu->per_cu;
6842 struct objfile *objfile = per_objfile->objfile;
6843 bfd *abfd;
6844 const gdb_byte *begin_info_ptr, *info_ptr;
6845 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6846 int i,num_extra_attrs;
6847 struct dwarf2_section_info *dwo_abbrev_section;
6848 struct die_info *comp_unit_die;
6849
6850 /* At most one of these may be provided. */
6851 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6852
6853 /* These attributes aren't processed until later:
6854 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6855 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6856 referenced later. However, these attributes are found in the stub
6857 which we won't have later. In order to not impose this complication
6858 on the rest of the code, we read them here and copy them to the
6859 DWO CU/TU die. */
6860
6861 stmt_list = NULL;
6862 low_pc = NULL;
6863 high_pc = NULL;
6864 ranges = NULL;
6865 comp_dir = NULL;
6866
6867 if (stub_comp_unit_die != NULL)
6868 {
6869 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6870 DWO file. */
6871 if (!per_cu->is_debug_types)
6872 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6873 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6874 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6875 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6876 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6877
6878 cu->addr_base = stub_comp_unit_die->addr_base ();
6879
6880 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6881 We need the value before we can process DW_AT_ranges values from the
6882 DWO. */
6883 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6884
6885 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6886 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6887 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6888 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6889 section. */
6890 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
6891 }
6892 else if (stub_comp_dir != NULL)
6893 {
6894 /* Reconstruct the comp_dir attribute to simplify the code below. */
6895 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
6896 comp_dir->name = DW_AT_comp_dir;
6897 comp_dir->form = DW_FORM_string;
6898 comp_dir->set_string_noncanonical (stub_comp_dir);
6899 }
6900
6901 /* Set up for reading the DWO CU/TU. */
6902 cu->dwo_unit = dwo_unit;
6903 dwarf2_section_info *section = dwo_unit->section;
6904 section->read (objfile);
6905 abfd = section->get_bfd_owner ();
6906 begin_info_ptr = info_ptr = (section->buffer
6907 + to_underlying (dwo_unit->sect_off));
6908 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6909
6910 if (per_cu->is_debug_types)
6911 {
6912 signatured_type *sig_type = (struct signatured_type *) per_cu;
6913
6914 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6915 section, dwo_abbrev_section,
6916 info_ptr, rcuh_kind::TYPE);
6917 /* This is not an assert because it can be caused by bad debug info. */
6918 if (sig_type->signature != cu->header.signature)
6919 {
6920 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6921 " TU at offset %s [in module %s]"),
6922 hex_string (sig_type->signature),
6923 hex_string (cu->header.signature),
6924 sect_offset_str (dwo_unit->sect_off),
6925 bfd_get_filename (abfd));
6926 }
6927 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6928 /* For DWOs coming from DWP files, we don't know the CU length
6929 nor the type's offset in the TU until now. */
6930 dwo_unit->length = cu->header.get_length ();
6931 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6932
6933 /* Establish the type offset that can be used to lookup the type.
6934 For DWO files, we don't know it until now. */
6935 sig_type->type_offset_in_section
6936 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6937 }
6938 else
6939 {
6940 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6941 section, dwo_abbrev_section,
6942 info_ptr, rcuh_kind::COMPILE);
6943 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6944 /* For DWOs coming from DWP files, we don't know the CU length
6945 until now. */
6946 dwo_unit->length = cu->header.get_length ();
6947 }
6948
6949 dwo_abbrev_section->read (objfile);
6950 *result_dwo_abbrev_table
6951 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
6952 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6953 result_dwo_abbrev_table->get ());
6954
6955 /* Read in the die, but leave space to copy over the attributes
6956 from the stub. This has the benefit of simplifying the rest of
6957 the code - all the work to maintain the illusion of a single
6958 DW_TAG_{compile,type}_unit DIE is done here. */
6959 num_extra_attrs = ((stmt_list != NULL)
6960 + (low_pc != NULL)
6961 + (high_pc != NULL)
6962 + (ranges != NULL)
6963 + (comp_dir != NULL));
6964 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6965 num_extra_attrs);
6966
6967 /* Copy over the attributes from the stub to the DIE we just read in. */
6968 comp_unit_die = *result_comp_unit_die;
6969 i = comp_unit_die->num_attrs;
6970 if (stmt_list != NULL)
6971 comp_unit_die->attrs[i++] = *stmt_list;
6972 if (low_pc != NULL)
6973 comp_unit_die->attrs[i++] = *low_pc;
6974 if (high_pc != NULL)
6975 comp_unit_die->attrs[i++] = *high_pc;
6976 if (ranges != NULL)
6977 comp_unit_die->attrs[i++] = *ranges;
6978 if (comp_dir != NULL)
6979 comp_unit_die->attrs[i++] = *comp_dir;
6980 comp_unit_die->num_attrs += num_extra_attrs;
6981
6982 if (dwarf_die_debug)
6983 {
6984 fprintf_unfiltered (gdb_stdlog,
6985 "Read die from %s@0x%x of %s:\n",
6986 section->get_name (),
6987 (unsigned) (begin_info_ptr - section->buffer),
6988 bfd_get_filename (abfd));
6989 dump_die (comp_unit_die, dwarf_die_debug);
6990 }
6991
6992 /* Skip dummy compilation units. */
6993 if (info_ptr >= begin_info_ptr + dwo_unit->length
6994 || peek_abbrev_code (abfd, info_ptr) == 0)
6995 return 0;
6996
6997 *result_info_ptr = info_ptr;
6998 return 1;
6999 }
7000
7001 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7002 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7003 signature is part of the header. */
7004 static gdb::optional<ULONGEST>
7005 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7006 {
7007 if (cu->header.version >= 5)
7008 return cu->header.signature;
7009 struct attribute *attr;
7010 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7011 if (attr == nullptr || !attr->form_is_unsigned ())
7012 return gdb::optional<ULONGEST> ();
7013 return attr->as_unsigned ();
7014 }
7015
7016 /* Subroutine of cutu_reader to simplify it.
7017 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7018 Returns NULL if the specified DWO unit cannot be found. */
7019
7020 static struct dwo_unit *
7021 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
7022 {
7023 dwarf2_per_cu_data *per_cu = cu->per_cu;
7024 struct dwo_unit *dwo_unit;
7025 const char *comp_dir;
7026
7027 gdb_assert (cu != NULL);
7028
7029 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7030 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7031 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7032
7033 if (per_cu->is_debug_types)
7034 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
7035 else
7036 {
7037 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7038
7039 if (!signature.has_value ())
7040 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7041 " [in module %s]"),
7042 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
7043
7044 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
7045 }
7046
7047 return dwo_unit;
7048 }
7049
7050 /* Subroutine of cutu_reader to simplify it.
7051 See it for a description of the parameters.
7052 Read a TU directly from a DWO file, bypassing the stub. */
7053
7054 void
7055 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
7056 dwarf2_per_objfile *per_objfile,
7057 dwarf2_cu *existing_cu)
7058 {
7059 struct signatured_type *sig_type;
7060
7061 /* Verify we can do the following downcast, and that we have the
7062 data we need. */
7063 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7064 sig_type = (struct signatured_type *) this_cu;
7065 gdb_assert (sig_type->dwo_unit != NULL);
7066
7067 dwarf2_cu *cu;
7068
7069 if (existing_cu != nullptr)
7070 {
7071 cu = existing_cu;
7072 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
7073 /* There's no need to do the rereading_dwo_cu handling that
7074 cutu_reader does since we don't read the stub. */
7075 }
7076 else
7077 {
7078 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7079 in per_objfile yet. */
7080 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7081 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7082 cu = m_new_cu.get ();
7083 }
7084
7085 /* A future optimization, if needed, would be to use an existing
7086 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7087 could share abbrev tables. */
7088
7089 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
7090 NULL /* stub_comp_unit_die */,
7091 sig_type->dwo_unit->dwo_file->comp_dir,
7092 this, &info_ptr,
7093 &comp_unit_die,
7094 &m_dwo_abbrev_table) == 0)
7095 {
7096 /* Dummy die. */
7097 dummy_p = true;
7098 }
7099 }
7100
7101 /* Initialize a CU (or TU) and read its DIEs.
7102 If the CU defers to a DWO file, read the DWO file as well.
7103
7104 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7105 Otherwise the table specified in the comp unit header is read in and used.
7106 This is an optimization for when we already have the abbrev table.
7107
7108 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7109 allocated. */
7110
7111 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7112 dwarf2_per_objfile *per_objfile,
7113 struct abbrev_table *abbrev_table,
7114 dwarf2_cu *existing_cu,
7115 bool skip_partial)
7116 : die_reader_specs {},
7117 m_this_cu (this_cu)
7118 {
7119 struct objfile *objfile = per_objfile->objfile;
7120 struct dwarf2_section_info *section = this_cu->section;
7121 bfd *abfd = section->get_bfd_owner ();
7122 const gdb_byte *begin_info_ptr;
7123 struct signatured_type *sig_type = NULL;
7124 struct dwarf2_section_info *abbrev_section;
7125 /* Non-zero if CU currently points to a DWO file and we need to
7126 reread it. When this happens we need to reread the skeleton die
7127 before we can reread the DWO file (this only applies to CUs, not TUs). */
7128 int rereading_dwo_cu = 0;
7129
7130 if (dwarf_die_debug)
7131 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7132 this_cu->is_debug_types ? "type" : "comp",
7133 sect_offset_str (this_cu->sect_off));
7134
7135 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7136 file (instead of going through the stub), short-circuit all of this. */
7137 if (this_cu->reading_dwo_directly)
7138 {
7139 /* Narrow down the scope of possibilities to have to understand. */
7140 gdb_assert (this_cu->is_debug_types);
7141 gdb_assert (abbrev_table == NULL);
7142 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
7143 return;
7144 }
7145
7146 /* This is cheap if the section is already read in. */
7147 section->read (objfile);
7148
7149 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7150
7151 abbrev_section = get_abbrev_section_for_cu (this_cu);
7152
7153 dwarf2_cu *cu;
7154
7155 if (existing_cu != nullptr)
7156 {
7157 cu = existing_cu;
7158 /* If this CU is from a DWO file we need to start over, we need to
7159 refetch the attributes from the skeleton CU.
7160 This could be optimized by retrieving those attributes from when we
7161 were here the first time: the previous comp_unit_die was stored in
7162 comp_unit_obstack. But there's no data yet that we need this
7163 optimization. */
7164 if (cu->dwo_unit != NULL)
7165 rereading_dwo_cu = 1;
7166 }
7167 else
7168 {
7169 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7170 in per_objfile yet. */
7171 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7172 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7173 cu = m_new_cu.get ();
7174 }
7175
7176 /* Get the header. */
7177 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7178 {
7179 /* We already have the header, there's no need to read it in again. */
7180 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7181 }
7182 else
7183 {
7184 if (this_cu->is_debug_types)
7185 {
7186 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7187 section, abbrev_section,
7188 info_ptr, rcuh_kind::TYPE);
7189
7190 /* Since per_cu is the first member of struct signatured_type,
7191 we can go from a pointer to one to a pointer to the other. */
7192 sig_type = (struct signatured_type *) this_cu;
7193 gdb_assert (sig_type->signature == cu->header.signature);
7194 gdb_assert (sig_type->type_offset_in_tu
7195 == cu->header.type_cu_offset_in_tu);
7196 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7197
7198 /* LENGTH has not been set yet for type units if we're
7199 using .gdb_index. */
7200 this_cu->length = cu->header.get_length ();
7201
7202 /* Establish the type offset that can be used to lookup the type. */
7203 sig_type->type_offset_in_section =
7204 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7205
7206 this_cu->dwarf_version = cu->header.version;
7207 }
7208 else
7209 {
7210 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7211 section, abbrev_section,
7212 info_ptr,
7213 rcuh_kind::COMPILE);
7214
7215 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7216 if (this_cu->length == 0)
7217 this_cu->length = cu->header.get_length ();
7218 else
7219 gdb_assert (this_cu->length == cu->header.get_length ());
7220 this_cu->dwarf_version = cu->header.version;
7221 }
7222 }
7223
7224 /* Skip dummy compilation units. */
7225 if (info_ptr >= begin_info_ptr + this_cu->length
7226 || peek_abbrev_code (abfd, info_ptr) == 0)
7227 {
7228 dummy_p = true;
7229 return;
7230 }
7231
7232 /* If we don't have them yet, read the abbrevs for this compilation unit.
7233 And if we need to read them now, make sure they're freed when we're
7234 done. */
7235 if (abbrev_table != NULL)
7236 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7237 else
7238 {
7239 abbrev_section->read (objfile);
7240 m_abbrev_table_holder
7241 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
7242 abbrev_table = m_abbrev_table_holder.get ();
7243 }
7244
7245 /* Read the top level CU/TU die. */
7246 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7247 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7248
7249 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7250 {
7251 dummy_p = true;
7252 return;
7253 }
7254
7255 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7256 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7257 table from the DWO file and pass the ownership over to us. It will be
7258 referenced from READER, so we must make sure to free it after we're done
7259 with READER.
7260
7261 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7262 DWO CU, that this test will fail (the attribute will not be present). */
7263 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7264 if (dwo_name != nullptr)
7265 {
7266 struct dwo_unit *dwo_unit;
7267 struct die_info *dwo_comp_unit_die;
7268
7269 if (comp_unit_die->has_children)
7270 {
7271 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7272 " has children (offset %s) [in module %s]"),
7273 sect_offset_str (this_cu->sect_off),
7274 bfd_get_filename (abfd));
7275 }
7276 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
7277 if (dwo_unit != NULL)
7278 {
7279 if (read_cutu_die_from_dwo (cu, dwo_unit,
7280 comp_unit_die, NULL,
7281 this, &info_ptr,
7282 &dwo_comp_unit_die,
7283 &m_dwo_abbrev_table) == 0)
7284 {
7285 /* Dummy die. */
7286 dummy_p = true;
7287 return;
7288 }
7289 comp_unit_die = dwo_comp_unit_die;
7290 }
7291 else
7292 {
7293 /* Yikes, we couldn't find the rest of the DIE, we only have
7294 the stub. A complaint has already been logged. There's
7295 not much more we can do except pass on the stub DIE to
7296 die_reader_func. We don't want to throw an error on bad
7297 debug info. */
7298 }
7299 }
7300 }
7301
7302 void
7303 cutu_reader::keep ()
7304 {
7305 /* Done, clean up. */
7306 gdb_assert (!dummy_p);
7307 if (m_new_cu != NULL)
7308 {
7309 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7310 now. */
7311 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7312 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
7313 }
7314 }
7315
7316 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7317 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7318 assumed to have already done the lookup to find the DWO file).
7319
7320 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7321 THIS_CU->is_debug_types, but nothing else.
7322
7323 We fill in THIS_CU->length.
7324
7325 THIS_CU->cu is always freed when done.
7326 This is done in order to not leave THIS_CU->cu in a state where we have
7327 to care whether it refers to the "main" CU or the DWO CU.
7328
7329 When parent_cu is passed, it is used to provide a default value for
7330 str_offsets_base and addr_base from the parent. */
7331
7332 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7333 dwarf2_per_objfile *per_objfile,
7334 struct dwarf2_cu *parent_cu,
7335 struct dwo_file *dwo_file)
7336 : die_reader_specs {},
7337 m_this_cu (this_cu)
7338 {
7339 struct objfile *objfile = per_objfile->objfile;
7340 struct dwarf2_section_info *section = this_cu->section;
7341 bfd *abfd = section->get_bfd_owner ();
7342 struct dwarf2_section_info *abbrev_section;
7343 const gdb_byte *begin_info_ptr, *info_ptr;
7344
7345 if (dwarf_die_debug)
7346 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7347 this_cu->is_debug_types ? "type" : "comp",
7348 sect_offset_str (this_cu->sect_off));
7349
7350 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7351
7352 abbrev_section = (dwo_file != NULL
7353 ? &dwo_file->sections.abbrev
7354 : get_abbrev_section_for_cu (this_cu));
7355
7356 /* This is cheap if the section is already read in. */
7357 section->read (objfile);
7358
7359 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7360
7361 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7362 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7363 section, abbrev_section, info_ptr,
7364 (this_cu->is_debug_types
7365 ? rcuh_kind::TYPE
7366 : rcuh_kind::COMPILE));
7367
7368 if (parent_cu != nullptr)
7369 {
7370 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7371 m_new_cu->addr_base = parent_cu->addr_base;
7372 }
7373 this_cu->length = m_new_cu->header.get_length ();
7374
7375 /* Skip dummy compilation units. */
7376 if (info_ptr >= begin_info_ptr + this_cu->length
7377 || peek_abbrev_code (abfd, info_ptr) == 0)
7378 {
7379 dummy_p = true;
7380 return;
7381 }
7382
7383 abbrev_section->read (objfile);
7384 m_abbrev_table_holder
7385 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
7386
7387 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7388 m_abbrev_table_holder.get ());
7389 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7390 }
7391
7392 \f
7393 /* Type Unit Groups.
7394
7395 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7396 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7397 so that all types coming from the same compilation (.o file) are grouped
7398 together. A future step could be to put the types in the same symtab as
7399 the CU the types ultimately came from. */
7400
7401 static hashval_t
7402 hash_type_unit_group (const void *item)
7403 {
7404 const struct type_unit_group *tu_group
7405 = (const struct type_unit_group *) item;
7406
7407 return hash_stmt_list_entry (&tu_group->hash);
7408 }
7409
7410 static int
7411 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7412 {
7413 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7414 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7415
7416 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7417 }
7418
7419 /* Allocate a hash table for type unit groups. */
7420
7421 static htab_up
7422 allocate_type_unit_groups_table ()
7423 {
7424 return htab_up (htab_create_alloc (3,
7425 hash_type_unit_group,
7426 eq_type_unit_group,
7427 NULL, xcalloc, xfree));
7428 }
7429
7430 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7431 partial symtabs. We combine several TUs per psymtab to not let the size
7432 of any one psymtab grow too big. */
7433 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7434 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7435
7436 /* Helper routine for get_type_unit_group.
7437 Create the type_unit_group object used to hold one or more TUs. */
7438
7439 static struct type_unit_group *
7440 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7441 {
7442 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7443 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7444 struct dwarf2_per_cu_data *per_cu;
7445 struct type_unit_group *tu_group;
7446
7447 tu_group = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, type_unit_group);
7448 per_cu = &tu_group->per_cu;
7449 per_cu->per_bfd = per_bfd;
7450
7451 if (per_bfd->using_index)
7452 {
7453 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7454 struct dwarf2_per_cu_quick_data);
7455 }
7456 else
7457 {
7458 unsigned int line_offset = to_underlying (line_offset_struct);
7459 dwarf2_psymtab *pst;
7460 std::string name;
7461
7462 /* Give the symtab a useful name for debug purposes. */
7463 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7464 name = string_printf ("<type_units_%d>",
7465 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7466 else
7467 name = string_printf ("<type_units_at_0x%x>", line_offset);
7468
7469 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
7470 pst->anonymous = true;
7471 }
7472
7473 tu_group->hash.dwo_unit = cu->dwo_unit;
7474 tu_group->hash.line_sect_off = line_offset_struct;
7475
7476 return tu_group;
7477 }
7478
7479 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7480 STMT_LIST is a DW_AT_stmt_list attribute. */
7481
7482 static struct type_unit_group *
7483 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7484 {
7485 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7486 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7487 struct type_unit_group *tu_group;
7488 void **slot;
7489 unsigned int line_offset;
7490 struct type_unit_group type_unit_group_for_lookup;
7491
7492 if (per_objfile->per_bfd->type_unit_groups == NULL)
7493 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7494
7495 /* Do we need to create a new group, or can we use an existing one? */
7496
7497 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
7498 {
7499 line_offset = stmt_list->as_unsigned ();
7500 ++tu_stats->nr_symtab_sharers;
7501 }
7502 else
7503 {
7504 /* Ugh, no stmt_list. Rare, but we have to handle it.
7505 We can do various things here like create one group per TU or
7506 spread them over multiple groups to split up the expansion work.
7507 To avoid worst case scenarios (too many groups or too large groups)
7508 we, umm, group them in bunches. */
7509 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7510 | (tu_stats->nr_stmt_less_type_units
7511 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7512 ++tu_stats->nr_stmt_less_type_units;
7513 }
7514
7515 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7516 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7517 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
7518 &type_unit_group_for_lookup, INSERT);
7519 if (*slot != NULL)
7520 {
7521 tu_group = (struct type_unit_group *) *slot;
7522 gdb_assert (tu_group != NULL);
7523 }
7524 else
7525 {
7526 sect_offset line_offset_struct = (sect_offset) line_offset;
7527 tu_group = create_type_unit_group (cu, line_offset_struct);
7528 *slot = tu_group;
7529 ++tu_stats->nr_symtabs;
7530 }
7531
7532 return tu_group;
7533 }
7534 \f
7535 /* Partial symbol tables. */
7536
7537 /* Create a psymtab named NAME and assign it to PER_CU.
7538
7539 The caller must fill in the following details:
7540 dirname, textlow, texthigh. */
7541
7542 static dwarf2_psymtab *
7543 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7544 dwarf2_per_objfile *per_objfile,
7545 const char *name)
7546 {
7547 struct objfile *objfile = per_objfile->objfile;
7548 dwarf2_psymtab *pst;
7549
7550 pst = new dwarf2_psymtab (name, objfile, per_cu);
7551
7552 pst->psymtabs_addrmap_supported = true;
7553
7554 /* This is the glue that links PST into GDB's symbol API. */
7555 per_cu->v.psymtab = pst;
7556
7557 return pst;
7558 }
7559
7560 /* DIE reader function for process_psymtab_comp_unit. */
7561
7562 static void
7563 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7564 const gdb_byte *info_ptr,
7565 struct die_info *comp_unit_die,
7566 enum language pretend_language)
7567 {
7568 struct dwarf2_cu *cu = reader->cu;
7569 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7570 struct objfile *objfile = per_objfile->objfile;
7571 struct gdbarch *gdbarch = objfile->arch ();
7572 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7573 CORE_ADDR baseaddr;
7574 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7575 dwarf2_psymtab *pst;
7576 enum pc_bounds_kind cu_bounds_kind;
7577 const char *filename;
7578
7579 gdb_assert (! per_cu->is_debug_types);
7580
7581 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7582
7583 /* Allocate a new partial symbol table structure. */
7584 gdb::unique_xmalloc_ptr<char> debug_filename;
7585 static const char artificial[] = "<artificial>";
7586 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7587 if (filename == NULL)
7588 filename = "";
7589 else if (strcmp (filename, artificial) == 0)
7590 {
7591 debug_filename.reset (concat (artificial, "@",
7592 sect_offset_str (per_cu->sect_off),
7593 (char *) NULL));
7594 filename = debug_filename.get ();
7595 }
7596
7597 pst = create_partial_symtab (per_cu, per_objfile, filename);
7598
7599 /* This must be done before calling dwarf2_build_include_psymtabs. */
7600 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7601
7602 baseaddr = objfile->text_section_offset ();
7603
7604 dwarf2_find_base_address (comp_unit_die, cu);
7605
7606 /* Possibly set the default values of LOWPC and HIGHPC from
7607 `DW_AT_ranges'. */
7608 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7609 &best_highpc, cu, pst);
7610 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7611 {
7612 CORE_ADDR low
7613 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7614 - baseaddr);
7615 CORE_ADDR high
7616 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7617 - baseaddr - 1);
7618 /* Store the contiguous range if it is not empty; it can be
7619 empty for CUs with no code. */
7620 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7621 low, high, pst);
7622 }
7623
7624 /* Check if comp unit has_children.
7625 If so, read the rest of the partial symbols from this comp unit.
7626 If not, there's no more debug_info for this comp unit. */
7627 if (comp_unit_die->has_children)
7628 {
7629 struct partial_die_info *first_die;
7630 CORE_ADDR lowpc, highpc;
7631
7632 lowpc = ((CORE_ADDR) -1);
7633 highpc = ((CORE_ADDR) 0);
7634
7635 first_die = load_partial_dies (reader, info_ptr, 1);
7636
7637 scan_partial_symbols (first_die, &lowpc, &highpc,
7638 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7639
7640 /* If we didn't find a lowpc, set it to highpc to avoid
7641 complaints from `maint check'. */
7642 if (lowpc == ((CORE_ADDR) -1))
7643 lowpc = highpc;
7644
7645 /* If the compilation unit didn't have an explicit address range,
7646 then use the information extracted from its child dies. */
7647 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7648 {
7649 best_lowpc = lowpc;
7650 best_highpc = highpc;
7651 }
7652 }
7653 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7654 best_lowpc + baseaddr)
7655 - baseaddr);
7656 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7657 best_highpc + baseaddr)
7658 - baseaddr);
7659
7660 pst->end ();
7661
7662 if (!cu->per_cu->imported_symtabs_empty ())
7663 {
7664 int i;
7665 int len = cu->per_cu->imported_symtabs_size ();
7666
7667 /* Fill in 'dependencies' here; we fill in 'users' in a
7668 post-pass. */
7669 pst->number_of_dependencies = len;
7670 pst->dependencies
7671 = objfile->partial_symtabs->allocate_dependencies (len);
7672 for (i = 0; i < len; ++i)
7673 {
7674 pst->dependencies[i]
7675 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7676 }
7677
7678 cu->per_cu->imported_symtabs_free ();
7679 }
7680
7681 /* Get the list of files included in the current compilation unit,
7682 and build a psymtab for each of them. */
7683 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7684
7685 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7686 ", %d global, %d static syms",
7687 per_cu->is_debug_types ? "type" : "comp",
7688 sect_offset_str (per_cu->sect_off),
7689 paddress (gdbarch, pst->text_low (objfile)),
7690 paddress (gdbarch, pst->text_high (objfile)),
7691 (int) pst->global_psymbols.size (),
7692 (int) pst->static_psymbols.size ());
7693 }
7694
7695 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7696 Process compilation unit THIS_CU for a psymtab. */
7697
7698 static void
7699 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7700 dwarf2_per_objfile *per_objfile,
7701 bool want_partial_unit,
7702 enum language pretend_language)
7703 {
7704 /* If this compilation unit was already read in, free the
7705 cached copy in order to read it in again. This is
7706 necessary because we skipped some symbols when we first
7707 read in the compilation unit (see load_partial_dies).
7708 This problem could be avoided, but the benefit is unclear. */
7709 per_objfile->remove_cu (this_cu);
7710
7711 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7712
7713 switch (reader.comp_unit_die->tag)
7714 {
7715 case DW_TAG_compile_unit:
7716 this_cu->unit_type = DW_UT_compile;
7717 break;
7718 case DW_TAG_partial_unit:
7719 this_cu->unit_type = DW_UT_partial;
7720 break;
7721 case DW_TAG_type_unit:
7722 this_cu->unit_type = DW_UT_type;
7723 break;
7724 default:
7725 abort ();
7726 }
7727
7728 if (reader.dummy_p)
7729 {
7730 /* Nothing. */
7731 }
7732 else if (this_cu->is_debug_types)
7733 build_type_psymtabs_reader (&reader, reader.info_ptr,
7734 reader.comp_unit_die);
7735 else if (want_partial_unit
7736 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7737 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7738 reader.comp_unit_die,
7739 pretend_language);
7740
7741 this_cu->lang = reader.cu->language;
7742
7743 /* Age out any secondary CUs. */
7744 per_objfile->age_comp_units ();
7745 }
7746
7747 /* Reader function for build_type_psymtabs. */
7748
7749 static void
7750 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7751 const gdb_byte *info_ptr,
7752 struct die_info *type_unit_die)
7753 {
7754 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7755 struct dwarf2_cu *cu = reader->cu;
7756 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7757 struct signatured_type *sig_type;
7758 struct type_unit_group *tu_group;
7759 struct attribute *attr;
7760 struct partial_die_info *first_die;
7761 CORE_ADDR lowpc, highpc;
7762 dwarf2_psymtab *pst;
7763
7764 gdb_assert (per_cu->is_debug_types);
7765 sig_type = (struct signatured_type *) per_cu;
7766
7767 if (! type_unit_die->has_children)
7768 return;
7769
7770 attr = type_unit_die->attr (DW_AT_stmt_list);
7771 tu_group = get_type_unit_group (cu, attr);
7772
7773 if (tu_group->tus == nullptr)
7774 tu_group->tus = new std::vector<signatured_type *>;
7775 tu_group->tus->push_back (sig_type);
7776
7777 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7778 pst = create_partial_symtab (per_cu, per_objfile, "");
7779 pst->anonymous = true;
7780
7781 first_die = load_partial_dies (reader, info_ptr, 1);
7782
7783 lowpc = (CORE_ADDR) -1;
7784 highpc = (CORE_ADDR) 0;
7785 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7786
7787 pst->end ();
7788 }
7789
7790 /* Struct used to sort TUs by their abbreviation table offset. */
7791
7792 struct tu_abbrev_offset
7793 {
7794 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7795 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7796 {}
7797
7798 signatured_type *sig_type;
7799 sect_offset abbrev_offset;
7800 };
7801
7802 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7803
7804 static bool
7805 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7806 const struct tu_abbrev_offset &b)
7807 {
7808 return a.abbrev_offset < b.abbrev_offset;
7809 }
7810
7811 /* Efficiently read all the type units.
7812 This does the bulk of the work for build_type_psymtabs.
7813
7814 The efficiency is because we sort TUs by the abbrev table they use and
7815 only read each abbrev table once. In one program there are 200K TUs
7816 sharing 8K abbrev tables.
7817
7818 The main purpose of this function is to support building the
7819 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7820 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7821 can collapse the search space by grouping them by stmt_list.
7822 The savings can be significant, in the same program from above the 200K TUs
7823 share 8K stmt_list tables.
7824
7825 FUNC is expected to call get_type_unit_group, which will create the
7826 struct type_unit_group if necessary and add it to
7827 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7828
7829 static void
7830 build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
7831 {
7832 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7833 abbrev_table_up abbrev_table;
7834 sect_offset abbrev_offset;
7835
7836 /* It's up to the caller to not call us multiple times. */
7837 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7838
7839 if (per_objfile->per_bfd->all_type_units.empty ())
7840 return;
7841
7842 /* TUs typically share abbrev tables, and there can be way more TUs than
7843 abbrev tables. Sort by abbrev table to reduce the number of times we
7844 read each abbrev table in.
7845 Alternatives are to punt or to maintain a cache of abbrev tables.
7846 This is simpler and efficient enough for now.
7847
7848 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7849 symtab to use). Typically TUs with the same abbrev offset have the same
7850 stmt_list value too so in practice this should work well.
7851
7852 The basic algorithm here is:
7853
7854 sort TUs by abbrev table
7855 for each TU with same abbrev table:
7856 read abbrev table if first user
7857 read TU top level DIE
7858 [IWBN if DWO skeletons had DW_AT_stmt_list]
7859 call FUNC */
7860
7861 dwarf_read_debug_printf ("Building type unit groups ...");
7862
7863 /* Sort in a separate table to maintain the order of all_type_units
7864 for .gdb_index: TU indices directly index all_type_units. */
7865 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7866 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
7867
7868 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
7869 sorted_by_abbrev.emplace_back
7870 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
7871 sig_type->per_cu.sect_off));
7872
7873 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7874 sort_tu_by_abbrev_offset);
7875
7876 abbrev_offset = (sect_offset) ~(unsigned) 0;
7877
7878 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7879 {
7880 /* Switch to the next abbrev table if necessary. */
7881 if (abbrev_table == NULL
7882 || tu.abbrev_offset != abbrev_offset)
7883 {
7884 abbrev_offset = tu.abbrev_offset;
7885 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
7886 abbrev_table =
7887 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
7888 ++tu_stats->nr_uniq_abbrev_tables;
7889 }
7890
7891 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
7892 abbrev_table.get (), nullptr, false);
7893 if (!reader.dummy_p)
7894 build_type_psymtabs_reader (&reader, reader.info_ptr,
7895 reader.comp_unit_die);
7896 }
7897 }
7898
7899 /* Print collected type unit statistics. */
7900
7901 static void
7902 print_tu_stats (dwarf2_per_objfile *per_objfile)
7903 {
7904 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7905
7906 dwarf_read_debug_printf ("Type unit statistics:");
7907 dwarf_read_debug_printf (" %zu TUs",
7908 per_objfile->per_bfd->all_type_units.size ());
7909 dwarf_read_debug_printf (" %d uniq abbrev tables",
7910 tu_stats->nr_uniq_abbrev_tables);
7911 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7912 tu_stats->nr_symtabs);
7913 dwarf_read_debug_printf (" %d symtab sharers",
7914 tu_stats->nr_symtab_sharers);
7915 dwarf_read_debug_printf (" %d type units without a stmt_list",
7916 tu_stats->nr_stmt_less_type_units);
7917 dwarf_read_debug_printf (" %d all_type_units reallocs",
7918 tu_stats->nr_all_type_units_reallocs);
7919 }
7920
7921 /* Traversal function for build_type_psymtabs. */
7922
7923 static int
7924 build_type_psymtab_dependencies (void **slot, void *info)
7925 {
7926 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7927 struct objfile *objfile = per_objfile->objfile;
7928 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7929 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7930 dwarf2_psymtab *pst = per_cu->v.psymtab;
7931 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7932 int i;
7933
7934 gdb_assert (len > 0);
7935 gdb_assert (per_cu->type_unit_group_p ());
7936
7937 pst->number_of_dependencies = len;
7938 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7939 for (i = 0; i < len; ++i)
7940 {
7941 struct signatured_type *iter = tu_group->tus->at (i);
7942 gdb_assert (iter->per_cu.is_debug_types);
7943 pst->dependencies[i] = iter->per_cu.v.psymtab;
7944 iter->type_unit_group = tu_group;
7945 }
7946
7947 delete tu_group->tus;
7948 tu_group->tus = nullptr;
7949
7950 return 1;
7951 }
7952
7953 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7954 Build partial symbol tables for the .debug_types comp-units. */
7955
7956 static void
7957 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
7958 {
7959 if (! create_all_type_units (per_objfile))
7960 return;
7961
7962 build_type_psymtabs_1 (per_objfile);
7963 }
7964
7965 /* Traversal function for process_skeletonless_type_unit.
7966 Read a TU in a DWO file and build partial symbols for it. */
7967
7968 static int
7969 process_skeletonless_type_unit (void **slot, void *info)
7970 {
7971 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7972 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7973 struct signatured_type find_entry, *entry;
7974
7975 /* If this TU doesn't exist in the global table, add it and read it in. */
7976
7977 if (per_objfile->per_bfd->signatured_types == NULL)
7978 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7979
7980 find_entry.signature = dwo_unit->signature;
7981 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
7982 &find_entry, INSERT);
7983 /* If we've already seen this type there's nothing to do. What's happening
7984 is we're doing our own version of comdat-folding here. */
7985 if (*slot != NULL)
7986 return 1;
7987
7988 /* This does the job that create_all_type_units would have done for
7989 this TU. */
7990 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
7991 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
7992 *slot = entry;
7993
7994 /* This does the job that build_type_psymtabs_1 would have done. */
7995 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
7996 if (!reader.dummy_p)
7997 build_type_psymtabs_reader (&reader, reader.info_ptr,
7998 reader.comp_unit_die);
7999
8000 return 1;
8001 }
8002
8003 /* Traversal function for process_skeletonless_type_units. */
8004
8005 static int
8006 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8007 {
8008 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8009
8010 if (dwo_file->tus != NULL)
8011 htab_traverse_noresize (dwo_file->tus.get (),
8012 process_skeletonless_type_unit, info);
8013
8014 return 1;
8015 }
8016
8017 /* Scan all TUs of DWO files, verifying we've processed them.
8018 This is needed in case a TU was emitted without its skeleton.
8019 Note: This can't be done until we know what all the DWO files are. */
8020
8021 static void
8022 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
8023 {
8024 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8025 if (get_dwp_file (per_objfile) == NULL
8026 && per_objfile->per_bfd->dwo_files != NULL)
8027 {
8028 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
8029 process_dwo_file_for_skeletonless_type_units,
8030 per_objfile);
8031 }
8032 }
8033
8034 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8035
8036 static void
8037 set_partial_user (dwarf2_per_objfile *per_objfile)
8038 {
8039 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
8040 {
8041 dwarf2_psymtab *pst = per_cu->v.psymtab;
8042
8043 if (pst == NULL)
8044 continue;
8045
8046 for (int j = 0; j < pst->number_of_dependencies; ++j)
8047 {
8048 /* Set the 'user' field only if it is not already set. */
8049 if (pst->dependencies[j]->user == NULL)
8050 pst->dependencies[j]->user = pst;
8051 }
8052 }
8053 }
8054
8055 /* Build the partial symbol table by doing a quick pass through the
8056 .debug_info and .debug_abbrev sections. */
8057
8058 static void
8059 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
8060 {
8061 struct objfile *objfile = per_objfile->objfile;
8062
8063 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
8064 objfile_name (objfile));
8065
8066 scoped_restore restore_reading_psyms
8067 = make_scoped_restore (&per_objfile->per_bfd->reading_partial_symbols,
8068 true);
8069
8070 per_objfile->per_bfd->info.read (objfile);
8071
8072 /* Any cached compilation units will be linked by the per-objfile
8073 read_in_chain. Make sure to free them when we're done. */
8074 free_cached_comp_units freer (per_objfile);
8075
8076 build_type_psymtabs (per_objfile);
8077
8078 create_all_comp_units (per_objfile);
8079
8080 /* Create a temporary address map on a temporary obstack. We later
8081 copy this to the final obstack. */
8082 auto_obstack temp_obstack;
8083
8084 scoped_restore save_psymtabs_addrmap
8085 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8086 addrmap_create_mutable (&temp_obstack));
8087
8088 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
8089 {
8090 if (per_cu->v.psymtab != NULL)
8091 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8092 continue;
8093 process_psymtab_comp_unit (per_cu, per_objfile, false,
8094 language_minimal);
8095 }
8096
8097 /* This has to wait until we read the CUs, we need the list of DWOs. */
8098 process_skeletonless_type_units (per_objfile);
8099
8100 /* Now that all TUs have been processed we can fill in the dependencies. */
8101 if (per_objfile->per_bfd->type_unit_groups != NULL)
8102 {
8103 htab_traverse_noresize (per_objfile->per_bfd->type_unit_groups.get (),
8104 build_type_psymtab_dependencies, per_objfile);
8105 }
8106
8107 if (dwarf_read_debug > 0)
8108 print_tu_stats (per_objfile);
8109
8110 set_partial_user (per_objfile);
8111
8112 objfile->partial_symtabs->psymtabs_addrmap
8113 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8114 objfile->partial_symtabs->obstack ());
8115 /* At this point we want to keep the address map. */
8116 save_psymtabs_addrmap.release ();
8117
8118 dwarf_read_debug_printf ("Done building psymtabs of %s",
8119 objfile_name (objfile));
8120 }
8121
8122 /* Load the partial DIEs for a secondary CU into memory.
8123 This is also used when rereading a primary CU with load_all_dies. */
8124
8125 static void
8126 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
8127 dwarf2_per_objfile *per_objfile,
8128 dwarf2_cu *existing_cu)
8129 {
8130 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
8131
8132 if (!reader.dummy_p)
8133 {
8134 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8135 language_minimal);
8136
8137 /* Check if comp unit has_children.
8138 If so, read the rest of the partial symbols from this comp unit.
8139 If not, there's no more debug_info for this comp unit. */
8140 if (reader.comp_unit_die->has_children)
8141 load_partial_dies (&reader, reader.info_ptr, 0);
8142
8143 reader.keep ();
8144 }
8145 }
8146
8147 static void
8148 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
8149 struct dwarf2_section_info *section,
8150 struct dwarf2_section_info *abbrev_section,
8151 unsigned int is_dwz)
8152 {
8153 const gdb_byte *info_ptr;
8154 struct objfile *objfile = per_objfile->objfile;
8155
8156 dwarf_read_debug_printf ("Reading %s for %s",
8157 section->get_name (),
8158 section->get_file_name ());
8159
8160 section->read (objfile);
8161
8162 info_ptr = section->buffer;
8163
8164 while (info_ptr < section->buffer + section->size)
8165 {
8166 struct dwarf2_per_cu_data *this_cu;
8167
8168 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8169
8170 comp_unit_head cu_header;
8171 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
8172 abbrev_section, info_ptr,
8173 rcuh_kind::COMPILE);
8174
8175 /* Save the compilation unit for later lookup. */
8176 if (cu_header.unit_type != DW_UT_type)
8177 this_cu = per_objfile->per_bfd->allocate_per_cu ();
8178 else
8179 {
8180 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
8181 sig_type->signature = cu_header.signature;
8182 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8183 this_cu = &sig_type->per_cu;
8184 }
8185 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8186 this_cu->sect_off = sect_off;
8187 this_cu->length = cu_header.length + cu_header.initial_length_size;
8188 this_cu->is_dwz = is_dwz;
8189 this_cu->section = section;
8190
8191 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8192
8193 info_ptr = info_ptr + this_cu->length;
8194 }
8195 }
8196
8197 /* Create a list of all compilation units in OBJFILE.
8198 This is only done for -readnow and building partial symtabs. */
8199
8200 static void
8201 create_all_comp_units (dwarf2_per_objfile *per_objfile)
8202 {
8203 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8204 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8205 &per_objfile->per_bfd->abbrev, 0);
8206
8207 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8208 if (dwz != NULL)
8209 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
8210 }
8211
8212 /* Process all loaded DIEs for compilation unit CU, starting at
8213 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8214 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8215 DW_AT_ranges). See the comments of add_partial_subprogram on how
8216 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8217
8218 static void
8219 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8220 CORE_ADDR *highpc, int set_addrmap,
8221 struct dwarf2_cu *cu)
8222 {
8223 struct partial_die_info *pdi;
8224
8225 /* Now, march along the PDI's, descending into ones which have
8226 interesting children but skipping the children of the other ones,
8227 until we reach the end of the compilation unit. */
8228
8229 pdi = first_die;
8230
8231 while (pdi != NULL)
8232 {
8233 pdi->fixup (cu);
8234
8235 /* Anonymous namespaces or modules have no name but have interesting
8236 children, so we need to look at them. Ditto for anonymous
8237 enums. */
8238
8239 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
8240 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8241 || pdi->tag == DW_TAG_imported_unit
8242 || pdi->tag == DW_TAG_inlined_subroutine)
8243 {
8244 switch (pdi->tag)
8245 {
8246 case DW_TAG_subprogram:
8247 case DW_TAG_inlined_subroutine:
8248 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8249 if (cu->language == language_cplus)
8250 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8251 set_addrmap, cu);
8252 break;
8253 case DW_TAG_constant:
8254 case DW_TAG_variable:
8255 case DW_TAG_typedef:
8256 case DW_TAG_union_type:
8257 if (!pdi->is_declaration
8258 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8259 {
8260 add_partial_symbol (pdi, cu);
8261 }
8262 break;
8263 case DW_TAG_class_type:
8264 case DW_TAG_interface_type:
8265 case DW_TAG_structure_type:
8266 if (!pdi->is_declaration)
8267 {
8268 add_partial_symbol (pdi, cu);
8269 }
8270 if ((cu->language == language_rust
8271 || cu->language == language_cplus) && pdi->has_children)
8272 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8273 set_addrmap, cu);
8274 break;
8275 case DW_TAG_enumeration_type:
8276 if (!pdi->is_declaration)
8277 add_partial_enumeration (pdi, cu);
8278 break;
8279 case DW_TAG_base_type:
8280 case DW_TAG_subrange_type:
8281 /* File scope base type definitions are added to the partial
8282 symbol table. */
8283 add_partial_symbol (pdi, cu);
8284 break;
8285 case DW_TAG_namespace:
8286 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8287 break;
8288 case DW_TAG_module:
8289 if (!pdi->is_declaration)
8290 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8291 break;
8292 case DW_TAG_imported_unit:
8293 {
8294 struct dwarf2_per_cu_data *per_cu;
8295
8296 /* For now we don't handle imported units in type units. */
8297 if (cu->per_cu->is_debug_types)
8298 {
8299 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8300 " supported in type units [in module %s]"),
8301 objfile_name (cu->per_objfile->objfile));
8302 }
8303
8304 per_cu = dwarf2_find_containing_comp_unit
8305 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8306
8307 /* Go read the partial unit, if needed. */
8308 if (per_cu->v.psymtab == NULL)
8309 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8310 cu->language);
8311
8312 cu->per_cu->imported_symtabs_push (per_cu);
8313 }
8314 break;
8315 case DW_TAG_imported_declaration:
8316 add_partial_symbol (pdi, cu);
8317 break;
8318 default:
8319 break;
8320 }
8321 }
8322
8323 /* If the die has a sibling, skip to the sibling. */
8324
8325 pdi = pdi->die_sibling;
8326 }
8327 }
8328
8329 /* Functions used to compute the fully scoped name of a partial DIE.
8330
8331 Normally, this is simple. For C++, the parent DIE's fully scoped
8332 name is concatenated with "::" and the partial DIE's name.
8333 Enumerators are an exception; they use the scope of their parent
8334 enumeration type, i.e. the name of the enumeration type is not
8335 prepended to the enumerator.
8336
8337 There are two complexities. One is DW_AT_specification; in this
8338 case "parent" means the parent of the target of the specification,
8339 instead of the direct parent of the DIE. The other is compilers
8340 which do not emit DW_TAG_namespace; in this case we try to guess
8341 the fully qualified name of structure types from their members'
8342 linkage names. This must be done using the DIE's children rather
8343 than the children of any DW_AT_specification target. We only need
8344 to do this for structures at the top level, i.e. if the target of
8345 any DW_AT_specification (if any; otherwise the DIE itself) does not
8346 have a parent. */
8347
8348 /* Compute the scope prefix associated with PDI's parent, in
8349 compilation unit CU. The result will be allocated on CU's
8350 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8351 field. NULL is returned if no prefix is necessary. */
8352 static const char *
8353 partial_die_parent_scope (struct partial_die_info *pdi,
8354 struct dwarf2_cu *cu)
8355 {
8356 const char *grandparent_scope;
8357 struct partial_die_info *parent, *real_pdi;
8358
8359 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8360 then this means the parent of the specification DIE. */
8361
8362 real_pdi = pdi;
8363 while (real_pdi->has_specification)
8364 {
8365 auto res = find_partial_die (real_pdi->spec_offset,
8366 real_pdi->spec_is_dwz, cu);
8367 real_pdi = res.pdi;
8368 cu = res.cu;
8369 }
8370
8371 parent = real_pdi->die_parent;
8372 if (parent == NULL)
8373 return NULL;
8374
8375 if (parent->scope_set)
8376 return parent->scope;
8377
8378 parent->fixup (cu);
8379
8380 grandparent_scope = partial_die_parent_scope (parent, cu);
8381
8382 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8383 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8384 Work around this problem here. */
8385 if (cu->language == language_cplus
8386 && parent->tag == DW_TAG_namespace
8387 && strcmp (parent->name (cu), "::") == 0
8388 && grandparent_scope == NULL)
8389 {
8390 parent->scope = NULL;
8391 parent->scope_set = 1;
8392 return NULL;
8393 }
8394
8395 /* Nested subroutines in Fortran get a prefix. */
8396 if (pdi->tag == DW_TAG_enumerator)
8397 /* Enumerators should not get the name of the enumeration as a prefix. */
8398 parent->scope = grandparent_scope;
8399 else if (parent->tag == DW_TAG_namespace
8400 || parent->tag == DW_TAG_module
8401 || parent->tag == DW_TAG_structure_type
8402 || parent->tag == DW_TAG_class_type
8403 || parent->tag == DW_TAG_interface_type
8404 || parent->tag == DW_TAG_union_type
8405 || parent->tag == DW_TAG_enumeration_type
8406 || (cu->language == language_fortran
8407 && parent->tag == DW_TAG_subprogram
8408 && pdi->tag == DW_TAG_subprogram))
8409 {
8410 if (grandparent_scope == NULL)
8411 parent->scope = parent->name (cu);
8412 else
8413 parent->scope = typename_concat (&cu->comp_unit_obstack,
8414 grandparent_scope,
8415 parent->name (cu), 0, cu);
8416 }
8417 else
8418 {
8419 /* FIXME drow/2004-04-01: What should we be doing with
8420 function-local names? For partial symbols, we should probably be
8421 ignoring them. */
8422 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8423 dwarf_tag_name (parent->tag),
8424 sect_offset_str (pdi->sect_off));
8425 parent->scope = grandparent_scope;
8426 }
8427
8428 parent->scope_set = 1;
8429 return parent->scope;
8430 }
8431
8432 /* Return the fully scoped name associated with PDI, from compilation unit
8433 CU. The result will be allocated with malloc. */
8434
8435 static gdb::unique_xmalloc_ptr<char>
8436 partial_die_full_name (struct partial_die_info *pdi,
8437 struct dwarf2_cu *cu)
8438 {
8439 const char *parent_scope;
8440
8441 /* If this is a template instantiation, we can not work out the
8442 template arguments from partial DIEs. So, unfortunately, we have
8443 to go through the full DIEs. At least any work we do building
8444 types here will be reused if full symbols are loaded later. */
8445 if (pdi->has_template_arguments)
8446 {
8447 pdi->fixup (cu);
8448
8449 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
8450 {
8451 struct die_info *die;
8452 struct attribute attr;
8453 struct dwarf2_cu *ref_cu = cu;
8454
8455 /* DW_FORM_ref_addr is using section offset. */
8456 attr.name = (enum dwarf_attribute) 0;
8457 attr.form = DW_FORM_ref_addr;
8458 attr.u.unsnd = to_underlying (pdi->sect_off);
8459 die = follow_die_ref (NULL, &attr, &ref_cu);
8460
8461 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8462 }
8463 }
8464
8465 parent_scope = partial_die_parent_scope (pdi, cu);
8466 if (parent_scope == NULL)
8467 return NULL;
8468 else
8469 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8470 pdi->name (cu),
8471 0, cu));
8472 }
8473
8474 static void
8475 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8476 {
8477 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8478 struct objfile *objfile = per_objfile->objfile;
8479 struct gdbarch *gdbarch = objfile->arch ();
8480 CORE_ADDR addr = 0;
8481 const char *actual_name = NULL;
8482 CORE_ADDR baseaddr;
8483
8484 baseaddr = objfile->text_section_offset ();
8485
8486 gdb::unique_xmalloc_ptr<char> built_actual_name
8487 = partial_die_full_name (pdi, cu);
8488 if (built_actual_name != NULL)
8489 actual_name = built_actual_name.get ();
8490
8491 if (actual_name == NULL)
8492 actual_name = pdi->name (cu);
8493
8494 partial_symbol psymbol;
8495 memset (&psymbol, 0, sizeof (psymbol));
8496 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8497 psymbol.ginfo.set_section_index (-1);
8498
8499 /* The code below indicates that the psymbol should be installed by
8500 setting this. */
8501 gdb::optional<psymbol_placement> where;
8502
8503 switch (pdi->tag)
8504 {
8505 case DW_TAG_inlined_subroutine:
8506 case DW_TAG_subprogram:
8507 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8508 - baseaddr);
8509 if (pdi->is_external
8510 || cu->language == language_ada
8511 || (cu->language == language_fortran
8512 && pdi->die_parent != NULL
8513 && pdi->die_parent->tag == DW_TAG_subprogram))
8514 {
8515 /* Normally, only "external" DIEs are part of the global scope.
8516 But in Ada and Fortran, we want to be able to access nested
8517 procedures globally. So all Ada and Fortran subprograms are
8518 stored in the global scope. */
8519 where = psymbol_placement::GLOBAL;
8520 }
8521 else
8522 where = psymbol_placement::STATIC;
8523
8524 psymbol.domain = VAR_DOMAIN;
8525 psymbol.aclass = LOC_BLOCK;
8526 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8527 psymbol.ginfo.value.address = addr;
8528
8529 if (pdi->main_subprogram && actual_name != NULL)
8530 set_objfile_main_name (objfile, actual_name, cu->language);
8531 break;
8532 case DW_TAG_constant:
8533 psymbol.domain = VAR_DOMAIN;
8534 psymbol.aclass = LOC_STATIC;
8535 where = (pdi->is_external
8536 ? psymbol_placement::GLOBAL
8537 : psymbol_placement::STATIC);
8538 break;
8539 case DW_TAG_variable:
8540 if (pdi->d.locdesc)
8541 addr = decode_locdesc (pdi->d.locdesc, cu);
8542
8543 if (pdi->d.locdesc
8544 && addr == 0
8545 && !per_objfile->per_bfd->has_section_at_zero)
8546 {
8547 /* A global or static variable may also have been stripped
8548 out by the linker if unused, in which case its address
8549 will be nullified; do not add such variables into partial
8550 symbol table then. */
8551 }
8552 else if (pdi->is_external)
8553 {
8554 /* Global Variable.
8555 Don't enter into the minimal symbol tables as there is
8556 a minimal symbol table entry from the ELF symbols already.
8557 Enter into partial symbol table if it has a location
8558 descriptor or a type.
8559 If the location descriptor is missing, new_symbol will create
8560 a LOC_UNRESOLVED symbol, the address of the variable will then
8561 be determined from the minimal symbol table whenever the variable
8562 is referenced.
8563 The address for the partial symbol table entry is not
8564 used by GDB, but it comes in handy for debugging partial symbol
8565 table building. */
8566
8567 if (pdi->d.locdesc || pdi->has_type)
8568 {
8569 psymbol.domain = VAR_DOMAIN;
8570 psymbol.aclass = LOC_STATIC;
8571 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8572 psymbol.ginfo.value.address = addr;
8573 where = psymbol_placement::GLOBAL;
8574 }
8575 }
8576 else
8577 {
8578 int has_loc = pdi->d.locdesc != NULL;
8579
8580 /* Static Variable. Skip symbols whose value we cannot know (those
8581 without location descriptors or constant values). */
8582 if (!has_loc && !pdi->has_const_value)
8583 return;
8584
8585 psymbol.domain = VAR_DOMAIN;
8586 psymbol.aclass = LOC_STATIC;
8587 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8588 if (has_loc)
8589 psymbol.ginfo.value.address = addr;
8590 where = psymbol_placement::STATIC;
8591 }
8592 break;
8593 case DW_TAG_array_type:
8594 case DW_TAG_typedef:
8595 case DW_TAG_base_type:
8596 case DW_TAG_subrange_type:
8597 psymbol.domain = VAR_DOMAIN;
8598 psymbol.aclass = LOC_TYPEDEF;
8599 where = psymbol_placement::STATIC;
8600 break;
8601 case DW_TAG_imported_declaration:
8602 case DW_TAG_namespace:
8603 psymbol.domain = VAR_DOMAIN;
8604 psymbol.aclass = LOC_TYPEDEF;
8605 where = psymbol_placement::GLOBAL;
8606 break;
8607 case DW_TAG_module:
8608 /* With Fortran 77 there might be a "BLOCK DATA" module
8609 available without any name. If so, we skip the module as it
8610 doesn't bring any value. */
8611 if (actual_name != nullptr)
8612 {
8613 psymbol.domain = MODULE_DOMAIN;
8614 psymbol.aclass = LOC_TYPEDEF;
8615 where = psymbol_placement::GLOBAL;
8616 }
8617 break;
8618 case DW_TAG_class_type:
8619 case DW_TAG_interface_type:
8620 case DW_TAG_structure_type:
8621 case DW_TAG_union_type:
8622 case DW_TAG_enumeration_type:
8623 /* Skip external references. The DWARF standard says in the section
8624 about "Structure, Union, and Class Type Entries": "An incomplete
8625 structure, union or class type is represented by a structure,
8626 union or class entry that does not have a byte size attribute
8627 and that has a DW_AT_declaration attribute." */
8628 if (!pdi->has_byte_size && pdi->is_declaration)
8629 return;
8630
8631 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8632 static vs. global. */
8633 psymbol.domain = STRUCT_DOMAIN;
8634 psymbol.aclass = LOC_TYPEDEF;
8635 where = (cu->language == language_cplus
8636 ? psymbol_placement::GLOBAL
8637 : psymbol_placement::STATIC);
8638 break;
8639 case DW_TAG_enumerator:
8640 psymbol.domain = VAR_DOMAIN;
8641 psymbol.aclass = LOC_CONST;
8642 where = (cu->language == language_cplus
8643 ? psymbol_placement::GLOBAL
8644 : psymbol_placement::STATIC);
8645 break;
8646 default:
8647 break;
8648 }
8649
8650 if (where.has_value ())
8651 {
8652 if (built_actual_name != nullptr)
8653 actual_name = objfile->intern (actual_name);
8654 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8655 psymbol.ginfo.set_linkage_name (actual_name);
8656 else
8657 {
8658 psymbol.ginfo.set_demangled_name (actual_name,
8659 &objfile->objfile_obstack);
8660 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8661 }
8662 cu->per_cu->v.psymtab->add_psymbol (psymbol, *where, objfile);
8663 }
8664 }
8665
8666 /* Read a partial die corresponding to a namespace; also, add a symbol
8667 corresponding to that namespace to the symbol table. NAMESPACE is
8668 the name of the enclosing namespace. */
8669
8670 static void
8671 add_partial_namespace (struct partial_die_info *pdi,
8672 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8673 int set_addrmap, struct dwarf2_cu *cu)
8674 {
8675 /* Add a symbol for the namespace. */
8676
8677 add_partial_symbol (pdi, cu);
8678
8679 /* Now scan partial symbols in that namespace. */
8680
8681 if (pdi->has_children)
8682 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8683 }
8684
8685 /* Read a partial die corresponding to a Fortran module. */
8686
8687 static void
8688 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8689 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8690 {
8691 /* Add a symbol for the namespace. */
8692
8693 add_partial_symbol (pdi, cu);
8694
8695 /* Now scan partial symbols in that module. */
8696
8697 if (pdi->has_children)
8698 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8699 }
8700
8701 /* Read a partial die corresponding to a subprogram or an inlined
8702 subprogram and create a partial symbol for that subprogram.
8703 When the CU language allows it, this routine also defines a partial
8704 symbol for each nested subprogram that this subprogram contains.
8705 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8706 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8707
8708 PDI may also be a lexical block, in which case we simply search
8709 recursively for subprograms defined inside that lexical block.
8710 Again, this is only performed when the CU language allows this
8711 type of definitions. */
8712
8713 static void
8714 add_partial_subprogram (struct partial_die_info *pdi,
8715 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8716 int set_addrmap, struct dwarf2_cu *cu)
8717 {
8718 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8719 {
8720 if (pdi->has_pc_info)
8721 {
8722 if (pdi->lowpc < *lowpc)
8723 *lowpc = pdi->lowpc;
8724 if (pdi->highpc > *highpc)
8725 *highpc = pdi->highpc;
8726 if (set_addrmap)
8727 {
8728 struct objfile *objfile = cu->per_objfile->objfile;
8729 struct gdbarch *gdbarch = objfile->arch ();
8730 CORE_ADDR baseaddr;
8731 CORE_ADDR this_highpc;
8732 CORE_ADDR this_lowpc;
8733
8734 baseaddr = objfile->text_section_offset ();
8735 this_lowpc
8736 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8737 pdi->lowpc + baseaddr)
8738 - baseaddr);
8739 this_highpc
8740 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8741 pdi->highpc + baseaddr)
8742 - baseaddr);
8743 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8744 this_lowpc, this_highpc - 1,
8745 cu->per_cu->v.psymtab);
8746 }
8747 }
8748
8749 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8750 {
8751 if (!pdi->is_declaration)
8752 /* Ignore subprogram DIEs that do not have a name, they are
8753 illegal. Do not emit a complaint at this point, we will
8754 do so when we convert this psymtab into a symtab. */
8755 if (pdi->name (cu))
8756 add_partial_symbol (pdi, cu);
8757 }
8758 }
8759
8760 if (! pdi->has_children)
8761 return;
8762
8763 if (cu->language == language_ada || cu->language == language_fortran)
8764 {
8765 pdi = pdi->die_child;
8766 while (pdi != NULL)
8767 {
8768 pdi->fixup (cu);
8769 if (pdi->tag == DW_TAG_subprogram
8770 || pdi->tag == DW_TAG_inlined_subroutine
8771 || pdi->tag == DW_TAG_lexical_block)
8772 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8773 pdi = pdi->die_sibling;
8774 }
8775 }
8776 }
8777
8778 /* Read a partial die corresponding to an enumeration type. */
8779
8780 static void
8781 add_partial_enumeration (struct partial_die_info *enum_pdi,
8782 struct dwarf2_cu *cu)
8783 {
8784 struct partial_die_info *pdi;
8785
8786 if (enum_pdi->name (cu) != NULL)
8787 add_partial_symbol (enum_pdi, cu);
8788
8789 pdi = enum_pdi->die_child;
8790 while (pdi)
8791 {
8792 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8793 complaint (_("malformed enumerator DIE ignored"));
8794 else
8795 add_partial_symbol (pdi, cu);
8796 pdi = pdi->die_sibling;
8797 }
8798 }
8799
8800 /* Return the initial uleb128 in the die at INFO_PTR. */
8801
8802 static unsigned int
8803 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8804 {
8805 unsigned int bytes_read;
8806
8807 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8808 }
8809
8810 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8811 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8812
8813 Return the corresponding abbrev, or NULL if the number is zero (indicating
8814 an empty DIE). In either case *BYTES_READ will be set to the length of
8815 the initial number. */
8816
8817 static const struct abbrev_info *
8818 peek_die_abbrev (const die_reader_specs &reader,
8819 const gdb_byte *info_ptr, unsigned int *bytes_read)
8820 {
8821 dwarf2_cu *cu = reader.cu;
8822 bfd *abfd = reader.abfd;
8823 unsigned int abbrev_number
8824 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8825
8826 if (abbrev_number == 0)
8827 return NULL;
8828
8829 const abbrev_info *abbrev
8830 = reader.abbrev_table->lookup_abbrev (abbrev_number);
8831 if (!abbrev)
8832 {
8833 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8834 " at offset %s [in module %s]"),
8835 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8836 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8837 }
8838
8839 return abbrev;
8840 }
8841
8842 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8843 Returns a pointer to the end of a series of DIEs, terminated by an empty
8844 DIE. Any children of the skipped DIEs will also be skipped. */
8845
8846 static const gdb_byte *
8847 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8848 {
8849 while (1)
8850 {
8851 unsigned int bytes_read;
8852 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8853 &bytes_read);
8854
8855 if (abbrev == NULL)
8856 return info_ptr + bytes_read;
8857 else
8858 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8859 }
8860 }
8861
8862 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8863 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8864 abbrev corresponding to that skipped uleb128 should be passed in
8865 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8866 children. */
8867
8868 static const gdb_byte *
8869 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8870 const struct abbrev_info *abbrev)
8871 {
8872 unsigned int bytes_read;
8873 struct attribute attr;
8874 bfd *abfd = reader->abfd;
8875 struct dwarf2_cu *cu = reader->cu;
8876 const gdb_byte *buffer = reader->buffer;
8877 const gdb_byte *buffer_end = reader->buffer_end;
8878 unsigned int form, i;
8879
8880 for (i = 0; i < abbrev->num_attrs; i++)
8881 {
8882 /* The only abbrev we care about is DW_AT_sibling. */
8883 if (abbrev->attrs[i].name == DW_AT_sibling)
8884 {
8885 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8886 if (attr.form == DW_FORM_ref_addr)
8887 complaint (_("ignoring absolute DW_AT_sibling"));
8888 else
8889 {
8890 sect_offset off = attr.get_ref_die_offset ();
8891 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8892
8893 if (sibling_ptr < info_ptr)
8894 complaint (_("DW_AT_sibling points backwards"));
8895 else if (sibling_ptr > reader->buffer_end)
8896 reader->die_section->overflow_complaint ();
8897 else
8898 return sibling_ptr;
8899 }
8900 }
8901
8902 /* If it isn't DW_AT_sibling, skip this attribute. */
8903 form = abbrev->attrs[i].form;
8904 skip_attribute:
8905 switch (form)
8906 {
8907 case DW_FORM_ref_addr:
8908 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8909 and later it is offset sized. */
8910 if (cu->header.version == 2)
8911 info_ptr += cu->header.addr_size;
8912 else
8913 info_ptr += cu->header.offset_size;
8914 break;
8915 case DW_FORM_GNU_ref_alt:
8916 info_ptr += cu->header.offset_size;
8917 break;
8918 case DW_FORM_addr:
8919 info_ptr += cu->header.addr_size;
8920 break;
8921 case DW_FORM_data1:
8922 case DW_FORM_ref1:
8923 case DW_FORM_flag:
8924 case DW_FORM_strx1:
8925 info_ptr += 1;
8926 break;
8927 case DW_FORM_flag_present:
8928 case DW_FORM_implicit_const:
8929 break;
8930 case DW_FORM_data2:
8931 case DW_FORM_ref2:
8932 case DW_FORM_strx2:
8933 info_ptr += 2;
8934 break;
8935 case DW_FORM_strx3:
8936 info_ptr += 3;
8937 break;
8938 case DW_FORM_data4:
8939 case DW_FORM_ref4:
8940 case DW_FORM_strx4:
8941 info_ptr += 4;
8942 break;
8943 case DW_FORM_data8:
8944 case DW_FORM_ref8:
8945 case DW_FORM_ref_sig8:
8946 info_ptr += 8;
8947 break;
8948 case DW_FORM_data16:
8949 info_ptr += 16;
8950 break;
8951 case DW_FORM_string:
8952 read_direct_string (abfd, info_ptr, &bytes_read);
8953 info_ptr += bytes_read;
8954 break;
8955 case DW_FORM_sec_offset:
8956 case DW_FORM_strp:
8957 case DW_FORM_GNU_strp_alt:
8958 info_ptr += cu->header.offset_size;
8959 break;
8960 case DW_FORM_exprloc:
8961 case DW_FORM_block:
8962 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8963 info_ptr += bytes_read;
8964 break;
8965 case DW_FORM_block1:
8966 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8967 break;
8968 case DW_FORM_block2:
8969 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8970 break;
8971 case DW_FORM_block4:
8972 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8973 break;
8974 case DW_FORM_addrx:
8975 case DW_FORM_strx:
8976 case DW_FORM_sdata:
8977 case DW_FORM_udata:
8978 case DW_FORM_ref_udata:
8979 case DW_FORM_GNU_addr_index:
8980 case DW_FORM_GNU_str_index:
8981 case DW_FORM_rnglistx:
8982 case DW_FORM_loclistx:
8983 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8984 break;
8985 case DW_FORM_indirect:
8986 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8987 info_ptr += bytes_read;
8988 /* We need to continue parsing from here, so just go back to
8989 the top. */
8990 goto skip_attribute;
8991
8992 default:
8993 error (_("Dwarf Error: Cannot handle %s "
8994 "in DWARF reader [in module %s]"),
8995 dwarf_form_name (form),
8996 bfd_get_filename (abfd));
8997 }
8998 }
8999
9000 if (abbrev->has_children)
9001 return skip_children (reader, info_ptr);
9002 else
9003 return info_ptr;
9004 }
9005
9006 /* Locate ORIG_PDI's sibling.
9007 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9008
9009 static const gdb_byte *
9010 locate_pdi_sibling (const struct die_reader_specs *reader,
9011 struct partial_die_info *orig_pdi,
9012 const gdb_byte *info_ptr)
9013 {
9014 /* Do we know the sibling already? */
9015
9016 if (orig_pdi->sibling)
9017 return orig_pdi->sibling;
9018
9019 /* Are there any children to deal with? */
9020
9021 if (!orig_pdi->has_children)
9022 return info_ptr;
9023
9024 /* Skip the children the long way. */
9025
9026 return skip_children (reader, info_ptr);
9027 }
9028
9029 /* Expand this partial symbol table into a full symbol table. SELF is
9030 not NULL. */
9031
9032 void
9033 dwarf2_psymtab::read_symtab (struct objfile *objfile)
9034 {
9035 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9036
9037 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
9038
9039 /* If this psymtab is constructed from a debug-only objfile, the
9040 has_section_at_zero flag will not necessarily be correct. We
9041 can get the correct value for this flag by looking at the data
9042 associated with the (presumably stripped) associated objfile. */
9043 if (objfile->separate_debug_objfile_backlink)
9044 {
9045 dwarf2_per_objfile *per_objfile_backlink
9046 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9047
9048 per_objfile->per_bfd->has_section_at_zero
9049 = per_objfile_backlink->per_bfd->has_section_at_zero;
9050 }
9051
9052 expand_psymtab (objfile);
9053
9054 process_cu_includes (per_objfile);
9055 }
9056 \f
9057 /* Reading in full CUs. */
9058
9059 /* Add PER_CU to the queue. */
9060
9061 static void
9062 queue_comp_unit (dwarf2_per_cu_data *per_cu,
9063 dwarf2_per_objfile *per_objfile,
9064 enum language pretend_language)
9065 {
9066 per_cu->queued = 1;
9067
9068 gdb_assert (per_objfile->per_bfd->queue.has_value ());
9069 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
9070 }
9071
9072 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
9073
9074 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9075 dependency.
9076
9077 Return true if maybe_queue_comp_unit requires the caller to load the CU's
9078 DIEs, false otherwise.
9079
9080 Explanation: there is an invariant that if a CU is queued for expansion
9081 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
9082 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
9083 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
9084 are not yet loaded, the the caller must load the CU's DIEs to ensure the
9085 invariant is respected.
9086
9087 The caller is therefore not required to load the CU's DIEs (we return false)
9088 if:
9089
9090 - the CU is already expanded, and therefore does not get enqueued
9091 - the CU gets enqueued for expansion, but its DIEs are already loaded
9092
9093 Note that the caller should not use this function's return value as an
9094 indicator of whether the CU's DIEs are loaded right now, it should check
9095 that by calling `dwarf2_per_objfile::get_cu` instead. */
9096
9097 static int
9098 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9099 dwarf2_per_cu_data *per_cu,
9100 dwarf2_per_objfile *per_objfile,
9101 enum language pretend_language)
9102 {
9103 /* We may arrive here during partial symbol reading, if we need full
9104 DIEs to process an unusual case (e.g. template arguments). Do
9105 not queue PER_CU, just tell our caller to load its DIEs. */
9106 if (per_cu->per_bfd->reading_partial_symbols)
9107 {
9108 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9109
9110 if (cu == NULL || cu->dies == NULL)
9111 return 1;
9112 return 0;
9113 }
9114
9115 /* Mark the dependence relation so that we don't flush PER_CU
9116 too early. */
9117 if (dependent_cu != NULL)
9118 dwarf2_add_dependence (dependent_cu, per_cu);
9119
9120 /* If it's already on the queue, we have nothing to do. */
9121 if (per_cu->queued)
9122 {
9123 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
9124 loaded. */
9125 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
9126
9127 /* If the CU is queued for expansion, it should not already be
9128 expanded. */
9129 gdb_assert (!per_objfile->symtab_set_p (per_cu));
9130
9131 /* The DIEs are already loaded, the caller doesn't need to do it. */
9132 return 0;
9133 }
9134
9135 bool queued = false;
9136 if (!per_objfile->symtab_set_p (per_cu))
9137 {
9138 /* Add it to the queue. */
9139 queue_comp_unit (per_cu, per_objfile, pretend_language);
9140 queued = true;
9141 }
9142
9143 /* If the compilation unit is already loaded, just mark it as
9144 used. */
9145 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9146 if (cu != nullptr)
9147 cu->last_used = 0;
9148
9149 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
9150 and the DIEs are not already loaded. */
9151 return queued && cu == nullptr;
9152 }
9153
9154 /* Process the queue. */
9155
9156 static void
9157 process_queue (dwarf2_per_objfile *per_objfile)
9158 {
9159 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
9160 objfile_name (per_objfile->objfile));
9161
9162 /* The queue starts out with one item, but following a DIE reference
9163 may load a new CU, adding it to the end of the queue. */
9164 while (!per_objfile->per_bfd->queue->empty ())
9165 {
9166 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
9167 dwarf2_per_cu_data *per_cu = item.per_cu;
9168
9169 if (!per_objfile->symtab_set_p (per_cu))
9170 {
9171 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9172
9173 /* Skip dummy CUs. */
9174 if (cu != nullptr)
9175 {
9176 unsigned int debug_print_threshold;
9177 char buf[100];
9178
9179 if (per_cu->is_debug_types)
9180 {
9181 struct signatured_type *sig_type =
9182 (struct signatured_type *) per_cu;
9183
9184 sprintf (buf, "TU %s at offset %s",
9185 hex_string (sig_type->signature),
9186 sect_offset_str (per_cu->sect_off));
9187 /* There can be 100s of TUs.
9188 Only print them in verbose mode. */
9189 debug_print_threshold = 2;
9190 }
9191 else
9192 {
9193 sprintf (buf, "CU at offset %s",
9194 sect_offset_str (per_cu->sect_off));
9195 debug_print_threshold = 1;
9196 }
9197
9198 if (dwarf_read_debug >= debug_print_threshold)
9199 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
9200
9201 if (per_cu->is_debug_types)
9202 process_full_type_unit (cu, item.pretend_language);
9203 else
9204 process_full_comp_unit (cu, item.pretend_language);
9205
9206 if (dwarf_read_debug >= debug_print_threshold)
9207 dwarf_read_debug_printf ("Done expanding %s", buf);
9208 }
9209 }
9210
9211 per_cu->queued = 0;
9212 per_objfile->per_bfd->queue->pop ();
9213 }
9214
9215 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
9216 objfile_name (per_objfile->objfile));
9217 }
9218
9219 /* Read in full symbols for PST, and anything it depends on. */
9220
9221 void
9222 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9223 {
9224 gdb_assert (!readin_p (objfile));
9225
9226 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9227 free_cached_comp_units freer (per_objfile);
9228 expand_dependencies (objfile);
9229
9230 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9231 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9232 }
9233
9234 /* See psympriv.h. */
9235
9236 bool
9237 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9238 {
9239 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9240 return per_objfile->symtab_set_p (per_cu_data);
9241 }
9242
9243 /* See psympriv.h. */
9244
9245 compunit_symtab *
9246 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9247 {
9248 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9249 return per_objfile->get_symtab (per_cu_data);
9250 }
9251
9252 /* Trivial hash function for die_info: the hash value of a DIE
9253 is its offset in .debug_info for this objfile. */
9254
9255 static hashval_t
9256 die_hash (const void *item)
9257 {
9258 const struct die_info *die = (const struct die_info *) item;
9259
9260 return to_underlying (die->sect_off);
9261 }
9262
9263 /* Trivial comparison function for die_info structures: two DIEs
9264 are equal if they have the same offset. */
9265
9266 static int
9267 die_eq (const void *item_lhs, const void *item_rhs)
9268 {
9269 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9270 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9271
9272 return die_lhs->sect_off == die_rhs->sect_off;
9273 }
9274
9275 /* Load the DIEs associated with PER_CU into memory.
9276
9277 In some cases, the caller, while reading partial symbols, will need to load
9278 the full symbols for the CU for some reason. It will already have a
9279 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
9280 rather than creating a new one. */
9281
9282 static void
9283 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9284 dwarf2_per_objfile *per_objfile,
9285 dwarf2_cu *existing_cu,
9286 bool skip_partial,
9287 enum language pretend_language)
9288 {
9289 gdb_assert (! this_cu->is_debug_types);
9290
9291 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
9292 if (reader.dummy_p)
9293 return;
9294
9295 struct dwarf2_cu *cu = reader.cu;
9296 const gdb_byte *info_ptr = reader.info_ptr;
9297
9298 gdb_assert (cu->die_hash == NULL);
9299 cu->die_hash =
9300 htab_create_alloc_ex (cu->header.length / 12,
9301 die_hash,
9302 die_eq,
9303 NULL,
9304 &cu->comp_unit_obstack,
9305 hashtab_obstack_allocate,
9306 dummy_obstack_deallocate);
9307
9308 if (reader.comp_unit_die->has_children)
9309 reader.comp_unit_die->child
9310 = read_die_and_siblings (&reader, reader.info_ptr,
9311 &info_ptr, reader.comp_unit_die);
9312 cu->dies = reader.comp_unit_die;
9313 /* comp_unit_die is not stored in die_hash, no need. */
9314
9315 /* We try not to read any attributes in this function, because not
9316 all CUs needed for references have been loaded yet, and symbol
9317 table processing isn't initialized. But we have to set the CU language,
9318 or we won't be able to build types correctly.
9319 Similarly, if we do not read the producer, we can not apply
9320 producer-specific interpretation. */
9321 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9322
9323 reader.keep ();
9324 }
9325
9326 /* Add a DIE to the delayed physname list. */
9327
9328 static void
9329 add_to_method_list (struct type *type, int fnfield_index, int index,
9330 const char *name, struct die_info *die,
9331 struct dwarf2_cu *cu)
9332 {
9333 struct delayed_method_info mi;
9334 mi.type = type;
9335 mi.fnfield_index = fnfield_index;
9336 mi.index = index;
9337 mi.name = name;
9338 mi.die = die;
9339 cu->method_list.push_back (mi);
9340 }
9341
9342 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9343 "const" / "volatile". If so, decrements LEN by the length of the
9344 modifier and return true. Otherwise return false. */
9345
9346 template<size_t N>
9347 static bool
9348 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9349 {
9350 size_t mod_len = sizeof (mod) - 1;
9351 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9352 {
9353 len -= mod_len;
9354 return true;
9355 }
9356 return false;
9357 }
9358
9359 /* Compute the physnames of any methods on the CU's method list.
9360
9361 The computation of method physnames is delayed in order to avoid the
9362 (bad) condition that one of the method's formal parameters is of an as yet
9363 incomplete type. */
9364
9365 static void
9366 compute_delayed_physnames (struct dwarf2_cu *cu)
9367 {
9368 /* Only C++ delays computing physnames. */
9369 if (cu->method_list.empty ())
9370 return;
9371 gdb_assert (cu->language == language_cplus);
9372
9373 for (const delayed_method_info &mi : cu->method_list)
9374 {
9375 const char *physname;
9376 struct fn_fieldlist *fn_flp
9377 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9378 physname = dwarf2_physname (mi.name, mi.die, cu);
9379 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9380 = physname ? physname : "";
9381
9382 /* Since there's no tag to indicate whether a method is a
9383 const/volatile overload, extract that information out of the
9384 demangled name. */
9385 if (physname != NULL)
9386 {
9387 size_t len = strlen (physname);
9388
9389 while (1)
9390 {
9391 if (physname[len] == ')') /* shortcut */
9392 break;
9393 else if (check_modifier (physname, len, " const"))
9394 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9395 else if (check_modifier (physname, len, " volatile"))
9396 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9397 else
9398 break;
9399 }
9400 }
9401 }
9402
9403 /* The list is no longer needed. */
9404 cu->method_list.clear ();
9405 }
9406
9407 /* Go objects should be embedded in a DW_TAG_module DIE,
9408 and it's not clear if/how imported objects will appear.
9409 To keep Go support simple until that's worked out,
9410 go back through what we've read and create something usable.
9411 We could do this while processing each DIE, and feels kinda cleaner,
9412 but that way is more invasive.
9413 This is to, for example, allow the user to type "p var" or "b main"
9414 without having to specify the package name, and allow lookups
9415 of module.object to work in contexts that use the expression
9416 parser. */
9417
9418 static void
9419 fixup_go_packaging (struct dwarf2_cu *cu)
9420 {
9421 gdb::unique_xmalloc_ptr<char> package_name;
9422 struct pending *list;
9423 int i;
9424
9425 for (list = *cu->get_builder ()->get_global_symbols ();
9426 list != NULL;
9427 list = list->next)
9428 {
9429 for (i = 0; i < list->nsyms; ++i)
9430 {
9431 struct symbol *sym = list->symbol[i];
9432
9433 if (sym->language () == language_go
9434 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9435 {
9436 gdb::unique_xmalloc_ptr<char> this_package_name
9437 (go_symbol_package_name (sym));
9438
9439 if (this_package_name == NULL)
9440 continue;
9441 if (package_name == NULL)
9442 package_name = std::move (this_package_name);
9443 else
9444 {
9445 struct objfile *objfile = cu->per_objfile->objfile;
9446 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9447 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9448 (symbol_symtab (sym) != NULL
9449 ? symtab_to_filename_for_display
9450 (symbol_symtab (sym))
9451 : objfile_name (objfile)),
9452 this_package_name.get (), package_name.get ());
9453 }
9454 }
9455 }
9456 }
9457
9458 if (package_name != NULL)
9459 {
9460 struct objfile *objfile = cu->per_objfile->objfile;
9461 const char *saved_package_name = objfile->intern (package_name.get ());
9462 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9463 saved_package_name);
9464 struct symbol *sym;
9465
9466 sym = new (&objfile->objfile_obstack) symbol;
9467 sym->set_language (language_go, &objfile->objfile_obstack);
9468 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9469 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9470 e.g., "main" finds the "main" module and not C's main(). */
9471 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9472 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9473 SYMBOL_TYPE (sym) = type;
9474
9475 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9476 }
9477 }
9478
9479 /* Allocate a fully-qualified name consisting of the two parts on the
9480 obstack. */
9481
9482 static const char *
9483 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9484 {
9485 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9486 }
9487
9488 /* A helper that allocates a variant part to attach to a Rust enum
9489 type. OBSTACK is where the results should be allocated. TYPE is
9490 the type we're processing. DISCRIMINANT_INDEX is the index of the
9491 discriminant. It must be the index of one of the fields of TYPE,
9492 or -1 to mean there is no discriminant (univariant enum).
9493 DEFAULT_INDEX is the index of the default field; or -1 if there is
9494 no default. RANGES is indexed by "effective" field number (the
9495 field index, but omitting the discriminant and default fields) and
9496 must hold the discriminant values used by the variants. Note that
9497 RANGES must have a lifetime at least as long as OBSTACK -- either
9498 already allocated on it, or static. */
9499
9500 static void
9501 alloc_rust_variant (struct obstack *obstack, struct type *type,
9502 int discriminant_index, int default_index,
9503 gdb::array_view<discriminant_range> ranges)
9504 {
9505 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9506 gdb_assert (discriminant_index == -1
9507 || (discriminant_index >= 0
9508 && discriminant_index < type->num_fields ()));
9509 gdb_assert (default_index == -1
9510 || (default_index >= 0 && default_index < type->num_fields ()));
9511
9512 /* We have one variant for each non-discriminant field. */
9513 int n_variants = type->num_fields ();
9514 if (discriminant_index != -1)
9515 --n_variants;
9516
9517 variant *variants = new (obstack) variant[n_variants];
9518 int var_idx = 0;
9519 int range_idx = 0;
9520 for (int i = 0; i < type->num_fields (); ++i)
9521 {
9522 if (i == discriminant_index)
9523 continue;
9524
9525 variants[var_idx].first_field = i;
9526 variants[var_idx].last_field = i + 1;
9527
9528 /* The default field does not need a range, but other fields do.
9529 We skipped the discriminant above. */
9530 if (i != default_index)
9531 {
9532 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9533 ++range_idx;
9534 }
9535
9536 ++var_idx;
9537 }
9538
9539 gdb_assert (range_idx == ranges.size ());
9540 gdb_assert (var_idx == n_variants);
9541
9542 variant_part *part = new (obstack) variant_part;
9543 part->discriminant_index = discriminant_index;
9544 /* If there is no discriminant, then whether it is signed is of no
9545 consequence. */
9546 part->is_unsigned
9547 = (discriminant_index == -1
9548 ? false
9549 : type->field (discriminant_index).type ()->is_unsigned ());
9550 part->variants = gdb::array_view<variant> (variants, n_variants);
9551
9552 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9553 gdb::array_view<variant_part> *prop_value
9554 = new (storage) gdb::array_view<variant_part> (part, 1);
9555
9556 struct dynamic_prop prop;
9557 prop.set_variant_parts (prop_value);
9558
9559 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9560 }
9561
9562 /* Some versions of rustc emitted enums in an unusual way.
9563
9564 Ordinary enums were emitted as unions. The first element of each
9565 structure in the union was named "RUST$ENUM$DISR". This element
9566 held the discriminant.
9567
9568 These versions of Rust also implemented the "non-zero"
9569 optimization. When the enum had two values, and one is empty and
9570 the other holds a pointer that cannot be zero, the pointer is used
9571 as the discriminant, with a zero value meaning the empty variant.
9572 Here, the union's first member is of the form
9573 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9574 where the fieldnos are the indices of the fields that should be
9575 traversed in order to find the field (which may be several fields deep)
9576 and the variantname is the name of the variant of the case when the
9577 field is zero.
9578
9579 This function recognizes whether TYPE is of one of these forms,
9580 and, if so, smashes it to be a variant type. */
9581
9582 static void
9583 quirk_rust_enum (struct type *type, struct objfile *objfile)
9584 {
9585 gdb_assert (type->code () == TYPE_CODE_UNION);
9586
9587 /* We don't need to deal with empty enums. */
9588 if (type->num_fields () == 0)
9589 return;
9590
9591 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9592 if (type->num_fields () == 1
9593 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9594 {
9595 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9596
9597 /* Decode the field name to find the offset of the
9598 discriminant. */
9599 ULONGEST bit_offset = 0;
9600 struct type *field_type = type->field (0).type ();
9601 while (name[0] >= '0' && name[0] <= '9')
9602 {
9603 char *tail;
9604 unsigned long index = strtoul (name, &tail, 10);
9605 name = tail;
9606 if (*name != '$'
9607 || index >= field_type->num_fields ()
9608 || (TYPE_FIELD_LOC_KIND (field_type, index)
9609 != FIELD_LOC_KIND_BITPOS))
9610 {
9611 complaint (_("Could not parse Rust enum encoding string \"%s\""
9612 "[in module %s]"),
9613 TYPE_FIELD_NAME (type, 0),
9614 objfile_name (objfile));
9615 return;
9616 }
9617 ++name;
9618
9619 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9620 field_type = field_type->field (index).type ();
9621 }
9622
9623 /* Smash this type to be a structure type. We have to do this
9624 because the type has already been recorded. */
9625 type->set_code (TYPE_CODE_STRUCT);
9626 type->set_num_fields (3);
9627 /* Save the field we care about. */
9628 struct field saved_field = type->field (0);
9629 type->set_fields
9630 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9631
9632 /* Put the discriminant at index 0. */
9633 type->field (0).set_type (field_type);
9634 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9635 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9636 SET_FIELD_BITPOS (type->field (0), bit_offset);
9637
9638 /* The order of fields doesn't really matter, so put the real
9639 field at index 1 and the data-less field at index 2. */
9640 type->field (1) = saved_field;
9641 TYPE_FIELD_NAME (type, 1)
9642 = rust_last_path_segment (type->field (1).type ()->name ());
9643 type->field (1).type ()->set_name
9644 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9645 TYPE_FIELD_NAME (type, 1)));
9646
9647 const char *dataless_name
9648 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9649 name);
9650 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9651 dataless_name);
9652 type->field (2).set_type (dataless_type);
9653 /* NAME points into the original discriminant name, which
9654 already has the correct lifetime. */
9655 TYPE_FIELD_NAME (type, 2) = name;
9656 SET_FIELD_BITPOS (type->field (2), 0);
9657
9658 /* Indicate that this is a variant type. */
9659 static discriminant_range ranges[1] = { { 0, 0 } };
9660 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9661 }
9662 /* A union with a single anonymous field is probably an old-style
9663 univariant enum. */
9664 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9665 {
9666 /* Smash this type to be a structure type. We have to do this
9667 because the type has already been recorded. */
9668 type->set_code (TYPE_CODE_STRUCT);
9669
9670 struct type *field_type = type->field (0).type ();
9671 const char *variant_name
9672 = rust_last_path_segment (field_type->name ());
9673 TYPE_FIELD_NAME (type, 0) = variant_name;
9674 field_type->set_name
9675 (rust_fully_qualify (&objfile->objfile_obstack,
9676 type->name (), variant_name));
9677
9678 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
9679 }
9680 else
9681 {
9682 struct type *disr_type = nullptr;
9683 for (int i = 0; i < type->num_fields (); ++i)
9684 {
9685 disr_type = type->field (i).type ();
9686
9687 if (disr_type->code () != TYPE_CODE_STRUCT)
9688 {
9689 /* All fields of a true enum will be structs. */
9690 return;
9691 }
9692 else if (disr_type->num_fields () == 0)
9693 {
9694 /* Could be data-less variant, so keep going. */
9695 disr_type = nullptr;
9696 }
9697 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9698 "RUST$ENUM$DISR") != 0)
9699 {
9700 /* Not a Rust enum. */
9701 return;
9702 }
9703 else
9704 {
9705 /* Found one. */
9706 break;
9707 }
9708 }
9709
9710 /* If we got here without a discriminant, then it's probably
9711 just a union. */
9712 if (disr_type == nullptr)
9713 return;
9714
9715 /* Smash this type to be a structure type. We have to do this
9716 because the type has already been recorded. */
9717 type->set_code (TYPE_CODE_STRUCT);
9718
9719 /* Make space for the discriminant field. */
9720 struct field *disr_field = &disr_type->field (0);
9721 field *new_fields
9722 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9723 * sizeof (struct field)));
9724 memcpy (new_fields + 1, type->fields (),
9725 type->num_fields () * sizeof (struct field));
9726 type->set_fields (new_fields);
9727 type->set_num_fields (type->num_fields () + 1);
9728
9729 /* Install the discriminant at index 0 in the union. */
9730 type->field (0) = *disr_field;
9731 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9732 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9733
9734 /* We need a way to find the correct discriminant given a
9735 variant name. For convenience we build a map here. */
9736 struct type *enum_type = disr_field->type ();
9737 std::unordered_map<std::string, ULONGEST> discriminant_map;
9738 for (int i = 0; i < enum_type->num_fields (); ++i)
9739 {
9740 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9741 {
9742 const char *name
9743 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9744 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9745 }
9746 }
9747
9748 int n_fields = type->num_fields ();
9749 /* We don't need a range entry for the discriminant, but we do
9750 need one for every other field, as there is no default
9751 variant. */
9752 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9753 discriminant_range,
9754 n_fields - 1);
9755 /* Skip the discriminant here. */
9756 for (int i = 1; i < n_fields; ++i)
9757 {
9758 /* Find the final word in the name of this variant's type.
9759 That name can be used to look up the correct
9760 discriminant. */
9761 const char *variant_name
9762 = rust_last_path_segment (type->field (i).type ()->name ());
9763
9764 auto iter = discriminant_map.find (variant_name);
9765 if (iter != discriminant_map.end ())
9766 {
9767 ranges[i - 1].low = iter->second;
9768 ranges[i - 1].high = iter->second;
9769 }
9770
9771 /* In Rust, each element should have the size of the
9772 enclosing enum. */
9773 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9774
9775 /* Remove the discriminant field, if it exists. */
9776 struct type *sub_type = type->field (i).type ();
9777 if (sub_type->num_fields () > 0)
9778 {
9779 sub_type->set_num_fields (sub_type->num_fields () - 1);
9780 sub_type->set_fields (sub_type->fields () + 1);
9781 }
9782 TYPE_FIELD_NAME (type, i) = variant_name;
9783 sub_type->set_name
9784 (rust_fully_qualify (&objfile->objfile_obstack,
9785 type->name (), variant_name));
9786 }
9787
9788 /* Indicate that this is a variant type. */
9789 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9790 gdb::array_view<discriminant_range> (ranges,
9791 n_fields - 1));
9792 }
9793 }
9794
9795 /* Rewrite some Rust unions to be structures with variants parts. */
9796
9797 static void
9798 rust_union_quirks (struct dwarf2_cu *cu)
9799 {
9800 gdb_assert (cu->language == language_rust);
9801 for (type *type_ : cu->rust_unions)
9802 quirk_rust_enum (type_, cu->per_objfile->objfile);
9803 /* We don't need this any more. */
9804 cu->rust_unions.clear ();
9805 }
9806
9807 /* See read.h. */
9808
9809 type_unit_group_unshareable *
9810 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9811 {
9812 auto iter = this->m_type_units.find (tu_group);
9813 if (iter != this->m_type_units.end ())
9814 return iter->second.get ();
9815
9816 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9817 type_unit_group_unshareable *result = uniq.get ();
9818 this->m_type_units[tu_group] = std::move (uniq);
9819 return result;
9820 }
9821
9822 struct type *
9823 dwarf2_per_objfile::get_type_for_signatured_type
9824 (signatured_type *sig_type) const
9825 {
9826 auto iter = this->m_type_map.find (sig_type);
9827 if (iter == this->m_type_map.end ())
9828 return nullptr;
9829
9830 return iter->second;
9831 }
9832
9833 void dwarf2_per_objfile::set_type_for_signatured_type
9834 (signatured_type *sig_type, struct type *type)
9835 {
9836 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9837
9838 this->m_type_map[sig_type] = type;
9839 }
9840
9841 /* A helper function for computing the list of all symbol tables
9842 included by PER_CU. */
9843
9844 static void
9845 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9846 htab_t all_children, htab_t all_type_symtabs,
9847 dwarf2_per_cu_data *per_cu,
9848 dwarf2_per_objfile *per_objfile,
9849 struct compunit_symtab *immediate_parent)
9850 {
9851 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9852 if (*slot != NULL)
9853 {
9854 /* This inclusion and its children have been processed. */
9855 return;
9856 }
9857
9858 *slot = per_cu;
9859
9860 /* Only add a CU if it has a symbol table. */
9861 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9862 if (cust != NULL)
9863 {
9864 /* If this is a type unit only add its symbol table if we haven't
9865 seen it yet (type unit per_cu's can share symtabs). */
9866 if (per_cu->is_debug_types)
9867 {
9868 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9869 if (*slot == NULL)
9870 {
9871 *slot = cust;
9872 result->push_back (cust);
9873 if (cust->user == NULL)
9874 cust->user = immediate_parent;
9875 }
9876 }
9877 else
9878 {
9879 result->push_back (cust);
9880 if (cust->user == NULL)
9881 cust->user = immediate_parent;
9882 }
9883 }
9884
9885 if (!per_cu->imported_symtabs_empty ())
9886 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9887 {
9888 recursively_compute_inclusions (result, all_children,
9889 all_type_symtabs, ptr, per_objfile,
9890 cust);
9891 }
9892 }
9893
9894 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9895 PER_CU. */
9896
9897 static void
9898 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9899 dwarf2_per_objfile *per_objfile)
9900 {
9901 gdb_assert (! per_cu->is_debug_types);
9902
9903 if (!per_cu->imported_symtabs_empty ())
9904 {
9905 int len;
9906 std::vector<compunit_symtab *> result_symtabs;
9907 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9908
9909 /* If we don't have a symtab, we can just skip this case. */
9910 if (cust == NULL)
9911 return;
9912
9913 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9914 htab_eq_pointer,
9915 NULL, xcalloc, xfree));
9916 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9917 htab_eq_pointer,
9918 NULL, xcalloc, xfree));
9919
9920 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9921 {
9922 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9923 all_type_symtabs.get (), ptr,
9924 per_objfile, cust);
9925 }
9926
9927 /* Now we have a transitive closure of all the included symtabs. */
9928 len = result_symtabs.size ();
9929 cust->includes
9930 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9931 struct compunit_symtab *, len + 1);
9932 memcpy (cust->includes, result_symtabs.data (),
9933 len * sizeof (compunit_symtab *));
9934 cust->includes[len] = NULL;
9935 }
9936 }
9937
9938 /* Compute the 'includes' field for the symtabs of all the CUs we just
9939 read. */
9940
9941 static void
9942 process_cu_includes (dwarf2_per_objfile *per_objfile)
9943 {
9944 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
9945 {
9946 if (! iter->is_debug_types)
9947 compute_compunit_symtab_includes (iter, per_objfile);
9948 }
9949
9950 per_objfile->per_bfd->just_read_cus.clear ();
9951 }
9952
9953 /* Generate full symbol information for CU, whose DIEs have
9954 already been loaded into memory. */
9955
9956 static void
9957 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9958 {
9959 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9960 struct objfile *objfile = per_objfile->objfile;
9961 struct gdbarch *gdbarch = objfile->arch ();
9962 CORE_ADDR lowpc, highpc;
9963 struct compunit_symtab *cust;
9964 CORE_ADDR baseaddr;
9965 struct block *static_block;
9966 CORE_ADDR addr;
9967
9968 baseaddr = objfile->text_section_offset ();
9969
9970 /* Clear the list here in case something was left over. */
9971 cu->method_list.clear ();
9972
9973 cu->language = pretend_language;
9974 cu->language_defn = language_def (cu->language);
9975
9976 dwarf2_find_base_address (cu->dies, cu);
9977
9978 /* Do line number decoding in read_file_scope () */
9979 process_die (cu->dies, cu);
9980
9981 /* For now fudge the Go package. */
9982 if (cu->language == language_go)
9983 fixup_go_packaging (cu);
9984
9985 /* Now that we have processed all the DIEs in the CU, all the types
9986 should be complete, and it should now be safe to compute all of the
9987 physnames. */
9988 compute_delayed_physnames (cu);
9989
9990 if (cu->language == language_rust)
9991 rust_union_quirks (cu);
9992
9993 /* Some compilers don't define a DW_AT_high_pc attribute for the
9994 compilation unit. If the DW_AT_high_pc is missing, synthesize
9995 it, by scanning the DIE's below the compilation unit. */
9996 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9997
9998 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9999 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10000
10001 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10002 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10003 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10004 addrmap to help ensure it has an accurate map of pc values belonging to
10005 this comp unit. */
10006 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10007
10008 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10009 SECT_OFF_TEXT (objfile),
10010 0);
10011
10012 if (cust != NULL)
10013 {
10014 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10015
10016 /* Set symtab language to language from DW_AT_language. If the
10017 compilation is from a C file generated by language preprocessors, do
10018 not set the language if it was already deduced by start_subfile. */
10019 if (!(cu->language == language_c
10020 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10021 COMPUNIT_FILETABS (cust)->language = cu->language;
10022
10023 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10024 produce DW_AT_location with location lists but it can be possibly
10025 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10026 there were bugs in prologue debug info, fixed later in GCC-4.5
10027 by "unwind info for epilogues" patch (which is not directly related).
10028
10029 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10030 needed, it would be wrong due to missing DW_AT_producer there.
10031
10032 Still one can confuse GDB by using non-standard GCC compilation
10033 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10034 */
10035 if (cu->has_loclist && gcc_4_minor >= 5)
10036 cust->locations_valid = 1;
10037
10038 if (gcc_4_minor >= 5)
10039 cust->epilogue_unwind_valid = 1;
10040
10041 cust->call_site_htab = cu->call_site_htab;
10042 }
10043
10044 per_objfile->set_symtab (cu->per_cu, cust);
10045
10046 /* Push it for inclusion processing later. */
10047 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
10048
10049 /* Not needed any more. */
10050 cu->reset_builder ();
10051 }
10052
10053 /* Generate full symbol information for type unit CU, whose DIEs have
10054 already been loaded into memory. */
10055
10056 static void
10057 process_full_type_unit (dwarf2_cu *cu,
10058 enum language pretend_language)
10059 {
10060 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10061 struct objfile *objfile = per_objfile->objfile;
10062 struct compunit_symtab *cust;
10063 struct signatured_type *sig_type;
10064
10065 gdb_assert (cu->per_cu->is_debug_types);
10066 sig_type = (struct signatured_type *) cu->per_cu;
10067
10068 /* Clear the list here in case something was left over. */
10069 cu->method_list.clear ();
10070
10071 cu->language = pretend_language;
10072 cu->language_defn = language_def (cu->language);
10073
10074 /* The symbol tables are set up in read_type_unit_scope. */
10075 process_die (cu->dies, cu);
10076
10077 /* For now fudge the Go package. */
10078 if (cu->language == language_go)
10079 fixup_go_packaging (cu);
10080
10081 /* Now that we have processed all the DIEs in the CU, all the types
10082 should be complete, and it should now be safe to compute all of the
10083 physnames. */
10084 compute_delayed_physnames (cu);
10085
10086 if (cu->language == language_rust)
10087 rust_union_quirks (cu);
10088
10089 /* TUs share symbol tables.
10090 If this is the first TU to use this symtab, complete the construction
10091 of it with end_expandable_symtab. Otherwise, complete the addition of
10092 this TU's symbols to the existing symtab. */
10093 type_unit_group_unshareable *tug_unshare =
10094 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
10095 if (tug_unshare->compunit_symtab == NULL)
10096 {
10097 buildsym_compunit *builder = cu->get_builder ();
10098 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10099 tug_unshare->compunit_symtab = cust;
10100
10101 if (cust != NULL)
10102 {
10103 /* Set symtab language to language from DW_AT_language. If the
10104 compilation is from a C file generated by language preprocessors,
10105 do not set the language if it was already deduced by
10106 start_subfile. */
10107 if (!(cu->language == language_c
10108 && COMPUNIT_FILETABS (cust)->language != language_c))
10109 COMPUNIT_FILETABS (cust)->language = cu->language;
10110 }
10111 }
10112 else
10113 {
10114 cu->get_builder ()->augment_type_symtab ();
10115 cust = tug_unshare->compunit_symtab;
10116 }
10117
10118 per_objfile->set_symtab (cu->per_cu, cust);
10119
10120 /* Not needed any more. */
10121 cu->reset_builder ();
10122 }
10123
10124 /* Process an imported unit DIE. */
10125
10126 static void
10127 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10128 {
10129 struct attribute *attr;
10130
10131 /* For now we don't handle imported units in type units. */
10132 if (cu->per_cu->is_debug_types)
10133 {
10134 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10135 " supported in type units [in module %s]"),
10136 objfile_name (cu->per_objfile->objfile));
10137 }
10138
10139 attr = dwarf2_attr (die, DW_AT_import, cu);
10140 if (attr != NULL)
10141 {
10142 sect_offset sect_off = attr->get_ref_die_offset ();
10143 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10144 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10145 dwarf2_per_cu_data *per_cu
10146 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
10147
10148 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10149 into another compilation unit, at root level. Regard this as a hint,
10150 and ignore it. */
10151 if (die->parent && die->parent->parent == NULL
10152 && per_cu->unit_type == DW_UT_compile
10153 && per_cu->lang == language_cplus)
10154 return;
10155
10156 /* If necessary, add it to the queue and load its DIEs. */
10157 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
10158 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
10159 false, cu->language);
10160
10161 cu->per_cu->imported_symtabs_push (per_cu);
10162 }
10163 }
10164
10165 /* RAII object that represents a process_die scope: i.e.,
10166 starts/finishes processing a DIE. */
10167 class process_die_scope
10168 {
10169 public:
10170 process_die_scope (die_info *die, dwarf2_cu *cu)
10171 : m_die (die), m_cu (cu)
10172 {
10173 /* We should only be processing DIEs not already in process. */
10174 gdb_assert (!m_die->in_process);
10175 m_die->in_process = true;
10176 }
10177
10178 ~process_die_scope ()
10179 {
10180 m_die->in_process = false;
10181
10182 /* If we're done processing the DIE for the CU that owns the line
10183 header, we don't need the line header anymore. */
10184 if (m_cu->line_header_die_owner == m_die)
10185 {
10186 delete m_cu->line_header;
10187 m_cu->line_header = NULL;
10188 m_cu->line_header_die_owner = NULL;
10189 }
10190 }
10191
10192 private:
10193 die_info *m_die;
10194 dwarf2_cu *m_cu;
10195 };
10196
10197 /* Process a die and its children. */
10198
10199 static void
10200 process_die (struct die_info *die, struct dwarf2_cu *cu)
10201 {
10202 process_die_scope scope (die, cu);
10203
10204 switch (die->tag)
10205 {
10206 case DW_TAG_padding:
10207 break;
10208 case DW_TAG_compile_unit:
10209 case DW_TAG_partial_unit:
10210 read_file_scope (die, cu);
10211 break;
10212 case DW_TAG_type_unit:
10213 read_type_unit_scope (die, cu);
10214 break;
10215 case DW_TAG_subprogram:
10216 /* Nested subprograms in Fortran get a prefix. */
10217 if (cu->language == language_fortran
10218 && die->parent != NULL
10219 && die->parent->tag == DW_TAG_subprogram)
10220 cu->processing_has_namespace_info = true;
10221 /* Fall through. */
10222 case DW_TAG_inlined_subroutine:
10223 read_func_scope (die, cu);
10224 break;
10225 case DW_TAG_lexical_block:
10226 case DW_TAG_try_block:
10227 case DW_TAG_catch_block:
10228 read_lexical_block_scope (die, cu);
10229 break;
10230 case DW_TAG_call_site:
10231 case DW_TAG_GNU_call_site:
10232 read_call_site_scope (die, cu);
10233 break;
10234 case DW_TAG_class_type:
10235 case DW_TAG_interface_type:
10236 case DW_TAG_structure_type:
10237 case DW_TAG_union_type:
10238 process_structure_scope (die, cu);
10239 break;
10240 case DW_TAG_enumeration_type:
10241 process_enumeration_scope (die, cu);
10242 break;
10243
10244 /* These dies have a type, but processing them does not create
10245 a symbol or recurse to process the children. Therefore we can
10246 read them on-demand through read_type_die. */
10247 case DW_TAG_subroutine_type:
10248 case DW_TAG_set_type:
10249 case DW_TAG_pointer_type:
10250 case DW_TAG_ptr_to_member_type:
10251 case DW_TAG_reference_type:
10252 case DW_TAG_rvalue_reference_type:
10253 case DW_TAG_string_type:
10254 break;
10255
10256 case DW_TAG_array_type:
10257 /* We only need to handle this case for Ada -- in other
10258 languages, it's normal for the compiler to emit a typedef
10259 instead. */
10260 if (cu->language != language_ada)
10261 break;
10262 /* FALLTHROUGH */
10263 case DW_TAG_base_type:
10264 case DW_TAG_subrange_type:
10265 case DW_TAG_typedef:
10266 /* Add a typedef symbol for the type definition, if it has a
10267 DW_AT_name. */
10268 new_symbol (die, read_type_die (die, cu), cu);
10269 break;
10270 case DW_TAG_common_block:
10271 read_common_block (die, cu);
10272 break;
10273 case DW_TAG_common_inclusion:
10274 break;
10275 case DW_TAG_namespace:
10276 cu->processing_has_namespace_info = true;
10277 read_namespace (die, cu);
10278 break;
10279 case DW_TAG_module:
10280 cu->processing_has_namespace_info = true;
10281 read_module (die, cu);
10282 break;
10283 case DW_TAG_imported_declaration:
10284 cu->processing_has_namespace_info = true;
10285 if (read_namespace_alias (die, cu))
10286 break;
10287 /* The declaration is not a global namespace alias. */
10288 /* Fall through. */
10289 case DW_TAG_imported_module:
10290 cu->processing_has_namespace_info = true;
10291 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10292 || cu->language != language_fortran))
10293 complaint (_("Tag '%s' has unexpected children"),
10294 dwarf_tag_name (die->tag));
10295 read_import_statement (die, cu);
10296 break;
10297
10298 case DW_TAG_imported_unit:
10299 process_imported_unit_die (die, cu);
10300 break;
10301
10302 case DW_TAG_variable:
10303 read_variable (die, cu);
10304 break;
10305
10306 default:
10307 new_symbol (die, NULL, cu);
10308 break;
10309 }
10310 }
10311 \f
10312 /* DWARF name computation. */
10313
10314 /* A helper function for dwarf2_compute_name which determines whether DIE
10315 needs to have the name of the scope prepended to the name listed in the
10316 die. */
10317
10318 static int
10319 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10320 {
10321 struct attribute *attr;
10322
10323 switch (die->tag)
10324 {
10325 case DW_TAG_namespace:
10326 case DW_TAG_typedef:
10327 case DW_TAG_class_type:
10328 case DW_TAG_interface_type:
10329 case DW_TAG_structure_type:
10330 case DW_TAG_union_type:
10331 case DW_TAG_enumeration_type:
10332 case DW_TAG_enumerator:
10333 case DW_TAG_subprogram:
10334 case DW_TAG_inlined_subroutine:
10335 case DW_TAG_member:
10336 case DW_TAG_imported_declaration:
10337 return 1;
10338
10339 case DW_TAG_variable:
10340 case DW_TAG_constant:
10341 /* We only need to prefix "globally" visible variables. These include
10342 any variable marked with DW_AT_external or any variable that
10343 lives in a namespace. [Variables in anonymous namespaces
10344 require prefixing, but they are not DW_AT_external.] */
10345
10346 if (dwarf2_attr (die, DW_AT_specification, cu))
10347 {
10348 struct dwarf2_cu *spec_cu = cu;
10349
10350 return die_needs_namespace (die_specification (die, &spec_cu),
10351 spec_cu);
10352 }
10353
10354 attr = dwarf2_attr (die, DW_AT_external, cu);
10355 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10356 && die->parent->tag != DW_TAG_module)
10357 return 0;
10358 /* A variable in a lexical block of some kind does not need a
10359 namespace, even though in C++ such variables may be external
10360 and have a mangled name. */
10361 if (die->parent->tag == DW_TAG_lexical_block
10362 || die->parent->tag == DW_TAG_try_block
10363 || die->parent->tag == DW_TAG_catch_block
10364 || die->parent->tag == DW_TAG_subprogram)
10365 return 0;
10366 return 1;
10367
10368 default:
10369 return 0;
10370 }
10371 }
10372
10373 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10374 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10375 defined for the given DIE. */
10376
10377 static struct attribute *
10378 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10379 {
10380 struct attribute *attr;
10381
10382 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10383 if (attr == NULL)
10384 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10385
10386 return attr;
10387 }
10388
10389 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10390 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10391 defined for the given DIE. */
10392
10393 static const char *
10394 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10395 {
10396 const char *linkage_name;
10397
10398 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10399 if (linkage_name == NULL)
10400 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10401
10402 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10403 See https://github.com/rust-lang/rust/issues/32925. */
10404 if (cu->language == language_rust && linkage_name != NULL
10405 && strchr (linkage_name, '{') != NULL)
10406 linkage_name = NULL;
10407
10408 return linkage_name;
10409 }
10410
10411 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10412 compute the physname for the object, which include a method's:
10413 - formal parameters (C++),
10414 - receiver type (Go),
10415
10416 The term "physname" is a bit confusing.
10417 For C++, for example, it is the demangled name.
10418 For Go, for example, it's the mangled name.
10419
10420 For Ada, return the DIE's linkage name rather than the fully qualified
10421 name. PHYSNAME is ignored..
10422
10423 The result is allocated on the objfile->per_bfd's obstack and
10424 canonicalized. */
10425
10426 static const char *
10427 dwarf2_compute_name (const char *name,
10428 struct die_info *die, struct dwarf2_cu *cu,
10429 int physname)
10430 {
10431 struct objfile *objfile = cu->per_objfile->objfile;
10432
10433 if (name == NULL)
10434 name = dwarf2_name (die, cu);
10435
10436 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10437 but otherwise compute it by typename_concat inside GDB.
10438 FIXME: Actually this is not really true, or at least not always true.
10439 It's all very confusing. compute_and_set_names doesn't try to demangle
10440 Fortran names because there is no mangling standard. So new_symbol
10441 will set the demangled name to the result of dwarf2_full_name, and it is
10442 the demangled name that GDB uses if it exists. */
10443 if (cu->language == language_ada
10444 || (cu->language == language_fortran && physname))
10445 {
10446 /* For Ada unit, we prefer the linkage name over the name, as
10447 the former contains the exported name, which the user expects
10448 to be able to reference. Ideally, we want the user to be able
10449 to reference this entity using either natural or linkage name,
10450 but we haven't started looking at this enhancement yet. */
10451 const char *linkage_name = dw2_linkage_name (die, cu);
10452
10453 if (linkage_name != NULL)
10454 return linkage_name;
10455 }
10456
10457 /* These are the only languages we know how to qualify names in. */
10458 if (name != NULL
10459 && (cu->language == language_cplus
10460 || cu->language == language_fortran || cu->language == language_d
10461 || cu->language == language_rust))
10462 {
10463 if (die_needs_namespace (die, cu))
10464 {
10465 const char *prefix;
10466 const char *canonical_name = NULL;
10467
10468 string_file buf;
10469
10470 prefix = determine_prefix (die, cu);
10471 if (*prefix != '\0')
10472 {
10473 gdb::unique_xmalloc_ptr<char> prefixed_name
10474 (typename_concat (NULL, prefix, name, physname, cu));
10475
10476 buf.puts (prefixed_name.get ());
10477 }
10478 else
10479 buf.puts (name);
10480
10481 /* Template parameters may be specified in the DIE's DW_AT_name, or
10482 as children with DW_TAG_template_type_param or
10483 DW_TAG_value_type_param. If the latter, add them to the name
10484 here. If the name already has template parameters, then
10485 skip this step; some versions of GCC emit both, and
10486 it is more efficient to use the pre-computed name.
10487
10488 Something to keep in mind about this process: it is very
10489 unlikely, or in some cases downright impossible, to produce
10490 something that will match the mangled name of a function.
10491 If the definition of the function has the same debug info,
10492 we should be able to match up with it anyway. But fallbacks
10493 using the minimal symbol, for instance to find a method
10494 implemented in a stripped copy of libstdc++, will not work.
10495 If we do not have debug info for the definition, we will have to
10496 match them up some other way.
10497
10498 When we do name matching there is a related problem with function
10499 templates; two instantiated function templates are allowed to
10500 differ only by their return types, which we do not add here. */
10501
10502 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10503 {
10504 struct attribute *attr;
10505 struct die_info *child;
10506 int first = 1;
10507 const language_defn *cplus_lang = language_def (cu->language);
10508
10509 die->building_fullname = 1;
10510
10511 for (child = die->child; child != NULL; child = child->sibling)
10512 {
10513 struct type *type;
10514 LONGEST value;
10515 const gdb_byte *bytes;
10516 struct dwarf2_locexpr_baton *baton;
10517 struct value *v;
10518
10519 if (child->tag != DW_TAG_template_type_param
10520 && child->tag != DW_TAG_template_value_param)
10521 continue;
10522
10523 if (first)
10524 {
10525 buf.puts ("<");
10526 first = 0;
10527 }
10528 else
10529 buf.puts (", ");
10530
10531 attr = dwarf2_attr (child, DW_AT_type, cu);
10532 if (attr == NULL)
10533 {
10534 complaint (_("template parameter missing DW_AT_type"));
10535 buf.puts ("UNKNOWN_TYPE");
10536 continue;
10537 }
10538 type = die_type (child, cu);
10539
10540 if (child->tag == DW_TAG_template_type_param)
10541 {
10542 cplus_lang->print_type (type, "", &buf, -1, 0,
10543 &type_print_raw_options);
10544 continue;
10545 }
10546
10547 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10548 if (attr == NULL)
10549 {
10550 complaint (_("template parameter missing "
10551 "DW_AT_const_value"));
10552 buf.puts ("UNKNOWN_VALUE");
10553 continue;
10554 }
10555
10556 dwarf2_const_value_attr (attr, type, name,
10557 &cu->comp_unit_obstack, cu,
10558 &value, &bytes, &baton);
10559
10560 if (type->has_no_signedness ())
10561 /* GDB prints characters as NUMBER 'CHAR'. If that's
10562 changed, this can use value_print instead. */
10563 cplus_lang->printchar (value, type, &buf);
10564 else
10565 {
10566 struct value_print_options opts;
10567
10568 if (baton != NULL)
10569 v = dwarf2_evaluate_loc_desc (type, NULL,
10570 baton->data,
10571 baton->size,
10572 baton->per_cu,
10573 baton->per_objfile);
10574 else if (bytes != NULL)
10575 {
10576 v = allocate_value (type);
10577 memcpy (value_contents_writeable (v), bytes,
10578 TYPE_LENGTH (type));
10579 }
10580 else
10581 v = value_from_longest (type, value);
10582
10583 /* Specify decimal so that we do not depend on
10584 the radix. */
10585 get_formatted_print_options (&opts, 'd');
10586 opts.raw = 1;
10587 value_print (v, &buf, &opts);
10588 release_value (v);
10589 }
10590 }
10591
10592 die->building_fullname = 0;
10593
10594 if (!first)
10595 {
10596 /* Close the argument list, with a space if necessary
10597 (nested templates). */
10598 if (!buf.empty () && buf.string ().back () == '>')
10599 buf.puts (" >");
10600 else
10601 buf.puts (">");
10602 }
10603 }
10604
10605 /* For C++ methods, append formal parameter type
10606 information, if PHYSNAME. */
10607
10608 if (physname && die->tag == DW_TAG_subprogram
10609 && cu->language == language_cplus)
10610 {
10611 struct type *type = read_type_die (die, cu);
10612
10613 c_type_print_args (type, &buf, 1, cu->language,
10614 &type_print_raw_options);
10615
10616 if (cu->language == language_cplus)
10617 {
10618 /* Assume that an artificial first parameter is
10619 "this", but do not crash if it is not. RealView
10620 marks unnamed (and thus unused) parameters as
10621 artificial; there is no way to differentiate
10622 the two cases. */
10623 if (type->num_fields () > 0
10624 && TYPE_FIELD_ARTIFICIAL (type, 0)
10625 && type->field (0).type ()->code () == TYPE_CODE_PTR
10626 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
10627 buf.puts (" const");
10628 }
10629 }
10630
10631 const std::string &intermediate_name = buf.string ();
10632
10633 if (cu->language == language_cplus)
10634 canonical_name
10635 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10636 objfile);
10637
10638 /* If we only computed INTERMEDIATE_NAME, or if
10639 INTERMEDIATE_NAME is already canonical, then we need to
10640 intern it. */
10641 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10642 name = objfile->intern (intermediate_name);
10643 else
10644 name = canonical_name;
10645 }
10646 }
10647
10648 return name;
10649 }
10650
10651 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10652 If scope qualifiers are appropriate they will be added. The result
10653 will be allocated on the storage_obstack, or NULL if the DIE does
10654 not have a name. NAME may either be from a previous call to
10655 dwarf2_name or NULL.
10656
10657 The output string will be canonicalized (if C++). */
10658
10659 static const char *
10660 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10661 {
10662 return dwarf2_compute_name (name, die, cu, 0);
10663 }
10664
10665 /* Construct a physname for the given DIE in CU. NAME may either be
10666 from a previous call to dwarf2_name or NULL. The result will be
10667 allocated on the objfile_objstack or NULL if the DIE does not have a
10668 name.
10669
10670 The output string will be canonicalized (if C++). */
10671
10672 static const char *
10673 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10674 {
10675 struct objfile *objfile = cu->per_objfile->objfile;
10676 const char *retval, *mangled = NULL, *canon = NULL;
10677 int need_copy = 1;
10678
10679 /* In this case dwarf2_compute_name is just a shortcut not building anything
10680 on its own. */
10681 if (!die_needs_namespace (die, cu))
10682 return dwarf2_compute_name (name, die, cu, 1);
10683
10684 if (cu->language != language_rust)
10685 mangled = dw2_linkage_name (die, cu);
10686
10687 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10688 has computed. */
10689 gdb::unique_xmalloc_ptr<char> demangled;
10690 if (mangled != NULL)
10691 {
10692
10693 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
10694 {
10695 /* Do nothing (do not demangle the symbol name). */
10696 }
10697 else
10698 {
10699 /* Use DMGL_RET_DROP for C++ template functions to suppress
10700 their return type. It is easier for GDB users to search
10701 for such functions as `name(params)' than `long name(params)'.
10702 In such case the minimal symbol names do not match the full
10703 symbol names but for template functions there is never a need
10704 to look up their definition from their declaration so
10705 the only disadvantage remains the minimal symbol variant
10706 `long name(params)' does not have the proper inferior type. */
10707 demangled.reset (gdb_demangle (mangled,
10708 (DMGL_PARAMS | DMGL_ANSI
10709 | DMGL_RET_DROP)));
10710 }
10711 if (demangled)
10712 canon = demangled.get ();
10713 else
10714 {
10715 canon = mangled;
10716 need_copy = 0;
10717 }
10718 }
10719
10720 if (canon == NULL || check_physname)
10721 {
10722 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10723
10724 if (canon != NULL && strcmp (physname, canon) != 0)
10725 {
10726 /* It may not mean a bug in GDB. The compiler could also
10727 compute DW_AT_linkage_name incorrectly. But in such case
10728 GDB would need to be bug-to-bug compatible. */
10729
10730 complaint (_("Computed physname <%s> does not match demangled <%s> "
10731 "(from linkage <%s>) - DIE at %s [in module %s]"),
10732 physname, canon, mangled, sect_offset_str (die->sect_off),
10733 objfile_name (objfile));
10734
10735 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10736 is available here - over computed PHYSNAME. It is safer
10737 against both buggy GDB and buggy compilers. */
10738
10739 retval = canon;
10740 }
10741 else
10742 {
10743 retval = physname;
10744 need_copy = 0;
10745 }
10746 }
10747 else
10748 retval = canon;
10749
10750 if (need_copy)
10751 retval = objfile->intern (retval);
10752
10753 return retval;
10754 }
10755
10756 /* Inspect DIE in CU for a namespace alias. If one exists, record
10757 a new symbol for it.
10758
10759 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10760
10761 static int
10762 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10763 {
10764 struct attribute *attr;
10765
10766 /* If the die does not have a name, this is not a namespace
10767 alias. */
10768 attr = dwarf2_attr (die, DW_AT_name, cu);
10769 if (attr != NULL)
10770 {
10771 int num;
10772 struct die_info *d = die;
10773 struct dwarf2_cu *imported_cu = cu;
10774
10775 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10776 keep inspecting DIEs until we hit the underlying import. */
10777 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10778 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10779 {
10780 attr = dwarf2_attr (d, DW_AT_import, cu);
10781 if (attr == NULL)
10782 break;
10783
10784 d = follow_die_ref (d, attr, &imported_cu);
10785 if (d->tag != DW_TAG_imported_declaration)
10786 break;
10787 }
10788
10789 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10790 {
10791 complaint (_("DIE at %s has too many recursively imported "
10792 "declarations"), sect_offset_str (d->sect_off));
10793 return 0;
10794 }
10795
10796 if (attr != NULL)
10797 {
10798 struct type *type;
10799 sect_offset sect_off = attr->get_ref_die_offset ();
10800
10801 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10802 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10803 {
10804 /* This declaration is a global namespace alias. Add
10805 a symbol for it whose type is the aliased namespace. */
10806 new_symbol (die, type, cu);
10807 return 1;
10808 }
10809 }
10810 }
10811
10812 return 0;
10813 }
10814
10815 /* Return the using directives repository (global or local?) to use in the
10816 current context for CU.
10817
10818 For Ada, imported declarations can materialize renamings, which *may* be
10819 global. However it is impossible (for now?) in DWARF to distinguish
10820 "external" imported declarations and "static" ones. As all imported
10821 declarations seem to be static in all other languages, make them all CU-wide
10822 global only in Ada. */
10823
10824 static struct using_direct **
10825 using_directives (struct dwarf2_cu *cu)
10826 {
10827 if (cu->language == language_ada
10828 && cu->get_builder ()->outermost_context_p ())
10829 return cu->get_builder ()->get_global_using_directives ();
10830 else
10831 return cu->get_builder ()->get_local_using_directives ();
10832 }
10833
10834 /* Read the import statement specified by the given die and record it. */
10835
10836 static void
10837 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10838 {
10839 struct objfile *objfile = cu->per_objfile->objfile;
10840 struct attribute *import_attr;
10841 struct die_info *imported_die, *child_die;
10842 struct dwarf2_cu *imported_cu;
10843 const char *imported_name;
10844 const char *imported_name_prefix;
10845 const char *canonical_name;
10846 const char *import_alias;
10847 const char *imported_declaration = NULL;
10848 const char *import_prefix;
10849 std::vector<const char *> excludes;
10850
10851 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10852 if (import_attr == NULL)
10853 {
10854 complaint (_("Tag '%s' has no DW_AT_import"),
10855 dwarf_tag_name (die->tag));
10856 return;
10857 }
10858
10859 imported_cu = cu;
10860 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10861 imported_name = dwarf2_name (imported_die, imported_cu);
10862 if (imported_name == NULL)
10863 {
10864 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10865
10866 The import in the following code:
10867 namespace A
10868 {
10869 typedef int B;
10870 }
10871
10872 int main ()
10873 {
10874 using A::B;
10875 B b;
10876 return b;
10877 }
10878
10879 ...
10880 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10881 <52> DW_AT_decl_file : 1
10882 <53> DW_AT_decl_line : 6
10883 <54> DW_AT_import : <0x75>
10884 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10885 <59> DW_AT_name : B
10886 <5b> DW_AT_decl_file : 1
10887 <5c> DW_AT_decl_line : 2
10888 <5d> DW_AT_type : <0x6e>
10889 ...
10890 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10891 <76> DW_AT_byte_size : 4
10892 <77> DW_AT_encoding : 5 (signed)
10893
10894 imports the wrong die ( 0x75 instead of 0x58 ).
10895 This case will be ignored until the gcc bug is fixed. */
10896 return;
10897 }
10898
10899 /* Figure out the local name after import. */
10900 import_alias = dwarf2_name (die, cu);
10901
10902 /* Figure out where the statement is being imported to. */
10903 import_prefix = determine_prefix (die, cu);
10904
10905 /* Figure out what the scope of the imported die is and prepend it
10906 to the name of the imported die. */
10907 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10908
10909 if (imported_die->tag != DW_TAG_namespace
10910 && imported_die->tag != DW_TAG_module)
10911 {
10912 imported_declaration = imported_name;
10913 canonical_name = imported_name_prefix;
10914 }
10915 else if (strlen (imported_name_prefix) > 0)
10916 canonical_name = obconcat (&objfile->objfile_obstack,
10917 imported_name_prefix,
10918 (cu->language == language_d ? "." : "::"),
10919 imported_name, (char *) NULL);
10920 else
10921 canonical_name = imported_name;
10922
10923 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10924 for (child_die = die->child; child_die && child_die->tag;
10925 child_die = child_die->sibling)
10926 {
10927 /* DWARF-4: A Fortran use statement with a “rename list” may be
10928 represented by an imported module entry with an import attribute
10929 referring to the module and owned entries corresponding to those
10930 entities that are renamed as part of being imported. */
10931
10932 if (child_die->tag != DW_TAG_imported_declaration)
10933 {
10934 complaint (_("child DW_TAG_imported_declaration expected "
10935 "- DIE at %s [in module %s]"),
10936 sect_offset_str (child_die->sect_off),
10937 objfile_name (objfile));
10938 continue;
10939 }
10940
10941 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10942 if (import_attr == NULL)
10943 {
10944 complaint (_("Tag '%s' has no DW_AT_import"),
10945 dwarf_tag_name (child_die->tag));
10946 continue;
10947 }
10948
10949 imported_cu = cu;
10950 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10951 &imported_cu);
10952 imported_name = dwarf2_name (imported_die, imported_cu);
10953 if (imported_name == NULL)
10954 {
10955 complaint (_("child DW_TAG_imported_declaration has unknown "
10956 "imported name - DIE at %s [in module %s]"),
10957 sect_offset_str (child_die->sect_off),
10958 objfile_name (objfile));
10959 continue;
10960 }
10961
10962 excludes.push_back (imported_name);
10963
10964 process_die (child_die, cu);
10965 }
10966
10967 add_using_directive (using_directives (cu),
10968 import_prefix,
10969 canonical_name,
10970 import_alias,
10971 imported_declaration,
10972 excludes,
10973 0,
10974 &objfile->objfile_obstack);
10975 }
10976
10977 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10978 types, but gives them a size of zero. Starting with version 14,
10979 ICC is compatible with GCC. */
10980
10981 static bool
10982 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10983 {
10984 if (!cu->checked_producer)
10985 check_producer (cu);
10986
10987 return cu->producer_is_icc_lt_14;
10988 }
10989
10990 /* ICC generates a DW_AT_type for C void functions. This was observed on
10991 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10992 which says that void functions should not have a DW_AT_type. */
10993
10994 static bool
10995 producer_is_icc (struct dwarf2_cu *cu)
10996 {
10997 if (!cu->checked_producer)
10998 check_producer (cu);
10999
11000 return cu->producer_is_icc;
11001 }
11002
11003 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11004 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11005 this, it was first present in GCC release 4.3.0. */
11006
11007 static bool
11008 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11009 {
11010 if (!cu->checked_producer)
11011 check_producer (cu);
11012
11013 return cu->producer_is_gcc_lt_4_3;
11014 }
11015
11016 static file_and_directory
11017 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11018 {
11019 file_and_directory res;
11020
11021 /* Find the filename. Do not use dwarf2_name here, since the filename
11022 is not a source language identifier. */
11023 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11024 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11025
11026 if (res.comp_dir == NULL
11027 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11028 && IS_ABSOLUTE_PATH (res.name))
11029 {
11030 res.comp_dir_storage = ldirname (res.name);
11031 if (!res.comp_dir_storage.empty ())
11032 res.comp_dir = res.comp_dir_storage.c_str ();
11033 }
11034 if (res.comp_dir != NULL)
11035 {
11036 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11037 directory, get rid of it. */
11038 const char *cp = strchr (res.comp_dir, ':');
11039
11040 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11041 res.comp_dir = cp + 1;
11042 }
11043
11044 if (res.name == NULL)
11045 res.name = "<unknown>";
11046
11047 return res;
11048 }
11049
11050 /* Handle DW_AT_stmt_list for a compilation unit.
11051 DIE is the DW_TAG_compile_unit die for CU.
11052 COMP_DIR is the compilation directory. LOWPC is passed to
11053 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11054
11055 static void
11056 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11057 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11058 {
11059 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11060 struct attribute *attr;
11061 struct line_header line_header_local;
11062 hashval_t line_header_local_hash;
11063 void **slot;
11064 int decode_mapping;
11065
11066 gdb_assert (! cu->per_cu->is_debug_types);
11067
11068 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11069 if (attr == NULL || !attr->form_is_unsigned ())
11070 return;
11071
11072 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
11073
11074 /* The line header hash table is only created if needed (it exists to
11075 prevent redundant reading of the line table for partial_units).
11076 If we're given a partial_unit, we'll need it. If we're given a
11077 compile_unit, then use the line header hash table if it's already
11078 created, but don't create one just yet. */
11079
11080 if (per_objfile->line_header_hash == NULL
11081 && die->tag == DW_TAG_partial_unit)
11082 {
11083 per_objfile->line_header_hash
11084 .reset (htab_create_alloc (127, line_header_hash_voidp,
11085 line_header_eq_voidp,
11086 free_line_header_voidp,
11087 xcalloc, xfree));
11088 }
11089
11090 line_header_local.sect_off = line_offset;
11091 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11092 line_header_local_hash = line_header_hash (&line_header_local);
11093 if (per_objfile->line_header_hash != NULL)
11094 {
11095 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11096 &line_header_local,
11097 line_header_local_hash, NO_INSERT);
11098
11099 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11100 is not present in *SLOT (since if there is something in *SLOT then
11101 it will be for a partial_unit). */
11102 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11103 {
11104 gdb_assert (*slot != NULL);
11105 cu->line_header = (struct line_header *) *slot;
11106 return;
11107 }
11108 }
11109
11110 /* dwarf_decode_line_header does not yet provide sufficient information.
11111 We always have to call also dwarf_decode_lines for it. */
11112 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11113 if (lh == NULL)
11114 return;
11115
11116 cu->line_header = lh.release ();
11117 cu->line_header_die_owner = die;
11118
11119 if (per_objfile->line_header_hash == NULL)
11120 slot = NULL;
11121 else
11122 {
11123 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11124 &line_header_local,
11125 line_header_local_hash, INSERT);
11126 gdb_assert (slot != NULL);
11127 }
11128 if (slot != NULL && *slot == NULL)
11129 {
11130 /* This newly decoded line number information unit will be owned
11131 by line_header_hash hash table. */
11132 *slot = cu->line_header;
11133 cu->line_header_die_owner = NULL;
11134 }
11135 else
11136 {
11137 /* We cannot free any current entry in (*slot) as that struct line_header
11138 may be already used by multiple CUs. Create only temporary decoded
11139 line_header for this CU - it may happen at most once for each line
11140 number information unit. And if we're not using line_header_hash
11141 then this is what we want as well. */
11142 gdb_assert (die->tag != DW_TAG_partial_unit);
11143 }
11144 decode_mapping = (die->tag != DW_TAG_partial_unit);
11145 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11146 decode_mapping);
11147
11148 }
11149
11150 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11151
11152 static void
11153 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11154 {
11155 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11156 struct objfile *objfile = per_objfile->objfile;
11157 struct gdbarch *gdbarch = objfile->arch ();
11158 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11159 CORE_ADDR highpc = ((CORE_ADDR) 0);
11160 struct attribute *attr;
11161 struct die_info *child_die;
11162 CORE_ADDR baseaddr;
11163
11164 prepare_one_comp_unit (cu, die, cu->language);
11165 baseaddr = objfile->text_section_offset ();
11166
11167 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11168
11169 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11170 from finish_block. */
11171 if (lowpc == ((CORE_ADDR) -1))
11172 lowpc = highpc;
11173 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11174
11175 file_and_directory fnd = find_file_and_directory (die, cu);
11176
11177 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11178 standardised yet. As a workaround for the language detection we fall
11179 back to the DW_AT_producer string. */
11180 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11181 cu->language = language_opencl;
11182
11183 /* Similar hack for Go. */
11184 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11185 set_cu_language (DW_LANG_Go, cu);
11186
11187 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11188
11189 /* Decode line number information if present. We do this before
11190 processing child DIEs, so that the line header table is available
11191 for DW_AT_decl_file. */
11192 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11193
11194 /* Process all dies in compilation unit. */
11195 if (die->child != NULL)
11196 {
11197 child_die = die->child;
11198 while (child_die && child_die->tag)
11199 {
11200 process_die (child_die, cu);
11201 child_die = child_die->sibling;
11202 }
11203 }
11204
11205 /* Decode macro information, if present. Dwarf 2 macro information
11206 refers to information in the line number info statement program
11207 header, so we can only read it if we've read the header
11208 successfully. */
11209 attr = dwarf2_attr (die, DW_AT_macros, cu);
11210 if (attr == NULL)
11211 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11212 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
11213 {
11214 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11215 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11216
11217 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
11218 }
11219 else
11220 {
11221 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11222 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
11223 {
11224 unsigned int macro_offset = attr->as_unsigned ();
11225
11226 dwarf_decode_macros (cu, macro_offset, 0);
11227 }
11228 }
11229 }
11230
11231 void
11232 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11233 {
11234 struct type_unit_group *tu_group;
11235 int first_time;
11236 struct attribute *attr;
11237 unsigned int i;
11238 struct signatured_type *sig_type;
11239
11240 gdb_assert (per_cu->is_debug_types);
11241 sig_type = (struct signatured_type *) per_cu;
11242
11243 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11244
11245 /* If we're using .gdb_index (includes -readnow) then
11246 per_cu->type_unit_group may not have been set up yet. */
11247 if (sig_type->type_unit_group == NULL)
11248 sig_type->type_unit_group = get_type_unit_group (this, attr);
11249 tu_group = sig_type->type_unit_group;
11250
11251 /* If we've already processed this stmt_list there's no real need to
11252 do it again, we could fake it and just recreate the part we need
11253 (file name,index -> symtab mapping). If data shows this optimization
11254 is useful we can do it then. */
11255 type_unit_group_unshareable *tug_unshare
11256 = per_objfile->get_type_unit_group_unshareable (tu_group);
11257 first_time = tug_unshare->compunit_symtab == NULL;
11258
11259 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11260 debug info. */
11261 line_header_up lh;
11262 if (attr != NULL && attr->form_is_unsigned ())
11263 {
11264 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
11265 lh = dwarf_decode_line_header (line_offset, this);
11266 }
11267 if (lh == NULL)
11268 {
11269 if (first_time)
11270 start_symtab ("", NULL, 0);
11271 else
11272 {
11273 gdb_assert (tug_unshare->symtabs == NULL);
11274 gdb_assert (m_builder == nullptr);
11275 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11276 m_builder.reset (new struct buildsym_compunit
11277 (COMPUNIT_OBJFILE (cust), "",
11278 COMPUNIT_DIRNAME (cust),
11279 compunit_language (cust),
11280 0, cust));
11281 list_in_scope = get_builder ()->get_file_symbols ();
11282 }
11283 return;
11284 }
11285
11286 line_header = lh.release ();
11287 line_header_die_owner = die;
11288
11289 if (first_time)
11290 {
11291 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11292
11293 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11294 still initializing it, and our caller (a few levels up)
11295 process_full_type_unit still needs to know if this is the first
11296 time. */
11297
11298 tug_unshare->symtabs
11299 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11300 struct symtab *, line_header->file_names_size ());
11301
11302 auto &file_names = line_header->file_names ();
11303 for (i = 0; i < file_names.size (); ++i)
11304 {
11305 file_entry &fe = file_names[i];
11306 dwarf2_start_subfile (this, fe.name,
11307 fe.include_dir (line_header));
11308 buildsym_compunit *b = get_builder ();
11309 if (b->get_current_subfile ()->symtab == NULL)
11310 {
11311 /* NOTE: start_subfile will recognize when it's been
11312 passed a file it has already seen. So we can't
11313 assume there's a simple mapping from
11314 cu->line_header->file_names to subfiles, plus
11315 cu->line_header->file_names may contain dups. */
11316 b->get_current_subfile ()->symtab
11317 = allocate_symtab (cust, b->get_current_subfile ()->name);
11318 }
11319
11320 fe.symtab = b->get_current_subfile ()->symtab;
11321 tug_unshare->symtabs[i] = fe.symtab;
11322 }
11323 }
11324 else
11325 {
11326 gdb_assert (m_builder == nullptr);
11327 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11328 m_builder.reset (new struct buildsym_compunit
11329 (COMPUNIT_OBJFILE (cust), "",
11330 COMPUNIT_DIRNAME (cust),
11331 compunit_language (cust),
11332 0, cust));
11333 list_in_scope = get_builder ()->get_file_symbols ();
11334
11335 auto &file_names = line_header->file_names ();
11336 for (i = 0; i < file_names.size (); ++i)
11337 {
11338 file_entry &fe = file_names[i];
11339 fe.symtab = tug_unshare->symtabs[i];
11340 }
11341 }
11342
11343 /* The main symtab is allocated last. Type units don't have DW_AT_name
11344 so they don't have a "real" (so to speak) symtab anyway.
11345 There is later code that will assign the main symtab to all symbols
11346 that don't have one. We need to handle the case of a symbol with a
11347 missing symtab (DW_AT_decl_file) anyway. */
11348 }
11349
11350 /* Process DW_TAG_type_unit.
11351 For TUs we want to skip the first top level sibling if it's not the
11352 actual type being defined by this TU. In this case the first top
11353 level sibling is there to provide context only. */
11354
11355 static void
11356 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11357 {
11358 struct die_info *child_die;
11359
11360 prepare_one_comp_unit (cu, die, language_minimal);
11361
11362 /* Initialize (or reinitialize) the machinery for building symtabs.
11363 We do this before processing child DIEs, so that the line header table
11364 is available for DW_AT_decl_file. */
11365 cu->setup_type_unit_groups (die);
11366
11367 if (die->child != NULL)
11368 {
11369 child_die = die->child;
11370 while (child_die && child_die->tag)
11371 {
11372 process_die (child_die, cu);
11373 child_die = child_die->sibling;
11374 }
11375 }
11376 }
11377 \f
11378 /* DWO/DWP files.
11379
11380 http://gcc.gnu.org/wiki/DebugFission
11381 http://gcc.gnu.org/wiki/DebugFissionDWP
11382
11383 To simplify handling of both DWO files ("object" files with the DWARF info)
11384 and DWP files (a file with the DWOs packaged up into one file), we treat
11385 DWP files as having a collection of virtual DWO files. */
11386
11387 static hashval_t
11388 hash_dwo_file (const void *item)
11389 {
11390 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11391 hashval_t hash;
11392
11393 hash = htab_hash_string (dwo_file->dwo_name);
11394 if (dwo_file->comp_dir != NULL)
11395 hash += htab_hash_string (dwo_file->comp_dir);
11396 return hash;
11397 }
11398
11399 static int
11400 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11401 {
11402 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11403 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11404
11405 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11406 return 0;
11407 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11408 return lhs->comp_dir == rhs->comp_dir;
11409 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11410 }
11411
11412 /* Allocate a hash table for DWO files. */
11413
11414 static htab_up
11415 allocate_dwo_file_hash_table ()
11416 {
11417 auto delete_dwo_file = [] (void *item)
11418 {
11419 struct dwo_file *dwo_file = (struct dwo_file *) item;
11420
11421 delete dwo_file;
11422 };
11423
11424 return htab_up (htab_create_alloc (41,
11425 hash_dwo_file,
11426 eq_dwo_file,
11427 delete_dwo_file,
11428 xcalloc, xfree));
11429 }
11430
11431 /* Lookup DWO file DWO_NAME. */
11432
11433 static void **
11434 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
11435 const char *dwo_name,
11436 const char *comp_dir)
11437 {
11438 struct dwo_file find_entry;
11439 void **slot;
11440
11441 if (per_objfile->per_bfd->dwo_files == NULL)
11442 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11443
11444 find_entry.dwo_name = dwo_name;
11445 find_entry.comp_dir = comp_dir;
11446 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
11447 INSERT);
11448
11449 return slot;
11450 }
11451
11452 static hashval_t
11453 hash_dwo_unit (const void *item)
11454 {
11455 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11456
11457 /* This drops the top 32 bits of the id, but is ok for a hash. */
11458 return dwo_unit->signature;
11459 }
11460
11461 static int
11462 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11463 {
11464 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11465 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11466
11467 /* The signature is assumed to be unique within the DWO file.
11468 So while object file CU dwo_id's always have the value zero,
11469 that's OK, assuming each object file DWO file has only one CU,
11470 and that's the rule for now. */
11471 return lhs->signature == rhs->signature;
11472 }
11473
11474 /* Allocate a hash table for DWO CUs,TUs.
11475 There is one of these tables for each of CUs,TUs for each DWO file. */
11476
11477 static htab_up
11478 allocate_dwo_unit_table ()
11479 {
11480 /* Start out with a pretty small number.
11481 Generally DWO files contain only one CU and maybe some TUs. */
11482 return htab_up (htab_create_alloc (3,
11483 hash_dwo_unit,
11484 eq_dwo_unit,
11485 NULL, xcalloc, xfree));
11486 }
11487
11488 /* die_reader_func for create_dwo_cu. */
11489
11490 static void
11491 create_dwo_cu_reader (const struct die_reader_specs *reader,
11492 const gdb_byte *info_ptr,
11493 struct die_info *comp_unit_die,
11494 struct dwo_file *dwo_file,
11495 struct dwo_unit *dwo_unit)
11496 {
11497 struct dwarf2_cu *cu = reader->cu;
11498 sect_offset sect_off = cu->per_cu->sect_off;
11499 struct dwarf2_section_info *section = cu->per_cu->section;
11500
11501 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11502 if (!signature.has_value ())
11503 {
11504 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11505 " its dwo_id [in module %s]"),
11506 sect_offset_str (sect_off), dwo_file->dwo_name);
11507 return;
11508 }
11509
11510 dwo_unit->dwo_file = dwo_file;
11511 dwo_unit->signature = *signature;
11512 dwo_unit->section = section;
11513 dwo_unit->sect_off = sect_off;
11514 dwo_unit->length = cu->per_cu->length;
11515
11516 dwarf_read_debug_printf (" offset %s, dwo_id %s",
11517 sect_offset_str (sect_off),
11518 hex_string (dwo_unit->signature));
11519 }
11520
11521 /* Create the dwo_units for the CUs in a DWO_FILE.
11522 Note: This function processes DWO files only, not DWP files. */
11523
11524 static void
11525 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
11526 dwarf2_cu *cu, struct dwo_file &dwo_file,
11527 dwarf2_section_info &section, htab_up &cus_htab)
11528 {
11529 struct objfile *objfile = per_objfile->objfile;
11530 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
11531 const gdb_byte *info_ptr, *end_ptr;
11532
11533 section.read (objfile);
11534 info_ptr = section.buffer;
11535
11536 if (info_ptr == NULL)
11537 return;
11538
11539 dwarf_read_debug_printf ("Reading %s for %s:",
11540 section.get_name (),
11541 section.get_file_name ());
11542
11543 end_ptr = info_ptr + section.size;
11544 while (info_ptr < end_ptr)
11545 {
11546 struct dwarf2_per_cu_data per_cu;
11547 struct dwo_unit read_unit {};
11548 struct dwo_unit *dwo_unit;
11549 void **slot;
11550 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11551
11552 memset (&per_cu, 0, sizeof (per_cu));
11553 per_cu.per_bfd = per_bfd;
11554 per_cu.is_debug_types = 0;
11555 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11556 per_cu.section = &section;
11557
11558 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
11559 if (!reader.dummy_p)
11560 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11561 &dwo_file, &read_unit);
11562 info_ptr += per_cu.length;
11563
11564 // If the unit could not be parsed, skip it.
11565 if (read_unit.dwo_file == NULL)
11566 continue;
11567
11568 if (cus_htab == NULL)
11569 cus_htab = allocate_dwo_unit_table ();
11570
11571 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11572 struct dwo_unit);
11573 *dwo_unit = read_unit;
11574 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11575 gdb_assert (slot != NULL);
11576 if (*slot != NULL)
11577 {
11578 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11579 sect_offset dup_sect_off = dup_cu->sect_off;
11580
11581 complaint (_("debug cu entry at offset %s is duplicate to"
11582 " the entry at offset %s, signature %s"),
11583 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11584 hex_string (dwo_unit->signature));
11585 }
11586 *slot = (void *)dwo_unit;
11587 }
11588 }
11589
11590 /* DWP file .debug_{cu,tu}_index section format:
11591 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11592 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11593
11594 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11595 officially standard DWP format was published with DWARF v5 and is called
11596 Version 5. There are no versions 3 or 4.
11597
11598 DWP Version 1:
11599
11600 Both index sections have the same format, and serve to map a 64-bit
11601 signature to a set of section numbers. Each section begins with a header,
11602 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11603 indexes, and a pool of 32-bit section numbers. The index sections will be
11604 aligned at 8-byte boundaries in the file.
11605
11606 The index section header consists of:
11607
11608 V, 32 bit version number
11609 -, 32 bits unused
11610 N, 32 bit number of compilation units or type units in the index
11611 M, 32 bit number of slots in the hash table
11612
11613 Numbers are recorded using the byte order of the application binary.
11614
11615 The hash table begins at offset 16 in the section, and consists of an array
11616 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11617 order of the application binary). Unused slots in the hash table are 0.
11618 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11619
11620 The parallel table begins immediately after the hash table
11621 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11622 array of 32-bit indexes (using the byte order of the application binary),
11623 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11624 table contains a 32-bit index into the pool of section numbers. For unused
11625 hash table slots, the corresponding entry in the parallel table will be 0.
11626
11627 The pool of section numbers begins immediately following the hash table
11628 (at offset 16 + 12 * M from the beginning of the section). The pool of
11629 section numbers consists of an array of 32-bit words (using the byte order
11630 of the application binary). Each item in the array is indexed starting
11631 from 0. The hash table entry provides the index of the first section
11632 number in the set. Additional section numbers in the set follow, and the
11633 set is terminated by a 0 entry (section number 0 is not used in ELF).
11634
11635 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11636 section must be the first entry in the set, and the .debug_abbrev.dwo must
11637 be the second entry. Other members of the set may follow in any order.
11638
11639 ---
11640
11641 DWP Versions 2 and 5:
11642
11643 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
11644 and the entries in the index tables are now offsets into these sections.
11645 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11646 section.
11647
11648 Index Section Contents:
11649 Header
11650 Hash Table of Signatures dwp_hash_table.hash_table
11651 Parallel Table of Indices dwp_hash_table.unit_table
11652 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11653 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
11654
11655 The index section header consists of:
11656
11657 V, 32 bit version number
11658 L, 32 bit number of columns in the table of section offsets
11659 N, 32 bit number of compilation units or type units in the index
11660 M, 32 bit number of slots in the hash table
11661
11662 Numbers are recorded using the byte order of the application binary.
11663
11664 The hash table has the same format as version 1.
11665 The parallel table of indices has the same format as version 1,
11666 except that the entries are origin-1 indices into the table of sections
11667 offsets and the table of section sizes.
11668
11669 The table of offsets begins immediately following the parallel table
11670 (at offset 16 + 12 * M from the beginning of the section). The table is
11671 a two-dimensional array of 32-bit words (using the byte order of the
11672 application binary), with L columns and N+1 rows, in row-major order.
11673 Each row in the array is indexed starting from 0. The first row provides
11674 a key to the remaining rows: each column in this row provides an identifier
11675 for a debug section, and the offsets in the same column of subsequent rows
11676 refer to that section. The section identifiers for Version 2 are:
11677
11678 DW_SECT_INFO 1 .debug_info.dwo
11679 DW_SECT_TYPES 2 .debug_types.dwo
11680 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11681 DW_SECT_LINE 4 .debug_line.dwo
11682 DW_SECT_LOC 5 .debug_loc.dwo
11683 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11684 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11685 DW_SECT_MACRO 8 .debug_macro.dwo
11686
11687 The section identifiers for Version 5 are:
11688
11689 DW_SECT_INFO_V5 1 .debug_info.dwo
11690 DW_SECT_RESERVED_V5 2 --
11691 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11692 DW_SECT_LINE_V5 4 .debug_line.dwo
11693 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11694 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11695 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11696 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11697
11698 The offsets provided by the CU and TU index sections are the base offsets
11699 for the contributions made by each CU or TU to the corresponding section
11700 in the package file. Each CU and TU header contains an abbrev_offset
11701 field, used to find the abbreviations table for that CU or TU within the
11702 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11703 be interpreted as relative to the base offset given in the index section.
11704 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11705 should be interpreted as relative to the base offset for .debug_line.dwo,
11706 and offsets into other debug sections obtained from DWARF attributes should
11707 also be interpreted as relative to the corresponding base offset.
11708
11709 The table of sizes begins immediately following the table of offsets.
11710 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11711 with L columns and N rows, in row-major order. Each row in the array is
11712 indexed starting from 1 (row 0 is shared by the two tables).
11713
11714 ---
11715
11716 Hash table lookup is handled the same in version 1 and 2:
11717
11718 We assume that N and M will not exceed 2^32 - 1.
11719 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11720
11721 Given a 64-bit compilation unit signature or a type signature S, an entry
11722 in the hash table is located as follows:
11723
11724 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11725 the low-order k bits all set to 1.
11726
11727 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11728
11729 3) If the hash table entry at index H matches the signature, use that
11730 entry. If the hash table entry at index H is unused (all zeroes),
11731 terminate the search: the signature is not present in the table.
11732
11733 4) Let H = (H + H') modulo M. Repeat at Step 3.
11734
11735 Because M > N and H' and M are relatively prime, the search is guaranteed
11736 to stop at an unused slot or find the match. */
11737
11738 /* Create a hash table to map DWO IDs to their CU/TU entry in
11739 .debug_{info,types}.dwo in DWP_FILE.
11740 Returns NULL if there isn't one.
11741 Note: This function processes DWP files only, not DWO files. */
11742
11743 static struct dwp_hash_table *
11744 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11745 struct dwp_file *dwp_file, int is_debug_types)
11746 {
11747 struct objfile *objfile = per_objfile->objfile;
11748 bfd *dbfd = dwp_file->dbfd.get ();
11749 const gdb_byte *index_ptr, *index_end;
11750 struct dwarf2_section_info *index;
11751 uint32_t version, nr_columns, nr_units, nr_slots;
11752 struct dwp_hash_table *htab;
11753
11754 if (is_debug_types)
11755 index = &dwp_file->sections.tu_index;
11756 else
11757 index = &dwp_file->sections.cu_index;
11758
11759 if (index->empty ())
11760 return NULL;
11761 index->read (objfile);
11762
11763 index_ptr = index->buffer;
11764 index_end = index_ptr + index->size;
11765
11766 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11767 For now it's safe to just read 4 bytes (particularly as it's difficult to
11768 tell if you're dealing with Version 5 before you've read the version). */
11769 version = read_4_bytes (dbfd, index_ptr);
11770 index_ptr += 4;
11771 if (version == 2 || version == 5)
11772 nr_columns = read_4_bytes (dbfd, index_ptr);
11773 else
11774 nr_columns = 0;
11775 index_ptr += 4;
11776 nr_units = read_4_bytes (dbfd, index_ptr);
11777 index_ptr += 4;
11778 nr_slots = read_4_bytes (dbfd, index_ptr);
11779 index_ptr += 4;
11780
11781 if (version != 1 && version != 2 && version != 5)
11782 {
11783 error (_("Dwarf Error: unsupported DWP file version (%s)"
11784 " [in module %s]"),
11785 pulongest (version), dwp_file->name);
11786 }
11787 if (nr_slots != (nr_slots & -nr_slots))
11788 {
11789 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11790 " is not power of 2 [in module %s]"),
11791 pulongest (nr_slots), dwp_file->name);
11792 }
11793
11794 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11795 htab->version = version;
11796 htab->nr_columns = nr_columns;
11797 htab->nr_units = nr_units;
11798 htab->nr_slots = nr_slots;
11799 htab->hash_table = index_ptr;
11800 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11801
11802 /* Exit early if the table is empty. */
11803 if (nr_slots == 0 || nr_units == 0
11804 || (version == 2 && nr_columns == 0)
11805 || (version == 5 && nr_columns == 0))
11806 {
11807 /* All must be zero. */
11808 if (nr_slots != 0 || nr_units != 0
11809 || (version == 2 && nr_columns != 0)
11810 || (version == 5 && nr_columns != 0))
11811 {
11812 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11813 " all zero [in modules %s]"),
11814 dwp_file->name);
11815 }
11816 return htab;
11817 }
11818
11819 if (version == 1)
11820 {
11821 htab->section_pool.v1.indices =
11822 htab->unit_table + sizeof (uint32_t) * nr_slots;
11823 /* It's harder to decide whether the section is too small in v1.
11824 V1 is deprecated anyway so we punt. */
11825 }
11826 else if (version == 2)
11827 {
11828 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11829 int *ids = htab->section_pool.v2.section_ids;
11830 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11831 /* Reverse map for error checking. */
11832 int ids_seen[DW_SECT_MAX + 1];
11833 int i;
11834
11835 if (nr_columns < 2)
11836 {
11837 error (_("Dwarf Error: bad DWP hash table, too few columns"
11838 " in section table [in module %s]"),
11839 dwp_file->name);
11840 }
11841 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11842 {
11843 error (_("Dwarf Error: bad DWP hash table, too many columns"
11844 " in section table [in module %s]"),
11845 dwp_file->name);
11846 }
11847 memset (ids, 255, sizeof_ids);
11848 memset (ids_seen, 255, sizeof (ids_seen));
11849 for (i = 0; i < nr_columns; ++i)
11850 {
11851 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11852
11853 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11854 {
11855 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11856 " in section table [in module %s]"),
11857 id, dwp_file->name);
11858 }
11859 if (ids_seen[id] != -1)
11860 {
11861 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11862 " id %d in section table [in module %s]"),
11863 id, dwp_file->name);
11864 }
11865 ids_seen[id] = i;
11866 ids[i] = id;
11867 }
11868 /* Must have exactly one info or types section. */
11869 if (((ids_seen[DW_SECT_INFO] != -1)
11870 + (ids_seen[DW_SECT_TYPES] != -1))
11871 != 1)
11872 {
11873 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11874 " DWO info/types section [in module %s]"),
11875 dwp_file->name);
11876 }
11877 /* Must have an abbrev section. */
11878 if (ids_seen[DW_SECT_ABBREV] == -1)
11879 {
11880 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11881 " section [in module %s]"),
11882 dwp_file->name);
11883 }
11884 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11885 htab->section_pool.v2.sizes =
11886 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11887 * nr_units * nr_columns);
11888 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11889 * nr_units * nr_columns))
11890 > index_end)
11891 {
11892 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11893 " [in module %s]"),
11894 dwp_file->name);
11895 }
11896 }
11897 else /* version == 5 */
11898 {
11899 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11900 int *ids = htab->section_pool.v5.section_ids;
11901 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11902 /* Reverse map for error checking. */
11903 int ids_seen[DW_SECT_MAX_V5 + 1];
11904
11905 if (nr_columns < 2)
11906 {
11907 error (_("Dwarf Error: bad DWP hash table, too few columns"
11908 " in section table [in module %s]"),
11909 dwp_file->name);
11910 }
11911 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11912 {
11913 error (_("Dwarf Error: bad DWP hash table, too many columns"
11914 " in section table [in module %s]"),
11915 dwp_file->name);
11916 }
11917 memset (ids, 255, sizeof_ids);
11918 memset (ids_seen, 255, sizeof (ids_seen));
11919 for (int i = 0; i < nr_columns; ++i)
11920 {
11921 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11922
11923 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11924 {
11925 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11926 " in section table [in module %s]"),
11927 id, dwp_file->name);
11928 }
11929 if (ids_seen[id] != -1)
11930 {
11931 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11932 " id %d in section table [in module %s]"),
11933 id, dwp_file->name);
11934 }
11935 ids_seen[id] = i;
11936 ids[i] = id;
11937 }
11938 /* Must have seen an info section. */
11939 if (ids_seen[DW_SECT_INFO_V5] == -1)
11940 {
11941 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11942 " DWO info/types section [in module %s]"),
11943 dwp_file->name);
11944 }
11945 /* Must have an abbrev section. */
11946 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11947 {
11948 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11949 " section [in module %s]"),
11950 dwp_file->name);
11951 }
11952 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11953 htab->section_pool.v5.sizes
11954 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11955 * nr_units * nr_columns);
11956 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11957 * nr_units * nr_columns))
11958 > index_end)
11959 {
11960 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11961 " [in module %s]"),
11962 dwp_file->name);
11963 }
11964 }
11965
11966 return htab;
11967 }
11968
11969 /* Update SECTIONS with the data from SECTP.
11970
11971 This function is like the other "locate" section routines, but in
11972 this context the sections to read comes from the DWP V1 hash table,
11973 not the full ELF section table.
11974
11975 The result is non-zero for success, or zero if an error was found. */
11976
11977 static int
11978 locate_v1_virtual_dwo_sections (asection *sectp,
11979 struct virtual_v1_dwo_sections *sections)
11980 {
11981 const struct dwop_section_names *names = &dwop_section_names;
11982
11983 if (names->abbrev_dwo.matches (sectp->name))
11984 {
11985 /* There can be only one. */
11986 if (sections->abbrev.s.section != NULL)
11987 return 0;
11988 sections->abbrev.s.section = sectp;
11989 sections->abbrev.size = bfd_section_size (sectp);
11990 }
11991 else if (names->info_dwo.matches (sectp->name)
11992 || names->types_dwo.matches (sectp->name))
11993 {
11994 /* There can be only one. */
11995 if (sections->info_or_types.s.section != NULL)
11996 return 0;
11997 sections->info_or_types.s.section = sectp;
11998 sections->info_or_types.size = bfd_section_size (sectp);
11999 }
12000 else if (names->line_dwo.matches (sectp->name))
12001 {
12002 /* There can be only one. */
12003 if (sections->line.s.section != NULL)
12004 return 0;
12005 sections->line.s.section = sectp;
12006 sections->line.size = bfd_section_size (sectp);
12007 }
12008 else if (names->loc_dwo.matches (sectp->name))
12009 {
12010 /* There can be only one. */
12011 if (sections->loc.s.section != NULL)
12012 return 0;
12013 sections->loc.s.section = sectp;
12014 sections->loc.size = bfd_section_size (sectp);
12015 }
12016 else if (names->macinfo_dwo.matches (sectp->name))
12017 {
12018 /* There can be only one. */
12019 if (sections->macinfo.s.section != NULL)
12020 return 0;
12021 sections->macinfo.s.section = sectp;
12022 sections->macinfo.size = bfd_section_size (sectp);
12023 }
12024 else if (names->macro_dwo.matches (sectp->name))
12025 {
12026 /* There can be only one. */
12027 if (sections->macro.s.section != NULL)
12028 return 0;
12029 sections->macro.s.section = sectp;
12030 sections->macro.size = bfd_section_size (sectp);
12031 }
12032 else if (names->str_offsets_dwo.matches (sectp->name))
12033 {
12034 /* There can be only one. */
12035 if (sections->str_offsets.s.section != NULL)
12036 return 0;
12037 sections->str_offsets.s.section = sectp;
12038 sections->str_offsets.size = bfd_section_size (sectp);
12039 }
12040 else
12041 {
12042 /* No other kind of section is valid. */
12043 return 0;
12044 }
12045
12046 return 1;
12047 }
12048
12049 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12050 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12051 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12052 This is for DWP version 1 files. */
12053
12054 static struct dwo_unit *
12055 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
12056 struct dwp_file *dwp_file,
12057 uint32_t unit_index,
12058 const char *comp_dir,
12059 ULONGEST signature, int is_debug_types)
12060 {
12061 const struct dwp_hash_table *dwp_htab =
12062 is_debug_types ? dwp_file->tus : dwp_file->cus;
12063 bfd *dbfd = dwp_file->dbfd.get ();
12064 const char *kind = is_debug_types ? "TU" : "CU";
12065 struct dwo_file *dwo_file;
12066 struct dwo_unit *dwo_unit;
12067 struct virtual_v1_dwo_sections sections;
12068 void **dwo_file_slot;
12069 int i;
12070
12071 gdb_assert (dwp_file->version == 1);
12072
12073 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
12074 kind, pulongest (unit_index), hex_string (signature),
12075 dwp_file->name);
12076
12077 /* Fetch the sections of this DWO unit.
12078 Put a limit on the number of sections we look for so that bad data
12079 doesn't cause us to loop forever. */
12080
12081 #define MAX_NR_V1_DWO_SECTIONS \
12082 (1 /* .debug_info or .debug_types */ \
12083 + 1 /* .debug_abbrev */ \
12084 + 1 /* .debug_line */ \
12085 + 1 /* .debug_loc */ \
12086 + 1 /* .debug_str_offsets */ \
12087 + 1 /* .debug_macro or .debug_macinfo */ \
12088 + 1 /* trailing zero */)
12089
12090 memset (&sections, 0, sizeof (sections));
12091
12092 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12093 {
12094 asection *sectp;
12095 uint32_t section_nr =
12096 read_4_bytes (dbfd,
12097 dwp_htab->section_pool.v1.indices
12098 + (unit_index + i) * sizeof (uint32_t));
12099
12100 if (section_nr == 0)
12101 break;
12102 if (section_nr >= dwp_file->num_sections)
12103 {
12104 error (_("Dwarf Error: bad DWP hash table, section number too large"
12105 " [in module %s]"),
12106 dwp_file->name);
12107 }
12108
12109 sectp = dwp_file->elf_sections[section_nr];
12110 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12111 {
12112 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12113 " [in module %s]"),
12114 dwp_file->name);
12115 }
12116 }
12117
12118 if (i < 2
12119 || sections.info_or_types.empty ()
12120 || sections.abbrev.empty ())
12121 {
12122 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12123 " [in module %s]"),
12124 dwp_file->name);
12125 }
12126 if (i == MAX_NR_V1_DWO_SECTIONS)
12127 {
12128 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12129 " [in module %s]"),
12130 dwp_file->name);
12131 }
12132
12133 /* It's easier for the rest of the code if we fake a struct dwo_file and
12134 have dwo_unit "live" in that. At least for now.
12135
12136 The DWP file can be made up of a random collection of CUs and TUs.
12137 However, for each CU + set of TUs that came from the same original DWO
12138 file, we can combine them back into a virtual DWO file to save space
12139 (fewer struct dwo_file objects to allocate). Remember that for really
12140 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12141
12142 std::string virtual_dwo_name =
12143 string_printf ("virtual-dwo/%d-%d-%d-%d",
12144 sections.abbrev.get_id (),
12145 sections.line.get_id (),
12146 sections.loc.get_id (),
12147 sections.str_offsets.get_id ());
12148 /* Can we use an existing virtual DWO file? */
12149 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12150 comp_dir);
12151 /* Create one if necessary. */
12152 if (*dwo_file_slot == NULL)
12153 {
12154 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12155 virtual_dwo_name.c_str ());
12156
12157 dwo_file = new struct dwo_file;
12158 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12159 dwo_file->comp_dir = comp_dir;
12160 dwo_file->sections.abbrev = sections.abbrev;
12161 dwo_file->sections.line = sections.line;
12162 dwo_file->sections.loc = sections.loc;
12163 dwo_file->sections.macinfo = sections.macinfo;
12164 dwo_file->sections.macro = sections.macro;
12165 dwo_file->sections.str_offsets = sections.str_offsets;
12166 /* The "str" section is global to the entire DWP file. */
12167 dwo_file->sections.str = dwp_file->sections.str;
12168 /* The info or types section is assigned below to dwo_unit,
12169 there's no need to record it in dwo_file.
12170 Also, we can't simply record type sections in dwo_file because
12171 we record a pointer into the vector in dwo_unit. As we collect more
12172 types we'll grow the vector and eventually have to reallocate space
12173 for it, invalidating all copies of pointers into the previous
12174 contents. */
12175 *dwo_file_slot = dwo_file;
12176 }
12177 else
12178 {
12179 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12180 virtual_dwo_name.c_str ());
12181
12182 dwo_file = (struct dwo_file *) *dwo_file_slot;
12183 }
12184
12185 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12186 dwo_unit->dwo_file = dwo_file;
12187 dwo_unit->signature = signature;
12188 dwo_unit->section =
12189 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12190 *dwo_unit->section = sections.info_or_types;
12191 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12192
12193 return dwo_unit;
12194 }
12195
12196 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
12197 simplify them. Given a pointer to the containing section SECTION, and
12198 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
12199 virtual section of just that piece. */
12200
12201 static struct dwarf2_section_info
12202 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
12203 struct dwarf2_section_info *section,
12204 bfd_size_type offset, bfd_size_type size)
12205 {
12206 struct dwarf2_section_info result;
12207 asection *sectp;
12208
12209 gdb_assert (section != NULL);
12210 gdb_assert (!section->is_virtual);
12211
12212 memset (&result, 0, sizeof (result));
12213 result.s.containing_section = section;
12214 result.is_virtual = true;
12215
12216 if (size == 0)
12217 return result;
12218
12219 sectp = section->get_bfd_section ();
12220
12221 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12222 bounds of the real section. This is a pretty-rare event, so just
12223 flag an error (easier) instead of a warning and trying to cope. */
12224 if (sectp == NULL
12225 || offset + size > bfd_section_size (sectp))
12226 {
12227 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
12228 " in section %s [in module %s]"),
12229 sectp ? bfd_section_name (sectp) : "<unknown>",
12230 objfile_name (per_objfile->objfile));
12231 }
12232
12233 result.virtual_offset = offset;
12234 result.size = size;
12235 return result;
12236 }
12237
12238 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12239 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12240 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12241 This is for DWP version 2 files. */
12242
12243 static struct dwo_unit *
12244 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
12245 struct dwp_file *dwp_file,
12246 uint32_t unit_index,
12247 const char *comp_dir,
12248 ULONGEST signature, int is_debug_types)
12249 {
12250 const struct dwp_hash_table *dwp_htab =
12251 is_debug_types ? dwp_file->tus : dwp_file->cus;
12252 bfd *dbfd = dwp_file->dbfd.get ();
12253 const char *kind = is_debug_types ? "TU" : "CU";
12254 struct dwo_file *dwo_file;
12255 struct dwo_unit *dwo_unit;
12256 struct virtual_v2_or_v5_dwo_sections sections;
12257 void **dwo_file_slot;
12258 int i;
12259
12260 gdb_assert (dwp_file->version == 2);
12261
12262 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
12263 kind, pulongest (unit_index), hex_string (signature),
12264 dwp_file->name);
12265
12266 /* Fetch the section offsets of this DWO unit. */
12267
12268 memset (&sections, 0, sizeof (sections));
12269
12270 for (i = 0; i < dwp_htab->nr_columns; ++i)
12271 {
12272 uint32_t offset = read_4_bytes (dbfd,
12273 dwp_htab->section_pool.v2.offsets
12274 + (((unit_index - 1) * dwp_htab->nr_columns
12275 + i)
12276 * sizeof (uint32_t)));
12277 uint32_t size = read_4_bytes (dbfd,
12278 dwp_htab->section_pool.v2.sizes
12279 + (((unit_index - 1) * dwp_htab->nr_columns
12280 + i)
12281 * sizeof (uint32_t)));
12282
12283 switch (dwp_htab->section_pool.v2.section_ids[i])
12284 {
12285 case DW_SECT_INFO:
12286 case DW_SECT_TYPES:
12287 sections.info_or_types_offset = offset;
12288 sections.info_or_types_size = size;
12289 break;
12290 case DW_SECT_ABBREV:
12291 sections.abbrev_offset = offset;
12292 sections.abbrev_size = size;
12293 break;
12294 case DW_SECT_LINE:
12295 sections.line_offset = offset;
12296 sections.line_size = size;
12297 break;
12298 case DW_SECT_LOC:
12299 sections.loc_offset = offset;
12300 sections.loc_size = size;
12301 break;
12302 case DW_SECT_STR_OFFSETS:
12303 sections.str_offsets_offset = offset;
12304 sections.str_offsets_size = size;
12305 break;
12306 case DW_SECT_MACINFO:
12307 sections.macinfo_offset = offset;
12308 sections.macinfo_size = size;
12309 break;
12310 case DW_SECT_MACRO:
12311 sections.macro_offset = offset;
12312 sections.macro_size = size;
12313 break;
12314 }
12315 }
12316
12317 /* It's easier for the rest of the code if we fake a struct dwo_file and
12318 have dwo_unit "live" in that. At least for now.
12319
12320 The DWP file can be made up of a random collection of CUs and TUs.
12321 However, for each CU + set of TUs that came from the same original DWO
12322 file, we can combine them back into a virtual DWO file to save space
12323 (fewer struct dwo_file objects to allocate). Remember that for really
12324 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12325
12326 std::string virtual_dwo_name =
12327 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12328 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12329 (long) (sections.line_size ? sections.line_offset : 0),
12330 (long) (sections.loc_size ? sections.loc_offset : 0),
12331 (long) (sections.str_offsets_size
12332 ? sections.str_offsets_offset : 0));
12333 /* Can we use an existing virtual DWO file? */
12334 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12335 comp_dir);
12336 /* Create one if necessary. */
12337 if (*dwo_file_slot == NULL)
12338 {
12339 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12340 virtual_dwo_name.c_str ());
12341
12342 dwo_file = new struct dwo_file;
12343 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12344 dwo_file->comp_dir = comp_dir;
12345 dwo_file->sections.abbrev =
12346 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12347 sections.abbrev_offset,
12348 sections.abbrev_size);
12349 dwo_file->sections.line =
12350 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12351 sections.line_offset,
12352 sections.line_size);
12353 dwo_file->sections.loc =
12354 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12355 sections.loc_offset, sections.loc_size);
12356 dwo_file->sections.macinfo =
12357 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12358 sections.macinfo_offset,
12359 sections.macinfo_size);
12360 dwo_file->sections.macro =
12361 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12362 sections.macro_offset,
12363 sections.macro_size);
12364 dwo_file->sections.str_offsets =
12365 create_dwp_v2_or_v5_section (per_objfile,
12366 &dwp_file->sections.str_offsets,
12367 sections.str_offsets_offset,
12368 sections.str_offsets_size);
12369 /* The "str" section is global to the entire DWP file. */
12370 dwo_file->sections.str = dwp_file->sections.str;
12371 /* The info or types section is assigned below to dwo_unit,
12372 there's no need to record it in dwo_file.
12373 Also, we can't simply record type sections in dwo_file because
12374 we record a pointer into the vector in dwo_unit. As we collect more
12375 types we'll grow the vector and eventually have to reallocate space
12376 for it, invalidating all copies of pointers into the previous
12377 contents. */
12378 *dwo_file_slot = dwo_file;
12379 }
12380 else
12381 {
12382 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12383 virtual_dwo_name.c_str ());
12384
12385 dwo_file = (struct dwo_file *) *dwo_file_slot;
12386 }
12387
12388 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12389 dwo_unit->dwo_file = dwo_file;
12390 dwo_unit->signature = signature;
12391 dwo_unit->section =
12392 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12393 *dwo_unit->section = create_dwp_v2_or_v5_section
12394 (per_objfile,
12395 is_debug_types
12396 ? &dwp_file->sections.types
12397 : &dwp_file->sections.info,
12398 sections.info_or_types_offset,
12399 sections.info_or_types_size);
12400 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12401
12402 return dwo_unit;
12403 }
12404
12405 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12406 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12407 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12408 This is for DWP version 5 files. */
12409
12410 static struct dwo_unit *
12411 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12412 struct dwp_file *dwp_file,
12413 uint32_t unit_index,
12414 const char *comp_dir,
12415 ULONGEST signature, int is_debug_types)
12416 {
12417 const struct dwp_hash_table *dwp_htab
12418 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12419 bfd *dbfd = dwp_file->dbfd.get ();
12420 const char *kind = is_debug_types ? "TU" : "CU";
12421 struct dwo_file *dwo_file;
12422 struct dwo_unit *dwo_unit;
12423 struct virtual_v2_or_v5_dwo_sections sections {};
12424 void **dwo_file_slot;
12425
12426 gdb_assert (dwp_file->version == 5);
12427
12428 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
12429 kind, pulongest (unit_index), hex_string (signature),
12430 dwp_file->name);
12431
12432 /* Fetch the section offsets of this DWO unit. */
12433
12434 /* memset (&sections, 0, sizeof (sections)); */
12435
12436 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12437 {
12438 uint32_t offset = read_4_bytes (dbfd,
12439 dwp_htab->section_pool.v5.offsets
12440 + (((unit_index - 1)
12441 * dwp_htab->nr_columns
12442 + i)
12443 * sizeof (uint32_t)));
12444 uint32_t size = read_4_bytes (dbfd,
12445 dwp_htab->section_pool.v5.sizes
12446 + (((unit_index - 1) * dwp_htab->nr_columns
12447 + i)
12448 * sizeof (uint32_t)));
12449
12450 switch (dwp_htab->section_pool.v5.section_ids[i])
12451 {
12452 case DW_SECT_ABBREV_V5:
12453 sections.abbrev_offset = offset;
12454 sections.abbrev_size = size;
12455 break;
12456 case DW_SECT_INFO_V5:
12457 sections.info_or_types_offset = offset;
12458 sections.info_or_types_size = size;
12459 break;
12460 case DW_SECT_LINE_V5:
12461 sections.line_offset = offset;
12462 sections.line_size = size;
12463 break;
12464 case DW_SECT_LOCLISTS_V5:
12465 sections.loclists_offset = offset;
12466 sections.loclists_size = size;
12467 break;
12468 case DW_SECT_MACRO_V5:
12469 sections.macro_offset = offset;
12470 sections.macro_size = size;
12471 break;
12472 case DW_SECT_RNGLISTS_V5:
12473 sections.rnglists_offset = offset;
12474 sections.rnglists_size = size;
12475 break;
12476 case DW_SECT_STR_OFFSETS_V5:
12477 sections.str_offsets_offset = offset;
12478 sections.str_offsets_size = size;
12479 break;
12480 case DW_SECT_RESERVED_V5:
12481 default:
12482 break;
12483 }
12484 }
12485
12486 /* It's easier for the rest of the code if we fake a struct dwo_file and
12487 have dwo_unit "live" in that. At least for now.
12488
12489 The DWP file can be made up of a random collection of CUs and TUs.
12490 However, for each CU + set of TUs that came from the same original DWO
12491 file, we can combine them back into a virtual DWO file to save space
12492 (fewer struct dwo_file objects to allocate). Remember that for really
12493 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12494
12495 std::string virtual_dwo_name =
12496 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
12497 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12498 (long) (sections.line_size ? sections.line_offset : 0),
12499 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12500 (long) (sections.str_offsets_size
12501 ? sections.str_offsets_offset : 0),
12502 (long) (sections.macro_size ? sections.macro_offset : 0),
12503 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
12504 /* Can we use an existing virtual DWO file? */
12505 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
12506 virtual_dwo_name.c_str (),
12507 comp_dir);
12508 /* Create one if necessary. */
12509 if (*dwo_file_slot == NULL)
12510 {
12511 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12512 virtual_dwo_name.c_str ());
12513
12514 dwo_file = new struct dwo_file;
12515 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12516 dwo_file->comp_dir = comp_dir;
12517 dwo_file->sections.abbrev =
12518 create_dwp_v2_or_v5_section (per_objfile,
12519 &dwp_file->sections.abbrev,
12520 sections.abbrev_offset,
12521 sections.abbrev_size);
12522 dwo_file->sections.line =
12523 create_dwp_v2_or_v5_section (per_objfile,
12524 &dwp_file->sections.line,
12525 sections.line_offset, sections.line_size);
12526 dwo_file->sections.macro =
12527 create_dwp_v2_or_v5_section (per_objfile,
12528 &dwp_file->sections.macro,
12529 sections.macro_offset,
12530 sections.macro_size);
12531 dwo_file->sections.loclists =
12532 create_dwp_v2_or_v5_section (per_objfile,
12533 &dwp_file->sections.loclists,
12534 sections.loclists_offset,
12535 sections.loclists_size);
12536 dwo_file->sections.rnglists =
12537 create_dwp_v2_or_v5_section (per_objfile,
12538 &dwp_file->sections.rnglists,
12539 sections.rnglists_offset,
12540 sections.rnglists_size);
12541 dwo_file->sections.str_offsets =
12542 create_dwp_v2_or_v5_section (per_objfile,
12543 &dwp_file->sections.str_offsets,
12544 sections.str_offsets_offset,
12545 sections.str_offsets_size);
12546 /* The "str" section is global to the entire DWP file. */
12547 dwo_file->sections.str = dwp_file->sections.str;
12548 /* The info or types section is assigned below to dwo_unit,
12549 there's no need to record it in dwo_file.
12550 Also, we can't simply record type sections in dwo_file because
12551 we record a pointer into the vector in dwo_unit. As we collect more
12552 types we'll grow the vector and eventually have to reallocate space
12553 for it, invalidating all copies of pointers into the previous
12554 contents. */
12555 *dwo_file_slot = dwo_file;
12556 }
12557 else
12558 {
12559 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12560 virtual_dwo_name.c_str ());
12561
12562 dwo_file = (struct dwo_file *) *dwo_file_slot;
12563 }
12564
12565 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12566 dwo_unit->dwo_file = dwo_file;
12567 dwo_unit->signature = signature;
12568 dwo_unit->section
12569 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12570 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
12571 &dwp_file->sections.info,
12572 sections.info_or_types_offset,
12573 sections.info_or_types_size);
12574 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12575
12576 return dwo_unit;
12577 }
12578
12579 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12580 Returns NULL if the signature isn't found. */
12581
12582 static struct dwo_unit *
12583 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12584 struct dwp_file *dwp_file, const char *comp_dir,
12585 ULONGEST signature, int is_debug_types)
12586 {
12587 const struct dwp_hash_table *dwp_htab =
12588 is_debug_types ? dwp_file->tus : dwp_file->cus;
12589 bfd *dbfd = dwp_file->dbfd.get ();
12590 uint32_t mask = dwp_htab->nr_slots - 1;
12591 uint32_t hash = signature & mask;
12592 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12593 unsigned int i;
12594 void **slot;
12595 struct dwo_unit find_dwo_cu;
12596
12597 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12598 find_dwo_cu.signature = signature;
12599 slot = htab_find_slot (is_debug_types
12600 ? dwp_file->loaded_tus.get ()
12601 : dwp_file->loaded_cus.get (),
12602 &find_dwo_cu, INSERT);
12603
12604 if (*slot != NULL)
12605 return (struct dwo_unit *) *slot;
12606
12607 /* Use a for loop so that we don't loop forever on bad debug info. */
12608 for (i = 0; i < dwp_htab->nr_slots; ++i)
12609 {
12610 ULONGEST signature_in_table;
12611
12612 signature_in_table =
12613 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12614 if (signature_in_table == signature)
12615 {
12616 uint32_t unit_index =
12617 read_4_bytes (dbfd,
12618 dwp_htab->unit_table + hash * sizeof (uint32_t));
12619
12620 if (dwp_file->version == 1)
12621 {
12622 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12623 unit_index, comp_dir,
12624 signature, is_debug_types);
12625 }
12626 else if (dwp_file->version == 2)
12627 {
12628 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12629 unit_index, comp_dir,
12630 signature, is_debug_types);
12631 }
12632 else /* version == 5 */
12633 {
12634 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12635 unit_index, comp_dir,
12636 signature, is_debug_types);
12637 }
12638 return (struct dwo_unit *) *slot;
12639 }
12640 if (signature_in_table == 0)
12641 return NULL;
12642 hash = (hash + hash2) & mask;
12643 }
12644
12645 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12646 " [in module %s]"),
12647 dwp_file->name);
12648 }
12649
12650 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12651 Open the file specified by FILE_NAME and hand it off to BFD for
12652 preliminary analysis. Return a newly initialized bfd *, which
12653 includes a canonicalized copy of FILE_NAME.
12654 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12655 SEARCH_CWD is true if the current directory is to be searched.
12656 It will be searched before debug-file-directory.
12657 If successful, the file is added to the bfd include table of the
12658 objfile's bfd (see gdb_bfd_record_inclusion).
12659 If unable to find/open the file, return NULL.
12660 NOTE: This function is derived from symfile_bfd_open. */
12661
12662 static gdb_bfd_ref_ptr
12663 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12664 const char *file_name, int is_dwp, int search_cwd)
12665 {
12666 int desc;
12667 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12668 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12669 to debug_file_directory. */
12670 const char *search_path;
12671 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12672
12673 gdb::unique_xmalloc_ptr<char> search_path_holder;
12674 if (search_cwd)
12675 {
12676 if (*debug_file_directory != '\0')
12677 {
12678 search_path_holder.reset (concat (".", dirname_separator_string,
12679 debug_file_directory,
12680 (char *) NULL));
12681 search_path = search_path_holder.get ();
12682 }
12683 else
12684 search_path = ".";
12685 }
12686 else
12687 search_path = debug_file_directory;
12688
12689 openp_flags flags = OPF_RETURN_REALPATH;
12690 if (is_dwp)
12691 flags |= OPF_SEARCH_IN_PATH;
12692
12693 gdb::unique_xmalloc_ptr<char> absolute_name;
12694 desc = openp (search_path, flags, file_name,
12695 O_RDONLY | O_BINARY, &absolute_name);
12696 if (desc < 0)
12697 return NULL;
12698
12699 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12700 gnutarget, desc));
12701 if (sym_bfd == NULL)
12702 return NULL;
12703 bfd_set_cacheable (sym_bfd.get (), 1);
12704
12705 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12706 return NULL;
12707
12708 /* Success. Record the bfd as having been included by the objfile's bfd.
12709 This is important because things like demangled_names_hash lives in the
12710 objfile's per_bfd space and may have references to things like symbol
12711 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12712 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12713
12714 return sym_bfd;
12715 }
12716
12717 /* Try to open DWO file FILE_NAME.
12718 COMP_DIR is the DW_AT_comp_dir attribute.
12719 The result is the bfd handle of the file.
12720 If there is a problem finding or opening the file, return NULL.
12721 Upon success, the canonicalized path of the file is stored in the bfd,
12722 same as symfile_bfd_open. */
12723
12724 static gdb_bfd_ref_ptr
12725 open_dwo_file (dwarf2_per_objfile *per_objfile,
12726 const char *file_name, const char *comp_dir)
12727 {
12728 if (IS_ABSOLUTE_PATH (file_name))
12729 return try_open_dwop_file (per_objfile, file_name,
12730 0 /*is_dwp*/, 0 /*search_cwd*/);
12731
12732 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12733
12734 if (comp_dir != NULL)
12735 {
12736 gdb::unique_xmalloc_ptr<char> path_to_try
12737 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12738
12739 /* NOTE: If comp_dir is a relative path, this will also try the
12740 search path, which seems useful. */
12741 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12742 0 /*is_dwp*/,
12743 1 /*search_cwd*/));
12744 if (abfd != NULL)
12745 return abfd;
12746 }
12747
12748 /* That didn't work, try debug-file-directory, which, despite its name,
12749 is a list of paths. */
12750
12751 if (*debug_file_directory == '\0')
12752 return NULL;
12753
12754 return try_open_dwop_file (per_objfile, file_name,
12755 0 /*is_dwp*/, 1 /*search_cwd*/);
12756 }
12757
12758 /* This function is mapped across the sections and remembers the offset and
12759 size of each of the DWO debugging sections we are interested in. */
12760
12761 static void
12762 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12763 dwo_sections *dwo_sections)
12764 {
12765 const struct dwop_section_names *names = &dwop_section_names;
12766
12767 if (names->abbrev_dwo.matches (sectp->name))
12768 {
12769 dwo_sections->abbrev.s.section = sectp;
12770 dwo_sections->abbrev.size = bfd_section_size (sectp);
12771 }
12772 else if (names->info_dwo.matches (sectp->name))
12773 {
12774 dwo_sections->info.s.section = sectp;
12775 dwo_sections->info.size = bfd_section_size (sectp);
12776 }
12777 else if (names->line_dwo.matches (sectp->name))
12778 {
12779 dwo_sections->line.s.section = sectp;
12780 dwo_sections->line.size = bfd_section_size (sectp);
12781 }
12782 else if (names->loc_dwo.matches (sectp->name))
12783 {
12784 dwo_sections->loc.s.section = sectp;
12785 dwo_sections->loc.size = bfd_section_size (sectp);
12786 }
12787 else if (names->loclists_dwo.matches (sectp->name))
12788 {
12789 dwo_sections->loclists.s.section = sectp;
12790 dwo_sections->loclists.size = bfd_section_size (sectp);
12791 }
12792 else if (names->macinfo_dwo.matches (sectp->name))
12793 {
12794 dwo_sections->macinfo.s.section = sectp;
12795 dwo_sections->macinfo.size = bfd_section_size (sectp);
12796 }
12797 else if (names->macro_dwo.matches (sectp->name))
12798 {
12799 dwo_sections->macro.s.section = sectp;
12800 dwo_sections->macro.size = bfd_section_size (sectp);
12801 }
12802 else if (names->rnglists_dwo.matches (sectp->name))
12803 {
12804 dwo_sections->rnglists.s.section = sectp;
12805 dwo_sections->rnglists.size = bfd_section_size (sectp);
12806 }
12807 else if (names->str_dwo.matches (sectp->name))
12808 {
12809 dwo_sections->str.s.section = sectp;
12810 dwo_sections->str.size = bfd_section_size (sectp);
12811 }
12812 else if (names->str_offsets_dwo.matches (sectp->name))
12813 {
12814 dwo_sections->str_offsets.s.section = sectp;
12815 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12816 }
12817 else if (names->types_dwo.matches (sectp->name))
12818 {
12819 struct dwarf2_section_info type_section;
12820
12821 memset (&type_section, 0, sizeof (type_section));
12822 type_section.s.section = sectp;
12823 type_section.size = bfd_section_size (sectp);
12824 dwo_sections->types.push_back (type_section);
12825 }
12826 }
12827
12828 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12829 by PER_CU. This is for the non-DWP case.
12830 The result is NULL if DWO_NAME can't be found. */
12831
12832 static struct dwo_file *
12833 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12834 const char *comp_dir)
12835 {
12836 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12837
12838 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12839 if (dbfd == NULL)
12840 {
12841 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12842
12843 return NULL;
12844 }
12845
12846 dwo_file_up dwo_file (new struct dwo_file);
12847 dwo_file->dwo_name = dwo_name;
12848 dwo_file->comp_dir = comp_dir;
12849 dwo_file->dbfd = std::move (dbfd);
12850
12851 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12852 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12853 &dwo_file->sections);
12854
12855 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12856 dwo_file->cus);
12857
12858 if (cu->per_cu->dwarf_version < 5)
12859 {
12860 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12861 dwo_file->sections.types, dwo_file->tus);
12862 }
12863 else
12864 {
12865 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12866 &dwo_file->sections.info, dwo_file->tus,
12867 rcuh_kind::COMPILE);
12868 }
12869
12870 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
12871
12872 return dwo_file.release ();
12873 }
12874
12875 /* This function is mapped across the sections and remembers the offset and
12876 size of each of the DWP debugging sections common to version 1 and 2 that
12877 we are interested in. */
12878
12879 static void
12880 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12881 dwp_file *dwp_file)
12882 {
12883 const struct dwop_section_names *names = &dwop_section_names;
12884 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12885
12886 /* Record the ELF section number for later lookup: this is what the
12887 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12888 gdb_assert (elf_section_nr < dwp_file->num_sections);
12889 dwp_file->elf_sections[elf_section_nr] = sectp;
12890
12891 /* Look for specific sections that we need. */
12892 if (names->str_dwo.matches (sectp->name))
12893 {
12894 dwp_file->sections.str.s.section = sectp;
12895 dwp_file->sections.str.size = bfd_section_size (sectp);
12896 }
12897 else if (names->cu_index.matches (sectp->name))
12898 {
12899 dwp_file->sections.cu_index.s.section = sectp;
12900 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12901 }
12902 else if (names->tu_index.matches (sectp->name))
12903 {
12904 dwp_file->sections.tu_index.s.section = sectp;
12905 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12906 }
12907 }
12908
12909 /* This function is mapped across the sections and remembers the offset and
12910 size of each of the DWP version 2 debugging sections that we are interested
12911 in. This is split into a separate function because we don't know if we
12912 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12913
12914 static void
12915 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12916 {
12917 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12918 const struct dwop_section_names *names = &dwop_section_names;
12919 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12920
12921 /* Record the ELF section number for later lookup: this is what the
12922 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12923 gdb_assert (elf_section_nr < dwp_file->num_sections);
12924 dwp_file->elf_sections[elf_section_nr] = sectp;
12925
12926 /* Look for specific sections that we need. */
12927 if (names->abbrev_dwo.matches (sectp->name))
12928 {
12929 dwp_file->sections.abbrev.s.section = sectp;
12930 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12931 }
12932 else if (names->info_dwo.matches (sectp->name))
12933 {
12934 dwp_file->sections.info.s.section = sectp;
12935 dwp_file->sections.info.size = bfd_section_size (sectp);
12936 }
12937 else if (names->line_dwo.matches (sectp->name))
12938 {
12939 dwp_file->sections.line.s.section = sectp;
12940 dwp_file->sections.line.size = bfd_section_size (sectp);
12941 }
12942 else if (names->loc_dwo.matches (sectp->name))
12943 {
12944 dwp_file->sections.loc.s.section = sectp;
12945 dwp_file->sections.loc.size = bfd_section_size (sectp);
12946 }
12947 else if (names->macinfo_dwo.matches (sectp->name))
12948 {
12949 dwp_file->sections.macinfo.s.section = sectp;
12950 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12951 }
12952 else if (names->macro_dwo.matches (sectp->name))
12953 {
12954 dwp_file->sections.macro.s.section = sectp;
12955 dwp_file->sections.macro.size = bfd_section_size (sectp);
12956 }
12957 else if (names->str_offsets_dwo.matches (sectp->name))
12958 {
12959 dwp_file->sections.str_offsets.s.section = sectp;
12960 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12961 }
12962 else if (names->types_dwo.matches (sectp->name))
12963 {
12964 dwp_file->sections.types.s.section = sectp;
12965 dwp_file->sections.types.size = bfd_section_size (sectp);
12966 }
12967 }
12968
12969 /* This function is mapped across the sections and remembers the offset and
12970 size of each of the DWP version 5 debugging sections that we are interested
12971 in. This is split into a separate function because we don't know if we
12972 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12973
12974 static void
12975 dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12976 {
12977 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12978 const struct dwop_section_names *names = &dwop_section_names;
12979 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12980
12981 /* Record the ELF section number for later lookup: this is what the
12982 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12983 gdb_assert (elf_section_nr < dwp_file->num_sections);
12984 dwp_file->elf_sections[elf_section_nr] = sectp;
12985
12986 /* Look for specific sections that we need. */
12987 if (names->abbrev_dwo.matches (sectp->name))
12988 {
12989 dwp_file->sections.abbrev.s.section = sectp;
12990 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12991 }
12992 else if (names->info_dwo.matches (sectp->name))
12993 {
12994 dwp_file->sections.info.s.section = sectp;
12995 dwp_file->sections.info.size = bfd_section_size (sectp);
12996 }
12997 else if (names->line_dwo.matches (sectp->name))
12998 {
12999 dwp_file->sections.line.s.section = sectp;
13000 dwp_file->sections.line.size = bfd_section_size (sectp);
13001 }
13002 else if (names->loclists_dwo.matches (sectp->name))
13003 {
13004 dwp_file->sections.loclists.s.section = sectp;
13005 dwp_file->sections.loclists.size = bfd_section_size (sectp);
13006 }
13007 else if (names->macro_dwo.matches (sectp->name))
13008 {
13009 dwp_file->sections.macro.s.section = sectp;
13010 dwp_file->sections.macro.size = bfd_section_size (sectp);
13011 }
13012 else if (names->rnglists_dwo.matches (sectp->name))
13013 {
13014 dwp_file->sections.rnglists.s.section = sectp;
13015 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
13016 }
13017 else if (names->str_offsets_dwo.matches (sectp->name))
13018 {
13019 dwp_file->sections.str_offsets.s.section = sectp;
13020 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13021 }
13022 }
13023
13024 /* Hash function for dwp_file loaded CUs/TUs. */
13025
13026 static hashval_t
13027 hash_dwp_loaded_cutus (const void *item)
13028 {
13029 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13030
13031 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13032 return dwo_unit->signature;
13033 }
13034
13035 /* Equality function for dwp_file loaded CUs/TUs. */
13036
13037 static int
13038 eq_dwp_loaded_cutus (const void *a, const void *b)
13039 {
13040 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13041 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13042
13043 return dua->signature == dub->signature;
13044 }
13045
13046 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13047
13048 static htab_up
13049 allocate_dwp_loaded_cutus_table ()
13050 {
13051 return htab_up (htab_create_alloc (3,
13052 hash_dwp_loaded_cutus,
13053 eq_dwp_loaded_cutus,
13054 NULL, xcalloc, xfree));
13055 }
13056
13057 /* Try to open DWP file FILE_NAME.
13058 The result is the bfd handle of the file.
13059 If there is a problem finding or opening the file, return NULL.
13060 Upon success, the canonicalized path of the file is stored in the bfd,
13061 same as symfile_bfd_open. */
13062
13063 static gdb_bfd_ref_ptr
13064 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
13065 {
13066 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
13067 1 /*is_dwp*/,
13068 1 /*search_cwd*/));
13069 if (abfd != NULL)
13070 return abfd;
13071
13072 /* Work around upstream bug 15652.
13073 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13074 [Whether that's a "bug" is debatable, but it is getting in our way.]
13075 We have no real idea where the dwp file is, because gdb's realpath-ing
13076 of the executable's path may have discarded the needed info.
13077 [IWBN if the dwp file name was recorded in the executable, akin to
13078 .gnu_debuglink, but that doesn't exist yet.]
13079 Strip the directory from FILE_NAME and search again. */
13080 if (*debug_file_directory != '\0')
13081 {
13082 /* Don't implicitly search the current directory here.
13083 If the user wants to search "." to handle this case,
13084 it must be added to debug-file-directory. */
13085 return try_open_dwop_file (per_objfile, lbasename (file_name),
13086 1 /*is_dwp*/,
13087 0 /*search_cwd*/);
13088 }
13089
13090 return NULL;
13091 }
13092
13093 /* Initialize the use of the DWP file for the current objfile.
13094 By convention the name of the DWP file is ${objfile}.dwp.
13095 The result is NULL if it can't be found. */
13096
13097 static std::unique_ptr<struct dwp_file>
13098 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
13099 {
13100 struct objfile *objfile = per_objfile->objfile;
13101
13102 /* Try to find first .dwp for the binary file before any symbolic links
13103 resolving. */
13104
13105 /* If the objfile is a debug file, find the name of the real binary
13106 file and get the name of dwp file from there. */
13107 std::string dwp_name;
13108 if (objfile->separate_debug_objfile_backlink != NULL)
13109 {
13110 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13111 const char *backlink_basename = lbasename (backlink->original_name);
13112
13113 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13114 }
13115 else
13116 dwp_name = objfile->original_name;
13117
13118 dwp_name += ".dwp";
13119
13120 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
13121 if (dbfd == NULL
13122 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13123 {
13124 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13125 dwp_name = objfile_name (objfile);
13126 dwp_name += ".dwp";
13127 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
13128 }
13129
13130 if (dbfd == NULL)
13131 {
13132 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
13133
13134 return std::unique_ptr<dwp_file> ();
13135 }
13136
13137 const char *name = bfd_get_filename (dbfd.get ());
13138 std::unique_ptr<struct dwp_file> dwp_file
13139 (new struct dwp_file (name, std::move (dbfd)));
13140
13141 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13142 dwp_file->elf_sections =
13143 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
13144 dwp_file->num_sections, asection *);
13145
13146 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13147 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
13148 dwp_file.get ());
13149
13150 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
13151
13152 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
13153
13154 /* The DWP file version is stored in the hash table. Oh well. */
13155 if (dwp_file->cus && dwp_file->tus
13156 && dwp_file->cus->version != dwp_file->tus->version)
13157 {
13158 /* Technically speaking, we should try to limp along, but this is
13159 pretty bizarre. We use pulongest here because that's the established
13160 portability solution (e.g, we cannot use %u for uint32_t). */
13161 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13162 " TU version %s [in DWP file %s]"),
13163 pulongest (dwp_file->cus->version),
13164 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13165 }
13166
13167 if (dwp_file->cus)
13168 dwp_file->version = dwp_file->cus->version;
13169 else if (dwp_file->tus)
13170 dwp_file->version = dwp_file->tus->version;
13171 else
13172 dwp_file->version = 2;
13173
13174 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13175 {
13176 if (dwp_file->version == 2)
13177 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
13178 dwp_file.get ());
13179 else
13180 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
13181 dwp_file.get ());
13182 }
13183
13184 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
13185 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
13186
13187 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
13188 dwarf_read_debug_printf (" %s CUs, %s TUs",
13189 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13190 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13191
13192 return dwp_file;
13193 }
13194
13195 /* Wrapper around open_and_init_dwp_file, only open it once. */
13196
13197 static struct dwp_file *
13198 get_dwp_file (dwarf2_per_objfile *per_objfile)
13199 {
13200 if (!per_objfile->per_bfd->dwp_checked)
13201 {
13202 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
13203 per_objfile->per_bfd->dwp_checked = 1;
13204 }
13205 return per_objfile->per_bfd->dwp_file.get ();
13206 }
13207
13208 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13209 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13210 or in the DWP file for the objfile, referenced by THIS_UNIT.
13211 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13212 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13213
13214 This is called, for example, when wanting to read a variable with a
13215 complex location. Therefore we don't want to do file i/o for every call.
13216 Therefore we don't want to look for a DWO file on every call.
13217 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13218 then we check if we've already seen DWO_NAME, and only THEN do we check
13219 for a DWO file.
13220
13221 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13222 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13223
13224 static struct dwo_unit *
13225 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13226 ULONGEST signature, int is_debug_types)
13227 {
13228 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13229 struct objfile *objfile = per_objfile->objfile;
13230 const char *kind = is_debug_types ? "TU" : "CU";
13231 void **dwo_file_slot;
13232 struct dwo_file *dwo_file;
13233 struct dwp_file *dwp_file;
13234
13235 /* First see if there's a DWP file.
13236 If we have a DWP file but didn't find the DWO inside it, don't
13237 look for the original DWO file. It makes gdb behave differently
13238 depending on whether one is debugging in the build tree. */
13239
13240 dwp_file = get_dwp_file (per_objfile);
13241 if (dwp_file != NULL)
13242 {
13243 const struct dwp_hash_table *dwp_htab =
13244 is_debug_types ? dwp_file->tus : dwp_file->cus;
13245
13246 if (dwp_htab != NULL)
13247 {
13248 struct dwo_unit *dwo_cutu =
13249 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
13250 is_debug_types);
13251
13252 if (dwo_cutu != NULL)
13253 {
13254 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
13255 kind, hex_string (signature),
13256 host_address_to_string (dwo_cutu));
13257
13258 return dwo_cutu;
13259 }
13260 }
13261 }
13262 else
13263 {
13264 /* No DWP file, look for the DWO file. */
13265
13266 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
13267 if (*dwo_file_slot == NULL)
13268 {
13269 /* Read in the file and build a table of the CUs/TUs it contains. */
13270 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
13271 }
13272 /* NOTE: This will be NULL if unable to open the file. */
13273 dwo_file = (struct dwo_file *) *dwo_file_slot;
13274
13275 if (dwo_file != NULL)
13276 {
13277 struct dwo_unit *dwo_cutu = NULL;
13278
13279 if (is_debug_types && dwo_file->tus)
13280 {
13281 struct dwo_unit find_dwo_cutu;
13282
13283 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13284 find_dwo_cutu.signature = signature;
13285 dwo_cutu
13286 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
13287 &find_dwo_cutu);
13288 }
13289 else if (!is_debug_types && dwo_file->cus)
13290 {
13291 struct dwo_unit find_dwo_cutu;
13292
13293 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13294 find_dwo_cutu.signature = signature;
13295 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
13296 &find_dwo_cutu);
13297 }
13298
13299 if (dwo_cutu != NULL)
13300 {
13301 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
13302 kind, dwo_name, hex_string (signature),
13303 host_address_to_string (dwo_cutu));
13304
13305 return dwo_cutu;
13306 }
13307 }
13308 }
13309
13310 /* We didn't find it. This could mean a dwo_id mismatch, or
13311 someone deleted the DWO/DWP file, or the search path isn't set up
13312 correctly to find the file. */
13313
13314 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
13315 kind, dwo_name, hex_string (signature));
13316
13317 /* This is a warning and not a complaint because it can be caused by
13318 pilot error (e.g., user accidentally deleting the DWO). */
13319 {
13320 /* Print the name of the DWP file if we looked there, helps the user
13321 better diagnose the problem. */
13322 std::string dwp_text;
13323
13324 if (dwp_file != NULL)
13325 dwp_text = string_printf (" [in DWP file %s]",
13326 lbasename (dwp_file->name));
13327
13328 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13329 " [in module %s]"),
13330 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13331 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
13332 }
13333 return NULL;
13334 }
13335
13336 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13337 See lookup_dwo_cutu_unit for details. */
13338
13339 static struct dwo_unit *
13340 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13341 ULONGEST signature)
13342 {
13343 gdb_assert (!cu->per_cu->is_debug_types);
13344
13345 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
13346 }
13347
13348 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13349 See lookup_dwo_cutu_unit for details. */
13350
13351 static struct dwo_unit *
13352 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
13353 {
13354 gdb_assert (cu->per_cu->is_debug_types);
13355
13356 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13357
13358 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
13359 }
13360
13361 /* Traversal function for queue_and_load_all_dwo_tus. */
13362
13363 static int
13364 queue_and_load_dwo_tu (void **slot, void *info)
13365 {
13366 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13367 dwarf2_cu *cu = (dwarf2_cu *) info;
13368 ULONGEST signature = dwo_unit->signature;
13369 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
13370
13371 if (sig_type != NULL)
13372 {
13373 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13374
13375 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13376 a real dependency of PER_CU on SIG_TYPE. That is detected later
13377 while processing PER_CU. */
13378 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
13379 load_full_type_unit (sig_cu, cu->per_objfile);
13380 cu->per_cu->imported_symtabs_push (sig_cu);
13381 }
13382
13383 return 1;
13384 }
13385
13386 /* Queue all TUs contained in the DWO of CU to be read in.
13387 The DWO may have the only definition of the type, though it may not be
13388 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13389 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13390
13391 static void
13392 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
13393 {
13394 struct dwo_unit *dwo_unit;
13395 struct dwo_file *dwo_file;
13396
13397 gdb_assert (cu != nullptr);
13398 gdb_assert (!cu->per_cu->is_debug_types);
13399 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
13400
13401 dwo_unit = cu->dwo_unit;
13402 gdb_assert (dwo_unit != NULL);
13403
13404 dwo_file = dwo_unit->dwo_file;
13405 if (dwo_file->tus != NULL)
13406 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
13407 }
13408
13409 /* Read in various DIEs. */
13410
13411 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13412 Inherit only the children of the DW_AT_abstract_origin DIE not being
13413 already referenced by DW_AT_abstract_origin from the children of the
13414 current DIE. */
13415
13416 static void
13417 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13418 {
13419 struct die_info *child_die;
13420 sect_offset *offsetp;
13421 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13422 struct die_info *origin_die;
13423 /* Iterator of the ORIGIN_DIE children. */
13424 struct die_info *origin_child_die;
13425 struct attribute *attr;
13426 struct dwarf2_cu *origin_cu;
13427 struct pending **origin_previous_list_in_scope;
13428
13429 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13430 if (!attr)
13431 return;
13432
13433 /* Note that following die references may follow to a die in a
13434 different cu. */
13435
13436 origin_cu = cu;
13437 origin_die = follow_die_ref (die, attr, &origin_cu);
13438
13439 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13440 symbols in. */
13441 origin_previous_list_in_scope = origin_cu->list_in_scope;
13442 origin_cu->list_in_scope = cu->list_in_scope;
13443
13444 if (die->tag != origin_die->tag
13445 && !(die->tag == DW_TAG_inlined_subroutine
13446 && origin_die->tag == DW_TAG_subprogram))
13447 complaint (_("DIE %s and its abstract origin %s have different tags"),
13448 sect_offset_str (die->sect_off),
13449 sect_offset_str (origin_die->sect_off));
13450
13451 std::vector<sect_offset> offsets;
13452
13453 for (child_die = die->child;
13454 child_die && child_die->tag;
13455 child_die = child_die->sibling)
13456 {
13457 struct die_info *child_origin_die;
13458 struct dwarf2_cu *child_origin_cu;
13459
13460 /* We are trying to process concrete instance entries:
13461 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13462 it's not relevant to our analysis here. i.e. detecting DIEs that are
13463 present in the abstract instance but not referenced in the concrete
13464 one. */
13465 if (child_die->tag == DW_TAG_call_site
13466 || child_die->tag == DW_TAG_GNU_call_site)
13467 continue;
13468
13469 /* For each CHILD_DIE, find the corresponding child of
13470 ORIGIN_DIE. If there is more than one layer of
13471 DW_AT_abstract_origin, follow them all; there shouldn't be,
13472 but GCC versions at least through 4.4 generate this (GCC PR
13473 40573). */
13474 child_origin_die = child_die;
13475 child_origin_cu = cu;
13476 while (1)
13477 {
13478 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13479 child_origin_cu);
13480 if (attr == NULL)
13481 break;
13482 child_origin_die = follow_die_ref (child_origin_die, attr,
13483 &child_origin_cu);
13484 }
13485
13486 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13487 counterpart may exist. */
13488 if (child_origin_die != child_die)
13489 {
13490 if (child_die->tag != child_origin_die->tag
13491 && !(child_die->tag == DW_TAG_inlined_subroutine
13492 && child_origin_die->tag == DW_TAG_subprogram))
13493 complaint (_("Child DIE %s and its abstract origin %s have "
13494 "different tags"),
13495 sect_offset_str (child_die->sect_off),
13496 sect_offset_str (child_origin_die->sect_off));
13497 if (child_origin_die->parent != origin_die)
13498 complaint (_("Child DIE %s and its abstract origin %s have "
13499 "different parents"),
13500 sect_offset_str (child_die->sect_off),
13501 sect_offset_str (child_origin_die->sect_off));
13502 else
13503 offsets.push_back (child_origin_die->sect_off);
13504 }
13505 }
13506 std::sort (offsets.begin (), offsets.end ());
13507 sect_offset *offsets_end = offsets.data () + offsets.size ();
13508 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13509 if (offsetp[-1] == *offsetp)
13510 complaint (_("Multiple children of DIE %s refer "
13511 "to DIE %s as their abstract origin"),
13512 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13513
13514 offsetp = offsets.data ();
13515 origin_child_die = origin_die->child;
13516 while (origin_child_die && origin_child_die->tag)
13517 {
13518 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13519 while (offsetp < offsets_end
13520 && *offsetp < origin_child_die->sect_off)
13521 offsetp++;
13522 if (offsetp >= offsets_end
13523 || *offsetp > origin_child_die->sect_off)
13524 {
13525 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13526 Check whether we're already processing ORIGIN_CHILD_DIE.
13527 This can happen with mutually referenced abstract_origins.
13528 PR 16581. */
13529 if (!origin_child_die->in_process)
13530 process_die (origin_child_die, origin_cu);
13531 }
13532 origin_child_die = origin_child_die->sibling;
13533 }
13534 origin_cu->list_in_scope = origin_previous_list_in_scope;
13535
13536 if (cu != origin_cu)
13537 compute_delayed_physnames (origin_cu);
13538 }
13539
13540 static void
13541 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13542 {
13543 struct objfile *objfile = cu->per_objfile->objfile;
13544 struct gdbarch *gdbarch = objfile->arch ();
13545 struct context_stack *newobj;
13546 CORE_ADDR lowpc;
13547 CORE_ADDR highpc;
13548 struct die_info *child_die;
13549 struct attribute *attr, *call_line, *call_file;
13550 const char *name;
13551 CORE_ADDR baseaddr;
13552 struct block *block;
13553 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13554 std::vector<struct symbol *> template_args;
13555 struct template_symbol *templ_func = NULL;
13556
13557 if (inlined_func)
13558 {
13559 /* If we do not have call site information, we can't show the
13560 caller of this inlined function. That's too confusing, so
13561 only use the scope for local variables. */
13562 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13563 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13564 if (call_line == NULL || call_file == NULL)
13565 {
13566 read_lexical_block_scope (die, cu);
13567 return;
13568 }
13569 }
13570
13571 baseaddr = objfile->text_section_offset ();
13572
13573 name = dwarf2_name (die, cu);
13574
13575 /* Ignore functions with missing or empty names. These are actually
13576 illegal according to the DWARF standard. */
13577 if (name == NULL)
13578 {
13579 complaint (_("missing name for subprogram DIE at %s"),
13580 sect_offset_str (die->sect_off));
13581 return;
13582 }
13583
13584 /* Ignore functions with missing or invalid low and high pc attributes. */
13585 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13586 <= PC_BOUNDS_INVALID)
13587 {
13588 attr = dwarf2_attr (die, DW_AT_external, cu);
13589 if (attr == nullptr || !attr->as_boolean ())
13590 complaint (_("cannot get low and high bounds "
13591 "for subprogram DIE at %s"),
13592 sect_offset_str (die->sect_off));
13593 return;
13594 }
13595
13596 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13597 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13598
13599 /* If we have any template arguments, then we must allocate a
13600 different sort of symbol. */
13601 for (child_die = die->child; child_die; child_die = child_die->sibling)
13602 {
13603 if (child_die->tag == DW_TAG_template_type_param
13604 || child_die->tag == DW_TAG_template_value_param)
13605 {
13606 templ_func = new (&objfile->objfile_obstack) template_symbol;
13607 templ_func->subclass = SYMBOL_TEMPLATE;
13608 break;
13609 }
13610 }
13611
13612 newobj = cu->get_builder ()->push_context (0, lowpc);
13613 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13614 (struct symbol *) templ_func);
13615
13616 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13617 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13618 cu->language);
13619
13620 /* If there is a location expression for DW_AT_frame_base, record
13621 it. */
13622 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13623 if (attr != nullptr)
13624 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13625
13626 /* If there is a location for the static link, record it. */
13627 newobj->static_link = NULL;
13628 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13629 if (attr != nullptr)
13630 {
13631 newobj->static_link
13632 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13633 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13634 cu->addr_type ());
13635 }
13636
13637 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13638
13639 if (die->child != NULL)
13640 {
13641 child_die = die->child;
13642 while (child_die && child_die->tag)
13643 {
13644 if (child_die->tag == DW_TAG_template_type_param
13645 || child_die->tag == DW_TAG_template_value_param)
13646 {
13647 struct symbol *arg = new_symbol (child_die, NULL, cu);
13648
13649 if (arg != NULL)
13650 template_args.push_back (arg);
13651 }
13652 else
13653 process_die (child_die, cu);
13654 child_die = child_die->sibling;
13655 }
13656 }
13657
13658 inherit_abstract_dies (die, cu);
13659
13660 /* If we have a DW_AT_specification, we might need to import using
13661 directives from the context of the specification DIE. See the
13662 comment in determine_prefix. */
13663 if (cu->language == language_cplus
13664 && dwarf2_attr (die, DW_AT_specification, cu))
13665 {
13666 struct dwarf2_cu *spec_cu = cu;
13667 struct die_info *spec_die = die_specification (die, &spec_cu);
13668
13669 while (spec_die)
13670 {
13671 child_die = spec_die->child;
13672 while (child_die && child_die->tag)
13673 {
13674 if (child_die->tag == DW_TAG_imported_module)
13675 process_die (child_die, spec_cu);
13676 child_die = child_die->sibling;
13677 }
13678
13679 /* In some cases, GCC generates specification DIEs that
13680 themselves contain DW_AT_specification attributes. */
13681 spec_die = die_specification (spec_die, &spec_cu);
13682 }
13683 }
13684
13685 struct context_stack cstk = cu->get_builder ()->pop_context ();
13686 /* Make a block for the local symbols within. */
13687 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13688 cstk.static_link, lowpc, highpc);
13689
13690 /* For C++, set the block's scope. */
13691 if ((cu->language == language_cplus
13692 || cu->language == language_fortran
13693 || cu->language == language_d
13694 || cu->language == language_rust)
13695 && cu->processing_has_namespace_info)
13696 block_set_scope (block, determine_prefix (die, cu),
13697 &objfile->objfile_obstack);
13698
13699 /* If we have address ranges, record them. */
13700 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13701
13702 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13703
13704 /* Attach template arguments to function. */
13705 if (!template_args.empty ())
13706 {
13707 gdb_assert (templ_func != NULL);
13708
13709 templ_func->n_template_arguments = template_args.size ();
13710 templ_func->template_arguments
13711 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13712 templ_func->n_template_arguments);
13713 memcpy (templ_func->template_arguments,
13714 template_args.data (),
13715 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13716
13717 /* Make sure that the symtab is set on the new symbols. Even
13718 though they don't appear in this symtab directly, other parts
13719 of gdb assume that symbols do, and this is reasonably
13720 true. */
13721 for (symbol *sym : template_args)
13722 symbol_set_symtab (sym, symbol_symtab (templ_func));
13723 }
13724
13725 /* In C++, we can have functions nested inside functions (e.g., when
13726 a function declares a class that has methods). This means that
13727 when we finish processing a function scope, we may need to go
13728 back to building a containing block's symbol lists. */
13729 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13730 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13731
13732 /* If we've finished processing a top-level function, subsequent
13733 symbols go in the file symbol list. */
13734 if (cu->get_builder ()->outermost_context_p ())
13735 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13736 }
13737
13738 /* Process all the DIES contained within a lexical block scope. Start
13739 a new scope, process the dies, and then close the scope. */
13740
13741 static void
13742 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13743 {
13744 struct objfile *objfile = cu->per_objfile->objfile;
13745 struct gdbarch *gdbarch = objfile->arch ();
13746 CORE_ADDR lowpc, highpc;
13747 struct die_info *child_die;
13748 CORE_ADDR baseaddr;
13749
13750 baseaddr = objfile->text_section_offset ();
13751
13752 /* Ignore blocks with missing or invalid low and high pc attributes. */
13753 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13754 as multiple lexical blocks? Handling children in a sane way would
13755 be nasty. Might be easier to properly extend generic blocks to
13756 describe ranges. */
13757 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13758 {
13759 case PC_BOUNDS_NOT_PRESENT:
13760 /* DW_TAG_lexical_block has no attributes, process its children as if
13761 there was no wrapping by that DW_TAG_lexical_block.
13762 GCC does no longer produces such DWARF since GCC r224161. */
13763 for (child_die = die->child;
13764 child_die != NULL && child_die->tag;
13765 child_die = child_die->sibling)
13766 {
13767 /* We might already be processing this DIE. This can happen
13768 in an unusual circumstance -- where a subroutine A
13769 appears lexically in another subroutine B, but A actually
13770 inlines B. The recursion is broken here, rather than in
13771 inherit_abstract_dies, because it seems better to simply
13772 drop concrete children here. */
13773 if (!child_die->in_process)
13774 process_die (child_die, cu);
13775 }
13776 return;
13777 case PC_BOUNDS_INVALID:
13778 return;
13779 }
13780 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13781 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13782
13783 cu->get_builder ()->push_context (0, lowpc);
13784 if (die->child != NULL)
13785 {
13786 child_die = die->child;
13787 while (child_die && child_die->tag)
13788 {
13789 process_die (child_die, cu);
13790 child_die = child_die->sibling;
13791 }
13792 }
13793 inherit_abstract_dies (die, cu);
13794 struct context_stack cstk = cu->get_builder ()->pop_context ();
13795
13796 if (*cu->get_builder ()->get_local_symbols () != NULL
13797 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13798 {
13799 struct block *block
13800 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13801 cstk.start_addr, highpc);
13802
13803 /* Note that recording ranges after traversing children, as we
13804 do here, means that recording a parent's ranges entails
13805 walking across all its children's ranges as they appear in
13806 the address map, which is quadratic behavior.
13807
13808 It would be nicer to record the parent's ranges before
13809 traversing its children, simply overriding whatever you find
13810 there. But since we don't even decide whether to create a
13811 block until after we've traversed its children, that's hard
13812 to do. */
13813 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13814 }
13815 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13816 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13817 }
13818
13819 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13820
13821 static void
13822 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13823 {
13824 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13825 struct objfile *objfile = per_objfile->objfile;
13826 struct gdbarch *gdbarch = objfile->arch ();
13827 CORE_ADDR pc, baseaddr;
13828 struct attribute *attr;
13829 struct call_site *call_site, call_site_local;
13830 void **slot;
13831 int nparams;
13832 struct die_info *child_die;
13833
13834 baseaddr = objfile->text_section_offset ();
13835
13836 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13837 if (attr == NULL)
13838 {
13839 /* This was a pre-DWARF-5 GNU extension alias
13840 for DW_AT_call_return_pc. */
13841 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13842 }
13843 if (!attr)
13844 {
13845 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13846 "DIE %s [in module %s]"),
13847 sect_offset_str (die->sect_off), objfile_name (objfile));
13848 return;
13849 }
13850 pc = attr->as_address () + baseaddr;
13851 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13852
13853 if (cu->call_site_htab == NULL)
13854 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13855 NULL, &objfile->objfile_obstack,
13856 hashtab_obstack_allocate, NULL);
13857 call_site_local.pc = pc;
13858 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13859 if (*slot != NULL)
13860 {
13861 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13862 "DIE %s [in module %s]"),
13863 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13864 objfile_name (objfile));
13865 return;
13866 }
13867
13868 /* Count parameters at the caller. */
13869
13870 nparams = 0;
13871 for (child_die = die->child; child_die && child_die->tag;
13872 child_die = child_die->sibling)
13873 {
13874 if (child_die->tag != DW_TAG_call_site_parameter
13875 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13876 {
13877 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13878 "DW_TAG_call_site child DIE %s [in module %s]"),
13879 child_die->tag, sect_offset_str (child_die->sect_off),
13880 objfile_name (objfile));
13881 continue;
13882 }
13883
13884 nparams++;
13885 }
13886
13887 call_site
13888 = ((struct call_site *)
13889 obstack_alloc (&objfile->objfile_obstack,
13890 sizeof (*call_site)
13891 + (sizeof (*call_site->parameter) * (nparams - 1))));
13892 *slot = call_site;
13893 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13894 call_site->pc = pc;
13895
13896 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13897 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13898 {
13899 struct die_info *func_die;
13900
13901 /* Skip also over DW_TAG_inlined_subroutine. */
13902 for (func_die = die->parent;
13903 func_die && func_die->tag != DW_TAG_subprogram
13904 && func_die->tag != DW_TAG_subroutine_type;
13905 func_die = func_die->parent);
13906
13907 /* DW_AT_call_all_calls is a superset
13908 of DW_AT_call_all_tail_calls. */
13909 if (func_die
13910 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13911 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13912 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13913 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13914 {
13915 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13916 not complete. But keep CALL_SITE for look ups via call_site_htab,
13917 both the initial caller containing the real return address PC and
13918 the final callee containing the current PC of a chain of tail
13919 calls do not need to have the tail call list complete. But any
13920 function candidate for a virtual tail call frame searched via
13921 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13922 determined unambiguously. */
13923 }
13924 else
13925 {
13926 struct type *func_type = NULL;
13927
13928 if (func_die)
13929 func_type = get_die_type (func_die, cu);
13930 if (func_type != NULL)
13931 {
13932 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13933
13934 /* Enlist this call site to the function. */
13935 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13936 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13937 }
13938 else
13939 complaint (_("Cannot find function owning DW_TAG_call_site "
13940 "DIE %s [in module %s]"),
13941 sect_offset_str (die->sect_off), objfile_name (objfile));
13942 }
13943 }
13944
13945 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13946 if (attr == NULL)
13947 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13948 if (attr == NULL)
13949 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13950 if (attr == NULL)
13951 {
13952 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13953 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13954 }
13955 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13956 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
13957 /* Keep NULL DWARF_BLOCK. */;
13958 else if (attr->form_is_block ())
13959 {
13960 struct dwarf2_locexpr_baton *dlbaton;
13961 struct dwarf_block *block = attr->as_block ();
13962
13963 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13964 dlbaton->data = block->data;
13965 dlbaton->size = block->size;
13966 dlbaton->per_objfile = per_objfile;
13967 dlbaton->per_cu = cu->per_cu;
13968
13969 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13970 }
13971 else if (attr->form_is_ref ())
13972 {
13973 struct dwarf2_cu *target_cu = cu;
13974 struct die_info *target_die;
13975
13976 target_die = follow_die_ref (die, attr, &target_cu);
13977 gdb_assert (target_cu->per_objfile->objfile == objfile);
13978 if (die_is_declaration (target_die, target_cu))
13979 {
13980 const char *target_physname;
13981
13982 /* Prefer the mangled name; otherwise compute the demangled one. */
13983 target_physname = dw2_linkage_name (target_die, target_cu);
13984 if (target_physname == NULL)
13985 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13986 if (target_physname == NULL)
13987 complaint (_("DW_AT_call_target target DIE has invalid "
13988 "physname, for referencing DIE %s [in module %s]"),
13989 sect_offset_str (die->sect_off), objfile_name (objfile));
13990 else
13991 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13992 }
13993 else
13994 {
13995 CORE_ADDR lowpc;
13996
13997 /* DW_AT_entry_pc should be preferred. */
13998 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13999 <= PC_BOUNDS_INVALID)
14000 complaint (_("DW_AT_call_target target DIE has invalid "
14001 "low pc, for referencing DIE %s [in module %s]"),
14002 sect_offset_str (die->sect_off), objfile_name (objfile));
14003 else
14004 {
14005 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14006 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14007 }
14008 }
14009 }
14010 else
14011 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14012 "block nor reference, for DIE %s [in module %s]"),
14013 sect_offset_str (die->sect_off), objfile_name (objfile));
14014
14015 call_site->per_cu = cu->per_cu;
14016 call_site->per_objfile = per_objfile;
14017
14018 for (child_die = die->child;
14019 child_die && child_die->tag;
14020 child_die = child_die->sibling)
14021 {
14022 struct call_site_parameter *parameter;
14023 struct attribute *loc, *origin;
14024
14025 if (child_die->tag != DW_TAG_call_site_parameter
14026 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14027 {
14028 /* Already printed the complaint above. */
14029 continue;
14030 }
14031
14032 gdb_assert (call_site->parameter_count < nparams);
14033 parameter = &call_site->parameter[call_site->parameter_count];
14034
14035 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14036 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14037 register is contained in DW_AT_call_value. */
14038
14039 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14040 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14041 if (origin == NULL)
14042 {
14043 /* This was a pre-DWARF-5 GNU extension alias
14044 for DW_AT_call_parameter. */
14045 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14046 }
14047 if (loc == NULL && origin != NULL && origin->form_is_ref ())
14048 {
14049 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14050
14051 sect_offset sect_off = origin->get_ref_die_offset ();
14052 if (!cu->header.offset_in_cu_p (sect_off))
14053 {
14054 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14055 binding can be done only inside one CU. Such referenced DIE
14056 therefore cannot be even moved to DW_TAG_partial_unit. */
14057 complaint (_("DW_AT_call_parameter offset is not in CU for "
14058 "DW_TAG_call_site child DIE %s [in module %s]"),
14059 sect_offset_str (child_die->sect_off),
14060 objfile_name (objfile));
14061 continue;
14062 }
14063 parameter->u.param_cu_off
14064 = (cu_offset) (sect_off - cu->header.sect_off);
14065 }
14066 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
14067 {
14068 complaint (_("No DW_FORM_block* DW_AT_location for "
14069 "DW_TAG_call_site child DIE %s [in module %s]"),
14070 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14071 continue;
14072 }
14073 else
14074 {
14075 struct dwarf_block *block = loc->as_block ();
14076
14077 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14078 (block->data, &block->data[block->size]);
14079 if (parameter->u.dwarf_reg != -1)
14080 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14081 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
14082 &block->data[block->size],
14083 &parameter->u.fb_offset))
14084 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14085 else
14086 {
14087 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14088 "for DW_FORM_block* DW_AT_location is supported for "
14089 "DW_TAG_call_site child DIE %s "
14090 "[in module %s]"),
14091 sect_offset_str (child_die->sect_off),
14092 objfile_name (objfile));
14093 continue;
14094 }
14095 }
14096
14097 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14098 if (attr == NULL)
14099 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14100 if (attr == NULL || !attr->form_is_block ())
14101 {
14102 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14103 "DW_TAG_call_site child DIE %s [in module %s]"),
14104 sect_offset_str (child_die->sect_off),
14105 objfile_name (objfile));
14106 continue;
14107 }
14108
14109 struct dwarf_block *block = attr->as_block ();
14110 parameter->value = block->data;
14111 parameter->value_size = block->size;
14112
14113 /* Parameters are not pre-cleared by memset above. */
14114 parameter->data_value = NULL;
14115 parameter->data_value_size = 0;
14116 call_site->parameter_count++;
14117
14118 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14119 if (attr == NULL)
14120 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14121 if (attr != nullptr)
14122 {
14123 if (!attr->form_is_block ())
14124 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14125 "DW_TAG_call_site child DIE %s [in module %s]"),
14126 sect_offset_str (child_die->sect_off),
14127 objfile_name (objfile));
14128 else
14129 {
14130 block = attr->as_block ();
14131 parameter->data_value = block->data;
14132 parameter->data_value_size = block->size;
14133 }
14134 }
14135 }
14136 }
14137
14138 /* Helper function for read_variable. If DIE represents a virtual
14139 table, then return the type of the concrete object that is
14140 associated with the virtual table. Otherwise, return NULL. */
14141
14142 static struct type *
14143 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14144 {
14145 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14146 if (attr == NULL)
14147 return NULL;
14148
14149 /* Find the type DIE. */
14150 struct die_info *type_die = NULL;
14151 struct dwarf2_cu *type_cu = cu;
14152
14153 if (attr->form_is_ref ())
14154 type_die = follow_die_ref (die, attr, &type_cu);
14155 if (type_die == NULL)
14156 return NULL;
14157
14158 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14159 return NULL;
14160 return die_containing_type (type_die, type_cu);
14161 }
14162
14163 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14164
14165 static void
14166 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14167 {
14168 struct rust_vtable_symbol *storage = NULL;
14169
14170 if (cu->language == language_rust)
14171 {
14172 struct type *containing_type = rust_containing_type (die, cu);
14173
14174 if (containing_type != NULL)
14175 {
14176 struct objfile *objfile = cu->per_objfile->objfile;
14177
14178 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
14179 storage->concrete_type = containing_type;
14180 storage->subclass = SYMBOL_RUST_VTABLE;
14181 }
14182 }
14183
14184 struct symbol *res = new_symbol (die, NULL, cu, storage);
14185 struct attribute *abstract_origin
14186 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14187 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14188 if (res == NULL && loc && abstract_origin)
14189 {
14190 /* We have a variable without a name, but with a location and an abstract
14191 origin. This may be a concrete instance of an abstract variable
14192 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14193 later. */
14194 struct dwarf2_cu *origin_cu = cu;
14195 struct die_info *origin_die
14196 = follow_die_ref (die, abstract_origin, &origin_cu);
14197 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14198 per_objfile->per_bfd->abstract_to_concrete
14199 [origin_die->sect_off].push_back (die->sect_off);
14200 }
14201 }
14202
14203 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14204 reading .debug_rnglists.
14205 Callback's type should be:
14206 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14207 Return true if the attributes are present and valid, otherwise,
14208 return false. */
14209
14210 template <typename Callback>
14211 static bool
14212 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14213 dwarf_tag tag, Callback &&callback)
14214 {
14215 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14216 struct objfile *objfile = per_objfile->objfile;
14217 bfd *obfd = objfile->obfd;
14218 /* Base address selection entry. */
14219 gdb::optional<CORE_ADDR> base;
14220 const gdb_byte *buffer;
14221 CORE_ADDR baseaddr;
14222 bool overflow = false;
14223 ULONGEST addr_index;
14224 struct dwarf2_section_info *rnglists_section;
14225
14226 base = cu->base_address;
14227 rnglists_section = cu_debug_rnglists_section (cu, tag);
14228 rnglists_section->read (objfile);
14229
14230 if (offset >= rnglists_section->size)
14231 {
14232 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14233 offset);
14234 return false;
14235 }
14236 buffer = rnglists_section->buffer + offset;
14237
14238 baseaddr = objfile->text_section_offset ();
14239
14240 while (1)
14241 {
14242 /* Initialize it due to a false compiler warning. */
14243 CORE_ADDR range_beginning = 0, range_end = 0;
14244 const gdb_byte *buf_end = (rnglists_section->buffer
14245 + rnglists_section->size);
14246 unsigned int bytes_read;
14247
14248 if (buffer == buf_end)
14249 {
14250 overflow = true;
14251 break;
14252 }
14253 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14254 switch (rlet)
14255 {
14256 case DW_RLE_end_of_list:
14257 break;
14258 case DW_RLE_base_address:
14259 if (buffer + cu->header.addr_size > buf_end)
14260 {
14261 overflow = true;
14262 break;
14263 }
14264 base = cu->header.read_address (obfd, buffer, &bytes_read);
14265 buffer += bytes_read;
14266 break;
14267 case DW_RLE_base_addressx:
14268 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14269 buffer += bytes_read;
14270 base = read_addr_index (cu, addr_index);
14271 break;
14272 case DW_RLE_start_length:
14273 if (buffer + cu->header.addr_size > buf_end)
14274 {
14275 overflow = true;
14276 break;
14277 }
14278 range_beginning = cu->header.read_address (obfd, buffer,
14279 &bytes_read);
14280 buffer += bytes_read;
14281 range_end = (range_beginning
14282 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14283 buffer += bytes_read;
14284 if (buffer > buf_end)
14285 {
14286 overflow = true;
14287 break;
14288 }
14289 break;
14290 case DW_RLE_startx_length:
14291 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14292 buffer += bytes_read;
14293 range_beginning = read_addr_index (cu, addr_index);
14294 if (buffer > buf_end)
14295 {
14296 overflow = true;
14297 break;
14298 }
14299 range_end = (range_beginning
14300 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14301 buffer += bytes_read;
14302 break;
14303 case DW_RLE_offset_pair:
14304 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14305 buffer += bytes_read;
14306 if (buffer > buf_end)
14307 {
14308 overflow = true;
14309 break;
14310 }
14311 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14312 buffer += bytes_read;
14313 if (buffer > buf_end)
14314 {
14315 overflow = true;
14316 break;
14317 }
14318 break;
14319 case DW_RLE_start_end:
14320 if (buffer + 2 * cu->header.addr_size > buf_end)
14321 {
14322 overflow = true;
14323 break;
14324 }
14325 range_beginning = cu->header.read_address (obfd, buffer,
14326 &bytes_read);
14327 buffer += bytes_read;
14328 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
14329 buffer += bytes_read;
14330 break;
14331 case DW_RLE_startx_endx:
14332 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14333 buffer += bytes_read;
14334 range_beginning = read_addr_index (cu, addr_index);
14335 if (buffer > buf_end)
14336 {
14337 overflow = true;
14338 break;
14339 }
14340 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14341 buffer += bytes_read;
14342 range_end = read_addr_index (cu, addr_index);
14343 break;
14344 default:
14345 complaint (_("Invalid .debug_rnglists data (no base address)"));
14346 return false;
14347 }
14348 if (rlet == DW_RLE_end_of_list || overflow)
14349 break;
14350 if (rlet == DW_RLE_base_address)
14351 continue;
14352
14353 if (range_beginning > range_end)
14354 {
14355 /* Inverted range entries are invalid. */
14356 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14357 return false;
14358 }
14359
14360 /* Empty range entries have no effect. */
14361 if (range_beginning == range_end)
14362 continue;
14363
14364 /* Only DW_RLE_offset_pair needs the base address added. */
14365 if (rlet == DW_RLE_offset_pair)
14366 {
14367 if (!base.has_value ())
14368 {
14369 /* We have no valid base address for the DW_RLE_offset_pair. */
14370 complaint (_("Invalid .debug_rnglists data (no base address for "
14371 "DW_RLE_offset_pair)"));
14372 return false;
14373 }
14374
14375 range_beginning += *base;
14376 range_end += *base;
14377 }
14378
14379 /* A not-uncommon case of bad debug info.
14380 Don't pollute the addrmap with bad data. */
14381 if (range_beginning + baseaddr == 0
14382 && !per_objfile->per_bfd->has_section_at_zero)
14383 {
14384 complaint (_(".debug_rnglists entry has start address of zero"
14385 " [in module %s]"), objfile_name (objfile));
14386 continue;
14387 }
14388
14389 callback (range_beginning, range_end);
14390 }
14391
14392 if (overflow)
14393 {
14394 complaint (_("Offset %d is not terminated "
14395 "for DW_AT_ranges attribute"),
14396 offset);
14397 return false;
14398 }
14399
14400 return true;
14401 }
14402
14403 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14404 Callback's type should be:
14405 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14406 Return 1 if the attributes are present and valid, otherwise, return 0. */
14407
14408 template <typename Callback>
14409 static int
14410 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
14411 Callback &&callback)
14412 {
14413 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14414 struct objfile *objfile = per_objfile->objfile;
14415 struct comp_unit_head *cu_header = &cu->header;
14416 bfd *obfd = objfile->obfd;
14417 unsigned int addr_size = cu_header->addr_size;
14418 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14419 /* Base address selection entry. */
14420 gdb::optional<CORE_ADDR> base;
14421 unsigned int dummy;
14422 const gdb_byte *buffer;
14423 CORE_ADDR baseaddr;
14424
14425 if (cu_header->version >= 5)
14426 return dwarf2_rnglists_process (offset, cu, tag, callback);
14427
14428 base = cu->base_address;
14429
14430 per_objfile->per_bfd->ranges.read (objfile);
14431 if (offset >= per_objfile->per_bfd->ranges.size)
14432 {
14433 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14434 offset);
14435 return 0;
14436 }
14437 buffer = per_objfile->per_bfd->ranges.buffer + offset;
14438
14439 baseaddr = objfile->text_section_offset ();
14440
14441 while (1)
14442 {
14443 CORE_ADDR range_beginning, range_end;
14444
14445 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
14446 buffer += addr_size;
14447 range_end = cu->header.read_address (obfd, buffer, &dummy);
14448 buffer += addr_size;
14449 offset += 2 * addr_size;
14450
14451 /* An end of list marker is a pair of zero addresses. */
14452 if (range_beginning == 0 && range_end == 0)
14453 /* Found the end of list entry. */
14454 break;
14455
14456 /* Each base address selection entry is a pair of 2 values.
14457 The first is the largest possible address, the second is
14458 the base address. Check for a base address here. */
14459 if ((range_beginning & mask) == mask)
14460 {
14461 /* If we found the largest possible address, then we already
14462 have the base address in range_end. */
14463 base = range_end;
14464 continue;
14465 }
14466
14467 if (!base.has_value ())
14468 {
14469 /* We have no valid base address for the ranges
14470 data. */
14471 complaint (_("Invalid .debug_ranges data (no base address)"));
14472 return 0;
14473 }
14474
14475 if (range_beginning > range_end)
14476 {
14477 /* Inverted range entries are invalid. */
14478 complaint (_("Invalid .debug_ranges data (inverted range)"));
14479 return 0;
14480 }
14481
14482 /* Empty range entries have no effect. */
14483 if (range_beginning == range_end)
14484 continue;
14485
14486 range_beginning += *base;
14487 range_end += *base;
14488
14489 /* A not-uncommon case of bad debug info.
14490 Don't pollute the addrmap with bad data. */
14491 if (range_beginning + baseaddr == 0
14492 && !per_objfile->per_bfd->has_section_at_zero)
14493 {
14494 complaint (_(".debug_ranges entry has start address of zero"
14495 " [in module %s]"), objfile_name (objfile));
14496 continue;
14497 }
14498
14499 callback (range_beginning, range_end);
14500 }
14501
14502 return 1;
14503 }
14504
14505 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14506 Return 1 if the attributes are present and valid, otherwise, return 0.
14507 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14508
14509 static int
14510 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14511 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14512 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
14513 {
14514 struct objfile *objfile = cu->per_objfile->objfile;
14515 struct gdbarch *gdbarch = objfile->arch ();
14516 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14517 int low_set = 0;
14518 CORE_ADDR low = 0;
14519 CORE_ADDR high = 0;
14520 int retval;
14521
14522 retval = dwarf2_ranges_process (offset, cu, tag,
14523 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14524 {
14525 if (ranges_pst != NULL)
14526 {
14527 CORE_ADDR lowpc;
14528 CORE_ADDR highpc;
14529
14530 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14531 range_beginning + baseaddr)
14532 - baseaddr);
14533 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14534 range_end + baseaddr)
14535 - baseaddr);
14536 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14537 lowpc, highpc - 1, ranges_pst);
14538 }
14539
14540 /* FIXME: This is recording everything as a low-high
14541 segment of consecutive addresses. We should have a
14542 data structure for discontiguous block ranges
14543 instead. */
14544 if (! low_set)
14545 {
14546 low = range_beginning;
14547 high = range_end;
14548 low_set = 1;
14549 }
14550 else
14551 {
14552 if (range_beginning < low)
14553 low = range_beginning;
14554 if (range_end > high)
14555 high = range_end;
14556 }
14557 });
14558 if (!retval)
14559 return 0;
14560
14561 if (! low_set)
14562 /* If the first entry is an end-of-list marker, the range
14563 describes an empty scope, i.e. no instructions. */
14564 return 0;
14565
14566 if (low_return)
14567 *low_return = low;
14568 if (high_return)
14569 *high_return = high;
14570 return 1;
14571 }
14572
14573 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14574 definition for the return value. *LOWPC and *HIGHPC are set iff
14575 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14576
14577 static enum pc_bounds_kind
14578 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14579 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14580 dwarf2_psymtab *pst)
14581 {
14582 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14583 struct attribute *attr;
14584 struct attribute *attr_high;
14585 CORE_ADDR low = 0;
14586 CORE_ADDR high = 0;
14587 enum pc_bounds_kind ret;
14588
14589 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14590 if (attr_high)
14591 {
14592 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14593 if (attr != nullptr)
14594 {
14595 low = attr->as_address ();
14596 high = attr_high->as_address ();
14597 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14598 high += low;
14599 }
14600 else
14601 /* Found high w/o low attribute. */
14602 return PC_BOUNDS_INVALID;
14603
14604 /* Found consecutive range of addresses. */
14605 ret = PC_BOUNDS_HIGH_LOW;
14606 }
14607 else
14608 {
14609 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14610 if (attr != nullptr && attr->form_is_unsigned ())
14611 {
14612 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14613 on DWARF version). */
14614 ULONGEST ranges_offset = attr->as_unsigned ();
14615
14616 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14617 this value. */
14618 if (die->tag != DW_TAG_compile_unit)
14619 ranges_offset += cu->gnu_ranges_base;
14620
14621 /* Value of the DW_AT_ranges attribute is the offset in the
14622 .debug_ranges section. */
14623 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14624 die->tag))
14625 return PC_BOUNDS_INVALID;
14626 /* Found discontinuous range of addresses. */
14627 ret = PC_BOUNDS_RANGES;
14628 }
14629 else
14630 return PC_BOUNDS_NOT_PRESENT;
14631 }
14632
14633 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14634 if (high <= low)
14635 return PC_BOUNDS_INVALID;
14636
14637 /* When using the GNU linker, .gnu.linkonce. sections are used to
14638 eliminate duplicate copies of functions and vtables and such.
14639 The linker will arbitrarily choose one and discard the others.
14640 The AT_*_pc values for such functions refer to local labels in
14641 these sections. If the section from that file was discarded, the
14642 labels are not in the output, so the relocs get a value of 0.
14643 If this is a discarded function, mark the pc bounds as invalid,
14644 so that GDB will ignore it. */
14645 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14646 return PC_BOUNDS_INVALID;
14647
14648 *lowpc = low;
14649 if (highpc)
14650 *highpc = high;
14651 return ret;
14652 }
14653
14654 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14655 its low and high PC addresses. Do nothing if these addresses could not
14656 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14657 and HIGHPC to the high address if greater than HIGHPC. */
14658
14659 static void
14660 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14661 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14662 struct dwarf2_cu *cu)
14663 {
14664 CORE_ADDR low, high;
14665 struct die_info *child = die->child;
14666
14667 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14668 {
14669 *lowpc = std::min (*lowpc, low);
14670 *highpc = std::max (*highpc, high);
14671 }
14672
14673 /* If the language does not allow nested subprograms (either inside
14674 subprograms or lexical blocks), we're done. */
14675 if (cu->language != language_ada)
14676 return;
14677
14678 /* Check all the children of the given DIE. If it contains nested
14679 subprograms, then check their pc bounds. Likewise, we need to
14680 check lexical blocks as well, as they may also contain subprogram
14681 definitions. */
14682 while (child && child->tag)
14683 {
14684 if (child->tag == DW_TAG_subprogram
14685 || child->tag == DW_TAG_lexical_block)
14686 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14687 child = child->sibling;
14688 }
14689 }
14690
14691 /* Get the low and high pc's represented by the scope DIE, and store
14692 them in *LOWPC and *HIGHPC. If the correct values can't be
14693 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14694
14695 static void
14696 get_scope_pc_bounds (struct die_info *die,
14697 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14698 struct dwarf2_cu *cu)
14699 {
14700 CORE_ADDR best_low = (CORE_ADDR) -1;
14701 CORE_ADDR best_high = (CORE_ADDR) 0;
14702 CORE_ADDR current_low, current_high;
14703
14704 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14705 >= PC_BOUNDS_RANGES)
14706 {
14707 best_low = current_low;
14708 best_high = current_high;
14709 }
14710 else
14711 {
14712 struct die_info *child = die->child;
14713
14714 while (child && child->tag)
14715 {
14716 switch (child->tag) {
14717 case DW_TAG_subprogram:
14718 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14719 break;
14720 case DW_TAG_namespace:
14721 case DW_TAG_module:
14722 /* FIXME: carlton/2004-01-16: Should we do this for
14723 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14724 that current GCC's always emit the DIEs corresponding
14725 to definitions of methods of classes as children of a
14726 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14727 the DIEs giving the declarations, which could be
14728 anywhere). But I don't see any reason why the
14729 standards says that they have to be there. */
14730 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14731
14732 if (current_low != ((CORE_ADDR) -1))
14733 {
14734 best_low = std::min (best_low, current_low);
14735 best_high = std::max (best_high, current_high);
14736 }
14737 break;
14738 default:
14739 /* Ignore. */
14740 break;
14741 }
14742
14743 child = child->sibling;
14744 }
14745 }
14746
14747 *lowpc = best_low;
14748 *highpc = best_high;
14749 }
14750
14751 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14752 in DIE. */
14753
14754 static void
14755 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14756 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14757 {
14758 struct objfile *objfile = cu->per_objfile->objfile;
14759 struct gdbarch *gdbarch = objfile->arch ();
14760 struct attribute *attr;
14761 struct attribute *attr_high;
14762
14763 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14764 if (attr_high)
14765 {
14766 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14767 if (attr != nullptr)
14768 {
14769 CORE_ADDR low = attr->as_address ();
14770 CORE_ADDR high = attr_high->as_address ();
14771
14772 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14773 high += low;
14774
14775 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14776 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14777 cu->get_builder ()->record_block_range (block, low, high - 1);
14778 }
14779 }
14780
14781 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14782 if (attr != nullptr && attr->form_is_unsigned ())
14783 {
14784 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14785 on DWARF version). */
14786 ULONGEST ranges_offset = attr->as_unsigned ();
14787
14788 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14789 this value. */
14790 if (die->tag != DW_TAG_compile_unit)
14791 ranges_offset += cu->gnu_ranges_base;
14792
14793 std::vector<blockrange> blockvec;
14794 dwarf2_ranges_process (ranges_offset, cu, die->tag,
14795 [&] (CORE_ADDR start, CORE_ADDR end)
14796 {
14797 start += baseaddr;
14798 end += baseaddr;
14799 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14800 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14801 cu->get_builder ()->record_block_range (block, start, end - 1);
14802 blockvec.emplace_back (start, end);
14803 });
14804
14805 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14806 }
14807 }
14808
14809 /* Check whether the producer field indicates either of GCC < 4.6, or the
14810 Intel C/C++ compiler, and cache the result in CU. */
14811
14812 static void
14813 check_producer (struct dwarf2_cu *cu)
14814 {
14815 int major, minor;
14816
14817 if (cu->producer == NULL)
14818 {
14819 /* For unknown compilers expect their behavior is DWARF version
14820 compliant.
14821
14822 GCC started to support .debug_types sections by -gdwarf-4 since
14823 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14824 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14825 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14826 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14827 }
14828 else if (producer_is_gcc (cu->producer, &major, &minor))
14829 {
14830 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14831 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14832 }
14833 else if (producer_is_icc (cu->producer, &major, &minor))
14834 {
14835 cu->producer_is_icc = true;
14836 cu->producer_is_icc_lt_14 = major < 14;
14837 }
14838 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14839 cu->producer_is_codewarrior = true;
14840 else
14841 {
14842 /* For other non-GCC compilers, expect their behavior is DWARF version
14843 compliant. */
14844 }
14845
14846 cu->checked_producer = true;
14847 }
14848
14849 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14850 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14851 during 4.6.0 experimental. */
14852
14853 static bool
14854 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14855 {
14856 if (!cu->checked_producer)
14857 check_producer (cu);
14858
14859 return cu->producer_is_gxx_lt_4_6;
14860 }
14861
14862
14863 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14864 with incorrect is_stmt attributes. */
14865
14866 static bool
14867 producer_is_codewarrior (struct dwarf2_cu *cu)
14868 {
14869 if (!cu->checked_producer)
14870 check_producer (cu);
14871
14872 return cu->producer_is_codewarrior;
14873 }
14874
14875 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
14876 If that attribute is not available, return the appropriate
14877 default. */
14878
14879 static enum dwarf_access_attribute
14880 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14881 {
14882 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14883 if (attr != nullptr)
14884 {
14885 LONGEST value = attr->constant_value (-1);
14886 if (value == DW_ACCESS_public
14887 || value == DW_ACCESS_protected
14888 || value == DW_ACCESS_private)
14889 return (dwarf_access_attribute) value;
14890 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14891 plongest (value));
14892 }
14893
14894 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14895 {
14896 /* The default DWARF 2 accessibility for members is public, the default
14897 accessibility for inheritance is private. */
14898
14899 if (die->tag != DW_TAG_inheritance)
14900 return DW_ACCESS_public;
14901 else
14902 return DW_ACCESS_private;
14903 }
14904 else
14905 {
14906 /* DWARF 3+ defines the default accessibility a different way. The same
14907 rules apply now for DW_TAG_inheritance as for the members and it only
14908 depends on the container kind. */
14909
14910 if (die->parent->tag == DW_TAG_class_type)
14911 return DW_ACCESS_private;
14912 else
14913 return DW_ACCESS_public;
14914 }
14915 }
14916
14917 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14918 offset. If the attribute was not found return 0, otherwise return
14919 1. If it was found but could not properly be handled, set *OFFSET
14920 to 0. */
14921
14922 static int
14923 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14924 LONGEST *offset)
14925 {
14926 struct attribute *attr;
14927
14928 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14929 if (attr != NULL)
14930 {
14931 *offset = 0;
14932
14933 /* Note that we do not check for a section offset first here.
14934 This is because DW_AT_data_member_location is new in DWARF 4,
14935 so if we see it, we can assume that a constant form is really
14936 a constant and not a section offset. */
14937 if (attr->form_is_constant ())
14938 *offset = attr->constant_value (0);
14939 else if (attr->form_is_section_offset ())
14940 dwarf2_complex_location_expr_complaint ();
14941 else if (attr->form_is_block ())
14942 *offset = decode_locdesc (attr->as_block (), cu);
14943 else
14944 dwarf2_complex_location_expr_complaint ();
14945
14946 return 1;
14947 }
14948
14949 return 0;
14950 }
14951
14952 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14953
14954 static void
14955 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14956 struct field *field)
14957 {
14958 struct attribute *attr;
14959
14960 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14961 if (attr != NULL)
14962 {
14963 if (attr->form_is_constant ())
14964 {
14965 LONGEST offset = attr->constant_value (0);
14966 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14967 }
14968 else if (attr->form_is_section_offset ())
14969 dwarf2_complex_location_expr_complaint ();
14970 else if (attr->form_is_block ())
14971 {
14972 bool handled;
14973 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
14974 if (handled)
14975 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14976 else
14977 {
14978 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14979 struct objfile *objfile = per_objfile->objfile;
14980 struct dwarf2_locexpr_baton *dlbaton
14981 = XOBNEW (&objfile->objfile_obstack,
14982 struct dwarf2_locexpr_baton);
14983 dlbaton->data = attr->as_block ()->data;
14984 dlbaton->size = attr->as_block ()->size;
14985 /* When using this baton, we want to compute the address
14986 of the field, not the value. This is why
14987 is_reference is set to false here. */
14988 dlbaton->is_reference = false;
14989 dlbaton->per_objfile = per_objfile;
14990 dlbaton->per_cu = cu->per_cu;
14991
14992 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14993 }
14994 }
14995 else
14996 dwarf2_complex_location_expr_complaint ();
14997 }
14998 }
14999
15000 /* Add an aggregate field to the field list. */
15001
15002 static void
15003 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15004 struct dwarf2_cu *cu)
15005 {
15006 struct objfile *objfile = cu->per_objfile->objfile;
15007 struct gdbarch *gdbarch = objfile->arch ();
15008 struct nextfield *new_field;
15009 struct attribute *attr;
15010 struct field *fp;
15011 const char *fieldname = "";
15012
15013 if (die->tag == DW_TAG_inheritance)
15014 {
15015 fip->baseclasses.emplace_back ();
15016 new_field = &fip->baseclasses.back ();
15017 }
15018 else
15019 {
15020 fip->fields.emplace_back ();
15021 new_field = &fip->fields.back ();
15022 }
15023
15024 new_field->offset = die->sect_off;
15025
15026 new_field->accessibility = dwarf2_access_attribute (die, cu);
15027 if (new_field->accessibility != DW_ACCESS_public)
15028 fip->non_public_fields = true;
15029
15030 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15031 if (attr != nullptr)
15032 new_field->virtuality = attr->as_virtuality ();
15033 else
15034 new_field->virtuality = DW_VIRTUALITY_none;
15035
15036 fp = &new_field->field;
15037
15038 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15039 {
15040 /* Data member other than a C++ static data member. */
15041
15042 /* Get type of field. */
15043 fp->set_type (die_type (die, cu));
15044
15045 SET_FIELD_BITPOS (*fp, 0);
15046
15047 /* Get bit size of field (zero if none). */
15048 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15049 if (attr != nullptr)
15050 {
15051 FIELD_BITSIZE (*fp) = attr->constant_value (0);
15052 }
15053 else
15054 {
15055 FIELD_BITSIZE (*fp) = 0;
15056 }
15057
15058 /* Get bit offset of field. */
15059 handle_data_member_location (die, cu, fp);
15060 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15061 if (attr != nullptr && attr->form_is_constant ())
15062 {
15063 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
15064 {
15065 /* For big endian bits, the DW_AT_bit_offset gives the
15066 additional bit offset from the MSB of the containing
15067 anonymous object to the MSB of the field. We don't
15068 have to do anything special since we don't need to
15069 know the size of the anonymous object. */
15070 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15071 + attr->constant_value (0)));
15072 }
15073 else
15074 {
15075 /* For little endian bits, compute the bit offset to the
15076 MSB of the anonymous object, subtract off the number of
15077 bits from the MSB of the field to the MSB of the
15078 object, and then subtract off the number of bits of
15079 the field itself. The result is the bit offset of
15080 the LSB of the field. */
15081 int anonymous_size;
15082 int bit_offset = attr->constant_value (0);
15083
15084 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15085 if (attr != nullptr && attr->form_is_constant ())
15086 {
15087 /* The size of the anonymous object containing
15088 the bit field is explicit, so use the
15089 indicated size (in bytes). */
15090 anonymous_size = attr->constant_value (0);
15091 }
15092 else
15093 {
15094 /* The size of the anonymous object containing
15095 the bit field must be inferred from the type
15096 attribute of the data member containing the
15097 bit field. */
15098 anonymous_size = TYPE_LENGTH (fp->type ());
15099 }
15100 SET_FIELD_BITPOS (*fp,
15101 (FIELD_BITPOS (*fp)
15102 + anonymous_size * bits_per_byte
15103 - bit_offset - FIELD_BITSIZE (*fp)));
15104 }
15105 }
15106 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15107 if (attr != NULL)
15108 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15109 + attr->constant_value (0)));
15110
15111 /* Get name of field. */
15112 fieldname = dwarf2_name (die, cu);
15113 if (fieldname == NULL)
15114 fieldname = "";
15115
15116 /* The name is already allocated along with this objfile, so we don't
15117 need to duplicate it for the type. */
15118 fp->name = fieldname;
15119
15120 /* Change accessibility for artificial fields (e.g. virtual table
15121 pointer or virtual base class pointer) to private. */
15122 if (dwarf2_attr (die, DW_AT_artificial, cu))
15123 {
15124 FIELD_ARTIFICIAL (*fp) = 1;
15125 new_field->accessibility = DW_ACCESS_private;
15126 fip->non_public_fields = true;
15127 }
15128 }
15129 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15130 {
15131 /* C++ static member. */
15132
15133 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15134 is a declaration, but all versions of G++ as of this writing
15135 (so through at least 3.2.1) incorrectly generate
15136 DW_TAG_variable tags. */
15137
15138 const char *physname;
15139
15140 /* Get name of field. */
15141 fieldname = dwarf2_name (die, cu);
15142 if (fieldname == NULL)
15143 return;
15144
15145 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15146 if (attr
15147 /* Only create a symbol if this is an external value.
15148 new_symbol checks this and puts the value in the global symbol
15149 table, which we want. If it is not external, new_symbol
15150 will try to put the value in cu->list_in_scope which is wrong. */
15151 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15152 {
15153 /* A static const member, not much different than an enum as far as
15154 we're concerned, except that we can support more types. */
15155 new_symbol (die, NULL, cu);
15156 }
15157
15158 /* Get physical name. */
15159 physname = dwarf2_physname (fieldname, die, cu);
15160
15161 /* The name is already allocated along with this objfile, so we don't
15162 need to duplicate it for the type. */
15163 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15164 fp->set_type (die_type (die, cu));
15165 FIELD_NAME (*fp) = fieldname;
15166 }
15167 else if (die->tag == DW_TAG_inheritance)
15168 {
15169 /* C++ base class field. */
15170 handle_data_member_location (die, cu, fp);
15171 FIELD_BITSIZE (*fp) = 0;
15172 fp->set_type (die_type (die, cu));
15173 FIELD_NAME (*fp) = fp->type ()->name ();
15174 }
15175 else
15176 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15177 }
15178
15179 /* Can the type given by DIE define another type? */
15180
15181 static bool
15182 type_can_define_types (const struct die_info *die)
15183 {
15184 switch (die->tag)
15185 {
15186 case DW_TAG_typedef:
15187 case DW_TAG_class_type:
15188 case DW_TAG_structure_type:
15189 case DW_TAG_union_type:
15190 case DW_TAG_enumeration_type:
15191 return true;
15192
15193 default:
15194 return false;
15195 }
15196 }
15197
15198 /* Add a type definition defined in the scope of the FIP's class. */
15199
15200 static void
15201 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15202 struct dwarf2_cu *cu)
15203 {
15204 struct decl_field fp;
15205 memset (&fp, 0, sizeof (fp));
15206
15207 gdb_assert (type_can_define_types (die));
15208
15209 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15210 fp.name = dwarf2_name (die, cu);
15211 fp.type = read_type_die (die, cu);
15212
15213 /* Save accessibility. */
15214 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15215 switch (accessibility)
15216 {
15217 case DW_ACCESS_public:
15218 /* The assumed value if neither private nor protected. */
15219 break;
15220 case DW_ACCESS_private:
15221 fp.is_private = 1;
15222 break;
15223 case DW_ACCESS_protected:
15224 fp.is_protected = 1;
15225 break;
15226 }
15227
15228 if (die->tag == DW_TAG_typedef)
15229 fip->typedef_field_list.push_back (fp);
15230 else
15231 fip->nested_types_list.push_back (fp);
15232 }
15233
15234 /* A convenience typedef that's used when finding the discriminant
15235 field for a variant part. */
15236 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
15237 offset_map_type;
15238
15239 /* Compute the discriminant range for a given variant. OBSTACK is
15240 where the results will be stored. VARIANT is the variant to
15241 process. IS_UNSIGNED indicates whether the discriminant is signed
15242 or unsigned. */
15243
15244 static const gdb::array_view<discriminant_range>
15245 convert_variant_range (struct obstack *obstack, const variant_field &variant,
15246 bool is_unsigned)
15247 {
15248 std::vector<discriminant_range> ranges;
15249
15250 if (variant.default_branch)
15251 return {};
15252
15253 if (variant.discr_list_data == nullptr)
15254 {
15255 discriminant_range r
15256 = {variant.discriminant_value, variant.discriminant_value};
15257 ranges.push_back (r);
15258 }
15259 else
15260 {
15261 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15262 variant.discr_list_data->size);
15263 while (!data.empty ())
15264 {
15265 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15266 {
15267 complaint (_("invalid discriminant marker: %d"), data[0]);
15268 break;
15269 }
15270 bool is_range = data[0] == DW_DSC_range;
15271 data = data.slice (1);
15272
15273 ULONGEST low, high;
15274 unsigned int bytes_read;
15275
15276 if (data.empty ())
15277 {
15278 complaint (_("DW_AT_discr_list missing low value"));
15279 break;
15280 }
15281 if (is_unsigned)
15282 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15283 else
15284 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15285 &bytes_read);
15286 data = data.slice (bytes_read);
15287
15288 if (is_range)
15289 {
15290 if (data.empty ())
15291 {
15292 complaint (_("DW_AT_discr_list missing high value"));
15293 break;
15294 }
15295 if (is_unsigned)
15296 high = read_unsigned_leb128 (nullptr, data.data (),
15297 &bytes_read);
15298 else
15299 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15300 &bytes_read);
15301 data = data.slice (bytes_read);
15302 }
15303 else
15304 high = low;
15305
15306 ranges.push_back ({ low, high });
15307 }
15308 }
15309
15310 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15311 ranges.size ());
15312 std::copy (ranges.begin (), ranges.end (), result);
15313 return gdb::array_view<discriminant_range> (result, ranges.size ());
15314 }
15315
15316 static const gdb::array_view<variant_part> create_variant_parts
15317 (struct obstack *obstack,
15318 const offset_map_type &offset_map,
15319 struct field_info *fi,
15320 const std::vector<variant_part_builder> &variant_parts);
15321
15322 /* Fill in a "struct variant" for a given variant field. RESULT is
15323 the variant to fill in. OBSTACK is where any needed allocations
15324 will be done. OFFSET_MAP holds the mapping from section offsets to
15325 fields for the type. FI describes the fields of the type we're
15326 processing. FIELD is the variant field we're converting. */
15327
15328 static void
15329 create_one_variant (variant &result, struct obstack *obstack,
15330 const offset_map_type &offset_map,
15331 struct field_info *fi, const variant_field &field)
15332 {
15333 result.discriminants = convert_variant_range (obstack, field, false);
15334 result.first_field = field.first_field + fi->baseclasses.size ();
15335 result.last_field = field.last_field + fi->baseclasses.size ();
15336 result.parts = create_variant_parts (obstack, offset_map, fi,
15337 field.variant_parts);
15338 }
15339
15340 /* Fill in a "struct variant_part" for a given variant part. RESULT
15341 is the variant part to fill in. OBSTACK is where any needed
15342 allocations will be done. OFFSET_MAP holds the mapping from
15343 section offsets to fields for the type. FI describes the fields of
15344 the type we're processing. BUILDER is the variant part to be
15345 converted. */
15346
15347 static void
15348 create_one_variant_part (variant_part &result,
15349 struct obstack *obstack,
15350 const offset_map_type &offset_map,
15351 struct field_info *fi,
15352 const variant_part_builder &builder)
15353 {
15354 auto iter = offset_map.find (builder.discriminant_offset);
15355 if (iter == offset_map.end ())
15356 {
15357 result.discriminant_index = -1;
15358 /* Doesn't matter. */
15359 result.is_unsigned = false;
15360 }
15361 else
15362 {
15363 result.discriminant_index = iter->second;
15364 result.is_unsigned
15365 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
15366 }
15367
15368 size_t n = builder.variants.size ();
15369 variant *output = new (obstack) variant[n];
15370 for (size_t i = 0; i < n; ++i)
15371 create_one_variant (output[i], obstack, offset_map, fi,
15372 builder.variants[i]);
15373
15374 result.variants = gdb::array_view<variant> (output, n);
15375 }
15376
15377 /* Create a vector of variant parts that can be attached to a type.
15378 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15379 holds the mapping from section offsets to fields for the type. FI
15380 describes the fields of the type we're processing. VARIANT_PARTS
15381 is the vector to convert. */
15382
15383 static const gdb::array_view<variant_part>
15384 create_variant_parts (struct obstack *obstack,
15385 const offset_map_type &offset_map,
15386 struct field_info *fi,
15387 const std::vector<variant_part_builder> &variant_parts)
15388 {
15389 if (variant_parts.empty ())
15390 return {};
15391
15392 size_t n = variant_parts.size ();
15393 variant_part *result = new (obstack) variant_part[n];
15394 for (size_t i = 0; i < n; ++i)
15395 create_one_variant_part (result[i], obstack, offset_map, fi,
15396 variant_parts[i]);
15397
15398 return gdb::array_view<variant_part> (result, n);
15399 }
15400
15401 /* Compute the variant part vector for FIP, attaching it to TYPE when
15402 done. */
15403
15404 static void
15405 add_variant_property (struct field_info *fip, struct type *type,
15406 struct dwarf2_cu *cu)
15407 {
15408 /* Map section offsets of fields to their field index. Note the
15409 field index here does not take the number of baseclasses into
15410 account. */
15411 offset_map_type offset_map;
15412 for (int i = 0; i < fip->fields.size (); ++i)
15413 offset_map[fip->fields[i].offset] = i;
15414
15415 struct objfile *objfile = cu->per_objfile->objfile;
15416 gdb::array_view<variant_part> parts
15417 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15418 fip->variant_parts);
15419
15420 struct dynamic_prop prop;
15421 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15422 obstack_copy (&objfile->objfile_obstack, &parts,
15423 sizeof (parts)));
15424
15425 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
15426 }
15427
15428 /* Create the vector of fields, and attach it to the type. */
15429
15430 static void
15431 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15432 struct dwarf2_cu *cu)
15433 {
15434 int nfields = fip->nfields ();
15435
15436 /* Record the field count, allocate space for the array of fields,
15437 and create blank accessibility bitfields if necessary. */
15438 type->set_num_fields (nfields);
15439 type->set_fields
15440 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
15441
15442 if (fip->non_public_fields && cu->language != language_ada)
15443 {
15444 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15445
15446 TYPE_FIELD_PRIVATE_BITS (type) =
15447 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15448 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15449
15450 TYPE_FIELD_PROTECTED_BITS (type) =
15451 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15452 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15453
15454 TYPE_FIELD_IGNORE_BITS (type) =
15455 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15456 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15457 }
15458
15459 /* If the type has baseclasses, allocate and clear a bit vector for
15460 TYPE_FIELD_VIRTUAL_BITS. */
15461 if (!fip->baseclasses.empty () && cu->language != language_ada)
15462 {
15463 int num_bytes = B_BYTES (fip->baseclasses.size ());
15464 unsigned char *pointer;
15465
15466 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15467 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15468 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15469 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15470 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15471 }
15472
15473 if (!fip->variant_parts.empty ())
15474 add_variant_property (fip, type, cu);
15475
15476 /* Copy the saved-up fields into the field vector. */
15477 for (int i = 0; i < nfields; ++i)
15478 {
15479 struct nextfield &field
15480 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15481 : fip->fields[i - fip->baseclasses.size ()]);
15482
15483 type->field (i) = field.field;
15484 switch (field.accessibility)
15485 {
15486 case DW_ACCESS_private:
15487 if (cu->language != language_ada)
15488 SET_TYPE_FIELD_PRIVATE (type, i);
15489 break;
15490
15491 case DW_ACCESS_protected:
15492 if (cu->language != language_ada)
15493 SET_TYPE_FIELD_PROTECTED (type, i);
15494 break;
15495
15496 case DW_ACCESS_public:
15497 break;
15498
15499 default:
15500 /* Unknown accessibility. Complain and treat it as public. */
15501 {
15502 complaint (_("unsupported accessibility %d"),
15503 field.accessibility);
15504 }
15505 break;
15506 }
15507 if (i < fip->baseclasses.size ())
15508 {
15509 switch (field.virtuality)
15510 {
15511 case DW_VIRTUALITY_virtual:
15512 case DW_VIRTUALITY_pure_virtual:
15513 if (cu->language == language_ada)
15514 error (_("unexpected virtuality in component of Ada type"));
15515 SET_TYPE_FIELD_VIRTUAL (type, i);
15516 break;
15517 }
15518 }
15519 }
15520 }
15521
15522 /* Return true if this member function is a constructor, false
15523 otherwise. */
15524
15525 static int
15526 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15527 {
15528 const char *fieldname;
15529 const char *type_name;
15530 int len;
15531
15532 if (die->parent == NULL)
15533 return 0;
15534
15535 if (die->parent->tag != DW_TAG_structure_type
15536 && die->parent->tag != DW_TAG_union_type
15537 && die->parent->tag != DW_TAG_class_type)
15538 return 0;
15539
15540 fieldname = dwarf2_name (die, cu);
15541 type_name = dwarf2_name (die->parent, cu);
15542 if (fieldname == NULL || type_name == NULL)
15543 return 0;
15544
15545 len = strlen (fieldname);
15546 return (strncmp (fieldname, type_name, len) == 0
15547 && (type_name[len] == '\0' || type_name[len] == '<'));
15548 }
15549
15550 /* Add a member function to the proper fieldlist. */
15551
15552 static void
15553 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15554 struct type *type, struct dwarf2_cu *cu)
15555 {
15556 struct objfile *objfile = cu->per_objfile->objfile;
15557 struct attribute *attr;
15558 int i;
15559 struct fnfieldlist *flp = nullptr;
15560 struct fn_field *fnp;
15561 const char *fieldname;
15562 struct type *this_type;
15563
15564 if (cu->language == language_ada)
15565 error (_("unexpected member function in Ada type"));
15566
15567 /* Get name of member function. */
15568 fieldname = dwarf2_name (die, cu);
15569 if (fieldname == NULL)
15570 return;
15571
15572 /* Look up member function name in fieldlist. */
15573 for (i = 0; i < fip->fnfieldlists.size (); i++)
15574 {
15575 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15576 {
15577 flp = &fip->fnfieldlists[i];
15578 break;
15579 }
15580 }
15581
15582 /* Create a new fnfieldlist if necessary. */
15583 if (flp == nullptr)
15584 {
15585 fip->fnfieldlists.emplace_back ();
15586 flp = &fip->fnfieldlists.back ();
15587 flp->name = fieldname;
15588 i = fip->fnfieldlists.size () - 1;
15589 }
15590
15591 /* Create a new member function field and add it to the vector of
15592 fnfieldlists. */
15593 flp->fnfields.emplace_back ();
15594 fnp = &flp->fnfields.back ();
15595
15596 /* Delay processing of the physname until later. */
15597 if (cu->language == language_cplus)
15598 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15599 die, cu);
15600 else
15601 {
15602 const char *physname = dwarf2_physname (fieldname, die, cu);
15603 fnp->physname = physname ? physname : "";
15604 }
15605
15606 fnp->type = alloc_type (objfile);
15607 this_type = read_type_die (die, cu);
15608 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15609 {
15610 int nparams = this_type->num_fields ();
15611
15612 /* TYPE is the domain of this method, and THIS_TYPE is the type
15613 of the method itself (TYPE_CODE_METHOD). */
15614 smash_to_method_type (fnp->type, type,
15615 TYPE_TARGET_TYPE (this_type),
15616 this_type->fields (),
15617 this_type->num_fields (),
15618 this_type->has_varargs ());
15619
15620 /* Handle static member functions.
15621 Dwarf2 has no clean way to discern C++ static and non-static
15622 member functions. G++ helps GDB by marking the first
15623 parameter for non-static member functions (which is the this
15624 pointer) as artificial. We obtain this information from
15625 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15626 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15627 fnp->voffset = VOFFSET_STATIC;
15628 }
15629 else
15630 complaint (_("member function type missing for '%s'"),
15631 dwarf2_full_name (fieldname, die, cu));
15632
15633 /* Get fcontext from DW_AT_containing_type if present. */
15634 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15635 fnp->fcontext = die_containing_type (die, cu);
15636
15637 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15638 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15639
15640 /* Get accessibility. */
15641 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15642 switch (accessibility)
15643 {
15644 case DW_ACCESS_private:
15645 fnp->is_private = 1;
15646 break;
15647 case DW_ACCESS_protected:
15648 fnp->is_protected = 1;
15649 break;
15650 }
15651
15652 /* Check for artificial methods. */
15653 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15654 if (attr && attr->as_boolean ())
15655 fnp->is_artificial = 1;
15656
15657 /* Check for defaulted methods. */
15658 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15659 if (attr != nullptr)
15660 fnp->defaulted = attr->defaulted ();
15661
15662 /* Check for deleted methods. */
15663 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15664 if (attr != nullptr && attr->as_boolean ())
15665 fnp->is_deleted = 1;
15666
15667 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15668
15669 /* Get index in virtual function table if it is a virtual member
15670 function. For older versions of GCC, this is an offset in the
15671 appropriate virtual table, as specified by DW_AT_containing_type.
15672 For everyone else, it is an expression to be evaluated relative
15673 to the object address. */
15674
15675 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15676 if (attr != nullptr)
15677 {
15678 if (attr->form_is_block () && attr->as_block ()->size > 0)
15679 {
15680 struct dwarf_block *block = attr->as_block ();
15681
15682 if (block->data[0] == DW_OP_constu)
15683 {
15684 /* Old-style GCC. */
15685 fnp->voffset = decode_locdesc (block, cu) + 2;
15686 }
15687 else if (block->data[0] == DW_OP_deref
15688 || (block->size > 1
15689 && block->data[0] == DW_OP_deref_size
15690 && block->data[1] == cu->header.addr_size))
15691 {
15692 fnp->voffset = decode_locdesc (block, cu);
15693 if ((fnp->voffset % cu->header.addr_size) != 0)
15694 dwarf2_complex_location_expr_complaint ();
15695 else
15696 fnp->voffset /= cu->header.addr_size;
15697 fnp->voffset += 2;
15698 }
15699 else
15700 dwarf2_complex_location_expr_complaint ();
15701
15702 if (!fnp->fcontext)
15703 {
15704 /* If there is no `this' field and no DW_AT_containing_type,
15705 we cannot actually find a base class context for the
15706 vtable! */
15707 if (this_type->num_fields () == 0
15708 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15709 {
15710 complaint (_("cannot determine context for virtual member "
15711 "function \"%s\" (offset %s)"),
15712 fieldname, sect_offset_str (die->sect_off));
15713 }
15714 else
15715 {
15716 fnp->fcontext
15717 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15718 }
15719 }
15720 }
15721 else if (attr->form_is_section_offset ())
15722 {
15723 dwarf2_complex_location_expr_complaint ();
15724 }
15725 else
15726 {
15727 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15728 fieldname);
15729 }
15730 }
15731 else
15732 {
15733 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15734 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
15735 {
15736 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15737 complaint (_("Member function \"%s\" (offset %s) is virtual "
15738 "but the vtable offset is not specified"),
15739 fieldname, sect_offset_str (die->sect_off));
15740 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15741 TYPE_CPLUS_DYNAMIC (type) = 1;
15742 }
15743 }
15744 }
15745
15746 /* Create the vector of member function fields, and attach it to the type. */
15747
15748 static void
15749 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15750 struct dwarf2_cu *cu)
15751 {
15752 if (cu->language == language_ada)
15753 error (_("unexpected member functions in Ada type"));
15754
15755 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15756 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15757 TYPE_ALLOC (type,
15758 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15759
15760 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15761 {
15762 struct fnfieldlist &nf = fip->fnfieldlists[i];
15763 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15764
15765 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15766 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15767 fn_flp->fn_fields = (struct fn_field *)
15768 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15769
15770 for (int k = 0; k < nf.fnfields.size (); ++k)
15771 fn_flp->fn_fields[k] = nf.fnfields[k];
15772 }
15773
15774 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15775 }
15776
15777 /* Returns non-zero if NAME is the name of a vtable member in CU's
15778 language, zero otherwise. */
15779 static int
15780 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15781 {
15782 static const char vptr[] = "_vptr";
15783
15784 /* Look for the C++ form of the vtable. */
15785 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15786 return 1;
15787
15788 return 0;
15789 }
15790
15791 /* GCC outputs unnamed structures that are really pointers to member
15792 functions, with the ABI-specified layout. If TYPE describes
15793 such a structure, smash it into a member function type.
15794
15795 GCC shouldn't do this; it should just output pointer to member DIEs.
15796 This is GCC PR debug/28767. */
15797
15798 static void
15799 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15800 {
15801 struct type *pfn_type, *self_type, *new_type;
15802
15803 /* Check for a structure with no name and two children. */
15804 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15805 return;
15806
15807 /* Check for __pfn and __delta members. */
15808 if (TYPE_FIELD_NAME (type, 0) == NULL
15809 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15810 || TYPE_FIELD_NAME (type, 1) == NULL
15811 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15812 return;
15813
15814 /* Find the type of the method. */
15815 pfn_type = type->field (0).type ();
15816 if (pfn_type == NULL
15817 || pfn_type->code () != TYPE_CODE_PTR
15818 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15819 return;
15820
15821 /* Look for the "this" argument. */
15822 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15823 if (pfn_type->num_fields () == 0
15824 /* || pfn_type->field (0).type () == NULL */
15825 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15826 return;
15827
15828 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15829 new_type = alloc_type (objfile);
15830 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15831 pfn_type->fields (), pfn_type->num_fields (),
15832 pfn_type->has_varargs ());
15833 smash_to_methodptr_type (type, new_type);
15834 }
15835
15836 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15837 requires rewriting, then copy it and return the updated copy.
15838 Otherwise return nullptr. */
15839
15840 static struct type *
15841 rewrite_array_type (struct type *type)
15842 {
15843 if (type->code () != TYPE_CODE_ARRAY)
15844 return nullptr;
15845
15846 struct type *index_type = type->index_type ();
15847 range_bounds *current_bounds = index_type->bounds ();
15848
15849 /* Handle multi-dimensional arrays. */
15850 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15851 if (new_target == nullptr)
15852 {
15853 /* Maybe we don't need to rewrite this array. */
15854 if (current_bounds->low.kind () == PROP_CONST
15855 && current_bounds->high.kind () == PROP_CONST)
15856 return nullptr;
15857 }
15858
15859 /* Either the target type was rewritten, or the bounds have to be
15860 updated. Either way we want to copy the type and update
15861 everything. */
15862 struct type *copy = copy_type (type);
15863 int nfields = copy->num_fields ();
15864 field *new_fields
15865 = ((struct field *) TYPE_ZALLOC (copy,
15866 nfields * sizeof (struct field)));
15867 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15868 copy->set_fields (new_fields);
15869 if (new_target != nullptr)
15870 TYPE_TARGET_TYPE (copy) = new_target;
15871
15872 struct type *index_copy = copy_type (index_type);
15873 range_bounds *bounds
15874 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15875 sizeof (range_bounds));
15876 *bounds = *current_bounds;
15877 bounds->low.set_const_val (1);
15878 bounds->high.set_const_val (0);
15879 index_copy->set_bounds (bounds);
15880 copy->set_index_type (index_copy);
15881
15882 return copy;
15883 }
15884
15885 /* While some versions of GCC will generate complicated DWARF for an
15886 array (see quirk_ada_thick_pointer), more recent versions were
15887 modified to emit an explicit thick pointer structure. However, in
15888 this case, the array still has DWARF expressions for its ranges,
15889 and these must be ignored. */
15890
15891 static void
15892 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15893 struct type *type)
15894 {
15895 gdb_assert (cu->language == language_ada);
15896
15897 /* Check for a structure with two children. */
15898 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15899 return;
15900
15901 /* Check for P_ARRAY and P_BOUNDS members. */
15902 if (TYPE_FIELD_NAME (type, 0) == NULL
15903 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15904 || TYPE_FIELD_NAME (type, 1) == NULL
15905 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15906 return;
15907
15908 /* Make sure we're looking at a pointer to an array. */
15909 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15910 return;
15911
15912 /* The Ada code already knows how to handle these types, so all that
15913 we need to do is turn the bounds into static bounds. However, we
15914 don't want to rewrite existing array or index types in-place,
15915 because those may be referenced in other contexts where this
15916 rewriting is undesirable. */
15917 struct type *new_ary_type
15918 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15919 if (new_ary_type != nullptr)
15920 type->field (0).set_type (lookup_pointer_type (new_ary_type));
15921 }
15922
15923 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15924 appropriate error checking and issuing complaints if there is a
15925 problem. */
15926
15927 static ULONGEST
15928 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15929 {
15930 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15931
15932 if (attr == nullptr)
15933 return 0;
15934
15935 if (!attr->form_is_constant ())
15936 {
15937 complaint (_("DW_AT_alignment must have constant form"
15938 " - DIE at %s [in module %s]"),
15939 sect_offset_str (die->sect_off),
15940 objfile_name (cu->per_objfile->objfile));
15941 return 0;
15942 }
15943
15944 LONGEST val = attr->constant_value (0);
15945 if (val < 0)
15946 {
15947 complaint (_("DW_AT_alignment value must not be negative"
15948 " - DIE at %s [in module %s]"),
15949 sect_offset_str (die->sect_off),
15950 objfile_name (cu->per_objfile->objfile));
15951 return 0;
15952 }
15953 ULONGEST align = val;
15954
15955 if (align == 0)
15956 {
15957 complaint (_("DW_AT_alignment value must not be zero"
15958 " - DIE at %s [in module %s]"),
15959 sect_offset_str (die->sect_off),
15960 objfile_name (cu->per_objfile->objfile));
15961 return 0;
15962 }
15963 if ((align & (align - 1)) != 0)
15964 {
15965 complaint (_("DW_AT_alignment value must be a power of 2"
15966 " - DIE at %s [in module %s]"),
15967 sect_offset_str (die->sect_off),
15968 objfile_name (cu->per_objfile->objfile));
15969 return 0;
15970 }
15971
15972 return align;
15973 }
15974
15975 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15976 the alignment for TYPE. */
15977
15978 static void
15979 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15980 struct type *type)
15981 {
15982 if (!set_type_align (type, get_alignment (cu, die)))
15983 complaint (_("DW_AT_alignment value too large"
15984 " - DIE at %s [in module %s]"),
15985 sect_offset_str (die->sect_off),
15986 objfile_name (cu->per_objfile->objfile));
15987 }
15988
15989 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15990 constant for a type, according to DWARF5 spec, Table 5.5. */
15991
15992 static bool
15993 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15994 {
15995 switch (value)
15996 {
15997 case DW_CC_normal:
15998 case DW_CC_pass_by_reference:
15999 case DW_CC_pass_by_value:
16000 return true;
16001
16002 default:
16003 complaint (_("unrecognized DW_AT_calling_convention value "
16004 "(%s) for a type"), pulongest (value));
16005 return false;
16006 }
16007 }
16008
16009 /* Check if the given VALUE is a valid enum dwarf_calling_convention
16010 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
16011 also according to GNU-specific values (see include/dwarf2.h). */
16012
16013 static bool
16014 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
16015 {
16016 switch (value)
16017 {
16018 case DW_CC_normal:
16019 case DW_CC_program:
16020 case DW_CC_nocall:
16021 return true;
16022
16023 case DW_CC_GNU_renesas_sh:
16024 case DW_CC_GNU_borland_fastcall_i386:
16025 case DW_CC_GDB_IBM_OpenCL:
16026 return true;
16027
16028 default:
16029 complaint (_("unrecognized DW_AT_calling_convention value "
16030 "(%s) for a subroutine"), pulongest (value));
16031 return false;
16032 }
16033 }
16034
16035 /* Called when we find the DIE that starts a structure or union scope
16036 (definition) to create a type for the structure or union. Fill in
16037 the type's name and general properties; the members will not be
16038 processed until process_structure_scope. A symbol table entry for
16039 the type will also not be done until process_structure_scope (assuming
16040 the type has a name).
16041
16042 NOTE: we need to call these functions regardless of whether or not the
16043 DIE has a DW_AT_name attribute, since it might be an anonymous
16044 structure or union. This gets the type entered into our set of
16045 user defined types. */
16046
16047 static struct type *
16048 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
16049 {
16050 struct objfile *objfile = cu->per_objfile->objfile;
16051 struct type *type;
16052 struct attribute *attr;
16053 const char *name;
16054
16055 /* If the definition of this type lives in .debug_types, read that type.
16056 Don't follow DW_AT_specification though, that will take us back up
16057 the chain and we want to go down. */
16058 attr = die->attr (DW_AT_signature);
16059 if (attr != nullptr)
16060 {
16061 type = get_DW_AT_signature_type (die, attr, cu);
16062
16063 /* The type's CU may not be the same as CU.
16064 Ensure TYPE is recorded with CU in die_type_hash. */
16065 return set_die_type (die, type, cu);
16066 }
16067
16068 type = alloc_type (objfile);
16069 INIT_CPLUS_SPECIFIC (type);
16070
16071 name = dwarf2_name (die, cu);
16072 if (name != NULL)
16073 {
16074 if (cu->language == language_cplus
16075 || cu->language == language_d
16076 || cu->language == language_rust)
16077 {
16078 const char *full_name = dwarf2_full_name (name, die, cu);
16079
16080 /* dwarf2_full_name might have already finished building the DIE's
16081 type. If so, there is no need to continue. */
16082 if (get_die_type (die, cu) != NULL)
16083 return get_die_type (die, cu);
16084
16085 type->set_name (full_name);
16086 }
16087 else
16088 {
16089 /* The name is already allocated along with this objfile, so
16090 we don't need to duplicate it for the type. */
16091 type->set_name (name);
16092 }
16093 }
16094
16095 if (die->tag == DW_TAG_structure_type)
16096 {
16097 type->set_code (TYPE_CODE_STRUCT);
16098 }
16099 else if (die->tag == DW_TAG_union_type)
16100 {
16101 type->set_code (TYPE_CODE_UNION);
16102 }
16103 else
16104 {
16105 type->set_code (TYPE_CODE_STRUCT);
16106 }
16107
16108 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16109 TYPE_DECLARED_CLASS (type) = 1;
16110
16111 /* Store the calling convention in the type if it's available in
16112 the die. Otherwise the calling convention remains set to
16113 the default value DW_CC_normal. */
16114 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16115 if (attr != nullptr
16116 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
16117 {
16118 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16119 TYPE_CPLUS_CALLING_CONVENTION (type)
16120 = (enum dwarf_calling_convention) (attr->constant_value (0));
16121 }
16122
16123 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16124 if (attr != nullptr)
16125 {
16126 if (attr->form_is_constant ())
16127 TYPE_LENGTH (type) = attr->constant_value (0);
16128 else
16129 {
16130 struct dynamic_prop prop;
16131 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
16132 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
16133 TYPE_LENGTH (type) = 0;
16134 }
16135 }
16136 else
16137 {
16138 TYPE_LENGTH (type) = 0;
16139 }
16140
16141 maybe_set_alignment (cu, die, type);
16142
16143 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
16144 {
16145 /* ICC<14 does not output the required DW_AT_declaration on
16146 incomplete types, but gives them a size of zero. */
16147 type->set_is_stub (true);
16148 }
16149 else
16150 type->set_stub_is_supported (true);
16151
16152 if (die_is_declaration (die, cu))
16153 type->set_is_stub (true);
16154 else if (attr == NULL && die->child == NULL
16155 && producer_is_realview (cu->producer))
16156 /* RealView does not output the required DW_AT_declaration
16157 on incomplete types. */
16158 type->set_is_stub (true);
16159
16160 /* We need to add the type field to the die immediately so we don't
16161 infinitely recurse when dealing with pointers to the structure
16162 type within the structure itself. */
16163 set_die_type (die, type, cu);
16164
16165 /* set_die_type should be already done. */
16166 set_descriptive_type (type, die, cu);
16167
16168 return type;
16169 }
16170
16171 static void handle_struct_member_die
16172 (struct die_info *child_die,
16173 struct type *type,
16174 struct field_info *fi,
16175 std::vector<struct symbol *> *template_args,
16176 struct dwarf2_cu *cu);
16177
16178 /* A helper for handle_struct_member_die that handles
16179 DW_TAG_variant_part. */
16180
16181 static void
16182 handle_variant_part (struct die_info *die, struct type *type,
16183 struct field_info *fi,
16184 std::vector<struct symbol *> *template_args,
16185 struct dwarf2_cu *cu)
16186 {
16187 variant_part_builder *new_part;
16188 if (fi->current_variant_part == nullptr)
16189 {
16190 fi->variant_parts.emplace_back ();
16191 new_part = &fi->variant_parts.back ();
16192 }
16193 else if (!fi->current_variant_part->processing_variant)
16194 {
16195 complaint (_("nested DW_TAG_variant_part seen "
16196 "- DIE at %s [in module %s]"),
16197 sect_offset_str (die->sect_off),
16198 objfile_name (cu->per_objfile->objfile));
16199 return;
16200 }
16201 else
16202 {
16203 variant_field &current = fi->current_variant_part->variants.back ();
16204 current.variant_parts.emplace_back ();
16205 new_part = &current.variant_parts.back ();
16206 }
16207
16208 /* When we recurse, we want callees to add to this new variant
16209 part. */
16210 scoped_restore save_current_variant_part
16211 = make_scoped_restore (&fi->current_variant_part, new_part);
16212
16213 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16214 if (discr == NULL)
16215 {
16216 /* It's a univariant form, an extension we support. */
16217 }
16218 else if (discr->form_is_ref ())
16219 {
16220 struct dwarf2_cu *target_cu = cu;
16221 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16222
16223 new_part->discriminant_offset = target_die->sect_off;
16224 }
16225 else
16226 {
16227 complaint (_("DW_AT_discr does not have DIE reference form"
16228 " - DIE at %s [in module %s]"),
16229 sect_offset_str (die->sect_off),
16230 objfile_name (cu->per_objfile->objfile));
16231 }
16232
16233 for (die_info *child_die = die->child;
16234 child_die != NULL;
16235 child_die = child_die->sibling)
16236 handle_struct_member_die (child_die, type, fi, template_args, cu);
16237 }
16238
16239 /* A helper for handle_struct_member_die that handles
16240 DW_TAG_variant. */
16241
16242 static void
16243 handle_variant (struct die_info *die, struct type *type,
16244 struct field_info *fi,
16245 std::vector<struct symbol *> *template_args,
16246 struct dwarf2_cu *cu)
16247 {
16248 if (fi->current_variant_part == nullptr)
16249 {
16250 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
16251 "- DIE at %s [in module %s]"),
16252 sect_offset_str (die->sect_off),
16253 objfile_name (cu->per_objfile->objfile));
16254 return;
16255 }
16256 if (fi->current_variant_part->processing_variant)
16257 {
16258 complaint (_("nested DW_TAG_variant seen "
16259 "- DIE at %s [in module %s]"),
16260 sect_offset_str (die->sect_off),
16261 objfile_name (cu->per_objfile->objfile));
16262 return;
16263 }
16264
16265 scoped_restore save_processing_variant
16266 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16267 true);
16268
16269 fi->current_variant_part->variants.emplace_back ();
16270 variant_field &variant = fi->current_variant_part->variants.back ();
16271 variant.first_field = fi->fields.size ();
16272
16273 /* In a variant we want to get the discriminant and also add a
16274 field for our sole member child. */
16275 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
16276 if (discr == nullptr || !discr->form_is_constant ())
16277 {
16278 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
16279 if (discr == nullptr || discr->as_block ()->size == 0)
16280 variant.default_branch = true;
16281 else
16282 variant.discr_list_data = discr->as_block ();
16283 }
16284 else
16285 variant.discriminant_value = discr->constant_value (0);
16286
16287 for (die_info *variant_child = die->child;
16288 variant_child != NULL;
16289 variant_child = variant_child->sibling)
16290 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16291
16292 variant.last_field = fi->fields.size ();
16293 }
16294
16295 /* A helper for process_structure_scope that handles a single member
16296 DIE. */
16297
16298 static void
16299 handle_struct_member_die (struct die_info *child_die, struct type *type,
16300 struct field_info *fi,
16301 std::vector<struct symbol *> *template_args,
16302 struct dwarf2_cu *cu)
16303 {
16304 if (child_die->tag == DW_TAG_member
16305 || child_die->tag == DW_TAG_variable)
16306 {
16307 /* NOTE: carlton/2002-11-05: A C++ static data member
16308 should be a DW_TAG_member that is a declaration, but
16309 all versions of G++ as of this writing (so through at
16310 least 3.2.1) incorrectly generate DW_TAG_variable
16311 tags for them instead. */
16312 dwarf2_add_field (fi, child_die, cu);
16313 }
16314 else if (child_die->tag == DW_TAG_subprogram)
16315 {
16316 /* Rust doesn't have member functions in the C++ sense.
16317 However, it does emit ordinary functions as children
16318 of a struct DIE. */
16319 if (cu->language == language_rust)
16320 read_func_scope (child_die, cu);
16321 else
16322 {
16323 /* C++ member function. */
16324 dwarf2_add_member_fn (fi, child_die, type, cu);
16325 }
16326 }
16327 else if (child_die->tag == DW_TAG_inheritance)
16328 {
16329 /* C++ base class field. */
16330 dwarf2_add_field (fi, child_die, cu);
16331 }
16332 else if (type_can_define_types (child_die))
16333 dwarf2_add_type_defn (fi, child_die, cu);
16334 else if (child_die->tag == DW_TAG_template_type_param
16335 || child_die->tag == DW_TAG_template_value_param)
16336 {
16337 struct symbol *arg = new_symbol (child_die, NULL, cu);
16338
16339 if (arg != NULL)
16340 template_args->push_back (arg);
16341 }
16342 else if (child_die->tag == DW_TAG_variant_part)
16343 handle_variant_part (child_die, type, fi, template_args, cu);
16344 else if (child_die->tag == DW_TAG_variant)
16345 handle_variant (child_die, type, fi, template_args, cu);
16346 }
16347
16348 /* Finish creating a structure or union type, including filling in
16349 its members and creating a symbol for it. */
16350
16351 static void
16352 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16353 {
16354 struct objfile *objfile = cu->per_objfile->objfile;
16355 struct die_info *child_die;
16356 struct type *type;
16357
16358 type = get_die_type (die, cu);
16359 if (type == NULL)
16360 type = read_structure_type (die, cu);
16361
16362 bool has_template_parameters = false;
16363 if (die->child != NULL && ! die_is_declaration (die, cu))
16364 {
16365 struct field_info fi;
16366 std::vector<struct symbol *> template_args;
16367
16368 child_die = die->child;
16369
16370 while (child_die && child_die->tag)
16371 {
16372 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16373 child_die = child_die->sibling;
16374 }
16375
16376 /* Attach template arguments to type. */
16377 if (!template_args.empty ())
16378 {
16379 has_template_parameters = true;
16380 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16381 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16382 TYPE_TEMPLATE_ARGUMENTS (type)
16383 = XOBNEWVEC (&objfile->objfile_obstack,
16384 struct symbol *,
16385 TYPE_N_TEMPLATE_ARGUMENTS (type));
16386 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16387 template_args.data (),
16388 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16389 * sizeof (struct symbol *)));
16390 }
16391
16392 /* Attach fields and member functions to the type. */
16393 if (fi.nfields () > 0)
16394 dwarf2_attach_fields_to_type (&fi, type, cu);
16395 if (!fi.fnfieldlists.empty ())
16396 {
16397 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16398
16399 /* Get the type which refers to the base class (possibly this
16400 class itself) which contains the vtable pointer for the current
16401 class from the DW_AT_containing_type attribute. This use of
16402 DW_AT_containing_type is a GNU extension. */
16403
16404 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16405 {
16406 struct type *t = die_containing_type (die, cu);
16407
16408 set_type_vptr_basetype (type, t);
16409 if (type == t)
16410 {
16411 int i;
16412
16413 /* Our own class provides vtbl ptr. */
16414 for (i = t->num_fields () - 1;
16415 i >= TYPE_N_BASECLASSES (t);
16416 --i)
16417 {
16418 const char *fieldname = TYPE_FIELD_NAME (t, i);
16419
16420 if (is_vtable_name (fieldname, cu))
16421 {
16422 set_type_vptr_fieldno (type, i);
16423 break;
16424 }
16425 }
16426
16427 /* Complain if virtual function table field not found. */
16428 if (i < TYPE_N_BASECLASSES (t))
16429 complaint (_("virtual function table pointer "
16430 "not found when defining class '%s'"),
16431 type->name () ? type->name () : "");
16432 }
16433 else
16434 {
16435 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16436 }
16437 }
16438 else if (cu->producer
16439 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16440 {
16441 /* The IBM XLC compiler does not provide direct indication
16442 of the containing type, but the vtable pointer is
16443 always named __vfp. */
16444
16445 int i;
16446
16447 for (i = type->num_fields () - 1;
16448 i >= TYPE_N_BASECLASSES (type);
16449 --i)
16450 {
16451 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16452 {
16453 set_type_vptr_fieldno (type, i);
16454 set_type_vptr_basetype (type, type);
16455 break;
16456 }
16457 }
16458 }
16459 }
16460
16461 /* Copy fi.typedef_field_list linked list elements content into the
16462 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16463 if (!fi.typedef_field_list.empty ())
16464 {
16465 int count = fi.typedef_field_list.size ();
16466
16467 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16468 TYPE_TYPEDEF_FIELD_ARRAY (type)
16469 = ((struct decl_field *)
16470 TYPE_ALLOC (type,
16471 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16472 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16473
16474 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16475 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16476 }
16477
16478 /* Copy fi.nested_types_list linked list elements content into the
16479 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16480 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16481 {
16482 int count = fi.nested_types_list.size ();
16483
16484 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16485 TYPE_NESTED_TYPES_ARRAY (type)
16486 = ((struct decl_field *)
16487 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16488 TYPE_NESTED_TYPES_COUNT (type) = count;
16489
16490 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16491 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16492 }
16493 }
16494
16495 quirk_gcc_member_function_pointer (type, objfile);
16496 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16497 cu->rust_unions.push_back (type);
16498 else if (cu->language == language_ada)
16499 quirk_ada_thick_pointer_struct (die, cu, type);
16500
16501 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16502 snapshots) has been known to create a die giving a declaration
16503 for a class that has, as a child, a die giving a definition for a
16504 nested class. So we have to process our children even if the
16505 current die is a declaration. Normally, of course, a declaration
16506 won't have any children at all. */
16507
16508 child_die = die->child;
16509
16510 while (child_die != NULL && child_die->tag)
16511 {
16512 if (child_die->tag == DW_TAG_member
16513 || child_die->tag == DW_TAG_variable
16514 || child_die->tag == DW_TAG_inheritance
16515 || child_die->tag == DW_TAG_template_value_param
16516 || child_die->tag == DW_TAG_template_type_param)
16517 {
16518 /* Do nothing. */
16519 }
16520 else
16521 process_die (child_die, cu);
16522
16523 child_die = child_die->sibling;
16524 }
16525
16526 /* Do not consider external references. According to the DWARF standard,
16527 these DIEs are identified by the fact that they have no byte_size
16528 attribute, and a declaration attribute. */
16529 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16530 || !die_is_declaration (die, cu)
16531 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16532 {
16533 struct symbol *sym = new_symbol (die, type, cu);
16534
16535 if (has_template_parameters)
16536 {
16537 struct symtab *symtab;
16538 if (sym != nullptr)
16539 symtab = symbol_symtab (sym);
16540 else if (cu->line_header != nullptr)
16541 {
16542 /* Any related symtab will do. */
16543 symtab
16544 = cu->line_header->file_names ()[0].symtab;
16545 }
16546 else
16547 {
16548 symtab = nullptr;
16549 complaint (_("could not find suitable "
16550 "symtab for template parameter"
16551 " - DIE at %s [in module %s]"),
16552 sect_offset_str (die->sect_off),
16553 objfile_name (objfile));
16554 }
16555
16556 if (symtab != nullptr)
16557 {
16558 /* Make sure that the symtab is set on the new symbols.
16559 Even though they don't appear in this symtab directly,
16560 other parts of gdb assume that symbols do, and this is
16561 reasonably true. */
16562 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16563 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16564 }
16565 }
16566 }
16567 }
16568
16569 /* Assuming DIE is an enumeration type, and TYPE is its associated
16570 type, update TYPE using some information only available in DIE's
16571 children. In particular, the fields are computed. */
16572
16573 static void
16574 update_enumeration_type_from_children (struct die_info *die,
16575 struct type *type,
16576 struct dwarf2_cu *cu)
16577 {
16578 struct die_info *child_die;
16579 int unsigned_enum = 1;
16580 int flag_enum = 1;
16581
16582 auto_obstack obstack;
16583 std::vector<struct field> fields;
16584
16585 for (child_die = die->child;
16586 child_die != NULL && child_die->tag;
16587 child_die = child_die->sibling)
16588 {
16589 struct attribute *attr;
16590 LONGEST value;
16591 const gdb_byte *bytes;
16592 struct dwarf2_locexpr_baton *baton;
16593 const char *name;
16594
16595 if (child_die->tag != DW_TAG_enumerator)
16596 continue;
16597
16598 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16599 if (attr == NULL)
16600 continue;
16601
16602 name = dwarf2_name (child_die, cu);
16603 if (name == NULL)
16604 name = "<anonymous enumerator>";
16605
16606 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16607 &value, &bytes, &baton);
16608 if (value < 0)
16609 {
16610 unsigned_enum = 0;
16611 flag_enum = 0;
16612 }
16613 else
16614 {
16615 if (count_one_bits_ll (value) >= 2)
16616 flag_enum = 0;
16617 }
16618
16619 fields.emplace_back ();
16620 struct field &field = fields.back ();
16621 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16622 SET_FIELD_ENUMVAL (field, value);
16623 }
16624
16625 if (!fields.empty ())
16626 {
16627 type->set_num_fields (fields.size ());
16628 type->set_fields
16629 ((struct field *)
16630 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16631 memcpy (type->fields (), fields.data (),
16632 sizeof (struct field) * fields.size ());
16633 }
16634
16635 if (unsigned_enum)
16636 type->set_is_unsigned (true);
16637
16638 if (flag_enum)
16639 TYPE_FLAG_ENUM (type) = 1;
16640 }
16641
16642 /* Given a DW_AT_enumeration_type die, set its type. We do not
16643 complete the type's fields yet, or create any symbols. */
16644
16645 static struct type *
16646 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16647 {
16648 struct objfile *objfile = cu->per_objfile->objfile;
16649 struct type *type;
16650 struct attribute *attr;
16651 const char *name;
16652
16653 /* If the definition of this type lives in .debug_types, read that type.
16654 Don't follow DW_AT_specification though, that will take us back up
16655 the chain and we want to go down. */
16656 attr = die->attr (DW_AT_signature);
16657 if (attr != nullptr)
16658 {
16659 type = get_DW_AT_signature_type (die, attr, cu);
16660
16661 /* The type's CU may not be the same as CU.
16662 Ensure TYPE is recorded with CU in die_type_hash. */
16663 return set_die_type (die, type, cu);
16664 }
16665
16666 type = alloc_type (objfile);
16667
16668 type->set_code (TYPE_CODE_ENUM);
16669 name = dwarf2_full_name (NULL, die, cu);
16670 if (name != NULL)
16671 type->set_name (name);
16672
16673 attr = dwarf2_attr (die, DW_AT_type, cu);
16674 if (attr != NULL)
16675 {
16676 struct type *underlying_type = die_type (die, cu);
16677
16678 TYPE_TARGET_TYPE (type) = underlying_type;
16679 }
16680
16681 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16682 if (attr != nullptr)
16683 {
16684 TYPE_LENGTH (type) = attr->constant_value (0);
16685 }
16686 else
16687 {
16688 TYPE_LENGTH (type) = 0;
16689 }
16690
16691 maybe_set_alignment (cu, die, type);
16692
16693 /* The enumeration DIE can be incomplete. In Ada, any type can be
16694 declared as private in the package spec, and then defined only
16695 inside the package body. Such types are known as Taft Amendment
16696 Types. When another package uses such a type, an incomplete DIE
16697 may be generated by the compiler. */
16698 if (die_is_declaration (die, cu))
16699 type->set_is_stub (true);
16700
16701 /* If this type has an underlying type that is not a stub, then we
16702 may use its attributes. We always use the "unsigned" attribute
16703 in this situation, because ordinarily we guess whether the type
16704 is unsigned -- but the guess can be wrong and the underlying type
16705 can tell us the reality. However, we defer to a local size
16706 attribute if one exists, because this lets the compiler override
16707 the underlying type if needed. */
16708 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16709 {
16710 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16711 underlying_type = check_typedef (underlying_type);
16712
16713 type->set_is_unsigned (underlying_type->is_unsigned ());
16714
16715 if (TYPE_LENGTH (type) == 0)
16716 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16717
16718 if (TYPE_RAW_ALIGN (type) == 0
16719 && TYPE_RAW_ALIGN (underlying_type) != 0)
16720 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16721 }
16722
16723 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16724
16725 set_die_type (die, type, cu);
16726
16727 /* Finish the creation of this type by using the enum's children.
16728 Note that, as usual, this must come after set_die_type to avoid
16729 infinite recursion when trying to compute the names of the
16730 enumerators. */
16731 update_enumeration_type_from_children (die, type, cu);
16732
16733 return type;
16734 }
16735
16736 /* Given a pointer to a die which begins an enumeration, process all
16737 the dies that define the members of the enumeration, and create the
16738 symbol for the enumeration type.
16739
16740 NOTE: We reverse the order of the element list. */
16741
16742 static void
16743 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16744 {
16745 struct type *this_type;
16746
16747 this_type = get_die_type (die, cu);
16748 if (this_type == NULL)
16749 this_type = read_enumeration_type (die, cu);
16750
16751 if (die->child != NULL)
16752 {
16753 struct die_info *child_die;
16754 const char *name;
16755
16756 child_die = die->child;
16757 while (child_die && child_die->tag)
16758 {
16759 if (child_die->tag != DW_TAG_enumerator)
16760 {
16761 process_die (child_die, cu);
16762 }
16763 else
16764 {
16765 name = dwarf2_name (child_die, cu);
16766 if (name)
16767 new_symbol (child_die, this_type, cu);
16768 }
16769
16770 child_die = child_die->sibling;
16771 }
16772 }
16773
16774 /* If we are reading an enum from a .debug_types unit, and the enum
16775 is a declaration, and the enum is not the signatured type in the
16776 unit, then we do not want to add a symbol for it. Adding a
16777 symbol would in some cases obscure the true definition of the
16778 enum, giving users an incomplete type when the definition is
16779 actually available. Note that we do not want to do this for all
16780 enums which are just declarations, because C++0x allows forward
16781 enum declarations. */
16782 if (cu->per_cu->is_debug_types
16783 && die_is_declaration (die, cu))
16784 {
16785 struct signatured_type *sig_type;
16786
16787 sig_type = (struct signatured_type *) cu->per_cu;
16788 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16789 if (sig_type->type_offset_in_section != die->sect_off)
16790 return;
16791 }
16792
16793 new_symbol (die, this_type, cu);
16794 }
16795
16796 /* Helper function for quirk_ada_thick_pointer that examines a bounds
16797 expression for an index type and finds the corresponding field
16798 offset in the hidden "P_BOUNDS" structure. Returns true on success
16799 and updates *FIELD, false if it fails to recognize an
16800 expression. */
16801
16802 static bool
16803 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16804 int *bounds_offset, struct field *field,
16805 struct dwarf2_cu *cu)
16806 {
16807 struct attribute *attr = dwarf2_attr (die, name, cu);
16808 if (attr == nullptr || !attr->form_is_block ())
16809 return false;
16810
16811 const struct dwarf_block *block = attr->as_block ();
16812 const gdb_byte *start = block->data;
16813 const gdb_byte *end = block->data + block->size;
16814
16815 /* The expression to recognize generally looks like:
16816
16817 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16818 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16819
16820 However, the second "plus_uconst" may be missing:
16821
16822 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16823 DW_OP_deref_size: 4)
16824
16825 This happens when the field is at the start of the structure.
16826
16827 Also, the final deref may not be sized:
16828
16829 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16830 DW_OP_deref)
16831
16832 This happens when the size of the index type happens to be the
16833 same as the architecture's word size. This can occur with or
16834 without the second plus_uconst. */
16835
16836 if (end - start < 2)
16837 return false;
16838 if (*start++ != DW_OP_push_object_address)
16839 return false;
16840 if (*start++ != DW_OP_plus_uconst)
16841 return false;
16842
16843 uint64_t this_bound_off;
16844 start = gdb_read_uleb128 (start, end, &this_bound_off);
16845 if (start == nullptr || (int) this_bound_off != this_bound_off)
16846 return false;
16847 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16848 is consistent among all bounds. */
16849 if (*bounds_offset == -1)
16850 *bounds_offset = this_bound_off;
16851 else if (*bounds_offset != this_bound_off)
16852 return false;
16853
16854 if (start == end || *start++ != DW_OP_deref)
16855 return false;
16856
16857 int offset = 0;
16858 if (start ==end)
16859 return false;
16860 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16861 {
16862 /* This means an offset of 0. */
16863 }
16864 else if (*start++ != DW_OP_plus_uconst)
16865 return false;
16866 else
16867 {
16868 /* The size is the parameter to DW_OP_plus_uconst. */
16869 uint64_t val;
16870 start = gdb_read_uleb128 (start, end, &val);
16871 if (start == nullptr)
16872 return false;
16873 if ((int) val != val)
16874 return false;
16875 offset = val;
16876 }
16877
16878 if (start == end)
16879 return false;
16880
16881 uint64_t size;
16882 if (*start == DW_OP_deref_size)
16883 {
16884 start = gdb_read_uleb128 (start + 1, end, &size);
16885 if (start == nullptr)
16886 return false;
16887 }
16888 else if (*start == DW_OP_deref)
16889 {
16890 size = cu->header.addr_size;
16891 ++start;
16892 }
16893 else
16894 return false;
16895
16896 SET_FIELD_BITPOS (*field, 8 * offset);
16897 if (size != TYPE_LENGTH (field->type ()))
16898 FIELD_BITSIZE (*field) = 8 * size;
16899
16900 return true;
16901 }
16902
16903 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16904 some kinds of Ada arrays:
16905
16906 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16907 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16908 <11e0> DW_AT_data_location: 2 byte block: 97 6
16909 (DW_OP_push_object_address; DW_OP_deref)
16910 <11e3> DW_AT_type : <0x1173>
16911 <11e7> DW_AT_sibling : <0x1201>
16912 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16913 <11ec> DW_AT_type : <0x1206>
16914 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16915 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16916 DW_OP_deref_size: 4)
16917 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16918 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16919 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16920
16921 This actually represents a "thick pointer", which is a structure
16922 with two elements: one that is a pointer to the array data, and one
16923 that is a pointer to another structure; this second structure holds
16924 the array bounds.
16925
16926 This returns a new type on success, or nullptr if this didn't
16927 recognize the type. */
16928
16929 static struct type *
16930 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16931 struct type *type)
16932 {
16933 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16934 /* So far we've only seen this with block form. */
16935 if (attr == nullptr || !attr->form_is_block ())
16936 return nullptr;
16937
16938 /* Note that this will fail if the structure layout is changed by
16939 the compiler. However, we have no good way to recognize some
16940 other layout, because we don't know what expression the compiler
16941 might choose to emit should this happen. */
16942 struct dwarf_block *blk = attr->as_block ();
16943 if (blk->size != 2
16944 || blk->data[0] != DW_OP_push_object_address
16945 || blk->data[1] != DW_OP_deref)
16946 return nullptr;
16947
16948 int bounds_offset = -1;
16949 int max_align = -1;
16950 std::vector<struct field> range_fields;
16951 for (struct die_info *child_die = die->child;
16952 child_die;
16953 child_die = child_die->sibling)
16954 {
16955 if (child_die->tag == DW_TAG_subrange_type)
16956 {
16957 struct type *underlying = read_subrange_index_type (child_die, cu);
16958
16959 int this_align = type_align (underlying);
16960 if (this_align > max_align)
16961 max_align = this_align;
16962
16963 range_fields.emplace_back ();
16964 range_fields.emplace_back ();
16965
16966 struct field &lower = range_fields[range_fields.size () - 2];
16967 struct field &upper = range_fields[range_fields.size () - 1];
16968
16969 lower.set_type (underlying);
16970 FIELD_ARTIFICIAL (lower) = 1;
16971
16972 upper.set_type (underlying);
16973 FIELD_ARTIFICIAL (upper) = 1;
16974
16975 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16976 &bounds_offset, &lower, cu)
16977 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16978 &bounds_offset, &upper, cu))
16979 return nullptr;
16980 }
16981 }
16982
16983 /* This shouldn't really happen, but double-check that we found
16984 where the bounds are stored. */
16985 if (bounds_offset == -1)
16986 return nullptr;
16987
16988 struct objfile *objfile = cu->per_objfile->objfile;
16989 for (int i = 0; i < range_fields.size (); i += 2)
16990 {
16991 char name[20];
16992
16993 /* Set the name of each field in the bounds. */
16994 xsnprintf (name, sizeof (name), "LB%d", i / 2);
16995 FIELD_NAME (range_fields[i]) = objfile->intern (name);
16996 xsnprintf (name, sizeof (name), "UB%d", i / 2);
16997 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
16998 }
16999
17000 struct type *bounds = alloc_type (objfile);
17001 bounds->set_code (TYPE_CODE_STRUCT);
17002
17003 bounds->set_num_fields (range_fields.size ());
17004 bounds->set_fields
17005 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
17006 * sizeof (struct field))));
17007 memcpy (bounds->fields (), range_fields.data (),
17008 bounds->num_fields () * sizeof (struct field));
17009
17010 int last_fieldno = range_fields.size () - 1;
17011 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
17012 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
17013 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
17014
17015 /* Rewrite the existing array type in place. Specifically, we
17016 remove any dynamic properties we might have read, and we replace
17017 the index types. */
17018 struct type *iter = type;
17019 for (int i = 0; i < range_fields.size (); i += 2)
17020 {
17021 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
17022 iter->main_type->dyn_prop_list = nullptr;
17023 iter->set_index_type
17024 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
17025 iter = TYPE_TARGET_TYPE (iter);
17026 }
17027
17028 struct type *result = alloc_type (objfile);
17029 result->set_code (TYPE_CODE_STRUCT);
17030
17031 result->set_num_fields (2);
17032 result->set_fields
17033 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
17034 * sizeof (struct field))));
17035
17036 /* The names are chosen to coincide with what the compiler does with
17037 -fgnat-encodings=all, which the Ada code in gdb already
17038 understands. */
17039 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
17040 result->field (0).set_type (lookup_pointer_type (type));
17041
17042 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
17043 result->field (1).set_type (lookup_pointer_type (bounds));
17044 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
17045
17046 result->set_name (type->name ());
17047 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
17048 + TYPE_LENGTH (result->field (1).type ()));
17049
17050 return result;
17051 }
17052
17053 /* Extract all information from a DW_TAG_array_type DIE and put it in
17054 the DIE's type field. For now, this only handles one dimensional
17055 arrays. */
17056
17057 static struct type *
17058 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
17059 {
17060 struct objfile *objfile = cu->per_objfile->objfile;
17061 struct die_info *child_die;
17062 struct type *type;
17063 struct type *element_type, *range_type, *index_type;
17064 struct attribute *attr;
17065 const char *name;
17066 struct dynamic_prop *byte_stride_prop = NULL;
17067 unsigned int bit_stride = 0;
17068
17069 element_type = die_type (die, cu);
17070
17071 /* The die_type call above may have already set the type for this DIE. */
17072 type = get_die_type (die, cu);
17073 if (type)
17074 return type;
17075
17076 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
17077 if (attr != NULL)
17078 {
17079 int stride_ok;
17080 struct type *prop_type = cu->addr_sized_int_type (false);
17081
17082 byte_stride_prop
17083 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
17084 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
17085 prop_type);
17086 if (!stride_ok)
17087 {
17088 complaint (_("unable to read array DW_AT_byte_stride "
17089 " - DIE at %s [in module %s]"),
17090 sect_offset_str (die->sect_off),
17091 objfile_name (cu->per_objfile->objfile));
17092 /* Ignore this attribute. We will likely not be able to print
17093 arrays of this type correctly, but there is little we can do
17094 to help if we cannot read the attribute's value. */
17095 byte_stride_prop = NULL;
17096 }
17097 }
17098
17099 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
17100 if (attr != NULL)
17101 bit_stride = attr->constant_value (0);
17102
17103 /* Irix 6.2 native cc creates array types without children for
17104 arrays with unspecified length. */
17105 if (die->child == NULL)
17106 {
17107 index_type = objfile_type (objfile)->builtin_int;
17108 range_type = create_static_range_type (NULL, index_type, 0, -1);
17109 type = create_array_type_with_stride (NULL, element_type, range_type,
17110 byte_stride_prop, bit_stride);
17111 return set_die_type (die, type, cu);
17112 }
17113
17114 std::vector<struct type *> range_types;
17115 child_die = die->child;
17116 while (child_die && child_die->tag)
17117 {
17118 if (child_die->tag == DW_TAG_subrange_type)
17119 {
17120 struct type *child_type = read_type_die (child_die, cu);
17121
17122 if (child_type != NULL)
17123 {
17124 /* The range type was succesfully read. Save it for the
17125 array type creation. */
17126 range_types.push_back (child_type);
17127 }
17128 }
17129 child_die = child_die->sibling;
17130 }
17131
17132 if (range_types.empty ())
17133 {
17134 complaint (_("unable to find array range - DIE at %s [in module %s]"),
17135 sect_offset_str (die->sect_off),
17136 objfile_name (cu->per_objfile->objfile));
17137 return NULL;
17138 }
17139
17140 /* Dwarf2 dimensions are output from left to right, create the
17141 necessary array types in backwards order. */
17142
17143 type = element_type;
17144
17145 if (read_array_order (die, cu) == DW_ORD_col_major)
17146 {
17147 int i = 0;
17148
17149 while (i < range_types.size ())
17150 {
17151 type = create_array_type_with_stride (NULL, type, range_types[i++],
17152 byte_stride_prop, bit_stride);
17153 bit_stride = 0;
17154 byte_stride_prop = nullptr;
17155 }
17156 }
17157 else
17158 {
17159 size_t ndim = range_types.size ();
17160 while (ndim-- > 0)
17161 {
17162 type = create_array_type_with_stride (NULL, type, range_types[ndim],
17163 byte_stride_prop, bit_stride);
17164 bit_stride = 0;
17165 byte_stride_prop = nullptr;
17166 }
17167 }
17168
17169 gdb_assert (type != element_type);
17170
17171 /* Understand Dwarf2 support for vector types (like they occur on
17172 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
17173 array type. This is not part of the Dwarf2/3 standard yet, but a
17174 custom vendor extension. The main difference between a regular
17175 array and the vector variant is that vectors are passed by value
17176 to functions. */
17177 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
17178 if (attr != nullptr)
17179 make_vector_type (type);
17180
17181 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
17182 implementation may choose to implement triple vectors using this
17183 attribute. */
17184 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17185 if (attr != nullptr && attr->form_is_unsigned ())
17186 {
17187 if (attr->as_unsigned () >= TYPE_LENGTH (type))
17188 TYPE_LENGTH (type) = attr->as_unsigned ();
17189 else
17190 complaint (_("DW_AT_byte_size for array type smaller "
17191 "than the total size of elements"));
17192 }
17193
17194 name = dwarf2_name (die, cu);
17195 if (name)
17196 type->set_name (name);
17197
17198 maybe_set_alignment (cu, die, type);
17199
17200 struct type *replacement_type = nullptr;
17201 if (cu->language == language_ada)
17202 {
17203 replacement_type = quirk_ada_thick_pointer (die, cu, type);
17204 if (replacement_type != nullptr)
17205 type = replacement_type;
17206 }
17207
17208 /* Install the type in the die. */
17209 set_die_type (die, type, cu, replacement_type != nullptr);
17210
17211 /* set_die_type should be already done. */
17212 set_descriptive_type (type, die, cu);
17213
17214 return type;
17215 }
17216
17217 static enum dwarf_array_dim_ordering
17218 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
17219 {
17220 struct attribute *attr;
17221
17222 attr = dwarf2_attr (die, DW_AT_ordering, cu);
17223
17224 if (attr != nullptr)
17225 {
17226 LONGEST val = attr->constant_value (-1);
17227 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
17228 return (enum dwarf_array_dim_ordering) val;
17229 }
17230
17231 /* GNU F77 is a special case, as at 08/2004 array type info is the
17232 opposite order to the dwarf2 specification, but data is still
17233 laid out as per normal fortran.
17234
17235 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
17236 version checking. */
17237
17238 if (cu->language == language_fortran
17239 && cu->producer && strstr (cu->producer, "GNU F77"))
17240 {
17241 return DW_ORD_row_major;
17242 }
17243
17244 switch (cu->language_defn->array_ordering ())
17245 {
17246 case array_column_major:
17247 return DW_ORD_col_major;
17248 case array_row_major:
17249 default:
17250 return DW_ORD_row_major;
17251 };
17252 }
17253
17254 /* Extract all information from a DW_TAG_set_type DIE and put it in
17255 the DIE's type field. */
17256
17257 static struct type *
17258 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
17259 {
17260 struct type *domain_type, *set_type;
17261 struct attribute *attr;
17262
17263 domain_type = die_type (die, cu);
17264
17265 /* The die_type call above may have already set the type for this DIE. */
17266 set_type = get_die_type (die, cu);
17267 if (set_type)
17268 return set_type;
17269
17270 set_type = create_set_type (NULL, domain_type);
17271
17272 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17273 if (attr != nullptr && attr->form_is_unsigned ())
17274 TYPE_LENGTH (set_type) = attr->as_unsigned ();
17275
17276 maybe_set_alignment (cu, die, set_type);
17277
17278 return set_die_type (die, set_type, cu);
17279 }
17280
17281 /* A helper for read_common_block that creates a locexpr baton.
17282 SYM is the symbol which we are marking as computed.
17283 COMMON_DIE is the DIE for the common block.
17284 COMMON_LOC is the location expression attribute for the common
17285 block itself.
17286 MEMBER_LOC is the location expression attribute for the particular
17287 member of the common block that we are processing.
17288 CU is the CU from which the above come. */
17289
17290 static void
17291 mark_common_block_symbol_computed (struct symbol *sym,
17292 struct die_info *common_die,
17293 struct attribute *common_loc,
17294 struct attribute *member_loc,
17295 struct dwarf2_cu *cu)
17296 {
17297 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17298 struct objfile *objfile = per_objfile->objfile;
17299 struct dwarf2_locexpr_baton *baton;
17300 gdb_byte *ptr;
17301 unsigned int cu_off;
17302 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
17303 LONGEST offset = 0;
17304
17305 gdb_assert (common_loc && member_loc);
17306 gdb_assert (common_loc->form_is_block ());
17307 gdb_assert (member_loc->form_is_block ()
17308 || member_loc->form_is_constant ());
17309
17310 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
17311 baton->per_objfile = per_objfile;
17312 baton->per_cu = cu->per_cu;
17313 gdb_assert (baton->per_cu);
17314
17315 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17316
17317 if (member_loc->form_is_constant ())
17318 {
17319 offset = member_loc->constant_value (0);
17320 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17321 }
17322 else
17323 baton->size += member_loc->as_block ()->size;
17324
17325 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
17326 baton->data = ptr;
17327
17328 *ptr++ = DW_OP_call4;
17329 cu_off = common_die->sect_off - cu->per_cu->sect_off;
17330 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17331 ptr += 4;
17332
17333 if (member_loc->form_is_constant ())
17334 {
17335 *ptr++ = DW_OP_addr;
17336 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17337 ptr += cu->header.addr_size;
17338 }
17339 else
17340 {
17341 /* We have to copy the data here, because DW_OP_call4 will only
17342 use a DW_AT_location attribute. */
17343 struct dwarf_block *block = member_loc->as_block ();
17344 memcpy (ptr, block->data, block->size);
17345 ptr += block->size;
17346 }
17347
17348 *ptr++ = DW_OP_plus;
17349 gdb_assert (ptr - baton->data == baton->size);
17350
17351 SYMBOL_LOCATION_BATON (sym) = baton;
17352 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
17353 }
17354
17355 /* Create appropriate locally-scoped variables for all the
17356 DW_TAG_common_block entries. Also create a struct common_block
17357 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
17358 is used to separate the common blocks name namespace from regular
17359 variable names. */
17360
17361 static void
17362 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
17363 {
17364 struct attribute *attr;
17365
17366 attr = dwarf2_attr (die, DW_AT_location, cu);
17367 if (attr != nullptr)
17368 {
17369 /* Support the .debug_loc offsets. */
17370 if (attr->form_is_block ())
17371 {
17372 /* Ok. */
17373 }
17374 else if (attr->form_is_section_offset ())
17375 {
17376 dwarf2_complex_location_expr_complaint ();
17377 attr = NULL;
17378 }
17379 else
17380 {
17381 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17382 "common block member");
17383 attr = NULL;
17384 }
17385 }
17386
17387 if (die->child != NULL)
17388 {
17389 struct objfile *objfile = cu->per_objfile->objfile;
17390 struct die_info *child_die;
17391 size_t n_entries = 0, size;
17392 struct common_block *common_block;
17393 struct symbol *sym;
17394
17395 for (child_die = die->child;
17396 child_die && child_die->tag;
17397 child_die = child_die->sibling)
17398 ++n_entries;
17399
17400 size = (sizeof (struct common_block)
17401 + (n_entries - 1) * sizeof (struct symbol *));
17402 common_block
17403 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17404 size);
17405 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17406 common_block->n_entries = 0;
17407
17408 for (child_die = die->child;
17409 child_die && child_die->tag;
17410 child_die = child_die->sibling)
17411 {
17412 /* Create the symbol in the DW_TAG_common_block block in the current
17413 symbol scope. */
17414 sym = new_symbol (child_die, NULL, cu);
17415 if (sym != NULL)
17416 {
17417 struct attribute *member_loc;
17418
17419 common_block->contents[common_block->n_entries++] = sym;
17420
17421 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17422 cu);
17423 if (member_loc)
17424 {
17425 /* GDB has handled this for a long time, but it is
17426 not specified by DWARF. It seems to have been
17427 emitted by gfortran at least as recently as:
17428 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17429 complaint (_("Variable in common block has "
17430 "DW_AT_data_member_location "
17431 "- DIE at %s [in module %s]"),
17432 sect_offset_str (child_die->sect_off),
17433 objfile_name (objfile));
17434
17435 if (member_loc->form_is_section_offset ())
17436 dwarf2_complex_location_expr_complaint ();
17437 else if (member_loc->form_is_constant ()
17438 || member_loc->form_is_block ())
17439 {
17440 if (attr != nullptr)
17441 mark_common_block_symbol_computed (sym, die, attr,
17442 member_loc, cu);
17443 }
17444 else
17445 dwarf2_complex_location_expr_complaint ();
17446 }
17447 }
17448 }
17449
17450 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17451 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
17452 }
17453 }
17454
17455 /* Create a type for a C++ namespace. */
17456
17457 static struct type *
17458 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
17459 {
17460 struct objfile *objfile = cu->per_objfile->objfile;
17461 const char *previous_prefix, *name;
17462 int is_anonymous;
17463 struct type *type;
17464
17465 /* For extensions, reuse the type of the original namespace. */
17466 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17467 {
17468 struct die_info *ext_die;
17469 struct dwarf2_cu *ext_cu = cu;
17470
17471 ext_die = dwarf2_extension (die, &ext_cu);
17472 type = read_type_die (ext_die, ext_cu);
17473
17474 /* EXT_CU may not be the same as CU.
17475 Ensure TYPE is recorded with CU in die_type_hash. */
17476 return set_die_type (die, type, cu);
17477 }
17478
17479 name = namespace_name (die, &is_anonymous, cu);
17480
17481 /* Now build the name of the current namespace. */
17482
17483 previous_prefix = determine_prefix (die, cu);
17484 if (previous_prefix[0] != '\0')
17485 name = typename_concat (&objfile->objfile_obstack,
17486 previous_prefix, name, 0, cu);
17487
17488 /* Create the type. */
17489 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17490
17491 return set_die_type (die, type, cu);
17492 }
17493
17494 /* Read a namespace scope. */
17495
17496 static void
17497 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17498 {
17499 struct objfile *objfile = cu->per_objfile->objfile;
17500 int is_anonymous;
17501
17502 /* Add a symbol associated to this if we haven't seen the namespace
17503 before. Also, add a using directive if it's an anonymous
17504 namespace. */
17505
17506 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17507 {
17508 struct type *type;
17509
17510 type = read_type_die (die, cu);
17511 new_symbol (die, type, cu);
17512
17513 namespace_name (die, &is_anonymous, cu);
17514 if (is_anonymous)
17515 {
17516 const char *previous_prefix = determine_prefix (die, cu);
17517
17518 std::vector<const char *> excludes;
17519 add_using_directive (using_directives (cu),
17520 previous_prefix, type->name (), NULL,
17521 NULL, excludes, 0, &objfile->objfile_obstack);
17522 }
17523 }
17524
17525 if (die->child != NULL)
17526 {
17527 struct die_info *child_die = die->child;
17528
17529 while (child_die && child_die->tag)
17530 {
17531 process_die (child_die, cu);
17532 child_die = child_die->sibling;
17533 }
17534 }
17535 }
17536
17537 /* Read a Fortran module as type. This DIE can be only a declaration used for
17538 imported module. Still we need that type as local Fortran "use ... only"
17539 declaration imports depend on the created type in determine_prefix. */
17540
17541 static struct type *
17542 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17543 {
17544 struct objfile *objfile = cu->per_objfile->objfile;
17545 const char *module_name;
17546 struct type *type;
17547
17548 module_name = dwarf2_name (die, cu);
17549 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17550
17551 return set_die_type (die, type, cu);
17552 }
17553
17554 /* Read a Fortran module. */
17555
17556 static void
17557 read_module (struct die_info *die, struct dwarf2_cu *cu)
17558 {
17559 struct die_info *child_die = die->child;
17560 struct type *type;
17561
17562 type = read_type_die (die, cu);
17563 new_symbol (die, type, cu);
17564
17565 while (child_die && child_die->tag)
17566 {
17567 process_die (child_die, cu);
17568 child_die = child_die->sibling;
17569 }
17570 }
17571
17572 /* Return the name of the namespace represented by DIE. Set
17573 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17574 namespace. */
17575
17576 static const char *
17577 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17578 {
17579 struct die_info *current_die;
17580 const char *name = NULL;
17581
17582 /* Loop through the extensions until we find a name. */
17583
17584 for (current_die = die;
17585 current_die != NULL;
17586 current_die = dwarf2_extension (die, &cu))
17587 {
17588 /* We don't use dwarf2_name here so that we can detect the absence
17589 of a name -> anonymous namespace. */
17590 name = dwarf2_string_attr (die, DW_AT_name, cu);
17591
17592 if (name != NULL)
17593 break;
17594 }
17595
17596 /* Is it an anonymous namespace? */
17597
17598 *is_anonymous = (name == NULL);
17599 if (*is_anonymous)
17600 name = CP_ANONYMOUS_NAMESPACE_STR;
17601
17602 return name;
17603 }
17604
17605 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17606 the user defined type vector. */
17607
17608 static struct type *
17609 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17610 {
17611 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
17612 struct comp_unit_head *cu_header = &cu->header;
17613 struct type *type;
17614 struct attribute *attr_byte_size;
17615 struct attribute *attr_address_class;
17616 int byte_size, addr_class;
17617 struct type *target_type;
17618
17619 target_type = die_type (die, cu);
17620
17621 /* The die_type call above may have already set the type for this DIE. */
17622 type = get_die_type (die, cu);
17623 if (type)
17624 return type;
17625
17626 type = lookup_pointer_type (target_type);
17627
17628 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17629 if (attr_byte_size)
17630 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
17631 else
17632 byte_size = cu_header->addr_size;
17633
17634 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17635 if (attr_address_class)
17636 addr_class = attr_address_class->constant_value (DW_ADDR_none);
17637 else
17638 addr_class = DW_ADDR_none;
17639
17640 ULONGEST alignment = get_alignment (cu, die);
17641
17642 /* If the pointer size, alignment, or address class is different
17643 than the default, create a type variant marked as such and set
17644 the length accordingly. */
17645 if (TYPE_LENGTH (type) != byte_size
17646 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17647 && alignment != TYPE_RAW_ALIGN (type))
17648 || addr_class != DW_ADDR_none)
17649 {
17650 if (gdbarch_address_class_type_flags_p (gdbarch))
17651 {
17652 type_instance_flags type_flags
17653 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17654 addr_class);
17655 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17656 == 0);
17657 type = make_type_with_address_space (type, type_flags);
17658 }
17659 else if (TYPE_LENGTH (type) != byte_size)
17660 {
17661 complaint (_("invalid pointer size %d"), byte_size);
17662 }
17663 else if (TYPE_RAW_ALIGN (type) != alignment)
17664 {
17665 complaint (_("Invalid DW_AT_alignment"
17666 " - DIE at %s [in module %s]"),
17667 sect_offset_str (die->sect_off),
17668 objfile_name (cu->per_objfile->objfile));
17669 }
17670 else
17671 {
17672 /* Should we also complain about unhandled address classes? */
17673 }
17674 }
17675
17676 TYPE_LENGTH (type) = byte_size;
17677 set_type_align (type, alignment);
17678 return set_die_type (die, type, cu);
17679 }
17680
17681 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17682 the user defined type vector. */
17683
17684 static struct type *
17685 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17686 {
17687 struct type *type;
17688 struct type *to_type;
17689 struct type *domain;
17690
17691 to_type = die_type (die, cu);
17692 domain = die_containing_type (die, cu);
17693
17694 /* The calls above may have already set the type for this DIE. */
17695 type = get_die_type (die, cu);
17696 if (type)
17697 return type;
17698
17699 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17700 type = lookup_methodptr_type (to_type);
17701 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17702 {
17703 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17704
17705 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17706 to_type->fields (), to_type->num_fields (),
17707 to_type->has_varargs ());
17708 type = lookup_methodptr_type (new_type);
17709 }
17710 else
17711 type = lookup_memberptr_type (to_type, domain);
17712
17713 return set_die_type (die, type, cu);
17714 }
17715
17716 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17717 the user defined type vector. */
17718
17719 static struct type *
17720 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17721 enum type_code refcode)
17722 {
17723 struct comp_unit_head *cu_header = &cu->header;
17724 struct type *type, *target_type;
17725 struct attribute *attr;
17726
17727 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17728
17729 target_type = die_type (die, cu);
17730
17731 /* The die_type call above may have already set the type for this DIE. */
17732 type = get_die_type (die, cu);
17733 if (type)
17734 return type;
17735
17736 type = lookup_reference_type (target_type, refcode);
17737 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17738 if (attr != nullptr)
17739 {
17740 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
17741 }
17742 else
17743 {
17744 TYPE_LENGTH (type) = cu_header->addr_size;
17745 }
17746 maybe_set_alignment (cu, die, type);
17747 return set_die_type (die, type, cu);
17748 }
17749
17750 /* Add the given cv-qualifiers to the element type of the array. GCC
17751 outputs DWARF type qualifiers that apply to an array, not the
17752 element type. But GDB relies on the array element type to carry
17753 the cv-qualifiers. This mimics section 6.7.3 of the C99
17754 specification. */
17755
17756 static struct type *
17757 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17758 struct type *base_type, int cnst, int voltl)
17759 {
17760 struct type *el_type, *inner_array;
17761
17762 base_type = copy_type (base_type);
17763 inner_array = base_type;
17764
17765 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17766 {
17767 TYPE_TARGET_TYPE (inner_array) =
17768 copy_type (TYPE_TARGET_TYPE (inner_array));
17769 inner_array = TYPE_TARGET_TYPE (inner_array);
17770 }
17771
17772 el_type = TYPE_TARGET_TYPE (inner_array);
17773 cnst |= TYPE_CONST (el_type);
17774 voltl |= TYPE_VOLATILE (el_type);
17775 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17776
17777 return set_die_type (die, base_type, cu);
17778 }
17779
17780 static struct type *
17781 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17782 {
17783 struct type *base_type, *cv_type;
17784
17785 base_type = die_type (die, cu);
17786
17787 /* The die_type call above may have already set the type for this DIE. */
17788 cv_type = get_die_type (die, cu);
17789 if (cv_type)
17790 return cv_type;
17791
17792 /* In case the const qualifier is applied to an array type, the element type
17793 is so qualified, not the array type (section 6.7.3 of C99). */
17794 if (base_type->code () == TYPE_CODE_ARRAY)
17795 return add_array_cv_type (die, cu, base_type, 1, 0);
17796
17797 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17798 return set_die_type (die, cv_type, cu);
17799 }
17800
17801 static struct type *
17802 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17803 {
17804 struct type *base_type, *cv_type;
17805
17806 base_type = die_type (die, cu);
17807
17808 /* The die_type call above may have already set the type for this DIE. */
17809 cv_type = get_die_type (die, cu);
17810 if (cv_type)
17811 return cv_type;
17812
17813 /* In case the volatile qualifier is applied to an array type, the
17814 element type is so qualified, not the array type (section 6.7.3
17815 of C99). */
17816 if (base_type->code () == TYPE_CODE_ARRAY)
17817 return add_array_cv_type (die, cu, base_type, 0, 1);
17818
17819 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17820 return set_die_type (die, cv_type, cu);
17821 }
17822
17823 /* Handle DW_TAG_restrict_type. */
17824
17825 static struct type *
17826 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17827 {
17828 struct type *base_type, *cv_type;
17829
17830 base_type = die_type (die, cu);
17831
17832 /* The die_type call above may have already set the type for this DIE. */
17833 cv_type = get_die_type (die, cu);
17834 if (cv_type)
17835 return cv_type;
17836
17837 cv_type = make_restrict_type (base_type);
17838 return set_die_type (die, cv_type, cu);
17839 }
17840
17841 /* Handle DW_TAG_atomic_type. */
17842
17843 static struct type *
17844 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17845 {
17846 struct type *base_type, *cv_type;
17847
17848 base_type = die_type (die, cu);
17849
17850 /* The die_type call above may have already set the type for this DIE. */
17851 cv_type = get_die_type (die, cu);
17852 if (cv_type)
17853 return cv_type;
17854
17855 cv_type = make_atomic_type (base_type);
17856 return set_die_type (die, cv_type, cu);
17857 }
17858
17859 /* Extract all information from a DW_TAG_string_type DIE and add to
17860 the user defined type vector. It isn't really a user defined type,
17861 but it behaves like one, with other DIE's using an AT_user_def_type
17862 attribute to reference it. */
17863
17864 static struct type *
17865 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17866 {
17867 struct objfile *objfile = cu->per_objfile->objfile;
17868 struct gdbarch *gdbarch = objfile->arch ();
17869 struct type *type, *range_type, *index_type, *char_type;
17870 struct attribute *attr;
17871 struct dynamic_prop prop;
17872 bool length_is_constant = true;
17873 LONGEST length;
17874
17875 /* There are a couple of places where bit sizes might be made use of
17876 when parsing a DW_TAG_string_type, however, no producer that we know
17877 of make use of these. Handling bit sizes that are a multiple of the
17878 byte size is easy enough, but what about other bit sizes? Lets deal
17879 with that problem when we have to. Warn about these attributes being
17880 unsupported, then parse the type and ignore them like we always
17881 have. */
17882 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17883 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17884 {
17885 static bool warning_printed = false;
17886 if (!warning_printed)
17887 {
17888 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17889 "currently supported on DW_TAG_string_type."));
17890 warning_printed = true;
17891 }
17892 }
17893
17894 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17895 if (attr != nullptr && !attr->form_is_constant ())
17896 {
17897 /* The string length describes the location at which the length of
17898 the string can be found. The size of the length field can be
17899 specified with one of the attributes below. */
17900 struct type *prop_type;
17901 struct attribute *len
17902 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17903 if (len == nullptr)
17904 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17905 if (len != nullptr && len->form_is_constant ())
17906 {
17907 /* Pass 0 as the default as we know this attribute is constant
17908 and the default value will not be returned. */
17909 LONGEST sz = len->constant_value (0);
17910 prop_type = cu->per_objfile->int_type (sz, true);
17911 }
17912 else
17913 {
17914 /* If the size is not specified then we assume it is the size of
17915 an address on this target. */
17916 prop_type = cu->addr_sized_int_type (true);
17917 }
17918
17919 /* Convert the attribute into a dynamic property. */
17920 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17921 length = 1;
17922 else
17923 length_is_constant = false;
17924 }
17925 else if (attr != nullptr)
17926 {
17927 /* This DW_AT_string_length just contains the length with no
17928 indirection. There's no need to create a dynamic property in this
17929 case. Pass 0 for the default value as we know it will not be
17930 returned in this case. */
17931 length = attr->constant_value (0);
17932 }
17933 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17934 {
17935 /* We don't currently support non-constant byte sizes for strings. */
17936 length = attr->constant_value (1);
17937 }
17938 else
17939 {
17940 /* Use 1 as a fallback length if we have nothing else. */
17941 length = 1;
17942 }
17943
17944 index_type = objfile_type (objfile)->builtin_int;
17945 if (length_is_constant)
17946 range_type = create_static_range_type (NULL, index_type, 1, length);
17947 else
17948 {
17949 struct dynamic_prop low_bound;
17950
17951 low_bound.set_const_val (1);
17952 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17953 }
17954 char_type = language_string_char_type (cu->language_defn, gdbarch);
17955 type = create_string_type (NULL, char_type, range_type);
17956
17957 return set_die_type (die, type, cu);
17958 }
17959
17960 /* Assuming that DIE corresponds to a function, returns nonzero
17961 if the function is prototyped. */
17962
17963 static int
17964 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17965 {
17966 struct attribute *attr;
17967
17968 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17969 if (attr && attr->as_boolean ())
17970 return 1;
17971
17972 /* The DWARF standard implies that the DW_AT_prototyped attribute
17973 is only meaningful for C, but the concept also extends to other
17974 languages that allow unprototyped functions (Eg: Objective C).
17975 For all other languages, assume that functions are always
17976 prototyped. */
17977 if (cu->language != language_c
17978 && cu->language != language_objc
17979 && cu->language != language_opencl)
17980 return 1;
17981
17982 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17983 prototyped and unprototyped functions; default to prototyped,
17984 since that is more common in modern code (and RealView warns
17985 about unprototyped functions). */
17986 if (producer_is_realview (cu->producer))
17987 return 1;
17988
17989 return 0;
17990 }
17991
17992 /* Handle DIES due to C code like:
17993
17994 struct foo
17995 {
17996 int (*funcp)(int a, long l);
17997 int b;
17998 };
17999
18000 ('funcp' generates a DW_TAG_subroutine_type DIE). */
18001
18002 static struct type *
18003 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
18004 {
18005 struct objfile *objfile = cu->per_objfile->objfile;
18006 struct type *type; /* Type that this function returns. */
18007 struct type *ftype; /* Function that returns above type. */
18008 struct attribute *attr;
18009
18010 type = die_type (die, cu);
18011
18012 /* The die_type call above may have already set the type for this DIE. */
18013 ftype = get_die_type (die, cu);
18014 if (ftype)
18015 return ftype;
18016
18017 ftype = lookup_function_type (type);
18018
18019 if (prototyped_function_p (die, cu))
18020 ftype->set_is_prototyped (true);
18021
18022 /* Store the calling convention in the type if it's available in
18023 the subroutine die. Otherwise set the calling convention to
18024 the default value DW_CC_normal. */
18025 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
18026 if (attr != nullptr
18027 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
18028 TYPE_CALLING_CONVENTION (ftype)
18029 = (enum dwarf_calling_convention) attr->constant_value (0);
18030 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
18031 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
18032 else
18033 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
18034
18035 /* Record whether the function returns normally to its caller or not
18036 if the DWARF producer set that information. */
18037 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
18038 if (attr && attr->as_boolean ())
18039 TYPE_NO_RETURN (ftype) = 1;
18040
18041 /* We need to add the subroutine type to the die immediately so
18042 we don't infinitely recurse when dealing with parameters
18043 declared as the same subroutine type. */
18044 set_die_type (die, ftype, cu);
18045
18046 if (die->child != NULL)
18047 {
18048 struct type *void_type = objfile_type (objfile)->builtin_void;
18049 struct die_info *child_die;
18050 int nparams, iparams;
18051
18052 /* Count the number of parameters.
18053 FIXME: GDB currently ignores vararg functions, but knows about
18054 vararg member functions. */
18055 nparams = 0;
18056 child_die = die->child;
18057 while (child_die && child_die->tag)
18058 {
18059 if (child_die->tag == DW_TAG_formal_parameter)
18060 nparams++;
18061 else if (child_die->tag == DW_TAG_unspecified_parameters)
18062 ftype->set_has_varargs (true);
18063
18064 child_die = child_die->sibling;
18065 }
18066
18067 /* Allocate storage for parameters and fill them in. */
18068 ftype->set_num_fields (nparams);
18069 ftype->set_fields
18070 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
18071
18072 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
18073 even if we error out during the parameters reading below. */
18074 for (iparams = 0; iparams < nparams; iparams++)
18075 ftype->field (iparams).set_type (void_type);
18076
18077 iparams = 0;
18078 child_die = die->child;
18079 while (child_die && child_die->tag)
18080 {
18081 if (child_die->tag == DW_TAG_formal_parameter)
18082 {
18083 struct type *arg_type;
18084
18085 /* DWARF version 2 has no clean way to discern C++
18086 static and non-static member functions. G++ helps
18087 GDB by marking the first parameter for non-static
18088 member functions (which is the this pointer) as
18089 artificial. We pass this information to
18090 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
18091
18092 DWARF version 3 added DW_AT_object_pointer, which GCC
18093 4.5 does not yet generate. */
18094 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
18095 if (attr != nullptr)
18096 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
18097 else
18098 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
18099 arg_type = die_type (child_die, cu);
18100
18101 /* RealView does not mark THIS as const, which the testsuite
18102 expects. GCC marks THIS as const in method definitions,
18103 but not in the class specifications (GCC PR 43053). */
18104 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
18105 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
18106 {
18107 int is_this = 0;
18108 struct dwarf2_cu *arg_cu = cu;
18109 const char *name = dwarf2_name (child_die, cu);
18110
18111 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
18112 if (attr != nullptr)
18113 {
18114 /* If the compiler emits this, use it. */
18115 if (follow_die_ref (die, attr, &arg_cu) == child_die)
18116 is_this = 1;
18117 }
18118 else if (name && strcmp (name, "this") == 0)
18119 /* Function definitions will have the argument names. */
18120 is_this = 1;
18121 else if (name == NULL && iparams == 0)
18122 /* Declarations may not have the names, so like
18123 elsewhere in GDB, assume an artificial first
18124 argument is "this". */
18125 is_this = 1;
18126
18127 if (is_this)
18128 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
18129 arg_type, 0);
18130 }
18131
18132 ftype->field (iparams).set_type (arg_type);
18133 iparams++;
18134 }
18135 child_die = child_die->sibling;
18136 }
18137 }
18138
18139 return ftype;
18140 }
18141
18142 static struct type *
18143 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
18144 {
18145 struct objfile *objfile = cu->per_objfile->objfile;
18146 const char *name = NULL;
18147 struct type *this_type, *target_type;
18148
18149 name = dwarf2_full_name (NULL, die, cu);
18150 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
18151 this_type->set_target_is_stub (true);
18152 set_die_type (die, this_type, cu);
18153 target_type = die_type (die, cu);
18154 if (target_type != this_type)
18155 TYPE_TARGET_TYPE (this_type) = target_type;
18156 else
18157 {
18158 /* Self-referential typedefs are, it seems, not allowed by the DWARF
18159 spec and cause infinite loops in GDB. */
18160 complaint (_("Self-referential DW_TAG_typedef "
18161 "- DIE at %s [in module %s]"),
18162 sect_offset_str (die->sect_off), objfile_name (objfile));
18163 TYPE_TARGET_TYPE (this_type) = NULL;
18164 }
18165 if (name == NULL)
18166 {
18167 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
18168 anonymous typedefs, which is, strictly speaking, invalid DWARF.
18169 Handle these by just returning the target type, rather than
18170 constructing an anonymous typedef type and trying to handle this
18171 elsewhere. */
18172 set_die_type (die, target_type, cu);
18173 return target_type;
18174 }
18175 return this_type;
18176 }
18177
18178 /* Helper for get_dwarf2_rational_constant that computes the value of
18179 a given gmp_mpz given an attribute. */
18180
18181 static void
18182 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
18183 {
18184 /* GCC will sometimes emit a 16-byte constant value as a DWARF
18185 location expression that pushes an implicit value. */
18186 if (attr->form == DW_FORM_exprloc)
18187 {
18188 dwarf_block *blk = attr->as_block ();
18189 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
18190 {
18191 uint64_t len;
18192 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
18193 blk->data + blk->size,
18194 &len);
18195 if (ptr - blk->data + len <= blk->size)
18196 {
18197 mpz_import (value->val, len,
18198 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18199 1, 0, 0, ptr);
18200 return;
18201 }
18202 }
18203
18204 /* On failure set it to 1. */
18205 *value = gdb_mpz (1);
18206 }
18207 else if (attr->form_is_block ())
18208 {
18209 dwarf_block *blk = attr->as_block ();
18210 mpz_import (value->val, blk->size,
18211 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18212 1, 0, 0, blk->data);
18213 }
18214 else
18215 *value = gdb_mpz (attr->constant_value (1));
18216 }
18217
18218 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
18219 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
18220
18221 If the numerator and/or numerator attribute is missing,
18222 a complaint is filed, and NUMERATOR and DENOMINATOR are left
18223 untouched. */
18224
18225 static void
18226 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
18227 gdb_mpz *numerator, gdb_mpz *denominator)
18228 {
18229 struct attribute *num_attr, *denom_attr;
18230
18231 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
18232 if (num_attr == nullptr)
18233 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
18234 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18235
18236 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
18237 if (denom_attr == nullptr)
18238 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
18239 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18240
18241 if (num_attr == nullptr || denom_attr == nullptr)
18242 return;
18243
18244 get_mpz (cu, numerator, num_attr);
18245 get_mpz (cu, denominator, denom_attr);
18246 }
18247
18248 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
18249 rational constant, rather than a signed one.
18250
18251 If the rational constant has a negative value, a complaint
18252 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
18253
18254 static void
18255 get_dwarf2_unsigned_rational_constant (struct die_info *die,
18256 struct dwarf2_cu *cu,
18257 gdb_mpz *numerator,
18258 gdb_mpz *denominator)
18259 {
18260 gdb_mpz num (1);
18261 gdb_mpz denom (1);
18262
18263 get_dwarf2_rational_constant (die, cu, &num, &denom);
18264 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
18265 {
18266 mpz_neg (num.val, num.val);
18267 mpz_neg (denom.val, denom.val);
18268 }
18269 else if (mpz_sgn (num.val) == -1)
18270 {
18271 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
18272 " in DIE at %s"),
18273 sect_offset_str (die->sect_off));
18274 return;
18275 }
18276 else if (mpz_sgn (denom.val) == -1)
18277 {
18278 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
18279 " in DIE at %s"),
18280 sect_offset_str (die->sect_off));
18281 return;
18282 }
18283
18284 *numerator = std::move (num);
18285 *denominator = std::move (denom);
18286 }
18287
18288 /* Assuming that ENCODING is a string whose contents starting at the
18289 K'th character is "_nn" where "nn" is a decimal number, scan that
18290 number and set RESULT to the value. K is updated to point to the
18291 character immediately following the number.
18292
18293 If the string does not conform to the format described above, false
18294 is returned, and K may or may not be changed. */
18295
18296 static bool
18297 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
18298 {
18299 /* The next character should be an underscore ('_') followed
18300 by a digit. */
18301 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
18302 return false;
18303
18304 /* Skip the underscore. */
18305 k++;
18306 int start = k;
18307
18308 /* Determine the number of digits for our number. */
18309 while (isdigit (encoding[k]))
18310 k++;
18311 if (k == start)
18312 return false;
18313
18314 std::string copy (&encoding[start], k - start);
18315 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
18316 return false;
18317
18318 return true;
18319 }
18320
18321 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
18322 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
18323 DENOM, update OFFSET, and return true on success. Return false on
18324 failure. */
18325
18326 static bool
18327 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
18328 gdb_mpz *num, gdb_mpz *denom)
18329 {
18330 if (!ada_get_gnat_encoded_number (encoding, offset, num))
18331 return false;
18332 return ada_get_gnat_encoded_number (encoding, offset, denom);
18333 }
18334
18335 /* Assuming DIE corresponds to a fixed point type, finish the creation
18336 of the corresponding TYPE by setting its type-specific data. CU is
18337 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
18338 encodings. It is nullptr if the GNAT encoding should be
18339 ignored. */
18340
18341 static void
18342 finish_fixed_point_type (struct type *type, const char *suffix,
18343 struct die_info *die, struct dwarf2_cu *cu)
18344 {
18345 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
18346 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
18347
18348 /* If GNAT encodings are preferred, don't examine the
18349 attributes. */
18350 struct attribute *attr = nullptr;
18351 if (suffix == nullptr)
18352 {
18353 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
18354 if (attr == nullptr)
18355 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
18356 if (attr == nullptr)
18357 attr = dwarf2_attr (die, DW_AT_small, cu);
18358 }
18359
18360 /* Numerator and denominator of our fixed-point type's scaling factor.
18361 The default is a scaling factor of 1, which we use as a fallback
18362 when we are not able to decode it (problem with the debugging info,
18363 unsupported forms, bug in GDB, etc...). Using that as the default
18364 allows us to at least print the unscaled value, which might still
18365 be useful to a user. */
18366 gdb_mpz scale_num (1);
18367 gdb_mpz scale_denom (1);
18368
18369 if (attr == nullptr)
18370 {
18371 int offset = 0;
18372 if (suffix != nullptr
18373 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18374 &scale_denom)
18375 /* The number might be encoded as _nn_dd_nn_dd, where the
18376 second ratio is the 'small value. In this situation, we
18377 want the second value. */
18378 && (suffix[offset] != '_'
18379 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18380 &scale_denom)))
18381 {
18382 /* Found it. */
18383 }
18384 else
18385 {
18386 /* Scaling factor not found. Assume a scaling factor of 1,
18387 and hope for the best. At least the user will be able to
18388 see the encoded value. */
18389 scale_num = 1;
18390 scale_denom = 1;
18391 complaint (_("no scale found for fixed-point type (DIE at %s)"),
18392 sect_offset_str (die->sect_off));
18393 }
18394 }
18395 else if (attr->name == DW_AT_binary_scale)
18396 {
18397 LONGEST scale_exp = attr->constant_value (0);
18398 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18399
18400 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
18401 }
18402 else if (attr->name == DW_AT_decimal_scale)
18403 {
18404 LONGEST scale_exp = attr->constant_value (0);
18405 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18406
18407 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
18408 }
18409 else if (attr->name == DW_AT_small)
18410 {
18411 struct die_info *scale_die;
18412 struct dwarf2_cu *scale_cu = cu;
18413
18414 scale_die = follow_die_ref (die, attr, &scale_cu);
18415 if (scale_die->tag == DW_TAG_constant)
18416 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18417 &scale_num, &scale_denom);
18418 else
18419 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18420 " (DIE at %s)"),
18421 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18422 }
18423 else
18424 {
18425 complaint (_("unsupported scale attribute %s for fixed-point type"
18426 " (DIE at %s)"),
18427 dwarf_attr_name (attr->name),
18428 sect_offset_str (die->sect_off));
18429 }
18430
18431 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
18432 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
18433 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
18434 mpq_canonicalize (scaling_factor.val);
18435 }
18436
18437 /* The gnat-encoding suffix for fixed point. */
18438
18439 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
18440
18441 /* If NAME encodes an Ada fixed-point type, return a pointer to the
18442 "XF" suffix of the name. The text after this is what encodes the
18443 'small and 'delta information. Otherwise, return nullptr. */
18444
18445 static const char *
18446 gnat_encoded_fixed_point_type_info (const char *name)
18447 {
18448 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
18449 }
18450
18451 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18452 (which may be different from NAME) to the architecture back-end to allow
18453 it to guess the correct format if necessary. */
18454
18455 static struct type *
18456 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
18457 const char *name_hint, enum bfd_endian byte_order)
18458 {
18459 struct gdbarch *gdbarch = objfile->arch ();
18460 const struct floatformat **format;
18461 struct type *type;
18462
18463 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18464 if (format)
18465 type = init_float_type (objfile, bits, name, format, byte_order);
18466 else
18467 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18468
18469 return type;
18470 }
18471
18472 /* Allocate an integer type of size BITS and name NAME. */
18473
18474 static struct type *
18475 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18476 int bits, int unsigned_p, const char *name)
18477 {
18478 struct type *type;
18479
18480 /* Versions of Intel's C Compiler generate an integer type called "void"
18481 instead of using DW_TAG_unspecified_type. This has been seen on
18482 at least versions 14, 17, and 18. */
18483 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18484 && strcmp (name, "void") == 0)
18485 type = objfile_type (objfile)->builtin_void;
18486 else
18487 type = init_integer_type (objfile, bits, unsigned_p, name);
18488
18489 return type;
18490 }
18491
18492 /* Return true if DIE has a DW_AT_small attribute whose value is
18493 a constant rational, where both the numerator and denominator
18494 are equal to zero.
18495
18496 CU is the DIE's Compilation Unit. */
18497
18498 static bool
18499 has_zero_over_zero_small_attribute (struct die_info *die,
18500 struct dwarf2_cu *cu)
18501 {
18502 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18503 if (attr == nullptr)
18504 return false;
18505
18506 struct dwarf2_cu *scale_cu = cu;
18507 struct die_info *scale_die
18508 = follow_die_ref (die, attr, &scale_cu);
18509
18510 if (scale_die->tag != DW_TAG_constant)
18511 return false;
18512
18513 gdb_mpz num (1), denom (1);
18514 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
18515 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
18516 }
18517
18518 /* Initialise and return a floating point type of size BITS suitable for
18519 use as a component of a complex number. The NAME_HINT is passed through
18520 when initialising the floating point type and is the name of the complex
18521 type.
18522
18523 As DWARF doesn't currently provide an explicit name for the components
18524 of a complex number, but it can be helpful to have these components
18525 named, we try to select a suitable name based on the size of the
18526 component. */
18527 static struct type *
18528 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18529 struct objfile *objfile,
18530 int bits, const char *name_hint,
18531 enum bfd_endian byte_order)
18532 {
18533 gdbarch *gdbarch = objfile->arch ();
18534 struct type *tt = nullptr;
18535
18536 /* Try to find a suitable floating point builtin type of size BITS.
18537 We're going to use the name of this type as the name for the complex
18538 target type that we are about to create. */
18539 switch (cu->language)
18540 {
18541 case language_fortran:
18542 switch (bits)
18543 {
18544 case 32:
18545 tt = builtin_f_type (gdbarch)->builtin_real;
18546 break;
18547 case 64:
18548 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18549 break;
18550 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18551 case 128:
18552 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18553 break;
18554 }
18555 break;
18556 default:
18557 switch (bits)
18558 {
18559 case 32:
18560 tt = builtin_type (gdbarch)->builtin_float;
18561 break;
18562 case 64:
18563 tt = builtin_type (gdbarch)->builtin_double;
18564 break;
18565 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18566 case 128:
18567 tt = builtin_type (gdbarch)->builtin_long_double;
18568 break;
18569 }
18570 break;
18571 }
18572
18573 /* If the type we found doesn't match the size we were looking for, then
18574 pretend we didn't find a type at all, the complex target type we
18575 create will then be nameless. */
18576 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
18577 tt = nullptr;
18578
18579 const char *name = (tt == nullptr) ? nullptr : tt->name ();
18580 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
18581 }
18582
18583 /* Find a representation of a given base type and install
18584 it in the TYPE field of the die. */
18585
18586 static struct type *
18587 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
18588 {
18589 struct objfile *objfile = cu->per_objfile->objfile;
18590 struct type *type;
18591 struct attribute *attr;
18592 int encoding = 0, bits = 0;
18593 const char *name;
18594 gdbarch *arch;
18595
18596 attr = dwarf2_attr (die, DW_AT_encoding, cu);
18597 if (attr != nullptr && attr->form_is_constant ())
18598 encoding = attr->constant_value (0);
18599 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18600 if (attr != nullptr)
18601 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
18602 name = dwarf2_name (die, cu);
18603 if (!name)
18604 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
18605
18606 arch = objfile->arch ();
18607 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18608
18609 attr = dwarf2_attr (die, DW_AT_endianity, cu);
18610 if (attr != nullptr && attr->form_is_constant ())
18611 {
18612 int endianity = attr->constant_value (0);
18613
18614 switch (endianity)
18615 {
18616 case DW_END_big:
18617 byte_order = BFD_ENDIAN_BIG;
18618 break;
18619 case DW_END_little:
18620 byte_order = BFD_ENDIAN_LITTLE;
18621 break;
18622 default:
18623 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18624 break;
18625 }
18626 }
18627
18628 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18629 && cu->language == language_ada
18630 && has_zero_over_zero_small_attribute (die, cu))
18631 {
18632 /* brobecker/2018-02-24: This is a fixed point type for which
18633 the scaling factor is represented as fraction whose value
18634 does not make sense (zero divided by zero), so we should
18635 normally never see these. However, there is a small category
18636 of fixed point types for which GNAT is unable to provide
18637 the scaling factor via the standard DWARF mechanisms, and
18638 for which the info is provided via the GNAT encodings instead.
18639 This is likely what this DIE is about. */
18640 encoding = (encoding == DW_ATE_signed_fixed
18641 ? DW_ATE_signed
18642 : DW_ATE_unsigned);
18643 }
18644
18645 /* With GNAT encodings, fixed-point information will be encoded in
18646 the type name. Note that this can also occur with the above
18647 zero-over-zero case, which is why this is a separate "if" rather
18648 than an "else if". */
18649 const char *gnat_encoding_suffix = nullptr;
18650 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18651 && cu->language == language_ada
18652 && name != nullptr)
18653 {
18654 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18655 if (gnat_encoding_suffix != nullptr)
18656 {
18657 gdb_assert (startswith (gnat_encoding_suffix,
18658 GNAT_FIXED_POINT_SUFFIX));
18659 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18660 name, gnat_encoding_suffix - name);
18661 /* Use -1 here so that SUFFIX points at the "_" after the
18662 "XF". */
18663 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18664
18665 encoding = (encoding == DW_ATE_signed
18666 ? DW_ATE_signed_fixed
18667 : DW_ATE_unsigned_fixed);
18668 }
18669 }
18670
18671 switch (encoding)
18672 {
18673 case DW_ATE_address:
18674 /* Turn DW_ATE_address into a void * pointer. */
18675 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
18676 type = init_pointer_type (objfile, bits, name, type);
18677 break;
18678 case DW_ATE_boolean:
18679 type = init_boolean_type (objfile, bits, 1, name);
18680 break;
18681 case DW_ATE_complex_float:
18682 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18683 byte_order);
18684 if (type->code () == TYPE_CODE_ERROR)
18685 {
18686 if (name == nullptr)
18687 {
18688 struct obstack *obstack
18689 = &cu->per_objfile->objfile->objfile_obstack;
18690 name = obconcat (obstack, "_Complex ", type->name (),
18691 nullptr);
18692 }
18693 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18694 }
18695 else
18696 type = init_complex_type (name, type);
18697 break;
18698 case DW_ATE_decimal_float:
18699 type = init_decfloat_type (objfile, bits, name);
18700 break;
18701 case DW_ATE_float:
18702 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
18703 break;
18704 case DW_ATE_signed:
18705 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18706 break;
18707 case DW_ATE_unsigned:
18708 if (cu->language == language_fortran
18709 && name
18710 && startswith (name, "character("))
18711 type = init_character_type (objfile, bits, 1, name);
18712 else
18713 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18714 break;
18715 case DW_ATE_signed_char:
18716 if (cu->language == language_ada || cu->language == language_m2
18717 || cu->language == language_pascal
18718 || cu->language == language_fortran)
18719 type = init_character_type (objfile, bits, 0, name);
18720 else
18721 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18722 break;
18723 case DW_ATE_unsigned_char:
18724 if (cu->language == language_ada || cu->language == language_m2
18725 || cu->language == language_pascal
18726 || cu->language == language_fortran
18727 || cu->language == language_rust)
18728 type = init_character_type (objfile, bits, 1, name);
18729 else
18730 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18731 break;
18732 case DW_ATE_UTF:
18733 {
18734 if (bits == 16)
18735 type = builtin_type (arch)->builtin_char16;
18736 else if (bits == 32)
18737 type = builtin_type (arch)->builtin_char32;
18738 else
18739 {
18740 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
18741 bits);
18742 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18743 }
18744 return set_die_type (die, type, cu);
18745 }
18746 break;
18747 case DW_ATE_signed_fixed:
18748 type = init_fixed_point_type (objfile, bits, 0, name);
18749 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18750 break;
18751 case DW_ATE_unsigned_fixed:
18752 type = init_fixed_point_type (objfile, bits, 1, name);
18753 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18754 break;
18755
18756 default:
18757 complaint (_("unsupported DW_AT_encoding: '%s'"),
18758 dwarf_type_encoding_name (encoding));
18759 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18760 break;
18761 }
18762
18763 if (name && strcmp (name, "char") == 0)
18764 type->set_has_no_signedness (true);
18765
18766 maybe_set_alignment (cu, die, type);
18767
18768 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18769
18770 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18771 {
18772 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18773 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
18774 {
18775 unsigned real_bit_size = attr->as_unsigned ();
18776 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18777 /* Only use the attributes if they make sense together. */
18778 if (attr == nullptr
18779 || (attr->as_unsigned () + real_bit_size
18780 <= 8 * TYPE_LENGTH (type)))
18781 {
18782 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18783 = real_bit_size;
18784 if (attr != nullptr)
18785 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18786 = attr->as_unsigned ();
18787 }
18788 }
18789 }
18790
18791 return set_die_type (die, type, cu);
18792 }
18793
18794 /* Parse dwarf attribute if it's a block, reference or constant and put the
18795 resulting value of the attribute into struct bound_prop.
18796 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18797
18798 static int
18799 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18800 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18801 struct type *default_type)
18802 {
18803 struct dwarf2_property_baton *baton;
18804 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18805 struct objfile *objfile = per_objfile->objfile;
18806 struct obstack *obstack = &objfile->objfile_obstack;
18807
18808 gdb_assert (default_type != NULL);
18809
18810 if (attr == NULL || prop == NULL)
18811 return 0;
18812
18813 if (attr->form_is_block ())
18814 {
18815 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18816 baton->property_type = default_type;
18817 baton->locexpr.per_cu = cu->per_cu;
18818 baton->locexpr.per_objfile = per_objfile;
18819
18820 struct dwarf_block *block = attr->as_block ();
18821 baton->locexpr.size = block->size;
18822 baton->locexpr.data = block->data;
18823 switch (attr->name)
18824 {
18825 case DW_AT_string_length:
18826 baton->locexpr.is_reference = true;
18827 break;
18828 default:
18829 baton->locexpr.is_reference = false;
18830 break;
18831 }
18832
18833 prop->set_locexpr (baton);
18834 gdb_assert (prop->baton () != NULL);
18835 }
18836 else if (attr->form_is_ref ())
18837 {
18838 struct dwarf2_cu *target_cu = cu;
18839 struct die_info *target_die;
18840 struct attribute *target_attr;
18841
18842 target_die = follow_die_ref (die, attr, &target_cu);
18843 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18844 if (target_attr == NULL)
18845 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18846 target_cu);
18847 if (target_attr == NULL)
18848 return 0;
18849
18850 switch (target_attr->name)
18851 {
18852 case DW_AT_location:
18853 if (target_attr->form_is_section_offset ())
18854 {
18855 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18856 baton->property_type = die_type (target_die, target_cu);
18857 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18858 prop->set_loclist (baton);
18859 gdb_assert (prop->baton () != NULL);
18860 }
18861 else if (target_attr->form_is_block ())
18862 {
18863 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18864 baton->property_type = die_type (target_die, target_cu);
18865 baton->locexpr.per_cu = cu->per_cu;
18866 baton->locexpr.per_objfile = per_objfile;
18867 struct dwarf_block *block = target_attr->as_block ();
18868 baton->locexpr.size = block->size;
18869 baton->locexpr.data = block->data;
18870 baton->locexpr.is_reference = true;
18871 prop->set_locexpr (baton);
18872 gdb_assert (prop->baton () != NULL);
18873 }
18874 else
18875 {
18876 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18877 "dynamic property");
18878 return 0;
18879 }
18880 break;
18881 case DW_AT_data_member_location:
18882 {
18883 LONGEST offset;
18884
18885 if (!handle_data_member_location (target_die, target_cu,
18886 &offset))
18887 return 0;
18888
18889 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18890 baton->property_type = read_type_die (target_die->parent,
18891 target_cu);
18892 baton->offset_info.offset = offset;
18893 baton->offset_info.type = die_type (target_die, target_cu);
18894 prop->set_addr_offset (baton);
18895 break;
18896 }
18897 }
18898 }
18899 else if (attr->form_is_constant ())
18900 prop->set_const_val (attr->constant_value (0));
18901 else
18902 {
18903 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18904 dwarf2_name (die, cu));
18905 return 0;
18906 }
18907
18908 return 1;
18909 }
18910
18911 /* See read.h. */
18912
18913 struct type *
18914 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
18915 {
18916 struct type *int_type;
18917
18918 /* Helper macro to examine the various builtin types. */
18919 #define TRY_TYPE(F) \
18920 int_type = (unsigned_p \
18921 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18922 : objfile_type (objfile)->builtin_ ## F); \
18923 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
18924 return int_type
18925
18926 TRY_TYPE (char);
18927 TRY_TYPE (short);
18928 TRY_TYPE (int);
18929 TRY_TYPE (long);
18930 TRY_TYPE (long_long);
18931
18932 #undef TRY_TYPE
18933
18934 gdb_assert_not_reached ("unable to find suitable integer type");
18935 }
18936
18937 /* See read.h. */
18938
18939 struct type *
18940 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
18941 {
18942 int addr_size = this->per_cu->addr_size ();
18943 return this->per_objfile->int_type (addr_size, unsigned_p);
18944 }
18945
18946 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18947 present (which is valid) then compute the default type based on the
18948 compilation units address size. */
18949
18950 static struct type *
18951 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18952 {
18953 struct type *index_type = die_type (die, cu);
18954
18955 /* Dwarf-2 specifications explicitly allows to create subrange types
18956 without specifying a base type.
18957 In that case, the base type must be set to the type of
18958 the lower bound, upper bound or count, in that order, if any of these
18959 three attributes references an object that has a type.
18960 If no base type is found, the Dwarf-2 specifications say that
18961 a signed integer type of size equal to the size of an address should
18962 be used.
18963 For the following C code: `extern char gdb_int [];'
18964 GCC produces an empty range DIE.
18965 FIXME: muller/2010-05-28: Possible references to object for low bound,
18966 high bound or count are not yet handled by this code. */
18967 if (index_type->code () == TYPE_CODE_VOID)
18968 index_type = cu->addr_sized_int_type (false);
18969
18970 return index_type;
18971 }
18972
18973 /* Read the given DW_AT_subrange DIE. */
18974
18975 static struct type *
18976 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18977 {
18978 struct type *base_type, *orig_base_type;
18979 struct type *range_type;
18980 struct attribute *attr;
18981 struct dynamic_prop low, high;
18982 int low_default_is_valid;
18983 int high_bound_is_count = 0;
18984 const char *name;
18985 ULONGEST negative_mask;
18986
18987 orig_base_type = read_subrange_index_type (die, cu);
18988
18989 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18990 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18991 creating the range type, but we use the result of check_typedef
18992 when examining properties of the type. */
18993 base_type = check_typedef (orig_base_type);
18994
18995 /* The die_type call above may have already set the type for this DIE. */
18996 range_type = get_die_type (die, cu);
18997 if (range_type)
18998 return range_type;
18999
19000 high.set_const_val (0);
19001
19002 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
19003 omitting DW_AT_lower_bound. */
19004 switch (cu->language)
19005 {
19006 case language_c:
19007 case language_cplus:
19008 low.set_const_val (0);
19009 low_default_is_valid = 1;
19010 break;
19011 case language_fortran:
19012 low.set_const_val (1);
19013 low_default_is_valid = 1;
19014 break;
19015 case language_d:
19016 case language_objc:
19017 case language_rust:
19018 low.set_const_val (0);
19019 low_default_is_valid = (cu->header.version >= 4);
19020 break;
19021 case language_ada:
19022 case language_m2:
19023 case language_pascal:
19024 low.set_const_val (1);
19025 low_default_is_valid = (cu->header.version >= 4);
19026 break;
19027 default:
19028 low.set_const_val (0);
19029 low_default_is_valid = 0;
19030 break;
19031 }
19032
19033 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
19034 if (attr != nullptr)
19035 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
19036 else if (!low_default_is_valid)
19037 complaint (_("Missing DW_AT_lower_bound "
19038 "- DIE at %s [in module %s]"),
19039 sect_offset_str (die->sect_off),
19040 objfile_name (cu->per_objfile->objfile));
19041
19042 struct attribute *attr_ub, *attr_count;
19043 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
19044 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
19045 {
19046 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
19047 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
19048 {
19049 /* If bounds are constant do the final calculation here. */
19050 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
19051 high.set_const_val (low.const_val () + high.const_val () - 1);
19052 else
19053 high_bound_is_count = 1;
19054 }
19055 else
19056 {
19057 if (attr_ub != NULL)
19058 complaint (_("Unresolved DW_AT_upper_bound "
19059 "- DIE at %s [in module %s]"),
19060 sect_offset_str (die->sect_off),
19061 objfile_name (cu->per_objfile->objfile));
19062 if (attr_count != NULL)
19063 complaint (_("Unresolved DW_AT_count "
19064 "- DIE at %s [in module %s]"),
19065 sect_offset_str (die->sect_off),
19066 objfile_name (cu->per_objfile->objfile));
19067 }
19068 }
19069
19070 LONGEST bias = 0;
19071 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
19072 if (bias_attr != nullptr && bias_attr->form_is_constant ())
19073 bias = bias_attr->constant_value (0);
19074
19075 /* Normally, the DWARF producers are expected to use a signed
19076 constant form (Eg. DW_FORM_sdata) to express negative bounds.
19077 But this is unfortunately not always the case, as witnessed
19078 with GCC, for instance, where the ambiguous DW_FORM_dataN form
19079 is used instead. To work around that ambiguity, we treat
19080 the bounds as signed, and thus sign-extend their values, when
19081 the base type is signed. */
19082 negative_mask =
19083 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
19084 if (low.kind () == PROP_CONST
19085 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
19086 low.set_const_val (low.const_val () | negative_mask);
19087 if (high.kind () == PROP_CONST
19088 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
19089 high.set_const_val (high.const_val () | negative_mask);
19090
19091 /* Check for bit and byte strides. */
19092 struct dynamic_prop byte_stride_prop;
19093 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
19094 if (attr_byte_stride != nullptr)
19095 {
19096 struct type *prop_type = cu->addr_sized_int_type (false);
19097 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
19098 prop_type);
19099 }
19100
19101 struct dynamic_prop bit_stride_prop;
19102 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
19103 if (attr_bit_stride != nullptr)
19104 {
19105 /* It only makes sense to have either a bit or byte stride. */
19106 if (attr_byte_stride != nullptr)
19107 {
19108 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
19109 "- DIE at %s [in module %s]"),
19110 sect_offset_str (die->sect_off),
19111 objfile_name (cu->per_objfile->objfile));
19112 attr_bit_stride = nullptr;
19113 }
19114 else
19115 {
19116 struct type *prop_type = cu->addr_sized_int_type (false);
19117 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
19118 prop_type);
19119 }
19120 }
19121
19122 if (attr_byte_stride != nullptr
19123 || attr_bit_stride != nullptr)
19124 {
19125 bool byte_stride_p = (attr_byte_stride != nullptr);
19126 struct dynamic_prop *stride
19127 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
19128
19129 range_type
19130 = create_range_type_with_stride (NULL, orig_base_type, &low,
19131 &high, bias, stride, byte_stride_p);
19132 }
19133 else
19134 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
19135
19136 if (high_bound_is_count)
19137 range_type->bounds ()->flag_upper_bound_is_count = 1;
19138
19139 /* Ada expects an empty array on no boundary attributes. */
19140 if (attr == NULL && cu->language != language_ada)
19141 range_type->bounds ()->high.set_undefined ();
19142
19143 name = dwarf2_name (die, cu);
19144 if (name)
19145 range_type->set_name (name);
19146
19147 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
19148 if (attr != nullptr)
19149 TYPE_LENGTH (range_type) = attr->constant_value (0);
19150
19151 maybe_set_alignment (cu, die, range_type);
19152
19153 set_die_type (die, range_type, cu);
19154
19155 /* set_die_type should be already done. */
19156 set_descriptive_type (range_type, die, cu);
19157
19158 return range_type;
19159 }
19160
19161 static struct type *
19162 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
19163 {
19164 struct type *type;
19165
19166 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
19167 type->set_name (dwarf2_name (die, cu));
19168
19169 /* In Ada, an unspecified type is typically used when the description
19170 of the type is deferred to a different unit. When encountering
19171 such a type, we treat it as a stub, and try to resolve it later on,
19172 when needed. */
19173 if (cu->language == language_ada)
19174 type->set_is_stub (true);
19175
19176 return set_die_type (die, type, cu);
19177 }
19178
19179 /* Read a single die and all its descendents. Set the die's sibling
19180 field to NULL; set other fields in the die correctly, and set all
19181 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
19182 location of the info_ptr after reading all of those dies. PARENT
19183 is the parent of the die in question. */
19184
19185 static struct die_info *
19186 read_die_and_children (const struct die_reader_specs *reader,
19187 const gdb_byte *info_ptr,
19188 const gdb_byte **new_info_ptr,
19189 struct die_info *parent)
19190 {
19191 struct die_info *die;
19192 const gdb_byte *cur_ptr;
19193
19194 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
19195 if (die == NULL)
19196 {
19197 *new_info_ptr = cur_ptr;
19198 return NULL;
19199 }
19200 store_in_ref_table (die, reader->cu);
19201
19202 if (die->has_children)
19203 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
19204 else
19205 {
19206 die->child = NULL;
19207 *new_info_ptr = cur_ptr;
19208 }
19209
19210 die->sibling = NULL;
19211 die->parent = parent;
19212 return die;
19213 }
19214
19215 /* Read a die, all of its descendents, and all of its siblings; set
19216 all of the fields of all of the dies correctly. Arguments are as
19217 in read_die_and_children. */
19218
19219 static struct die_info *
19220 read_die_and_siblings_1 (const struct die_reader_specs *reader,
19221 const gdb_byte *info_ptr,
19222 const gdb_byte **new_info_ptr,
19223 struct die_info *parent)
19224 {
19225 struct die_info *first_die, *last_sibling;
19226 const gdb_byte *cur_ptr;
19227
19228 cur_ptr = info_ptr;
19229 first_die = last_sibling = NULL;
19230
19231 while (1)
19232 {
19233 struct die_info *die
19234 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
19235
19236 if (die == NULL)
19237 {
19238 *new_info_ptr = cur_ptr;
19239 return first_die;
19240 }
19241
19242 if (!first_die)
19243 first_die = die;
19244 else
19245 last_sibling->sibling = die;
19246
19247 last_sibling = die;
19248 }
19249 }
19250
19251 /* Read a die, all of its descendents, and all of its siblings; set
19252 all of the fields of all of the dies correctly. Arguments are as
19253 in read_die_and_children.
19254 This the main entry point for reading a DIE and all its children. */
19255
19256 static struct die_info *
19257 read_die_and_siblings (const struct die_reader_specs *reader,
19258 const gdb_byte *info_ptr,
19259 const gdb_byte **new_info_ptr,
19260 struct die_info *parent)
19261 {
19262 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
19263 new_info_ptr, parent);
19264
19265 if (dwarf_die_debug)
19266 {
19267 fprintf_unfiltered (gdb_stdlog,
19268 "Read die from %s@0x%x of %s:\n",
19269 reader->die_section->get_name (),
19270 (unsigned) (info_ptr - reader->die_section->buffer),
19271 bfd_get_filename (reader->abfd));
19272 dump_die (die, dwarf_die_debug);
19273 }
19274
19275 return die;
19276 }
19277
19278 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
19279 attributes.
19280 The caller is responsible for filling in the extra attributes
19281 and updating (*DIEP)->num_attrs.
19282 Set DIEP to point to a newly allocated die with its information,
19283 except for its child, sibling, and parent fields. */
19284
19285 static const gdb_byte *
19286 read_full_die_1 (const struct die_reader_specs *reader,
19287 struct die_info **diep, const gdb_byte *info_ptr,
19288 int num_extra_attrs)
19289 {
19290 unsigned int abbrev_number, bytes_read, i;
19291 const struct abbrev_info *abbrev;
19292 struct die_info *die;
19293 struct dwarf2_cu *cu = reader->cu;
19294 bfd *abfd = reader->abfd;
19295
19296 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
19297 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19298 info_ptr += bytes_read;
19299 if (!abbrev_number)
19300 {
19301 *diep = NULL;
19302 return info_ptr;
19303 }
19304
19305 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
19306 if (!abbrev)
19307 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
19308 abbrev_number,
19309 bfd_get_filename (abfd));
19310
19311 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
19312 die->sect_off = sect_off;
19313 die->tag = abbrev->tag;
19314 die->abbrev = abbrev_number;
19315 die->has_children = abbrev->has_children;
19316
19317 /* Make the result usable.
19318 The caller needs to update num_attrs after adding the extra
19319 attributes. */
19320 die->num_attrs = abbrev->num_attrs;
19321
19322 bool any_need_reprocess = false;
19323 for (i = 0; i < abbrev->num_attrs; ++i)
19324 {
19325 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
19326 info_ptr);
19327 if (die->attrs[i].requires_reprocessing_p ())
19328 any_need_reprocess = true;
19329 }
19330
19331 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
19332 if (attr != nullptr && attr->form_is_unsigned ())
19333 cu->str_offsets_base = attr->as_unsigned ();
19334
19335 attr = die->attr (DW_AT_loclists_base);
19336 if (attr != nullptr)
19337 cu->loclist_base = attr->as_unsigned ();
19338
19339 auto maybe_addr_base = die->addr_base ();
19340 if (maybe_addr_base.has_value ())
19341 cu->addr_base = *maybe_addr_base;
19342
19343 attr = die->attr (DW_AT_rnglists_base);
19344 if (attr != nullptr)
19345 cu->rnglists_base = attr->as_unsigned ();
19346
19347 if (any_need_reprocess)
19348 {
19349 for (i = 0; i < abbrev->num_attrs; ++i)
19350 {
19351 if (die->attrs[i].requires_reprocessing_p ())
19352 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
19353 }
19354 }
19355 *diep = die;
19356 return info_ptr;
19357 }
19358
19359 /* Read a die and all its attributes.
19360 Set DIEP to point to a newly allocated die with its information,
19361 except for its child, sibling, and parent fields. */
19362
19363 static const gdb_byte *
19364 read_full_die (const struct die_reader_specs *reader,
19365 struct die_info **diep, const gdb_byte *info_ptr)
19366 {
19367 const gdb_byte *result;
19368
19369 result = read_full_die_1 (reader, diep, info_ptr, 0);
19370
19371 if (dwarf_die_debug)
19372 {
19373 fprintf_unfiltered (gdb_stdlog,
19374 "Read die from %s@0x%x of %s:\n",
19375 reader->die_section->get_name (),
19376 (unsigned) (info_ptr - reader->die_section->buffer),
19377 bfd_get_filename (reader->abfd));
19378 dump_die (*diep, dwarf_die_debug);
19379 }
19380
19381 return result;
19382 }
19383 \f
19384
19385 /* Returns nonzero if TAG represents a type that we might generate a partial
19386 symbol for. */
19387
19388 static int
19389 is_type_tag_for_partial (int tag, enum language lang)
19390 {
19391 switch (tag)
19392 {
19393 #if 0
19394 /* Some types that would be reasonable to generate partial symbols for,
19395 that we don't at present. Note that normally this does not
19396 matter, mainly because C compilers don't give names to these
19397 types, but instead emit DW_TAG_typedef. */
19398 case DW_TAG_file_type:
19399 case DW_TAG_ptr_to_member_type:
19400 case DW_TAG_set_type:
19401 case DW_TAG_string_type:
19402 case DW_TAG_subroutine_type:
19403 #endif
19404
19405 /* GNAT may emit an array with a name, but no typedef, so we
19406 need to make a symbol in this case. */
19407 case DW_TAG_array_type:
19408 return lang == language_ada;
19409
19410 case DW_TAG_base_type:
19411 case DW_TAG_class_type:
19412 case DW_TAG_interface_type:
19413 case DW_TAG_enumeration_type:
19414 case DW_TAG_structure_type:
19415 case DW_TAG_subrange_type:
19416 case DW_TAG_typedef:
19417 case DW_TAG_union_type:
19418 return 1;
19419 default:
19420 return 0;
19421 }
19422 }
19423
19424 /* Load all DIEs that are interesting for partial symbols into memory. */
19425
19426 static struct partial_die_info *
19427 load_partial_dies (const struct die_reader_specs *reader,
19428 const gdb_byte *info_ptr, int building_psymtab)
19429 {
19430 struct dwarf2_cu *cu = reader->cu;
19431 struct objfile *objfile = cu->per_objfile->objfile;
19432 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
19433 unsigned int bytes_read;
19434 unsigned int load_all = 0;
19435 int nesting_level = 1;
19436
19437 parent_die = NULL;
19438 last_die = NULL;
19439
19440 gdb_assert (cu->per_cu != NULL);
19441 if (cu->per_cu->load_all_dies)
19442 load_all = 1;
19443
19444 cu->partial_dies
19445 = htab_create_alloc_ex (cu->header.length / 12,
19446 partial_die_hash,
19447 partial_die_eq,
19448 NULL,
19449 &cu->comp_unit_obstack,
19450 hashtab_obstack_allocate,
19451 dummy_obstack_deallocate);
19452
19453 while (1)
19454 {
19455 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
19456 &bytes_read);
19457
19458 /* A NULL abbrev means the end of a series of children. */
19459 if (abbrev == NULL)
19460 {
19461 if (--nesting_level == 0)
19462 return first_die;
19463
19464 info_ptr += bytes_read;
19465 last_die = parent_die;
19466 parent_die = parent_die->die_parent;
19467 continue;
19468 }
19469
19470 /* Check for template arguments. We never save these; if
19471 they're seen, we just mark the parent, and go on our way. */
19472 if (parent_die != NULL
19473 && cu->language == language_cplus
19474 && (abbrev->tag == DW_TAG_template_type_param
19475 || abbrev->tag == DW_TAG_template_value_param))
19476 {
19477 parent_die->has_template_arguments = 1;
19478
19479 if (!load_all)
19480 {
19481 /* We don't need a partial DIE for the template argument. */
19482 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19483 continue;
19484 }
19485 }
19486
19487 /* We only recurse into c++ subprograms looking for template arguments.
19488 Skip their other children. */
19489 if (!load_all
19490 && cu->language == language_cplus
19491 && parent_die != NULL
19492 && parent_die->tag == DW_TAG_subprogram
19493 && abbrev->tag != DW_TAG_inlined_subroutine)
19494 {
19495 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19496 continue;
19497 }
19498
19499 /* Check whether this DIE is interesting enough to save. Normally
19500 we would not be interested in members here, but there may be
19501 later variables referencing them via DW_AT_specification (for
19502 static members). */
19503 if (!load_all
19504 && !is_type_tag_for_partial (abbrev->tag, cu->language)
19505 && abbrev->tag != DW_TAG_constant
19506 && abbrev->tag != DW_TAG_enumerator
19507 && abbrev->tag != DW_TAG_subprogram
19508 && abbrev->tag != DW_TAG_inlined_subroutine
19509 && abbrev->tag != DW_TAG_lexical_block
19510 && abbrev->tag != DW_TAG_variable
19511 && abbrev->tag != DW_TAG_namespace
19512 && abbrev->tag != DW_TAG_module
19513 && abbrev->tag != DW_TAG_member
19514 && abbrev->tag != DW_TAG_imported_unit
19515 && abbrev->tag != DW_TAG_imported_declaration)
19516 {
19517 /* Otherwise we skip to the next sibling, if any. */
19518 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19519 continue;
19520 }
19521
19522 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19523 abbrev);
19524
19525 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
19526
19527 /* This two-pass algorithm for processing partial symbols has a
19528 high cost in cache pressure. Thus, handle some simple cases
19529 here which cover the majority of C partial symbols. DIEs
19530 which neither have specification tags in them, nor could have
19531 specification tags elsewhere pointing at them, can simply be
19532 processed and discarded.
19533
19534 This segment is also optional; scan_partial_symbols and
19535 add_partial_symbol will handle these DIEs if we chain
19536 them in normally. When compilers which do not emit large
19537 quantities of duplicate debug information are more common,
19538 this code can probably be removed. */
19539
19540 /* Any complete simple types at the top level (pretty much all
19541 of them, for a language without namespaces), can be processed
19542 directly. */
19543 if (parent_die == NULL
19544 && pdi.has_specification == 0
19545 && pdi.is_declaration == 0
19546 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19547 || pdi.tag == DW_TAG_base_type
19548 || pdi.tag == DW_TAG_array_type
19549 || pdi.tag == DW_TAG_subrange_type))
19550 {
19551 if (building_psymtab && pdi.raw_name != NULL)
19552 add_partial_symbol (&pdi, cu);
19553
19554 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19555 continue;
19556 }
19557
19558 /* The exception for DW_TAG_typedef with has_children above is
19559 a workaround of GCC PR debug/47510. In the case of this complaint
19560 type_name_or_error will error on such types later.
19561
19562 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19563 it could not find the child DIEs referenced later, this is checked
19564 above. In correct DWARF DW_TAG_typedef should have no children. */
19565
19566 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
19567 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
19568 "- DIE at %s [in module %s]"),
19569 sect_offset_str (pdi.sect_off), objfile_name (objfile));
19570
19571 /* If we're at the second level, and we're an enumerator, and
19572 our parent has no specification (meaning possibly lives in a
19573 namespace elsewhere), then we can add the partial symbol now
19574 instead of queueing it. */
19575 if (pdi.tag == DW_TAG_enumerator
19576 && parent_die != NULL
19577 && parent_die->die_parent == NULL
19578 && parent_die->tag == DW_TAG_enumeration_type
19579 && parent_die->has_specification == 0)
19580 {
19581 if (pdi.raw_name == NULL)
19582 complaint (_("malformed enumerator DIE ignored"));
19583 else if (building_psymtab)
19584 add_partial_symbol (&pdi, cu);
19585
19586 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19587 continue;
19588 }
19589
19590 struct partial_die_info *part_die
19591 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
19592
19593 /* We'll save this DIE so link it in. */
19594 part_die->die_parent = parent_die;
19595 part_die->die_sibling = NULL;
19596 part_die->die_child = NULL;
19597
19598 if (last_die && last_die == parent_die)
19599 last_die->die_child = part_die;
19600 else if (last_die)
19601 last_die->die_sibling = part_die;
19602
19603 last_die = part_die;
19604
19605 if (first_die == NULL)
19606 first_die = part_die;
19607
19608 /* Maybe add the DIE to the hash table. Not all DIEs that we
19609 find interesting need to be in the hash table, because we
19610 also have the parent/sibling/child chains; only those that we
19611 might refer to by offset later during partial symbol reading.
19612
19613 For now this means things that might have be the target of a
19614 DW_AT_specification, DW_AT_abstract_origin, or
19615 DW_AT_extension. DW_AT_extension will refer only to
19616 namespaces; DW_AT_abstract_origin refers to functions (and
19617 many things under the function DIE, but we do not recurse
19618 into function DIEs during partial symbol reading) and
19619 possibly variables as well; DW_AT_specification refers to
19620 declarations. Declarations ought to have the DW_AT_declaration
19621 flag. It happens that GCC forgets to put it in sometimes, but
19622 only for functions, not for types.
19623
19624 Adding more things than necessary to the hash table is harmless
19625 except for the performance cost. Adding too few will result in
19626 wasted time in find_partial_die, when we reread the compilation
19627 unit with load_all_dies set. */
19628
19629 if (load_all
19630 || abbrev->tag == DW_TAG_constant
19631 || abbrev->tag == DW_TAG_subprogram
19632 || abbrev->tag == DW_TAG_variable
19633 || abbrev->tag == DW_TAG_namespace
19634 || part_die->is_declaration)
19635 {
19636 void **slot;
19637
19638 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
19639 to_underlying (part_die->sect_off),
19640 INSERT);
19641 *slot = part_die;
19642 }
19643
19644 /* For some DIEs we want to follow their children (if any). For C
19645 we have no reason to follow the children of structures; for other
19646 languages we have to, so that we can get at method physnames
19647 to infer fully qualified class names, for DW_AT_specification,
19648 and for C++ template arguments. For C++, we also look one level
19649 inside functions to find template arguments (if the name of the
19650 function does not already contain the template arguments).
19651
19652 For Ada and Fortran, we need to scan the children of subprograms
19653 and lexical blocks as well because these languages allow the
19654 definition of nested entities that could be interesting for the
19655 debugger, such as nested subprograms for instance. */
19656 if (last_die->has_children
19657 && (load_all
19658 || last_die->tag == DW_TAG_namespace
19659 || last_die->tag == DW_TAG_module
19660 || last_die->tag == DW_TAG_enumeration_type
19661 || (cu->language == language_cplus
19662 && last_die->tag == DW_TAG_subprogram
19663 && (last_die->raw_name == NULL
19664 || strchr (last_die->raw_name, '<') == NULL))
19665 || (cu->language != language_c
19666 && (last_die->tag == DW_TAG_class_type
19667 || last_die->tag == DW_TAG_interface_type
19668 || last_die->tag == DW_TAG_structure_type
19669 || last_die->tag == DW_TAG_union_type))
19670 || ((cu->language == language_ada
19671 || cu->language == language_fortran)
19672 && (last_die->tag == DW_TAG_subprogram
19673 || last_die->tag == DW_TAG_lexical_block))))
19674 {
19675 nesting_level++;
19676 parent_die = last_die;
19677 continue;
19678 }
19679
19680 /* Otherwise we skip to the next sibling, if any. */
19681 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
19682
19683 /* Back to the top, do it again. */
19684 }
19685 }
19686
19687 partial_die_info::partial_die_info (sect_offset sect_off_,
19688 const struct abbrev_info *abbrev)
19689 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19690 {
19691 }
19692
19693 /* See class definition. */
19694
19695 const char *
19696 partial_die_info::name (dwarf2_cu *cu)
19697 {
19698 if (!canonical_name && raw_name != nullptr)
19699 {
19700 struct objfile *objfile = cu->per_objfile->objfile;
19701 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19702 canonical_name = 1;
19703 }
19704
19705 return raw_name;
19706 }
19707
19708 /* Read a minimal amount of information into the minimal die structure.
19709 INFO_PTR should point just after the initial uleb128 of a DIE. */
19710
19711 const gdb_byte *
19712 partial_die_info::read (const struct die_reader_specs *reader,
19713 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
19714 {
19715 struct dwarf2_cu *cu = reader->cu;
19716 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19717 unsigned int i;
19718 int has_low_pc_attr = 0;
19719 int has_high_pc_attr = 0;
19720 int high_pc_relative = 0;
19721
19722 for (i = 0; i < abbrev.num_attrs; ++i)
19723 {
19724 attribute attr;
19725 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19726 /* String and address offsets that need to do the reprocessing have
19727 already been read at this point, so there is no need to wait until
19728 the loop terminates to do the reprocessing. */
19729 if (attr.requires_reprocessing_p ())
19730 read_attribute_reprocess (reader, &attr, tag);
19731 /* Store the data if it is of an attribute we want to keep in a
19732 partial symbol table. */
19733 switch (attr.name)
19734 {
19735 case DW_AT_name:
19736 switch (tag)
19737 {
19738 case DW_TAG_compile_unit:
19739 case DW_TAG_partial_unit:
19740 case DW_TAG_type_unit:
19741 /* Compilation units have a DW_AT_name that is a filename, not
19742 a source language identifier. */
19743 case DW_TAG_enumeration_type:
19744 case DW_TAG_enumerator:
19745 /* These tags always have simple identifiers already; no need
19746 to canonicalize them. */
19747 canonical_name = 1;
19748 raw_name = attr.as_string ();
19749 break;
19750 default:
19751 canonical_name = 0;
19752 raw_name = attr.as_string ();
19753 break;
19754 }
19755 break;
19756 case DW_AT_linkage_name:
19757 case DW_AT_MIPS_linkage_name:
19758 /* Note that both forms of linkage name might appear. We
19759 assume they will be the same, and we only store the last
19760 one we see. */
19761 linkage_name = attr.as_string ();
19762 break;
19763 case DW_AT_low_pc:
19764 has_low_pc_attr = 1;
19765 lowpc = attr.as_address ();
19766 break;
19767 case DW_AT_high_pc:
19768 has_high_pc_attr = 1;
19769 highpc = attr.as_address ();
19770 if (cu->header.version >= 4 && attr.form_is_constant ())
19771 high_pc_relative = 1;
19772 break;
19773 case DW_AT_location:
19774 /* Support the .debug_loc offsets. */
19775 if (attr.form_is_block ())
19776 {
19777 d.locdesc = attr.as_block ();
19778 }
19779 else if (attr.form_is_section_offset ())
19780 {
19781 dwarf2_complex_location_expr_complaint ();
19782 }
19783 else
19784 {
19785 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19786 "partial symbol information");
19787 }
19788 break;
19789 case DW_AT_external:
19790 is_external = attr.as_boolean ();
19791 break;
19792 case DW_AT_declaration:
19793 is_declaration = attr.as_boolean ();
19794 break;
19795 case DW_AT_type:
19796 has_type = 1;
19797 break;
19798 case DW_AT_abstract_origin:
19799 case DW_AT_specification:
19800 case DW_AT_extension:
19801 has_specification = 1;
19802 spec_offset = attr.get_ref_die_offset ();
19803 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19804 || cu->per_cu->is_dwz);
19805 break;
19806 case DW_AT_sibling:
19807 /* Ignore absolute siblings, they might point outside of
19808 the current compile unit. */
19809 if (attr.form == DW_FORM_ref_addr)
19810 complaint (_("ignoring absolute DW_AT_sibling"));
19811 else
19812 {
19813 const gdb_byte *buffer = reader->buffer;
19814 sect_offset off = attr.get_ref_die_offset ();
19815 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19816
19817 if (sibling_ptr < info_ptr)
19818 complaint (_("DW_AT_sibling points backwards"));
19819 else if (sibling_ptr > reader->buffer_end)
19820 reader->die_section->overflow_complaint ();
19821 else
19822 sibling = sibling_ptr;
19823 }
19824 break;
19825 case DW_AT_byte_size:
19826 has_byte_size = 1;
19827 break;
19828 case DW_AT_const_value:
19829 has_const_value = 1;
19830 break;
19831 case DW_AT_calling_convention:
19832 /* DWARF doesn't provide a way to identify a program's source-level
19833 entry point. DW_AT_calling_convention attributes are only meant
19834 to describe functions' calling conventions.
19835
19836 However, because it's a necessary piece of information in
19837 Fortran, and before DWARF 4 DW_CC_program was the only
19838 piece of debugging information whose definition refers to
19839 a 'main program' at all, several compilers marked Fortran
19840 main programs with DW_CC_program --- even when those
19841 functions use the standard calling conventions.
19842
19843 Although DWARF now specifies a way to provide this
19844 information, we support this practice for backward
19845 compatibility. */
19846 if (attr.constant_value (0) == DW_CC_program
19847 && cu->language == language_fortran)
19848 main_subprogram = 1;
19849 break;
19850 case DW_AT_inline:
19851 {
19852 LONGEST value = attr.constant_value (-1);
19853 if (value == DW_INL_inlined
19854 || value == DW_INL_declared_inlined)
19855 may_be_inlined = 1;
19856 }
19857 break;
19858
19859 case DW_AT_import:
19860 if (tag == DW_TAG_imported_unit)
19861 {
19862 d.sect_off = attr.get_ref_die_offset ();
19863 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19864 || cu->per_cu->is_dwz);
19865 }
19866 break;
19867
19868 case DW_AT_main_subprogram:
19869 main_subprogram = attr.as_boolean ();
19870 break;
19871
19872 case DW_AT_ranges:
19873 {
19874 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19875 on DWARF version). */
19876 ULONGEST ranges_offset = attr.as_unsigned ();
19877
19878 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19879 this value. */
19880 if (tag != DW_TAG_compile_unit)
19881 ranges_offset += cu->gnu_ranges_base;
19882
19883 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19884 nullptr, tag))
19885 has_pc_info = 1;
19886 }
19887 break;
19888
19889 default:
19890 break;
19891 }
19892 }
19893
19894 /* For Ada, if both the name and the linkage name appear, we prefer
19895 the latter. This lets "catch exception" work better, regardless
19896 of the order in which the name and linkage name were emitted.
19897 Really, though, this is just a workaround for the fact that gdb
19898 doesn't store both the name and the linkage name. */
19899 if (cu->language == language_ada && linkage_name != nullptr)
19900 raw_name = linkage_name;
19901
19902 if (high_pc_relative)
19903 highpc += lowpc;
19904
19905 if (has_low_pc_attr && has_high_pc_attr)
19906 {
19907 /* When using the GNU linker, .gnu.linkonce. sections are used to
19908 eliminate duplicate copies of functions and vtables and such.
19909 The linker will arbitrarily choose one and discard the others.
19910 The AT_*_pc values for such functions refer to local labels in
19911 these sections. If the section from that file was discarded, the
19912 labels are not in the output, so the relocs get a value of 0.
19913 If this is a discarded function, mark the pc bounds as invalid,
19914 so that GDB will ignore it. */
19915 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
19916 {
19917 struct objfile *objfile = per_objfile->objfile;
19918 struct gdbarch *gdbarch = objfile->arch ();
19919
19920 complaint (_("DW_AT_low_pc %s is zero "
19921 "for DIE at %s [in module %s]"),
19922 paddress (gdbarch, lowpc),
19923 sect_offset_str (sect_off),
19924 objfile_name (objfile));
19925 }
19926 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19927 else if (lowpc >= highpc)
19928 {
19929 struct objfile *objfile = per_objfile->objfile;
19930 struct gdbarch *gdbarch = objfile->arch ();
19931
19932 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19933 "for DIE at %s [in module %s]"),
19934 paddress (gdbarch, lowpc),
19935 paddress (gdbarch, highpc),
19936 sect_offset_str (sect_off),
19937 objfile_name (objfile));
19938 }
19939 else
19940 has_pc_info = 1;
19941 }
19942
19943 return info_ptr;
19944 }
19945
19946 /* Find a cached partial DIE at OFFSET in CU. */
19947
19948 struct partial_die_info *
19949 dwarf2_cu::find_partial_die (sect_offset sect_off)
19950 {
19951 struct partial_die_info *lookup_die = NULL;
19952 struct partial_die_info part_die (sect_off);
19953
19954 lookup_die = ((struct partial_die_info *)
19955 htab_find_with_hash (partial_dies, &part_die,
19956 to_underlying (sect_off)));
19957
19958 return lookup_die;
19959 }
19960
19961 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19962 except in the case of .debug_types DIEs which do not reference
19963 outside their CU (they do however referencing other types via
19964 DW_FORM_ref_sig8). */
19965
19966 static const struct cu_partial_die_info
19967 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19968 {
19969 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19970 struct objfile *objfile = per_objfile->objfile;
19971 struct partial_die_info *pd = NULL;
19972
19973 if (offset_in_dwz == cu->per_cu->is_dwz
19974 && cu->header.offset_in_cu_p (sect_off))
19975 {
19976 pd = cu->find_partial_die (sect_off);
19977 if (pd != NULL)
19978 return { cu, pd };
19979 /* We missed recording what we needed.
19980 Load all dies and try again. */
19981 }
19982 else
19983 {
19984 /* TUs don't reference other CUs/TUs (except via type signatures). */
19985 if (cu->per_cu->is_debug_types)
19986 {
19987 error (_("Dwarf Error: Type Unit at offset %s contains"
19988 " external reference to offset %s [in module %s].\n"),
19989 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19990 bfd_get_filename (objfile->obfd));
19991 }
19992 dwarf2_per_cu_data *per_cu
19993 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19994 per_objfile);
19995
19996 cu = per_objfile->get_cu (per_cu);
19997 if (cu == NULL || cu->partial_dies == NULL)
19998 load_partial_comp_unit (per_cu, per_objfile, nullptr);
19999
20000 cu = per_objfile->get_cu (per_cu);
20001
20002 cu->last_used = 0;
20003 pd = cu->find_partial_die (sect_off);
20004 }
20005
20006 /* If we didn't find it, and not all dies have been loaded,
20007 load them all and try again. */
20008
20009 if (pd == NULL && cu->per_cu->load_all_dies == 0)
20010 {
20011 cu->per_cu->load_all_dies = 1;
20012
20013 /* This is nasty. When we reread the DIEs, somewhere up the call chain
20014 THIS_CU->cu may already be in use. So we can't just free it and
20015 replace its DIEs with the ones we read in. Instead, we leave those
20016 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
20017 and clobber THIS_CU->cu->partial_dies with the hash table for the new
20018 set. */
20019 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
20020
20021 pd = cu->find_partial_die (sect_off);
20022 }
20023
20024 if (pd == NULL)
20025 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
20026 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
20027 return { cu, pd };
20028 }
20029
20030 /* See if we can figure out if the class lives in a namespace. We do
20031 this by looking for a member function; its demangled name will
20032 contain namespace info, if there is any. */
20033
20034 static void
20035 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
20036 struct dwarf2_cu *cu)
20037 {
20038 /* NOTE: carlton/2003-10-07: Getting the info this way changes
20039 what template types look like, because the demangler
20040 frequently doesn't give the same name as the debug info. We
20041 could fix this by only using the demangled name to get the
20042 prefix (but see comment in read_structure_type). */
20043
20044 struct partial_die_info *real_pdi;
20045 struct partial_die_info *child_pdi;
20046
20047 /* If this DIE (this DIE's specification, if any) has a parent, then
20048 we should not do this. We'll prepend the parent's fully qualified
20049 name when we create the partial symbol. */
20050
20051 real_pdi = struct_pdi;
20052 while (real_pdi->has_specification)
20053 {
20054 auto res = find_partial_die (real_pdi->spec_offset,
20055 real_pdi->spec_is_dwz, cu);
20056 real_pdi = res.pdi;
20057 cu = res.cu;
20058 }
20059
20060 if (real_pdi->die_parent != NULL)
20061 return;
20062
20063 for (child_pdi = struct_pdi->die_child;
20064 child_pdi != NULL;
20065 child_pdi = child_pdi->die_sibling)
20066 {
20067 if (child_pdi->tag == DW_TAG_subprogram
20068 && child_pdi->linkage_name != NULL)
20069 {
20070 gdb::unique_xmalloc_ptr<char> actual_class_name
20071 (cu->language_defn->class_name_from_physname
20072 (child_pdi->linkage_name));
20073 if (actual_class_name != NULL)
20074 {
20075 struct objfile *objfile = cu->per_objfile->objfile;
20076 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
20077 struct_pdi->canonical_name = 1;
20078 }
20079 break;
20080 }
20081 }
20082 }
20083
20084 /* Return true if a DIE with TAG may have the DW_AT_const_value
20085 attribute. */
20086
20087 static bool
20088 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
20089 {
20090 switch (tag)
20091 {
20092 case DW_TAG_constant:
20093 case DW_TAG_enumerator:
20094 case DW_TAG_formal_parameter:
20095 case DW_TAG_template_value_param:
20096 case DW_TAG_variable:
20097 return true;
20098 }
20099
20100 return false;
20101 }
20102
20103 void
20104 partial_die_info::fixup (struct dwarf2_cu *cu)
20105 {
20106 /* Once we've fixed up a die, there's no point in doing so again.
20107 This also avoids a memory leak if we were to call
20108 guess_partial_die_structure_name multiple times. */
20109 if (fixup_called)
20110 return;
20111
20112 /* If we found a reference attribute and the DIE has no name, try
20113 to find a name in the referred to DIE. */
20114
20115 if (raw_name == NULL && has_specification)
20116 {
20117 struct partial_die_info *spec_die;
20118
20119 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20120 spec_die = res.pdi;
20121 cu = res.cu;
20122
20123 spec_die->fixup (cu);
20124
20125 if (spec_die->raw_name)
20126 {
20127 raw_name = spec_die->raw_name;
20128 canonical_name = spec_die->canonical_name;
20129
20130 /* Copy DW_AT_external attribute if it is set. */
20131 if (spec_die->is_external)
20132 is_external = spec_die->is_external;
20133 }
20134 }
20135
20136 if (!has_const_value && has_specification
20137 && can_have_DW_AT_const_value_p (tag))
20138 {
20139 struct partial_die_info *spec_die;
20140
20141 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20142 spec_die = res.pdi;
20143 cu = res.cu;
20144
20145 spec_die->fixup (cu);
20146
20147 if (spec_die->has_const_value)
20148 {
20149 /* Copy DW_AT_const_value attribute if it is set. */
20150 has_const_value = spec_die->has_const_value;
20151 }
20152 }
20153
20154 /* Set default names for some unnamed DIEs. */
20155
20156 if (raw_name == NULL && tag == DW_TAG_namespace)
20157 {
20158 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
20159 canonical_name = 1;
20160 }
20161
20162 /* If there is no parent die to provide a namespace, and there are
20163 children, see if we can determine the namespace from their linkage
20164 name. */
20165 if (cu->language == language_cplus
20166 && !cu->per_objfile->per_bfd->types.empty ()
20167 && die_parent == NULL
20168 && has_children
20169 && (tag == DW_TAG_class_type
20170 || tag == DW_TAG_structure_type
20171 || tag == DW_TAG_union_type))
20172 guess_partial_die_structure_name (this, cu);
20173
20174 /* GCC might emit a nameless struct or union that has a linkage
20175 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20176 if (raw_name == NULL
20177 && (tag == DW_TAG_class_type
20178 || tag == DW_TAG_interface_type
20179 || tag == DW_TAG_structure_type
20180 || tag == DW_TAG_union_type)
20181 && linkage_name != NULL)
20182 {
20183 gdb::unique_xmalloc_ptr<char> demangled
20184 (gdb_demangle (linkage_name, DMGL_TYPES));
20185 if (demangled != nullptr)
20186 {
20187 const char *base;
20188
20189 /* Strip any leading namespaces/classes, keep only the base name.
20190 DW_AT_name for named DIEs does not contain the prefixes. */
20191 base = strrchr (demangled.get (), ':');
20192 if (base && base > demangled.get () && base[-1] == ':')
20193 base++;
20194 else
20195 base = demangled.get ();
20196
20197 struct objfile *objfile = cu->per_objfile->objfile;
20198 raw_name = objfile->intern (base);
20199 canonical_name = 1;
20200 }
20201 }
20202
20203 fixup_called = 1;
20204 }
20205
20206 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
20207 contents from the given SECTION in the HEADER.
20208
20209 HEADER_OFFSET is the offset of the header in the section. */
20210 static void
20211 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
20212 struct dwarf2_section_info *section,
20213 sect_offset header_offset)
20214 {
20215 unsigned int bytes_read;
20216 bfd *abfd = section->get_bfd_owner ();
20217 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
20218
20219 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
20220 info_ptr += bytes_read;
20221
20222 header->version = read_2_bytes (abfd, info_ptr);
20223 info_ptr += 2;
20224
20225 header->addr_size = read_1_byte (abfd, info_ptr);
20226 info_ptr += 1;
20227
20228 header->segment_collector_size = read_1_byte (abfd, info_ptr);
20229 info_ptr += 1;
20230
20231 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
20232 }
20233
20234 /* Return the DW_AT_loclists_base value for the CU. */
20235 static ULONGEST
20236 lookup_loclist_base (struct dwarf2_cu *cu)
20237 {
20238 /* For the .dwo unit, the loclist_base points to the first offset following
20239 the header. The header consists of the following entities-
20240 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
20241 bit format)
20242 2. version (2 bytes)
20243 3. address size (1 byte)
20244 4. segment selector size (1 byte)
20245 5. offset entry count (4 bytes)
20246 These sizes are derived as per the DWARFv5 standard. */
20247 if (cu->dwo_unit != nullptr)
20248 {
20249 if (cu->header.initial_length_size == 4)
20250 return LOCLIST_HEADER_SIZE32;
20251 return LOCLIST_HEADER_SIZE64;
20252 }
20253 return cu->loclist_base;
20254 }
20255
20256 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
20257 array of offsets in the .debug_loclists section. */
20258
20259 static sect_offset
20260 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
20261 {
20262 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20263 struct objfile *objfile = per_objfile->objfile;
20264 bfd *abfd = objfile->obfd;
20265 ULONGEST loclist_header_size =
20266 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
20267 : LOCLIST_HEADER_SIZE64);
20268 ULONGEST loclist_base = lookup_loclist_base (cu);
20269
20270 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
20271 ULONGEST start_offset =
20272 loclist_base + loclist_index * cu->header.offset_size;
20273
20274 /* Get loclists section. */
20275 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20276
20277 /* Read the loclists section content. */
20278 section->read (objfile);
20279 if (section->buffer == NULL)
20280 error (_("DW_FORM_loclistx used without .debug_loclists "
20281 "section [in module %s]"), objfile_name (objfile));
20282
20283 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
20284 so if loclist_base is smaller than the header size, we have a problem. */
20285 if (loclist_base < loclist_header_size)
20286 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
20287 objfile_name (objfile));
20288
20289 /* Read the header of the loclists contribution. */
20290 struct loclists_rnglists_header header;
20291 read_loclists_rnglists_header (&header, section,
20292 (sect_offset) (loclist_base - loclist_header_size));
20293
20294 /* Verify the loclist index is valid. */
20295 if (loclist_index >= header.offset_entry_count)
20296 error (_("DW_FORM_loclistx pointing outside of "
20297 ".debug_loclists offset array [in module %s]"),
20298 objfile_name (objfile));
20299
20300 /* Validate that reading won't go beyond the end of the section. */
20301 if (start_offset + cu->header.offset_size > section->size)
20302 error (_("Reading DW_FORM_loclistx index beyond end of"
20303 ".debug_loclists section [in module %s]"),
20304 objfile_name (objfile));
20305
20306 const gdb_byte *info_ptr = section->buffer + start_offset;
20307
20308 if (cu->header.offset_size == 4)
20309 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
20310 else
20311 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
20312 }
20313
20314 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
20315 array of offsets in the .debug_rnglists section. */
20316
20317 static sect_offset
20318 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
20319 dwarf_tag tag)
20320 {
20321 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20322 struct objfile *objfile = dwarf2_per_objfile->objfile;
20323 bfd *abfd = objfile->obfd;
20324 ULONGEST rnglist_header_size =
20325 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
20326 : RNGLIST_HEADER_SIZE64);
20327
20328 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
20329 .debug_rnglists.dwo section. The rnglists base given in the skeleton
20330 doesn't apply. */
20331 ULONGEST rnglist_base =
20332 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
20333
20334 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
20335 ULONGEST start_offset =
20336 rnglist_base + rnglist_index * cu->header.offset_size;
20337
20338 /* Get rnglists section. */
20339 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
20340
20341 /* Read the rnglists section content. */
20342 section->read (objfile);
20343 if (section->buffer == nullptr)
20344 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
20345 "[in module %s]"),
20346 objfile_name (objfile));
20347
20348 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
20349 so if rnglist_base is smaller than the header size, we have a problem. */
20350 if (rnglist_base < rnglist_header_size)
20351 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
20352 objfile_name (objfile));
20353
20354 /* Read the header of the rnglists contribution. */
20355 struct loclists_rnglists_header header;
20356 read_loclists_rnglists_header (&header, section,
20357 (sect_offset) (rnglist_base - rnglist_header_size));
20358
20359 /* Verify the rnglist index is valid. */
20360 if (rnglist_index >= header.offset_entry_count)
20361 error (_("DW_FORM_rnglistx index pointing outside of "
20362 ".debug_rnglists offset array [in module %s]"),
20363 objfile_name (objfile));
20364
20365 /* Validate that reading won't go beyond the end of the section. */
20366 if (start_offset + cu->header.offset_size > section->size)
20367 error (_("Reading DW_FORM_rnglistx index beyond end of"
20368 ".debug_rnglists section [in module %s]"),
20369 objfile_name (objfile));
20370
20371 const gdb_byte *info_ptr = section->buffer + start_offset;
20372
20373 if (cu->header.offset_size == 4)
20374 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
20375 else
20376 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
20377 }
20378
20379 /* Process the attributes that had to be skipped in the first round. These
20380 attributes are the ones that need str_offsets_base or addr_base attributes.
20381 They could not have been processed in the first round, because at the time
20382 the values of str_offsets_base or addr_base may not have been known. */
20383 static void
20384 read_attribute_reprocess (const struct die_reader_specs *reader,
20385 struct attribute *attr, dwarf_tag tag)
20386 {
20387 struct dwarf2_cu *cu = reader->cu;
20388 switch (attr->form)
20389 {
20390 case DW_FORM_addrx:
20391 case DW_FORM_GNU_addr_index:
20392 attr->set_address (read_addr_index (cu,
20393 attr->as_unsigned_reprocess ()));
20394 break;
20395 case DW_FORM_loclistx:
20396 {
20397 sect_offset loclists_sect_off
20398 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
20399
20400 attr->set_unsigned (to_underlying (loclists_sect_off));
20401 }
20402 break;
20403 case DW_FORM_rnglistx:
20404 {
20405 sect_offset rnglists_sect_off
20406 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
20407
20408 attr->set_unsigned (to_underlying (rnglists_sect_off));
20409 }
20410 break;
20411 case DW_FORM_strx:
20412 case DW_FORM_strx1:
20413 case DW_FORM_strx2:
20414 case DW_FORM_strx3:
20415 case DW_FORM_strx4:
20416 case DW_FORM_GNU_str_index:
20417 {
20418 unsigned int str_index = attr->as_unsigned_reprocess ();
20419 gdb_assert (!attr->canonical_string_p ());
20420 if (reader->dwo_file != NULL)
20421 attr->set_string_noncanonical (read_dwo_str_index (reader,
20422 str_index));
20423 else
20424 attr->set_string_noncanonical (read_stub_str_index (cu,
20425 str_index));
20426 break;
20427 }
20428 default:
20429 gdb_assert_not_reached (_("Unexpected DWARF form."));
20430 }
20431 }
20432
20433 /* Read an attribute value described by an attribute form. */
20434
20435 static const gdb_byte *
20436 read_attribute_value (const struct die_reader_specs *reader,
20437 struct attribute *attr, unsigned form,
20438 LONGEST implicit_const, const gdb_byte *info_ptr)
20439 {
20440 struct dwarf2_cu *cu = reader->cu;
20441 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20442 struct objfile *objfile = per_objfile->objfile;
20443 bfd *abfd = reader->abfd;
20444 struct comp_unit_head *cu_header = &cu->header;
20445 unsigned int bytes_read;
20446 struct dwarf_block *blk;
20447
20448 attr->form = (enum dwarf_form) form;
20449 switch (form)
20450 {
20451 case DW_FORM_ref_addr:
20452 if (cu_header->version == 2)
20453 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
20454 &bytes_read));
20455 else
20456 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20457 &bytes_read));
20458 info_ptr += bytes_read;
20459 break;
20460 case DW_FORM_GNU_ref_alt:
20461 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20462 &bytes_read));
20463 info_ptr += bytes_read;
20464 break;
20465 case DW_FORM_addr:
20466 {
20467 struct gdbarch *gdbarch = objfile->arch ();
20468 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
20469 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20470 attr->set_address (addr);
20471 info_ptr += bytes_read;
20472 }
20473 break;
20474 case DW_FORM_block2:
20475 blk = dwarf_alloc_block (cu);
20476 blk->size = read_2_bytes (abfd, info_ptr);
20477 info_ptr += 2;
20478 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20479 info_ptr += blk->size;
20480 attr->set_block (blk);
20481 break;
20482 case DW_FORM_block4:
20483 blk = dwarf_alloc_block (cu);
20484 blk->size = read_4_bytes (abfd, info_ptr);
20485 info_ptr += 4;
20486 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20487 info_ptr += blk->size;
20488 attr->set_block (blk);
20489 break;
20490 case DW_FORM_data2:
20491 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
20492 info_ptr += 2;
20493 break;
20494 case DW_FORM_data4:
20495 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
20496 info_ptr += 4;
20497 break;
20498 case DW_FORM_data8:
20499 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
20500 info_ptr += 8;
20501 break;
20502 case DW_FORM_data16:
20503 blk = dwarf_alloc_block (cu);
20504 blk->size = 16;
20505 blk->data = read_n_bytes (abfd, info_ptr, 16);
20506 info_ptr += 16;
20507 attr->set_block (blk);
20508 break;
20509 case DW_FORM_sec_offset:
20510 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20511 &bytes_read));
20512 info_ptr += bytes_read;
20513 break;
20514 case DW_FORM_loclistx:
20515 {
20516 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20517 &bytes_read));
20518 info_ptr += bytes_read;
20519 }
20520 break;
20521 case DW_FORM_string:
20522 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20523 &bytes_read));
20524 info_ptr += bytes_read;
20525 break;
20526 case DW_FORM_strp:
20527 if (!cu->per_cu->is_dwz)
20528 {
20529 attr->set_string_noncanonical
20530 (read_indirect_string (per_objfile,
20531 abfd, info_ptr, cu_header,
20532 &bytes_read));
20533 info_ptr += bytes_read;
20534 break;
20535 }
20536 /* FALLTHROUGH */
20537 case DW_FORM_line_strp:
20538 if (!cu->per_cu->is_dwz)
20539 {
20540 attr->set_string_noncanonical
20541 (per_objfile->read_line_string (info_ptr, cu_header,
20542 &bytes_read));
20543 info_ptr += bytes_read;
20544 break;
20545 }
20546 /* FALLTHROUGH */
20547 case DW_FORM_GNU_strp_alt:
20548 {
20549 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20550 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20551 &bytes_read);
20552
20553 attr->set_string_noncanonical
20554 (dwz->read_string (objfile, str_offset));
20555 info_ptr += bytes_read;
20556 }
20557 break;
20558 case DW_FORM_exprloc:
20559 case DW_FORM_block:
20560 blk = dwarf_alloc_block (cu);
20561 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20562 info_ptr += bytes_read;
20563 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20564 info_ptr += blk->size;
20565 attr->set_block (blk);
20566 break;
20567 case DW_FORM_block1:
20568 blk = dwarf_alloc_block (cu);
20569 blk->size = read_1_byte (abfd, info_ptr);
20570 info_ptr += 1;
20571 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20572 info_ptr += blk->size;
20573 attr->set_block (blk);
20574 break;
20575 case DW_FORM_data1:
20576 case DW_FORM_flag:
20577 attr->set_unsigned (read_1_byte (abfd, info_ptr));
20578 info_ptr += 1;
20579 break;
20580 case DW_FORM_flag_present:
20581 attr->set_unsigned (1);
20582 break;
20583 case DW_FORM_sdata:
20584 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
20585 info_ptr += bytes_read;
20586 break;
20587 case DW_FORM_rnglistx:
20588 {
20589 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20590 &bytes_read));
20591 info_ptr += bytes_read;
20592 }
20593 break;
20594 case DW_FORM_udata:
20595 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
20596 info_ptr += bytes_read;
20597 break;
20598 case DW_FORM_ref1:
20599 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20600 + read_1_byte (abfd, info_ptr)));
20601 info_ptr += 1;
20602 break;
20603 case DW_FORM_ref2:
20604 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20605 + read_2_bytes (abfd, info_ptr)));
20606 info_ptr += 2;
20607 break;
20608 case DW_FORM_ref4:
20609 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20610 + read_4_bytes (abfd, info_ptr)));
20611 info_ptr += 4;
20612 break;
20613 case DW_FORM_ref8:
20614 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20615 + read_8_bytes (abfd, info_ptr)));
20616 info_ptr += 8;
20617 break;
20618 case DW_FORM_ref_sig8:
20619 attr->set_signature (read_8_bytes (abfd, info_ptr));
20620 info_ptr += 8;
20621 break;
20622 case DW_FORM_ref_udata:
20623 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20624 + read_unsigned_leb128 (abfd, info_ptr,
20625 &bytes_read)));
20626 info_ptr += bytes_read;
20627 break;
20628 case DW_FORM_indirect:
20629 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20630 info_ptr += bytes_read;
20631 if (form == DW_FORM_implicit_const)
20632 {
20633 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20634 info_ptr += bytes_read;
20635 }
20636 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
20637 info_ptr);
20638 break;
20639 case DW_FORM_implicit_const:
20640 attr->set_signed (implicit_const);
20641 break;
20642 case DW_FORM_addrx:
20643 case DW_FORM_GNU_addr_index:
20644 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20645 &bytes_read));
20646 info_ptr += bytes_read;
20647 break;
20648 case DW_FORM_strx:
20649 case DW_FORM_strx1:
20650 case DW_FORM_strx2:
20651 case DW_FORM_strx3:
20652 case DW_FORM_strx4:
20653 case DW_FORM_GNU_str_index:
20654 {
20655 ULONGEST str_index;
20656 if (form == DW_FORM_strx1)
20657 {
20658 str_index = read_1_byte (abfd, info_ptr);
20659 info_ptr += 1;
20660 }
20661 else if (form == DW_FORM_strx2)
20662 {
20663 str_index = read_2_bytes (abfd, info_ptr);
20664 info_ptr += 2;
20665 }
20666 else if (form == DW_FORM_strx3)
20667 {
20668 str_index = read_3_bytes (abfd, info_ptr);
20669 info_ptr += 3;
20670 }
20671 else if (form == DW_FORM_strx4)
20672 {
20673 str_index = read_4_bytes (abfd, info_ptr);
20674 info_ptr += 4;
20675 }
20676 else
20677 {
20678 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20679 info_ptr += bytes_read;
20680 }
20681 attr->set_unsigned_reprocess (str_index);
20682 }
20683 break;
20684 default:
20685 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
20686 dwarf_form_name (form),
20687 bfd_get_filename (abfd));
20688 }
20689
20690 /* Super hack. */
20691 if (cu->per_cu->is_dwz && attr->form_is_ref ())
20692 attr->form = DW_FORM_GNU_ref_alt;
20693
20694 /* We have seen instances where the compiler tried to emit a byte
20695 size attribute of -1 which ended up being encoded as an unsigned
20696 0xffffffff. Although 0xffffffff is technically a valid size value,
20697 an object of this size seems pretty unlikely so we can relatively
20698 safely treat these cases as if the size attribute was invalid and
20699 treat them as zero by default. */
20700 if (attr->name == DW_AT_byte_size
20701 && form == DW_FORM_data4
20702 && attr->as_unsigned () >= 0xffffffff)
20703 {
20704 complaint
20705 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20706 hex_string (attr->as_unsigned ()));
20707 attr->set_unsigned (0);
20708 }
20709
20710 return info_ptr;
20711 }
20712
20713 /* Read an attribute described by an abbreviated attribute. */
20714
20715 static const gdb_byte *
20716 read_attribute (const struct die_reader_specs *reader,
20717 struct attribute *attr, const struct attr_abbrev *abbrev,
20718 const gdb_byte *info_ptr)
20719 {
20720 attr->name = abbrev->name;
20721 attr->string_is_canonical = 0;
20722 attr->requires_reprocessing = 0;
20723 return read_attribute_value (reader, attr, abbrev->form,
20724 abbrev->implicit_const, info_ptr);
20725 }
20726
20727 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20728
20729 static const char *
20730 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
20731 LONGEST str_offset)
20732 {
20733 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20734 str_offset, "DW_FORM_strp");
20735 }
20736
20737 /* Return pointer to string at .debug_str offset as read from BUF.
20738 BUF is assumed to be in a compilation unit described by CU_HEADER.
20739 Return *BYTES_READ_PTR count of bytes read from BUF. */
20740
20741 static const char *
20742 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
20743 const gdb_byte *buf,
20744 const struct comp_unit_head *cu_header,
20745 unsigned int *bytes_read_ptr)
20746 {
20747 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20748
20749 return read_indirect_string_at_offset (per_objfile, str_offset);
20750 }
20751
20752 /* See read.h. */
20753
20754 const char *
20755 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20756 const struct comp_unit_head *cu_header,
20757 unsigned int *bytes_read_ptr)
20758 {
20759 bfd *abfd = objfile->obfd;
20760 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20761
20762 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20763 }
20764
20765 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20766 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20767 ADDR_SIZE is the size of addresses from the CU header. */
20768
20769 static CORE_ADDR
20770 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20771 gdb::optional<ULONGEST> addr_base, int addr_size)
20772 {
20773 struct objfile *objfile = per_objfile->objfile;
20774 bfd *abfd = objfile->obfd;
20775 const gdb_byte *info_ptr;
20776 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20777
20778 per_objfile->per_bfd->addr.read (objfile);
20779 if (per_objfile->per_bfd->addr.buffer == NULL)
20780 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20781 objfile_name (objfile));
20782 if (addr_base_or_zero + addr_index * addr_size
20783 >= per_objfile->per_bfd->addr.size)
20784 error (_("DW_FORM_addr_index pointing outside of "
20785 ".debug_addr section [in module %s]"),
20786 objfile_name (objfile));
20787 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20788 + addr_index * addr_size);
20789 if (addr_size == 4)
20790 return bfd_get_32 (abfd, info_ptr);
20791 else
20792 return bfd_get_64 (abfd, info_ptr);
20793 }
20794
20795 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20796
20797 static CORE_ADDR
20798 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20799 {
20800 return read_addr_index_1 (cu->per_objfile, addr_index,
20801 cu->addr_base, cu->header.addr_size);
20802 }
20803
20804 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20805
20806 static CORE_ADDR
20807 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20808 unsigned int *bytes_read)
20809 {
20810 bfd *abfd = cu->per_objfile->objfile->obfd;
20811 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20812
20813 return read_addr_index (cu, addr_index);
20814 }
20815
20816 /* See read.h. */
20817
20818 CORE_ADDR
20819 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20820 dwarf2_per_objfile *per_objfile,
20821 unsigned int addr_index)
20822 {
20823 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20824 gdb::optional<ULONGEST> addr_base;
20825 int addr_size;
20826
20827 /* We need addr_base and addr_size.
20828 If we don't have PER_CU->cu, we have to get it.
20829 Nasty, but the alternative is storing the needed info in PER_CU,
20830 which at this point doesn't seem justified: it's not clear how frequently
20831 it would get used and it would increase the size of every PER_CU.
20832 Entry points like dwarf2_per_cu_addr_size do a similar thing
20833 so we're not in uncharted territory here.
20834 Alas we need to be a bit more complicated as addr_base is contained
20835 in the DIE.
20836
20837 We don't need to read the entire CU(/TU).
20838 We just need the header and top level die.
20839
20840 IWBN to use the aging mechanism to let us lazily later discard the CU.
20841 For now we skip this optimization. */
20842
20843 if (cu != NULL)
20844 {
20845 addr_base = cu->addr_base;
20846 addr_size = cu->header.addr_size;
20847 }
20848 else
20849 {
20850 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
20851 addr_base = reader.cu->addr_base;
20852 addr_size = reader.cu->header.addr_size;
20853 }
20854
20855 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
20856 }
20857
20858 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20859 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20860 DWO file. */
20861
20862 static const char *
20863 read_str_index (struct dwarf2_cu *cu,
20864 struct dwarf2_section_info *str_section,
20865 struct dwarf2_section_info *str_offsets_section,
20866 ULONGEST str_offsets_base, ULONGEST str_index)
20867 {
20868 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20869 struct objfile *objfile = per_objfile->objfile;
20870 const char *objf_name = objfile_name (objfile);
20871 bfd *abfd = objfile->obfd;
20872 const gdb_byte *info_ptr;
20873 ULONGEST str_offset;
20874 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20875
20876 str_section->read (objfile);
20877 str_offsets_section->read (objfile);
20878 if (str_section->buffer == NULL)
20879 error (_("%s used without %s section"
20880 " in CU at offset %s [in module %s]"),
20881 form_name, str_section->get_name (),
20882 sect_offset_str (cu->header.sect_off), objf_name);
20883 if (str_offsets_section->buffer == NULL)
20884 error (_("%s used without %s section"
20885 " in CU at offset %s [in module %s]"),
20886 form_name, str_section->get_name (),
20887 sect_offset_str (cu->header.sect_off), objf_name);
20888 info_ptr = (str_offsets_section->buffer
20889 + str_offsets_base
20890 + str_index * cu->header.offset_size);
20891 if (cu->header.offset_size == 4)
20892 str_offset = bfd_get_32 (abfd, info_ptr);
20893 else
20894 str_offset = bfd_get_64 (abfd, info_ptr);
20895 if (str_offset >= str_section->size)
20896 error (_("Offset from %s pointing outside of"
20897 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20898 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20899 return (const char *) (str_section->buffer + str_offset);
20900 }
20901
20902 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20903
20904 static const char *
20905 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20906 {
20907 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20908 ? reader->cu->header.addr_size : 0;
20909 return read_str_index (reader->cu,
20910 &reader->dwo_file->sections.str,
20911 &reader->dwo_file->sections.str_offsets,
20912 str_offsets_base, str_index);
20913 }
20914
20915 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20916
20917 static const char *
20918 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20919 {
20920 struct objfile *objfile = cu->per_objfile->objfile;
20921 const char *objf_name = objfile_name (objfile);
20922 static const char form_name[] = "DW_FORM_GNU_str_index";
20923 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20924
20925 if (!cu->str_offsets_base.has_value ())
20926 error (_("%s used in Fission stub without %s"
20927 " in CU at offset 0x%lx [in module %s]"),
20928 form_name, str_offsets_attr_name,
20929 (long) cu->header.offset_size, objf_name);
20930
20931 return read_str_index (cu,
20932 &cu->per_objfile->per_bfd->str,
20933 &cu->per_objfile->per_bfd->str_offsets,
20934 *cu->str_offsets_base, str_index);
20935 }
20936
20937 /* Return the length of an LEB128 number in BUF. */
20938
20939 static int
20940 leb128_size (const gdb_byte *buf)
20941 {
20942 const gdb_byte *begin = buf;
20943 gdb_byte byte;
20944
20945 while (1)
20946 {
20947 byte = *buf++;
20948 if ((byte & 128) == 0)
20949 return buf - begin;
20950 }
20951 }
20952
20953 static void
20954 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20955 {
20956 switch (lang)
20957 {
20958 case DW_LANG_C89:
20959 case DW_LANG_C99:
20960 case DW_LANG_C11:
20961 case DW_LANG_C:
20962 case DW_LANG_UPC:
20963 cu->language = language_c;
20964 break;
20965 case DW_LANG_Java:
20966 case DW_LANG_C_plus_plus:
20967 case DW_LANG_C_plus_plus_11:
20968 case DW_LANG_C_plus_plus_14:
20969 cu->language = language_cplus;
20970 break;
20971 case DW_LANG_D:
20972 cu->language = language_d;
20973 break;
20974 case DW_LANG_Fortran77:
20975 case DW_LANG_Fortran90:
20976 case DW_LANG_Fortran95:
20977 case DW_LANG_Fortran03:
20978 case DW_LANG_Fortran08:
20979 cu->language = language_fortran;
20980 break;
20981 case DW_LANG_Go:
20982 cu->language = language_go;
20983 break;
20984 case DW_LANG_Mips_Assembler:
20985 cu->language = language_asm;
20986 break;
20987 case DW_LANG_Ada83:
20988 case DW_LANG_Ada95:
20989 cu->language = language_ada;
20990 break;
20991 case DW_LANG_Modula2:
20992 cu->language = language_m2;
20993 break;
20994 case DW_LANG_Pascal83:
20995 cu->language = language_pascal;
20996 break;
20997 case DW_LANG_ObjC:
20998 cu->language = language_objc;
20999 break;
21000 case DW_LANG_Rust:
21001 case DW_LANG_Rust_old:
21002 cu->language = language_rust;
21003 break;
21004 case DW_LANG_Cobol74:
21005 case DW_LANG_Cobol85:
21006 default:
21007 cu->language = language_minimal;
21008 break;
21009 }
21010 cu->language_defn = language_def (cu->language);
21011 }
21012
21013 /* Return the named attribute or NULL if not there. */
21014
21015 static struct attribute *
21016 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
21017 {
21018 for (;;)
21019 {
21020 unsigned int i;
21021 struct attribute *spec = NULL;
21022
21023 for (i = 0; i < die->num_attrs; ++i)
21024 {
21025 if (die->attrs[i].name == name)
21026 return &die->attrs[i];
21027 if (die->attrs[i].name == DW_AT_specification
21028 || die->attrs[i].name == DW_AT_abstract_origin)
21029 spec = &die->attrs[i];
21030 }
21031
21032 if (!spec)
21033 break;
21034
21035 die = follow_die_ref (die, spec, &cu);
21036 }
21037
21038 return NULL;
21039 }
21040
21041 /* Return the string associated with a string-typed attribute, or NULL if it
21042 is either not found or is of an incorrect type. */
21043
21044 static const char *
21045 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
21046 {
21047 struct attribute *attr;
21048 const char *str = NULL;
21049
21050 attr = dwarf2_attr (die, name, cu);
21051
21052 if (attr != NULL)
21053 {
21054 str = attr->as_string ();
21055 if (str == nullptr)
21056 complaint (_("string type expected for attribute %s for "
21057 "DIE at %s in module %s"),
21058 dwarf_attr_name (name), sect_offset_str (die->sect_off),
21059 objfile_name (cu->per_objfile->objfile));
21060 }
21061
21062 return str;
21063 }
21064
21065 /* Return the dwo name or NULL if not present. If present, it is in either
21066 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
21067 static const char *
21068 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
21069 {
21070 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
21071 if (dwo_name == nullptr)
21072 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
21073 return dwo_name;
21074 }
21075
21076 /* Return non-zero iff the attribute NAME is defined for the given DIE,
21077 and holds a non-zero value. This function should only be used for
21078 DW_FORM_flag or DW_FORM_flag_present attributes. */
21079
21080 static int
21081 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
21082 {
21083 struct attribute *attr = dwarf2_attr (die, name, cu);
21084
21085 return attr != nullptr && attr->as_boolean ();
21086 }
21087
21088 static int
21089 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
21090 {
21091 /* A DIE is a declaration if it has a DW_AT_declaration attribute
21092 which value is non-zero. However, we have to be careful with
21093 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
21094 (via dwarf2_flag_true_p) follows this attribute. So we may
21095 end up accidently finding a declaration attribute that belongs
21096 to a different DIE referenced by the specification attribute,
21097 even though the given DIE does not have a declaration attribute. */
21098 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
21099 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
21100 }
21101
21102 /* Return the die giving the specification for DIE, if there is
21103 one. *SPEC_CU is the CU containing DIE on input, and the CU
21104 containing the return value on output. If there is no
21105 specification, but there is an abstract origin, that is
21106 returned. */
21107
21108 static struct die_info *
21109 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
21110 {
21111 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
21112 *spec_cu);
21113
21114 if (spec_attr == NULL)
21115 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
21116
21117 if (spec_attr == NULL)
21118 return NULL;
21119 else
21120 return follow_die_ref (die, spec_attr, spec_cu);
21121 }
21122
21123 /* Stub for free_line_header to match void * callback types. */
21124
21125 static void
21126 free_line_header_voidp (void *arg)
21127 {
21128 struct line_header *lh = (struct line_header *) arg;
21129
21130 delete lh;
21131 }
21132
21133 /* A convenience function to find the proper .debug_line section for a CU. */
21134
21135 static struct dwarf2_section_info *
21136 get_debug_line_section (struct dwarf2_cu *cu)
21137 {
21138 struct dwarf2_section_info *section;
21139 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21140
21141 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
21142 DWO file. */
21143 if (cu->dwo_unit && cu->per_cu->is_debug_types)
21144 section = &cu->dwo_unit->dwo_file->sections.line;
21145 else if (cu->per_cu->is_dwz)
21146 {
21147 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
21148
21149 section = &dwz->line;
21150 }
21151 else
21152 section = &per_objfile->per_bfd->line;
21153
21154 return section;
21155 }
21156
21157 /* Read the statement program header starting at OFFSET in
21158 .debug_line, or .debug_line.dwo. Return a pointer
21159 to a struct line_header, allocated using xmalloc.
21160 Returns NULL if there is a problem reading the header, e.g., if it
21161 has a version we don't understand.
21162
21163 NOTE: the strings in the include directory and file name tables of
21164 the returned object point into the dwarf line section buffer,
21165 and must not be freed. */
21166
21167 static line_header_up
21168 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
21169 {
21170 struct dwarf2_section_info *section;
21171 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21172
21173 section = get_debug_line_section (cu);
21174 section->read (per_objfile->objfile);
21175 if (section->buffer == NULL)
21176 {
21177 if (cu->dwo_unit && cu->per_cu->is_debug_types)
21178 complaint (_("missing .debug_line.dwo section"));
21179 else
21180 complaint (_("missing .debug_line section"));
21181 return 0;
21182 }
21183
21184 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
21185 per_objfile, section, &cu->header);
21186 }
21187
21188 /* Subroutine of dwarf_decode_lines to simplify it.
21189 Return the file name of the psymtab for the given file_entry.
21190 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21191 If space for the result is malloc'd, *NAME_HOLDER will be set.
21192 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
21193
21194 static const char *
21195 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
21196 const dwarf2_psymtab *pst,
21197 const char *comp_dir,
21198 gdb::unique_xmalloc_ptr<char> *name_holder)
21199 {
21200 const char *include_name = fe.name;
21201 const char *include_name_to_compare = include_name;
21202 const char *pst_filename;
21203 int file_is_pst;
21204
21205 const char *dir_name = fe.include_dir (lh);
21206
21207 gdb::unique_xmalloc_ptr<char> hold_compare;
21208 if (!IS_ABSOLUTE_PATH (include_name)
21209 && (dir_name != NULL || comp_dir != NULL))
21210 {
21211 /* Avoid creating a duplicate psymtab for PST.
21212 We do this by comparing INCLUDE_NAME and PST_FILENAME.
21213 Before we do the comparison, however, we need to account
21214 for DIR_NAME and COMP_DIR.
21215 First prepend dir_name (if non-NULL). If we still don't
21216 have an absolute path prepend comp_dir (if non-NULL).
21217 However, the directory we record in the include-file's
21218 psymtab does not contain COMP_DIR (to match the
21219 corresponding symtab(s)).
21220
21221 Example:
21222
21223 bash$ cd /tmp
21224 bash$ gcc -g ./hello.c
21225 include_name = "hello.c"
21226 dir_name = "."
21227 DW_AT_comp_dir = comp_dir = "/tmp"
21228 DW_AT_name = "./hello.c"
21229
21230 */
21231
21232 if (dir_name != NULL)
21233 {
21234 name_holder->reset (concat (dir_name, SLASH_STRING,
21235 include_name, (char *) NULL));
21236 include_name = name_holder->get ();
21237 include_name_to_compare = include_name;
21238 }
21239 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
21240 {
21241 hold_compare.reset (concat (comp_dir, SLASH_STRING,
21242 include_name, (char *) NULL));
21243 include_name_to_compare = hold_compare.get ();
21244 }
21245 }
21246
21247 pst_filename = pst->filename;
21248 gdb::unique_xmalloc_ptr<char> copied_name;
21249 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
21250 {
21251 copied_name.reset (concat (pst->dirname, SLASH_STRING,
21252 pst_filename, (char *) NULL));
21253 pst_filename = copied_name.get ();
21254 }
21255
21256 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
21257
21258 if (file_is_pst)
21259 return NULL;
21260 return include_name;
21261 }
21262
21263 /* State machine to track the state of the line number program. */
21264
21265 class lnp_state_machine
21266 {
21267 public:
21268 /* Initialize a machine state for the start of a line number
21269 program. */
21270 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
21271 bool record_lines_p);
21272
21273 file_entry *current_file ()
21274 {
21275 /* lh->file_names is 0-based, but the file name numbers in the
21276 statement program are 1-based. */
21277 return m_line_header->file_name_at (m_file);
21278 }
21279
21280 /* Record the line in the state machine. END_SEQUENCE is true if
21281 we're processing the end of a sequence. */
21282 void record_line (bool end_sequence);
21283
21284 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
21285 nop-out rest of the lines in this sequence. */
21286 void check_line_address (struct dwarf2_cu *cu,
21287 const gdb_byte *line_ptr,
21288 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
21289
21290 void handle_set_discriminator (unsigned int discriminator)
21291 {
21292 m_discriminator = discriminator;
21293 m_line_has_non_zero_discriminator |= discriminator != 0;
21294 }
21295
21296 /* Handle DW_LNE_set_address. */
21297 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21298 {
21299 m_op_index = 0;
21300 address += baseaddr;
21301 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21302 }
21303
21304 /* Handle DW_LNS_advance_pc. */
21305 void handle_advance_pc (CORE_ADDR adjust);
21306
21307 /* Handle a special opcode. */
21308 void handle_special_opcode (unsigned char op_code);
21309
21310 /* Handle DW_LNS_advance_line. */
21311 void handle_advance_line (int line_delta)
21312 {
21313 advance_line (line_delta);
21314 }
21315
21316 /* Handle DW_LNS_set_file. */
21317 void handle_set_file (file_name_index file);
21318
21319 /* Handle DW_LNS_negate_stmt. */
21320 void handle_negate_stmt ()
21321 {
21322 m_is_stmt = !m_is_stmt;
21323 }
21324
21325 /* Handle DW_LNS_const_add_pc. */
21326 void handle_const_add_pc ();
21327
21328 /* Handle DW_LNS_fixed_advance_pc. */
21329 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21330 {
21331 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21332 m_op_index = 0;
21333 }
21334
21335 /* Handle DW_LNS_copy. */
21336 void handle_copy ()
21337 {
21338 record_line (false);
21339 m_discriminator = 0;
21340 }
21341
21342 /* Handle DW_LNE_end_sequence. */
21343 void handle_end_sequence ()
21344 {
21345 m_currently_recording_lines = true;
21346 }
21347
21348 private:
21349 /* Advance the line by LINE_DELTA. */
21350 void advance_line (int line_delta)
21351 {
21352 m_line += line_delta;
21353
21354 if (line_delta != 0)
21355 m_line_has_non_zero_discriminator = m_discriminator != 0;
21356 }
21357
21358 struct dwarf2_cu *m_cu;
21359
21360 gdbarch *m_gdbarch;
21361
21362 /* True if we're recording lines.
21363 Otherwise we're building partial symtabs and are just interested in
21364 finding include files mentioned by the line number program. */
21365 bool m_record_lines_p;
21366
21367 /* The line number header. */
21368 line_header *m_line_header;
21369
21370 /* These are part of the standard DWARF line number state machine,
21371 and initialized according to the DWARF spec. */
21372
21373 unsigned char m_op_index = 0;
21374 /* The line table index of the current file. */
21375 file_name_index m_file = 1;
21376 unsigned int m_line = 1;
21377
21378 /* These are initialized in the constructor. */
21379
21380 CORE_ADDR m_address;
21381 bool m_is_stmt;
21382 unsigned int m_discriminator;
21383
21384 /* Additional bits of state we need to track. */
21385
21386 /* The last file that we called dwarf2_start_subfile for.
21387 This is only used for TLLs. */
21388 unsigned int m_last_file = 0;
21389 /* The last file a line number was recorded for. */
21390 struct subfile *m_last_subfile = NULL;
21391
21392 /* The address of the last line entry. */
21393 CORE_ADDR m_last_address;
21394
21395 /* Set to true when a previous line at the same address (using
21396 m_last_address) had m_is_stmt true. This is reset to false when a
21397 line entry at a new address (m_address different to m_last_address) is
21398 processed. */
21399 bool m_stmt_at_address = false;
21400
21401 /* When true, record the lines we decode. */
21402 bool m_currently_recording_lines = false;
21403
21404 /* The last line number that was recorded, used to coalesce
21405 consecutive entries for the same line. This can happen, for
21406 example, when discriminators are present. PR 17276. */
21407 unsigned int m_last_line = 0;
21408 bool m_line_has_non_zero_discriminator = false;
21409 };
21410
21411 void
21412 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21413 {
21414 CORE_ADDR addr_adj = (((m_op_index + adjust)
21415 / m_line_header->maximum_ops_per_instruction)
21416 * m_line_header->minimum_instruction_length);
21417 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21418 m_op_index = ((m_op_index + adjust)
21419 % m_line_header->maximum_ops_per_instruction);
21420 }
21421
21422 void
21423 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21424 {
21425 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21426 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21427 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21428 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
21429 / m_line_header->maximum_ops_per_instruction)
21430 * m_line_header->minimum_instruction_length);
21431 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21432 m_op_index = ((m_op_index + adj_opcode_d)
21433 % m_line_header->maximum_ops_per_instruction);
21434
21435 int line_delta = m_line_header->line_base + adj_opcode_r;
21436 advance_line (line_delta);
21437 record_line (false);
21438 m_discriminator = 0;
21439 }
21440
21441 void
21442 lnp_state_machine::handle_set_file (file_name_index file)
21443 {
21444 m_file = file;
21445
21446 const file_entry *fe = current_file ();
21447 if (fe == NULL)
21448 dwarf2_debug_line_missing_file_complaint ();
21449 else if (m_record_lines_p)
21450 {
21451 const char *dir = fe->include_dir (m_line_header);
21452
21453 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21454 m_line_has_non_zero_discriminator = m_discriminator != 0;
21455 dwarf2_start_subfile (m_cu, fe->name, dir);
21456 }
21457 }
21458
21459 void
21460 lnp_state_machine::handle_const_add_pc ()
21461 {
21462 CORE_ADDR adjust
21463 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21464
21465 CORE_ADDR addr_adj
21466 = (((m_op_index + adjust)
21467 / m_line_header->maximum_ops_per_instruction)
21468 * m_line_header->minimum_instruction_length);
21469
21470 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21471 m_op_index = ((m_op_index + adjust)
21472 % m_line_header->maximum_ops_per_instruction);
21473 }
21474
21475 /* Return non-zero if we should add LINE to the line number table.
21476 LINE is the line to add, LAST_LINE is the last line that was added,
21477 LAST_SUBFILE is the subfile for LAST_LINE.
21478 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21479 had a non-zero discriminator.
21480
21481 We have to be careful in the presence of discriminators.
21482 E.g., for this line:
21483
21484 for (i = 0; i < 100000; i++);
21485
21486 clang can emit four line number entries for that one line,
21487 each with a different discriminator.
21488 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21489
21490 However, we want gdb to coalesce all four entries into one.
21491 Otherwise the user could stepi into the middle of the line and
21492 gdb would get confused about whether the pc really was in the
21493 middle of the line.
21494
21495 Things are further complicated by the fact that two consecutive
21496 line number entries for the same line is a heuristic used by gcc
21497 to denote the end of the prologue. So we can't just discard duplicate
21498 entries, we have to be selective about it. The heuristic we use is
21499 that we only collapse consecutive entries for the same line if at least
21500 one of those entries has a non-zero discriminator. PR 17276.
21501
21502 Note: Addresses in the line number state machine can never go backwards
21503 within one sequence, thus this coalescing is ok. */
21504
21505 static int
21506 dwarf_record_line_p (struct dwarf2_cu *cu,
21507 unsigned int line, unsigned int last_line,
21508 int line_has_non_zero_discriminator,
21509 struct subfile *last_subfile)
21510 {
21511 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21512 return 1;
21513 if (line != last_line)
21514 return 1;
21515 /* Same line for the same file that we've seen already.
21516 As a last check, for pr 17276, only record the line if the line
21517 has never had a non-zero discriminator. */
21518 if (!line_has_non_zero_discriminator)
21519 return 1;
21520 return 0;
21521 }
21522
21523 /* Use the CU's builder to record line number LINE beginning at
21524 address ADDRESS in the line table of subfile SUBFILE. */
21525
21526 static void
21527 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21528 unsigned int line, CORE_ADDR address, bool is_stmt,
21529 struct dwarf2_cu *cu)
21530 {
21531 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21532
21533 if (dwarf_line_debug)
21534 {
21535 fprintf_unfiltered (gdb_stdlog,
21536 "Recording line %u, file %s, address %s\n",
21537 line, lbasename (subfile->name),
21538 paddress (gdbarch, address));
21539 }
21540
21541 if (cu != nullptr)
21542 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
21543 }
21544
21545 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21546 Mark the end of a set of line number records.
21547 The arguments are the same as for dwarf_record_line_1.
21548 If SUBFILE is NULL the request is ignored. */
21549
21550 static void
21551 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21552 CORE_ADDR address, struct dwarf2_cu *cu)
21553 {
21554 if (subfile == NULL)
21555 return;
21556
21557 if (dwarf_line_debug)
21558 {
21559 fprintf_unfiltered (gdb_stdlog,
21560 "Finishing current line, file %s, address %s\n",
21561 lbasename (subfile->name),
21562 paddress (gdbarch, address));
21563 }
21564
21565 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
21566 }
21567
21568 void
21569 lnp_state_machine::record_line (bool end_sequence)
21570 {
21571 if (dwarf_line_debug)
21572 {
21573 fprintf_unfiltered (gdb_stdlog,
21574 "Processing actual line %u: file %u,"
21575 " address %s, is_stmt %u, discrim %u%s\n",
21576 m_line, m_file,
21577 paddress (m_gdbarch, m_address),
21578 m_is_stmt, m_discriminator,
21579 (end_sequence ? "\t(end sequence)" : ""));
21580 }
21581
21582 file_entry *fe = current_file ();
21583
21584 if (fe == NULL)
21585 dwarf2_debug_line_missing_file_complaint ();
21586 /* For now we ignore lines not starting on an instruction boundary.
21587 But not when processing end_sequence for compatibility with the
21588 previous version of the code. */
21589 else if (m_op_index == 0 || end_sequence)
21590 {
21591 fe->included_p = 1;
21592 if (m_record_lines_p)
21593 {
21594 /* When we switch files we insert an end maker in the first file,
21595 switch to the second file and add a new line entry. The
21596 problem is that the end marker inserted in the first file will
21597 discard any previous line entries at the same address. If the
21598 line entries in the first file are marked as is-stmt, while
21599 the new line in the second file is non-stmt, then this means
21600 the end marker will discard is-stmt lines so we can have a
21601 non-stmt line. This means that there are less addresses at
21602 which the user can insert a breakpoint.
21603
21604 To improve this we track the last address in m_last_address,
21605 and whether we have seen an is-stmt at this address. Then
21606 when switching files, if we have seen a stmt at the current
21607 address, and we are switching to create a non-stmt line, then
21608 discard the new line. */
21609 bool file_changed
21610 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21611 bool ignore_this_line
21612 = ((file_changed && !end_sequence && m_last_address == m_address
21613 && !m_is_stmt && m_stmt_at_address)
21614 || (!end_sequence && m_line == 0));
21615
21616 if ((file_changed && !ignore_this_line) || end_sequence)
21617 {
21618 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21619 m_currently_recording_lines ? m_cu : nullptr);
21620 }
21621
21622 if (!end_sequence && !ignore_this_line)
21623 {
21624 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21625
21626 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21627 m_line_has_non_zero_discriminator,
21628 m_last_subfile))
21629 {
21630 buildsym_compunit *builder = m_cu->get_builder ();
21631 dwarf_record_line_1 (m_gdbarch,
21632 builder->get_current_subfile (),
21633 m_line, m_address, is_stmt,
21634 m_currently_recording_lines ? m_cu : nullptr);
21635 }
21636 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21637 m_last_line = m_line;
21638 }
21639 }
21640 }
21641
21642 /* Track whether we have seen any m_is_stmt true at m_address in case we
21643 have multiple line table entries all at m_address. */
21644 if (m_last_address != m_address)
21645 {
21646 m_stmt_at_address = false;
21647 m_last_address = m_address;
21648 }
21649 m_stmt_at_address |= m_is_stmt;
21650 }
21651
21652 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21653 line_header *lh, bool record_lines_p)
21654 {
21655 m_cu = cu;
21656 m_gdbarch = arch;
21657 m_record_lines_p = record_lines_p;
21658 m_line_header = lh;
21659
21660 m_currently_recording_lines = true;
21661
21662 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21663 was a line entry for it so that the backend has a chance to adjust it
21664 and also record it in case it needs it. This is currently used by MIPS
21665 code, cf. `mips_adjust_dwarf2_line'. */
21666 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21667 m_is_stmt = lh->default_is_stmt;
21668 m_discriminator = 0;
21669
21670 m_last_address = m_address;
21671 m_stmt_at_address = false;
21672 }
21673
21674 void
21675 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21676 const gdb_byte *line_ptr,
21677 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21678 {
21679 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21680 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21681 located at 0x0. In this case, additionally check that if
21682 ADDRESS < UNRELOCATED_LOWPC. */
21683
21684 if ((address == 0 && address < unrelocated_lowpc)
21685 || address == (CORE_ADDR) -1)
21686 {
21687 /* This line table is for a function which has been
21688 GCd by the linker. Ignore it. PR gdb/12528 */
21689
21690 struct objfile *objfile = cu->per_objfile->objfile;
21691 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21692
21693 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21694 line_offset, objfile_name (objfile));
21695 m_currently_recording_lines = false;
21696 /* Note: m_currently_recording_lines is left as false until we see
21697 DW_LNE_end_sequence. */
21698 }
21699 }
21700
21701 /* Subroutine of dwarf_decode_lines to simplify it.
21702 Process the line number information in LH.
21703 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21704 program in order to set included_p for every referenced header. */
21705
21706 static void
21707 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21708 const int decode_for_pst_p, CORE_ADDR lowpc)
21709 {
21710 const gdb_byte *line_ptr, *extended_end;
21711 const gdb_byte *line_end;
21712 unsigned int bytes_read, extended_len;
21713 unsigned char op_code, extended_op;
21714 CORE_ADDR baseaddr;
21715 struct objfile *objfile = cu->per_objfile->objfile;
21716 bfd *abfd = objfile->obfd;
21717 struct gdbarch *gdbarch = objfile->arch ();
21718 /* True if we're recording line info (as opposed to building partial
21719 symtabs and just interested in finding include files mentioned by
21720 the line number program). */
21721 bool record_lines_p = !decode_for_pst_p;
21722
21723 baseaddr = objfile->text_section_offset ();
21724
21725 line_ptr = lh->statement_program_start;
21726 line_end = lh->statement_program_end;
21727
21728 /* Read the statement sequences until there's nothing left. */
21729 while (line_ptr < line_end)
21730 {
21731 /* The DWARF line number program state machine. Reset the state
21732 machine at the start of each sequence. */
21733 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21734 bool end_sequence = false;
21735
21736 if (record_lines_p)
21737 {
21738 /* Start a subfile for the current file of the state
21739 machine. */
21740 const file_entry *fe = state_machine.current_file ();
21741
21742 if (fe != NULL)
21743 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21744 }
21745
21746 /* Decode the table. */
21747 while (line_ptr < line_end && !end_sequence)
21748 {
21749 op_code = read_1_byte (abfd, line_ptr);
21750 line_ptr += 1;
21751
21752 if (op_code >= lh->opcode_base)
21753 {
21754 /* Special opcode. */
21755 state_machine.handle_special_opcode (op_code);
21756 }
21757 else switch (op_code)
21758 {
21759 case DW_LNS_extended_op:
21760 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21761 &bytes_read);
21762 line_ptr += bytes_read;
21763 extended_end = line_ptr + extended_len;
21764 extended_op = read_1_byte (abfd, line_ptr);
21765 line_ptr += 1;
21766 if (DW_LNE_lo_user <= extended_op
21767 && extended_op <= DW_LNE_hi_user)
21768 {
21769 /* Vendor extension, ignore. */
21770 line_ptr = extended_end;
21771 break;
21772 }
21773 switch (extended_op)
21774 {
21775 case DW_LNE_end_sequence:
21776 state_machine.handle_end_sequence ();
21777 end_sequence = true;
21778 break;
21779 case DW_LNE_set_address:
21780 {
21781 CORE_ADDR address
21782 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21783 line_ptr += bytes_read;
21784
21785 state_machine.check_line_address (cu, line_ptr,
21786 lowpc - baseaddr, address);
21787 state_machine.handle_set_address (baseaddr, address);
21788 }
21789 break;
21790 case DW_LNE_define_file:
21791 {
21792 const char *cur_file;
21793 unsigned int mod_time, length;
21794 dir_index dindex;
21795
21796 cur_file = read_direct_string (abfd, line_ptr,
21797 &bytes_read);
21798 line_ptr += bytes_read;
21799 dindex = (dir_index)
21800 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21801 line_ptr += bytes_read;
21802 mod_time =
21803 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21804 line_ptr += bytes_read;
21805 length =
21806 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21807 line_ptr += bytes_read;
21808 lh->add_file_name (cur_file, dindex, mod_time, length);
21809 }
21810 break;
21811 case DW_LNE_set_discriminator:
21812 {
21813 /* The discriminator is not interesting to the
21814 debugger; just ignore it. We still need to
21815 check its value though:
21816 if there are consecutive entries for the same
21817 (non-prologue) line we want to coalesce them.
21818 PR 17276. */
21819 unsigned int discr
21820 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21821 line_ptr += bytes_read;
21822
21823 state_machine.handle_set_discriminator (discr);
21824 }
21825 break;
21826 default:
21827 complaint (_("mangled .debug_line section"));
21828 return;
21829 }
21830 /* Make sure that we parsed the extended op correctly. If e.g.
21831 we expected a different address size than the producer used,
21832 we may have read the wrong number of bytes. */
21833 if (line_ptr != extended_end)
21834 {
21835 complaint (_("mangled .debug_line section"));
21836 return;
21837 }
21838 break;
21839 case DW_LNS_copy:
21840 state_machine.handle_copy ();
21841 break;
21842 case DW_LNS_advance_pc:
21843 {
21844 CORE_ADDR adjust
21845 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21846 line_ptr += bytes_read;
21847
21848 state_machine.handle_advance_pc (adjust);
21849 }
21850 break;
21851 case DW_LNS_advance_line:
21852 {
21853 int line_delta
21854 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21855 line_ptr += bytes_read;
21856
21857 state_machine.handle_advance_line (line_delta);
21858 }
21859 break;
21860 case DW_LNS_set_file:
21861 {
21862 file_name_index file
21863 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21864 &bytes_read);
21865 line_ptr += bytes_read;
21866
21867 state_machine.handle_set_file (file);
21868 }
21869 break;
21870 case DW_LNS_set_column:
21871 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21872 line_ptr += bytes_read;
21873 break;
21874 case DW_LNS_negate_stmt:
21875 state_machine.handle_negate_stmt ();
21876 break;
21877 case DW_LNS_set_basic_block:
21878 break;
21879 /* Add to the address register of the state machine the
21880 address increment value corresponding to special opcode
21881 255. I.e., this value is scaled by the minimum
21882 instruction length since special opcode 255 would have
21883 scaled the increment. */
21884 case DW_LNS_const_add_pc:
21885 state_machine.handle_const_add_pc ();
21886 break;
21887 case DW_LNS_fixed_advance_pc:
21888 {
21889 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21890 line_ptr += 2;
21891
21892 state_machine.handle_fixed_advance_pc (addr_adj);
21893 }
21894 break;
21895 default:
21896 {
21897 /* Unknown standard opcode, ignore it. */
21898 int i;
21899
21900 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21901 {
21902 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21903 line_ptr += bytes_read;
21904 }
21905 }
21906 }
21907 }
21908
21909 if (!end_sequence)
21910 dwarf2_debug_line_missing_end_sequence_complaint ();
21911
21912 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21913 in which case we still finish recording the last line). */
21914 state_machine.record_line (true);
21915 }
21916 }
21917
21918 /* Decode the Line Number Program (LNP) for the given line_header
21919 structure and CU. The actual information extracted and the type
21920 of structures created from the LNP depends on the value of PST.
21921
21922 1. If PST is NULL, then this procedure uses the data from the program
21923 to create all necessary symbol tables, and their linetables.
21924
21925 2. If PST is not NULL, this procedure reads the program to determine
21926 the list of files included by the unit represented by PST, and
21927 builds all the associated partial symbol tables.
21928
21929 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21930 It is used for relative paths in the line table.
21931 NOTE: When processing partial symtabs (pst != NULL),
21932 comp_dir == pst->dirname.
21933
21934 NOTE: It is important that psymtabs have the same file name (via strcmp)
21935 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21936 symtab we don't use it in the name of the psymtabs we create.
21937 E.g. expand_line_sal requires this when finding psymtabs to expand.
21938 A good testcase for this is mb-inline.exp.
21939
21940 LOWPC is the lowest address in CU (or 0 if not known).
21941
21942 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21943 for its PC<->lines mapping information. Otherwise only the filename
21944 table is read in. */
21945
21946 static void
21947 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21948 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21949 CORE_ADDR lowpc, int decode_mapping)
21950 {
21951 struct objfile *objfile = cu->per_objfile->objfile;
21952 const int decode_for_pst_p = (pst != NULL);
21953
21954 if (decode_mapping)
21955 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21956
21957 if (decode_for_pst_p)
21958 {
21959 /* Now that we're done scanning the Line Header Program, we can
21960 create the psymtab of each included file. */
21961 for (auto &file_entry : lh->file_names ())
21962 if (file_entry.included_p == 1)
21963 {
21964 gdb::unique_xmalloc_ptr<char> name_holder;
21965 const char *include_name =
21966 psymtab_include_file_name (lh, file_entry, pst,
21967 comp_dir, &name_holder);
21968 if (include_name != NULL)
21969 dwarf2_create_include_psymtab (include_name, pst, objfile);
21970 }
21971 }
21972 else
21973 {
21974 /* Make sure a symtab is created for every file, even files
21975 which contain only variables (i.e. no code with associated
21976 line numbers). */
21977 buildsym_compunit *builder = cu->get_builder ();
21978 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21979
21980 for (auto &fe : lh->file_names ())
21981 {
21982 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21983 if (builder->get_current_subfile ()->symtab == NULL)
21984 {
21985 builder->get_current_subfile ()->symtab
21986 = allocate_symtab (cust,
21987 builder->get_current_subfile ()->name);
21988 }
21989 fe.symtab = builder->get_current_subfile ()->symtab;
21990 }
21991 }
21992 }
21993
21994 /* Start a subfile for DWARF. FILENAME is the name of the file and
21995 DIRNAME the name of the source directory which contains FILENAME
21996 or NULL if not known.
21997 This routine tries to keep line numbers from identical absolute and
21998 relative file names in a common subfile.
21999
22000 Using the `list' example from the GDB testsuite, which resides in
22001 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
22002 of /srcdir/list0.c yields the following debugging information for list0.c:
22003
22004 DW_AT_name: /srcdir/list0.c
22005 DW_AT_comp_dir: /compdir
22006 files.files[0].name: list0.h
22007 files.files[0].dir: /srcdir
22008 files.files[1].name: list0.c
22009 files.files[1].dir: /srcdir
22010
22011 The line number information for list0.c has to end up in a single
22012 subfile, so that `break /srcdir/list0.c:1' works as expected.
22013 start_subfile will ensure that this happens provided that we pass the
22014 concatenation of files.files[1].dir and files.files[1].name as the
22015 subfile's name. */
22016
22017 static void
22018 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
22019 const char *dirname)
22020 {
22021 gdb::unique_xmalloc_ptr<char> copy;
22022
22023 /* In order not to lose the line information directory,
22024 we concatenate it to the filename when it makes sense.
22025 Note that the Dwarf3 standard says (speaking of filenames in line
22026 information): ``The directory index is ignored for file names
22027 that represent full path names''. Thus ignoring dirname in the
22028 `else' branch below isn't an issue. */
22029
22030 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
22031 {
22032 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
22033 filename = copy.get ();
22034 }
22035
22036 cu->get_builder ()->start_subfile (filename);
22037 }
22038
22039 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
22040 buildsym_compunit constructor. */
22041
22042 struct compunit_symtab *
22043 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
22044 CORE_ADDR low_pc)
22045 {
22046 gdb_assert (m_builder == nullptr);
22047
22048 m_builder.reset (new struct buildsym_compunit
22049 (this->per_objfile->objfile,
22050 name, comp_dir, language, low_pc));
22051
22052 list_in_scope = get_builder ()->get_file_symbols ();
22053
22054 get_builder ()->record_debugformat ("DWARF 2");
22055 get_builder ()->record_producer (producer);
22056
22057 processing_has_namespace_info = false;
22058
22059 return get_builder ()->get_compunit_symtab ();
22060 }
22061
22062 static void
22063 var_decode_location (struct attribute *attr, struct symbol *sym,
22064 struct dwarf2_cu *cu)
22065 {
22066 struct objfile *objfile = cu->per_objfile->objfile;
22067 struct comp_unit_head *cu_header = &cu->header;
22068
22069 /* NOTE drow/2003-01-30: There used to be a comment and some special
22070 code here to turn a symbol with DW_AT_external and a
22071 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
22072 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
22073 with some versions of binutils) where shared libraries could have
22074 relocations against symbols in their debug information - the
22075 minimal symbol would have the right address, but the debug info
22076 would not. It's no longer necessary, because we will explicitly
22077 apply relocations when we read in the debug information now. */
22078
22079 /* A DW_AT_location attribute with no contents indicates that a
22080 variable has been optimized away. */
22081 if (attr->form_is_block () && attr->as_block ()->size == 0)
22082 {
22083 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22084 return;
22085 }
22086
22087 /* Handle one degenerate form of location expression specially, to
22088 preserve GDB's previous behavior when section offsets are
22089 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
22090 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
22091
22092 if (attr->form_is_block ())
22093 {
22094 struct dwarf_block *block = attr->as_block ();
22095
22096 if ((block->data[0] == DW_OP_addr
22097 && block->size == 1 + cu_header->addr_size)
22098 || ((block->data[0] == DW_OP_GNU_addr_index
22099 || block->data[0] == DW_OP_addrx)
22100 && (block->size
22101 == 1 + leb128_size (&block->data[1]))))
22102 {
22103 unsigned int dummy;
22104
22105 if (block->data[0] == DW_OP_addr)
22106 SET_SYMBOL_VALUE_ADDRESS
22107 (sym, cu->header.read_address (objfile->obfd,
22108 block->data + 1,
22109 &dummy));
22110 else
22111 SET_SYMBOL_VALUE_ADDRESS
22112 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
22113 &dummy));
22114 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
22115 fixup_symbol_section (sym, objfile);
22116 SET_SYMBOL_VALUE_ADDRESS
22117 (sym,
22118 SYMBOL_VALUE_ADDRESS (sym)
22119 + objfile->section_offsets[sym->section_index ()]);
22120 return;
22121 }
22122 }
22123
22124 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
22125 expression evaluator, and use LOC_COMPUTED only when necessary
22126 (i.e. when the value of a register or memory location is
22127 referenced, or a thread-local block, etc.). Then again, it might
22128 not be worthwhile. I'm assuming that it isn't unless performance
22129 or memory numbers show me otherwise. */
22130
22131 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
22132
22133 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
22134 cu->has_loclist = true;
22135 }
22136
22137 /* Given a pointer to a DWARF information entry, figure out if we need
22138 to make a symbol table entry for it, and if so, create a new entry
22139 and return a pointer to it.
22140 If TYPE is NULL, determine symbol type from the die, otherwise
22141 used the passed type.
22142 If SPACE is not NULL, use it to hold the new symbol. If it is
22143 NULL, allocate a new symbol on the objfile's obstack. */
22144
22145 static struct symbol *
22146 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
22147 struct symbol *space)
22148 {
22149 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22150 struct objfile *objfile = per_objfile->objfile;
22151 struct gdbarch *gdbarch = objfile->arch ();
22152 struct symbol *sym = NULL;
22153 const char *name;
22154 struct attribute *attr = NULL;
22155 struct attribute *attr2 = NULL;
22156 CORE_ADDR baseaddr;
22157 struct pending **list_to_add = NULL;
22158
22159 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
22160
22161 baseaddr = objfile->text_section_offset ();
22162
22163 name = dwarf2_name (die, cu);
22164 if (name)
22165 {
22166 int suppress_add = 0;
22167
22168 if (space)
22169 sym = space;
22170 else
22171 sym = new (&objfile->objfile_obstack) symbol;
22172 OBJSTAT (objfile, n_syms++);
22173
22174 /* Cache this symbol's name and the name's demangled form (if any). */
22175 sym->set_language (cu->language, &objfile->objfile_obstack);
22176 /* Fortran does not have mangling standard and the mangling does differ
22177 between gfortran, iFort etc. */
22178 const char *physname
22179 = (cu->language == language_fortran
22180 ? dwarf2_full_name (name, die, cu)
22181 : dwarf2_physname (name, die, cu));
22182 const char *linkagename = dw2_linkage_name (die, cu);
22183
22184 if (linkagename == nullptr || cu->language == language_ada)
22185 sym->set_linkage_name (physname);
22186 else
22187 {
22188 sym->set_demangled_name (physname, &objfile->objfile_obstack);
22189 sym->set_linkage_name (linkagename);
22190 }
22191
22192 /* Default assumptions.
22193 Use the passed type or decode it from the die. */
22194 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22195 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22196 if (type != NULL)
22197 SYMBOL_TYPE (sym) = type;
22198 else
22199 SYMBOL_TYPE (sym) = die_type (die, cu);
22200 attr = dwarf2_attr (die,
22201 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
22202 cu);
22203 if (attr != nullptr)
22204 SYMBOL_LINE (sym) = attr->constant_value (0);
22205
22206 attr = dwarf2_attr (die,
22207 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
22208 cu);
22209 if (attr != nullptr && attr->is_nonnegative ())
22210 {
22211 file_name_index file_index
22212 = (file_name_index) attr->as_nonnegative ();
22213 struct file_entry *fe;
22214
22215 if (cu->line_header != NULL)
22216 fe = cu->line_header->file_name_at (file_index);
22217 else
22218 fe = NULL;
22219
22220 if (fe == NULL)
22221 complaint (_("file index out of range"));
22222 else
22223 symbol_set_symtab (sym, fe->symtab);
22224 }
22225
22226 switch (die->tag)
22227 {
22228 case DW_TAG_label:
22229 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
22230 if (attr != nullptr)
22231 {
22232 CORE_ADDR addr;
22233
22234 addr = attr->as_address ();
22235 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
22236 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
22237 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
22238 }
22239 else
22240 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22241 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
22242 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
22243 add_symbol_to_list (sym, cu->list_in_scope);
22244 break;
22245 case DW_TAG_subprogram:
22246 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22247 finish_block. */
22248 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
22249 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22250 if ((attr2 != nullptr && attr2->as_boolean ())
22251 || cu->language == language_ada
22252 || cu->language == language_fortran)
22253 {
22254 /* Subprograms marked external are stored as a global symbol.
22255 Ada and Fortran subprograms, whether marked external or
22256 not, are always stored as a global symbol, because we want
22257 to be able to access them globally. For instance, we want
22258 to be able to break on a nested subprogram without having
22259 to specify the context. */
22260 list_to_add = cu->get_builder ()->get_global_symbols ();
22261 }
22262 else
22263 {
22264 list_to_add = cu->list_in_scope;
22265 }
22266 break;
22267 case DW_TAG_inlined_subroutine:
22268 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22269 finish_block. */
22270 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
22271 SYMBOL_INLINED (sym) = 1;
22272 list_to_add = cu->list_in_scope;
22273 break;
22274 case DW_TAG_template_value_param:
22275 suppress_add = 1;
22276 /* Fall through. */
22277 case DW_TAG_constant:
22278 case DW_TAG_variable:
22279 case DW_TAG_member:
22280 /* Compilation with minimal debug info may result in
22281 variables with missing type entries. Change the
22282 misleading `void' type to something sensible. */
22283 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
22284 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
22285
22286 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22287 /* In the case of DW_TAG_member, we should only be called for
22288 static const members. */
22289 if (die->tag == DW_TAG_member)
22290 {
22291 /* dwarf2_add_field uses die_is_declaration,
22292 so we do the same. */
22293 gdb_assert (die_is_declaration (die, cu));
22294 gdb_assert (attr);
22295 }
22296 if (attr != nullptr)
22297 {
22298 dwarf2_const_value (attr, sym, cu);
22299 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22300 if (!suppress_add)
22301 {
22302 if (attr2 != nullptr && attr2->as_boolean ())
22303 list_to_add = cu->get_builder ()->get_global_symbols ();
22304 else
22305 list_to_add = cu->list_in_scope;
22306 }
22307 break;
22308 }
22309 attr = dwarf2_attr (die, DW_AT_location, cu);
22310 if (attr != nullptr)
22311 {
22312 var_decode_location (attr, sym, cu);
22313 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22314
22315 /* Fortran explicitly imports any global symbols to the local
22316 scope by DW_TAG_common_block. */
22317 if (cu->language == language_fortran && die->parent
22318 && die->parent->tag == DW_TAG_common_block)
22319 attr2 = NULL;
22320
22321 if (SYMBOL_CLASS (sym) == LOC_STATIC
22322 && SYMBOL_VALUE_ADDRESS (sym) == 0
22323 && !per_objfile->per_bfd->has_section_at_zero)
22324 {
22325 /* When a static variable is eliminated by the linker,
22326 the corresponding debug information is not stripped
22327 out, but the variable address is set to null;
22328 do not add such variables into symbol table. */
22329 }
22330 else if (attr2 != nullptr && attr2->as_boolean ())
22331 {
22332 if (SYMBOL_CLASS (sym) == LOC_STATIC
22333 && (objfile->flags & OBJF_MAINLINE) == 0
22334 && per_objfile->per_bfd->can_copy)
22335 {
22336 /* A global static variable might be subject to
22337 copy relocation. We first check for a local
22338 minsym, though, because maybe the symbol was
22339 marked hidden, in which case this would not
22340 apply. */
22341 bound_minimal_symbol found
22342 = (lookup_minimal_symbol_linkage
22343 (sym->linkage_name (), objfile));
22344 if (found.minsym != nullptr)
22345 sym->maybe_copied = 1;
22346 }
22347
22348 /* A variable with DW_AT_external is never static,
22349 but it may be block-scoped. */
22350 list_to_add
22351 = ((cu->list_in_scope
22352 == cu->get_builder ()->get_file_symbols ())
22353 ? cu->get_builder ()->get_global_symbols ()
22354 : cu->list_in_scope);
22355 }
22356 else
22357 list_to_add = cu->list_in_scope;
22358 }
22359 else
22360 {
22361 /* We do not know the address of this symbol.
22362 If it is an external symbol and we have type information
22363 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22364 The address of the variable will then be determined from
22365 the minimal symbol table whenever the variable is
22366 referenced. */
22367 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22368
22369 /* Fortran explicitly imports any global symbols to the local
22370 scope by DW_TAG_common_block. */
22371 if (cu->language == language_fortran && die->parent
22372 && die->parent->tag == DW_TAG_common_block)
22373 {
22374 /* SYMBOL_CLASS doesn't matter here because
22375 read_common_block is going to reset it. */
22376 if (!suppress_add)
22377 list_to_add = cu->list_in_scope;
22378 }
22379 else if (attr2 != nullptr && attr2->as_boolean ()
22380 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
22381 {
22382 /* A variable with DW_AT_external is never static, but it
22383 may be block-scoped. */
22384 list_to_add
22385 = ((cu->list_in_scope
22386 == cu->get_builder ()->get_file_symbols ())
22387 ? cu->get_builder ()->get_global_symbols ()
22388 : cu->list_in_scope);
22389
22390 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
22391 }
22392 else if (!die_is_declaration (die, cu))
22393 {
22394 /* Use the default LOC_OPTIMIZED_OUT class. */
22395 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
22396 if (!suppress_add)
22397 list_to_add = cu->list_in_scope;
22398 }
22399 }
22400 break;
22401 case DW_TAG_formal_parameter:
22402 {
22403 /* If we are inside a function, mark this as an argument. If
22404 not, we might be looking at an argument to an inlined function
22405 when we do not have enough information to show inlined frames;
22406 pretend it's a local variable in that case so that the user can
22407 still see it. */
22408 struct context_stack *curr
22409 = cu->get_builder ()->get_current_context_stack ();
22410 if (curr != nullptr && curr->name != nullptr)
22411 SYMBOL_IS_ARGUMENT (sym) = 1;
22412 attr = dwarf2_attr (die, DW_AT_location, cu);
22413 if (attr != nullptr)
22414 {
22415 var_decode_location (attr, sym, cu);
22416 }
22417 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22418 if (attr != nullptr)
22419 {
22420 dwarf2_const_value (attr, sym, cu);
22421 }
22422
22423 list_to_add = cu->list_in_scope;
22424 }
22425 break;
22426 case DW_TAG_unspecified_parameters:
22427 /* From varargs functions; gdb doesn't seem to have any
22428 interest in this information, so just ignore it for now.
22429 (FIXME?) */
22430 break;
22431 case DW_TAG_template_type_param:
22432 suppress_add = 1;
22433 /* Fall through. */
22434 case DW_TAG_class_type:
22435 case DW_TAG_interface_type:
22436 case DW_TAG_structure_type:
22437 case DW_TAG_union_type:
22438 case DW_TAG_set_type:
22439 case DW_TAG_enumeration_type:
22440 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22441 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
22442
22443 {
22444 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22445 really ever be static objects: otherwise, if you try
22446 to, say, break of a class's method and you're in a file
22447 which doesn't mention that class, it won't work unless
22448 the check for all static symbols in lookup_symbol_aux
22449 saves you. See the OtherFileClass tests in
22450 gdb.c++/namespace.exp. */
22451
22452 if (!suppress_add)
22453 {
22454 buildsym_compunit *builder = cu->get_builder ();
22455 list_to_add
22456 = (cu->list_in_scope == builder->get_file_symbols ()
22457 && cu->language == language_cplus
22458 ? builder->get_global_symbols ()
22459 : cu->list_in_scope);
22460
22461 /* The semantics of C++ state that "struct foo {
22462 ... }" also defines a typedef for "foo". */
22463 if (cu->language == language_cplus
22464 || cu->language == language_ada
22465 || cu->language == language_d
22466 || cu->language == language_rust)
22467 {
22468 /* The symbol's name is already allocated along
22469 with this objfile, so we don't need to
22470 duplicate it for the type. */
22471 if (SYMBOL_TYPE (sym)->name () == 0)
22472 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
22473 }
22474 }
22475 }
22476 break;
22477 case DW_TAG_typedef:
22478 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22479 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22480 list_to_add = cu->list_in_scope;
22481 break;
22482 case DW_TAG_array_type:
22483 case DW_TAG_base_type:
22484 case DW_TAG_subrange_type:
22485 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22486 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22487 list_to_add = cu->list_in_scope;
22488 break;
22489 case DW_TAG_enumerator:
22490 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22491 if (attr != nullptr)
22492 {
22493 dwarf2_const_value (attr, sym, cu);
22494 }
22495 {
22496 /* NOTE: carlton/2003-11-10: See comment above in the
22497 DW_TAG_class_type, etc. block. */
22498
22499 list_to_add
22500 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22501 && cu->language == language_cplus
22502 ? cu->get_builder ()->get_global_symbols ()
22503 : cu->list_in_scope);
22504 }
22505 break;
22506 case DW_TAG_imported_declaration:
22507 case DW_TAG_namespace:
22508 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22509 list_to_add = cu->get_builder ()->get_global_symbols ();
22510 break;
22511 case DW_TAG_module:
22512 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22513 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22514 list_to_add = cu->get_builder ()->get_global_symbols ();
22515 break;
22516 case DW_TAG_common_block:
22517 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22518 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22519 add_symbol_to_list (sym, cu->list_in_scope);
22520 break;
22521 default:
22522 /* Not a tag we recognize. Hopefully we aren't processing
22523 trash data, but since we must specifically ignore things
22524 we don't recognize, there is nothing else we should do at
22525 this point. */
22526 complaint (_("unsupported tag: '%s'"),
22527 dwarf_tag_name (die->tag));
22528 break;
22529 }
22530
22531 if (suppress_add)
22532 {
22533 sym->hash_next = objfile->template_symbols;
22534 objfile->template_symbols = sym;
22535 list_to_add = NULL;
22536 }
22537
22538 if (list_to_add != NULL)
22539 add_symbol_to_list (sym, list_to_add);
22540
22541 /* For the benefit of old versions of GCC, check for anonymous
22542 namespaces based on the demangled name. */
22543 if (!cu->processing_has_namespace_info
22544 && cu->language == language_cplus)
22545 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22546 }
22547 return (sym);
22548 }
22549
22550 /* Given an attr with a DW_FORM_dataN value in host byte order,
22551 zero-extend it as appropriate for the symbol's type. The DWARF
22552 standard (v4) is not entirely clear about the meaning of using
22553 DW_FORM_dataN for a constant with a signed type, where the type is
22554 wider than the data. The conclusion of a discussion on the DWARF
22555 list was that this is unspecified. We choose to always zero-extend
22556 because that is the interpretation long in use by GCC. */
22557
22558 static gdb_byte *
22559 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22560 struct dwarf2_cu *cu, LONGEST *value, int bits)
22561 {
22562 struct objfile *objfile = cu->per_objfile->objfile;
22563 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22564 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22565 LONGEST l = attr->constant_value (0);
22566
22567 if (bits < sizeof (*value) * 8)
22568 {
22569 l &= ((LONGEST) 1 << bits) - 1;
22570 *value = l;
22571 }
22572 else if (bits == sizeof (*value) * 8)
22573 *value = l;
22574 else
22575 {
22576 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22577 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22578 return bytes;
22579 }
22580
22581 return NULL;
22582 }
22583
22584 /* Read a constant value from an attribute. Either set *VALUE, or if
22585 the value does not fit in *VALUE, set *BYTES - either already
22586 allocated on the objfile obstack, or newly allocated on OBSTACK,
22587 or, set *BATON, if we translated the constant to a location
22588 expression. */
22589
22590 static void
22591 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22592 const char *name, struct obstack *obstack,
22593 struct dwarf2_cu *cu,
22594 LONGEST *value, const gdb_byte **bytes,
22595 struct dwarf2_locexpr_baton **baton)
22596 {
22597 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22598 struct objfile *objfile = per_objfile->objfile;
22599 struct comp_unit_head *cu_header = &cu->header;
22600 struct dwarf_block *blk;
22601 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22602 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22603
22604 *value = 0;
22605 *bytes = NULL;
22606 *baton = NULL;
22607
22608 switch (attr->form)
22609 {
22610 case DW_FORM_addr:
22611 case DW_FORM_addrx:
22612 case DW_FORM_GNU_addr_index:
22613 {
22614 gdb_byte *data;
22615
22616 if (TYPE_LENGTH (type) != cu_header->addr_size)
22617 dwarf2_const_value_length_mismatch_complaint (name,
22618 cu_header->addr_size,
22619 TYPE_LENGTH (type));
22620 /* Symbols of this form are reasonably rare, so we just
22621 piggyback on the existing location code rather than writing
22622 a new implementation of symbol_computed_ops. */
22623 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22624 (*baton)->per_objfile = per_objfile;
22625 (*baton)->per_cu = cu->per_cu;
22626 gdb_assert ((*baton)->per_cu);
22627
22628 (*baton)->size = 2 + cu_header->addr_size;
22629 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22630 (*baton)->data = data;
22631
22632 data[0] = DW_OP_addr;
22633 store_unsigned_integer (&data[1], cu_header->addr_size,
22634 byte_order, attr->as_address ());
22635 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22636 }
22637 break;
22638 case DW_FORM_string:
22639 case DW_FORM_strp:
22640 case DW_FORM_strx:
22641 case DW_FORM_GNU_str_index:
22642 case DW_FORM_GNU_strp_alt:
22643 /* The string is already allocated on the objfile obstack, point
22644 directly to it. */
22645 *bytes = (const gdb_byte *) attr->as_string ();
22646 break;
22647 case DW_FORM_block1:
22648 case DW_FORM_block2:
22649 case DW_FORM_block4:
22650 case DW_FORM_block:
22651 case DW_FORM_exprloc:
22652 case DW_FORM_data16:
22653 blk = attr->as_block ();
22654 if (TYPE_LENGTH (type) != blk->size)
22655 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22656 TYPE_LENGTH (type));
22657 *bytes = blk->data;
22658 break;
22659
22660 /* The DW_AT_const_value attributes are supposed to carry the
22661 symbol's value "represented as it would be on the target
22662 architecture." By the time we get here, it's already been
22663 converted to host endianness, so we just need to sign- or
22664 zero-extend it as appropriate. */
22665 case DW_FORM_data1:
22666 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22667 break;
22668 case DW_FORM_data2:
22669 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22670 break;
22671 case DW_FORM_data4:
22672 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22673 break;
22674 case DW_FORM_data8:
22675 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22676 break;
22677
22678 case DW_FORM_sdata:
22679 case DW_FORM_implicit_const:
22680 *value = attr->as_signed ();
22681 break;
22682
22683 case DW_FORM_udata:
22684 *value = attr->as_unsigned ();
22685 break;
22686
22687 default:
22688 complaint (_("unsupported const value attribute form: '%s'"),
22689 dwarf_form_name (attr->form));
22690 *value = 0;
22691 break;
22692 }
22693 }
22694
22695
22696 /* Copy constant value from an attribute to a symbol. */
22697
22698 static void
22699 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22700 struct dwarf2_cu *cu)
22701 {
22702 struct objfile *objfile = cu->per_objfile->objfile;
22703 LONGEST value;
22704 const gdb_byte *bytes;
22705 struct dwarf2_locexpr_baton *baton;
22706
22707 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22708 sym->print_name (),
22709 &objfile->objfile_obstack, cu,
22710 &value, &bytes, &baton);
22711
22712 if (baton != NULL)
22713 {
22714 SYMBOL_LOCATION_BATON (sym) = baton;
22715 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22716 }
22717 else if (bytes != NULL)
22718 {
22719 SYMBOL_VALUE_BYTES (sym) = bytes;
22720 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22721 }
22722 else
22723 {
22724 SYMBOL_VALUE (sym) = value;
22725 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22726 }
22727 }
22728
22729 /* Return the type of the die in question using its DW_AT_type attribute. */
22730
22731 static struct type *
22732 die_type (struct die_info *die, struct dwarf2_cu *cu)
22733 {
22734 struct attribute *type_attr;
22735
22736 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22737 if (!type_attr)
22738 {
22739 struct objfile *objfile = cu->per_objfile->objfile;
22740 /* A missing DW_AT_type represents a void type. */
22741 return objfile_type (objfile)->builtin_void;
22742 }
22743
22744 return lookup_die_type (die, type_attr, cu);
22745 }
22746
22747 /* True iff CU's producer generates GNAT Ada auxiliary information
22748 that allows to find parallel types through that information instead
22749 of having to do expensive parallel lookups by type name. */
22750
22751 static int
22752 need_gnat_info (struct dwarf2_cu *cu)
22753 {
22754 /* Assume that the Ada compiler was GNAT, which always produces
22755 the auxiliary information. */
22756 return (cu->language == language_ada);
22757 }
22758
22759 /* Return the auxiliary type of the die in question using its
22760 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22761 attribute is not present. */
22762
22763 static struct type *
22764 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22765 {
22766 struct attribute *type_attr;
22767
22768 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22769 if (!type_attr)
22770 return NULL;
22771
22772 return lookup_die_type (die, type_attr, cu);
22773 }
22774
22775 /* If DIE has a descriptive_type attribute, then set the TYPE's
22776 descriptive type accordingly. */
22777
22778 static void
22779 set_descriptive_type (struct type *type, struct die_info *die,
22780 struct dwarf2_cu *cu)
22781 {
22782 struct type *descriptive_type = die_descriptive_type (die, cu);
22783
22784 if (descriptive_type)
22785 {
22786 ALLOCATE_GNAT_AUX_TYPE (type);
22787 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22788 }
22789 }
22790
22791 /* Return the containing type of the die in question using its
22792 DW_AT_containing_type attribute. */
22793
22794 static struct type *
22795 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22796 {
22797 struct attribute *type_attr;
22798 struct objfile *objfile = cu->per_objfile->objfile;
22799
22800 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22801 if (!type_attr)
22802 error (_("Dwarf Error: Problem turning containing type into gdb type "
22803 "[in module %s]"), objfile_name (objfile));
22804
22805 return lookup_die_type (die, type_attr, cu);
22806 }
22807
22808 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22809
22810 static struct type *
22811 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22812 {
22813 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22814 struct objfile *objfile = per_objfile->objfile;
22815 char *saved;
22816
22817 std::string message
22818 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22819 objfile_name (objfile),
22820 sect_offset_str (cu->header.sect_off),
22821 sect_offset_str (die->sect_off));
22822 saved = obstack_strdup (&objfile->objfile_obstack, message);
22823
22824 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22825 }
22826
22827 /* Look up the type of DIE in CU using its type attribute ATTR.
22828 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22829 DW_AT_containing_type.
22830 If there is no type substitute an error marker. */
22831
22832 static struct type *
22833 lookup_die_type (struct die_info *die, const struct attribute *attr,
22834 struct dwarf2_cu *cu)
22835 {
22836 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22837 struct objfile *objfile = per_objfile->objfile;
22838 struct type *this_type;
22839
22840 gdb_assert (attr->name == DW_AT_type
22841 || attr->name == DW_AT_GNAT_descriptive_type
22842 || attr->name == DW_AT_containing_type);
22843
22844 /* First see if we have it cached. */
22845
22846 if (attr->form == DW_FORM_GNU_ref_alt)
22847 {
22848 struct dwarf2_per_cu_data *per_cu;
22849 sect_offset sect_off = attr->get_ref_die_offset ();
22850
22851 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22852 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
22853 }
22854 else if (attr->form_is_ref ())
22855 {
22856 sect_offset sect_off = attr->get_ref_die_offset ();
22857
22858 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
22859 }
22860 else if (attr->form == DW_FORM_ref_sig8)
22861 {
22862 ULONGEST signature = attr->as_signature ();
22863
22864 return get_signatured_type (die, signature, cu);
22865 }
22866 else
22867 {
22868 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22869 " at %s [in module %s]"),
22870 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22871 objfile_name (objfile));
22872 return build_error_marker_type (cu, die);
22873 }
22874
22875 /* If not cached we need to read it in. */
22876
22877 if (this_type == NULL)
22878 {
22879 struct die_info *type_die = NULL;
22880 struct dwarf2_cu *type_cu = cu;
22881
22882 if (attr->form_is_ref ())
22883 type_die = follow_die_ref (die, attr, &type_cu);
22884 if (type_die == NULL)
22885 return build_error_marker_type (cu, die);
22886 /* If we find the type now, it's probably because the type came
22887 from an inter-CU reference and the type's CU got expanded before
22888 ours. */
22889 this_type = read_type_die (type_die, type_cu);
22890 }
22891
22892 /* If we still don't have a type use an error marker. */
22893
22894 if (this_type == NULL)
22895 return build_error_marker_type (cu, die);
22896
22897 return this_type;
22898 }
22899
22900 /* Return the type in DIE, CU.
22901 Returns NULL for invalid types.
22902
22903 This first does a lookup in die_type_hash,
22904 and only reads the die in if necessary.
22905
22906 NOTE: This can be called when reading in partial or full symbols. */
22907
22908 static struct type *
22909 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22910 {
22911 struct type *this_type;
22912
22913 this_type = get_die_type (die, cu);
22914 if (this_type)
22915 return this_type;
22916
22917 return read_type_die_1 (die, cu);
22918 }
22919
22920 /* Read the type in DIE, CU.
22921 Returns NULL for invalid types. */
22922
22923 static struct type *
22924 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22925 {
22926 struct type *this_type = NULL;
22927
22928 switch (die->tag)
22929 {
22930 case DW_TAG_class_type:
22931 case DW_TAG_interface_type:
22932 case DW_TAG_structure_type:
22933 case DW_TAG_union_type:
22934 this_type = read_structure_type (die, cu);
22935 break;
22936 case DW_TAG_enumeration_type:
22937 this_type = read_enumeration_type (die, cu);
22938 break;
22939 case DW_TAG_subprogram:
22940 case DW_TAG_subroutine_type:
22941 case DW_TAG_inlined_subroutine:
22942 this_type = read_subroutine_type (die, cu);
22943 break;
22944 case DW_TAG_array_type:
22945 this_type = read_array_type (die, cu);
22946 break;
22947 case DW_TAG_set_type:
22948 this_type = read_set_type (die, cu);
22949 break;
22950 case DW_TAG_pointer_type:
22951 this_type = read_tag_pointer_type (die, cu);
22952 break;
22953 case DW_TAG_ptr_to_member_type:
22954 this_type = read_tag_ptr_to_member_type (die, cu);
22955 break;
22956 case DW_TAG_reference_type:
22957 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22958 break;
22959 case DW_TAG_rvalue_reference_type:
22960 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22961 break;
22962 case DW_TAG_const_type:
22963 this_type = read_tag_const_type (die, cu);
22964 break;
22965 case DW_TAG_volatile_type:
22966 this_type = read_tag_volatile_type (die, cu);
22967 break;
22968 case DW_TAG_restrict_type:
22969 this_type = read_tag_restrict_type (die, cu);
22970 break;
22971 case DW_TAG_string_type:
22972 this_type = read_tag_string_type (die, cu);
22973 break;
22974 case DW_TAG_typedef:
22975 this_type = read_typedef (die, cu);
22976 break;
22977 case DW_TAG_subrange_type:
22978 this_type = read_subrange_type (die, cu);
22979 break;
22980 case DW_TAG_base_type:
22981 this_type = read_base_type (die, cu);
22982 break;
22983 case DW_TAG_unspecified_type:
22984 this_type = read_unspecified_type (die, cu);
22985 break;
22986 case DW_TAG_namespace:
22987 this_type = read_namespace_type (die, cu);
22988 break;
22989 case DW_TAG_module:
22990 this_type = read_module_type (die, cu);
22991 break;
22992 case DW_TAG_atomic_type:
22993 this_type = read_tag_atomic_type (die, cu);
22994 break;
22995 default:
22996 complaint (_("unexpected tag in read_type_die: '%s'"),
22997 dwarf_tag_name (die->tag));
22998 break;
22999 }
23000
23001 return this_type;
23002 }
23003
23004 /* See if we can figure out if the class lives in a namespace. We do
23005 this by looking for a member function; its demangled name will
23006 contain namespace info, if there is any.
23007 Return the computed name or NULL.
23008 Space for the result is allocated on the objfile's obstack.
23009 This is the full-die version of guess_partial_die_structure_name.
23010 In this case we know DIE has no useful parent. */
23011
23012 static const char *
23013 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
23014 {
23015 struct die_info *spec_die;
23016 struct dwarf2_cu *spec_cu;
23017 struct die_info *child;
23018 struct objfile *objfile = cu->per_objfile->objfile;
23019
23020 spec_cu = cu;
23021 spec_die = die_specification (die, &spec_cu);
23022 if (spec_die != NULL)
23023 {
23024 die = spec_die;
23025 cu = spec_cu;
23026 }
23027
23028 for (child = die->child;
23029 child != NULL;
23030 child = child->sibling)
23031 {
23032 if (child->tag == DW_TAG_subprogram)
23033 {
23034 const char *linkage_name = dw2_linkage_name (child, cu);
23035
23036 if (linkage_name != NULL)
23037 {
23038 gdb::unique_xmalloc_ptr<char> actual_name
23039 (cu->language_defn->class_name_from_physname (linkage_name));
23040 const char *name = NULL;
23041
23042 if (actual_name != NULL)
23043 {
23044 const char *die_name = dwarf2_name (die, cu);
23045
23046 if (die_name != NULL
23047 && strcmp (die_name, actual_name.get ()) != 0)
23048 {
23049 /* Strip off the class name from the full name.
23050 We want the prefix. */
23051 int die_name_len = strlen (die_name);
23052 int actual_name_len = strlen (actual_name.get ());
23053 const char *ptr = actual_name.get ();
23054
23055 /* Test for '::' as a sanity check. */
23056 if (actual_name_len > die_name_len + 2
23057 && ptr[actual_name_len - die_name_len - 1] == ':')
23058 name = obstack_strndup (
23059 &objfile->per_bfd->storage_obstack,
23060 ptr, actual_name_len - die_name_len - 2);
23061 }
23062 }
23063 return name;
23064 }
23065 }
23066 }
23067
23068 return NULL;
23069 }
23070
23071 /* GCC might emit a nameless typedef that has a linkage name. Determine the
23072 prefix part in such case. See
23073 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23074
23075 static const char *
23076 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
23077 {
23078 struct attribute *attr;
23079 const char *base;
23080
23081 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
23082 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
23083 return NULL;
23084
23085 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
23086 return NULL;
23087
23088 attr = dw2_linkage_name_attr (die, cu);
23089 const char *attr_name = attr->as_string ();
23090 if (attr == NULL || attr_name == NULL)
23091 return NULL;
23092
23093 /* dwarf2_name had to be already called. */
23094 gdb_assert (attr->canonical_string_p ());
23095
23096 /* Strip the base name, keep any leading namespaces/classes. */
23097 base = strrchr (attr_name, ':');
23098 if (base == NULL || base == attr_name || base[-1] != ':')
23099 return "";
23100
23101 struct objfile *objfile = cu->per_objfile->objfile;
23102 return obstack_strndup (&objfile->per_bfd->storage_obstack,
23103 attr_name,
23104 &base[-1] - attr_name);
23105 }
23106
23107 /* Return the name of the namespace/class that DIE is defined within,
23108 or "" if we can't tell. The caller should not xfree the result.
23109
23110 For example, if we're within the method foo() in the following
23111 code:
23112
23113 namespace N {
23114 class C {
23115 void foo () {
23116 }
23117 };
23118 }
23119
23120 then determine_prefix on foo's die will return "N::C". */
23121
23122 static const char *
23123 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
23124 {
23125 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23126 struct die_info *parent, *spec_die;
23127 struct dwarf2_cu *spec_cu;
23128 struct type *parent_type;
23129 const char *retval;
23130
23131 if (cu->language != language_cplus
23132 && cu->language != language_fortran && cu->language != language_d
23133 && cu->language != language_rust)
23134 return "";
23135
23136 retval = anonymous_struct_prefix (die, cu);
23137 if (retval)
23138 return retval;
23139
23140 /* We have to be careful in the presence of DW_AT_specification.
23141 For example, with GCC 3.4, given the code
23142
23143 namespace N {
23144 void foo() {
23145 // Definition of N::foo.
23146 }
23147 }
23148
23149 then we'll have a tree of DIEs like this:
23150
23151 1: DW_TAG_compile_unit
23152 2: DW_TAG_namespace // N
23153 3: DW_TAG_subprogram // declaration of N::foo
23154 4: DW_TAG_subprogram // definition of N::foo
23155 DW_AT_specification // refers to die #3
23156
23157 Thus, when processing die #4, we have to pretend that we're in
23158 the context of its DW_AT_specification, namely the contex of die
23159 #3. */
23160 spec_cu = cu;
23161 spec_die = die_specification (die, &spec_cu);
23162 if (spec_die == NULL)
23163 parent = die->parent;
23164 else
23165 {
23166 parent = spec_die->parent;
23167 cu = spec_cu;
23168 }
23169
23170 if (parent == NULL)
23171 return "";
23172 else if (parent->building_fullname)
23173 {
23174 const char *name;
23175 const char *parent_name;
23176
23177 /* It has been seen on RealView 2.2 built binaries,
23178 DW_TAG_template_type_param types actually _defined_ as
23179 children of the parent class:
23180
23181 enum E {};
23182 template class <class Enum> Class{};
23183 Class<enum E> class_e;
23184
23185 1: DW_TAG_class_type (Class)
23186 2: DW_TAG_enumeration_type (E)
23187 3: DW_TAG_enumerator (enum1:0)
23188 3: DW_TAG_enumerator (enum2:1)
23189 ...
23190 2: DW_TAG_template_type_param
23191 DW_AT_type DW_FORM_ref_udata (E)
23192
23193 Besides being broken debug info, it can put GDB into an
23194 infinite loop. Consider:
23195
23196 When we're building the full name for Class<E>, we'll start
23197 at Class, and go look over its template type parameters,
23198 finding E. We'll then try to build the full name of E, and
23199 reach here. We're now trying to build the full name of E,
23200 and look over the parent DIE for containing scope. In the
23201 broken case, if we followed the parent DIE of E, we'd again
23202 find Class, and once again go look at its template type
23203 arguments, etc., etc. Simply don't consider such parent die
23204 as source-level parent of this die (it can't be, the language
23205 doesn't allow it), and break the loop here. */
23206 name = dwarf2_name (die, cu);
23207 parent_name = dwarf2_name (parent, cu);
23208 complaint (_("template param type '%s' defined within parent '%s'"),
23209 name ? name : "<unknown>",
23210 parent_name ? parent_name : "<unknown>");
23211 return "";
23212 }
23213 else
23214 switch (parent->tag)
23215 {
23216 case DW_TAG_namespace:
23217 parent_type = read_type_die (parent, cu);
23218 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
23219 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
23220 Work around this problem here. */
23221 if (cu->language == language_cplus
23222 && strcmp (parent_type->name (), "::") == 0)
23223 return "";
23224 /* We give a name to even anonymous namespaces. */
23225 return parent_type->name ();
23226 case DW_TAG_class_type:
23227 case DW_TAG_interface_type:
23228 case DW_TAG_structure_type:
23229 case DW_TAG_union_type:
23230 case DW_TAG_module:
23231 parent_type = read_type_die (parent, cu);
23232 if (parent_type->name () != NULL)
23233 return parent_type->name ();
23234 else
23235 /* An anonymous structure is only allowed non-static data
23236 members; no typedefs, no member functions, et cetera.
23237 So it does not need a prefix. */
23238 return "";
23239 case DW_TAG_compile_unit:
23240 case DW_TAG_partial_unit:
23241 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
23242 if (cu->language == language_cplus
23243 && !per_objfile->per_bfd->types.empty ()
23244 && die->child != NULL
23245 && (die->tag == DW_TAG_class_type
23246 || die->tag == DW_TAG_structure_type
23247 || die->tag == DW_TAG_union_type))
23248 {
23249 const char *name = guess_full_die_structure_name (die, cu);
23250 if (name != NULL)
23251 return name;
23252 }
23253 return "";
23254 case DW_TAG_subprogram:
23255 /* Nested subroutines in Fortran get a prefix with the name
23256 of the parent's subroutine. */
23257 if (cu->language == language_fortran)
23258 {
23259 if ((die->tag == DW_TAG_subprogram)
23260 && (dwarf2_name (parent, cu) != NULL))
23261 return dwarf2_name (parent, cu);
23262 }
23263 return determine_prefix (parent, cu);
23264 case DW_TAG_enumeration_type:
23265 parent_type = read_type_die (parent, cu);
23266 if (TYPE_DECLARED_CLASS (parent_type))
23267 {
23268 if (parent_type->name () != NULL)
23269 return parent_type->name ();
23270 return "";
23271 }
23272 /* Fall through. */
23273 default:
23274 return determine_prefix (parent, cu);
23275 }
23276 }
23277
23278 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
23279 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
23280 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
23281 an obconcat, otherwise allocate storage for the result. The CU argument is
23282 used to determine the language and hence, the appropriate separator. */
23283
23284 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
23285
23286 static char *
23287 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
23288 int physname, struct dwarf2_cu *cu)
23289 {
23290 const char *lead = "";
23291 const char *sep;
23292
23293 if (suffix == NULL || suffix[0] == '\0'
23294 || prefix == NULL || prefix[0] == '\0')
23295 sep = "";
23296 else if (cu->language == language_d)
23297 {
23298 /* For D, the 'main' function could be defined in any module, but it
23299 should never be prefixed. */
23300 if (strcmp (suffix, "D main") == 0)
23301 {
23302 prefix = "";
23303 sep = "";
23304 }
23305 else
23306 sep = ".";
23307 }
23308 else if (cu->language == language_fortran && physname)
23309 {
23310 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
23311 DW_AT_MIPS_linkage_name is preferred and used instead. */
23312
23313 lead = "__";
23314 sep = "_MOD_";
23315 }
23316 else
23317 sep = "::";
23318
23319 if (prefix == NULL)
23320 prefix = "";
23321 if (suffix == NULL)
23322 suffix = "";
23323
23324 if (obs == NULL)
23325 {
23326 char *retval
23327 = ((char *)
23328 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
23329
23330 strcpy (retval, lead);
23331 strcat (retval, prefix);
23332 strcat (retval, sep);
23333 strcat (retval, suffix);
23334 return retval;
23335 }
23336 else
23337 {
23338 /* We have an obstack. */
23339 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
23340 }
23341 }
23342
23343 /* Get name of a die, return NULL if not found. */
23344
23345 static const char *
23346 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
23347 struct objfile *objfile)
23348 {
23349 if (name && cu->language == language_cplus)
23350 {
23351 gdb::unique_xmalloc_ptr<char> canon_name
23352 = cp_canonicalize_string (name);
23353
23354 if (canon_name != nullptr)
23355 name = objfile->intern (canon_name.get ());
23356 }
23357
23358 return name;
23359 }
23360
23361 /* Get name of a die, return NULL if not found.
23362 Anonymous namespaces are converted to their magic string. */
23363
23364 static const char *
23365 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
23366 {
23367 struct attribute *attr;
23368 struct objfile *objfile = cu->per_objfile->objfile;
23369
23370 attr = dwarf2_attr (die, DW_AT_name, cu);
23371 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
23372 if (attr_name == nullptr
23373 && die->tag != DW_TAG_namespace
23374 && die->tag != DW_TAG_class_type
23375 && die->tag != DW_TAG_interface_type
23376 && die->tag != DW_TAG_structure_type
23377 && die->tag != DW_TAG_union_type)
23378 return NULL;
23379
23380 switch (die->tag)
23381 {
23382 case DW_TAG_compile_unit:
23383 case DW_TAG_partial_unit:
23384 /* Compilation units have a DW_AT_name that is a filename, not
23385 a source language identifier. */
23386 case DW_TAG_enumeration_type:
23387 case DW_TAG_enumerator:
23388 /* These tags always have simple identifiers already; no need
23389 to canonicalize them. */
23390 return attr_name;
23391
23392 case DW_TAG_namespace:
23393 if (attr_name != nullptr)
23394 return attr_name;
23395 return CP_ANONYMOUS_NAMESPACE_STR;
23396
23397 case DW_TAG_class_type:
23398 case DW_TAG_interface_type:
23399 case DW_TAG_structure_type:
23400 case DW_TAG_union_type:
23401 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23402 structures or unions. These were of the form "._%d" in GCC 4.1,
23403 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23404 and GCC 4.4. We work around this problem by ignoring these. */
23405 if (attr_name != nullptr
23406 && (startswith (attr_name, "._")
23407 || startswith (attr_name, "<anonymous")))
23408 return NULL;
23409
23410 /* GCC might emit a nameless typedef that has a linkage name. See
23411 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23412 if (!attr || attr_name == NULL)
23413 {
23414 attr = dw2_linkage_name_attr (die, cu);
23415 attr_name = attr == nullptr ? nullptr : attr->as_string ();
23416 if (attr == NULL || attr_name == NULL)
23417 return NULL;
23418
23419 /* Avoid demangling attr_name the second time on a second
23420 call for the same DIE. */
23421 if (!attr->canonical_string_p ())
23422 {
23423 gdb::unique_xmalloc_ptr<char> demangled
23424 (gdb_demangle (attr_name, DMGL_TYPES));
23425 if (demangled == nullptr)
23426 return nullptr;
23427
23428 attr->set_string_canonical (objfile->intern (demangled.get ()));
23429 attr_name = attr->as_string ();
23430 }
23431
23432 /* Strip any leading namespaces/classes, keep only the
23433 base name. DW_AT_name for named DIEs does not
23434 contain the prefixes. */
23435 const char *base = strrchr (attr_name, ':');
23436 if (base && base > attr_name && base[-1] == ':')
23437 return &base[1];
23438 else
23439 return attr_name;
23440 }
23441 break;
23442
23443 default:
23444 break;
23445 }
23446
23447 if (!attr->canonical_string_p ())
23448 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23449 objfile));
23450 return attr->as_string ();
23451 }
23452
23453 /* Return the die that this die in an extension of, or NULL if there
23454 is none. *EXT_CU is the CU containing DIE on input, and the CU
23455 containing the return value on output. */
23456
23457 static struct die_info *
23458 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23459 {
23460 struct attribute *attr;
23461
23462 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23463 if (attr == NULL)
23464 return NULL;
23465
23466 return follow_die_ref (die, attr, ext_cu);
23467 }
23468
23469 static void
23470 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23471 {
23472 unsigned int i;
23473
23474 print_spaces (indent, f);
23475 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23476 dwarf_tag_name (die->tag), die->abbrev,
23477 sect_offset_str (die->sect_off));
23478
23479 if (die->parent != NULL)
23480 {
23481 print_spaces (indent, f);
23482 fprintf_unfiltered (f, " parent at offset: %s\n",
23483 sect_offset_str (die->parent->sect_off));
23484 }
23485
23486 print_spaces (indent, f);
23487 fprintf_unfiltered (f, " has children: %s\n",
23488 dwarf_bool_name (die->child != NULL));
23489
23490 print_spaces (indent, f);
23491 fprintf_unfiltered (f, " attributes:\n");
23492
23493 for (i = 0; i < die->num_attrs; ++i)
23494 {
23495 print_spaces (indent, f);
23496 fprintf_unfiltered (f, " %s (%s) ",
23497 dwarf_attr_name (die->attrs[i].name),
23498 dwarf_form_name (die->attrs[i].form));
23499
23500 switch (die->attrs[i].form)
23501 {
23502 case DW_FORM_addr:
23503 case DW_FORM_addrx:
23504 case DW_FORM_GNU_addr_index:
23505 fprintf_unfiltered (f, "address: ");
23506 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
23507 break;
23508 case DW_FORM_block2:
23509 case DW_FORM_block4:
23510 case DW_FORM_block:
23511 case DW_FORM_block1:
23512 fprintf_unfiltered (f, "block: size %s",
23513 pulongest (die->attrs[i].as_block ()->size));
23514 break;
23515 case DW_FORM_exprloc:
23516 fprintf_unfiltered (f, "expression: size %s",
23517 pulongest (die->attrs[i].as_block ()->size));
23518 break;
23519 case DW_FORM_data16:
23520 fprintf_unfiltered (f, "constant of 16 bytes");
23521 break;
23522 case DW_FORM_ref_addr:
23523 fprintf_unfiltered (f, "ref address: ");
23524 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23525 break;
23526 case DW_FORM_GNU_ref_alt:
23527 fprintf_unfiltered (f, "alt ref address: ");
23528 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23529 break;
23530 case DW_FORM_ref1:
23531 case DW_FORM_ref2:
23532 case DW_FORM_ref4:
23533 case DW_FORM_ref8:
23534 case DW_FORM_ref_udata:
23535 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23536 (long) (die->attrs[i].as_unsigned ()));
23537 break;
23538 case DW_FORM_data1:
23539 case DW_FORM_data2:
23540 case DW_FORM_data4:
23541 case DW_FORM_data8:
23542 case DW_FORM_udata:
23543 fprintf_unfiltered (f, "constant: %s",
23544 pulongest (die->attrs[i].as_unsigned ()));
23545 break;
23546 case DW_FORM_sec_offset:
23547 fprintf_unfiltered (f, "section offset: %s",
23548 pulongest (die->attrs[i].as_unsigned ()));
23549 break;
23550 case DW_FORM_ref_sig8:
23551 fprintf_unfiltered (f, "signature: %s",
23552 hex_string (die->attrs[i].as_signature ()));
23553 break;
23554 case DW_FORM_string:
23555 case DW_FORM_strp:
23556 case DW_FORM_line_strp:
23557 case DW_FORM_strx:
23558 case DW_FORM_GNU_str_index:
23559 case DW_FORM_GNU_strp_alt:
23560 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23561 die->attrs[i].as_string ()
23562 ? die->attrs[i].as_string () : "",
23563 die->attrs[i].canonical_string_p () ? "is" : "not");
23564 break;
23565 case DW_FORM_flag:
23566 if (die->attrs[i].as_boolean ())
23567 fprintf_unfiltered (f, "flag: TRUE");
23568 else
23569 fprintf_unfiltered (f, "flag: FALSE");
23570 break;
23571 case DW_FORM_flag_present:
23572 fprintf_unfiltered (f, "flag: TRUE");
23573 break;
23574 case DW_FORM_indirect:
23575 /* The reader will have reduced the indirect form to
23576 the "base form" so this form should not occur. */
23577 fprintf_unfiltered (f,
23578 "unexpected attribute form: DW_FORM_indirect");
23579 break;
23580 case DW_FORM_sdata:
23581 case DW_FORM_implicit_const:
23582 fprintf_unfiltered (f, "constant: %s",
23583 plongest (die->attrs[i].as_signed ()));
23584 break;
23585 default:
23586 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23587 die->attrs[i].form);
23588 break;
23589 }
23590 fprintf_unfiltered (f, "\n");
23591 }
23592 }
23593
23594 static void
23595 dump_die_for_error (struct die_info *die)
23596 {
23597 dump_die_shallow (gdb_stderr, 0, die);
23598 }
23599
23600 static void
23601 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23602 {
23603 int indent = level * 4;
23604
23605 gdb_assert (die != NULL);
23606
23607 if (level >= max_level)
23608 return;
23609
23610 dump_die_shallow (f, indent, die);
23611
23612 if (die->child != NULL)
23613 {
23614 print_spaces (indent, f);
23615 fprintf_unfiltered (f, " Children:");
23616 if (level + 1 < max_level)
23617 {
23618 fprintf_unfiltered (f, "\n");
23619 dump_die_1 (f, level + 1, max_level, die->child);
23620 }
23621 else
23622 {
23623 fprintf_unfiltered (f,
23624 " [not printed, max nesting level reached]\n");
23625 }
23626 }
23627
23628 if (die->sibling != NULL && level > 0)
23629 {
23630 dump_die_1 (f, level, max_level, die->sibling);
23631 }
23632 }
23633
23634 /* This is called from the pdie macro in gdbinit.in.
23635 It's not static so gcc will keep a copy callable from gdb. */
23636
23637 void
23638 dump_die (struct die_info *die, int max_level)
23639 {
23640 dump_die_1 (gdb_stdlog, 0, max_level, die);
23641 }
23642
23643 static void
23644 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23645 {
23646 void **slot;
23647
23648 slot = htab_find_slot_with_hash (cu->die_hash, die,
23649 to_underlying (die->sect_off),
23650 INSERT);
23651
23652 *slot = die;
23653 }
23654
23655 /* Follow reference or signature attribute ATTR of SRC_DIE.
23656 On entry *REF_CU is the CU of SRC_DIE.
23657 On exit *REF_CU is the CU of the result. */
23658
23659 static struct die_info *
23660 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23661 struct dwarf2_cu **ref_cu)
23662 {
23663 struct die_info *die;
23664
23665 if (attr->form_is_ref ())
23666 die = follow_die_ref (src_die, attr, ref_cu);
23667 else if (attr->form == DW_FORM_ref_sig8)
23668 die = follow_die_sig (src_die, attr, ref_cu);
23669 else
23670 {
23671 dump_die_for_error (src_die);
23672 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23673 objfile_name ((*ref_cu)->per_objfile->objfile));
23674 }
23675
23676 return die;
23677 }
23678
23679 /* Follow reference OFFSET.
23680 On entry *REF_CU is the CU of the source die referencing OFFSET.
23681 On exit *REF_CU is the CU of the result.
23682 Returns NULL if OFFSET is invalid. */
23683
23684 static struct die_info *
23685 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23686 struct dwarf2_cu **ref_cu)
23687 {
23688 struct die_info temp_die;
23689 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23690 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23691
23692 gdb_assert (cu->per_cu != NULL);
23693
23694 target_cu = cu;
23695
23696 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23697 "source CU contains target offset: %d",
23698 sect_offset_str (cu->per_cu->sect_off),
23699 sect_offset_str (sect_off),
23700 cu->header.offset_in_cu_p (sect_off));
23701
23702 if (cu->per_cu->is_debug_types)
23703 {
23704 /* .debug_types CUs cannot reference anything outside their CU.
23705 If they need to, they have to reference a signatured type via
23706 DW_FORM_ref_sig8. */
23707 if (!cu->header.offset_in_cu_p (sect_off))
23708 return NULL;
23709 }
23710 else if (offset_in_dwz != cu->per_cu->is_dwz
23711 || !cu->header.offset_in_cu_p (sect_off))
23712 {
23713 struct dwarf2_per_cu_data *per_cu;
23714
23715 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23716 per_objfile);
23717
23718 dwarf_read_debug_printf_v ("target CU offset: %s, "
23719 "target CU DIEs loaded: %d",
23720 sect_offset_str (per_cu->sect_off),
23721 per_objfile->get_cu (per_cu) != nullptr);
23722
23723 /* If necessary, add it to the queue and load its DIEs.
23724
23725 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23726 it doesn't mean they are currently loaded. Since we require them
23727 to be loaded, we must check for ourselves. */
23728 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language)
23729 || per_objfile->get_cu (per_cu) == nullptr)
23730 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23731 false, cu->language);
23732
23733 target_cu = per_objfile->get_cu (per_cu);
23734 gdb_assert (target_cu != nullptr);
23735 }
23736 else if (cu->dies == NULL)
23737 {
23738 /* We're loading full DIEs during partial symbol reading. */
23739 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
23740 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23741 language_minimal);
23742 }
23743
23744 *ref_cu = target_cu;
23745 temp_die.sect_off = sect_off;
23746
23747 if (target_cu != cu)
23748 target_cu->ancestor = cu;
23749
23750 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23751 &temp_die,
23752 to_underlying (sect_off));
23753 }
23754
23755 /* Follow reference attribute ATTR of SRC_DIE.
23756 On entry *REF_CU is the CU of SRC_DIE.
23757 On exit *REF_CU is the CU of the result. */
23758
23759 static struct die_info *
23760 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23761 struct dwarf2_cu **ref_cu)
23762 {
23763 sect_offset sect_off = attr->get_ref_die_offset ();
23764 struct dwarf2_cu *cu = *ref_cu;
23765 struct die_info *die;
23766
23767 die = follow_die_offset (sect_off,
23768 (attr->form == DW_FORM_GNU_ref_alt
23769 || cu->per_cu->is_dwz),
23770 ref_cu);
23771 if (!die)
23772 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23773 "at %s [in module %s]"),
23774 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23775 objfile_name (cu->per_objfile->objfile));
23776
23777 return die;
23778 }
23779
23780 /* See read.h. */
23781
23782 struct dwarf2_locexpr_baton
23783 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23784 dwarf2_per_cu_data *per_cu,
23785 dwarf2_per_objfile *per_objfile,
23786 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23787 bool resolve_abstract_p)
23788 {
23789 struct die_info *die;
23790 struct attribute *attr;
23791 struct dwarf2_locexpr_baton retval;
23792 struct objfile *objfile = per_objfile->objfile;
23793
23794 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23795 if (cu == nullptr)
23796 cu = load_cu (per_cu, per_objfile, false);
23797
23798 if (cu == nullptr)
23799 {
23800 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23801 Instead just throw an error, not much else we can do. */
23802 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23803 sect_offset_str (sect_off), objfile_name (objfile));
23804 }
23805
23806 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23807 if (!die)
23808 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23809 sect_offset_str (sect_off), objfile_name (objfile));
23810
23811 attr = dwarf2_attr (die, DW_AT_location, cu);
23812 if (!attr && resolve_abstract_p
23813 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23814 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23815 {
23816 CORE_ADDR pc = get_frame_pc ();
23817 CORE_ADDR baseaddr = objfile->text_section_offset ();
23818 struct gdbarch *gdbarch = objfile->arch ();
23819
23820 for (const auto &cand_off
23821 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23822 {
23823 struct dwarf2_cu *cand_cu = cu;
23824 struct die_info *cand
23825 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23826 if (!cand
23827 || !cand->parent
23828 || cand->parent->tag != DW_TAG_subprogram)
23829 continue;
23830
23831 CORE_ADDR pc_low, pc_high;
23832 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23833 if (pc_low == ((CORE_ADDR) -1))
23834 continue;
23835 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23836 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23837 if (!(pc_low <= pc && pc < pc_high))
23838 continue;
23839
23840 die = cand;
23841 attr = dwarf2_attr (die, DW_AT_location, cu);
23842 break;
23843 }
23844 }
23845
23846 if (!attr)
23847 {
23848 /* DWARF: "If there is no such attribute, then there is no effect.".
23849 DATA is ignored if SIZE is 0. */
23850
23851 retval.data = NULL;
23852 retval.size = 0;
23853 }
23854 else if (attr->form_is_section_offset ())
23855 {
23856 struct dwarf2_loclist_baton loclist_baton;
23857 CORE_ADDR pc = get_frame_pc ();
23858 size_t size;
23859
23860 fill_in_loclist_baton (cu, &loclist_baton, attr);
23861
23862 retval.data = dwarf2_find_location_expression (&loclist_baton,
23863 &size, pc);
23864 retval.size = size;
23865 }
23866 else
23867 {
23868 if (!attr->form_is_block ())
23869 error (_("Dwarf Error: DIE at %s referenced in module %s "
23870 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23871 sect_offset_str (sect_off), objfile_name (objfile));
23872
23873 struct dwarf_block *block = attr->as_block ();
23874 retval.data = block->data;
23875 retval.size = block->size;
23876 }
23877 retval.per_objfile = per_objfile;
23878 retval.per_cu = cu->per_cu;
23879
23880 per_objfile->age_comp_units ();
23881
23882 return retval;
23883 }
23884
23885 /* See read.h. */
23886
23887 struct dwarf2_locexpr_baton
23888 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23889 dwarf2_per_cu_data *per_cu,
23890 dwarf2_per_objfile *per_objfile,
23891 gdb::function_view<CORE_ADDR ()> get_frame_pc)
23892 {
23893 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23894
23895 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
23896 get_frame_pc);
23897 }
23898
23899 /* Write a constant of a given type as target-ordered bytes into
23900 OBSTACK. */
23901
23902 static const gdb_byte *
23903 write_constant_as_bytes (struct obstack *obstack,
23904 enum bfd_endian byte_order,
23905 struct type *type,
23906 ULONGEST value,
23907 LONGEST *len)
23908 {
23909 gdb_byte *result;
23910
23911 *len = TYPE_LENGTH (type);
23912 result = (gdb_byte *) obstack_alloc (obstack, *len);
23913 store_unsigned_integer (result, *len, byte_order, value);
23914
23915 return result;
23916 }
23917
23918 /* See read.h. */
23919
23920 const gdb_byte *
23921 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23922 dwarf2_per_cu_data *per_cu,
23923 dwarf2_per_objfile *per_objfile,
23924 obstack *obstack,
23925 LONGEST *len)
23926 {
23927 struct die_info *die;
23928 struct attribute *attr;
23929 const gdb_byte *result = NULL;
23930 struct type *type;
23931 LONGEST value;
23932 enum bfd_endian byte_order;
23933 struct objfile *objfile = per_objfile->objfile;
23934
23935 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23936 if (cu == nullptr)
23937 cu = load_cu (per_cu, per_objfile, false);
23938
23939 if (cu == nullptr)
23940 {
23941 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23942 Instead just throw an error, not much else we can do. */
23943 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23944 sect_offset_str (sect_off), objfile_name (objfile));
23945 }
23946
23947 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23948 if (!die)
23949 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23950 sect_offset_str (sect_off), objfile_name (objfile));
23951
23952 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23953 if (attr == NULL)
23954 return NULL;
23955
23956 byte_order = (bfd_big_endian (objfile->obfd)
23957 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23958
23959 switch (attr->form)
23960 {
23961 case DW_FORM_addr:
23962 case DW_FORM_addrx:
23963 case DW_FORM_GNU_addr_index:
23964 {
23965 gdb_byte *tem;
23966
23967 *len = cu->header.addr_size;
23968 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23969 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
23970 result = tem;
23971 }
23972 break;
23973 case DW_FORM_string:
23974 case DW_FORM_strp:
23975 case DW_FORM_strx:
23976 case DW_FORM_GNU_str_index:
23977 case DW_FORM_GNU_strp_alt:
23978 /* The string is already allocated on the objfile obstack, point
23979 directly to it. */
23980 {
23981 const char *attr_name = attr->as_string ();
23982 result = (const gdb_byte *) attr_name;
23983 *len = strlen (attr_name);
23984 }
23985 break;
23986 case DW_FORM_block1:
23987 case DW_FORM_block2:
23988 case DW_FORM_block4:
23989 case DW_FORM_block:
23990 case DW_FORM_exprloc:
23991 case DW_FORM_data16:
23992 {
23993 struct dwarf_block *block = attr->as_block ();
23994 result = block->data;
23995 *len = block->size;
23996 }
23997 break;
23998
23999 /* The DW_AT_const_value attributes are supposed to carry the
24000 symbol's value "represented as it would be on the target
24001 architecture." By the time we get here, it's already been
24002 converted to host endianness, so we just need to sign- or
24003 zero-extend it as appropriate. */
24004 case DW_FORM_data1:
24005 type = die_type (die, cu);
24006 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
24007 if (result == NULL)
24008 result = write_constant_as_bytes (obstack, byte_order,
24009 type, value, len);
24010 break;
24011 case DW_FORM_data2:
24012 type = die_type (die, cu);
24013 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
24014 if (result == NULL)
24015 result = write_constant_as_bytes (obstack, byte_order,
24016 type, value, len);
24017 break;
24018 case DW_FORM_data4:
24019 type = die_type (die, cu);
24020 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
24021 if (result == NULL)
24022 result = write_constant_as_bytes (obstack, byte_order,
24023 type, value, len);
24024 break;
24025 case DW_FORM_data8:
24026 type = die_type (die, cu);
24027 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
24028 if (result == NULL)
24029 result = write_constant_as_bytes (obstack, byte_order,
24030 type, value, len);
24031 break;
24032
24033 case DW_FORM_sdata:
24034 case DW_FORM_implicit_const:
24035 type = die_type (die, cu);
24036 result = write_constant_as_bytes (obstack, byte_order,
24037 type, attr->as_signed (), len);
24038 break;
24039
24040 case DW_FORM_udata:
24041 type = die_type (die, cu);
24042 result = write_constant_as_bytes (obstack, byte_order,
24043 type, attr->as_unsigned (), len);
24044 break;
24045
24046 default:
24047 complaint (_("unsupported const value attribute form: '%s'"),
24048 dwarf_form_name (attr->form));
24049 break;
24050 }
24051
24052 return result;
24053 }
24054
24055 /* See read.h. */
24056
24057 struct type *
24058 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
24059 dwarf2_per_cu_data *per_cu,
24060 dwarf2_per_objfile *per_objfile)
24061 {
24062 struct die_info *die;
24063
24064 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
24065 if (cu == nullptr)
24066 cu = load_cu (per_cu, per_objfile, false);
24067
24068 if (cu == nullptr)
24069 return nullptr;
24070
24071 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
24072 if (!die)
24073 return NULL;
24074
24075 return die_type (die, cu);
24076 }
24077
24078 /* See read.h. */
24079
24080 struct type *
24081 dwarf2_get_die_type (cu_offset die_offset,
24082 dwarf2_per_cu_data *per_cu,
24083 dwarf2_per_objfile *per_objfile)
24084 {
24085 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
24086 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
24087 }
24088
24089 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
24090 On entry *REF_CU is the CU of SRC_DIE.
24091 On exit *REF_CU is the CU of the result.
24092 Returns NULL if the referenced DIE isn't found. */
24093
24094 static struct die_info *
24095 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
24096 struct dwarf2_cu **ref_cu)
24097 {
24098 struct die_info temp_die;
24099 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
24100 struct die_info *die;
24101 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
24102
24103
24104 /* While it might be nice to assert sig_type->type == NULL here,
24105 we can get here for DW_AT_imported_declaration where we need
24106 the DIE not the type. */
24107
24108 /* If necessary, add it to the queue and load its DIEs.
24109
24110 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
24111 it doesn't mean they are currently loaded. Since we require them
24112 to be loaded, we must check for ourselves. */
24113 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
24114 language_minimal)
24115 || per_objfile->get_cu (&sig_type->per_cu) == nullptr)
24116 read_signatured_type (sig_type, per_objfile);
24117
24118 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
24119 gdb_assert (sig_cu != NULL);
24120 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
24121 temp_die.sect_off = sig_type->type_offset_in_section;
24122 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
24123 to_underlying (temp_die.sect_off));
24124 if (die)
24125 {
24126 /* For .gdb_index version 7 keep track of included TUs.
24127 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
24128 if (per_objfile->per_bfd->index_table != NULL
24129 && per_objfile->per_bfd->index_table->version <= 7)
24130 {
24131 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
24132 }
24133
24134 *ref_cu = sig_cu;
24135 if (sig_cu != cu)
24136 sig_cu->ancestor = cu;
24137
24138 return die;
24139 }
24140
24141 return NULL;
24142 }
24143
24144 /* Follow signatured type referenced by ATTR in SRC_DIE.
24145 On entry *REF_CU is the CU of SRC_DIE.
24146 On exit *REF_CU is the CU of the result.
24147 The result is the DIE of the type.
24148 If the referenced type cannot be found an error is thrown. */
24149
24150 static struct die_info *
24151 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
24152 struct dwarf2_cu **ref_cu)
24153 {
24154 ULONGEST signature = attr->as_signature ();
24155 struct signatured_type *sig_type;
24156 struct die_info *die;
24157
24158 gdb_assert (attr->form == DW_FORM_ref_sig8);
24159
24160 sig_type = lookup_signatured_type (*ref_cu, signature);
24161 /* sig_type will be NULL if the signatured type is missing from
24162 the debug info. */
24163 if (sig_type == NULL)
24164 {
24165 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24166 " from DIE at %s [in module %s]"),
24167 hex_string (signature), sect_offset_str (src_die->sect_off),
24168 objfile_name ((*ref_cu)->per_objfile->objfile));
24169 }
24170
24171 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
24172 if (die == NULL)
24173 {
24174 dump_die_for_error (src_die);
24175 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24176 " from DIE at %s [in module %s]"),
24177 hex_string (signature), sect_offset_str (src_die->sect_off),
24178 objfile_name ((*ref_cu)->per_objfile->objfile));
24179 }
24180
24181 return die;
24182 }
24183
24184 /* Get the type specified by SIGNATURE referenced in DIE/CU,
24185 reading in and processing the type unit if necessary. */
24186
24187 static struct type *
24188 get_signatured_type (struct die_info *die, ULONGEST signature,
24189 struct dwarf2_cu *cu)
24190 {
24191 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24192 struct signatured_type *sig_type;
24193 struct dwarf2_cu *type_cu;
24194 struct die_info *type_die;
24195 struct type *type;
24196
24197 sig_type = lookup_signatured_type (cu, signature);
24198 /* sig_type will be NULL if the signatured type is missing from
24199 the debug info. */
24200 if (sig_type == NULL)
24201 {
24202 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24203 " from DIE at %s [in module %s]"),
24204 hex_string (signature), sect_offset_str (die->sect_off),
24205 objfile_name (per_objfile->objfile));
24206 return build_error_marker_type (cu, die);
24207 }
24208
24209 /* If we already know the type we're done. */
24210 type = per_objfile->get_type_for_signatured_type (sig_type);
24211 if (type != nullptr)
24212 return type;
24213
24214 type_cu = cu;
24215 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24216 if (type_die != NULL)
24217 {
24218 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24219 is created. This is important, for example, because for c++ classes
24220 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24221 type = read_type_die (type_die, type_cu);
24222 if (type == NULL)
24223 {
24224 complaint (_("Dwarf Error: Cannot build signatured type %s"
24225 " referenced from DIE at %s [in module %s]"),
24226 hex_string (signature), sect_offset_str (die->sect_off),
24227 objfile_name (per_objfile->objfile));
24228 type = build_error_marker_type (cu, die);
24229 }
24230 }
24231 else
24232 {
24233 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24234 " from DIE at %s [in module %s]"),
24235 hex_string (signature), sect_offset_str (die->sect_off),
24236 objfile_name (per_objfile->objfile));
24237 type = build_error_marker_type (cu, die);
24238 }
24239
24240 per_objfile->set_type_for_signatured_type (sig_type, type);
24241
24242 return type;
24243 }
24244
24245 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24246 reading in and processing the type unit if necessary. */
24247
24248 static struct type *
24249 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
24250 struct dwarf2_cu *cu) /* ARI: editCase function */
24251 {
24252 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
24253 if (attr->form_is_ref ())
24254 {
24255 struct dwarf2_cu *type_cu = cu;
24256 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24257
24258 return read_type_die (type_die, type_cu);
24259 }
24260 else if (attr->form == DW_FORM_ref_sig8)
24261 {
24262 return get_signatured_type (die, attr->as_signature (), cu);
24263 }
24264 else
24265 {
24266 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24267
24268 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
24269 " at %s [in module %s]"),
24270 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
24271 objfile_name (per_objfile->objfile));
24272 return build_error_marker_type (cu, die);
24273 }
24274 }
24275
24276 /* Load the DIEs associated with type unit PER_CU into memory. */
24277
24278 static void
24279 load_full_type_unit (dwarf2_per_cu_data *per_cu,
24280 dwarf2_per_objfile *per_objfile)
24281 {
24282 struct signatured_type *sig_type;
24283
24284 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24285 gdb_assert (! per_cu->type_unit_group_p ());
24286
24287 /* We have the per_cu, but we need the signatured_type.
24288 Fortunately this is an easy translation. */
24289 gdb_assert (per_cu->is_debug_types);
24290 sig_type = (struct signatured_type *) per_cu;
24291
24292 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
24293
24294 read_signatured_type (sig_type, per_objfile);
24295
24296 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
24297 }
24298
24299 /* Read in a signatured type and build its CU and DIEs.
24300 If the type is a stub for the real type in a DWO file,
24301 read in the real type from the DWO file as well. */
24302
24303 static void
24304 read_signatured_type (signatured_type *sig_type,
24305 dwarf2_per_objfile *per_objfile)
24306 {
24307 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
24308
24309 gdb_assert (per_cu->is_debug_types);
24310 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
24311
24312 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
24313
24314 if (!reader.dummy_p)
24315 {
24316 struct dwarf2_cu *cu = reader.cu;
24317 const gdb_byte *info_ptr = reader.info_ptr;
24318
24319 gdb_assert (cu->die_hash == NULL);
24320 cu->die_hash =
24321 htab_create_alloc_ex (cu->header.length / 12,
24322 die_hash,
24323 die_eq,
24324 NULL,
24325 &cu->comp_unit_obstack,
24326 hashtab_obstack_allocate,
24327 dummy_obstack_deallocate);
24328
24329 if (reader.comp_unit_die->has_children)
24330 reader.comp_unit_die->child
24331 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24332 reader.comp_unit_die);
24333 cu->dies = reader.comp_unit_die;
24334 /* comp_unit_die is not stored in die_hash, no need. */
24335
24336 /* We try not to read any attributes in this function, because
24337 not all CUs needed for references have been loaded yet, and
24338 symbol table processing isn't initialized. But we have to
24339 set the CU language, or we won't be able to build types
24340 correctly. Similarly, if we do not read the producer, we can
24341 not apply producer-specific interpretation. */
24342 prepare_one_comp_unit (cu, cu->dies, language_minimal);
24343
24344 reader.keep ();
24345 }
24346
24347 sig_type->per_cu.tu_read = 1;
24348 }
24349
24350 /* Decode simple location descriptions.
24351 Given a pointer to a dwarf block that defines a location, compute
24352 the location and return the value. If COMPUTED is non-null, it is
24353 set to true to indicate that decoding was successful, and false
24354 otherwise. If COMPUTED is null, then this function may emit a
24355 complaint. */
24356
24357 static CORE_ADDR
24358 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
24359 {
24360 struct objfile *objfile = cu->per_objfile->objfile;
24361 size_t i;
24362 size_t size = blk->size;
24363 const gdb_byte *data = blk->data;
24364 CORE_ADDR stack[64];
24365 int stacki;
24366 unsigned int bytes_read, unsnd;
24367 gdb_byte op;
24368
24369 if (computed != nullptr)
24370 *computed = false;
24371
24372 i = 0;
24373 stacki = 0;
24374 stack[stacki] = 0;
24375 stack[++stacki] = 0;
24376
24377 while (i < size)
24378 {
24379 op = data[i++];
24380 switch (op)
24381 {
24382 case DW_OP_lit0:
24383 case DW_OP_lit1:
24384 case DW_OP_lit2:
24385 case DW_OP_lit3:
24386 case DW_OP_lit4:
24387 case DW_OP_lit5:
24388 case DW_OP_lit6:
24389 case DW_OP_lit7:
24390 case DW_OP_lit8:
24391 case DW_OP_lit9:
24392 case DW_OP_lit10:
24393 case DW_OP_lit11:
24394 case DW_OP_lit12:
24395 case DW_OP_lit13:
24396 case DW_OP_lit14:
24397 case DW_OP_lit15:
24398 case DW_OP_lit16:
24399 case DW_OP_lit17:
24400 case DW_OP_lit18:
24401 case DW_OP_lit19:
24402 case DW_OP_lit20:
24403 case DW_OP_lit21:
24404 case DW_OP_lit22:
24405 case DW_OP_lit23:
24406 case DW_OP_lit24:
24407 case DW_OP_lit25:
24408 case DW_OP_lit26:
24409 case DW_OP_lit27:
24410 case DW_OP_lit28:
24411 case DW_OP_lit29:
24412 case DW_OP_lit30:
24413 case DW_OP_lit31:
24414 stack[++stacki] = op - DW_OP_lit0;
24415 break;
24416
24417 case DW_OP_reg0:
24418 case DW_OP_reg1:
24419 case DW_OP_reg2:
24420 case DW_OP_reg3:
24421 case DW_OP_reg4:
24422 case DW_OP_reg5:
24423 case DW_OP_reg6:
24424 case DW_OP_reg7:
24425 case DW_OP_reg8:
24426 case DW_OP_reg9:
24427 case DW_OP_reg10:
24428 case DW_OP_reg11:
24429 case DW_OP_reg12:
24430 case DW_OP_reg13:
24431 case DW_OP_reg14:
24432 case DW_OP_reg15:
24433 case DW_OP_reg16:
24434 case DW_OP_reg17:
24435 case DW_OP_reg18:
24436 case DW_OP_reg19:
24437 case DW_OP_reg20:
24438 case DW_OP_reg21:
24439 case DW_OP_reg22:
24440 case DW_OP_reg23:
24441 case DW_OP_reg24:
24442 case DW_OP_reg25:
24443 case DW_OP_reg26:
24444 case DW_OP_reg27:
24445 case DW_OP_reg28:
24446 case DW_OP_reg29:
24447 case DW_OP_reg30:
24448 case DW_OP_reg31:
24449 stack[++stacki] = op - DW_OP_reg0;
24450 if (i < size)
24451 {
24452 if (computed == nullptr)
24453 dwarf2_complex_location_expr_complaint ();
24454 else
24455 return 0;
24456 }
24457 break;
24458
24459 case DW_OP_regx:
24460 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24461 i += bytes_read;
24462 stack[++stacki] = unsnd;
24463 if (i < size)
24464 {
24465 if (computed == nullptr)
24466 dwarf2_complex_location_expr_complaint ();
24467 else
24468 return 0;
24469 }
24470 break;
24471
24472 case DW_OP_addr:
24473 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24474 &bytes_read);
24475 i += bytes_read;
24476 break;
24477
24478 case DW_OP_const1u:
24479 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24480 i += 1;
24481 break;
24482
24483 case DW_OP_const1s:
24484 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24485 i += 1;
24486 break;
24487
24488 case DW_OP_const2u:
24489 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24490 i += 2;
24491 break;
24492
24493 case DW_OP_const2s:
24494 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24495 i += 2;
24496 break;
24497
24498 case DW_OP_const4u:
24499 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24500 i += 4;
24501 break;
24502
24503 case DW_OP_const4s:
24504 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24505 i += 4;
24506 break;
24507
24508 case DW_OP_const8u:
24509 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24510 i += 8;
24511 break;
24512
24513 case DW_OP_constu:
24514 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24515 &bytes_read);
24516 i += bytes_read;
24517 break;
24518
24519 case DW_OP_consts:
24520 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24521 i += bytes_read;
24522 break;
24523
24524 case DW_OP_dup:
24525 stack[stacki + 1] = stack[stacki];
24526 stacki++;
24527 break;
24528
24529 case DW_OP_plus:
24530 stack[stacki - 1] += stack[stacki];
24531 stacki--;
24532 break;
24533
24534 case DW_OP_plus_uconst:
24535 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24536 &bytes_read);
24537 i += bytes_read;
24538 break;
24539
24540 case DW_OP_minus:
24541 stack[stacki - 1] -= stack[stacki];
24542 stacki--;
24543 break;
24544
24545 case DW_OP_deref:
24546 /* If we're not the last op, then we definitely can't encode
24547 this using GDB's address_class enum. This is valid for partial
24548 global symbols, although the variable's address will be bogus
24549 in the psymtab. */
24550 if (i < size)
24551 {
24552 if (computed == nullptr)
24553 dwarf2_complex_location_expr_complaint ();
24554 else
24555 return 0;
24556 }
24557 break;
24558
24559 case DW_OP_GNU_push_tls_address:
24560 case DW_OP_form_tls_address:
24561 /* The top of the stack has the offset from the beginning
24562 of the thread control block at which the variable is located. */
24563 /* Nothing should follow this operator, so the top of stack would
24564 be returned. */
24565 /* This is valid for partial global symbols, but the variable's
24566 address will be bogus in the psymtab. Make it always at least
24567 non-zero to not look as a variable garbage collected by linker
24568 which have DW_OP_addr 0. */
24569 if (i < size)
24570 {
24571 if (computed == nullptr)
24572 dwarf2_complex_location_expr_complaint ();
24573 else
24574 return 0;
24575 }
24576 stack[stacki]++;
24577 break;
24578
24579 case DW_OP_GNU_uninit:
24580 if (computed != nullptr)
24581 return 0;
24582 break;
24583
24584 case DW_OP_addrx:
24585 case DW_OP_GNU_addr_index:
24586 case DW_OP_GNU_const_index:
24587 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24588 &bytes_read);
24589 i += bytes_read;
24590 break;
24591
24592 default:
24593 if (computed == nullptr)
24594 {
24595 const char *name = get_DW_OP_name (op);
24596
24597 if (name)
24598 complaint (_("unsupported stack op: '%s'"),
24599 name);
24600 else
24601 complaint (_("unsupported stack op: '%02x'"),
24602 op);
24603 }
24604
24605 return (stack[stacki]);
24606 }
24607
24608 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24609 outside of the allocated space. Also enforce minimum>0. */
24610 if (stacki >= ARRAY_SIZE (stack) - 1)
24611 {
24612 if (computed == nullptr)
24613 complaint (_("location description stack overflow"));
24614 return 0;
24615 }
24616
24617 if (stacki <= 0)
24618 {
24619 if (computed == nullptr)
24620 complaint (_("location description stack underflow"));
24621 return 0;
24622 }
24623 }
24624
24625 if (computed != nullptr)
24626 *computed = true;
24627 return (stack[stacki]);
24628 }
24629
24630 /* memory allocation interface */
24631
24632 static struct dwarf_block *
24633 dwarf_alloc_block (struct dwarf2_cu *cu)
24634 {
24635 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24636 }
24637
24638 static struct die_info *
24639 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24640 {
24641 struct die_info *die;
24642 size_t size = sizeof (struct die_info);
24643
24644 if (num_attrs > 1)
24645 size += (num_attrs - 1) * sizeof (struct attribute);
24646
24647 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24648 memset (die, 0, sizeof (struct die_info));
24649 return (die);
24650 }
24651
24652 \f
24653
24654 /* Macro support. */
24655
24656 /* An overload of dwarf_decode_macros that finds the correct section
24657 and ensures it is read in before calling the other overload. */
24658
24659 static void
24660 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24661 int section_is_gnu)
24662 {
24663 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24664 struct objfile *objfile = per_objfile->objfile;
24665 const struct line_header *lh = cu->line_header;
24666 unsigned int offset_size = cu->header.offset_size;
24667 struct dwarf2_section_info *section;
24668 const char *section_name;
24669
24670 if (cu->dwo_unit != nullptr)
24671 {
24672 if (section_is_gnu)
24673 {
24674 section = &cu->dwo_unit->dwo_file->sections.macro;
24675 section_name = ".debug_macro.dwo";
24676 }
24677 else
24678 {
24679 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24680 section_name = ".debug_macinfo.dwo";
24681 }
24682 }
24683 else
24684 {
24685 if (section_is_gnu)
24686 {
24687 section = &per_objfile->per_bfd->macro;
24688 section_name = ".debug_macro";
24689 }
24690 else
24691 {
24692 section = &per_objfile->per_bfd->macinfo;
24693 section_name = ".debug_macinfo";
24694 }
24695 }
24696
24697 section->read (objfile);
24698 if (section->buffer == nullptr)
24699 {
24700 complaint (_("missing %s section"), section_name);
24701 return;
24702 }
24703
24704 buildsym_compunit *builder = cu->get_builder ();
24705
24706 struct dwarf2_section_info *str_offsets_section;
24707 struct dwarf2_section_info *str_section;
24708 ULONGEST str_offsets_base;
24709
24710 if (cu->dwo_unit != nullptr)
24711 {
24712 str_offsets_section = &cu->dwo_unit->dwo_file
24713 ->sections.str_offsets;
24714 str_section = &cu->dwo_unit->dwo_file->sections.str;
24715 str_offsets_base = cu->header.addr_size;
24716 }
24717 else
24718 {
24719 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24720 str_section = &per_objfile->per_bfd->str;
24721 str_offsets_base = *cu->str_offsets_base;
24722 }
24723
24724 dwarf_decode_macros (per_objfile, builder, section, lh,
24725 offset_size, offset, str_section, str_offsets_section,
24726 str_offsets_base, section_is_gnu);
24727 }
24728
24729 /* Return the .debug_loc section to use for CU.
24730 For DWO files use .debug_loc.dwo. */
24731
24732 static struct dwarf2_section_info *
24733 cu_debug_loc_section (struct dwarf2_cu *cu)
24734 {
24735 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24736
24737 if (cu->dwo_unit)
24738 {
24739 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24740
24741 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24742 }
24743 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24744 : &per_objfile->per_bfd->loc);
24745 }
24746
24747 /* Return the .debug_rnglists section to use for CU. */
24748 static struct dwarf2_section_info *
24749 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24750 {
24751 if (cu->header.version < 5)
24752 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24753 cu->header.version);
24754 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24755
24756 /* Make sure we read the .debug_rnglists section from the file that
24757 contains the DW_AT_ranges attribute we are reading. Normally that
24758 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24759 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24760 program. */
24761 if (cu->dwo_unit != nullptr
24762 && tag != DW_TAG_compile_unit
24763 && tag != DW_TAG_skeleton_unit)
24764 {
24765 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24766
24767 if (sections->rnglists.size > 0)
24768 return &sections->rnglists;
24769 else
24770 error (_(".debug_rnglists section is missing from .dwo file."));
24771 }
24772 return &dwarf2_per_objfile->per_bfd->rnglists;
24773 }
24774
24775 /* A helper function that fills in a dwarf2_loclist_baton. */
24776
24777 static void
24778 fill_in_loclist_baton (struct dwarf2_cu *cu,
24779 struct dwarf2_loclist_baton *baton,
24780 const struct attribute *attr)
24781 {
24782 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24783 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24784
24785 section->read (per_objfile->objfile);
24786
24787 baton->per_objfile = per_objfile;
24788 baton->per_cu = cu->per_cu;
24789 gdb_assert (baton->per_cu);
24790 /* We don't know how long the location list is, but make sure we
24791 don't run off the edge of the section. */
24792 baton->size = section->size - attr->as_unsigned ();
24793 baton->data = section->buffer + attr->as_unsigned ();
24794 if (cu->base_address.has_value ())
24795 baton->base_address = *cu->base_address;
24796 else
24797 baton->base_address = 0;
24798 baton->from_dwo = cu->dwo_unit != NULL;
24799 }
24800
24801 static void
24802 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24803 struct dwarf2_cu *cu, int is_block)
24804 {
24805 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24806 struct objfile *objfile = per_objfile->objfile;
24807 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24808
24809 if (attr->form_is_section_offset ()
24810 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24811 the section. If so, fall through to the complaint in the
24812 other branch. */
24813 && attr->as_unsigned () < section->get_size (objfile))
24814 {
24815 struct dwarf2_loclist_baton *baton;
24816
24817 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24818
24819 fill_in_loclist_baton (cu, baton, attr);
24820
24821 if (!cu->base_address.has_value ())
24822 complaint (_("Location list used without "
24823 "specifying the CU base address."));
24824
24825 SYMBOL_ACLASS_INDEX (sym) = (is_block
24826 ? dwarf2_loclist_block_index
24827 : dwarf2_loclist_index);
24828 SYMBOL_LOCATION_BATON (sym) = baton;
24829 }
24830 else
24831 {
24832 struct dwarf2_locexpr_baton *baton;
24833
24834 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24835 baton->per_objfile = per_objfile;
24836 baton->per_cu = cu->per_cu;
24837 gdb_assert (baton->per_cu);
24838
24839 if (attr->form_is_block ())
24840 {
24841 /* Note that we're just copying the block's data pointer
24842 here, not the actual data. We're still pointing into the
24843 info_buffer for SYM's objfile; right now we never release
24844 that buffer, but when we do clean up properly this may
24845 need to change. */
24846 struct dwarf_block *block = attr->as_block ();
24847 baton->size = block->size;
24848 baton->data = block->data;
24849 }
24850 else
24851 {
24852 dwarf2_invalid_attrib_class_complaint ("location description",
24853 sym->natural_name ());
24854 baton->size = 0;
24855 }
24856
24857 SYMBOL_ACLASS_INDEX (sym) = (is_block
24858 ? dwarf2_locexpr_block_index
24859 : dwarf2_locexpr_index);
24860 SYMBOL_LOCATION_BATON (sym) = baton;
24861 }
24862 }
24863
24864 /* See read.h. */
24865
24866 const comp_unit_head *
24867 dwarf2_per_cu_data::get_header () const
24868 {
24869 if (!m_header_read_in)
24870 {
24871 const gdb_byte *info_ptr
24872 = this->section->buffer + to_underlying (this->sect_off);
24873
24874 memset (&m_header, 0, sizeof (m_header));
24875
24876 read_comp_unit_head (&m_header, info_ptr, this->section,
24877 rcuh_kind::COMPILE);
24878
24879 m_header_read_in = true;
24880 }
24881
24882 return &m_header;
24883 }
24884
24885 /* See read.h. */
24886
24887 int
24888 dwarf2_per_cu_data::addr_size () const
24889 {
24890 return this->get_header ()->addr_size;
24891 }
24892
24893 /* See read.h. */
24894
24895 int
24896 dwarf2_per_cu_data::offset_size () const
24897 {
24898 return this->get_header ()->offset_size;
24899 }
24900
24901 /* See read.h. */
24902
24903 int
24904 dwarf2_per_cu_data::ref_addr_size () const
24905 {
24906 const comp_unit_head *header = this->get_header ();
24907
24908 if (header->version == 2)
24909 return header->addr_size;
24910 else
24911 return header->offset_size;
24912 }
24913
24914 /* See read.h. */
24915
24916 struct type *
24917 dwarf2_cu::addr_type () const
24918 {
24919 struct objfile *objfile = this->per_objfile->objfile;
24920 struct type *void_type = objfile_type (objfile)->builtin_void;
24921 struct type *addr_type = lookup_pointer_type (void_type);
24922 int addr_size = this->per_cu->addr_size ();
24923
24924 if (TYPE_LENGTH (addr_type) == addr_size)
24925 return addr_type;
24926
24927 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
24928 return addr_type;
24929 }
24930
24931 /* A helper function for dwarf2_find_containing_comp_unit that returns
24932 the index of the result, and that searches a vector. It will
24933 return a result even if the offset in question does not actually
24934 occur in any CU. This is separate so that it can be unit
24935 tested. */
24936
24937 static int
24938 dwarf2_find_containing_comp_unit
24939 (sect_offset sect_off,
24940 unsigned int offset_in_dwz,
24941 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
24942 {
24943 int low, high;
24944
24945 low = 0;
24946 high = all_comp_units.size () - 1;
24947 while (high > low)
24948 {
24949 struct dwarf2_per_cu_data *mid_cu;
24950 int mid = low + (high - low) / 2;
24951
24952 mid_cu = all_comp_units[mid];
24953 if (mid_cu->is_dwz > offset_in_dwz
24954 || (mid_cu->is_dwz == offset_in_dwz
24955 && mid_cu->sect_off + mid_cu->length > sect_off))
24956 high = mid;
24957 else
24958 low = mid + 1;
24959 }
24960 gdb_assert (low == high);
24961 return low;
24962 }
24963
24964 /* Locate the .debug_info compilation unit from CU's objfile which contains
24965 the DIE at OFFSET. Raises an error on failure. */
24966
24967 static struct dwarf2_per_cu_data *
24968 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24969 unsigned int offset_in_dwz,
24970 dwarf2_per_objfile *per_objfile)
24971 {
24972 int low = dwarf2_find_containing_comp_unit
24973 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24974 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
24975
24976 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24977 {
24978 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24979 error (_("Dwarf Error: could not find partial DIE containing "
24980 "offset %s [in module %s]"),
24981 sect_offset_str (sect_off),
24982 bfd_get_filename (per_objfile->objfile->obfd));
24983
24984 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
24985 <= sect_off);
24986 return per_objfile->per_bfd->all_comp_units[low-1];
24987 }
24988 else
24989 {
24990 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
24991 && sect_off >= this_cu->sect_off + this_cu->length)
24992 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24993 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24994 return this_cu;
24995 }
24996 }
24997
24998 #if GDB_SELF_TEST
24999
25000 namespace selftests {
25001 namespace find_containing_comp_unit {
25002
25003 static void
25004 run_test ()
25005 {
25006 struct dwarf2_per_cu_data one {};
25007 struct dwarf2_per_cu_data two {};
25008 struct dwarf2_per_cu_data three {};
25009 struct dwarf2_per_cu_data four {};
25010
25011 one.length = 5;
25012 two.sect_off = sect_offset (one.length);
25013 two.length = 7;
25014
25015 three.length = 5;
25016 three.is_dwz = 1;
25017 four.sect_off = sect_offset (three.length);
25018 four.length = 7;
25019 four.is_dwz = 1;
25020
25021 std::vector<dwarf2_per_cu_data *> units;
25022 units.push_back (&one);
25023 units.push_back (&two);
25024 units.push_back (&three);
25025 units.push_back (&four);
25026
25027 int result;
25028
25029 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
25030 SELF_CHECK (units[result] == &one);
25031 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
25032 SELF_CHECK (units[result] == &one);
25033 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
25034 SELF_CHECK (units[result] == &two);
25035
25036 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
25037 SELF_CHECK (units[result] == &three);
25038 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
25039 SELF_CHECK (units[result] == &three);
25040 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
25041 SELF_CHECK (units[result] == &four);
25042 }
25043
25044 }
25045 }
25046
25047 #endif /* GDB_SELF_TEST */
25048
25049 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
25050
25051 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
25052 dwarf2_per_objfile *per_objfile)
25053 : per_cu (per_cu),
25054 per_objfile (per_objfile),
25055 mark (false),
25056 has_loclist (false),
25057 checked_producer (false),
25058 producer_is_gxx_lt_4_6 (false),
25059 producer_is_gcc_lt_4_3 (false),
25060 producer_is_icc (false),
25061 producer_is_icc_lt_14 (false),
25062 producer_is_codewarrior (false),
25063 processing_has_namespace_info (false)
25064 {
25065 }
25066
25067 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25068
25069 static void
25070 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25071 enum language pretend_language)
25072 {
25073 struct attribute *attr;
25074
25075 /* Set the language we're debugging. */
25076 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25077 if (attr != nullptr)
25078 set_cu_language (attr->constant_value (0), cu);
25079 else
25080 {
25081 cu->language = pretend_language;
25082 cu->language_defn = language_def (cu->language);
25083 }
25084
25085 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25086 }
25087
25088 /* See read.h. */
25089
25090 dwarf2_cu *
25091 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
25092 {
25093 auto it = m_dwarf2_cus.find (per_cu);
25094 if (it == m_dwarf2_cus.end ())
25095 return nullptr;
25096
25097 return it->second;
25098 }
25099
25100 /* See read.h. */
25101
25102 void
25103 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
25104 {
25105 gdb_assert (this->get_cu (per_cu) == nullptr);
25106
25107 m_dwarf2_cus[per_cu] = cu;
25108 }
25109
25110 /* See read.h. */
25111
25112 void
25113 dwarf2_per_objfile::age_comp_units ()
25114 {
25115 dwarf_read_debug_printf_v ("running");
25116
25117 /* This is not expected to be called in the middle of CU expansion. There is
25118 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
25119 loaded in memory. Calling age_comp_units while the queue is in use could
25120 make us free the DIEs for a CU that is in the queue and therefore break
25121 that invariant. */
25122 gdb_assert (!this->per_bfd->queue.has_value ());
25123
25124 /* Start by clearing all marks. */
25125 for (auto pair : m_dwarf2_cus)
25126 pair.second->mark = false;
25127
25128 /* Traverse all CUs, mark them and their dependencies if used recently
25129 enough. */
25130 for (auto pair : m_dwarf2_cus)
25131 {
25132 dwarf2_cu *cu = pair.second;
25133
25134 cu->last_used++;
25135 if (cu->last_used <= dwarf_max_cache_age)
25136 dwarf2_mark (cu);
25137 }
25138
25139 /* Delete all CUs still not marked. */
25140 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
25141 {
25142 dwarf2_cu *cu = it->second;
25143
25144 if (!cu->mark)
25145 {
25146 dwarf_read_debug_printf_v ("deleting old CU %s",
25147 sect_offset_str (cu->per_cu->sect_off));
25148 delete cu;
25149 it = m_dwarf2_cus.erase (it);
25150 }
25151 else
25152 it++;
25153 }
25154 }
25155
25156 /* See read.h. */
25157
25158 void
25159 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
25160 {
25161 auto it = m_dwarf2_cus.find (per_cu);
25162 if (it == m_dwarf2_cus.end ())
25163 return;
25164
25165 delete it->second;
25166
25167 m_dwarf2_cus.erase (it);
25168 }
25169
25170 dwarf2_per_objfile::~dwarf2_per_objfile ()
25171 {
25172 remove_all_cus ();
25173 }
25174
25175 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25176 We store these in a hash table separate from the DIEs, and preserve them
25177 when the DIEs are flushed out of cache.
25178
25179 The CU "per_cu" pointer is needed because offset alone is not enough to
25180 uniquely identify the type. A file may have multiple .debug_types sections,
25181 or the type may come from a DWO file. Furthermore, while it's more logical
25182 to use per_cu->section+offset, with Fission the section with the data is in
25183 the DWO file but we don't know that section at the point we need it.
25184 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25185 because we can enter the lookup routine, get_die_type_at_offset, from
25186 outside this file, and thus won't necessarily have PER_CU->cu.
25187 Fortunately, PER_CU is stable for the life of the objfile. */
25188
25189 struct dwarf2_per_cu_offset_and_type
25190 {
25191 const struct dwarf2_per_cu_data *per_cu;
25192 sect_offset sect_off;
25193 struct type *type;
25194 };
25195
25196 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25197
25198 static hashval_t
25199 per_cu_offset_and_type_hash (const void *item)
25200 {
25201 const struct dwarf2_per_cu_offset_and_type *ofs
25202 = (const struct dwarf2_per_cu_offset_and_type *) item;
25203
25204 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25205 }
25206
25207 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25208
25209 static int
25210 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25211 {
25212 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25213 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25214 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25215 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25216
25217 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25218 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25219 }
25220
25221 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25222 table if necessary. For convenience, return TYPE.
25223
25224 The DIEs reading must have careful ordering to:
25225 * Not cause infinite loops trying to read in DIEs as a prerequisite for
25226 reading current DIE.
25227 * Not trying to dereference contents of still incompletely read in types
25228 while reading in other DIEs.
25229 * Enable referencing still incompletely read in types just by a pointer to
25230 the type without accessing its fields.
25231
25232 Therefore caller should follow these rules:
25233 * Try to fetch any prerequisite types we may need to build this DIE type
25234 before building the type and calling set_die_type.
25235 * After building type call set_die_type for current DIE as soon as
25236 possible before fetching more types to complete the current type.
25237 * Make the type as complete as possible before fetching more types. */
25238
25239 static struct type *
25240 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
25241 bool skip_data_location)
25242 {
25243 dwarf2_per_objfile *per_objfile = cu->per_objfile;
25244 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25245 struct objfile *objfile = per_objfile->objfile;
25246 struct attribute *attr;
25247 struct dynamic_prop prop;
25248
25249 /* For Ada types, make sure that the gnat-specific data is always
25250 initialized (if not already set). There are a few types where
25251 we should not be doing so, because the type-specific area is
25252 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25253 where the type-specific area is used to store the floatformat).
25254 But this is not a problem, because the gnat-specific information
25255 is actually not needed for these types. */
25256 if (need_gnat_info (cu)
25257 && type->code () != TYPE_CODE_FUNC
25258 && type->code () != TYPE_CODE_FLT
25259 && type->code () != TYPE_CODE_METHODPTR
25260 && type->code () != TYPE_CODE_MEMBERPTR
25261 && type->code () != TYPE_CODE_METHOD
25262 && type->code () != TYPE_CODE_FIXED_POINT
25263 && !HAVE_GNAT_AUX_INFO (type))
25264 INIT_GNAT_SPECIFIC (type);
25265
25266 /* Read DW_AT_allocated and set in type. */
25267 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25268 if (attr != NULL)
25269 {
25270 struct type *prop_type = cu->addr_sized_int_type (false);
25271 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25272 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
25273 }
25274
25275 /* Read DW_AT_associated and set in type. */
25276 attr = dwarf2_attr (die, DW_AT_associated, cu);
25277 if (attr != NULL)
25278 {
25279 struct type *prop_type = cu->addr_sized_int_type (false);
25280 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25281 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
25282 }
25283
25284 /* Read DW_AT_data_location and set in type. */
25285 if (!skip_data_location)
25286 {
25287 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25288 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
25289 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
25290 }
25291
25292 if (per_objfile->die_type_hash == NULL)
25293 per_objfile->die_type_hash
25294 = htab_up (htab_create_alloc (127,
25295 per_cu_offset_and_type_hash,
25296 per_cu_offset_and_type_eq,
25297 NULL, xcalloc, xfree));
25298
25299 ofs.per_cu = cu->per_cu;
25300 ofs.sect_off = die->sect_off;
25301 ofs.type = type;
25302 slot = (struct dwarf2_per_cu_offset_and_type **)
25303 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
25304 if (*slot)
25305 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25306 sect_offset_str (die->sect_off));
25307 *slot = XOBNEW (&objfile->objfile_obstack,
25308 struct dwarf2_per_cu_offset_and_type);
25309 **slot = ofs;
25310 return type;
25311 }
25312
25313 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25314 or return NULL if the die does not have a saved type. */
25315
25316 static struct type *
25317 get_die_type_at_offset (sect_offset sect_off,
25318 dwarf2_per_cu_data *per_cu,
25319 dwarf2_per_objfile *per_objfile)
25320 {
25321 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25322
25323 if (per_objfile->die_type_hash == NULL)
25324 return NULL;
25325
25326 ofs.per_cu = per_cu;
25327 ofs.sect_off = sect_off;
25328 slot = ((struct dwarf2_per_cu_offset_and_type *)
25329 htab_find (per_objfile->die_type_hash.get (), &ofs));
25330 if (slot)
25331 return slot->type;
25332 else
25333 return NULL;
25334 }
25335
25336 /* Look up the type for DIE in CU in die_type_hash,
25337 or return NULL if DIE does not have a saved type. */
25338
25339 static struct type *
25340 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25341 {
25342 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
25343 }
25344
25345 /* Add a dependence relationship from CU to REF_PER_CU. */
25346
25347 static void
25348 dwarf2_add_dependence (struct dwarf2_cu *cu,
25349 struct dwarf2_per_cu_data *ref_per_cu)
25350 {
25351 void **slot;
25352
25353 if (cu->dependencies == NULL)
25354 cu->dependencies
25355 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25356 NULL, &cu->comp_unit_obstack,
25357 hashtab_obstack_allocate,
25358 dummy_obstack_deallocate);
25359
25360 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25361 if (*slot == NULL)
25362 *slot = ref_per_cu;
25363 }
25364
25365 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25366 Set the mark field in every compilation unit in the
25367 cache that we must keep because we are keeping CU.
25368
25369 DATA is the dwarf2_per_objfile object in which to look up CUs. */
25370
25371 static int
25372 dwarf2_mark_helper (void **slot, void *data)
25373 {
25374 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
25375 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
25376 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
25377
25378 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25379 reading of the chain. As such dependencies remain valid it is not much
25380 useful to track and undo them during QUIT cleanups. */
25381 if (cu == nullptr)
25382 return 1;
25383
25384 if (cu->mark)
25385 return 1;
25386
25387 cu->mark = true;
25388
25389 if (cu->dependencies != nullptr)
25390 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
25391
25392 return 1;
25393 }
25394
25395 /* Set the mark field in CU and in every other compilation unit in the
25396 cache that we must keep because we are keeping CU. */
25397
25398 static void
25399 dwarf2_mark (struct dwarf2_cu *cu)
25400 {
25401 if (cu->mark)
25402 return;
25403
25404 cu->mark = true;
25405
25406 if (cu->dependencies != nullptr)
25407 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
25408 }
25409
25410 /* Trivial hash function for partial_die_info: the hash value of a DIE
25411 is its offset in .debug_info for this objfile. */
25412
25413 static hashval_t
25414 partial_die_hash (const void *item)
25415 {
25416 const struct partial_die_info *part_die
25417 = (const struct partial_die_info *) item;
25418
25419 return to_underlying (part_die->sect_off);
25420 }
25421
25422 /* Trivial comparison function for partial_die_info structures: two DIEs
25423 are equal if they have the same offset. */
25424
25425 static int
25426 partial_die_eq (const void *item_lhs, const void *item_rhs)
25427 {
25428 const struct partial_die_info *part_die_lhs
25429 = (const struct partial_die_info *) item_lhs;
25430 const struct partial_die_info *part_die_rhs
25431 = (const struct partial_die_info *) item_rhs;
25432
25433 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25434 }
25435
25436 struct cmd_list_element *set_dwarf_cmdlist;
25437 struct cmd_list_element *show_dwarf_cmdlist;
25438
25439 static void
25440 show_check_physname (struct ui_file *file, int from_tty,
25441 struct cmd_list_element *c, const char *value)
25442 {
25443 fprintf_filtered (file,
25444 _("Whether to check \"physname\" is %s.\n"),
25445 value);
25446 }
25447
25448 void _initialize_dwarf2_read ();
25449 void
25450 _initialize_dwarf2_read ()
25451 {
25452 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
25453 Set DWARF specific variables.\n\
25454 Configure DWARF variables such as the cache size."),
25455 &set_dwarf_cmdlist, "maintenance set dwarf ",
25456 0/*allow-unknown*/, &maintenance_set_cmdlist);
25457
25458 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
25459 Show DWARF specific variables.\n\
25460 Show DWARF variables such as the cache size."),
25461 &show_dwarf_cmdlist, "maintenance show dwarf ",
25462 0/*allow-unknown*/, &maintenance_show_cmdlist);
25463
25464 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25465 &dwarf_max_cache_age, _("\
25466 Set the upper bound on the age of cached DWARF compilation units."), _("\
25467 Show the upper bound on the age of cached DWARF compilation units."), _("\
25468 A higher limit means that cached compilation units will be stored\n\
25469 in memory longer, and more total memory will be used. Zero disables\n\
25470 caching, which can slow down startup."),
25471 NULL,
25472 show_dwarf_max_cache_age,
25473 &set_dwarf_cmdlist,
25474 &show_dwarf_cmdlist);
25475
25476 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25477 Set debugging of the DWARF reader."), _("\
25478 Show debugging of the DWARF reader."), _("\
25479 When enabled (non-zero), debugging messages are printed during DWARF\n\
25480 reading and symtab expansion. A value of 1 (one) provides basic\n\
25481 information. A value greater than 1 provides more verbose information."),
25482 NULL,
25483 NULL,
25484 &setdebuglist, &showdebuglist);
25485
25486 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25487 Set debugging of the DWARF DIE reader."), _("\
25488 Show debugging of the DWARF DIE reader."), _("\
25489 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25490 The value is the maximum depth to print."),
25491 NULL,
25492 NULL,
25493 &setdebuglist, &showdebuglist);
25494
25495 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25496 Set debugging of the dwarf line reader."), _("\
25497 Show debugging of the dwarf line reader."), _("\
25498 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25499 A value of 1 (one) provides basic information.\n\
25500 A value greater than 1 provides more verbose information."),
25501 NULL,
25502 NULL,
25503 &setdebuglist, &showdebuglist);
25504
25505 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25506 Set cross-checking of \"physname\" code against demangler."), _("\
25507 Show cross-checking of \"physname\" code against demangler."), _("\
25508 When enabled, GDB's internal \"physname\" code is checked against\n\
25509 the demangler."),
25510 NULL, show_check_physname,
25511 &setdebuglist, &showdebuglist);
25512
25513 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25514 no_class, &use_deprecated_index_sections, _("\
25515 Set whether to use deprecated gdb_index sections."), _("\
25516 Show whether to use deprecated gdb_index sections."), _("\
25517 When enabled, deprecated .gdb_index sections are used anyway.\n\
25518 Normally they are ignored either because of a missing feature or\n\
25519 performance issue.\n\
25520 Warning: This option must be enabled before gdb reads the file."),
25521 NULL,
25522 NULL,
25523 &setlist, &showlist);
25524
25525 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25526 &dwarf2_locexpr_funcs);
25527 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25528 &dwarf2_loclist_funcs);
25529
25530 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25531 &dwarf2_block_frame_base_locexpr_funcs);
25532 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25533 &dwarf2_block_frame_base_loclist_funcs);
25534
25535 #if GDB_SELF_TEST
25536 selftests::register_test ("dw2_expand_symtabs_matching",
25537 selftests::dw2_expand_symtabs_matching::run_test);
25538 selftests::register_test ("dwarf2_find_containing_comp_unit",
25539 selftests::find_containing_comp_unit::run_test);
25540 #endif
25541 }