Switch objfile to hold a list of psymbol readers
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2021 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit.h"
36 #include "dwarf2/index-cache.h"
37 #include "dwarf2/index-common.h"
38 #include "dwarf2/leb.h"
39 #include "dwarf2/line-header.h"
40 #include "dwarf2/dwz.h"
41 #include "dwarf2/macro.h"
42 #include "dwarf2/die.h"
43 #include "dwarf2/sect-names.h"
44 #include "dwarf2/stringify.h"
45 #include "dwarf2/public.h"
46 #include "bfd.h"
47 #include "elf-bfd.h"
48 #include "symtab.h"
49 #include "gdbtypes.h"
50 #include "objfiles.h"
51 #include "dwarf2.h"
52 #include "buildsym.h"
53 #include "demangle.h"
54 #include "gdb-demangle.h"
55 #include "filenames.h" /* for DOSish file names */
56 #include "language.h"
57 #include "complaints.h"
58 #include "dwarf2/expr.h"
59 #include "dwarf2/loc.h"
60 #include "cp-support.h"
61 #include "hashtab.h"
62 #include "command.h"
63 #include "gdbcmd.h"
64 #include "block.h"
65 #include "addrmap.h"
66 #include "typeprint.h"
67 #include "psympriv.h"
68 #include "c-lang.h"
69 #include "go-lang.h"
70 #include "valprint.h"
71 #include "gdbcore.h" /* for gnutarget */
72 #include "gdb/gdb-index.h"
73 #include "gdb_bfd.h"
74 #include "f-lang.h"
75 #include "source.h"
76 #include "build-id.h"
77 #include "namespace.h"
78 #include "gdbsupport/function-view.h"
79 #include "gdbsupport/gdb_optional.h"
80 #include "gdbsupport/underlying.h"
81 #include "gdbsupport/hash_enum.h"
82 #include "filename-seen-cache.h"
83 #include "producer.h"
84 #include <fcntl.h>
85 #include <algorithm>
86 #include <unordered_map>
87 #include "gdbsupport/selftest.h"
88 #include "rust-lang.h"
89 #include "gdbsupport/pathstuff.h"
90 #include "count-one-bits.h"
91
92 /* When == 1, print basic high level tracing messages.
93 When > 1, be more verbose.
94 This is in contrast to the low level DIE reading of dwarf_die_debug. */
95 static unsigned int dwarf_read_debug = 0;
96
97 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
98
99 #define dwarf_read_debug_printf(fmt, ...) \
100 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
101 ##__VA_ARGS__)
102
103 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
104
105 #define dwarf_read_debug_printf_v(fmt, ...) \
106 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
107 ##__VA_ARGS__)
108
109 /* When non-zero, dump DIEs after they are read in. */
110 static unsigned int dwarf_die_debug = 0;
111
112 /* When non-zero, dump line number entries as they are read in. */
113 unsigned int dwarf_line_debug = 0;
114
115 /* When true, cross-check physname against demangler. */
116 static bool check_physname = false;
117
118 /* When true, do not reject deprecated .gdb_index sections. */
119 static bool use_deprecated_index_sections = false;
120
121 /* This is used to store the data that is always per objfile. */
122 static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
123
124 /* These are used to store the dwarf2_per_bfd objects.
125
126 objfiles having the same BFD, which doesn't require relocations, are going to
127 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
128
129 Other objfiles are not going to share a dwarf2_per_bfd with any other
130 objfiles, so they'll have their own version kept in the _objfile_data_key
131 version. */
132 static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
133 static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
134
135 /* The "aclass" indices for various kinds of computed DWARF symbols. */
136
137 static int dwarf2_locexpr_index;
138 static int dwarf2_loclist_index;
139 static int dwarf2_locexpr_block_index;
140 static int dwarf2_loclist_block_index;
141
142 /* Size of .debug_loclists section header for 32-bit DWARF format. */
143 #define LOCLIST_HEADER_SIZE32 12
144
145 /* Size of .debug_loclists section header for 64-bit DWARF format. */
146 #define LOCLIST_HEADER_SIZE64 20
147
148 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
149 #define RNGLIST_HEADER_SIZE32 12
150
151 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
152 #define RNGLIST_HEADER_SIZE64 20
153
154 /* An index into a (C++) symbol name component in a symbol name as
155 recorded in the mapped_index's symbol table. For each C++ symbol
156 in the symbol table, we record one entry for the start of each
157 component in the symbol in a table of name components, and then
158 sort the table, in order to be able to binary search symbol names,
159 ignoring leading namespaces, both completion and regular look up.
160 For example, for symbol "A::B::C", we'll have an entry that points
161 to "A::B::C", another that points to "B::C", and another for "C".
162 Note that function symbols in GDB index have no parameter
163 information, just the function/method names. You can convert a
164 name_component to a "const char *" using the
165 'mapped_index::symbol_name_at(offset_type)' method. */
166
167 struct name_component
168 {
169 /* Offset in the symbol name where the component starts. Stored as
170 a (32-bit) offset instead of a pointer to save memory and improve
171 locality on 64-bit architectures. */
172 offset_type name_offset;
173
174 /* The symbol's index in the symbol and constant pool tables of a
175 mapped_index. */
176 offset_type idx;
177 };
178
179 /* Base class containing bits shared by both .gdb_index and
180 .debug_name indexes. */
181
182 struct mapped_index_base
183 {
184 mapped_index_base () = default;
185 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
186
187 /* The name_component table (a sorted vector). See name_component's
188 description above. */
189 std::vector<name_component> name_components;
190
191 /* How NAME_COMPONENTS is sorted. */
192 enum case_sensitivity name_components_casing;
193
194 /* Return the number of names in the symbol table. */
195 virtual size_t symbol_name_count () const = 0;
196
197 /* Get the name of the symbol at IDX in the symbol table. */
198 virtual const char *symbol_name_at
199 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
200
201 /* Return whether the name at IDX in the symbol table should be
202 ignored. */
203 virtual bool symbol_name_slot_invalid (offset_type idx) const
204 {
205 return false;
206 }
207
208 /* Build the symbol name component sorted vector, if we haven't
209 yet. */
210 void build_name_components (dwarf2_per_objfile *per_objfile);
211
212 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
213 possible matches for LN_NO_PARAMS in the name component
214 vector. */
215 std::pair<std::vector<name_component>::const_iterator,
216 std::vector<name_component>::const_iterator>
217 find_name_components_bounds (const lookup_name_info &ln_no_params,
218 enum language lang,
219 dwarf2_per_objfile *per_objfile) const;
220
221 /* Prevent deleting/destroying via a base class pointer. */
222 protected:
223 ~mapped_index_base() = default;
224 };
225
226 /* A description of the mapped index. The file format is described in
227 a comment by the code that writes the index. */
228 struct mapped_index final : public mapped_index_base
229 {
230 /* A slot/bucket in the symbol table hash. */
231 struct symbol_table_slot
232 {
233 const offset_type name;
234 const offset_type vec;
235 };
236
237 /* Index data format version. */
238 int version = 0;
239
240 /* The address table data. */
241 gdb::array_view<const gdb_byte> address_table;
242
243 /* The symbol table, implemented as a hash table. */
244 gdb::array_view<symbol_table_slot> symbol_table;
245
246 /* A pointer to the constant pool. */
247 const char *constant_pool = nullptr;
248
249 bool symbol_name_slot_invalid (offset_type idx) const override
250 {
251 const auto &bucket = this->symbol_table[idx];
252 return bucket.name == 0 && bucket.vec == 0;
253 }
254
255 /* Convenience method to get at the name of the symbol at IDX in the
256 symbol table. */
257 const char *symbol_name_at
258 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
259 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
260
261 size_t symbol_name_count () const override
262 { return this->symbol_table.size (); }
263 };
264
265 /* A description of the mapped .debug_names.
266 Uninitialized map has CU_COUNT 0. */
267 struct mapped_debug_names final : public mapped_index_base
268 {
269 bfd_endian dwarf5_byte_order;
270 bool dwarf5_is_dwarf64;
271 bool augmentation_is_gdb;
272 uint8_t offset_size;
273 uint32_t cu_count = 0;
274 uint32_t tu_count, bucket_count, name_count;
275 const gdb_byte *cu_table_reordered, *tu_table_reordered;
276 const uint32_t *bucket_table_reordered, *hash_table_reordered;
277 const gdb_byte *name_table_string_offs_reordered;
278 const gdb_byte *name_table_entry_offs_reordered;
279 const gdb_byte *entry_pool;
280
281 struct index_val
282 {
283 ULONGEST dwarf_tag;
284 struct attr
285 {
286 /* Attribute name DW_IDX_*. */
287 ULONGEST dw_idx;
288
289 /* Attribute form DW_FORM_*. */
290 ULONGEST form;
291
292 /* Value if FORM is DW_FORM_implicit_const. */
293 LONGEST implicit_const;
294 };
295 std::vector<attr> attr_vec;
296 };
297
298 std::unordered_map<ULONGEST, index_val> abbrev_map;
299
300 const char *namei_to_name
301 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
302
303 /* Implementation of the mapped_index_base virtual interface, for
304 the name_components cache. */
305
306 const char *symbol_name_at
307 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
308 { return namei_to_name (idx, per_objfile); }
309
310 size_t symbol_name_count () const override
311 { return this->name_count; }
312 };
313
314 /* See dwarf2read.h. */
315
316 dwarf2_per_objfile *
317 get_dwarf2_per_objfile (struct objfile *objfile)
318 {
319 return dwarf2_objfile_data_key.get (objfile);
320 }
321
322 /* Default names of the debugging sections. */
323
324 /* Note that if the debugging section has been compressed, it might
325 have a name like .zdebug_info. */
326
327 const struct dwarf2_debug_sections dwarf2_elf_names =
328 {
329 { ".debug_info", ".zdebug_info" },
330 { ".debug_abbrev", ".zdebug_abbrev" },
331 { ".debug_line", ".zdebug_line" },
332 { ".debug_loc", ".zdebug_loc" },
333 { ".debug_loclists", ".zdebug_loclists" },
334 { ".debug_macinfo", ".zdebug_macinfo" },
335 { ".debug_macro", ".zdebug_macro" },
336 { ".debug_str", ".zdebug_str" },
337 { ".debug_str_offsets", ".zdebug_str_offsets" },
338 { ".debug_line_str", ".zdebug_line_str" },
339 { ".debug_ranges", ".zdebug_ranges" },
340 { ".debug_rnglists", ".zdebug_rnglists" },
341 { ".debug_types", ".zdebug_types" },
342 { ".debug_addr", ".zdebug_addr" },
343 { ".debug_frame", ".zdebug_frame" },
344 { ".eh_frame", NULL },
345 { ".gdb_index", ".zgdb_index" },
346 { ".debug_names", ".zdebug_names" },
347 { ".debug_aranges", ".zdebug_aranges" },
348 23
349 };
350
351 /* List of DWO/DWP sections. */
352
353 static const struct dwop_section_names
354 {
355 struct dwarf2_section_names abbrev_dwo;
356 struct dwarf2_section_names info_dwo;
357 struct dwarf2_section_names line_dwo;
358 struct dwarf2_section_names loc_dwo;
359 struct dwarf2_section_names loclists_dwo;
360 struct dwarf2_section_names macinfo_dwo;
361 struct dwarf2_section_names macro_dwo;
362 struct dwarf2_section_names rnglists_dwo;
363 struct dwarf2_section_names str_dwo;
364 struct dwarf2_section_names str_offsets_dwo;
365 struct dwarf2_section_names types_dwo;
366 struct dwarf2_section_names cu_index;
367 struct dwarf2_section_names tu_index;
368 }
369 dwop_section_names =
370 {
371 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
372 { ".debug_info.dwo", ".zdebug_info.dwo" },
373 { ".debug_line.dwo", ".zdebug_line.dwo" },
374 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
375 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
376 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
377 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
378 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
379 { ".debug_str.dwo", ".zdebug_str.dwo" },
380 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
381 { ".debug_types.dwo", ".zdebug_types.dwo" },
382 { ".debug_cu_index", ".zdebug_cu_index" },
383 { ".debug_tu_index", ".zdebug_tu_index" },
384 };
385
386 /* local data types */
387
388 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
389 begin with a header, which contains the following information. */
390 struct loclists_rnglists_header
391 {
392 /* A 4-byte or 12-byte length containing the length of the
393 set of entries for this compilation unit, not including the
394 length field itself. */
395 unsigned int length;
396
397 /* A 2-byte version identifier. */
398 short version;
399
400 /* A 1-byte unsigned integer containing the size in bytes of an address on
401 the target system. */
402 unsigned char addr_size;
403
404 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
405 on the target system. */
406 unsigned char segment_collector_size;
407
408 /* A 4-byte count of the number of offsets that follow the header. */
409 unsigned int offset_entry_count;
410 };
411
412 /* Type used for delaying computation of method physnames.
413 See comments for compute_delayed_physnames. */
414 struct delayed_method_info
415 {
416 /* The type to which the method is attached, i.e., its parent class. */
417 struct type *type;
418
419 /* The index of the method in the type's function fieldlists. */
420 int fnfield_index;
421
422 /* The index of the method in the fieldlist. */
423 int index;
424
425 /* The name of the DIE. */
426 const char *name;
427
428 /* The DIE associated with this method. */
429 struct die_info *die;
430 };
431
432 /* Internal state when decoding a particular compilation unit. */
433 struct dwarf2_cu
434 {
435 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
436 dwarf2_per_objfile *per_objfile);
437
438 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
439
440 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
441 Create the set of symtabs used by this TU, or if this TU is sharing
442 symtabs with another TU and the symtabs have already been created
443 then restore those symtabs in the line header.
444 We don't need the pc/line-number mapping for type units. */
445 void setup_type_unit_groups (struct die_info *die);
446
447 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
448 buildsym_compunit constructor. */
449 struct compunit_symtab *start_symtab (const char *name,
450 const char *comp_dir,
451 CORE_ADDR low_pc);
452
453 /* Reset the builder. */
454 void reset_builder () { m_builder.reset (); }
455
456 /* Return a type that is a generic pointer type, the size of which
457 matches the address size given in the compilation unit header for
458 this CU. */
459 struct type *addr_type () const;
460
461 /* Find an integer type the same size as the address size given in
462 the compilation unit header for this CU. UNSIGNED_P controls if
463 the integer is unsigned or not. */
464 struct type *addr_sized_int_type (bool unsigned_p) const;
465
466 /* The header of the compilation unit. */
467 struct comp_unit_head header {};
468
469 /* Base address of this compilation unit. */
470 gdb::optional<CORE_ADDR> base_address;
471
472 /* The language we are debugging. */
473 enum language language = language_unknown;
474 const struct language_defn *language_defn = nullptr;
475
476 const char *producer = nullptr;
477
478 private:
479 /* The symtab builder for this CU. This is only non-NULL when full
480 symbols are being read. */
481 std::unique_ptr<buildsym_compunit> m_builder;
482
483 public:
484 /* The generic symbol table building routines have separate lists for
485 file scope symbols and all all other scopes (local scopes). So
486 we need to select the right one to pass to add_symbol_to_list().
487 We do it by keeping a pointer to the correct list in list_in_scope.
488
489 FIXME: The original dwarf code just treated the file scope as the
490 first local scope, and all other local scopes as nested local
491 scopes, and worked fine. Check to see if we really need to
492 distinguish these in buildsym.c. */
493 struct pending **list_in_scope = nullptr;
494
495 /* Hash table holding all the loaded partial DIEs
496 with partial_die->offset.SECT_OFF as hash. */
497 htab_t partial_dies = nullptr;
498
499 /* Storage for things with the same lifetime as this read-in compilation
500 unit, including partial DIEs. */
501 auto_obstack comp_unit_obstack;
502
503 /* Backlink to our per_cu entry. */
504 struct dwarf2_per_cu_data *per_cu;
505
506 /* The dwarf2_per_objfile that owns this. */
507 dwarf2_per_objfile *per_objfile;
508
509 /* How many compilation units ago was this CU last referenced? */
510 int last_used = 0;
511
512 /* A hash table of DIE cu_offset for following references with
513 die_info->offset.sect_off as hash. */
514 htab_t die_hash = nullptr;
515
516 /* Full DIEs if read in. */
517 struct die_info *dies = nullptr;
518
519 /* A set of pointers to dwarf2_per_cu_data objects for compilation
520 units referenced by this one. Only set during full symbol processing;
521 partial symbol tables do not have dependencies. */
522 htab_t dependencies = nullptr;
523
524 /* Header data from the line table, during full symbol processing. */
525 struct line_header *line_header = nullptr;
526 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
527 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
528 this is the DW_TAG_compile_unit die for this CU. We'll hold on
529 to the line header as long as this DIE is being processed. See
530 process_die_scope. */
531 die_info *line_header_die_owner = nullptr;
532
533 /* A list of methods which need to have physnames computed
534 after all type information has been read. */
535 std::vector<delayed_method_info> method_list;
536
537 /* To be copied to symtab->call_site_htab. */
538 htab_t call_site_htab = nullptr;
539
540 /* Non-NULL if this CU came from a DWO file.
541 There is an invariant here that is important to remember:
542 Except for attributes copied from the top level DIE in the "main"
543 (or "stub") file in preparation for reading the DWO file
544 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
545 Either there isn't a DWO file (in which case this is NULL and the point
546 is moot), or there is and either we're not going to read it (in which
547 case this is NULL) or there is and we are reading it (in which case this
548 is non-NULL). */
549 struct dwo_unit *dwo_unit = nullptr;
550
551 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
552 Note this value comes from the Fission stub CU/TU's DIE. */
553 gdb::optional<ULONGEST> addr_base;
554
555 /* The DW_AT_GNU_ranges_base attribute, if present.
556
557 This is only relevant in the context of pre-DWARF 5 split units. In this
558 context, there is a .debug_ranges section in the linked executable,
559 containing all the ranges data for all the compilation units. Each
560 skeleton/stub unit has (if needed) a DW_AT_GNU_ranges_base attribute that
561 indicates the base of its contribution to that section. The DW_AT_ranges
562 attributes in the split-unit are of the form DW_FORM_sec_offset and point
563 into the .debug_ranges section of the linked file. However, they are not
564 "true" DW_FORM_sec_offset, because they are relative to the base of their
565 compilation unit's contribution, rather than relative to the beginning of
566 the section. The DW_AT_GNU_ranges_base value must be added to it to make
567 it relative to the beginning of the section.
568
569 Note that the value is zero when we are not in a pre-DWARF 5 split-unit
570 case, so this value can be added without needing to know whether we are in
571 this case or not.
572
573 N.B. If a DW_AT_ranges attribute is found on the DW_TAG_compile_unit in the
574 skeleton/stub, it must not have the base added, as it already points to the
575 right place. And since the DW_TAG_compile_unit DIE in the split-unit can't
576 have a DW_AT_ranges attribute, we can use the
577
578 die->tag != DW_AT_compile_unit
579
580 to determine whether the base should be added or not. */
581 ULONGEST gnu_ranges_base = 0;
582
583 /* The DW_AT_rnglists_base attribute, if present.
584
585 This is used when processing attributes of form DW_FORM_rnglistx in
586 non-split units. Attributes of this form found in a split unit don't
587 use it, as split-unit files have their own non-shared .debug_rnglists.dwo
588 section. */
589 ULONGEST rnglists_base = 0;
590
591 /* The DW_AT_loclists_base attribute if present. */
592 ULONGEST loclist_base = 0;
593
594 /* When reading debug info generated by older versions of rustc, we
595 have to rewrite some union types to be struct types with a
596 variant part. This rewriting must be done after the CU is fully
597 read in, because otherwise at the point of rewriting some struct
598 type might not have been fully processed. So, we keep a list of
599 all such types here and process them after expansion. */
600 std::vector<struct type *> rust_unions;
601
602 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
603 files, the value is implicitly zero. For DWARF 5 version DWO files, the
604 value is often implicit and is the size of the header of
605 .debug_str_offsets section (8 or 4, depending on the address size). */
606 gdb::optional<ULONGEST> str_offsets_base;
607
608 /* Mark used when releasing cached dies. */
609 bool mark : 1;
610
611 /* This CU references .debug_loc. See the symtab->locations_valid field.
612 This test is imperfect as there may exist optimized debug code not using
613 any location list and still facing inlining issues if handled as
614 unoptimized code. For a future better test see GCC PR other/32998. */
615 bool has_loclist : 1;
616
617 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
618 if all the producer_is_* fields are valid. This information is cached
619 because profiling CU expansion showed excessive time spent in
620 producer_is_gxx_lt_4_6. */
621 bool checked_producer : 1;
622 bool producer_is_gxx_lt_4_6 : 1;
623 bool producer_is_gcc_lt_4_3 : 1;
624 bool producer_is_icc : 1;
625 bool producer_is_icc_lt_14 : 1;
626 bool producer_is_codewarrior : 1;
627
628 /* When true, the file that we're processing is known to have
629 debugging info for C++ namespaces. GCC 3.3.x did not produce
630 this information, but later versions do. */
631
632 bool processing_has_namespace_info : 1;
633
634 struct partial_die_info *find_partial_die (sect_offset sect_off);
635
636 /* If this CU was inherited by another CU (via specification,
637 abstract_origin, etc), this is the ancestor CU. */
638 dwarf2_cu *ancestor;
639
640 /* Get the buildsym_compunit for this CU. */
641 buildsym_compunit *get_builder ()
642 {
643 /* If this CU has a builder associated with it, use that. */
644 if (m_builder != nullptr)
645 return m_builder.get ();
646
647 /* Otherwise, search ancestors for a valid builder. */
648 if (ancestor != nullptr)
649 return ancestor->get_builder ();
650
651 return nullptr;
652 }
653 };
654
655 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
656 This includes type_unit_group and quick_file_names. */
657
658 struct stmt_list_hash
659 {
660 /* The DWO unit this table is from or NULL if there is none. */
661 struct dwo_unit *dwo_unit;
662
663 /* Offset in .debug_line or .debug_line.dwo. */
664 sect_offset line_sect_off;
665 };
666
667 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
668 an object of this type. This contains elements of type unit groups
669 that can be shared across objfiles. The non-shareable parts are in
670 type_unit_group_unshareable. */
671
672 struct type_unit_group
673 {
674 /* dwarf2read.c's main "handle" on a TU symtab.
675 To simplify things we create an artificial CU that "includes" all the
676 type units using this stmt_list so that the rest of the code still has
677 a "per_cu" handle on the symtab. */
678 struct dwarf2_per_cu_data per_cu;
679
680 /* The TUs that share this DW_AT_stmt_list entry.
681 This is added to while parsing type units to build partial symtabs,
682 and is deleted afterwards and not used again. */
683 std::vector<signatured_type *> *tus;
684
685 /* The data used to construct the hash key. */
686 struct stmt_list_hash hash;
687 };
688
689 /* These sections are what may appear in a (real or virtual) DWO file. */
690
691 struct dwo_sections
692 {
693 struct dwarf2_section_info abbrev;
694 struct dwarf2_section_info line;
695 struct dwarf2_section_info loc;
696 struct dwarf2_section_info loclists;
697 struct dwarf2_section_info macinfo;
698 struct dwarf2_section_info macro;
699 struct dwarf2_section_info rnglists;
700 struct dwarf2_section_info str;
701 struct dwarf2_section_info str_offsets;
702 /* In the case of a virtual DWO file, these two are unused. */
703 struct dwarf2_section_info info;
704 std::vector<dwarf2_section_info> types;
705 };
706
707 /* CUs/TUs in DWP/DWO files. */
708
709 struct dwo_unit
710 {
711 /* Backlink to the containing struct dwo_file. */
712 struct dwo_file *dwo_file;
713
714 /* The "id" that distinguishes this CU/TU.
715 .debug_info calls this "dwo_id", .debug_types calls this "signature".
716 Since signatures came first, we stick with it for consistency. */
717 ULONGEST signature;
718
719 /* The section this CU/TU lives in, in the DWO file. */
720 struct dwarf2_section_info *section;
721
722 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
723 sect_offset sect_off;
724 unsigned int length;
725
726 /* For types, offset in the type's DIE of the type defined by this TU. */
727 cu_offset type_offset_in_tu;
728 };
729
730 /* include/dwarf2.h defines the DWP section codes.
731 It defines a max value but it doesn't define a min value, which we
732 use for error checking, so provide one. */
733
734 enum dwp_v2_section_ids
735 {
736 DW_SECT_MIN = 1
737 };
738
739 /* Data for one DWO file.
740
741 This includes virtual DWO files (a virtual DWO file is a DWO file as it
742 appears in a DWP file). DWP files don't really have DWO files per se -
743 comdat folding of types "loses" the DWO file they came from, and from
744 a high level view DWP files appear to contain a mass of random types.
745 However, to maintain consistency with the non-DWP case we pretend DWP
746 files contain virtual DWO files, and we assign each TU with one virtual
747 DWO file (generally based on the line and abbrev section offsets -
748 a heuristic that seems to work in practice). */
749
750 struct dwo_file
751 {
752 dwo_file () = default;
753 DISABLE_COPY_AND_ASSIGN (dwo_file);
754
755 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
756 For virtual DWO files the name is constructed from the section offsets
757 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
758 from related CU+TUs. */
759 const char *dwo_name = nullptr;
760
761 /* The DW_AT_comp_dir attribute. */
762 const char *comp_dir = nullptr;
763
764 /* The bfd, when the file is open. Otherwise this is NULL.
765 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
766 gdb_bfd_ref_ptr dbfd;
767
768 /* The sections that make up this DWO file.
769 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
770 sections (for lack of a better name). */
771 struct dwo_sections sections {};
772
773 /* The CUs in the file.
774 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
775 an extension to handle LLVM's Link Time Optimization output (where
776 multiple source files may be compiled into a single object/dwo pair). */
777 htab_up cus;
778
779 /* Table of TUs in the file.
780 Each element is a struct dwo_unit. */
781 htab_up tus;
782 };
783
784 /* These sections are what may appear in a DWP file. */
785
786 struct dwp_sections
787 {
788 /* These are used by all DWP versions (1, 2 and 5). */
789 struct dwarf2_section_info str;
790 struct dwarf2_section_info cu_index;
791 struct dwarf2_section_info tu_index;
792
793 /* These are only used by DWP version 2 and version 5 files.
794 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
795 sections are referenced by section number, and are not recorded here.
796 In DWP version 2 or 5 there is at most one copy of all these sections,
797 each section being (effectively) comprised of the concatenation of all of
798 the individual sections that exist in the version 1 format.
799 To keep the code simple we treat each of these concatenated pieces as a
800 section itself (a virtual section?). */
801 struct dwarf2_section_info abbrev;
802 struct dwarf2_section_info info;
803 struct dwarf2_section_info line;
804 struct dwarf2_section_info loc;
805 struct dwarf2_section_info loclists;
806 struct dwarf2_section_info macinfo;
807 struct dwarf2_section_info macro;
808 struct dwarf2_section_info rnglists;
809 struct dwarf2_section_info str_offsets;
810 struct dwarf2_section_info types;
811 };
812
813 /* These sections are what may appear in a virtual DWO file in DWP version 1.
814 A virtual DWO file is a DWO file as it appears in a DWP file. */
815
816 struct virtual_v1_dwo_sections
817 {
818 struct dwarf2_section_info abbrev;
819 struct dwarf2_section_info line;
820 struct dwarf2_section_info loc;
821 struct dwarf2_section_info macinfo;
822 struct dwarf2_section_info macro;
823 struct dwarf2_section_info str_offsets;
824 /* Each DWP hash table entry records one CU or one TU.
825 That is recorded here, and copied to dwo_unit.section. */
826 struct dwarf2_section_info info_or_types;
827 };
828
829 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
830 In version 2, the sections of the DWO files are concatenated together
831 and stored in one section of that name. Thus each ELF section contains
832 several "virtual" sections. */
833
834 struct virtual_v2_or_v5_dwo_sections
835 {
836 bfd_size_type abbrev_offset;
837 bfd_size_type abbrev_size;
838
839 bfd_size_type line_offset;
840 bfd_size_type line_size;
841
842 bfd_size_type loc_offset;
843 bfd_size_type loc_size;
844
845 bfd_size_type loclists_offset;
846 bfd_size_type loclists_size;
847
848 bfd_size_type macinfo_offset;
849 bfd_size_type macinfo_size;
850
851 bfd_size_type macro_offset;
852 bfd_size_type macro_size;
853
854 bfd_size_type rnglists_offset;
855 bfd_size_type rnglists_size;
856
857 bfd_size_type str_offsets_offset;
858 bfd_size_type str_offsets_size;
859
860 /* Each DWP hash table entry records one CU or one TU.
861 That is recorded here, and copied to dwo_unit.section. */
862 bfd_size_type info_or_types_offset;
863 bfd_size_type info_or_types_size;
864 };
865
866 /* Contents of DWP hash tables. */
867
868 struct dwp_hash_table
869 {
870 uint32_t version, nr_columns;
871 uint32_t nr_units, nr_slots;
872 const gdb_byte *hash_table, *unit_table;
873 union
874 {
875 struct
876 {
877 const gdb_byte *indices;
878 } v1;
879 struct
880 {
881 /* This is indexed by column number and gives the id of the section
882 in that column. */
883 #define MAX_NR_V2_DWO_SECTIONS \
884 (1 /* .debug_info or .debug_types */ \
885 + 1 /* .debug_abbrev */ \
886 + 1 /* .debug_line */ \
887 + 1 /* .debug_loc */ \
888 + 1 /* .debug_str_offsets */ \
889 + 1 /* .debug_macro or .debug_macinfo */)
890 int section_ids[MAX_NR_V2_DWO_SECTIONS];
891 const gdb_byte *offsets;
892 const gdb_byte *sizes;
893 } v2;
894 struct
895 {
896 /* This is indexed by column number and gives the id of the section
897 in that column. */
898 #define MAX_NR_V5_DWO_SECTIONS \
899 (1 /* .debug_info */ \
900 + 1 /* .debug_abbrev */ \
901 + 1 /* .debug_line */ \
902 + 1 /* .debug_loclists */ \
903 + 1 /* .debug_str_offsets */ \
904 + 1 /* .debug_macro */ \
905 + 1 /* .debug_rnglists */)
906 int section_ids[MAX_NR_V5_DWO_SECTIONS];
907 const gdb_byte *offsets;
908 const gdb_byte *sizes;
909 } v5;
910 } section_pool;
911 };
912
913 /* Data for one DWP file. */
914
915 struct dwp_file
916 {
917 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
918 : name (name_),
919 dbfd (std::move (abfd))
920 {
921 }
922
923 /* Name of the file. */
924 const char *name;
925
926 /* File format version. */
927 int version = 0;
928
929 /* The bfd. */
930 gdb_bfd_ref_ptr dbfd;
931
932 /* Section info for this file. */
933 struct dwp_sections sections {};
934
935 /* Table of CUs in the file. */
936 const struct dwp_hash_table *cus = nullptr;
937
938 /* Table of TUs in the file. */
939 const struct dwp_hash_table *tus = nullptr;
940
941 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
942 htab_up loaded_cus;
943 htab_up loaded_tus;
944
945 /* Table to map ELF section numbers to their sections.
946 This is only needed for the DWP V1 file format. */
947 unsigned int num_sections = 0;
948 asection **elf_sections = nullptr;
949 };
950
951 /* Struct used to pass misc. parameters to read_die_and_children, et
952 al. which are used for both .debug_info and .debug_types dies.
953 All parameters here are unchanging for the life of the call. This
954 struct exists to abstract away the constant parameters of die reading. */
955
956 struct die_reader_specs
957 {
958 /* The bfd of die_section. */
959 bfd *abfd;
960
961 /* The CU of the DIE we are parsing. */
962 struct dwarf2_cu *cu;
963
964 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
965 struct dwo_file *dwo_file;
966
967 /* The section the die comes from.
968 This is either .debug_info or .debug_types, or the .dwo variants. */
969 struct dwarf2_section_info *die_section;
970
971 /* die_section->buffer. */
972 const gdb_byte *buffer;
973
974 /* The end of the buffer. */
975 const gdb_byte *buffer_end;
976
977 /* The abbreviation table to use when reading the DIEs. */
978 struct abbrev_table *abbrev_table;
979 };
980
981 /* A subclass of die_reader_specs that holds storage and has complex
982 constructor and destructor behavior. */
983
984 class cutu_reader : public die_reader_specs
985 {
986 public:
987
988 cutu_reader (dwarf2_per_cu_data *this_cu,
989 dwarf2_per_objfile *per_objfile,
990 struct abbrev_table *abbrev_table,
991 dwarf2_cu *existing_cu,
992 bool skip_partial);
993
994 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
995 dwarf2_per_objfile *per_objfile,
996 struct dwarf2_cu *parent_cu = nullptr,
997 struct dwo_file *dwo_file = nullptr);
998
999 DISABLE_COPY_AND_ASSIGN (cutu_reader);
1000
1001 const gdb_byte *info_ptr = nullptr;
1002 struct die_info *comp_unit_die = nullptr;
1003 bool dummy_p = false;
1004
1005 /* Release the new CU, putting it on the chain. This cannot be done
1006 for dummy CUs. */
1007 void keep ();
1008
1009 private:
1010 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
1011 dwarf2_per_objfile *per_objfile,
1012 dwarf2_cu *existing_cu);
1013
1014 struct dwarf2_per_cu_data *m_this_cu;
1015 std::unique_ptr<dwarf2_cu> m_new_cu;
1016
1017 /* The ordinary abbreviation table. */
1018 abbrev_table_up m_abbrev_table_holder;
1019
1020 /* The DWO abbreviation table. */
1021 abbrev_table_up m_dwo_abbrev_table;
1022 };
1023
1024 /* When we construct a partial symbol table entry we only
1025 need this much information. */
1026 struct partial_die_info : public allocate_on_obstack
1027 {
1028 partial_die_info (sect_offset sect_off, const struct abbrev_info *abbrev);
1029
1030 /* Disable assign but still keep copy ctor, which is needed
1031 load_partial_dies. */
1032 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1033
1034 /* Adjust the partial die before generating a symbol for it. This
1035 function may set the is_external flag or change the DIE's
1036 name. */
1037 void fixup (struct dwarf2_cu *cu);
1038
1039 /* Read a minimal amount of information into the minimal die
1040 structure. */
1041 const gdb_byte *read (const struct die_reader_specs *reader,
1042 const struct abbrev_info &abbrev,
1043 const gdb_byte *info_ptr);
1044
1045 /* Compute the name of this partial DIE. This memoizes the
1046 result, so it is safe to call multiple times. */
1047 const char *name (dwarf2_cu *cu);
1048
1049 /* Offset of this DIE. */
1050 const sect_offset sect_off;
1051
1052 /* DWARF-2 tag for this DIE. */
1053 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1054
1055 /* Assorted flags describing the data found in this DIE. */
1056 const unsigned int has_children : 1;
1057
1058 unsigned int is_external : 1;
1059 unsigned int is_declaration : 1;
1060 unsigned int has_type : 1;
1061 unsigned int has_specification : 1;
1062 unsigned int has_pc_info : 1;
1063 unsigned int may_be_inlined : 1;
1064
1065 /* This DIE has been marked DW_AT_main_subprogram. */
1066 unsigned int main_subprogram : 1;
1067
1068 /* Flag set if the SCOPE field of this structure has been
1069 computed. */
1070 unsigned int scope_set : 1;
1071
1072 /* Flag set if the DIE has a byte_size attribute. */
1073 unsigned int has_byte_size : 1;
1074
1075 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1076 unsigned int has_const_value : 1;
1077
1078 /* Flag set if any of the DIE's children are template arguments. */
1079 unsigned int has_template_arguments : 1;
1080
1081 /* Flag set if fixup has been called on this die. */
1082 unsigned int fixup_called : 1;
1083
1084 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1085 unsigned int is_dwz : 1;
1086
1087 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1088 unsigned int spec_is_dwz : 1;
1089
1090 unsigned int canonical_name : 1;
1091
1092 /* The name of this DIE. Normally the value of DW_AT_name, but
1093 sometimes a default name for unnamed DIEs. */
1094 const char *raw_name = nullptr;
1095
1096 /* The linkage name, if present. */
1097 const char *linkage_name = nullptr;
1098
1099 /* The scope to prepend to our children. This is generally
1100 allocated on the comp_unit_obstack, so will disappear
1101 when this compilation unit leaves the cache. */
1102 const char *scope = nullptr;
1103
1104 /* Some data associated with the partial DIE. The tag determines
1105 which field is live. */
1106 union
1107 {
1108 /* The location description associated with this DIE, if any. */
1109 struct dwarf_block *locdesc;
1110 /* The offset of an import, for DW_TAG_imported_unit. */
1111 sect_offset sect_off;
1112 } d {};
1113
1114 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1115 CORE_ADDR lowpc = 0;
1116 CORE_ADDR highpc = 0;
1117
1118 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1119 DW_AT_sibling, if any. */
1120 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1121 could return DW_AT_sibling values to its caller load_partial_dies. */
1122 const gdb_byte *sibling = nullptr;
1123
1124 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1125 DW_AT_specification (or DW_AT_abstract_origin or
1126 DW_AT_extension). */
1127 sect_offset spec_offset {};
1128
1129 /* Pointers to this DIE's parent, first child, and next sibling,
1130 if any. */
1131 struct partial_die_info *die_parent = nullptr;
1132 struct partial_die_info *die_child = nullptr;
1133 struct partial_die_info *die_sibling = nullptr;
1134
1135 friend struct partial_die_info *
1136 dwarf2_cu::find_partial_die (sect_offset sect_off);
1137
1138 private:
1139 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1140 partial_die_info (sect_offset sect_off)
1141 : partial_die_info (sect_off, DW_TAG_padding, 0)
1142 {
1143 }
1144
1145 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1146 int has_children_)
1147 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1148 {
1149 is_external = 0;
1150 is_declaration = 0;
1151 has_type = 0;
1152 has_specification = 0;
1153 has_pc_info = 0;
1154 may_be_inlined = 0;
1155 main_subprogram = 0;
1156 scope_set = 0;
1157 has_byte_size = 0;
1158 has_const_value = 0;
1159 has_template_arguments = 0;
1160 fixup_called = 0;
1161 is_dwz = 0;
1162 spec_is_dwz = 0;
1163 canonical_name = 0;
1164 }
1165 };
1166
1167 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1168 but this would require a corresponding change in unpack_field_as_long
1169 and friends. */
1170 static int bits_per_byte = 8;
1171
1172 struct variant_part_builder;
1173
1174 /* When reading a variant, we track a bit more information about the
1175 field, and store it in an object of this type. */
1176
1177 struct variant_field
1178 {
1179 int first_field = -1;
1180 int last_field = -1;
1181
1182 /* A variant can contain other variant parts. */
1183 std::vector<variant_part_builder> variant_parts;
1184
1185 /* If we see a DW_TAG_variant, then this will be set if this is the
1186 default branch. */
1187 bool default_branch = false;
1188 /* If we see a DW_AT_discr_value, then this will be the discriminant
1189 value. */
1190 ULONGEST discriminant_value = 0;
1191 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1192 data. */
1193 struct dwarf_block *discr_list_data = nullptr;
1194 };
1195
1196 /* This represents a DW_TAG_variant_part. */
1197
1198 struct variant_part_builder
1199 {
1200 /* The offset of the discriminant field. */
1201 sect_offset discriminant_offset {};
1202
1203 /* Variants that are direct children of this variant part. */
1204 std::vector<variant_field> variants;
1205
1206 /* True if we're currently reading a variant. */
1207 bool processing_variant = false;
1208 };
1209
1210 struct nextfield
1211 {
1212 int accessibility = 0;
1213 int virtuality = 0;
1214 /* Variant parts need to find the discriminant, which is a DIE
1215 reference. We track the section offset of each field to make
1216 this link. */
1217 sect_offset offset;
1218 struct field field {};
1219 };
1220
1221 struct fnfieldlist
1222 {
1223 const char *name = nullptr;
1224 std::vector<struct fn_field> fnfields;
1225 };
1226
1227 /* The routines that read and process dies for a C struct or C++ class
1228 pass lists of data member fields and lists of member function fields
1229 in an instance of a field_info structure, as defined below. */
1230 struct field_info
1231 {
1232 /* List of data member and baseclasses fields. */
1233 std::vector<struct nextfield> fields;
1234 std::vector<struct nextfield> baseclasses;
1235
1236 /* Set if the accessibility of one of the fields is not public. */
1237 bool non_public_fields = false;
1238
1239 /* Member function fieldlist array, contains name of possibly overloaded
1240 member function, number of overloaded member functions and a pointer
1241 to the head of the member function field chain. */
1242 std::vector<struct fnfieldlist> fnfieldlists;
1243
1244 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1245 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1246 std::vector<struct decl_field> typedef_field_list;
1247
1248 /* Nested types defined by this class and the number of elements in this
1249 list. */
1250 std::vector<struct decl_field> nested_types_list;
1251
1252 /* If non-null, this is the variant part we are currently
1253 reading. */
1254 variant_part_builder *current_variant_part = nullptr;
1255 /* This holds all the top-level variant parts attached to the type
1256 we're reading. */
1257 std::vector<variant_part_builder> variant_parts;
1258
1259 /* Return the total number of fields (including baseclasses). */
1260 int nfields () const
1261 {
1262 return fields.size () + baseclasses.size ();
1263 }
1264 };
1265
1266 /* Loaded secondary compilation units are kept in memory until they
1267 have not been referenced for the processing of this many
1268 compilation units. Set this to zero to disable caching. Cache
1269 sizes of up to at least twenty will improve startup time for
1270 typical inter-CU-reference binaries, at an obvious memory cost. */
1271 static int dwarf_max_cache_age = 5;
1272 static void
1273 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1274 struct cmd_list_element *c, const char *value)
1275 {
1276 fprintf_filtered (file, _("The upper bound on the age of cached "
1277 "DWARF compilation units is %s.\n"),
1278 value);
1279 }
1280 \f
1281 /* local function prototypes */
1282
1283 static void dwarf2_find_base_address (struct die_info *die,
1284 struct dwarf2_cu *cu);
1285
1286 static dwarf2_psymtab *create_partial_symtab
1287 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1288 const char *name);
1289
1290 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1291 const gdb_byte *info_ptr,
1292 struct die_info *type_unit_die);
1293
1294 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
1295
1296 static void scan_partial_symbols (struct partial_die_info *,
1297 CORE_ADDR *, CORE_ADDR *,
1298 int, struct dwarf2_cu *);
1299
1300 static void add_partial_symbol (struct partial_die_info *,
1301 struct dwarf2_cu *);
1302
1303 static void add_partial_namespace (struct partial_die_info *pdi,
1304 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1305 int set_addrmap, struct dwarf2_cu *cu);
1306
1307 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1308 CORE_ADDR *highpc, int set_addrmap,
1309 struct dwarf2_cu *cu);
1310
1311 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1312 struct dwarf2_cu *cu);
1313
1314 static void add_partial_subprogram (struct partial_die_info *pdi,
1315 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1316 int need_pc, struct dwarf2_cu *cu);
1317
1318 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1319
1320 static struct partial_die_info *load_partial_dies
1321 (const struct die_reader_specs *, const gdb_byte *, int);
1322
1323 /* A pair of partial_die_info and compilation unit. */
1324 struct cu_partial_die_info
1325 {
1326 /* The compilation unit of the partial_die_info. */
1327 struct dwarf2_cu *cu;
1328 /* A partial_die_info. */
1329 struct partial_die_info *pdi;
1330
1331 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1332 : cu (cu),
1333 pdi (pdi)
1334 { /* Nothing. */ }
1335
1336 private:
1337 cu_partial_die_info () = delete;
1338 };
1339
1340 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1341 struct dwarf2_cu *);
1342
1343 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1344 struct attribute *,
1345 const struct attr_abbrev *,
1346 const gdb_byte *);
1347
1348 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1349 struct attribute *attr, dwarf_tag tag);
1350
1351 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1352
1353 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1354 dwarf2_section_info *, sect_offset);
1355
1356 static const char *read_indirect_string
1357 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
1358 const struct comp_unit_head *, unsigned int *);
1359
1360 static const char *read_indirect_string_at_offset
1361 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
1362
1363 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1364 const gdb_byte *,
1365 unsigned int *);
1366
1367 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1368 ULONGEST str_index);
1369
1370 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1371 ULONGEST str_index);
1372
1373 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1374
1375 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1376 struct dwarf2_cu *);
1377
1378 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1379 struct dwarf2_cu *cu);
1380
1381 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1382
1383 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1384 struct dwarf2_cu *cu);
1385
1386 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1387
1388 static struct die_info *die_specification (struct die_info *die,
1389 struct dwarf2_cu **);
1390
1391 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1392 struct dwarf2_cu *cu);
1393
1394 static void dwarf_decode_lines (struct line_header *, const char *,
1395 struct dwarf2_cu *, dwarf2_psymtab *,
1396 CORE_ADDR, int decode_mapping);
1397
1398 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1399 const char *);
1400
1401 static struct symbol *new_symbol (struct die_info *, struct type *,
1402 struct dwarf2_cu *, struct symbol * = NULL);
1403
1404 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1405 struct dwarf2_cu *);
1406
1407 static void dwarf2_const_value_attr (const struct attribute *attr,
1408 struct type *type,
1409 const char *name,
1410 struct obstack *obstack,
1411 struct dwarf2_cu *cu, LONGEST *value,
1412 const gdb_byte **bytes,
1413 struct dwarf2_locexpr_baton **baton);
1414
1415 static struct type *read_subrange_index_type (struct die_info *die,
1416 struct dwarf2_cu *cu);
1417
1418 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1419
1420 static int need_gnat_info (struct dwarf2_cu *);
1421
1422 static struct type *die_descriptive_type (struct die_info *,
1423 struct dwarf2_cu *);
1424
1425 static void set_descriptive_type (struct type *, struct die_info *,
1426 struct dwarf2_cu *);
1427
1428 static struct type *die_containing_type (struct die_info *,
1429 struct dwarf2_cu *);
1430
1431 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1432 struct dwarf2_cu *);
1433
1434 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1435
1436 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1437
1438 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1439
1440 static char *typename_concat (struct obstack *obs, const char *prefix,
1441 const char *suffix, int physname,
1442 struct dwarf2_cu *cu);
1443
1444 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1445
1446 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1447
1448 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1449
1450 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1451
1452 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1453
1454 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1455
1456 /* Return the .debug_loclists section to use for cu. */
1457 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1458
1459 /* Return the .debug_rnglists section to use for cu. */
1460 static struct dwarf2_section_info *cu_debug_rnglists_section
1461 (struct dwarf2_cu *cu, dwarf_tag tag);
1462
1463 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1464 values. Keep the items ordered with increasing constraints compliance. */
1465 enum pc_bounds_kind
1466 {
1467 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1468 PC_BOUNDS_NOT_PRESENT,
1469
1470 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1471 were present but they do not form a valid range of PC addresses. */
1472 PC_BOUNDS_INVALID,
1473
1474 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1475 PC_BOUNDS_RANGES,
1476
1477 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1478 PC_BOUNDS_HIGH_LOW,
1479 };
1480
1481 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1482 CORE_ADDR *, CORE_ADDR *,
1483 struct dwarf2_cu *,
1484 dwarf2_psymtab *);
1485
1486 static void get_scope_pc_bounds (struct die_info *,
1487 CORE_ADDR *, CORE_ADDR *,
1488 struct dwarf2_cu *);
1489
1490 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1491 CORE_ADDR, struct dwarf2_cu *);
1492
1493 static void dwarf2_add_field (struct field_info *, struct die_info *,
1494 struct dwarf2_cu *);
1495
1496 static void dwarf2_attach_fields_to_type (struct field_info *,
1497 struct type *, struct dwarf2_cu *);
1498
1499 static void dwarf2_add_member_fn (struct field_info *,
1500 struct die_info *, struct type *,
1501 struct dwarf2_cu *);
1502
1503 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1504 struct type *,
1505 struct dwarf2_cu *);
1506
1507 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1508
1509 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1510
1511 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1512
1513 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1514
1515 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1516
1517 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1518
1519 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1520
1521 static struct type *read_module_type (struct die_info *die,
1522 struct dwarf2_cu *cu);
1523
1524 static const char *namespace_name (struct die_info *die,
1525 int *is_anonymous, struct dwarf2_cu *);
1526
1527 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1528
1529 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1530 bool * = nullptr);
1531
1532 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1533 struct dwarf2_cu *);
1534
1535 static struct die_info *read_die_and_siblings_1
1536 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1537 struct die_info *);
1538
1539 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1540 const gdb_byte *info_ptr,
1541 const gdb_byte **new_info_ptr,
1542 struct die_info *parent);
1543
1544 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1545 struct die_info **, const gdb_byte *,
1546 int);
1547
1548 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1549 struct die_info **, const gdb_byte *);
1550
1551 static void process_die (struct die_info *, struct dwarf2_cu *);
1552
1553 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1554 struct objfile *);
1555
1556 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1557
1558 static const char *dwarf2_full_name (const char *name,
1559 struct die_info *die,
1560 struct dwarf2_cu *cu);
1561
1562 static const char *dwarf2_physname (const char *name, struct die_info *die,
1563 struct dwarf2_cu *cu);
1564
1565 static struct die_info *dwarf2_extension (struct die_info *die,
1566 struct dwarf2_cu **);
1567
1568 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1569
1570 static void dump_die_for_error (struct die_info *);
1571
1572 static void dump_die_1 (struct ui_file *, int level, int max_level,
1573 struct die_info *);
1574
1575 /*static*/ void dump_die (struct die_info *, int max_level);
1576
1577 static void store_in_ref_table (struct die_info *,
1578 struct dwarf2_cu *);
1579
1580 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1581 const struct attribute *,
1582 struct dwarf2_cu **);
1583
1584 static struct die_info *follow_die_ref (struct die_info *,
1585 const struct attribute *,
1586 struct dwarf2_cu **);
1587
1588 static struct die_info *follow_die_sig (struct die_info *,
1589 const struct attribute *,
1590 struct dwarf2_cu **);
1591
1592 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1593 struct dwarf2_cu *);
1594
1595 static struct type *get_DW_AT_signature_type (struct die_info *,
1596 const struct attribute *,
1597 struct dwarf2_cu *);
1598
1599 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1600 dwarf2_per_objfile *per_objfile);
1601
1602 static void read_signatured_type (signatured_type *sig_type,
1603 dwarf2_per_objfile *per_objfile);
1604
1605 static int attr_to_dynamic_prop (const struct attribute *attr,
1606 struct die_info *die, struct dwarf2_cu *cu,
1607 struct dynamic_prop *prop, struct type *type);
1608
1609 /* memory allocation interface */
1610
1611 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1612
1613 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1614
1615 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1616
1617 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1618 struct dwarf2_loclist_baton *baton,
1619 const struct attribute *attr);
1620
1621 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1622 struct symbol *sym,
1623 struct dwarf2_cu *cu,
1624 int is_block);
1625
1626 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1627 const gdb_byte *info_ptr,
1628 const struct abbrev_info *abbrev);
1629
1630 static hashval_t partial_die_hash (const void *item);
1631
1632 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1633
1634 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1635 (sect_offset sect_off, unsigned int offset_in_dwz,
1636 dwarf2_per_objfile *per_objfile);
1637
1638 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1639 struct die_info *comp_unit_die,
1640 enum language pretend_language);
1641
1642 static struct type *set_die_type (struct die_info *, struct type *,
1643 struct dwarf2_cu *, bool = false);
1644
1645 static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
1646
1647 static int create_all_type_units (dwarf2_per_objfile *per_objfile);
1648
1649 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1650 dwarf2_per_objfile *per_objfile,
1651 dwarf2_cu *existing_cu,
1652 bool skip_partial,
1653 enum language pretend_language);
1654
1655 static void process_full_comp_unit (dwarf2_cu *cu,
1656 enum language pretend_language);
1657
1658 static void process_full_type_unit (dwarf2_cu *cu,
1659 enum language pretend_language);
1660
1661 static void dwarf2_add_dependence (struct dwarf2_cu *,
1662 struct dwarf2_per_cu_data *);
1663
1664 static void dwarf2_mark (struct dwarf2_cu *);
1665
1666 static struct type *get_die_type_at_offset (sect_offset,
1667 dwarf2_per_cu_data *per_cu,
1668 dwarf2_per_objfile *per_objfile);
1669
1670 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1671
1672 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1673 dwarf2_per_objfile *per_objfile,
1674 enum language pretend_language);
1675
1676 static void process_queue (dwarf2_per_objfile *per_objfile);
1677
1678 /* Class, the destructor of which frees all allocated queue entries. This
1679 will only have work to do if an error was thrown while processing the
1680 dwarf. If no error was thrown then the queue entries should have all
1681 been processed, and freed, as we went along. */
1682
1683 class dwarf2_queue_guard
1684 {
1685 public:
1686 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1687 : m_per_objfile (per_objfile)
1688 {
1689 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1690
1691 m_per_objfile->per_bfd->queue.emplace ();
1692 }
1693
1694 /* Free any entries remaining on the queue. There should only be
1695 entries left if we hit an error while processing the dwarf. */
1696 ~dwarf2_queue_guard ()
1697 {
1698 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1699
1700 m_per_objfile->per_bfd->queue.reset ();
1701 }
1702
1703 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1704
1705 private:
1706 dwarf2_per_objfile *m_per_objfile;
1707 };
1708
1709 dwarf2_queue_item::~dwarf2_queue_item ()
1710 {
1711 /* Anything still marked queued is likely to be in an
1712 inconsistent state, so discard it. */
1713 if (per_cu->queued)
1714 {
1715 per_objfile->remove_cu (per_cu);
1716 per_cu->queued = 0;
1717 }
1718 }
1719
1720 /* The return type of find_file_and_directory. Note, the enclosed
1721 string pointers are only valid while this object is valid. */
1722
1723 struct file_and_directory
1724 {
1725 /* The filename. This is never NULL. */
1726 const char *name;
1727
1728 /* The compilation directory. NULL if not known. If we needed to
1729 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1730 points directly to the DW_AT_comp_dir string attribute owned by
1731 the obstack that owns the DIE. */
1732 const char *comp_dir;
1733
1734 /* If we needed to build a new string for comp_dir, this is what
1735 owns the storage. */
1736 std::string comp_dir_storage;
1737 };
1738
1739 static file_and_directory find_file_and_directory (struct die_info *die,
1740 struct dwarf2_cu *cu);
1741
1742 static htab_up allocate_signatured_type_table ();
1743
1744 static htab_up allocate_dwo_unit_table ();
1745
1746 static struct dwo_unit *lookup_dwo_unit_in_dwp
1747 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1748 const char *comp_dir, ULONGEST signature, int is_debug_types);
1749
1750 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1751
1752 static struct dwo_unit *lookup_dwo_comp_unit
1753 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1754 ULONGEST signature);
1755
1756 static struct dwo_unit *lookup_dwo_type_unit
1757 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1758
1759 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1760
1761 /* A unique pointer to a dwo_file. */
1762
1763 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1764
1765 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1766
1767 static void check_producer (struct dwarf2_cu *cu);
1768
1769 static void free_line_header_voidp (void *arg);
1770 \f
1771 /* Various complaints about symbol reading that don't abort the process. */
1772
1773 static void
1774 dwarf2_debug_line_missing_file_complaint (void)
1775 {
1776 complaint (_(".debug_line section has line data without a file"));
1777 }
1778
1779 static void
1780 dwarf2_debug_line_missing_end_sequence_complaint (void)
1781 {
1782 complaint (_(".debug_line section has line "
1783 "program sequence without an end"));
1784 }
1785
1786 static void
1787 dwarf2_complex_location_expr_complaint (void)
1788 {
1789 complaint (_("location expression too complex"));
1790 }
1791
1792 static void
1793 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1794 int arg3)
1795 {
1796 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1797 arg1, arg2, arg3);
1798 }
1799
1800 static void
1801 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1802 {
1803 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1804 arg1, arg2);
1805 }
1806
1807 /* Hash function for line_header_hash. */
1808
1809 static hashval_t
1810 line_header_hash (const struct line_header *ofs)
1811 {
1812 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1813 }
1814
1815 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1816
1817 static hashval_t
1818 line_header_hash_voidp (const void *item)
1819 {
1820 const struct line_header *ofs = (const struct line_header *) item;
1821
1822 return line_header_hash (ofs);
1823 }
1824
1825 /* Equality function for line_header_hash. */
1826
1827 static int
1828 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1829 {
1830 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1831 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1832
1833 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1834 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1835 }
1836
1837 \f
1838
1839 /* See declaration. */
1840
1841 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1842 bool can_copy_)
1843 : obfd (obfd),
1844 can_copy (can_copy_)
1845 {
1846 if (names == NULL)
1847 names = &dwarf2_elf_names;
1848
1849 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1850 locate_sections (obfd, sec, *names);
1851 }
1852
1853 dwarf2_per_bfd::~dwarf2_per_bfd ()
1854 {
1855 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1856 per_cu->imported_symtabs_free ();
1857
1858 for (signatured_type *sig_type : all_type_units)
1859 sig_type->per_cu.imported_symtabs_free ();
1860
1861 /* Everything else should be on this->obstack. */
1862 }
1863
1864 /* See read.h. */
1865
1866 void
1867 dwarf2_per_objfile::remove_all_cus ()
1868 {
1869 gdb_assert (!this->per_bfd->queue.has_value ());
1870
1871 for (auto pair : m_dwarf2_cus)
1872 delete pair.second;
1873
1874 m_dwarf2_cus.clear ();
1875 }
1876
1877 /* A helper class that calls free_cached_comp_units on
1878 destruction. */
1879
1880 class free_cached_comp_units
1881 {
1882 public:
1883
1884 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1885 : m_per_objfile (per_objfile)
1886 {
1887 }
1888
1889 ~free_cached_comp_units ()
1890 {
1891 m_per_objfile->remove_all_cus ();
1892 }
1893
1894 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1895
1896 private:
1897
1898 dwarf2_per_objfile *m_per_objfile;
1899 };
1900
1901 /* See read.h. */
1902
1903 bool
1904 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1905 {
1906 gdb_assert (per_cu->index < this->m_symtabs.size ());
1907
1908 return this->m_symtabs[per_cu->index] != nullptr;
1909 }
1910
1911 /* See read.h. */
1912
1913 compunit_symtab *
1914 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1915 {
1916 gdb_assert (per_cu->index < this->m_symtabs.size ());
1917
1918 return this->m_symtabs[per_cu->index];
1919 }
1920
1921 /* See read.h. */
1922
1923 void
1924 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1925 compunit_symtab *symtab)
1926 {
1927 gdb_assert (per_cu->index < this->m_symtabs.size ());
1928 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1929
1930 this->m_symtabs[per_cu->index] = symtab;
1931 }
1932
1933 /* Try to locate the sections we need for DWARF 2 debugging
1934 information and return true if we have enough to do something.
1935 NAMES points to the dwarf2 section names, or is NULL if the standard
1936 ELF names are used. CAN_COPY is true for formats where symbol
1937 interposition is possible and so symbol values must follow copy
1938 relocation rules. */
1939
1940 int
1941 dwarf2_has_info (struct objfile *objfile,
1942 const struct dwarf2_debug_sections *names,
1943 bool can_copy)
1944 {
1945 if (objfile->flags & OBJF_READNEVER)
1946 return 0;
1947
1948 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1949
1950 if (per_objfile == NULL)
1951 {
1952 dwarf2_per_bfd *per_bfd;
1953
1954 /* We can share a "dwarf2_per_bfd" with other objfiles if the 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, bool print_bcache) 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 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 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2711 struct gdbarch *gdbarch = objfile->arch ();
2712 const gdb_byte *iter, *end;
2713 struct addrmap *mutable_map;
2714 CORE_ADDR baseaddr;
2715
2716 auto_obstack temp_obstack;
2717
2718 mutable_map = addrmap_create_mutable (&temp_obstack);
2719
2720 iter = index->address_table.data ();
2721 end = iter + index->address_table.size ();
2722
2723 baseaddr = objfile->text_section_offset ();
2724
2725 while (iter < end)
2726 {
2727 ULONGEST hi, lo, cu_index;
2728 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2729 iter += 8;
2730 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2731 iter += 8;
2732 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2733 iter += 4;
2734
2735 if (lo > hi)
2736 {
2737 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2738 hex_string (lo), hex_string (hi));
2739 continue;
2740 }
2741
2742 if (cu_index >= per_bfd->all_comp_units.size ())
2743 {
2744 complaint (_(".gdb_index address table has invalid CU number %u"),
2745 (unsigned) cu_index);
2746 continue;
2747 }
2748
2749 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2750 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2751 addrmap_set_empty (mutable_map, lo, hi - 1,
2752 per_bfd->get_cu (cu_index));
2753 }
2754
2755 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2756 &per_bfd->obstack);
2757 }
2758
2759 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2760 populate the psymtabs_addrmap. */
2761
2762 static void
2763 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2764 struct dwarf2_section_info *section)
2765 {
2766 struct objfile *objfile = per_objfile->objfile;
2767 bfd *abfd = objfile->obfd;
2768 struct gdbarch *gdbarch = objfile->arch ();
2769 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2770 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2771
2772 auto_obstack temp_obstack;
2773 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2774
2775 std::unordered_map<sect_offset,
2776 dwarf2_per_cu_data *,
2777 gdb::hash_enum<sect_offset>>
2778 debug_info_offset_to_per_cu;
2779 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
2780 {
2781 const auto insertpair
2782 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2783 if (!insertpair.second)
2784 {
2785 warning (_("Section .debug_aranges in %s has duplicate "
2786 "debug_info_offset %s, ignoring .debug_aranges."),
2787 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2788 return;
2789 }
2790 }
2791
2792 section->read (objfile);
2793
2794 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2795
2796 const gdb_byte *addr = section->buffer;
2797
2798 while (addr < section->buffer + section->size)
2799 {
2800 const gdb_byte *const entry_addr = addr;
2801 unsigned int bytes_read;
2802
2803 const LONGEST entry_length = read_initial_length (abfd, addr,
2804 &bytes_read);
2805 addr += bytes_read;
2806
2807 const gdb_byte *const entry_end = addr + entry_length;
2808 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2809 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2810 if (addr + entry_length > section->buffer + section->size)
2811 {
2812 warning (_("Section .debug_aranges in %s entry at offset %s "
2813 "length %s exceeds section length %s, "
2814 "ignoring .debug_aranges."),
2815 objfile_name (objfile),
2816 plongest (entry_addr - section->buffer),
2817 plongest (bytes_read + entry_length),
2818 pulongest (section->size));
2819 return;
2820 }
2821
2822 /* The version number. */
2823 const uint16_t version = read_2_bytes (abfd, addr);
2824 addr += 2;
2825 if (version != 2)
2826 {
2827 warning (_("Section .debug_aranges in %s entry at offset %s "
2828 "has unsupported version %d, ignoring .debug_aranges."),
2829 objfile_name (objfile),
2830 plongest (entry_addr - section->buffer), version);
2831 return;
2832 }
2833
2834 const uint64_t debug_info_offset
2835 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2836 addr += offset_size;
2837 const auto per_cu_it
2838 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2839 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2840 {
2841 warning (_("Section .debug_aranges in %s entry at offset %s "
2842 "debug_info_offset %s does not exists, "
2843 "ignoring .debug_aranges."),
2844 objfile_name (objfile),
2845 plongest (entry_addr - section->buffer),
2846 pulongest (debug_info_offset));
2847 return;
2848 }
2849 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2850
2851 const uint8_t address_size = *addr++;
2852 if (address_size < 1 || address_size > 8)
2853 {
2854 warning (_("Section .debug_aranges in %s entry at offset %s "
2855 "address_size %u is invalid, ignoring .debug_aranges."),
2856 objfile_name (objfile),
2857 plongest (entry_addr - section->buffer), address_size);
2858 return;
2859 }
2860
2861 const uint8_t segment_selector_size = *addr++;
2862 if (segment_selector_size != 0)
2863 {
2864 warning (_("Section .debug_aranges in %s entry at offset %s "
2865 "segment_selector_size %u is not supported, "
2866 "ignoring .debug_aranges."),
2867 objfile_name (objfile),
2868 plongest (entry_addr - section->buffer),
2869 segment_selector_size);
2870 return;
2871 }
2872
2873 /* Must pad to an alignment boundary that is twice the address
2874 size. It is undocumented by the DWARF standard but GCC does
2875 use it. */
2876 for (size_t padding = ((-(addr - section->buffer))
2877 & (2 * address_size - 1));
2878 padding > 0; padding--)
2879 if (*addr++ != 0)
2880 {
2881 warning (_("Section .debug_aranges in %s entry at offset %s "
2882 "padding is not zero, ignoring .debug_aranges."),
2883 objfile_name (objfile),
2884 plongest (entry_addr - section->buffer));
2885 return;
2886 }
2887
2888 for (;;)
2889 {
2890 if (addr + 2 * address_size > entry_end)
2891 {
2892 warning (_("Section .debug_aranges in %s entry at offset %s "
2893 "address list is not properly terminated, "
2894 "ignoring .debug_aranges."),
2895 objfile_name (objfile),
2896 plongest (entry_addr - section->buffer));
2897 return;
2898 }
2899 ULONGEST start = extract_unsigned_integer (addr, address_size,
2900 dwarf5_byte_order);
2901 addr += address_size;
2902 ULONGEST length = extract_unsigned_integer (addr, address_size,
2903 dwarf5_byte_order);
2904 addr += address_size;
2905 if (start == 0 && length == 0)
2906 break;
2907 if (start == 0 && !per_bfd->has_section_at_zero)
2908 {
2909 /* Symbol was eliminated due to a COMDAT group. */
2910 continue;
2911 }
2912 ULONGEST end = start + length;
2913 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2914 - baseaddr);
2915 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2916 - baseaddr);
2917 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2918 }
2919 }
2920
2921 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2922 &per_bfd->obstack);
2923 }
2924
2925 /* Find a slot in the mapped index INDEX for the object named NAME.
2926 If NAME is found, set *VEC_OUT to point to the CU vector in the
2927 constant pool and return true. If NAME cannot be found, return
2928 false. */
2929
2930 static bool
2931 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2932 offset_type **vec_out)
2933 {
2934 offset_type hash;
2935 offset_type slot, step;
2936 int (*cmp) (const char *, const char *);
2937
2938 gdb::unique_xmalloc_ptr<char> without_params;
2939 if (current_language->la_language == language_cplus
2940 || current_language->la_language == language_fortran
2941 || current_language->la_language == language_d)
2942 {
2943 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2944 not contain any. */
2945
2946 if (strchr (name, '(') != NULL)
2947 {
2948 without_params = cp_remove_params (name);
2949
2950 if (without_params != NULL)
2951 name = without_params.get ();
2952 }
2953 }
2954
2955 /* Index version 4 did not support case insensitive searches. But the
2956 indices for case insensitive languages are built in lowercase, therefore
2957 simulate our NAME being searched is also lowercased. */
2958 hash = mapped_index_string_hash ((index->version == 4
2959 && case_sensitivity == case_sensitive_off
2960 ? 5 : index->version),
2961 name);
2962
2963 slot = hash & (index->symbol_table.size () - 1);
2964 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2965 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2966
2967 for (;;)
2968 {
2969 const char *str;
2970
2971 const auto &bucket = index->symbol_table[slot];
2972 if (bucket.name == 0 && bucket.vec == 0)
2973 return false;
2974
2975 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2976 if (!cmp (name, str))
2977 {
2978 *vec_out = (offset_type *) (index->constant_pool
2979 + MAYBE_SWAP (bucket.vec));
2980 return true;
2981 }
2982
2983 slot = (slot + step) & (index->symbol_table.size () - 1);
2984 }
2985 }
2986
2987 /* A helper function that reads the .gdb_index from BUFFER and fills
2988 in MAP. FILENAME is the name of the file containing the data;
2989 it is used for error reporting. DEPRECATED_OK is true if it is
2990 ok to use deprecated sections.
2991
2992 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2993 out parameters that are filled in with information about the CU and
2994 TU lists in the section.
2995
2996 Returns true if all went well, false otherwise. */
2997
2998 static bool
2999 read_gdb_index_from_buffer (const char *filename,
3000 bool deprecated_ok,
3001 gdb::array_view<const gdb_byte> buffer,
3002 struct mapped_index *map,
3003 const gdb_byte **cu_list,
3004 offset_type *cu_list_elements,
3005 const gdb_byte **types_list,
3006 offset_type *types_list_elements)
3007 {
3008 const gdb_byte *addr = &buffer[0];
3009
3010 /* Version check. */
3011 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3012 /* Versions earlier than 3 emitted every copy of a psymbol. This
3013 causes the index to behave very poorly for certain requests. Version 3
3014 contained incomplete addrmap. So, it seems better to just ignore such
3015 indices. */
3016 if (version < 4)
3017 {
3018 static int warning_printed = 0;
3019 if (!warning_printed)
3020 {
3021 warning (_("Skipping obsolete .gdb_index section in %s."),
3022 filename);
3023 warning_printed = 1;
3024 }
3025 return 0;
3026 }
3027 /* Index version 4 uses a different hash function than index version
3028 5 and later.
3029
3030 Versions earlier than 6 did not emit psymbols for inlined
3031 functions. Using these files will cause GDB not to be able to
3032 set breakpoints on inlined functions by name, so we ignore these
3033 indices unless the user has done
3034 "set use-deprecated-index-sections on". */
3035 if (version < 6 && !deprecated_ok)
3036 {
3037 static int warning_printed = 0;
3038 if (!warning_printed)
3039 {
3040 warning (_("\
3041 Skipping deprecated .gdb_index section in %s.\n\
3042 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3043 to use the section anyway."),
3044 filename);
3045 warning_printed = 1;
3046 }
3047 return 0;
3048 }
3049 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3050 of the TU (for symbols coming from TUs),
3051 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3052 Plus gold-generated indices can have duplicate entries for global symbols,
3053 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3054 These are just performance bugs, and we can't distinguish gdb-generated
3055 indices from gold-generated ones, so issue no warning here. */
3056
3057 /* Indexes with higher version than the one supported by GDB may be no
3058 longer backward compatible. */
3059 if (version > 8)
3060 return 0;
3061
3062 map->version = version;
3063
3064 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3065
3066 int i = 0;
3067 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3068 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3069 / 8);
3070 ++i;
3071
3072 *types_list = addr + MAYBE_SWAP (metadata[i]);
3073 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3074 - MAYBE_SWAP (metadata[i]))
3075 / 8);
3076 ++i;
3077
3078 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3079 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3080 map->address_table
3081 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3082 ++i;
3083
3084 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3085 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3086 map->symbol_table
3087 = gdb::array_view<mapped_index::symbol_table_slot>
3088 ((mapped_index::symbol_table_slot *) symbol_table,
3089 (mapped_index::symbol_table_slot *) symbol_table_end);
3090
3091 ++i;
3092 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3093
3094 return 1;
3095 }
3096
3097 /* Callback types for dwarf2_read_gdb_index. */
3098
3099 typedef gdb::function_view
3100 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3101 get_gdb_index_contents_ftype;
3102 typedef gdb::function_view
3103 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3104 get_gdb_index_contents_dwz_ftype;
3105
3106 /* Read .gdb_index. If everything went ok, initialize the "quick"
3107 elements of all the CUs and return 1. Otherwise, return 0. */
3108
3109 static int
3110 dwarf2_read_gdb_index
3111 (dwarf2_per_objfile *per_objfile,
3112 get_gdb_index_contents_ftype get_gdb_index_contents,
3113 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3114 {
3115 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3116 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3117 struct dwz_file *dwz;
3118 struct objfile *objfile = per_objfile->objfile;
3119 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
3120
3121 gdb::array_view<const gdb_byte> main_index_contents
3122 = get_gdb_index_contents (objfile, per_bfd);
3123
3124 if (main_index_contents.empty ())
3125 return 0;
3126
3127 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3128 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3129 use_deprecated_index_sections,
3130 main_index_contents, map.get (), &cu_list,
3131 &cu_list_elements, &types_list,
3132 &types_list_elements))
3133 return 0;
3134
3135 /* Don't use the index if it's empty. */
3136 if (map->symbol_table.empty ())
3137 return 0;
3138
3139 /* If there is a .dwz file, read it so we can get its CU list as
3140 well. */
3141 dwz = dwarf2_get_dwz_file (per_bfd);
3142 if (dwz != NULL)
3143 {
3144 struct mapped_index dwz_map;
3145 const gdb_byte *dwz_types_ignore;
3146 offset_type dwz_types_elements_ignore;
3147
3148 gdb::array_view<const gdb_byte> dwz_index_content
3149 = get_gdb_index_contents_dwz (objfile, dwz);
3150
3151 if (dwz_index_content.empty ())
3152 return 0;
3153
3154 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3155 1, dwz_index_content, &dwz_map,
3156 &dwz_list, &dwz_list_elements,
3157 &dwz_types_ignore,
3158 &dwz_types_elements_ignore))
3159 {
3160 warning (_("could not read '.gdb_index' section from %s; skipping"),
3161 bfd_get_filename (dwz->dwz_bfd.get ()));
3162 return 0;
3163 }
3164 }
3165
3166 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3167 dwz_list_elements);
3168
3169 if (types_list_elements)
3170 {
3171 /* We can only handle a single .debug_types when we have an
3172 index. */
3173 if (per_bfd->types.size () != 1)
3174 return 0;
3175
3176 dwarf2_section_info *section = &per_bfd->types[0];
3177
3178 create_signatured_type_table_from_index (per_bfd, section, types_list,
3179 types_list_elements);
3180 }
3181
3182 create_addrmap_from_index (per_objfile, map.get ());
3183
3184 per_bfd->index_table = std::move (map);
3185 per_bfd->using_index = 1;
3186 per_bfd->quick_file_names_table =
3187 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3188
3189 return 1;
3190 }
3191
3192 /* die_reader_func for dw2_get_file_names. */
3193
3194 static void
3195 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3196 struct die_info *comp_unit_die)
3197 {
3198 struct dwarf2_cu *cu = reader->cu;
3199 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3200 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3201 struct dwarf2_per_cu_data *lh_cu;
3202 struct attribute *attr;
3203 void **slot;
3204 struct quick_file_names *qfn;
3205
3206 gdb_assert (! this_cu->is_debug_types);
3207
3208 /* Our callers never want to match partial units -- instead they
3209 will match the enclosing full CU. */
3210 if (comp_unit_die->tag == DW_TAG_partial_unit)
3211 {
3212 this_cu->v.quick->no_file_data = 1;
3213 return;
3214 }
3215
3216 lh_cu = this_cu;
3217 slot = NULL;
3218
3219 line_header_up lh;
3220 sect_offset line_offset {};
3221
3222 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3223 if (attr != nullptr && attr->form_is_unsigned ())
3224 {
3225 struct quick_file_names find_entry;
3226
3227 line_offset = (sect_offset) attr->as_unsigned ();
3228
3229 /* We may have already read in this line header (TU line header sharing).
3230 If we have we're done. */
3231 find_entry.hash.dwo_unit = cu->dwo_unit;
3232 find_entry.hash.line_sect_off = line_offset;
3233 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
3234 &find_entry, INSERT);
3235 if (*slot != NULL)
3236 {
3237 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3238 return;
3239 }
3240
3241 lh = dwarf_decode_line_header (line_offset, cu);
3242 }
3243 if (lh == NULL)
3244 {
3245 lh_cu->v.quick->no_file_data = 1;
3246 return;
3247 }
3248
3249 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
3250 qfn->hash.dwo_unit = cu->dwo_unit;
3251 qfn->hash.line_sect_off = line_offset;
3252 gdb_assert (slot != NULL);
3253 *slot = qfn;
3254
3255 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3256
3257 int offset = 0;
3258 if (strcmp (fnd.name, "<unknown>") != 0)
3259 ++offset;
3260
3261 qfn->num_file_names = offset + lh->file_names_size ();
3262 qfn->file_names =
3263 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
3264 qfn->num_file_names);
3265 if (offset != 0)
3266 qfn->file_names[0] = xstrdup (fnd.name);
3267 for (int i = 0; i < lh->file_names_size (); ++i)
3268 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3269 fnd.comp_dir).release ();
3270 qfn->real_names = NULL;
3271
3272 lh_cu->v.quick->file_names = qfn;
3273 }
3274
3275 /* A helper for the "quick" functions which attempts to read the line
3276 table for THIS_CU. */
3277
3278 static struct quick_file_names *
3279 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3280 dwarf2_per_objfile *per_objfile)
3281 {
3282 /* This should never be called for TUs. */
3283 gdb_assert (! this_cu->is_debug_types);
3284 /* Nor type unit groups. */
3285 gdb_assert (! this_cu->type_unit_group_p ());
3286
3287 if (this_cu->v.quick->file_names != NULL)
3288 return this_cu->v.quick->file_names;
3289 /* If we know there is no line data, no point in looking again. */
3290 if (this_cu->v.quick->no_file_data)
3291 return NULL;
3292
3293 cutu_reader reader (this_cu, per_objfile);
3294 if (!reader.dummy_p)
3295 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
3296
3297 if (this_cu->v.quick->no_file_data)
3298 return NULL;
3299 return this_cu->v.quick->file_names;
3300 }
3301
3302 /* A helper for the "quick" functions which computes and caches the
3303 real path for a given file name from the line table. */
3304
3305 static const char *
3306 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3307 struct quick_file_names *qfn, int index)
3308 {
3309 if (qfn->real_names == NULL)
3310 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3311 qfn->num_file_names, const char *);
3312
3313 if (qfn->real_names[index] == NULL)
3314 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3315
3316 return qfn->real_names[index];
3317 }
3318
3319 struct symtab *
3320 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
3321 {
3322 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3323 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3324 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3325
3326 if (cust == NULL)
3327 return NULL;
3328
3329 return compunit_primary_filetab (cust);
3330 }
3331
3332 /* Traversal function for dw2_forget_cached_source_info. */
3333
3334 static int
3335 dw2_free_cached_file_names (void **slot, void *info)
3336 {
3337 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3338
3339 if (file_data->real_names)
3340 {
3341 int i;
3342
3343 for (i = 0; i < file_data->num_file_names; ++i)
3344 {
3345 xfree ((void*) file_data->real_names[i]);
3346 file_data->real_names[i] = NULL;
3347 }
3348 }
3349
3350 return 1;
3351 }
3352
3353 void
3354 dwarf2_base_index_functions::forget_cached_source_info
3355 (struct objfile *objfile)
3356 {
3357 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3358
3359 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
3360 dw2_free_cached_file_names, NULL);
3361 }
3362
3363 /* Helper function for dw2_map_symtabs_matching_filename that expands
3364 the symtabs and calls the iterator. */
3365
3366 static int
3367 dw2_map_expand_apply (struct objfile *objfile,
3368 struct dwarf2_per_cu_data *per_cu,
3369 const char *name, const char *real_path,
3370 gdb::function_view<bool (symtab *)> callback)
3371 {
3372 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3373
3374 /* Don't visit already-expanded CUs. */
3375 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3376 if (per_objfile->symtab_set_p (per_cu))
3377 return 0;
3378
3379 /* This may expand more than one symtab, and we want to iterate over
3380 all of them. */
3381 dw2_instantiate_symtab (per_cu, per_objfile, false);
3382
3383 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3384 last_made, callback);
3385 }
3386
3387 /* Implementation of the map_symtabs_matching_filename method. */
3388
3389 bool
3390 dwarf2_base_index_functions::map_symtabs_matching_filename
3391 (struct objfile *objfile, const char *name, const char *real_path,
3392 gdb::function_view<bool (symtab *)> callback)
3393 {
3394 const char *name_basename = lbasename (name);
3395 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3396
3397 /* The rule is CUs specify all the files, including those used by
3398 any TU, so there's no need to scan TUs here. */
3399
3400 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3401 {
3402 /* We only need to look at symtabs not already expanded. */
3403 if (per_objfile->symtab_set_p (per_cu))
3404 continue;
3405
3406 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3407 if (file_data == NULL)
3408 continue;
3409
3410 for (int j = 0; j < file_data->num_file_names; ++j)
3411 {
3412 const char *this_name = file_data->file_names[j];
3413 const char *this_real_name;
3414
3415 if (compare_filenames_for_search (this_name, name))
3416 {
3417 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3418 callback))
3419 return true;
3420 continue;
3421 }
3422
3423 /* Before we invoke realpath, which can get expensive when many
3424 files are involved, do a quick comparison of the basenames. */
3425 if (! basenames_may_differ
3426 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3427 continue;
3428
3429 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
3430 if (compare_filenames_for_search (this_real_name, name))
3431 {
3432 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3433 callback))
3434 return true;
3435 continue;
3436 }
3437
3438 if (real_path != NULL)
3439 {
3440 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3441 gdb_assert (IS_ABSOLUTE_PATH (name));
3442 if (this_real_name != NULL
3443 && FILENAME_CMP (real_path, this_real_name) == 0)
3444 {
3445 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3446 callback))
3447 return true;
3448 continue;
3449 }
3450 }
3451 }
3452 }
3453
3454 return false;
3455 }
3456
3457 /* Struct used to manage iterating over all CUs looking for a symbol. */
3458
3459 struct dw2_symtab_iterator
3460 {
3461 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3462 dwarf2_per_objfile *per_objfile;
3463 /* If set, only look for symbols that match that block. Valid values are
3464 GLOBAL_BLOCK and STATIC_BLOCK. */
3465 gdb::optional<block_enum> block_index;
3466 /* The kind of symbol we're looking for. */
3467 domain_enum domain;
3468 /* The list of CUs from the index entry of the symbol,
3469 or NULL if not found. */
3470 offset_type *vec;
3471 /* The next element in VEC to look at. */
3472 int next;
3473 /* The number of elements in VEC, or zero if there is no match. */
3474 int length;
3475 /* Have we seen a global version of the symbol?
3476 If so we can ignore all further global instances.
3477 This is to work around gold/15646, inefficient gold-generated
3478 indices. */
3479 int global_seen;
3480 };
3481
3482 /* Initialize the index symtab iterator ITER, common part. */
3483
3484 static void
3485 dw2_symtab_iter_init_common (struct dw2_symtab_iterator *iter,
3486 dwarf2_per_objfile *per_objfile,
3487 gdb::optional<block_enum> block_index,
3488 domain_enum domain)
3489 {
3490 iter->per_objfile = per_objfile;
3491 iter->block_index = block_index;
3492 iter->domain = domain;
3493 iter->next = 0;
3494 iter->global_seen = 0;
3495 iter->vec = NULL;
3496 iter->length = 0;
3497 }
3498
3499 /* Initialize the index symtab iterator ITER, const char *NAME variant. */
3500
3501 static void
3502 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3503 dwarf2_per_objfile *per_objfile,
3504 gdb::optional<block_enum> block_index,
3505 domain_enum domain,
3506 const char *name)
3507 {
3508 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3509
3510 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3511 /* index is NULL if OBJF_READNOW. */
3512 if (index == NULL)
3513 return;
3514
3515 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3516 iter->length = MAYBE_SWAP (*iter->vec);
3517 }
3518
3519 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3520
3521 static void
3522 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3523 dwarf2_per_objfile *per_objfile,
3524 gdb::optional<block_enum> block_index,
3525 domain_enum domain, offset_type namei)
3526 {
3527 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3528
3529 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3530 /* index is NULL if OBJF_READNOW. */
3531 if (index == NULL)
3532 return;
3533
3534 gdb_assert (!index->symbol_name_slot_invalid (namei));
3535 const auto &bucket = index->symbol_table[namei];
3536
3537 iter->vec = (offset_type *) (index->constant_pool
3538 + MAYBE_SWAP (bucket.vec));
3539 iter->length = MAYBE_SWAP (*iter->vec);
3540 }
3541
3542 /* Return the next matching CU or NULL if there are no more. */
3543
3544 static struct dwarf2_per_cu_data *
3545 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3546 {
3547 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3548
3549 for ( ; iter->next < iter->length; ++iter->next)
3550 {
3551 offset_type cu_index_and_attrs =
3552 MAYBE_SWAP (iter->vec[iter->next + 1]);
3553 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3554 gdb_index_symbol_kind symbol_kind =
3555 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3556 /* Only check the symbol attributes if they're present.
3557 Indices prior to version 7 don't record them,
3558 and indices >= 7 may elide them for certain symbols
3559 (gold does this). */
3560 int attrs_valid =
3561 (per_objfile->per_bfd->index_table->version >= 7
3562 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3563
3564 /* Don't crash on bad data. */
3565 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3566 + per_objfile->per_bfd->all_type_units.size ()))
3567 {
3568 complaint (_(".gdb_index entry has bad CU index"
3569 " [in module %s]"), objfile_name (per_objfile->objfile));
3570 continue;
3571 }
3572
3573 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3574
3575 /* Skip if already read in. */
3576 if (per_objfile->symtab_set_p (per_cu))
3577 continue;
3578
3579 /* Check static vs global. */
3580 if (attrs_valid)
3581 {
3582 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3583
3584 if (iter->block_index.has_value ())
3585 {
3586 bool want_static = *iter->block_index == STATIC_BLOCK;
3587
3588 if (is_static != want_static)
3589 continue;
3590 }
3591
3592 /* Work around gold/15646. */
3593 if (!is_static
3594 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3595 {
3596 if (iter->global_seen)
3597 continue;
3598
3599 iter->global_seen = 1;
3600 }
3601 }
3602
3603 /* Only check the symbol's kind if it has one. */
3604 if (attrs_valid)
3605 {
3606 switch (iter->domain)
3607 {
3608 case VAR_DOMAIN:
3609 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3610 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3611 /* Some types are also in VAR_DOMAIN. */
3612 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3613 continue;
3614 break;
3615 case STRUCT_DOMAIN:
3616 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3617 continue;
3618 break;
3619 case LABEL_DOMAIN:
3620 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3621 continue;
3622 break;
3623 case MODULE_DOMAIN:
3624 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3625 continue;
3626 break;
3627 default:
3628 break;
3629 }
3630 }
3631
3632 ++iter->next;
3633 return per_cu;
3634 }
3635
3636 return NULL;
3637 }
3638
3639 struct compunit_symtab *
3640 dwarf2_gdb_index::lookup_symbol (struct objfile *objfile,
3641 block_enum block_index,
3642 const char *name, domain_enum domain)
3643 {
3644 struct compunit_symtab *stab_best = NULL;
3645 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3646
3647 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3648
3649 struct dw2_symtab_iterator iter;
3650 struct dwarf2_per_cu_data *per_cu;
3651
3652 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
3653
3654 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3655 {
3656 struct symbol *sym, *with_opaque = NULL;
3657 struct compunit_symtab *stab
3658 = dw2_instantiate_symtab (per_cu, per_objfile, false);
3659 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3660 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3661
3662 sym = block_find_symbol (block, name, domain,
3663 block_find_non_opaque_type_preferred,
3664 &with_opaque);
3665
3666 /* Some caution must be observed with overloaded functions
3667 and methods, since the index will not contain any overload
3668 information (but NAME might contain it). */
3669
3670 if (sym != NULL
3671 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3672 return stab;
3673 if (with_opaque != NULL
3674 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3675 stab_best = stab;
3676
3677 /* Keep looking through other CUs. */
3678 }
3679
3680 return stab_best;
3681 }
3682
3683 void
3684 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3685 bool print_bcache)
3686 {
3687 if (print_bcache)
3688 return;
3689
3690 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3691 int total = (per_objfile->per_bfd->all_comp_units.size ()
3692 + per_objfile->per_bfd->all_type_units.size ());
3693 int count = 0;
3694
3695 for (int i = 0; i < total; ++i)
3696 {
3697 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3698
3699 if (!per_objfile->symtab_set_p (per_cu))
3700 ++count;
3701 }
3702 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3703 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3704 }
3705
3706 /* This dumps minimal information about the index.
3707 It is called via "mt print objfiles".
3708 One use is to verify .gdb_index has been loaded by the
3709 gdb.dwarf2/gdb-index.exp testcase. */
3710
3711 void
3712 dwarf2_gdb_index::dump (struct objfile *objfile)
3713 {
3714 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3715
3716 gdb_assert (per_objfile->per_bfd->using_index);
3717 printf_filtered (".gdb_index:");
3718 if (per_objfile->per_bfd->index_table != NULL)
3719 {
3720 printf_filtered (" version %d\n",
3721 per_objfile->per_bfd->index_table->version);
3722 }
3723 else
3724 printf_filtered (" faked for \"readnow\"\n");
3725 printf_filtered ("\n");
3726 }
3727
3728 void
3729 dwarf2_gdb_index::expand_symtabs_for_function (struct objfile *objfile,
3730 const char *func_name)
3731 {
3732 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3733
3734 struct dw2_symtab_iterator iter;
3735 struct dwarf2_per_cu_data *per_cu;
3736
3737 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
3738
3739 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3740 dw2_instantiate_symtab (per_cu, per_objfile, false);
3741
3742 }
3743
3744 void
3745 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
3746 {
3747 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3748 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3749 + per_objfile->per_bfd->all_type_units.size ());
3750
3751 for (int i = 0; i < total_units; ++i)
3752 {
3753 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3754
3755 /* We don't want to directly expand a partial CU, because if we
3756 read it with the wrong language, then assertion failures can
3757 be triggered later on. See PR symtab/23010. So, tell
3758 dw2_instantiate_symtab to skip partial CUs -- any important
3759 partial CU will be read via DW_TAG_imported_unit anyway. */
3760 dw2_instantiate_symtab (per_cu, per_objfile, true);
3761 }
3762 }
3763
3764 void
3765 dwarf2_base_index_functions::expand_symtabs_with_fullname
3766 (struct objfile *objfile, const char *fullname)
3767 {
3768 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3769
3770 /* We don't need to consider type units here.
3771 This is only called for examining code, e.g. expand_line_sal.
3772 There can be an order of magnitude (or more) more type units
3773 than comp units, and we avoid them if we can. */
3774
3775 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3776 {
3777 /* We only need to look at symtabs not already expanded. */
3778 if (per_objfile->symtab_set_p (per_cu))
3779 continue;
3780
3781 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3782 if (file_data == NULL)
3783 continue;
3784
3785 for (int j = 0; j < file_data->num_file_names; ++j)
3786 {
3787 const char *this_fullname = file_data->file_names[j];
3788
3789 if (filename_cmp (this_fullname, fullname) == 0)
3790 {
3791 dw2_instantiate_symtab (per_cu, per_objfile, false);
3792 break;
3793 }
3794 }
3795 }
3796 }
3797
3798 static void
3799 dw2_expand_symtabs_matching_symbol
3800 (mapped_index_base &index,
3801 const lookup_name_info &lookup_name_in,
3802 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3803 enum search_domain kind,
3804 gdb::function_view<bool (offset_type)> match_callback,
3805 dwarf2_per_objfile *per_objfile);
3806
3807 static void
3808 dw2_expand_symtabs_matching_one
3809 (dwarf2_per_cu_data *per_cu,
3810 dwarf2_per_objfile *per_objfile,
3811 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3812 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3813
3814 static void
3815 dw2_map_matching_symbols
3816 (struct objfile *objfile,
3817 const lookup_name_info &name, domain_enum domain,
3818 int global,
3819 gdb::function_view<symbol_found_callback_ftype> callback,
3820 symbol_compare_ftype *ordered_compare)
3821 {
3822 /* Used for Ada. */
3823 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3824
3825 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3826
3827 if (per_objfile->per_bfd->index_table != nullptr)
3828 {
3829 mapped_index &index = *per_objfile->per_bfd->index_table;
3830
3831 const char *match_name = name.ada ().lookup_name ().c_str ();
3832 auto matcher = [&] (const char *symname)
3833 {
3834 if (ordered_compare == nullptr)
3835 return true;
3836 return ordered_compare (symname, match_name) == 0;
3837 };
3838
3839 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3840 [&] (offset_type namei)
3841 {
3842 struct dw2_symtab_iterator iter;
3843 struct dwarf2_per_cu_data *per_cu;
3844
3845 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3846 namei);
3847 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3848 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3849 nullptr);
3850 return true;
3851 }, per_objfile);
3852 }
3853 else
3854 {
3855 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3856 proceed assuming all symtabs have been read in. */
3857 }
3858
3859 for (compunit_symtab *cust : objfile->compunits ())
3860 {
3861 const struct block *block;
3862
3863 if (cust == NULL)
3864 continue;
3865 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3866 if (!iterate_over_symbols_terminated (block, name,
3867 domain, callback))
3868 return;
3869 }
3870 }
3871
3872 void
3873 dwarf2_gdb_index::map_matching_symbols
3874 (struct objfile *objfile,
3875 const lookup_name_info &name, domain_enum domain,
3876 int global,
3877 gdb::function_view<symbol_found_callback_ftype> callback,
3878 symbol_compare_ftype *ordered_compare)
3879 {
3880 dw2_map_matching_symbols (objfile, name, domain, global, callback,
3881 ordered_compare);
3882 }
3883
3884 /* Starting from a search name, return the string that finds the upper
3885 bound of all strings that start with SEARCH_NAME in a sorted name
3886 list. Returns the empty string to indicate that the upper bound is
3887 the end of the list. */
3888
3889 static std::string
3890 make_sort_after_prefix_name (const char *search_name)
3891 {
3892 /* When looking to complete "func", we find the upper bound of all
3893 symbols that start with "func" by looking for where we'd insert
3894 the closest string that would follow "func" in lexicographical
3895 order. Usually, that's "func"-with-last-character-incremented,
3896 i.e. "fund". Mind non-ASCII characters, though. Usually those
3897 will be UTF-8 multi-byte sequences, but we can't be certain.
3898 Especially mind the 0xff character, which is a valid character in
3899 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3900 rule out compilers allowing it in identifiers. Note that
3901 conveniently, strcmp/strcasecmp are specified to compare
3902 characters interpreted as unsigned char. So what we do is treat
3903 the whole string as a base 256 number composed of a sequence of
3904 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3905 to 0, and carries 1 to the following more-significant position.
3906 If the very first character in SEARCH_NAME ends up incremented
3907 and carries/overflows, then the upper bound is the end of the
3908 list. The string after the empty string is also the empty
3909 string.
3910
3911 Some examples of this operation:
3912
3913 SEARCH_NAME => "+1" RESULT
3914
3915 "abc" => "abd"
3916 "ab\xff" => "ac"
3917 "\xff" "a" "\xff" => "\xff" "b"
3918 "\xff" => ""
3919 "\xff\xff" => ""
3920 "" => ""
3921
3922 Then, with these symbols for example:
3923
3924 func
3925 func1
3926 fund
3927
3928 completing "func" looks for symbols between "func" and
3929 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3930 which finds "func" and "func1", but not "fund".
3931
3932 And with:
3933
3934 funcÿ (Latin1 'ÿ' [0xff])
3935 funcÿ1
3936 fund
3937
3938 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3939 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3940
3941 And with:
3942
3943 ÿÿ (Latin1 'ÿ' [0xff])
3944 ÿÿ1
3945
3946 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3947 the end of the list.
3948 */
3949 std::string after = search_name;
3950 while (!after.empty () && (unsigned char) after.back () == 0xff)
3951 after.pop_back ();
3952 if (!after.empty ())
3953 after.back () = (unsigned char) after.back () + 1;
3954 return after;
3955 }
3956
3957 /* See declaration. */
3958
3959 std::pair<std::vector<name_component>::const_iterator,
3960 std::vector<name_component>::const_iterator>
3961 mapped_index_base::find_name_components_bounds
3962 (const lookup_name_info &lookup_name_without_params, language lang,
3963 dwarf2_per_objfile *per_objfile) const
3964 {
3965 auto *name_cmp
3966 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3967
3968 const char *lang_name
3969 = lookup_name_without_params.language_lookup_name (lang);
3970
3971 /* Comparison function object for lower_bound that matches against a
3972 given symbol name. */
3973 auto lookup_compare_lower = [&] (const name_component &elem,
3974 const char *name)
3975 {
3976 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3977 const char *elem_name = elem_qualified + elem.name_offset;
3978 return name_cmp (elem_name, name) < 0;
3979 };
3980
3981 /* Comparison function object for upper_bound that matches against a
3982 given symbol name. */
3983 auto lookup_compare_upper = [&] (const char *name,
3984 const name_component &elem)
3985 {
3986 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3987 const char *elem_name = elem_qualified + elem.name_offset;
3988 return name_cmp (name, elem_name) < 0;
3989 };
3990
3991 auto begin = this->name_components.begin ();
3992 auto end = this->name_components.end ();
3993
3994 /* Find the lower bound. */
3995 auto lower = [&] ()
3996 {
3997 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3998 return begin;
3999 else
4000 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
4001 } ();
4002
4003 /* Find the upper bound. */
4004 auto upper = [&] ()
4005 {
4006 if (lookup_name_without_params.completion_mode ())
4007 {
4008 /* In completion mode, we want UPPER to point past all
4009 symbols names that have the same prefix. I.e., with
4010 these symbols, and completing "func":
4011
4012 function << lower bound
4013 function1
4014 other_function << upper bound
4015
4016 We find the upper bound by looking for the insertion
4017 point of "func"-with-last-character-incremented,
4018 i.e. "fund". */
4019 std::string after = make_sort_after_prefix_name (lang_name);
4020 if (after.empty ())
4021 return end;
4022 return std::lower_bound (lower, end, after.c_str (),
4023 lookup_compare_lower);
4024 }
4025 else
4026 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4027 } ();
4028
4029 return {lower, upper};
4030 }
4031
4032 /* See declaration. */
4033
4034 void
4035 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
4036 {
4037 if (!this->name_components.empty ())
4038 return;
4039
4040 this->name_components_casing = case_sensitivity;
4041 auto *name_cmp
4042 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4043
4044 /* The code below only knows how to break apart components of C++
4045 symbol names (and other languages that use '::' as
4046 namespace/module separator) and Ada symbol names. */
4047 auto count = this->symbol_name_count ();
4048 for (offset_type idx = 0; idx < count; idx++)
4049 {
4050 if (this->symbol_name_slot_invalid (idx))
4051 continue;
4052
4053 const char *name = this->symbol_name_at (idx, per_objfile);
4054
4055 /* Add each name component to the name component table. */
4056 unsigned int previous_len = 0;
4057
4058 if (strstr (name, "::") != nullptr)
4059 {
4060 for (unsigned int current_len = cp_find_first_component (name);
4061 name[current_len] != '\0';
4062 current_len += cp_find_first_component (name + current_len))
4063 {
4064 gdb_assert (name[current_len] == ':');
4065 this->name_components.push_back ({previous_len, idx});
4066 /* Skip the '::'. */
4067 current_len += 2;
4068 previous_len = current_len;
4069 }
4070 }
4071 else
4072 {
4073 /* Handle the Ada encoded (aka mangled) form here. */
4074 for (const char *iter = strstr (name, "__");
4075 iter != nullptr;
4076 iter = strstr (iter, "__"))
4077 {
4078 this->name_components.push_back ({previous_len, idx});
4079 iter += 2;
4080 previous_len = iter - name;
4081 }
4082 }
4083
4084 this->name_components.push_back ({previous_len, idx});
4085 }
4086
4087 /* Sort name_components elements by name. */
4088 auto name_comp_compare = [&] (const name_component &left,
4089 const name_component &right)
4090 {
4091 const char *left_qualified
4092 = this->symbol_name_at (left.idx, per_objfile);
4093 const char *right_qualified
4094 = this->symbol_name_at (right.idx, per_objfile);
4095
4096 const char *left_name = left_qualified + left.name_offset;
4097 const char *right_name = right_qualified + right.name_offset;
4098
4099 return name_cmp (left_name, right_name) < 0;
4100 };
4101
4102 std::sort (this->name_components.begin (),
4103 this->name_components.end (),
4104 name_comp_compare);
4105 }
4106
4107 /* Helper for dw2_expand_symtabs_matching that works with a
4108 mapped_index_base instead of the containing objfile. This is split
4109 to a separate function in order to be able to unit test the
4110 name_components matching using a mock mapped_index_base. For each
4111 symbol name that matches, calls MATCH_CALLBACK, passing it the
4112 symbol's index in the mapped_index_base symbol table. */
4113
4114 static void
4115 dw2_expand_symtabs_matching_symbol
4116 (mapped_index_base &index,
4117 const lookup_name_info &lookup_name_in,
4118 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4119 enum search_domain kind,
4120 gdb::function_view<bool (offset_type)> match_callback,
4121 dwarf2_per_objfile *per_objfile)
4122 {
4123 lookup_name_info lookup_name_without_params
4124 = lookup_name_in.make_ignore_params ();
4125
4126 /* Build the symbol name component sorted vector, if we haven't
4127 yet. */
4128 index.build_name_components (per_objfile);
4129
4130 /* The same symbol may appear more than once in the range though.
4131 E.g., if we're looking for symbols that complete "w", and we have
4132 a symbol named "w1::w2", we'll find the two name components for
4133 that same symbol in the range. To be sure we only call the
4134 callback once per symbol, we first collect the symbol name
4135 indexes that matched in a temporary vector and ignore
4136 duplicates. */
4137 std::vector<offset_type> matches;
4138
4139 struct name_and_matcher
4140 {
4141 symbol_name_matcher_ftype *matcher;
4142 const char *name;
4143
4144 bool operator== (const name_and_matcher &other) const
4145 {
4146 return matcher == other.matcher && strcmp (name, other.name) == 0;
4147 }
4148 };
4149
4150 /* A vector holding all the different symbol name matchers, for all
4151 languages. */
4152 std::vector<name_and_matcher> matchers;
4153
4154 for (int i = 0; i < nr_languages; i++)
4155 {
4156 enum language lang_e = (enum language) i;
4157
4158 const language_defn *lang = language_def (lang_e);
4159 symbol_name_matcher_ftype *name_matcher
4160 = lang->get_symbol_name_matcher (lookup_name_without_params);
4161
4162 name_and_matcher key {
4163 name_matcher,
4164 lookup_name_without_params.language_lookup_name (lang_e)
4165 };
4166
4167 /* Don't insert the same comparison routine more than once.
4168 Note that we do this linear walk. This is not a problem in
4169 practice because the number of supported languages is
4170 low. */
4171 if (std::find (matchers.begin (), matchers.end (), key)
4172 != matchers.end ())
4173 continue;
4174 matchers.push_back (std::move (key));
4175
4176 auto bounds
4177 = index.find_name_components_bounds (lookup_name_without_params,
4178 lang_e, per_objfile);
4179
4180 /* Now for each symbol name in range, check to see if we have a name
4181 match, and if so, call the MATCH_CALLBACK callback. */
4182
4183 for (; bounds.first != bounds.second; ++bounds.first)
4184 {
4185 const char *qualified
4186 = index.symbol_name_at (bounds.first->idx, per_objfile);
4187
4188 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4189 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4190 continue;
4191
4192 matches.push_back (bounds.first->idx);
4193 }
4194 }
4195
4196 std::sort (matches.begin (), matches.end ());
4197
4198 /* Finally call the callback, once per match. */
4199 ULONGEST prev = -1;
4200 for (offset_type idx : matches)
4201 {
4202 if (prev != idx)
4203 {
4204 if (!match_callback (idx))
4205 break;
4206 prev = idx;
4207 }
4208 }
4209
4210 /* Above we use a type wider than idx's for 'prev', since 0 and
4211 (offset_type)-1 are both possible values. */
4212 static_assert (sizeof (prev) > sizeof (offset_type), "");
4213 }
4214
4215 #if GDB_SELF_TEST
4216
4217 namespace selftests { namespace dw2_expand_symtabs_matching {
4218
4219 /* A mock .gdb_index/.debug_names-like name index table, enough to
4220 exercise dw2_expand_symtabs_matching_symbol, which works with the
4221 mapped_index_base interface. Builds an index from the symbol list
4222 passed as parameter to the constructor. */
4223 class mock_mapped_index : public mapped_index_base
4224 {
4225 public:
4226 mock_mapped_index (gdb::array_view<const char *> symbols)
4227 : m_symbol_table (symbols)
4228 {}
4229
4230 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4231
4232 /* Return the number of names in the symbol table. */
4233 size_t symbol_name_count () const override
4234 {
4235 return m_symbol_table.size ();
4236 }
4237
4238 /* Get the name of the symbol at IDX in the symbol table. */
4239 const char *symbol_name_at
4240 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
4241 {
4242 return m_symbol_table[idx];
4243 }
4244
4245 private:
4246 gdb::array_view<const char *> m_symbol_table;
4247 };
4248
4249 /* Convenience function that converts a NULL pointer to a "<null>"
4250 string, to pass to print routines. */
4251
4252 static const char *
4253 string_or_null (const char *str)
4254 {
4255 return str != NULL ? str : "<null>";
4256 }
4257
4258 /* Check if a lookup_name_info built from
4259 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4260 index. EXPECTED_LIST is the list of expected matches, in expected
4261 matching order. If no match expected, then an empty list is
4262 specified. Returns true on success. On failure prints a warning
4263 indicating the file:line that failed, and returns false. */
4264
4265 static bool
4266 check_match (const char *file, int line,
4267 mock_mapped_index &mock_index,
4268 const char *name, symbol_name_match_type match_type,
4269 bool completion_mode,
4270 std::initializer_list<const char *> expected_list,
4271 dwarf2_per_objfile *per_objfile)
4272 {
4273 lookup_name_info lookup_name (name, match_type, completion_mode);
4274
4275 bool matched = true;
4276
4277 auto mismatch = [&] (const char *expected_str,
4278 const char *got)
4279 {
4280 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4281 "expected=\"%s\", got=\"%s\"\n"),
4282 file, line,
4283 (match_type == symbol_name_match_type::FULL
4284 ? "FULL" : "WILD"),
4285 name, string_or_null (expected_str), string_or_null (got));
4286 matched = false;
4287 };
4288
4289 auto expected_it = expected_list.begin ();
4290 auto expected_end = expected_list.end ();
4291
4292 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4293 NULL, ALL_DOMAIN,
4294 [&] (offset_type idx)
4295 {
4296 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
4297 const char *expected_str
4298 = expected_it == expected_end ? NULL : *expected_it++;
4299
4300 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4301 mismatch (expected_str, matched_name);
4302 return true;
4303 }, per_objfile);
4304
4305 const char *expected_str
4306 = expected_it == expected_end ? NULL : *expected_it++;
4307 if (expected_str != NULL)
4308 mismatch (expected_str, NULL);
4309
4310 return matched;
4311 }
4312
4313 /* The symbols added to the mock mapped_index for testing (in
4314 canonical form). */
4315 static const char *test_symbols[] = {
4316 "function",
4317 "std::bar",
4318 "std::zfunction",
4319 "std::zfunction2",
4320 "w1::w2",
4321 "ns::foo<char*>",
4322 "ns::foo<int>",
4323 "ns::foo<long>",
4324 "ns2::tmpl<int>::foo2",
4325 "(anonymous namespace)::A::B::C",
4326
4327 /* These are used to check that the increment-last-char in the
4328 matching algorithm for completion doesn't match "t1_fund" when
4329 completing "t1_func". */
4330 "t1_func",
4331 "t1_func1",
4332 "t1_fund",
4333 "t1_fund1",
4334
4335 /* A UTF-8 name with multi-byte sequences to make sure that
4336 cp-name-parser understands this as a single identifier ("função"
4337 is "function" in PT). */
4338 u8"u8função",
4339
4340 /* \377 (0xff) is Latin1 'ÿ'. */
4341 "yfunc\377",
4342
4343 /* \377 (0xff) is Latin1 'ÿ'. */
4344 "\377",
4345 "\377\377123",
4346
4347 /* A name with all sorts of complications. Starts with "z" to make
4348 it easier for the completion tests below. */
4349 #define Z_SYM_NAME \
4350 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4351 "::tuple<(anonymous namespace)::ui*, " \
4352 "std::default_delete<(anonymous namespace)::ui>, void>"
4353
4354 Z_SYM_NAME
4355 };
4356
4357 /* Returns true if the mapped_index_base::find_name_component_bounds
4358 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4359 in completion mode. */
4360
4361 static bool
4362 check_find_bounds_finds (mapped_index_base &index,
4363 const char *search_name,
4364 gdb::array_view<const char *> expected_syms,
4365 dwarf2_per_objfile *per_objfile)
4366 {
4367 lookup_name_info lookup_name (search_name,
4368 symbol_name_match_type::FULL, true);
4369
4370 auto bounds = index.find_name_components_bounds (lookup_name,
4371 language_cplus,
4372 per_objfile);
4373
4374 size_t distance = std::distance (bounds.first, bounds.second);
4375 if (distance != expected_syms.size ())
4376 return false;
4377
4378 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4379 {
4380 auto nc_elem = bounds.first + exp_elem;
4381 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
4382 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4383 return false;
4384 }
4385
4386 return true;
4387 }
4388
4389 /* Test the lower-level mapped_index::find_name_component_bounds
4390 method. */
4391
4392 static void
4393 test_mapped_index_find_name_component_bounds ()
4394 {
4395 mock_mapped_index mock_index (test_symbols);
4396
4397 mock_index.build_name_components (NULL /* per_objfile */);
4398
4399 /* Test the lower-level mapped_index::find_name_component_bounds
4400 method in completion mode. */
4401 {
4402 static const char *expected_syms[] = {
4403 "t1_func",
4404 "t1_func1",
4405 };
4406
4407 SELF_CHECK (check_find_bounds_finds
4408 (mock_index, "t1_func", expected_syms,
4409 NULL /* per_objfile */));
4410 }
4411
4412 /* Check that the increment-last-char in the name matching algorithm
4413 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4414 {
4415 static const char *expected_syms1[] = {
4416 "\377",
4417 "\377\377123",
4418 };
4419 SELF_CHECK (check_find_bounds_finds
4420 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
4421
4422 static const char *expected_syms2[] = {
4423 "\377\377123",
4424 };
4425 SELF_CHECK (check_find_bounds_finds
4426 (mock_index, "\377\377", expected_syms2,
4427 NULL /* per_objfile */));
4428 }
4429 }
4430
4431 /* Test dw2_expand_symtabs_matching_symbol. */
4432
4433 static void
4434 test_dw2_expand_symtabs_matching_symbol ()
4435 {
4436 mock_mapped_index mock_index (test_symbols);
4437
4438 /* We let all tests run until the end even if some fails, for debug
4439 convenience. */
4440 bool any_mismatch = false;
4441
4442 /* Create the expected symbols list (an initializer_list). Needed
4443 because lists have commas, and we need to pass them to CHECK,
4444 which is a macro. */
4445 #define EXPECT(...) { __VA_ARGS__ }
4446
4447 /* Wrapper for check_match that passes down the current
4448 __FILE__/__LINE__. */
4449 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4450 any_mismatch |= !check_match (__FILE__, __LINE__, \
4451 mock_index, \
4452 NAME, MATCH_TYPE, COMPLETION_MODE, \
4453 EXPECTED_LIST, NULL)
4454
4455 /* Identity checks. */
4456 for (const char *sym : test_symbols)
4457 {
4458 /* Should be able to match all existing symbols. */
4459 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4460 EXPECT (sym));
4461
4462 /* Should be able to match all existing symbols with
4463 parameters. */
4464 std::string with_params = std::string (sym) + "(int)";
4465 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4466 EXPECT (sym));
4467
4468 /* Should be able to match all existing symbols with
4469 parameters and qualifiers. */
4470 with_params = std::string (sym) + " ( int ) const";
4471 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4472 EXPECT (sym));
4473
4474 /* This should really find sym, but cp-name-parser.y doesn't
4475 know about lvalue/rvalue qualifiers yet. */
4476 with_params = std::string (sym) + " ( int ) &&";
4477 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4478 {});
4479 }
4480
4481 /* Check that the name matching algorithm for completion doesn't get
4482 confused with Latin1 'ÿ' / 0xff. */
4483 {
4484 static const char str[] = "\377";
4485 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4486 EXPECT ("\377", "\377\377123"));
4487 }
4488
4489 /* Check that the increment-last-char in the matching algorithm for
4490 completion doesn't match "t1_fund" when completing "t1_func". */
4491 {
4492 static const char str[] = "t1_func";
4493 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4494 EXPECT ("t1_func", "t1_func1"));
4495 }
4496
4497 /* Check that completion mode works at each prefix of the expected
4498 symbol name. */
4499 {
4500 static const char str[] = "function(int)";
4501 size_t len = strlen (str);
4502 std::string lookup;
4503
4504 for (size_t i = 1; i < len; i++)
4505 {
4506 lookup.assign (str, i);
4507 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4508 EXPECT ("function"));
4509 }
4510 }
4511
4512 /* While "w" is a prefix of both components, the match function
4513 should still only be called once. */
4514 {
4515 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4516 EXPECT ("w1::w2"));
4517 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4518 EXPECT ("w1::w2"));
4519 }
4520
4521 /* Same, with a "complicated" symbol. */
4522 {
4523 static const char str[] = Z_SYM_NAME;
4524 size_t len = strlen (str);
4525 std::string lookup;
4526
4527 for (size_t i = 1; i < len; i++)
4528 {
4529 lookup.assign (str, i);
4530 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4531 EXPECT (Z_SYM_NAME));
4532 }
4533 }
4534
4535 /* In FULL mode, an incomplete symbol doesn't match. */
4536 {
4537 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4538 {});
4539 }
4540
4541 /* A complete symbol with parameters matches any overload, since the
4542 index has no overload info. */
4543 {
4544 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4545 EXPECT ("std::zfunction", "std::zfunction2"));
4546 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4547 EXPECT ("std::zfunction", "std::zfunction2"));
4548 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4549 EXPECT ("std::zfunction", "std::zfunction2"));
4550 }
4551
4552 /* Check that whitespace is ignored appropriately. A symbol with a
4553 template argument list. */
4554 {
4555 static const char expected[] = "ns::foo<int>";
4556 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4557 EXPECT (expected));
4558 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4559 EXPECT (expected));
4560 }
4561
4562 /* Check that whitespace is ignored appropriately. A symbol with a
4563 template argument list that includes a pointer. */
4564 {
4565 static const char expected[] = "ns::foo<char*>";
4566 /* Try both completion and non-completion modes. */
4567 static const bool completion_mode[2] = {false, true};
4568 for (size_t i = 0; i < 2; i++)
4569 {
4570 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4571 completion_mode[i], EXPECT (expected));
4572 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4573 completion_mode[i], EXPECT (expected));
4574
4575 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4576 completion_mode[i], EXPECT (expected));
4577 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4578 completion_mode[i], EXPECT (expected));
4579 }
4580 }
4581
4582 {
4583 /* Check method qualifiers are ignored. */
4584 static const char expected[] = "ns::foo<char*>";
4585 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4586 symbol_name_match_type::FULL, true, EXPECT (expected));
4587 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4588 symbol_name_match_type::FULL, true, EXPECT (expected));
4589 CHECK_MATCH ("foo < char * > ( int ) const",
4590 symbol_name_match_type::WILD, true, EXPECT (expected));
4591 CHECK_MATCH ("foo < char * > ( int ) &&",
4592 symbol_name_match_type::WILD, true, EXPECT (expected));
4593 }
4594
4595 /* Test lookup names that don't match anything. */
4596 {
4597 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4598 {});
4599
4600 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4601 {});
4602 }
4603
4604 /* Some wild matching tests, exercising "(anonymous namespace)",
4605 which should not be confused with a parameter list. */
4606 {
4607 static const char *syms[] = {
4608 "A::B::C",
4609 "B::C",
4610 "C",
4611 "A :: B :: C ( int )",
4612 "B :: C ( int )",
4613 "C ( int )",
4614 };
4615
4616 for (const char *s : syms)
4617 {
4618 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4619 EXPECT ("(anonymous namespace)::A::B::C"));
4620 }
4621 }
4622
4623 {
4624 static const char expected[] = "ns2::tmpl<int>::foo2";
4625 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4626 EXPECT (expected));
4627 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4628 EXPECT (expected));
4629 }
4630
4631 SELF_CHECK (!any_mismatch);
4632
4633 #undef EXPECT
4634 #undef CHECK_MATCH
4635 }
4636
4637 static void
4638 run_test ()
4639 {
4640 test_mapped_index_find_name_component_bounds ();
4641 test_dw2_expand_symtabs_matching_symbol ();
4642 }
4643
4644 }} // namespace selftests::dw2_expand_symtabs_matching
4645
4646 #endif /* GDB_SELF_TEST */
4647
4648 /* If FILE_MATCHER is NULL or if PER_CU has
4649 dwarf2_per_cu_quick_data::MARK set (see
4650 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4651 EXPANSION_NOTIFY on it. */
4652
4653 static void
4654 dw2_expand_symtabs_matching_one
4655 (dwarf2_per_cu_data *per_cu,
4656 dwarf2_per_objfile *per_objfile,
4657 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4658 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4659 {
4660 if (file_matcher == NULL || per_cu->v.quick->mark)
4661 {
4662 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4663
4664 compunit_symtab *symtab
4665 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4666 gdb_assert (symtab != nullptr);
4667
4668 if (expansion_notify != NULL && symtab_was_null)
4669 expansion_notify (symtab);
4670 }
4671 }
4672
4673 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4674 matched, to expand corresponding CUs that were marked. IDX is the
4675 index of the symbol name that matched. */
4676
4677 static void
4678 dw2_expand_marked_cus
4679 (dwarf2_per_objfile *per_objfile, offset_type idx,
4680 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4681 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4682 search_domain kind)
4683 {
4684 offset_type *vec, vec_len, vec_idx;
4685 bool global_seen = false;
4686 mapped_index &index = *per_objfile->per_bfd->index_table;
4687
4688 vec = (offset_type *) (index.constant_pool
4689 + MAYBE_SWAP (index.symbol_table[idx].vec));
4690 vec_len = MAYBE_SWAP (vec[0]);
4691 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4692 {
4693 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4694 /* This value is only valid for index versions >= 7. */
4695 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4696 gdb_index_symbol_kind symbol_kind =
4697 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4698 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4699 /* Only check the symbol attributes if they're present.
4700 Indices prior to version 7 don't record them,
4701 and indices >= 7 may elide them for certain symbols
4702 (gold does this). */
4703 int attrs_valid =
4704 (index.version >= 7
4705 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4706
4707 /* Work around gold/15646. */
4708 if (attrs_valid
4709 && !is_static
4710 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4711 {
4712 if (global_seen)
4713 continue;
4714
4715 global_seen = true;
4716 }
4717
4718 /* Only check the symbol's kind if it has one. */
4719 if (attrs_valid)
4720 {
4721 switch (kind)
4722 {
4723 case VARIABLES_DOMAIN:
4724 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4725 continue;
4726 break;
4727 case FUNCTIONS_DOMAIN:
4728 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4729 continue;
4730 break;
4731 case TYPES_DOMAIN:
4732 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4733 continue;
4734 break;
4735 case MODULES_DOMAIN:
4736 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4737 continue;
4738 break;
4739 default:
4740 break;
4741 }
4742 }
4743
4744 /* Don't crash on bad data. */
4745 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4746 + per_objfile->per_bfd->all_type_units.size ()))
4747 {
4748 complaint (_(".gdb_index entry has bad CU index"
4749 " [in module %s]"), objfile_name (per_objfile->objfile));
4750 continue;
4751 }
4752
4753 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4754 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4755 expansion_notify);
4756 }
4757 }
4758
4759 /* If FILE_MATCHER is non-NULL, set all the
4760 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4761 that match FILE_MATCHER. */
4762
4763 static void
4764 dw_expand_symtabs_matching_file_matcher
4765 (dwarf2_per_objfile *per_objfile,
4766 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4767 {
4768 if (file_matcher == NULL)
4769 return;
4770
4771 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4772 htab_eq_pointer,
4773 NULL, xcalloc, xfree));
4774 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4775 htab_eq_pointer,
4776 NULL, xcalloc, xfree));
4777
4778 /* The rule is CUs specify all the files, including those used by
4779 any TU, so there's no need to scan TUs here. */
4780
4781 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4782 {
4783 QUIT;
4784
4785 per_cu->v.quick->mark = 0;
4786
4787 /* We only need to look at symtabs not already expanded. */
4788 if (per_objfile->symtab_set_p (per_cu))
4789 continue;
4790
4791 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4792 if (file_data == NULL)
4793 continue;
4794
4795 if (htab_find (visited_not_found.get (), file_data) != NULL)
4796 continue;
4797 else if (htab_find (visited_found.get (), file_data) != NULL)
4798 {
4799 per_cu->v.quick->mark = 1;
4800 continue;
4801 }
4802
4803 for (int j = 0; j < file_data->num_file_names; ++j)
4804 {
4805 const char *this_real_name;
4806
4807 if (file_matcher (file_data->file_names[j], false))
4808 {
4809 per_cu->v.quick->mark = 1;
4810 break;
4811 }
4812
4813 /* Before we invoke realpath, which can get expensive when many
4814 files are involved, do a quick comparison of the basenames. */
4815 if (!basenames_may_differ
4816 && !file_matcher (lbasename (file_data->file_names[j]),
4817 true))
4818 continue;
4819
4820 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4821 if (file_matcher (this_real_name, false))
4822 {
4823 per_cu->v.quick->mark = 1;
4824 break;
4825 }
4826 }
4827
4828 void **slot = htab_find_slot (per_cu->v.quick->mark
4829 ? visited_found.get ()
4830 : visited_not_found.get (),
4831 file_data, INSERT);
4832 *slot = file_data;
4833 }
4834 }
4835
4836 static void
4837 dw2_expand_symtabs_matching
4838 (struct objfile *objfile,
4839 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4840 const lookup_name_info *lookup_name,
4841 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4842 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4843 enum search_domain kind)
4844 {
4845 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4846
4847 /* index_table is NULL if OBJF_READNOW. */
4848 if (!per_objfile->per_bfd->index_table)
4849 return;
4850
4851 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4852
4853 if (symbol_matcher == NULL && lookup_name == NULL)
4854 {
4855 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4856 {
4857 QUIT;
4858
4859 dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4860 file_matcher, expansion_notify);
4861 }
4862 return;
4863 }
4864
4865 mapped_index &index = *per_objfile->per_bfd->index_table;
4866
4867 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4868 symbol_matcher,
4869 kind, [&] (offset_type idx)
4870 {
4871 dw2_expand_marked_cus (per_objfile, idx, file_matcher, expansion_notify,
4872 kind);
4873 return true;
4874 }, per_objfile);
4875 }
4876
4877 void
4878 dwarf2_gdb_index::expand_symtabs_matching
4879 (struct objfile *objfile,
4880 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4881 const lookup_name_info *lookup_name,
4882 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4883 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4884 enum search_domain kind)
4885 {
4886 dw2_expand_symtabs_matching (objfile, file_matcher, lookup_name,
4887 symbol_matcher, expansion_notify, kind);
4888 }
4889
4890 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4891 symtab. */
4892
4893 static struct compunit_symtab *
4894 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4895 CORE_ADDR pc)
4896 {
4897 int i;
4898
4899 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4900 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4901 return cust;
4902
4903 if (cust->includes == NULL)
4904 return NULL;
4905
4906 for (i = 0; cust->includes[i]; ++i)
4907 {
4908 struct compunit_symtab *s = cust->includes[i];
4909
4910 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4911 if (s != NULL)
4912 return s;
4913 }
4914
4915 return NULL;
4916 }
4917
4918 struct compunit_symtab *
4919 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4920 (struct objfile *objfile,
4921 struct bound_minimal_symbol msymbol,
4922 CORE_ADDR pc,
4923 struct obj_section *section,
4924 int warn_if_readin)
4925 {
4926 struct dwarf2_per_cu_data *data;
4927 struct compunit_symtab *result;
4928
4929 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4930 if (per_objfile->per_bfd->index_addrmap == nullptr)
4931 return NULL;
4932
4933 CORE_ADDR baseaddr = objfile->text_section_offset ();
4934 data = ((struct dwarf2_per_cu_data *)
4935 addrmap_find (per_objfile->per_bfd->index_addrmap,
4936 pc - baseaddr));
4937 if (!data)
4938 return NULL;
4939
4940 if (warn_if_readin && per_objfile->symtab_set_p (data))
4941 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4942 paddress (objfile->arch (), pc));
4943
4944 result = recursively_find_pc_sect_compunit_symtab
4945 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4946
4947 gdb_assert (result != NULL);
4948 return result;
4949 }
4950
4951 void
4952 dwarf2_base_index_functions::map_symbol_filenames (struct objfile *objfile,
4953 symbol_filename_ftype *fun,
4954 void *data,
4955 int need_fullname)
4956 {
4957 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4958
4959 if (!per_objfile->per_bfd->filenames_cache)
4960 {
4961 per_objfile->per_bfd->filenames_cache.emplace ();
4962
4963 htab_up visited (htab_create_alloc (10,
4964 htab_hash_pointer, htab_eq_pointer,
4965 NULL, xcalloc, xfree));
4966
4967 /* The rule is CUs specify all the files, including those used
4968 by any TU, so there's no need to scan TUs here. We can
4969 ignore file names coming from already-expanded CUs. */
4970
4971 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4972 {
4973 if (per_objfile->symtab_set_p (per_cu))
4974 {
4975 void **slot = htab_find_slot (visited.get (),
4976 per_cu->v.quick->file_names,
4977 INSERT);
4978
4979 *slot = per_cu->v.quick->file_names;
4980 }
4981 }
4982
4983 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4984 {
4985 /* We only need to look at symtabs not already expanded. */
4986 if (per_objfile->symtab_set_p (per_cu))
4987 continue;
4988
4989 quick_file_names *file_data
4990 = dw2_get_file_names (per_cu, per_objfile);
4991 if (file_data == NULL)
4992 continue;
4993
4994 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4995 if (*slot)
4996 {
4997 /* Already visited. */
4998 continue;
4999 }
5000 *slot = file_data;
5001
5002 for (int j = 0; j < file_data->num_file_names; ++j)
5003 {
5004 const char *filename = file_data->file_names[j];
5005 per_objfile->per_bfd->filenames_cache->seen (filename);
5006 }
5007 }
5008 }
5009
5010 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
5011 {
5012 gdb::unique_xmalloc_ptr<char> this_real_name;
5013
5014 if (need_fullname)
5015 this_real_name = gdb_realpath (filename);
5016 (*fun) (filename, this_real_name.get (), data);
5017 });
5018 }
5019
5020 bool
5021 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
5022 {
5023 return true;
5024 }
5025
5026 /* DWARF-5 debug_names reader. */
5027
5028 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5029 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5030
5031 /* A helper function that reads the .debug_names section in SECTION
5032 and fills in MAP. FILENAME is the name of the file containing the
5033 section; it is used for error reporting.
5034
5035 Returns true if all went well, false otherwise. */
5036
5037 static bool
5038 read_debug_names_from_section (struct objfile *objfile,
5039 const char *filename,
5040 struct dwarf2_section_info *section,
5041 mapped_debug_names &map)
5042 {
5043 if (section->empty ())
5044 return false;
5045
5046 /* Older elfutils strip versions could keep the section in the main
5047 executable while splitting it for the separate debug info file. */
5048 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5049 return false;
5050
5051 section->read (objfile);
5052
5053 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
5054
5055 const gdb_byte *addr = section->buffer;
5056
5057 bfd *const abfd = section->get_bfd_owner ();
5058
5059 unsigned int bytes_read;
5060 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5061 addr += bytes_read;
5062
5063 map.dwarf5_is_dwarf64 = bytes_read != 4;
5064 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5065 if (bytes_read + length != section->size)
5066 {
5067 /* There may be multiple per-CU indices. */
5068 warning (_("Section .debug_names in %s length %s does not match "
5069 "section length %s, ignoring .debug_names."),
5070 filename, plongest (bytes_read + length),
5071 pulongest (section->size));
5072 return false;
5073 }
5074
5075 /* The version number. */
5076 uint16_t version = read_2_bytes (abfd, addr);
5077 addr += 2;
5078 if (version != 5)
5079 {
5080 warning (_("Section .debug_names in %s has unsupported version %d, "
5081 "ignoring .debug_names."),
5082 filename, version);
5083 return false;
5084 }
5085
5086 /* Padding. */
5087 uint16_t padding = read_2_bytes (abfd, addr);
5088 addr += 2;
5089 if (padding != 0)
5090 {
5091 warning (_("Section .debug_names in %s has unsupported padding %d, "
5092 "ignoring .debug_names."),
5093 filename, padding);
5094 return false;
5095 }
5096
5097 /* comp_unit_count - The number of CUs in the CU list. */
5098 map.cu_count = read_4_bytes (abfd, addr);
5099 addr += 4;
5100
5101 /* local_type_unit_count - The number of TUs in the local TU
5102 list. */
5103 map.tu_count = read_4_bytes (abfd, addr);
5104 addr += 4;
5105
5106 /* foreign_type_unit_count - The number of TUs in the foreign TU
5107 list. */
5108 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5109 addr += 4;
5110 if (foreign_tu_count != 0)
5111 {
5112 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5113 "ignoring .debug_names."),
5114 filename, static_cast<unsigned long> (foreign_tu_count));
5115 return false;
5116 }
5117
5118 /* bucket_count - The number of hash buckets in the hash lookup
5119 table. */
5120 map.bucket_count = read_4_bytes (abfd, addr);
5121 addr += 4;
5122
5123 /* name_count - The number of unique names in the index. */
5124 map.name_count = read_4_bytes (abfd, addr);
5125 addr += 4;
5126
5127 /* abbrev_table_size - The size in bytes of the abbreviations
5128 table. */
5129 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5130 addr += 4;
5131
5132 /* augmentation_string_size - The size in bytes of the augmentation
5133 string. This value is rounded up to a multiple of 4. */
5134 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5135 addr += 4;
5136 map.augmentation_is_gdb = ((augmentation_string_size
5137 == sizeof (dwarf5_augmentation))
5138 && memcmp (addr, dwarf5_augmentation,
5139 sizeof (dwarf5_augmentation)) == 0);
5140 augmentation_string_size += (-augmentation_string_size) & 3;
5141 addr += augmentation_string_size;
5142
5143 /* List of CUs */
5144 map.cu_table_reordered = addr;
5145 addr += map.cu_count * map.offset_size;
5146
5147 /* List of Local TUs */
5148 map.tu_table_reordered = addr;
5149 addr += map.tu_count * map.offset_size;
5150
5151 /* Hash Lookup Table */
5152 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5153 addr += map.bucket_count * 4;
5154 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5155 addr += map.name_count * 4;
5156
5157 /* Name Table */
5158 map.name_table_string_offs_reordered = addr;
5159 addr += map.name_count * map.offset_size;
5160 map.name_table_entry_offs_reordered = addr;
5161 addr += map.name_count * map.offset_size;
5162
5163 const gdb_byte *abbrev_table_start = addr;
5164 for (;;)
5165 {
5166 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5167 addr += bytes_read;
5168 if (index_num == 0)
5169 break;
5170
5171 const auto insertpair
5172 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5173 if (!insertpair.second)
5174 {
5175 warning (_("Section .debug_names in %s has duplicate index %s, "
5176 "ignoring .debug_names."),
5177 filename, pulongest (index_num));
5178 return false;
5179 }
5180 mapped_debug_names::index_val &indexval = insertpair.first->second;
5181 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5182 addr += bytes_read;
5183
5184 for (;;)
5185 {
5186 mapped_debug_names::index_val::attr attr;
5187 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5188 addr += bytes_read;
5189 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5190 addr += bytes_read;
5191 if (attr.form == DW_FORM_implicit_const)
5192 {
5193 attr.implicit_const = read_signed_leb128 (abfd, addr,
5194 &bytes_read);
5195 addr += bytes_read;
5196 }
5197 if (attr.dw_idx == 0 && attr.form == 0)
5198 break;
5199 indexval.attr_vec.push_back (std::move (attr));
5200 }
5201 }
5202 if (addr != abbrev_table_start + abbrev_table_size)
5203 {
5204 warning (_("Section .debug_names in %s has abbreviation_table "
5205 "of size %s vs. written as %u, ignoring .debug_names."),
5206 filename, plongest (addr - abbrev_table_start),
5207 abbrev_table_size);
5208 return false;
5209 }
5210 map.entry_pool = addr;
5211
5212 return true;
5213 }
5214
5215 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5216 list. */
5217
5218 static void
5219 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
5220 const mapped_debug_names &map,
5221 dwarf2_section_info &section,
5222 bool is_dwz)
5223 {
5224 if (!map.augmentation_is_gdb)
5225 {
5226 for (uint32_t i = 0; i < map.cu_count; ++i)
5227 {
5228 sect_offset sect_off
5229 = (sect_offset) (extract_unsigned_integer
5230 (map.cu_table_reordered + i * map.offset_size,
5231 map.offset_size,
5232 map.dwarf5_byte_order));
5233 /* We don't know the length of the CU, because the CU list in a
5234 .debug_names index can be incomplete, so we can't use the start
5235 of the next CU as end of this CU. We create the CUs here with
5236 length 0, and in cutu_reader::cutu_reader we'll fill in the
5237 actual length. */
5238 dwarf2_per_cu_data *per_cu
5239 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5240 sect_off, 0);
5241 per_bfd->all_comp_units.push_back (per_cu);
5242 }
5243 return;
5244 }
5245
5246 sect_offset sect_off_prev;
5247 for (uint32_t i = 0; i <= map.cu_count; ++i)
5248 {
5249 sect_offset sect_off_next;
5250 if (i < map.cu_count)
5251 {
5252 sect_off_next
5253 = (sect_offset) (extract_unsigned_integer
5254 (map.cu_table_reordered + i * map.offset_size,
5255 map.offset_size,
5256 map.dwarf5_byte_order));
5257 }
5258 else
5259 sect_off_next = (sect_offset) section.size;
5260 if (i >= 1)
5261 {
5262 const ULONGEST length = sect_off_next - sect_off_prev;
5263 dwarf2_per_cu_data *per_cu
5264 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5265 sect_off_prev, length);
5266 per_bfd->all_comp_units.push_back (per_cu);
5267 }
5268 sect_off_prev = sect_off_next;
5269 }
5270 }
5271
5272 /* Read the CU list from the mapped index, and use it to create all
5273 the CU objects for this dwarf2_per_objfile. */
5274
5275 static void
5276 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
5277 const mapped_debug_names &map,
5278 const mapped_debug_names &dwz_map)
5279 {
5280 gdb_assert (per_bfd->all_comp_units.empty ());
5281 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5282
5283 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
5284 false /* is_dwz */);
5285
5286 if (dwz_map.cu_count == 0)
5287 return;
5288
5289 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5290 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
5291 true /* is_dwz */);
5292 }
5293
5294 /* Read .debug_names. If everything went ok, initialize the "quick"
5295 elements of all the CUs and return true. Otherwise, return false. */
5296
5297 static bool
5298 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
5299 {
5300 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5301 mapped_debug_names dwz_map;
5302 struct objfile *objfile = per_objfile->objfile;
5303 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5304
5305 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5306 &per_objfile->per_bfd->debug_names, *map))
5307 return false;
5308
5309 /* Don't use the index if it's empty. */
5310 if (map->name_count == 0)
5311 return false;
5312
5313 /* If there is a .dwz file, read it so we can get its CU list as
5314 well. */
5315 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5316 if (dwz != NULL)
5317 {
5318 if (!read_debug_names_from_section (objfile,
5319 bfd_get_filename (dwz->dwz_bfd.get ()),
5320 &dwz->debug_names, dwz_map))
5321 {
5322 warning (_("could not read '.debug_names' section from %s; skipping"),
5323 bfd_get_filename (dwz->dwz_bfd.get ()));
5324 return false;
5325 }
5326 }
5327
5328 create_cus_from_debug_names (per_bfd, *map, dwz_map);
5329
5330 if (map->tu_count != 0)
5331 {
5332 /* We can only handle a single .debug_types when we have an
5333 index. */
5334 if (per_bfd->types.size () != 1)
5335 return false;
5336
5337 dwarf2_section_info *section = &per_bfd->types[0];
5338
5339 create_signatured_type_table_from_debug_names
5340 (per_objfile, *map, section, &per_bfd->abbrev);
5341 }
5342
5343 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
5344
5345 per_bfd->debug_names_table = std::move (map);
5346 per_bfd->using_index = 1;
5347 per_bfd->quick_file_names_table =
5348 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
5349
5350 return true;
5351 }
5352
5353 /* Type used to manage iterating over all CUs looking for a symbol for
5354 .debug_names. */
5355
5356 class dw2_debug_names_iterator
5357 {
5358 public:
5359 dw2_debug_names_iterator (const mapped_debug_names &map,
5360 gdb::optional<block_enum> block_index,
5361 domain_enum domain,
5362 const char *name, dwarf2_per_objfile *per_objfile)
5363 : m_map (map), m_block_index (block_index), m_domain (domain),
5364 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5365 m_per_objfile (per_objfile)
5366 {}
5367
5368 dw2_debug_names_iterator (const mapped_debug_names &map,
5369 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
5370 : m_map (map),
5371 m_search (search),
5372 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5373 m_per_objfile (per_objfile)
5374 {}
5375
5376 dw2_debug_names_iterator (const mapped_debug_names &map,
5377 block_enum block_index, domain_enum domain,
5378 uint32_t namei, dwarf2_per_objfile *per_objfile)
5379 : m_map (map), m_block_index (block_index), m_domain (domain),
5380 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5381 m_per_objfile (per_objfile)
5382 {}
5383
5384 /* Return the next matching CU or NULL if there are no more. */
5385 dwarf2_per_cu_data *next ();
5386
5387 private:
5388 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5389 const char *name,
5390 dwarf2_per_objfile *per_objfile);
5391 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5392 uint32_t namei,
5393 dwarf2_per_objfile *per_objfile);
5394
5395 /* The internalized form of .debug_names. */
5396 const mapped_debug_names &m_map;
5397
5398 /* If set, only look for symbols that match that block. Valid values are
5399 GLOBAL_BLOCK and STATIC_BLOCK. */
5400 const gdb::optional<block_enum> m_block_index;
5401
5402 /* The kind of symbol we're looking for. */
5403 const domain_enum m_domain = UNDEF_DOMAIN;
5404 const search_domain m_search = ALL_DOMAIN;
5405
5406 /* The list of CUs from the index entry of the symbol, or NULL if
5407 not found. */
5408 const gdb_byte *m_addr;
5409
5410 dwarf2_per_objfile *m_per_objfile;
5411 };
5412
5413 const char *
5414 mapped_debug_names::namei_to_name
5415 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
5416 {
5417 const ULONGEST namei_string_offs
5418 = extract_unsigned_integer ((name_table_string_offs_reordered
5419 + namei * offset_size),
5420 offset_size,
5421 dwarf5_byte_order);
5422 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
5423 }
5424
5425 /* Find a slot in .debug_names for the object named NAME. If NAME is
5426 found, return pointer to its pool data. If NAME cannot be found,
5427 return NULL. */
5428
5429 const gdb_byte *
5430 dw2_debug_names_iterator::find_vec_in_debug_names
5431 (const mapped_debug_names &map, const char *name,
5432 dwarf2_per_objfile *per_objfile)
5433 {
5434 int (*cmp) (const char *, const char *);
5435
5436 gdb::unique_xmalloc_ptr<char> without_params;
5437 if (current_language->la_language == language_cplus
5438 || current_language->la_language == language_fortran
5439 || current_language->la_language == language_d)
5440 {
5441 /* NAME is already canonical. Drop any qualifiers as
5442 .debug_names does not contain any. */
5443
5444 if (strchr (name, '(') != NULL)
5445 {
5446 without_params = cp_remove_params (name);
5447 if (without_params != NULL)
5448 name = without_params.get ();
5449 }
5450 }
5451
5452 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5453
5454 const uint32_t full_hash = dwarf5_djb_hash (name);
5455 uint32_t namei
5456 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5457 (map.bucket_table_reordered
5458 + (full_hash % map.bucket_count)), 4,
5459 map.dwarf5_byte_order);
5460 if (namei == 0)
5461 return NULL;
5462 --namei;
5463 if (namei >= map.name_count)
5464 {
5465 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5466 "[in module %s]"),
5467 namei, map.name_count,
5468 objfile_name (per_objfile->objfile));
5469 return NULL;
5470 }
5471
5472 for (;;)
5473 {
5474 const uint32_t namei_full_hash
5475 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5476 (map.hash_table_reordered + namei), 4,
5477 map.dwarf5_byte_order);
5478 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5479 return NULL;
5480
5481 if (full_hash == namei_full_hash)
5482 {
5483 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5484
5485 #if 0 /* An expensive sanity check. */
5486 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5487 {
5488 complaint (_("Wrong .debug_names hash for string at index %u "
5489 "[in module %s]"),
5490 namei, objfile_name (dwarf2_per_objfile->objfile));
5491 return NULL;
5492 }
5493 #endif
5494
5495 if (cmp (namei_string, name) == 0)
5496 {
5497 const ULONGEST namei_entry_offs
5498 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5499 + namei * map.offset_size),
5500 map.offset_size, map.dwarf5_byte_order);
5501 return map.entry_pool + namei_entry_offs;
5502 }
5503 }
5504
5505 ++namei;
5506 if (namei >= map.name_count)
5507 return NULL;
5508 }
5509 }
5510
5511 const gdb_byte *
5512 dw2_debug_names_iterator::find_vec_in_debug_names
5513 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5514 {
5515 if (namei >= map.name_count)
5516 {
5517 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5518 "[in module %s]"),
5519 namei, map.name_count,
5520 objfile_name (per_objfile->objfile));
5521 return NULL;
5522 }
5523
5524 const ULONGEST namei_entry_offs
5525 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5526 + namei * map.offset_size),
5527 map.offset_size, map.dwarf5_byte_order);
5528 return map.entry_pool + namei_entry_offs;
5529 }
5530
5531 /* See dw2_debug_names_iterator. */
5532
5533 dwarf2_per_cu_data *
5534 dw2_debug_names_iterator::next ()
5535 {
5536 if (m_addr == NULL)
5537 return NULL;
5538
5539 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5540 struct objfile *objfile = m_per_objfile->objfile;
5541 bfd *const abfd = objfile->obfd;
5542
5543 again:
5544
5545 unsigned int bytes_read;
5546 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5547 m_addr += bytes_read;
5548 if (abbrev == 0)
5549 return NULL;
5550
5551 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5552 if (indexval_it == m_map.abbrev_map.cend ())
5553 {
5554 complaint (_("Wrong .debug_names undefined abbrev code %s "
5555 "[in module %s]"),
5556 pulongest (abbrev), objfile_name (objfile));
5557 return NULL;
5558 }
5559 const mapped_debug_names::index_val &indexval = indexval_it->second;
5560 enum class symbol_linkage {
5561 unknown,
5562 static_,
5563 extern_,
5564 } symbol_linkage_ = symbol_linkage::unknown;
5565 dwarf2_per_cu_data *per_cu = NULL;
5566 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5567 {
5568 ULONGEST ull;
5569 switch (attr.form)
5570 {
5571 case DW_FORM_implicit_const:
5572 ull = attr.implicit_const;
5573 break;
5574 case DW_FORM_flag_present:
5575 ull = 1;
5576 break;
5577 case DW_FORM_udata:
5578 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5579 m_addr += bytes_read;
5580 break;
5581 case DW_FORM_ref4:
5582 ull = read_4_bytes (abfd, m_addr);
5583 m_addr += 4;
5584 break;
5585 case DW_FORM_ref8:
5586 ull = read_8_bytes (abfd, m_addr);
5587 m_addr += 8;
5588 break;
5589 case DW_FORM_ref_sig8:
5590 ull = read_8_bytes (abfd, m_addr);
5591 m_addr += 8;
5592 break;
5593 default:
5594 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5595 dwarf_form_name (attr.form),
5596 objfile_name (objfile));
5597 return NULL;
5598 }
5599 switch (attr.dw_idx)
5600 {
5601 case DW_IDX_compile_unit:
5602 /* Don't crash on bad data. */
5603 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
5604 {
5605 complaint (_(".debug_names entry has bad CU index %s"
5606 " [in module %s]"),
5607 pulongest (ull),
5608 objfile_name (objfile));
5609 continue;
5610 }
5611 per_cu = per_bfd->get_cutu (ull);
5612 break;
5613 case DW_IDX_type_unit:
5614 /* Don't crash on bad data. */
5615 if (ull >= per_bfd->all_type_units.size ())
5616 {
5617 complaint (_(".debug_names entry has bad TU index %s"
5618 " [in module %s]"),
5619 pulongest (ull),
5620 objfile_name (objfile));
5621 continue;
5622 }
5623 per_cu = &per_bfd->get_tu (ull)->per_cu;
5624 break;
5625 case DW_IDX_die_offset:
5626 /* In a per-CU index (as opposed to a per-module index), index
5627 entries without CU attribute implicitly refer to the single CU. */
5628 if (per_cu == NULL)
5629 per_cu = per_bfd->get_cu (0);
5630 break;
5631 case DW_IDX_GNU_internal:
5632 if (!m_map.augmentation_is_gdb)
5633 break;
5634 symbol_linkage_ = symbol_linkage::static_;
5635 break;
5636 case DW_IDX_GNU_external:
5637 if (!m_map.augmentation_is_gdb)
5638 break;
5639 symbol_linkage_ = symbol_linkage::extern_;
5640 break;
5641 }
5642 }
5643
5644 /* Skip if already read in. */
5645 if (m_per_objfile->symtab_set_p (per_cu))
5646 goto again;
5647
5648 /* Check static vs global. */
5649 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5650 {
5651 const bool want_static = *m_block_index == STATIC_BLOCK;
5652 const bool symbol_is_static =
5653 symbol_linkage_ == symbol_linkage::static_;
5654 if (want_static != symbol_is_static)
5655 goto again;
5656 }
5657
5658 /* Match dw2_symtab_iter_next, symbol_kind
5659 and debug_names::psymbol_tag. */
5660 switch (m_domain)
5661 {
5662 case VAR_DOMAIN:
5663 switch (indexval.dwarf_tag)
5664 {
5665 case DW_TAG_variable:
5666 case DW_TAG_subprogram:
5667 /* Some types are also in VAR_DOMAIN. */
5668 case DW_TAG_typedef:
5669 case DW_TAG_structure_type:
5670 break;
5671 default:
5672 goto again;
5673 }
5674 break;
5675 case STRUCT_DOMAIN:
5676 switch (indexval.dwarf_tag)
5677 {
5678 case DW_TAG_typedef:
5679 case DW_TAG_structure_type:
5680 break;
5681 default:
5682 goto again;
5683 }
5684 break;
5685 case LABEL_DOMAIN:
5686 switch (indexval.dwarf_tag)
5687 {
5688 case 0:
5689 case DW_TAG_variable:
5690 break;
5691 default:
5692 goto again;
5693 }
5694 break;
5695 case MODULE_DOMAIN:
5696 switch (indexval.dwarf_tag)
5697 {
5698 case DW_TAG_module:
5699 break;
5700 default:
5701 goto again;
5702 }
5703 break;
5704 default:
5705 break;
5706 }
5707
5708 /* Match dw2_expand_symtabs_matching, symbol_kind and
5709 debug_names::psymbol_tag. */
5710 switch (m_search)
5711 {
5712 case VARIABLES_DOMAIN:
5713 switch (indexval.dwarf_tag)
5714 {
5715 case DW_TAG_variable:
5716 break;
5717 default:
5718 goto again;
5719 }
5720 break;
5721 case FUNCTIONS_DOMAIN:
5722 switch (indexval.dwarf_tag)
5723 {
5724 case DW_TAG_subprogram:
5725 break;
5726 default:
5727 goto again;
5728 }
5729 break;
5730 case TYPES_DOMAIN:
5731 switch (indexval.dwarf_tag)
5732 {
5733 case DW_TAG_typedef:
5734 case DW_TAG_structure_type:
5735 break;
5736 default:
5737 goto again;
5738 }
5739 break;
5740 case MODULES_DOMAIN:
5741 switch (indexval.dwarf_tag)
5742 {
5743 case DW_TAG_module:
5744 break;
5745 default:
5746 goto again;
5747 }
5748 default:
5749 break;
5750 }
5751
5752 return per_cu;
5753 }
5754
5755 struct compunit_symtab *
5756 dwarf2_debug_names_index::lookup_symbol
5757 (struct objfile *objfile, block_enum block_index,
5758 const char *name, domain_enum domain)
5759 {
5760 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5761
5762 const auto &mapp = per_objfile->per_bfd->debug_names_table;
5763 if (!mapp)
5764 {
5765 /* index is NULL if OBJF_READNOW. */
5766 return NULL;
5767 }
5768 const auto &map = *mapp;
5769
5770 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
5771
5772 struct compunit_symtab *stab_best = NULL;
5773 struct dwarf2_per_cu_data *per_cu;
5774 while ((per_cu = iter.next ()) != NULL)
5775 {
5776 struct symbol *sym, *with_opaque = NULL;
5777 compunit_symtab *stab
5778 = dw2_instantiate_symtab (per_cu, per_objfile, false);
5779 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5780 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5781
5782 sym = block_find_symbol (block, name, domain,
5783 block_find_non_opaque_type_preferred,
5784 &with_opaque);
5785
5786 /* Some caution must be observed with overloaded functions and
5787 methods, since the index will not contain any overload
5788 information (but NAME might contain it). */
5789
5790 if (sym != NULL
5791 && strcmp_iw (sym->search_name (), name) == 0)
5792 return stab;
5793 if (with_opaque != NULL
5794 && strcmp_iw (with_opaque->search_name (), name) == 0)
5795 stab_best = stab;
5796
5797 /* Keep looking through other CUs. */
5798 }
5799
5800 return stab_best;
5801 }
5802
5803 /* This dumps minimal information about .debug_names. It is called
5804 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5805 uses this to verify that .debug_names has been loaded. */
5806
5807 void
5808 dwarf2_debug_names_index::dump (struct objfile *objfile)
5809 {
5810 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5811
5812 gdb_assert (per_objfile->per_bfd->using_index);
5813 printf_filtered (".debug_names:");
5814 if (per_objfile->per_bfd->debug_names_table)
5815 printf_filtered (" exists\n");
5816 else
5817 printf_filtered (" faked for \"readnow\"\n");
5818 printf_filtered ("\n");
5819 }
5820
5821 void
5822 dwarf2_debug_names_index::expand_symtabs_for_function
5823 (struct objfile *objfile, const char *func_name)
5824 {
5825 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5826
5827 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5828 if (per_objfile->per_bfd->debug_names_table)
5829 {
5830 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5831
5832 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
5833 per_objfile);
5834
5835 struct dwarf2_per_cu_data *per_cu;
5836 while ((per_cu = iter.next ()) != NULL)
5837 dw2_instantiate_symtab (per_cu, per_objfile, false);
5838 }
5839 }
5840
5841 void
5842 dwarf2_debug_names_index::map_matching_symbols
5843 (struct objfile *objfile,
5844 const lookup_name_info &name, domain_enum domain,
5845 int global,
5846 gdb::function_view<symbol_found_callback_ftype> callback,
5847 symbol_compare_ftype *ordered_compare)
5848 {
5849 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5850
5851 /* debug_names_table is NULL if OBJF_READNOW. */
5852 if (!per_objfile->per_bfd->debug_names_table)
5853 return;
5854
5855 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5856 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5857
5858 const char *match_name = name.ada ().lookup_name ().c_str ();
5859 auto matcher = [&] (const char *symname)
5860 {
5861 if (ordered_compare == nullptr)
5862 return true;
5863 return ordered_compare (symname, match_name) == 0;
5864 };
5865
5866 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5867 [&] (offset_type namei)
5868 {
5869 /* The name was matched, now expand corresponding CUs that were
5870 marked. */
5871 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
5872 per_objfile);
5873
5874 struct dwarf2_per_cu_data *per_cu;
5875 while ((per_cu = iter.next ()) != NULL)
5876 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5877 nullptr);
5878 return true;
5879 }, per_objfile);
5880
5881 /* It's a shame we couldn't do this inside the
5882 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5883 that have already been expanded. Instead, this loop matches what
5884 the psymtab code does. */
5885 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5886 {
5887 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
5888 if (symtab != nullptr)
5889 {
5890 const struct block *block
5891 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5892 if (!iterate_over_symbols_terminated (block, name,
5893 domain, callback))
5894 break;
5895 }
5896 }
5897 }
5898
5899 void
5900 dwarf2_debug_names_index::expand_symtabs_matching
5901 (struct objfile *objfile,
5902 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5903 const lookup_name_info *lookup_name,
5904 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5905 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5906 enum search_domain kind)
5907 {
5908 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5909
5910 /* debug_names_table is NULL if OBJF_READNOW. */
5911 if (!per_objfile->per_bfd->debug_names_table)
5912 return;
5913
5914 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5915
5916 if (symbol_matcher == NULL && lookup_name == NULL)
5917 {
5918 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5919 {
5920 QUIT;
5921
5922 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5923 expansion_notify);
5924 }
5925 return;
5926 }
5927
5928 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5929
5930 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5931 symbol_matcher,
5932 kind, [&] (offset_type namei)
5933 {
5934 /* The name was matched, now expand corresponding CUs that were
5935 marked. */
5936 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
5937
5938 struct dwarf2_per_cu_data *per_cu;
5939 while ((per_cu = iter.next ()) != NULL)
5940 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5941 expansion_notify);
5942 return true;
5943 }, per_objfile);
5944 }
5945
5946 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5947 to either a dwarf2_per_bfd or dwz_file object. */
5948
5949 template <typename T>
5950 static gdb::array_view<const gdb_byte>
5951 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5952 {
5953 dwarf2_section_info *section = &section_owner->gdb_index;
5954
5955 if (section->empty ())
5956 return {};
5957
5958 /* Older elfutils strip versions could keep the section in the main
5959 executable while splitting it for the separate debug info file. */
5960 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5961 return {};
5962
5963 section->read (obj);
5964
5965 /* dwarf2_section_info::size is a bfd_size_type, while
5966 gdb::array_view works with size_t. On 32-bit hosts, with
5967 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5968 is 32-bit. So we need an explicit narrowing conversion here.
5969 This is fine, because it's impossible to allocate or mmap an
5970 array/buffer larger than what size_t can represent. */
5971 return gdb::make_array_view (section->buffer, section->size);
5972 }
5973
5974 /* Lookup the index cache for the contents of the index associated to
5975 DWARF2_OBJ. */
5976
5977 static gdb::array_view<const gdb_byte>
5978 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5979 {
5980 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5981 if (build_id == nullptr)
5982 return {};
5983
5984 return global_index_cache.lookup_gdb_index (build_id,
5985 &dwarf2_per_bfd->index_cache_res);
5986 }
5987
5988 /* Same as the above, but for DWZ. */
5989
5990 static gdb::array_view<const gdb_byte>
5991 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5992 {
5993 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5994 if (build_id == nullptr)
5995 return {};
5996
5997 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5998 }
5999
6000 /* See symfile.h. */
6001
6002 bool
6003 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6004 {
6005 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6006 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6007
6008 dwarf_read_debug_printf ("called");
6009
6010 /* If we're about to read full symbols, don't bother with the
6011 indices. In this case we also don't care if some other debug
6012 format is making psymtabs, because they are all about to be
6013 expanded anyway. */
6014 if ((objfile->flags & OBJF_READNOW))
6015 {
6016 dwarf_read_debug_printf ("readnow requested");
6017
6018 /* When using READNOW, the using_index flag (set below) indicates that
6019 PER_BFD was already initialized, when we loaded some other objfile. */
6020 if (per_bfd->using_index)
6021 {
6022 dwarf_read_debug_printf ("using_index already set");
6023 *index_kind = dw_index_kind::GDB_INDEX;
6024 per_objfile->resize_symtabs ();
6025 return true;
6026 }
6027
6028 per_bfd->using_index = 1;
6029 create_all_comp_units (per_objfile);
6030 create_all_type_units (per_objfile);
6031 per_bfd->quick_file_names_table
6032 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
6033 per_objfile->resize_symtabs ();
6034
6035 for (int i = 0; i < (per_bfd->all_comp_units.size ()
6036 + per_bfd->all_type_units.size ()); ++i)
6037 {
6038 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
6039
6040 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6041 struct dwarf2_per_cu_quick_data);
6042 }
6043
6044 /* Return 1 so that gdb sees the "quick" functions. However,
6045 these functions will be no-ops because we will have expanded
6046 all symtabs. */
6047 *index_kind = dw_index_kind::GDB_INDEX;
6048 return true;
6049 }
6050
6051 /* Was a debug names index already read when we processed an objfile sharing
6052 PER_BFD? */
6053 if (per_bfd->debug_names_table != nullptr)
6054 {
6055 dwarf_read_debug_printf ("re-using shared debug names table");
6056 *index_kind = dw_index_kind::DEBUG_NAMES;
6057 per_objfile->resize_symtabs ();
6058 return true;
6059 }
6060
6061 /* Was a GDB index already read when we processed an objfile sharing
6062 PER_BFD? */
6063 if (per_bfd->index_table != nullptr)
6064 {
6065 dwarf_read_debug_printf ("re-using shared index table");
6066 *index_kind = dw_index_kind::GDB_INDEX;
6067 per_objfile->resize_symtabs ();
6068 return true;
6069 }
6070
6071 /* There might already be partial symtabs built for this BFD. This happens
6072 when loading the same binary twice with the index-cache enabled. If so,
6073 don't try to read an index. The objfile / per_objfile initialization will
6074 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
6075 code path. */
6076 if (per_bfd->partial_symtabs != nullptr)
6077 {
6078 dwarf_read_debug_printf ("re-using shared partial symtabs");
6079 return false;
6080 }
6081
6082 if (dwarf2_read_debug_names (per_objfile))
6083 {
6084 dwarf_read_debug_printf ("found debug names");
6085 *index_kind = dw_index_kind::DEBUG_NAMES;
6086 per_objfile->resize_symtabs ();
6087 return true;
6088 }
6089
6090 if (dwarf2_read_gdb_index (per_objfile,
6091 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
6092 get_gdb_index_contents_from_section<dwz_file>))
6093 {
6094 dwarf_read_debug_printf ("found gdb index from file");
6095 *index_kind = dw_index_kind::GDB_INDEX;
6096 per_objfile->resize_symtabs ();
6097 return true;
6098 }
6099
6100 /* ... otherwise, try to find the index in the index cache. */
6101 if (dwarf2_read_gdb_index (per_objfile,
6102 get_gdb_index_contents_from_cache,
6103 get_gdb_index_contents_from_cache_dwz))
6104 {
6105 dwarf_read_debug_printf ("found gdb index from cache");
6106 global_index_cache.hit ();
6107 *index_kind = dw_index_kind::GDB_INDEX;
6108 per_objfile->resize_symtabs ();
6109 return true;
6110 }
6111
6112 global_index_cache.miss ();
6113 return false;
6114 }
6115
6116 \f
6117
6118 /* Build a partial symbol table. */
6119
6120 void
6121 dwarf2_build_psymtabs (struct objfile *objfile)
6122 {
6123 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6124 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6125
6126 if (per_bfd->partial_symtabs != nullptr)
6127 {
6128 /* Partial symbols were already read, so now we can simply
6129 attach them. */
6130 objfile->partial_symtabs = per_bfd->partial_symtabs;
6131 /* This is a temporary hack to ensure that the objfile and 'qf'
6132 psymtabs are identical. */
6133 psymbol_functions *psf
6134 = dynamic_cast<psymbol_functions *> (objfile->qf.front ().get ());
6135 gdb_assert (psf != nullptr);
6136 psf->set_partial_symtabs (per_bfd->partial_symtabs);
6137 per_objfile->resize_symtabs ();
6138 return;
6139 }
6140
6141 /* Set the local reference to partial symtabs, so that we don't try
6142 to read them again if reading another objfile with the same BFD.
6143 If we can't in fact share, this won't make a difference anyway as
6144 the dwarf2_per_bfd object won't be shared. */
6145 per_bfd->partial_symtabs = objfile->partial_symtabs;
6146
6147 try
6148 {
6149 /* This isn't really ideal: all the data we allocate on the
6150 objfile's obstack is still uselessly kept around. However,
6151 freeing it seems unsafe. */
6152 psymtab_discarder psymtabs (objfile->partial_symtabs.get ());
6153 dwarf2_build_psymtabs_hard (per_objfile);
6154 psymtabs.keep ();
6155
6156 per_objfile->resize_symtabs ();
6157
6158 /* (maybe) store an index in the cache. */
6159 global_index_cache.store (per_objfile);
6160 }
6161 catch (const gdb_exception_error &except)
6162 {
6163 exception_print (gdb_stderr, except);
6164 }
6165 }
6166
6167 /* Find the base address of the compilation unit for range lists and
6168 location lists. It will normally be specified by DW_AT_low_pc.
6169 In DWARF-3 draft 4, the base address could be overridden by
6170 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6171 compilation units with discontinuous ranges. */
6172
6173 static void
6174 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6175 {
6176 struct attribute *attr;
6177
6178 cu->base_address.reset ();
6179
6180 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6181 if (attr != nullptr)
6182 cu->base_address = attr->as_address ();
6183 else
6184 {
6185 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6186 if (attr != nullptr)
6187 cu->base_address = attr->as_address ();
6188 }
6189 }
6190
6191 /* Helper function that returns the proper abbrev section for
6192 THIS_CU. */
6193
6194 static struct dwarf2_section_info *
6195 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6196 {
6197 struct dwarf2_section_info *abbrev;
6198 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6199
6200 if (this_cu->is_dwz)
6201 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
6202 else
6203 abbrev = &per_bfd->abbrev;
6204
6205 return abbrev;
6206 }
6207
6208 /* Fetch the abbreviation table offset from a comp or type unit header. */
6209
6210 static sect_offset
6211 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
6212 struct dwarf2_section_info *section,
6213 sect_offset sect_off)
6214 {
6215 bfd *abfd = section->get_bfd_owner ();
6216 const gdb_byte *info_ptr;
6217 unsigned int initial_length_size, offset_size;
6218 uint16_t version;
6219
6220 section->read (per_objfile->objfile);
6221 info_ptr = section->buffer + to_underlying (sect_off);
6222 read_initial_length (abfd, info_ptr, &initial_length_size);
6223 offset_size = initial_length_size == 4 ? 4 : 8;
6224 info_ptr += initial_length_size;
6225
6226 version = read_2_bytes (abfd, info_ptr);
6227 info_ptr += 2;
6228 if (version >= 5)
6229 {
6230 /* Skip unit type and address size. */
6231 info_ptr += 2;
6232 }
6233
6234 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6235 }
6236
6237 /* A partial symtab that is used only for include files. */
6238 struct dwarf2_include_psymtab : public partial_symtab
6239 {
6240 dwarf2_include_psymtab (const char *filename,
6241 psymtab_storage *partial_symtabs,
6242 struct objfile *objfile)
6243 : partial_symtab (filename, partial_symtabs, objfile)
6244 {
6245 }
6246
6247 void read_symtab (struct objfile *objfile) override
6248 {
6249 /* It's an include file, no symbols to read for it.
6250 Everything is in the includer symtab. */
6251
6252 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6253 expansion of the includer psymtab. We use the dependencies[0] field to
6254 model the includer. But if we go the regular route of calling
6255 expand_psymtab here, and having expand_psymtab call expand_dependencies
6256 to expand the includer, we'll only use expand_psymtab on the includer
6257 (making it a non-toplevel psymtab), while if we expand the includer via
6258 another path, we'll use read_symtab (making it a toplevel psymtab).
6259 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6260 psymtab, and trigger read_symtab on the includer here directly. */
6261 includer ()->read_symtab (objfile);
6262 }
6263
6264 void expand_psymtab (struct objfile *objfile) override
6265 {
6266 /* This is not called by read_symtab, and should not be called by any
6267 expand_dependencies. */
6268 gdb_assert (false);
6269 }
6270
6271 bool readin_p (struct objfile *objfile) const override
6272 {
6273 return includer ()->readin_p (objfile);
6274 }
6275
6276 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6277 {
6278 return nullptr;
6279 }
6280
6281 private:
6282 partial_symtab *includer () const
6283 {
6284 /* An include psymtab has exactly one dependency: the psymtab that
6285 includes it. */
6286 gdb_assert (this->number_of_dependencies == 1);
6287 return this->dependencies[0];
6288 }
6289 };
6290
6291 /* Allocate a new partial symtab for file named NAME and mark this new
6292 partial symtab as being an include of PST. */
6293
6294 static void
6295 dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
6296 const char *name,
6297 dwarf2_psymtab *pst,
6298 psymtab_storage *partial_symtabs,
6299 struct objfile *objfile)
6300 {
6301 dwarf2_include_psymtab *subpst
6302 = new dwarf2_include_psymtab (name, partial_symtabs, objfile);
6303
6304 if (!IS_ABSOLUTE_PATH (subpst->filename))
6305 subpst->dirname = pst->dirname;
6306
6307 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
6308 subpst->dependencies[0] = pst;
6309 subpst->number_of_dependencies = 1;
6310 }
6311
6312 /* Read the Line Number Program data and extract the list of files
6313 included by the source file represented by PST. Build an include
6314 partial symtab for each of these included files. */
6315
6316 static void
6317 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6318 struct die_info *die,
6319 dwarf2_psymtab *pst)
6320 {
6321 line_header_up lh;
6322 struct attribute *attr;
6323
6324 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6325 if (attr != nullptr && attr->form_is_unsigned ())
6326 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
6327 if (lh == NULL)
6328 return; /* No linetable, so no includes. */
6329
6330 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6331 that we pass in the raw text_low here; that is ok because we're
6332 only decoding the line table to make include partial symtabs, and
6333 so the addresses aren't really used. */
6334 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6335 pst->raw_text_low (), 1);
6336 }
6337
6338 static hashval_t
6339 hash_signatured_type (const void *item)
6340 {
6341 const struct signatured_type *sig_type
6342 = (const struct signatured_type *) item;
6343
6344 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6345 return sig_type->signature;
6346 }
6347
6348 static int
6349 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6350 {
6351 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6352 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6353
6354 return lhs->signature == rhs->signature;
6355 }
6356
6357 /* Allocate a hash table for signatured types. */
6358
6359 static htab_up
6360 allocate_signatured_type_table ()
6361 {
6362 return htab_up (htab_create_alloc (41,
6363 hash_signatured_type,
6364 eq_signatured_type,
6365 NULL, xcalloc, xfree));
6366 }
6367
6368 /* A helper function to add a signatured type CU to a table. */
6369
6370 static int
6371 add_signatured_type_cu_to_table (void **slot, void *datum)
6372 {
6373 struct signatured_type *sigt = (struct signatured_type *) *slot;
6374 std::vector<signatured_type *> *all_type_units
6375 = (std::vector<signatured_type *> *) datum;
6376
6377 all_type_units->push_back (sigt);
6378
6379 return 1;
6380 }
6381
6382 /* A helper for create_debug_types_hash_table. Read types from SECTION
6383 and fill them into TYPES_HTAB. It will process only type units,
6384 therefore DW_UT_type. */
6385
6386 static void
6387 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
6388 struct dwo_file *dwo_file,
6389 dwarf2_section_info *section, htab_up &types_htab,
6390 rcuh_kind section_kind)
6391 {
6392 struct objfile *objfile = per_objfile->objfile;
6393 struct dwarf2_section_info *abbrev_section;
6394 bfd *abfd;
6395 const gdb_byte *info_ptr, *end_ptr;
6396
6397 abbrev_section = (dwo_file != NULL
6398 ? &dwo_file->sections.abbrev
6399 : &per_objfile->per_bfd->abbrev);
6400
6401 dwarf_read_debug_printf ("Reading %s for %s",
6402 section->get_name (),
6403 abbrev_section->get_file_name ());
6404
6405 section->read (objfile);
6406 info_ptr = section->buffer;
6407
6408 if (info_ptr == NULL)
6409 return;
6410
6411 /* We can't set abfd until now because the section may be empty or
6412 not present, in which case the bfd is unknown. */
6413 abfd = section->get_bfd_owner ();
6414
6415 /* We don't use cutu_reader here because we don't need to read
6416 any dies: the signature is in the header. */
6417
6418 end_ptr = info_ptr + section->size;
6419 while (info_ptr < end_ptr)
6420 {
6421 struct signatured_type *sig_type;
6422 struct dwo_unit *dwo_tu;
6423 void **slot;
6424 const gdb_byte *ptr = info_ptr;
6425 struct comp_unit_head header;
6426 unsigned int length;
6427
6428 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6429
6430 /* Initialize it due to a false compiler warning. */
6431 header.signature = -1;
6432 header.type_cu_offset_in_tu = (cu_offset) -1;
6433
6434 /* We need to read the type's signature in order to build the hash
6435 table, but we don't need anything else just yet. */
6436
6437 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
6438 abbrev_section, ptr, section_kind);
6439
6440 length = header.get_length ();
6441
6442 /* Skip dummy type units. */
6443 if (ptr >= info_ptr + length
6444 || peek_abbrev_code (abfd, ptr) == 0
6445 || (header.unit_type != DW_UT_type
6446 && header.unit_type != DW_UT_split_type))
6447 {
6448 info_ptr += length;
6449 continue;
6450 }
6451
6452 if (types_htab == NULL)
6453 {
6454 if (dwo_file)
6455 types_htab = allocate_dwo_unit_table ();
6456 else
6457 types_htab = allocate_signatured_type_table ();
6458 }
6459
6460 if (dwo_file)
6461 {
6462 sig_type = NULL;
6463 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
6464 dwo_tu->dwo_file = dwo_file;
6465 dwo_tu->signature = header.signature;
6466 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6467 dwo_tu->section = section;
6468 dwo_tu->sect_off = sect_off;
6469 dwo_tu->length = length;
6470 }
6471 else
6472 {
6473 /* N.B.: type_offset is not usable if this type uses a DWO file.
6474 The real type_offset is in the DWO file. */
6475 dwo_tu = NULL;
6476 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6477 sig_type->signature = header.signature;
6478 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6479 sig_type->per_cu.is_debug_types = 1;
6480 sig_type->per_cu.section = section;
6481 sig_type->per_cu.sect_off = sect_off;
6482 sig_type->per_cu.length = length;
6483 }
6484
6485 slot = htab_find_slot (types_htab.get (),
6486 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6487 INSERT);
6488 gdb_assert (slot != NULL);
6489 if (*slot != NULL)
6490 {
6491 sect_offset dup_sect_off;
6492
6493 if (dwo_file)
6494 {
6495 const struct dwo_unit *dup_tu
6496 = (const struct dwo_unit *) *slot;
6497
6498 dup_sect_off = dup_tu->sect_off;
6499 }
6500 else
6501 {
6502 const struct signatured_type *dup_tu
6503 = (const struct signatured_type *) *slot;
6504
6505 dup_sect_off = dup_tu->per_cu.sect_off;
6506 }
6507
6508 complaint (_("debug type entry at offset %s is duplicate to"
6509 " the entry at offset %s, signature %s"),
6510 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6511 hex_string (header.signature));
6512 }
6513 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6514
6515 dwarf_read_debug_printf_v (" offset %s, signature %s",
6516 sect_offset_str (sect_off),
6517 hex_string (header.signature));
6518
6519 info_ptr += length;
6520 }
6521 }
6522
6523 /* Create the hash table of all entries in the .debug_types
6524 (or .debug_types.dwo) section(s).
6525 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6526 otherwise it is NULL.
6527
6528 The result is a pointer to the hash table or NULL if there are no types.
6529
6530 Note: This function processes DWO files only, not DWP files. */
6531
6532 static void
6533 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
6534 struct dwo_file *dwo_file,
6535 gdb::array_view<dwarf2_section_info> type_sections,
6536 htab_up &types_htab)
6537 {
6538 for (dwarf2_section_info &section : type_sections)
6539 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6540 rcuh_kind::TYPE);
6541 }
6542
6543 /* Create the hash table of all entries in the .debug_types section,
6544 and initialize all_type_units.
6545 The result is zero if there is an error (e.g. missing .debug_types section),
6546 otherwise non-zero. */
6547
6548 static int
6549 create_all_type_units (dwarf2_per_objfile *per_objfile)
6550 {
6551 htab_up types_htab;
6552
6553 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6554 types_htab, rcuh_kind::COMPILE);
6555 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6556 types_htab);
6557 if (types_htab == NULL)
6558 {
6559 per_objfile->per_bfd->signatured_types = NULL;
6560 return 0;
6561 }
6562
6563 per_objfile->per_bfd->signatured_types = std::move (types_htab);
6564
6565 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6566 per_objfile->per_bfd->all_type_units.reserve
6567 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
6568
6569 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
6570 add_signatured_type_cu_to_table,
6571 &per_objfile->per_bfd->all_type_units);
6572
6573 return 1;
6574 }
6575
6576 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6577 If SLOT is non-NULL, it is the entry to use in the hash table.
6578 Otherwise we find one. */
6579
6580 static struct signatured_type *
6581 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6582 {
6583 if (per_objfile->per_bfd->all_type_units.size ()
6584 == per_objfile->per_bfd->all_type_units.capacity ())
6585 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6586
6587 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6588
6589 per_objfile->resize_symtabs ();
6590
6591 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6592 sig_type->signature = sig;
6593 sig_type->per_cu.is_debug_types = 1;
6594 if (per_objfile->per_bfd->using_index)
6595 {
6596 sig_type->per_cu.v.quick =
6597 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6598 struct dwarf2_per_cu_quick_data);
6599 }
6600
6601 if (slot == NULL)
6602 {
6603 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6604 sig_type, INSERT);
6605 }
6606 gdb_assert (*slot == NULL);
6607 *slot = sig_type;
6608 /* The rest of sig_type must be filled in by the caller. */
6609 return sig_type;
6610 }
6611
6612 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6613 Fill in SIG_ENTRY with DWO_ENTRY. */
6614
6615 static void
6616 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
6617 struct signatured_type *sig_entry,
6618 struct dwo_unit *dwo_entry)
6619 {
6620 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6621
6622 /* Make sure we're not clobbering something we don't expect to. */
6623 gdb_assert (! sig_entry->per_cu.queued);
6624 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
6625 if (per_bfd->using_index)
6626 {
6627 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6628 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6629 }
6630 else
6631 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6632 gdb_assert (sig_entry->signature == dwo_entry->signature);
6633 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6634 gdb_assert (sig_entry->type_unit_group == NULL);
6635 gdb_assert (sig_entry->dwo_unit == NULL);
6636
6637 sig_entry->per_cu.section = dwo_entry->section;
6638 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6639 sig_entry->per_cu.length = dwo_entry->length;
6640 sig_entry->per_cu.reading_dwo_directly = 1;
6641 sig_entry->per_cu.per_bfd = per_bfd;
6642 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6643 sig_entry->dwo_unit = dwo_entry;
6644 }
6645
6646 /* Subroutine of lookup_signatured_type.
6647 If we haven't read the TU yet, create the signatured_type data structure
6648 for a TU to be read in directly from a DWO file, bypassing the stub.
6649 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6650 using .gdb_index, then when reading a CU we want to stay in the DWO file
6651 containing that CU. Otherwise we could end up reading several other DWO
6652 files (due to comdat folding) to process the transitive closure of all the
6653 mentioned TUs, and that can be slow. The current DWO file will have every
6654 type signature that it needs.
6655 We only do this for .gdb_index because in the psymtab case we already have
6656 to read all the DWOs to build the type unit groups. */
6657
6658 static struct signatured_type *
6659 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6660 {
6661 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6662 struct dwo_file *dwo_file;
6663 struct dwo_unit find_dwo_entry, *dwo_entry;
6664 struct signatured_type find_sig_entry, *sig_entry;
6665 void **slot;
6666
6667 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6668
6669 /* If TU skeletons have been removed then we may not have read in any
6670 TUs yet. */
6671 if (per_objfile->per_bfd->signatured_types == NULL)
6672 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6673
6674 /* We only ever need to read in one copy of a signatured type.
6675 Use the global signatured_types array to do our own comdat-folding
6676 of types. If this is the first time we're reading this TU, and
6677 the TU has an entry in .gdb_index, replace the recorded data from
6678 .gdb_index with this TU. */
6679
6680 find_sig_entry.signature = sig;
6681 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6682 &find_sig_entry, INSERT);
6683 sig_entry = (struct signatured_type *) *slot;
6684
6685 /* We can get here with the TU already read, *or* in the process of being
6686 read. Don't reassign the global entry to point to this DWO if that's
6687 the case. Also note that if the TU is already being read, it may not
6688 have come from a DWO, the program may be a mix of Fission-compiled
6689 code and non-Fission-compiled code. */
6690
6691 /* Have we already tried to read this TU?
6692 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6693 needn't exist in the global table yet). */
6694 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6695 return sig_entry;
6696
6697 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6698 dwo_unit of the TU itself. */
6699 dwo_file = cu->dwo_unit->dwo_file;
6700
6701 /* Ok, this is the first time we're reading this TU. */
6702 if (dwo_file->tus == NULL)
6703 return NULL;
6704 find_dwo_entry.signature = sig;
6705 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6706 &find_dwo_entry);
6707 if (dwo_entry == NULL)
6708 return NULL;
6709
6710 /* If the global table doesn't have an entry for this TU, add one. */
6711 if (sig_entry == NULL)
6712 sig_entry = add_type_unit (per_objfile, sig, slot);
6713
6714 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6715 sig_entry->per_cu.tu_read = 1;
6716 return sig_entry;
6717 }
6718
6719 /* Subroutine of lookup_signatured_type.
6720 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6721 then try the DWP file. If the TU stub (skeleton) has been removed then
6722 it won't be in .gdb_index. */
6723
6724 static struct signatured_type *
6725 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6726 {
6727 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6728 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6729 struct dwo_unit *dwo_entry;
6730 struct signatured_type find_sig_entry, *sig_entry;
6731 void **slot;
6732
6733 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6734 gdb_assert (dwp_file != NULL);
6735
6736 /* If TU skeletons have been removed then we may not have read in any
6737 TUs yet. */
6738 if (per_objfile->per_bfd->signatured_types == NULL)
6739 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6740
6741 find_sig_entry.signature = sig;
6742 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6743 &find_sig_entry, INSERT);
6744 sig_entry = (struct signatured_type *) *slot;
6745
6746 /* Have we already tried to read this TU?
6747 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6748 needn't exist in the global table yet). */
6749 if (sig_entry != NULL)
6750 return sig_entry;
6751
6752 if (dwp_file->tus == NULL)
6753 return NULL;
6754 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6755 1 /* is_debug_types */);
6756 if (dwo_entry == NULL)
6757 return NULL;
6758
6759 sig_entry = add_type_unit (per_objfile, sig, slot);
6760 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6761
6762 return sig_entry;
6763 }
6764
6765 /* Lookup a signature based type for DW_FORM_ref_sig8.
6766 Returns NULL if signature SIG is not present in the table.
6767 It is up to the caller to complain about this. */
6768
6769 static struct signatured_type *
6770 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6771 {
6772 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6773
6774 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6775 {
6776 /* We're in a DWO/DWP file, and we're using .gdb_index.
6777 These cases require special processing. */
6778 if (get_dwp_file (per_objfile) == NULL)
6779 return lookup_dwo_signatured_type (cu, sig);
6780 else
6781 return lookup_dwp_signatured_type (cu, sig);
6782 }
6783 else
6784 {
6785 struct signatured_type find_entry, *entry;
6786
6787 if (per_objfile->per_bfd->signatured_types == NULL)
6788 return NULL;
6789 find_entry.signature = sig;
6790 entry = ((struct signatured_type *)
6791 htab_find (per_objfile->per_bfd->signatured_types.get (),
6792 &find_entry));
6793 return entry;
6794 }
6795 }
6796
6797 /* Low level DIE reading support. */
6798
6799 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6800
6801 static void
6802 init_cu_die_reader (struct die_reader_specs *reader,
6803 struct dwarf2_cu *cu,
6804 struct dwarf2_section_info *section,
6805 struct dwo_file *dwo_file,
6806 struct abbrev_table *abbrev_table)
6807 {
6808 gdb_assert (section->readin && section->buffer != NULL);
6809 reader->abfd = section->get_bfd_owner ();
6810 reader->cu = cu;
6811 reader->dwo_file = dwo_file;
6812 reader->die_section = section;
6813 reader->buffer = section->buffer;
6814 reader->buffer_end = section->buffer + section->size;
6815 reader->abbrev_table = abbrev_table;
6816 }
6817
6818 /* Subroutine of cutu_reader to simplify it.
6819 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6820 There's just a lot of work to do, and cutu_reader is big enough
6821 already.
6822
6823 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6824 from it to the DIE in the DWO. If NULL we are skipping the stub.
6825 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6826 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6827 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6828 STUB_COMP_DIR may be non-NULL.
6829 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6830 are filled in with the info of the DIE from the DWO file.
6831 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6832 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6833 kept around for at least as long as *RESULT_READER.
6834
6835 The result is non-zero if a valid (non-dummy) DIE was found. */
6836
6837 static int
6838 read_cutu_die_from_dwo (dwarf2_cu *cu,
6839 struct dwo_unit *dwo_unit,
6840 struct die_info *stub_comp_unit_die,
6841 const char *stub_comp_dir,
6842 struct die_reader_specs *result_reader,
6843 const gdb_byte **result_info_ptr,
6844 struct die_info **result_comp_unit_die,
6845 abbrev_table_up *result_dwo_abbrev_table)
6846 {
6847 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6848 dwarf2_per_cu_data *per_cu = cu->per_cu;
6849 struct objfile *objfile = per_objfile->objfile;
6850 bfd *abfd;
6851 const gdb_byte *begin_info_ptr, *info_ptr;
6852 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6853 int i,num_extra_attrs;
6854 struct dwarf2_section_info *dwo_abbrev_section;
6855 struct die_info *comp_unit_die;
6856
6857 /* At most one of these may be provided. */
6858 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6859
6860 /* These attributes aren't processed until later:
6861 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6862 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6863 referenced later. However, these attributes are found in the stub
6864 which we won't have later. In order to not impose this complication
6865 on the rest of the code, we read them here and copy them to the
6866 DWO CU/TU die. */
6867
6868 stmt_list = NULL;
6869 low_pc = NULL;
6870 high_pc = NULL;
6871 ranges = NULL;
6872 comp_dir = NULL;
6873
6874 if (stub_comp_unit_die != NULL)
6875 {
6876 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6877 DWO file. */
6878 if (!per_cu->is_debug_types)
6879 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6880 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6881 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6882 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6883 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6884
6885 cu->addr_base = stub_comp_unit_die->addr_base ();
6886
6887 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6888 We need the value before we can process DW_AT_ranges values from the
6889 DWO. */
6890 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6891
6892 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6893 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6894 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6895 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6896 section. */
6897 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
6898 }
6899 else if (stub_comp_dir != NULL)
6900 {
6901 /* Reconstruct the comp_dir attribute to simplify the code below. */
6902 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
6903 comp_dir->name = DW_AT_comp_dir;
6904 comp_dir->form = DW_FORM_string;
6905 comp_dir->set_string_noncanonical (stub_comp_dir);
6906 }
6907
6908 /* Set up for reading the DWO CU/TU. */
6909 cu->dwo_unit = dwo_unit;
6910 dwarf2_section_info *section = dwo_unit->section;
6911 section->read (objfile);
6912 abfd = section->get_bfd_owner ();
6913 begin_info_ptr = info_ptr = (section->buffer
6914 + to_underlying (dwo_unit->sect_off));
6915 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6916
6917 if (per_cu->is_debug_types)
6918 {
6919 signatured_type *sig_type = (struct signatured_type *) per_cu;
6920
6921 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6922 section, dwo_abbrev_section,
6923 info_ptr, rcuh_kind::TYPE);
6924 /* This is not an assert because it can be caused by bad debug info. */
6925 if (sig_type->signature != cu->header.signature)
6926 {
6927 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6928 " TU at offset %s [in module %s]"),
6929 hex_string (sig_type->signature),
6930 hex_string (cu->header.signature),
6931 sect_offset_str (dwo_unit->sect_off),
6932 bfd_get_filename (abfd));
6933 }
6934 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6935 /* For DWOs coming from DWP files, we don't know the CU length
6936 nor the type's offset in the TU until now. */
6937 dwo_unit->length = cu->header.get_length ();
6938 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6939
6940 /* Establish the type offset that can be used to lookup the type.
6941 For DWO files, we don't know it until now. */
6942 sig_type->type_offset_in_section
6943 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6944 }
6945 else
6946 {
6947 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6948 section, dwo_abbrev_section,
6949 info_ptr, rcuh_kind::COMPILE);
6950 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6951 /* For DWOs coming from DWP files, we don't know the CU length
6952 until now. */
6953 dwo_unit->length = cu->header.get_length ();
6954 }
6955
6956 dwo_abbrev_section->read (objfile);
6957 *result_dwo_abbrev_table
6958 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
6959 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6960 result_dwo_abbrev_table->get ());
6961
6962 /* Read in the die, but leave space to copy over the attributes
6963 from the stub. This has the benefit of simplifying the rest of
6964 the code - all the work to maintain the illusion of a single
6965 DW_TAG_{compile,type}_unit DIE is done here. */
6966 num_extra_attrs = ((stmt_list != NULL)
6967 + (low_pc != NULL)
6968 + (high_pc != NULL)
6969 + (ranges != NULL)
6970 + (comp_dir != NULL));
6971 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6972 num_extra_attrs);
6973
6974 /* Copy over the attributes from the stub to the DIE we just read in. */
6975 comp_unit_die = *result_comp_unit_die;
6976 i = comp_unit_die->num_attrs;
6977 if (stmt_list != NULL)
6978 comp_unit_die->attrs[i++] = *stmt_list;
6979 if (low_pc != NULL)
6980 comp_unit_die->attrs[i++] = *low_pc;
6981 if (high_pc != NULL)
6982 comp_unit_die->attrs[i++] = *high_pc;
6983 if (ranges != NULL)
6984 comp_unit_die->attrs[i++] = *ranges;
6985 if (comp_dir != NULL)
6986 comp_unit_die->attrs[i++] = *comp_dir;
6987 comp_unit_die->num_attrs += num_extra_attrs;
6988
6989 if (dwarf_die_debug)
6990 {
6991 fprintf_unfiltered (gdb_stdlog,
6992 "Read die from %s@0x%x of %s:\n",
6993 section->get_name (),
6994 (unsigned) (begin_info_ptr - section->buffer),
6995 bfd_get_filename (abfd));
6996 dump_die (comp_unit_die, dwarf_die_debug);
6997 }
6998
6999 /* Skip dummy compilation units. */
7000 if (info_ptr >= begin_info_ptr + dwo_unit->length
7001 || peek_abbrev_code (abfd, info_ptr) == 0)
7002 return 0;
7003
7004 *result_info_ptr = info_ptr;
7005 return 1;
7006 }
7007
7008 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7009 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7010 signature is part of the header. */
7011 static gdb::optional<ULONGEST>
7012 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7013 {
7014 if (cu->header.version >= 5)
7015 return cu->header.signature;
7016 struct attribute *attr;
7017 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7018 if (attr == nullptr || !attr->form_is_unsigned ())
7019 return gdb::optional<ULONGEST> ();
7020 return attr->as_unsigned ();
7021 }
7022
7023 /* Subroutine of cutu_reader to simplify it.
7024 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7025 Returns NULL if the specified DWO unit cannot be found. */
7026
7027 static struct dwo_unit *
7028 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
7029 {
7030 dwarf2_per_cu_data *per_cu = cu->per_cu;
7031 struct dwo_unit *dwo_unit;
7032 const char *comp_dir;
7033
7034 gdb_assert (cu != NULL);
7035
7036 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7037 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7038 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7039
7040 if (per_cu->is_debug_types)
7041 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
7042 else
7043 {
7044 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7045
7046 if (!signature.has_value ())
7047 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7048 " [in module %s]"),
7049 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
7050
7051 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
7052 }
7053
7054 return dwo_unit;
7055 }
7056
7057 /* Subroutine of cutu_reader to simplify it.
7058 See it for a description of the parameters.
7059 Read a TU directly from a DWO file, bypassing the stub. */
7060
7061 void
7062 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
7063 dwarf2_per_objfile *per_objfile,
7064 dwarf2_cu *existing_cu)
7065 {
7066 struct signatured_type *sig_type;
7067
7068 /* Verify we can do the following downcast, and that we have the
7069 data we need. */
7070 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7071 sig_type = (struct signatured_type *) this_cu;
7072 gdb_assert (sig_type->dwo_unit != NULL);
7073
7074 dwarf2_cu *cu;
7075
7076 if (existing_cu != nullptr)
7077 {
7078 cu = existing_cu;
7079 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
7080 /* There's no need to do the rereading_dwo_cu handling that
7081 cutu_reader does since we don't read the stub. */
7082 }
7083 else
7084 {
7085 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7086 in per_objfile yet. */
7087 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7088 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7089 cu = m_new_cu.get ();
7090 }
7091
7092 /* A future optimization, if needed, would be to use an existing
7093 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7094 could share abbrev tables. */
7095
7096 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
7097 NULL /* stub_comp_unit_die */,
7098 sig_type->dwo_unit->dwo_file->comp_dir,
7099 this, &info_ptr,
7100 &comp_unit_die,
7101 &m_dwo_abbrev_table) == 0)
7102 {
7103 /* Dummy die. */
7104 dummy_p = true;
7105 }
7106 }
7107
7108 /* Initialize a CU (or TU) and read its DIEs.
7109 If the CU defers to a DWO file, read the DWO file as well.
7110
7111 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7112 Otherwise the table specified in the comp unit header is read in and used.
7113 This is an optimization for when we already have the abbrev table.
7114
7115 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7116 allocated. */
7117
7118 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7119 dwarf2_per_objfile *per_objfile,
7120 struct abbrev_table *abbrev_table,
7121 dwarf2_cu *existing_cu,
7122 bool skip_partial)
7123 : die_reader_specs {},
7124 m_this_cu (this_cu)
7125 {
7126 struct objfile *objfile = per_objfile->objfile;
7127 struct dwarf2_section_info *section = this_cu->section;
7128 bfd *abfd = section->get_bfd_owner ();
7129 const gdb_byte *begin_info_ptr;
7130 struct signatured_type *sig_type = NULL;
7131 struct dwarf2_section_info *abbrev_section;
7132 /* Non-zero if CU currently points to a DWO file and we need to
7133 reread it. When this happens we need to reread the skeleton die
7134 before we can reread the DWO file (this only applies to CUs, not TUs). */
7135 int rereading_dwo_cu = 0;
7136
7137 if (dwarf_die_debug)
7138 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7139 this_cu->is_debug_types ? "type" : "comp",
7140 sect_offset_str (this_cu->sect_off));
7141
7142 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7143 file (instead of going through the stub), short-circuit all of this. */
7144 if (this_cu->reading_dwo_directly)
7145 {
7146 /* Narrow down the scope of possibilities to have to understand. */
7147 gdb_assert (this_cu->is_debug_types);
7148 gdb_assert (abbrev_table == NULL);
7149 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
7150 return;
7151 }
7152
7153 /* This is cheap if the section is already read in. */
7154 section->read (objfile);
7155
7156 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7157
7158 abbrev_section = get_abbrev_section_for_cu (this_cu);
7159
7160 dwarf2_cu *cu;
7161
7162 if (existing_cu != nullptr)
7163 {
7164 cu = existing_cu;
7165 /* If this CU is from a DWO file we need to start over, we need to
7166 refetch the attributes from the skeleton CU.
7167 This could be optimized by retrieving those attributes from when we
7168 were here the first time: the previous comp_unit_die was stored in
7169 comp_unit_obstack. But there's no data yet that we need this
7170 optimization. */
7171 if (cu->dwo_unit != NULL)
7172 rereading_dwo_cu = 1;
7173 }
7174 else
7175 {
7176 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7177 in per_objfile yet. */
7178 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7179 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7180 cu = m_new_cu.get ();
7181 }
7182
7183 /* Get the header. */
7184 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7185 {
7186 /* We already have the header, there's no need to read it in again. */
7187 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7188 }
7189 else
7190 {
7191 if (this_cu->is_debug_types)
7192 {
7193 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7194 section, abbrev_section,
7195 info_ptr, rcuh_kind::TYPE);
7196
7197 /* Since per_cu is the first member of struct signatured_type,
7198 we can go from a pointer to one to a pointer to the other. */
7199 sig_type = (struct signatured_type *) this_cu;
7200 gdb_assert (sig_type->signature == cu->header.signature);
7201 gdb_assert (sig_type->type_offset_in_tu
7202 == cu->header.type_cu_offset_in_tu);
7203 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7204
7205 /* LENGTH has not been set yet for type units if we're
7206 using .gdb_index. */
7207 this_cu->length = cu->header.get_length ();
7208
7209 /* Establish the type offset that can be used to lookup the type. */
7210 sig_type->type_offset_in_section =
7211 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7212
7213 this_cu->dwarf_version = cu->header.version;
7214 }
7215 else
7216 {
7217 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7218 section, abbrev_section,
7219 info_ptr,
7220 rcuh_kind::COMPILE);
7221
7222 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7223 if (this_cu->length == 0)
7224 this_cu->length = cu->header.get_length ();
7225 else
7226 gdb_assert (this_cu->length == cu->header.get_length ());
7227 this_cu->dwarf_version = cu->header.version;
7228 }
7229 }
7230
7231 /* Skip dummy compilation units. */
7232 if (info_ptr >= begin_info_ptr + this_cu->length
7233 || peek_abbrev_code (abfd, info_ptr) == 0)
7234 {
7235 dummy_p = true;
7236 return;
7237 }
7238
7239 /* If we don't have them yet, read the abbrevs for this compilation unit.
7240 And if we need to read them now, make sure they're freed when we're
7241 done. */
7242 if (abbrev_table != NULL)
7243 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7244 else
7245 {
7246 abbrev_section->read (objfile);
7247 m_abbrev_table_holder
7248 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
7249 abbrev_table = m_abbrev_table_holder.get ();
7250 }
7251
7252 /* Read the top level CU/TU die. */
7253 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7254 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7255
7256 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7257 {
7258 dummy_p = true;
7259 return;
7260 }
7261
7262 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7263 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7264 table from the DWO file and pass the ownership over to us. It will be
7265 referenced from READER, so we must make sure to free it after we're done
7266 with READER.
7267
7268 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7269 DWO CU, that this test will fail (the attribute will not be present). */
7270 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7271 if (dwo_name != nullptr)
7272 {
7273 struct dwo_unit *dwo_unit;
7274 struct die_info *dwo_comp_unit_die;
7275
7276 if (comp_unit_die->has_children)
7277 {
7278 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7279 " has children (offset %s) [in module %s]"),
7280 sect_offset_str (this_cu->sect_off),
7281 bfd_get_filename (abfd));
7282 }
7283 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
7284 if (dwo_unit != NULL)
7285 {
7286 if (read_cutu_die_from_dwo (cu, dwo_unit,
7287 comp_unit_die, NULL,
7288 this, &info_ptr,
7289 &dwo_comp_unit_die,
7290 &m_dwo_abbrev_table) == 0)
7291 {
7292 /* Dummy die. */
7293 dummy_p = true;
7294 return;
7295 }
7296 comp_unit_die = dwo_comp_unit_die;
7297 }
7298 else
7299 {
7300 /* Yikes, we couldn't find the rest of the DIE, we only have
7301 the stub. A complaint has already been logged. There's
7302 not much more we can do except pass on the stub DIE to
7303 die_reader_func. We don't want to throw an error on bad
7304 debug info. */
7305 }
7306 }
7307 }
7308
7309 void
7310 cutu_reader::keep ()
7311 {
7312 /* Done, clean up. */
7313 gdb_assert (!dummy_p);
7314 if (m_new_cu != NULL)
7315 {
7316 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7317 now. */
7318 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7319 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
7320 }
7321 }
7322
7323 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7324 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7325 assumed to have already done the lookup to find the DWO file).
7326
7327 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7328 THIS_CU->is_debug_types, but nothing else.
7329
7330 We fill in THIS_CU->length.
7331
7332 THIS_CU->cu is always freed when done.
7333 This is done in order to not leave THIS_CU->cu in a state where we have
7334 to care whether it refers to the "main" CU or the DWO CU.
7335
7336 When parent_cu is passed, it is used to provide a default value for
7337 str_offsets_base and addr_base from the parent. */
7338
7339 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7340 dwarf2_per_objfile *per_objfile,
7341 struct dwarf2_cu *parent_cu,
7342 struct dwo_file *dwo_file)
7343 : die_reader_specs {},
7344 m_this_cu (this_cu)
7345 {
7346 struct objfile *objfile = per_objfile->objfile;
7347 struct dwarf2_section_info *section = this_cu->section;
7348 bfd *abfd = section->get_bfd_owner ();
7349 struct dwarf2_section_info *abbrev_section;
7350 const gdb_byte *begin_info_ptr, *info_ptr;
7351
7352 if (dwarf_die_debug)
7353 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7354 this_cu->is_debug_types ? "type" : "comp",
7355 sect_offset_str (this_cu->sect_off));
7356
7357 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7358
7359 abbrev_section = (dwo_file != NULL
7360 ? &dwo_file->sections.abbrev
7361 : get_abbrev_section_for_cu (this_cu));
7362
7363 /* This is cheap if the section is already read in. */
7364 section->read (objfile);
7365
7366 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7367
7368 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7369 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7370 section, abbrev_section, info_ptr,
7371 (this_cu->is_debug_types
7372 ? rcuh_kind::TYPE
7373 : rcuh_kind::COMPILE));
7374
7375 if (parent_cu != nullptr)
7376 {
7377 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7378 m_new_cu->addr_base = parent_cu->addr_base;
7379 }
7380 this_cu->length = m_new_cu->header.get_length ();
7381
7382 /* Skip dummy compilation units. */
7383 if (info_ptr >= begin_info_ptr + this_cu->length
7384 || peek_abbrev_code (abfd, info_ptr) == 0)
7385 {
7386 dummy_p = true;
7387 return;
7388 }
7389
7390 abbrev_section->read (objfile);
7391 m_abbrev_table_holder
7392 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
7393
7394 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7395 m_abbrev_table_holder.get ());
7396 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7397 }
7398
7399 \f
7400 /* Type Unit Groups.
7401
7402 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7403 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7404 so that all types coming from the same compilation (.o file) are grouped
7405 together. A future step could be to put the types in the same symtab as
7406 the CU the types ultimately came from. */
7407
7408 static hashval_t
7409 hash_type_unit_group (const void *item)
7410 {
7411 const struct type_unit_group *tu_group
7412 = (const struct type_unit_group *) item;
7413
7414 return hash_stmt_list_entry (&tu_group->hash);
7415 }
7416
7417 static int
7418 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7419 {
7420 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7421 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7422
7423 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7424 }
7425
7426 /* Allocate a hash table for type unit groups. */
7427
7428 static htab_up
7429 allocate_type_unit_groups_table ()
7430 {
7431 return htab_up (htab_create_alloc (3,
7432 hash_type_unit_group,
7433 eq_type_unit_group,
7434 NULL, xcalloc, xfree));
7435 }
7436
7437 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7438 partial symtabs. We combine several TUs per psymtab to not let the size
7439 of any one psymtab grow too big. */
7440 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7441 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7442
7443 /* Helper routine for get_type_unit_group.
7444 Create the type_unit_group object used to hold one or more TUs. */
7445
7446 static struct type_unit_group *
7447 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7448 {
7449 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7450 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7451 struct dwarf2_per_cu_data *per_cu;
7452 struct type_unit_group *tu_group;
7453
7454 tu_group = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, type_unit_group);
7455 per_cu = &tu_group->per_cu;
7456 per_cu->per_bfd = per_bfd;
7457
7458 if (per_bfd->using_index)
7459 {
7460 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7461 struct dwarf2_per_cu_quick_data);
7462 }
7463 else
7464 {
7465 unsigned int line_offset = to_underlying (line_offset_struct);
7466 dwarf2_psymtab *pst;
7467 std::string name;
7468
7469 /* Give the symtab a useful name for debug purposes. */
7470 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7471 name = string_printf ("<type_units_%d>",
7472 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7473 else
7474 name = string_printf ("<type_units_at_0x%x>", line_offset);
7475
7476 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
7477 pst->anonymous = true;
7478 }
7479
7480 tu_group->hash.dwo_unit = cu->dwo_unit;
7481 tu_group->hash.line_sect_off = line_offset_struct;
7482
7483 return tu_group;
7484 }
7485
7486 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7487 STMT_LIST is a DW_AT_stmt_list attribute. */
7488
7489 static struct type_unit_group *
7490 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7491 {
7492 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7493 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7494 struct type_unit_group *tu_group;
7495 void **slot;
7496 unsigned int line_offset;
7497 struct type_unit_group type_unit_group_for_lookup;
7498
7499 if (per_objfile->per_bfd->type_unit_groups == NULL)
7500 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7501
7502 /* Do we need to create a new group, or can we use an existing one? */
7503
7504 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
7505 {
7506 line_offset = stmt_list->as_unsigned ();
7507 ++tu_stats->nr_symtab_sharers;
7508 }
7509 else
7510 {
7511 /* Ugh, no stmt_list. Rare, but we have to handle it.
7512 We can do various things here like create one group per TU or
7513 spread them over multiple groups to split up the expansion work.
7514 To avoid worst case scenarios (too many groups or too large groups)
7515 we, umm, group them in bunches. */
7516 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7517 | (tu_stats->nr_stmt_less_type_units
7518 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7519 ++tu_stats->nr_stmt_less_type_units;
7520 }
7521
7522 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7523 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7524 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
7525 &type_unit_group_for_lookup, INSERT);
7526 if (*slot != NULL)
7527 {
7528 tu_group = (struct type_unit_group *) *slot;
7529 gdb_assert (tu_group != NULL);
7530 }
7531 else
7532 {
7533 sect_offset line_offset_struct = (sect_offset) line_offset;
7534 tu_group = create_type_unit_group (cu, line_offset_struct);
7535 *slot = tu_group;
7536 ++tu_stats->nr_symtabs;
7537 }
7538
7539 return tu_group;
7540 }
7541 \f
7542 /* Partial symbol tables. */
7543
7544 /* Create a psymtab named NAME and assign it to PER_CU.
7545
7546 The caller must fill in the following details:
7547 dirname, textlow, texthigh. */
7548
7549 static dwarf2_psymtab *
7550 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7551 dwarf2_per_objfile *per_objfile,
7552 const char *name)
7553 {
7554 struct objfile *objfile = per_objfile->objfile;
7555 dwarf2_psymtab *pst;
7556
7557 pst = new dwarf2_psymtab (name, per_objfile->per_bfd->partial_symtabs.get (),
7558 objfile, per_cu);
7559
7560 pst->psymtabs_addrmap_supported = true;
7561
7562 /* This is the glue that links PST into GDB's symbol API. */
7563 per_cu->v.psymtab = pst;
7564
7565 return pst;
7566 }
7567
7568 /* DIE reader function for process_psymtab_comp_unit. */
7569
7570 static void
7571 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7572 const gdb_byte *info_ptr,
7573 struct die_info *comp_unit_die,
7574 enum language pretend_language)
7575 {
7576 struct dwarf2_cu *cu = reader->cu;
7577 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7578 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7579 struct objfile *objfile = per_objfile->objfile;
7580 struct gdbarch *gdbarch = objfile->arch ();
7581 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7582 CORE_ADDR baseaddr;
7583 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7584 dwarf2_psymtab *pst;
7585 enum pc_bounds_kind cu_bounds_kind;
7586 const char *filename;
7587
7588 gdb_assert (! per_cu->is_debug_types);
7589
7590 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7591
7592 /* Allocate a new partial symbol table structure. */
7593 gdb::unique_xmalloc_ptr<char> debug_filename;
7594 static const char artificial[] = "<artificial>";
7595 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7596 if (filename == NULL)
7597 filename = "";
7598 else if (strcmp (filename, artificial) == 0)
7599 {
7600 debug_filename.reset (concat (artificial, "@",
7601 sect_offset_str (per_cu->sect_off),
7602 (char *) NULL));
7603 filename = debug_filename.get ();
7604 }
7605
7606 pst = create_partial_symtab (per_cu, per_objfile, filename);
7607
7608 /* This must be done before calling dwarf2_build_include_psymtabs. */
7609 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7610
7611 baseaddr = objfile->text_section_offset ();
7612
7613 dwarf2_find_base_address (comp_unit_die, cu);
7614
7615 /* Possibly set the default values of LOWPC and HIGHPC from
7616 `DW_AT_ranges'. */
7617 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7618 &best_highpc, cu, pst);
7619 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7620 {
7621 CORE_ADDR low
7622 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7623 - baseaddr);
7624 CORE_ADDR high
7625 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7626 - baseaddr - 1);
7627 /* Store the contiguous range if it is not empty; it can be
7628 empty for CUs with no code. */
7629 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
7630 low, high, pst);
7631 }
7632
7633 /* Check if comp unit has_children.
7634 If so, read the rest of the partial symbols from this comp unit.
7635 If not, there's no more debug_info for this comp unit. */
7636 if (comp_unit_die->has_children)
7637 {
7638 struct partial_die_info *first_die;
7639 CORE_ADDR lowpc, highpc;
7640
7641 lowpc = ((CORE_ADDR) -1);
7642 highpc = ((CORE_ADDR) 0);
7643
7644 first_die = load_partial_dies (reader, info_ptr, 1);
7645
7646 scan_partial_symbols (first_die, &lowpc, &highpc,
7647 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7648
7649 /* If we didn't find a lowpc, set it to highpc to avoid
7650 complaints from `maint check'. */
7651 if (lowpc == ((CORE_ADDR) -1))
7652 lowpc = highpc;
7653
7654 /* If the compilation unit didn't have an explicit address range,
7655 then use the information extracted from its child dies. */
7656 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7657 {
7658 best_lowpc = lowpc;
7659 best_highpc = highpc;
7660 }
7661 }
7662 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7663 best_lowpc + baseaddr)
7664 - baseaddr);
7665 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7666 best_highpc + baseaddr)
7667 - baseaddr);
7668
7669 pst->end ();
7670
7671 if (!cu->per_cu->imported_symtabs_empty ())
7672 {
7673 int i;
7674 int len = cu->per_cu->imported_symtabs_size ();
7675
7676 /* Fill in 'dependencies' here; we fill in 'users' in a
7677 post-pass. */
7678 pst->number_of_dependencies = len;
7679 pst->dependencies
7680 = per_bfd->partial_symtabs->allocate_dependencies (len);
7681 for (i = 0; i < len; ++i)
7682 {
7683 pst->dependencies[i]
7684 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7685 }
7686
7687 cu->per_cu->imported_symtabs_free ();
7688 }
7689
7690 /* Get the list of files included in the current compilation unit,
7691 and build a psymtab for each of them. */
7692 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7693
7694 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7695 ", %d global, %d static syms",
7696 per_cu->is_debug_types ? "type" : "comp",
7697 sect_offset_str (per_cu->sect_off),
7698 paddress (gdbarch, pst->text_low (objfile)),
7699 paddress (gdbarch, pst->text_high (objfile)),
7700 (int) pst->global_psymbols.size (),
7701 (int) pst->static_psymbols.size ());
7702 }
7703
7704 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7705 Process compilation unit THIS_CU for a psymtab. */
7706
7707 static void
7708 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7709 dwarf2_per_objfile *per_objfile,
7710 bool want_partial_unit,
7711 enum language pretend_language)
7712 {
7713 /* If this compilation unit was already read in, free the
7714 cached copy in order to read it in again. This is
7715 necessary because we skipped some symbols when we first
7716 read in the compilation unit (see load_partial_dies).
7717 This problem could be avoided, but the benefit is unclear. */
7718 per_objfile->remove_cu (this_cu);
7719
7720 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7721
7722 switch (reader.comp_unit_die->tag)
7723 {
7724 case DW_TAG_compile_unit:
7725 this_cu->unit_type = DW_UT_compile;
7726 break;
7727 case DW_TAG_partial_unit:
7728 this_cu->unit_type = DW_UT_partial;
7729 break;
7730 case DW_TAG_type_unit:
7731 this_cu->unit_type = DW_UT_type;
7732 break;
7733 default:
7734 abort ();
7735 }
7736
7737 if (reader.dummy_p)
7738 {
7739 /* Nothing. */
7740 }
7741 else if (this_cu->is_debug_types)
7742 build_type_psymtabs_reader (&reader, reader.info_ptr,
7743 reader.comp_unit_die);
7744 else if (want_partial_unit
7745 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7746 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7747 reader.comp_unit_die,
7748 pretend_language);
7749
7750 this_cu->lang = reader.cu->language;
7751
7752 /* Age out any secondary CUs. */
7753 per_objfile->age_comp_units ();
7754 }
7755
7756 /* Reader function for build_type_psymtabs. */
7757
7758 static void
7759 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7760 const gdb_byte *info_ptr,
7761 struct die_info *type_unit_die)
7762 {
7763 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7764 struct dwarf2_cu *cu = reader->cu;
7765 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7766 struct signatured_type *sig_type;
7767 struct type_unit_group *tu_group;
7768 struct attribute *attr;
7769 struct partial_die_info *first_die;
7770 CORE_ADDR lowpc, highpc;
7771 dwarf2_psymtab *pst;
7772
7773 gdb_assert (per_cu->is_debug_types);
7774 sig_type = (struct signatured_type *) per_cu;
7775
7776 if (! type_unit_die->has_children)
7777 return;
7778
7779 attr = type_unit_die->attr (DW_AT_stmt_list);
7780 tu_group = get_type_unit_group (cu, attr);
7781
7782 if (tu_group->tus == nullptr)
7783 tu_group->tus = new std::vector<signatured_type *>;
7784 tu_group->tus->push_back (sig_type);
7785
7786 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7787 pst = create_partial_symtab (per_cu, per_objfile, "");
7788 pst->anonymous = true;
7789
7790 first_die = load_partial_dies (reader, info_ptr, 1);
7791
7792 lowpc = (CORE_ADDR) -1;
7793 highpc = (CORE_ADDR) 0;
7794 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7795
7796 pst->end ();
7797 }
7798
7799 /* Struct used to sort TUs by their abbreviation table offset. */
7800
7801 struct tu_abbrev_offset
7802 {
7803 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7804 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7805 {}
7806
7807 signatured_type *sig_type;
7808 sect_offset abbrev_offset;
7809 };
7810
7811 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7812
7813 static bool
7814 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7815 const struct tu_abbrev_offset &b)
7816 {
7817 return a.abbrev_offset < b.abbrev_offset;
7818 }
7819
7820 /* Efficiently read all the type units.
7821 This does the bulk of the work for build_type_psymtabs.
7822
7823 The efficiency is because we sort TUs by the abbrev table they use and
7824 only read each abbrev table once. In one program there are 200K TUs
7825 sharing 8K abbrev tables.
7826
7827 The main purpose of this function is to support building the
7828 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7829 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7830 can collapse the search space by grouping them by stmt_list.
7831 The savings can be significant, in the same program from above the 200K TUs
7832 share 8K stmt_list tables.
7833
7834 FUNC is expected to call get_type_unit_group, which will create the
7835 struct type_unit_group if necessary and add it to
7836 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7837
7838 static void
7839 build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
7840 {
7841 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7842 abbrev_table_up abbrev_table;
7843 sect_offset abbrev_offset;
7844
7845 /* It's up to the caller to not call us multiple times. */
7846 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7847
7848 if (per_objfile->per_bfd->all_type_units.empty ())
7849 return;
7850
7851 /* TUs typically share abbrev tables, and there can be way more TUs than
7852 abbrev tables. Sort by abbrev table to reduce the number of times we
7853 read each abbrev table in.
7854 Alternatives are to punt or to maintain a cache of abbrev tables.
7855 This is simpler and efficient enough for now.
7856
7857 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7858 symtab to use). Typically TUs with the same abbrev offset have the same
7859 stmt_list value too so in practice this should work well.
7860
7861 The basic algorithm here is:
7862
7863 sort TUs by abbrev table
7864 for each TU with same abbrev table:
7865 read abbrev table if first user
7866 read TU top level DIE
7867 [IWBN if DWO skeletons had DW_AT_stmt_list]
7868 call FUNC */
7869
7870 dwarf_read_debug_printf ("Building type unit groups ...");
7871
7872 /* Sort in a separate table to maintain the order of all_type_units
7873 for .gdb_index: TU indices directly index all_type_units. */
7874 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7875 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
7876
7877 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
7878 sorted_by_abbrev.emplace_back
7879 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
7880 sig_type->per_cu.sect_off));
7881
7882 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7883 sort_tu_by_abbrev_offset);
7884
7885 abbrev_offset = (sect_offset) ~(unsigned) 0;
7886
7887 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7888 {
7889 /* Switch to the next abbrev table if necessary. */
7890 if (abbrev_table == NULL
7891 || tu.abbrev_offset != abbrev_offset)
7892 {
7893 abbrev_offset = tu.abbrev_offset;
7894 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
7895 abbrev_table =
7896 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
7897 ++tu_stats->nr_uniq_abbrev_tables;
7898 }
7899
7900 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
7901 abbrev_table.get (), nullptr, false);
7902 if (!reader.dummy_p)
7903 build_type_psymtabs_reader (&reader, reader.info_ptr,
7904 reader.comp_unit_die);
7905 }
7906 }
7907
7908 /* Print collected type unit statistics. */
7909
7910 static void
7911 print_tu_stats (dwarf2_per_objfile *per_objfile)
7912 {
7913 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7914
7915 dwarf_read_debug_printf ("Type unit statistics:");
7916 dwarf_read_debug_printf (" %zu TUs",
7917 per_objfile->per_bfd->all_type_units.size ());
7918 dwarf_read_debug_printf (" %d uniq abbrev tables",
7919 tu_stats->nr_uniq_abbrev_tables);
7920 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7921 tu_stats->nr_symtabs);
7922 dwarf_read_debug_printf (" %d symtab sharers",
7923 tu_stats->nr_symtab_sharers);
7924 dwarf_read_debug_printf (" %d type units without a stmt_list",
7925 tu_stats->nr_stmt_less_type_units);
7926 dwarf_read_debug_printf (" %d all_type_units reallocs",
7927 tu_stats->nr_all_type_units_reallocs);
7928 }
7929
7930 /* Traversal function for build_type_psymtabs. */
7931
7932 static int
7933 build_type_psymtab_dependencies (void **slot, void *info)
7934 {
7935 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7936 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7937 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7938 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7939 dwarf2_psymtab *pst = per_cu->v.psymtab;
7940 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7941 int i;
7942
7943 gdb_assert (len > 0);
7944 gdb_assert (per_cu->type_unit_group_p ());
7945
7946 pst->number_of_dependencies = len;
7947 pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
7948 for (i = 0; i < len; ++i)
7949 {
7950 struct signatured_type *iter = tu_group->tus->at (i);
7951 gdb_assert (iter->per_cu.is_debug_types);
7952 pst->dependencies[i] = iter->per_cu.v.psymtab;
7953 iter->type_unit_group = tu_group;
7954 }
7955
7956 delete tu_group->tus;
7957 tu_group->tus = nullptr;
7958
7959 return 1;
7960 }
7961
7962 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7963 Build partial symbol tables for the .debug_types comp-units. */
7964
7965 static void
7966 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
7967 {
7968 if (! create_all_type_units (per_objfile))
7969 return;
7970
7971 build_type_psymtabs_1 (per_objfile);
7972 }
7973
7974 /* Traversal function for process_skeletonless_type_unit.
7975 Read a TU in a DWO file and build partial symbols for it. */
7976
7977 static int
7978 process_skeletonless_type_unit (void **slot, void *info)
7979 {
7980 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7981 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7982 struct signatured_type find_entry, *entry;
7983
7984 /* If this TU doesn't exist in the global table, add it and read it in. */
7985
7986 if (per_objfile->per_bfd->signatured_types == NULL)
7987 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7988
7989 find_entry.signature = dwo_unit->signature;
7990 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
7991 &find_entry, INSERT);
7992 /* If we've already seen this type there's nothing to do. What's happening
7993 is we're doing our own version of comdat-folding here. */
7994 if (*slot != NULL)
7995 return 1;
7996
7997 /* This does the job that create_all_type_units would have done for
7998 this TU. */
7999 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
8000 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
8001 *slot = entry;
8002
8003 /* This does the job that build_type_psymtabs_1 would have done. */
8004 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
8005 if (!reader.dummy_p)
8006 build_type_psymtabs_reader (&reader, reader.info_ptr,
8007 reader.comp_unit_die);
8008
8009 return 1;
8010 }
8011
8012 /* Traversal function for process_skeletonless_type_units. */
8013
8014 static int
8015 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8016 {
8017 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8018
8019 if (dwo_file->tus != NULL)
8020 htab_traverse_noresize (dwo_file->tus.get (),
8021 process_skeletonless_type_unit, info);
8022
8023 return 1;
8024 }
8025
8026 /* Scan all TUs of DWO files, verifying we've processed them.
8027 This is needed in case a TU was emitted without its skeleton.
8028 Note: This can't be done until we know what all the DWO files are. */
8029
8030 static void
8031 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
8032 {
8033 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8034 if (get_dwp_file (per_objfile) == NULL
8035 && per_objfile->per_bfd->dwo_files != NULL)
8036 {
8037 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
8038 process_dwo_file_for_skeletonless_type_units,
8039 per_objfile);
8040 }
8041 }
8042
8043 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8044
8045 static void
8046 set_partial_user (dwarf2_per_objfile *per_objfile)
8047 {
8048 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
8049 {
8050 dwarf2_psymtab *pst = per_cu->v.psymtab;
8051
8052 if (pst == NULL)
8053 continue;
8054
8055 for (int j = 0; j < pst->number_of_dependencies; ++j)
8056 {
8057 /* Set the 'user' field only if it is not already set. */
8058 if (pst->dependencies[j]->user == NULL)
8059 pst->dependencies[j]->user = pst;
8060 }
8061 }
8062 }
8063
8064 /* Build the partial symbol table by doing a quick pass through the
8065 .debug_info and .debug_abbrev sections. */
8066
8067 static void
8068 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
8069 {
8070 struct objfile *objfile = per_objfile->objfile;
8071 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
8072
8073 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
8074 objfile_name (objfile));
8075
8076 scoped_restore restore_reading_psyms
8077 = make_scoped_restore (&per_objfile->per_bfd->reading_partial_symbols,
8078 true);
8079
8080 per_bfd->info.read (objfile);
8081
8082 /* Any cached compilation units will be linked by the per-objfile
8083 read_in_chain. Make sure to free them when we're done. */
8084 free_cached_comp_units freer (per_objfile);
8085
8086 build_type_psymtabs (per_objfile);
8087
8088 create_all_comp_units (per_objfile);
8089
8090 /* Create a temporary address map on a temporary obstack. We later
8091 copy this to the final obstack. */
8092 auto_obstack temp_obstack;
8093
8094 scoped_restore save_psymtabs_addrmap
8095 = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
8096 addrmap_create_mutable (&temp_obstack));
8097
8098 for (dwarf2_per_cu_data *per_cu : per_bfd->all_comp_units)
8099 {
8100 if (per_cu->v.psymtab != NULL)
8101 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8102 continue;
8103 process_psymtab_comp_unit (per_cu, per_objfile, false,
8104 language_minimal);
8105 }
8106
8107 /* This has to wait until we read the CUs, we need the list of DWOs. */
8108 process_skeletonless_type_units (per_objfile);
8109
8110 /* Now that all TUs have been processed we can fill in the dependencies. */
8111 if (per_bfd->type_unit_groups != NULL)
8112 {
8113 htab_traverse_noresize (per_bfd->type_unit_groups.get (),
8114 build_type_psymtab_dependencies, per_objfile);
8115 }
8116
8117 if (dwarf_read_debug > 0)
8118 print_tu_stats (per_objfile);
8119
8120 set_partial_user (per_objfile);
8121
8122 per_bfd->partial_symtabs->psymtabs_addrmap
8123 = addrmap_create_fixed (per_bfd->partial_symtabs->psymtabs_addrmap,
8124 per_bfd->partial_symtabs->obstack ());
8125 /* At this point we want to keep the address map. */
8126 save_psymtabs_addrmap.release ();
8127
8128 dwarf_read_debug_printf ("Done building psymtabs of %s",
8129 objfile_name (objfile));
8130 }
8131
8132 /* Load the partial DIEs for a secondary CU into memory.
8133 This is also used when rereading a primary CU with load_all_dies. */
8134
8135 static void
8136 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
8137 dwarf2_per_objfile *per_objfile,
8138 dwarf2_cu *existing_cu)
8139 {
8140 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
8141
8142 if (!reader.dummy_p)
8143 {
8144 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8145 language_minimal);
8146
8147 /* Check if comp unit has_children.
8148 If so, read the rest of the partial symbols from this comp unit.
8149 If not, there's no more debug_info for this comp unit. */
8150 if (reader.comp_unit_die->has_children)
8151 load_partial_dies (&reader, reader.info_ptr, 0);
8152
8153 reader.keep ();
8154 }
8155 }
8156
8157 static void
8158 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
8159 struct dwarf2_section_info *section,
8160 struct dwarf2_section_info *abbrev_section,
8161 unsigned int is_dwz)
8162 {
8163 const gdb_byte *info_ptr;
8164 struct objfile *objfile = per_objfile->objfile;
8165
8166 dwarf_read_debug_printf ("Reading %s for %s",
8167 section->get_name (),
8168 section->get_file_name ());
8169
8170 section->read (objfile);
8171
8172 info_ptr = section->buffer;
8173
8174 while (info_ptr < section->buffer + section->size)
8175 {
8176 struct dwarf2_per_cu_data *this_cu;
8177
8178 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8179
8180 comp_unit_head cu_header;
8181 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
8182 abbrev_section, info_ptr,
8183 rcuh_kind::COMPILE);
8184
8185 /* Save the compilation unit for later lookup. */
8186 if (cu_header.unit_type != DW_UT_type)
8187 this_cu = per_objfile->per_bfd->allocate_per_cu ();
8188 else
8189 {
8190 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
8191 sig_type->signature = cu_header.signature;
8192 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8193 this_cu = &sig_type->per_cu;
8194 }
8195 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8196 this_cu->sect_off = sect_off;
8197 this_cu->length = cu_header.length + cu_header.initial_length_size;
8198 this_cu->is_dwz = is_dwz;
8199 this_cu->section = section;
8200
8201 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8202
8203 info_ptr = info_ptr + this_cu->length;
8204 }
8205 }
8206
8207 /* Create a list of all compilation units in OBJFILE.
8208 This is only done for -readnow and building partial symtabs. */
8209
8210 static void
8211 create_all_comp_units (dwarf2_per_objfile *per_objfile)
8212 {
8213 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8214 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8215 &per_objfile->per_bfd->abbrev, 0);
8216
8217 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8218 if (dwz != NULL)
8219 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
8220 }
8221
8222 /* Process all loaded DIEs for compilation unit CU, starting at
8223 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8224 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8225 DW_AT_ranges). See the comments of add_partial_subprogram on how
8226 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8227
8228 static void
8229 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8230 CORE_ADDR *highpc, int set_addrmap,
8231 struct dwarf2_cu *cu)
8232 {
8233 struct partial_die_info *pdi;
8234
8235 /* Now, march along the PDI's, descending into ones which have
8236 interesting children but skipping the children of the other ones,
8237 until we reach the end of the compilation unit. */
8238
8239 pdi = first_die;
8240
8241 while (pdi != NULL)
8242 {
8243 pdi->fixup (cu);
8244
8245 /* Anonymous namespaces or modules have no name but have interesting
8246 children, so we need to look at them. Ditto for anonymous
8247 enums. */
8248
8249 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
8250 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8251 || pdi->tag == DW_TAG_imported_unit
8252 || pdi->tag == DW_TAG_inlined_subroutine)
8253 {
8254 switch (pdi->tag)
8255 {
8256 case DW_TAG_subprogram:
8257 case DW_TAG_inlined_subroutine:
8258 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8259 if (cu->language == language_cplus)
8260 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8261 set_addrmap, cu);
8262 break;
8263 case DW_TAG_constant:
8264 case DW_TAG_variable:
8265 case DW_TAG_typedef:
8266 case DW_TAG_union_type:
8267 if (!pdi->is_declaration
8268 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8269 {
8270 add_partial_symbol (pdi, cu);
8271 }
8272 break;
8273 case DW_TAG_class_type:
8274 case DW_TAG_interface_type:
8275 case DW_TAG_structure_type:
8276 if (!pdi->is_declaration)
8277 {
8278 add_partial_symbol (pdi, cu);
8279 }
8280 if ((cu->language == language_rust
8281 || cu->language == language_cplus) && pdi->has_children)
8282 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8283 set_addrmap, cu);
8284 break;
8285 case DW_TAG_enumeration_type:
8286 if (!pdi->is_declaration)
8287 add_partial_enumeration (pdi, cu);
8288 break;
8289 case DW_TAG_base_type:
8290 case DW_TAG_subrange_type:
8291 /* File scope base type definitions are added to the partial
8292 symbol table. */
8293 add_partial_symbol (pdi, cu);
8294 break;
8295 case DW_TAG_namespace:
8296 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8297 break;
8298 case DW_TAG_module:
8299 if (!pdi->is_declaration)
8300 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8301 break;
8302 case DW_TAG_imported_unit:
8303 {
8304 struct dwarf2_per_cu_data *per_cu;
8305
8306 /* For now we don't handle imported units in type units. */
8307 if (cu->per_cu->is_debug_types)
8308 {
8309 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8310 " supported in type units [in module %s]"),
8311 objfile_name (cu->per_objfile->objfile));
8312 }
8313
8314 per_cu = dwarf2_find_containing_comp_unit
8315 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8316
8317 /* Go read the partial unit, if needed. */
8318 if (per_cu->v.psymtab == NULL)
8319 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8320 cu->language);
8321
8322 cu->per_cu->imported_symtabs_push (per_cu);
8323 }
8324 break;
8325 case DW_TAG_imported_declaration:
8326 add_partial_symbol (pdi, cu);
8327 break;
8328 default:
8329 break;
8330 }
8331 }
8332
8333 /* If the die has a sibling, skip to the sibling. */
8334
8335 pdi = pdi->die_sibling;
8336 }
8337 }
8338
8339 /* Functions used to compute the fully scoped name of a partial DIE.
8340
8341 Normally, this is simple. For C++, the parent DIE's fully scoped
8342 name is concatenated with "::" and the partial DIE's name.
8343 Enumerators are an exception; they use the scope of their parent
8344 enumeration type, i.e. the name of the enumeration type is not
8345 prepended to the enumerator.
8346
8347 There are two complexities. One is DW_AT_specification; in this
8348 case "parent" means the parent of the target of the specification,
8349 instead of the direct parent of the DIE. The other is compilers
8350 which do not emit DW_TAG_namespace; in this case we try to guess
8351 the fully qualified name of structure types from their members'
8352 linkage names. This must be done using the DIE's children rather
8353 than the children of any DW_AT_specification target. We only need
8354 to do this for structures at the top level, i.e. if the target of
8355 any DW_AT_specification (if any; otherwise the DIE itself) does not
8356 have a parent. */
8357
8358 /* Compute the scope prefix associated with PDI's parent, in
8359 compilation unit CU. The result will be allocated on CU's
8360 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8361 field. NULL is returned if no prefix is necessary. */
8362 static const char *
8363 partial_die_parent_scope (struct partial_die_info *pdi,
8364 struct dwarf2_cu *cu)
8365 {
8366 const char *grandparent_scope;
8367 struct partial_die_info *parent, *real_pdi;
8368
8369 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8370 then this means the parent of the specification DIE. */
8371
8372 real_pdi = pdi;
8373 while (real_pdi->has_specification)
8374 {
8375 auto res = find_partial_die (real_pdi->spec_offset,
8376 real_pdi->spec_is_dwz, cu);
8377 real_pdi = res.pdi;
8378 cu = res.cu;
8379 }
8380
8381 parent = real_pdi->die_parent;
8382 if (parent == NULL)
8383 return NULL;
8384
8385 if (parent->scope_set)
8386 return parent->scope;
8387
8388 parent->fixup (cu);
8389
8390 grandparent_scope = partial_die_parent_scope (parent, cu);
8391
8392 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8393 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8394 Work around this problem here. */
8395 if (cu->language == language_cplus
8396 && parent->tag == DW_TAG_namespace
8397 && strcmp (parent->name (cu), "::") == 0
8398 && grandparent_scope == NULL)
8399 {
8400 parent->scope = NULL;
8401 parent->scope_set = 1;
8402 return NULL;
8403 }
8404
8405 /* Nested subroutines in Fortran get a prefix. */
8406 if (pdi->tag == DW_TAG_enumerator)
8407 /* Enumerators should not get the name of the enumeration as a prefix. */
8408 parent->scope = grandparent_scope;
8409 else if (parent->tag == DW_TAG_namespace
8410 || parent->tag == DW_TAG_module
8411 || parent->tag == DW_TAG_structure_type
8412 || parent->tag == DW_TAG_class_type
8413 || parent->tag == DW_TAG_interface_type
8414 || parent->tag == DW_TAG_union_type
8415 || parent->tag == DW_TAG_enumeration_type
8416 || (cu->language == language_fortran
8417 && parent->tag == DW_TAG_subprogram
8418 && pdi->tag == DW_TAG_subprogram))
8419 {
8420 if (grandparent_scope == NULL)
8421 parent->scope = parent->name (cu);
8422 else
8423 parent->scope = typename_concat (&cu->comp_unit_obstack,
8424 grandparent_scope,
8425 parent->name (cu), 0, cu);
8426 }
8427 else
8428 {
8429 /* FIXME drow/2004-04-01: What should we be doing with
8430 function-local names? For partial symbols, we should probably be
8431 ignoring them. */
8432 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8433 dwarf_tag_name (parent->tag),
8434 sect_offset_str (pdi->sect_off));
8435 parent->scope = grandparent_scope;
8436 }
8437
8438 parent->scope_set = 1;
8439 return parent->scope;
8440 }
8441
8442 /* Return the fully scoped name associated with PDI, from compilation unit
8443 CU. The result will be allocated with malloc. */
8444
8445 static gdb::unique_xmalloc_ptr<char>
8446 partial_die_full_name (struct partial_die_info *pdi,
8447 struct dwarf2_cu *cu)
8448 {
8449 const char *parent_scope;
8450
8451 /* If this is a template instantiation, we can not work out the
8452 template arguments from partial DIEs. So, unfortunately, we have
8453 to go through the full DIEs. At least any work we do building
8454 types here will be reused if full symbols are loaded later. */
8455 if (pdi->has_template_arguments)
8456 {
8457 pdi->fixup (cu);
8458
8459 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
8460 {
8461 struct die_info *die;
8462 struct attribute attr;
8463 struct dwarf2_cu *ref_cu = cu;
8464
8465 /* DW_FORM_ref_addr is using section offset. */
8466 attr.name = (enum dwarf_attribute) 0;
8467 attr.form = DW_FORM_ref_addr;
8468 attr.u.unsnd = to_underlying (pdi->sect_off);
8469 die = follow_die_ref (NULL, &attr, &ref_cu);
8470
8471 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8472 }
8473 }
8474
8475 parent_scope = partial_die_parent_scope (pdi, cu);
8476 if (parent_scope == NULL)
8477 return NULL;
8478 else
8479 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8480 pdi->name (cu),
8481 0, cu));
8482 }
8483
8484 static void
8485 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8486 {
8487 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8488 struct objfile *objfile = per_objfile->objfile;
8489 struct gdbarch *gdbarch = objfile->arch ();
8490 CORE_ADDR addr = 0;
8491 const char *actual_name = NULL;
8492 CORE_ADDR baseaddr;
8493
8494 baseaddr = objfile->text_section_offset ();
8495
8496 gdb::unique_xmalloc_ptr<char> built_actual_name
8497 = partial_die_full_name (pdi, cu);
8498 if (built_actual_name != NULL)
8499 actual_name = built_actual_name.get ();
8500
8501 if (actual_name == NULL)
8502 actual_name = pdi->name (cu);
8503
8504 partial_symbol psymbol;
8505 memset (&psymbol, 0, sizeof (psymbol));
8506 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8507 psymbol.ginfo.set_section_index (-1);
8508
8509 /* The code below indicates that the psymbol should be installed by
8510 setting this. */
8511 gdb::optional<psymbol_placement> where;
8512
8513 switch (pdi->tag)
8514 {
8515 case DW_TAG_inlined_subroutine:
8516 case DW_TAG_subprogram:
8517 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8518 - baseaddr);
8519 if (pdi->is_external
8520 || cu->language == language_ada
8521 || (cu->language == language_fortran
8522 && pdi->die_parent != NULL
8523 && pdi->die_parent->tag == DW_TAG_subprogram))
8524 {
8525 /* Normally, only "external" DIEs are part of the global scope.
8526 But in Ada and Fortran, we want to be able to access nested
8527 procedures globally. So all Ada and Fortran subprograms are
8528 stored in the global scope. */
8529 where = psymbol_placement::GLOBAL;
8530 }
8531 else
8532 where = psymbol_placement::STATIC;
8533
8534 psymbol.domain = VAR_DOMAIN;
8535 psymbol.aclass = LOC_BLOCK;
8536 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8537 psymbol.ginfo.value.address = addr;
8538
8539 if (pdi->main_subprogram && actual_name != NULL)
8540 set_objfile_main_name (objfile, actual_name, cu->language);
8541 break;
8542 case DW_TAG_constant:
8543 psymbol.domain = VAR_DOMAIN;
8544 psymbol.aclass = LOC_STATIC;
8545 where = (pdi->is_external
8546 ? psymbol_placement::GLOBAL
8547 : psymbol_placement::STATIC);
8548 break;
8549 case DW_TAG_variable:
8550 if (pdi->d.locdesc)
8551 addr = decode_locdesc (pdi->d.locdesc, cu);
8552
8553 if (pdi->d.locdesc
8554 && addr == 0
8555 && !per_objfile->per_bfd->has_section_at_zero)
8556 {
8557 /* A global or static variable may also have been stripped
8558 out by the linker if unused, in which case its address
8559 will be nullified; do not add such variables into partial
8560 symbol table then. */
8561 }
8562 else if (pdi->is_external)
8563 {
8564 /* Global Variable.
8565 Don't enter into the minimal symbol tables as there is
8566 a minimal symbol table entry from the ELF symbols already.
8567 Enter into partial symbol table if it has a location
8568 descriptor or a type.
8569 If the location descriptor is missing, new_symbol will create
8570 a LOC_UNRESOLVED symbol, the address of the variable will then
8571 be determined from the minimal symbol table whenever the variable
8572 is referenced.
8573 The address for the partial symbol table entry is not
8574 used by GDB, but it comes in handy for debugging partial symbol
8575 table building. */
8576
8577 if (pdi->d.locdesc || pdi->has_type)
8578 {
8579 psymbol.domain = VAR_DOMAIN;
8580 psymbol.aclass = LOC_STATIC;
8581 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8582 psymbol.ginfo.value.address = addr;
8583 where = psymbol_placement::GLOBAL;
8584 }
8585 }
8586 else
8587 {
8588 int has_loc = pdi->d.locdesc != NULL;
8589
8590 /* Static Variable. Skip symbols whose value we cannot know (those
8591 without location descriptors or constant values). */
8592 if (!has_loc && !pdi->has_const_value)
8593 return;
8594
8595 psymbol.domain = VAR_DOMAIN;
8596 psymbol.aclass = LOC_STATIC;
8597 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8598 if (has_loc)
8599 psymbol.ginfo.value.address = addr;
8600 where = psymbol_placement::STATIC;
8601 }
8602 break;
8603 case DW_TAG_array_type:
8604 case DW_TAG_typedef:
8605 case DW_TAG_base_type:
8606 case DW_TAG_subrange_type:
8607 psymbol.domain = VAR_DOMAIN;
8608 psymbol.aclass = LOC_TYPEDEF;
8609 where = psymbol_placement::STATIC;
8610 break;
8611 case DW_TAG_imported_declaration:
8612 case DW_TAG_namespace:
8613 psymbol.domain = VAR_DOMAIN;
8614 psymbol.aclass = LOC_TYPEDEF;
8615 where = psymbol_placement::GLOBAL;
8616 break;
8617 case DW_TAG_module:
8618 /* With Fortran 77 there might be a "BLOCK DATA" module
8619 available without any name. If so, we skip the module as it
8620 doesn't bring any value. */
8621 if (actual_name != nullptr)
8622 {
8623 psymbol.domain = MODULE_DOMAIN;
8624 psymbol.aclass = LOC_TYPEDEF;
8625 where = psymbol_placement::GLOBAL;
8626 }
8627 break;
8628 case DW_TAG_class_type:
8629 case DW_TAG_interface_type:
8630 case DW_TAG_structure_type:
8631 case DW_TAG_union_type:
8632 case DW_TAG_enumeration_type:
8633 /* Skip external references. The DWARF standard says in the section
8634 about "Structure, Union, and Class Type Entries": "An incomplete
8635 structure, union or class type is represented by a structure,
8636 union or class entry that does not have a byte size attribute
8637 and that has a DW_AT_declaration attribute." */
8638 if (!pdi->has_byte_size && pdi->is_declaration)
8639 return;
8640
8641 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8642 static vs. global. */
8643 psymbol.domain = STRUCT_DOMAIN;
8644 psymbol.aclass = LOC_TYPEDEF;
8645 where = (cu->language == language_cplus
8646 ? psymbol_placement::GLOBAL
8647 : psymbol_placement::STATIC);
8648 break;
8649 case DW_TAG_enumerator:
8650 psymbol.domain = VAR_DOMAIN;
8651 psymbol.aclass = LOC_CONST;
8652 where = (cu->language == language_cplus
8653 ? psymbol_placement::GLOBAL
8654 : psymbol_placement::STATIC);
8655 break;
8656 default:
8657 break;
8658 }
8659
8660 if (where.has_value ())
8661 {
8662 if (built_actual_name != nullptr)
8663 actual_name = objfile->intern (actual_name);
8664 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8665 psymbol.ginfo.set_linkage_name (actual_name);
8666 else
8667 {
8668 psymbol.ginfo.set_demangled_name (actual_name,
8669 &objfile->objfile_obstack);
8670 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8671 }
8672 cu->per_cu->v.psymtab->add_psymbol
8673 (psymbol, *where, per_objfile->per_bfd->partial_symtabs.get (),
8674 objfile);
8675 }
8676 }
8677
8678 /* Read a partial die corresponding to a namespace; also, add a symbol
8679 corresponding to that namespace to the symbol table. NAMESPACE is
8680 the name of the enclosing namespace. */
8681
8682 static void
8683 add_partial_namespace (struct partial_die_info *pdi,
8684 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8685 int set_addrmap, struct dwarf2_cu *cu)
8686 {
8687 /* Add a symbol for the namespace. */
8688
8689 add_partial_symbol (pdi, cu);
8690
8691 /* Now scan partial symbols in that namespace. */
8692
8693 if (pdi->has_children)
8694 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8695 }
8696
8697 /* Read a partial die corresponding to a Fortran module. */
8698
8699 static void
8700 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8701 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8702 {
8703 /* Add a symbol for the namespace. */
8704
8705 add_partial_symbol (pdi, cu);
8706
8707 /* Now scan partial symbols in that module. */
8708
8709 if (pdi->has_children)
8710 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8711 }
8712
8713 /* Read a partial die corresponding to a subprogram or an inlined
8714 subprogram and create a partial symbol for that subprogram.
8715 When the CU language allows it, this routine also defines a partial
8716 symbol for each nested subprogram that this subprogram contains.
8717 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8718 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8719
8720 PDI may also be a lexical block, in which case we simply search
8721 recursively for subprograms defined inside that lexical block.
8722 Again, this is only performed when the CU language allows this
8723 type of definitions. */
8724
8725 static void
8726 add_partial_subprogram (struct partial_die_info *pdi,
8727 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8728 int set_addrmap, struct dwarf2_cu *cu)
8729 {
8730 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8731 {
8732 if (pdi->has_pc_info)
8733 {
8734 if (pdi->lowpc < *lowpc)
8735 *lowpc = pdi->lowpc;
8736 if (pdi->highpc > *highpc)
8737 *highpc = pdi->highpc;
8738 if (set_addrmap)
8739 {
8740 struct objfile *objfile = cu->per_objfile->objfile;
8741 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
8742 struct gdbarch *gdbarch = objfile->arch ();
8743 CORE_ADDR baseaddr;
8744 CORE_ADDR this_highpc;
8745 CORE_ADDR this_lowpc;
8746
8747 baseaddr = objfile->text_section_offset ();
8748 this_lowpc
8749 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8750 pdi->lowpc + baseaddr)
8751 - baseaddr);
8752 this_highpc
8753 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8754 pdi->highpc + baseaddr)
8755 - baseaddr);
8756 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
8757 this_lowpc, this_highpc - 1,
8758 cu->per_cu->v.psymtab);
8759 }
8760 }
8761
8762 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8763 {
8764 if (!pdi->is_declaration)
8765 /* Ignore subprogram DIEs that do not have a name, they are
8766 illegal. Do not emit a complaint at this point, we will
8767 do so when we convert this psymtab into a symtab. */
8768 if (pdi->name (cu))
8769 add_partial_symbol (pdi, cu);
8770 }
8771 }
8772
8773 if (! pdi->has_children)
8774 return;
8775
8776 if (cu->language == language_ada || cu->language == language_fortran)
8777 {
8778 pdi = pdi->die_child;
8779 while (pdi != NULL)
8780 {
8781 pdi->fixup (cu);
8782 if (pdi->tag == DW_TAG_subprogram
8783 || pdi->tag == DW_TAG_inlined_subroutine
8784 || pdi->tag == DW_TAG_lexical_block)
8785 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8786 pdi = pdi->die_sibling;
8787 }
8788 }
8789 }
8790
8791 /* Read a partial die corresponding to an enumeration type. */
8792
8793 static void
8794 add_partial_enumeration (struct partial_die_info *enum_pdi,
8795 struct dwarf2_cu *cu)
8796 {
8797 struct partial_die_info *pdi;
8798
8799 if (enum_pdi->name (cu) != NULL)
8800 add_partial_symbol (enum_pdi, cu);
8801
8802 pdi = enum_pdi->die_child;
8803 while (pdi)
8804 {
8805 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8806 complaint (_("malformed enumerator DIE ignored"));
8807 else
8808 add_partial_symbol (pdi, cu);
8809 pdi = pdi->die_sibling;
8810 }
8811 }
8812
8813 /* Return the initial uleb128 in the die at INFO_PTR. */
8814
8815 static unsigned int
8816 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8817 {
8818 unsigned int bytes_read;
8819
8820 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8821 }
8822
8823 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8824 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8825
8826 Return the corresponding abbrev, or NULL if the number is zero (indicating
8827 an empty DIE). In either case *BYTES_READ will be set to the length of
8828 the initial number. */
8829
8830 static const struct abbrev_info *
8831 peek_die_abbrev (const die_reader_specs &reader,
8832 const gdb_byte *info_ptr, unsigned int *bytes_read)
8833 {
8834 dwarf2_cu *cu = reader.cu;
8835 bfd *abfd = reader.abfd;
8836 unsigned int abbrev_number
8837 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8838
8839 if (abbrev_number == 0)
8840 return NULL;
8841
8842 const abbrev_info *abbrev
8843 = reader.abbrev_table->lookup_abbrev (abbrev_number);
8844 if (!abbrev)
8845 {
8846 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8847 " at offset %s [in module %s]"),
8848 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8849 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8850 }
8851
8852 return abbrev;
8853 }
8854
8855 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8856 Returns a pointer to the end of a series of DIEs, terminated by an empty
8857 DIE. Any children of the skipped DIEs will also be skipped. */
8858
8859 static const gdb_byte *
8860 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8861 {
8862 while (1)
8863 {
8864 unsigned int bytes_read;
8865 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8866 &bytes_read);
8867
8868 if (abbrev == NULL)
8869 return info_ptr + bytes_read;
8870 else
8871 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8872 }
8873 }
8874
8875 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8876 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8877 abbrev corresponding to that skipped uleb128 should be passed in
8878 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8879 children. */
8880
8881 static const gdb_byte *
8882 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8883 const struct abbrev_info *abbrev)
8884 {
8885 unsigned int bytes_read;
8886 struct attribute attr;
8887 bfd *abfd = reader->abfd;
8888 struct dwarf2_cu *cu = reader->cu;
8889 const gdb_byte *buffer = reader->buffer;
8890 const gdb_byte *buffer_end = reader->buffer_end;
8891 unsigned int form, i;
8892
8893 for (i = 0; i < abbrev->num_attrs; i++)
8894 {
8895 /* The only abbrev we care about is DW_AT_sibling. */
8896 if (abbrev->attrs[i].name == DW_AT_sibling)
8897 {
8898 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8899 if (attr.form == DW_FORM_ref_addr)
8900 complaint (_("ignoring absolute DW_AT_sibling"));
8901 else
8902 {
8903 sect_offset off = attr.get_ref_die_offset ();
8904 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8905
8906 if (sibling_ptr < info_ptr)
8907 complaint (_("DW_AT_sibling points backwards"));
8908 else if (sibling_ptr > reader->buffer_end)
8909 reader->die_section->overflow_complaint ();
8910 else
8911 return sibling_ptr;
8912 }
8913 }
8914
8915 /* If it isn't DW_AT_sibling, skip this attribute. */
8916 form = abbrev->attrs[i].form;
8917 skip_attribute:
8918 switch (form)
8919 {
8920 case DW_FORM_ref_addr:
8921 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8922 and later it is offset sized. */
8923 if (cu->header.version == 2)
8924 info_ptr += cu->header.addr_size;
8925 else
8926 info_ptr += cu->header.offset_size;
8927 break;
8928 case DW_FORM_GNU_ref_alt:
8929 info_ptr += cu->header.offset_size;
8930 break;
8931 case DW_FORM_addr:
8932 info_ptr += cu->header.addr_size;
8933 break;
8934 case DW_FORM_data1:
8935 case DW_FORM_ref1:
8936 case DW_FORM_flag:
8937 case DW_FORM_strx1:
8938 info_ptr += 1;
8939 break;
8940 case DW_FORM_flag_present:
8941 case DW_FORM_implicit_const:
8942 break;
8943 case DW_FORM_data2:
8944 case DW_FORM_ref2:
8945 case DW_FORM_strx2:
8946 info_ptr += 2;
8947 break;
8948 case DW_FORM_strx3:
8949 info_ptr += 3;
8950 break;
8951 case DW_FORM_data4:
8952 case DW_FORM_ref4:
8953 case DW_FORM_strx4:
8954 info_ptr += 4;
8955 break;
8956 case DW_FORM_data8:
8957 case DW_FORM_ref8:
8958 case DW_FORM_ref_sig8:
8959 info_ptr += 8;
8960 break;
8961 case DW_FORM_data16:
8962 info_ptr += 16;
8963 break;
8964 case DW_FORM_string:
8965 read_direct_string (abfd, info_ptr, &bytes_read);
8966 info_ptr += bytes_read;
8967 break;
8968 case DW_FORM_sec_offset:
8969 case DW_FORM_strp:
8970 case DW_FORM_GNU_strp_alt:
8971 info_ptr += cu->header.offset_size;
8972 break;
8973 case DW_FORM_exprloc:
8974 case DW_FORM_block:
8975 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8976 info_ptr += bytes_read;
8977 break;
8978 case DW_FORM_block1:
8979 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8980 break;
8981 case DW_FORM_block2:
8982 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8983 break;
8984 case DW_FORM_block4:
8985 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8986 break;
8987 case DW_FORM_addrx:
8988 case DW_FORM_strx:
8989 case DW_FORM_sdata:
8990 case DW_FORM_udata:
8991 case DW_FORM_ref_udata:
8992 case DW_FORM_GNU_addr_index:
8993 case DW_FORM_GNU_str_index:
8994 case DW_FORM_rnglistx:
8995 case DW_FORM_loclistx:
8996 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8997 break;
8998 case DW_FORM_indirect:
8999 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9000 info_ptr += bytes_read;
9001 /* We need to continue parsing from here, so just go back to
9002 the top. */
9003 goto skip_attribute;
9004
9005 default:
9006 error (_("Dwarf Error: Cannot handle %s "
9007 "in DWARF reader [in module %s]"),
9008 dwarf_form_name (form),
9009 bfd_get_filename (abfd));
9010 }
9011 }
9012
9013 if (abbrev->has_children)
9014 return skip_children (reader, info_ptr);
9015 else
9016 return info_ptr;
9017 }
9018
9019 /* Locate ORIG_PDI's sibling.
9020 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9021
9022 static const gdb_byte *
9023 locate_pdi_sibling (const struct die_reader_specs *reader,
9024 struct partial_die_info *orig_pdi,
9025 const gdb_byte *info_ptr)
9026 {
9027 /* Do we know the sibling already? */
9028
9029 if (orig_pdi->sibling)
9030 return orig_pdi->sibling;
9031
9032 /* Are there any children to deal with? */
9033
9034 if (!orig_pdi->has_children)
9035 return info_ptr;
9036
9037 /* Skip the children the long way. */
9038
9039 return skip_children (reader, info_ptr);
9040 }
9041
9042 /* Expand this partial symbol table into a full symbol table. SELF is
9043 not NULL. */
9044
9045 void
9046 dwarf2_psymtab::read_symtab (struct objfile *objfile)
9047 {
9048 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9049
9050 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
9051
9052 /* If this psymtab is constructed from a debug-only objfile, the
9053 has_section_at_zero flag will not necessarily be correct. We
9054 can get the correct value for this flag by looking at the data
9055 associated with the (presumably stripped) associated objfile. */
9056 if (objfile->separate_debug_objfile_backlink)
9057 {
9058 dwarf2_per_objfile *per_objfile_backlink
9059 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9060
9061 per_objfile->per_bfd->has_section_at_zero
9062 = per_objfile_backlink->per_bfd->has_section_at_zero;
9063 }
9064
9065 expand_psymtab (objfile);
9066
9067 process_cu_includes (per_objfile);
9068 }
9069 \f
9070 /* Reading in full CUs. */
9071
9072 /* Add PER_CU to the queue. */
9073
9074 static void
9075 queue_comp_unit (dwarf2_per_cu_data *per_cu,
9076 dwarf2_per_objfile *per_objfile,
9077 enum language pretend_language)
9078 {
9079 per_cu->queued = 1;
9080
9081 gdb_assert (per_objfile->per_bfd->queue.has_value ());
9082 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
9083 }
9084
9085 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
9086
9087 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9088 dependency.
9089
9090 Return true if maybe_queue_comp_unit requires the caller to load the CU's
9091 DIEs, false otherwise.
9092
9093 Explanation: there is an invariant that if a CU is queued for expansion
9094 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
9095 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
9096 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
9097 are not yet loaded, the the caller must load the CU's DIEs to ensure the
9098 invariant is respected.
9099
9100 The caller is therefore not required to load the CU's DIEs (we return false)
9101 if:
9102
9103 - the CU is already expanded, and therefore does not get enqueued
9104 - the CU gets enqueued for expansion, but its DIEs are already loaded
9105
9106 Note that the caller should not use this function's return value as an
9107 indicator of whether the CU's DIEs are loaded right now, it should check
9108 that by calling `dwarf2_per_objfile::get_cu` instead. */
9109
9110 static int
9111 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9112 dwarf2_per_cu_data *per_cu,
9113 dwarf2_per_objfile *per_objfile,
9114 enum language pretend_language)
9115 {
9116 /* We may arrive here during partial symbol reading, if we need full
9117 DIEs to process an unusual case (e.g. template arguments). Do
9118 not queue PER_CU, just tell our caller to load its DIEs. */
9119 if (per_cu->per_bfd->reading_partial_symbols)
9120 {
9121 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9122
9123 if (cu == NULL || cu->dies == NULL)
9124 return 1;
9125 return 0;
9126 }
9127
9128 /* Mark the dependence relation so that we don't flush PER_CU
9129 too early. */
9130 if (dependent_cu != NULL)
9131 dwarf2_add_dependence (dependent_cu, per_cu);
9132
9133 /* If it's already on the queue, we have nothing to do. */
9134 if (per_cu->queued)
9135 {
9136 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
9137 loaded. */
9138 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
9139
9140 /* If the CU is queued for expansion, it should not already be
9141 expanded. */
9142 gdb_assert (!per_objfile->symtab_set_p (per_cu));
9143
9144 /* The DIEs are already loaded, the caller doesn't need to do it. */
9145 return 0;
9146 }
9147
9148 bool queued = false;
9149 if (!per_objfile->symtab_set_p (per_cu))
9150 {
9151 /* Add it to the queue. */
9152 queue_comp_unit (per_cu, per_objfile, pretend_language);
9153 queued = true;
9154 }
9155
9156 /* If the compilation unit is already loaded, just mark it as
9157 used. */
9158 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9159 if (cu != nullptr)
9160 cu->last_used = 0;
9161
9162 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
9163 and the DIEs are not already loaded. */
9164 return queued && cu == nullptr;
9165 }
9166
9167 /* Process the queue. */
9168
9169 static void
9170 process_queue (dwarf2_per_objfile *per_objfile)
9171 {
9172 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
9173 objfile_name (per_objfile->objfile));
9174
9175 /* The queue starts out with one item, but following a DIE reference
9176 may load a new CU, adding it to the end of the queue. */
9177 while (!per_objfile->per_bfd->queue->empty ())
9178 {
9179 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
9180 dwarf2_per_cu_data *per_cu = item.per_cu;
9181
9182 if (!per_objfile->symtab_set_p (per_cu))
9183 {
9184 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9185
9186 /* Skip dummy CUs. */
9187 if (cu != nullptr)
9188 {
9189 unsigned int debug_print_threshold;
9190 char buf[100];
9191
9192 if (per_cu->is_debug_types)
9193 {
9194 struct signatured_type *sig_type =
9195 (struct signatured_type *) per_cu;
9196
9197 sprintf (buf, "TU %s at offset %s",
9198 hex_string (sig_type->signature),
9199 sect_offset_str (per_cu->sect_off));
9200 /* There can be 100s of TUs.
9201 Only print them in verbose mode. */
9202 debug_print_threshold = 2;
9203 }
9204 else
9205 {
9206 sprintf (buf, "CU at offset %s",
9207 sect_offset_str (per_cu->sect_off));
9208 debug_print_threshold = 1;
9209 }
9210
9211 if (dwarf_read_debug >= debug_print_threshold)
9212 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
9213
9214 if (per_cu->is_debug_types)
9215 process_full_type_unit (cu, item.pretend_language);
9216 else
9217 process_full_comp_unit (cu, item.pretend_language);
9218
9219 if (dwarf_read_debug >= debug_print_threshold)
9220 dwarf_read_debug_printf ("Done expanding %s", buf);
9221 }
9222 }
9223
9224 per_cu->queued = 0;
9225 per_objfile->per_bfd->queue->pop ();
9226 }
9227
9228 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
9229 objfile_name (per_objfile->objfile));
9230 }
9231
9232 /* Read in full symbols for PST, and anything it depends on. */
9233
9234 void
9235 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9236 {
9237 gdb_assert (!readin_p (objfile));
9238
9239 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9240 free_cached_comp_units freer (per_objfile);
9241 expand_dependencies (objfile);
9242
9243 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9244 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9245 }
9246
9247 /* See psympriv.h. */
9248
9249 bool
9250 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9251 {
9252 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9253 return per_objfile->symtab_set_p (per_cu_data);
9254 }
9255
9256 /* See psympriv.h. */
9257
9258 compunit_symtab *
9259 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9260 {
9261 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9262 return per_objfile->get_symtab (per_cu_data);
9263 }
9264
9265 /* Trivial hash function for die_info: the hash value of a DIE
9266 is its offset in .debug_info for this objfile. */
9267
9268 static hashval_t
9269 die_hash (const void *item)
9270 {
9271 const struct die_info *die = (const struct die_info *) item;
9272
9273 return to_underlying (die->sect_off);
9274 }
9275
9276 /* Trivial comparison function for die_info structures: two DIEs
9277 are equal if they have the same offset. */
9278
9279 static int
9280 die_eq (const void *item_lhs, const void *item_rhs)
9281 {
9282 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9283 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9284
9285 return die_lhs->sect_off == die_rhs->sect_off;
9286 }
9287
9288 /* Load the DIEs associated with PER_CU into memory.
9289
9290 In some cases, the caller, while reading partial symbols, will need to load
9291 the full symbols for the CU for some reason. It will already have a
9292 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
9293 rather than creating a new one. */
9294
9295 static void
9296 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9297 dwarf2_per_objfile *per_objfile,
9298 dwarf2_cu *existing_cu,
9299 bool skip_partial,
9300 enum language pretend_language)
9301 {
9302 gdb_assert (! this_cu->is_debug_types);
9303
9304 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
9305 if (reader.dummy_p)
9306 return;
9307
9308 struct dwarf2_cu *cu = reader.cu;
9309 const gdb_byte *info_ptr = reader.info_ptr;
9310
9311 gdb_assert (cu->die_hash == NULL);
9312 cu->die_hash =
9313 htab_create_alloc_ex (cu->header.length / 12,
9314 die_hash,
9315 die_eq,
9316 NULL,
9317 &cu->comp_unit_obstack,
9318 hashtab_obstack_allocate,
9319 dummy_obstack_deallocate);
9320
9321 if (reader.comp_unit_die->has_children)
9322 reader.comp_unit_die->child
9323 = read_die_and_siblings (&reader, reader.info_ptr,
9324 &info_ptr, reader.comp_unit_die);
9325 cu->dies = reader.comp_unit_die;
9326 /* comp_unit_die is not stored in die_hash, no need. */
9327
9328 /* We try not to read any attributes in this function, because not
9329 all CUs needed for references have been loaded yet, and symbol
9330 table processing isn't initialized. But we have to set the CU language,
9331 or we won't be able to build types correctly.
9332 Similarly, if we do not read the producer, we can not apply
9333 producer-specific interpretation. */
9334 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9335
9336 reader.keep ();
9337 }
9338
9339 /* Add a DIE to the delayed physname list. */
9340
9341 static void
9342 add_to_method_list (struct type *type, int fnfield_index, int index,
9343 const char *name, struct die_info *die,
9344 struct dwarf2_cu *cu)
9345 {
9346 struct delayed_method_info mi;
9347 mi.type = type;
9348 mi.fnfield_index = fnfield_index;
9349 mi.index = index;
9350 mi.name = name;
9351 mi.die = die;
9352 cu->method_list.push_back (mi);
9353 }
9354
9355 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9356 "const" / "volatile". If so, decrements LEN by the length of the
9357 modifier and return true. Otherwise return false. */
9358
9359 template<size_t N>
9360 static bool
9361 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9362 {
9363 size_t mod_len = sizeof (mod) - 1;
9364 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9365 {
9366 len -= mod_len;
9367 return true;
9368 }
9369 return false;
9370 }
9371
9372 /* Compute the physnames of any methods on the CU's method list.
9373
9374 The computation of method physnames is delayed in order to avoid the
9375 (bad) condition that one of the method's formal parameters is of an as yet
9376 incomplete type. */
9377
9378 static void
9379 compute_delayed_physnames (struct dwarf2_cu *cu)
9380 {
9381 /* Only C++ delays computing physnames. */
9382 if (cu->method_list.empty ())
9383 return;
9384 gdb_assert (cu->language == language_cplus);
9385
9386 for (const delayed_method_info &mi : cu->method_list)
9387 {
9388 const char *physname;
9389 struct fn_fieldlist *fn_flp
9390 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9391 physname = dwarf2_physname (mi.name, mi.die, cu);
9392 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9393 = physname ? physname : "";
9394
9395 /* Since there's no tag to indicate whether a method is a
9396 const/volatile overload, extract that information out of the
9397 demangled name. */
9398 if (physname != NULL)
9399 {
9400 size_t len = strlen (physname);
9401
9402 while (1)
9403 {
9404 if (physname[len] == ')') /* shortcut */
9405 break;
9406 else if (check_modifier (physname, len, " const"))
9407 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9408 else if (check_modifier (physname, len, " volatile"))
9409 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9410 else
9411 break;
9412 }
9413 }
9414 }
9415
9416 /* The list is no longer needed. */
9417 cu->method_list.clear ();
9418 }
9419
9420 /* Go objects should be embedded in a DW_TAG_module DIE,
9421 and it's not clear if/how imported objects will appear.
9422 To keep Go support simple until that's worked out,
9423 go back through what we've read and create something usable.
9424 We could do this while processing each DIE, and feels kinda cleaner,
9425 but that way is more invasive.
9426 This is to, for example, allow the user to type "p var" or "b main"
9427 without having to specify the package name, and allow lookups
9428 of module.object to work in contexts that use the expression
9429 parser. */
9430
9431 static void
9432 fixup_go_packaging (struct dwarf2_cu *cu)
9433 {
9434 gdb::unique_xmalloc_ptr<char> package_name;
9435 struct pending *list;
9436 int i;
9437
9438 for (list = *cu->get_builder ()->get_global_symbols ();
9439 list != NULL;
9440 list = list->next)
9441 {
9442 for (i = 0; i < list->nsyms; ++i)
9443 {
9444 struct symbol *sym = list->symbol[i];
9445
9446 if (sym->language () == language_go
9447 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9448 {
9449 gdb::unique_xmalloc_ptr<char> this_package_name
9450 (go_symbol_package_name (sym));
9451
9452 if (this_package_name == NULL)
9453 continue;
9454 if (package_name == NULL)
9455 package_name = std::move (this_package_name);
9456 else
9457 {
9458 struct objfile *objfile = cu->per_objfile->objfile;
9459 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9460 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9461 (symbol_symtab (sym) != NULL
9462 ? symtab_to_filename_for_display
9463 (symbol_symtab (sym))
9464 : objfile_name (objfile)),
9465 this_package_name.get (), package_name.get ());
9466 }
9467 }
9468 }
9469 }
9470
9471 if (package_name != NULL)
9472 {
9473 struct objfile *objfile = cu->per_objfile->objfile;
9474 const char *saved_package_name = objfile->intern (package_name.get ());
9475 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9476 saved_package_name);
9477 struct symbol *sym;
9478
9479 sym = new (&objfile->objfile_obstack) symbol;
9480 sym->set_language (language_go, &objfile->objfile_obstack);
9481 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9482 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9483 e.g., "main" finds the "main" module and not C's main(). */
9484 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9485 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9486 SYMBOL_TYPE (sym) = type;
9487
9488 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9489 }
9490 }
9491
9492 /* Allocate a fully-qualified name consisting of the two parts on the
9493 obstack. */
9494
9495 static const char *
9496 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9497 {
9498 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9499 }
9500
9501 /* A helper that allocates a variant part to attach to a Rust enum
9502 type. OBSTACK is where the results should be allocated. TYPE is
9503 the type we're processing. DISCRIMINANT_INDEX is the index of the
9504 discriminant. It must be the index of one of the fields of TYPE,
9505 or -1 to mean there is no discriminant (univariant enum).
9506 DEFAULT_INDEX is the index of the default field; or -1 if there is
9507 no default. RANGES is indexed by "effective" field number (the
9508 field index, but omitting the discriminant and default fields) and
9509 must hold the discriminant values used by the variants. Note that
9510 RANGES must have a lifetime at least as long as OBSTACK -- either
9511 already allocated on it, or static. */
9512
9513 static void
9514 alloc_rust_variant (struct obstack *obstack, struct type *type,
9515 int discriminant_index, int default_index,
9516 gdb::array_view<discriminant_range> ranges)
9517 {
9518 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9519 gdb_assert (discriminant_index == -1
9520 || (discriminant_index >= 0
9521 && discriminant_index < type->num_fields ()));
9522 gdb_assert (default_index == -1
9523 || (default_index >= 0 && default_index < type->num_fields ()));
9524
9525 /* We have one variant for each non-discriminant field. */
9526 int n_variants = type->num_fields ();
9527 if (discriminant_index != -1)
9528 --n_variants;
9529
9530 variant *variants = new (obstack) variant[n_variants];
9531 int var_idx = 0;
9532 int range_idx = 0;
9533 for (int i = 0; i < type->num_fields (); ++i)
9534 {
9535 if (i == discriminant_index)
9536 continue;
9537
9538 variants[var_idx].first_field = i;
9539 variants[var_idx].last_field = i + 1;
9540
9541 /* The default field does not need a range, but other fields do.
9542 We skipped the discriminant above. */
9543 if (i != default_index)
9544 {
9545 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9546 ++range_idx;
9547 }
9548
9549 ++var_idx;
9550 }
9551
9552 gdb_assert (range_idx == ranges.size ());
9553 gdb_assert (var_idx == n_variants);
9554
9555 variant_part *part = new (obstack) variant_part;
9556 part->discriminant_index = discriminant_index;
9557 /* If there is no discriminant, then whether it is signed is of no
9558 consequence. */
9559 part->is_unsigned
9560 = (discriminant_index == -1
9561 ? false
9562 : type->field (discriminant_index).type ()->is_unsigned ());
9563 part->variants = gdb::array_view<variant> (variants, n_variants);
9564
9565 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9566 gdb::array_view<variant_part> *prop_value
9567 = new (storage) gdb::array_view<variant_part> (part, 1);
9568
9569 struct dynamic_prop prop;
9570 prop.set_variant_parts (prop_value);
9571
9572 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9573 }
9574
9575 /* Some versions of rustc emitted enums in an unusual way.
9576
9577 Ordinary enums were emitted as unions. The first element of each
9578 structure in the union was named "RUST$ENUM$DISR". This element
9579 held the discriminant.
9580
9581 These versions of Rust also implemented the "non-zero"
9582 optimization. When the enum had two values, and one is empty and
9583 the other holds a pointer that cannot be zero, the pointer is used
9584 as the discriminant, with a zero value meaning the empty variant.
9585 Here, the union's first member is of the form
9586 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9587 where the fieldnos are the indices of the fields that should be
9588 traversed in order to find the field (which may be several fields deep)
9589 and the variantname is the name of the variant of the case when the
9590 field is zero.
9591
9592 This function recognizes whether TYPE is of one of these forms,
9593 and, if so, smashes it to be a variant type. */
9594
9595 static void
9596 quirk_rust_enum (struct type *type, struct objfile *objfile)
9597 {
9598 gdb_assert (type->code () == TYPE_CODE_UNION);
9599
9600 /* We don't need to deal with empty enums. */
9601 if (type->num_fields () == 0)
9602 return;
9603
9604 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9605 if (type->num_fields () == 1
9606 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9607 {
9608 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9609
9610 /* Decode the field name to find the offset of the
9611 discriminant. */
9612 ULONGEST bit_offset = 0;
9613 struct type *field_type = type->field (0).type ();
9614 while (name[0] >= '0' && name[0] <= '9')
9615 {
9616 char *tail;
9617 unsigned long index = strtoul (name, &tail, 10);
9618 name = tail;
9619 if (*name != '$'
9620 || index >= field_type->num_fields ()
9621 || (TYPE_FIELD_LOC_KIND (field_type, index)
9622 != FIELD_LOC_KIND_BITPOS))
9623 {
9624 complaint (_("Could not parse Rust enum encoding string \"%s\""
9625 "[in module %s]"),
9626 TYPE_FIELD_NAME (type, 0),
9627 objfile_name (objfile));
9628 return;
9629 }
9630 ++name;
9631
9632 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9633 field_type = field_type->field (index).type ();
9634 }
9635
9636 /* Smash this type to be a structure type. We have to do this
9637 because the type has already been recorded. */
9638 type->set_code (TYPE_CODE_STRUCT);
9639 type->set_num_fields (3);
9640 /* Save the field we care about. */
9641 struct field saved_field = type->field (0);
9642 type->set_fields
9643 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9644
9645 /* Put the discriminant at index 0. */
9646 type->field (0).set_type (field_type);
9647 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9648 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9649 SET_FIELD_BITPOS (type->field (0), bit_offset);
9650
9651 /* The order of fields doesn't really matter, so put the real
9652 field at index 1 and the data-less field at index 2. */
9653 type->field (1) = saved_field;
9654 TYPE_FIELD_NAME (type, 1)
9655 = rust_last_path_segment (type->field (1).type ()->name ());
9656 type->field (1).type ()->set_name
9657 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9658 TYPE_FIELD_NAME (type, 1)));
9659
9660 const char *dataless_name
9661 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9662 name);
9663 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9664 dataless_name);
9665 type->field (2).set_type (dataless_type);
9666 /* NAME points into the original discriminant name, which
9667 already has the correct lifetime. */
9668 TYPE_FIELD_NAME (type, 2) = name;
9669 SET_FIELD_BITPOS (type->field (2), 0);
9670
9671 /* Indicate that this is a variant type. */
9672 static discriminant_range ranges[1] = { { 0, 0 } };
9673 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9674 }
9675 /* A union with a single anonymous field is probably an old-style
9676 univariant enum. */
9677 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9678 {
9679 /* Smash this type to be a structure type. We have to do this
9680 because the type has already been recorded. */
9681 type->set_code (TYPE_CODE_STRUCT);
9682
9683 struct type *field_type = type->field (0).type ();
9684 const char *variant_name
9685 = rust_last_path_segment (field_type->name ());
9686 TYPE_FIELD_NAME (type, 0) = variant_name;
9687 field_type->set_name
9688 (rust_fully_qualify (&objfile->objfile_obstack,
9689 type->name (), variant_name));
9690
9691 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
9692 }
9693 else
9694 {
9695 struct type *disr_type = nullptr;
9696 for (int i = 0; i < type->num_fields (); ++i)
9697 {
9698 disr_type = type->field (i).type ();
9699
9700 if (disr_type->code () != TYPE_CODE_STRUCT)
9701 {
9702 /* All fields of a true enum will be structs. */
9703 return;
9704 }
9705 else if (disr_type->num_fields () == 0)
9706 {
9707 /* Could be data-less variant, so keep going. */
9708 disr_type = nullptr;
9709 }
9710 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9711 "RUST$ENUM$DISR") != 0)
9712 {
9713 /* Not a Rust enum. */
9714 return;
9715 }
9716 else
9717 {
9718 /* Found one. */
9719 break;
9720 }
9721 }
9722
9723 /* If we got here without a discriminant, then it's probably
9724 just a union. */
9725 if (disr_type == nullptr)
9726 return;
9727
9728 /* Smash this type to be a structure type. We have to do this
9729 because the type has already been recorded. */
9730 type->set_code (TYPE_CODE_STRUCT);
9731
9732 /* Make space for the discriminant field. */
9733 struct field *disr_field = &disr_type->field (0);
9734 field *new_fields
9735 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9736 * sizeof (struct field)));
9737 memcpy (new_fields + 1, type->fields (),
9738 type->num_fields () * sizeof (struct field));
9739 type->set_fields (new_fields);
9740 type->set_num_fields (type->num_fields () + 1);
9741
9742 /* Install the discriminant at index 0 in the union. */
9743 type->field (0) = *disr_field;
9744 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9745 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9746
9747 /* We need a way to find the correct discriminant given a
9748 variant name. For convenience we build a map here. */
9749 struct type *enum_type = disr_field->type ();
9750 std::unordered_map<std::string, ULONGEST> discriminant_map;
9751 for (int i = 0; i < enum_type->num_fields (); ++i)
9752 {
9753 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9754 {
9755 const char *name
9756 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9757 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9758 }
9759 }
9760
9761 int n_fields = type->num_fields ();
9762 /* We don't need a range entry for the discriminant, but we do
9763 need one for every other field, as there is no default
9764 variant. */
9765 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9766 discriminant_range,
9767 n_fields - 1);
9768 /* Skip the discriminant here. */
9769 for (int i = 1; i < n_fields; ++i)
9770 {
9771 /* Find the final word in the name of this variant's type.
9772 That name can be used to look up the correct
9773 discriminant. */
9774 const char *variant_name
9775 = rust_last_path_segment (type->field (i).type ()->name ());
9776
9777 auto iter = discriminant_map.find (variant_name);
9778 if (iter != discriminant_map.end ())
9779 {
9780 ranges[i - 1].low = iter->second;
9781 ranges[i - 1].high = iter->second;
9782 }
9783
9784 /* In Rust, each element should have the size of the
9785 enclosing enum. */
9786 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9787
9788 /* Remove the discriminant field, if it exists. */
9789 struct type *sub_type = type->field (i).type ();
9790 if (sub_type->num_fields () > 0)
9791 {
9792 sub_type->set_num_fields (sub_type->num_fields () - 1);
9793 sub_type->set_fields (sub_type->fields () + 1);
9794 }
9795 TYPE_FIELD_NAME (type, i) = variant_name;
9796 sub_type->set_name
9797 (rust_fully_qualify (&objfile->objfile_obstack,
9798 type->name (), variant_name));
9799 }
9800
9801 /* Indicate that this is a variant type. */
9802 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9803 gdb::array_view<discriminant_range> (ranges,
9804 n_fields - 1));
9805 }
9806 }
9807
9808 /* Rewrite some Rust unions to be structures with variants parts. */
9809
9810 static void
9811 rust_union_quirks (struct dwarf2_cu *cu)
9812 {
9813 gdb_assert (cu->language == language_rust);
9814 for (type *type_ : cu->rust_unions)
9815 quirk_rust_enum (type_, cu->per_objfile->objfile);
9816 /* We don't need this any more. */
9817 cu->rust_unions.clear ();
9818 }
9819
9820 /* See read.h. */
9821
9822 type_unit_group_unshareable *
9823 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9824 {
9825 auto iter = this->m_type_units.find (tu_group);
9826 if (iter != this->m_type_units.end ())
9827 return iter->second.get ();
9828
9829 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9830 type_unit_group_unshareable *result = uniq.get ();
9831 this->m_type_units[tu_group] = std::move (uniq);
9832 return result;
9833 }
9834
9835 struct type *
9836 dwarf2_per_objfile::get_type_for_signatured_type
9837 (signatured_type *sig_type) const
9838 {
9839 auto iter = this->m_type_map.find (sig_type);
9840 if (iter == this->m_type_map.end ())
9841 return nullptr;
9842
9843 return iter->second;
9844 }
9845
9846 void dwarf2_per_objfile::set_type_for_signatured_type
9847 (signatured_type *sig_type, struct type *type)
9848 {
9849 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9850
9851 this->m_type_map[sig_type] = type;
9852 }
9853
9854 /* A helper function for computing the list of all symbol tables
9855 included by PER_CU. */
9856
9857 static void
9858 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9859 htab_t all_children, htab_t all_type_symtabs,
9860 dwarf2_per_cu_data *per_cu,
9861 dwarf2_per_objfile *per_objfile,
9862 struct compunit_symtab *immediate_parent)
9863 {
9864 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9865 if (*slot != NULL)
9866 {
9867 /* This inclusion and its children have been processed. */
9868 return;
9869 }
9870
9871 *slot = per_cu;
9872
9873 /* Only add a CU if it has a symbol table. */
9874 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9875 if (cust != NULL)
9876 {
9877 /* If this is a type unit only add its symbol table if we haven't
9878 seen it yet (type unit per_cu's can share symtabs). */
9879 if (per_cu->is_debug_types)
9880 {
9881 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9882 if (*slot == NULL)
9883 {
9884 *slot = cust;
9885 result->push_back (cust);
9886 if (cust->user == NULL)
9887 cust->user = immediate_parent;
9888 }
9889 }
9890 else
9891 {
9892 result->push_back (cust);
9893 if (cust->user == NULL)
9894 cust->user = immediate_parent;
9895 }
9896 }
9897
9898 if (!per_cu->imported_symtabs_empty ())
9899 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9900 {
9901 recursively_compute_inclusions (result, all_children,
9902 all_type_symtabs, ptr, per_objfile,
9903 cust);
9904 }
9905 }
9906
9907 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9908 PER_CU. */
9909
9910 static void
9911 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9912 dwarf2_per_objfile *per_objfile)
9913 {
9914 gdb_assert (! per_cu->is_debug_types);
9915
9916 if (!per_cu->imported_symtabs_empty ())
9917 {
9918 int len;
9919 std::vector<compunit_symtab *> result_symtabs;
9920 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9921
9922 /* If we don't have a symtab, we can just skip this case. */
9923 if (cust == NULL)
9924 return;
9925
9926 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9927 htab_eq_pointer,
9928 NULL, xcalloc, xfree));
9929 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9930 htab_eq_pointer,
9931 NULL, xcalloc, xfree));
9932
9933 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9934 {
9935 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9936 all_type_symtabs.get (), ptr,
9937 per_objfile, cust);
9938 }
9939
9940 /* Now we have a transitive closure of all the included symtabs. */
9941 len = result_symtabs.size ();
9942 cust->includes
9943 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9944 struct compunit_symtab *, len + 1);
9945 memcpy (cust->includes, result_symtabs.data (),
9946 len * sizeof (compunit_symtab *));
9947 cust->includes[len] = NULL;
9948 }
9949 }
9950
9951 /* Compute the 'includes' field for the symtabs of all the CUs we just
9952 read. */
9953
9954 static void
9955 process_cu_includes (dwarf2_per_objfile *per_objfile)
9956 {
9957 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
9958 {
9959 if (! iter->is_debug_types)
9960 compute_compunit_symtab_includes (iter, per_objfile);
9961 }
9962
9963 per_objfile->per_bfd->just_read_cus.clear ();
9964 }
9965
9966 /* Generate full symbol information for CU, whose DIEs have
9967 already been loaded into memory. */
9968
9969 static void
9970 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9971 {
9972 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9973 struct objfile *objfile = per_objfile->objfile;
9974 struct gdbarch *gdbarch = objfile->arch ();
9975 CORE_ADDR lowpc, highpc;
9976 struct compunit_symtab *cust;
9977 CORE_ADDR baseaddr;
9978 struct block *static_block;
9979 CORE_ADDR addr;
9980
9981 baseaddr = objfile->text_section_offset ();
9982
9983 /* Clear the list here in case something was left over. */
9984 cu->method_list.clear ();
9985
9986 cu->language = pretend_language;
9987 cu->language_defn = language_def (cu->language);
9988
9989 dwarf2_find_base_address (cu->dies, cu);
9990
9991 /* Do line number decoding in read_file_scope () */
9992 process_die (cu->dies, cu);
9993
9994 /* For now fudge the Go package. */
9995 if (cu->language == language_go)
9996 fixup_go_packaging (cu);
9997
9998 /* Now that we have processed all the DIEs in the CU, all the types
9999 should be complete, and it should now be safe to compute all of the
10000 physnames. */
10001 compute_delayed_physnames (cu);
10002
10003 if (cu->language == language_rust)
10004 rust_union_quirks (cu);
10005
10006 /* Some compilers don't define a DW_AT_high_pc attribute for the
10007 compilation unit. If the DW_AT_high_pc is missing, synthesize
10008 it, by scanning the DIE's below the compilation unit. */
10009 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10010
10011 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10012 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10013
10014 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10015 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10016 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10017 addrmap to help ensure it has an accurate map of pc values belonging to
10018 this comp unit. */
10019 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10020
10021 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10022 SECT_OFF_TEXT (objfile),
10023 0);
10024
10025 if (cust != NULL)
10026 {
10027 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10028
10029 /* Set symtab language to language from DW_AT_language. If the
10030 compilation is from a C file generated by language preprocessors, do
10031 not set the language if it was already deduced by start_subfile. */
10032 if (!(cu->language == language_c
10033 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10034 COMPUNIT_FILETABS (cust)->language = cu->language;
10035
10036 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10037 produce DW_AT_location with location lists but it can be possibly
10038 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10039 there were bugs in prologue debug info, fixed later in GCC-4.5
10040 by "unwind info for epilogues" patch (which is not directly related).
10041
10042 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10043 needed, it would be wrong due to missing DW_AT_producer there.
10044
10045 Still one can confuse GDB by using non-standard GCC compilation
10046 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10047 */
10048 if (cu->has_loclist && gcc_4_minor >= 5)
10049 cust->locations_valid = 1;
10050
10051 if (gcc_4_minor >= 5)
10052 cust->epilogue_unwind_valid = 1;
10053
10054 cust->call_site_htab = cu->call_site_htab;
10055 }
10056
10057 per_objfile->set_symtab (cu->per_cu, cust);
10058
10059 /* Push it for inclusion processing later. */
10060 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
10061
10062 /* Not needed any more. */
10063 cu->reset_builder ();
10064 }
10065
10066 /* Generate full symbol information for type unit CU, whose DIEs have
10067 already been loaded into memory. */
10068
10069 static void
10070 process_full_type_unit (dwarf2_cu *cu,
10071 enum language pretend_language)
10072 {
10073 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10074 struct objfile *objfile = per_objfile->objfile;
10075 struct compunit_symtab *cust;
10076 struct signatured_type *sig_type;
10077
10078 gdb_assert (cu->per_cu->is_debug_types);
10079 sig_type = (struct signatured_type *) cu->per_cu;
10080
10081 /* Clear the list here in case something was left over. */
10082 cu->method_list.clear ();
10083
10084 cu->language = pretend_language;
10085 cu->language_defn = language_def (cu->language);
10086
10087 /* The symbol tables are set up in read_type_unit_scope. */
10088 process_die (cu->dies, cu);
10089
10090 /* For now fudge the Go package. */
10091 if (cu->language == language_go)
10092 fixup_go_packaging (cu);
10093
10094 /* Now that we have processed all the DIEs in the CU, all the types
10095 should be complete, and it should now be safe to compute all of the
10096 physnames. */
10097 compute_delayed_physnames (cu);
10098
10099 if (cu->language == language_rust)
10100 rust_union_quirks (cu);
10101
10102 /* TUs share symbol tables.
10103 If this is the first TU to use this symtab, complete the construction
10104 of it with end_expandable_symtab. Otherwise, complete the addition of
10105 this TU's symbols to the existing symtab. */
10106 type_unit_group_unshareable *tug_unshare =
10107 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
10108 if (tug_unshare->compunit_symtab == NULL)
10109 {
10110 buildsym_compunit *builder = cu->get_builder ();
10111 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10112 tug_unshare->compunit_symtab = cust;
10113
10114 if (cust != NULL)
10115 {
10116 /* Set symtab language to language from DW_AT_language. If the
10117 compilation is from a C file generated by language preprocessors,
10118 do not set the language if it was already deduced by
10119 start_subfile. */
10120 if (!(cu->language == language_c
10121 && COMPUNIT_FILETABS (cust)->language != language_c))
10122 COMPUNIT_FILETABS (cust)->language = cu->language;
10123 }
10124 }
10125 else
10126 {
10127 cu->get_builder ()->augment_type_symtab ();
10128 cust = tug_unshare->compunit_symtab;
10129 }
10130
10131 per_objfile->set_symtab (cu->per_cu, cust);
10132
10133 /* Not needed any more. */
10134 cu->reset_builder ();
10135 }
10136
10137 /* Process an imported unit DIE. */
10138
10139 static void
10140 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10141 {
10142 struct attribute *attr;
10143
10144 /* For now we don't handle imported units in type units. */
10145 if (cu->per_cu->is_debug_types)
10146 {
10147 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10148 " supported in type units [in module %s]"),
10149 objfile_name (cu->per_objfile->objfile));
10150 }
10151
10152 attr = dwarf2_attr (die, DW_AT_import, cu);
10153 if (attr != NULL)
10154 {
10155 sect_offset sect_off = attr->get_ref_die_offset ();
10156 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10157 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10158 dwarf2_per_cu_data *per_cu
10159 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
10160
10161 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10162 into another compilation unit, at root level. Regard this as a hint,
10163 and ignore it. */
10164 if (die->parent && die->parent->parent == NULL
10165 && per_cu->unit_type == DW_UT_compile
10166 && per_cu->lang == language_cplus)
10167 return;
10168
10169 /* If necessary, add it to the queue and load its DIEs. */
10170 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
10171 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
10172 false, cu->language);
10173
10174 cu->per_cu->imported_symtabs_push (per_cu);
10175 }
10176 }
10177
10178 /* RAII object that represents a process_die scope: i.e.,
10179 starts/finishes processing a DIE. */
10180 class process_die_scope
10181 {
10182 public:
10183 process_die_scope (die_info *die, dwarf2_cu *cu)
10184 : m_die (die), m_cu (cu)
10185 {
10186 /* We should only be processing DIEs not already in process. */
10187 gdb_assert (!m_die->in_process);
10188 m_die->in_process = true;
10189 }
10190
10191 ~process_die_scope ()
10192 {
10193 m_die->in_process = false;
10194
10195 /* If we're done processing the DIE for the CU that owns the line
10196 header, we don't need the line header anymore. */
10197 if (m_cu->line_header_die_owner == m_die)
10198 {
10199 delete m_cu->line_header;
10200 m_cu->line_header = NULL;
10201 m_cu->line_header_die_owner = NULL;
10202 }
10203 }
10204
10205 private:
10206 die_info *m_die;
10207 dwarf2_cu *m_cu;
10208 };
10209
10210 /* Process a die and its children. */
10211
10212 static void
10213 process_die (struct die_info *die, struct dwarf2_cu *cu)
10214 {
10215 process_die_scope scope (die, cu);
10216
10217 switch (die->tag)
10218 {
10219 case DW_TAG_padding:
10220 break;
10221 case DW_TAG_compile_unit:
10222 case DW_TAG_partial_unit:
10223 read_file_scope (die, cu);
10224 break;
10225 case DW_TAG_type_unit:
10226 read_type_unit_scope (die, cu);
10227 break;
10228 case DW_TAG_subprogram:
10229 /* Nested subprograms in Fortran get a prefix. */
10230 if (cu->language == language_fortran
10231 && die->parent != NULL
10232 && die->parent->tag == DW_TAG_subprogram)
10233 cu->processing_has_namespace_info = true;
10234 /* Fall through. */
10235 case DW_TAG_inlined_subroutine:
10236 read_func_scope (die, cu);
10237 break;
10238 case DW_TAG_lexical_block:
10239 case DW_TAG_try_block:
10240 case DW_TAG_catch_block:
10241 read_lexical_block_scope (die, cu);
10242 break;
10243 case DW_TAG_call_site:
10244 case DW_TAG_GNU_call_site:
10245 read_call_site_scope (die, cu);
10246 break;
10247 case DW_TAG_class_type:
10248 case DW_TAG_interface_type:
10249 case DW_TAG_structure_type:
10250 case DW_TAG_union_type:
10251 process_structure_scope (die, cu);
10252 break;
10253 case DW_TAG_enumeration_type:
10254 process_enumeration_scope (die, cu);
10255 break;
10256
10257 /* These dies have a type, but processing them does not create
10258 a symbol or recurse to process the children. Therefore we can
10259 read them on-demand through read_type_die. */
10260 case DW_TAG_subroutine_type:
10261 case DW_TAG_set_type:
10262 case DW_TAG_pointer_type:
10263 case DW_TAG_ptr_to_member_type:
10264 case DW_TAG_reference_type:
10265 case DW_TAG_rvalue_reference_type:
10266 case DW_TAG_string_type:
10267 break;
10268
10269 case DW_TAG_array_type:
10270 /* We only need to handle this case for Ada -- in other
10271 languages, it's normal for the compiler to emit a typedef
10272 instead. */
10273 if (cu->language != language_ada)
10274 break;
10275 /* FALLTHROUGH */
10276 case DW_TAG_base_type:
10277 case DW_TAG_subrange_type:
10278 case DW_TAG_typedef:
10279 /* Add a typedef symbol for the type definition, if it has a
10280 DW_AT_name. */
10281 new_symbol (die, read_type_die (die, cu), cu);
10282 break;
10283 case DW_TAG_common_block:
10284 read_common_block (die, cu);
10285 break;
10286 case DW_TAG_common_inclusion:
10287 break;
10288 case DW_TAG_namespace:
10289 cu->processing_has_namespace_info = true;
10290 read_namespace (die, cu);
10291 break;
10292 case DW_TAG_module:
10293 cu->processing_has_namespace_info = true;
10294 read_module (die, cu);
10295 break;
10296 case DW_TAG_imported_declaration:
10297 cu->processing_has_namespace_info = true;
10298 if (read_namespace_alias (die, cu))
10299 break;
10300 /* The declaration is not a global namespace alias. */
10301 /* Fall through. */
10302 case DW_TAG_imported_module:
10303 cu->processing_has_namespace_info = true;
10304 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10305 || cu->language != language_fortran))
10306 complaint (_("Tag '%s' has unexpected children"),
10307 dwarf_tag_name (die->tag));
10308 read_import_statement (die, cu);
10309 break;
10310
10311 case DW_TAG_imported_unit:
10312 process_imported_unit_die (die, cu);
10313 break;
10314
10315 case DW_TAG_variable:
10316 read_variable (die, cu);
10317 break;
10318
10319 default:
10320 new_symbol (die, NULL, cu);
10321 break;
10322 }
10323 }
10324 \f
10325 /* DWARF name computation. */
10326
10327 /* A helper function for dwarf2_compute_name which determines whether DIE
10328 needs to have the name of the scope prepended to the name listed in the
10329 die. */
10330
10331 static int
10332 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10333 {
10334 struct attribute *attr;
10335
10336 switch (die->tag)
10337 {
10338 case DW_TAG_namespace:
10339 case DW_TAG_typedef:
10340 case DW_TAG_class_type:
10341 case DW_TAG_interface_type:
10342 case DW_TAG_structure_type:
10343 case DW_TAG_union_type:
10344 case DW_TAG_enumeration_type:
10345 case DW_TAG_enumerator:
10346 case DW_TAG_subprogram:
10347 case DW_TAG_inlined_subroutine:
10348 case DW_TAG_member:
10349 case DW_TAG_imported_declaration:
10350 return 1;
10351
10352 case DW_TAG_variable:
10353 case DW_TAG_constant:
10354 /* We only need to prefix "globally" visible variables. These include
10355 any variable marked with DW_AT_external or any variable that
10356 lives in a namespace. [Variables in anonymous namespaces
10357 require prefixing, but they are not DW_AT_external.] */
10358
10359 if (dwarf2_attr (die, DW_AT_specification, cu))
10360 {
10361 struct dwarf2_cu *spec_cu = cu;
10362
10363 return die_needs_namespace (die_specification (die, &spec_cu),
10364 spec_cu);
10365 }
10366
10367 attr = dwarf2_attr (die, DW_AT_external, cu);
10368 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10369 && die->parent->tag != DW_TAG_module)
10370 return 0;
10371 /* A variable in a lexical block of some kind does not need a
10372 namespace, even though in C++ such variables may be external
10373 and have a mangled name. */
10374 if (die->parent->tag == DW_TAG_lexical_block
10375 || die->parent->tag == DW_TAG_try_block
10376 || die->parent->tag == DW_TAG_catch_block
10377 || die->parent->tag == DW_TAG_subprogram)
10378 return 0;
10379 return 1;
10380
10381 default:
10382 return 0;
10383 }
10384 }
10385
10386 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10387 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10388 defined for the given DIE. */
10389
10390 static struct attribute *
10391 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10392 {
10393 struct attribute *attr;
10394
10395 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10396 if (attr == NULL)
10397 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10398
10399 return attr;
10400 }
10401
10402 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10403 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10404 defined for the given DIE. */
10405
10406 static const char *
10407 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10408 {
10409 const char *linkage_name;
10410
10411 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10412 if (linkage_name == NULL)
10413 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10414
10415 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10416 See https://github.com/rust-lang/rust/issues/32925. */
10417 if (cu->language == language_rust && linkage_name != NULL
10418 && strchr (linkage_name, '{') != NULL)
10419 linkage_name = NULL;
10420
10421 return linkage_name;
10422 }
10423
10424 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10425 compute the physname for the object, which include a method's:
10426 - formal parameters (C++),
10427 - receiver type (Go),
10428
10429 The term "physname" is a bit confusing.
10430 For C++, for example, it is the demangled name.
10431 For Go, for example, it's the mangled name.
10432
10433 For Ada, return the DIE's linkage name rather than the fully qualified
10434 name. PHYSNAME is ignored..
10435
10436 The result is allocated on the objfile->per_bfd's obstack and
10437 canonicalized. */
10438
10439 static const char *
10440 dwarf2_compute_name (const char *name,
10441 struct die_info *die, struct dwarf2_cu *cu,
10442 int physname)
10443 {
10444 struct objfile *objfile = cu->per_objfile->objfile;
10445
10446 if (name == NULL)
10447 name = dwarf2_name (die, cu);
10448
10449 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10450 but otherwise compute it by typename_concat inside GDB.
10451 FIXME: Actually this is not really true, or at least not always true.
10452 It's all very confusing. compute_and_set_names doesn't try to demangle
10453 Fortran names because there is no mangling standard. So new_symbol
10454 will set the demangled name to the result of dwarf2_full_name, and it is
10455 the demangled name that GDB uses if it exists. */
10456 if (cu->language == language_ada
10457 || (cu->language == language_fortran && physname))
10458 {
10459 /* For Ada unit, we prefer the linkage name over the name, as
10460 the former contains the exported name, which the user expects
10461 to be able to reference. Ideally, we want the user to be able
10462 to reference this entity using either natural or linkage name,
10463 but we haven't started looking at this enhancement yet. */
10464 const char *linkage_name = dw2_linkage_name (die, cu);
10465
10466 if (linkage_name != NULL)
10467 return linkage_name;
10468 }
10469
10470 /* These are the only languages we know how to qualify names in. */
10471 if (name != NULL
10472 && (cu->language == language_cplus
10473 || cu->language == language_fortran || cu->language == language_d
10474 || cu->language == language_rust))
10475 {
10476 if (die_needs_namespace (die, cu))
10477 {
10478 const char *prefix;
10479 const char *canonical_name = NULL;
10480
10481 string_file buf;
10482
10483 prefix = determine_prefix (die, cu);
10484 if (*prefix != '\0')
10485 {
10486 gdb::unique_xmalloc_ptr<char> prefixed_name
10487 (typename_concat (NULL, prefix, name, physname, cu));
10488
10489 buf.puts (prefixed_name.get ());
10490 }
10491 else
10492 buf.puts (name);
10493
10494 /* Template parameters may be specified in the DIE's DW_AT_name, or
10495 as children with DW_TAG_template_type_param or
10496 DW_TAG_value_type_param. If the latter, add them to the name
10497 here. If the name already has template parameters, then
10498 skip this step; some versions of GCC emit both, and
10499 it is more efficient to use the pre-computed name.
10500
10501 Something to keep in mind about this process: it is very
10502 unlikely, or in some cases downright impossible, to produce
10503 something that will match the mangled name of a function.
10504 If the definition of the function has the same debug info,
10505 we should be able to match up with it anyway. But fallbacks
10506 using the minimal symbol, for instance to find a method
10507 implemented in a stripped copy of libstdc++, will not work.
10508 If we do not have debug info for the definition, we will have to
10509 match them up some other way.
10510
10511 When we do name matching there is a related problem with function
10512 templates; two instantiated function templates are allowed to
10513 differ only by their return types, which we do not add here. */
10514
10515 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10516 {
10517 struct attribute *attr;
10518 struct die_info *child;
10519 int first = 1;
10520 const language_defn *cplus_lang = language_def (cu->language);
10521
10522 die->building_fullname = 1;
10523
10524 for (child = die->child; child != NULL; child = child->sibling)
10525 {
10526 struct type *type;
10527 LONGEST value;
10528 const gdb_byte *bytes;
10529 struct dwarf2_locexpr_baton *baton;
10530 struct value *v;
10531
10532 if (child->tag != DW_TAG_template_type_param
10533 && child->tag != DW_TAG_template_value_param)
10534 continue;
10535
10536 if (first)
10537 {
10538 buf.puts ("<");
10539 first = 0;
10540 }
10541 else
10542 buf.puts (", ");
10543
10544 attr = dwarf2_attr (child, DW_AT_type, cu);
10545 if (attr == NULL)
10546 {
10547 complaint (_("template parameter missing DW_AT_type"));
10548 buf.puts ("UNKNOWN_TYPE");
10549 continue;
10550 }
10551 type = die_type (child, cu);
10552
10553 if (child->tag == DW_TAG_template_type_param)
10554 {
10555 cplus_lang->print_type (type, "", &buf, -1, 0,
10556 &type_print_raw_options);
10557 continue;
10558 }
10559
10560 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10561 if (attr == NULL)
10562 {
10563 complaint (_("template parameter missing "
10564 "DW_AT_const_value"));
10565 buf.puts ("UNKNOWN_VALUE");
10566 continue;
10567 }
10568
10569 dwarf2_const_value_attr (attr, type, name,
10570 &cu->comp_unit_obstack, cu,
10571 &value, &bytes, &baton);
10572
10573 if (type->has_no_signedness ())
10574 /* GDB prints characters as NUMBER 'CHAR'. If that's
10575 changed, this can use value_print instead. */
10576 cplus_lang->printchar (value, type, &buf);
10577 else
10578 {
10579 struct value_print_options opts;
10580
10581 if (baton != NULL)
10582 v = dwarf2_evaluate_loc_desc (type, NULL,
10583 baton->data,
10584 baton->size,
10585 baton->per_cu,
10586 baton->per_objfile);
10587 else if (bytes != NULL)
10588 {
10589 v = allocate_value (type);
10590 memcpy (value_contents_writeable (v), bytes,
10591 TYPE_LENGTH (type));
10592 }
10593 else
10594 v = value_from_longest (type, value);
10595
10596 /* Specify decimal so that we do not depend on
10597 the radix. */
10598 get_formatted_print_options (&opts, 'd');
10599 opts.raw = 1;
10600 value_print (v, &buf, &opts);
10601 release_value (v);
10602 }
10603 }
10604
10605 die->building_fullname = 0;
10606
10607 if (!first)
10608 {
10609 /* Close the argument list, with a space if necessary
10610 (nested templates). */
10611 if (!buf.empty () && buf.string ().back () == '>')
10612 buf.puts (" >");
10613 else
10614 buf.puts (">");
10615 }
10616 }
10617
10618 /* For C++ methods, append formal parameter type
10619 information, if PHYSNAME. */
10620
10621 if (physname && die->tag == DW_TAG_subprogram
10622 && cu->language == language_cplus)
10623 {
10624 struct type *type = read_type_die (die, cu);
10625
10626 c_type_print_args (type, &buf, 1, cu->language,
10627 &type_print_raw_options);
10628
10629 if (cu->language == language_cplus)
10630 {
10631 /* Assume that an artificial first parameter is
10632 "this", but do not crash if it is not. RealView
10633 marks unnamed (and thus unused) parameters as
10634 artificial; there is no way to differentiate
10635 the two cases. */
10636 if (type->num_fields () > 0
10637 && TYPE_FIELD_ARTIFICIAL (type, 0)
10638 && type->field (0).type ()->code () == TYPE_CODE_PTR
10639 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
10640 buf.puts (" const");
10641 }
10642 }
10643
10644 const std::string &intermediate_name = buf.string ();
10645
10646 if (cu->language == language_cplus)
10647 canonical_name
10648 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10649 objfile);
10650
10651 /* If we only computed INTERMEDIATE_NAME, or if
10652 INTERMEDIATE_NAME is already canonical, then we need to
10653 intern it. */
10654 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10655 name = objfile->intern (intermediate_name);
10656 else
10657 name = canonical_name;
10658 }
10659 }
10660
10661 return name;
10662 }
10663
10664 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10665 If scope qualifiers are appropriate they will be added. The result
10666 will be allocated on the storage_obstack, or NULL if the DIE does
10667 not have a name. NAME may either be from a previous call to
10668 dwarf2_name or NULL.
10669
10670 The output string will be canonicalized (if C++). */
10671
10672 static const char *
10673 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10674 {
10675 return dwarf2_compute_name (name, die, cu, 0);
10676 }
10677
10678 /* Construct a physname for the given DIE in CU. NAME may either be
10679 from a previous call to dwarf2_name or NULL. The result will be
10680 allocated on the objfile_objstack or NULL if the DIE does not have a
10681 name.
10682
10683 The output string will be canonicalized (if C++). */
10684
10685 static const char *
10686 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10687 {
10688 struct objfile *objfile = cu->per_objfile->objfile;
10689 const char *retval, *mangled = NULL, *canon = NULL;
10690 int need_copy = 1;
10691
10692 /* In this case dwarf2_compute_name is just a shortcut not building anything
10693 on its own. */
10694 if (!die_needs_namespace (die, cu))
10695 return dwarf2_compute_name (name, die, cu, 1);
10696
10697 if (cu->language != language_rust)
10698 mangled = dw2_linkage_name (die, cu);
10699
10700 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10701 has computed. */
10702 gdb::unique_xmalloc_ptr<char> demangled;
10703 if (mangled != NULL)
10704 {
10705
10706 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
10707 {
10708 /* Do nothing (do not demangle the symbol name). */
10709 }
10710 else
10711 {
10712 /* Use DMGL_RET_DROP for C++ template functions to suppress
10713 their return type. It is easier for GDB users to search
10714 for such functions as `name(params)' than `long name(params)'.
10715 In such case the minimal symbol names do not match the full
10716 symbol names but for template functions there is never a need
10717 to look up their definition from their declaration so
10718 the only disadvantage remains the minimal symbol variant
10719 `long name(params)' does not have the proper inferior type. */
10720 demangled.reset (gdb_demangle (mangled,
10721 (DMGL_PARAMS | DMGL_ANSI
10722 | DMGL_RET_DROP)));
10723 }
10724 if (demangled)
10725 canon = demangled.get ();
10726 else
10727 {
10728 canon = mangled;
10729 need_copy = 0;
10730 }
10731 }
10732
10733 if (canon == NULL || check_physname)
10734 {
10735 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10736
10737 if (canon != NULL && strcmp (physname, canon) != 0)
10738 {
10739 /* It may not mean a bug in GDB. The compiler could also
10740 compute DW_AT_linkage_name incorrectly. But in such case
10741 GDB would need to be bug-to-bug compatible. */
10742
10743 complaint (_("Computed physname <%s> does not match demangled <%s> "
10744 "(from linkage <%s>) - DIE at %s [in module %s]"),
10745 physname, canon, mangled, sect_offset_str (die->sect_off),
10746 objfile_name (objfile));
10747
10748 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10749 is available here - over computed PHYSNAME. It is safer
10750 against both buggy GDB and buggy compilers. */
10751
10752 retval = canon;
10753 }
10754 else
10755 {
10756 retval = physname;
10757 need_copy = 0;
10758 }
10759 }
10760 else
10761 retval = canon;
10762
10763 if (need_copy)
10764 retval = objfile->intern (retval);
10765
10766 return retval;
10767 }
10768
10769 /* Inspect DIE in CU for a namespace alias. If one exists, record
10770 a new symbol for it.
10771
10772 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10773
10774 static int
10775 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10776 {
10777 struct attribute *attr;
10778
10779 /* If the die does not have a name, this is not a namespace
10780 alias. */
10781 attr = dwarf2_attr (die, DW_AT_name, cu);
10782 if (attr != NULL)
10783 {
10784 int num;
10785 struct die_info *d = die;
10786 struct dwarf2_cu *imported_cu = cu;
10787
10788 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10789 keep inspecting DIEs until we hit the underlying import. */
10790 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10791 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10792 {
10793 attr = dwarf2_attr (d, DW_AT_import, cu);
10794 if (attr == NULL)
10795 break;
10796
10797 d = follow_die_ref (d, attr, &imported_cu);
10798 if (d->tag != DW_TAG_imported_declaration)
10799 break;
10800 }
10801
10802 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10803 {
10804 complaint (_("DIE at %s has too many recursively imported "
10805 "declarations"), sect_offset_str (d->sect_off));
10806 return 0;
10807 }
10808
10809 if (attr != NULL)
10810 {
10811 struct type *type;
10812 sect_offset sect_off = attr->get_ref_die_offset ();
10813
10814 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10815 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10816 {
10817 /* This declaration is a global namespace alias. Add
10818 a symbol for it whose type is the aliased namespace. */
10819 new_symbol (die, type, cu);
10820 return 1;
10821 }
10822 }
10823 }
10824
10825 return 0;
10826 }
10827
10828 /* Return the using directives repository (global or local?) to use in the
10829 current context for CU.
10830
10831 For Ada, imported declarations can materialize renamings, which *may* be
10832 global. However it is impossible (for now?) in DWARF to distinguish
10833 "external" imported declarations and "static" ones. As all imported
10834 declarations seem to be static in all other languages, make them all CU-wide
10835 global only in Ada. */
10836
10837 static struct using_direct **
10838 using_directives (struct dwarf2_cu *cu)
10839 {
10840 if (cu->language == language_ada
10841 && cu->get_builder ()->outermost_context_p ())
10842 return cu->get_builder ()->get_global_using_directives ();
10843 else
10844 return cu->get_builder ()->get_local_using_directives ();
10845 }
10846
10847 /* Read the import statement specified by the given die and record it. */
10848
10849 static void
10850 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10851 {
10852 struct objfile *objfile = cu->per_objfile->objfile;
10853 struct attribute *import_attr;
10854 struct die_info *imported_die, *child_die;
10855 struct dwarf2_cu *imported_cu;
10856 const char *imported_name;
10857 const char *imported_name_prefix;
10858 const char *canonical_name;
10859 const char *import_alias;
10860 const char *imported_declaration = NULL;
10861 const char *import_prefix;
10862 std::vector<const char *> excludes;
10863
10864 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10865 if (import_attr == NULL)
10866 {
10867 complaint (_("Tag '%s' has no DW_AT_import"),
10868 dwarf_tag_name (die->tag));
10869 return;
10870 }
10871
10872 imported_cu = cu;
10873 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10874 imported_name = dwarf2_name (imported_die, imported_cu);
10875 if (imported_name == NULL)
10876 {
10877 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10878
10879 The import in the following code:
10880 namespace A
10881 {
10882 typedef int B;
10883 }
10884
10885 int main ()
10886 {
10887 using A::B;
10888 B b;
10889 return b;
10890 }
10891
10892 ...
10893 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10894 <52> DW_AT_decl_file : 1
10895 <53> DW_AT_decl_line : 6
10896 <54> DW_AT_import : <0x75>
10897 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10898 <59> DW_AT_name : B
10899 <5b> DW_AT_decl_file : 1
10900 <5c> DW_AT_decl_line : 2
10901 <5d> DW_AT_type : <0x6e>
10902 ...
10903 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10904 <76> DW_AT_byte_size : 4
10905 <77> DW_AT_encoding : 5 (signed)
10906
10907 imports the wrong die ( 0x75 instead of 0x58 ).
10908 This case will be ignored until the gcc bug is fixed. */
10909 return;
10910 }
10911
10912 /* Figure out the local name after import. */
10913 import_alias = dwarf2_name (die, cu);
10914
10915 /* Figure out where the statement is being imported to. */
10916 import_prefix = determine_prefix (die, cu);
10917
10918 /* Figure out what the scope of the imported die is and prepend it
10919 to the name of the imported die. */
10920 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10921
10922 if (imported_die->tag != DW_TAG_namespace
10923 && imported_die->tag != DW_TAG_module)
10924 {
10925 imported_declaration = imported_name;
10926 canonical_name = imported_name_prefix;
10927 }
10928 else if (strlen (imported_name_prefix) > 0)
10929 canonical_name = obconcat (&objfile->objfile_obstack,
10930 imported_name_prefix,
10931 (cu->language == language_d ? "." : "::"),
10932 imported_name, (char *) NULL);
10933 else
10934 canonical_name = imported_name;
10935
10936 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10937 for (child_die = die->child; child_die && child_die->tag;
10938 child_die = child_die->sibling)
10939 {
10940 /* DWARF-4: A Fortran use statement with a “rename list” may be
10941 represented by an imported module entry with an import attribute
10942 referring to the module and owned entries corresponding to those
10943 entities that are renamed as part of being imported. */
10944
10945 if (child_die->tag != DW_TAG_imported_declaration)
10946 {
10947 complaint (_("child DW_TAG_imported_declaration expected "
10948 "- DIE at %s [in module %s]"),
10949 sect_offset_str (child_die->sect_off),
10950 objfile_name (objfile));
10951 continue;
10952 }
10953
10954 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10955 if (import_attr == NULL)
10956 {
10957 complaint (_("Tag '%s' has no DW_AT_import"),
10958 dwarf_tag_name (child_die->tag));
10959 continue;
10960 }
10961
10962 imported_cu = cu;
10963 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10964 &imported_cu);
10965 imported_name = dwarf2_name (imported_die, imported_cu);
10966 if (imported_name == NULL)
10967 {
10968 complaint (_("child DW_TAG_imported_declaration has unknown "
10969 "imported name - DIE at %s [in module %s]"),
10970 sect_offset_str (child_die->sect_off),
10971 objfile_name (objfile));
10972 continue;
10973 }
10974
10975 excludes.push_back (imported_name);
10976
10977 process_die (child_die, cu);
10978 }
10979
10980 add_using_directive (using_directives (cu),
10981 import_prefix,
10982 canonical_name,
10983 import_alias,
10984 imported_declaration,
10985 excludes,
10986 0,
10987 &objfile->objfile_obstack);
10988 }
10989
10990 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10991 types, but gives them a size of zero. Starting with version 14,
10992 ICC is compatible with GCC. */
10993
10994 static bool
10995 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10996 {
10997 if (!cu->checked_producer)
10998 check_producer (cu);
10999
11000 return cu->producer_is_icc_lt_14;
11001 }
11002
11003 /* ICC generates a DW_AT_type for C void functions. This was observed on
11004 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11005 which says that void functions should not have a DW_AT_type. */
11006
11007 static bool
11008 producer_is_icc (struct dwarf2_cu *cu)
11009 {
11010 if (!cu->checked_producer)
11011 check_producer (cu);
11012
11013 return cu->producer_is_icc;
11014 }
11015
11016 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11017 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11018 this, it was first present in GCC release 4.3.0. */
11019
11020 static bool
11021 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11022 {
11023 if (!cu->checked_producer)
11024 check_producer (cu);
11025
11026 return cu->producer_is_gcc_lt_4_3;
11027 }
11028
11029 static file_and_directory
11030 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11031 {
11032 file_and_directory res;
11033
11034 /* Find the filename. Do not use dwarf2_name here, since the filename
11035 is not a source language identifier. */
11036 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11037 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11038
11039 if (res.comp_dir == NULL
11040 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11041 && IS_ABSOLUTE_PATH (res.name))
11042 {
11043 res.comp_dir_storage = ldirname (res.name);
11044 if (!res.comp_dir_storage.empty ())
11045 res.comp_dir = res.comp_dir_storage.c_str ();
11046 }
11047 if (res.comp_dir != NULL)
11048 {
11049 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11050 directory, get rid of it. */
11051 const char *cp = strchr (res.comp_dir, ':');
11052
11053 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11054 res.comp_dir = cp + 1;
11055 }
11056
11057 if (res.name == NULL)
11058 res.name = "<unknown>";
11059
11060 return res;
11061 }
11062
11063 /* Handle DW_AT_stmt_list for a compilation unit.
11064 DIE is the DW_TAG_compile_unit die for CU.
11065 COMP_DIR is the compilation directory. LOWPC is passed to
11066 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11067
11068 static void
11069 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11070 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11071 {
11072 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11073 struct attribute *attr;
11074 struct line_header line_header_local;
11075 hashval_t line_header_local_hash;
11076 void **slot;
11077 int decode_mapping;
11078
11079 gdb_assert (! cu->per_cu->is_debug_types);
11080
11081 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11082 if (attr == NULL || !attr->form_is_unsigned ())
11083 return;
11084
11085 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
11086
11087 /* The line header hash table is only created if needed (it exists to
11088 prevent redundant reading of the line table for partial_units).
11089 If we're given a partial_unit, we'll need it. If we're given a
11090 compile_unit, then use the line header hash table if it's already
11091 created, but don't create one just yet. */
11092
11093 if (per_objfile->line_header_hash == NULL
11094 && die->tag == DW_TAG_partial_unit)
11095 {
11096 per_objfile->line_header_hash
11097 .reset (htab_create_alloc (127, line_header_hash_voidp,
11098 line_header_eq_voidp,
11099 free_line_header_voidp,
11100 xcalloc, xfree));
11101 }
11102
11103 line_header_local.sect_off = line_offset;
11104 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11105 line_header_local_hash = line_header_hash (&line_header_local);
11106 if (per_objfile->line_header_hash != NULL)
11107 {
11108 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11109 &line_header_local,
11110 line_header_local_hash, NO_INSERT);
11111
11112 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11113 is not present in *SLOT (since if there is something in *SLOT then
11114 it will be for a partial_unit). */
11115 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11116 {
11117 gdb_assert (*slot != NULL);
11118 cu->line_header = (struct line_header *) *slot;
11119 return;
11120 }
11121 }
11122
11123 /* dwarf_decode_line_header does not yet provide sufficient information.
11124 We always have to call also dwarf_decode_lines for it. */
11125 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11126 if (lh == NULL)
11127 return;
11128
11129 cu->line_header = lh.release ();
11130 cu->line_header_die_owner = die;
11131
11132 if (per_objfile->line_header_hash == NULL)
11133 slot = NULL;
11134 else
11135 {
11136 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11137 &line_header_local,
11138 line_header_local_hash, INSERT);
11139 gdb_assert (slot != NULL);
11140 }
11141 if (slot != NULL && *slot == NULL)
11142 {
11143 /* This newly decoded line number information unit will be owned
11144 by line_header_hash hash table. */
11145 *slot = cu->line_header;
11146 cu->line_header_die_owner = NULL;
11147 }
11148 else
11149 {
11150 /* We cannot free any current entry in (*slot) as that struct line_header
11151 may be already used by multiple CUs. Create only temporary decoded
11152 line_header for this CU - it may happen at most once for each line
11153 number information unit. And if we're not using line_header_hash
11154 then this is what we want as well. */
11155 gdb_assert (die->tag != DW_TAG_partial_unit);
11156 }
11157 decode_mapping = (die->tag != DW_TAG_partial_unit);
11158 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11159 decode_mapping);
11160
11161 }
11162
11163 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11164
11165 static void
11166 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11167 {
11168 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11169 struct objfile *objfile = per_objfile->objfile;
11170 struct gdbarch *gdbarch = objfile->arch ();
11171 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11172 CORE_ADDR highpc = ((CORE_ADDR) 0);
11173 struct attribute *attr;
11174 struct die_info *child_die;
11175 CORE_ADDR baseaddr;
11176
11177 prepare_one_comp_unit (cu, die, cu->language);
11178 baseaddr = objfile->text_section_offset ();
11179
11180 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11181
11182 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11183 from finish_block. */
11184 if (lowpc == ((CORE_ADDR) -1))
11185 lowpc = highpc;
11186 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11187
11188 file_and_directory fnd = find_file_and_directory (die, cu);
11189
11190 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11191 standardised yet. As a workaround for the language detection we fall
11192 back to the DW_AT_producer string. */
11193 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11194 cu->language = language_opencl;
11195
11196 /* Similar hack for Go. */
11197 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11198 set_cu_language (DW_LANG_Go, cu);
11199
11200 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11201
11202 /* Decode line number information if present. We do this before
11203 processing child DIEs, so that the line header table is available
11204 for DW_AT_decl_file. */
11205 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11206
11207 /* Process all dies in compilation unit. */
11208 if (die->child != NULL)
11209 {
11210 child_die = die->child;
11211 while (child_die && child_die->tag)
11212 {
11213 process_die (child_die, cu);
11214 child_die = child_die->sibling;
11215 }
11216 }
11217
11218 /* Decode macro information, if present. Dwarf 2 macro information
11219 refers to information in the line number info statement program
11220 header, so we can only read it if we've read the header
11221 successfully. */
11222 attr = dwarf2_attr (die, DW_AT_macros, cu);
11223 if (attr == NULL)
11224 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11225 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
11226 {
11227 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11228 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11229
11230 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
11231 }
11232 else
11233 {
11234 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11235 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
11236 {
11237 unsigned int macro_offset = attr->as_unsigned ();
11238
11239 dwarf_decode_macros (cu, macro_offset, 0);
11240 }
11241 }
11242 }
11243
11244 void
11245 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11246 {
11247 struct type_unit_group *tu_group;
11248 int first_time;
11249 struct attribute *attr;
11250 unsigned int i;
11251 struct signatured_type *sig_type;
11252
11253 gdb_assert (per_cu->is_debug_types);
11254 sig_type = (struct signatured_type *) per_cu;
11255
11256 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11257
11258 /* If we're using .gdb_index (includes -readnow) then
11259 per_cu->type_unit_group may not have been set up yet. */
11260 if (sig_type->type_unit_group == NULL)
11261 sig_type->type_unit_group = get_type_unit_group (this, attr);
11262 tu_group = sig_type->type_unit_group;
11263
11264 /* If we've already processed this stmt_list there's no real need to
11265 do it again, we could fake it and just recreate the part we need
11266 (file name,index -> symtab mapping). If data shows this optimization
11267 is useful we can do it then. */
11268 type_unit_group_unshareable *tug_unshare
11269 = per_objfile->get_type_unit_group_unshareable (tu_group);
11270 first_time = tug_unshare->compunit_symtab == NULL;
11271
11272 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11273 debug info. */
11274 line_header_up lh;
11275 if (attr != NULL && attr->form_is_unsigned ())
11276 {
11277 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
11278 lh = dwarf_decode_line_header (line_offset, this);
11279 }
11280 if (lh == NULL)
11281 {
11282 if (first_time)
11283 start_symtab ("", NULL, 0);
11284 else
11285 {
11286 gdb_assert (tug_unshare->symtabs == NULL);
11287 gdb_assert (m_builder == nullptr);
11288 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11289 m_builder.reset (new struct buildsym_compunit
11290 (COMPUNIT_OBJFILE (cust), "",
11291 COMPUNIT_DIRNAME (cust),
11292 compunit_language (cust),
11293 0, cust));
11294 list_in_scope = get_builder ()->get_file_symbols ();
11295 }
11296 return;
11297 }
11298
11299 line_header = lh.release ();
11300 line_header_die_owner = die;
11301
11302 if (first_time)
11303 {
11304 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11305
11306 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11307 still initializing it, and our caller (a few levels up)
11308 process_full_type_unit still needs to know if this is the first
11309 time. */
11310
11311 tug_unshare->symtabs
11312 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11313 struct symtab *, line_header->file_names_size ());
11314
11315 auto &file_names = line_header->file_names ();
11316 for (i = 0; i < file_names.size (); ++i)
11317 {
11318 file_entry &fe = file_names[i];
11319 dwarf2_start_subfile (this, fe.name,
11320 fe.include_dir (line_header));
11321 buildsym_compunit *b = get_builder ();
11322 if (b->get_current_subfile ()->symtab == NULL)
11323 {
11324 /* NOTE: start_subfile will recognize when it's been
11325 passed a file it has already seen. So we can't
11326 assume there's a simple mapping from
11327 cu->line_header->file_names to subfiles, plus
11328 cu->line_header->file_names may contain dups. */
11329 b->get_current_subfile ()->symtab
11330 = allocate_symtab (cust, b->get_current_subfile ()->name);
11331 }
11332
11333 fe.symtab = b->get_current_subfile ()->symtab;
11334 tug_unshare->symtabs[i] = fe.symtab;
11335 }
11336 }
11337 else
11338 {
11339 gdb_assert (m_builder == nullptr);
11340 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11341 m_builder.reset (new struct buildsym_compunit
11342 (COMPUNIT_OBJFILE (cust), "",
11343 COMPUNIT_DIRNAME (cust),
11344 compunit_language (cust),
11345 0, cust));
11346 list_in_scope = get_builder ()->get_file_symbols ();
11347
11348 auto &file_names = line_header->file_names ();
11349 for (i = 0; i < file_names.size (); ++i)
11350 {
11351 file_entry &fe = file_names[i];
11352 fe.symtab = tug_unshare->symtabs[i];
11353 }
11354 }
11355
11356 /* The main symtab is allocated last. Type units don't have DW_AT_name
11357 so they don't have a "real" (so to speak) symtab anyway.
11358 There is later code that will assign the main symtab to all symbols
11359 that don't have one. We need to handle the case of a symbol with a
11360 missing symtab (DW_AT_decl_file) anyway. */
11361 }
11362
11363 /* Process DW_TAG_type_unit.
11364 For TUs we want to skip the first top level sibling if it's not the
11365 actual type being defined by this TU. In this case the first top
11366 level sibling is there to provide context only. */
11367
11368 static void
11369 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11370 {
11371 struct die_info *child_die;
11372
11373 prepare_one_comp_unit (cu, die, language_minimal);
11374
11375 /* Initialize (or reinitialize) the machinery for building symtabs.
11376 We do this before processing child DIEs, so that the line header table
11377 is available for DW_AT_decl_file. */
11378 cu->setup_type_unit_groups (die);
11379
11380 if (die->child != NULL)
11381 {
11382 child_die = die->child;
11383 while (child_die && child_die->tag)
11384 {
11385 process_die (child_die, cu);
11386 child_die = child_die->sibling;
11387 }
11388 }
11389 }
11390 \f
11391 /* DWO/DWP files.
11392
11393 http://gcc.gnu.org/wiki/DebugFission
11394 http://gcc.gnu.org/wiki/DebugFissionDWP
11395
11396 To simplify handling of both DWO files ("object" files with the DWARF info)
11397 and DWP files (a file with the DWOs packaged up into one file), we treat
11398 DWP files as having a collection of virtual DWO files. */
11399
11400 static hashval_t
11401 hash_dwo_file (const void *item)
11402 {
11403 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11404 hashval_t hash;
11405
11406 hash = htab_hash_string (dwo_file->dwo_name);
11407 if (dwo_file->comp_dir != NULL)
11408 hash += htab_hash_string (dwo_file->comp_dir);
11409 return hash;
11410 }
11411
11412 static int
11413 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11414 {
11415 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11416 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11417
11418 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11419 return 0;
11420 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11421 return lhs->comp_dir == rhs->comp_dir;
11422 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11423 }
11424
11425 /* Allocate a hash table for DWO files. */
11426
11427 static htab_up
11428 allocate_dwo_file_hash_table ()
11429 {
11430 auto delete_dwo_file = [] (void *item)
11431 {
11432 struct dwo_file *dwo_file = (struct dwo_file *) item;
11433
11434 delete dwo_file;
11435 };
11436
11437 return htab_up (htab_create_alloc (41,
11438 hash_dwo_file,
11439 eq_dwo_file,
11440 delete_dwo_file,
11441 xcalloc, xfree));
11442 }
11443
11444 /* Lookup DWO file DWO_NAME. */
11445
11446 static void **
11447 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
11448 const char *dwo_name,
11449 const char *comp_dir)
11450 {
11451 struct dwo_file find_entry;
11452 void **slot;
11453
11454 if (per_objfile->per_bfd->dwo_files == NULL)
11455 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11456
11457 find_entry.dwo_name = dwo_name;
11458 find_entry.comp_dir = comp_dir;
11459 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
11460 INSERT);
11461
11462 return slot;
11463 }
11464
11465 static hashval_t
11466 hash_dwo_unit (const void *item)
11467 {
11468 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11469
11470 /* This drops the top 32 bits of the id, but is ok for a hash. */
11471 return dwo_unit->signature;
11472 }
11473
11474 static int
11475 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11476 {
11477 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11478 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11479
11480 /* The signature is assumed to be unique within the DWO file.
11481 So while object file CU dwo_id's always have the value zero,
11482 that's OK, assuming each object file DWO file has only one CU,
11483 and that's the rule for now. */
11484 return lhs->signature == rhs->signature;
11485 }
11486
11487 /* Allocate a hash table for DWO CUs,TUs.
11488 There is one of these tables for each of CUs,TUs for each DWO file. */
11489
11490 static htab_up
11491 allocate_dwo_unit_table ()
11492 {
11493 /* Start out with a pretty small number.
11494 Generally DWO files contain only one CU and maybe some TUs. */
11495 return htab_up (htab_create_alloc (3,
11496 hash_dwo_unit,
11497 eq_dwo_unit,
11498 NULL, xcalloc, xfree));
11499 }
11500
11501 /* die_reader_func for create_dwo_cu. */
11502
11503 static void
11504 create_dwo_cu_reader (const struct die_reader_specs *reader,
11505 const gdb_byte *info_ptr,
11506 struct die_info *comp_unit_die,
11507 struct dwo_file *dwo_file,
11508 struct dwo_unit *dwo_unit)
11509 {
11510 struct dwarf2_cu *cu = reader->cu;
11511 sect_offset sect_off = cu->per_cu->sect_off;
11512 struct dwarf2_section_info *section = cu->per_cu->section;
11513
11514 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11515 if (!signature.has_value ())
11516 {
11517 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11518 " its dwo_id [in module %s]"),
11519 sect_offset_str (sect_off), dwo_file->dwo_name);
11520 return;
11521 }
11522
11523 dwo_unit->dwo_file = dwo_file;
11524 dwo_unit->signature = *signature;
11525 dwo_unit->section = section;
11526 dwo_unit->sect_off = sect_off;
11527 dwo_unit->length = cu->per_cu->length;
11528
11529 dwarf_read_debug_printf (" offset %s, dwo_id %s",
11530 sect_offset_str (sect_off),
11531 hex_string (dwo_unit->signature));
11532 }
11533
11534 /* Create the dwo_units for the CUs in a DWO_FILE.
11535 Note: This function processes DWO files only, not DWP files. */
11536
11537 static void
11538 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
11539 dwarf2_cu *cu, struct dwo_file &dwo_file,
11540 dwarf2_section_info &section, htab_up &cus_htab)
11541 {
11542 struct objfile *objfile = per_objfile->objfile;
11543 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
11544 const gdb_byte *info_ptr, *end_ptr;
11545
11546 section.read (objfile);
11547 info_ptr = section.buffer;
11548
11549 if (info_ptr == NULL)
11550 return;
11551
11552 dwarf_read_debug_printf ("Reading %s for %s:",
11553 section.get_name (),
11554 section.get_file_name ());
11555
11556 end_ptr = info_ptr + section.size;
11557 while (info_ptr < end_ptr)
11558 {
11559 struct dwarf2_per_cu_data per_cu;
11560 struct dwo_unit read_unit {};
11561 struct dwo_unit *dwo_unit;
11562 void **slot;
11563 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11564
11565 memset (&per_cu, 0, sizeof (per_cu));
11566 per_cu.per_bfd = per_bfd;
11567 per_cu.is_debug_types = 0;
11568 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11569 per_cu.section = &section;
11570
11571 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
11572 if (!reader.dummy_p)
11573 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11574 &dwo_file, &read_unit);
11575 info_ptr += per_cu.length;
11576
11577 // If the unit could not be parsed, skip it.
11578 if (read_unit.dwo_file == NULL)
11579 continue;
11580
11581 if (cus_htab == NULL)
11582 cus_htab = allocate_dwo_unit_table ();
11583
11584 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11585 struct dwo_unit);
11586 *dwo_unit = read_unit;
11587 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11588 gdb_assert (slot != NULL);
11589 if (*slot != NULL)
11590 {
11591 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11592 sect_offset dup_sect_off = dup_cu->sect_off;
11593
11594 complaint (_("debug cu entry at offset %s is duplicate to"
11595 " the entry at offset %s, signature %s"),
11596 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11597 hex_string (dwo_unit->signature));
11598 }
11599 *slot = (void *)dwo_unit;
11600 }
11601 }
11602
11603 /* DWP file .debug_{cu,tu}_index section format:
11604 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11605 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11606
11607 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11608 officially standard DWP format was published with DWARF v5 and is called
11609 Version 5. There are no versions 3 or 4.
11610
11611 DWP Version 1:
11612
11613 Both index sections have the same format, and serve to map a 64-bit
11614 signature to a set of section numbers. Each section begins with a header,
11615 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11616 indexes, and a pool of 32-bit section numbers. The index sections will be
11617 aligned at 8-byte boundaries in the file.
11618
11619 The index section header consists of:
11620
11621 V, 32 bit version number
11622 -, 32 bits unused
11623 N, 32 bit number of compilation units or type units in the index
11624 M, 32 bit number of slots in the hash table
11625
11626 Numbers are recorded using the byte order of the application binary.
11627
11628 The hash table begins at offset 16 in the section, and consists of an array
11629 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11630 order of the application binary). Unused slots in the hash table are 0.
11631 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11632
11633 The parallel table begins immediately after the hash table
11634 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11635 array of 32-bit indexes (using the byte order of the application binary),
11636 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11637 table contains a 32-bit index into the pool of section numbers. For unused
11638 hash table slots, the corresponding entry in the parallel table will be 0.
11639
11640 The pool of section numbers begins immediately following the hash table
11641 (at offset 16 + 12 * M from the beginning of the section). The pool of
11642 section numbers consists of an array of 32-bit words (using the byte order
11643 of the application binary). Each item in the array is indexed starting
11644 from 0. The hash table entry provides the index of the first section
11645 number in the set. Additional section numbers in the set follow, and the
11646 set is terminated by a 0 entry (section number 0 is not used in ELF).
11647
11648 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11649 section must be the first entry in the set, and the .debug_abbrev.dwo must
11650 be the second entry. Other members of the set may follow in any order.
11651
11652 ---
11653
11654 DWP Versions 2 and 5:
11655
11656 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
11657 and the entries in the index tables are now offsets into these sections.
11658 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11659 section.
11660
11661 Index Section Contents:
11662 Header
11663 Hash Table of Signatures dwp_hash_table.hash_table
11664 Parallel Table of Indices dwp_hash_table.unit_table
11665 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11666 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
11667
11668 The index section header consists of:
11669
11670 V, 32 bit version number
11671 L, 32 bit number of columns in the table of section offsets
11672 N, 32 bit number of compilation units or type units in the index
11673 M, 32 bit number of slots in the hash table
11674
11675 Numbers are recorded using the byte order of the application binary.
11676
11677 The hash table has the same format as version 1.
11678 The parallel table of indices has the same format as version 1,
11679 except that the entries are origin-1 indices into the table of sections
11680 offsets and the table of section sizes.
11681
11682 The table of offsets begins immediately following the parallel table
11683 (at offset 16 + 12 * M from the beginning of the section). The table is
11684 a two-dimensional array of 32-bit words (using the byte order of the
11685 application binary), with L columns and N+1 rows, in row-major order.
11686 Each row in the array is indexed starting from 0. The first row provides
11687 a key to the remaining rows: each column in this row provides an identifier
11688 for a debug section, and the offsets in the same column of subsequent rows
11689 refer to that section. The section identifiers for Version 2 are:
11690
11691 DW_SECT_INFO 1 .debug_info.dwo
11692 DW_SECT_TYPES 2 .debug_types.dwo
11693 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11694 DW_SECT_LINE 4 .debug_line.dwo
11695 DW_SECT_LOC 5 .debug_loc.dwo
11696 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11697 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11698 DW_SECT_MACRO 8 .debug_macro.dwo
11699
11700 The section identifiers for Version 5 are:
11701
11702 DW_SECT_INFO_V5 1 .debug_info.dwo
11703 DW_SECT_RESERVED_V5 2 --
11704 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11705 DW_SECT_LINE_V5 4 .debug_line.dwo
11706 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11707 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11708 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11709 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11710
11711 The offsets provided by the CU and TU index sections are the base offsets
11712 for the contributions made by each CU or TU to the corresponding section
11713 in the package file. Each CU and TU header contains an abbrev_offset
11714 field, used to find the abbreviations table for that CU or TU within the
11715 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11716 be interpreted as relative to the base offset given in the index section.
11717 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11718 should be interpreted as relative to the base offset for .debug_line.dwo,
11719 and offsets into other debug sections obtained from DWARF attributes should
11720 also be interpreted as relative to the corresponding base offset.
11721
11722 The table of sizes begins immediately following the table of offsets.
11723 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11724 with L columns and N rows, in row-major order. Each row in the array is
11725 indexed starting from 1 (row 0 is shared by the two tables).
11726
11727 ---
11728
11729 Hash table lookup is handled the same in version 1 and 2:
11730
11731 We assume that N and M will not exceed 2^32 - 1.
11732 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11733
11734 Given a 64-bit compilation unit signature or a type signature S, an entry
11735 in the hash table is located as follows:
11736
11737 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11738 the low-order k bits all set to 1.
11739
11740 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11741
11742 3) If the hash table entry at index H matches the signature, use that
11743 entry. If the hash table entry at index H is unused (all zeroes),
11744 terminate the search: the signature is not present in the table.
11745
11746 4) Let H = (H + H') modulo M. Repeat at Step 3.
11747
11748 Because M > N and H' and M are relatively prime, the search is guaranteed
11749 to stop at an unused slot or find the match. */
11750
11751 /* Create a hash table to map DWO IDs to their CU/TU entry in
11752 .debug_{info,types}.dwo in DWP_FILE.
11753 Returns NULL if there isn't one.
11754 Note: This function processes DWP files only, not DWO files. */
11755
11756 static struct dwp_hash_table *
11757 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11758 struct dwp_file *dwp_file, int is_debug_types)
11759 {
11760 struct objfile *objfile = per_objfile->objfile;
11761 bfd *dbfd = dwp_file->dbfd.get ();
11762 const gdb_byte *index_ptr, *index_end;
11763 struct dwarf2_section_info *index;
11764 uint32_t version, nr_columns, nr_units, nr_slots;
11765 struct dwp_hash_table *htab;
11766
11767 if (is_debug_types)
11768 index = &dwp_file->sections.tu_index;
11769 else
11770 index = &dwp_file->sections.cu_index;
11771
11772 if (index->empty ())
11773 return NULL;
11774 index->read (objfile);
11775
11776 index_ptr = index->buffer;
11777 index_end = index_ptr + index->size;
11778
11779 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11780 For now it's safe to just read 4 bytes (particularly as it's difficult to
11781 tell if you're dealing with Version 5 before you've read the version). */
11782 version = read_4_bytes (dbfd, index_ptr);
11783 index_ptr += 4;
11784 if (version == 2 || version == 5)
11785 nr_columns = read_4_bytes (dbfd, index_ptr);
11786 else
11787 nr_columns = 0;
11788 index_ptr += 4;
11789 nr_units = read_4_bytes (dbfd, index_ptr);
11790 index_ptr += 4;
11791 nr_slots = read_4_bytes (dbfd, index_ptr);
11792 index_ptr += 4;
11793
11794 if (version != 1 && version != 2 && version != 5)
11795 {
11796 error (_("Dwarf Error: unsupported DWP file version (%s)"
11797 " [in module %s]"),
11798 pulongest (version), dwp_file->name);
11799 }
11800 if (nr_slots != (nr_slots & -nr_slots))
11801 {
11802 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11803 " is not power of 2 [in module %s]"),
11804 pulongest (nr_slots), dwp_file->name);
11805 }
11806
11807 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11808 htab->version = version;
11809 htab->nr_columns = nr_columns;
11810 htab->nr_units = nr_units;
11811 htab->nr_slots = nr_slots;
11812 htab->hash_table = index_ptr;
11813 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11814
11815 /* Exit early if the table is empty. */
11816 if (nr_slots == 0 || nr_units == 0
11817 || (version == 2 && nr_columns == 0)
11818 || (version == 5 && nr_columns == 0))
11819 {
11820 /* All must be zero. */
11821 if (nr_slots != 0 || nr_units != 0
11822 || (version == 2 && nr_columns != 0)
11823 || (version == 5 && nr_columns != 0))
11824 {
11825 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11826 " all zero [in modules %s]"),
11827 dwp_file->name);
11828 }
11829 return htab;
11830 }
11831
11832 if (version == 1)
11833 {
11834 htab->section_pool.v1.indices =
11835 htab->unit_table + sizeof (uint32_t) * nr_slots;
11836 /* It's harder to decide whether the section is too small in v1.
11837 V1 is deprecated anyway so we punt. */
11838 }
11839 else if (version == 2)
11840 {
11841 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11842 int *ids = htab->section_pool.v2.section_ids;
11843 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11844 /* Reverse map for error checking. */
11845 int ids_seen[DW_SECT_MAX + 1];
11846 int i;
11847
11848 if (nr_columns < 2)
11849 {
11850 error (_("Dwarf Error: bad DWP hash table, too few columns"
11851 " in section table [in module %s]"),
11852 dwp_file->name);
11853 }
11854 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11855 {
11856 error (_("Dwarf Error: bad DWP hash table, too many columns"
11857 " in section table [in module %s]"),
11858 dwp_file->name);
11859 }
11860 memset (ids, 255, sizeof_ids);
11861 memset (ids_seen, 255, sizeof (ids_seen));
11862 for (i = 0; i < nr_columns; ++i)
11863 {
11864 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11865
11866 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11867 {
11868 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11869 " in section table [in module %s]"),
11870 id, dwp_file->name);
11871 }
11872 if (ids_seen[id] != -1)
11873 {
11874 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11875 " id %d in section table [in module %s]"),
11876 id, dwp_file->name);
11877 }
11878 ids_seen[id] = i;
11879 ids[i] = id;
11880 }
11881 /* Must have exactly one info or types section. */
11882 if (((ids_seen[DW_SECT_INFO] != -1)
11883 + (ids_seen[DW_SECT_TYPES] != -1))
11884 != 1)
11885 {
11886 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11887 " DWO info/types section [in module %s]"),
11888 dwp_file->name);
11889 }
11890 /* Must have an abbrev section. */
11891 if (ids_seen[DW_SECT_ABBREV] == -1)
11892 {
11893 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11894 " section [in module %s]"),
11895 dwp_file->name);
11896 }
11897 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11898 htab->section_pool.v2.sizes =
11899 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11900 * nr_units * nr_columns);
11901 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11902 * nr_units * nr_columns))
11903 > index_end)
11904 {
11905 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11906 " [in module %s]"),
11907 dwp_file->name);
11908 }
11909 }
11910 else /* version == 5 */
11911 {
11912 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11913 int *ids = htab->section_pool.v5.section_ids;
11914 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11915 /* Reverse map for error checking. */
11916 int ids_seen[DW_SECT_MAX_V5 + 1];
11917
11918 if (nr_columns < 2)
11919 {
11920 error (_("Dwarf Error: bad DWP hash table, too few columns"
11921 " in section table [in module %s]"),
11922 dwp_file->name);
11923 }
11924 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11925 {
11926 error (_("Dwarf Error: bad DWP hash table, too many columns"
11927 " in section table [in module %s]"),
11928 dwp_file->name);
11929 }
11930 memset (ids, 255, sizeof_ids);
11931 memset (ids_seen, 255, sizeof (ids_seen));
11932 for (int i = 0; i < nr_columns; ++i)
11933 {
11934 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11935
11936 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11937 {
11938 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11939 " in section table [in module %s]"),
11940 id, dwp_file->name);
11941 }
11942 if (ids_seen[id] != -1)
11943 {
11944 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11945 " id %d in section table [in module %s]"),
11946 id, dwp_file->name);
11947 }
11948 ids_seen[id] = i;
11949 ids[i] = id;
11950 }
11951 /* Must have seen an info section. */
11952 if (ids_seen[DW_SECT_INFO_V5] == -1)
11953 {
11954 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11955 " DWO info/types section [in module %s]"),
11956 dwp_file->name);
11957 }
11958 /* Must have an abbrev section. */
11959 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11960 {
11961 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11962 " section [in module %s]"),
11963 dwp_file->name);
11964 }
11965 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11966 htab->section_pool.v5.sizes
11967 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11968 * nr_units * nr_columns);
11969 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11970 * nr_units * nr_columns))
11971 > index_end)
11972 {
11973 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11974 " [in module %s]"),
11975 dwp_file->name);
11976 }
11977 }
11978
11979 return htab;
11980 }
11981
11982 /* Update SECTIONS with the data from SECTP.
11983
11984 This function is like the other "locate" section routines, but in
11985 this context the sections to read comes from the DWP V1 hash table,
11986 not the full ELF section table.
11987
11988 The result is non-zero for success, or zero if an error was found. */
11989
11990 static int
11991 locate_v1_virtual_dwo_sections (asection *sectp,
11992 struct virtual_v1_dwo_sections *sections)
11993 {
11994 const struct dwop_section_names *names = &dwop_section_names;
11995
11996 if (names->abbrev_dwo.matches (sectp->name))
11997 {
11998 /* There can be only one. */
11999 if (sections->abbrev.s.section != NULL)
12000 return 0;
12001 sections->abbrev.s.section = sectp;
12002 sections->abbrev.size = bfd_section_size (sectp);
12003 }
12004 else if (names->info_dwo.matches (sectp->name)
12005 || names->types_dwo.matches (sectp->name))
12006 {
12007 /* There can be only one. */
12008 if (sections->info_or_types.s.section != NULL)
12009 return 0;
12010 sections->info_or_types.s.section = sectp;
12011 sections->info_or_types.size = bfd_section_size (sectp);
12012 }
12013 else if (names->line_dwo.matches (sectp->name))
12014 {
12015 /* There can be only one. */
12016 if (sections->line.s.section != NULL)
12017 return 0;
12018 sections->line.s.section = sectp;
12019 sections->line.size = bfd_section_size (sectp);
12020 }
12021 else if (names->loc_dwo.matches (sectp->name))
12022 {
12023 /* There can be only one. */
12024 if (sections->loc.s.section != NULL)
12025 return 0;
12026 sections->loc.s.section = sectp;
12027 sections->loc.size = bfd_section_size (sectp);
12028 }
12029 else if (names->macinfo_dwo.matches (sectp->name))
12030 {
12031 /* There can be only one. */
12032 if (sections->macinfo.s.section != NULL)
12033 return 0;
12034 sections->macinfo.s.section = sectp;
12035 sections->macinfo.size = bfd_section_size (sectp);
12036 }
12037 else if (names->macro_dwo.matches (sectp->name))
12038 {
12039 /* There can be only one. */
12040 if (sections->macro.s.section != NULL)
12041 return 0;
12042 sections->macro.s.section = sectp;
12043 sections->macro.size = bfd_section_size (sectp);
12044 }
12045 else if (names->str_offsets_dwo.matches (sectp->name))
12046 {
12047 /* There can be only one. */
12048 if (sections->str_offsets.s.section != NULL)
12049 return 0;
12050 sections->str_offsets.s.section = sectp;
12051 sections->str_offsets.size = bfd_section_size (sectp);
12052 }
12053 else
12054 {
12055 /* No other kind of section is valid. */
12056 return 0;
12057 }
12058
12059 return 1;
12060 }
12061
12062 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12063 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12064 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12065 This is for DWP version 1 files. */
12066
12067 static struct dwo_unit *
12068 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
12069 struct dwp_file *dwp_file,
12070 uint32_t unit_index,
12071 const char *comp_dir,
12072 ULONGEST signature, int is_debug_types)
12073 {
12074 const struct dwp_hash_table *dwp_htab =
12075 is_debug_types ? dwp_file->tus : dwp_file->cus;
12076 bfd *dbfd = dwp_file->dbfd.get ();
12077 const char *kind = is_debug_types ? "TU" : "CU";
12078 struct dwo_file *dwo_file;
12079 struct dwo_unit *dwo_unit;
12080 struct virtual_v1_dwo_sections sections;
12081 void **dwo_file_slot;
12082 int i;
12083
12084 gdb_assert (dwp_file->version == 1);
12085
12086 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
12087 kind, pulongest (unit_index), hex_string (signature),
12088 dwp_file->name);
12089
12090 /* Fetch the sections of this DWO unit.
12091 Put a limit on the number of sections we look for so that bad data
12092 doesn't cause us to loop forever. */
12093
12094 #define MAX_NR_V1_DWO_SECTIONS \
12095 (1 /* .debug_info or .debug_types */ \
12096 + 1 /* .debug_abbrev */ \
12097 + 1 /* .debug_line */ \
12098 + 1 /* .debug_loc */ \
12099 + 1 /* .debug_str_offsets */ \
12100 + 1 /* .debug_macro or .debug_macinfo */ \
12101 + 1 /* trailing zero */)
12102
12103 memset (&sections, 0, sizeof (sections));
12104
12105 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12106 {
12107 asection *sectp;
12108 uint32_t section_nr =
12109 read_4_bytes (dbfd,
12110 dwp_htab->section_pool.v1.indices
12111 + (unit_index + i) * sizeof (uint32_t));
12112
12113 if (section_nr == 0)
12114 break;
12115 if (section_nr >= dwp_file->num_sections)
12116 {
12117 error (_("Dwarf Error: bad DWP hash table, section number too large"
12118 " [in module %s]"),
12119 dwp_file->name);
12120 }
12121
12122 sectp = dwp_file->elf_sections[section_nr];
12123 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12124 {
12125 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12126 " [in module %s]"),
12127 dwp_file->name);
12128 }
12129 }
12130
12131 if (i < 2
12132 || sections.info_or_types.empty ()
12133 || sections.abbrev.empty ())
12134 {
12135 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12136 " [in module %s]"),
12137 dwp_file->name);
12138 }
12139 if (i == MAX_NR_V1_DWO_SECTIONS)
12140 {
12141 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12142 " [in module %s]"),
12143 dwp_file->name);
12144 }
12145
12146 /* It's easier for the rest of the code if we fake a struct dwo_file and
12147 have dwo_unit "live" in that. At least for now.
12148
12149 The DWP file can be made up of a random collection of CUs and TUs.
12150 However, for each CU + set of TUs that came from the same original DWO
12151 file, we can combine them back into a virtual DWO file to save space
12152 (fewer struct dwo_file objects to allocate). Remember that for really
12153 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12154
12155 std::string virtual_dwo_name =
12156 string_printf ("virtual-dwo/%d-%d-%d-%d",
12157 sections.abbrev.get_id (),
12158 sections.line.get_id (),
12159 sections.loc.get_id (),
12160 sections.str_offsets.get_id ());
12161 /* Can we use an existing virtual DWO file? */
12162 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12163 comp_dir);
12164 /* Create one if necessary. */
12165 if (*dwo_file_slot == NULL)
12166 {
12167 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12168 virtual_dwo_name.c_str ());
12169
12170 dwo_file = new struct dwo_file;
12171 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12172 dwo_file->comp_dir = comp_dir;
12173 dwo_file->sections.abbrev = sections.abbrev;
12174 dwo_file->sections.line = sections.line;
12175 dwo_file->sections.loc = sections.loc;
12176 dwo_file->sections.macinfo = sections.macinfo;
12177 dwo_file->sections.macro = sections.macro;
12178 dwo_file->sections.str_offsets = sections.str_offsets;
12179 /* The "str" section is global to the entire DWP file. */
12180 dwo_file->sections.str = dwp_file->sections.str;
12181 /* The info or types section is assigned below to dwo_unit,
12182 there's no need to record it in dwo_file.
12183 Also, we can't simply record type sections in dwo_file because
12184 we record a pointer into the vector in dwo_unit. As we collect more
12185 types we'll grow the vector and eventually have to reallocate space
12186 for it, invalidating all copies of pointers into the previous
12187 contents. */
12188 *dwo_file_slot = dwo_file;
12189 }
12190 else
12191 {
12192 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12193 virtual_dwo_name.c_str ());
12194
12195 dwo_file = (struct dwo_file *) *dwo_file_slot;
12196 }
12197
12198 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12199 dwo_unit->dwo_file = dwo_file;
12200 dwo_unit->signature = signature;
12201 dwo_unit->section =
12202 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12203 *dwo_unit->section = sections.info_or_types;
12204 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12205
12206 return dwo_unit;
12207 }
12208
12209 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
12210 simplify them. Given a pointer to the containing section SECTION, and
12211 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
12212 virtual section of just that piece. */
12213
12214 static struct dwarf2_section_info
12215 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
12216 struct dwarf2_section_info *section,
12217 bfd_size_type offset, bfd_size_type size)
12218 {
12219 struct dwarf2_section_info result;
12220 asection *sectp;
12221
12222 gdb_assert (section != NULL);
12223 gdb_assert (!section->is_virtual);
12224
12225 memset (&result, 0, sizeof (result));
12226 result.s.containing_section = section;
12227 result.is_virtual = true;
12228
12229 if (size == 0)
12230 return result;
12231
12232 sectp = section->get_bfd_section ();
12233
12234 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12235 bounds of the real section. This is a pretty-rare event, so just
12236 flag an error (easier) instead of a warning and trying to cope. */
12237 if (sectp == NULL
12238 || offset + size > bfd_section_size (sectp))
12239 {
12240 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
12241 " in section %s [in module %s]"),
12242 sectp ? bfd_section_name (sectp) : "<unknown>",
12243 objfile_name (per_objfile->objfile));
12244 }
12245
12246 result.virtual_offset = offset;
12247 result.size = size;
12248 return result;
12249 }
12250
12251 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12252 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12253 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12254 This is for DWP version 2 files. */
12255
12256 static struct dwo_unit *
12257 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
12258 struct dwp_file *dwp_file,
12259 uint32_t unit_index,
12260 const char *comp_dir,
12261 ULONGEST signature, int is_debug_types)
12262 {
12263 const struct dwp_hash_table *dwp_htab =
12264 is_debug_types ? dwp_file->tus : dwp_file->cus;
12265 bfd *dbfd = dwp_file->dbfd.get ();
12266 const char *kind = is_debug_types ? "TU" : "CU";
12267 struct dwo_file *dwo_file;
12268 struct dwo_unit *dwo_unit;
12269 struct virtual_v2_or_v5_dwo_sections sections;
12270 void **dwo_file_slot;
12271 int i;
12272
12273 gdb_assert (dwp_file->version == 2);
12274
12275 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
12276 kind, pulongest (unit_index), hex_string (signature),
12277 dwp_file->name);
12278
12279 /* Fetch the section offsets of this DWO unit. */
12280
12281 memset (&sections, 0, sizeof (sections));
12282
12283 for (i = 0; i < dwp_htab->nr_columns; ++i)
12284 {
12285 uint32_t offset = read_4_bytes (dbfd,
12286 dwp_htab->section_pool.v2.offsets
12287 + (((unit_index - 1) * dwp_htab->nr_columns
12288 + i)
12289 * sizeof (uint32_t)));
12290 uint32_t size = read_4_bytes (dbfd,
12291 dwp_htab->section_pool.v2.sizes
12292 + (((unit_index - 1) * dwp_htab->nr_columns
12293 + i)
12294 * sizeof (uint32_t)));
12295
12296 switch (dwp_htab->section_pool.v2.section_ids[i])
12297 {
12298 case DW_SECT_INFO:
12299 case DW_SECT_TYPES:
12300 sections.info_or_types_offset = offset;
12301 sections.info_or_types_size = size;
12302 break;
12303 case DW_SECT_ABBREV:
12304 sections.abbrev_offset = offset;
12305 sections.abbrev_size = size;
12306 break;
12307 case DW_SECT_LINE:
12308 sections.line_offset = offset;
12309 sections.line_size = size;
12310 break;
12311 case DW_SECT_LOC:
12312 sections.loc_offset = offset;
12313 sections.loc_size = size;
12314 break;
12315 case DW_SECT_STR_OFFSETS:
12316 sections.str_offsets_offset = offset;
12317 sections.str_offsets_size = size;
12318 break;
12319 case DW_SECT_MACINFO:
12320 sections.macinfo_offset = offset;
12321 sections.macinfo_size = size;
12322 break;
12323 case DW_SECT_MACRO:
12324 sections.macro_offset = offset;
12325 sections.macro_size = size;
12326 break;
12327 }
12328 }
12329
12330 /* It's easier for the rest of the code if we fake a struct dwo_file and
12331 have dwo_unit "live" in that. At least for now.
12332
12333 The DWP file can be made up of a random collection of CUs and TUs.
12334 However, for each CU + set of TUs that came from the same original DWO
12335 file, we can combine them back into a virtual DWO file to save space
12336 (fewer struct dwo_file objects to allocate). Remember that for really
12337 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12338
12339 std::string virtual_dwo_name =
12340 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12341 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12342 (long) (sections.line_size ? sections.line_offset : 0),
12343 (long) (sections.loc_size ? sections.loc_offset : 0),
12344 (long) (sections.str_offsets_size
12345 ? sections.str_offsets_offset : 0));
12346 /* Can we use an existing virtual DWO file? */
12347 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12348 comp_dir);
12349 /* Create one if necessary. */
12350 if (*dwo_file_slot == NULL)
12351 {
12352 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12353 virtual_dwo_name.c_str ());
12354
12355 dwo_file = new struct dwo_file;
12356 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12357 dwo_file->comp_dir = comp_dir;
12358 dwo_file->sections.abbrev =
12359 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12360 sections.abbrev_offset,
12361 sections.abbrev_size);
12362 dwo_file->sections.line =
12363 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12364 sections.line_offset,
12365 sections.line_size);
12366 dwo_file->sections.loc =
12367 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12368 sections.loc_offset, sections.loc_size);
12369 dwo_file->sections.macinfo =
12370 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12371 sections.macinfo_offset,
12372 sections.macinfo_size);
12373 dwo_file->sections.macro =
12374 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12375 sections.macro_offset,
12376 sections.macro_size);
12377 dwo_file->sections.str_offsets =
12378 create_dwp_v2_or_v5_section (per_objfile,
12379 &dwp_file->sections.str_offsets,
12380 sections.str_offsets_offset,
12381 sections.str_offsets_size);
12382 /* The "str" section is global to the entire DWP file. */
12383 dwo_file->sections.str = dwp_file->sections.str;
12384 /* The info or types section is assigned below to dwo_unit,
12385 there's no need to record it in dwo_file.
12386 Also, we can't simply record type sections in dwo_file because
12387 we record a pointer into the vector in dwo_unit. As we collect more
12388 types we'll grow the vector and eventually have to reallocate space
12389 for it, invalidating all copies of pointers into the previous
12390 contents. */
12391 *dwo_file_slot = dwo_file;
12392 }
12393 else
12394 {
12395 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12396 virtual_dwo_name.c_str ());
12397
12398 dwo_file = (struct dwo_file *) *dwo_file_slot;
12399 }
12400
12401 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12402 dwo_unit->dwo_file = dwo_file;
12403 dwo_unit->signature = signature;
12404 dwo_unit->section =
12405 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12406 *dwo_unit->section = create_dwp_v2_or_v5_section
12407 (per_objfile,
12408 is_debug_types
12409 ? &dwp_file->sections.types
12410 : &dwp_file->sections.info,
12411 sections.info_or_types_offset,
12412 sections.info_or_types_size);
12413 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12414
12415 return dwo_unit;
12416 }
12417
12418 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12419 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12420 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12421 This is for DWP version 5 files. */
12422
12423 static struct dwo_unit *
12424 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12425 struct dwp_file *dwp_file,
12426 uint32_t unit_index,
12427 const char *comp_dir,
12428 ULONGEST signature, int is_debug_types)
12429 {
12430 const struct dwp_hash_table *dwp_htab
12431 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12432 bfd *dbfd = dwp_file->dbfd.get ();
12433 const char *kind = is_debug_types ? "TU" : "CU";
12434 struct dwo_file *dwo_file;
12435 struct dwo_unit *dwo_unit;
12436 struct virtual_v2_or_v5_dwo_sections sections {};
12437 void **dwo_file_slot;
12438
12439 gdb_assert (dwp_file->version == 5);
12440
12441 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
12442 kind, pulongest (unit_index), hex_string (signature),
12443 dwp_file->name);
12444
12445 /* Fetch the section offsets of this DWO unit. */
12446
12447 /* memset (&sections, 0, sizeof (sections)); */
12448
12449 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12450 {
12451 uint32_t offset = read_4_bytes (dbfd,
12452 dwp_htab->section_pool.v5.offsets
12453 + (((unit_index - 1)
12454 * dwp_htab->nr_columns
12455 + i)
12456 * sizeof (uint32_t)));
12457 uint32_t size = read_4_bytes (dbfd,
12458 dwp_htab->section_pool.v5.sizes
12459 + (((unit_index - 1) * dwp_htab->nr_columns
12460 + i)
12461 * sizeof (uint32_t)));
12462
12463 switch (dwp_htab->section_pool.v5.section_ids[i])
12464 {
12465 case DW_SECT_ABBREV_V5:
12466 sections.abbrev_offset = offset;
12467 sections.abbrev_size = size;
12468 break;
12469 case DW_SECT_INFO_V5:
12470 sections.info_or_types_offset = offset;
12471 sections.info_or_types_size = size;
12472 break;
12473 case DW_SECT_LINE_V5:
12474 sections.line_offset = offset;
12475 sections.line_size = size;
12476 break;
12477 case DW_SECT_LOCLISTS_V5:
12478 sections.loclists_offset = offset;
12479 sections.loclists_size = size;
12480 break;
12481 case DW_SECT_MACRO_V5:
12482 sections.macro_offset = offset;
12483 sections.macro_size = size;
12484 break;
12485 case DW_SECT_RNGLISTS_V5:
12486 sections.rnglists_offset = offset;
12487 sections.rnglists_size = size;
12488 break;
12489 case DW_SECT_STR_OFFSETS_V5:
12490 sections.str_offsets_offset = offset;
12491 sections.str_offsets_size = size;
12492 break;
12493 case DW_SECT_RESERVED_V5:
12494 default:
12495 break;
12496 }
12497 }
12498
12499 /* It's easier for the rest of the code if we fake a struct dwo_file and
12500 have dwo_unit "live" in that. At least for now.
12501
12502 The DWP file can be made up of a random collection of CUs and TUs.
12503 However, for each CU + set of TUs that came from the same original DWO
12504 file, we can combine them back into a virtual DWO file to save space
12505 (fewer struct dwo_file objects to allocate). Remember that for really
12506 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12507
12508 std::string virtual_dwo_name =
12509 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
12510 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12511 (long) (sections.line_size ? sections.line_offset : 0),
12512 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12513 (long) (sections.str_offsets_size
12514 ? sections.str_offsets_offset : 0),
12515 (long) (sections.macro_size ? sections.macro_offset : 0),
12516 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
12517 /* Can we use an existing virtual DWO file? */
12518 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
12519 virtual_dwo_name.c_str (),
12520 comp_dir);
12521 /* Create one if necessary. */
12522 if (*dwo_file_slot == NULL)
12523 {
12524 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12525 virtual_dwo_name.c_str ());
12526
12527 dwo_file = new struct dwo_file;
12528 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12529 dwo_file->comp_dir = comp_dir;
12530 dwo_file->sections.abbrev =
12531 create_dwp_v2_or_v5_section (per_objfile,
12532 &dwp_file->sections.abbrev,
12533 sections.abbrev_offset,
12534 sections.abbrev_size);
12535 dwo_file->sections.line =
12536 create_dwp_v2_or_v5_section (per_objfile,
12537 &dwp_file->sections.line,
12538 sections.line_offset, sections.line_size);
12539 dwo_file->sections.macro =
12540 create_dwp_v2_or_v5_section (per_objfile,
12541 &dwp_file->sections.macro,
12542 sections.macro_offset,
12543 sections.macro_size);
12544 dwo_file->sections.loclists =
12545 create_dwp_v2_or_v5_section (per_objfile,
12546 &dwp_file->sections.loclists,
12547 sections.loclists_offset,
12548 sections.loclists_size);
12549 dwo_file->sections.rnglists =
12550 create_dwp_v2_or_v5_section (per_objfile,
12551 &dwp_file->sections.rnglists,
12552 sections.rnglists_offset,
12553 sections.rnglists_size);
12554 dwo_file->sections.str_offsets =
12555 create_dwp_v2_or_v5_section (per_objfile,
12556 &dwp_file->sections.str_offsets,
12557 sections.str_offsets_offset,
12558 sections.str_offsets_size);
12559 /* The "str" section is global to the entire DWP file. */
12560 dwo_file->sections.str = dwp_file->sections.str;
12561 /* The info or types section is assigned below to dwo_unit,
12562 there's no need to record it in dwo_file.
12563 Also, we can't simply record type sections in dwo_file because
12564 we record a pointer into the vector in dwo_unit. As we collect more
12565 types we'll grow the vector and eventually have to reallocate space
12566 for it, invalidating all copies of pointers into the previous
12567 contents. */
12568 *dwo_file_slot = dwo_file;
12569 }
12570 else
12571 {
12572 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12573 virtual_dwo_name.c_str ());
12574
12575 dwo_file = (struct dwo_file *) *dwo_file_slot;
12576 }
12577
12578 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12579 dwo_unit->dwo_file = dwo_file;
12580 dwo_unit->signature = signature;
12581 dwo_unit->section
12582 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12583 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
12584 &dwp_file->sections.info,
12585 sections.info_or_types_offset,
12586 sections.info_or_types_size);
12587 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12588
12589 return dwo_unit;
12590 }
12591
12592 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12593 Returns NULL if the signature isn't found. */
12594
12595 static struct dwo_unit *
12596 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12597 struct dwp_file *dwp_file, const char *comp_dir,
12598 ULONGEST signature, int is_debug_types)
12599 {
12600 const struct dwp_hash_table *dwp_htab =
12601 is_debug_types ? dwp_file->tus : dwp_file->cus;
12602 bfd *dbfd = dwp_file->dbfd.get ();
12603 uint32_t mask = dwp_htab->nr_slots - 1;
12604 uint32_t hash = signature & mask;
12605 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12606 unsigned int i;
12607 void **slot;
12608 struct dwo_unit find_dwo_cu;
12609
12610 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12611 find_dwo_cu.signature = signature;
12612 slot = htab_find_slot (is_debug_types
12613 ? dwp_file->loaded_tus.get ()
12614 : dwp_file->loaded_cus.get (),
12615 &find_dwo_cu, INSERT);
12616
12617 if (*slot != NULL)
12618 return (struct dwo_unit *) *slot;
12619
12620 /* Use a for loop so that we don't loop forever on bad debug info. */
12621 for (i = 0; i < dwp_htab->nr_slots; ++i)
12622 {
12623 ULONGEST signature_in_table;
12624
12625 signature_in_table =
12626 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12627 if (signature_in_table == signature)
12628 {
12629 uint32_t unit_index =
12630 read_4_bytes (dbfd,
12631 dwp_htab->unit_table + hash * sizeof (uint32_t));
12632
12633 if (dwp_file->version == 1)
12634 {
12635 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12636 unit_index, comp_dir,
12637 signature, is_debug_types);
12638 }
12639 else if (dwp_file->version == 2)
12640 {
12641 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12642 unit_index, comp_dir,
12643 signature, is_debug_types);
12644 }
12645 else /* version == 5 */
12646 {
12647 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12648 unit_index, comp_dir,
12649 signature, is_debug_types);
12650 }
12651 return (struct dwo_unit *) *slot;
12652 }
12653 if (signature_in_table == 0)
12654 return NULL;
12655 hash = (hash + hash2) & mask;
12656 }
12657
12658 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12659 " [in module %s]"),
12660 dwp_file->name);
12661 }
12662
12663 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12664 Open the file specified by FILE_NAME and hand it off to BFD for
12665 preliminary analysis. Return a newly initialized bfd *, which
12666 includes a canonicalized copy of FILE_NAME.
12667 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12668 SEARCH_CWD is true if the current directory is to be searched.
12669 It will be searched before debug-file-directory.
12670 If successful, the file is added to the bfd include table of the
12671 objfile's bfd (see gdb_bfd_record_inclusion).
12672 If unable to find/open the file, return NULL.
12673 NOTE: This function is derived from symfile_bfd_open. */
12674
12675 static gdb_bfd_ref_ptr
12676 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12677 const char *file_name, int is_dwp, int search_cwd)
12678 {
12679 int desc;
12680 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12681 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12682 to debug_file_directory. */
12683 const char *search_path;
12684 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12685
12686 gdb::unique_xmalloc_ptr<char> search_path_holder;
12687 if (search_cwd)
12688 {
12689 if (*debug_file_directory != '\0')
12690 {
12691 search_path_holder.reset (concat (".", dirname_separator_string,
12692 debug_file_directory,
12693 (char *) NULL));
12694 search_path = search_path_holder.get ();
12695 }
12696 else
12697 search_path = ".";
12698 }
12699 else
12700 search_path = debug_file_directory;
12701
12702 openp_flags flags = OPF_RETURN_REALPATH;
12703 if (is_dwp)
12704 flags |= OPF_SEARCH_IN_PATH;
12705
12706 gdb::unique_xmalloc_ptr<char> absolute_name;
12707 desc = openp (search_path, flags, file_name,
12708 O_RDONLY | O_BINARY, &absolute_name);
12709 if (desc < 0)
12710 return NULL;
12711
12712 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12713 gnutarget, desc));
12714 if (sym_bfd == NULL)
12715 return NULL;
12716 bfd_set_cacheable (sym_bfd.get (), 1);
12717
12718 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12719 return NULL;
12720
12721 /* Success. Record the bfd as having been included by the objfile's bfd.
12722 This is important because things like demangled_names_hash lives in the
12723 objfile's per_bfd space and may have references to things like symbol
12724 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12725 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12726
12727 return sym_bfd;
12728 }
12729
12730 /* Try to open DWO file FILE_NAME.
12731 COMP_DIR is the DW_AT_comp_dir attribute.
12732 The result is the bfd handle of the file.
12733 If there is a problem finding or opening the file, return NULL.
12734 Upon success, the canonicalized path of the file is stored in the bfd,
12735 same as symfile_bfd_open. */
12736
12737 static gdb_bfd_ref_ptr
12738 open_dwo_file (dwarf2_per_objfile *per_objfile,
12739 const char *file_name, const char *comp_dir)
12740 {
12741 if (IS_ABSOLUTE_PATH (file_name))
12742 return try_open_dwop_file (per_objfile, file_name,
12743 0 /*is_dwp*/, 0 /*search_cwd*/);
12744
12745 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12746
12747 if (comp_dir != NULL)
12748 {
12749 gdb::unique_xmalloc_ptr<char> path_to_try
12750 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12751
12752 /* NOTE: If comp_dir is a relative path, this will also try the
12753 search path, which seems useful. */
12754 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12755 0 /*is_dwp*/,
12756 1 /*search_cwd*/));
12757 if (abfd != NULL)
12758 return abfd;
12759 }
12760
12761 /* That didn't work, try debug-file-directory, which, despite its name,
12762 is a list of paths. */
12763
12764 if (*debug_file_directory == '\0')
12765 return NULL;
12766
12767 return try_open_dwop_file (per_objfile, file_name,
12768 0 /*is_dwp*/, 1 /*search_cwd*/);
12769 }
12770
12771 /* This function is mapped across the sections and remembers the offset and
12772 size of each of the DWO debugging sections we are interested in. */
12773
12774 static void
12775 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12776 dwo_sections *dwo_sections)
12777 {
12778 const struct dwop_section_names *names = &dwop_section_names;
12779
12780 if (names->abbrev_dwo.matches (sectp->name))
12781 {
12782 dwo_sections->abbrev.s.section = sectp;
12783 dwo_sections->abbrev.size = bfd_section_size (sectp);
12784 }
12785 else if (names->info_dwo.matches (sectp->name))
12786 {
12787 dwo_sections->info.s.section = sectp;
12788 dwo_sections->info.size = bfd_section_size (sectp);
12789 }
12790 else if (names->line_dwo.matches (sectp->name))
12791 {
12792 dwo_sections->line.s.section = sectp;
12793 dwo_sections->line.size = bfd_section_size (sectp);
12794 }
12795 else if (names->loc_dwo.matches (sectp->name))
12796 {
12797 dwo_sections->loc.s.section = sectp;
12798 dwo_sections->loc.size = bfd_section_size (sectp);
12799 }
12800 else if (names->loclists_dwo.matches (sectp->name))
12801 {
12802 dwo_sections->loclists.s.section = sectp;
12803 dwo_sections->loclists.size = bfd_section_size (sectp);
12804 }
12805 else if (names->macinfo_dwo.matches (sectp->name))
12806 {
12807 dwo_sections->macinfo.s.section = sectp;
12808 dwo_sections->macinfo.size = bfd_section_size (sectp);
12809 }
12810 else if (names->macro_dwo.matches (sectp->name))
12811 {
12812 dwo_sections->macro.s.section = sectp;
12813 dwo_sections->macro.size = bfd_section_size (sectp);
12814 }
12815 else if (names->rnglists_dwo.matches (sectp->name))
12816 {
12817 dwo_sections->rnglists.s.section = sectp;
12818 dwo_sections->rnglists.size = bfd_section_size (sectp);
12819 }
12820 else if (names->str_dwo.matches (sectp->name))
12821 {
12822 dwo_sections->str.s.section = sectp;
12823 dwo_sections->str.size = bfd_section_size (sectp);
12824 }
12825 else if (names->str_offsets_dwo.matches (sectp->name))
12826 {
12827 dwo_sections->str_offsets.s.section = sectp;
12828 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12829 }
12830 else if (names->types_dwo.matches (sectp->name))
12831 {
12832 struct dwarf2_section_info type_section;
12833
12834 memset (&type_section, 0, sizeof (type_section));
12835 type_section.s.section = sectp;
12836 type_section.size = bfd_section_size (sectp);
12837 dwo_sections->types.push_back (type_section);
12838 }
12839 }
12840
12841 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12842 by PER_CU. This is for the non-DWP case.
12843 The result is NULL if DWO_NAME can't be found. */
12844
12845 static struct dwo_file *
12846 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12847 const char *comp_dir)
12848 {
12849 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12850
12851 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12852 if (dbfd == NULL)
12853 {
12854 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12855
12856 return NULL;
12857 }
12858
12859 dwo_file_up dwo_file (new struct dwo_file);
12860 dwo_file->dwo_name = dwo_name;
12861 dwo_file->comp_dir = comp_dir;
12862 dwo_file->dbfd = std::move (dbfd);
12863
12864 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12865 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12866 &dwo_file->sections);
12867
12868 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12869 dwo_file->cus);
12870
12871 if (cu->per_cu->dwarf_version < 5)
12872 {
12873 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12874 dwo_file->sections.types, dwo_file->tus);
12875 }
12876 else
12877 {
12878 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12879 &dwo_file->sections.info, dwo_file->tus,
12880 rcuh_kind::COMPILE);
12881 }
12882
12883 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
12884
12885 return dwo_file.release ();
12886 }
12887
12888 /* This function is mapped across the sections and remembers the offset and
12889 size of each of the DWP debugging sections common to version 1 and 2 that
12890 we are interested in. */
12891
12892 static void
12893 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12894 dwp_file *dwp_file)
12895 {
12896 const struct dwop_section_names *names = &dwop_section_names;
12897 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12898
12899 /* Record the ELF section number for later lookup: this is what the
12900 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12901 gdb_assert (elf_section_nr < dwp_file->num_sections);
12902 dwp_file->elf_sections[elf_section_nr] = sectp;
12903
12904 /* Look for specific sections that we need. */
12905 if (names->str_dwo.matches (sectp->name))
12906 {
12907 dwp_file->sections.str.s.section = sectp;
12908 dwp_file->sections.str.size = bfd_section_size (sectp);
12909 }
12910 else if (names->cu_index.matches (sectp->name))
12911 {
12912 dwp_file->sections.cu_index.s.section = sectp;
12913 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12914 }
12915 else if (names->tu_index.matches (sectp->name))
12916 {
12917 dwp_file->sections.tu_index.s.section = sectp;
12918 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12919 }
12920 }
12921
12922 /* This function is mapped across the sections and remembers the offset and
12923 size of each of the DWP version 2 debugging sections that we are interested
12924 in. This is split into a separate function because we don't know if we
12925 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12926
12927 static void
12928 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12929 {
12930 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12931 const struct dwop_section_names *names = &dwop_section_names;
12932 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12933
12934 /* Record the ELF section number for later lookup: this is what the
12935 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12936 gdb_assert (elf_section_nr < dwp_file->num_sections);
12937 dwp_file->elf_sections[elf_section_nr] = sectp;
12938
12939 /* Look for specific sections that we need. */
12940 if (names->abbrev_dwo.matches (sectp->name))
12941 {
12942 dwp_file->sections.abbrev.s.section = sectp;
12943 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12944 }
12945 else if (names->info_dwo.matches (sectp->name))
12946 {
12947 dwp_file->sections.info.s.section = sectp;
12948 dwp_file->sections.info.size = bfd_section_size (sectp);
12949 }
12950 else if (names->line_dwo.matches (sectp->name))
12951 {
12952 dwp_file->sections.line.s.section = sectp;
12953 dwp_file->sections.line.size = bfd_section_size (sectp);
12954 }
12955 else if (names->loc_dwo.matches (sectp->name))
12956 {
12957 dwp_file->sections.loc.s.section = sectp;
12958 dwp_file->sections.loc.size = bfd_section_size (sectp);
12959 }
12960 else if (names->macinfo_dwo.matches (sectp->name))
12961 {
12962 dwp_file->sections.macinfo.s.section = sectp;
12963 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12964 }
12965 else if (names->macro_dwo.matches (sectp->name))
12966 {
12967 dwp_file->sections.macro.s.section = sectp;
12968 dwp_file->sections.macro.size = bfd_section_size (sectp);
12969 }
12970 else if (names->str_offsets_dwo.matches (sectp->name))
12971 {
12972 dwp_file->sections.str_offsets.s.section = sectp;
12973 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12974 }
12975 else if (names->types_dwo.matches (sectp->name))
12976 {
12977 dwp_file->sections.types.s.section = sectp;
12978 dwp_file->sections.types.size = bfd_section_size (sectp);
12979 }
12980 }
12981
12982 /* This function is mapped across the sections and remembers the offset and
12983 size of each of the DWP version 5 debugging sections that we are interested
12984 in. This is split into a separate function because we don't know if we
12985 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12986
12987 static void
12988 dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12989 {
12990 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12991 const struct dwop_section_names *names = &dwop_section_names;
12992 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12993
12994 /* Record the ELF section number for later lookup: this is what the
12995 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12996 gdb_assert (elf_section_nr < dwp_file->num_sections);
12997 dwp_file->elf_sections[elf_section_nr] = sectp;
12998
12999 /* Look for specific sections that we need. */
13000 if (names->abbrev_dwo.matches (sectp->name))
13001 {
13002 dwp_file->sections.abbrev.s.section = sectp;
13003 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13004 }
13005 else if (names->info_dwo.matches (sectp->name))
13006 {
13007 dwp_file->sections.info.s.section = sectp;
13008 dwp_file->sections.info.size = bfd_section_size (sectp);
13009 }
13010 else if (names->line_dwo.matches (sectp->name))
13011 {
13012 dwp_file->sections.line.s.section = sectp;
13013 dwp_file->sections.line.size = bfd_section_size (sectp);
13014 }
13015 else if (names->loclists_dwo.matches (sectp->name))
13016 {
13017 dwp_file->sections.loclists.s.section = sectp;
13018 dwp_file->sections.loclists.size = bfd_section_size (sectp);
13019 }
13020 else if (names->macro_dwo.matches (sectp->name))
13021 {
13022 dwp_file->sections.macro.s.section = sectp;
13023 dwp_file->sections.macro.size = bfd_section_size (sectp);
13024 }
13025 else if (names->rnglists_dwo.matches (sectp->name))
13026 {
13027 dwp_file->sections.rnglists.s.section = sectp;
13028 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
13029 }
13030 else if (names->str_offsets_dwo.matches (sectp->name))
13031 {
13032 dwp_file->sections.str_offsets.s.section = sectp;
13033 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13034 }
13035 }
13036
13037 /* Hash function for dwp_file loaded CUs/TUs. */
13038
13039 static hashval_t
13040 hash_dwp_loaded_cutus (const void *item)
13041 {
13042 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13043
13044 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13045 return dwo_unit->signature;
13046 }
13047
13048 /* Equality function for dwp_file loaded CUs/TUs. */
13049
13050 static int
13051 eq_dwp_loaded_cutus (const void *a, const void *b)
13052 {
13053 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13054 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13055
13056 return dua->signature == dub->signature;
13057 }
13058
13059 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13060
13061 static htab_up
13062 allocate_dwp_loaded_cutus_table ()
13063 {
13064 return htab_up (htab_create_alloc (3,
13065 hash_dwp_loaded_cutus,
13066 eq_dwp_loaded_cutus,
13067 NULL, xcalloc, xfree));
13068 }
13069
13070 /* Try to open DWP file FILE_NAME.
13071 The result is the bfd handle of the file.
13072 If there is a problem finding or opening the file, return NULL.
13073 Upon success, the canonicalized path of the file is stored in the bfd,
13074 same as symfile_bfd_open. */
13075
13076 static gdb_bfd_ref_ptr
13077 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
13078 {
13079 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
13080 1 /*is_dwp*/,
13081 1 /*search_cwd*/));
13082 if (abfd != NULL)
13083 return abfd;
13084
13085 /* Work around upstream bug 15652.
13086 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13087 [Whether that's a "bug" is debatable, but it is getting in our way.]
13088 We have no real idea where the dwp file is, because gdb's realpath-ing
13089 of the executable's path may have discarded the needed info.
13090 [IWBN if the dwp file name was recorded in the executable, akin to
13091 .gnu_debuglink, but that doesn't exist yet.]
13092 Strip the directory from FILE_NAME and search again. */
13093 if (*debug_file_directory != '\0')
13094 {
13095 /* Don't implicitly search the current directory here.
13096 If the user wants to search "." to handle this case,
13097 it must be added to debug-file-directory. */
13098 return try_open_dwop_file (per_objfile, lbasename (file_name),
13099 1 /*is_dwp*/,
13100 0 /*search_cwd*/);
13101 }
13102
13103 return NULL;
13104 }
13105
13106 /* Initialize the use of the DWP file for the current objfile.
13107 By convention the name of the DWP file is ${objfile}.dwp.
13108 The result is NULL if it can't be found. */
13109
13110 static std::unique_ptr<struct dwp_file>
13111 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
13112 {
13113 struct objfile *objfile = per_objfile->objfile;
13114
13115 /* Try to find first .dwp for the binary file before any symbolic links
13116 resolving. */
13117
13118 /* If the objfile is a debug file, find the name of the real binary
13119 file and get the name of dwp file from there. */
13120 std::string dwp_name;
13121 if (objfile->separate_debug_objfile_backlink != NULL)
13122 {
13123 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13124 const char *backlink_basename = lbasename (backlink->original_name);
13125
13126 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13127 }
13128 else
13129 dwp_name = objfile->original_name;
13130
13131 dwp_name += ".dwp";
13132
13133 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
13134 if (dbfd == NULL
13135 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13136 {
13137 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13138 dwp_name = objfile_name (objfile);
13139 dwp_name += ".dwp";
13140 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
13141 }
13142
13143 if (dbfd == NULL)
13144 {
13145 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
13146
13147 return std::unique_ptr<dwp_file> ();
13148 }
13149
13150 const char *name = bfd_get_filename (dbfd.get ());
13151 std::unique_ptr<struct dwp_file> dwp_file
13152 (new struct dwp_file (name, std::move (dbfd)));
13153
13154 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13155 dwp_file->elf_sections =
13156 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
13157 dwp_file->num_sections, asection *);
13158
13159 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13160 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
13161 dwp_file.get ());
13162
13163 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
13164
13165 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
13166
13167 /* The DWP file version is stored in the hash table. Oh well. */
13168 if (dwp_file->cus && dwp_file->tus
13169 && dwp_file->cus->version != dwp_file->tus->version)
13170 {
13171 /* Technically speaking, we should try to limp along, but this is
13172 pretty bizarre. We use pulongest here because that's the established
13173 portability solution (e.g, we cannot use %u for uint32_t). */
13174 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13175 " TU version %s [in DWP file %s]"),
13176 pulongest (dwp_file->cus->version),
13177 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13178 }
13179
13180 if (dwp_file->cus)
13181 dwp_file->version = dwp_file->cus->version;
13182 else if (dwp_file->tus)
13183 dwp_file->version = dwp_file->tus->version;
13184 else
13185 dwp_file->version = 2;
13186
13187 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13188 {
13189 if (dwp_file->version == 2)
13190 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
13191 dwp_file.get ());
13192 else
13193 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
13194 dwp_file.get ());
13195 }
13196
13197 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
13198 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
13199
13200 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
13201 dwarf_read_debug_printf (" %s CUs, %s TUs",
13202 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13203 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13204
13205 return dwp_file;
13206 }
13207
13208 /* Wrapper around open_and_init_dwp_file, only open it once. */
13209
13210 static struct dwp_file *
13211 get_dwp_file (dwarf2_per_objfile *per_objfile)
13212 {
13213 if (!per_objfile->per_bfd->dwp_checked)
13214 {
13215 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
13216 per_objfile->per_bfd->dwp_checked = 1;
13217 }
13218 return per_objfile->per_bfd->dwp_file.get ();
13219 }
13220
13221 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13222 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13223 or in the DWP file for the objfile, referenced by THIS_UNIT.
13224 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13225 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13226
13227 This is called, for example, when wanting to read a variable with a
13228 complex location. Therefore we don't want to do file i/o for every call.
13229 Therefore we don't want to look for a DWO file on every call.
13230 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13231 then we check if we've already seen DWO_NAME, and only THEN do we check
13232 for a DWO file.
13233
13234 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13235 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13236
13237 static struct dwo_unit *
13238 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13239 ULONGEST signature, int is_debug_types)
13240 {
13241 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13242 struct objfile *objfile = per_objfile->objfile;
13243 const char *kind = is_debug_types ? "TU" : "CU";
13244 void **dwo_file_slot;
13245 struct dwo_file *dwo_file;
13246 struct dwp_file *dwp_file;
13247
13248 /* First see if there's a DWP file.
13249 If we have a DWP file but didn't find the DWO inside it, don't
13250 look for the original DWO file. It makes gdb behave differently
13251 depending on whether one is debugging in the build tree. */
13252
13253 dwp_file = get_dwp_file (per_objfile);
13254 if (dwp_file != NULL)
13255 {
13256 const struct dwp_hash_table *dwp_htab =
13257 is_debug_types ? dwp_file->tus : dwp_file->cus;
13258
13259 if (dwp_htab != NULL)
13260 {
13261 struct dwo_unit *dwo_cutu =
13262 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
13263 is_debug_types);
13264
13265 if (dwo_cutu != NULL)
13266 {
13267 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
13268 kind, hex_string (signature),
13269 host_address_to_string (dwo_cutu));
13270
13271 return dwo_cutu;
13272 }
13273 }
13274 }
13275 else
13276 {
13277 /* No DWP file, look for the DWO file. */
13278
13279 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
13280 if (*dwo_file_slot == NULL)
13281 {
13282 /* Read in the file and build a table of the CUs/TUs it contains. */
13283 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
13284 }
13285 /* NOTE: This will be NULL if unable to open the file. */
13286 dwo_file = (struct dwo_file *) *dwo_file_slot;
13287
13288 if (dwo_file != NULL)
13289 {
13290 struct dwo_unit *dwo_cutu = NULL;
13291
13292 if (is_debug_types && dwo_file->tus)
13293 {
13294 struct dwo_unit find_dwo_cutu;
13295
13296 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13297 find_dwo_cutu.signature = signature;
13298 dwo_cutu
13299 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
13300 &find_dwo_cutu);
13301 }
13302 else if (!is_debug_types && dwo_file->cus)
13303 {
13304 struct dwo_unit find_dwo_cutu;
13305
13306 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13307 find_dwo_cutu.signature = signature;
13308 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
13309 &find_dwo_cutu);
13310 }
13311
13312 if (dwo_cutu != NULL)
13313 {
13314 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
13315 kind, dwo_name, hex_string (signature),
13316 host_address_to_string (dwo_cutu));
13317
13318 return dwo_cutu;
13319 }
13320 }
13321 }
13322
13323 /* We didn't find it. This could mean a dwo_id mismatch, or
13324 someone deleted the DWO/DWP file, or the search path isn't set up
13325 correctly to find the file. */
13326
13327 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
13328 kind, dwo_name, hex_string (signature));
13329
13330 /* This is a warning and not a complaint because it can be caused by
13331 pilot error (e.g., user accidentally deleting the DWO). */
13332 {
13333 /* Print the name of the DWP file if we looked there, helps the user
13334 better diagnose the problem. */
13335 std::string dwp_text;
13336
13337 if (dwp_file != NULL)
13338 dwp_text = string_printf (" [in DWP file %s]",
13339 lbasename (dwp_file->name));
13340
13341 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13342 " [in module %s]"),
13343 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13344 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
13345 }
13346 return NULL;
13347 }
13348
13349 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13350 See lookup_dwo_cutu_unit for details. */
13351
13352 static struct dwo_unit *
13353 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13354 ULONGEST signature)
13355 {
13356 gdb_assert (!cu->per_cu->is_debug_types);
13357
13358 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
13359 }
13360
13361 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13362 See lookup_dwo_cutu_unit for details. */
13363
13364 static struct dwo_unit *
13365 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
13366 {
13367 gdb_assert (cu->per_cu->is_debug_types);
13368
13369 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13370
13371 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
13372 }
13373
13374 /* Traversal function for queue_and_load_all_dwo_tus. */
13375
13376 static int
13377 queue_and_load_dwo_tu (void **slot, void *info)
13378 {
13379 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13380 dwarf2_cu *cu = (dwarf2_cu *) info;
13381 ULONGEST signature = dwo_unit->signature;
13382 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
13383
13384 if (sig_type != NULL)
13385 {
13386 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13387
13388 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13389 a real dependency of PER_CU on SIG_TYPE. That is detected later
13390 while processing PER_CU. */
13391 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
13392 load_full_type_unit (sig_cu, cu->per_objfile);
13393 cu->per_cu->imported_symtabs_push (sig_cu);
13394 }
13395
13396 return 1;
13397 }
13398
13399 /* Queue all TUs contained in the DWO of CU to be read in.
13400 The DWO may have the only definition of the type, though it may not be
13401 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13402 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13403
13404 static void
13405 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
13406 {
13407 struct dwo_unit *dwo_unit;
13408 struct dwo_file *dwo_file;
13409
13410 gdb_assert (cu != nullptr);
13411 gdb_assert (!cu->per_cu->is_debug_types);
13412 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
13413
13414 dwo_unit = cu->dwo_unit;
13415 gdb_assert (dwo_unit != NULL);
13416
13417 dwo_file = dwo_unit->dwo_file;
13418 if (dwo_file->tus != NULL)
13419 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
13420 }
13421
13422 /* Read in various DIEs. */
13423
13424 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13425 Inherit only the children of the DW_AT_abstract_origin DIE not being
13426 already referenced by DW_AT_abstract_origin from the children of the
13427 current DIE. */
13428
13429 static void
13430 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13431 {
13432 struct die_info *child_die;
13433 sect_offset *offsetp;
13434 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13435 struct die_info *origin_die;
13436 /* Iterator of the ORIGIN_DIE children. */
13437 struct die_info *origin_child_die;
13438 struct attribute *attr;
13439 struct dwarf2_cu *origin_cu;
13440 struct pending **origin_previous_list_in_scope;
13441
13442 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13443 if (!attr)
13444 return;
13445
13446 /* Note that following die references may follow to a die in a
13447 different cu. */
13448
13449 origin_cu = cu;
13450 origin_die = follow_die_ref (die, attr, &origin_cu);
13451
13452 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13453 symbols in. */
13454 origin_previous_list_in_scope = origin_cu->list_in_scope;
13455 origin_cu->list_in_scope = cu->list_in_scope;
13456
13457 if (die->tag != origin_die->tag
13458 && !(die->tag == DW_TAG_inlined_subroutine
13459 && origin_die->tag == DW_TAG_subprogram))
13460 complaint (_("DIE %s and its abstract origin %s have different tags"),
13461 sect_offset_str (die->sect_off),
13462 sect_offset_str (origin_die->sect_off));
13463
13464 std::vector<sect_offset> offsets;
13465
13466 for (child_die = die->child;
13467 child_die && child_die->tag;
13468 child_die = child_die->sibling)
13469 {
13470 struct die_info *child_origin_die;
13471 struct dwarf2_cu *child_origin_cu;
13472
13473 /* We are trying to process concrete instance entries:
13474 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13475 it's not relevant to our analysis here. i.e. detecting DIEs that are
13476 present in the abstract instance but not referenced in the concrete
13477 one. */
13478 if (child_die->tag == DW_TAG_call_site
13479 || child_die->tag == DW_TAG_GNU_call_site)
13480 continue;
13481
13482 /* For each CHILD_DIE, find the corresponding child of
13483 ORIGIN_DIE. If there is more than one layer of
13484 DW_AT_abstract_origin, follow them all; there shouldn't be,
13485 but GCC versions at least through 4.4 generate this (GCC PR
13486 40573). */
13487 child_origin_die = child_die;
13488 child_origin_cu = cu;
13489 while (1)
13490 {
13491 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13492 child_origin_cu);
13493 if (attr == NULL)
13494 break;
13495 child_origin_die = follow_die_ref (child_origin_die, attr,
13496 &child_origin_cu);
13497 }
13498
13499 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13500 counterpart may exist. */
13501 if (child_origin_die != child_die)
13502 {
13503 if (child_die->tag != child_origin_die->tag
13504 && !(child_die->tag == DW_TAG_inlined_subroutine
13505 && child_origin_die->tag == DW_TAG_subprogram))
13506 complaint (_("Child DIE %s and its abstract origin %s have "
13507 "different tags"),
13508 sect_offset_str (child_die->sect_off),
13509 sect_offset_str (child_origin_die->sect_off));
13510 if (child_origin_die->parent != origin_die)
13511 complaint (_("Child DIE %s and its abstract origin %s have "
13512 "different parents"),
13513 sect_offset_str (child_die->sect_off),
13514 sect_offset_str (child_origin_die->sect_off));
13515 else
13516 offsets.push_back (child_origin_die->sect_off);
13517 }
13518 }
13519 std::sort (offsets.begin (), offsets.end ());
13520 sect_offset *offsets_end = offsets.data () + offsets.size ();
13521 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13522 if (offsetp[-1] == *offsetp)
13523 complaint (_("Multiple children of DIE %s refer "
13524 "to DIE %s as their abstract origin"),
13525 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13526
13527 offsetp = offsets.data ();
13528 origin_child_die = origin_die->child;
13529 while (origin_child_die && origin_child_die->tag)
13530 {
13531 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13532 while (offsetp < offsets_end
13533 && *offsetp < origin_child_die->sect_off)
13534 offsetp++;
13535 if (offsetp >= offsets_end
13536 || *offsetp > origin_child_die->sect_off)
13537 {
13538 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13539 Check whether we're already processing ORIGIN_CHILD_DIE.
13540 This can happen with mutually referenced abstract_origins.
13541 PR 16581. */
13542 if (!origin_child_die->in_process)
13543 process_die (origin_child_die, origin_cu);
13544 }
13545 origin_child_die = origin_child_die->sibling;
13546 }
13547 origin_cu->list_in_scope = origin_previous_list_in_scope;
13548
13549 if (cu != origin_cu)
13550 compute_delayed_physnames (origin_cu);
13551 }
13552
13553 static void
13554 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13555 {
13556 struct objfile *objfile = cu->per_objfile->objfile;
13557 struct gdbarch *gdbarch = objfile->arch ();
13558 struct context_stack *newobj;
13559 CORE_ADDR lowpc;
13560 CORE_ADDR highpc;
13561 struct die_info *child_die;
13562 struct attribute *attr, *call_line, *call_file;
13563 const char *name;
13564 CORE_ADDR baseaddr;
13565 struct block *block;
13566 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13567 std::vector<struct symbol *> template_args;
13568 struct template_symbol *templ_func = NULL;
13569
13570 if (inlined_func)
13571 {
13572 /* If we do not have call site information, we can't show the
13573 caller of this inlined function. That's too confusing, so
13574 only use the scope for local variables. */
13575 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13576 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13577 if (call_line == NULL || call_file == NULL)
13578 {
13579 read_lexical_block_scope (die, cu);
13580 return;
13581 }
13582 }
13583
13584 baseaddr = objfile->text_section_offset ();
13585
13586 name = dwarf2_name (die, cu);
13587
13588 /* Ignore functions with missing or empty names. These are actually
13589 illegal according to the DWARF standard. */
13590 if (name == NULL)
13591 {
13592 complaint (_("missing name for subprogram DIE at %s"),
13593 sect_offset_str (die->sect_off));
13594 return;
13595 }
13596
13597 /* Ignore functions with missing or invalid low and high pc attributes. */
13598 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13599 <= PC_BOUNDS_INVALID)
13600 {
13601 attr = dwarf2_attr (die, DW_AT_external, cu);
13602 if (attr == nullptr || !attr->as_boolean ())
13603 complaint (_("cannot get low and high bounds "
13604 "for subprogram DIE at %s"),
13605 sect_offset_str (die->sect_off));
13606 return;
13607 }
13608
13609 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13610 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13611
13612 /* If we have any template arguments, then we must allocate a
13613 different sort of symbol. */
13614 for (child_die = die->child; child_die; child_die = child_die->sibling)
13615 {
13616 if (child_die->tag == DW_TAG_template_type_param
13617 || child_die->tag == DW_TAG_template_value_param)
13618 {
13619 templ_func = new (&objfile->objfile_obstack) template_symbol;
13620 templ_func->subclass = SYMBOL_TEMPLATE;
13621 break;
13622 }
13623 }
13624
13625 newobj = cu->get_builder ()->push_context (0, lowpc);
13626 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13627 (struct symbol *) templ_func);
13628
13629 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13630 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13631 cu->language);
13632
13633 /* If there is a location expression for DW_AT_frame_base, record
13634 it. */
13635 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13636 if (attr != nullptr)
13637 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13638
13639 /* If there is a location for the static link, record it. */
13640 newobj->static_link = NULL;
13641 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13642 if (attr != nullptr)
13643 {
13644 newobj->static_link
13645 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13646 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13647 cu->addr_type ());
13648 }
13649
13650 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13651
13652 if (die->child != NULL)
13653 {
13654 child_die = die->child;
13655 while (child_die && child_die->tag)
13656 {
13657 if (child_die->tag == DW_TAG_template_type_param
13658 || child_die->tag == DW_TAG_template_value_param)
13659 {
13660 struct symbol *arg = new_symbol (child_die, NULL, cu);
13661
13662 if (arg != NULL)
13663 template_args.push_back (arg);
13664 }
13665 else
13666 process_die (child_die, cu);
13667 child_die = child_die->sibling;
13668 }
13669 }
13670
13671 inherit_abstract_dies (die, cu);
13672
13673 /* If we have a DW_AT_specification, we might need to import using
13674 directives from the context of the specification DIE. See the
13675 comment in determine_prefix. */
13676 if (cu->language == language_cplus
13677 && dwarf2_attr (die, DW_AT_specification, cu))
13678 {
13679 struct dwarf2_cu *spec_cu = cu;
13680 struct die_info *spec_die = die_specification (die, &spec_cu);
13681
13682 while (spec_die)
13683 {
13684 child_die = spec_die->child;
13685 while (child_die && child_die->tag)
13686 {
13687 if (child_die->tag == DW_TAG_imported_module)
13688 process_die (child_die, spec_cu);
13689 child_die = child_die->sibling;
13690 }
13691
13692 /* In some cases, GCC generates specification DIEs that
13693 themselves contain DW_AT_specification attributes. */
13694 spec_die = die_specification (spec_die, &spec_cu);
13695 }
13696 }
13697
13698 struct context_stack cstk = cu->get_builder ()->pop_context ();
13699 /* Make a block for the local symbols within. */
13700 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13701 cstk.static_link, lowpc, highpc);
13702
13703 /* For C++, set the block's scope. */
13704 if ((cu->language == language_cplus
13705 || cu->language == language_fortran
13706 || cu->language == language_d
13707 || cu->language == language_rust)
13708 && cu->processing_has_namespace_info)
13709 block_set_scope (block, determine_prefix (die, cu),
13710 &objfile->objfile_obstack);
13711
13712 /* If we have address ranges, record them. */
13713 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13714
13715 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13716
13717 /* Attach template arguments to function. */
13718 if (!template_args.empty ())
13719 {
13720 gdb_assert (templ_func != NULL);
13721
13722 templ_func->n_template_arguments = template_args.size ();
13723 templ_func->template_arguments
13724 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13725 templ_func->n_template_arguments);
13726 memcpy (templ_func->template_arguments,
13727 template_args.data (),
13728 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13729
13730 /* Make sure that the symtab is set on the new symbols. Even
13731 though they don't appear in this symtab directly, other parts
13732 of gdb assume that symbols do, and this is reasonably
13733 true. */
13734 for (symbol *sym : template_args)
13735 symbol_set_symtab (sym, symbol_symtab (templ_func));
13736 }
13737
13738 /* In C++, we can have functions nested inside functions (e.g., when
13739 a function declares a class that has methods). This means that
13740 when we finish processing a function scope, we may need to go
13741 back to building a containing block's symbol lists. */
13742 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13743 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13744
13745 /* If we've finished processing a top-level function, subsequent
13746 symbols go in the file symbol list. */
13747 if (cu->get_builder ()->outermost_context_p ())
13748 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13749 }
13750
13751 /* Process all the DIES contained within a lexical block scope. Start
13752 a new scope, process the dies, and then close the scope. */
13753
13754 static void
13755 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13756 {
13757 struct objfile *objfile = cu->per_objfile->objfile;
13758 struct gdbarch *gdbarch = objfile->arch ();
13759 CORE_ADDR lowpc, highpc;
13760 struct die_info *child_die;
13761 CORE_ADDR baseaddr;
13762
13763 baseaddr = objfile->text_section_offset ();
13764
13765 /* Ignore blocks with missing or invalid low and high pc attributes. */
13766 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13767 as multiple lexical blocks? Handling children in a sane way would
13768 be nasty. Might be easier to properly extend generic blocks to
13769 describe ranges. */
13770 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13771 {
13772 case PC_BOUNDS_NOT_PRESENT:
13773 /* DW_TAG_lexical_block has no attributes, process its children as if
13774 there was no wrapping by that DW_TAG_lexical_block.
13775 GCC does no longer produces such DWARF since GCC r224161. */
13776 for (child_die = die->child;
13777 child_die != NULL && child_die->tag;
13778 child_die = child_die->sibling)
13779 {
13780 /* We might already be processing this DIE. This can happen
13781 in an unusual circumstance -- where a subroutine A
13782 appears lexically in another subroutine B, but A actually
13783 inlines B. The recursion is broken here, rather than in
13784 inherit_abstract_dies, because it seems better to simply
13785 drop concrete children here. */
13786 if (!child_die->in_process)
13787 process_die (child_die, cu);
13788 }
13789 return;
13790 case PC_BOUNDS_INVALID:
13791 return;
13792 }
13793 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13794 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13795
13796 cu->get_builder ()->push_context (0, lowpc);
13797 if (die->child != NULL)
13798 {
13799 child_die = die->child;
13800 while (child_die && child_die->tag)
13801 {
13802 process_die (child_die, cu);
13803 child_die = child_die->sibling;
13804 }
13805 }
13806 inherit_abstract_dies (die, cu);
13807 struct context_stack cstk = cu->get_builder ()->pop_context ();
13808
13809 if (*cu->get_builder ()->get_local_symbols () != NULL
13810 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13811 {
13812 struct block *block
13813 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13814 cstk.start_addr, highpc);
13815
13816 /* Note that recording ranges after traversing children, as we
13817 do here, means that recording a parent's ranges entails
13818 walking across all its children's ranges as they appear in
13819 the address map, which is quadratic behavior.
13820
13821 It would be nicer to record the parent's ranges before
13822 traversing its children, simply overriding whatever you find
13823 there. But since we don't even decide whether to create a
13824 block until after we've traversed its children, that's hard
13825 to do. */
13826 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13827 }
13828 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13829 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13830 }
13831
13832 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13833
13834 static void
13835 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13836 {
13837 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13838 struct objfile *objfile = per_objfile->objfile;
13839 struct gdbarch *gdbarch = objfile->arch ();
13840 CORE_ADDR pc, baseaddr;
13841 struct attribute *attr;
13842 struct call_site *call_site, call_site_local;
13843 void **slot;
13844 int nparams;
13845 struct die_info *child_die;
13846
13847 baseaddr = objfile->text_section_offset ();
13848
13849 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13850 if (attr == NULL)
13851 {
13852 /* This was a pre-DWARF-5 GNU extension alias
13853 for DW_AT_call_return_pc. */
13854 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13855 }
13856 if (!attr)
13857 {
13858 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13859 "DIE %s [in module %s]"),
13860 sect_offset_str (die->sect_off), objfile_name (objfile));
13861 return;
13862 }
13863 pc = attr->as_address () + baseaddr;
13864 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13865
13866 if (cu->call_site_htab == NULL)
13867 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13868 NULL, &objfile->objfile_obstack,
13869 hashtab_obstack_allocate, NULL);
13870 call_site_local.pc = pc;
13871 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13872 if (*slot != NULL)
13873 {
13874 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13875 "DIE %s [in module %s]"),
13876 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13877 objfile_name (objfile));
13878 return;
13879 }
13880
13881 /* Count parameters at the caller. */
13882
13883 nparams = 0;
13884 for (child_die = die->child; child_die && child_die->tag;
13885 child_die = child_die->sibling)
13886 {
13887 if (child_die->tag != DW_TAG_call_site_parameter
13888 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13889 {
13890 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13891 "DW_TAG_call_site child DIE %s [in module %s]"),
13892 child_die->tag, sect_offset_str (child_die->sect_off),
13893 objfile_name (objfile));
13894 continue;
13895 }
13896
13897 nparams++;
13898 }
13899
13900 call_site
13901 = ((struct call_site *)
13902 obstack_alloc (&objfile->objfile_obstack,
13903 sizeof (*call_site)
13904 + (sizeof (*call_site->parameter) * (nparams - 1))));
13905 *slot = call_site;
13906 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13907 call_site->pc = pc;
13908
13909 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13910 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13911 {
13912 struct die_info *func_die;
13913
13914 /* Skip also over DW_TAG_inlined_subroutine. */
13915 for (func_die = die->parent;
13916 func_die && func_die->tag != DW_TAG_subprogram
13917 && func_die->tag != DW_TAG_subroutine_type;
13918 func_die = func_die->parent);
13919
13920 /* DW_AT_call_all_calls is a superset
13921 of DW_AT_call_all_tail_calls. */
13922 if (func_die
13923 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13924 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13925 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13926 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13927 {
13928 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13929 not complete. But keep CALL_SITE for look ups via call_site_htab,
13930 both the initial caller containing the real return address PC and
13931 the final callee containing the current PC of a chain of tail
13932 calls do not need to have the tail call list complete. But any
13933 function candidate for a virtual tail call frame searched via
13934 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13935 determined unambiguously. */
13936 }
13937 else
13938 {
13939 struct type *func_type = NULL;
13940
13941 if (func_die)
13942 func_type = get_die_type (func_die, cu);
13943 if (func_type != NULL)
13944 {
13945 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13946
13947 /* Enlist this call site to the function. */
13948 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13949 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13950 }
13951 else
13952 complaint (_("Cannot find function owning DW_TAG_call_site "
13953 "DIE %s [in module %s]"),
13954 sect_offset_str (die->sect_off), objfile_name (objfile));
13955 }
13956 }
13957
13958 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13959 if (attr == NULL)
13960 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13961 if (attr == NULL)
13962 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13963 if (attr == NULL)
13964 {
13965 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13966 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13967 }
13968 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13969 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
13970 /* Keep NULL DWARF_BLOCK. */;
13971 else if (attr->form_is_block ())
13972 {
13973 struct dwarf2_locexpr_baton *dlbaton;
13974 struct dwarf_block *block = attr->as_block ();
13975
13976 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13977 dlbaton->data = block->data;
13978 dlbaton->size = block->size;
13979 dlbaton->per_objfile = per_objfile;
13980 dlbaton->per_cu = cu->per_cu;
13981
13982 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13983 }
13984 else if (attr->form_is_ref ())
13985 {
13986 struct dwarf2_cu *target_cu = cu;
13987 struct die_info *target_die;
13988
13989 target_die = follow_die_ref (die, attr, &target_cu);
13990 gdb_assert (target_cu->per_objfile->objfile == objfile);
13991 if (die_is_declaration (target_die, target_cu))
13992 {
13993 const char *target_physname;
13994
13995 /* Prefer the mangled name; otherwise compute the demangled one. */
13996 target_physname = dw2_linkage_name (target_die, target_cu);
13997 if (target_physname == NULL)
13998 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13999 if (target_physname == NULL)
14000 complaint (_("DW_AT_call_target target DIE has invalid "
14001 "physname, for referencing DIE %s [in module %s]"),
14002 sect_offset_str (die->sect_off), objfile_name (objfile));
14003 else
14004 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14005 }
14006 else
14007 {
14008 CORE_ADDR lowpc;
14009
14010 /* DW_AT_entry_pc should be preferred. */
14011 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14012 <= PC_BOUNDS_INVALID)
14013 complaint (_("DW_AT_call_target target DIE has invalid "
14014 "low pc, for referencing DIE %s [in module %s]"),
14015 sect_offset_str (die->sect_off), objfile_name (objfile));
14016 else
14017 {
14018 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14019 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14020 }
14021 }
14022 }
14023 else
14024 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14025 "block nor reference, for DIE %s [in module %s]"),
14026 sect_offset_str (die->sect_off), objfile_name (objfile));
14027
14028 call_site->per_cu = cu->per_cu;
14029 call_site->per_objfile = per_objfile;
14030
14031 for (child_die = die->child;
14032 child_die && child_die->tag;
14033 child_die = child_die->sibling)
14034 {
14035 struct call_site_parameter *parameter;
14036 struct attribute *loc, *origin;
14037
14038 if (child_die->tag != DW_TAG_call_site_parameter
14039 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14040 {
14041 /* Already printed the complaint above. */
14042 continue;
14043 }
14044
14045 gdb_assert (call_site->parameter_count < nparams);
14046 parameter = &call_site->parameter[call_site->parameter_count];
14047
14048 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14049 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14050 register is contained in DW_AT_call_value. */
14051
14052 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14053 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14054 if (origin == NULL)
14055 {
14056 /* This was a pre-DWARF-5 GNU extension alias
14057 for DW_AT_call_parameter. */
14058 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14059 }
14060 if (loc == NULL && origin != NULL && origin->form_is_ref ())
14061 {
14062 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14063
14064 sect_offset sect_off = origin->get_ref_die_offset ();
14065 if (!cu->header.offset_in_cu_p (sect_off))
14066 {
14067 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14068 binding can be done only inside one CU. Such referenced DIE
14069 therefore cannot be even moved to DW_TAG_partial_unit. */
14070 complaint (_("DW_AT_call_parameter offset is not in CU for "
14071 "DW_TAG_call_site child DIE %s [in module %s]"),
14072 sect_offset_str (child_die->sect_off),
14073 objfile_name (objfile));
14074 continue;
14075 }
14076 parameter->u.param_cu_off
14077 = (cu_offset) (sect_off - cu->header.sect_off);
14078 }
14079 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
14080 {
14081 complaint (_("No DW_FORM_block* DW_AT_location for "
14082 "DW_TAG_call_site child DIE %s [in module %s]"),
14083 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14084 continue;
14085 }
14086 else
14087 {
14088 struct dwarf_block *block = loc->as_block ();
14089
14090 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14091 (block->data, &block->data[block->size]);
14092 if (parameter->u.dwarf_reg != -1)
14093 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14094 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
14095 &block->data[block->size],
14096 &parameter->u.fb_offset))
14097 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14098 else
14099 {
14100 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14101 "for DW_FORM_block* DW_AT_location is supported for "
14102 "DW_TAG_call_site child DIE %s "
14103 "[in module %s]"),
14104 sect_offset_str (child_die->sect_off),
14105 objfile_name (objfile));
14106 continue;
14107 }
14108 }
14109
14110 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14111 if (attr == NULL)
14112 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14113 if (attr == NULL || !attr->form_is_block ())
14114 {
14115 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14116 "DW_TAG_call_site child DIE %s [in module %s]"),
14117 sect_offset_str (child_die->sect_off),
14118 objfile_name (objfile));
14119 continue;
14120 }
14121
14122 struct dwarf_block *block = attr->as_block ();
14123 parameter->value = block->data;
14124 parameter->value_size = block->size;
14125
14126 /* Parameters are not pre-cleared by memset above. */
14127 parameter->data_value = NULL;
14128 parameter->data_value_size = 0;
14129 call_site->parameter_count++;
14130
14131 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14132 if (attr == NULL)
14133 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14134 if (attr != nullptr)
14135 {
14136 if (!attr->form_is_block ())
14137 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14138 "DW_TAG_call_site child DIE %s [in module %s]"),
14139 sect_offset_str (child_die->sect_off),
14140 objfile_name (objfile));
14141 else
14142 {
14143 block = attr->as_block ();
14144 parameter->data_value = block->data;
14145 parameter->data_value_size = block->size;
14146 }
14147 }
14148 }
14149 }
14150
14151 /* Helper function for read_variable. If DIE represents a virtual
14152 table, then return the type of the concrete object that is
14153 associated with the virtual table. Otherwise, return NULL. */
14154
14155 static struct type *
14156 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14157 {
14158 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14159 if (attr == NULL)
14160 return NULL;
14161
14162 /* Find the type DIE. */
14163 struct die_info *type_die = NULL;
14164 struct dwarf2_cu *type_cu = cu;
14165
14166 if (attr->form_is_ref ())
14167 type_die = follow_die_ref (die, attr, &type_cu);
14168 if (type_die == NULL)
14169 return NULL;
14170
14171 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14172 return NULL;
14173 return die_containing_type (type_die, type_cu);
14174 }
14175
14176 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14177
14178 static void
14179 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14180 {
14181 struct rust_vtable_symbol *storage = NULL;
14182
14183 if (cu->language == language_rust)
14184 {
14185 struct type *containing_type = rust_containing_type (die, cu);
14186
14187 if (containing_type != NULL)
14188 {
14189 struct objfile *objfile = cu->per_objfile->objfile;
14190
14191 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
14192 storage->concrete_type = containing_type;
14193 storage->subclass = SYMBOL_RUST_VTABLE;
14194 }
14195 }
14196
14197 struct symbol *res = new_symbol (die, NULL, cu, storage);
14198 struct attribute *abstract_origin
14199 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14200 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14201 if (res == NULL && loc && abstract_origin)
14202 {
14203 /* We have a variable without a name, but with a location and an abstract
14204 origin. This may be a concrete instance of an abstract variable
14205 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14206 later. */
14207 struct dwarf2_cu *origin_cu = cu;
14208 struct die_info *origin_die
14209 = follow_die_ref (die, abstract_origin, &origin_cu);
14210 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14211 per_objfile->per_bfd->abstract_to_concrete
14212 [origin_die->sect_off].push_back (die->sect_off);
14213 }
14214 }
14215
14216 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14217 reading .debug_rnglists.
14218 Callback's type should be:
14219 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14220 Return true if the attributes are present and valid, otherwise,
14221 return false. */
14222
14223 template <typename Callback>
14224 static bool
14225 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14226 dwarf_tag tag, Callback &&callback)
14227 {
14228 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14229 struct objfile *objfile = per_objfile->objfile;
14230 bfd *obfd = objfile->obfd;
14231 /* Base address selection entry. */
14232 gdb::optional<CORE_ADDR> base;
14233 const gdb_byte *buffer;
14234 CORE_ADDR baseaddr;
14235 bool overflow = false;
14236 ULONGEST addr_index;
14237 struct dwarf2_section_info *rnglists_section;
14238
14239 base = cu->base_address;
14240 rnglists_section = cu_debug_rnglists_section (cu, tag);
14241 rnglists_section->read (objfile);
14242
14243 if (offset >= rnglists_section->size)
14244 {
14245 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14246 offset);
14247 return false;
14248 }
14249 buffer = rnglists_section->buffer + offset;
14250
14251 baseaddr = objfile->text_section_offset ();
14252
14253 while (1)
14254 {
14255 /* Initialize it due to a false compiler warning. */
14256 CORE_ADDR range_beginning = 0, range_end = 0;
14257 const gdb_byte *buf_end = (rnglists_section->buffer
14258 + rnglists_section->size);
14259 unsigned int bytes_read;
14260
14261 if (buffer == buf_end)
14262 {
14263 overflow = true;
14264 break;
14265 }
14266 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14267 switch (rlet)
14268 {
14269 case DW_RLE_end_of_list:
14270 break;
14271 case DW_RLE_base_address:
14272 if (buffer + cu->header.addr_size > buf_end)
14273 {
14274 overflow = true;
14275 break;
14276 }
14277 base = cu->header.read_address (obfd, buffer, &bytes_read);
14278 buffer += bytes_read;
14279 break;
14280 case DW_RLE_base_addressx:
14281 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14282 buffer += bytes_read;
14283 base = read_addr_index (cu, addr_index);
14284 break;
14285 case DW_RLE_start_length:
14286 if (buffer + cu->header.addr_size > buf_end)
14287 {
14288 overflow = true;
14289 break;
14290 }
14291 range_beginning = cu->header.read_address (obfd, buffer,
14292 &bytes_read);
14293 buffer += bytes_read;
14294 range_end = (range_beginning
14295 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14296 buffer += bytes_read;
14297 if (buffer > buf_end)
14298 {
14299 overflow = true;
14300 break;
14301 }
14302 break;
14303 case DW_RLE_startx_length:
14304 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14305 buffer += bytes_read;
14306 range_beginning = read_addr_index (cu, addr_index);
14307 if (buffer > buf_end)
14308 {
14309 overflow = true;
14310 break;
14311 }
14312 range_end = (range_beginning
14313 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14314 buffer += bytes_read;
14315 break;
14316 case DW_RLE_offset_pair:
14317 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14318 buffer += bytes_read;
14319 if (buffer > buf_end)
14320 {
14321 overflow = true;
14322 break;
14323 }
14324 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14325 buffer += bytes_read;
14326 if (buffer > buf_end)
14327 {
14328 overflow = true;
14329 break;
14330 }
14331 break;
14332 case DW_RLE_start_end:
14333 if (buffer + 2 * cu->header.addr_size > buf_end)
14334 {
14335 overflow = true;
14336 break;
14337 }
14338 range_beginning = cu->header.read_address (obfd, buffer,
14339 &bytes_read);
14340 buffer += bytes_read;
14341 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
14342 buffer += bytes_read;
14343 break;
14344 case DW_RLE_startx_endx:
14345 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14346 buffer += bytes_read;
14347 range_beginning = read_addr_index (cu, addr_index);
14348 if (buffer > buf_end)
14349 {
14350 overflow = true;
14351 break;
14352 }
14353 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14354 buffer += bytes_read;
14355 range_end = read_addr_index (cu, addr_index);
14356 break;
14357 default:
14358 complaint (_("Invalid .debug_rnglists data (no base address)"));
14359 return false;
14360 }
14361 if (rlet == DW_RLE_end_of_list || overflow)
14362 break;
14363 if (rlet == DW_RLE_base_address)
14364 continue;
14365
14366 if (range_beginning > range_end)
14367 {
14368 /* Inverted range entries are invalid. */
14369 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14370 return false;
14371 }
14372
14373 /* Empty range entries have no effect. */
14374 if (range_beginning == range_end)
14375 continue;
14376
14377 /* Only DW_RLE_offset_pair needs the base address added. */
14378 if (rlet == DW_RLE_offset_pair)
14379 {
14380 if (!base.has_value ())
14381 {
14382 /* We have no valid base address for the DW_RLE_offset_pair. */
14383 complaint (_("Invalid .debug_rnglists data (no base address for "
14384 "DW_RLE_offset_pair)"));
14385 return false;
14386 }
14387
14388 range_beginning += *base;
14389 range_end += *base;
14390 }
14391
14392 /* A not-uncommon case of bad debug info.
14393 Don't pollute the addrmap with bad data. */
14394 if (range_beginning + baseaddr == 0
14395 && !per_objfile->per_bfd->has_section_at_zero)
14396 {
14397 complaint (_(".debug_rnglists entry has start address of zero"
14398 " [in module %s]"), objfile_name (objfile));
14399 continue;
14400 }
14401
14402 callback (range_beginning, range_end);
14403 }
14404
14405 if (overflow)
14406 {
14407 complaint (_("Offset %d is not terminated "
14408 "for DW_AT_ranges attribute"),
14409 offset);
14410 return false;
14411 }
14412
14413 return true;
14414 }
14415
14416 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14417 Callback's type should be:
14418 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14419 Return 1 if the attributes are present and valid, otherwise, return 0. */
14420
14421 template <typename Callback>
14422 static int
14423 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
14424 Callback &&callback)
14425 {
14426 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14427 struct objfile *objfile = per_objfile->objfile;
14428 struct comp_unit_head *cu_header = &cu->header;
14429 bfd *obfd = objfile->obfd;
14430 unsigned int addr_size = cu_header->addr_size;
14431 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14432 /* Base address selection entry. */
14433 gdb::optional<CORE_ADDR> base;
14434 unsigned int dummy;
14435 const gdb_byte *buffer;
14436 CORE_ADDR baseaddr;
14437
14438 if (cu_header->version >= 5)
14439 return dwarf2_rnglists_process (offset, cu, tag, callback);
14440
14441 base = cu->base_address;
14442
14443 per_objfile->per_bfd->ranges.read (objfile);
14444 if (offset >= per_objfile->per_bfd->ranges.size)
14445 {
14446 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14447 offset);
14448 return 0;
14449 }
14450 buffer = per_objfile->per_bfd->ranges.buffer + offset;
14451
14452 baseaddr = objfile->text_section_offset ();
14453
14454 while (1)
14455 {
14456 CORE_ADDR range_beginning, range_end;
14457
14458 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
14459 buffer += addr_size;
14460 range_end = cu->header.read_address (obfd, buffer, &dummy);
14461 buffer += addr_size;
14462 offset += 2 * addr_size;
14463
14464 /* An end of list marker is a pair of zero addresses. */
14465 if (range_beginning == 0 && range_end == 0)
14466 /* Found the end of list entry. */
14467 break;
14468
14469 /* Each base address selection entry is a pair of 2 values.
14470 The first is the largest possible address, the second is
14471 the base address. Check for a base address here. */
14472 if ((range_beginning & mask) == mask)
14473 {
14474 /* If we found the largest possible address, then we already
14475 have the base address in range_end. */
14476 base = range_end;
14477 continue;
14478 }
14479
14480 if (!base.has_value ())
14481 {
14482 /* We have no valid base address for the ranges
14483 data. */
14484 complaint (_("Invalid .debug_ranges data (no base address)"));
14485 return 0;
14486 }
14487
14488 if (range_beginning > range_end)
14489 {
14490 /* Inverted range entries are invalid. */
14491 complaint (_("Invalid .debug_ranges data (inverted range)"));
14492 return 0;
14493 }
14494
14495 /* Empty range entries have no effect. */
14496 if (range_beginning == range_end)
14497 continue;
14498
14499 range_beginning += *base;
14500 range_end += *base;
14501
14502 /* A not-uncommon case of bad debug info.
14503 Don't pollute the addrmap with bad data. */
14504 if (range_beginning + baseaddr == 0
14505 && !per_objfile->per_bfd->has_section_at_zero)
14506 {
14507 complaint (_(".debug_ranges entry has start address of zero"
14508 " [in module %s]"), objfile_name (objfile));
14509 continue;
14510 }
14511
14512 callback (range_beginning, range_end);
14513 }
14514
14515 return 1;
14516 }
14517
14518 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14519 Return 1 if the attributes are present and valid, otherwise, return 0.
14520 If RANGES_PST is not NULL we should set up the `psymtabs_addrmap'. */
14521
14522 static int
14523 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14524 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14525 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
14526 {
14527 struct objfile *objfile = cu->per_objfile->objfile;
14528 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
14529 struct gdbarch *gdbarch = objfile->arch ();
14530 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14531 int low_set = 0;
14532 CORE_ADDR low = 0;
14533 CORE_ADDR high = 0;
14534 int retval;
14535
14536 retval = dwarf2_ranges_process (offset, cu, tag,
14537 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14538 {
14539 if (ranges_pst != NULL)
14540 {
14541 CORE_ADDR lowpc;
14542 CORE_ADDR highpc;
14543
14544 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14545 range_beginning + baseaddr)
14546 - baseaddr);
14547 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14548 range_end + baseaddr)
14549 - baseaddr);
14550 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
14551 lowpc, highpc - 1, ranges_pst);
14552 }
14553
14554 /* FIXME: This is recording everything as a low-high
14555 segment of consecutive addresses. We should have a
14556 data structure for discontiguous block ranges
14557 instead. */
14558 if (! low_set)
14559 {
14560 low = range_beginning;
14561 high = range_end;
14562 low_set = 1;
14563 }
14564 else
14565 {
14566 if (range_beginning < low)
14567 low = range_beginning;
14568 if (range_end > high)
14569 high = range_end;
14570 }
14571 });
14572 if (!retval)
14573 return 0;
14574
14575 if (! low_set)
14576 /* If the first entry is an end-of-list marker, the range
14577 describes an empty scope, i.e. no instructions. */
14578 return 0;
14579
14580 if (low_return)
14581 *low_return = low;
14582 if (high_return)
14583 *high_return = high;
14584 return 1;
14585 }
14586
14587 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14588 definition for the return value. *LOWPC and *HIGHPC are set iff
14589 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14590
14591 static enum pc_bounds_kind
14592 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14593 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14594 dwarf2_psymtab *pst)
14595 {
14596 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14597 struct attribute *attr;
14598 struct attribute *attr_high;
14599 CORE_ADDR low = 0;
14600 CORE_ADDR high = 0;
14601 enum pc_bounds_kind ret;
14602
14603 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14604 if (attr_high)
14605 {
14606 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14607 if (attr != nullptr)
14608 {
14609 low = attr->as_address ();
14610 high = attr_high->as_address ();
14611 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14612 high += low;
14613 }
14614 else
14615 /* Found high w/o low attribute. */
14616 return PC_BOUNDS_INVALID;
14617
14618 /* Found consecutive range of addresses. */
14619 ret = PC_BOUNDS_HIGH_LOW;
14620 }
14621 else
14622 {
14623 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14624 if (attr != nullptr && attr->form_is_unsigned ())
14625 {
14626 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14627 on DWARF version). */
14628 ULONGEST ranges_offset = attr->as_unsigned ();
14629
14630 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14631 this value. */
14632 if (die->tag != DW_TAG_compile_unit)
14633 ranges_offset += cu->gnu_ranges_base;
14634
14635 /* Value of the DW_AT_ranges attribute is the offset in the
14636 .debug_ranges section. */
14637 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14638 die->tag))
14639 return PC_BOUNDS_INVALID;
14640 /* Found discontinuous range of addresses. */
14641 ret = PC_BOUNDS_RANGES;
14642 }
14643 else
14644 return PC_BOUNDS_NOT_PRESENT;
14645 }
14646
14647 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14648 if (high <= low)
14649 return PC_BOUNDS_INVALID;
14650
14651 /* When using the GNU linker, .gnu.linkonce. sections are used to
14652 eliminate duplicate copies of functions and vtables and such.
14653 The linker will arbitrarily choose one and discard the others.
14654 The AT_*_pc values for such functions refer to local labels in
14655 these sections. If the section from that file was discarded, the
14656 labels are not in the output, so the relocs get a value of 0.
14657 If this is a discarded function, mark the pc bounds as invalid,
14658 so that GDB will ignore it. */
14659 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14660 return PC_BOUNDS_INVALID;
14661
14662 *lowpc = low;
14663 if (highpc)
14664 *highpc = high;
14665 return ret;
14666 }
14667
14668 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14669 its low and high PC addresses. Do nothing if these addresses could not
14670 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14671 and HIGHPC to the high address if greater than HIGHPC. */
14672
14673 static void
14674 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14675 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14676 struct dwarf2_cu *cu)
14677 {
14678 CORE_ADDR low, high;
14679 struct die_info *child = die->child;
14680
14681 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14682 {
14683 *lowpc = std::min (*lowpc, low);
14684 *highpc = std::max (*highpc, high);
14685 }
14686
14687 /* If the language does not allow nested subprograms (either inside
14688 subprograms or lexical blocks), we're done. */
14689 if (cu->language != language_ada)
14690 return;
14691
14692 /* Check all the children of the given DIE. If it contains nested
14693 subprograms, then check their pc bounds. Likewise, we need to
14694 check lexical blocks as well, as they may also contain subprogram
14695 definitions. */
14696 while (child && child->tag)
14697 {
14698 if (child->tag == DW_TAG_subprogram
14699 || child->tag == DW_TAG_lexical_block)
14700 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14701 child = child->sibling;
14702 }
14703 }
14704
14705 /* Get the low and high pc's represented by the scope DIE, and store
14706 them in *LOWPC and *HIGHPC. If the correct values can't be
14707 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14708
14709 static void
14710 get_scope_pc_bounds (struct die_info *die,
14711 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14712 struct dwarf2_cu *cu)
14713 {
14714 CORE_ADDR best_low = (CORE_ADDR) -1;
14715 CORE_ADDR best_high = (CORE_ADDR) 0;
14716 CORE_ADDR current_low, current_high;
14717
14718 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14719 >= PC_BOUNDS_RANGES)
14720 {
14721 best_low = current_low;
14722 best_high = current_high;
14723 }
14724 else
14725 {
14726 struct die_info *child = die->child;
14727
14728 while (child && child->tag)
14729 {
14730 switch (child->tag) {
14731 case DW_TAG_subprogram:
14732 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14733 break;
14734 case DW_TAG_namespace:
14735 case DW_TAG_module:
14736 /* FIXME: carlton/2004-01-16: Should we do this for
14737 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14738 that current GCC's always emit the DIEs corresponding
14739 to definitions of methods of classes as children of a
14740 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14741 the DIEs giving the declarations, which could be
14742 anywhere). But I don't see any reason why the
14743 standards says that they have to be there. */
14744 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14745
14746 if (current_low != ((CORE_ADDR) -1))
14747 {
14748 best_low = std::min (best_low, current_low);
14749 best_high = std::max (best_high, current_high);
14750 }
14751 break;
14752 default:
14753 /* Ignore. */
14754 break;
14755 }
14756
14757 child = child->sibling;
14758 }
14759 }
14760
14761 *lowpc = best_low;
14762 *highpc = best_high;
14763 }
14764
14765 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14766 in DIE. */
14767
14768 static void
14769 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14770 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14771 {
14772 struct objfile *objfile = cu->per_objfile->objfile;
14773 struct gdbarch *gdbarch = objfile->arch ();
14774 struct attribute *attr;
14775 struct attribute *attr_high;
14776
14777 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14778 if (attr_high)
14779 {
14780 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14781 if (attr != nullptr)
14782 {
14783 CORE_ADDR low = attr->as_address ();
14784 CORE_ADDR high = attr_high->as_address ();
14785
14786 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14787 high += low;
14788
14789 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14790 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14791 cu->get_builder ()->record_block_range (block, low, high - 1);
14792 }
14793 }
14794
14795 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14796 if (attr != nullptr && attr->form_is_unsigned ())
14797 {
14798 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14799 on DWARF version). */
14800 ULONGEST ranges_offset = attr->as_unsigned ();
14801
14802 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14803 this value. */
14804 if (die->tag != DW_TAG_compile_unit)
14805 ranges_offset += cu->gnu_ranges_base;
14806
14807 std::vector<blockrange> blockvec;
14808 dwarf2_ranges_process (ranges_offset, cu, die->tag,
14809 [&] (CORE_ADDR start, CORE_ADDR end)
14810 {
14811 start += baseaddr;
14812 end += baseaddr;
14813 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14814 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14815 cu->get_builder ()->record_block_range (block, start, end - 1);
14816 blockvec.emplace_back (start, end);
14817 });
14818
14819 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14820 }
14821 }
14822
14823 /* Check whether the producer field indicates either of GCC < 4.6, or the
14824 Intel C/C++ compiler, and cache the result in CU. */
14825
14826 static void
14827 check_producer (struct dwarf2_cu *cu)
14828 {
14829 int major, minor;
14830
14831 if (cu->producer == NULL)
14832 {
14833 /* For unknown compilers expect their behavior is DWARF version
14834 compliant.
14835
14836 GCC started to support .debug_types sections by -gdwarf-4 since
14837 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14838 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14839 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14840 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14841 }
14842 else if (producer_is_gcc (cu->producer, &major, &minor))
14843 {
14844 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14845 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14846 }
14847 else if (producer_is_icc (cu->producer, &major, &minor))
14848 {
14849 cu->producer_is_icc = true;
14850 cu->producer_is_icc_lt_14 = major < 14;
14851 }
14852 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14853 cu->producer_is_codewarrior = true;
14854 else
14855 {
14856 /* For other non-GCC compilers, expect their behavior is DWARF version
14857 compliant. */
14858 }
14859
14860 cu->checked_producer = true;
14861 }
14862
14863 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14864 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14865 during 4.6.0 experimental. */
14866
14867 static bool
14868 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14869 {
14870 if (!cu->checked_producer)
14871 check_producer (cu);
14872
14873 return cu->producer_is_gxx_lt_4_6;
14874 }
14875
14876
14877 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14878 with incorrect is_stmt attributes. */
14879
14880 static bool
14881 producer_is_codewarrior (struct dwarf2_cu *cu)
14882 {
14883 if (!cu->checked_producer)
14884 check_producer (cu);
14885
14886 return cu->producer_is_codewarrior;
14887 }
14888
14889 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
14890 If that attribute is not available, return the appropriate
14891 default. */
14892
14893 static enum dwarf_access_attribute
14894 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14895 {
14896 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14897 if (attr != nullptr)
14898 {
14899 LONGEST value = attr->constant_value (-1);
14900 if (value == DW_ACCESS_public
14901 || value == DW_ACCESS_protected
14902 || value == DW_ACCESS_private)
14903 return (dwarf_access_attribute) value;
14904 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14905 plongest (value));
14906 }
14907
14908 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14909 {
14910 /* The default DWARF 2 accessibility for members is public, the default
14911 accessibility for inheritance is private. */
14912
14913 if (die->tag != DW_TAG_inheritance)
14914 return DW_ACCESS_public;
14915 else
14916 return DW_ACCESS_private;
14917 }
14918 else
14919 {
14920 /* DWARF 3+ defines the default accessibility a different way. The same
14921 rules apply now for DW_TAG_inheritance as for the members and it only
14922 depends on the container kind. */
14923
14924 if (die->parent->tag == DW_TAG_class_type)
14925 return DW_ACCESS_private;
14926 else
14927 return DW_ACCESS_public;
14928 }
14929 }
14930
14931 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14932 offset. If the attribute was not found return 0, otherwise return
14933 1. If it was found but could not properly be handled, set *OFFSET
14934 to 0. */
14935
14936 static int
14937 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14938 LONGEST *offset)
14939 {
14940 struct attribute *attr;
14941
14942 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14943 if (attr != NULL)
14944 {
14945 *offset = 0;
14946
14947 /* Note that we do not check for a section offset first here.
14948 This is because DW_AT_data_member_location is new in DWARF 4,
14949 so if we see it, we can assume that a constant form is really
14950 a constant and not a section offset. */
14951 if (attr->form_is_constant ())
14952 *offset = attr->constant_value (0);
14953 else if (attr->form_is_section_offset ())
14954 dwarf2_complex_location_expr_complaint ();
14955 else if (attr->form_is_block ())
14956 *offset = decode_locdesc (attr->as_block (), cu);
14957 else
14958 dwarf2_complex_location_expr_complaint ();
14959
14960 return 1;
14961 }
14962
14963 return 0;
14964 }
14965
14966 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14967
14968 static void
14969 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14970 struct field *field)
14971 {
14972 struct attribute *attr;
14973
14974 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14975 if (attr != NULL)
14976 {
14977 if (attr->form_is_constant ())
14978 {
14979 LONGEST offset = attr->constant_value (0);
14980 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14981 }
14982 else if (attr->form_is_section_offset ())
14983 dwarf2_complex_location_expr_complaint ();
14984 else if (attr->form_is_block ())
14985 {
14986 bool handled;
14987 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
14988 if (handled)
14989 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14990 else
14991 {
14992 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14993 struct objfile *objfile = per_objfile->objfile;
14994 struct dwarf2_locexpr_baton *dlbaton
14995 = XOBNEW (&objfile->objfile_obstack,
14996 struct dwarf2_locexpr_baton);
14997 dlbaton->data = attr->as_block ()->data;
14998 dlbaton->size = attr->as_block ()->size;
14999 /* When using this baton, we want to compute the address
15000 of the field, not the value. This is why
15001 is_reference is set to false here. */
15002 dlbaton->is_reference = false;
15003 dlbaton->per_objfile = per_objfile;
15004 dlbaton->per_cu = cu->per_cu;
15005
15006 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
15007 }
15008 }
15009 else
15010 dwarf2_complex_location_expr_complaint ();
15011 }
15012 }
15013
15014 /* Add an aggregate field to the field list. */
15015
15016 static void
15017 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15018 struct dwarf2_cu *cu)
15019 {
15020 struct objfile *objfile = cu->per_objfile->objfile;
15021 struct gdbarch *gdbarch = objfile->arch ();
15022 struct nextfield *new_field;
15023 struct attribute *attr;
15024 struct field *fp;
15025 const char *fieldname = "";
15026
15027 if (die->tag == DW_TAG_inheritance)
15028 {
15029 fip->baseclasses.emplace_back ();
15030 new_field = &fip->baseclasses.back ();
15031 }
15032 else
15033 {
15034 fip->fields.emplace_back ();
15035 new_field = &fip->fields.back ();
15036 }
15037
15038 new_field->offset = die->sect_off;
15039
15040 new_field->accessibility = dwarf2_access_attribute (die, cu);
15041 if (new_field->accessibility != DW_ACCESS_public)
15042 fip->non_public_fields = true;
15043
15044 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15045 if (attr != nullptr)
15046 new_field->virtuality = attr->as_virtuality ();
15047 else
15048 new_field->virtuality = DW_VIRTUALITY_none;
15049
15050 fp = &new_field->field;
15051
15052 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15053 {
15054 /* Data member other than a C++ static data member. */
15055
15056 /* Get type of field. */
15057 fp->set_type (die_type (die, cu));
15058
15059 SET_FIELD_BITPOS (*fp, 0);
15060
15061 /* Get bit size of field (zero if none). */
15062 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15063 if (attr != nullptr)
15064 {
15065 FIELD_BITSIZE (*fp) = attr->constant_value (0);
15066 }
15067 else
15068 {
15069 FIELD_BITSIZE (*fp) = 0;
15070 }
15071
15072 /* Get bit offset of field. */
15073 handle_data_member_location (die, cu, fp);
15074 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15075 if (attr != nullptr && attr->form_is_constant ())
15076 {
15077 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
15078 {
15079 /* For big endian bits, the DW_AT_bit_offset gives the
15080 additional bit offset from the MSB of the containing
15081 anonymous object to the MSB of the field. We don't
15082 have to do anything special since we don't need to
15083 know the size of the anonymous object. */
15084 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15085 + attr->constant_value (0)));
15086 }
15087 else
15088 {
15089 /* For little endian bits, compute the bit offset to the
15090 MSB of the anonymous object, subtract off the number of
15091 bits from the MSB of the field to the MSB of the
15092 object, and then subtract off the number of bits of
15093 the field itself. The result is the bit offset of
15094 the LSB of the field. */
15095 int anonymous_size;
15096 int bit_offset = attr->constant_value (0);
15097
15098 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15099 if (attr != nullptr && attr->form_is_constant ())
15100 {
15101 /* The size of the anonymous object containing
15102 the bit field is explicit, so use the
15103 indicated size (in bytes). */
15104 anonymous_size = attr->constant_value (0);
15105 }
15106 else
15107 {
15108 /* The size of the anonymous object containing
15109 the bit field must be inferred from the type
15110 attribute of the data member containing the
15111 bit field. */
15112 anonymous_size = TYPE_LENGTH (fp->type ());
15113 }
15114 SET_FIELD_BITPOS (*fp,
15115 (FIELD_BITPOS (*fp)
15116 + anonymous_size * bits_per_byte
15117 - bit_offset - FIELD_BITSIZE (*fp)));
15118 }
15119 }
15120 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15121 if (attr != NULL)
15122 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15123 + attr->constant_value (0)));
15124
15125 /* Get name of field. */
15126 fieldname = dwarf2_name (die, cu);
15127 if (fieldname == NULL)
15128 fieldname = "";
15129
15130 /* The name is already allocated along with this objfile, so we don't
15131 need to duplicate it for the type. */
15132 fp->name = fieldname;
15133
15134 /* Change accessibility for artificial fields (e.g. virtual table
15135 pointer or virtual base class pointer) to private. */
15136 if (dwarf2_attr (die, DW_AT_artificial, cu))
15137 {
15138 FIELD_ARTIFICIAL (*fp) = 1;
15139 new_field->accessibility = DW_ACCESS_private;
15140 fip->non_public_fields = true;
15141 }
15142 }
15143 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15144 {
15145 /* C++ static member. */
15146
15147 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15148 is a declaration, but all versions of G++ as of this writing
15149 (so through at least 3.2.1) incorrectly generate
15150 DW_TAG_variable tags. */
15151
15152 const char *physname;
15153
15154 /* Get name of field. */
15155 fieldname = dwarf2_name (die, cu);
15156 if (fieldname == NULL)
15157 return;
15158
15159 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15160 if (attr
15161 /* Only create a symbol if this is an external value.
15162 new_symbol checks this and puts the value in the global symbol
15163 table, which we want. If it is not external, new_symbol
15164 will try to put the value in cu->list_in_scope which is wrong. */
15165 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15166 {
15167 /* A static const member, not much different than an enum as far as
15168 we're concerned, except that we can support more types. */
15169 new_symbol (die, NULL, cu);
15170 }
15171
15172 /* Get physical name. */
15173 physname = dwarf2_physname (fieldname, die, cu);
15174
15175 /* The name is already allocated along with this objfile, so we don't
15176 need to duplicate it for the type. */
15177 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15178 fp->set_type (die_type (die, cu));
15179 FIELD_NAME (*fp) = fieldname;
15180 }
15181 else if (die->tag == DW_TAG_inheritance)
15182 {
15183 /* C++ base class field. */
15184 handle_data_member_location (die, cu, fp);
15185 FIELD_BITSIZE (*fp) = 0;
15186 fp->set_type (die_type (die, cu));
15187 FIELD_NAME (*fp) = fp->type ()->name ();
15188 }
15189 else
15190 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15191 }
15192
15193 /* Can the type given by DIE define another type? */
15194
15195 static bool
15196 type_can_define_types (const struct die_info *die)
15197 {
15198 switch (die->tag)
15199 {
15200 case DW_TAG_typedef:
15201 case DW_TAG_class_type:
15202 case DW_TAG_structure_type:
15203 case DW_TAG_union_type:
15204 case DW_TAG_enumeration_type:
15205 return true;
15206
15207 default:
15208 return false;
15209 }
15210 }
15211
15212 /* Add a type definition defined in the scope of the FIP's class. */
15213
15214 static void
15215 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15216 struct dwarf2_cu *cu)
15217 {
15218 struct decl_field fp;
15219 memset (&fp, 0, sizeof (fp));
15220
15221 gdb_assert (type_can_define_types (die));
15222
15223 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15224 fp.name = dwarf2_name (die, cu);
15225 fp.type = read_type_die (die, cu);
15226
15227 /* Save accessibility. */
15228 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15229 switch (accessibility)
15230 {
15231 case DW_ACCESS_public:
15232 /* The assumed value if neither private nor protected. */
15233 break;
15234 case DW_ACCESS_private:
15235 fp.is_private = 1;
15236 break;
15237 case DW_ACCESS_protected:
15238 fp.is_protected = 1;
15239 break;
15240 }
15241
15242 if (die->tag == DW_TAG_typedef)
15243 fip->typedef_field_list.push_back (fp);
15244 else
15245 fip->nested_types_list.push_back (fp);
15246 }
15247
15248 /* A convenience typedef that's used when finding the discriminant
15249 field for a variant part. */
15250 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
15251 offset_map_type;
15252
15253 /* Compute the discriminant range for a given variant. OBSTACK is
15254 where the results will be stored. VARIANT is the variant to
15255 process. IS_UNSIGNED indicates whether the discriminant is signed
15256 or unsigned. */
15257
15258 static const gdb::array_view<discriminant_range>
15259 convert_variant_range (struct obstack *obstack, const variant_field &variant,
15260 bool is_unsigned)
15261 {
15262 std::vector<discriminant_range> ranges;
15263
15264 if (variant.default_branch)
15265 return {};
15266
15267 if (variant.discr_list_data == nullptr)
15268 {
15269 discriminant_range r
15270 = {variant.discriminant_value, variant.discriminant_value};
15271 ranges.push_back (r);
15272 }
15273 else
15274 {
15275 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15276 variant.discr_list_data->size);
15277 while (!data.empty ())
15278 {
15279 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15280 {
15281 complaint (_("invalid discriminant marker: %d"), data[0]);
15282 break;
15283 }
15284 bool is_range = data[0] == DW_DSC_range;
15285 data = data.slice (1);
15286
15287 ULONGEST low, high;
15288 unsigned int bytes_read;
15289
15290 if (data.empty ())
15291 {
15292 complaint (_("DW_AT_discr_list missing low value"));
15293 break;
15294 }
15295 if (is_unsigned)
15296 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15297 else
15298 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15299 &bytes_read);
15300 data = data.slice (bytes_read);
15301
15302 if (is_range)
15303 {
15304 if (data.empty ())
15305 {
15306 complaint (_("DW_AT_discr_list missing high value"));
15307 break;
15308 }
15309 if (is_unsigned)
15310 high = read_unsigned_leb128 (nullptr, data.data (),
15311 &bytes_read);
15312 else
15313 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15314 &bytes_read);
15315 data = data.slice (bytes_read);
15316 }
15317 else
15318 high = low;
15319
15320 ranges.push_back ({ low, high });
15321 }
15322 }
15323
15324 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15325 ranges.size ());
15326 std::copy (ranges.begin (), ranges.end (), result);
15327 return gdb::array_view<discriminant_range> (result, ranges.size ());
15328 }
15329
15330 static const gdb::array_view<variant_part> create_variant_parts
15331 (struct obstack *obstack,
15332 const offset_map_type &offset_map,
15333 struct field_info *fi,
15334 const std::vector<variant_part_builder> &variant_parts);
15335
15336 /* Fill in a "struct variant" for a given variant field. RESULT is
15337 the variant to fill in. OBSTACK is where any needed allocations
15338 will be done. OFFSET_MAP holds the mapping from section offsets to
15339 fields for the type. FI describes the fields of the type we're
15340 processing. FIELD is the variant field we're converting. */
15341
15342 static void
15343 create_one_variant (variant &result, struct obstack *obstack,
15344 const offset_map_type &offset_map,
15345 struct field_info *fi, const variant_field &field)
15346 {
15347 result.discriminants = convert_variant_range (obstack, field, false);
15348 result.first_field = field.first_field + fi->baseclasses.size ();
15349 result.last_field = field.last_field + fi->baseclasses.size ();
15350 result.parts = create_variant_parts (obstack, offset_map, fi,
15351 field.variant_parts);
15352 }
15353
15354 /* Fill in a "struct variant_part" for a given variant part. RESULT
15355 is the variant part to fill in. OBSTACK is where any needed
15356 allocations will be done. OFFSET_MAP holds the mapping from
15357 section offsets to fields for the type. FI describes the fields of
15358 the type we're processing. BUILDER is the variant part to be
15359 converted. */
15360
15361 static void
15362 create_one_variant_part (variant_part &result,
15363 struct obstack *obstack,
15364 const offset_map_type &offset_map,
15365 struct field_info *fi,
15366 const variant_part_builder &builder)
15367 {
15368 auto iter = offset_map.find (builder.discriminant_offset);
15369 if (iter == offset_map.end ())
15370 {
15371 result.discriminant_index = -1;
15372 /* Doesn't matter. */
15373 result.is_unsigned = false;
15374 }
15375 else
15376 {
15377 result.discriminant_index = iter->second;
15378 result.is_unsigned
15379 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
15380 }
15381
15382 size_t n = builder.variants.size ();
15383 variant *output = new (obstack) variant[n];
15384 for (size_t i = 0; i < n; ++i)
15385 create_one_variant (output[i], obstack, offset_map, fi,
15386 builder.variants[i]);
15387
15388 result.variants = gdb::array_view<variant> (output, n);
15389 }
15390
15391 /* Create a vector of variant parts that can be attached to a type.
15392 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15393 holds the mapping from section offsets to fields for the type. FI
15394 describes the fields of the type we're processing. VARIANT_PARTS
15395 is the vector to convert. */
15396
15397 static const gdb::array_view<variant_part>
15398 create_variant_parts (struct obstack *obstack,
15399 const offset_map_type &offset_map,
15400 struct field_info *fi,
15401 const std::vector<variant_part_builder> &variant_parts)
15402 {
15403 if (variant_parts.empty ())
15404 return {};
15405
15406 size_t n = variant_parts.size ();
15407 variant_part *result = new (obstack) variant_part[n];
15408 for (size_t i = 0; i < n; ++i)
15409 create_one_variant_part (result[i], obstack, offset_map, fi,
15410 variant_parts[i]);
15411
15412 return gdb::array_view<variant_part> (result, n);
15413 }
15414
15415 /* Compute the variant part vector for FIP, attaching it to TYPE when
15416 done. */
15417
15418 static void
15419 add_variant_property (struct field_info *fip, struct type *type,
15420 struct dwarf2_cu *cu)
15421 {
15422 /* Map section offsets of fields to their field index. Note the
15423 field index here does not take the number of baseclasses into
15424 account. */
15425 offset_map_type offset_map;
15426 for (int i = 0; i < fip->fields.size (); ++i)
15427 offset_map[fip->fields[i].offset] = i;
15428
15429 struct objfile *objfile = cu->per_objfile->objfile;
15430 gdb::array_view<variant_part> parts
15431 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15432 fip->variant_parts);
15433
15434 struct dynamic_prop prop;
15435 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15436 obstack_copy (&objfile->objfile_obstack, &parts,
15437 sizeof (parts)));
15438
15439 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
15440 }
15441
15442 /* Create the vector of fields, and attach it to the type. */
15443
15444 static void
15445 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15446 struct dwarf2_cu *cu)
15447 {
15448 int nfields = fip->nfields ();
15449
15450 /* Record the field count, allocate space for the array of fields,
15451 and create blank accessibility bitfields if necessary. */
15452 type->set_num_fields (nfields);
15453 type->set_fields
15454 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
15455
15456 if (fip->non_public_fields && cu->language != language_ada)
15457 {
15458 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15459
15460 TYPE_FIELD_PRIVATE_BITS (type) =
15461 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15462 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15463
15464 TYPE_FIELD_PROTECTED_BITS (type) =
15465 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15466 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15467
15468 TYPE_FIELD_IGNORE_BITS (type) =
15469 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15470 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15471 }
15472
15473 /* If the type has baseclasses, allocate and clear a bit vector for
15474 TYPE_FIELD_VIRTUAL_BITS. */
15475 if (!fip->baseclasses.empty () && cu->language != language_ada)
15476 {
15477 int num_bytes = B_BYTES (fip->baseclasses.size ());
15478 unsigned char *pointer;
15479
15480 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15481 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15482 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15483 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15484 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15485 }
15486
15487 if (!fip->variant_parts.empty ())
15488 add_variant_property (fip, type, cu);
15489
15490 /* Copy the saved-up fields into the field vector. */
15491 for (int i = 0; i < nfields; ++i)
15492 {
15493 struct nextfield &field
15494 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15495 : fip->fields[i - fip->baseclasses.size ()]);
15496
15497 type->field (i) = field.field;
15498 switch (field.accessibility)
15499 {
15500 case DW_ACCESS_private:
15501 if (cu->language != language_ada)
15502 SET_TYPE_FIELD_PRIVATE (type, i);
15503 break;
15504
15505 case DW_ACCESS_protected:
15506 if (cu->language != language_ada)
15507 SET_TYPE_FIELD_PROTECTED (type, i);
15508 break;
15509
15510 case DW_ACCESS_public:
15511 break;
15512
15513 default:
15514 /* Unknown accessibility. Complain and treat it as public. */
15515 {
15516 complaint (_("unsupported accessibility %d"),
15517 field.accessibility);
15518 }
15519 break;
15520 }
15521 if (i < fip->baseclasses.size ())
15522 {
15523 switch (field.virtuality)
15524 {
15525 case DW_VIRTUALITY_virtual:
15526 case DW_VIRTUALITY_pure_virtual:
15527 if (cu->language == language_ada)
15528 error (_("unexpected virtuality in component of Ada type"));
15529 SET_TYPE_FIELD_VIRTUAL (type, i);
15530 break;
15531 }
15532 }
15533 }
15534 }
15535
15536 /* Return true if this member function is a constructor, false
15537 otherwise. */
15538
15539 static int
15540 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15541 {
15542 const char *fieldname;
15543 const char *type_name;
15544 int len;
15545
15546 if (die->parent == NULL)
15547 return 0;
15548
15549 if (die->parent->tag != DW_TAG_structure_type
15550 && die->parent->tag != DW_TAG_union_type
15551 && die->parent->tag != DW_TAG_class_type)
15552 return 0;
15553
15554 fieldname = dwarf2_name (die, cu);
15555 type_name = dwarf2_name (die->parent, cu);
15556 if (fieldname == NULL || type_name == NULL)
15557 return 0;
15558
15559 len = strlen (fieldname);
15560 return (strncmp (fieldname, type_name, len) == 0
15561 && (type_name[len] == '\0' || type_name[len] == '<'));
15562 }
15563
15564 /* Add a member function to the proper fieldlist. */
15565
15566 static void
15567 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15568 struct type *type, struct dwarf2_cu *cu)
15569 {
15570 struct objfile *objfile = cu->per_objfile->objfile;
15571 struct attribute *attr;
15572 int i;
15573 struct fnfieldlist *flp = nullptr;
15574 struct fn_field *fnp;
15575 const char *fieldname;
15576 struct type *this_type;
15577
15578 if (cu->language == language_ada)
15579 error (_("unexpected member function in Ada type"));
15580
15581 /* Get name of member function. */
15582 fieldname = dwarf2_name (die, cu);
15583 if (fieldname == NULL)
15584 return;
15585
15586 /* Look up member function name in fieldlist. */
15587 for (i = 0; i < fip->fnfieldlists.size (); i++)
15588 {
15589 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15590 {
15591 flp = &fip->fnfieldlists[i];
15592 break;
15593 }
15594 }
15595
15596 /* Create a new fnfieldlist if necessary. */
15597 if (flp == nullptr)
15598 {
15599 fip->fnfieldlists.emplace_back ();
15600 flp = &fip->fnfieldlists.back ();
15601 flp->name = fieldname;
15602 i = fip->fnfieldlists.size () - 1;
15603 }
15604
15605 /* Create a new member function field and add it to the vector of
15606 fnfieldlists. */
15607 flp->fnfields.emplace_back ();
15608 fnp = &flp->fnfields.back ();
15609
15610 /* Delay processing of the physname until later. */
15611 if (cu->language == language_cplus)
15612 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15613 die, cu);
15614 else
15615 {
15616 const char *physname = dwarf2_physname (fieldname, die, cu);
15617 fnp->physname = physname ? physname : "";
15618 }
15619
15620 fnp->type = alloc_type (objfile);
15621 this_type = read_type_die (die, cu);
15622 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15623 {
15624 int nparams = this_type->num_fields ();
15625
15626 /* TYPE is the domain of this method, and THIS_TYPE is the type
15627 of the method itself (TYPE_CODE_METHOD). */
15628 smash_to_method_type (fnp->type, type,
15629 TYPE_TARGET_TYPE (this_type),
15630 this_type->fields (),
15631 this_type->num_fields (),
15632 this_type->has_varargs ());
15633
15634 /* Handle static member functions.
15635 Dwarf2 has no clean way to discern C++ static and non-static
15636 member functions. G++ helps GDB by marking the first
15637 parameter for non-static member functions (which is the this
15638 pointer) as artificial. We obtain this information from
15639 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15640 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15641 fnp->voffset = VOFFSET_STATIC;
15642 }
15643 else
15644 complaint (_("member function type missing for '%s'"),
15645 dwarf2_full_name (fieldname, die, cu));
15646
15647 /* Get fcontext from DW_AT_containing_type if present. */
15648 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15649 fnp->fcontext = die_containing_type (die, cu);
15650
15651 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15652 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15653
15654 /* Get accessibility. */
15655 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15656 switch (accessibility)
15657 {
15658 case DW_ACCESS_private:
15659 fnp->is_private = 1;
15660 break;
15661 case DW_ACCESS_protected:
15662 fnp->is_protected = 1;
15663 break;
15664 }
15665
15666 /* Check for artificial methods. */
15667 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15668 if (attr && attr->as_boolean ())
15669 fnp->is_artificial = 1;
15670
15671 /* Check for defaulted methods. */
15672 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15673 if (attr != nullptr)
15674 fnp->defaulted = attr->defaulted ();
15675
15676 /* Check for deleted methods. */
15677 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15678 if (attr != nullptr && attr->as_boolean ())
15679 fnp->is_deleted = 1;
15680
15681 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15682
15683 /* Get index in virtual function table if it is a virtual member
15684 function. For older versions of GCC, this is an offset in the
15685 appropriate virtual table, as specified by DW_AT_containing_type.
15686 For everyone else, it is an expression to be evaluated relative
15687 to the object address. */
15688
15689 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15690 if (attr != nullptr)
15691 {
15692 if (attr->form_is_block () && attr->as_block ()->size > 0)
15693 {
15694 struct dwarf_block *block = attr->as_block ();
15695
15696 if (block->data[0] == DW_OP_constu)
15697 {
15698 /* Old-style GCC. */
15699 fnp->voffset = decode_locdesc (block, cu) + 2;
15700 }
15701 else if (block->data[0] == DW_OP_deref
15702 || (block->size > 1
15703 && block->data[0] == DW_OP_deref_size
15704 && block->data[1] == cu->header.addr_size))
15705 {
15706 fnp->voffset = decode_locdesc (block, cu);
15707 if ((fnp->voffset % cu->header.addr_size) != 0)
15708 dwarf2_complex_location_expr_complaint ();
15709 else
15710 fnp->voffset /= cu->header.addr_size;
15711 fnp->voffset += 2;
15712 }
15713 else
15714 dwarf2_complex_location_expr_complaint ();
15715
15716 if (!fnp->fcontext)
15717 {
15718 /* If there is no `this' field and no DW_AT_containing_type,
15719 we cannot actually find a base class context for the
15720 vtable! */
15721 if (this_type->num_fields () == 0
15722 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15723 {
15724 complaint (_("cannot determine context for virtual member "
15725 "function \"%s\" (offset %s)"),
15726 fieldname, sect_offset_str (die->sect_off));
15727 }
15728 else
15729 {
15730 fnp->fcontext
15731 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15732 }
15733 }
15734 }
15735 else if (attr->form_is_section_offset ())
15736 {
15737 dwarf2_complex_location_expr_complaint ();
15738 }
15739 else
15740 {
15741 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15742 fieldname);
15743 }
15744 }
15745 else
15746 {
15747 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15748 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
15749 {
15750 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15751 complaint (_("Member function \"%s\" (offset %s) is virtual "
15752 "but the vtable offset is not specified"),
15753 fieldname, sect_offset_str (die->sect_off));
15754 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15755 TYPE_CPLUS_DYNAMIC (type) = 1;
15756 }
15757 }
15758 }
15759
15760 /* Create the vector of member function fields, and attach it to the type. */
15761
15762 static void
15763 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15764 struct dwarf2_cu *cu)
15765 {
15766 if (cu->language == language_ada)
15767 error (_("unexpected member functions in Ada type"));
15768
15769 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15770 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15771 TYPE_ALLOC (type,
15772 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15773
15774 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15775 {
15776 struct fnfieldlist &nf = fip->fnfieldlists[i];
15777 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15778
15779 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15780 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15781 fn_flp->fn_fields = (struct fn_field *)
15782 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15783
15784 for (int k = 0; k < nf.fnfields.size (); ++k)
15785 fn_flp->fn_fields[k] = nf.fnfields[k];
15786 }
15787
15788 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15789 }
15790
15791 /* Returns non-zero if NAME is the name of a vtable member in CU's
15792 language, zero otherwise. */
15793 static int
15794 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15795 {
15796 static const char vptr[] = "_vptr";
15797
15798 /* Look for the C++ form of the vtable. */
15799 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15800 return 1;
15801
15802 return 0;
15803 }
15804
15805 /* GCC outputs unnamed structures that are really pointers to member
15806 functions, with the ABI-specified layout. If TYPE describes
15807 such a structure, smash it into a member function type.
15808
15809 GCC shouldn't do this; it should just output pointer to member DIEs.
15810 This is GCC PR debug/28767. */
15811
15812 static void
15813 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15814 {
15815 struct type *pfn_type, *self_type, *new_type;
15816
15817 /* Check for a structure with no name and two children. */
15818 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15819 return;
15820
15821 /* Check for __pfn and __delta members. */
15822 if (TYPE_FIELD_NAME (type, 0) == NULL
15823 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15824 || TYPE_FIELD_NAME (type, 1) == NULL
15825 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15826 return;
15827
15828 /* Find the type of the method. */
15829 pfn_type = type->field (0).type ();
15830 if (pfn_type == NULL
15831 || pfn_type->code () != TYPE_CODE_PTR
15832 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15833 return;
15834
15835 /* Look for the "this" argument. */
15836 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15837 if (pfn_type->num_fields () == 0
15838 /* || pfn_type->field (0).type () == NULL */
15839 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15840 return;
15841
15842 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15843 new_type = alloc_type (objfile);
15844 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15845 pfn_type->fields (), pfn_type->num_fields (),
15846 pfn_type->has_varargs ());
15847 smash_to_methodptr_type (type, new_type);
15848 }
15849
15850 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15851 requires rewriting, then copy it and return the updated copy.
15852 Otherwise return nullptr. */
15853
15854 static struct type *
15855 rewrite_array_type (struct type *type)
15856 {
15857 if (type->code () != TYPE_CODE_ARRAY)
15858 return nullptr;
15859
15860 struct type *index_type = type->index_type ();
15861 range_bounds *current_bounds = index_type->bounds ();
15862
15863 /* Handle multi-dimensional arrays. */
15864 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15865 if (new_target == nullptr)
15866 {
15867 /* Maybe we don't need to rewrite this array. */
15868 if (current_bounds->low.kind () == PROP_CONST
15869 && current_bounds->high.kind () == PROP_CONST)
15870 return nullptr;
15871 }
15872
15873 /* Either the target type was rewritten, or the bounds have to be
15874 updated. Either way we want to copy the type and update
15875 everything. */
15876 struct type *copy = copy_type (type);
15877 int nfields = copy->num_fields ();
15878 field *new_fields
15879 = ((struct field *) TYPE_ZALLOC (copy,
15880 nfields * sizeof (struct field)));
15881 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15882 copy->set_fields (new_fields);
15883 if (new_target != nullptr)
15884 TYPE_TARGET_TYPE (copy) = new_target;
15885
15886 struct type *index_copy = copy_type (index_type);
15887 range_bounds *bounds
15888 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15889 sizeof (range_bounds));
15890 *bounds = *current_bounds;
15891 bounds->low.set_const_val (1);
15892 bounds->high.set_const_val (0);
15893 index_copy->set_bounds (bounds);
15894 copy->set_index_type (index_copy);
15895
15896 return copy;
15897 }
15898
15899 /* While some versions of GCC will generate complicated DWARF for an
15900 array (see quirk_ada_thick_pointer), more recent versions were
15901 modified to emit an explicit thick pointer structure. However, in
15902 this case, the array still has DWARF expressions for its ranges,
15903 and these must be ignored. */
15904
15905 static void
15906 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15907 struct type *type)
15908 {
15909 gdb_assert (cu->language == language_ada);
15910
15911 /* Check for a structure with two children. */
15912 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15913 return;
15914
15915 /* Check for P_ARRAY and P_BOUNDS members. */
15916 if (TYPE_FIELD_NAME (type, 0) == NULL
15917 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15918 || TYPE_FIELD_NAME (type, 1) == NULL
15919 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15920 return;
15921
15922 /* Make sure we're looking at a pointer to an array. */
15923 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15924 return;
15925
15926 /* The Ada code already knows how to handle these types, so all that
15927 we need to do is turn the bounds into static bounds. However, we
15928 don't want to rewrite existing array or index types in-place,
15929 because those may be referenced in other contexts where this
15930 rewriting is undesirable. */
15931 struct type *new_ary_type
15932 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15933 if (new_ary_type != nullptr)
15934 type->field (0).set_type (lookup_pointer_type (new_ary_type));
15935 }
15936
15937 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15938 appropriate error checking and issuing complaints if there is a
15939 problem. */
15940
15941 static ULONGEST
15942 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15943 {
15944 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15945
15946 if (attr == nullptr)
15947 return 0;
15948
15949 if (!attr->form_is_constant ())
15950 {
15951 complaint (_("DW_AT_alignment must have constant form"
15952 " - DIE at %s [in module %s]"),
15953 sect_offset_str (die->sect_off),
15954 objfile_name (cu->per_objfile->objfile));
15955 return 0;
15956 }
15957
15958 LONGEST val = attr->constant_value (0);
15959 if (val < 0)
15960 {
15961 complaint (_("DW_AT_alignment value must not be negative"
15962 " - DIE at %s [in module %s]"),
15963 sect_offset_str (die->sect_off),
15964 objfile_name (cu->per_objfile->objfile));
15965 return 0;
15966 }
15967 ULONGEST align = val;
15968
15969 if (align == 0)
15970 {
15971 complaint (_("DW_AT_alignment value must not be zero"
15972 " - DIE at %s [in module %s]"),
15973 sect_offset_str (die->sect_off),
15974 objfile_name (cu->per_objfile->objfile));
15975 return 0;
15976 }
15977 if ((align & (align - 1)) != 0)
15978 {
15979 complaint (_("DW_AT_alignment value must be a power of 2"
15980 " - DIE at %s [in module %s]"),
15981 sect_offset_str (die->sect_off),
15982 objfile_name (cu->per_objfile->objfile));
15983 return 0;
15984 }
15985
15986 return align;
15987 }
15988
15989 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15990 the alignment for TYPE. */
15991
15992 static void
15993 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15994 struct type *type)
15995 {
15996 if (!set_type_align (type, get_alignment (cu, die)))
15997 complaint (_("DW_AT_alignment value too large"
15998 " - DIE at %s [in module %s]"),
15999 sect_offset_str (die->sect_off),
16000 objfile_name (cu->per_objfile->objfile));
16001 }
16002
16003 /* Check if the given VALUE is a valid enum dwarf_calling_convention
16004 constant for a type, according to DWARF5 spec, Table 5.5. */
16005
16006 static bool
16007 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
16008 {
16009 switch (value)
16010 {
16011 case DW_CC_normal:
16012 case DW_CC_pass_by_reference:
16013 case DW_CC_pass_by_value:
16014 return true;
16015
16016 default:
16017 complaint (_("unrecognized DW_AT_calling_convention value "
16018 "(%s) for a type"), pulongest (value));
16019 return false;
16020 }
16021 }
16022
16023 /* Check if the given VALUE is a valid enum dwarf_calling_convention
16024 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
16025 also according to GNU-specific values (see include/dwarf2.h). */
16026
16027 static bool
16028 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
16029 {
16030 switch (value)
16031 {
16032 case DW_CC_normal:
16033 case DW_CC_program:
16034 case DW_CC_nocall:
16035 return true;
16036
16037 case DW_CC_GNU_renesas_sh:
16038 case DW_CC_GNU_borland_fastcall_i386:
16039 case DW_CC_GDB_IBM_OpenCL:
16040 return true;
16041
16042 default:
16043 complaint (_("unrecognized DW_AT_calling_convention value "
16044 "(%s) for a subroutine"), pulongest (value));
16045 return false;
16046 }
16047 }
16048
16049 /* Called when we find the DIE that starts a structure or union scope
16050 (definition) to create a type for the structure or union. Fill in
16051 the type's name and general properties; the members will not be
16052 processed until process_structure_scope. A symbol table entry for
16053 the type will also not be done until process_structure_scope (assuming
16054 the type has a name).
16055
16056 NOTE: we need to call these functions regardless of whether or not the
16057 DIE has a DW_AT_name attribute, since it might be an anonymous
16058 structure or union. This gets the type entered into our set of
16059 user defined types. */
16060
16061 static struct type *
16062 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
16063 {
16064 struct objfile *objfile = cu->per_objfile->objfile;
16065 struct type *type;
16066 struct attribute *attr;
16067 const char *name;
16068
16069 /* If the definition of this type lives in .debug_types, read that type.
16070 Don't follow DW_AT_specification though, that will take us back up
16071 the chain and we want to go down. */
16072 attr = die->attr (DW_AT_signature);
16073 if (attr != nullptr)
16074 {
16075 type = get_DW_AT_signature_type (die, attr, cu);
16076
16077 /* The type's CU may not be the same as CU.
16078 Ensure TYPE is recorded with CU in die_type_hash. */
16079 return set_die_type (die, type, cu);
16080 }
16081
16082 type = alloc_type (objfile);
16083 INIT_CPLUS_SPECIFIC (type);
16084
16085 name = dwarf2_name (die, cu);
16086 if (name != NULL)
16087 {
16088 if (cu->language == language_cplus
16089 || cu->language == language_d
16090 || cu->language == language_rust)
16091 {
16092 const char *full_name = dwarf2_full_name (name, die, cu);
16093
16094 /* dwarf2_full_name might have already finished building the DIE's
16095 type. If so, there is no need to continue. */
16096 if (get_die_type (die, cu) != NULL)
16097 return get_die_type (die, cu);
16098
16099 type->set_name (full_name);
16100 }
16101 else
16102 {
16103 /* The name is already allocated along with this objfile, so
16104 we don't need to duplicate it for the type. */
16105 type->set_name (name);
16106 }
16107 }
16108
16109 if (die->tag == DW_TAG_structure_type)
16110 {
16111 type->set_code (TYPE_CODE_STRUCT);
16112 }
16113 else if (die->tag == DW_TAG_union_type)
16114 {
16115 type->set_code (TYPE_CODE_UNION);
16116 }
16117 else
16118 {
16119 type->set_code (TYPE_CODE_STRUCT);
16120 }
16121
16122 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16123 TYPE_DECLARED_CLASS (type) = 1;
16124
16125 /* Store the calling convention in the type if it's available in
16126 the die. Otherwise the calling convention remains set to
16127 the default value DW_CC_normal. */
16128 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16129 if (attr != nullptr
16130 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
16131 {
16132 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16133 TYPE_CPLUS_CALLING_CONVENTION (type)
16134 = (enum dwarf_calling_convention) (attr->constant_value (0));
16135 }
16136
16137 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16138 if (attr != nullptr)
16139 {
16140 if (attr->form_is_constant ())
16141 TYPE_LENGTH (type) = attr->constant_value (0);
16142 else
16143 {
16144 struct dynamic_prop prop;
16145 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
16146 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
16147 TYPE_LENGTH (type) = 0;
16148 }
16149 }
16150 else
16151 {
16152 TYPE_LENGTH (type) = 0;
16153 }
16154
16155 maybe_set_alignment (cu, die, type);
16156
16157 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
16158 {
16159 /* ICC<14 does not output the required DW_AT_declaration on
16160 incomplete types, but gives them a size of zero. */
16161 type->set_is_stub (true);
16162 }
16163 else
16164 type->set_stub_is_supported (true);
16165
16166 if (die_is_declaration (die, cu))
16167 type->set_is_stub (true);
16168 else if (attr == NULL && die->child == NULL
16169 && producer_is_realview (cu->producer))
16170 /* RealView does not output the required DW_AT_declaration
16171 on incomplete types. */
16172 type->set_is_stub (true);
16173
16174 /* We need to add the type field to the die immediately so we don't
16175 infinitely recurse when dealing with pointers to the structure
16176 type within the structure itself. */
16177 set_die_type (die, type, cu);
16178
16179 /* set_die_type should be already done. */
16180 set_descriptive_type (type, die, cu);
16181
16182 return type;
16183 }
16184
16185 static void handle_struct_member_die
16186 (struct die_info *child_die,
16187 struct type *type,
16188 struct field_info *fi,
16189 std::vector<struct symbol *> *template_args,
16190 struct dwarf2_cu *cu);
16191
16192 /* A helper for handle_struct_member_die that handles
16193 DW_TAG_variant_part. */
16194
16195 static void
16196 handle_variant_part (struct die_info *die, struct type *type,
16197 struct field_info *fi,
16198 std::vector<struct symbol *> *template_args,
16199 struct dwarf2_cu *cu)
16200 {
16201 variant_part_builder *new_part;
16202 if (fi->current_variant_part == nullptr)
16203 {
16204 fi->variant_parts.emplace_back ();
16205 new_part = &fi->variant_parts.back ();
16206 }
16207 else if (!fi->current_variant_part->processing_variant)
16208 {
16209 complaint (_("nested DW_TAG_variant_part seen "
16210 "- DIE at %s [in module %s]"),
16211 sect_offset_str (die->sect_off),
16212 objfile_name (cu->per_objfile->objfile));
16213 return;
16214 }
16215 else
16216 {
16217 variant_field &current = fi->current_variant_part->variants.back ();
16218 current.variant_parts.emplace_back ();
16219 new_part = &current.variant_parts.back ();
16220 }
16221
16222 /* When we recurse, we want callees to add to this new variant
16223 part. */
16224 scoped_restore save_current_variant_part
16225 = make_scoped_restore (&fi->current_variant_part, new_part);
16226
16227 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16228 if (discr == NULL)
16229 {
16230 /* It's a univariant form, an extension we support. */
16231 }
16232 else if (discr->form_is_ref ())
16233 {
16234 struct dwarf2_cu *target_cu = cu;
16235 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16236
16237 new_part->discriminant_offset = target_die->sect_off;
16238 }
16239 else
16240 {
16241 complaint (_("DW_AT_discr does not have DIE reference form"
16242 " - DIE at %s [in module %s]"),
16243 sect_offset_str (die->sect_off),
16244 objfile_name (cu->per_objfile->objfile));
16245 }
16246
16247 for (die_info *child_die = die->child;
16248 child_die != NULL;
16249 child_die = child_die->sibling)
16250 handle_struct_member_die (child_die, type, fi, template_args, cu);
16251 }
16252
16253 /* A helper for handle_struct_member_die that handles
16254 DW_TAG_variant. */
16255
16256 static void
16257 handle_variant (struct die_info *die, struct type *type,
16258 struct field_info *fi,
16259 std::vector<struct symbol *> *template_args,
16260 struct dwarf2_cu *cu)
16261 {
16262 if (fi->current_variant_part == nullptr)
16263 {
16264 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
16265 "- DIE at %s [in module %s]"),
16266 sect_offset_str (die->sect_off),
16267 objfile_name (cu->per_objfile->objfile));
16268 return;
16269 }
16270 if (fi->current_variant_part->processing_variant)
16271 {
16272 complaint (_("nested DW_TAG_variant seen "
16273 "- DIE at %s [in module %s]"),
16274 sect_offset_str (die->sect_off),
16275 objfile_name (cu->per_objfile->objfile));
16276 return;
16277 }
16278
16279 scoped_restore save_processing_variant
16280 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16281 true);
16282
16283 fi->current_variant_part->variants.emplace_back ();
16284 variant_field &variant = fi->current_variant_part->variants.back ();
16285 variant.first_field = fi->fields.size ();
16286
16287 /* In a variant we want to get the discriminant and also add a
16288 field for our sole member child. */
16289 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
16290 if (discr == nullptr || !discr->form_is_constant ())
16291 {
16292 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
16293 if (discr == nullptr || discr->as_block ()->size == 0)
16294 variant.default_branch = true;
16295 else
16296 variant.discr_list_data = discr->as_block ();
16297 }
16298 else
16299 variant.discriminant_value = discr->constant_value (0);
16300
16301 for (die_info *variant_child = die->child;
16302 variant_child != NULL;
16303 variant_child = variant_child->sibling)
16304 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16305
16306 variant.last_field = fi->fields.size ();
16307 }
16308
16309 /* A helper for process_structure_scope that handles a single member
16310 DIE. */
16311
16312 static void
16313 handle_struct_member_die (struct die_info *child_die, struct type *type,
16314 struct field_info *fi,
16315 std::vector<struct symbol *> *template_args,
16316 struct dwarf2_cu *cu)
16317 {
16318 if (child_die->tag == DW_TAG_member
16319 || child_die->tag == DW_TAG_variable)
16320 {
16321 /* NOTE: carlton/2002-11-05: A C++ static data member
16322 should be a DW_TAG_member that is a declaration, but
16323 all versions of G++ as of this writing (so through at
16324 least 3.2.1) incorrectly generate DW_TAG_variable
16325 tags for them instead. */
16326 dwarf2_add_field (fi, child_die, cu);
16327 }
16328 else if (child_die->tag == DW_TAG_subprogram)
16329 {
16330 /* Rust doesn't have member functions in the C++ sense.
16331 However, it does emit ordinary functions as children
16332 of a struct DIE. */
16333 if (cu->language == language_rust)
16334 read_func_scope (child_die, cu);
16335 else
16336 {
16337 /* C++ member function. */
16338 dwarf2_add_member_fn (fi, child_die, type, cu);
16339 }
16340 }
16341 else if (child_die->tag == DW_TAG_inheritance)
16342 {
16343 /* C++ base class field. */
16344 dwarf2_add_field (fi, child_die, cu);
16345 }
16346 else if (type_can_define_types (child_die))
16347 dwarf2_add_type_defn (fi, child_die, cu);
16348 else if (child_die->tag == DW_TAG_template_type_param
16349 || child_die->tag == DW_TAG_template_value_param)
16350 {
16351 struct symbol *arg = new_symbol (child_die, NULL, cu);
16352
16353 if (arg != NULL)
16354 template_args->push_back (arg);
16355 }
16356 else if (child_die->tag == DW_TAG_variant_part)
16357 handle_variant_part (child_die, type, fi, template_args, cu);
16358 else if (child_die->tag == DW_TAG_variant)
16359 handle_variant (child_die, type, fi, template_args, cu);
16360 }
16361
16362 /* Finish creating a structure or union type, including filling in
16363 its members and creating a symbol for it. */
16364
16365 static void
16366 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16367 {
16368 struct objfile *objfile = cu->per_objfile->objfile;
16369 struct die_info *child_die;
16370 struct type *type;
16371
16372 type = get_die_type (die, cu);
16373 if (type == NULL)
16374 type = read_structure_type (die, cu);
16375
16376 bool has_template_parameters = false;
16377 if (die->child != NULL && ! die_is_declaration (die, cu))
16378 {
16379 struct field_info fi;
16380 std::vector<struct symbol *> template_args;
16381
16382 child_die = die->child;
16383
16384 while (child_die && child_die->tag)
16385 {
16386 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16387 child_die = child_die->sibling;
16388 }
16389
16390 /* Attach template arguments to type. */
16391 if (!template_args.empty ())
16392 {
16393 has_template_parameters = true;
16394 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16395 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16396 TYPE_TEMPLATE_ARGUMENTS (type)
16397 = XOBNEWVEC (&objfile->objfile_obstack,
16398 struct symbol *,
16399 TYPE_N_TEMPLATE_ARGUMENTS (type));
16400 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16401 template_args.data (),
16402 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16403 * sizeof (struct symbol *)));
16404 }
16405
16406 /* Attach fields and member functions to the type. */
16407 if (fi.nfields () > 0)
16408 dwarf2_attach_fields_to_type (&fi, type, cu);
16409 if (!fi.fnfieldlists.empty ())
16410 {
16411 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16412
16413 /* Get the type which refers to the base class (possibly this
16414 class itself) which contains the vtable pointer for the current
16415 class from the DW_AT_containing_type attribute. This use of
16416 DW_AT_containing_type is a GNU extension. */
16417
16418 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16419 {
16420 struct type *t = die_containing_type (die, cu);
16421
16422 set_type_vptr_basetype (type, t);
16423 if (type == t)
16424 {
16425 int i;
16426
16427 /* Our own class provides vtbl ptr. */
16428 for (i = t->num_fields () - 1;
16429 i >= TYPE_N_BASECLASSES (t);
16430 --i)
16431 {
16432 const char *fieldname = TYPE_FIELD_NAME (t, i);
16433
16434 if (is_vtable_name (fieldname, cu))
16435 {
16436 set_type_vptr_fieldno (type, i);
16437 break;
16438 }
16439 }
16440
16441 /* Complain if virtual function table field not found. */
16442 if (i < TYPE_N_BASECLASSES (t))
16443 complaint (_("virtual function table pointer "
16444 "not found when defining class '%s'"),
16445 type->name () ? type->name () : "");
16446 }
16447 else
16448 {
16449 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16450 }
16451 }
16452 else if (cu->producer
16453 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16454 {
16455 /* The IBM XLC compiler does not provide direct indication
16456 of the containing type, but the vtable pointer is
16457 always named __vfp. */
16458
16459 int i;
16460
16461 for (i = type->num_fields () - 1;
16462 i >= TYPE_N_BASECLASSES (type);
16463 --i)
16464 {
16465 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16466 {
16467 set_type_vptr_fieldno (type, i);
16468 set_type_vptr_basetype (type, type);
16469 break;
16470 }
16471 }
16472 }
16473 }
16474
16475 /* Copy fi.typedef_field_list linked list elements content into the
16476 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16477 if (!fi.typedef_field_list.empty ())
16478 {
16479 int count = fi.typedef_field_list.size ();
16480
16481 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16482 TYPE_TYPEDEF_FIELD_ARRAY (type)
16483 = ((struct decl_field *)
16484 TYPE_ALLOC (type,
16485 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16486 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16487
16488 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16489 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16490 }
16491
16492 /* Copy fi.nested_types_list linked list elements content into the
16493 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16494 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16495 {
16496 int count = fi.nested_types_list.size ();
16497
16498 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16499 TYPE_NESTED_TYPES_ARRAY (type)
16500 = ((struct decl_field *)
16501 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16502 TYPE_NESTED_TYPES_COUNT (type) = count;
16503
16504 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16505 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16506 }
16507 }
16508
16509 quirk_gcc_member_function_pointer (type, objfile);
16510 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16511 cu->rust_unions.push_back (type);
16512 else if (cu->language == language_ada)
16513 quirk_ada_thick_pointer_struct (die, cu, type);
16514
16515 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16516 snapshots) has been known to create a die giving a declaration
16517 for a class that has, as a child, a die giving a definition for a
16518 nested class. So we have to process our children even if the
16519 current die is a declaration. Normally, of course, a declaration
16520 won't have any children at all. */
16521
16522 child_die = die->child;
16523
16524 while (child_die != NULL && child_die->tag)
16525 {
16526 if (child_die->tag == DW_TAG_member
16527 || child_die->tag == DW_TAG_variable
16528 || child_die->tag == DW_TAG_inheritance
16529 || child_die->tag == DW_TAG_template_value_param
16530 || child_die->tag == DW_TAG_template_type_param)
16531 {
16532 /* Do nothing. */
16533 }
16534 else
16535 process_die (child_die, cu);
16536
16537 child_die = child_die->sibling;
16538 }
16539
16540 /* Do not consider external references. According to the DWARF standard,
16541 these DIEs are identified by the fact that they have no byte_size
16542 attribute, and a declaration attribute. */
16543 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16544 || !die_is_declaration (die, cu)
16545 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16546 {
16547 struct symbol *sym = new_symbol (die, type, cu);
16548
16549 if (has_template_parameters)
16550 {
16551 struct symtab *symtab;
16552 if (sym != nullptr)
16553 symtab = symbol_symtab (sym);
16554 else if (cu->line_header != nullptr)
16555 {
16556 /* Any related symtab will do. */
16557 symtab
16558 = cu->line_header->file_names ()[0].symtab;
16559 }
16560 else
16561 {
16562 symtab = nullptr;
16563 complaint (_("could not find suitable "
16564 "symtab for template parameter"
16565 " - DIE at %s [in module %s]"),
16566 sect_offset_str (die->sect_off),
16567 objfile_name (objfile));
16568 }
16569
16570 if (symtab != nullptr)
16571 {
16572 /* Make sure that the symtab is set on the new symbols.
16573 Even though they don't appear in this symtab directly,
16574 other parts of gdb assume that symbols do, and this is
16575 reasonably true. */
16576 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16577 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16578 }
16579 }
16580 }
16581 }
16582
16583 /* Assuming DIE is an enumeration type, and TYPE is its associated
16584 type, update TYPE using some information only available in DIE's
16585 children. In particular, the fields are computed. */
16586
16587 static void
16588 update_enumeration_type_from_children (struct die_info *die,
16589 struct type *type,
16590 struct dwarf2_cu *cu)
16591 {
16592 struct die_info *child_die;
16593 int unsigned_enum = 1;
16594 int flag_enum = 1;
16595
16596 auto_obstack obstack;
16597 std::vector<struct field> fields;
16598
16599 for (child_die = die->child;
16600 child_die != NULL && child_die->tag;
16601 child_die = child_die->sibling)
16602 {
16603 struct attribute *attr;
16604 LONGEST value;
16605 const gdb_byte *bytes;
16606 struct dwarf2_locexpr_baton *baton;
16607 const char *name;
16608
16609 if (child_die->tag != DW_TAG_enumerator)
16610 continue;
16611
16612 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16613 if (attr == NULL)
16614 continue;
16615
16616 name = dwarf2_name (child_die, cu);
16617 if (name == NULL)
16618 name = "<anonymous enumerator>";
16619
16620 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16621 &value, &bytes, &baton);
16622 if (value < 0)
16623 {
16624 unsigned_enum = 0;
16625 flag_enum = 0;
16626 }
16627 else
16628 {
16629 if (count_one_bits_ll (value) >= 2)
16630 flag_enum = 0;
16631 }
16632
16633 fields.emplace_back ();
16634 struct field &field = fields.back ();
16635 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16636 SET_FIELD_ENUMVAL (field, value);
16637 }
16638
16639 if (!fields.empty ())
16640 {
16641 type->set_num_fields (fields.size ());
16642 type->set_fields
16643 ((struct field *)
16644 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16645 memcpy (type->fields (), fields.data (),
16646 sizeof (struct field) * fields.size ());
16647 }
16648
16649 if (unsigned_enum)
16650 type->set_is_unsigned (true);
16651
16652 if (flag_enum)
16653 TYPE_FLAG_ENUM (type) = 1;
16654 }
16655
16656 /* Given a DW_AT_enumeration_type die, set its type. We do not
16657 complete the type's fields yet, or create any symbols. */
16658
16659 static struct type *
16660 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16661 {
16662 struct objfile *objfile = cu->per_objfile->objfile;
16663 struct type *type;
16664 struct attribute *attr;
16665 const char *name;
16666
16667 /* If the definition of this type lives in .debug_types, read that type.
16668 Don't follow DW_AT_specification though, that will take us back up
16669 the chain and we want to go down. */
16670 attr = die->attr (DW_AT_signature);
16671 if (attr != nullptr)
16672 {
16673 type = get_DW_AT_signature_type (die, attr, cu);
16674
16675 /* The type's CU may not be the same as CU.
16676 Ensure TYPE is recorded with CU in die_type_hash. */
16677 return set_die_type (die, type, cu);
16678 }
16679
16680 type = alloc_type (objfile);
16681
16682 type->set_code (TYPE_CODE_ENUM);
16683 name = dwarf2_full_name (NULL, die, cu);
16684 if (name != NULL)
16685 type->set_name (name);
16686
16687 attr = dwarf2_attr (die, DW_AT_type, cu);
16688 if (attr != NULL)
16689 {
16690 struct type *underlying_type = die_type (die, cu);
16691
16692 TYPE_TARGET_TYPE (type) = underlying_type;
16693 }
16694
16695 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16696 if (attr != nullptr)
16697 {
16698 TYPE_LENGTH (type) = attr->constant_value (0);
16699 }
16700 else
16701 {
16702 TYPE_LENGTH (type) = 0;
16703 }
16704
16705 maybe_set_alignment (cu, die, type);
16706
16707 /* The enumeration DIE can be incomplete. In Ada, any type can be
16708 declared as private in the package spec, and then defined only
16709 inside the package body. Such types are known as Taft Amendment
16710 Types. When another package uses such a type, an incomplete DIE
16711 may be generated by the compiler. */
16712 if (die_is_declaration (die, cu))
16713 type->set_is_stub (true);
16714
16715 /* If this type has an underlying type that is not a stub, then we
16716 may use its attributes. We always use the "unsigned" attribute
16717 in this situation, because ordinarily we guess whether the type
16718 is unsigned -- but the guess can be wrong and the underlying type
16719 can tell us the reality. However, we defer to a local size
16720 attribute if one exists, because this lets the compiler override
16721 the underlying type if needed. */
16722 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16723 {
16724 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16725 underlying_type = check_typedef (underlying_type);
16726
16727 type->set_is_unsigned (underlying_type->is_unsigned ());
16728
16729 if (TYPE_LENGTH (type) == 0)
16730 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16731
16732 if (TYPE_RAW_ALIGN (type) == 0
16733 && TYPE_RAW_ALIGN (underlying_type) != 0)
16734 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16735 }
16736
16737 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16738
16739 set_die_type (die, type, cu);
16740
16741 /* Finish the creation of this type by using the enum's children.
16742 Note that, as usual, this must come after set_die_type to avoid
16743 infinite recursion when trying to compute the names of the
16744 enumerators. */
16745 update_enumeration_type_from_children (die, type, cu);
16746
16747 return type;
16748 }
16749
16750 /* Given a pointer to a die which begins an enumeration, process all
16751 the dies that define the members of the enumeration, and create the
16752 symbol for the enumeration type.
16753
16754 NOTE: We reverse the order of the element list. */
16755
16756 static void
16757 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16758 {
16759 struct type *this_type;
16760
16761 this_type = get_die_type (die, cu);
16762 if (this_type == NULL)
16763 this_type = read_enumeration_type (die, cu);
16764
16765 if (die->child != NULL)
16766 {
16767 struct die_info *child_die;
16768 const char *name;
16769
16770 child_die = die->child;
16771 while (child_die && child_die->tag)
16772 {
16773 if (child_die->tag != DW_TAG_enumerator)
16774 {
16775 process_die (child_die, cu);
16776 }
16777 else
16778 {
16779 name = dwarf2_name (child_die, cu);
16780 if (name)
16781 new_symbol (child_die, this_type, cu);
16782 }
16783
16784 child_die = child_die->sibling;
16785 }
16786 }
16787
16788 /* If we are reading an enum from a .debug_types unit, and the enum
16789 is a declaration, and the enum is not the signatured type in the
16790 unit, then we do not want to add a symbol for it. Adding a
16791 symbol would in some cases obscure the true definition of the
16792 enum, giving users an incomplete type when the definition is
16793 actually available. Note that we do not want to do this for all
16794 enums which are just declarations, because C++0x allows forward
16795 enum declarations. */
16796 if (cu->per_cu->is_debug_types
16797 && die_is_declaration (die, cu))
16798 {
16799 struct signatured_type *sig_type;
16800
16801 sig_type = (struct signatured_type *) cu->per_cu;
16802 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16803 if (sig_type->type_offset_in_section != die->sect_off)
16804 return;
16805 }
16806
16807 new_symbol (die, this_type, cu);
16808 }
16809
16810 /* Helper function for quirk_ada_thick_pointer that examines a bounds
16811 expression for an index type and finds the corresponding field
16812 offset in the hidden "P_BOUNDS" structure. Returns true on success
16813 and updates *FIELD, false if it fails to recognize an
16814 expression. */
16815
16816 static bool
16817 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16818 int *bounds_offset, struct field *field,
16819 struct dwarf2_cu *cu)
16820 {
16821 struct attribute *attr = dwarf2_attr (die, name, cu);
16822 if (attr == nullptr || !attr->form_is_block ())
16823 return false;
16824
16825 const struct dwarf_block *block = attr->as_block ();
16826 const gdb_byte *start = block->data;
16827 const gdb_byte *end = block->data + block->size;
16828
16829 /* The expression to recognize generally looks like:
16830
16831 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16832 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16833
16834 However, the second "plus_uconst" may be missing:
16835
16836 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16837 DW_OP_deref_size: 4)
16838
16839 This happens when the field is at the start of the structure.
16840
16841 Also, the final deref may not be sized:
16842
16843 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16844 DW_OP_deref)
16845
16846 This happens when the size of the index type happens to be the
16847 same as the architecture's word size. This can occur with or
16848 without the second plus_uconst. */
16849
16850 if (end - start < 2)
16851 return false;
16852 if (*start++ != DW_OP_push_object_address)
16853 return false;
16854 if (*start++ != DW_OP_plus_uconst)
16855 return false;
16856
16857 uint64_t this_bound_off;
16858 start = gdb_read_uleb128 (start, end, &this_bound_off);
16859 if (start == nullptr || (int) this_bound_off != this_bound_off)
16860 return false;
16861 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16862 is consistent among all bounds. */
16863 if (*bounds_offset == -1)
16864 *bounds_offset = this_bound_off;
16865 else if (*bounds_offset != this_bound_off)
16866 return false;
16867
16868 if (start == end || *start++ != DW_OP_deref)
16869 return false;
16870
16871 int offset = 0;
16872 if (start ==end)
16873 return false;
16874 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16875 {
16876 /* This means an offset of 0. */
16877 }
16878 else if (*start++ != DW_OP_plus_uconst)
16879 return false;
16880 else
16881 {
16882 /* The size is the parameter to DW_OP_plus_uconst. */
16883 uint64_t val;
16884 start = gdb_read_uleb128 (start, end, &val);
16885 if (start == nullptr)
16886 return false;
16887 if ((int) val != val)
16888 return false;
16889 offset = val;
16890 }
16891
16892 if (start == end)
16893 return false;
16894
16895 uint64_t size;
16896 if (*start == DW_OP_deref_size)
16897 {
16898 start = gdb_read_uleb128 (start + 1, end, &size);
16899 if (start == nullptr)
16900 return false;
16901 }
16902 else if (*start == DW_OP_deref)
16903 {
16904 size = cu->header.addr_size;
16905 ++start;
16906 }
16907 else
16908 return false;
16909
16910 SET_FIELD_BITPOS (*field, 8 * offset);
16911 if (size != TYPE_LENGTH (field->type ()))
16912 FIELD_BITSIZE (*field) = 8 * size;
16913
16914 return true;
16915 }
16916
16917 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16918 some kinds of Ada arrays:
16919
16920 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16921 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16922 <11e0> DW_AT_data_location: 2 byte block: 97 6
16923 (DW_OP_push_object_address; DW_OP_deref)
16924 <11e3> DW_AT_type : <0x1173>
16925 <11e7> DW_AT_sibling : <0x1201>
16926 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16927 <11ec> DW_AT_type : <0x1206>
16928 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16929 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16930 DW_OP_deref_size: 4)
16931 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16932 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16933 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16934
16935 This actually represents a "thick pointer", which is a structure
16936 with two elements: one that is a pointer to the array data, and one
16937 that is a pointer to another structure; this second structure holds
16938 the array bounds.
16939
16940 This returns a new type on success, or nullptr if this didn't
16941 recognize the type. */
16942
16943 static struct type *
16944 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16945 struct type *type)
16946 {
16947 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16948 /* So far we've only seen this with block form. */
16949 if (attr == nullptr || !attr->form_is_block ())
16950 return nullptr;
16951
16952 /* Note that this will fail if the structure layout is changed by
16953 the compiler. However, we have no good way to recognize some
16954 other layout, because we don't know what expression the compiler
16955 might choose to emit should this happen. */
16956 struct dwarf_block *blk = attr->as_block ();
16957 if (blk->size != 2
16958 || blk->data[0] != DW_OP_push_object_address
16959 || blk->data[1] != DW_OP_deref)
16960 return nullptr;
16961
16962 int bounds_offset = -1;
16963 int max_align = -1;
16964 std::vector<struct field> range_fields;
16965 for (struct die_info *child_die = die->child;
16966 child_die;
16967 child_die = child_die->sibling)
16968 {
16969 if (child_die->tag == DW_TAG_subrange_type)
16970 {
16971 struct type *underlying = read_subrange_index_type (child_die, cu);
16972
16973 int this_align = type_align (underlying);
16974 if (this_align > max_align)
16975 max_align = this_align;
16976
16977 range_fields.emplace_back ();
16978 range_fields.emplace_back ();
16979
16980 struct field &lower = range_fields[range_fields.size () - 2];
16981 struct field &upper = range_fields[range_fields.size () - 1];
16982
16983 lower.set_type (underlying);
16984 FIELD_ARTIFICIAL (lower) = 1;
16985
16986 upper.set_type (underlying);
16987 FIELD_ARTIFICIAL (upper) = 1;
16988
16989 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16990 &bounds_offset, &lower, cu)
16991 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16992 &bounds_offset, &upper, cu))
16993 return nullptr;
16994 }
16995 }
16996
16997 /* This shouldn't really happen, but double-check that we found
16998 where the bounds are stored. */
16999 if (bounds_offset == -1)
17000 return nullptr;
17001
17002 struct objfile *objfile = cu->per_objfile->objfile;
17003 for (int i = 0; i < range_fields.size (); i += 2)
17004 {
17005 char name[20];
17006
17007 /* Set the name of each field in the bounds. */
17008 xsnprintf (name, sizeof (name), "LB%d", i / 2);
17009 FIELD_NAME (range_fields[i]) = objfile->intern (name);
17010 xsnprintf (name, sizeof (name), "UB%d", i / 2);
17011 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
17012 }
17013
17014 struct type *bounds = alloc_type (objfile);
17015 bounds->set_code (TYPE_CODE_STRUCT);
17016
17017 bounds->set_num_fields (range_fields.size ());
17018 bounds->set_fields
17019 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
17020 * sizeof (struct field))));
17021 memcpy (bounds->fields (), range_fields.data (),
17022 bounds->num_fields () * sizeof (struct field));
17023
17024 int last_fieldno = range_fields.size () - 1;
17025 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
17026 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
17027 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
17028
17029 /* Rewrite the existing array type in place. Specifically, we
17030 remove any dynamic properties we might have read, and we replace
17031 the index types. */
17032 struct type *iter = type;
17033 for (int i = 0; i < range_fields.size (); i += 2)
17034 {
17035 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
17036 iter->main_type->dyn_prop_list = nullptr;
17037 iter->set_index_type
17038 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
17039 iter = TYPE_TARGET_TYPE (iter);
17040 }
17041
17042 struct type *result = alloc_type (objfile);
17043 result->set_code (TYPE_CODE_STRUCT);
17044
17045 result->set_num_fields (2);
17046 result->set_fields
17047 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
17048 * sizeof (struct field))));
17049
17050 /* The names are chosen to coincide with what the compiler does with
17051 -fgnat-encodings=all, which the Ada code in gdb already
17052 understands. */
17053 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
17054 result->field (0).set_type (lookup_pointer_type (type));
17055
17056 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
17057 result->field (1).set_type (lookup_pointer_type (bounds));
17058 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
17059
17060 result->set_name (type->name ());
17061 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
17062 + TYPE_LENGTH (result->field (1).type ()));
17063
17064 return result;
17065 }
17066
17067 /* Extract all information from a DW_TAG_array_type DIE and put it in
17068 the DIE's type field. For now, this only handles one dimensional
17069 arrays. */
17070
17071 static struct type *
17072 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
17073 {
17074 struct objfile *objfile = cu->per_objfile->objfile;
17075 struct die_info *child_die;
17076 struct type *type;
17077 struct type *element_type, *range_type, *index_type;
17078 struct attribute *attr;
17079 const char *name;
17080 struct dynamic_prop *byte_stride_prop = NULL;
17081 unsigned int bit_stride = 0;
17082
17083 element_type = die_type (die, cu);
17084
17085 /* The die_type call above may have already set the type for this DIE. */
17086 type = get_die_type (die, cu);
17087 if (type)
17088 return type;
17089
17090 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
17091 if (attr != NULL)
17092 {
17093 int stride_ok;
17094 struct type *prop_type = cu->addr_sized_int_type (false);
17095
17096 byte_stride_prop
17097 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
17098 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
17099 prop_type);
17100 if (!stride_ok)
17101 {
17102 complaint (_("unable to read array DW_AT_byte_stride "
17103 " - DIE at %s [in module %s]"),
17104 sect_offset_str (die->sect_off),
17105 objfile_name (cu->per_objfile->objfile));
17106 /* Ignore this attribute. We will likely not be able to print
17107 arrays of this type correctly, but there is little we can do
17108 to help if we cannot read the attribute's value. */
17109 byte_stride_prop = NULL;
17110 }
17111 }
17112
17113 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
17114 if (attr != NULL)
17115 bit_stride = attr->constant_value (0);
17116
17117 /* Irix 6.2 native cc creates array types without children for
17118 arrays with unspecified length. */
17119 if (die->child == NULL)
17120 {
17121 index_type = objfile_type (objfile)->builtin_int;
17122 range_type = create_static_range_type (NULL, index_type, 0, -1);
17123 type = create_array_type_with_stride (NULL, element_type, range_type,
17124 byte_stride_prop, bit_stride);
17125 return set_die_type (die, type, cu);
17126 }
17127
17128 std::vector<struct type *> range_types;
17129 child_die = die->child;
17130 while (child_die && child_die->tag)
17131 {
17132 if (child_die->tag == DW_TAG_subrange_type)
17133 {
17134 struct type *child_type = read_type_die (child_die, cu);
17135
17136 if (child_type != NULL)
17137 {
17138 /* The range type was succesfully read. Save it for the
17139 array type creation. */
17140 range_types.push_back (child_type);
17141 }
17142 }
17143 child_die = child_die->sibling;
17144 }
17145
17146 if (range_types.empty ())
17147 {
17148 complaint (_("unable to find array range - DIE at %s [in module %s]"),
17149 sect_offset_str (die->sect_off),
17150 objfile_name (cu->per_objfile->objfile));
17151 return NULL;
17152 }
17153
17154 /* Dwarf2 dimensions are output from left to right, create the
17155 necessary array types in backwards order. */
17156
17157 type = element_type;
17158
17159 if (read_array_order (die, cu) == DW_ORD_col_major)
17160 {
17161 int i = 0;
17162
17163 while (i < range_types.size ())
17164 {
17165 type = create_array_type_with_stride (NULL, type, range_types[i++],
17166 byte_stride_prop, bit_stride);
17167 bit_stride = 0;
17168 byte_stride_prop = nullptr;
17169 }
17170 }
17171 else
17172 {
17173 size_t ndim = range_types.size ();
17174 while (ndim-- > 0)
17175 {
17176 type = create_array_type_with_stride (NULL, type, range_types[ndim],
17177 byte_stride_prop, bit_stride);
17178 bit_stride = 0;
17179 byte_stride_prop = nullptr;
17180 }
17181 }
17182
17183 gdb_assert (type != element_type);
17184
17185 /* Understand Dwarf2 support for vector types (like they occur on
17186 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
17187 array type. This is not part of the Dwarf2/3 standard yet, but a
17188 custom vendor extension. The main difference between a regular
17189 array and the vector variant is that vectors are passed by value
17190 to functions. */
17191 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
17192 if (attr != nullptr)
17193 make_vector_type (type);
17194
17195 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
17196 implementation may choose to implement triple vectors using this
17197 attribute. */
17198 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17199 if (attr != nullptr && attr->form_is_unsigned ())
17200 {
17201 if (attr->as_unsigned () >= TYPE_LENGTH (type))
17202 TYPE_LENGTH (type) = attr->as_unsigned ();
17203 else
17204 complaint (_("DW_AT_byte_size for array type smaller "
17205 "than the total size of elements"));
17206 }
17207
17208 name = dwarf2_name (die, cu);
17209 if (name)
17210 type->set_name (name);
17211
17212 maybe_set_alignment (cu, die, type);
17213
17214 struct type *replacement_type = nullptr;
17215 if (cu->language == language_ada)
17216 {
17217 replacement_type = quirk_ada_thick_pointer (die, cu, type);
17218 if (replacement_type != nullptr)
17219 type = replacement_type;
17220 }
17221
17222 /* Install the type in the die. */
17223 set_die_type (die, type, cu, replacement_type != nullptr);
17224
17225 /* set_die_type should be already done. */
17226 set_descriptive_type (type, die, cu);
17227
17228 return type;
17229 }
17230
17231 static enum dwarf_array_dim_ordering
17232 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
17233 {
17234 struct attribute *attr;
17235
17236 attr = dwarf2_attr (die, DW_AT_ordering, cu);
17237
17238 if (attr != nullptr)
17239 {
17240 LONGEST val = attr->constant_value (-1);
17241 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
17242 return (enum dwarf_array_dim_ordering) val;
17243 }
17244
17245 /* GNU F77 is a special case, as at 08/2004 array type info is the
17246 opposite order to the dwarf2 specification, but data is still
17247 laid out as per normal fortran.
17248
17249 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
17250 version checking. */
17251
17252 if (cu->language == language_fortran
17253 && cu->producer && strstr (cu->producer, "GNU F77"))
17254 {
17255 return DW_ORD_row_major;
17256 }
17257
17258 switch (cu->language_defn->array_ordering ())
17259 {
17260 case array_column_major:
17261 return DW_ORD_col_major;
17262 case array_row_major:
17263 default:
17264 return DW_ORD_row_major;
17265 };
17266 }
17267
17268 /* Extract all information from a DW_TAG_set_type DIE and put it in
17269 the DIE's type field. */
17270
17271 static struct type *
17272 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
17273 {
17274 struct type *domain_type, *set_type;
17275 struct attribute *attr;
17276
17277 domain_type = die_type (die, cu);
17278
17279 /* The die_type call above may have already set the type for this DIE. */
17280 set_type = get_die_type (die, cu);
17281 if (set_type)
17282 return set_type;
17283
17284 set_type = create_set_type (NULL, domain_type);
17285
17286 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17287 if (attr != nullptr && attr->form_is_unsigned ())
17288 TYPE_LENGTH (set_type) = attr->as_unsigned ();
17289
17290 maybe_set_alignment (cu, die, set_type);
17291
17292 return set_die_type (die, set_type, cu);
17293 }
17294
17295 /* A helper for read_common_block that creates a locexpr baton.
17296 SYM is the symbol which we are marking as computed.
17297 COMMON_DIE is the DIE for the common block.
17298 COMMON_LOC is the location expression attribute for the common
17299 block itself.
17300 MEMBER_LOC is the location expression attribute for the particular
17301 member of the common block that we are processing.
17302 CU is the CU from which the above come. */
17303
17304 static void
17305 mark_common_block_symbol_computed (struct symbol *sym,
17306 struct die_info *common_die,
17307 struct attribute *common_loc,
17308 struct attribute *member_loc,
17309 struct dwarf2_cu *cu)
17310 {
17311 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17312 struct objfile *objfile = per_objfile->objfile;
17313 struct dwarf2_locexpr_baton *baton;
17314 gdb_byte *ptr;
17315 unsigned int cu_off;
17316 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
17317 LONGEST offset = 0;
17318
17319 gdb_assert (common_loc && member_loc);
17320 gdb_assert (common_loc->form_is_block ());
17321 gdb_assert (member_loc->form_is_block ()
17322 || member_loc->form_is_constant ());
17323
17324 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
17325 baton->per_objfile = per_objfile;
17326 baton->per_cu = cu->per_cu;
17327 gdb_assert (baton->per_cu);
17328
17329 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17330
17331 if (member_loc->form_is_constant ())
17332 {
17333 offset = member_loc->constant_value (0);
17334 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17335 }
17336 else
17337 baton->size += member_loc->as_block ()->size;
17338
17339 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
17340 baton->data = ptr;
17341
17342 *ptr++ = DW_OP_call4;
17343 cu_off = common_die->sect_off - cu->per_cu->sect_off;
17344 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17345 ptr += 4;
17346
17347 if (member_loc->form_is_constant ())
17348 {
17349 *ptr++ = DW_OP_addr;
17350 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17351 ptr += cu->header.addr_size;
17352 }
17353 else
17354 {
17355 /* We have to copy the data here, because DW_OP_call4 will only
17356 use a DW_AT_location attribute. */
17357 struct dwarf_block *block = member_loc->as_block ();
17358 memcpy (ptr, block->data, block->size);
17359 ptr += block->size;
17360 }
17361
17362 *ptr++ = DW_OP_plus;
17363 gdb_assert (ptr - baton->data == baton->size);
17364
17365 SYMBOL_LOCATION_BATON (sym) = baton;
17366 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
17367 }
17368
17369 /* Create appropriate locally-scoped variables for all the
17370 DW_TAG_common_block entries. Also create a struct common_block
17371 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
17372 is used to separate the common blocks name namespace from regular
17373 variable names. */
17374
17375 static void
17376 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
17377 {
17378 struct attribute *attr;
17379
17380 attr = dwarf2_attr (die, DW_AT_location, cu);
17381 if (attr != nullptr)
17382 {
17383 /* Support the .debug_loc offsets. */
17384 if (attr->form_is_block ())
17385 {
17386 /* Ok. */
17387 }
17388 else if (attr->form_is_section_offset ())
17389 {
17390 dwarf2_complex_location_expr_complaint ();
17391 attr = NULL;
17392 }
17393 else
17394 {
17395 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17396 "common block member");
17397 attr = NULL;
17398 }
17399 }
17400
17401 if (die->child != NULL)
17402 {
17403 struct objfile *objfile = cu->per_objfile->objfile;
17404 struct die_info *child_die;
17405 size_t n_entries = 0, size;
17406 struct common_block *common_block;
17407 struct symbol *sym;
17408
17409 for (child_die = die->child;
17410 child_die && child_die->tag;
17411 child_die = child_die->sibling)
17412 ++n_entries;
17413
17414 size = (sizeof (struct common_block)
17415 + (n_entries - 1) * sizeof (struct symbol *));
17416 common_block
17417 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17418 size);
17419 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17420 common_block->n_entries = 0;
17421
17422 for (child_die = die->child;
17423 child_die && child_die->tag;
17424 child_die = child_die->sibling)
17425 {
17426 /* Create the symbol in the DW_TAG_common_block block in the current
17427 symbol scope. */
17428 sym = new_symbol (child_die, NULL, cu);
17429 if (sym != NULL)
17430 {
17431 struct attribute *member_loc;
17432
17433 common_block->contents[common_block->n_entries++] = sym;
17434
17435 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17436 cu);
17437 if (member_loc)
17438 {
17439 /* GDB has handled this for a long time, but it is
17440 not specified by DWARF. It seems to have been
17441 emitted by gfortran at least as recently as:
17442 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17443 complaint (_("Variable in common block has "
17444 "DW_AT_data_member_location "
17445 "- DIE at %s [in module %s]"),
17446 sect_offset_str (child_die->sect_off),
17447 objfile_name (objfile));
17448
17449 if (member_loc->form_is_section_offset ())
17450 dwarf2_complex_location_expr_complaint ();
17451 else if (member_loc->form_is_constant ()
17452 || member_loc->form_is_block ())
17453 {
17454 if (attr != nullptr)
17455 mark_common_block_symbol_computed (sym, die, attr,
17456 member_loc, cu);
17457 }
17458 else
17459 dwarf2_complex_location_expr_complaint ();
17460 }
17461 }
17462 }
17463
17464 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17465 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
17466 }
17467 }
17468
17469 /* Create a type for a C++ namespace. */
17470
17471 static struct type *
17472 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
17473 {
17474 struct objfile *objfile = cu->per_objfile->objfile;
17475 const char *previous_prefix, *name;
17476 int is_anonymous;
17477 struct type *type;
17478
17479 /* For extensions, reuse the type of the original namespace. */
17480 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17481 {
17482 struct die_info *ext_die;
17483 struct dwarf2_cu *ext_cu = cu;
17484
17485 ext_die = dwarf2_extension (die, &ext_cu);
17486 type = read_type_die (ext_die, ext_cu);
17487
17488 /* EXT_CU may not be the same as CU.
17489 Ensure TYPE is recorded with CU in die_type_hash. */
17490 return set_die_type (die, type, cu);
17491 }
17492
17493 name = namespace_name (die, &is_anonymous, cu);
17494
17495 /* Now build the name of the current namespace. */
17496
17497 previous_prefix = determine_prefix (die, cu);
17498 if (previous_prefix[0] != '\0')
17499 name = typename_concat (&objfile->objfile_obstack,
17500 previous_prefix, name, 0, cu);
17501
17502 /* Create the type. */
17503 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17504
17505 return set_die_type (die, type, cu);
17506 }
17507
17508 /* Read a namespace scope. */
17509
17510 static void
17511 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17512 {
17513 struct objfile *objfile = cu->per_objfile->objfile;
17514 int is_anonymous;
17515
17516 /* Add a symbol associated to this if we haven't seen the namespace
17517 before. Also, add a using directive if it's an anonymous
17518 namespace. */
17519
17520 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17521 {
17522 struct type *type;
17523
17524 type = read_type_die (die, cu);
17525 new_symbol (die, type, cu);
17526
17527 namespace_name (die, &is_anonymous, cu);
17528 if (is_anonymous)
17529 {
17530 const char *previous_prefix = determine_prefix (die, cu);
17531
17532 std::vector<const char *> excludes;
17533 add_using_directive (using_directives (cu),
17534 previous_prefix, type->name (), NULL,
17535 NULL, excludes, 0, &objfile->objfile_obstack);
17536 }
17537 }
17538
17539 if (die->child != NULL)
17540 {
17541 struct die_info *child_die = die->child;
17542
17543 while (child_die && child_die->tag)
17544 {
17545 process_die (child_die, cu);
17546 child_die = child_die->sibling;
17547 }
17548 }
17549 }
17550
17551 /* Read a Fortran module as type. This DIE can be only a declaration used for
17552 imported module. Still we need that type as local Fortran "use ... only"
17553 declaration imports depend on the created type in determine_prefix. */
17554
17555 static struct type *
17556 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17557 {
17558 struct objfile *objfile = cu->per_objfile->objfile;
17559 const char *module_name;
17560 struct type *type;
17561
17562 module_name = dwarf2_name (die, cu);
17563 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17564
17565 return set_die_type (die, type, cu);
17566 }
17567
17568 /* Read a Fortran module. */
17569
17570 static void
17571 read_module (struct die_info *die, struct dwarf2_cu *cu)
17572 {
17573 struct die_info *child_die = die->child;
17574 struct type *type;
17575
17576 type = read_type_die (die, cu);
17577 new_symbol (die, type, cu);
17578
17579 while (child_die && child_die->tag)
17580 {
17581 process_die (child_die, cu);
17582 child_die = child_die->sibling;
17583 }
17584 }
17585
17586 /* Return the name of the namespace represented by DIE. Set
17587 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17588 namespace. */
17589
17590 static const char *
17591 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17592 {
17593 struct die_info *current_die;
17594 const char *name = NULL;
17595
17596 /* Loop through the extensions until we find a name. */
17597
17598 for (current_die = die;
17599 current_die != NULL;
17600 current_die = dwarf2_extension (die, &cu))
17601 {
17602 /* We don't use dwarf2_name here so that we can detect the absence
17603 of a name -> anonymous namespace. */
17604 name = dwarf2_string_attr (die, DW_AT_name, cu);
17605
17606 if (name != NULL)
17607 break;
17608 }
17609
17610 /* Is it an anonymous namespace? */
17611
17612 *is_anonymous = (name == NULL);
17613 if (*is_anonymous)
17614 name = CP_ANONYMOUS_NAMESPACE_STR;
17615
17616 return name;
17617 }
17618
17619 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17620 the user defined type vector. */
17621
17622 static struct type *
17623 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17624 {
17625 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
17626 struct comp_unit_head *cu_header = &cu->header;
17627 struct type *type;
17628 struct attribute *attr_byte_size;
17629 struct attribute *attr_address_class;
17630 int byte_size, addr_class;
17631 struct type *target_type;
17632
17633 target_type = die_type (die, cu);
17634
17635 /* The die_type call above may have already set the type for this DIE. */
17636 type = get_die_type (die, cu);
17637 if (type)
17638 return type;
17639
17640 type = lookup_pointer_type (target_type);
17641
17642 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17643 if (attr_byte_size)
17644 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
17645 else
17646 byte_size = cu_header->addr_size;
17647
17648 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17649 if (attr_address_class)
17650 addr_class = attr_address_class->constant_value (DW_ADDR_none);
17651 else
17652 addr_class = DW_ADDR_none;
17653
17654 ULONGEST alignment = get_alignment (cu, die);
17655
17656 /* If the pointer size, alignment, or address class is different
17657 than the default, create a type variant marked as such and set
17658 the length accordingly. */
17659 if (TYPE_LENGTH (type) != byte_size
17660 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17661 && alignment != TYPE_RAW_ALIGN (type))
17662 || addr_class != DW_ADDR_none)
17663 {
17664 if (gdbarch_address_class_type_flags_p (gdbarch))
17665 {
17666 type_instance_flags type_flags
17667 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17668 addr_class);
17669 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17670 == 0);
17671 type = make_type_with_address_space (type, type_flags);
17672 }
17673 else if (TYPE_LENGTH (type) != byte_size)
17674 {
17675 complaint (_("invalid pointer size %d"), byte_size);
17676 }
17677 else if (TYPE_RAW_ALIGN (type) != alignment)
17678 {
17679 complaint (_("Invalid DW_AT_alignment"
17680 " - DIE at %s [in module %s]"),
17681 sect_offset_str (die->sect_off),
17682 objfile_name (cu->per_objfile->objfile));
17683 }
17684 else
17685 {
17686 /* Should we also complain about unhandled address classes? */
17687 }
17688 }
17689
17690 TYPE_LENGTH (type) = byte_size;
17691 set_type_align (type, alignment);
17692 return set_die_type (die, type, cu);
17693 }
17694
17695 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17696 the user defined type vector. */
17697
17698 static struct type *
17699 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17700 {
17701 struct type *type;
17702 struct type *to_type;
17703 struct type *domain;
17704
17705 to_type = die_type (die, cu);
17706 domain = die_containing_type (die, cu);
17707
17708 /* The calls above may have already set the type for this DIE. */
17709 type = get_die_type (die, cu);
17710 if (type)
17711 return type;
17712
17713 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17714 type = lookup_methodptr_type (to_type);
17715 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17716 {
17717 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17718
17719 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17720 to_type->fields (), to_type->num_fields (),
17721 to_type->has_varargs ());
17722 type = lookup_methodptr_type (new_type);
17723 }
17724 else
17725 type = lookup_memberptr_type (to_type, domain);
17726
17727 return set_die_type (die, type, cu);
17728 }
17729
17730 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17731 the user defined type vector. */
17732
17733 static struct type *
17734 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17735 enum type_code refcode)
17736 {
17737 struct comp_unit_head *cu_header = &cu->header;
17738 struct type *type, *target_type;
17739 struct attribute *attr;
17740
17741 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17742
17743 target_type = die_type (die, cu);
17744
17745 /* The die_type call above may have already set the type for this DIE. */
17746 type = get_die_type (die, cu);
17747 if (type)
17748 return type;
17749
17750 type = lookup_reference_type (target_type, refcode);
17751 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17752 if (attr != nullptr)
17753 {
17754 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
17755 }
17756 else
17757 {
17758 TYPE_LENGTH (type) = cu_header->addr_size;
17759 }
17760 maybe_set_alignment (cu, die, type);
17761 return set_die_type (die, type, cu);
17762 }
17763
17764 /* Add the given cv-qualifiers to the element type of the array. GCC
17765 outputs DWARF type qualifiers that apply to an array, not the
17766 element type. But GDB relies on the array element type to carry
17767 the cv-qualifiers. This mimics section 6.7.3 of the C99
17768 specification. */
17769
17770 static struct type *
17771 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17772 struct type *base_type, int cnst, int voltl)
17773 {
17774 struct type *el_type, *inner_array;
17775
17776 base_type = copy_type (base_type);
17777 inner_array = base_type;
17778
17779 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17780 {
17781 TYPE_TARGET_TYPE (inner_array) =
17782 copy_type (TYPE_TARGET_TYPE (inner_array));
17783 inner_array = TYPE_TARGET_TYPE (inner_array);
17784 }
17785
17786 el_type = TYPE_TARGET_TYPE (inner_array);
17787 cnst |= TYPE_CONST (el_type);
17788 voltl |= TYPE_VOLATILE (el_type);
17789 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17790
17791 return set_die_type (die, base_type, cu);
17792 }
17793
17794 static struct type *
17795 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17796 {
17797 struct type *base_type, *cv_type;
17798
17799 base_type = die_type (die, cu);
17800
17801 /* The die_type call above may have already set the type for this DIE. */
17802 cv_type = get_die_type (die, cu);
17803 if (cv_type)
17804 return cv_type;
17805
17806 /* In case the const qualifier is applied to an array type, the element type
17807 is so qualified, not the array type (section 6.7.3 of C99). */
17808 if (base_type->code () == TYPE_CODE_ARRAY)
17809 return add_array_cv_type (die, cu, base_type, 1, 0);
17810
17811 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17812 return set_die_type (die, cv_type, cu);
17813 }
17814
17815 static struct type *
17816 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17817 {
17818 struct type *base_type, *cv_type;
17819
17820 base_type = die_type (die, cu);
17821
17822 /* The die_type call above may have already set the type for this DIE. */
17823 cv_type = get_die_type (die, cu);
17824 if (cv_type)
17825 return cv_type;
17826
17827 /* In case the volatile qualifier is applied to an array type, the
17828 element type is so qualified, not the array type (section 6.7.3
17829 of C99). */
17830 if (base_type->code () == TYPE_CODE_ARRAY)
17831 return add_array_cv_type (die, cu, base_type, 0, 1);
17832
17833 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17834 return set_die_type (die, cv_type, cu);
17835 }
17836
17837 /* Handle DW_TAG_restrict_type. */
17838
17839 static struct type *
17840 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17841 {
17842 struct type *base_type, *cv_type;
17843
17844 base_type = die_type (die, cu);
17845
17846 /* The die_type call above may have already set the type for this DIE. */
17847 cv_type = get_die_type (die, cu);
17848 if (cv_type)
17849 return cv_type;
17850
17851 cv_type = make_restrict_type (base_type);
17852 return set_die_type (die, cv_type, cu);
17853 }
17854
17855 /* Handle DW_TAG_atomic_type. */
17856
17857 static struct type *
17858 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17859 {
17860 struct type *base_type, *cv_type;
17861
17862 base_type = die_type (die, cu);
17863
17864 /* The die_type call above may have already set the type for this DIE. */
17865 cv_type = get_die_type (die, cu);
17866 if (cv_type)
17867 return cv_type;
17868
17869 cv_type = make_atomic_type (base_type);
17870 return set_die_type (die, cv_type, cu);
17871 }
17872
17873 /* Extract all information from a DW_TAG_string_type DIE and add to
17874 the user defined type vector. It isn't really a user defined type,
17875 but it behaves like one, with other DIE's using an AT_user_def_type
17876 attribute to reference it. */
17877
17878 static struct type *
17879 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17880 {
17881 struct objfile *objfile = cu->per_objfile->objfile;
17882 struct gdbarch *gdbarch = objfile->arch ();
17883 struct type *type, *range_type, *index_type, *char_type;
17884 struct attribute *attr;
17885 struct dynamic_prop prop;
17886 bool length_is_constant = true;
17887 LONGEST length;
17888
17889 /* There are a couple of places where bit sizes might be made use of
17890 when parsing a DW_TAG_string_type, however, no producer that we know
17891 of make use of these. Handling bit sizes that are a multiple of the
17892 byte size is easy enough, but what about other bit sizes? Lets deal
17893 with that problem when we have to. Warn about these attributes being
17894 unsupported, then parse the type and ignore them like we always
17895 have. */
17896 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17897 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17898 {
17899 static bool warning_printed = false;
17900 if (!warning_printed)
17901 {
17902 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17903 "currently supported on DW_TAG_string_type."));
17904 warning_printed = true;
17905 }
17906 }
17907
17908 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17909 if (attr != nullptr && !attr->form_is_constant ())
17910 {
17911 /* The string length describes the location at which the length of
17912 the string can be found. The size of the length field can be
17913 specified with one of the attributes below. */
17914 struct type *prop_type;
17915 struct attribute *len
17916 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17917 if (len == nullptr)
17918 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17919 if (len != nullptr && len->form_is_constant ())
17920 {
17921 /* Pass 0 as the default as we know this attribute is constant
17922 and the default value will not be returned. */
17923 LONGEST sz = len->constant_value (0);
17924 prop_type = cu->per_objfile->int_type (sz, true);
17925 }
17926 else
17927 {
17928 /* If the size is not specified then we assume it is the size of
17929 an address on this target. */
17930 prop_type = cu->addr_sized_int_type (true);
17931 }
17932
17933 /* Convert the attribute into a dynamic property. */
17934 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17935 length = 1;
17936 else
17937 length_is_constant = false;
17938 }
17939 else if (attr != nullptr)
17940 {
17941 /* This DW_AT_string_length just contains the length with no
17942 indirection. There's no need to create a dynamic property in this
17943 case. Pass 0 for the default value as we know it will not be
17944 returned in this case. */
17945 length = attr->constant_value (0);
17946 }
17947 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17948 {
17949 /* We don't currently support non-constant byte sizes for strings. */
17950 length = attr->constant_value (1);
17951 }
17952 else
17953 {
17954 /* Use 1 as a fallback length if we have nothing else. */
17955 length = 1;
17956 }
17957
17958 index_type = objfile_type (objfile)->builtin_int;
17959 if (length_is_constant)
17960 range_type = create_static_range_type (NULL, index_type, 1, length);
17961 else
17962 {
17963 struct dynamic_prop low_bound;
17964
17965 low_bound.set_const_val (1);
17966 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17967 }
17968 char_type = language_string_char_type (cu->language_defn, gdbarch);
17969 type = create_string_type (NULL, char_type, range_type);
17970
17971 return set_die_type (die, type, cu);
17972 }
17973
17974 /* Assuming that DIE corresponds to a function, returns nonzero
17975 if the function is prototyped. */
17976
17977 static int
17978 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17979 {
17980 struct attribute *attr;
17981
17982 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17983 if (attr && attr->as_boolean ())
17984 return 1;
17985
17986 /* The DWARF standard implies that the DW_AT_prototyped attribute
17987 is only meaningful for C, but the concept also extends to other
17988 languages that allow unprototyped functions (Eg: Objective C).
17989 For all other languages, assume that functions are always
17990 prototyped. */
17991 if (cu->language != language_c
17992 && cu->language != language_objc
17993 && cu->language != language_opencl)
17994 return 1;
17995
17996 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17997 prototyped and unprototyped functions; default to prototyped,
17998 since that is more common in modern code (and RealView warns
17999 about unprototyped functions). */
18000 if (producer_is_realview (cu->producer))
18001 return 1;
18002
18003 return 0;
18004 }
18005
18006 /* Handle DIES due to C code like:
18007
18008 struct foo
18009 {
18010 int (*funcp)(int a, long l);
18011 int b;
18012 };
18013
18014 ('funcp' generates a DW_TAG_subroutine_type DIE). */
18015
18016 static struct type *
18017 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
18018 {
18019 struct objfile *objfile = cu->per_objfile->objfile;
18020 struct type *type; /* Type that this function returns. */
18021 struct type *ftype; /* Function that returns above type. */
18022 struct attribute *attr;
18023
18024 type = die_type (die, cu);
18025
18026 /* The die_type call above may have already set the type for this DIE. */
18027 ftype = get_die_type (die, cu);
18028 if (ftype)
18029 return ftype;
18030
18031 ftype = lookup_function_type (type);
18032
18033 if (prototyped_function_p (die, cu))
18034 ftype->set_is_prototyped (true);
18035
18036 /* Store the calling convention in the type if it's available in
18037 the subroutine die. Otherwise set the calling convention to
18038 the default value DW_CC_normal. */
18039 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
18040 if (attr != nullptr
18041 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
18042 TYPE_CALLING_CONVENTION (ftype)
18043 = (enum dwarf_calling_convention) attr->constant_value (0);
18044 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
18045 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
18046 else
18047 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
18048
18049 /* Record whether the function returns normally to its caller or not
18050 if the DWARF producer set that information. */
18051 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
18052 if (attr && attr->as_boolean ())
18053 TYPE_NO_RETURN (ftype) = 1;
18054
18055 /* We need to add the subroutine type to the die immediately so
18056 we don't infinitely recurse when dealing with parameters
18057 declared as the same subroutine type. */
18058 set_die_type (die, ftype, cu);
18059
18060 if (die->child != NULL)
18061 {
18062 struct type *void_type = objfile_type (objfile)->builtin_void;
18063 struct die_info *child_die;
18064 int nparams, iparams;
18065
18066 /* Count the number of parameters.
18067 FIXME: GDB currently ignores vararg functions, but knows about
18068 vararg member functions. */
18069 nparams = 0;
18070 child_die = die->child;
18071 while (child_die && child_die->tag)
18072 {
18073 if (child_die->tag == DW_TAG_formal_parameter)
18074 nparams++;
18075 else if (child_die->tag == DW_TAG_unspecified_parameters)
18076 ftype->set_has_varargs (true);
18077
18078 child_die = child_die->sibling;
18079 }
18080
18081 /* Allocate storage for parameters and fill them in. */
18082 ftype->set_num_fields (nparams);
18083 ftype->set_fields
18084 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
18085
18086 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
18087 even if we error out during the parameters reading below. */
18088 for (iparams = 0; iparams < nparams; iparams++)
18089 ftype->field (iparams).set_type (void_type);
18090
18091 iparams = 0;
18092 child_die = die->child;
18093 while (child_die && child_die->tag)
18094 {
18095 if (child_die->tag == DW_TAG_formal_parameter)
18096 {
18097 struct type *arg_type;
18098
18099 /* DWARF version 2 has no clean way to discern C++
18100 static and non-static member functions. G++ helps
18101 GDB by marking the first parameter for non-static
18102 member functions (which is the this pointer) as
18103 artificial. We pass this information to
18104 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
18105
18106 DWARF version 3 added DW_AT_object_pointer, which GCC
18107 4.5 does not yet generate. */
18108 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
18109 if (attr != nullptr)
18110 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
18111 else
18112 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
18113 arg_type = die_type (child_die, cu);
18114
18115 /* RealView does not mark THIS as const, which the testsuite
18116 expects. GCC marks THIS as const in method definitions,
18117 but not in the class specifications (GCC PR 43053). */
18118 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
18119 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
18120 {
18121 int is_this = 0;
18122 struct dwarf2_cu *arg_cu = cu;
18123 const char *name = dwarf2_name (child_die, cu);
18124
18125 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
18126 if (attr != nullptr)
18127 {
18128 /* If the compiler emits this, use it. */
18129 if (follow_die_ref (die, attr, &arg_cu) == child_die)
18130 is_this = 1;
18131 }
18132 else if (name && strcmp (name, "this") == 0)
18133 /* Function definitions will have the argument names. */
18134 is_this = 1;
18135 else if (name == NULL && iparams == 0)
18136 /* Declarations may not have the names, so like
18137 elsewhere in GDB, assume an artificial first
18138 argument is "this". */
18139 is_this = 1;
18140
18141 if (is_this)
18142 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
18143 arg_type, 0);
18144 }
18145
18146 ftype->field (iparams).set_type (arg_type);
18147 iparams++;
18148 }
18149 child_die = child_die->sibling;
18150 }
18151 }
18152
18153 return ftype;
18154 }
18155
18156 static struct type *
18157 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
18158 {
18159 struct objfile *objfile = cu->per_objfile->objfile;
18160 const char *name = NULL;
18161 struct type *this_type, *target_type;
18162
18163 name = dwarf2_full_name (NULL, die, cu);
18164 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
18165 this_type->set_target_is_stub (true);
18166 set_die_type (die, this_type, cu);
18167 target_type = die_type (die, cu);
18168 if (target_type != this_type)
18169 TYPE_TARGET_TYPE (this_type) = target_type;
18170 else
18171 {
18172 /* Self-referential typedefs are, it seems, not allowed by the DWARF
18173 spec and cause infinite loops in GDB. */
18174 complaint (_("Self-referential DW_TAG_typedef "
18175 "- DIE at %s [in module %s]"),
18176 sect_offset_str (die->sect_off), objfile_name (objfile));
18177 TYPE_TARGET_TYPE (this_type) = NULL;
18178 }
18179 if (name == NULL)
18180 {
18181 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
18182 anonymous typedefs, which is, strictly speaking, invalid DWARF.
18183 Handle these by just returning the target type, rather than
18184 constructing an anonymous typedef type and trying to handle this
18185 elsewhere. */
18186 set_die_type (die, target_type, cu);
18187 return target_type;
18188 }
18189 return this_type;
18190 }
18191
18192 /* Helper for get_dwarf2_rational_constant that computes the value of
18193 a given gmp_mpz given an attribute. */
18194
18195 static void
18196 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
18197 {
18198 /* GCC will sometimes emit a 16-byte constant value as a DWARF
18199 location expression that pushes an implicit value. */
18200 if (attr->form == DW_FORM_exprloc)
18201 {
18202 dwarf_block *blk = attr->as_block ();
18203 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
18204 {
18205 uint64_t len;
18206 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
18207 blk->data + blk->size,
18208 &len);
18209 if (ptr - blk->data + len <= blk->size)
18210 {
18211 mpz_import (value->val, len,
18212 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18213 1, 0, 0, ptr);
18214 return;
18215 }
18216 }
18217
18218 /* On failure set it to 1. */
18219 *value = gdb_mpz (1);
18220 }
18221 else if (attr->form_is_block ())
18222 {
18223 dwarf_block *blk = attr->as_block ();
18224 mpz_import (value->val, blk->size,
18225 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18226 1, 0, 0, blk->data);
18227 }
18228 else
18229 *value = gdb_mpz (attr->constant_value (1));
18230 }
18231
18232 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
18233 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
18234
18235 If the numerator and/or numerator attribute is missing,
18236 a complaint is filed, and NUMERATOR and DENOMINATOR are left
18237 untouched. */
18238
18239 static void
18240 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
18241 gdb_mpz *numerator, gdb_mpz *denominator)
18242 {
18243 struct attribute *num_attr, *denom_attr;
18244
18245 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
18246 if (num_attr == nullptr)
18247 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
18248 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18249
18250 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
18251 if (denom_attr == nullptr)
18252 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
18253 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18254
18255 if (num_attr == nullptr || denom_attr == nullptr)
18256 return;
18257
18258 get_mpz (cu, numerator, num_attr);
18259 get_mpz (cu, denominator, denom_attr);
18260 }
18261
18262 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
18263 rational constant, rather than a signed one.
18264
18265 If the rational constant has a negative value, a complaint
18266 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
18267
18268 static void
18269 get_dwarf2_unsigned_rational_constant (struct die_info *die,
18270 struct dwarf2_cu *cu,
18271 gdb_mpz *numerator,
18272 gdb_mpz *denominator)
18273 {
18274 gdb_mpz num (1);
18275 gdb_mpz denom (1);
18276
18277 get_dwarf2_rational_constant (die, cu, &num, &denom);
18278 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
18279 {
18280 mpz_neg (num.val, num.val);
18281 mpz_neg (denom.val, denom.val);
18282 }
18283 else if (mpz_sgn (num.val) == -1)
18284 {
18285 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
18286 " in DIE at %s"),
18287 sect_offset_str (die->sect_off));
18288 return;
18289 }
18290 else if (mpz_sgn (denom.val) == -1)
18291 {
18292 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
18293 " in DIE at %s"),
18294 sect_offset_str (die->sect_off));
18295 return;
18296 }
18297
18298 *numerator = std::move (num);
18299 *denominator = std::move (denom);
18300 }
18301
18302 /* Assuming that ENCODING is a string whose contents starting at the
18303 K'th character is "_nn" where "nn" is a decimal number, scan that
18304 number and set RESULT to the value. K is updated to point to the
18305 character immediately following the number.
18306
18307 If the string does not conform to the format described above, false
18308 is returned, and K may or may not be changed. */
18309
18310 static bool
18311 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
18312 {
18313 /* The next character should be an underscore ('_') followed
18314 by a digit. */
18315 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
18316 return false;
18317
18318 /* Skip the underscore. */
18319 k++;
18320 int start = k;
18321
18322 /* Determine the number of digits for our number. */
18323 while (isdigit (encoding[k]))
18324 k++;
18325 if (k == start)
18326 return false;
18327
18328 std::string copy (&encoding[start], k - start);
18329 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
18330 return false;
18331
18332 return true;
18333 }
18334
18335 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
18336 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
18337 DENOM, update OFFSET, and return true on success. Return false on
18338 failure. */
18339
18340 static bool
18341 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
18342 gdb_mpz *num, gdb_mpz *denom)
18343 {
18344 if (!ada_get_gnat_encoded_number (encoding, offset, num))
18345 return false;
18346 return ada_get_gnat_encoded_number (encoding, offset, denom);
18347 }
18348
18349 /* Assuming DIE corresponds to a fixed point type, finish the creation
18350 of the corresponding TYPE by setting its type-specific data. CU is
18351 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
18352 encodings. It is nullptr if the GNAT encoding should be
18353 ignored. */
18354
18355 static void
18356 finish_fixed_point_type (struct type *type, const char *suffix,
18357 struct die_info *die, struct dwarf2_cu *cu)
18358 {
18359 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
18360 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
18361
18362 /* If GNAT encodings are preferred, don't examine the
18363 attributes. */
18364 struct attribute *attr = nullptr;
18365 if (suffix == nullptr)
18366 {
18367 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
18368 if (attr == nullptr)
18369 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
18370 if (attr == nullptr)
18371 attr = dwarf2_attr (die, DW_AT_small, cu);
18372 }
18373
18374 /* Numerator and denominator of our fixed-point type's scaling factor.
18375 The default is a scaling factor of 1, which we use as a fallback
18376 when we are not able to decode it (problem with the debugging info,
18377 unsupported forms, bug in GDB, etc...). Using that as the default
18378 allows us to at least print the unscaled value, which might still
18379 be useful to a user. */
18380 gdb_mpz scale_num (1);
18381 gdb_mpz scale_denom (1);
18382
18383 if (attr == nullptr)
18384 {
18385 int offset = 0;
18386 if (suffix != nullptr
18387 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18388 &scale_denom)
18389 /* The number might be encoded as _nn_dd_nn_dd, where the
18390 second ratio is the 'small value. In this situation, we
18391 want the second value. */
18392 && (suffix[offset] != '_'
18393 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18394 &scale_denom)))
18395 {
18396 /* Found it. */
18397 }
18398 else
18399 {
18400 /* Scaling factor not found. Assume a scaling factor of 1,
18401 and hope for the best. At least the user will be able to
18402 see the encoded value. */
18403 scale_num = 1;
18404 scale_denom = 1;
18405 complaint (_("no scale found for fixed-point type (DIE at %s)"),
18406 sect_offset_str (die->sect_off));
18407 }
18408 }
18409 else if (attr->name == DW_AT_binary_scale)
18410 {
18411 LONGEST scale_exp = attr->constant_value (0);
18412 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18413
18414 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
18415 }
18416 else if (attr->name == DW_AT_decimal_scale)
18417 {
18418 LONGEST scale_exp = attr->constant_value (0);
18419 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18420
18421 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
18422 }
18423 else if (attr->name == DW_AT_small)
18424 {
18425 struct die_info *scale_die;
18426 struct dwarf2_cu *scale_cu = cu;
18427
18428 scale_die = follow_die_ref (die, attr, &scale_cu);
18429 if (scale_die->tag == DW_TAG_constant)
18430 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18431 &scale_num, &scale_denom);
18432 else
18433 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18434 " (DIE at %s)"),
18435 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18436 }
18437 else
18438 {
18439 complaint (_("unsupported scale attribute %s for fixed-point type"
18440 " (DIE at %s)"),
18441 dwarf_attr_name (attr->name),
18442 sect_offset_str (die->sect_off));
18443 }
18444
18445 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
18446 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
18447 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
18448 mpq_canonicalize (scaling_factor.val);
18449 }
18450
18451 /* The gnat-encoding suffix for fixed point. */
18452
18453 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
18454
18455 /* If NAME encodes an Ada fixed-point type, return a pointer to the
18456 "XF" suffix of the name. The text after this is what encodes the
18457 'small and 'delta information. Otherwise, return nullptr. */
18458
18459 static const char *
18460 gnat_encoded_fixed_point_type_info (const char *name)
18461 {
18462 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
18463 }
18464
18465 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18466 (which may be different from NAME) to the architecture back-end to allow
18467 it to guess the correct format if necessary. */
18468
18469 static struct type *
18470 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
18471 const char *name_hint, enum bfd_endian byte_order)
18472 {
18473 struct gdbarch *gdbarch = objfile->arch ();
18474 const struct floatformat **format;
18475 struct type *type;
18476
18477 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18478 if (format)
18479 type = init_float_type (objfile, bits, name, format, byte_order);
18480 else
18481 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18482
18483 return type;
18484 }
18485
18486 /* Allocate an integer type of size BITS and name NAME. */
18487
18488 static struct type *
18489 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18490 int bits, int unsigned_p, const char *name)
18491 {
18492 struct type *type;
18493
18494 /* Versions of Intel's C Compiler generate an integer type called "void"
18495 instead of using DW_TAG_unspecified_type. This has been seen on
18496 at least versions 14, 17, and 18. */
18497 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18498 && strcmp (name, "void") == 0)
18499 type = objfile_type (objfile)->builtin_void;
18500 else
18501 type = init_integer_type (objfile, bits, unsigned_p, name);
18502
18503 return type;
18504 }
18505
18506 /* Return true if DIE has a DW_AT_small attribute whose value is
18507 a constant rational, where both the numerator and denominator
18508 are equal to zero.
18509
18510 CU is the DIE's Compilation Unit. */
18511
18512 static bool
18513 has_zero_over_zero_small_attribute (struct die_info *die,
18514 struct dwarf2_cu *cu)
18515 {
18516 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18517 if (attr == nullptr)
18518 return false;
18519
18520 struct dwarf2_cu *scale_cu = cu;
18521 struct die_info *scale_die
18522 = follow_die_ref (die, attr, &scale_cu);
18523
18524 if (scale_die->tag != DW_TAG_constant)
18525 return false;
18526
18527 gdb_mpz num (1), denom (1);
18528 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
18529 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
18530 }
18531
18532 /* Initialise and return a floating point type of size BITS suitable for
18533 use as a component of a complex number. The NAME_HINT is passed through
18534 when initialising the floating point type and is the name of the complex
18535 type.
18536
18537 As DWARF doesn't currently provide an explicit name for the components
18538 of a complex number, but it can be helpful to have these components
18539 named, we try to select a suitable name based on the size of the
18540 component. */
18541 static struct type *
18542 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18543 struct objfile *objfile,
18544 int bits, const char *name_hint,
18545 enum bfd_endian byte_order)
18546 {
18547 gdbarch *gdbarch = objfile->arch ();
18548 struct type *tt = nullptr;
18549
18550 /* Try to find a suitable floating point builtin type of size BITS.
18551 We're going to use the name of this type as the name for the complex
18552 target type that we are about to create. */
18553 switch (cu->language)
18554 {
18555 case language_fortran:
18556 switch (bits)
18557 {
18558 case 32:
18559 tt = builtin_f_type (gdbarch)->builtin_real;
18560 break;
18561 case 64:
18562 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18563 break;
18564 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18565 case 128:
18566 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18567 break;
18568 }
18569 break;
18570 default:
18571 switch (bits)
18572 {
18573 case 32:
18574 tt = builtin_type (gdbarch)->builtin_float;
18575 break;
18576 case 64:
18577 tt = builtin_type (gdbarch)->builtin_double;
18578 break;
18579 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18580 case 128:
18581 tt = builtin_type (gdbarch)->builtin_long_double;
18582 break;
18583 }
18584 break;
18585 }
18586
18587 /* If the type we found doesn't match the size we were looking for, then
18588 pretend we didn't find a type at all, the complex target type we
18589 create will then be nameless. */
18590 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
18591 tt = nullptr;
18592
18593 const char *name = (tt == nullptr) ? nullptr : tt->name ();
18594 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
18595 }
18596
18597 /* Find a representation of a given base type and install
18598 it in the TYPE field of the die. */
18599
18600 static struct type *
18601 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
18602 {
18603 struct objfile *objfile = cu->per_objfile->objfile;
18604 struct type *type;
18605 struct attribute *attr;
18606 int encoding = 0, bits = 0;
18607 const char *name;
18608 gdbarch *arch;
18609
18610 attr = dwarf2_attr (die, DW_AT_encoding, cu);
18611 if (attr != nullptr && attr->form_is_constant ())
18612 encoding = attr->constant_value (0);
18613 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18614 if (attr != nullptr)
18615 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
18616 name = dwarf2_name (die, cu);
18617 if (!name)
18618 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
18619
18620 arch = objfile->arch ();
18621 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18622
18623 attr = dwarf2_attr (die, DW_AT_endianity, cu);
18624 if (attr != nullptr && attr->form_is_constant ())
18625 {
18626 int endianity = attr->constant_value (0);
18627
18628 switch (endianity)
18629 {
18630 case DW_END_big:
18631 byte_order = BFD_ENDIAN_BIG;
18632 break;
18633 case DW_END_little:
18634 byte_order = BFD_ENDIAN_LITTLE;
18635 break;
18636 default:
18637 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18638 break;
18639 }
18640 }
18641
18642 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18643 && cu->language == language_ada
18644 && has_zero_over_zero_small_attribute (die, cu))
18645 {
18646 /* brobecker/2018-02-24: This is a fixed point type for which
18647 the scaling factor is represented as fraction whose value
18648 does not make sense (zero divided by zero), so we should
18649 normally never see these. However, there is a small category
18650 of fixed point types for which GNAT is unable to provide
18651 the scaling factor via the standard DWARF mechanisms, and
18652 for which the info is provided via the GNAT encodings instead.
18653 This is likely what this DIE is about. */
18654 encoding = (encoding == DW_ATE_signed_fixed
18655 ? DW_ATE_signed
18656 : DW_ATE_unsigned);
18657 }
18658
18659 /* With GNAT encodings, fixed-point information will be encoded in
18660 the type name. Note that this can also occur with the above
18661 zero-over-zero case, which is why this is a separate "if" rather
18662 than an "else if". */
18663 const char *gnat_encoding_suffix = nullptr;
18664 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18665 && cu->language == language_ada
18666 && name != nullptr)
18667 {
18668 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18669 if (gnat_encoding_suffix != nullptr)
18670 {
18671 gdb_assert (startswith (gnat_encoding_suffix,
18672 GNAT_FIXED_POINT_SUFFIX));
18673 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18674 name, gnat_encoding_suffix - name);
18675 /* Use -1 here so that SUFFIX points at the "_" after the
18676 "XF". */
18677 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18678
18679 encoding = (encoding == DW_ATE_signed
18680 ? DW_ATE_signed_fixed
18681 : DW_ATE_unsigned_fixed);
18682 }
18683 }
18684
18685 switch (encoding)
18686 {
18687 case DW_ATE_address:
18688 /* Turn DW_ATE_address into a void * pointer. */
18689 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
18690 type = init_pointer_type (objfile, bits, name, type);
18691 break;
18692 case DW_ATE_boolean:
18693 type = init_boolean_type (objfile, bits, 1, name);
18694 break;
18695 case DW_ATE_complex_float:
18696 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18697 byte_order);
18698 if (type->code () == TYPE_CODE_ERROR)
18699 {
18700 if (name == nullptr)
18701 {
18702 struct obstack *obstack
18703 = &cu->per_objfile->objfile->objfile_obstack;
18704 name = obconcat (obstack, "_Complex ", type->name (),
18705 nullptr);
18706 }
18707 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18708 }
18709 else
18710 type = init_complex_type (name, type);
18711 break;
18712 case DW_ATE_decimal_float:
18713 type = init_decfloat_type (objfile, bits, name);
18714 break;
18715 case DW_ATE_float:
18716 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
18717 break;
18718 case DW_ATE_signed:
18719 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18720 break;
18721 case DW_ATE_unsigned:
18722 if (cu->language == language_fortran
18723 && name
18724 && startswith (name, "character("))
18725 type = init_character_type (objfile, bits, 1, name);
18726 else
18727 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18728 break;
18729 case DW_ATE_signed_char:
18730 if (cu->language == language_ada || cu->language == language_m2
18731 || cu->language == language_pascal
18732 || cu->language == language_fortran)
18733 type = init_character_type (objfile, bits, 0, name);
18734 else
18735 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18736 break;
18737 case DW_ATE_unsigned_char:
18738 if (cu->language == language_ada || cu->language == language_m2
18739 || cu->language == language_pascal
18740 || cu->language == language_fortran
18741 || cu->language == language_rust)
18742 type = init_character_type (objfile, bits, 1, name);
18743 else
18744 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18745 break;
18746 case DW_ATE_UTF:
18747 {
18748 if (bits == 16)
18749 type = builtin_type (arch)->builtin_char16;
18750 else if (bits == 32)
18751 type = builtin_type (arch)->builtin_char32;
18752 else
18753 {
18754 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
18755 bits);
18756 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18757 }
18758 return set_die_type (die, type, cu);
18759 }
18760 break;
18761 case DW_ATE_signed_fixed:
18762 type = init_fixed_point_type (objfile, bits, 0, name);
18763 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18764 break;
18765 case DW_ATE_unsigned_fixed:
18766 type = init_fixed_point_type (objfile, bits, 1, name);
18767 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18768 break;
18769
18770 default:
18771 complaint (_("unsupported DW_AT_encoding: '%s'"),
18772 dwarf_type_encoding_name (encoding));
18773 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18774 break;
18775 }
18776
18777 if (name && strcmp (name, "char") == 0)
18778 type->set_has_no_signedness (true);
18779
18780 maybe_set_alignment (cu, die, type);
18781
18782 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18783
18784 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18785 {
18786 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18787 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
18788 {
18789 unsigned real_bit_size = attr->as_unsigned ();
18790 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18791 /* Only use the attributes if they make sense together. */
18792 if (attr == nullptr
18793 || (attr->as_unsigned () + real_bit_size
18794 <= 8 * TYPE_LENGTH (type)))
18795 {
18796 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18797 = real_bit_size;
18798 if (attr != nullptr)
18799 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18800 = attr->as_unsigned ();
18801 }
18802 }
18803 }
18804
18805 return set_die_type (die, type, cu);
18806 }
18807
18808 /* Parse dwarf attribute if it's a block, reference or constant and put the
18809 resulting value of the attribute into struct bound_prop.
18810 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18811
18812 static int
18813 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18814 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18815 struct type *default_type)
18816 {
18817 struct dwarf2_property_baton *baton;
18818 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18819 struct objfile *objfile = per_objfile->objfile;
18820 struct obstack *obstack = &objfile->objfile_obstack;
18821
18822 gdb_assert (default_type != NULL);
18823
18824 if (attr == NULL || prop == NULL)
18825 return 0;
18826
18827 if (attr->form_is_block ())
18828 {
18829 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18830 baton->property_type = default_type;
18831 baton->locexpr.per_cu = cu->per_cu;
18832 baton->locexpr.per_objfile = per_objfile;
18833
18834 struct dwarf_block *block = attr->as_block ();
18835 baton->locexpr.size = block->size;
18836 baton->locexpr.data = block->data;
18837 switch (attr->name)
18838 {
18839 case DW_AT_string_length:
18840 baton->locexpr.is_reference = true;
18841 break;
18842 default:
18843 baton->locexpr.is_reference = false;
18844 break;
18845 }
18846
18847 prop->set_locexpr (baton);
18848 gdb_assert (prop->baton () != NULL);
18849 }
18850 else if (attr->form_is_ref ())
18851 {
18852 struct dwarf2_cu *target_cu = cu;
18853 struct die_info *target_die;
18854 struct attribute *target_attr;
18855
18856 target_die = follow_die_ref (die, attr, &target_cu);
18857 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18858 if (target_attr == NULL)
18859 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18860 target_cu);
18861 if (target_attr == NULL)
18862 return 0;
18863
18864 switch (target_attr->name)
18865 {
18866 case DW_AT_location:
18867 if (target_attr->form_is_section_offset ())
18868 {
18869 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18870 baton->property_type = die_type (target_die, target_cu);
18871 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18872 prop->set_loclist (baton);
18873 gdb_assert (prop->baton () != NULL);
18874 }
18875 else if (target_attr->form_is_block ())
18876 {
18877 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18878 baton->property_type = die_type (target_die, target_cu);
18879 baton->locexpr.per_cu = cu->per_cu;
18880 baton->locexpr.per_objfile = per_objfile;
18881 struct dwarf_block *block = target_attr->as_block ();
18882 baton->locexpr.size = block->size;
18883 baton->locexpr.data = block->data;
18884 baton->locexpr.is_reference = true;
18885 prop->set_locexpr (baton);
18886 gdb_assert (prop->baton () != NULL);
18887 }
18888 else
18889 {
18890 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18891 "dynamic property");
18892 return 0;
18893 }
18894 break;
18895 case DW_AT_data_member_location:
18896 {
18897 LONGEST offset;
18898
18899 if (!handle_data_member_location (target_die, target_cu,
18900 &offset))
18901 return 0;
18902
18903 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18904 baton->property_type = read_type_die (target_die->parent,
18905 target_cu);
18906 baton->offset_info.offset = offset;
18907 baton->offset_info.type = die_type (target_die, target_cu);
18908 prop->set_addr_offset (baton);
18909 break;
18910 }
18911 }
18912 }
18913 else if (attr->form_is_constant ())
18914 prop->set_const_val (attr->constant_value (0));
18915 else
18916 {
18917 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18918 dwarf2_name (die, cu));
18919 return 0;
18920 }
18921
18922 return 1;
18923 }
18924
18925 /* See read.h. */
18926
18927 struct type *
18928 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
18929 {
18930 struct type *int_type;
18931
18932 /* Helper macro to examine the various builtin types. */
18933 #define TRY_TYPE(F) \
18934 int_type = (unsigned_p \
18935 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18936 : objfile_type (objfile)->builtin_ ## F); \
18937 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
18938 return int_type
18939
18940 TRY_TYPE (char);
18941 TRY_TYPE (short);
18942 TRY_TYPE (int);
18943 TRY_TYPE (long);
18944 TRY_TYPE (long_long);
18945
18946 #undef TRY_TYPE
18947
18948 gdb_assert_not_reached ("unable to find suitable integer type");
18949 }
18950
18951 /* See read.h. */
18952
18953 struct type *
18954 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
18955 {
18956 int addr_size = this->per_cu->addr_size ();
18957 return this->per_objfile->int_type (addr_size, unsigned_p);
18958 }
18959
18960 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18961 present (which is valid) then compute the default type based on the
18962 compilation units address size. */
18963
18964 static struct type *
18965 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18966 {
18967 struct type *index_type = die_type (die, cu);
18968
18969 /* Dwarf-2 specifications explicitly allows to create subrange types
18970 without specifying a base type.
18971 In that case, the base type must be set to the type of
18972 the lower bound, upper bound or count, in that order, if any of these
18973 three attributes references an object that has a type.
18974 If no base type is found, the Dwarf-2 specifications say that
18975 a signed integer type of size equal to the size of an address should
18976 be used.
18977 For the following C code: `extern char gdb_int [];'
18978 GCC produces an empty range DIE.
18979 FIXME: muller/2010-05-28: Possible references to object for low bound,
18980 high bound or count are not yet handled by this code. */
18981 if (index_type->code () == TYPE_CODE_VOID)
18982 index_type = cu->addr_sized_int_type (false);
18983
18984 return index_type;
18985 }
18986
18987 /* Read the given DW_AT_subrange DIE. */
18988
18989 static struct type *
18990 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18991 {
18992 struct type *base_type, *orig_base_type;
18993 struct type *range_type;
18994 struct attribute *attr;
18995 struct dynamic_prop low, high;
18996 int low_default_is_valid;
18997 int high_bound_is_count = 0;
18998 const char *name;
18999 ULONGEST negative_mask;
19000
19001 orig_base_type = read_subrange_index_type (die, cu);
19002
19003 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
19004 whereas the real type might be. So, we use ORIG_BASE_TYPE when
19005 creating the range type, but we use the result of check_typedef
19006 when examining properties of the type. */
19007 base_type = check_typedef (orig_base_type);
19008
19009 /* The die_type call above may have already set the type for this DIE. */
19010 range_type = get_die_type (die, cu);
19011 if (range_type)
19012 return range_type;
19013
19014 high.set_const_val (0);
19015
19016 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
19017 omitting DW_AT_lower_bound. */
19018 switch (cu->language)
19019 {
19020 case language_c:
19021 case language_cplus:
19022 low.set_const_val (0);
19023 low_default_is_valid = 1;
19024 break;
19025 case language_fortran:
19026 low.set_const_val (1);
19027 low_default_is_valid = 1;
19028 break;
19029 case language_d:
19030 case language_objc:
19031 case language_rust:
19032 low.set_const_val (0);
19033 low_default_is_valid = (cu->header.version >= 4);
19034 break;
19035 case language_ada:
19036 case language_m2:
19037 case language_pascal:
19038 low.set_const_val (1);
19039 low_default_is_valid = (cu->header.version >= 4);
19040 break;
19041 default:
19042 low.set_const_val (0);
19043 low_default_is_valid = 0;
19044 break;
19045 }
19046
19047 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
19048 if (attr != nullptr)
19049 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
19050 else if (!low_default_is_valid)
19051 complaint (_("Missing DW_AT_lower_bound "
19052 "- DIE at %s [in module %s]"),
19053 sect_offset_str (die->sect_off),
19054 objfile_name (cu->per_objfile->objfile));
19055
19056 struct attribute *attr_ub, *attr_count;
19057 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
19058 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
19059 {
19060 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
19061 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
19062 {
19063 /* If bounds are constant do the final calculation here. */
19064 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
19065 high.set_const_val (low.const_val () + high.const_val () - 1);
19066 else
19067 high_bound_is_count = 1;
19068 }
19069 else
19070 {
19071 if (attr_ub != NULL)
19072 complaint (_("Unresolved DW_AT_upper_bound "
19073 "- DIE at %s [in module %s]"),
19074 sect_offset_str (die->sect_off),
19075 objfile_name (cu->per_objfile->objfile));
19076 if (attr_count != NULL)
19077 complaint (_("Unresolved DW_AT_count "
19078 "- DIE at %s [in module %s]"),
19079 sect_offset_str (die->sect_off),
19080 objfile_name (cu->per_objfile->objfile));
19081 }
19082 }
19083
19084 LONGEST bias = 0;
19085 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
19086 if (bias_attr != nullptr && bias_attr->form_is_constant ())
19087 bias = bias_attr->constant_value (0);
19088
19089 /* Normally, the DWARF producers are expected to use a signed
19090 constant form (Eg. DW_FORM_sdata) to express negative bounds.
19091 But this is unfortunately not always the case, as witnessed
19092 with GCC, for instance, where the ambiguous DW_FORM_dataN form
19093 is used instead. To work around that ambiguity, we treat
19094 the bounds as signed, and thus sign-extend their values, when
19095 the base type is signed. */
19096 negative_mask =
19097 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
19098 if (low.kind () == PROP_CONST
19099 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
19100 low.set_const_val (low.const_val () | negative_mask);
19101 if (high.kind () == PROP_CONST
19102 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
19103 high.set_const_val (high.const_val () | negative_mask);
19104
19105 /* Check for bit and byte strides. */
19106 struct dynamic_prop byte_stride_prop;
19107 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
19108 if (attr_byte_stride != nullptr)
19109 {
19110 struct type *prop_type = cu->addr_sized_int_type (false);
19111 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
19112 prop_type);
19113 }
19114
19115 struct dynamic_prop bit_stride_prop;
19116 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
19117 if (attr_bit_stride != nullptr)
19118 {
19119 /* It only makes sense to have either a bit or byte stride. */
19120 if (attr_byte_stride != nullptr)
19121 {
19122 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
19123 "- DIE at %s [in module %s]"),
19124 sect_offset_str (die->sect_off),
19125 objfile_name (cu->per_objfile->objfile));
19126 attr_bit_stride = nullptr;
19127 }
19128 else
19129 {
19130 struct type *prop_type = cu->addr_sized_int_type (false);
19131 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
19132 prop_type);
19133 }
19134 }
19135
19136 if (attr_byte_stride != nullptr
19137 || attr_bit_stride != nullptr)
19138 {
19139 bool byte_stride_p = (attr_byte_stride != nullptr);
19140 struct dynamic_prop *stride
19141 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
19142
19143 range_type
19144 = create_range_type_with_stride (NULL, orig_base_type, &low,
19145 &high, bias, stride, byte_stride_p);
19146 }
19147 else
19148 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
19149
19150 if (high_bound_is_count)
19151 range_type->bounds ()->flag_upper_bound_is_count = 1;
19152
19153 /* Ada expects an empty array on no boundary attributes. */
19154 if (attr == NULL && cu->language != language_ada)
19155 range_type->bounds ()->high.set_undefined ();
19156
19157 name = dwarf2_name (die, cu);
19158 if (name)
19159 range_type->set_name (name);
19160
19161 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
19162 if (attr != nullptr)
19163 TYPE_LENGTH (range_type) = attr->constant_value (0);
19164
19165 maybe_set_alignment (cu, die, range_type);
19166
19167 set_die_type (die, range_type, cu);
19168
19169 /* set_die_type should be already done. */
19170 set_descriptive_type (range_type, die, cu);
19171
19172 return range_type;
19173 }
19174
19175 static struct type *
19176 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
19177 {
19178 struct type *type;
19179
19180 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
19181 type->set_name (dwarf2_name (die, cu));
19182
19183 /* In Ada, an unspecified type is typically used when the description
19184 of the type is deferred to a different unit. When encountering
19185 such a type, we treat it as a stub, and try to resolve it later on,
19186 when needed. */
19187 if (cu->language == language_ada)
19188 type->set_is_stub (true);
19189
19190 return set_die_type (die, type, cu);
19191 }
19192
19193 /* Read a single die and all its descendents. Set the die's sibling
19194 field to NULL; set other fields in the die correctly, and set all
19195 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
19196 location of the info_ptr after reading all of those dies. PARENT
19197 is the parent of the die in question. */
19198
19199 static struct die_info *
19200 read_die_and_children (const struct die_reader_specs *reader,
19201 const gdb_byte *info_ptr,
19202 const gdb_byte **new_info_ptr,
19203 struct die_info *parent)
19204 {
19205 struct die_info *die;
19206 const gdb_byte *cur_ptr;
19207
19208 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
19209 if (die == NULL)
19210 {
19211 *new_info_ptr = cur_ptr;
19212 return NULL;
19213 }
19214 store_in_ref_table (die, reader->cu);
19215
19216 if (die->has_children)
19217 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
19218 else
19219 {
19220 die->child = NULL;
19221 *new_info_ptr = cur_ptr;
19222 }
19223
19224 die->sibling = NULL;
19225 die->parent = parent;
19226 return die;
19227 }
19228
19229 /* Read a die, all of its descendents, and all of its siblings; set
19230 all of the fields of all of the dies correctly. Arguments are as
19231 in read_die_and_children. */
19232
19233 static struct die_info *
19234 read_die_and_siblings_1 (const struct die_reader_specs *reader,
19235 const gdb_byte *info_ptr,
19236 const gdb_byte **new_info_ptr,
19237 struct die_info *parent)
19238 {
19239 struct die_info *first_die, *last_sibling;
19240 const gdb_byte *cur_ptr;
19241
19242 cur_ptr = info_ptr;
19243 first_die = last_sibling = NULL;
19244
19245 while (1)
19246 {
19247 struct die_info *die
19248 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
19249
19250 if (die == NULL)
19251 {
19252 *new_info_ptr = cur_ptr;
19253 return first_die;
19254 }
19255
19256 if (!first_die)
19257 first_die = die;
19258 else
19259 last_sibling->sibling = die;
19260
19261 last_sibling = die;
19262 }
19263 }
19264
19265 /* Read a die, all of its descendents, and all of its siblings; set
19266 all of the fields of all of the dies correctly. Arguments are as
19267 in read_die_and_children.
19268 This the main entry point for reading a DIE and all its children. */
19269
19270 static struct die_info *
19271 read_die_and_siblings (const struct die_reader_specs *reader,
19272 const gdb_byte *info_ptr,
19273 const gdb_byte **new_info_ptr,
19274 struct die_info *parent)
19275 {
19276 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
19277 new_info_ptr, parent);
19278
19279 if (dwarf_die_debug)
19280 {
19281 fprintf_unfiltered (gdb_stdlog,
19282 "Read die from %s@0x%x of %s:\n",
19283 reader->die_section->get_name (),
19284 (unsigned) (info_ptr - reader->die_section->buffer),
19285 bfd_get_filename (reader->abfd));
19286 dump_die (die, dwarf_die_debug);
19287 }
19288
19289 return die;
19290 }
19291
19292 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
19293 attributes.
19294 The caller is responsible for filling in the extra attributes
19295 and updating (*DIEP)->num_attrs.
19296 Set DIEP to point to a newly allocated die with its information,
19297 except for its child, sibling, and parent fields. */
19298
19299 static const gdb_byte *
19300 read_full_die_1 (const struct die_reader_specs *reader,
19301 struct die_info **diep, const gdb_byte *info_ptr,
19302 int num_extra_attrs)
19303 {
19304 unsigned int abbrev_number, bytes_read, i;
19305 const struct abbrev_info *abbrev;
19306 struct die_info *die;
19307 struct dwarf2_cu *cu = reader->cu;
19308 bfd *abfd = reader->abfd;
19309
19310 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
19311 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19312 info_ptr += bytes_read;
19313 if (!abbrev_number)
19314 {
19315 *diep = NULL;
19316 return info_ptr;
19317 }
19318
19319 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
19320 if (!abbrev)
19321 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
19322 abbrev_number,
19323 bfd_get_filename (abfd));
19324
19325 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
19326 die->sect_off = sect_off;
19327 die->tag = abbrev->tag;
19328 die->abbrev = abbrev_number;
19329 die->has_children = abbrev->has_children;
19330
19331 /* Make the result usable.
19332 The caller needs to update num_attrs after adding the extra
19333 attributes. */
19334 die->num_attrs = abbrev->num_attrs;
19335
19336 bool any_need_reprocess = false;
19337 for (i = 0; i < abbrev->num_attrs; ++i)
19338 {
19339 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
19340 info_ptr);
19341 if (die->attrs[i].requires_reprocessing_p ())
19342 any_need_reprocess = true;
19343 }
19344
19345 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
19346 if (attr != nullptr && attr->form_is_unsigned ())
19347 cu->str_offsets_base = attr->as_unsigned ();
19348
19349 attr = die->attr (DW_AT_loclists_base);
19350 if (attr != nullptr)
19351 cu->loclist_base = attr->as_unsigned ();
19352
19353 auto maybe_addr_base = die->addr_base ();
19354 if (maybe_addr_base.has_value ())
19355 cu->addr_base = *maybe_addr_base;
19356
19357 attr = die->attr (DW_AT_rnglists_base);
19358 if (attr != nullptr)
19359 cu->rnglists_base = attr->as_unsigned ();
19360
19361 if (any_need_reprocess)
19362 {
19363 for (i = 0; i < abbrev->num_attrs; ++i)
19364 {
19365 if (die->attrs[i].requires_reprocessing_p ())
19366 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
19367 }
19368 }
19369 *diep = die;
19370 return info_ptr;
19371 }
19372
19373 /* Read a die and all its attributes.
19374 Set DIEP to point to a newly allocated die with its information,
19375 except for its child, sibling, and parent fields. */
19376
19377 static const gdb_byte *
19378 read_full_die (const struct die_reader_specs *reader,
19379 struct die_info **diep, const gdb_byte *info_ptr)
19380 {
19381 const gdb_byte *result;
19382
19383 result = read_full_die_1 (reader, diep, info_ptr, 0);
19384
19385 if (dwarf_die_debug)
19386 {
19387 fprintf_unfiltered (gdb_stdlog,
19388 "Read die from %s@0x%x of %s:\n",
19389 reader->die_section->get_name (),
19390 (unsigned) (info_ptr - reader->die_section->buffer),
19391 bfd_get_filename (reader->abfd));
19392 dump_die (*diep, dwarf_die_debug);
19393 }
19394
19395 return result;
19396 }
19397 \f
19398
19399 /* Returns nonzero if TAG represents a type that we might generate a partial
19400 symbol for. */
19401
19402 static int
19403 is_type_tag_for_partial (int tag, enum language lang)
19404 {
19405 switch (tag)
19406 {
19407 #if 0
19408 /* Some types that would be reasonable to generate partial symbols for,
19409 that we don't at present. Note that normally this does not
19410 matter, mainly because C compilers don't give names to these
19411 types, but instead emit DW_TAG_typedef. */
19412 case DW_TAG_file_type:
19413 case DW_TAG_ptr_to_member_type:
19414 case DW_TAG_set_type:
19415 case DW_TAG_string_type:
19416 case DW_TAG_subroutine_type:
19417 #endif
19418
19419 /* GNAT may emit an array with a name, but no typedef, so we
19420 need to make a symbol in this case. */
19421 case DW_TAG_array_type:
19422 return lang == language_ada;
19423
19424 case DW_TAG_base_type:
19425 case DW_TAG_class_type:
19426 case DW_TAG_interface_type:
19427 case DW_TAG_enumeration_type:
19428 case DW_TAG_structure_type:
19429 case DW_TAG_subrange_type:
19430 case DW_TAG_typedef:
19431 case DW_TAG_union_type:
19432 return 1;
19433 default:
19434 return 0;
19435 }
19436 }
19437
19438 /* Load all DIEs that are interesting for partial symbols into memory. */
19439
19440 static struct partial_die_info *
19441 load_partial_dies (const struct die_reader_specs *reader,
19442 const gdb_byte *info_ptr, int building_psymtab)
19443 {
19444 struct dwarf2_cu *cu = reader->cu;
19445 struct objfile *objfile = cu->per_objfile->objfile;
19446 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
19447 unsigned int bytes_read;
19448 unsigned int load_all = 0;
19449 int nesting_level = 1;
19450
19451 parent_die = NULL;
19452 last_die = NULL;
19453
19454 gdb_assert (cu->per_cu != NULL);
19455 if (cu->per_cu->load_all_dies)
19456 load_all = 1;
19457
19458 cu->partial_dies
19459 = htab_create_alloc_ex (cu->header.length / 12,
19460 partial_die_hash,
19461 partial_die_eq,
19462 NULL,
19463 &cu->comp_unit_obstack,
19464 hashtab_obstack_allocate,
19465 dummy_obstack_deallocate);
19466
19467 while (1)
19468 {
19469 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
19470 &bytes_read);
19471
19472 /* A NULL abbrev means the end of a series of children. */
19473 if (abbrev == NULL)
19474 {
19475 if (--nesting_level == 0)
19476 return first_die;
19477
19478 info_ptr += bytes_read;
19479 last_die = parent_die;
19480 parent_die = parent_die->die_parent;
19481 continue;
19482 }
19483
19484 /* Check for template arguments. We never save these; if
19485 they're seen, we just mark the parent, and go on our way. */
19486 if (parent_die != NULL
19487 && cu->language == language_cplus
19488 && (abbrev->tag == DW_TAG_template_type_param
19489 || abbrev->tag == DW_TAG_template_value_param))
19490 {
19491 parent_die->has_template_arguments = 1;
19492
19493 if (!load_all)
19494 {
19495 /* We don't need a partial DIE for the template argument. */
19496 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19497 continue;
19498 }
19499 }
19500
19501 /* We only recurse into c++ subprograms looking for template arguments.
19502 Skip their other children. */
19503 if (!load_all
19504 && cu->language == language_cplus
19505 && parent_die != NULL
19506 && parent_die->tag == DW_TAG_subprogram
19507 && abbrev->tag != DW_TAG_inlined_subroutine)
19508 {
19509 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19510 continue;
19511 }
19512
19513 /* Check whether this DIE is interesting enough to save. Normally
19514 we would not be interested in members here, but there may be
19515 later variables referencing them via DW_AT_specification (for
19516 static members). */
19517 if (!load_all
19518 && !is_type_tag_for_partial (abbrev->tag, cu->language)
19519 && abbrev->tag != DW_TAG_constant
19520 && abbrev->tag != DW_TAG_enumerator
19521 && abbrev->tag != DW_TAG_subprogram
19522 && abbrev->tag != DW_TAG_inlined_subroutine
19523 && abbrev->tag != DW_TAG_lexical_block
19524 && abbrev->tag != DW_TAG_variable
19525 && abbrev->tag != DW_TAG_namespace
19526 && abbrev->tag != DW_TAG_module
19527 && abbrev->tag != DW_TAG_member
19528 && abbrev->tag != DW_TAG_imported_unit
19529 && abbrev->tag != DW_TAG_imported_declaration)
19530 {
19531 /* Otherwise we skip to the next sibling, if any. */
19532 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19533 continue;
19534 }
19535
19536 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19537 abbrev);
19538
19539 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
19540
19541 /* This two-pass algorithm for processing partial symbols has a
19542 high cost in cache pressure. Thus, handle some simple cases
19543 here which cover the majority of C partial symbols. DIEs
19544 which neither have specification tags in them, nor could have
19545 specification tags elsewhere pointing at them, can simply be
19546 processed and discarded.
19547
19548 This segment is also optional; scan_partial_symbols and
19549 add_partial_symbol will handle these DIEs if we chain
19550 them in normally. When compilers which do not emit large
19551 quantities of duplicate debug information are more common,
19552 this code can probably be removed. */
19553
19554 /* Any complete simple types at the top level (pretty much all
19555 of them, for a language without namespaces), can be processed
19556 directly. */
19557 if (parent_die == NULL
19558 && pdi.has_specification == 0
19559 && pdi.is_declaration == 0
19560 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19561 || pdi.tag == DW_TAG_base_type
19562 || pdi.tag == DW_TAG_array_type
19563 || pdi.tag == DW_TAG_subrange_type))
19564 {
19565 if (building_psymtab && pdi.raw_name != NULL)
19566 add_partial_symbol (&pdi, cu);
19567
19568 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19569 continue;
19570 }
19571
19572 /* The exception for DW_TAG_typedef with has_children above is
19573 a workaround of GCC PR debug/47510. In the case of this complaint
19574 type_name_or_error will error on such types later.
19575
19576 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19577 it could not find the child DIEs referenced later, this is checked
19578 above. In correct DWARF DW_TAG_typedef should have no children. */
19579
19580 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
19581 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
19582 "- DIE at %s [in module %s]"),
19583 sect_offset_str (pdi.sect_off), objfile_name (objfile));
19584
19585 /* If we're at the second level, and we're an enumerator, and
19586 our parent has no specification (meaning possibly lives in a
19587 namespace elsewhere), then we can add the partial symbol now
19588 instead of queueing it. */
19589 if (pdi.tag == DW_TAG_enumerator
19590 && parent_die != NULL
19591 && parent_die->die_parent == NULL
19592 && parent_die->tag == DW_TAG_enumeration_type
19593 && parent_die->has_specification == 0)
19594 {
19595 if (pdi.raw_name == NULL)
19596 complaint (_("malformed enumerator DIE ignored"));
19597 else if (building_psymtab)
19598 add_partial_symbol (&pdi, cu);
19599
19600 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19601 continue;
19602 }
19603
19604 struct partial_die_info *part_die
19605 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
19606
19607 /* We'll save this DIE so link it in. */
19608 part_die->die_parent = parent_die;
19609 part_die->die_sibling = NULL;
19610 part_die->die_child = NULL;
19611
19612 if (last_die && last_die == parent_die)
19613 last_die->die_child = part_die;
19614 else if (last_die)
19615 last_die->die_sibling = part_die;
19616
19617 last_die = part_die;
19618
19619 if (first_die == NULL)
19620 first_die = part_die;
19621
19622 /* Maybe add the DIE to the hash table. Not all DIEs that we
19623 find interesting need to be in the hash table, because we
19624 also have the parent/sibling/child chains; only those that we
19625 might refer to by offset later during partial symbol reading.
19626
19627 For now this means things that might have be the target of a
19628 DW_AT_specification, DW_AT_abstract_origin, or
19629 DW_AT_extension. DW_AT_extension will refer only to
19630 namespaces; DW_AT_abstract_origin refers to functions (and
19631 many things under the function DIE, but we do not recurse
19632 into function DIEs during partial symbol reading) and
19633 possibly variables as well; DW_AT_specification refers to
19634 declarations. Declarations ought to have the DW_AT_declaration
19635 flag. It happens that GCC forgets to put it in sometimes, but
19636 only for functions, not for types.
19637
19638 Adding more things than necessary to the hash table is harmless
19639 except for the performance cost. Adding too few will result in
19640 wasted time in find_partial_die, when we reread the compilation
19641 unit with load_all_dies set. */
19642
19643 if (load_all
19644 || abbrev->tag == DW_TAG_constant
19645 || abbrev->tag == DW_TAG_subprogram
19646 || abbrev->tag == DW_TAG_variable
19647 || abbrev->tag == DW_TAG_namespace
19648 || part_die->is_declaration)
19649 {
19650 void **slot;
19651
19652 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
19653 to_underlying (part_die->sect_off),
19654 INSERT);
19655 *slot = part_die;
19656 }
19657
19658 /* For some DIEs we want to follow their children (if any). For C
19659 we have no reason to follow the children of structures; for other
19660 languages we have to, so that we can get at method physnames
19661 to infer fully qualified class names, for DW_AT_specification,
19662 and for C++ template arguments. For C++, we also look one level
19663 inside functions to find template arguments (if the name of the
19664 function does not already contain the template arguments).
19665
19666 For Ada and Fortran, we need to scan the children of subprograms
19667 and lexical blocks as well because these languages allow the
19668 definition of nested entities that could be interesting for the
19669 debugger, such as nested subprograms for instance. */
19670 if (last_die->has_children
19671 && (load_all
19672 || last_die->tag == DW_TAG_namespace
19673 || last_die->tag == DW_TAG_module
19674 || last_die->tag == DW_TAG_enumeration_type
19675 || (cu->language == language_cplus
19676 && last_die->tag == DW_TAG_subprogram
19677 && (last_die->raw_name == NULL
19678 || strchr (last_die->raw_name, '<') == NULL))
19679 || (cu->language != language_c
19680 && (last_die->tag == DW_TAG_class_type
19681 || last_die->tag == DW_TAG_interface_type
19682 || last_die->tag == DW_TAG_structure_type
19683 || last_die->tag == DW_TAG_union_type))
19684 || ((cu->language == language_ada
19685 || cu->language == language_fortran)
19686 && (last_die->tag == DW_TAG_subprogram
19687 || last_die->tag == DW_TAG_lexical_block))))
19688 {
19689 nesting_level++;
19690 parent_die = last_die;
19691 continue;
19692 }
19693
19694 /* Otherwise we skip to the next sibling, if any. */
19695 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
19696
19697 /* Back to the top, do it again. */
19698 }
19699 }
19700
19701 partial_die_info::partial_die_info (sect_offset sect_off_,
19702 const struct abbrev_info *abbrev)
19703 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19704 {
19705 }
19706
19707 /* See class definition. */
19708
19709 const char *
19710 partial_die_info::name (dwarf2_cu *cu)
19711 {
19712 if (!canonical_name && raw_name != nullptr)
19713 {
19714 struct objfile *objfile = cu->per_objfile->objfile;
19715 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19716 canonical_name = 1;
19717 }
19718
19719 return raw_name;
19720 }
19721
19722 /* Read a minimal amount of information into the minimal die structure.
19723 INFO_PTR should point just after the initial uleb128 of a DIE. */
19724
19725 const gdb_byte *
19726 partial_die_info::read (const struct die_reader_specs *reader,
19727 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
19728 {
19729 struct dwarf2_cu *cu = reader->cu;
19730 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19731 unsigned int i;
19732 int has_low_pc_attr = 0;
19733 int has_high_pc_attr = 0;
19734 int high_pc_relative = 0;
19735
19736 for (i = 0; i < abbrev.num_attrs; ++i)
19737 {
19738 attribute attr;
19739 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19740 /* String and address offsets that need to do the reprocessing have
19741 already been read at this point, so there is no need to wait until
19742 the loop terminates to do the reprocessing. */
19743 if (attr.requires_reprocessing_p ())
19744 read_attribute_reprocess (reader, &attr, tag);
19745 /* Store the data if it is of an attribute we want to keep in a
19746 partial symbol table. */
19747 switch (attr.name)
19748 {
19749 case DW_AT_name:
19750 switch (tag)
19751 {
19752 case DW_TAG_compile_unit:
19753 case DW_TAG_partial_unit:
19754 case DW_TAG_type_unit:
19755 /* Compilation units have a DW_AT_name that is a filename, not
19756 a source language identifier. */
19757 case DW_TAG_enumeration_type:
19758 case DW_TAG_enumerator:
19759 /* These tags always have simple identifiers already; no need
19760 to canonicalize them. */
19761 canonical_name = 1;
19762 raw_name = attr.as_string ();
19763 break;
19764 default:
19765 canonical_name = 0;
19766 raw_name = attr.as_string ();
19767 break;
19768 }
19769 break;
19770 case DW_AT_linkage_name:
19771 case DW_AT_MIPS_linkage_name:
19772 /* Note that both forms of linkage name might appear. We
19773 assume they will be the same, and we only store the last
19774 one we see. */
19775 linkage_name = attr.as_string ();
19776 break;
19777 case DW_AT_low_pc:
19778 has_low_pc_attr = 1;
19779 lowpc = attr.as_address ();
19780 break;
19781 case DW_AT_high_pc:
19782 has_high_pc_attr = 1;
19783 highpc = attr.as_address ();
19784 if (cu->header.version >= 4 && attr.form_is_constant ())
19785 high_pc_relative = 1;
19786 break;
19787 case DW_AT_location:
19788 /* Support the .debug_loc offsets. */
19789 if (attr.form_is_block ())
19790 {
19791 d.locdesc = attr.as_block ();
19792 }
19793 else if (attr.form_is_section_offset ())
19794 {
19795 dwarf2_complex_location_expr_complaint ();
19796 }
19797 else
19798 {
19799 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19800 "partial symbol information");
19801 }
19802 break;
19803 case DW_AT_external:
19804 is_external = attr.as_boolean ();
19805 break;
19806 case DW_AT_declaration:
19807 is_declaration = attr.as_boolean ();
19808 break;
19809 case DW_AT_type:
19810 has_type = 1;
19811 break;
19812 case DW_AT_abstract_origin:
19813 case DW_AT_specification:
19814 case DW_AT_extension:
19815 has_specification = 1;
19816 spec_offset = attr.get_ref_die_offset ();
19817 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19818 || cu->per_cu->is_dwz);
19819 break;
19820 case DW_AT_sibling:
19821 /* Ignore absolute siblings, they might point outside of
19822 the current compile unit. */
19823 if (attr.form == DW_FORM_ref_addr)
19824 complaint (_("ignoring absolute DW_AT_sibling"));
19825 else
19826 {
19827 const gdb_byte *buffer = reader->buffer;
19828 sect_offset off = attr.get_ref_die_offset ();
19829 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19830
19831 if (sibling_ptr < info_ptr)
19832 complaint (_("DW_AT_sibling points backwards"));
19833 else if (sibling_ptr > reader->buffer_end)
19834 reader->die_section->overflow_complaint ();
19835 else
19836 sibling = sibling_ptr;
19837 }
19838 break;
19839 case DW_AT_byte_size:
19840 has_byte_size = 1;
19841 break;
19842 case DW_AT_const_value:
19843 has_const_value = 1;
19844 break;
19845 case DW_AT_calling_convention:
19846 /* DWARF doesn't provide a way to identify a program's source-level
19847 entry point. DW_AT_calling_convention attributes are only meant
19848 to describe functions' calling conventions.
19849
19850 However, because it's a necessary piece of information in
19851 Fortran, and before DWARF 4 DW_CC_program was the only
19852 piece of debugging information whose definition refers to
19853 a 'main program' at all, several compilers marked Fortran
19854 main programs with DW_CC_program --- even when those
19855 functions use the standard calling conventions.
19856
19857 Although DWARF now specifies a way to provide this
19858 information, we support this practice for backward
19859 compatibility. */
19860 if (attr.constant_value (0) == DW_CC_program
19861 && cu->language == language_fortran)
19862 main_subprogram = 1;
19863 break;
19864 case DW_AT_inline:
19865 {
19866 LONGEST value = attr.constant_value (-1);
19867 if (value == DW_INL_inlined
19868 || value == DW_INL_declared_inlined)
19869 may_be_inlined = 1;
19870 }
19871 break;
19872
19873 case DW_AT_import:
19874 if (tag == DW_TAG_imported_unit)
19875 {
19876 d.sect_off = attr.get_ref_die_offset ();
19877 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19878 || cu->per_cu->is_dwz);
19879 }
19880 break;
19881
19882 case DW_AT_main_subprogram:
19883 main_subprogram = attr.as_boolean ();
19884 break;
19885
19886 case DW_AT_ranges:
19887 {
19888 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19889 on DWARF version). */
19890 ULONGEST ranges_offset = attr.as_unsigned ();
19891
19892 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19893 this value. */
19894 if (tag != DW_TAG_compile_unit)
19895 ranges_offset += cu->gnu_ranges_base;
19896
19897 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19898 nullptr, tag))
19899 has_pc_info = 1;
19900 }
19901 break;
19902
19903 default:
19904 break;
19905 }
19906 }
19907
19908 /* For Ada, if both the name and the linkage name appear, we prefer
19909 the latter. This lets "catch exception" work better, regardless
19910 of the order in which the name and linkage name were emitted.
19911 Really, though, this is just a workaround for the fact that gdb
19912 doesn't store both the name and the linkage name. */
19913 if (cu->language == language_ada && linkage_name != nullptr)
19914 raw_name = linkage_name;
19915
19916 if (high_pc_relative)
19917 highpc += lowpc;
19918
19919 if (has_low_pc_attr && has_high_pc_attr)
19920 {
19921 /* When using the GNU linker, .gnu.linkonce. sections are used to
19922 eliminate duplicate copies of functions and vtables and such.
19923 The linker will arbitrarily choose one and discard the others.
19924 The AT_*_pc values for such functions refer to local labels in
19925 these sections. If the section from that file was discarded, the
19926 labels are not in the output, so the relocs get a value of 0.
19927 If this is a discarded function, mark the pc bounds as invalid,
19928 so that GDB will ignore it. */
19929 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
19930 {
19931 struct objfile *objfile = per_objfile->objfile;
19932 struct gdbarch *gdbarch = objfile->arch ();
19933
19934 complaint (_("DW_AT_low_pc %s is zero "
19935 "for DIE at %s [in module %s]"),
19936 paddress (gdbarch, lowpc),
19937 sect_offset_str (sect_off),
19938 objfile_name (objfile));
19939 }
19940 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19941 else if (lowpc >= highpc)
19942 {
19943 struct objfile *objfile = per_objfile->objfile;
19944 struct gdbarch *gdbarch = objfile->arch ();
19945
19946 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19947 "for DIE at %s [in module %s]"),
19948 paddress (gdbarch, lowpc),
19949 paddress (gdbarch, highpc),
19950 sect_offset_str (sect_off),
19951 objfile_name (objfile));
19952 }
19953 else
19954 has_pc_info = 1;
19955 }
19956
19957 return info_ptr;
19958 }
19959
19960 /* Find a cached partial DIE at OFFSET in CU. */
19961
19962 struct partial_die_info *
19963 dwarf2_cu::find_partial_die (sect_offset sect_off)
19964 {
19965 struct partial_die_info *lookup_die = NULL;
19966 struct partial_die_info part_die (sect_off);
19967
19968 lookup_die = ((struct partial_die_info *)
19969 htab_find_with_hash (partial_dies, &part_die,
19970 to_underlying (sect_off)));
19971
19972 return lookup_die;
19973 }
19974
19975 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19976 except in the case of .debug_types DIEs which do not reference
19977 outside their CU (they do however referencing other types via
19978 DW_FORM_ref_sig8). */
19979
19980 static const struct cu_partial_die_info
19981 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19982 {
19983 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19984 struct objfile *objfile = per_objfile->objfile;
19985 struct partial_die_info *pd = NULL;
19986
19987 if (offset_in_dwz == cu->per_cu->is_dwz
19988 && cu->header.offset_in_cu_p (sect_off))
19989 {
19990 pd = cu->find_partial_die (sect_off);
19991 if (pd != NULL)
19992 return { cu, pd };
19993 /* We missed recording what we needed.
19994 Load all dies and try again. */
19995 }
19996 else
19997 {
19998 /* TUs don't reference other CUs/TUs (except via type signatures). */
19999 if (cu->per_cu->is_debug_types)
20000 {
20001 error (_("Dwarf Error: Type Unit at offset %s contains"
20002 " external reference to offset %s [in module %s].\n"),
20003 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
20004 bfd_get_filename (objfile->obfd));
20005 }
20006 dwarf2_per_cu_data *per_cu
20007 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
20008 per_objfile);
20009
20010 cu = per_objfile->get_cu (per_cu);
20011 if (cu == NULL || cu->partial_dies == NULL)
20012 load_partial_comp_unit (per_cu, per_objfile, nullptr);
20013
20014 cu = per_objfile->get_cu (per_cu);
20015
20016 cu->last_used = 0;
20017 pd = cu->find_partial_die (sect_off);
20018 }
20019
20020 /* If we didn't find it, and not all dies have been loaded,
20021 load them all and try again. */
20022
20023 if (pd == NULL && cu->per_cu->load_all_dies == 0)
20024 {
20025 cu->per_cu->load_all_dies = 1;
20026
20027 /* This is nasty. When we reread the DIEs, somewhere up the call chain
20028 THIS_CU->cu may already be in use. So we can't just free it and
20029 replace its DIEs with the ones we read in. Instead, we leave those
20030 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
20031 and clobber THIS_CU->cu->partial_dies with the hash table for the new
20032 set. */
20033 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
20034
20035 pd = cu->find_partial_die (sect_off);
20036 }
20037
20038 if (pd == NULL)
20039 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
20040 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
20041 return { cu, pd };
20042 }
20043
20044 /* See if we can figure out if the class lives in a namespace. We do
20045 this by looking for a member function; its demangled name will
20046 contain namespace info, if there is any. */
20047
20048 static void
20049 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
20050 struct dwarf2_cu *cu)
20051 {
20052 /* NOTE: carlton/2003-10-07: Getting the info this way changes
20053 what template types look like, because the demangler
20054 frequently doesn't give the same name as the debug info. We
20055 could fix this by only using the demangled name to get the
20056 prefix (but see comment in read_structure_type). */
20057
20058 struct partial_die_info *real_pdi;
20059 struct partial_die_info *child_pdi;
20060
20061 /* If this DIE (this DIE's specification, if any) has a parent, then
20062 we should not do this. We'll prepend the parent's fully qualified
20063 name when we create the partial symbol. */
20064
20065 real_pdi = struct_pdi;
20066 while (real_pdi->has_specification)
20067 {
20068 auto res = find_partial_die (real_pdi->spec_offset,
20069 real_pdi->spec_is_dwz, cu);
20070 real_pdi = res.pdi;
20071 cu = res.cu;
20072 }
20073
20074 if (real_pdi->die_parent != NULL)
20075 return;
20076
20077 for (child_pdi = struct_pdi->die_child;
20078 child_pdi != NULL;
20079 child_pdi = child_pdi->die_sibling)
20080 {
20081 if (child_pdi->tag == DW_TAG_subprogram
20082 && child_pdi->linkage_name != NULL)
20083 {
20084 gdb::unique_xmalloc_ptr<char> actual_class_name
20085 (cu->language_defn->class_name_from_physname
20086 (child_pdi->linkage_name));
20087 if (actual_class_name != NULL)
20088 {
20089 struct objfile *objfile = cu->per_objfile->objfile;
20090 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
20091 struct_pdi->canonical_name = 1;
20092 }
20093 break;
20094 }
20095 }
20096 }
20097
20098 /* Return true if a DIE with TAG may have the DW_AT_const_value
20099 attribute. */
20100
20101 static bool
20102 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
20103 {
20104 switch (tag)
20105 {
20106 case DW_TAG_constant:
20107 case DW_TAG_enumerator:
20108 case DW_TAG_formal_parameter:
20109 case DW_TAG_template_value_param:
20110 case DW_TAG_variable:
20111 return true;
20112 }
20113
20114 return false;
20115 }
20116
20117 void
20118 partial_die_info::fixup (struct dwarf2_cu *cu)
20119 {
20120 /* Once we've fixed up a die, there's no point in doing so again.
20121 This also avoids a memory leak if we were to call
20122 guess_partial_die_structure_name multiple times. */
20123 if (fixup_called)
20124 return;
20125
20126 /* If we found a reference attribute and the DIE has no name, try
20127 to find a name in the referred to DIE. */
20128
20129 if (raw_name == NULL && has_specification)
20130 {
20131 struct partial_die_info *spec_die;
20132
20133 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20134 spec_die = res.pdi;
20135 cu = res.cu;
20136
20137 spec_die->fixup (cu);
20138
20139 if (spec_die->raw_name)
20140 {
20141 raw_name = spec_die->raw_name;
20142 canonical_name = spec_die->canonical_name;
20143
20144 /* Copy DW_AT_external attribute if it is set. */
20145 if (spec_die->is_external)
20146 is_external = spec_die->is_external;
20147 }
20148 }
20149
20150 if (!has_const_value && has_specification
20151 && can_have_DW_AT_const_value_p (tag))
20152 {
20153 struct partial_die_info *spec_die;
20154
20155 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20156 spec_die = res.pdi;
20157 cu = res.cu;
20158
20159 spec_die->fixup (cu);
20160
20161 if (spec_die->has_const_value)
20162 {
20163 /* Copy DW_AT_const_value attribute if it is set. */
20164 has_const_value = spec_die->has_const_value;
20165 }
20166 }
20167
20168 /* Set default names for some unnamed DIEs. */
20169
20170 if (raw_name == NULL && tag == DW_TAG_namespace)
20171 {
20172 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
20173 canonical_name = 1;
20174 }
20175
20176 /* If there is no parent die to provide a namespace, and there are
20177 children, see if we can determine the namespace from their linkage
20178 name. */
20179 if (cu->language == language_cplus
20180 && !cu->per_objfile->per_bfd->types.empty ()
20181 && die_parent == NULL
20182 && has_children
20183 && (tag == DW_TAG_class_type
20184 || tag == DW_TAG_structure_type
20185 || tag == DW_TAG_union_type))
20186 guess_partial_die_structure_name (this, cu);
20187
20188 /* GCC might emit a nameless struct or union that has a linkage
20189 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20190 if (raw_name == NULL
20191 && (tag == DW_TAG_class_type
20192 || tag == DW_TAG_interface_type
20193 || tag == DW_TAG_structure_type
20194 || tag == DW_TAG_union_type)
20195 && linkage_name != NULL)
20196 {
20197 gdb::unique_xmalloc_ptr<char> demangled
20198 (gdb_demangle (linkage_name, DMGL_TYPES));
20199 if (demangled != nullptr)
20200 {
20201 const char *base;
20202
20203 /* Strip any leading namespaces/classes, keep only the base name.
20204 DW_AT_name for named DIEs does not contain the prefixes. */
20205 base = strrchr (demangled.get (), ':');
20206 if (base && base > demangled.get () && base[-1] == ':')
20207 base++;
20208 else
20209 base = demangled.get ();
20210
20211 struct objfile *objfile = cu->per_objfile->objfile;
20212 raw_name = objfile->intern (base);
20213 canonical_name = 1;
20214 }
20215 }
20216
20217 fixup_called = 1;
20218 }
20219
20220 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
20221 contents from the given SECTION in the HEADER.
20222
20223 HEADER_OFFSET is the offset of the header in the section. */
20224 static void
20225 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
20226 struct dwarf2_section_info *section,
20227 sect_offset header_offset)
20228 {
20229 unsigned int bytes_read;
20230 bfd *abfd = section->get_bfd_owner ();
20231 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
20232
20233 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
20234 info_ptr += bytes_read;
20235
20236 header->version = read_2_bytes (abfd, info_ptr);
20237 info_ptr += 2;
20238
20239 header->addr_size = read_1_byte (abfd, info_ptr);
20240 info_ptr += 1;
20241
20242 header->segment_collector_size = read_1_byte (abfd, info_ptr);
20243 info_ptr += 1;
20244
20245 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
20246 }
20247
20248 /* Return the DW_AT_loclists_base value for the CU. */
20249 static ULONGEST
20250 lookup_loclist_base (struct dwarf2_cu *cu)
20251 {
20252 /* For the .dwo unit, the loclist_base points to the first offset following
20253 the header. The header consists of the following entities-
20254 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
20255 bit format)
20256 2. version (2 bytes)
20257 3. address size (1 byte)
20258 4. segment selector size (1 byte)
20259 5. offset entry count (4 bytes)
20260 These sizes are derived as per the DWARFv5 standard. */
20261 if (cu->dwo_unit != nullptr)
20262 {
20263 if (cu->header.initial_length_size == 4)
20264 return LOCLIST_HEADER_SIZE32;
20265 return LOCLIST_HEADER_SIZE64;
20266 }
20267 return cu->loclist_base;
20268 }
20269
20270 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
20271 array of offsets in the .debug_loclists section. */
20272
20273 static sect_offset
20274 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
20275 {
20276 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20277 struct objfile *objfile = per_objfile->objfile;
20278 bfd *abfd = objfile->obfd;
20279 ULONGEST loclist_header_size =
20280 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
20281 : LOCLIST_HEADER_SIZE64);
20282 ULONGEST loclist_base = lookup_loclist_base (cu);
20283
20284 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
20285 ULONGEST start_offset =
20286 loclist_base + loclist_index * cu->header.offset_size;
20287
20288 /* Get loclists section. */
20289 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20290
20291 /* Read the loclists section content. */
20292 section->read (objfile);
20293 if (section->buffer == NULL)
20294 error (_("DW_FORM_loclistx used without .debug_loclists "
20295 "section [in module %s]"), objfile_name (objfile));
20296
20297 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
20298 so if loclist_base is smaller than the header size, we have a problem. */
20299 if (loclist_base < loclist_header_size)
20300 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
20301 objfile_name (objfile));
20302
20303 /* Read the header of the loclists contribution. */
20304 struct loclists_rnglists_header header;
20305 read_loclists_rnglists_header (&header, section,
20306 (sect_offset) (loclist_base - loclist_header_size));
20307
20308 /* Verify the loclist index is valid. */
20309 if (loclist_index >= header.offset_entry_count)
20310 error (_("DW_FORM_loclistx pointing outside of "
20311 ".debug_loclists offset array [in module %s]"),
20312 objfile_name (objfile));
20313
20314 /* Validate that reading won't go beyond the end of the section. */
20315 if (start_offset + cu->header.offset_size > section->size)
20316 error (_("Reading DW_FORM_loclistx index beyond end of"
20317 ".debug_loclists section [in module %s]"),
20318 objfile_name (objfile));
20319
20320 const gdb_byte *info_ptr = section->buffer + start_offset;
20321
20322 if (cu->header.offset_size == 4)
20323 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
20324 else
20325 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
20326 }
20327
20328 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
20329 array of offsets in the .debug_rnglists section. */
20330
20331 static sect_offset
20332 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
20333 dwarf_tag tag)
20334 {
20335 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20336 struct objfile *objfile = dwarf2_per_objfile->objfile;
20337 bfd *abfd = objfile->obfd;
20338 ULONGEST rnglist_header_size =
20339 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
20340 : RNGLIST_HEADER_SIZE64);
20341
20342 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
20343 .debug_rnglists.dwo section. The rnglists base given in the skeleton
20344 doesn't apply. */
20345 ULONGEST rnglist_base =
20346 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
20347
20348 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
20349 ULONGEST start_offset =
20350 rnglist_base + rnglist_index * cu->header.offset_size;
20351
20352 /* Get rnglists section. */
20353 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
20354
20355 /* Read the rnglists section content. */
20356 section->read (objfile);
20357 if (section->buffer == nullptr)
20358 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
20359 "[in module %s]"),
20360 objfile_name (objfile));
20361
20362 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
20363 so if rnglist_base is smaller than the header size, we have a problem. */
20364 if (rnglist_base < rnglist_header_size)
20365 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
20366 objfile_name (objfile));
20367
20368 /* Read the header of the rnglists contribution. */
20369 struct loclists_rnglists_header header;
20370 read_loclists_rnglists_header (&header, section,
20371 (sect_offset) (rnglist_base - rnglist_header_size));
20372
20373 /* Verify the rnglist index is valid. */
20374 if (rnglist_index >= header.offset_entry_count)
20375 error (_("DW_FORM_rnglistx index pointing outside of "
20376 ".debug_rnglists offset array [in module %s]"),
20377 objfile_name (objfile));
20378
20379 /* Validate that reading won't go beyond the end of the section. */
20380 if (start_offset + cu->header.offset_size > section->size)
20381 error (_("Reading DW_FORM_rnglistx index beyond end of"
20382 ".debug_rnglists section [in module %s]"),
20383 objfile_name (objfile));
20384
20385 const gdb_byte *info_ptr = section->buffer + start_offset;
20386
20387 if (cu->header.offset_size == 4)
20388 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
20389 else
20390 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
20391 }
20392
20393 /* Process the attributes that had to be skipped in the first round. These
20394 attributes are the ones that need str_offsets_base or addr_base attributes.
20395 They could not have been processed in the first round, because at the time
20396 the values of str_offsets_base or addr_base may not have been known. */
20397 static void
20398 read_attribute_reprocess (const struct die_reader_specs *reader,
20399 struct attribute *attr, dwarf_tag tag)
20400 {
20401 struct dwarf2_cu *cu = reader->cu;
20402 switch (attr->form)
20403 {
20404 case DW_FORM_addrx:
20405 case DW_FORM_GNU_addr_index:
20406 attr->set_address (read_addr_index (cu,
20407 attr->as_unsigned_reprocess ()));
20408 break;
20409 case DW_FORM_loclistx:
20410 {
20411 sect_offset loclists_sect_off
20412 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
20413
20414 attr->set_unsigned (to_underlying (loclists_sect_off));
20415 }
20416 break;
20417 case DW_FORM_rnglistx:
20418 {
20419 sect_offset rnglists_sect_off
20420 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
20421
20422 attr->set_unsigned (to_underlying (rnglists_sect_off));
20423 }
20424 break;
20425 case DW_FORM_strx:
20426 case DW_FORM_strx1:
20427 case DW_FORM_strx2:
20428 case DW_FORM_strx3:
20429 case DW_FORM_strx4:
20430 case DW_FORM_GNU_str_index:
20431 {
20432 unsigned int str_index = attr->as_unsigned_reprocess ();
20433 gdb_assert (!attr->canonical_string_p ());
20434 if (reader->dwo_file != NULL)
20435 attr->set_string_noncanonical (read_dwo_str_index (reader,
20436 str_index));
20437 else
20438 attr->set_string_noncanonical (read_stub_str_index (cu,
20439 str_index));
20440 break;
20441 }
20442 default:
20443 gdb_assert_not_reached (_("Unexpected DWARF form."));
20444 }
20445 }
20446
20447 /* Read an attribute value described by an attribute form. */
20448
20449 static const gdb_byte *
20450 read_attribute_value (const struct die_reader_specs *reader,
20451 struct attribute *attr, unsigned form,
20452 LONGEST implicit_const, const gdb_byte *info_ptr)
20453 {
20454 struct dwarf2_cu *cu = reader->cu;
20455 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20456 struct objfile *objfile = per_objfile->objfile;
20457 bfd *abfd = reader->abfd;
20458 struct comp_unit_head *cu_header = &cu->header;
20459 unsigned int bytes_read;
20460 struct dwarf_block *blk;
20461
20462 attr->form = (enum dwarf_form) form;
20463 switch (form)
20464 {
20465 case DW_FORM_ref_addr:
20466 if (cu_header->version == 2)
20467 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
20468 &bytes_read));
20469 else
20470 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20471 &bytes_read));
20472 info_ptr += bytes_read;
20473 break;
20474 case DW_FORM_GNU_ref_alt:
20475 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20476 &bytes_read));
20477 info_ptr += bytes_read;
20478 break;
20479 case DW_FORM_addr:
20480 {
20481 struct gdbarch *gdbarch = objfile->arch ();
20482 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
20483 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20484 attr->set_address (addr);
20485 info_ptr += bytes_read;
20486 }
20487 break;
20488 case DW_FORM_block2:
20489 blk = dwarf_alloc_block (cu);
20490 blk->size = read_2_bytes (abfd, info_ptr);
20491 info_ptr += 2;
20492 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20493 info_ptr += blk->size;
20494 attr->set_block (blk);
20495 break;
20496 case DW_FORM_block4:
20497 blk = dwarf_alloc_block (cu);
20498 blk->size = read_4_bytes (abfd, info_ptr);
20499 info_ptr += 4;
20500 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20501 info_ptr += blk->size;
20502 attr->set_block (blk);
20503 break;
20504 case DW_FORM_data2:
20505 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
20506 info_ptr += 2;
20507 break;
20508 case DW_FORM_data4:
20509 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
20510 info_ptr += 4;
20511 break;
20512 case DW_FORM_data8:
20513 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
20514 info_ptr += 8;
20515 break;
20516 case DW_FORM_data16:
20517 blk = dwarf_alloc_block (cu);
20518 blk->size = 16;
20519 blk->data = read_n_bytes (abfd, info_ptr, 16);
20520 info_ptr += 16;
20521 attr->set_block (blk);
20522 break;
20523 case DW_FORM_sec_offset:
20524 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20525 &bytes_read));
20526 info_ptr += bytes_read;
20527 break;
20528 case DW_FORM_loclistx:
20529 {
20530 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20531 &bytes_read));
20532 info_ptr += bytes_read;
20533 }
20534 break;
20535 case DW_FORM_string:
20536 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20537 &bytes_read));
20538 info_ptr += bytes_read;
20539 break;
20540 case DW_FORM_strp:
20541 if (!cu->per_cu->is_dwz)
20542 {
20543 attr->set_string_noncanonical
20544 (read_indirect_string (per_objfile,
20545 abfd, info_ptr, cu_header,
20546 &bytes_read));
20547 info_ptr += bytes_read;
20548 break;
20549 }
20550 /* FALLTHROUGH */
20551 case DW_FORM_line_strp:
20552 if (!cu->per_cu->is_dwz)
20553 {
20554 attr->set_string_noncanonical
20555 (per_objfile->read_line_string (info_ptr, cu_header,
20556 &bytes_read));
20557 info_ptr += bytes_read;
20558 break;
20559 }
20560 /* FALLTHROUGH */
20561 case DW_FORM_GNU_strp_alt:
20562 {
20563 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20564 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20565 &bytes_read);
20566
20567 attr->set_string_noncanonical
20568 (dwz->read_string (objfile, str_offset));
20569 info_ptr += bytes_read;
20570 }
20571 break;
20572 case DW_FORM_exprloc:
20573 case DW_FORM_block:
20574 blk = dwarf_alloc_block (cu);
20575 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20576 info_ptr += bytes_read;
20577 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20578 info_ptr += blk->size;
20579 attr->set_block (blk);
20580 break;
20581 case DW_FORM_block1:
20582 blk = dwarf_alloc_block (cu);
20583 blk->size = read_1_byte (abfd, info_ptr);
20584 info_ptr += 1;
20585 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20586 info_ptr += blk->size;
20587 attr->set_block (blk);
20588 break;
20589 case DW_FORM_data1:
20590 case DW_FORM_flag:
20591 attr->set_unsigned (read_1_byte (abfd, info_ptr));
20592 info_ptr += 1;
20593 break;
20594 case DW_FORM_flag_present:
20595 attr->set_unsigned (1);
20596 break;
20597 case DW_FORM_sdata:
20598 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
20599 info_ptr += bytes_read;
20600 break;
20601 case DW_FORM_rnglistx:
20602 {
20603 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20604 &bytes_read));
20605 info_ptr += bytes_read;
20606 }
20607 break;
20608 case DW_FORM_udata:
20609 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
20610 info_ptr += bytes_read;
20611 break;
20612 case DW_FORM_ref1:
20613 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20614 + read_1_byte (abfd, info_ptr)));
20615 info_ptr += 1;
20616 break;
20617 case DW_FORM_ref2:
20618 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20619 + read_2_bytes (abfd, info_ptr)));
20620 info_ptr += 2;
20621 break;
20622 case DW_FORM_ref4:
20623 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20624 + read_4_bytes (abfd, info_ptr)));
20625 info_ptr += 4;
20626 break;
20627 case DW_FORM_ref8:
20628 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20629 + read_8_bytes (abfd, info_ptr)));
20630 info_ptr += 8;
20631 break;
20632 case DW_FORM_ref_sig8:
20633 attr->set_signature (read_8_bytes (abfd, info_ptr));
20634 info_ptr += 8;
20635 break;
20636 case DW_FORM_ref_udata:
20637 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20638 + read_unsigned_leb128 (abfd, info_ptr,
20639 &bytes_read)));
20640 info_ptr += bytes_read;
20641 break;
20642 case DW_FORM_indirect:
20643 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20644 info_ptr += bytes_read;
20645 if (form == DW_FORM_implicit_const)
20646 {
20647 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20648 info_ptr += bytes_read;
20649 }
20650 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
20651 info_ptr);
20652 break;
20653 case DW_FORM_implicit_const:
20654 attr->set_signed (implicit_const);
20655 break;
20656 case DW_FORM_addrx:
20657 case DW_FORM_GNU_addr_index:
20658 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20659 &bytes_read));
20660 info_ptr += bytes_read;
20661 break;
20662 case DW_FORM_strx:
20663 case DW_FORM_strx1:
20664 case DW_FORM_strx2:
20665 case DW_FORM_strx3:
20666 case DW_FORM_strx4:
20667 case DW_FORM_GNU_str_index:
20668 {
20669 ULONGEST str_index;
20670 if (form == DW_FORM_strx1)
20671 {
20672 str_index = read_1_byte (abfd, info_ptr);
20673 info_ptr += 1;
20674 }
20675 else if (form == DW_FORM_strx2)
20676 {
20677 str_index = read_2_bytes (abfd, info_ptr);
20678 info_ptr += 2;
20679 }
20680 else if (form == DW_FORM_strx3)
20681 {
20682 str_index = read_3_bytes (abfd, info_ptr);
20683 info_ptr += 3;
20684 }
20685 else if (form == DW_FORM_strx4)
20686 {
20687 str_index = read_4_bytes (abfd, info_ptr);
20688 info_ptr += 4;
20689 }
20690 else
20691 {
20692 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20693 info_ptr += bytes_read;
20694 }
20695 attr->set_unsigned_reprocess (str_index);
20696 }
20697 break;
20698 default:
20699 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
20700 dwarf_form_name (form),
20701 bfd_get_filename (abfd));
20702 }
20703
20704 /* Super hack. */
20705 if (cu->per_cu->is_dwz && attr->form_is_ref ())
20706 attr->form = DW_FORM_GNU_ref_alt;
20707
20708 /* We have seen instances where the compiler tried to emit a byte
20709 size attribute of -1 which ended up being encoded as an unsigned
20710 0xffffffff. Although 0xffffffff is technically a valid size value,
20711 an object of this size seems pretty unlikely so we can relatively
20712 safely treat these cases as if the size attribute was invalid and
20713 treat them as zero by default. */
20714 if (attr->name == DW_AT_byte_size
20715 && form == DW_FORM_data4
20716 && attr->as_unsigned () >= 0xffffffff)
20717 {
20718 complaint
20719 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20720 hex_string (attr->as_unsigned ()));
20721 attr->set_unsigned (0);
20722 }
20723
20724 return info_ptr;
20725 }
20726
20727 /* Read an attribute described by an abbreviated attribute. */
20728
20729 static const gdb_byte *
20730 read_attribute (const struct die_reader_specs *reader,
20731 struct attribute *attr, const struct attr_abbrev *abbrev,
20732 const gdb_byte *info_ptr)
20733 {
20734 attr->name = abbrev->name;
20735 attr->string_is_canonical = 0;
20736 attr->requires_reprocessing = 0;
20737 return read_attribute_value (reader, attr, abbrev->form,
20738 abbrev->implicit_const, info_ptr);
20739 }
20740
20741 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20742
20743 static const char *
20744 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
20745 LONGEST str_offset)
20746 {
20747 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20748 str_offset, "DW_FORM_strp");
20749 }
20750
20751 /* Return pointer to string at .debug_str offset as read from BUF.
20752 BUF is assumed to be in a compilation unit described by CU_HEADER.
20753 Return *BYTES_READ_PTR count of bytes read from BUF. */
20754
20755 static const char *
20756 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
20757 const gdb_byte *buf,
20758 const struct comp_unit_head *cu_header,
20759 unsigned int *bytes_read_ptr)
20760 {
20761 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20762
20763 return read_indirect_string_at_offset (per_objfile, str_offset);
20764 }
20765
20766 /* See read.h. */
20767
20768 const char *
20769 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20770 const struct comp_unit_head *cu_header,
20771 unsigned int *bytes_read_ptr)
20772 {
20773 bfd *abfd = objfile->obfd;
20774 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20775
20776 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20777 }
20778
20779 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20780 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20781 ADDR_SIZE is the size of addresses from the CU header. */
20782
20783 static CORE_ADDR
20784 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20785 gdb::optional<ULONGEST> addr_base, int addr_size)
20786 {
20787 struct objfile *objfile = per_objfile->objfile;
20788 bfd *abfd = objfile->obfd;
20789 const gdb_byte *info_ptr;
20790 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20791
20792 per_objfile->per_bfd->addr.read (objfile);
20793 if (per_objfile->per_bfd->addr.buffer == NULL)
20794 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20795 objfile_name (objfile));
20796 if (addr_base_or_zero + addr_index * addr_size
20797 >= per_objfile->per_bfd->addr.size)
20798 error (_("DW_FORM_addr_index pointing outside of "
20799 ".debug_addr section [in module %s]"),
20800 objfile_name (objfile));
20801 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20802 + addr_index * addr_size);
20803 if (addr_size == 4)
20804 return bfd_get_32 (abfd, info_ptr);
20805 else
20806 return bfd_get_64 (abfd, info_ptr);
20807 }
20808
20809 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20810
20811 static CORE_ADDR
20812 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20813 {
20814 return read_addr_index_1 (cu->per_objfile, addr_index,
20815 cu->addr_base, cu->header.addr_size);
20816 }
20817
20818 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20819
20820 static CORE_ADDR
20821 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20822 unsigned int *bytes_read)
20823 {
20824 bfd *abfd = cu->per_objfile->objfile->obfd;
20825 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20826
20827 return read_addr_index (cu, addr_index);
20828 }
20829
20830 /* See read.h. */
20831
20832 CORE_ADDR
20833 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20834 dwarf2_per_objfile *per_objfile,
20835 unsigned int addr_index)
20836 {
20837 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20838 gdb::optional<ULONGEST> addr_base;
20839 int addr_size;
20840
20841 /* We need addr_base and addr_size.
20842 If we don't have PER_CU->cu, we have to get it.
20843 Nasty, but the alternative is storing the needed info in PER_CU,
20844 which at this point doesn't seem justified: it's not clear how frequently
20845 it would get used and it would increase the size of every PER_CU.
20846 Entry points like dwarf2_per_cu_addr_size do a similar thing
20847 so we're not in uncharted territory here.
20848 Alas we need to be a bit more complicated as addr_base is contained
20849 in the DIE.
20850
20851 We don't need to read the entire CU(/TU).
20852 We just need the header and top level die.
20853
20854 IWBN to use the aging mechanism to let us lazily later discard the CU.
20855 For now we skip this optimization. */
20856
20857 if (cu != NULL)
20858 {
20859 addr_base = cu->addr_base;
20860 addr_size = cu->header.addr_size;
20861 }
20862 else
20863 {
20864 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
20865 addr_base = reader.cu->addr_base;
20866 addr_size = reader.cu->header.addr_size;
20867 }
20868
20869 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
20870 }
20871
20872 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20873 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20874 DWO file. */
20875
20876 static const char *
20877 read_str_index (struct dwarf2_cu *cu,
20878 struct dwarf2_section_info *str_section,
20879 struct dwarf2_section_info *str_offsets_section,
20880 ULONGEST str_offsets_base, ULONGEST str_index)
20881 {
20882 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20883 struct objfile *objfile = per_objfile->objfile;
20884 const char *objf_name = objfile_name (objfile);
20885 bfd *abfd = objfile->obfd;
20886 const gdb_byte *info_ptr;
20887 ULONGEST str_offset;
20888 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20889
20890 str_section->read (objfile);
20891 str_offsets_section->read (objfile);
20892 if (str_section->buffer == NULL)
20893 error (_("%s used without %s section"
20894 " in CU at offset %s [in module %s]"),
20895 form_name, str_section->get_name (),
20896 sect_offset_str (cu->header.sect_off), objf_name);
20897 if (str_offsets_section->buffer == NULL)
20898 error (_("%s used without %s section"
20899 " in CU at offset %s [in module %s]"),
20900 form_name, str_section->get_name (),
20901 sect_offset_str (cu->header.sect_off), objf_name);
20902 info_ptr = (str_offsets_section->buffer
20903 + str_offsets_base
20904 + str_index * cu->header.offset_size);
20905 if (cu->header.offset_size == 4)
20906 str_offset = bfd_get_32 (abfd, info_ptr);
20907 else
20908 str_offset = bfd_get_64 (abfd, info_ptr);
20909 if (str_offset >= str_section->size)
20910 error (_("Offset from %s pointing outside of"
20911 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20912 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20913 return (const char *) (str_section->buffer + str_offset);
20914 }
20915
20916 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20917
20918 static const char *
20919 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20920 {
20921 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20922 ? reader->cu->header.addr_size : 0;
20923 return read_str_index (reader->cu,
20924 &reader->dwo_file->sections.str,
20925 &reader->dwo_file->sections.str_offsets,
20926 str_offsets_base, str_index);
20927 }
20928
20929 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20930
20931 static const char *
20932 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20933 {
20934 struct objfile *objfile = cu->per_objfile->objfile;
20935 const char *objf_name = objfile_name (objfile);
20936 static const char form_name[] = "DW_FORM_GNU_str_index";
20937 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20938
20939 if (!cu->str_offsets_base.has_value ())
20940 error (_("%s used in Fission stub without %s"
20941 " in CU at offset 0x%lx [in module %s]"),
20942 form_name, str_offsets_attr_name,
20943 (long) cu->header.offset_size, objf_name);
20944
20945 return read_str_index (cu,
20946 &cu->per_objfile->per_bfd->str,
20947 &cu->per_objfile->per_bfd->str_offsets,
20948 *cu->str_offsets_base, str_index);
20949 }
20950
20951 /* Return the length of an LEB128 number in BUF. */
20952
20953 static int
20954 leb128_size (const gdb_byte *buf)
20955 {
20956 const gdb_byte *begin = buf;
20957 gdb_byte byte;
20958
20959 while (1)
20960 {
20961 byte = *buf++;
20962 if ((byte & 128) == 0)
20963 return buf - begin;
20964 }
20965 }
20966
20967 static void
20968 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20969 {
20970 switch (lang)
20971 {
20972 case DW_LANG_C89:
20973 case DW_LANG_C99:
20974 case DW_LANG_C11:
20975 case DW_LANG_C:
20976 case DW_LANG_UPC:
20977 cu->language = language_c;
20978 break;
20979 case DW_LANG_Java:
20980 case DW_LANG_C_plus_plus:
20981 case DW_LANG_C_plus_plus_11:
20982 case DW_LANG_C_plus_plus_14:
20983 cu->language = language_cplus;
20984 break;
20985 case DW_LANG_D:
20986 cu->language = language_d;
20987 break;
20988 case DW_LANG_Fortran77:
20989 case DW_LANG_Fortran90:
20990 case DW_LANG_Fortran95:
20991 case DW_LANG_Fortran03:
20992 case DW_LANG_Fortran08:
20993 cu->language = language_fortran;
20994 break;
20995 case DW_LANG_Go:
20996 cu->language = language_go;
20997 break;
20998 case DW_LANG_Mips_Assembler:
20999 cu->language = language_asm;
21000 break;
21001 case DW_LANG_Ada83:
21002 case DW_LANG_Ada95:
21003 cu->language = language_ada;
21004 break;
21005 case DW_LANG_Modula2:
21006 cu->language = language_m2;
21007 break;
21008 case DW_LANG_Pascal83:
21009 cu->language = language_pascal;
21010 break;
21011 case DW_LANG_ObjC:
21012 cu->language = language_objc;
21013 break;
21014 case DW_LANG_Rust:
21015 case DW_LANG_Rust_old:
21016 cu->language = language_rust;
21017 break;
21018 case DW_LANG_Cobol74:
21019 case DW_LANG_Cobol85:
21020 default:
21021 cu->language = language_minimal;
21022 break;
21023 }
21024 cu->language_defn = language_def (cu->language);
21025 }
21026
21027 /* Return the named attribute or NULL if not there. */
21028
21029 static struct attribute *
21030 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
21031 {
21032 for (;;)
21033 {
21034 unsigned int i;
21035 struct attribute *spec = NULL;
21036
21037 for (i = 0; i < die->num_attrs; ++i)
21038 {
21039 if (die->attrs[i].name == name)
21040 return &die->attrs[i];
21041 if (die->attrs[i].name == DW_AT_specification
21042 || die->attrs[i].name == DW_AT_abstract_origin)
21043 spec = &die->attrs[i];
21044 }
21045
21046 if (!spec)
21047 break;
21048
21049 die = follow_die_ref (die, spec, &cu);
21050 }
21051
21052 return NULL;
21053 }
21054
21055 /* Return the string associated with a string-typed attribute, or NULL if it
21056 is either not found or is of an incorrect type. */
21057
21058 static const char *
21059 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
21060 {
21061 struct attribute *attr;
21062 const char *str = NULL;
21063
21064 attr = dwarf2_attr (die, name, cu);
21065
21066 if (attr != NULL)
21067 {
21068 str = attr->as_string ();
21069 if (str == nullptr)
21070 complaint (_("string type expected for attribute %s for "
21071 "DIE at %s in module %s"),
21072 dwarf_attr_name (name), sect_offset_str (die->sect_off),
21073 objfile_name (cu->per_objfile->objfile));
21074 }
21075
21076 return str;
21077 }
21078
21079 /* Return the dwo name or NULL if not present. If present, it is in either
21080 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
21081 static const char *
21082 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
21083 {
21084 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
21085 if (dwo_name == nullptr)
21086 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
21087 return dwo_name;
21088 }
21089
21090 /* Return non-zero iff the attribute NAME is defined for the given DIE,
21091 and holds a non-zero value. This function should only be used for
21092 DW_FORM_flag or DW_FORM_flag_present attributes. */
21093
21094 static int
21095 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
21096 {
21097 struct attribute *attr = dwarf2_attr (die, name, cu);
21098
21099 return attr != nullptr && attr->as_boolean ();
21100 }
21101
21102 static int
21103 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
21104 {
21105 /* A DIE is a declaration if it has a DW_AT_declaration attribute
21106 which value is non-zero. However, we have to be careful with
21107 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
21108 (via dwarf2_flag_true_p) follows this attribute. So we may
21109 end up accidently finding a declaration attribute that belongs
21110 to a different DIE referenced by the specification attribute,
21111 even though the given DIE does not have a declaration attribute. */
21112 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
21113 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
21114 }
21115
21116 /* Return the die giving the specification for DIE, if there is
21117 one. *SPEC_CU is the CU containing DIE on input, and the CU
21118 containing the return value on output. If there is no
21119 specification, but there is an abstract origin, that is
21120 returned. */
21121
21122 static struct die_info *
21123 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
21124 {
21125 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
21126 *spec_cu);
21127
21128 if (spec_attr == NULL)
21129 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
21130
21131 if (spec_attr == NULL)
21132 return NULL;
21133 else
21134 return follow_die_ref (die, spec_attr, spec_cu);
21135 }
21136
21137 /* Stub for free_line_header to match void * callback types. */
21138
21139 static void
21140 free_line_header_voidp (void *arg)
21141 {
21142 struct line_header *lh = (struct line_header *) arg;
21143
21144 delete lh;
21145 }
21146
21147 /* A convenience function to find the proper .debug_line section for a CU. */
21148
21149 static struct dwarf2_section_info *
21150 get_debug_line_section (struct dwarf2_cu *cu)
21151 {
21152 struct dwarf2_section_info *section;
21153 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21154
21155 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
21156 DWO file. */
21157 if (cu->dwo_unit && cu->per_cu->is_debug_types)
21158 section = &cu->dwo_unit->dwo_file->sections.line;
21159 else if (cu->per_cu->is_dwz)
21160 {
21161 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
21162
21163 section = &dwz->line;
21164 }
21165 else
21166 section = &per_objfile->per_bfd->line;
21167
21168 return section;
21169 }
21170
21171 /* Read the statement program header starting at OFFSET in
21172 .debug_line, or .debug_line.dwo. Return a pointer
21173 to a struct line_header, allocated using xmalloc.
21174 Returns NULL if there is a problem reading the header, e.g., if it
21175 has a version we don't understand.
21176
21177 NOTE: the strings in the include directory and file name tables of
21178 the returned object point into the dwarf line section buffer,
21179 and must not be freed. */
21180
21181 static line_header_up
21182 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
21183 {
21184 struct dwarf2_section_info *section;
21185 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21186
21187 section = get_debug_line_section (cu);
21188 section->read (per_objfile->objfile);
21189 if (section->buffer == NULL)
21190 {
21191 if (cu->dwo_unit && cu->per_cu->is_debug_types)
21192 complaint (_("missing .debug_line.dwo section"));
21193 else
21194 complaint (_("missing .debug_line section"));
21195 return 0;
21196 }
21197
21198 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
21199 per_objfile, section, &cu->header);
21200 }
21201
21202 /* Subroutine of dwarf_decode_lines to simplify it.
21203 Return the file name of the psymtab for the given file_entry.
21204 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21205 If space for the result is malloc'd, *NAME_HOLDER will be set.
21206 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
21207
21208 static const char *
21209 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
21210 const dwarf2_psymtab *pst,
21211 const char *comp_dir,
21212 gdb::unique_xmalloc_ptr<char> *name_holder)
21213 {
21214 const char *include_name = fe.name;
21215 const char *include_name_to_compare = include_name;
21216 const char *pst_filename;
21217 int file_is_pst;
21218
21219 const char *dir_name = fe.include_dir (lh);
21220
21221 gdb::unique_xmalloc_ptr<char> hold_compare;
21222 if (!IS_ABSOLUTE_PATH (include_name)
21223 && (dir_name != NULL || comp_dir != NULL))
21224 {
21225 /* Avoid creating a duplicate psymtab for PST.
21226 We do this by comparing INCLUDE_NAME and PST_FILENAME.
21227 Before we do the comparison, however, we need to account
21228 for DIR_NAME and COMP_DIR.
21229 First prepend dir_name (if non-NULL). If we still don't
21230 have an absolute path prepend comp_dir (if non-NULL).
21231 However, the directory we record in the include-file's
21232 psymtab does not contain COMP_DIR (to match the
21233 corresponding symtab(s)).
21234
21235 Example:
21236
21237 bash$ cd /tmp
21238 bash$ gcc -g ./hello.c
21239 include_name = "hello.c"
21240 dir_name = "."
21241 DW_AT_comp_dir = comp_dir = "/tmp"
21242 DW_AT_name = "./hello.c"
21243
21244 */
21245
21246 if (dir_name != NULL)
21247 {
21248 name_holder->reset (concat (dir_name, SLASH_STRING,
21249 include_name, (char *) NULL));
21250 include_name = name_holder->get ();
21251 include_name_to_compare = include_name;
21252 }
21253 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
21254 {
21255 hold_compare.reset (concat (comp_dir, SLASH_STRING,
21256 include_name, (char *) NULL));
21257 include_name_to_compare = hold_compare.get ();
21258 }
21259 }
21260
21261 pst_filename = pst->filename;
21262 gdb::unique_xmalloc_ptr<char> copied_name;
21263 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
21264 {
21265 copied_name.reset (concat (pst->dirname, SLASH_STRING,
21266 pst_filename, (char *) NULL));
21267 pst_filename = copied_name.get ();
21268 }
21269
21270 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
21271
21272 if (file_is_pst)
21273 return NULL;
21274 return include_name;
21275 }
21276
21277 /* State machine to track the state of the line number program. */
21278
21279 class lnp_state_machine
21280 {
21281 public:
21282 /* Initialize a machine state for the start of a line number
21283 program. */
21284 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
21285 bool record_lines_p);
21286
21287 file_entry *current_file ()
21288 {
21289 /* lh->file_names is 0-based, but the file name numbers in the
21290 statement program are 1-based. */
21291 return m_line_header->file_name_at (m_file);
21292 }
21293
21294 /* Record the line in the state machine. END_SEQUENCE is true if
21295 we're processing the end of a sequence. */
21296 void record_line (bool end_sequence);
21297
21298 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
21299 nop-out rest of the lines in this sequence. */
21300 void check_line_address (struct dwarf2_cu *cu,
21301 const gdb_byte *line_ptr,
21302 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
21303
21304 void handle_set_discriminator (unsigned int discriminator)
21305 {
21306 m_discriminator = discriminator;
21307 m_line_has_non_zero_discriminator |= discriminator != 0;
21308 }
21309
21310 /* Handle DW_LNE_set_address. */
21311 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21312 {
21313 m_op_index = 0;
21314 address += baseaddr;
21315 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21316 }
21317
21318 /* Handle DW_LNS_advance_pc. */
21319 void handle_advance_pc (CORE_ADDR adjust);
21320
21321 /* Handle a special opcode. */
21322 void handle_special_opcode (unsigned char op_code);
21323
21324 /* Handle DW_LNS_advance_line. */
21325 void handle_advance_line (int line_delta)
21326 {
21327 advance_line (line_delta);
21328 }
21329
21330 /* Handle DW_LNS_set_file. */
21331 void handle_set_file (file_name_index file);
21332
21333 /* Handle DW_LNS_negate_stmt. */
21334 void handle_negate_stmt ()
21335 {
21336 m_is_stmt = !m_is_stmt;
21337 }
21338
21339 /* Handle DW_LNS_const_add_pc. */
21340 void handle_const_add_pc ();
21341
21342 /* Handle DW_LNS_fixed_advance_pc. */
21343 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21344 {
21345 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21346 m_op_index = 0;
21347 }
21348
21349 /* Handle DW_LNS_copy. */
21350 void handle_copy ()
21351 {
21352 record_line (false);
21353 m_discriminator = 0;
21354 }
21355
21356 /* Handle DW_LNE_end_sequence. */
21357 void handle_end_sequence ()
21358 {
21359 m_currently_recording_lines = true;
21360 }
21361
21362 private:
21363 /* Advance the line by LINE_DELTA. */
21364 void advance_line (int line_delta)
21365 {
21366 m_line += line_delta;
21367
21368 if (line_delta != 0)
21369 m_line_has_non_zero_discriminator = m_discriminator != 0;
21370 }
21371
21372 struct dwarf2_cu *m_cu;
21373
21374 gdbarch *m_gdbarch;
21375
21376 /* True if we're recording lines.
21377 Otherwise we're building partial symtabs and are just interested in
21378 finding include files mentioned by the line number program. */
21379 bool m_record_lines_p;
21380
21381 /* The line number header. */
21382 line_header *m_line_header;
21383
21384 /* These are part of the standard DWARF line number state machine,
21385 and initialized according to the DWARF spec. */
21386
21387 unsigned char m_op_index = 0;
21388 /* The line table index of the current file. */
21389 file_name_index m_file = 1;
21390 unsigned int m_line = 1;
21391
21392 /* These are initialized in the constructor. */
21393
21394 CORE_ADDR m_address;
21395 bool m_is_stmt;
21396 unsigned int m_discriminator;
21397
21398 /* Additional bits of state we need to track. */
21399
21400 /* The last file that we called dwarf2_start_subfile for.
21401 This is only used for TLLs. */
21402 unsigned int m_last_file = 0;
21403 /* The last file a line number was recorded for. */
21404 struct subfile *m_last_subfile = NULL;
21405
21406 /* The address of the last line entry. */
21407 CORE_ADDR m_last_address;
21408
21409 /* Set to true when a previous line at the same address (using
21410 m_last_address) had m_is_stmt true. This is reset to false when a
21411 line entry at a new address (m_address different to m_last_address) is
21412 processed. */
21413 bool m_stmt_at_address = false;
21414
21415 /* When true, record the lines we decode. */
21416 bool m_currently_recording_lines = false;
21417
21418 /* The last line number that was recorded, used to coalesce
21419 consecutive entries for the same line. This can happen, for
21420 example, when discriminators are present. PR 17276. */
21421 unsigned int m_last_line = 0;
21422 bool m_line_has_non_zero_discriminator = false;
21423 };
21424
21425 void
21426 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21427 {
21428 CORE_ADDR addr_adj = (((m_op_index + adjust)
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 + adjust)
21433 % m_line_header->maximum_ops_per_instruction);
21434 }
21435
21436 void
21437 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21438 {
21439 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21440 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21441 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21442 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
21443 / m_line_header->maximum_ops_per_instruction)
21444 * m_line_header->minimum_instruction_length);
21445 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21446 m_op_index = ((m_op_index + adj_opcode_d)
21447 % m_line_header->maximum_ops_per_instruction);
21448
21449 int line_delta = m_line_header->line_base + adj_opcode_r;
21450 advance_line (line_delta);
21451 record_line (false);
21452 m_discriminator = 0;
21453 }
21454
21455 void
21456 lnp_state_machine::handle_set_file (file_name_index file)
21457 {
21458 m_file = file;
21459
21460 const file_entry *fe = current_file ();
21461 if (fe == NULL)
21462 dwarf2_debug_line_missing_file_complaint ();
21463 else if (m_record_lines_p)
21464 {
21465 const char *dir = fe->include_dir (m_line_header);
21466
21467 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21468 m_line_has_non_zero_discriminator = m_discriminator != 0;
21469 dwarf2_start_subfile (m_cu, fe->name, dir);
21470 }
21471 }
21472
21473 void
21474 lnp_state_machine::handle_const_add_pc ()
21475 {
21476 CORE_ADDR adjust
21477 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21478
21479 CORE_ADDR addr_adj
21480 = (((m_op_index + adjust)
21481 / m_line_header->maximum_ops_per_instruction)
21482 * m_line_header->minimum_instruction_length);
21483
21484 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21485 m_op_index = ((m_op_index + adjust)
21486 % m_line_header->maximum_ops_per_instruction);
21487 }
21488
21489 /* Return non-zero if we should add LINE to the line number table.
21490 LINE is the line to add, LAST_LINE is the last line that was added,
21491 LAST_SUBFILE is the subfile for LAST_LINE.
21492 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21493 had a non-zero discriminator.
21494
21495 We have to be careful in the presence of discriminators.
21496 E.g., for this line:
21497
21498 for (i = 0; i < 100000; i++);
21499
21500 clang can emit four line number entries for that one line,
21501 each with a different discriminator.
21502 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21503
21504 However, we want gdb to coalesce all four entries into one.
21505 Otherwise the user could stepi into the middle of the line and
21506 gdb would get confused about whether the pc really was in the
21507 middle of the line.
21508
21509 Things are further complicated by the fact that two consecutive
21510 line number entries for the same line is a heuristic used by gcc
21511 to denote the end of the prologue. So we can't just discard duplicate
21512 entries, we have to be selective about it. The heuristic we use is
21513 that we only collapse consecutive entries for the same line if at least
21514 one of those entries has a non-zero discriminator. PR 17276.
21515
21516 Note: Addresses in the line number state machine can never go backwards
21517 within one sequence, thus this coalescing is ok. */
21518
21519 static int
21520 dwarf_record_line_p (struct dwarf2_cu *cu,
21521 unsigned int line, unsigned int last_line,
21522 int line_has_non_zero_discriminator,
21523 struct subfile *last_subfile)
21524 {
21525 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21526 return 1;
21527 if (line != last_line)
21528 return 1;
21529 /* Same line for the same file that we've seen already.
21530 As a last check, for pr 17276, only record the line if the line
21531 has never had a non-zero discriminator. */
21532 if (!line_has_non_zero_discriminator)
21533 return 1;
21534 return 0;
21535 }
21536
21537 /* Use the CU's builder to record line number LINE beginning at
21538 address ADDRESS in the line table of subfile SUBFILE. */
21539
21540 static void
21541 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21542 unsigned int line, CORE_ADDR address, bool is_stmt,
21543 struct dwarf2_cu *cu)
21544 {
21545 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21546
21547 if (dwarf_line_debug)
21548 {
21549 fprintf_unfiltered (gdb_stdlog,
21550 "Recording line %u, file %s, address %s\n",
21551 line, lbasename (subfile->name),
21552 paddress (gdbarch, address));
21553 }
21554
21555 if (cu != nullptr)
21556 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
21557 }
21558
21559 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21560 Mark the end of a set of line number records.
21561 The arguments are the same as for dwarf_record_line_1.
21562 If SUBFILE is NULL the request is ignored. */
21563
21564 static void
21565 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21566 CORE_ADDR address, struct dwarf2_cu *cu)
21567 {
21568 if (subfile == NULL)
21569 return;
21570
21571 if (dwarf_line_debug)
21572 {
21573 fprintf_unfiltered (gdb_stdlog,
21574 "Finishing current line, file %s, address %s\n",
21575 lbasename (subfile->name),
21576 paddress (gdbarch, address));
21577 }
21578
21579 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
21580 }
21581
21582 void
21583 lnp_state_machine::record_line (bool end_sequence)
21584 {
21585 if (dwarf_line_debug)
21586 {
21587 fprintf_unfiltered (gdb_stdlog,
21588 "Processing actual line %u: file %u,"
21589 " address %s, is_stmt %u, discrim %u%s\n",
21590 m_line, m_file,
21591 paddress (m_gdbarch, m_address),
21592 m_is_stmt, m_discriminator,
21593 (end_sequence ? "\t(end sequence)" : ""));
21594 }
21595
21596 file_entry *fe = current_file ();
21597
21598 if (fe == NULL)
21599 dwarf2_debug_line_missing_file_complaint ();
21600 /* For now we ignore lines not starting on an instruction boundary.
21601 But not when processing end_sequence for compatibility with the
21602 previous version of the code. */
21603 else if (m_op_index == 0 || end_sequence)
21604 {
21605 fe->included_p = 1;
21606 if (m_record_lines_p)
21607 {
21608 /* When we switch files we insert an end maker in the first file,
21609 switch to the second file and add a new line entry. The
21610 problem is that the end marker inserted in the first file will
21611 discard any previous line entries at the same address. If the
21612 line entries in the first file are marked as is-stmt, while
21613 the new line in the second file is non-stmt, then this means
21614 the end marker will discard is-stmt lines so we can have a
21615 non-stmt line. This means that there are less addresses at
21616 which the user can insert a breakpoint.
21617
21618 To improve this we track the last address in m_last_address,
21619 and whether we have seen an is-stmt at this address. Then
21620 when switching files, if we have seen a stmt at the current
21621 address, and we are switching to create a non-stmt line, then
21622 discard the new line. */
21623 bool file_changed
21624 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21625 bool ignore_this_line
21626 = ((file_changed && !end_sequence && m_last_address == m_address
21627 && !m_is_stmt && m_stmt_at_address)
21628 || (!end_sequence && m_line == 0));
21629
21630 if ((file_changed && !ignore_this_line) || end_sequence)
21631 {
21632 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21633 m_currently_recording_lines ? m_cu : nullptr);
21634 }
21635
21636 if (!end_sequence && !ignore_this_line)
21637 {
21638 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21639
21640 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21641 m_line_has_non_zero_discriminator,
21642 m_last_subfile))
21643 {
21644 buildsym_compunit *builder = m_cu->get_builder ();
21645 dwarf_record_line_1 (m_gdbarch,
21646 builder->get_current_subfile (),
21647 m_line, m_address, is_stmt,
21648 m_currently_recording_lines ? m_cu : nullptr);
21649 }
21650 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21651 m_last_line = m_line;
21652 }
21653 }
21654 }
21655
21656 /* Track whether we have seen any m_is_stmt true at m_address in case we
21657 have multiple line table entries all at m_address. */
21658 if (m_last_address != m_address)
21659 {
21660 m_stmt_at_address = false;
21661 m_last_address = m_address;
21662 }
21663 m_stmt_at_address |= m_is_stmt;
21664 }
21665
21666 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21667 line_header *lh, bool record_lines_p)
21668 {
21669 m_cu = cu;
21670 m_gdbarch = arch;
21671 m_record_lines_p = record_lines_p;
21672 m_line_header = lh;
21673
21674 m_currently_recording_lines = true;
21675
21676 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21677 was a line entry for it so that the backend has a chance to adjust it
21678 and also record it in case it needs it. This is currently used by MIPS
21679 code, cf. `mips_adjust_dwarf2_line'. */
21680 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21681 m_is_stmt = lh->default_is_stmt;
21682 m_discriminator = 0;
21683
21684 m_last_address = m_address;
21685 m_stmt_at_address = false;
21686 }
21687
21688 void
21689 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21690 const gdb_byte *line_ptr,
21691 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21692 {
21693 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21694 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21695 located at 0x0. In this case, additionally check that if
21696 ADDRESS < UNRELOCATED_LOWPC. */
21697
21698 if ((address == 0 && address < unrelocated_lowpc)
21699 || address == (CORE_ADDR) -1)
21700 {
21701 /* This line table is for a function which has been
21702 GCd by the linker. Ignore it. PR gdb/12528 */
21703
21704 struct objfile *objfile = cu->per_objfile->objfile;
21705 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21706
21707 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21708 line_offset, objfile_name (objfile));
21709 m_currently_recording_lines = false;
21710 /* Note: m_currently_recording_lines is left as false until we see
21711 DW_LNE_end_sequence. */
21712 }
21713 }
21714
21715 /* Subroutine of dwarf_decode_lines to simplify it.
21716 Process the line number information in LH.
21717 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21718 program in order to set included_p for every referenced header. */
21719
21720 static void
21721 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21722 const int decode_for_pst_p, CORE_ADDR lowpc)
21723 {
21724 const gdb_byte *line_ptr, *extended_end;
21725 const gdb_byte *line_end;
21726 unsigned int bytes_read, extended_len;
21727 unsigned char op_code, extended_op;
21728 CORE_ADDR baseaddr;
21729 struct objfile *objfile = cu->per_objfile->objfile;
21730 bfd *abfd = objfile->obfd;
21731 struct gdbarch *gdbarch = objfile->arch ();
21732 /* True if we're recording line info (as opposed to building partial
21733 symtabs and just interested in finding include files mentioned by
21734 the line number program). */
21735 bool record_lines_p = !decode_for_pst_p;
21736
21737 baseaddr = objfile->text_section_offset ();
21738
21739 line_ptr = lh->statement_program_start;
21740 line_end = lh->statement_program_end;
21741
21742 /* Read the statement sequences until there's nothing left. */
21743 while (line_ptr < line_end)
21744 {
21745 /* The DWARF line number program state machine. Reset the state
21746 machine at the start of each sequence. */
21747 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21748 bool end_sequence = false;
21749
21750 if (record_lines_p)
21751 {
21752 /* Start a subfile for the current file of the state
21753 machine. */
21754 const file_entry *fe = state_machine.current_file ();
21755
21756 if (fe != NULL)
21757 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21758 }
21759
21760 /* Decode the table. */
21761 while (line_ptr < line_end && !end_sequence)
21762 {
21763 op_code = read_1_byte (abfd, line_ptr);
21764 line_ptr += 1;
21765
21766 if (op_code >= lh->opcode_base)
21767 {
21768 /* Special opcode. */
21769 state_machine.handle_special_opcode (op_code);
21770 }
21771 else switch (op_code)
21772 {
21773 case DW_LNS_extended_op:
21774 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21775 &bytes_read);
21776 line_ptr += bytes_read;
21777 extended_end = line_ptr + extended_len;
21778 extended_op = read_1_byte (abfd, line_ptr);
21779 line_ptr += 1;
21780 if (DW_LNE_lo_user <= extended_op
21781 && extended_op <= DW_LNE_hi_user)
21782 {
21783 /* Vendor extension, ignore. */
21784 line_ptr = extended_end;
21785 break;
21786 }
21787 switch (extended_op)
21788 {
21789 case DW_LNE_end_sequence:
21790 state_machine.handle_end_sequence ();
21791 end_sequence = true;
21792 break;
21793 case DW_LNE_set_address:
21794 {
21795 CORE_ADDR address
21796 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21797 line_ptr += bytes_read;
21798
21799 state_machine.check_line_address (cu, line_ptr,
21800 lowpc - baseaddr, address);
21801 state_machine.handle_set_address (baseaddr, address);
21802 }
21803 break;
21804 case DW_LNE_define_file:
21805 {
21806 const char *cur_file;
21807 unsigned int mod_time, length;
21808 dir_index dindex;
21809
21810 cur_file = read_direct_string (abfd, line_ptr,
21811 &bytes_read);
21812 line_ptr += bytes_read;
21813 dindex = (dir_index)
21814 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21815 line_ptr += bytes_read;
21816 mod_time =
21817 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21818 line_ptr += bytes_read;
21819 length =
21820 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21821 line_ptr += bytes_read;
21822 lh->add_file_name (cur_file, dindex, mod_time, length);
21823 }
21824 break;
21825 case DW_LNE_set_discriminator:
21826 {
21827 /* The discriminator is not interesting to the
21828 debugger; just ignore it. We still need to
21829 check its value though:
21830 if there are consecutive entries for the same
21831 (non-prologue) line we want to coalesce them.
21832 PR 17276. */
21833 unsigned int discr
21834 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21835 line_ptr += bytes_read;
21836
21837 state_machine.handle_set_discriminator (discr);
21838 }
21839 break;
21840 default:
21841 complaint (_("mangled .debug_line section"));
21842 return;
21843 }
21844 /* Make sure that we parsed the extended op correctly. If e.g.
21845 we expected a different address size than the producer used,
21846 we may have read the wrong number of bytes. */
21847 if (line_ptr != extended_end)
21848 {
21849 complaint (_("mangled .debug_line section"));
21850 return;
21851 }
21852 break;
21853 case DW_LNS_copy:
21854 state_machine.handle_copy ();
21855 break;
21856 case DW_LNS_advance_pc:
21857 {
21858 CORE_ADDR adjust
21859 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21860 line_ptr += bytes_read;
21861
21862 state_machine.handle_advance_pc (adjust);
21863 }
21864 break;
21865 case DW_LNS_advance_line:
21866 {
21867 int line_delta
21868 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21869 line_ptr += bytes_read;
21870
21871 state_machine.handle_advance_line (line_delta);
21872 }
21873 break;
21874 case DW_LNS_set_file:
21875 {
21876 file_name_index file
21877 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21878 &bytes_read);
21879 line_ptr += bytes_read;
21880
21881 state_machine.handle_set_file (file);
21882 }
21883 break;
21884 case DW_LNS_set_column:
21885 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21886 line_ptr += bytes_read;
21887 break;
21888 case DW_LNS_negate_stmt:
21889 state_machine.handle_negate_stmt ();
21890 break;
21891 case DW_LNS_set_basic_block:
21892 break;
21893 /* Add to the address register of the state machine the
21894 address increment value corresponding to special opcode
21895 255. I.e., this value is scaled by the minimum
21896 instruction length since special opcode 255 would have
21897 scaled the increment. */
21898 case DW_LNS_const_add_pc:
21899 state_machine.handle_const_add_pc ();
21900 break;
21901 case DW_LNS_fixed_advance_pc:
21902 {
21903 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21904 line_ptr += 2;
21905
21906 state_machine.handle_fixed_advance_pc (addr_adj);
21907 }
21908 break;
21909 default:
21910 {
21911 /* Unknown standard opcode, ignore it. */
21912 int i;
21913
21914 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21915 {
21916 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21917 line_ptr += bytes_read;
21918 }
21919 }
21920 }
21921 }
21922
21923 if (!end_sequence)
21924 dwarf2_debug_line_missing_end_sequence_complaint ();
21925
21926 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21927 in which case we still finish recording the last line). */
21928 state_machine.record_line (true);
21929 }
21930 }
21931
21932 /* Decode the Line Number Program (LNP) for the given line_header
21933 structure and CU. The actual information extracted and the type
21934 of structures created from the LNP depends on the value of PST.
21935
21936 1. If PST is NULL, then this procedure uses the data from the program
21937 to create all necessary symbol tables, and their linetables.
21938
21939 2. If PST is not NULL, this procedure reads the program to determine
21940 the list of files included by the unit represented by PST, and
21941 builds all the associated partial symbol tables.
21942
21943 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21944 It is used for relative paths in the line table.
21945 NOTE: When processing partial symtabs (pst != NULL),
21946 comp_dir == pst->dirname.
21947
21948 NOTE: It is important that psymtabs have the same file name (via strcmp)
21949 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21950 symtab we don't use it in the name of the psymtabs we create.
21951 E.g. expand_line_sal requires this when finding psymtabs to expand.
21952 A good testcase for this is mb-inline.exp.
21953
21954 LOWPC is the lowest address in CU (or 0 if not known).
21955
21956 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21957 for its PC<->lines mapping information. Otherwise only the filename
21958 table is read in. */
21959
21960 static void
21961 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21962 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21963 CORE_ADDR lowpc, int decode_mapping)
21964 {
21965 struct objfile *objfile = cu->per_objfile->objfile;
21966 const int decode_for_pst_p = (pst != NULL);
21967
21968 if (decode_mapping)
21969 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21970
21971 if (decode_for_pst_p)
21972 {
21973 /* Now that we're done scanning the Line Header Program, we can
21974 create the psymtab of each included file. */
21975 for (auto &file_entry : lh->file_names ())
21976 if (file_entry.included_p == 1)
21977 {
21978 gdb::unique_xmalloc_ptr<char> name_holder;
21979 const char *include_name =
21980 psymtab_include_file_name (lh, file_entry, pst,
21981 comp_dir, &name_holder);
21982 if (include_name != NULL)
21983 dwarf2_create_include_psymtab
21984 (cu->per_objfile->per_bfd, include_name, pst,
21985 cu->per_objfile->per_bfd->partial_symtabs.get (),
21986 objfile);
21987 }
21988 }
21989 else
21990 {
21991 /* Make sure a symtab is created for every file, even files
21992 which contain only variables (i.e. no code with associated
21993 line numbers). */
21994 buildsym_compunit *builder = cu->get_builder ();
21995 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21996
21997 for (auto &fe : lh->file_names ())
21998 {
21999 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
22000 if (builder->get_current_subfile ()->symtab == NULL)
22001 {
22002 builder->get_current_subfile ()->symtab
22003 = allocate_symtab (cust,
22004 builder->get_current_subfile ()->name);
22005 }
22006 fe.symtab = builder->get_current_subfile ()->symtab;
22007 }
22008 }
22009 }
22010
22011 /* Start a subfile for DWARF. FILENAME is the name of the file and
22012 DIRNAME the name of the source directory which contains FILENAME
22013 or NULL if not known.
22014 This routine tries to keep line numbers from identical absolute and
22015 relative file names in a common subfile.
22016
22017 Using the `list' example from the GDB testsuite, which resides in
22018 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
22019 of /srcdir/list0.c yields the following debugging information for list0.c:
22020
22021 DW_AT_name: /srcdir/list0.c
22022 DW_AT_comp_dir: /compdir
22023 files.files[0].name: list0.h
22024 files.files[0].dir: /srcdir
22025 files.files[1].name: list0.c
22026 files.files[1].dir: /srcdir
22027
22028 The line number information for list0.c has to end up in a single
22029 subfile, so that `break /srcdir/list0.c:1' works as expected.
22030 start_subfile will ensure that this happens provided that we pass the
22031 concatenation of files.files[1].dir and files.files[1].name as the
22032 subfile's name. */
22033
22034 static void
22035 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
22036 const char *dirname)
22037 {
22038 gdb::unique_xmalloc_ptr<char> copy;
22039
22040 /* In order not to lose the line information directory,
22041 we concatenate it to the filename when it makes sense.
22042 Note that the Dwarf3 standard says (speaking of filenames in line
22043 information): ``The directory index is ignored for file names
22044 that represent full path names''. Thus ignoring dirname in the
22045 `else' branch below isn't an issue. */
22046
22047 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
22048 {
22049 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
22050 filename = copy.get ();
22051 }
22052
22053 cu->get_builder ()->start_subfile (filename);
22054 }
22055
22056 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
22057 buildsym_compunit constructor. */
22058
22059 struct compunit_symtab *
22060 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
22061 CORE_ADDR low_pc)
22062 {
22063 gdb_assert (m_builder == nullptr);
22064
22065 m_builder.reset (new struct buildsym_compunit
22066 (this->per_objfile->objfile,
22067 name, comp_dir, language, low_pc));
22068
22069 list_in_scope = get_builder ()->get_file_symbols ();
22070
22071 get_builder ()->record_debugformat ("DWARF 2");
22072 get_builder ()->record_producer (producer);
22073
22074 processing_has_namespace_info = false;
22075
22076 return get_builder ()->get_compunit_symtab ();
22077 }
22078
22079 static void
22080 var_decode_location (struct attribute *attr, struct symbol *sym,
22081 struct dwarf2_cu *cu)
22082 {
22083 struct objfile *objfile = cu->per_objfile->objfile;
22084 struct comp_unit_head *cu_header = &cu->header;
22085
22086 /* NOTE drow/2003-01-30: There used to be a comment and some special
22087 code here to turn a symbol with DW_AT_external and a
22088 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
22089 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
22090 with some versions of binutils) where shared libraries could have
22091 relocations against symbols in their debug information - the
22092 minimal symbol would have the right address, but the debug info
22093 would not. It's no longer necessary, because we will explicitly
22094 apply relocations when we read in the debug information now. */
22095
22096 /* A DW_AT_location attribute with no contents indicates that a
22097 variable has been optimized away. */
22098 if (attr->form_is_block () && attr->as_block ()->size == 0)
22099 {
22100 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22101 return;
22102 }
22103
22104 /* Handle one degenerate form of location expression specially, to
22105 preserve GDB's previous behavior when section offsets are
22106 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
22107 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
22108
22109 if (attr->form_is_block ())
22110 {
22111 struct dwarf_block *block = attr->as_block ();
22112
22113 if ((block->data[0] == DW_OP_addr
22114 && block->size == 1 + cu_header->addr_size)
22115 || ((block->data[0] == DW_OP_GNU_addr_index
22116 || block->data[0] == DW_OP_addrx)
22117 && (block->size
22118 == 1 + leb128_size (&block->data[1]))))
22119 {
22120 unsigned int dummy;
22121
22122 if (block->data[0] == DW_OP_addr)
22123 SET_SYMBOL_VALUE_ADDRESS
22124 (sym, cu->header.read_address (objfile->obfd,
22125 block->data + 1,
22126 &dummy));
22127 else
22128 SET_SYMBOL_VALUE_ADDRESS
22129 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
22130 &dummy));
22131 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
22132 fixup_symbol_section (sym, objfile);
22133 SET_SYMBOL_VALUE_ADDRESS
22134 (sym,
22135 SYMBOL_VALUE_ADDRESS (sym)
22136 + objfile->section_offsets[sym->section_index ()]);
22137 return;
22138 }
22139 }
22140
22141 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
22142 expression evaluator, and use LOC_COMPUTED only when necessary
22143 (i.e. when the value of a register or memory location is
22144 referenced, or a thread-local block, etc.). Then again, it might
22145 not be worthwhile. I'm assuming that it isn't unless performance
22146 or memory numbers show me otherwise. */
22147
22148 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
22149
22150 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
22151 cu->has_loclist = true;
22152 }
22153
22154 /* Given a pointer to a DWARF information entry, figure out if we need
22155 to make a symbol table entry for it, and if so, create a new entry
22156 and return a pointer to it.
22157 If TYPE is NULL, determine symbol type from the die, otherwise
22158 used the passed type.
22159 If SPACE is not NULL, use it to hold the new symbol. If it is
22160 NULL, allocate a new symbol on the objfile's obstack. */
22161
22162 static struct symbol *
22163 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
22164 struct symbol *space)
22165 {
22166 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22167 struct objfile *objfile = per_objfile->objfile;
22168 struct gdbarch *gdbarch = objfile->arch ();
22169 struct symbol *sym = NULL;
22170 const char *name;
22171 struct attribute *attr = NULL;
22172 struct attribute *attr2 = NULL;
22173 CORE_ADDR baseaddr;
22174 struct pending **list_to_add = NULL;
22175
22176 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
22177
22178 baseaddr = objfile->text_section_offset ();
22179
22180 name = dwarf2_name (die, cu);
22181 if (name)
22182 {
22183 int suppress_add = 0;
22184
22185 if (space)
22186 sym = space;
22187 else
22188 sym = new (&objfile->objfile_obstack) symbol;
22189 OBJSTAT (objfile, n_syms++);
22190
22191 /* Cache this symbol's name and the name's demangled form (if any). */
22192 sym->set_language (cu->language, &objfile->objfile_obstack);
22193 /* Fortran does not have mangling standard and the mangling does differ
22194 between gfortran, iFort etc. */
22195 const char *physname
22196 = (cu->language == language_fortran
22197 ? dwarf2_full_name (name, die, cu)
22198 : dwarf2_physname (name, die, cu));
22199 const char *linkagename = dw2_linkage_name (die, cu);
22200
22201 if (linkagename == nullptr || cu->language == language_ada)
22202 sym->set_linkage_name (physname);
22203 else
22204 {
22205 sym->set_demangled_name (physname, &objfile->objfile_obstack);
22206 sym->set_linkage_name (linkagename);
22207 }
22208
22209 /* Default assumptions.
22210 Use the passed type or decode it from the die. */
22211 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22212 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22213 if (type != NULL)
22214 SYMBOL_TYPE (sym) = type;
22215 else
22216 SYMBOL_TYPE (sym) = die_type (die, cu);
22217 attr = dwarf2_attr (die,
22218 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
22219 cu);
22220 if (attr != nullptr)
22221 SYMBOL_LINE (sym) = attr->constant_value (0);
22222
22223 attr = dwarf2_attr (die,
22224 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
22225 cu);
22226 if (attr != nullptr && attr->is_nonnegative ())
22227 {
22228 file_name_index file_index
22229 = (file_name_index) attr->as_nonnegative ();
22230 struct file_entry *fe;
22231
22232 if (cu->line_header != NULL)
22233 fe = cu->line_header->file_name_at (file_index);
22234 else
22235 fe = NULL;
22236
22237 if (fe == NULL)
22238 complaint (_("file index out of range"));
22239 else
22240 symbol_set_symtab (sym, fe->symtab);
22241 }
22242
22243 switch (die->tag)
22244 {
22245 case DW_TAG_label:
22246 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
22247 if (attr != nullptr)
22248 {
22249 CORE_ADDR addr;
22250
22251 addr = attr->as_address ();
22252 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
22253 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
22254 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
22255 }
22256 else
22257 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22258 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
22259 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
22260 add_symbol_to_list (sym, cu->list_in_scope);
22261 break;
22262 case DW_TAG_subprogram:
22263 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22264 finish_block. */
22265 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
22266 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22267 if ((attr2 != nullptr && attr2->as_boolean ())
22268 || cu->language == language_ada
22269 || cu->language == language_fortran)
22270 {
22271 /* Subprograms marked external are stored as a global symbol.
22272 Ada and Fortran subprograms, whether marked external or
22273 not, are always stored as a global symbol, because we want
22274 to be able to access them globally. For instance, we want
22275 to be able to break on a nested subprogram without having
22276 to specify the context. */
22277 list_to_add = cu->get_builder ()->get_global_symbols ();
22278 }
22279 else
22280 {
22281 list_to_add = cu->list_in_scope;
22282 }
22283 break;
22284 case DW_TAG_inlined_subroutine:
22285 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22286 finish_block. */
22287 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
22288 SYMBOL_INLINED (sym) = 1;
22289 list_to_add = cu->list_in_scope;
22290 break;
22291 case DW_TAG_template_value_param:
22292 suppress_add = 1;
22293 /* Fall through. */
22294 case DW_TAG_constant:
22295 case DW_TAG_variable:
22296 case DW_TAG_member:
22297 /* Compilation with minimal debug info may result in
22298 variables with missing type entries. Change the
22299 misleading `void' type to something sensible. */
22300 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
22301 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
22302
22303 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22304 /* In the case of DW_TAG_member, we should only be called for
22305 static const members. */
22306 if (die->tag == DW_TAG_member)
22307 {
22308 /* dwarf2_add_field uses die_is_declaration,
22309 so we do the same. */
22310 gdb_assert (die_is_declaration (die, cu));
22311 gdb_assert (attr);
22312 }
22313 if (attr != nullptr)
22314 {
22315 dwarf2_const_value (attr, sym, cu);
22316 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22317 if (!suppress_add)
22318 {
22319 if (attr2 != nullptr && attr2->as_boolean ())
22320 list_to_add = cu->get_builder ()->get_global_symbols ();
22321 else
22322 list_to_add = cu->list_in_scope;
22323 }
22324 break;
22325 }
22326 attr = dwarf2_attr (die, DW_AT_location, cu);
22327 if (attr != nullptr)
22328 {
22329 var_decode_location (attr, sym, cu);
22330 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22331
22332 /* Fortran explicitly imports any global symbols to the local
22333 scope by DW_TAG_common_block. */
22334 if (cu->language == language_fortran && die->parent
22335 && die->parent->tag == DW_TAG_common_block)
22336 attr2 = NULL;
22337
22338 if (SYMBOL_CLASS (sym) == LOC_STATIC
22339 && SYMBOL_VALUE_ADDRESS (sym) == 0
22340 && !per_objfile->per_bfd->has_section_at_zero)
22341 {
22342 /* When a static variable is eliminated by the linker,
22343 the corresponding debug information is not stripped
22344 out, but the variable address is set to null;
22345 do not add such variables into symbol table. */
22346 }
22347 else if (attr2 != nullptr && attr2->as_boolean ())
22348 {
22349 if (SYMBOL_CLASS (sym) == LOC_STATIC
22350 && (objfile->flags & OBJF_MAINLINE) == 0
22351 && per_objfile->per_bfd->can_copy)
22352 {
22353 /* A global static variable might be subject to
22354 copy relocation. We first check for a local
22355 minsym, though, because maybe the symbol was
22356 marked hidden, in which case this would not
22357 apply. */
22358 bound_minimal_symbol found
22359 = (lookup_minimal_symbol_linkage
22360 (sym->linkage_name (), objfile));
22361 if (found.minsym != nullptr)
22362 sym->maybe_copied = 1;
22363 }
22364
22365 /* A variable with DW_AT_external is never static,
22366 but it may be block-scoped. */
22367 list_to_add
22368 = ((cu->list_in_scope
22369 == cu->get_builder ()->get_file_symbols ())
22370 ? cu->get_builder ()->get_global_symbols ()
22371 : cu->list_in_scope);
22372 }
22373 else
22374 list_to_add = cu->list_in_scope;
22375 }
22376 else
22377 {
22378 /* We do not know the address of this symbol.
22379 If it is an external symbol and we have type information
22380 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22381 The address of the variable will then be determined from
22382 the minimal symbol table whenever the variable is
22383 referenced. */
22384 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22385
22386 /* Fortran explicitly imports any global symbols to the local
22387 scope by DW_TAG_common_block. */
22388 if (cu->language == language_fortran && die->parent
22389 && die->parent->tag == DW_TAG_common_block)
22390 {
22391 /* SYMBOL_CLASS doesn't matter here because
22392 read_common_block is going to reset it. */
22393 if (!suppress_add)
22394 list_to_add = cu->list_in_scope;
22395 }
22396 else if (attr2 != nullptr && attr2->as_boolean ()
22397 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
22398 {
22399 /* A variable with DW_AT_external is never static, but it
22400 may be block-scoped. */
22401 list_to_add
22402 = ((cu->list_in_scope
22403 == cu->get_builder ()->get_file_symbols ())
22404 ? cu->get_builder ()->get_global_symbols ()
22405 : cu->list_in_scope);
22406
22407 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
22408 }
22409 else if (!die_is_declaration (die, cu))
22410 {
22411 /* Use the default LOC_OPTIMIZED_OUT class. */
22412 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
22413 if (!suppress_add)
22414 list_to_add = cu->list_in_scope;
22415 }
22416 }
22417 break;
22418 case DW_TAG_formal_parameter:
22419 {
22420 /* If we are inside a function, mark this as an argument. If
22421 not, we might be looking at an argument to an inlined function
22422 when we do not have enough information to show inlined frames;
22423 pretend it's a local variable in that case so that the user can
22424 still see it. */
22425 struct context_stack *curr
22426 = cu->get_builder ()->get_current_context_stack ();
22427 if (curr != nullptr && curr->name != nullptr)
22428 SYMBOL_IS_ARGUMENT (sym) = 1;
22429 attr = dwarf2_attr (die, DW_AT_location, cu);
22430 if (attr != nullptr)
22431 {
22432 var_decode_location (attr, sym, cu);
22433 }
22434 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22435 if (attr != nullptr)
22436 {
22437 dwarf2_const_value (attr, sym, cu);
22438 }
22439
22440 list_to_add = cu->list_in_scope;
22441 }
22442 break;
22443 case DW_TAG_unspecified_parameters:
22444 /* From varargs functions; gdb doesn't seem to have any
22445 interest in this information, so just ignore it for now.
22446 (FIXME?) */
22447 break;
22448 case DW_TAG_template_type_param:
22449 suppress_add = 1;
22450 /* Fall through. */
22451 case DW_TAG_class_type:
22452 case DW_TAG_interface_type:
22453 case DW_TAG_structure_type:
22454 case DW_TAG_union_type:
22455 case DW_TAG_set_type:
22456 case DW_TAG_enumeration_type:
22457 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22458 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
22459
22460 {
22461 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22462 really ever be static objects: otherwise, if you try
22463 to, say, break of a class's method and you're in a file
22464 which doesn't mention that class, it won't work unless
22465 the check for all static symbols in lookup_symbol_aux
22466 saves you. See the OtherFileClass tests in
22467 gdb.c++/namespace.exp. */
22468
22469 if (!suppress_add)
22470 {
22471 buildsym_compunit *builder = cu->get_builder ();
22472 list_to_add
22473 = (cu->list_in_scope == builder->get_file_symbols ()
22474 && cu->language == language_cplus
22475 ? builder->get_global_symbols ()
22476 : cu->list_in_scope);
22477
22478 /* The semantics of C++ state that "struct foo {
22479 ... }" also defines a typedef for "foo". */
22480 if (cu->language == language_cplus
22481 || cu->language == language_ada
22482 || cu->language == language_d
22483 || cu->language == language_rust)
22484 {
22485 /* The symbol's name is already allocated along
22486 with this objfile, so we don't need to
22487 duplicate it for the type. */
22488 if (SYMBOL_TYPE (sym)->name () == 0)
22489 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
22490 }
22491 }
22492 }
22493 break;
22494 case DW_TAG_typedef:
22495 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22496 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22497 list_to_add = cu->list_in_scope;
22498 break;
22499 case DW_TAG_array_type:
22500 case DW_TAG_base_type:
22501 case DW_TAG_subrange_type:
22502 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22503 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22504 list_to_add = cu->list_in_scope;
22505 break;
22506 case DW_TAG_enumerator:
22507 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22508 if (attr != nullptr)
22509 {
22510 dwarf2_const_value (attr, sym, cu);
22511 }
22512 {
22513 /* NOTE: carlton/2003-11-10: See comment above in the
22514 DW_TAG_class_type, etc. block. */
22515
22516 list_to_add
22517 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22518 && cu->language == language_cplus
22519 ? cu->get_builder ()->get_global_symbols ()
22520 : cu->list_in_scope);
22521 }
22522 break;
22523 case DW_TAG_imported_declaration:
22524 case DW_TAG_namespace:
22525 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22526 list_to_add = cu->get_builder ()->get_global_symbols ();
22527 break;
22528 case DW_TAG_module:
22529 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22530 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22531 list_to_add = cu->get_builder ()->get_global_symbols ();
22532 break;
22533 case DW_TAG_common_block:
22534 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22535 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22536 add_symbol_to_list (sym, cu->list_in_scope);
22537 break;
22538 default:
22539 /* Not a tag we recognize. Hopefully we aren't processing
22540 trash data, but since we must specifically ignore things
22541 we don't recognize, there is nothing else we should do at
22542 this point. */
22543 complaint (_("unsupported tag: '%s'"),
22544 dwarf_tag_name (die->tag));
22545 break;
22546 }
22547
22548 if (suppress_add)
22549 {
22550 sym->hash_next = objfile->template_symbols;
22551 objfile->template_symbols = sym;
22552 list_to_add = NULL;
22553 }
22554
22555 if (list_to_add != NULL)
22556 add_symbol_to_list (sym, list_to_add);
22557
22558 /* For the benefit of old versions of GCC, check for anonymous
22559 namespaces based on the demangled name. */
22560 if (!cu->processing_has_namespace_info
22561 && cu->language == language_cplus)
22562 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22563 }
22564 return (sym);
22565 }
22566
22567 /* Given an attr with a DW_FORM_dataN value in host byte order,
22568 zero-extend it as appropriate for the symbol's type. The DWARF
22569 standard (v4) is not entirely clear about the meaning of using
22570 DW_FORM_dataN for a constant with a signed type, where the type is
22571 wider than the data. The conclusion of a discussion on the DWARF
22572 list was that this is unspecified. We choose to always zero-extend
22573 because that is the interpretation long in use by GCC. */
22574
22575 static gdb_byte *
22576 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22577 struct dwarf2_cu *cu, LONGEST *value, int bits)
22578 {
22579 struct objfile *objfile = cu->per_objfile->objfile;
22580 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22581 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22582 LONGEST l = attr->constant_value (0);
22583
22584 if (bits < sizeof (*value) * 8)
22585 {
22586 l &= ((LONGEST) 1 << bits) - 1;
22587 *value = l;
22588 }
22589 else if (bits == sizeof (*value) * 8)
22590 *value = l;
22591 else
22592 {
22593 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22594 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22595 return bytes;
22596 }
22597
22598 return NULL;
22599 }
22600
22601 /* Read a constant value from an attribute. Either set *VALUE, or if
22602 the value does not fit in *VALUE, set *BYTES - either already
22603 allocated on the objfile obstack, or newly allocated on OBSTACK,
22604 or, set *BATON, if we translated the constant to a location
22605 expression. */
22606
22607 static void
22608 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22609 const char *name, struct obstack *obstack,
22610 struct dwarf2_cu *cu,
22611 LONGEST *value, const gdb_byte **bytes,
22612 struct dwarf2_locexpr_baton **baton)
22613 {
22614 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22615 struct objfile *objfile = per_objfile->objfile;
22616 struct comp_unit_head *cu_header = &cu->header;
22617 struct dwarf_block *blk;
22618 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22619 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22620
22621 *value = 0;
22622 *bytes = NULL;
22623 *baton = NULL;
22624
22625 switch (attr->form)
22626 {
22627 case DW_FORM_addr:
22628 case DW_FORM_addrx:
22629 case DW_FORM_GNU_addr_index:
22630 {
22631 gdb_byte *data;
22632
22633 if (TYPE_LENGTH (type) != cu_header->addr_size)
22634 dwarf2_const_value_length_mismatch_complaint (name,
22635 cu_header->addr_size,
22636 TYPE_LENGTH (type));
22637 /* Symbols of this form are reasonably rare, so we just
22638 piggyback on the existing location code rather than writing
22639 a new implementation of symbol_computed_ops. */
22640 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22641 (*baton)->per_objfile = per_objfile;
22642 (*baton)->per_cu = cu->per_cu;
22643 gdb_assert ((*baton)->per_cu);
22644
22645 (*baton)->size = 2 + cu_header->addr_size;
22646 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22647 (*baton)->data = data;
22648
22649 data[0] = DW_OP_addr;
22650 store_unsigned_integer (&data[1], cu_header->addr_size,
22651 byte_order, attr->as_address ());
22652 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22653 }
22654 break;
22655 case DW_FORM_string:
22656 case DW_FORM_strp:
22657 case DW_FORM_strx:
22658 case DW_FORM_GNU_str_index:
22659 case DW_FORM_GNU_strp_alt:
22660 /* The string is already allocated on the objfile obstack, point
22661 directly to it. */
22662 *bytes = (const gdb_byte *) attr->as_string ();
22663 break;
22664 case DW_FORM_block1:
22665 case DW_FORM_block2:
22666 case DW_FORM_block4:
22667 case DW_FORM_block:
22668 case DW_FORM_exprloc:
22669 case DW_FORM_data16:
22670 blk = attr->as_block ();
22671 if (TYPE_LENGTH (type) != blk->size)
22672 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22673 TYPE_LENGTH (type));
22674 *bytes = blk->data;
22675 break;
22676
22677 /* The DW_AT_const_value attributes are supposed to carry the
22678 symbol's value "represented as it would be on the target
22679 architecture." By the time we get here, it's already been
22680 converted to host endianness, so we just need to sign- or
22681 zero-extend it as appropriate. */
22682 case DW_FORM_data1:
22683 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22684 break;
22685 case DW_FORM_data2:
22686 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22687 break;
22688 case DW_FORM_data4:
22689 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22690 break;
22691 case DW_FORM_data8:
22692 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22693 break;
22694
22695 case DW_FORM_sdata:
22696 case DW_FORM_implicit_const:
22697 *value = attr->as_signed ();
22698 break;
22699
22700 case DW_FORM_udata:
22701 *value = attr->as_unsigned ();
22702 break;
22703
22704 default:
22705 complaint (_("unsupported const value attribute form: '%s'"),
22706 dwarf_form_name (attr->form));
22707 *value = 0;
22708 break;
22709 }
22710 }
22711
22712
22713 /* Copy constant value from an attribute to a symbol. */
22714
22715 static void
22716 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22717 struct dwarf2_cu *cu)
22718 {
22719 struct objfile *objfile = cu->per_objfile->objfile;
22720 LONGEST value;
22721 const gdb_byte *bytes;
22722 struct dwarf2_locexpr_baton *baton;
22723
22724 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22725 sym->print_name (),
22726 &objfile->objfile_obstack, cu,
22727 &value, &bytes, &baton);
22728
22729 if (baton != NULL)
22730 {
22731 SYMBOL_LOCATION_BATON (sym) = baton;
22732 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22733 }
22734 else if (bytes != NULL)
22735 {
22736 SYMBOL_VALUE_BYTES (sym) = bytes;
22737 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22738 }
22739 else
22740 {
22741 SYMBOL_VALUE (sym) = value;
22742 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22743 }
22744 }
22745
22746 /* Return the type of the die in question using its DW_AT_type attribute. */
22747
22748 static struct type *
22749 die_type (struct die_info *die, struct dwarf2_cu *cu)
22750 {
22751 struct attribute *type_attr;
22752
22753 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22754 if (!type_attr)
22755 {
22756 struct objfile *objfile = cu->per_objfile->objfile;
22757 /* A missing DW_AT_type represents a void type. */
22758 return objfile_type (objfile)->builtin_void;
22759 }
22760
22761 return lookup_die_type (die, type_attr, cu);
22762 }
22763
22764 /* True iff CU's producer generates GNAT Ada auxiliary information
22765 that allows to find parallel types through that information instead
22766 of having to do expensive parallel lookups by type name. */
22767
22768 static int
22769 need_gnat_info (struct dwarf2_cu *cu)
22770 {
22771 /* Assume that the Ada compiler was GNAT, which always produces
22772 the auxiliary information. */
22773 return (cu->language == language_ada);
22774 }
22775
22776 /* Return the auxiliary type of the die in question using its
22777 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22778 attribute is not present. */
22779
22780 static struct type *
22781 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22782 {
22783 struct attribute *type_attr;
22784
22785 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22786 if (!type_attr)
22787 return NULL;
22788
22789 return lookup_die_type (die, type_attr, cu);
22790 }
22791
22792 /* If DIE has a descriptive_type attribute, then set the TYPE's
22793 descriptive type accordingly. */
22794
22795 static void
22796 set_descriptive_type (struct type *type, struct die_info *die,
22797 struct dwarf2_cu *cu)
22798 {
22799 struct type *descriptive_type = die_descriptive_type (die, cu);
22800
22801 if (descriptive_type)
22802 {
22803 ALLOCATE_GNAT_AUX_TYPE (type);
22804 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22805 }
22806 }
22807
22808 /* Return the containing type of the die in question using its
22809 DW_AT_containing_type attribute. */
22810
22811 static struct type *
22812 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22813 {
22814 struct attribute *type_attr;
22815 struct objfile *objfile = cu->per_objfile->objfile;
22816
22817 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22818 if (!type_attr)
22819 error (_("Dwarf Error: Problem turning containing type into gdb type "
22820 "[in module %s]"), objfile_name (objfile));
22821
22822 return lookup_die_type (die, type_attr, cu);
22823 }
22824
22825 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22826
22827 static struct type *
22828 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22829 {
22830 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22831 struct objfile *objfile = per_objfile->objfile;
22832 char *saved;
22833
22834 std::string message
22835 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22836 objfile_name (objfile),
22837 sect_offset_str (cu->header.sect_off),
22838 sect_offset_str (die->sect_off));
22839 saved = obstack_strdup (&objfile->objfile_obstack, message);
22840
22841 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22842 }
22843
22844 /* Look up the type of DIE in CU using its type attribute ATTR.
22845 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22846 DW_AT_containing_type.
22847 If there is no type substitute an error marker. */
22848
22849 static struct type *
22850 lookup_die_type (struct die_info *die, const struct attribute *attr,
22851 struct dwarf2_cu *cu)
22852 {
22853 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22854 struct objfile *objfile = per_objfile->objfile;
22855 struct type *this_type;
22856
22857 gdb_assert (attr->name == DW_AT_type
22858 || attr->name == DW_AT_GNAT_descriptive_type
22859 || attr->name == DW_AT_containing_type);
22860
22861 /* First see if we have it cached. */
22862
22863 if (attr->form == DW_FORM_GNU_ref_alt)
22864 {
22865 struct dwarf2_per_cu_data *per_cu;
22866 sect_offset sect_off = attr->get_ref_die_offset ();
22867
22868 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22869 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
22870 }
22871 else if (attr->form_is_ref ())
22872 {
22873 sect_offset sect_off = attr->get_ref_die_offset ();
22874
22875 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
22876 }
22877 else if (attr->form == DW_FORM_ref_sig8)
22878 {
22879 ULONGEST signature = attr->as_signature ();
22880
22881 return get_signatured_type (die, signature, cu);
22882 }
22883 else
22884 {
22885 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22886 " at %s [in module %s]"),
22887 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22888 objfile_name (objfile));
22889 return build_error_marker_type (cu, die);
22890 }
22891
22892 /* If not cached we need to read it in. */
22893
22894 if (this_type == NULL)
22895 {
22896 struct die_info *type_die = NULL;
22897 struct dwarf2_cu *type_cu = cu;
22898
22899 if (attr->form_is_ref ())
22900 type_die = follow_die_ref (die, attr, &type_cu);
22901 if (type_die == NULL)
22902 return build_error_marker_type (cu, die);
22903 /* If we find the type now, it's probably because the type came
22904 from an inter-CU reference and the type's CU got expanded before
22905 ours. */
22906 this_type = read_type_die (type_die, type_cu);
22907 }
22908
22909 /* If we still don't have a type use an error marker. */
22910
22911 if (this_type == NULL)
22912 return build_error_marker_type (cu, die);
22913
22914 return this_type;
22915 }
22916
22917 /* Return the type in DIE, CU.
22918 Returns NULL for invalid types.
22919
22920 This first does a lookup in die_type_hash,
22921 and only reads the die in if necessary.
22922
22923 NOTE: This can be called when reading in partial or full symbols. */
22924
22925 static struct type *
22926 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22927 {
22928 struct type *this_type;
22929
22930 this_type = get_die_type (die, cu);
22931 if (this_type)
22932 return this_type;
22933
22934 return read_type_die_1 (die, cu);
22935 }
22936
22937 /* Read the type in DIE, CU.
22938 Returns NULL for invalid types. */
22939
22940 static struct type *
22941 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22942 {
22943 struct type *this_type = NULL;
22944
22945 switch (die->tag)
22946 {
22947 case DW_TAG_class_type:
22948 case DW_TAG_interface_type:
22949 case DW_TAG_structure_type:
22950 case DW_TAG_union_type:
22951 this_type = read_structure_type (die, cu);
22952 break;
22953 case DW_TAG_enumeration_type:
22954 this_type = read_enumeration_type (die, cu);
22955 break;
22956 case DW_TAG_subprogram:
22957 case DW_TAG_subroutine_type:
22958 case DW_TAG_inlined_subroutine:
22959 this_type = read_subroutine_type (die, cu);
22960 break;
22961 case DW_TAG_array_type:
22962 this_type = read_array_type (die, cu);
22963 break;
22964 case DW_TAG_set_type:
22965 this_type = read_set_type (die, cu);
22966 break;
22967 case DW_TAG_pointer_type:
22968 this_type = read_tag_pointer_type (die, cu);
22969 break;
22970 case DW_TAG_ptr_to_member_type:
22971 this_type = read_tag_ptr_to_member_type (die, cu);
22972 break;
22973 case DW_TAG_reference_type:
22974 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22975 break;
22976 case DW_TAG_rvalue_reference_type:
22977 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22978 break;
22979 case DW_TAG_const_type:
22980 this_type = read_tag_const_type (die, cu);
22981 break;
22982 case DW_TAG_volatile_type:
22983 this_type = read_tag_volatile_type (die, cu);
22984 break;
22985 case DW_TAG_restrict_type:
22986 this_type = read_tag_restrict_type (die, cu);
22987 break;
22988 case DW_TAG_string_type:
22989 this_type = read_tag_string_type (die, cu);
22990 break;
22991 case DW_TAG_typedef:
22992 this_type = read_typedef (die, cu);
22993 break;
22994 case DW_TAG_subrange_type:
22995 this_type = read_subrange_type (die, cu);
22996 break;
22997 case DW_TAG_base_type:
22998 this_type = read_base_type (die, cu);
22999 break;
23000 case DW_TAG_unspecified_type:
23001 this_type = read_unspecified_type (die, cu);
23002 break;
23003 case DW_TAG_namespace:
23004 this_type = read_namespace_type (die, cu);
23005 break;
23006 case DW_TAG_module:
23007 this_type = read_module_type (die, cu);
23008 break;
23009 case DW_TAG_atomic_type:
23010 this_type = read_tag_atomic_type (die, cu);
23011 break;
23012 default:
23013 complaint (_("unexpected tag in read_type_die: '%s'"),
23014 dwarf_tag_name (die->tag));
23015 break;
23016 }
23017
23018 return this_type;
23019 }
23020
23021 /* See if we can figure out if the class lives in a namespace. We do
23022 this by looking for a member function; its demangled name will
23023 contain namespace info, if there is any.
23024 Return the computed name or NULL.
23025 Space for the result is allocated on the objfile's obstack.
23026 This is the full-die version of guess_partial_die_structure_name.
23027 In this case we know DIE has no useful parent. */
23028
23029 static const char *
23030 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
23031 {
23032 struct die_info *spec_die;
23033 struct dwarf2_cu *spec_cu;
23034 struct die_info *child;
23035 struct objfile *objfile = cu->per_objfile->objfile;
23036
23037 spec_cu = cu;
23038 spec_die = die_specification (die, &spec_cu);
23039 if (spec_die != NULL)
23040 {
23041 die = spec_die;
23042 cu = spec_cu;
23043 }
23044
23045 for (child = die->child;
23046 child != NULL;
23047 child = child->sibling)
23048 {
23049 if (child->tag == DW_TAG_subprogram)
23050 {
23051 const char *linkage_name = dw2_linkage_name (child, cu);
23052
23053 if (linkage_name != NULL)
23054 {
23055 gdb::unique_xmalloc_ptr<char> actual_name
23056 (cu->language_defn->class_name_from_physname (linkage_name));
23057 const char *name = NULL;
23058
23059 if (actual_name != NULL)
23060 {
23061 const char *die_name = dwarf2_name (die, cu);
23062
23063 if (die_name != NULL
23064 && strcmp (die_name, actual_name.get ()) != 0)
23065 {
23066 /* Strip off the class name from the full name.
23067 We want the prefix. */
23068 int die_name_len = strlen (die_name);
23069 int actual_name_len = strlen (actual_name.get ());
23070 const char *ptr = actual_name.get ();
23071
23072 /* Test for '::' as a sanity check. */
23073 if (actual_name_len > die_name_len + 2
23074 && ptr[actual_name_len - die_name_len - 1] == ':')
23075 name = obstack_strndup (
23076 &objfile->per_bfd->storage_obstack,
23077 ptr, actual_name_len - die_name_len - 2);
23078 }
23079 }
23080 return name;
23081 }
23082 }
23083 }
23084
23085 return NULL;
23086 }
23087
23088 /* GCC might emit a nameless typedef that has a linkage name. Determine the
23089 prefix part in such case. See
23090 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23091
23092 static const char *
23093 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
23094 {
23095 struct attribute *attr;
23096 const char *base;
23097
23098 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
23099 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
23100 return NULL;
23101
23102 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
23103 return NULL;
23104
23105 attr = dw2_linkage_name_attr (die, cu);
23106 const char *attr_name = attr->as_string ();
23107 if (attr == NULL || attr_name == NULL)
23108 return NULL;
23109
23110 /* dwarf2_name had to be already called. */
23111 gdb_assert (attr->canonical_string_p ());
23112
23113 /* Strip the base name, keep any leading namespaces/classes. */
23114 base = strrchr (attr_name, ':');
23115 if (base == NULL || base == attr_name || base[-1] != ':')
23116 return "";
23117
23118 struct objfile *objfile = cu->per_objfile->objfile;
23119 return obstack_strndup (&objfile->per_bfd->storage_obstack,
23120 attr_name,
23121 &base[-1] - attr_name);
23122 }
23123
23124 /* Return the name of the namespace/class that DIE is defined within,
23125 or "" if we can't tell. The caller should not xfree the result.
23126
23127 For example, if we're within the method foo() in the following
23128 code:
23129
23130 namespace N {
23131 class C {
23132 void foo () {
23133 }
23134 };
23135 }
23136
23137 then determine_prefix on foo's die will return "N::C". */
23138
23139 static const char *
23140 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
23141 {
23142 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23143 struct die_info *parent, *spec_die;
23144 struct dwarf2_cu *spec_cu;
23145 struct type *parent_type;
23146 const char *retval;
23147
23148 if (cu->language != language_cplus
23149 && cu->language != language_fortran && cu->language != language_d
23150 && cu->language != language_rust)
23151 return "";
23152
23153 retval = anonymous_struct_prefix (die, cu);
23154 if (retval)
23155 return retval;
23156
23157 /* We have to be careful in the presence of DW_AT_specification.
23158 For example, with GCC 3.4, given the code
23159
23160 namespace N {
23161 void foo() {
23162 // Definition of N::foo.
23163 }
23164 }
23165
23166 then we'll have a tree of DIEs like this:
23167
23168 1: DW_TAG_compile_unit
23169 2: DW_TAG_namespace // N
23170 3: DW_TAG_subprogram // declaration of N::foo
23171 4: DW_TAG_subprogram // definition of N::foo
23172 DW_AT_specification // refers to die #3
23173
23174 Thus, when processing die #4, we have to pretend that we're in
23175 the context of its DW_AT_specification, namely the contex of die
23176 #3. */
23177 spec_cu = cu;
23178 spec_die = die_specification (die, &spec_cu);
23179 if (spec_die == NULL)
23180 parent = die->parent;
23181 else
23182 {
23183 parent = spec_die->parent;
23184 cu = spec_cu;
23185 }
23186
23187 if (parent == NULL)
23188 return "";
23189 else if (parent->building_fullname)
23190 {
23191 const char *name;
23192 const char *parent_name;
23193
23194 /* It has been seen on RealView 2.2 built binaries,
23195 DW_TAG_template_type_param types actually _defined_ as
23196 children of the parent class:
23197
23198 enum E {};
23199 template class <class Enum> Class{};
23200 Class<enum E> class_e;
23201
23202 1: DW_TAG_class_type (Class)
23203 2: DW_TAG_enumeration_type (E)
23204 3: DW_TAG_enumerator (enum1:0)
23205 3: DW_TAG_enumerator (enum2:1)
23206 ...
23207 2: DW_TAG_template_type_param
23208 DW_AT_type DW_FORM_ref_udata (E)
23209
23210 Besides being broken debug info, it can put GDB into an
23211 infinite loop. Consider:
23212
23213 When we're building the full name for Class<E>, we'll start
23214 at Class, and go look over its template type parameters,
23215 finding E. We'll then try to build the full name of E, and
23216 reach here. We're now trying to build the full name of E,
23217 and look over the parent DIE for containing scope. In the
23218 broken case, if we followed the parent DIE of E, we'd again
23219 find Class, and once again go look at its template type
23220 arguments, etc., etc. Simply don't consider such parent die
23221 as source-level parent of this die (it can't be, the language
23222 doesn't allow it), and break the loop here. */
23223 name = dwarf2_name (die, cu);
23224 parent_name = dwarf2_name (parent, cu);
23225 complaint (_("template param type '%s' defined within parent '%s'"),
23226 name ? name : "<unknown>",
23227 parent_name ? parent_name : "<unknown>");
23228 return "";
23229 }
23230 else
23231 switch (parent->tag)
23232 {
23233 case DW_TAG_namespace:
23234 parent_type = read_type_die (parent, cu);
23235 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
23236 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
23237 Work around this problem here. */
23238 if (cu->language == language_cplus
23239 && strcmp (parent_type->name (), "::") == 0)
23240 return "";
23241 /* We give a name to even anonymous namespaces. */
23242 return parent_type->name ();
23243 case DW_TAG_class_type:
23244 case DW_TAG_interface_type:
23245 case DW_TAG_structure_type:
23246 case DW_TAG_union_type:
23247 case DW_TAG_module:
23248 parent_type = read_type_die (parent, cu);
23249 if (parent_type->name () != NULL)
23250 return parent_type->name ();
23251 else
23252 /* An anonymous structure is only allowed non-static data
23253 members; no typedefs, no member functions, et cetera.
23254 So it does not need a prefix. */
23255 return "";
23256 case DW_TAG_compile_unit:
23257 case DW_TAG_partial_unit:
23258 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
23259 if (cu->language == language_cplus
23260 && !per_objfile->per_bfd->types.empty ()
23261 && die->child != NULL
23262 && (die->tag == DW_TAG_class_type
23263 || die->tag == DW_TAG_structure_type
23264 || die->tag == DW_TAG_union_type))
23265 {
23266 const char *name = guess_full_die_structure_name (die, cu);
23267 if (name != NULL)
23268 return name;
23269 }
23270 return "";
23271 case DW_TAG_subprogram:
23272 /* Nested subroutines in Fortran get a prefix with the name
23273 of the parent's subroutine. */
23274 if (cu->language == language_fortran)
23275 {
23276 if ((die->tag == DW_TAG_subprogram)
23277 && (dwarf2_name (parent, cu) != NULL))
23278 return dwarf2_name (parent, cu);
23279 }
23280 return determine_prefix (parent, cu);
23281 case DW_TAG_enumeration_type:
23282 parent_type = read_type_die (parent, cu);
23283 if (TYPE_DECLARED_CLASS (parent_type))
23284 {
23285 if (parent_type->name () != NULL)
23286 return parent_type->name ();
23287 return "";
23288 }
23289 /* Fall through. */
23290 default:
23291 return determine_prefix (parent, cu);
23292 }
23293 }
23294
23295 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
23296 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
23297 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
23298 an obconcat, otherwise allocate storage for the result. The CU argument is
23299 used to determine the language and hence, the appropriate separator. */
23300
23301 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
23302
23303 static char *
23304 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
23305 int physname, struct dwarf2_cu *cu)
23306 {
23307 const char *lead = "";
23308 const char *sep;
23309
23310 if (suffix == NULL || suffix[0] == '\0'
23311 || prefix == NULL || prefix[0] == '\0')
23312 sep = "";
23313 else if (cu->language == language_d)
23314 {
23315 /* For D, the 'main' function could be defined in any module, but it
23316 should never be prefixed. */
23317 if (strcmp (suffix, "D main") == 0)
23318 {
23319 prefix = "";
23320 sep = "";
23321 }
23322 else
23323 sep = ".";
23324 }
23325 else if (cu->language == language_fortran && physname)
23326 {
23327 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
23328 DW_AT_MIPS_linkage_name is preferred and used instead. */
23329
23330 lead = "__";
23331 sep = "_MOD_";
23332 }
23333 else
23334 sep = "::";
23335
23336 if (prefix == NULL)
23337 prefix = "";
23338 if (suffix == NULL)
23339 suffix = "";
23340
23341 if (obs == NULL)
23342 {
23343 char *retval
23344 = ((char *)
23345 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
23346
23347 strcpy (retval, lead);
23348 strcat (retval, prefix);
23349 strcat (retval, sep);
23350 strcat (retval, suffix);
23351 return retval;
23352 }
23353 else
23354 {
23355 /* We have an obstack. */
23356 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
23357 }
23358 }
23359
23360 /* Get name of a die, return NULL if not found. */
23361
23362 static const char *
23363 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
23364 struct objfile *objfile)
23365 {
23366 if (name && cu->language == language_cplus)
23367 {
23368 gdb::unique_xmalloc_ptr<char> canon_name
23369 = cp_canonicalize_string (name);
23370
23371 if (canon_name != nullptr)
23372 name = objfile->intern (canon_name.get ());
23373 }
23374
23375 return name;
23376 }
23377
23378 /* Get name of a die, return NULL if not found.
23379 Anonymous namespaces are converted to their magic string. */
23380
23381 static const char *
23382 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
23383 {
23384 struct attribute *attr;
23385 struct objfile *objfile = cu->per_objfile->objfile;
23386
23387 attr = dwarf2_attr (die, DW_AT_name, cu);
23388 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
23389 if (attr_name == nullptr
23390 && die->tag != DW_TAG_namespace
23391 && die->tag != DW_TAG_class_type
23392 && die->tag != DW_TAG_interface_type
23393 && die->tag != DW_TAG_structure_type
23394 && die->tag != DW_TAG_union_type)
23395 return NULL;
23396
23397 switch (die->tag)
23398 {
23399 case DW_TAG_compile_unit:
23400 case DW_TAG_partial_unit:
23401 /* Compilation units have a DW_AT_name that is a filename, not
23402 a source language identifier. */
23403 case DW_TAG_enumeration_type:
23404 case DW_TAG_enumerator:
23405 /* These tags always have simple identifiers already; no need
23406 to canonicalize them. */
23407 return attr_name;
23408
23409 case DW_TAG_namespace:
23410 if (attr_name != nullptr)
23411 return attr_name;
23412 return CP_ANONYMOUS_NAMESPACE_STR;
23413
23414 case DW_TAG_class_type:
23415 case DW_TAG_interface_type:
23416 case DW_TAG_structure_type:
23417 case DW_TAG_union_type:
23418 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23419 structures or unions. These were of the form "._%d" in GCC 4.1,
23420 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23421 and GCC 4.4. We work around this problem by ignoring these. */
23422 if (attr_name != nullptr
23423 && (startswith (attr_name, "._")
23424 || startswith (attr_name, "<anonymous")))
23425 return NULL;
23426
23427 /* GCC might emit a nameless typedef that has a linkage name. See
23428 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23429 if (!attr || attr_name == NULL)
23430 {
23431 attr = dw2_linkage_name_attr (die, cu);
23432 attr_name = attr == nullptr ? nullptr : attr->as_string ();
23433 if (attr == NULL || attr_name == NULL)
23434 return NULL;
23435
23436 /* Avoid demangling attr_name the second time on a second
23437 call for the same DIE. */
23438 if (!attr->canonical_string_p ())
23439 {
23440 gdb::unique_xmalloc_ptr<char> demangled
23441 (gdb_demangle (attr_name, DMGL_TYPES));
23442 if (demangled == nullptr)
23443 return nullptr;
23444
23445 attr->set_string_canonical (objfile->intern (demangled.get ()));
23446 attr_name = attr->as_string ();
23447 }
23448
23449 /* Strip any leading namespaces/classes, keep only the
23450 base name. DW_AT_name for named DIEs does not
23451 contain the prefixes. */
23452 const char *base = strrchr (attr_name, ':');
23453 if (base && base > attr_name && base[-1] == ':')
23454 return &base[1];
23455 else
23456 return attr_name;
23457 }
23458 break;
23459
23460 default:
23461 break;
23462 }
23463
23464 if (!attr->canonical_string_p ())
23465 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23466 objfile));
23467 return attr->as_string ();
23468 }
23469
23470 /* Return the die that this die in an extension of, or NULL if there
23471 is none. *EXT_CU is the CU containing DIE on input, and the CU
23472 containing the return value on output. */
23473
23474 static struct die_info *
23475 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23476 {
23477 struct attribute *attr;
23478
23479 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23480 if (attr == NULL)
23481 return NULL;
23482
23483 return follow_die_ref (die, attr, ext_cu);
23484 }
23485
23486 static void
23487 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23488 {
23489 unsigned int i;
23490
23491 print_spaces (indent, f);
23492 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23493 dwarf_tag_name (die->tag), die->abbrev,
23494 sect_offset_str (die->sect_off));
23495
23496 if (die->parent != NULL)
23497 {
23498 print_spaces (indent, f);
23499 fprintf_unfiltered (f, " parent at offset: %s\n",
23500 sect_offset_str (die->parent->sect_off));
23501 }
23502
23503 print_spaces (indent, f);
23504 fprintf_unfiltered (f, " has children: %s\n",
23505 dwarf_bool_name (die->child != NULL));
23506
23507 print_spaces (indent, f);
23508 fprintf_unfiltered (f, " attributes:\n");
23509
23510 for (i = 0; i < die->num_attrs; ++i)
23511 {
23512 print_spaces (indent, f);
23513 fprintf_unfiltered (f, " %s (%s) ",
23514 dwarf_attr_name (die->attrs[i].name),
23515 dwarf_form_name (die->attrs[i].form));
23516
23517 switch (die->attrs[i].form)
23518 {
23519 case DW_FORM_addr:
23520 case DW_FORM_addrx:
23521 case DW_FORM_GNU_addr_index:
23522 fprintf_unfiltered (f, "address: ");
23523 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
23524 break;
23525 case DW_FORM_block2:
23526 case DW_FORM_block4:
23527 case DW_FORM_block:
23528 case DW_FORM_block1:
23529 fprintf_unfiltered (f, "block: size %s",
23530 pulongest (die->attrs[i].as_block ()->size));
23531 break;
23532 case DW_FORM_exprloc:
23533 fprintf_unfiltered (f, "expression: size %s",
23534 pulongest (die->attrs[i].as_block ()->size));
23535 break;
23536 case DW_FORM_data16:
23537 fprintf_unfiltered (f, "constant of 16 bytes");
23538 break;
23539 case DW_FORM_ref_addr:
23540 fprintf_unfiltered (f, "ref address: ");
23541 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23542 break;
23543 case DW_FORM_GNU_ref_alt:
23544 fprintf_unfiltered (f, "alt ref address: ");
23545 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23546 break;
23547 case DW_FORM_ref1:
23548 case DW_FORM_ref2:
23549 case DW_FORM_ref4:
23550 case DW_FORM_ref8:
23551 case DW_FORM_ref_udata:
23552 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23553 (long) (die->attrs[i].as_unsigned ()));
23554 break;
23555 case DW_FORM_data1:
23556 case DW_FORM_data2:
23557 case DW_FORM_data4:
23558 case DW_FORM_data8:
23559 case DW_FORM_udata:
23560 fprintf_unfiltered (f, "constant: %s",
23561 pulongest (die->attrs[i].as_unsigned ()));
23562 break;
23563 case DW_FORM_sec_offset:
23564 fprintf_unfiltered (f, "section offset: %s",
23565 pulongest (die->attrs[i].as_unsigned ()));
23566 break;
23567 case DW_FORM_ref_sig8:
23568 fprintf_unfiltered (f, "signature: %s",
23569 hex_string (die->attrs[i].as_signature ()));
23570 break;
23571 case DW_FORM_string:
23572 case DW_FORM_strp:
23573 case DW_FORM_line_strp:
23574 case DW_FORM_strx:
23575 case DW_FORM_GNU_str_index:
23576 case DW_FORM_GNU_strp_alt:
23577 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23578 die->attrs[i].as_string ()
23579 ? die->attrs[i].as_string () : "",
23580 die->attrs[i].canonical_string_p () ? "is" : "not");
23581 break;
23582 case DW_FORM_flag:
23583 if (die->attrs[i].as_boolean ())
23584 fprintf_unfiltered (f, "flag: TRUE");
23585 else
23586 fprintf_unfiltered (f, "flag: FALSE");
23587 break;
23588 case DW_FORM_flag_present:
23589 fprintf_unfiltered (f, "flag: TRUE");
23590 break;
23591 case DW_FORM_indirect:
23592 /* The reader will have reduced the indirect form to
23593 the "base form" so this form should not occur. */
23594 fprintf_unfiltered (f,
23595 "unexpected attribute form: DW_FORM_indirect");
23596 break;
23597 case DW_FORM_sdata:
23598 case DW_FORM_implicit_const:
23599 fprintf_unfiltered (f, "constant: %s",
23600 plongest (die->attrs[i].as_signed ()));
23601 break;
23602 default:
23603 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23604 die->attrs[i].form);
23605 break;
23606 }
23607 fprintf_unfiltered (f, "\n");
23608 }
23609 }
23610
23611 static void
23612 dump_die_for_error (struct die_info *die)
23613 {
23614 dump_die_shallow (gdb_stderr, 0, die);
23615 }
23616
23617 static void
23618 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23619 {
23620 int indent = level * 4;
23621
23622 gdb_assert (die != NULL);
23623
23624 if (level >= max_level)
23625 return;
23626
23627 dump_die_shallow (f, indent, die);
23628
23629 if (die->child != NULL)
23630 {
23631 print_spaces (indent, f);
23632 fprintf_unfiltered (f, " Children:");
23633 if (level + 1 < max_level)
23634 {
23635 fprintf_unfiltered (f, "\n");
23636 dump_die_1 (f, level + 1, max_level, die->child);
23637 }
23638 else
23639 {
23640 fprintf_unfiltered (f,
23641 " [not printed, max nesting level reached]\n");
23642 }
23643 }
23644
23645 if (die->sibling != NULL && level > 0)
23646 {
23647 dump_die_1 (f, level, max_level, die->sibling);
23648 }
23649 }
23650
23651 /* This is called from the pdie macro in gdbinit.in.
23652 It's not static so gcc will keep a copy callable from gdb. */
23653
23654 void
23655 dump_die (struct die_info *die, int max_level)
23656 {
23657 dump_die_1 (gdb_stdlog, 0, max_level, die);
23658 }
23659
23660 static void
23661 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23662 {
23663 void **slot;
23664
23665 slot = htab_find_slot_with_hash (cu->die_hash, die,
23666 to_underlying (die->sect_off),
23667 INSERT);
23668
23669 *slot = die;
23670 }
23671
23672 /* Follow reference or signature attribute ATTR of SRC_DIE.
23673 On entry *REF_CU is the CU of SRC_DIE.
23674 On exit *REF_CU is the CU of the result. */
23675
23676 static struct die_info *
23677 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23678 struct dwarf2_cu **ref_cu)
23679 {
23680 struct die_info *die;
23681
23682 if (attr->form_is_ref ())
23683 die = follow_die_ref (src_die, attr, ref_cu);
23684 else if (attr->form == DW_FORM_ref_sig8)
23685 die = follow_die_sig (src_die, attr, ref_cu);
23686 else
23687 {
23688 dump_die_for_error (src_die);
23689 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23690 objfile_name ((*ref_cu)->per_objfile->objfile));
23691 }
23692
23693 return die;
23694 }
23695
23696 /* Follow reference OFFSET.
23697 On entry *REF_CU is the CU of the source die referencing OFFSET.
23698 On exit *REF_CU is the CU of the result.
23699 Returns NULL if OFFSET is invalid. */
23700
23701 static struct die_info *
23702 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23703 struct dwarf2_cu **ref_cu)
23704 {
23705 struct die_info temp_die;
23706 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23707 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23708
23709 gdb_assert (cu->per_cu != NULL);
23710
23711 target_cu = cu;
23712
23713 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23714 "source CU contains target offset: %d",
23715 sect_offset_str (cu->per_cu->sect_off),
23716 sect_offset_str (sect_off),
23717 cu->header.offset_in_cu_p (sect_off));
23718
23719 if (cu->per_cu->is_debug_types)
23720 {
23721 /* .debug_types CUs cannot reference anything outside their CU.
23722 If they need to, they have to reference a signatured type via
23723 DW_FORM_ref_sig8. */
23724 if (!cu->header.offset_in_cu_p (sect_off))
23725 return NULL;
23726 }
23727 else if (offset_in_dwz != cu->per_cu->is_dwz
23728 || !cu->header.offset_in_cu_p (sect_off))
23729 {
23730 struct dwarf2_per_cu_data *per_cu;
23731
23732 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23733 per_objfile);
23734
23735 dwarf_read_debug_printf_v ("target CU offset: %s, "
23736 "target CU DIEs loaded: %d",
23737 sect_offset_str (per_cu->sect_off),
23738 per_objfile->get_cu (per_cu) != nullptr);
23739
23740 /* If necessary, add it to the queue and load its DIEs.
23741
23742 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23743 it doesn't mean they are currently loaded. Since we require them
23744 to be loaded, we must check for ourselves. */
23745 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language)
23746 || per_objfile->get_cu (per_cu) == nullptr)
23747 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23748 false, cu->language);
23749
23750 target_cu = per_objfile->get_cu (per_cu);
23751 gdb_assert (target_cu != nullptr);
23752 }
23753 else if (cu->dies == NULL)
23754 {
23755 /* We're loading full DIEs during partial symbol reading. */
23756 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
23757 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23758 language_minimal);
23759 }
23760
23761 *ref_cu = target_cu;
23762 temp_die.sect_off = sect_off;
23763
23764 if (target_cu != cu)
23765 target_cu->ancestor = cu;
23766
23767 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23768 &temp_die,
23769 to_underlying (sect_off));
23770 }
23771
23772 /* Follow reference attribute ATTR of SRC_DIE.
23773 On entry *REF_CU is the CU of SRC_DIE.
23774 On exit *REF_CU is the CU of the result. */
23775
23776 static struct die_info *
23777 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23778 struct dwarf2_cu **ref_cu)
23779 {
23780 sect_offset sect_off = attr->get_ref_die_offset ();
23781 struct dwarf2_cu *cu = *ref_cu;
23782 struct die_info *die;
23783
23784 die = follow_die_offset (sect_off,
23785 (attr->form == DW_FORM_GNU_ref_alt
23786 || cu->per_cu->is_dwz),
23787 ref_cu);
23788 if (!die)
23789 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23790 "at %s [in module %s]"),
23791 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23792 objfile_name (cu->per_objfile->objfile));
23793
23794 return die;
23795 }
23796
23797 /* See read.h. */
23798
23799 struct dwarf2_locexpr_baton
23800 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23801 dwarf2_per_cu_data *per_cu,
23802 dwarf2_per_objfile *per_objfile,
23803 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23804 bool resolve_abstract_p)
23805 {
23806 struct die_info *die;
23807 struct attribute *attr;
23808 struct dwarf2_locexpr_baton retval;
23809 struct objfile *objfile = per_objfile->objfile;
23810
23811 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23812 if (cu == nullptr)
23813 cu = load_cu (per_cu, per_objfile, false);
23814
23815 if (cu == nullptr)
23816 {
23817 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23818 Instead just throw an error, not much else we can do. */
23819 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23820 sect_offset_str (sect_off), objfile_name (objfile));
23821 }
23822
23823 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23824 if (!die)
23825 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23826 sect_offset_str (sect_off), objfile_name (objfile));
23827
23828 attr = dwarf2_attr (die, DW_AT_location, cu);
23829 if (!attr && resolve_abstract_p
23830 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23831 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23832 {
23833 CORE_ADDR pc = get_frame_pc ();
23834 CORE_ADDR baseaddr = objfile->text_section_offset ();
23835 struct gdbarch *gdbarch = objfile->arch ();
23836
23837 for (const auto &cand_off
23838 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23839 {
23840 struct dwarf2_cu *cand_cu = cu;
23841 struct die_info *cand
23842 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23843 if (!cand
23844 || !cand->parent
23845 || cand->parent->tag != DW_TAG_subprogram)
23846 continue;
23847
23848 CORE_ADDR pc_low, pc_high;
23849 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23850 if (pc_low == ((CORE_ADDR) -1))
23851 continue;
23852 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23853 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23854 if (!(pc_low <= pc && pc < pc_high))
23855 continue;
23856
23857 die = cand;
23858 attr = dwarf2_attr (die, DW_AT_location, cu);
23859 break;
23860 }
23861 }
23862
23863 if (!attr)
23864 {
23865 /* DWARF: "If there is no such attribute, then there is no effect.".
23866 DATA is ignored if SIZE is 0. */
23867
23868 retval.data = NULL;
23869 retval.size = 0;
23870 }
23871 else if (attr->form_is_section_offset ())
23872 {
23873 struct dwarf2_loclist_baton loclist_baton;
23874 CORE_ADDR pc = get_frame_pc ();
23875 size_t size;
23876
23877 fill_in_loclist_baton (cu, &loclist_baton, attr);
23878
23879 retval.data = dwarf2_find_location_expression (&loclist_baton,
23880 &size, pc);
23881 retval.size = size;
23882 }
23883 else
23884 {
23885 if (!attr->form_is_block ())
23886 error (_("Dwarf Error: DIE at %s referenced in module %s "
23887 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23888 sect_offset_str (sect_off), objfile_name (objfile));
23889
23890 struct dwarf_block *block = attr->as_block ();
23891 retval.data = block->data;
23892 retval.size = block->size;
23893 }
23894 retval.per_objfile = per_objfile;
23895 retval.per_cu = cu->per_cu;
23896
23897 per_objfile->age_comp_units ();
23898
23899 return retval;
23900 }
23901
23902 /* See read.h. */
23903
23904 struct dwarf2_locexpr_baton
23905 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23906 dwarf2_per_cu_data *per_cu,
23907 dwarf2_per_objfile *per_objfile,
23908 gdb::function_view<CORE_ADDR ()> get_frame_pc)
23909 {
23910 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23911
23912 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
23913 get_frame_pc);
23914 }
23915
23916 /* Write a constant of a given type as target-ordered bytes into
23917 OBSTACK. */
23918
23919 static const gdb_byte *
23920 write_constant_as_bytes (struct obstack *obstack,
23921 enum bfd_endian byte_order,
23922 struct type *type,
23923 ULONGEST value,
23924 LONGEST *len)
23925 {
23926 gdb_byte *result;
23927
23928 *len = TYPE_LENGTH (type);
23929 result = (gdb_byte *) obstack_alloc (obstack, *len);
23930 store_unsigned_integer (result, *len, byte_order, value);
23931
23932 return result;
23933 }
23934
23935 /* See read.h. */
23936
23937 const gdb_byte *
23938 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23939 dwarf2_per_cu_data *per_cu,
23940 dwarf2_per_objfile *per_objfile,
23941 obstack *obstack,
23942 LONGEST *len)
23943 {
23944 struct die_info *die;
23945 struct attribute *attr;
23946 const gdb_byte *result = NULL;
23947 struct type *type;
23948 LONGEST value;
23949 enum bfd_endian byte_order;
23950 struct objfile *objfile = per_objfile->objfile;
23951
23952 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23953 if (cu == nullptr)
23954 cu = load_cu (per_cu, per_objfile, false);
23955
23956 if (cu == nullptr)
23957 {
23958 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23959 Instead just throw an error, not much else we can do. */
23960 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23961 sect_offset_str (sect_off), objfile_name (objfile));
23962 }
23963
23964 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23965 if (!die)
23966 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23967 sect_offset_str (sect_off), objfile_name (objfile));
23968
23969 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23970 if (attr == NULL)
23971 return NULL;
23972
23973 byte_order = (bfd_big_endian (objfile->obfd)
23974 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23975
23976 switch (attr->form)
23977 {
23978 case DW_FORM_addr:
23979 case DW_FORM_addrx:
23980 case DW_FORM_GNU_addr_index:
23981 {
23982 gdb_byte *tem;
23983
23984 *len = cu->header.addr_size;
23985 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23986 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
23987 result = tem;
23988 }
23989 break;
23990 case DW_FORM_string:
23991 case DW_FORM_strp:
23992 case DW_FORM_strx:
23993 case DW_FORM_GNU_str_index:
23994 case DW_FORM_GNU_strp_alt:
23995 /* The string is already allocated on the objfile obstack, point
23996 directly to it. */
23997 {
23998 const char *attr_name = attr->as_string ();
23999 result = (const gdb_byte *) attr_name;
24000 *len = strlen (attr_name);
24001 }
24002 break;
24003 case DW_FORM_block1:
24004 case DW_FORM_block2:
24005 case DW_FORM_block4:
24006 case DW_FORM_block:
24007 case DW_FORM_exprloc:
24008 case DW_FORM_data16:
24009 {
24010 struct dwarf_block *block = attr->as_block ();
24011 result = block->data;
24012 *len = block->size;
24013 }
24014 break;
24015
24016 /* The DW_AT_const_value attributes are supposed to carry the
24017 symbol's value "represented as it would be on the target
24018 architecture." By the time we get here, it's already been
24019 converted to host endianness, so we just need to sign- or
24020 zero-extend it as appropriate. */
24021 case DW_FORM_data1:
24022 type = die_type (die, cu);
24023 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
24024 if (result == NULL)
24025 result = write_constant_as_bytes (obstack, byte_order,
24026 type, value, len);
24027 break;
24028 case DW_FORM_data2:
24029 type = die_type (die, cu);
24030 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
24031 if (result == NULL)
24032 result = write_constant_as_bytes (obstack, byte_order,
24033 type, value, len);
24034 break;
24035 case DW_FORM_data4:
24036 type = die_type (die, cu);
24037 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
24038 if (result == NULL)
24039 result = write_constant_as_bytes (obstack, byte_order,
24040 type, value, len);
24041 break;
24042 case DW_FORM_data8:
24043 type = die_type (die, cu);
24044 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
24045 if (result == NULL)
24046 result = write_constant_as_bytes (obstack, byte_order,
24047 type, value, len);
24048 break;
24049
24050 case DW_FORM_sdata:
24051 case DW_FORM_implicit_const:
24052 type = die_type (die, cu);
24053 result = write_constant_as_bytes (obstack, byte_order,
24054 type, attr->as_signed (), len);
24055 break;
24056
24057 case DW_FORM_udata:
24058 type = die_type (die, cu);
24059 result = write_constant_as_bytes (obstack, byte_order,
24060 type, attr->as_unsigned (), len);
24061 break;
24062
24063 default:
24064 complaint (_("unsupported const value attribute form: '%s'"),
24065 dwarf_form_name (attr->form));
24066 break;
24067 }
24068
24069 return result;
24070 }
24071
24072 /* See read.h. */
24073
24074 struct type *
24075 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
24076 dwarf2_per_cu_data *per_cu,
24077 dwarf2_per_objfile *per_objfile)
24078 {
24079 struct die_info *die;
24080
24081 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
24082 if (cu == nullptr)
24083 cu = load_cu (per_cu, per_objfile, false);
24084
24085 if (cu == nullptr)
24086 return nullptr;
24087
24088 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
24089 if (!die)
24090 return NULL;
24091
24092 return die_type (die, cu);
24093 }
24094
24095 /* See read.h. */
24096
24097 struct type *
24098 dwarf2_get_die_type (cu_offset die_offset,
24099 dwarf2_per_cu_data *per_cu,
24100 dwarf2_per_objfile *per_objfile)
24101 {
24102 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
24103 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
24104 }
24105
24106 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
24107 On entry *REF_CU is the CU of SRC_DIE.
24108 On exit *REF_CU is the CU of the result.
24109 Returns NULL if the referenced DIE isn't found. */
24110
24111 static struct die_info *
24112 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
24113 struct dwarf2_cu **ref_cu)
24114 {
24115 struct die_info temp_die;
24116 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
24117 struct die_info *die;
24118 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
24119
24120
24121 /* While it might be nice to assert sig_type->type == NULL here,
24122 we can get here for DW_AT_imported_declaration where we need
24123 the DIE not the type. */
24124
24125 /* If necessary, add it to the queue and load its DIEs.
24126
24127 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
24128 it doesn't mean they are currently loaded. Since we require them
24129 to be loaded, we must check for ourselves. */
24130 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
24131 language_minimal)
24132 || per_objfile->get_cu (&sig_type->per_cu) == nullptr)
24133 read_signatured_type (sig_type, per_objfile);
24134
24135 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
24136 gdb_assert (sig_cu != NULL);
24137 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
24138 temp_die.sect_off = sig_type->type_offset_in_section;
24139 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
24140 to_underlying (temp_die.sect_off));
24141 if (die)
24142 {
24143 /* For .gdb_index version 7 keep track of included TUs.
24144 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
24145 if (per_objfile->per_bfd->index_table != NULL
24146 && per_objfile->per_bfd->index_table->version <= 7)
24147 {
24148 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
24149 }
24150
24151 *ref_cu = sig_cu;
24152 if (sig_cu != cu)
24153 sig_cu->ancestor = cu;
24154
24155 return die;
24156 }
24157
24158 return NULL;
24159 }
24160
24161 /* Follow signatured type referenced by ATTR in SRC_DIE.
24162 On entry *REF_CU is the CU of SRC_DIE.
24163 On exit *REF_CU is the CU of the result.
24164 The result is the DIE of the type.
24165 If the referenced type cannot be found an error is thrown. */
24166
24167 static struct die_info *
24168 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
24169 struct dwarf2_cu **ref_cu)
24170 {
24171 ULONGEST signature = attr->as_signature ();
24172 struct signatured_type *sig_type;
24173 struct die_info *die;
24174
24175 gdb_assert (attr->form == DW_FORM_ref_sig8);
24176
24177 sig_type = lookup_signatured_type (*ref_cu, signature);
24178 /* sig_type will be NULL if the signatured type is missing from
24179 the debug info. */
24180 if (sig_type == NULL)
24181 {
24182 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24183 " from DIE at %s [in module %s]"),
24184 hex_string (signature), sect_offset_str (src_die->sect_off),
24185 objfile_name ((*ref_cu)->per_objfile->objfile));
24186 }
24187
24188 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
24189 if (die == NULL)
24190 {
24191 dump_die_for_error (src_die);
24192 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24193 " from DIE at %s [in module %s]"),
24194 hex_string (signature), sect_offset_str (src_die->sect_off),
24195 objfile_name ((*ref_cu)->per_objfile->objfile));
24196 }
24197
24198 return die;
24199 }
24200
24201 /* Get the type specified by SIGNATURE referenced in DIE/CU,
24202 reading in and processing the type unit if necessary. */
24203
24204 static struct type *
24205 get_signatured_type (struct die_info *die, ULONGEST signature,
24206 struct dwarf2_cu *cu)
24207 {
24208 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24209 struct signatured_type *sig_type;
24210 struct dwarf2_cu *type_cu;
24211 struct die_info *type_die;
24212 struct type *type;
24213
24214 sig_type = lookup_signatured_type (cu, signature);
24215 /* sig_type will be NULL if the signatured type is missing from
24216 the debug info. */
24217 if (sig_type == NULL)
24218 {
24219 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24220 " from DIE at %s [in module %s]"),
24221 hex_string (signature), sect_offset_str (die->sect_off),
24222 objfile_name (per_objfile->objfile));
24223 return build_error_marker_type (cu, die);
24224 }
24225
24226 /* If we already know the type we're done. */
24227 type = per_objfile->get_type_for_signatured_type (sig_type);
24228 if (type != nullptr)
24229 return type;
24230
24231 type_cu = cu;
24232 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24233 if (type_die != NULL)
24234 {
24235 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24236 is created. This is important, for example, because for c++ classes
24237 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24238 type = read_type_die (type_die, type_cu);
24239 if (type == NULL)
24240 {
24241 complaint (_("Dwarf Error: Cannot build signatured type %s"
24242 " referenced from DIE at %s [in module %s]"),
24243 hex_string (signature), sect_offset_str (die->sect_off),
24244 objfile_name (per_objfile->objfile));
24245 type = build_error_marker_type (cu, die);
24246 }
24247 }
24248 else
24249 {
24250 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24251 " from DIE at %s [in module %s]"),
24252 hex_string (signature), sect_offset_str (die->sect_off),
24253 objfile_name (per_objfile->objfile));
24254 type = build_error_marker_type (cu, die);
24255 }
24256
24257 per_objfile->set_type_for_signatured_type (sig_type, type);
24258
24259 return type;
24260 }
24261
24262 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24263 reading in and processing the type unit if necessary. */
24264
24265 static struct type *
24266 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
24267 struct dwarf2_cu *cu) /* ARI: editCase function */
24268 {
24269 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
24270 if (attr->form_is_ref ())
24271 {
24272 struct dwarf2_cu *type_cu = cu;
24273 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24274
24275 return read_type_die (type_die, type_cu);
24276 }
24277 else if (attr->form == DW_FORM_ref_sig8)
24278 {
24279 return get_signatured_type (die, attr->as_signature (), cu);
24280 }
24281 else
24282 {
24283 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24284
24285 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
24286 " at %s [in module %s]"),
24287 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
24288 objfile_name (per_objfile->objfile));
24289 return build_error_marker_type (cu, die);
24290 }
24291 }
24292
24293 /* Load the DIEs associated with type unit PER_CU into memory. */
24294
24295 static void
24296 load_full_type_unit (dwarf2_per_cu_data *per_cu,
24297 dwarf2_per_objfile *per_objfile)
24298 {
24299 struct signatured_type *sig_type;
24300
24301 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24302 gdb_assert (! per_cu->type_unit_group_p ());
24303
24304 /* We have the per_cu, but we need the signatured_type.
24305 Fortunately this is an easy translation. */
24306 gdb_assert (per_cu->is_debug_types);
24307 sig_type = (struct signatured_type *) per_cu;
24308
24309 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
24310
24311 read_signatured_type (sig_type, per_objfile);
24312
24313 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
24314 }
24315
24316 /* Read in a signatured type and build its CU and DIEs.
24317 If the type is a stub for the real type in a DWO file,
24318 read in the real type from the DWO file as well. */
24319
24320 static void
24321 read_signatured_type (signatured_type *sig_type,
24322 dwarf2_per_objfile *per_objfile)
24323 {
24324 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
24325
24326 gdb_assert (per_cu->is_debug_types);
24327 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
24328
24329 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
24330
24331 if (!reader.dummy_p)
24332 {
24333 struct dwarf2_cu *cu = reader.cu;
24334 const gdb_byte *info_ptr = reader.info_ptr;
24335
24336 gdb_assert (cu->die_hash == NULL);
24337 cu->die_hash =
24338 htab_create_alloc_ex (cu->header.length / 12,
24339 die_hash,
24340 die_eq,
24341 NULL,
24342 &cu->comp_unit_obstack,
24343 hashtab_obstack_allocate,
24344 dummy_obstack_deallocate);
24345
24346 if (reader.comp_unit_die->has_children)
24347 reader.comp_unit_die->child
24348 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24349 reader.comp_unit_die);
24350 cu->dies = reader.comp_unit_die;
24351 /* comp_unit_die is not stored in die_hash, no need. */
24352
24353 /* We try not to read any attributes in this function, because
24354 not all CUs needed for references have been loaded yet, and
24355 symbol table processing isn't initialized. But we have to
24356 set the CU language, or we won't be able to build types
24357 correctly. Similarly, if we do not read the producer, we can
24358 not apply producer-specific interpretation. */
24359 prepare_one_comp_unit (cu, cu->dies, language_minimal);
24360
24361 reader.keep ();
24362 }
24363
24364 sig_type->per_cu.tu_read = 1;
24365 }
24366
24367 /* Decode simple location descriptions.
24368 Given a pointer to a dwarf block that defines a location, compute
24369 the location and return the value. If COMPUTED is non-null, it is
24370 set to true to indicate that decoding was successful, and false
24371 otherwise. If COMPUTED is null, then this function may emit a
24372 complaint. */
24373
24374 static CORE_ADDR
24375 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
24376 {
24377 struct objfile *objfile = cu->per_objfile->objfile;
24378 size_t i;
24379 size_t size = blk->size;
24380 const gdb_byte *data = blk->data;
24381 CORE_ADDR stack[64];
24382 int stacki;
24383 unsigned int bytes_read, unsnd;
24384 gdb_byte op;
24385
24386 if (computed != nullptr)
24387 *computed = false;
24388
24389 i = 0;
24390 stacki = 0;
24391 stack[stacki] = 0;
24392 stack[++stacki] = 0;
24393
24394 while (i < size)
24395 {
24396 op = data[i++];
24397 switch (op)
24398 {
24399 case DW_OP_lit0:
24400 case DW_OP_lit1:
24401 case DW_OP_lit2:
24402 case DW_OP_lit3:
24403 case DW_OP_lit4:
24404 case DW_OP_lit5:
24405 case DW_OP_lit6:
24406 case DW_OP_lit7:
24407 case DW_OP_lit8:
24408 case DW_OP_lit9:
24409 case DW_OP_lit10:
24410 case DW_OP_lit11:
24411 case DW_OP_lit12:
24412 case DW_OP_lit13:
24413 case DW_OP_lit14:
24414 case DW_OP_lit15:
24415 case DW_OP_lit16:
24416 case DW_OP_lit17:
24417 case DW_OP_lit18:
24418 case DW_OP_lit19:
24419 case DW_OP_lit20:
24420 case DW_OP_lit21:
24421 case DW_OP_lit22:
24422 case DW_OP_lit23:
24423 case DW_OP_lit24:
24424 case DW_OP_lit25:
24425 case DW_OP_lit26:
24426 case DW_OP_lit27:
24427 case DW_OP_lit28:
24428 case DW_OP_lit29:
24429 case DW_OP_lit30:
24430 case DW_OP_lit31:
24431 stack[++stacki] = op - DW_OP_lit0;
24432 break;
24433
24434 case DW_OP_reg0:
24435 case DW_OP_reg1:
24436 case DW_OP_reg2:
24437 case DW_OP_reg3:
24438 case DW_OP_reg4:
24439 case DW_OP_reg5:
24440 case DW_OP_reg6:
24441 case DW_OP_reg7:
24442 case DW_OP_reg8:
24443 case DW_OP_reg9:
24444 case DW_OP_reg10:
24445 case DW_OP_reg11:
24446 case DW_OP_reg12:
24447 case DW_OP_reg13:
24448 case DW_OP_reg14:
24449 case DW_OP_reg15:
24450 case DW_OP_reg16:
24451 case DW_OP_reg17:
24452 case DW_OP_reg18:
24453 case DW_OP_reg19:
24454 case DW_OP_reg20:
24455 case DW_OP_reg21:
24456 case DW_OP_reg22:
24457 case DW_OP_reg23:
24458 case DW_OP_reg24:
24459 case DW_OP_reg25:
24460 case DW_OP_reg26:
24461 case DW_OP_reg27:
24462 case DW_OP_reg28:
24463 case DW_OP_reg29:
24464 case DW_OP_reg30:
24465 case DW_OP_reg31:
24466 stack[++stacki] = op - DW_OP_reg0;
24467 if (i < size)
24468 {
24469 if (computed == nullptr)
24470 dwarf2_complex_location_expr_complaint ();
24471 else
24472 return 0;
24473 }
24474 break;
24475
24476 case DW_OP_regx:
24477 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24478 i += bytes_read;
24479 stack[++stacki] = unsnd;
24480 if (i < size)
24481 {
24482 if (computed == nullptr)
24483 dwarf2_complex_location_expr_complaint ();
24484 else
24485 return 0;
24486 }
24487 break;
24488
24489 case DW_OP_addr:
24490 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24491 &bytes_read);
24492 i += bytes_read;
24493 break;
24494
24495 case DW_OP_const1u:
24496 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24497 i += 1;
24498 break;
24499
24500 case DW_OP_const1s:
24501 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24502 i += 1;
24503 break;
24504
24505 case DW_OP_const2u:
24506 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24507 i += 2;
24508 break;
24509
24510 case DW_OP_const2s:
24511 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24512 i += 2;
24513 break;
24514
24515 case DW_OP_const4u:
24516 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24517 i += 4;
24518 break;
24519
24520 case DW_OP_const4s:
24521 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24522 i += 4;
24523 break;
24524
24525 case DW_OP_const8u:
24526 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24527 i += 8;
24528 break;
24529
24530 case DW_OP_constu:
24531 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24532 &bytes_read);
24533 i += bytes_read;
24534 break;
24535
24536 case DW_OP_consts:
24537 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24538 i += bytes_read;
24539 break;
24540
24541 case DW_OP_dup:
24542 stack[stacki + 1] = stack[stacki];
24543 stacki++;
24544 break;
24545
24546 case DW_OP_plus:
24547 stack[stacki - 1] += stack[stacki];
24548 stacki--;
24549 break;
24550
24551 case DW_OP_plus_uconst:
24552 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24553 &bytes_read);
24554 i += bytes_read;
24555 break;
24556
24557 case DW_OP_minus:
24558 stack[stacki - 1] -= stack[stacki];
24559 stacki--;
24560 break;
24561
24562 case DW_OP_deref:
24563 /* If we're not the last op, then we definitely can't encode
24564 this using GDB's address_class enum. This is valid for partial
24565 global symbols, although the variable's address will be bogus
24566 in the psymtab. */
24567 if (i < size)
24568 {
24569 if (computed == nullptr)
24570 dwarf2_complex_location_expr_complaint ();
24571 else
24572 return 0;
24573 }
24574 break;
24575
24576 case DW_OP_GNU_push_tls_address:
24577 case DW_OP_form_tls_address:
24578 /* The top of the stack has the offset from the beginning
24579 of the thread control block at which the variable is located. */
24580 /* Nothing should follow this operator, so the top of stack would
24581 be returned. */
24582 /* This is valid for partial global symbols, but the variable's
24583 address will be bogus in the psymtab. Make it always at least
24584 non-zero to not look as a variable garbage collected by linker
24585 which have DW_OP_addr 0. */
24586 if (i < size)
24587 {
24588 if (computed == nullptr)
24589 dwarf2_complex_location_expr_complaint ();
24590 else
24591 return 0;
24592 }
24593 stack[stacki]++;
24594 break;
24595
24596 case DW_OP_GNU_uninit:
24597 if (computed != nullptr)
24598 return 0;
24599 break;
24600
24601 case DW_OP_addrx:
24602 case DW_OP_GNU_addr_index:
24603 case DW_OP_GNU_const_index:
24604 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24605 &bytes_read);
24606 i += bytes_read;
24607 break;
24608
24609 default:
24610 if (computed == nullptr)
24611 {
24612 const char *name = get_DW_OP_name (op);
24613
24614 if (name)
24615 complaint (_("unsupported stack op: '%s'"),
24616 name);
24617 else
24618 complaint (_("unsupported stack op: '%02x'"),
24619 op);
24620 }
24621
24622 return (stack[stacki]);
24623 }
24624
24625 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24626 outside of the allocated space. Also enforce minimum>0. */
24627 if (stacki >= ARRAY_SIZE (stack) - 1)
24628 {
24629 if (computed == nullptr)
24630 complaint (_("location description stack overflow"));
24631 return 0;
24632 }
24633
24634 if (stacki <= 0)
24635 {
24636 if (computed == nullptr)
24637 complaint (_("location description stack underflow"));
24638 return 0;
24639 }
24640 }
24641
24642 if (computed != nullptr)
24643 *computed = true;
24644 return (stack[stacki]);
24645 }
24646
24647 /* memory allocation interface */
24648
24649 static struct dwarf_block *
24650 dwarf_alloc_block (struct dwarf2_cu *cu)
24651 {
24652 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24653 }
24654
24655 static struct die_info *
24656 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24657 {
24658 struct die_info *die;
24659 size_t size = sizeof (struct die_info);
24660
24661 if (num_attrs > 1)
24662 size += (num_attrs - 1) * sizeof (struct attribute);
24663
24664 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24665 memset (die, 0, sizeof (struct die_info));
24666 return (die);
24667 }
24668
24669 \f
24670
24671 /* Macro support. */
24672
24673 /* An overload of dwarf_decode_macros that finds the correct section
24674 and ensures it is read in before calling the other overload. */
24675
24676 static void
24677 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24678 int section_is_gnu)
24679 {
24680 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24681 struct objfile *objfile = per_objfile->objfile;
24682 const struct line_header *lh = cu->line_header;
24683 unsigned int offset_size = cu->header.offset_size;
24684 struct dwarf2_section_info *section;
24685 const char *section_name;
24686
24687 if (cu->dwo_unit != nullptr)
24688 {
24689 if (section_is_gnu)
24690 {
24691 section = &cu->dwo_unit->dwo_file->sections.macro;
24692 section_name = ".debug_macro.dwo";
24693 }
24694 else
24695 {
24696 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24697 section_name = ".debug_macinfo.dwo";
24698 }
24699 }
24700 else
24701 {
24702 if (section_is_gnu)
24703 {
24704 section = &per_objfile->per_bfd->macro;
24705 section_name = ".debug_macro";
24706 }
24707 else
24708 {
24709 section = &per_objfile->per_bfd->macinfo;
24710 section_name = ".debug_macinfo";
24711 }
24712 }
24713
24714 section->read (objfile);
24715 if (section->buffer == nullptr)
24716 {
24717 complaint (_("missing %s section"), section_name);
24718 return;
24719 }
24720
24721 buildsym_compunit *builder = cu->get_builder ();
24722
24723 struct dwarf2_section_info *str_offsets_section;
24724 struct dwarf2_section_info *str_section;
24725 ULONGEST str_offsets_base;
24726
24727 if (cu->dwo_unit != nullptr)
24728 {
24729 str_offsets_section = &cu->dwo_unit->dwo_file
24730 ->sections.str_offsets;
24731 str_section = &cu->dwo_unit->dwo_file->sections.str;
24732 str_offsets_base = cu->header.addr_size;
24733 }
24734 else
24735 {
24736 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24737 str_section = &per_objfile->per_bfd->str;
24738 str_offsets_base = *cu->str_offsets_base;
24739 }
24740
24741 dwarf_decode_macros (per_objfile, builder, section, lh,
24742 offset_size, offset, str_section, str_offsets_section,
24743 str_offsets_base, section_is_gnu);
24744 }
24745
24746 /* Return the .debug_loc section to use for CU.
24747 For DWO files use .debug_loc.dwo. */
24748
24749 static struct dwarf2_section_info *
24750 cu_debug_loc_section (struct dwarf2_cu *cu)
24751 {
24752 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24753
24754 if (cu->dwo_unit)
24755 {
24756 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24757
24758 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24759 }
24760 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24761 : &per_objfile->per_bfd->loc);
24762 }
24763
24764 /* Return the .debug_rnglists section to use for CU. */
24765 static struct dwarf2_section_info *
24766 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24767 {
24768 if (cu->header.version < 5)
24769 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24770 cu->header.version);
24771 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24772
24773 /* Make sure we read the .debug_rnglists section from the file that
24774 contains the DW_AT_ranges attribute we are reading. Normally that
24775 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24776 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24777 program. */
24778 if (cu->dwo_unit != nullptr
24779 && tag != DW_TAG_compile_unit
24780 && tag != DW_TAG_skeleton_unit)
24781 {
24782 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24783
24784 if (sections->rnglists.size > 0)
24785 return &sections->rnglists;
24786 else
24787 error (_(".debug_rnglists section is missing from .dwo file."));
24788 }
24789 return &dwarf2_per_objfile->per_bfd->rnglists;
24790 }
24791
24792 /* A helper function that fills in a dwarf2_loclist_baton. */
24793
24794 static void
24795 fill_in_loclist_baton (struct dwarf2_cu *cu,
24796 struct dwarf2_loclist_baton *baton,
24797 const struct attribute *attr)
24798 {
24799 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24800 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24801
24802 section->read (per_objfile->objfile);
24803
24804 baton->per_objfile = per_objfile;
24805 baton->per_cu = cu->per_cu;
24806 gdb_assert (baton->per_cu);
24807 /* We don't know how long the location list is, but make sure we
24808 don't run off the edge of the section. */
24809 baton->size = section->size - attr->as_unsigned ();
24810 baton->data = section->buffer + attr->as_unsigned ();
24811 if (cu->base_address.has_value ())
24812 baton->base_address = *cu->base_address;
24813 else
24814 baton->base_address = 0;
24815 baton->from_dwo = cu->dwo_unit != NULL;
24816 }
24817
24818 static void
24819 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24820 struct dwarf2_cu *cu, int is_block)
24821 {
24822 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24823 struct objfile *objfile = per_objfile->objfile;
24824 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24825
24826 if (attr->form_is_section_offset ()
24827 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24828 the section. If so, fall through to the complaint in the
24829 other branch. */
24830 && attr->as_unsigned () < section->get_size (objfile))
24831 {
24832 struct dwarf2_loclist_baton *baton;
24833
24834 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24835
24836 fill_in_loclist_baton (cu, baton, attr);
24837
24838 if (!cu->base_address.has_value ())
24839 complaint (_("Location list used without "
24840 "specifying the CU base address."));
24841
24842 SYMBOL_ACLASS_INDEX (sym) = (is_block
24843 ? dwarf2_loclist_block_index
24844 : dwarf2_loclist_index);
24845 SYMBOL_LOCATION_BATON (sym) = baton;
24846 }
24847 else
24848 {
24849 struct dwarf2_locexpr_baton *baton;
24850
24851 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24852 baton->per_objfile = per_objfile;
24853 baton->per_cu = cu->per_cu;
24854 gdb_assert (baton->per_cu);
24855
24856 if (attr->form_is_block ())
24857 {
24858 /* Note that we're just copying the block's data pointer
24859 here, not the actual data. We're still pointing into the
24860 info_buffer for SYM's objfile; right now we never release
24861 that buffer, but when we do clean up properly this may
24862 need to change. */
24863 struct dwarf_block *block = attr->as_block ();
24864 baton->size = block->size;
24865 baton->data = block->data;
24866 }
24867 else
24868 {
24869 dwarf2_invalid_attrib_class_complaint ("location description",
24870 sym->natural_name ());
24871 baton->size = 0;
24872 }
24873
24874 SYMBOL_ACLASS_INDEX (sym) = (is_block
24875 ? dwarf2_locexpr_block_index
24876 : dwarf2_locexpr_index);
24877 SYMBOL_LOCATION_BATON (sym) = baton;
24878 }
24879 }
24880
24881 /* See read.h. */
24882
24883 const comp_unit_head *
24884 dwarf2_per_cu_data::get_header () const
24885 {
24886 if (!m_header_read_in)
24887 {
24888 const gdb_byte *info_ptr
24889 = this->section->buffer + to_underlying (this->sect_off);
24890
24891 memset (&m_header, 0, sizeof (m_header));
24892
24893 read_comp_unit_head (&m_header, info_ptr, this->section,
24894 rcuh_kind::COMPILE);
24895
24896 m_header_read_in = true;
24897 }
24898
24899 return &m_header;
24900 }
24901
24902 /* See read.h. */
24903
24904 int
24905 dwarf2_per_cu_data::addr_size () const
24906 {
24907 return this->get_header ()->addr_size;
24908 }
24909
24910 /* See read.h. */
24911
24912 int
24913 dwarf2_per_cu_data::offset_size () const
24914 {
24915 return this->get_header ()->offset_size;
24916 }
24917
24918 /* See read.h. */
24919
24920 int
24921 dwarf2_per_cu_data::ref_addr_size () const
24922 {
24923 const comp_unit_head *header = this->get_header ();
24924
24925 if (header->version == 2)
24926 return header->addr_size;
24927 else
24928 return header->offset_size;
24929 }
24930
24931 /* See read.h. */
24932
24933 struct type *
24934 dwarf2_cu::addr_type () const
24935 {
24936 struct objfile *objfile = this->per_objfile->objfile;
24937 struct type *void_type = objfile_type (objfile)->builtin_void;
24938 struct type *addr_type = lookup_pointer_type (void_type);
24939 int addr_size = this->per_cu->addr_size ();
24940
24941 if (TYPE_LENGTH (addr_type) == addr_size)
24942 return addr_type;
24943
24944 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
24945 return addr_type;
24946 }
24947
24948 /* A helper function for dwarf2_find_containing_comp_unit that returns
24949 the index of the result, and that searches a vector. It will
24950 return a result even if the offset in question does not actually
24951 occur in any CU. This is separate so that it can be unit
24952 tested. */
24953
24954 static int
24955 dwarf2_find_containing_comp_unit
24956 (sect_offset sect_off,
24957 unsigned int offset_in_dwz,
24958 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
24959 {
24960 int low, high;
24961
24962 low = 0;
24963 high = all_comp_units.size () - 1;
24964 while (high > low)
24965 {
24966 struct dwarf2_per_cu_data *mid_cu;
24967 int mid = low + (high - low) / 2;
24968
24969 mid_cu = all_comp_units[mid];
24970 if (mid_cu->is_dwz > offset_in_dwz
24971 || (mid_cu->is_dwz == offset_in_dwz
24972 && mid_cu->sect_off + mid_cu->length > sect_off))
24973 high = mid;
24974 else
24975 low = mid + 1;
24976 }
24977 gdb_assert (low == high);
24978 return low;
24979 }
24980
24981 /* Locate the .debug_info compilation unit from CU's objfile which contains
24982 the DIE at OFFSET. Raises an error on failure. */
24983
24984 static struct dwarf2_per_cu_data *
24985 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24986 unsigned int offset_in_dwz,
24987 dwarf2_per_objfile *per_objfile)
24988 {
24989 int low = dwarf2_find_containing_comp_unit
24990 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24991 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
24992
24993 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24994 {
24995 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24996 error (_("Dwarf Error: could not find partial DIE containing "
24997 "offset %s [in module %s]"),
24998 sect_offset_str (sect_off),
24999 bfd_get_filename (per_objfile->objfile->obfd));
25000
25001 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
25002 <= sect_off);
25003 return per_objfile->per_bfd->all_comp_units[low-1];
25004 }
25005 else
25006 {
25007 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
25008 && sect_off >= this_cu->sect_off + this_cu->length)
25009 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25010 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25011 return this_cu;
25012 }
25013 }
25014
25015 #if GDB_SELF_TEST
25016
25017 namespace selftests {
25018 namespace find_containing_comp_unit {
25019
25020 static void
25021 run_test ()
25022 {
25023 struct dwarf2_per_cu_data one {};
25024 struct dwarf2_per_cu_data two {};
25025 struct dwarf2_per_cu_data three {};
25026 struct dwarf2_per_cu_data four {};
25027
25028 one.length = 5;
25029 two.sect_off = sect_offset (one.length);
25030 two.length = 7;
25031
25032 three.length = 5;
25033 three.is_dwz = 1;
25034 four.sect_off = sect_offset (three.length);
25035 four.length = 7;
25036 four.is_dwz = 1;
25037
25038 std::vector<dwarf2_per_cu_data *> units;
25039 units.push_back (&one);
25040 units.push_back (&two);
25041 units.push_back (&three);
25042 units.push_back (&four);
25043
25044 int result;
25045
25046 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
25047 SELF_CHECK (units[result] == &one);
25048 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
25049 SELF_CHECK (units[result] == &one);
25050 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
25051 SELF_CHECK (units[result] == &two);
25052
25053 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
25054 SELF_CHECK (units[result] == &three);
25055 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
25056 SELF_CHECK (units[result] == &three);
25057 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
25058 SELF_CHECK (units[result] == &four);
25059 }
25060
25061 }
25062 }
25063
25064 #endif /* GDB_SELF_TEST */
25065
25066 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
25067
25068 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
25069 dwarf2_per_objfile *per_objfile)
25070 : per_cu (per_cu),
25071 per_objfile (per_objfile),
25072 mark (false),
25073 has_loclist (false),
25074 checked_producer (false),
25075 producer_is_gxx_lt_4_6 (false),
25076 producer_is_gcc_lt_4_3 (false),
25077 producer_is_icc (false),
25078 producer_is_icc_lt_14 (false),
25079 producer_is_codewarrior (false),
25080 processing_has_namespace_info (false)
25081 {
25082 }
25083
25084 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25085
25086 static void
25087 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25088 enum language pretend_language)
25089 {
25090 struct attribute *attr;
25091
25092 /* Set the language we're debugging. */
25093 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25094 if (attr != nullptr)
25095 set_cu_language (attr->constant_value (0), cu);
25096 else
25097 {
25098 cu->language = pretend_language;
25099 cu->language_defn = language_def (cu->language);
25100 }
25101
25102 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25103 }
25104
25105 /* See read.h. */
25106
25107 dwarf2_cu *
25108 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
25109 {
25110 auto it = m_dwarf2_cus.find (per_cu);
25111 if (it == m_dwarf2_cus.end ())
25112 return nullptr;
25113
25114 return it->second;
25115 }
25116
25117 /* See read.h. */
25118
25119 void
25120 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
25121 {
25122 gdb_assert (this->get_cu (per_cu) == nullptr);
25123
25124 m_dwarf2_cus[per_cu] = cu;
25125 }
25126
25127 /* See read.h. */
25128
25129 void
25130 dwarf2_per_objfile::age_comp_units ()
25131 {
25132 dwarf_read_debug_printf_v ("running");
25133
25134 /* This is not expected to be called in the middle of CU expansion. There is
25135 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
25136 loaded in memory. Calling age_comp_units while the queue is in use could
25137 make us free the DIEs for a CU that is in the queue and therefore break
25138 that invariant. */
25139 gdb_assert (!this->per_bfd->queue.has_value ());
25140
25141 /* Start by clearing all marks. */
25142 for (auto pair : m_dwarf2_cus)
25143 pair.second->mark = false;
25144
25145 /* Traverse all CUs, mark them and their dependencies if used recently
25146 enough. */
25147 for (auto pair : m_dwarf2_cus)
25148 {
25149 dwarf2_cu *cu = pair.second;
25150
25151 cu->last_used++;
25152 if (cu->last_used <= dwarf_max_cache_age)
25153 dwarf2_mark (cu);
25154 }
25155
25156 /* Delete all CUs still not marked. */
25157 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
25158 {
25159 dwarf2_cu *cu = it->second;
25160
25161 if (!cu->mark)
25162 {
25163 dwarf_read_debug_printf_v ("deleting old CU %s",
25164 sect_offset_str (cu->per_cu->sect_off));
25165 delete cu;
25166 it = m_dwarf2_cus.erase (it);
25167 }
25168 else
25169 it++;
25170 }
25171 }
25172
25173 /* See read.h. */
25174
25175 void
25176 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
25177 {
25178 auto it = m_dwarf2_cus.find (per_cu);
25179 if (it == m_dwarf2_cus.end ())
25180 return;
25181
25182 delete it->second;
25183
25184 m_dwarf2_cus.erase (it);
25185 }
25186
25187 dwarf2_per_objfile::~dwarf2_per_objfile ()
25188 {
25189 remove_all_cus ();
25190 }
25191
25192 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25193 We store these in a hash table separate from the DIEs, and preserve them
25194 when the DIEs are flushed out of cache.
25195
25196 The CU "per_cu" pointer is needed because offset alone is not enough to
25197 uniquely identify the type. A file may have multiple .debug_types sections,
25198 or the type may come from a DWO file. Furthermore, while it's more logical
25199 to use per_cu->section+offset, with Fission the section with the data is in
25200 the DWO file but we don't know that section at the point we need it.
25201 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25202 because we can enter the lookup routine, get_die_type_at_offset, from
25203 outside this file, and thus won't necessarily have PER_CU->cu.
25204 Fortunately, PER_CU is stable for the life of the objfile. */
25205
25206 struct dwarf2_per_cu_offset_and_type
25207 {
25208 const struct dwarf2_per_cu_data *per_cu;
25209 sect_offset sect_off;
25210 struct type *type;
25211 };
25212
25213 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25214
25215 static hashval_t
25216 per_cu_offset_and_type_hash (const void *item)
25217 {
25218 const struct dwarf2_per_cu_offset_and_type *ofs
25219 = (const struct dwarf2_per_cu_offset_and_type *) item;
25220
25221 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25222 }
25223
25224 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25225
25226 static int
25227 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25228 {
25229 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25230 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25231 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25232 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25233
25234 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25235 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25236 }
25237
25238 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25239 table if necessary. For convenience, return TYPE.
25240
25241 The DIEs reading must have careful ordering to:
25242 * Not cause infinite loops trying to read in DIEs as a prerequisite for
25243 reading current DIE.
25244 * Not trying to dereference contents of still incompletely read in types
25245 while reading in other DIEs.
25246 * Enable referencing still incompletely read in types just by a pointer to
25247 the type without accessing its fields.
25248
25249 Therefore caller should follow these rules:
25250 * Try to fetch any prerequisite types we may need to build this DIE type
25251 before building the type and calling set_die_type.
25252 * After building type call set_die_type for current DIE as soon as
25253 possible before fetching more types to complete the current type.
25254 * Make the type as complete as possible before fetching more types. */
25255
25256 static struct type *
25257 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
25258 bool skip_data_location)
25259 {
25260 dwarf2_per_objfile *per_objfile = cu->per_objfile;
25261 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25262 struct objfile *objfile = per_objfile->objfile;
25263 struct attribute *attr;
25264 struct dynamic_prop prop;
25265
25266 /* For Ada types, make sure that the gnat-specific data is always
25267 initialized (if not already set). There are a few types where
25268 we should not be doing so, because the type-specific area is
25269 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25270 where the type-specific area is used to store the floatformat).
25271 But this is not a problem, because the gnat-specific information
25272 is actually not needed for these types. */
25273 if (need_gnat_info (cu)
25274 && type->code () != TYPE_CODE_FUNC
25275 && type->code () != TYPE_CODE_FLT
25276 && type->code () != TYPE_CODE_METHODPTR
25277 && type->code () != TYPE_CODE_MEMBERPTR
25278 && type->code () != TYPE_CODE_METHOD
25279 && type->code () != TYPE_CODE_FIXED_POINT
25280 && !HAVE_GNAT_AUX_INFO (type))
25281 INIT_GNAT_SPECIFIC (type);
25282
25283 /* Read DW_AT_allocated and set in type. */
25284 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25285 if (attr != NULL)
25286 {
25287 struct type *prop_type = cu->addr_sized_int_type (false);
25288 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25289 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
25290 }
25291
25292 /* Read DW_AT_associated and set in type. */
25293 attr = dwarf2_attr (die, DW_AT_associated, cu);
25294 if (attr != NULL)
25295 {
25296 struct type *prop_type = cu->addr_sized_int_type (false);
25297 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25298 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
25299 }
25300
25301 /* Read DW_AT_data_location and set in type. */
25302 if (!skip_data_location)
25303 {
25304 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25305 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
25306 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
25307 }
25308
25309 if (per_objfile->die_type_hash == NULL)
25310 per_objfile->die_type_hash
25311 = htab_up (htab_create_alloc (127,
25312 per_cu_offset_and_type_hash,
25313 per_cu_offset_and_type_eq,
25314 NULL, xcalloc, xfree));
25315
25316 ofs.per_cu = cu->per_cu;
25317 ofs.sect_off = die->sect_off;
25318 ofs.type = type;
25319 slot = (struct dwarf2_per_cu_offset_and_type **)
25320 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
25321 if (*slot)
25322 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25323 sect_offset_str (die->sect_off));
25324 *slot = XOBNEW (&objfile->objfile_obstack,
25325 struct dwarf2_per_cu_offset_and_type);
25326 **slot = ofs;
25327 return type;
25328 }
25329
25330 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25331 or return NULL if the die does not have a saved type. */
25332
25333 static struct type *
25334 get_die_type_at_offset (sect_offset sect_off,
25335 dwarf2_per_cu_data *per_cu,
25336 dwarf2_per_objfile *per_objfile)
25337 {
25338 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25339
25340 if (per_objfile->die_type_hash == NULL)
25341 return NULL;
25342
25343 ofs.per_cu = per_cu;
25344 ofs.sect_off = sect_off;
25345 slot = ((struct dwarf2_per_cu_offset_and_type *)
25346 htab_find (per_objfile->die_type_hash.get (), &ofs));
25347 if (slot)
25348 return slot->type;
25349 else
25350 return NULL;
25351 }
25352
25353 /* Look up the type for DIE in CU in die_type_hash,
25354 or return NULL if DIE does not have a saved type. */
25355
25356 static struct type *
25357 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25358 {
25359 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
25360 }
25361
25362 /* Add a dependence relationship from CU to REF_PER_CU. */
25363
25364 static void
25365 dwarf2_add_dependence (struct dwarf2_cu *cu,
25366 struct dwarf2_per_cu_data *ref_per_cu)
25367 {
25368 void **slot;
25369
25370 if (cu->dependencies == NULL)
25371 cu->dependencies
25372 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25373 NULL, &cu->comp_unit_obstack,
25374 hashtab_obstack_allocate,
25375 dummy_obstack_deallocate);
25376
25377 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25378 if (*slot == NULL)
25379 *slot = ref_per_cu;
25380 }
25381
25382 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25383 Set the mark field in every compilation unit in the
25384 cache that we must keep because we are keeping CU.
25385
25386 DATA is the dwarf2_per_objfile object in which to look up CUs. */
25387
25388 static int
25389 dwarf2_mark_helper (void **slot, void *data)
25390 {
25391 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
25392 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
25393 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
25394
25395 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25396 reading of the chain. As such dependencies remain valid it is not much
25397 useful to track and undo them during QUIT cleanups. */
25398 if (cu == nullptr)
25399 return 1;
25400
25401 if (cu->mark)
25402 return 1;
25403
25404 cu->mark = true;
25405
25406 if (cu->dependencies != nullptr)
25407 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
25408
25409 return 1;
25410 }
25411
25412 /* Set the mark field in CU and in every other compilation unit in the
25413 cache that we must keep because we are keeping CU. */
25414
25415 static void
25416 dwarf2_mark (struct dwarf2_cu *cu)
25417 {
25418 if (cu->mark)
25419 return;
25420
25421 cu->mark = true;
25422
25423 if (cu->dependencies != nullptr)
25424 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
25425 }
25426
25427 /* Trivial hash function for partial_die_info: the hash value of a DIE
25428 is its offset in .debug_info for this objfile. */
25429
25430 static hashval_t
25431 partial_die_hash (const void *item)
25432 {
25433 const struct partial_die_info *part_die
25434 = (const struct partial_die_info *) item;
25435
25436 return to_underlying (part_die->sect_off);
25437 }
25438
25439 /* Trivial comparison function for partial_die_info structures: two DIEs
25440 are equal if they have the same offset. */
25441
25442 static int
25443 partial_die_eq (const void *item_lhs, const void *item_rhs)
25444 {
25445 const struct partial_die_info *part_die_lhs
25446 = (const struct partial_die_info *) item_lhs;
25447 const struct partial_die_info *part_die_rhs
25448 = (const struct partial_die_info *) item_rhs;
25449
25450 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25451 }
25452
25453 struct cmd_list_element *set_dwarf_cmdlist;
25454 struct cmd_list_element *show_dwarf_cmdlist;
25455
25456 static void
25457 show_check_physname (struct ui_file *file, int from_tty,
25458 struct cmd_list_element *c, const char *value)
25459 {
25460 fprintf_filtered (file,
25461 _("Whether to check \"physname\" is %s.\n"),
25462 value);
25463 }
25464
25465 void _initialize_dwarf2_read ();
25466 void
25467 _initialize_dwarf2_read ()
25468 {
25469 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
25470 Set DWARF specific variables.\n\
25471 Configure DWARF variables such as the cache size."),
25472 &set_dwarf_cmdlist, "maintenance set dwarf ",
25473 0/*allow-unknown*/, &maintenance_set_cmdlist);
25474
25475 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
25476 Show DWARF specific variables.\n\
25477 Show DWARF variables such as the cache size."),
25478 &show_dwarf_cmdlist, "maintenance show dwarf ",
25479 0/*allow-unknown*/, &maintenance_show_cmdlist);
25480
25481 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25482 &dwarf_max_cache_age, _("\
25483 Set the upper bound on the age of cached DWARF compilation units."), _("\
25484 Show the upper bound on the age of cached DWARF compilation units."), _("\
25485 A higher limit means that cached compilation units will be stored\n\
25486 in memory longer, and more total memory will be used. Zero disables\n\
25487 caching, which can slow down startup."),
25488 NULL,
25489 show_dwarf_max_cache_age,
25490 &set_dwarf_cmdlist,
25491 &show_dwarf_cmdlist);
25492
25493 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25494 Set debugging of the DWARF reader."), _("\
25495 Show debugging of the DWARF reader."), _("\
25496 When enabled (non-zero), debugging messages are printed during DWARF\n\
25497 reading and symtab expansion. A value of 1 (one) provides basic\n\
25498 information. A value greater than 1 provides more verbose information."),
25499 NULL,
25500 NULL,
25501 &setdebuglist, &showdebuglist);
25502
25503 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25504 Set debugging of the DWARF DIE reader."), _("\
25505 Show debugging of the DWARF DIE reader."), _("\
25506 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25507 The value is the maximum depth to print."),
25508 NULL,
25509 NULL,
25510 &setdebuglist, &showdebuglist);
25511
25512 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25513 Set debugging of the dwarf line reader."), _("\
25514 Show debugging of the dwarf line reader."), _("\
25515 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25516 A value of 1 (one) provides basic information.\n\
25517 A value greater than 1 provides more verbose information."),
25518 NULL,
25519 NULL,
25520 &setdebuglist, &showdebuglist);
25521
25522 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25523 Set cross-checking of \"physname\" code against demangler."), _("\
25524 Show cross-checking of \"physname\" code against demangler."), _("\
25525 When enabled, GDB's internal \"physname\" code is checked against\n\
25526 the demangler."),
25527 NULL, show_check_physname,
25528 &setdebuglist, &showdebuglist);
25529
25530 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25531 no_class, &use_deprecated_index_sections, _("\
25532 Set whether to use deprecated gdb_index sections."), _("\
25533 Show whether to use deprecated gdb_index sections."), _("\
25534 When enabled, deprecated .gdb_index sections are used anyway.\n\
25535 Normally they are ignored either because of a missing feature or\n\
25536 performance issue.\n\
25537 Warning: This option must be enabled before gdb reads the file."),
25538 NULL,
25539 NULL,
25540 &setlist, &showlist);
25541
25542 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25543 &dwarf2_locexpr_funcs);
25544 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25545 &dwarf2_loclist_funcs);
25546
25547 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25548 &dwarf2_block_frame_base_locexpr_funcs);
25549 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25550 &dwarf2_block_frame_base_loclist_funcs);
25551
25552 #if GDB_SELF_TEST
25553 selftests::register_test ("dw2_expand_symtabs_matching",
25554 selftests::dw2_expand_symtabs_matching::run_test);
25555 selftests::register_test ("dwarf2_find_containing_comp_unit",
25556 selftests::find_containing_comp_unit::run_test);
25557 #endif
25558 }
This page took 0.55069 seconds and 5 git commands to generate.