Avoid crash on missing dwz file
[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 "bfd.h"
46 #include "elf-bfd.h"
47 #include "symtab.h"
48 #include "gdbtypes.h"
49 #include "objfiles.h"
50 #include "dwarf2.h"
51 #include "buildsym.h"
52 #include "demangle.h"
53 #include "gdb-demangle.h"
54 #include "filenames.h" /* for DOSish file names */
55 #include "language.h"
56 #include "complaints.h"
57 #include "dwarf2/expr.h"
58 #include "dwarf2/loc.h"
59 #include "cp-support.h"
60 #include "hashtab.h"
61 #include "command.h"
62 #include "gdbcmd.h"
63 #include "block.h"
64 #include "addrmap.h"
65 #include "typeprint.h"
66 #include "psympriv.h"
67 #include "c-lang.h"
68 #include "go-lang.h"
69 #include "valprint.h"
70 #include "gdbcore.h" /* for gnutarget */
71 #include "gdb/gdb-index.h"
72 #include "gdb_bfd.h"
73 #include "f-lang.h"
74 #include "source.h"
75 #include "build-id.h"
76 #include "namespace.h"
77 #include "gdbsupport/function-view.h"
78 #include "gdbsupport/gdb_optional.h"
79 #include "gdbsupport/underlying.h"
80 #include "gdbsupport/hash_enum.h"
81 #include "filename-seen-cache.h"
82 #include "producer.h"
83 #include <fcntl.h>
84 #include <algorithm>
85 #include <unordered_map>
86 #include "gdbsupport/selftest.h"
87 #include "rust-lang.h"
88 #include "gdbsupport/pathstuff.h"
89 #include "count-one-bits.h"
90 #include "debuginfod-support.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 static 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, 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 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 (objfile)
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 /* A helper function to find the sections for a .dwz file. */
2153
2154 static void
2155 locate_dwz_sections (bfd *abfd, asection *sectp, dwz_file *dwz_file)
2156 {
2157 /* Note that we only support the standard ELF names, because .dwz
2158 is ELF-only (at the time of writing). */
2159 if (dwarf2_elf_names.abbrev.matches (sectp->name))
2160 {
2161 dwz_file->abbrev.s.section = sectp;
2162 dwz_file->abbrev.size = bfd_section_size (sectp);
2163 }
2164 else if (dwarf2_elf_names.info.matches (sectp->name))
2165 {
2166 dwz_file->info.s.section = sectp;
2167 dwz_file->info.size = bfd_section_size (sectp);
2168 }
2169 else if (dwarf2_elf_names.str.matches (sectp->name))
2170 {
2171 dwz_file->str.s.section = sectp;
2172 dwz_file->str.size = bfd_section_size (sectp);
2173 }
2174 else if (dwarf2_elf_names.line.matches (sectp->name))
2175 {
2176 dwz_file->line.s.section = sectp;
2177 dwz_file->line.size = bfd_section_size (sectp);
2178 }
2179 else if (dwarf2_elf_names.macro.matches (sectp->name))
2180 {
2181 dwz_file->macro.s.section = sectp;
2182 dwz_file->macro.size = bfd_section_size (sectp);
2183 }
2184 else if (dwarf2_elf_names.gdb_index.matches (sectp->name))
2185 {
2186 dwz_file->gdb_index.s.section = sectp;
2187 dwz_file->gdb_index.size = bfd_section_size (sectp);
2188 }
2189 else if (dwarf2_elf_names.debug_names.matches (sectp->name))
2190 {
2191 dwz_file->debug_names.s.section = sectp;
2192 dwz_file->debug_names.size = bfd_section_size (sectp);
2193 }
2194 }
2195
2196 /* Attempt to find a .dwz file (whose full path is represented by
2197 FILENAME) in all of the specified debug file directories provided.
2198
2199 Return the equivalent gdb_bfd_ref_ptr of the .dwz file found, or
2200 nullptr if it could not find anything. */
2201
2202 static gdb_bfd_ref_ptr
2203 dwz_search_other_debugdirs (std::string &filename, bfd_byte *buildid,
2204 size_t buildid_len)
2205 {
2206 /* Let's assume that the path represented by FILENAME has the
2207 "/.dwz/" subpath in it. This is what (most) GNU/Linux
2208 distributions do, anyway. */
2209 size_t dwz_pos = filename.find ("/.dwz/");
2210
2211 if (dwz_pos == std::string::npos)
2212 return nullptr;
2213
2214 /* This is an obvious assertion, but it's here more to educate
2215 future readers of this code that FILENAME at DWZ_POS *must*
2216 contain a directory separator. */
2217 gdb_assert (IS_DIR_SEPARATOR (filename[dwz_pos]));
2218
2219 gdb_bfd_ref_ptr dwz_bfd;
2220 std::vector<gdb::unique_xmalloc_ptr<char>> debugdir_vec
2221 = dirnames_to_char_ptr_vec (debug_file_directory);
2222
2223 for (const gdb::unique_xmalloc_ptr<char> &debugdir : debugdir_vec)
2224 {
2225 /* The idea is to iterate over the
2226 debug file directories provided by the user and
2227 replace the hard-coded path in the "filename" by each
2228 debug-file-directory.
2229
2230 For example, suppose that filename is:
2231
2232 /usr/lib/debug/.dwz/foo.dwz
2233
2234 And suppose that we have "$HOME/bar" as the
2235 debug-file-directory. We would then adjust filename
2236 to look like:
2237
2238 $HOME/bar/.dwz/foo.dwz
2239
2240 which would hopefully allow us to find the alt debug
2241 file. */
2242 std::string ddir = debugdir.get ();
2243
2244 if (ddir.empty ())
2245 continue;
2246
2247 /* Make sure the current debug-file-directory ends with a
2248 directory separator. This is needed because, if FILENAME
2249 contains something like "/usr/lib/abcde/.dwz/foo.dwz" and
2250 DDIR is "/usr/lib/abc", then could wrongfully skip it
2251 below. */
2252 if (!IS_DIR_SEPARATOR (ddir.back ()))
2253 ddir += SLASH_STRING;
2254
2255 /* Check whether the beginning of FILENAME is DDIR. If it is,
2256 then we are dealing with a file which we already attempted to
2257 open before, so we just skip it and continue processing the
2258 remaining debug file directories. */
2259 if (filename.size () > ddir.size ()
2260 && filename.compare (0, ddir.size (), ddir) == 0)
2261 continue;
2262
2263 /* Replace FILENAME's default debug-file-directory with
2264 DDIR. */
2265 std::string new_filename = ddir + &filename[dwz_pos + 1];
2266
2267 dwz_bfd = gdb_bfd_open (new_filename.c_str (), gnutarget);
2268
2269 if (dwz_bfd == nullptr)
2270 continue;
2271
2272 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2273 {
2274 dwz_bfd.reset (nullptr);
2275 continue;
2276 }
2277
2278 /* Found it. */
2279 break;
2280 }
2281
2282 return dwz_bfd;
2283 }
2284
2285 /* See dwarf2read.h. */
2286
2287 struct dwz_file *
2288 dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd, bool require)
2289 {
2290 bfd_size_type buildid_len_arg;
2291 size_t buildid_len;
2292 bfd_byte *buildid;
2293
2294 if (per_bfd->dwz_file != NULL)
2295 return per_bfd->dwz_file.get ();
2296
2297 bfd_set_error (bfd_error_no_error);
2298 gdb::unique_xmalloc_ptr<char> data
2299 (bfd_get_alt_debug_link_info (per_bfd->obfd,
2300 &buildid_len_arg, &buildid));
2301 if (data == NULL)
2302 {
2303 if (bfd_get_error () == bfd_error_no_error)
2304 {
2305 if (!require)
2306 return nullptr;
2307 error (_("could not read '.gnu_debugaltlink' section"));
2308 }
2309 error (_("could not read '.gnu_debugaltlink' section: %s"),
2310 bfd_errmsg (bfd_get_error ()));
2311 }
2312
2313 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2314
2315 buildid_len = (size_t) buildid_len_arg;
2316
2317 std::string filename = data.get ();
2318
2319 if (!IS_ABSOLUTE_PATH (filename.c_str ()))
2320 {
2321 gdb::unique_xmalloc_ptr<char> abs
2322 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
2323
2324 filename = ldirname (abs.get ()) + SLASH_STRING + filename;
2325 }
2326
2327 /* First try the file name given in the section. If that doesn't
2328 work, try to use the build-id instead. */
2329 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename.c_str (), gnutarget));
2330 if (dwz_bfd != NULL)
2331 {
2332 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2333 dwz_bfd.reset (nullptr);
2334 }
2335
2336 if (dwz_bfd == NULL)
2337 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2338
2339 if (dwz_bfd == nullptr)
2340 {
2341 /* If the user has provided us with different
2342 debug file directories, we can try them in order. */
2343 dwz_bfd = dwz_search_other_debugdirs (filename, buildid, buildid_len);
2344 }
2345
2346 if (dwz_bfd == nullptr)
2347 {
2348 gdb::unique_xmalloc_ptr<char> alt_filename;
2349 const char *origname = bfd_get_filename (per_bfd->obfd);
2350
2351 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2352 buildid_len,
2353 origname,
2354 &alt_filename));
2355
2356 if (fd.get () >= 0)
2357 {
2358 /* File successfully retrieved from server. */
2359 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
2360
2361 if (dwz_bfd == nullptr)
2362 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2363 alt_filename.get ());
2364 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2365 dwz_bfd.reset (nullptr);
2366 }
2367 }
2368
2369 if (dwz_bfd == NULL)
2370 error (_("could not find '.gnu_debugaltlink' file for %s"),
2371 bfd_get_filename (per_bfd->obfd));
2372
2373 std::unique_ptr<struct dwz_file> result
2374 (new struct dwz_file (std::move (dwz_bfd)));
2375
2376 for (asection *sec : gdb_bfd_sections (result->dwz_bfd))
2377 locate_dwz_sections (result->dwz_bfd.get (), sec, result.get ());
2378
2379 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2380 per_bfd->dwz_file = std::move (result);
2381 return per_bfd->dwz_file.get ();
2382 }
2383 \f
2384 /* DWARF quick_symbols_functions support. */
2385
2386 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2387 unique line tables, so we maintain a separate table of all .debug_line
2388 derived entries to support the sharing.
2389 All the quick functions need is the list of file names. We discard the
2390 line_header when we're done and don't need to record it here. */
2391 struct quick_file_names
2392 {
2393 /* The data used to construct the hash key. */
2394 struct stmt_list_hash hash;
2395
2396 /* The number of entries in file_names, real_names. */
2397 unsigned int num_file_names;
2398
2399 /* The file names from the line table, after being run through
2400 file_full_name. */
2401 const char **file_names;
2402
2403 /* The file names from the line table after being run through
2404 gdb_realpath. These are computed lazily. */
2405 const char **real_names;
2406 };
2407
2408 /* When using the index (and thus not using psymtabs), each CU has an
2409 object of this type. This is used to hold information needed by
2410 the various "quick" methods. */
2411 struct dwarf2_per_cu_quick_data
2412 {
2413 /* The file table. This can be NULL if there was no file table
2414 or it's currently not read in.
2415 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2416 struct quick_file_names *file_names;
2417
2418 /* A temporary mark bit used when iterating over all CUs in
2419 expand_symtabs_matching. */
2420 unsigned int mark : 1;
2421
2422 /* True if we've tried to read the file table and found there isn't one.
2423 There will be no point in trying to read it again next time. */
2424 unsigned int no_file_data : 1;
2425 };
2426
2427 /* Utility hash function for a stmt_list_hash. */
2428
2429 static hashval_t
2430 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2431 {
2432 hashval_t v = 0;
2433
2434 if (stmt_list_hash->dwo_unit != NULL)
2435 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2436 v += to_underlying (stmt_list_hash->line_sect_off);
2437 return v;
2438 }
2439
2440 /* Utility equality function for a stmt_list_hash. */
2441
2442 static int
2443 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2444 const struct stmt_list_hash *rhs)
2445 {
2446 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2447 return 0;
2448 if (lhs->dwo_unit != NULL
2449 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2450 return 0;
2451
2452 return lhs->line_sect_off == rhs->line_sect_off;
2453 }
2454
2455 /* Hash function for a quick_file_names. */
2456
2457 static hashval_t
2458 hash_file_name_entry (const void *e)
2459 {
2460 const struct quick_file_names *file_data
2461 = (const struct quick_file_names *) e;
2462
2463 return hash_stmt_list_entry (&file_data->hash);
2464 }
2465
2466 /* Equality function for a quick_file_names. */
2467
2468 static int
2469 eq_file_name_entry (const void *a, const void *b)
2470 {
2471 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2472 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2473
2474 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2475 }
2476
2477 /* Delete function for a quick_file_names. */
2478
2479 static void
2480 delete_file_name_entry (void *e)
2481 {
2482 struct quick_file_names *file_data = (struct quick_file_names *) e;
2483 int i;
2484
2485 for (i = 0; i < file_data->num_file_names; ++i)
2486 {
2487 xfree ((void*) file_data->file_names[i]);
2488 if (file_data->real_names)
2489 xfree ((void*) file_data->real_names[i]);
2490 }
2491
2492 /* The space for the struct itself lives on the obstack, so we don't
2493 free it here. */
2494 }
2495
2496 /* Create a quick_file_names hash table. */
2497
2498 static htab_up
2499 create_quick_file_names_table (unsigned int nr_initial_entries)
2500 {
2501 return htab_up (htab_create_alloc (nr_initial_entries,
2502 hash_file_name_entry, eq_file_name_entry,
2503 delete_file_name_entry, xcalloc, xfree));
2504 }
2505
2506 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2507 function is unrelated to symtabs, symtab would have to be created afterwards.
2508 You should call age_cached_comp_units after processing the CU. */
2509
2510 static dwarf2_cu *
2511 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2512 bool skip_partial)
2513 {
2514 if (per_cu->is_debug_types)
2515 load_full_type_unit (per_cu, per_objfile);
2516 else
2517 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2518 skip_partial, language_minimal);
2519
2520 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2521 if (cu == nullptr)
2522 return nullptr; /* Dummy CU. */
2523
2524 dwarf2_find_base_address (cu->dies, cu);
2525
2526 return cu;
2527 }
2528
2529 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2530
2531 static void
2532 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2533 dwarf2_per_objfile *per_objfile, bool skip_partial)
2534 {
2535 /* Skip type_unit_groups, reading the type units they contain
2536 is handled elsewhere. */
2537 if (per_cu->type_unit_group_p ())
2538 return;
2539
2540 {
2541 /* The destructor of dwarf2_queue_guard frees any entries left on
2542 the queue. After this point we're guaranteed to leave this function
2543 with the dwarf queue empty. */
2544 dwarf2_queue_guard q_guard (per_objfile);
2545
2546 if (!per_objfile->symtab_set_p (per_cu))
2547 {
2548 queue_comp_unit (per_cu, per_objfile, language_minimal);
2549 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2550
2551 /* If we just loaded a CU from a DWO, and we're working with an index
2552 that may badly handle TUs, load all the TUs in that DWO as well.
2553 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2554 if (!per_cu->is_debug_types
2555 && cu != NULL
2556 && cu->dwo_unit != NULL
2557 && per_objfile->per_bfd->index_table != NULL
2558 && per_objfile->per_bfd->index_table->version <= 7
2559 /* DWP files aren't supported yet. */
2560 && get_dwp_file (per_objfile) == NULL)
2561 queue_and_load_all_dwo_tus (cu);
2562 }
2563
2564 process_queue (per_objfile);
2565 }
2566
2567 /* Age the cache, releasing compilation units that have not
2568 been used recently. */
2569 per_objfile->age_comp_units ();
2570 }
2571
2572 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2573 the per-objfile for which this symtab is instantiated.
2574
2575 Returns the resulting symbol table. */
2576
2577 static struct compunit_symtab *
2578 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2579 dwarf2_per_objfile *per_objfile,
2580 bool skip_partial)
2581 {
2582 gdb_assert (per_objfile->per_bfd->using_index);
2583
2584 if (!per_objfile->symtab_set_p (per_cu))
2585 {
2586 free_cached_comp_units freer (per_objfile);
2587 scoped_restore decrementer = increment_reading_symtab ();
2588 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2589 process_cu_includes (per_objfile);
2590 }
2591
2592 return per_objfile->get_symtab (per_cu);
2593 }
2594
2595 /* See declaration. */
2596
2597 dwarf2_per_cu_data *
2598 dwarf2_per_bfd::get_cutu (int index)
2599 {
2600 if (index >= this->all_comp_units.size ())
2601 {
2602 index -= this->all_comp_units.size ();
2603 gdb_assert (index < this->all_type_units.size ());
2604 return &this->all_type_units[index]->per_cu;
2605 }
2606
2607 return this->all_comp_units[index];
2608 }
2609
2610 /* See declaration. */
2611
2612 dwarf2_per_cu_data *
2613 dwarf2_per_bfd::get_cu (int index)
2614 {
2615 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2616
2617 return this->all_comp_units[index];
2618 }
2619
2620 /* See declaration. */
2621
2622 signatured_type *
2623 dwarf2_per_bfd::get_tu (int index)
2624 {
2625 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2626
2627 return this->all_type_units[index];
2628 }
2629
2630 /* See read.h. */
2631
2632 dwarf2_per_cu_data *
2633 dwarf2_per_bfd::allocate_per_cu ()
2634 {
2635 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2636 result->per_bfd = this;
2637 result->index = m_num_psymtabs++;
2638 return result;
2639 }
2640
2641 /* See read.h. */
2642
2643 signatured_type *
2644 dwarf2_per_bfd::allocate_signatured_type ()
2645 {
2646 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2647 result->per_cu.per_bfd = this;
2648 result->per_cu.index = m_num_psymtabs++;
2649 return result;
2650 }
2651
2652 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2653 obstack, and constructed with the specified field values. */
2654
2655 static dwarf2_per_cu_data *
2656 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2657 struct dwarf2_section_info *section,
2658 int is_dwz,
2659 sect_offset sect_off, ULONGEST length)
2660 {
2661 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
2662 the_cu->sect_off = sect_off;
2663 the_cu->length = length;
2664 the_cu->section = section;
2665 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2666 struct dwarf2_per_cu_quick_data);
2667 the_cu->is_dwz = is_dwz;
2668 return the_cu;
2669 }
2670
2671 /* A helper for create_cus_from_index that handles a given list of
2672 CUs. */
2673
2674 static void
2675 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2676 const gdb_byte *cu_list, offset_type n_elements,
2677 struct dwarf2_section_info *section,
2678 int is_dwz)
2679 {
2680 for (offset_type i = 0; i < n_elements; i += 2)
2681 {
2682 gdb_static_assert (sizeof (ULONGEST) >= 8);
2683
2684 sect_offset sect_off
2685 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2686 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2687 cu_list += 2 * 8;
2688
2689 dwarf2_per_cu_data *per_cu
2690 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2691 length);
2692 per_bfd->all_comp_units.push_back (per_cu);
2693 }
2694 }
2695
2696 /* Read the CU list from the mapped index, and use it to create all
2697 the CU objects for PER_BFD. */
2698
2699 static void
2700 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2701 const gdb_byte *cu_list, offset_type cu_list_elements,
2702 const gdb_byte *dwz_list, offset_type dwz_elements)
2703 {
2704 gdb_assert (per_bfd->all_comp_units.empty ());
2705 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2706
2707 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2708 &per_bfd->info, 0);
2709
2710 if (dwz_elements == 0)
2711 return;
2712
2713 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2714 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2715 &dwz->info, 1);
2716 }
2717
2718 /* Create the signatured type hash table from the index. */
2719
2720 static void
2721 create_signatured_type_table_from_index
2722 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2723 const gdb_byte *bytes, offset_type elements)
2724 {
2725 gdb_assert (per_bfd->all_type_units.empty ());
2726 per_bfd->all_type_units.reserve (elements / 3);
2727
2728 htab_up sig_types_hash = allocate_signatured_type_table ();
2729
2730 for (offset_type i = 0; i < elements; i += 3)
2731 {
2732 struct signatured_type *sig_type;
2733 ULONGEST signature;
2734 void **slot;
2735 cu_offset type_offset_in_tu;
2736
2737 gdb_static_assert (sizeof (ULONGEST) >= 8);
2738 sect_offset sect_off
2739 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2740 type_offset_in_tu
2741 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2742 BFD_ENDIAN_LITTLE);
2743 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2744 bytes += 3 * 8;
2745
2746 sig_type = per_bfd->allocate_signatured_type ();
2747 sig_type->signature = signature;
2748 sig_type->type_offset_in_tu = type_offset_in_tu;
2749 sig_type->per_cu.is_debug_types = 1;
2750 sig_type->per_cu.section = section;
2751 sig_type->per_cu.sect_off = sect_off;
2752 sig_type->per_cu.v.quick
2753 = OBSTACK_ZALLOC (&per_bfd->obstack,
2754 struct dwarf2_per_cu_quick_data);
2755
2756 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2757 *slot = sig_type;
2758
2759 per_bfd->all_type_units.push_back (sig_type);
2760 }
2761
2762 per_bfd->signatured_types = std::move (sig_types_hash);
2763 }
2764
2765 /* Create the signatured type hash table from .debug_names. */
2766
2767 static void
2768 create_signatured_type_table_from_debug_names
2769 (dwarf2_per_objfile *per_objfile,
2770 const mapped_debug_names &map,
2771 struct dwarf2_section_info *section,
2772 struct dwarf2_section_info *abbrev_section)
2773 {
2774 struct objfile *objfile = per_objfile->objfile;
2775
2776 section->read (objfile);
2777 abbrev_section->read (objfile);
2778
2779 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2780 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2781
2782 htab_up sig_types_hash = allocate_signatured_type_table ();
2783
2784 for (uint32_t i = 0; i < map.tu_count; ++i)
2785 {
2786 struct signatured_type *sig_type;
2787 void **slot;
2788
2789 sect_offset sect_off
2790 = (sect_offset) (extract_unsigned_integer
2791 (map.tu_table_reordered + i * map.offset_size,
2792 map.offset_size,
2793 map.dwarf5_byte_order));
2794
2795 comp_unit_head cu_header;
2796 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2797 abbrev_section,
2798 section->buffer + to_underlying (sect_off),
2799 rcuh_kind::TYPE);
2800
2801 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
2802 sig_type->signature = cu_header.signature;
2803 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2804 sig_type->per_cu.is_debug_types = 1;
2805 sig_type->per_cu.section = section;
2806 sig_type->per_cu.sect_off = sect_off;
2807 sig_type->per_cu.v.quick
2808 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
2809 struct dwarf2_per_cu_quick_data);
2810
2811 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2812 *slot = sig_type;
2813
2814 per_objfile->per_bfd->all_type_units.push_back (sig_type);
2815 }
2816
2817 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2818 }
2819
2820 /* Read the address map data from the mapped index, and use it to
2821 populate the objfile's psymtabs_addrmap. */
2822
2823 static void
2824 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2825 struct mapped_index *index)
2826 {
2827 struct objfile *objfile = per_objfile->objfile;
2828 struct gdbarch *gdbarch = objfile->arch ();
2829 const gdb_byte *iter, *end;
2830 struct addrmap *mutable_map;
2831 CORE_ADDR baseaddr;
2832
2833 auto_obstack temp_obstack;
2834
2835 mutable_map = addrmap_create_mutable (&temp_obstack);
2836
2837 iter = index->address_table.data ();
2838 end = iter + index->address_table.size ();
2839
2840 baseaddr = objfile->text_section_offset ();
2841
2842 while (iter < end)
2843 {
2844 ULONGEST hi, lo, cu_index;
2845 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2846 iter += 8;
2847 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2848 iter += 8;
2849 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2850 iter += 4;
2851
2852 if (lo > hi)
2853 {
2854 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2855 hex_string (lo), hex_string (hi));
2856 continue;
2857 }
2858
2859 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
2860 {
2861 complaint (_(".gdb_index address table has invalid CU number %u"),
2862 (unsigned) cu_index);
2863 continue;
2864 }
2865
2866 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2867 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2868 addrmap_set_empty (mutable_map, lo, hi - 1,
2869 per_objfile->per_bfd->get_cu (cu_index));
2870 }
2871
2872 objfile->partial_symtabs->psymtabs_addrmap
2873 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2874 }
2875
2876 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2877 populate the objfile's psymtabs_addrmap. */
2878
2879 static void
2880 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2881 struct dwarf2_section_info *section)
2882 {
2883 struct objfile *objfile = per_objfile->objfile;
2884 bfd *abfd = objfile->obfd;
2885 struct gdbarch *gdbarch = objfile->arch ();
2886 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2887
2888 auto_obstack temp_obstack;
2889 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2890
2891 std::unordered_map<sect_offset,
2892 dwarf2_per_cu_data *,
2893 gdb::hash_enum<sect_offset>>
2894 debug_info_offset_to_per_cu;
2895 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
2896 {
2897 const auto insertpair
2898 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2899 if (!insertpair.second)
2900 {
2901 warning (_("Section .debug_aranges in %s has duplicate "
2902 "debug_info_offset %s, ignoring .debug_aranges."),
2903 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2904 return;
2905 }
2906 }
2907
2908 section->read (objfile);
2909
2910 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2911
2912 const gdb_byte *addr = section->buffer;
2913
2914 while (addr < section->buffer + section->size)
2915 {
2916 const gdb_byte *const entry_addr = addr;
2917 unsigned int bytes_read;
2918
2919 const LONGEST entry_length = read_initial_length (abfd, addr,
2920 &bytes_read);
2921 addr += bytes_read;
2922
2923 const gdb_byte *const entry_end = addr + entry_length;
2924 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2925 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2926 if (addr + entry_length > section->buffer + section->size)
2927 {
2928 warning (_("Section .debug_aranges in %s entry at offset %s "
2929 "length %s exceeds section length %s, "
2930 "ignoring .debug_aranges."),
2931 objfile_name (objfile),
2932 plongest (entry_addr - section->buffer),
2933 plongest (bytes_read + entry_length),
2934 pulongest (section->size));
2935 return;
2936 }
2937
2938 /* The version number. */
2939 const uint16_t version = read_2_bytes (abfd, addr);
2940 addr += 2;
2941 if (version != 2)
2942 {
2943 warning (_("Section .debug_aranges in %s entry at offset %s "
2944 "has unsupported version %d, ignoring .debug_aranges."),
2945 objfile_name (objfile),
2946 plongest (entry_addr - section->buffer), version);
2947 return;
2948 }
2949
2950 const uint64_t debug_info_offset
2951 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2952 addr += offset_size;
2953 const auto per_cu_it
2954 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2955 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2956 {
2957 warning (_("Section .debug_aranges in %s entry at offset %s "
2958 "debug_info_offset %s does not exists, "
2959 "ignoring .debug_aranges."),
2960 objfile_name (objfile),
2961 plongest (entry_addr - section->buffer),
2962 pulongest (debug_info_offset));
2963 return;
2964 }
2965 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2966
2967 const uint8_t address_size = *addr++;
2968 if (address_size < 1 || address_size > 8)
2969 {
2970 warning (_("Section .debug_aranges in %s entry at offset %s "
2971 "address_size %u is invalid, ignoring .debug_aranges."),
2972 objfile_name (objfile),
2973 plongest (entry_addr - section->buffer), address_size);
2974 return;
2975 }
2976
2977 const uint8_t segment_selector_size = *addr++;
2978 if (segment_selector_size != 0)
2979 {
2980 warning (_("Section .debug_aranges in %s entry at offset %s "
2981 "segment_selector_size %u is not supported, "
2982 "ignoring .debug_aranges."),
2983 objfile_name (objfile),
2984 plongest (entry_addr - section->buffer),
2985 segment_selector_size);
2986 return;
2987 }
2988
2989 /* Must pad to an alignment boundary that is twice the address
2990 size. It is undocumented by the DWARF standard but GCC does
2991 use it. */
2992 for (size_t padding = ((-(addr - section->buffer))
2993 & (2 * address_size - 1));
2994 padding > 0; padding--)
2995 if (*addr++ != 0)
2996 {
2997 warning (_("Section .debug_aranges in %s entry at offset %s "
2998 "padding is not zero, ignoring .debug_aranges."),
2999 objfile_name (objfile),
3000 plongest (entry_addr - section->buffer));
3001 return;
3002 }
3003
3004 for (;;)
3005 {
3006 if (addr + 2 * address_size > entry_end)
3007 {
3008 warning (_("Section .debug_aranges in %s entry at offset %s "
3009 "address list is not properly terminated, "
3010 "ignoring .debug_aranges."),
3011 objfile_name (objfile),
3012 plongest (entry_addr - section->buffer));
3013 return;
3014 }
3015 ULONGEST start = extract_unsigned_integer (addr, address_size,
3016 dwarf5_byte_order);
3017 addr += address_size;
3018 ULONGEST length = extract_unsigned_integer (addr, address_size,
3019 dwarf5_byte_order);
3020 addr += address_size;
3021 if (start == 0 && length == 0)
3022 break;
3023 if (start == 0 && !per_objfile->per_bfd->has_section_at_zero)
3024 {
3025 /* Symbol was eliminated due to a COMDAT group. */
3026 continue;
3027 }
3028 ULONGEST end = start + length;
3029 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3030 - baseaddr);
3031 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3032 - baseaddr);
3033 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3034 }
3035 }
3036
3037 objfile->partial_symtabs->psymtabs_addrmap
3038 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3039 }
3040
3041 /* Find a slot in the mapped index INDEX for the object named NAME.
3042 If NAME is found, set *VEC_OUT to point to the CU vector in the
3043 constant pool and return true. If NAME cannot be found, return
3044 false. */
3045
3046 static bool
3047 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3048 offset_type **vec_out)
3049 {
3050 offset_type hash;
3051 offset_type slot, step;
3052 int (*cmp) (const char *, const char *);
3053
3054 gdb::unique_xmalloc_ptr<char> without_params;
3055 if (current_language->la_language == language_cplus
3056 || current_language->la_language == language_fortran
3057 || current_language->la_language == language_d)
3058 {
3059 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3060 not contain any. */
3061
3062 if (strchr (name, '(') != NULL)
3063 {
3064 without_params = cp_remove_params (name);
3065
3066 if (without_params != NULL)
3067 name = without_params.get ();
3068 }
3069 }
3070
3071 /* Index version 4 did not support case insensitive searches. But the
3072 indices for case insensitive languages are built in lowercase, therefore
3073 simulate our NAME being searched is also lowercased. */
3074 hash = mapped_index_string_hash ((index->version == 4
3075 && case_sensitivity == case_sensitive_off
3076 ? 5 : index->version),
3077 name);
3078
3079 slot = hash & (index->symbol_table.size () - 1);
3080 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3081 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3082
3083 for (;;)
3084 {
3085 const char *str;
3086
3087 const auto &bucket = index->symbol_table[slot];
3088 if (bucket.name == 0 && bucket.vec == 0)
3089 return false;
3090
3091 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3092 if (!cmp (name, str))
3093 {
3094 *vec_out = (offset_type *) (index->constant_pool
3095 + MAYBE_SWAP (bucket.vec));
3096 return true;
3097 }
3098
3099 slot = (slot + step) & (index->symbol_table.size () - 1);
3100 }
3101 }
3102
3103 /* A helper function that reads the .gdb_index from BUFFER and fills
3104 in MAP. FILENAME is the name of the file containing the data;
3105 it is used for error reporting. DEPRECATED_OK is true if it is
3106 ok to use deprecated sections.
3107
3108 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3109 out parameters that are filled in with information about the CU and
3110 TU lists in the section.
3111
3112 Returns true if all went well, false otherwise. */
3113
3114 static bool
3115 read_gdb_index_from_buffer (const char *filename,
3116 bool deprecated_ok,
3117 gdb::array_view<const gdb_byte> buffer,
3118 struct mapped_index *map,
3119 const gdb_byte **cu_list,
3120 offset_type *cu_list_elements,
3121 const gdb_byte **types_list,
3122 offset_type *types_list_elements)
3123 {
3124 const gdb_byte *addr = &buffer[0];
3125
3126 /* Version check. */
3127 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3128 /* Versions earlier than 3 emitted every copy of a psymbol. This
3129 causes the index to behave very poorly for certain requests. Version 3
3130 contained incomplete addrmap. So, it seems better to just ignore such
3131 indices. */
3132 if (version < 4)
3133 {
3134 static int warning_printed = 0;
3135 if (!warning_printed)
3136 {
3137 warning (_("Skipping obsolete .gdb_index section in %s."),
3138 filename);
3139 warning_printed = 1;
3140 }
3141 return 0;
3142 }
3143 /* Index version 4 uses a different hash function than index version
3144 5 and later.
3145
3146 Versions earlier than 6 did not emit psymbols for inlined
3147 functions. Using these files will cause GDB not to be able to
3148 set breakpoints on inlined functions by name, so we ignore these
3149 indices unless the user has done
3150 "set use-deprecated-index-sections on". */
3151 if (version < 6 && !deprecated_ok)
3152 {
3153 static int warning_printed = 0;
3154 if (!warning_printed)
3155 {
3156 warning (_("\
3157 Skipping deprecated .gdb_index section in %s.\n\
3158 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3159 to use the section anyway."),
3160 filename);
3161 warning_printed = 1;
3162 }
3163 return 0;
3164 }
3165 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3166 of the TU (for symbols coming from TUs),
3167 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3168 Plus gold-generated indices can have duplicate entries for global symbols,
3169 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3170 These are just performance bugs, and we can't distinguish gdb-generated
3171 indices from gold-generated ones, so issue no warning here. */
3172
3173 /* Indexes with higher version than the one supported by GDB may be no
3174 longer backward compatible. */
3175 if (version > 8)
3176 return 0;
3177
3178 map->version = version;
3179
3180 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3181
3182 int i = 0;
3183 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3184 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3185 / 8);
3186 ++i;
3187
3188 *types_list = addr + MAYBE_SWAP (metadata[i]);
3189 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3190 - MAYBE_SWAP (metadata[i]))
3191 / 8);
3192 ++i;
3193
3194 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3195 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3196 map->address_table
3197 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3198 ++i;
3199
3200 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3201 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3202 map->symbol_table
3203 = gdb::array_view<mapped_index::symbol_table_slot>
3204 ((mapped_index::symbol_table_slot *) symbol_table,
3205 (mapped_index::symbol_table_slot *) symbol_table_end);
3206
3207 ++i;
3208 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3209
3210 return 1;
3211 }
3212
3213 /* Callback types for dwarf2_read_gdb_index. */
3214
3215 typedef gdb::function_view
3216 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3217 get_gdb_index_contents_ftype;
3218 typedef gdb::function_view
3219 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3220 get_gdb_index_contents_dwz_ftype;
3221
3222 /* Read .gdb_index. If everything went ok, initialize the "quick"
3223 elements of all the CUs and return 1. Otherwise, return 0. */
3224
3225 static int
3226 dwarf2_read_gdb_index
3227 (dwarf2_per_objfile *per_objfile,
3228 get_gdb_index_contents_ftype get_gdb_index_contents,
3229 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3230 {
3231 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3232 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3233 struct dwz_file *dwz;
3234 struct objfile *objfile = per_objfile->objfile;
3235 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
3236
3237 gdb::array_view<const gdb_byte> main_index_contents
3238 = get_gdb_index_contents (objfile, per_bfd);
3239
3240 if (main_index_contents.empty ())
3241 return 0;
3242
3243 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3244 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3245 use_deprecated_index_sections,
3246 main_index_contents, map.get (), &cu_list,
3247 &cu_list_elements, &types_list,
3248 &types_list_elements))
3249 return 0;
3250
3251 /* Don't use the index if it's empty. */
3252 if (map->symbol_table.empty ())
3253 return 0;
3254
3255 /* If there is a .dwz file, read it so we can get its CU list as
3256 well. */
3257 dwz = dwarf2_get_dwz_file (per_bfd);
3258 if (dwz != NULL)
3259 {
3260 struct mapped_index dwz_map;
3261 const gdb_byte *dwz_types_ignore;
3262 offset_type dwz_types_elements_ignore;
3263
3264 gdb::array_view<const gdb_byte> dwz_index_content
3265 = get_gdb_index_contents_dwz (objfile, dwz);
3266
3267 if (dwz_index_content.empty ())
3268 return 0;
3269
3270 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3271 1, dwz_index_content, &dwz_map,
3272 &dwz_list, &dwz_list_elements,
3273 &dwz_types_ignore,
3274 &dwz_types_elements_ignore))
3275 {
3276 warning (_("could not read '.gdb_index' section from %s; skipping"),
3277 bfd_get_filename (dwz->dwz_bfd.get ()));
3278 return 0;
3279 }
3280 }
3281
3282 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3283 dwz_list_elements);
3284
3285 if (types_list_elements)
3286 {
3287 /* We can only handle a single .debug_types when we have an
3288 index. */
3289 if (per_bfd->types.size () != 1)
3290 return 0;
3291
3292 dwarf2_section_info *section = &per_bfd->types[0];
3293
3294 create_signatured_type_table_from_index (per_bfd, section, types_list,
3295 types_list_elements);
3296 }
3297
3298 create_addrmap_from_index (per_objfile, map.get ());
3299
3300 per_bfd->index_table = std::move (map);
3301 per_bfd->using_index = 1;
3302 per_bfd->quick_file_names_table =
3303 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3304
3305 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
3306 objfiles using the same BFD. */
3307 gdb_assert (per_bfd->partial_symtabs == nullptr);
3308 per_bfd->partial_symtabs = objfile->partial_symtabs;
3309
3310 return 1;
3311 }
3312
3313 /* die_reader_func for dw2_get_file_names. */
3314
3315 static void
3316 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3317 const gdb_byte *info_ptr,
3318 struct die_info *comp_unit_die)
3319 {
3320 struct dwarf2_cu *cu = reader->cu;
3321 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3322 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3323 struct dwarf2_per_cu_data *lh_cu;
3324 struct attribute *attr;
3325 void **slot;
3326 struct quick_file_names *qfn;
3327
3328 gdb_assert (! this_cu->is_debug_types);
3329
3330 /* Our callers never want to match partial units -- instead they
3331 will match the enclosing full CU. */
3332 if (comp_unit_die->tag == DW_TAG_partial_unit)
3333 {
3334 this_cu->v.quick->no_file_data = 1;
3335 return;
3336 }
3337
3338 lh_cu = this_cu;
3339 slot = NULL;
3340
3341 line_header_up lh;
3342 sect_offset line_offset {};
3343
3344 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3345 if (attr != nullptr && attr->form_is_unsigned ())
3346 {
3347 struct quick_file_names find_entry;
3348
3349 line_offset = (sect_offset) attr->as_unsigned ();
3350
3351 /* We may have already read in this line header (TU line header sharing).
3352 If we have we're done. */
3353 find_entry.hash.dwo_unit = cu->dwo_unit;
3354 find_entry.hash.line_sect_off = line_offset;
3355 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
3356 &find_entry, INSERT);
3357 if (*slot != NULL)
3358 {
3359 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3360 return;
3361 }
3362
3363 lh = dwarf_decode_line_header (line_offset, cu);
3364 }
3365 if (lh == NULL)
3366 {
3367 lh_cu->v.quick->no_file_data = 1;
3368 return;
3369 }
3370
3371 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
3372 qfn->hash.dwo_unit = cu->dwo_unit;
3373 qfn->hash.line_sect_off = line_offset;
3374 gdb_assert (slot != NULL);
3375 *slot = qfn;
3376
3377 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3378
3379 int offset = 0;
3380 if (strcmp (fnd.name, "<unknown>") != 0)
3381 ++offset;
3382
3383 qfn->num_file_names = offset + lh->file_names_size ();
3384 qfn->file_names =
3385 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
3386 qfn->num_file_names);
3387 if (offset != 0)
3388 qfn->file_names[0] = xstrdup (fnd.name);
3389 for (int i = 0; i < lh->file_names_size (); ++i)
3390 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3391 fnd.comp_dir).release ();
3392 qfn->real_names = NULL;
3393
3394 lh_cu->v.quick->file_names = qfn;
3395 }
3396
3397 /* A helper for the "quick" functions which attempts to read the line
3398 table for THIS_CU. */
3399
3400 static struct quick_file_names *
3401 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3402 dwarf2_per_objfile *per_objfile)
3403 {
3404 /* This should never be called for TUs. */
3405 gdb_assert (! this_cu->is_debug_types);
3406 /* Nor type unit groups. */
3407 gdb_assert (! this_cu->type_unit_group_p ());
3408
3409 if (this_cu->v.quick->file_names != NULL)
3410 return this_cu->v.quick->file_names;
3411 /* If we know there is no line data, no point in looking again. */
3412 if (this_cu->v.quick->no_file_data)
3413 return NULL;
3414
3415 cutu_reader reader (this_cu, per_objfile);
3416 if (!reader.dummy_p)
3417 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3418
3419 if (this_cu->v.quick->no_file_data)
3420 return NULL;
3421 return this_cu->v.quick->file_names;
3422 }
3423
3424 /* A helper for the "quick" functions which computes and caches the
3425 real path for a given file name from the line table. */
3426
3427 static const char *
3428 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3429 struct quick_file_names *qfn, int index)
3430 {
3431 if (qfn->real_names == NULL)
3432 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3433 qfn->num_file_names, const char *);
3434
3435 if (qfn->real_names[index] == NULL)
3436 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3437
3438 return qfn->real_names[index];
3439 }
3440
3441 static struct symtab *
3442 dw2_find_last_source_symtab (struct objfile *objfile)
3443 {
3444 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3445 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3446 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3447
3448 if (cust == NULL)
3449 return NULL;
3450
3451 return compunit_primary_filetab (cust);
3452 }
3453
3454 /* Traversal function for dw2_forget_cached_source_info. */
3455
3456 static int
3457 dw2_free_cached_file_names (void **slot, void *info)
3458 {
3459 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3460
3461 if (file_data->real_names)
3462 {
3463 int i;
3464
3465 for (i = 0; i < file_data->num_file_names; ++i)
3466 {
3467 xfree ((void*) file_data->real_names[i]);
3468 file_data->real_names[i] = NULL;
3469 }
3470 }
3471
3472 return 1;
3473 }
3474
3475 static void
3476 dw2_forget_cached_source_info (struct objfile *objfile)
3477 {
3478 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3479
3480 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
3481 dw2_free_cached_file_names, NULL);
3482 }
3483
3484 /* Helper function for dw2_map_symtabs_matching_filename that expands
3485 the symtabs and calls the iterator. */
3486
3487 static int
3488 dw2_map_expand_apply (struct objfile *objfile,
3489 struct dwarf2_per_cu_data *per_cu,
3490 const char *name, const char *real_path,
3491 gdb::function_view<bool (symtab *)> callback)
3492 {
3493 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3494
3495 /* Don't visit already-expanded CUs. */
3496 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3497 if (per_objfile->symtab_set_p (per_cu))
3498 return 0;
3499
3500 /* This may expand more than one symtab, and we want to iterate over
3501 all of them. */
3502 dw2_instantiate_symtab (per_cu, per_objfile, false);
3503
3504 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3505 last_made, callback);
3506 }
3507
3508 /* Implementation of the map_symtabs_matching_filename method. */
3509
3510 static bool
3511 dw2_map_symtabs_matching_filename
3512 (struct objfile *objfile, const char *name, const char *real_path,
3513 gdb::function_view<bool (symtab *)> callback)
3514 {
3515 const char *name_basename = lbasename (name);
3516 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3517
3518 /* The rule is CUs specify all the files, including those used by
3519 any TU, so there's no need to scan TUs here. */
3520
3521 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3522 {
3523 /* We only need to look at symtabs not already expanded. */
3524 if (per_objfile->symtab_set_p (per_cu))
3525 continue;
3526
3527 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3528 if (file_data == NULL)
3529 continue;
3530
3531 for (int j = 0; j < file_data->num_file_names; ++j)
3532 {
3533 const char *this_name = file_data->file_names[j];
3534 const char *this_real_name;
3535
3536 if (compare_filenames_for_search (this_name, name))
3537 {
3538 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3539 callback))
3540 return true;
3541 continue;
3542 }
3543
3544 /* Before we invoke realpath, which can get expensive when many
3545 files are involved, do a quick comparison of the basenames. */
3546 if (! basenames_may_differ
3547 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3548 continue;
3549
3550 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
3551 if (compare_filenames_for_search (this_real_name, name))
3552 {
3553 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3554 callback))
3555 return true;
3556 continue;
3557 }
3558
3559 if (real_path != NULL)
3560 {
3561 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3562 gdb_assert (IS_ABSOLUTE_PATH (name));
3563 if (this_real_name != NULL
3564 && FILENAME_CMP (real_path, this_real_name) == 0)
3565 {
3566 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3567 callback))
3568 return true;
3569 continue;
3570 }
3571 }
3572 }
3573 }
3574
3575 return false;
3576 }
3577
3578 /* Struct used to manage iterating over all CUs looking for a symbol. */
3579
3580 struct dw2_symtab_iterator
3581 {
3582 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3583 dwarf2_per_objfile *per_objfile;
3584 /* If set, only look for symbols that match that block. Valid values are
3585 GLOBAL_BLOCK and STATIC_BLOCK. */
3586 gdb::optional<block_enum> block_index;
3587 /* The kind of symbol we're looking for. */
3588 domain_enum domain;
3589 /* The list of CUs from the index entry of the symbol,
3590 or NULL if not found. */
3591 offset_type *vec;
3592 /* The next element in VEC to look at. */
3593 int next;
3594 /* The number of elements in VEC, or zero if there is no match. */
3595 int length;
3596 /* Have we seen a global version of the symbol?
3597 If so we can ignore all further global instances.
3598 This is to work around gold/15646, inefficient gold-generated
3599 indices. */
3600 int global_seen;
3601 };
3602
3603 /* Initialize the index symtab iterator ITER, common part. */
3604
3605 static void
3606 dw2_symtab_iter_init_common (struct dw2_symtab_iterator *iter,
3607 dwarf2_per_objfile *per_objfile,
3608 gdb::optional<block_enum> block_index,
3609 domain_enum domain)
3610 {
3611 iter->per_objfile = per_objfile;
3612 iter->block_index = block_index;
3613 iter->domain = domain;
3614 iter->next = 0;
3615 iter->global_seen = 0;
3616 iter->vec = NULL;
3617 iter->length = 0;
3618 }
3619
3620 /* Initialize the index symtab iterator ITER, const char *NAME variant. */
3621
3622 static void
3623 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3624 dwarf2_per_objfile *per_objfile,
3625 gdb::optional<block_enum> block_index,
3626 domain_enum domain,
3627 const char *name)
3628 {
3629 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3630
3631 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3632 /* index is NULL if OBJF_READNOW. */
3633 if (index == NULL)
3634 return;
3635
3636 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3637 iter->length = MAYBE_SWAP (*iter->vec);
3638 }
3639
3640 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3641
3642 static void
3643 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3644 dwarf2_per_objfile *per_objfile,
3645 gdb::optional<block_enum> block_index,
3646 domain_enum domain, offset_type namei)
3647 {
3648 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3649
3650 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3651 /* index is NULL if OBJF_READNOW. */
3652 if (index == NULL)
3653 return;
3654
3655 gdb_assert (!index->symbol_name_slot_invalid (namei));
3656 const auto &bucket = index->symbol_table[namei];
3657
3658 iter->vec = (offset_type *) (index->constant_pool
3659 + MAYBE_SWAP (bucket.vec));
3660 iter->length = MAYBE_SWAP (*iter->vec);
3661 }
3662
3663 /* Return the next matching CU or NULL if there are no more. */
3664
3665 static struct dwarf2_per_cu_data *
3666 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3667 {
3668 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3669
3670 for ( ; iter->next < iter->length; ++iter->next)
3671 {
3672 offset_type cu_index_and_attrs =
3673 MAYBE_SWAP (iter->vec[iter->next + 1]);
3674 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3675 gdb_index_symbol_kind symbol_kind =
3676 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3677 /* Only check the symbol attributes if they're present.
3678 Indices prior to version 7 don't record them,
3679 and indices >= 7 may elide them for certain symbols
3680 (gold does this). */
3681 int attrs_valid =
3682 (per_objfile->per_bfd->index_table->version >= 7
3683 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3684
3685 /* Don't crash on bad data. */
3686 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3687 + per_objfile->per_bfd->all_type_units.size ()))
3688 {
3689 complaint (_(".gdb_index entry has bad CU index"
3690 " [in module %s]"), objfile_name (per_objfile->objfile));
3691 continue;
3692 }
3693
3694 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3695
3696 /* Skip if already read in. */
3697 if (per_objfile->symtab_set_p (per_cu))
3698 continue;
3699
3700 /* Check static vs global. */
3701 if (attrs_valid)
3702 {
3703 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3704
3705 if (iter->block_index.has_value ())
3706 {
3707 bool want_static = *iter->block_index == STATIC_BLOCK;
3708
3709 if (is_static != want_static)
3710 continue;
3711 }
3712
3713 /* Work around gold/15646. */
3714 if (!is_static
3715 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3716 {
3717 if (iter->global_seen)
3718 continue;
3719
3720 iter->global_seen = 1;
3721 }
3722 }
3723
3724 /* Only check the symbol's kind if it has one. */
3725 if (attrs_valid)
3726 {
3727 switch (iter->domain)
3728 {
3729 case VAR_DOMAIN:
3730 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3731 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3732 /* Some types are also in VAR_DOMAIN. */
3733 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3734 continue;
3735 break;
3736 case STRUCT_DOMAIN:
3737 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3738 continue;
3739 break;
3740 case LABEL_DOMAIN:
3741 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3742 continue;
3743 break;
3744 case MODULE_DOMAIN:
3745 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3746 continue;
3747 break;
3748 default:
3749 break;
3750 }
3751 }
3752
3753 ++iter->next;
3754 return per_cu;
3755 }
3756
3757 return NULL;
3758 }
3759
3760 static struct compunit_symtab *
3761 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3762 const char *name, domain_enum domain)
3763 {
3764 struct compunit_symtab *stab_best = NULL;
3765 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3766
3767 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3768
3769 struct dw2_symtab_iterator iter;
3770 struct dwarf2_per_cu_data *per_cu;
3771
3772 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
3773
3774 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3775 {
3776 struct symbol *sym, *with_opaque = NULL;
3777 struct compunit_symtab *stab
3778 = dw2_instantiate_symtab (per_cu, per_objfile, false);
3779 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3780 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3781
3782 sym = block_find_symbol (block, name, domain,
3783 block_find_non_opaque_type_preferred,
3784 &with_opaque);
3785
3786 /* Some caution must be observed with overloaded functions
3787 and methods, since the index will not contain any overload
3788 information (but NAME might contain it). */
3789
3790 if (sym != NULL
3791 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3792 return stab;
3793 if (with_opaque != NULL
3794 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3795 stab_best = stab;
3796
3797 /* Keep looking through other CUs. */
3798 }
3799
3800 return stab_best;
3801 }
3802
3803 static void
3804 dw2_print_stats (struct objfile *objfile)
3805 {
3806 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3807 int total = (per_objfile->per_bfd->all_comp_units.size ()
3808 + per_objfile->per_bfd->all_type_units.size ());
3809 int count = 0;
3810
3811 for (int i = 0; i < total; ++i)
3812 {
3813 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3814
3815 if (!per_objfile->symtab_set_p (per_cu))
3816 ++count;
3817 }
3818 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3819 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3820 }
3821
3822 /* This dumps minimal information about the index.
3823 It is called via "mt print objfiles".
3824 One use is to verify .gdb_index has been loaded by the
3825 gdb.dwarf2/gdb-index.exp testcase. */
3826
3827 static void
3828 dw2_dump (struct objfile *objfile)
3829 {
3830 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3831
3832 gdb_assert (per_objfile->per_bfd->using_index);
3833 printf_filtered (".gdb_index:");
3834 if (per_objfile->per_bfd->index_table != NULL)
3835 {
3836 printf_filtered (" version %d\n",
3837 per_objfile->per_bfd->index_table->version);
3838 }
3839 else
3840 printf_filtered (" faked for \"readnow\"\n");
3841 printf_filtered ("\n");
3842 }
3843
3844 static void
3845 dw2_expand_symtabs_for_function (struct objfile *objfile,
3846 const char *func_name)
3847 {
3848 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3849
3850 struct dw2_symtab_iterator iter;
3851 struct dwarf2_per_cu_data *per_cu;
3852
3853 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
3854
3855 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3856 dw2_instantiate_symtab (per_cu, per_objfile, false);
3857
3858 }
3859
3860 static void
3861 dw2_expand_all_symtabs (struct objfile *objfile)
3862 {
3863 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3864 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3865 + per_objfile->per_bfd->all_type_units.size ());
3866
3867 for (int i = 0; i < total_units; ++i)
3868 {
3869 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3870
3871 /* We don't want to directly expand a partial CU, because if we
3872 read it with the wrong language, then assertion failures can
3873 be triggered later on. See PR symtab/23010. So, tell
3874 dw2_instantiate_symtab to skip partial CUs -- any important
3875 partial CU will be read via DW_TAG_imported_unit anyway. */
3876 dw2_instantiate_symtab (per_cu, per_objfile, true);
3877 }
3878 }
3879
3880 static void
3881 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3882 const char *fullname)
3883 {
3884 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3885
3886 /* We don't need to consider type units here.
3887 This is only called for examining code, e.g. expand_line_sal.
3888 There can be an order of magnitude (or more) more type units
3889 than comp units, and we avoid them if we can. */
3890
3891 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3892 {
3893 /* We only need to look at symtabs not already expanded. */
3894 if (per_objfile->symtab_set_p (per_cu))
3895 continue;
3896
3897 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3898 if (file_data == NULL)
3899 continue;
3900
3901 for (int j = 0; j < file_data->num_file_names; ++j)
3902 {
3903 const char *this_fullname = file_data->file_names[j];
3904
3905 if (filename_cmp (this_fullname, fullname) == 0)
3906 {
3907 dw2_instantiate_symtab (per_cu, per_objfile, false);
3908 break;
3909 }
3910 }
3911 }
3912 }
3913
3914 static void
3915 dw2_expand_symtabs_matching_symbol
3916 (mapped_index_base &index,
3917 const lookup_name_info &lookup_name_in,
3918 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3919 enum search_domain kind,
3920 gdb::function_view<bool (offset_type)> match_callback,
3921 dwarf2_per_objfile *per_objfile);
3922
3923 static void
3924 dw2_expand_symtabs_matching_one
3925 (dwarf2_per_cu_data *per_cu,
3926 dwarf2_per_objfile *per_objfile,
3927 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3928 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3929
3930 static void
3931 dw2_map_matching_symbols
3932 (struct objfile *objfile,
3933 const lookup_name_info &name, domain_enum domain,
3934 int global,
3935 gdb::function_view<symbol_found_callback_ftype> callback,
3936 symbol_compare_ftype *ordered_compare)
3937 {
3938 /* Used for Ada. */
3939 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3940
3941 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3942
3943 if (per_objfile->per_bfd->index_table != nullptr)
3944 {
3945 mapped_index &index = *per_objfile->per_bfd->index_table;
3946
3947 const char *match_name = name.ada ().lookup_name ().c_str ();
3948 auto matcher = [&] (const char *symname)
3949 {
3950 if (ordered_compare == nullptr)
3951 return true;
3952 return ordered_compare (symname, match_name) == 0;
3953 };
3954
3955 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3956 [&] (offset_type namei)
3957 {
3958 struct dw2_symtab_iterator iter;
3959 struct dwarf2_per_cu_data *per_cu;
3960
3961 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3962 namei);
3963 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3964 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3965 nullptr);
3966 return true;
3967 }, per_objfile);
3968 }
3969 else
3970 {
3971 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3972 proceed assuming all symtabs have been read in. */
3973 }
3974
3975 for (compunit_symtab *cust : objfile->compunits ())
3976 {
3977 const struct block *block;
3978
3979 if (cust == NULL)
3980 continue;
3981 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3982 if (!iterate_over_symbols_terminated (block, name,
3983 domain, callback))
3984 return;
3985 }
3986 }
3987
3988 /* Starting from a search name, return the string that finds the upper
3989 bound of all strings that start with SEARCH_NAME in a sorted name
3990 list. Returns the empty string to indicate that the upper bound is
3991 the end of the list. */
3992
3993 static std::string
3994 make_sort_after_prefix_name (const char *search_name)
3995 {
3996 /* When looking to complete "func", we find the upper bound of all
3997 symbols that start with "func" by looking for where we'd insert
3998 the closest string that would follow "func" in lexicographical
3999 order. Usually, that's "func"-with-last-character-incremented,
4000 i.e. "fund". Mind non-ASCII characters, though. Usually those
4001 will be UTF-8 multi-byte sequences, but we can't be certain.
4002 Especially mind the 0xff character, which is a valid character in
4003 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4004 rule out compilers allowing it in identifiers. Note that
4005 conveniently, strcmp/strcasecmp are specified to compare
4006 characters interpreted as unsigned char. So what we do is treat
4007 the whole string as a base 256 number composed of a sequence of
4008 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4009 to 0, and carries 1 to the following more-significant position.
4010 If the very first character in SEARCH_NAME ends up incremented
4011 and carries/overflows, then the upper bound is the end of the
4012 list. The string after the empty string is also the empty
4013 string.
4014
4015 Some examples of this operation:
4016
4017 SEARCH_NAME => "+1" RESULT
4018
4019 "abc" => "abd"
4020 "ab\xff" => "ac"
4021 "\xff" "a" "\xff" => "\xff" "b"
4022 "\xff" => ""
4023 "\xff\xff" => ""
4024 "" => ""
4025
4026 Then, with these symbols for example:
4027
4028 func
4029 func1
4030 fund
4031
4032 completing "func" looks for symbols between "func" and
4033 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4034 which finds "func" and "func1", but not "fund".
4035
4036 And with:
4037
4038 funcÿ (Latin1 'ÿ' [0xff])
4039 funcÿ1
4040 fund
4041
4042 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4043 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4044
4045 And with:
4046
4047 ÿÿ (Latin1 'ÿ' [0xff])
4048 ÿÿ1
4049
4050 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4051 the end of the list.
4052 */
4053 std::string after = search_name;
4054 while (!after.empty () && (unsigned char) after.back () == 0xff)
4055 after.pop_back ();
4056 if (!after.empty ())
4057 after.back () = (unsigned char) after.back () + 1;
4058 return after;
4059 }
4060
4061 /* See declaration. */
4062
4063 std::pair<std::vector<name_component>::const_iterator,
4064 std::vector<name_component>::const_iterator>
4065 mapped_index_base::find_name_components_bounds
4066 (const lookup_name_info &lookup_name_without_params, language lang,
4067 dwarf2_per_objfile *per_objfile) const
4068 {
4069 auto *name_cmp
4070 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4071
4072 const char *lang_name
4073 = lookup_name_without_params.language_lookup_name (lang);
4074
4075 /* Comparison function object for lower_bound that matches against a
4076 given symbol name. */
4077 auto lookup_compare_lower = [&] (const name_component &elem,
4078 const char *name)
4079 {
4080 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
4081 const char *elem_name = elem_qualified + elem.name_offset;
4082 return name_cmp (elem_name, name) < 0;
4083 };
4084
4085 /* Comparison function object for upper_bound that matches against a
4086 given symbol name. */
4087 auto lookup_compare_upper = [&] (const char *name,
4088 const name_component &elem)
4089 {
4090 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
4091 const char *elem_name = elem_qualified + elem.name_offset;
4092 return name_cmp (name, elem_name) < 0;
4093 };
4094
4095 auto begin = this->name_components.begin ();
4096 auto end = this->name_components.end ();
4097
4098 /* Find the lower bound. */
4099 auto lower = [&] ()
4100 {
4101 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
4102 return begin;
4103 else
4104 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
4105 } ();
4106
4107 /* Find the upper bound. */
4108 auto upper = [&] ()
4109 {
4110 if (lookup_name_without_params.completion_mode ())
4111 {
4112 /* In completion mode, we want UPPER to point past all
4113 symbols names that have the same prefix. I.e., with
4114 these symbols, and completing "func":
4115
4116 function << lower bound
4117 function1
4118 other_function << upper bound
4119
4120 We find the upper bound by looking for the insertion
4121 point of "func"-with-last-character-incremented,
4122 i.e. "fund". */
4123 std::string after = make_sort_after_prefix_name (lang_name);
4124 if (after.empty ())
4125 return end;
4126 return std::lower_bound (lower, end, after.c_str (),
4127 lookup_compare_lower);
4128 }
4129 else
4130 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4131 } ();
4132
4133 return {lower, upper};
4134 }
4135
4136 /* See declaration. */
4137
4138 void
4139 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
4140 {
4141 if (!this->name_components.empty ())
4142 return;
4143
4144 this->name_components_casing = case_sensitivity;
4145 auto *name_cmp
4146 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4147
4148 /* The code below only knows how to break apart components of C++
4149 symbol names (and other languages that use '::' as
4150 namespace/module separator) and Ada symbol names. */
4151 auto count = this->symbol_name_count ();
4152 for (offset_type idx = 0; idx < count; idx++)
4153 {
4154 if (this->symbol_name_slot_invalid (idx))
4155 continue;
4156
4157 const char *name = this->symbol_name_at (idx, per_objfile);
4158
4159 /* Add each name component to the name component table. */
4160 unsigned int previous_len = 0;
4161
4162 if (strstr (name, "::") != nullptr)
4163 {
4164 for (unsigned int current_len = cp_find_first_component (name);
4165 name[current_len] != '\0';
4166 current_len += cp_find_first_component (name + current_len))
4167 {
4168 gdb_assert (name[current_len] == ':');
4169 this->name_components.push_back ({previous_len, idx});
4170 /* Skip the '::'. */
4171 current_len += 2;
4172 previous_len = current_len;
4173 }
4174 }
4175 else
4176 {
4177 /* Handle the Ada encoded (aka mangled) form here. */
4178 for (const char *iter = strstr (name, "__");
4179 iter != nullptr;
4180 iter = strstr (iter, "__"))
4181 {
4182 this->name_components.push_back ({previous_len, idx});
4183 iter += 2;
4184 previous_len = iter - name;
4185 }
4186 }
4187
4188 this->name_components.push_back ({previous_len, idx});
4189 }
4190
4191 /* Sort name_components elements by name. */
4192 auto name_comp_compare = [&] (const name_component &left,
4193 const name_component &right)
4194 {
4195 const char *left_qualified
4196 = this->symbol_name_at (left.idx, per_objfile);
4197 const char *right_qualified
4198 = this->symbol_name_at (right.idx, per_objfile);
4199
4200 const char *left_name = left_qualified + left.name_offset;
4201 const char *right_name = right_qualified + right.name_offset;
4202
4203 return name_cmp (left_name, right_name) < 0;
4204 };
4205
4206 std::sort (this->name_components.begin (),
4207 this->name_components.end (),
4208 name_comp_compare);
4209 }
4210
4211 /* Helper for dw2_expand_symtabs_matching that works with a
4212 mapped_index_base instead of the containing objfile. This is split
4213 to a separate function in order to be able to unit test the
4214 name_components matching using a mock mapped_index_base. For each
4215 symbol name that matches, calls MATCH_CALLBACK, passing it the
4216 symbol's index in the mapped_index_base symbol table. */
4217
4218 static void
4219 dw2_expand_symtabs_matching_symbol
4220 (mapped_index_base &index,
4221 const lookup_name_info &lookup_name_in,
4222 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4223 enum search_domain kind,
4224 gdb::function_view<bool (offset_type)> match_callback,
4225 dwarf2_per_objfile *per_objfile)
4226 {
4227 lookup_name_info lookup_name_without_params
4228 = lookup_name_in.make_ignore_params ();
4229
4230 /* Build the symbol name component sorted vector, if we haven't
4231 yet. */
4232 index.build_name_components (per_objfile);
4233
4234 /* The same symbol may appear more than once in the range though.
4235 E.g., if we're looking for symbols that complete "w", and we have
4236 a symbol named "w1::w2", we'll find the two name components for
4237 that same symbol in the range. To be sure we only call the
4238 callback once per symbol, we first collect the symbol name
4239 indexes that matched in a temporary vector and ignore
4240 duplicates. */
4241 std::vector<offset_type> matches;
4242
4243 struct name_and_matcher
4244 {
4245 symbol_name_matcher_ftype *matcher;
4246 const char *name;
4247
4248 bool operator== (const name_and_matcher &other) const
4249 {
4250 return matcher == other.matcher && strcmp (name, other.name) == 0;
4251 }
4252 };
4253
4254 /* A vector holding all the different symbol name matchers, for all
4255 languages. */
4256 std::vector<name_and_matcher> matchers;
4257
4258 for (int i = 0; i < nr_languages; i++)
4259 {
4260 enum language lang_e = (enum language) i;
4261
4262 const language_defn *lang = language_def (lang_e);
4263 symbol_name_matcher_ftype *name_matcher
4264 = lang->get_symbol_name_matcher (lookup_name_without_params);
4265
4266 name_and_matcher key {
4267 name_matcher,
4268 lookup_name_without_params.language_lookup_name (lang_e)
4269 };
4270
4271 /* Don't insert the same comparison routine more than once.
4272 Note that we do this linear walk. This is not a problem in
4273 practice because the number of supported languages is
4274 low. */
4275 if (std::find (matchers.begin (), matchers.end (), key)
4276 != matchers.end ())
4277 continue;
4278 matchers.push_back (std::move (key));
4279
4280 auto bounds
4281 = index.find_name_components_bounds (lookup_name_without_params,
4282 lang_e, per_objfile);
4283
4284 /* Now for each symbol name in range, check to see if we have a name
4285 match, and if so, call the MATCH_CALLBACK callback. */
4286
4287 for (; bounds.first != bounds.second; ++bounds.first)
4288 {
4289 const char *qualified
4290 = index.symbol_name_at (bounds.first->idx, per_objfile);
4291
4292 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4293 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4294 continue;
4295
4296 matches.push_back (bounds.first->idx);
4297 }
4298 }
4299
4300 std::sort (matches.begin (), matches.end ());
4301
4302 /* Finally call the callback, once per match. */
4303 ULONGEST prev = -1;
4304 for (offset_type idx : matches)
4305 {
4306 if (prev != idx)
4307 {
4308 if (!match_callback (idx))
4309 break;
4310 prev = idx;
4311 }
4312 }
4313
4314 /* Above we use a type wider than idx's for 'prev', since 0 and
4315 (offset_type)-1 are both possible values. */
4316 static_assert (sizeof (prev) > sizeof (offset_type), "");
4317 }
4318
4319 #if GDB_SELF_TEST
4320
4321 namespace selftests { namespace dw2_expand_symtabs_matching {
4322
4323 /* A mock .gdb_index/.debug_names-like name index table, enough to
4324 exercise dw2_expand_symtabs_matching_symbol, which works with the
4325 mapped_index_base interface. Builds an index from the symbol list
4326 passed as parameter to the constructor. */
4327 class mock_mapped_index : public mapped_index_base
4328 {
4329 public:
4330 mock_mapped_index (gdb::array_view<const char *> symbols)
4331 : m_symbol_table (symbols)
4332 {}
4333
4334 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4335
4336 /* Return the number of names in the symbol table. */
4337 size_t symbol_name_count () const override
4338 {
4339 return m_symbol_table.size ();
4340 }
4341
4342 /* Get the name of the symbol at IDX in the symbol table. */
4343 const char *symbol_name_at
4344 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
4345 {
4346 return m_symbol_table[idx];
4347 }
4348
4349 private:
4350 gdb::array_view<const char *> m_symbol_table;
4351 };
4352
4353 /* Convenience function that converts a NULL pointer to a "<null>"
4354 string, to pass to print routines. */
4355
4356 static const char *
4357 string_or_null (const char *str)
4358 {
4359 return str != NULL ? str : "<null>";
4360 }
4361
4362 /* Check if a lookup_name_info built from
4363 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4364 index. EXPECTED_LIST is the list of expected matches, in expected
4365 matching order. If no match expected, then an empty list is
4366 specified. Returns true on success. On failure prints a warning
4367 indicating the file:line that failed, and returns false. */
4368
4369 static bool
4370 check_match (const char *file, int line,
4371 mock_mapped_index &mock_index,
4372 const char *name, symbol_name_match_type match_type,
4373 bool completion_mode,
4374 std::initializer_list<const char *> expected_list,
4375 dwarf2_per_objfile *per_objfile)
4376 {
4377 lookup_name_info lookup_name (name, match_type, completion_mode);
4378
4379 bool matched = true;
4380
4381 auto mismatch = [&] (const char *expected_str,
4382 const char *got)
4383 {
4384 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4385 "expected=\"%s\", got=\"%s\"\n"),
4386 file, line,
4387 (match_type == symbol_name_match_type::FULL
4388 ? "FULL" : "WILD"),
4389 name, string_or_null (expected_str), string_or_null (got));
4390 matched = false;
4391 };
4392
4393 auto expected_it = expected_list.begin ();
4394 auto expected_end = expected_list.end ();
4395
4396 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4397 NULL, ALL_DOMAIN,
4398 [&] (offset_type idx)
4399 {
4400 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
4401 const char *expected_str
4402 = expected_it == expected_end ? NULL : *expected_it++;
4403
4404 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4405 mismatch (expected_str, matched_name);
4406 return true;
4407 }, per_objfile);
4408
4409 const char *expected_str
4410 = expected_it == expected_end ? NULL : *expected_it++;
4411 if (expected_str != NULL)
4412 mismatch (expected_str, NULL);
4413
4414 return matched;
4415 }
4416
4417 /* The symbols added to the mock mapped_index for testing (in
4418 canonical form). */
4419 static const char *test_symbols[] = {
4420 "function",
4421 "std::bar",
4422 "std::zfunction",
4423 "std::zfunction2",
4424 "w1::w2",
4425 "ns::foo<char*>",
4426 "ns::foo<int>",
4427 "ns::foo<long>",
4428 "ns2::tmpl<int>::foo2",
4429 "(anonymous namespace)::A::B::C",
4430
4431 /* These are used to check that the increment-last-char in the
4432 matching algorithm for completion doesn't match "t1_fund" when
4433 completing "t1_func". */
4434 "t1_func",
4435 "t1_func1",
4436 "t1_fund",
4437 "t1_fund1",
4438
4439 /* A UTF-8 name with multi-byte sequences to make sure that
4440 cp-name-parser understands this as a single identifier ("função"
4441 is "function" in PT). */
4442 u8"u8função",
4443
4444 /* \377 (0xff) is Latin1 'ÿ'. */
4445 "yfunc\377",
4446
4447 /* \377 (0xff) is Latin1 'ÿ'. */
4448 "\377",
4449 "\377\377123",
4450
4451 /* A name with all sorts of complications. Starts with "z" to make
4452 it easier for the completion tests below. */
4453 #define Z_SYM_NAME \
4454 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4455 "::tuple<(anonymous namespace)::ui*, " \
4456 "std::default_delete<(anonymous namespace)::ui>, void>"
4457
4458 Z_SYM_NAME
4459 };
4460
4461 /* Returns true if the mapped_index_base::find_name_component_bounds
4462 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4463 in completion mode. */
4464
4465 static bool
4466 check_find_bounds_finds (mapped_index_base &index,
4467 const char *search_name,
4468 gdb::array_view<const char *> expected_syms,
4469 dwarf2_per_objfile *per_objfile)
4470 {
4471 lookup_name_info lookup_name (search_name,
4472 symbol_name_match_type::FULL, true);
4473
4474 auto bounds = index.find_name_components_bounds (lookup_name,
4475 language_cplus,
4476 per_objfile);
4477
4478 size_t distance = std::distance (bounds.first, bounds.second);
4479 if (distance != expected_syms.size ())
4480 return false;
4481
4482 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4483 {
4484 auto nc_elem = bounds.first + exp_elem;
4485 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
4486 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4487 return false;
4488 }
4489
4490 return true;
4491 }
4492
4493 /* Test the lower-level mapped_index::find_name_component_bounds
4494 method. */
4495
4496 static void
4497 test_mapped_index_find_name_component_bounds ()
4498 {
4499 mock_mapped_index mock_index (test_symbols);
4500
4501 mock_index.build_name_components (NULL /* per_objfile */);
4502
4503 /* Test the lower-level mapped_index::find_name_component_bounds
4504 method in completion mode. */
4505 {
4506 static const char *expected_syms[] = {
4507 "t1_func",
4508 "t1_func1",
4509 };
4510
4511 SELF_CHECK (check_find_bounds_finds
4512 (mock_index, "t1_func", expected_syms,
4513 NULL /* per_objfile */));
4514 }
4515
4516 /* Check that the increment-last-char in the name matching algorithm
4517 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4518 {
4519 static const char *expected_syms1[] = {
4520 "\377",
4521 "\377\377123",
4522 };
4523 SELF_CHECK (check_find_bounds_finds
4524 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
4525
4526 static const char *expected_syms2[] = {
4527 "\377\377123",
4528 };
4529 SELF_CHECK (check_find_bounds_finds
4530 (mock_index, "\377\377", expected_syms2,
4531 NULL /* per_objfile */));
4532 }
4533 }
4534
4535 /* Test dw2_expand_symtabs_matching_symbol. */
4536
4537 static void
4538 test_dw2_expand_symtabs_matching_symbol ()
4539 {
4540 mock_mapped_index mock_index (test_symbols);
4541
4542 /* We let all tests run until the end even if some fails, for debug
4543 convenience. */
4544 bool any_mismatch = false;
4545
4546 /* Create the expected symbols list (an initializer_list). Needed
4547 because lists have commas, and we need to pass them to CHECK,
4548 which is a macro. */
4549 #define EXPECT(...) { __VA_ARGS__ }
4550
4551 /* Wrapper for check_match that passes down the current
4552 __FILE__/__LINE__. */
4553 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4554 any_mismatch |= !check_match (__FILE__, __LINE__, \
4555 mock_index, \
4556 NAME, MATCH_TYPE, COMPLETION_MODE, \
4557 EXPECTED_LIST, NULL)
4558
4559 /* Identity checks. */
4560 for (const char *sym : test_symbols)
4561 {
4562 /* Should be able to match all existing symbols. */
4563 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4564 EXPECT (sym));
4565
4566 /* Should be able to match all existing symbols with
4567 parameters. */
4568 std::string with_params = std::string (sym) + "(int)";
4569 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4570 EXPECT (sym));
4571
4572 /* Should be able to match all existing symbols with
4573 parameters and qualifiers. */
4574 with_params = std::string (sym) + " ( int ) const";
4575 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4576 EXPECT (sym));
4577
4578 /* This should really find sym, but cp-name-parser.y doesn't
4579 know about lvalue/rvalue qualifiers yet. */
4580 with_params = std::string (sym) + " ( int ) &&";
4581 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4582 {});
4583 }
4584
4585 /* Check that the name matching algorithm for completion doesn't get
4586 confused with Latin1 'ÿ' / 0xff. */
4587 {
4588 static const char str[] = "\377";
4589 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4590 EXPECT ("\377", "\377\377123"));
4591 }
4592
4593 /* Check that the increment-last-char in the matching algorithm for
4594 completion doesn't match "t1_fund" when completing "t1_func". */
4595 {
4596 static const char str[] = "t1_func";
4597 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4598 EXPECT ("t1_func", "t1_func1"));
4599 }
4600
4601 /* Check that completion mode works at each prefix of the expected
4602 symbol name. */
4603 {
4604 static const char str[] = "function(int)";
4605 size_t len = strlen (str);
4606 std::string lookup;
4607
4608 for (size_t i = 1; i < len; i++)
4609 {
4610 lookup.assign (str, i);
4611 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4612 EXPECT ("function"));
4613 }
4614 }
4615
4616 /* While "w" is a prefix of both components, the match function
4617 should still only be called once. */
4618 {
4619 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4620 EXPECT ("w1::w2"));
4621 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4622 EXPECT ("w1::w2"));
4623 }
4624
4625 /* Same, with a "complicated" symbol. */
4626 {
4627 static const char str[] = Z_SYM_NAME;
4628 size_t len = strlen (str);
4629 std::string lookup;
4630
4631 for (size_t i = 1; i < len; i++)
4632 {
4633 lookup.assign (str, i);
4634 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4635 EXPECT (Z_SYM_NAME));
4636 }
4637 }
4638
4639 /* In FULL mode, an incomplete symbol doesn't match. */
4640 {
4641 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4642 {});
4643 }
4644
4645 /* A complete symbol with parameters matches any overload, since the
4646 index has no overload info. */
4647 {
4648 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4649 EXPECT ("std::zfunction", "std::zfunction2"));
4650 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4651 EXPECT ("std::zfunction", "std::zfunction2"));
4652 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4653 EXPECT ("std::zfunction", "std::zfunction2"));
4654 }
4655
4656 /* Check that whitespace is ignored appropriately. A symbol with a
4657 template argument list. */
4658 {
4659 static const char expected[] = "ns::foo<int>";
4660 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4661 EXPECT (expected));
4662 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4663 EXPECT (expected));
4664 }
4665
4666 /* Check that whitespace is ignored appropriately. A symbol with a
4667 template argument list that includes a pointer. */
4668 {
4669 static const char expected[] = "ns::foo<char*>";
4670 /* Try both completion and non-completion modes. */
4671 static const bool completion_mode[2] = {false, true};
4672 for (size_t i = 0; i < 2; i++)
4673 {
4674 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4675 completion_mode[i], EXPECT (expected));
4676 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4677 completion_mode[i], EXPECT (expected));
4678
4679 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4680 completion_mode[i], EXPECT (expected));
4681 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4682 completion_mode[i], EXPECT (expected));
4683 }
4684 }
4685
4686 {
4687 /* Check method qualifiers are ignored. */
4688 static const char expected[] = "ns::foo<char*>";
4689 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4690 symbol_name_match_type::FULL, true, EXPECT (expected));
4691 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4692 symbol_name_match_type::FULL, true, EXPECT (expected));
4693 CHECK_MATCH ("foo < char * > ( int ) const",
4694 symbol_name_match_type::WILD, true, EXPECT (expected));
4695 CHECK_MATCH ("foo < char * > ( int ) &&",
4696 symbol_name_match_type::WILD, true, EXPECT (expected));
4697 }
4698
4699 /* Test lookup names that don't match anything. */
4700 {
4701 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4702 {});
4703
4704 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4705 {});
4706 }
4707
4708 /* Some wild matching tests, exercising "(anonymous namespace)",
4709 which should not be confused with a parameter list. */
4710 {
4711 static const char *syms[] = {
4712 "A::B::C",
4713 "B::C",
4714 "C",
4715 "A :: B :: C ( int )",
4716 "B :: C ( int )",
4717 "C ( int )",
4718 };
4719
4720 for (const char *s : syms)
4721 {
4722 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4723 EXPECT ("(anonymous namespace)::A::B::C"));
4724 }
4725 }
4726
4727 {
4728 static const char expected[] = "ns2::tmpl<int>::foo2";
4729 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4730 EXPECT (expected));
4731 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4732 EXPECT (expected));
4733 }
4734
4735 SELF_CHECK (!any_mismatch);
4736
4737 #undef EXPECT
4738 #undef CHECK_MATCH
4739 }
4740
4741 static void
4742 run_test ()
4743 {
4744 test_mapped_index_find_name_component_bounds ();
4745 test_dw2_expand_symtabs_matching_symbol ();
4746 }
4747
4748 }} // namespace selftests::dw2_expand_symtabs_matching
4749
4750 #endif /* GDB_SELF_TEST */
4751
4752 /* If FILE_MATCHER is NULL or if PER_CU has
4753 dwarf2_per_cu_quick_data::MARK set (see
4754 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4755 EXPANSION_NOTIFY on it. */
4756
4757 static void
4758 dw2_expand_symtabs_matching_one
4759 (dwarf2_per_cu_data *per_cu,
4760 dwarf2_per_objfile *per_objfile,
4761 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4762 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4763 {
4764 if (file_matcher == NULL || per_cu->v.quick->mark)
4765 {
4766 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4767
4768 compunit_symtab *symtab
4769 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4770 gdb_assert (symtab != nullptr);
4771
4772 if (expansion_notify != NULL && symtab_was_null)
4773 expansion_notify (symtab);
4774 }
4775 }
4776
4777 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4778 matched, to expand corresponding CUs that were marked. IDX is the
4779 index of the symbol name that matched. */
4780
4781 static void
4782 dw2_expand_marked_cus
4783 (dwarf2_per_objfile *per_objfile, offset_type idx,
4784 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4785 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4786 search_domain kind)
4787 {
4788 offset_type *vec, vec_len, vec_idx;
4789 bool global_seen = false;
4790 mapped_index &index = *per_objfile->per_bfd->index_table;
4791
4792 vec = (offset_type *) (index.constant_pool
4793 + MAYBE_SWAP (index.symbol_table[idx].vec));
4794 vec_len = MAYBE_SWAP (vec[0]);
4795 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4796 {
4797 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4798 /* This value is only valid for index versions >= 7. */
4799 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4800 gdb_index_symbol_kind symbol_kind =
4801 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4802 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4803 /* Only check the symbol attributes if they're present.
4804 Indices prior to version 7 don't record them,
4805 and indices >= 7 may elide them for certain symbols
4806 (gold does this). */
4807 int attrs_valid =
4808 (index.version >= 7
4809 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4810
4811 /* Work around gold/15646. */
4812 if (attrs_valid
4813 && !is_static
4814 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4815 {
4816 if (global_seen)
4817 continue;
4818
4819 global_seen = true;
4820 }
4821
4822 /* Only check the symbol's kind if it has one. */
4823 if (attrs_valid)
4824 {
4825 switch (kind)
4826 {
4827 case VARIABLES_DOMAIN:
4828 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4829 continue;
4830 break;
4831 case FUNCTIONS_DOMAIN:
4832 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4833 continue;
4834 break;
4835 case TYPES_DOMAIN:
4836 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4837 continue;
4838 break;
4839 case MODULES_DOMAIN:
4840 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4841 continue;
4842 break;
4843 default:
4844 break;
4845 }
4846 }
4847
4848 /* Don't crash on bad data. */
4849 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4850 + per_objfile->per_bfd->all_type_units.size ()))
4851 {
4852 complaint (_(".gdb_index entry has bad CU index"
4853 " [in module %s]"), objfile_name (per_objfile->objfile));
4854 continue;
4855 }
4856
4857 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4858 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4859 expansion_notify);
4860 }
4861 }
4862
4863 /* If FILE_MATCHER is non-NULL, set all the
4864 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4865 that match FILE_MATCHER. */
4866
4867 static void
4868 dw_expand_symtabs_matching_file_matcher
4869 (dwarf2_per_objfile *per_objfile,
4870 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4871 {
4872 if (file_matcher == NULL)
4873 return;
4874
4875 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4876 htab_eq_pointer,
4877 NULL, xcalloc, xfree));
4878 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4879 htab_eq_pointer,
4880 NULL, xcalloc, xfree));
4881
4882 /* The rule is CUs specify all the files, including those used by
4883 any TU, so there's no need to scan TUs here. */
4884
4885 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4886 {
4887 QUIT;
4888
4889 per_cu->v.quick->mark = 0;
4890
4891 /* We only need to look at symtabs not already expanded. */
4892 if (per_objfile->symtab_set_p (per_cu))
4893 continue;
4894
4895 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4896 if (file_data == NULL)
4897 continue;
4898
4899 if (htab_find (visited_not_found.get (), file_data) != NULL)
4900 continue;
4901 else if (htab_find (visited_found.get (), file_data) != NULL)
4902 {
4903 per_cu->v.quick->mark = 1;
4904 continue;
4905 }
4906
4907 for (int j = 0; j < file_data->num_file_names; ++j)
4908 {
4909 const char *this_real_name;
4910
4911 if (file_matcher (file_data->file_names[j], false))
4912 {
4913 per_cu->v.quick->mark = 1;
4914 break;
4915 }
4916
4917 /* Before we invoke realpath, which can get expensive when many
4918 files are involved, do a quick comparison of the basenames. */
4919 if (!basenames_may_differ
4920 && !file_matcher (lbasename (file_data->file_names[j]),
4921 true))
4922 continue;
4923
4924 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4925 if (file_matcher (this_real_name, false))
4926 {
4927 per_cu->v.quick->mark = 1;
4928 break;
4929 }
4930 }
4931
4932 void **slot = htab_find_slot (per_cu->v.quick->mark
4933 ? visited_found.get ()
4934 : visited_not_found.get (),
4935 file_data, INSERT);
4936 *slot = file_data;
4937 }
4938 }
4939
4940 static void
4941 dw2_expand_symtabs_matching
4942 (struct objfile *objfile,
4943 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4944 const lookup_name_info *lookup_name,
4945 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4946 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4947 enum search_domain kind)
4948 {
4949 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4950
4951 /* index_table is NULL if OBJF_READNOW. */
4952 if (!per_objfile->per_bfd->index_table)
4953 return;
4954
4955 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4956
4957 if (symbol_matcher == NULL && lookup_name == NULL)
4958 {
4959 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4960 {
4961 QUIT;
4962
4963 dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4964 file_matcher, expansion_notify);
4965 }
4966 return;
4967 }
4968
4969 mapped_index &index = *per_objfile->per_bfd->index_table;
4970
4971 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4972 symbol_matcher,
4973 kind, [&] (offset_type idx)
4974 {
4975 dw2_expand_marked_cus (per_objfile, idx, file_matcher, expansion_notify,
4976 kind);
4977 return true;
4978 }, per_objfile);
4979 }
4980
4981 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4982 symtab. */
4983
4984 static struct compunit_symtab *
4985 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4986 CORE_ADDR pc)
4987 {
4988 int i;
4989
4990 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4991 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4992 return cust;
4993
4994 if (cust->includes == NULL)
4995 return NULL;
4996
4997 for (i = 0; cust->includes[i]; ++i)
4998 {
4999 struct compunit_symtab *s = cust->includes[i];
5000
5001 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5002 if (s != NULL)
5003 return s;
5004 }
5005
5006 return NULL;
5007 }
5008
5009 static struct compunit_symtab *
5010 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5011 struct bound_minimal_symbol msymbol,
5012 CORE_ADDR pc,
5013 struct obj_section *section,
5014 int warn_if_readin)
5015 {
5016 struct dwarf2_per_cu_data *data;
5017 struct compunit_symtab *result;
5018
5019 if (!objfile->partial_symtabs->psymtabs_addrmap)
5020 return NULL;
5021
5022 CORE_ADDR baseaddr = objfile->text_section_offset ();
5023 data = (struct dwarf2_per_cu_data *) addrmap_find
5024 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
5025 if (!data)
5026 return NULL;
5027
5028 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5029 if (warn_if_readin && per_objfile->symtab_set_p (data))
5030 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5031 paddress (objfile->arch (), pc));
5032
5033 result = recursively_find_pc_sect_compunit_symtab
5034 (dw2_instantiate_symtab (data, per_objfile, false), pc);
5035
5036 gdb_assert (result != NULL);
5037 return result;
5038 }
5039
5040 static void
5041 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5042 void *data, int need_fullname)
5043 {
5044 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5045
5046 if (!per_objfile->per_bfd->filenames_cache)
5047 {
5048 per_objfile->per_bfd->filenames_cache.emplace ();
5049
5050 htab_up visited (htab_create_alloc (10,
5051 htab_hash_pointer, htab_eq_pointer,
5052 NULL, xcalloc, xfree));
5053
5054 /* The rule is CUs specify all the files, including those used
5055 by any TU, so there's no need to scan TUs here. We can
5056 ignore file names coming from already-expanded CUs. */
5057
5058 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5059 {
5060 if (per_objfile->symtab_set_p (per_cu))
5061 {
5062 void **slot = htab_find_slot (visited.get (),
5063 per_cu->v.quick->file_names,
5064 INSERT);
5065
5066 *slot = per_cu->v.quick->file_names;
5067 }
5068 }
5069
5070 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5071 {
5072 /* We only need to look at symtabs not already expanded. */
5073 if (per_objfile->symtab_set_p (per_cu))
5074 continue;
5075
5076 quick_file_names *file_data
5077 = dw2_get_file_names (per_cu, per_objfile);
5078 if (file_data == NULL)
5079 continue;
5080
5081 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
5082 if (*slot)
5083 {
5084 /* Already visited. */
5085 continue;
5086 }
5087 *slot = file_data;
5088
5089 for (int j = 0; j < file_data->num_file_names; ++j)
5090 {
5091 const char *filename = file_data->file_names[j];
5092 per_objfile->per_bfd->filenames_cache->seen (filename);
5093 }
5094 }
5095 }
5096
5097 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
5098 {
5099 gdb::unique_xmalloc_ptr<char> this_real_name;
5100
5101 if (need_fullname)
5102 this_real_name = gdb_realpath (filename);
5103 (*fun) (filename, this_real_name.get (), data);
5104 });
5105 }
5106
5107 static int
5108 dw2_has_symbols (struct objfile *objfile)
5109 {
5110 return 1;
5111 }
5112
5113 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5114 {
5115 dw2_has_symbols,
5116 dw2_find_last_source_symtab,
5117 dw2_forget_cached_source_info,
5118 dw2_map_symtabs_matching_filename,
5119 dw2_lookup_symbol,
5120 NULL,
5121 dw2_print_stats,
5122 dw2_dump,
5123 dw2_expand_symtabs_for_function,
5124 dw2_expand_all_symtabs,
5125 dw2_expand_symtabs_with_fullname,
5126 dw2_map_matching_symbols,
5127 dw2_expand_symtabs_matching,
5128 dw2_find_pc_sect_compunit_symtab,
5129 NULL,
5130 dw2_map_symbol_filenames
5131 };
5132
5133 /* DWARF-5 debug_names reader. */
5134
5135 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5136 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5137
5138 /* A helper function that reads the .debug_names section in SECTION
5139 and fills in MAP. FILENAME is the name of the file containing the
5140 section; it is used for error reporting.
5141
5142 Returns true if all went well, false otherwise. */
5143
5144 static bool
5145 read_debug_names_from_section (struct objfile *objfile,
5146 const char *filename,
5147 struct dwarf2_section_info *section,
5148 mapped_debug_names &map)
5149 {
5150 if (section->empty ())
5151 return false;
5152
5153 /* Older elfutils strip versions could keep the section in the main
5154 executable while splitting it for the separate debug info file. */
5155 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5156 return false;
5157
5158 section->read (objfile);
5159
5160 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
5161
5162 const gdb_byte *addr = section->buffer;
5163
5164 bfd *const abfd = section->get_bfd_owner ();
5165
5166 unsigned int bytes_read;
5167 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5168 addr += bytes_read;
5169
5170 map.dwarf5_is_dwarf64 = bytes_read != 4;
5171 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5172 if (bytes_read + length != section->size)
5173 {
5174 /* There may be multiple per-CU indices. */
5175 warning (_("Section .debug_names in %s length %s does not match "
5176 "section length %s, ignoring .debug_names."),
5177 filename, plongest (bytes_read + length),
5178 pulongest (section->size));
5179 return false;
5180 }
5181
5182 /* The version number. */
5183 uint16_t version = read_2_bytes (abfd, addr);
5184 addr += 2;
5185 if (version != 5)
5186 {
5187 warning (_("Section .debug_names in %s has unsupported version %d, "
5188 "ignoring .debug_names."),
5189 filename, version);
5190 return false;
5191 }
5192
5193 /* Padding. */
5194 uint16_t padding = read_2_bytes (abfd, addr);
5195 addr += 2;
5196 if (padding != 0)
5197 {
5198 warning (_("Section .debug_names in %s has unsupported padding %d, "
5199 "ignoring .debug_names."),
5200 filename, padding);
5201 return false;
5202 }
5203
5204 /* comp_unit_count - The number of CUs in the CU list. */
5205 map.cu_count = read_4_bytes (abfd, addr);
5206 addr += 4;
5207
5208 /* local_type_unit_count - The number of TUs in the local TU
5209 list. */
5210 map.tu_count = read_4_bytes (abfd, addr);
5211 addr += 4;
5212
5213 /* foreign_type_unit_count - The number of TUs in the foreign TU
5214 list. */
5215 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5216 addr += 4;
5217 if (foreign_tu_count != 0)
5218 {
5219 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5220 "ignoring .debug_names."),
5221 filename, static_cast<unsigned long> (foreign_tu_count));
5222 return false;
5223 }
5224
5225 /* bucket_count - The number of hash buckets in the hash lookup
5226 table. */
5227 map.bucket_count = read_4_bytes (abfd, addr);
5228 addr += 4;
5229
5230 /* name_count - The number of unique names in the index. */
5231 map.name_count = read_4_bytes (abfd, addr);
5232 addr += 4;
5233
5234 /* abbrev_table_size - The size in bytes of the abbreviations
5235 table. */
5236 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5237 addr += 4;
5238
5239 /* augmentation_string_size - The size in bytes of the augmentation
5240 string. This value is rounded up to a multiple of 4. */
5241 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5242 addr += 4;
5243 map.augmentation_is_gdb = ((augmentation_string_size
5244 == sizeof (dwarf5_augmentation))
5245 && memcmp (addr, dwarf5_augmentation,
5246 sizeof (dwarf5_augmentation)) == 0);
5247 augmentation_string_size += (-augmentation_string_size) & 3;
5248 addr += augmentation_string_size;
5249
5250 /* List of CUs */
5251 map.cu_table_reordered = addr;
5252 addr += map.cu_count * map.offset_size;
5253
5254 /* List of Local TUs */
5255 map.tu_table_reordered = addr;
5256 addr += map.tu_count * map.offset_size;
5257
5258 /* Hash Lookup Table */
5259 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5260 addr += map.bucket_count * 4;
5261 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5262 addr += map.name_count * 4;
5263
5264 /* Name Table */
5265 map.name_table_string_offs_reordered = addr;
5266 addr += map.name_count * map.offset_size;
5267 map.name_table_entry_offs_reordered = addr;
5268 addr += map.name_count * map.offset_size;
5269
5270 const gdb_byte *abbrev_table_start = addr;
5271 for (;;)
5272 {
5273 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5274 addr += bytes_read;
5275 if (index_num == 0)
5276 break;
5277
5278 const auto insertpair
5279 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5280 if (!insertpair.second)
5281 {
5282 warning (_("Section .debug_names in %s has duplicate index %s, "
5283 "ignoring .debug_names."),
5284 filename, pulongest (index_num));
5285 return false;
5286 }
5287 mapped_debug_names::index_val &indexval = insertpair.first->second;
5288 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5289 addr += bytes_read;
5290
5291 for (;;)
5292 {
5293 mapped_debug_names::index_val::attr attr;
5294 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5295 addr += bytes_read;
5296 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5297 addr += bytes_read;
5298 if (attr.form == DW_FORM_implicit_const)
5299 {
5300 attr.implicit_const = read_signed_leb128 (abfd, addr,
5301 &bytes_read);
5302 addr += bytes_read;
5303 }
5304 if (attr.dw_idx == 0 && attr.form == 0)
5305 break;
5306 indexval.attr_vec.push_back (std::move (attr));
5307 }
5308 }
5309 if (addr != abbrev_table_start + abbrev_table_size)
5310 {
5311 warning (_("Section .debug_names in %s has abbreviation_table "
5312 "of size %s vs. written as %u, ignoring .debug_names."),
5313 filename, plongest (addr - abbrev_table_start),
5314 abbrev_table_size);
5315 return false;
5316 }
5317 map.entry_pool = addr;
5318
5319 return true;
5320 }
5321
5322 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5323 list. */
5324
5325 static void
5326 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
5327 const mapped_debug_names &map,
5328 dwarf2_section_info &section,
5329 bool is_dwz)
5330 {
5331 if (!map.augmentation_is_gdb)
5332 {
5333 for (uint32_t i = 0; i < map.cu_count; ++i)
5334 {
5335 sect_offset sect_off
5336 = (sect_offset) (extract_unsigned_integer
5337 (map.cu_table_reordered + i * map.offset_size,
5338 map.offset_size,
5339 map.dwarf5_byte_order));
5340 /* We don't know the length of the CU, because the CU list in a
5341 .debug_names index can be incomplete, so we can't use the start
5342 of the next CU as end of this CU. We create the CUs here with
5343 length 0, and in cutu_reader::cutu_reader we'll fill in the
5344 actual length. */
5345 dwarf2_per_cu_data *per_cu
5346 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5347 sect_off, 0);
5348 per_bfd->all_comp_units.push_back (per_cu);
5349 }
5350 return;
5351 }
5352
5353 sect_offset sect_off_prev;
5354 for (uint32_t i = 0; i <= map.cu_count; ++i)
5355 {
5356 sect_offset sect_off_next;
5357 if (i < map.cu_count)
5358 {
5359 sect_off_next
5360 = (sect_offset) (extract_unsigned_integer
5361 (map.cu_table_reordered + i * map.offset_size,
5362 map.offset_size,
5363 map.dwarf5_byte_order));
5364 }
5365 else
5366 sect_off_next = (sect_offset) section.size;
5367 if (i >= 1)
5368 {
5369 const ULONGEST length = sect_off_next - sect_off_prev;
5370 dwarf2_per_cu_data *per_cu
5371 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5372 sect_off_prev, length);
5373 per_bfd->all_comp_units.push_back (per_cu);
5374 }
5375 sect_off_prev = sect_off_next;
5376 }
5377 }
5378
5379 /* Read the CU list from the mapped index, and use it to create all
5380 the CU objects for this dwarf2_per_objfile. */
5381
5382 static void
5383 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
5384 const mapped_debug_names &map,
5385 const mapped_debug_names &dwz_map)
5386 {
5387 gdb_assert (per_bfd->all_comp_units.empty ());
5388 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5389
5390 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
5391 false /* is_dwz */);
5392
5393 if (dwz_map.cu_count == 0)
5394 return;
5395
5396 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5397 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
5398 true /* is_dwz */);
5399 }
5400
5401 /* Read .debug_names. If everything went ok, initialize the "quick"
5402 elements of all the CUs and return true. Otherwise, return false. */
5403
5404 static bool
5405 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
5406 {
5407 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5408 mapped_debug_names dwz_map;
5409 struct objfile *objfile = per_objfile->objfile;
5410 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5411
5412 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5413 &per_objfile->per_bfd->debug_names, *map))
5414 return false;
5415
5416 /* Don't use the index if it's empty. */
5417 if (map->name_count == 0)
5418 return false;
5419
5420 /* If there is a .dwz file, read it so we can get its CU list as
5421 well. */
5422 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5423 if (dwz != NULL)
5424 {
5425 if (!read_debug_names_from_section (objfile,
5426 bfd_get_filename (dwz->dwz_bfd.get ()),
5427 &dwz->debug_names, dwz_map))
5428 {
5429 warning (_("could not read '.debug_names' section from %s; skipping"),
5430 bfd_get_filename (dwz->dwz_bfd.get ()));
5431 return false;
5432 }
5433 }
5434
5435 create_cus_from_debug_names (per_bfd, *map, dwz_map);
5436
5437 if (map->tu_count != 0)
5438 {
5439 /* We can only handle a single .debug_types when we have an
5440 index. */
5441 if (per_bfd->types.size () != 1)
5442 return false;
5443
5444 dwarf2_section_info *section = &per_bfd->types[0];
5445
5446 create_signatured_type_table_from_debug_names
5447 (per_objfile, *map, section, &per_bfd->abbrev);
5448 }
5449
5450 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
5451
5452 per_bfd->debug_names_table = std::move (map);
5453 per_bfd->using_index = 1;
5454 per_bfd->quick_file_names_table =
5455 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
5456
5457 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
5458 objfiles using the same BFD. */
5459 gdb_assert (per_bfd->partial_symtabs == nullptr);
5460 per_bfd->partial_symtabs = objfile->partial_symtabs;
5461
5462 return true;
5463 }
5464
5465 /* Type used to manage iterating over all CUs looking for a symbol for
5466 .debug_names. */
5467
5468 class dw2_debug_names_iterator
5469 {
5470 public:
5471 dw2_debug_names_iterator (const mapped_debug_names &map,
5472 gdb::optional<block_enum> block_index,
5473 domain_enum domain,
5474 const char *name, dwarf2_per_objfile *per_objfile)
5475 : m_map (map), m_block_index (block_index), m_domain (domain),
5476 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5477 m_per_objfile (per_objfile)
5478 {}
5479
5480 dw2_debug_names_iterator (const mapped_debug_names &map,
5481 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
5482 : m_map (map),
5483 m_search (search),
5484 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5485 m_per_objfile (per_objfile)
5486 {}
5487
5488 dw2_debug_names_iterator (const mapped_debug_names &map,
5489 block_enum block_index, domain_enum domain,
5490 uint32_t namei, dwarf2_per_objfile *per_objfile)
5491 : m_map (map), m_block_index (block_index), m_domain (domain),
5492 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5493 m_per_objfile (per_objfile)
5494 {}
5495
5496 /* Return the next matching CU or NULL if there are no more. */
5497 dwarf2_per_cu_data *next ();
5498
5499 private:
5500 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5501 const char *name,
5502 dwarf2_per_objfile *per_objfile);
5503 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5504 uint32_t namei,
5505 dwarf2_per_objfile *per_objfile);
5506
5507 /* The internalized form of .debug_names. */
5508 const mapped_debug_names &m_map;
5509
5510 /* If set, only look for symbols that match that block. Valid values are
5511 GLOBAL_BLOCK and STATIC_BLOCK. */
5512 const gdb::optional<block_enum> m_block_index;
5513
5514 /* The kind of symbol we're looking for. */
5515 const domain_enum m_domain = UNDEF_DOMAIN;
5516 const search_domain m_search = ALL_DOMAIN;
5517
5518 /* The list of CUs from the index entry of the symbol, or NULL if
5519 not found. */
5520 const gdb_byte *m_addr;
5521
5522 dwarf2_per_objfile *m_per_objfile;
5523 };
5524
5525 const char *
5526 mapped_debug_names::namei_to_name
5527 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
5528 {
5529 const ULONGEST namei_string_offs
5530 = extract_unsigned_integer ((name_table_string_offs_reordered
5531 + namei * offset_size),
5532 offset_size,
5533 dwarf5_byte_order);
5534 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
5535 }
5536
5537 /* Find a slot in .debug_names for the object named NAME. If NAME is
5538 found, return pointer to its pool data. If NAME cannot be found,
5539 return NULL. */
5540
5541 const gdb_byte *
5542 dw2_debug_names_iterator::find_vec_in_debug_names
5543 (const mapped_debug_names &map, const char *name,
5544 dwarf2_per_objfile *per_objfile)
5545 {
5546 int (*cmp) (const char *, const char *);
5547
5548 gdb::unique_xmalloc_ptr<char> without_params;
5549 if (current_language->la_language == language_cplus
5550 || current_language->la_language == language_fortran
5551 || current_language->la_language == language_d)
5552 {
5553 /* NAME is already canonical. Drop any qualifiers as
5554 .debug_names does not contain any. */
5555
5556 if (strchr (name, '(') != NULL)
5557 {
5558 without_params = cp_remove_params (name);
5559 if (without_params != NULL)
5560 name = without_params.get ();
5561 }
5562 }
5563
5564 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5565
5566 const uint32_t full_hash = dwarf5_djb_hash (name);
5567 uint32_t namei
5568 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5569 (map.bucket_table_reordered
5570 + (full_hash % map.bucket_count)), 4,
5571 map.dwarf5_byte_order);
5572 if (namei == 0)
5573 return NULL;
5574 --namei;
5575 if (namei >= map.name_count)
5576 {
5577 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5578 "[in module %s]"),
5579 namei, map.name_count,
5580 objfile_name (per_objfile->objfile));
5581 return NULL;
5582 }
5583
5584 for (;;)
5585 {
5586 const uint32_t namei_full_hash
5587 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5588 (map.hash_table_reordered + namei), 4,
5589 map.dwarf5_byte_order);
5590 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5591 return NULL;
5592
5593 if (full_hash == namei_full_hash)
5594 {
5595 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5596
5597 #if 0 /* An expensive sanity check. */
5598 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5599 {
5600 complaint (_("Wrong .debug_names hash for string at index %u "
5601 "[in module %s]"),
5602 namei, objfile_name (dwarf2_per_objfile->objfile));
5603 return NULL;
5604 }
5605 #endif
5606
5607 if (cmp (namei_string, name) == 0)
5608 {
5609 const ULONGEST namei_entry_offs
5610 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5611 + namei * map.offset_size),
5612 map.offset_size, map.dwarf5_byte_order);
5613 return map.entry_pool + namei_entry_offs;
5614 }
5615 }
5616
5617 ++namei;
5618 if (namei >= map.name_count)
5619 return NULL;
5620 }
5621 }
5622
5623 const gdb_byte *
5624 dw2_debug_names_iterator::find_vec_in_debug_names
5625 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5626 {
5627 if (namei >= map.name_count)
5628 {
5629 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5630 "[in module %s]"),
5631 namei, map.name_count,
5632 objfile_name (per_objfile->objfile));
5633 return NULL;
5634 }
5635
5636 const ULONGEST namei_entry_offs
5637 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5638 + namei * map.offset_size),
5639 map.offset_size, map.dwarf5_byte_order);
5640 return map.entry_pool + namei_entry_offs;
5641 }
5642
5643 /* See dw2_debug_names_iterator. */
5644
5645 dwarf2_per_cu_data *
5646 dw2_debug_names_iterator::next ()
5647 {
5648 if (m_addr == NULL)
5649 return NULL;
5650
5651 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5652 struct objfile *objfile = m_per_objfile->objfile;
5653 bfd *const abfd = objfile->obfd;
5654
5655 again:
5656
5657 unsigned int bytes_read;
5658 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5659 m_addr += bytes_read;
5660 if (abbrev == 0)
5661 return NULL;
5662
5663 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5664 if (indexval_it == m_map.abbrev_map.cend ())
5665 {
5666 complaint (_("Wrong .debug_names undefined abbrev code %s "
5667 "[in module %s]"),
5668 pulongest (abbrev), objfile_name (objfile));
5669 return NULL;
5670 }
5671 const mapped_debug_names::index_val &indexval = indexval_it->second;
5672 enum class symbol_linkage {
5673 unknown,
5674 static_,
5675 extern_,
5676 } symbol_linkage_ = symbol_linkage::unknown;
5677 dwarf2_per_cu_data *per_cu = NULL;
5678 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5679 {
5680 ULONGEST ull;
5681 switch (attr.form)
5682 {
5683 case DW_FORM_implicit_const:
5684 ull = attr.implicit_const;
5685 break;
5686 case DW_FORM_flag_present:
5687 ull = 1;
5688 break;
5689 case DW_FORM_udata:
5690 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5691 m_addr += bytes_read;
5692 break;
5693 case DW_FORM_ref4:
5694 ull = read_4_bytes (abfd, m_addr);
5695 m_addr += 4;
5696 break;
5697 case DW_FORM_ref8:
5698 ull = read_8_bytes (abfd, m_addr);
5699 m_addr += 8;
5700 break;
5701 case DW_FORM_ref_sig8:
5702 ull = read_8_bytes (abfd, m_addr);
5703 m_addr += 8;
5704 break;
5705 default:
5706 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5707 dwarf_form_name (attr.form),
5708 objfile_name (objfile));
5709 return NULL;
5710 }
5711 switch (attr.dw_idx)
5712 {
5713 case DW_IDX_compile_unit:
5714 /* Don't crash on bad data. */
5715 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
5716 {
5717 complaint (_(".debug_names entry has bad CU index %s"
5718 " [in module %s]"),
5719 pulongest (ull),
5720 objfile_name (objfile));
5721 continue;
5722 }
5723 per_cu = per_bfd->get_cutu (ull);
5724 break;
5725 case DW_IDX_type_unit:
5726 /* Don't crash on bad data. */
5727 if (ull >= per_bfd->all_type_units.size ())
5728 {
5729 complaint (_(".debug_names entry has bad TU index %s"
5730 " [in module %s]"),
5731 pulongest (ull),
5732 objfile_name (objfile));
5733 continue;
5734 }
5735 per_cu = &per_bfd->get_tu (ull)->per_cu;
5736 break;
5737 case DW_IDX_die_offset:
5738 /* In a per-CU index (as opposed to a per-module index), index
5739 entries without CU attribute implicitly refer to the single CU. */
5740 if (per_cu == NULL)
5741 per_cu = per_bfd->get_cu (0);
5742 break;
5743 case DW_IDX_GNU_internal:
5744 if (!m_map.augmentation_is_gdb)
5745 break;
5746 symbol_linkage_ = symbol_linkage::static_;
5747 break;
5748 case DW_IDX_GNU_external:
5749 if (!m_map.augmentation_is_gdb)
5750 break;
5751 symbol_linkage_ = symbol_linkage::extern_;
5752 break;
5753 }
5754 }
5755
5756 /* Skip if already read in. */
5757 if (m_per_objfile->symtab_set_p (per_cu))
5758 goto again;
5759
5760 /* Check static vs global. */
5761 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5762 {
5763 const bool want_static = *m_block_index == STATIC_BLOCK;
5764 const bool symbol_is_static =
5765 symbol_linkage_ == symbol_linkage::static_;
5766 if (want_static != symbol_is_static)
5767 goto again;
5768 }
5769
5770 /* Match dw2_symtab_iter_next, symbol_kind
5771 and debug_names::psymbol_tag. */
5772 switch (m_domain)
5773 {
5774 case VAR_DOMAIN:
5775 switch (indexval.dwarf_tag)
5776 {
5777 case DW_TAG_variable:
5778 case DW_TAG_subprogram:
5779 /* Some types are also in VAR_DOMAIN. */
5780 case DW_TAG_typedef:
5781 case DW_TAG_structure_type:
5782 break;
5783 default:
5784 goto again;
5785 }
5786 break;
5787 case STRUCT_DOMAIN:
5788 switch (indexval.dwarf_tag)
5789 {
5790 case DW_TAG_typedef:
5791 case DW_TAG_structure_type:
5792 break;
5793 default:
5794 goto again;
5795 }
5796 break;
5797 case LABEL_DOMAIN:
5798 switch (indexval.dwarf_tag)
5799 {
5800 case 0:
5801 case DW_TAG_variable:
5802 break;
5803 default:
5804 goto again;
5805 }
5806 break;
5807 case MODULE_DOMAIN:
5808 switch (indexval.dwarf_tag)
5809 {
5810 case DW_TAG_module:
5811 break;
5812 default:
5813 goto again;
5814 }
5815 break;
5816 default:
5817 break;
5818 }
5819
5820 /* Match dw2_expand_symtabs_matching, symbol_kind and
5821 debug_names::psymbol_tag. */
5822 switch (m_search)
5823 {
5824 case VARIABLES_DOMAIN:
5825 switch (indexval.dwarf_tag)
5826 {
5827 case DW_TAG_variable:
5828 break;
5829 default:
5830 goto again;
5831 }
5832 break;
5833 case FUNCTIONS_DOMAIN:
5834 switch (indexval.dwarf_tag)
5835 {
5836 case DW_TAG_subprogram:
5837 break;
5838 default:
5839 goto again;
5840 }
5841 break;
5842 case TYPES_DOMAIN:
5843 switch (indexval.dwarf_tag)
5844 {
5845 case DW_TAG_typedef:
5846 case DW_TAG_structure_type:
5847 break;
5848 default:
5849 goto again;
5850 }
5851 break;
5852 case MODULES_DOMAIN:
5853 switch (indexval.dwarf_tag)
5854 {
5855 case DW_TAG_module:
5856 break;
5857 default:
5858 goto again;
5859 }
5860 default:
5861 break;
5862 }
5863
5864 return per_cu;
5865 }
5866
5867 static struct compunit_symtab *
5868 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5869 const char *name, domain_enum domain)
5870 {
5871 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5872
5873 const auto &mapp = per_objfile->per_bfd->debug_names_table;
5874 if (!mapp)
5875 {
5876 /* index is NULL if OBJF_READNOW. */
5877 return NULL;
5878 }
5879 const auto &map = *mapp;
5880
5881 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
5882
5883 struct compunit_symtab *stab_best = NULL;
5884 struct dwarf2_per_cu_data *per_cu;
5885 while ((per_cu = iter.next ()) != NULL)
5886 {
5887 struct symbol *sym, *with_opaque = NULL;
5888 compunit_symtab *stab
5889 = dw2_instantiate_symtab (per_cu, per_objfile, false);
5890 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5891 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5892
5893 sym = block_find_symbol (block, name, domain,
5894 block_find_non_opaque_type_preferred,
5895 &with_opaque);
5896
5897 /* Some caution must be observed with overloaded functions and
5898 methods, since the index will not contain any overload
5899 information (but NAME might contain it). */
5900
5901 if (sym != NULL
5902 && strcmp_iw (sym->search_name (), name) == 0)
5903 return stab;
5904 if (with_opaque != NULL
5905 && strcmp_iw (with_opaque->search_name (), name) == 0)
5906 stab_best = stab;
5907
5908 /* Keep looking through other CUs. */
5909 }
5910
5911 return stab_best;
5912 }
5913
5914 /* This dumps minimal information about .debug_names. It is called
5915 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5916 uses this to verify that .debug_names has been loaded. */
5917
5918 static void
5919 dw2_debug_names_dump (struct objfile *objfile)
5920 {
5921 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5922
5923 gdb_assert (per_objfile->per_bfd->using_index);
5924 printf_filtered (".debug_names:");
5925 if (per_objfile->per_bfd->debug_names_table)
5926 printf_filtered (" exists\n");
5927 else
5928 printf_filtered (" faked for \"readnow\"\n");
5929 printf_filtered ("\n");
5930 }
5931
5932 static void
5933 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5934 const char *func_name)
5935 {
5936 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5937
5938 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5939 if (per_objfile->per_bfd->debug_names_table)
5940 {
5941 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5942
5943 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
5944 per_objfile);
5945
5946 struct dwarf2_per_cu_data *per_cu;
5947 while ((per_cu = iter.next ()) != NULL)
5948 dw2_instantiate_symtab (per_cu, per_objfile, false);
5949 }
5950 }
5951
5952 static void
5953 dw2_debug_names_map_matching_symbols
5954 (struct objfile *objfile,
5955 const lookup_name_info &name, domain_enum domain,
5956 int global,
5957 gdb::function_view<symbol_found_callback_ftype> callback,
5958 symbol_compare_ftype *ordered_compare)
5959 {
5960 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5961
5962 /* debug_names_table is NULL if OBJF_READNOW. */
5963 if (!per_objfile->per_bfd->debug_names_table)
5964 return;
5965
5966 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5967 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5968
5969 const char *match_name = name.ada ().lookup_name ().c_str ();
5970 auto matcher = [&] (const char *symname)
5971 {
5972 if (ordered_compare == nullptr)
5973 return true;
5974 return ordered_compare (symname, match_name) == 0;
5975 };
5976
5977 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5978 [&] (offset_type namei)
5979 {
5980 /* The name was matched, now expand corresponding CUs that were
5981 marked. */
5982 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
5983 per_objfile);
5984
5985 struct dwarf2_per_cu_data *per_cu;
5986 while ((per_cu = iter.next ()) != NULL)
5987 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5988 nullptr);
5989 return true;
5990 }, per_objfile);
5991
5992 /* It's a shame we couldn't do this inside the
5993 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5994 that have already been expanded. Instead, this loop matches what
5995 the psymtab code does. */
5996 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5997 {
5998 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
5999 if (symtab != nullptr)
6000 {
6001 const struct block *block
6002 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
6003 if (!iterate_over_symbols_terminated (block, name,
6004 domain, callback))
6005 break;
6006 }
6007 }
6008 }
6009
6010 static void
6011 dw2_debug_names_expand_symtabs_matching
6012 (struct objfile *objfile,
6013 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6014 const lookup_name_info *lookup_name,
6015 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6016 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6017 enum search_domain kind)
6018 {
6019 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6020
6021 /* debug_names_table is NULL if OBJF_READNOW. */
6022 if (!per_objfile->per_bfd->debug_names_table)
6023 return;
6024
6025 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
6026
6027 if (symbol_matcher == NULL && lookup_name == NULL)
6028 {
6029 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
6030 {
6031 QUIT;
6032
6033 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
6034 expansion_notify);
6035 }
6036 return;
6037 }
6038
6039 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
6040
6041 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
6042 symbol_matcher,
6043 kind, [&] (offset_type namei)
6044 {
6045 /* The name was matched, now expand corresponding CUs that were
6046 marked. */
6047 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
6048
6049 struct dwarf2_per_cu_data *per_cu;
6050 while ((per_cu = iter.next ()) != NULL)
6051 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
6052 expansion_notify);
6053 return true;
6054 }, per_objfile);
6055 }
6056
6057 const struct quick_symbol_functions dwarf2_debug_names_functions =
6058 {
6059 dw2_has_symbols,
6060 dw2_find_last_source_symtab,
6061 dw2_forget_cached_source_info,
6062 dw2_map_symtabs_matching_filename,
6063 dw2_debug_names_lookup_symbol,
6064 NULL,
6065 dw2_print_stats,
6066 dw2_debug_names_dump,
6067 dw2_debug_names_expand_symtabs_for_function,
6068 dw2_expand_all_symtabs,
6069 dw2_expand_symtabs_with_fullname,
6070 dw2_debug_names_map_matching_symbols,
6071 dw2_debug_names_expand_symtabs_matching,
6072 dw2_find_pc_sect_compunit_symtab,
6073 NULL,
6074 dw2_map_symbol_filenames
6075 };
6076
6077 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6078 to either a dwarf2_per_bfd or dwz_file object. */
6079
6080 template <typename T>
6081 static gdb::array_view<const gdb_byte>
6082 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6083 {
6084 dwarf2_section_info *section = &section_owner->gdb_index;
6085
6086 if (section->empty ())
6087 return {};
6088
6089 /* Older elfutils strip versions could keep the section in the main
6090 executable while splitting it for the separate debug info file. */
6091 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
6092 return {};
6093
6094 section->read (obj);
6095
6096 /* dwarf2_section_info::size is a bfd_size_type, while
6097 gdb::array_view works with size_t. On 32-bit hosts, with
6098 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6099 is 32-bit. So we need an explicit narrowing conversion here.
6100 This is fine, because it's impossible to allocate or mmap an
6101 array/buffer larger than what size_t can represent. */
6102 return gdb::make_array_view (section->buffer, section->size);
6103 }
6104
6105 /* Lookup the index cache for the contents of the index associated to
6106 DWARF2_OBJ. */
6107
6108 static gdb::array_view<const gdb_byte>
6109 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
6110 {
6111 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6112 if (build_id == nullptr)
6113 return {};
6114
6115 return global_index_cache.lookup_gdb_index (build_id,
6116 &dwarf2_per_bfd->index_cache_res);
6117 }
6118
6119 /* Same as the above, but for DWZ. */
6120
6121 static gdb::array_view<const gdb_byte>
6122 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6123 {
6124 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6125 if (build_id == nullptr)
6126 return {};
6127
6128 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6129 }
6130
6131 /* See symfile.h. */
6132
6133 bool
6134 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6135 {
6136 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6137 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6138
6139 /* If we're about to read full symbols, don't bother with the
6140 indices. In this case we also don't care if some other debug
6141 format is making psymtabs, because they are all about to be
6142 expanded anyway. */
6143 if ((objfile->flags & OBJF_READNOW))
6144 {
6145 /* When using READNOW, the using_index flag (set below) indicates that
6146 PER_BFD was already initialized, when we loaded some other objfile. */
6147 if (per_bfd->using_index)
6148 {
6149 *index_kind = dw_index_kind::GDB_INDEX;
6150 per_objfile->resize_symtabs ();
6151 return true;
6152 }
6153
6154 per_bfd->using_index = 1;
6155 create_all_comp_units (per_objfile);
6156 create_all_type_units (per_objfile);
6157 per_bfd->quick_file_names_table
6158 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
6159 per_objfile->resize_symtabs ();
6160
6161 for (int i = 0; i < (per_bfd->all_comp_units.size ()
6162 + per_bfd->all_type_units.size ()); ++i)
6163 {
6164 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
6165
6166 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6167 struct dwarf2_per_cu_quick_data);
6168 }
6169
6170 /* Return 1 so that gdb sees the "quick" functions. However,
6171 these functions will be no-ops because we will have expanded
6172 all symtabs. */
6173 *index_kind = dw_index_kind::GDB_INDEX;
6174 return true;
6175 }
6176
6177 /* Was a debug names index already read when we processed an objfile sharing
6178 PER_BFD? */
6179 if (per_bfd->debug_names_table != nullptr)
6180 {
6181 *index_kind = dw_index_kind::DEBUG_NAMES;
6182 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
6183 per_objfile->resize_symtabs ();
6184 return true;
6185 }
6186
6187 /* Was a GDB index already read when we processed an objfile sharing
6188 PER_BFD? */
6189 if (per_bfd->index_table != nullptr)
6190 {
6191 *index_kind = dw_index_kind::GDB_INDEX;
6192 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
6193 per_objfile->resize_symtabs ();
6194 return true;
6195 }
6196
6197 /* There might already be partial symtabs built for this BFD. This happens
6198 when loading the same binary twice with the index-cache enabled. If so,
6199 don't try to read an index. The objfile / per_objfile initialization will
6200 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
6201 code path. */
6202 if (per_bfd->partial_symtabs != nullptr)
6203 return false;
6204
6205 if (dwarf2_read_debug_names (per_objfile))
6206 {
6207 *index_kind = dw_index_kind::DEBUG_NAMES;
6208 per_objfile->resize_symtabs ();
6209 return true;
6210 }
6211
6212 if (dwarf2_read_gdb_index (per_objfile,
6213 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
6214 get_gdb_index_contents_from_section<dwz_file>))
6215 {
6216 *index_kind = dw_index_kind::GDB_INDEX;
6217 per_objfile->resize_symtabs ();
6218 return true;
6219 }
6220
6221 /* ... otherwise, try to find the index in the index cache. */
6222 if (dwarf2_read_gdb_index (per_objfile,
6223 get_gdb_index_contents_from_cache,
6224 get_gdb_index_contents_from_cache_dwz))
6225 {
6226 global_index_cache.hit ();
6227 *index_kind = dw_index_kind::GDB_INDEX;
6228 per_objfile->resize_symtabs ();
6229 return true;
6230 }
6231
6232 global_index_cache.miss ();
6233 return false;
6234 }
6235
6236 \f
6237
6238 /* Build a partial symbol table. */
6239
6240 void
6241 dwarf2_build_psymtabs (struct objfile *objfile)
6242 {
6243 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6244 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6245
6246 if (per_bfd->partial_symtabs != nullptr)
6247 {
6248 /* Partial symbols were already read, so now we can simply
6249 attach them. */
6250 objfile->partial_symtabs = per_bfd->partial_symtabs;
6251 per_objfile->resize_symtabs ();
6252 return;
6253 }
6254
6255 try
6256 {
6257 /* This isn't really ideal: all the data we allocate on the
6258 objfile's obstack is still uselessly kept around. However,
6259 freeing it seems unsafe. */
6260 psymtab_discarder psymtabs (objfile);
6261 dwarf2_build_psymtabs_hard (per_objfile);
6262 psymtabs.keep ();
6263
6264 per_objfile->resize_symtabs ();
6265
6266 /* (maybe) store an index in the cache. */
6267 global_index_cache.store (per_objfile);
6268 }
6269 catch (const gdb_exception_error &except)
6270 {
6271 exception_print (gdb_stderr, except);
6272 }
6273
6274 /* Finish by setting the local reference to partial symtabs, so that
6275 we don't try to read them again if reading another objfile with the same
6276 BFD. If we can't in fact share, this won't make a difference anyway as
6277 the dwarf2_per_bfd object won't be shared. */
6278 per_bfd->partial_symtabs = objfile->partial_symtabs;
6279 }
6280
6281 /* Find the base address of the compilation unit for range lists and
6282 location lists. It will normally be specified by DW_AT_low_pc.
6283 In DWARF-3 draft 4, the base address could be overridden by
6284 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6285 compilation units with discontinuous ranges. */
6286
6287 static void
6288 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6289 {
6290 struct attribute *attr;
6291
6292 cu->base_address.reset ();
6293
6294 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6295 if (attr != nullptr)
6296 cu->base_address = attr->as_address ();
6297 else
6298 {
6299 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6300 if (attr != nullptr)
6301 cu->base_address = attr->as_address ();
6302 }
6303 }
6304
6305 /* Helper function that returns the proper abbrev section for
6306 THIS_CU. */
6307
6308 static struct dwarf2_section_info *
6309 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6310 {
6311 struct dwarf2_section_info *abbrev;
6312 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6313
6314 if (this_cu->is_dwz)
6315 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
6316 else
6317 abbrev = &per_bfd->abbrev;
6318
6319 return abbrev;
6320 }
6321
6322 /* Fetch the abbreviation table offset from a comp or type unit header. */
6323
6324 static sect_offset
6325 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
6326 struct dwarf2_section_info *section,
6327 sect_offset sect_off)
6328 {
6329 bfd *abfd = section->get_bfd_owner ();
6330 const gdb_byte *info_ptr;
6331 unsigned int initial_length_size, offset_size;
6332 uint16_t version;
6333
6334 section->read (per_objfile->objfile);
6335 info_ptr = section->buffer + to_underlying (sect_off);
6336 read_initial_length (abfd, info_ptr, &initial_length_size);
6337 offset_size = initial_length_size == 4 ? 4 : 8;
6338 info_ptr += initial_length_size;
6339
6340 version = read_2_bytes (abfd, info_ptr);
6341 info_ptr += 2;
6342 if (version >= 5)
6343 {
6344 /* Skip unit type and address size. */
6345 info_ptr += 2;
6346 }
6347
6348 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6349 }
6350
6351 /* A partial symtab that is used only for include files. */
6352 struct dwarf2_include_psymtab : public partial_symtab
6353 {
6354 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6355 : partial_symtab (filename, objfile)
6356 {
6357 }
6358
6359 void read_symtab (struct objfile *objfile) override
6360 {
6361 /* It's an include file, no symbols to read for it.
6362 Everything is in the includer symtab. */
6363
6364 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6365 expansion of the includer psymtab. We use the dependencies[0] field to
6366 model the includer. But if we go the regular route of calling
6367 expand_psymtab here, and having expand_psymtab call expand_dependencies
6368 to expand the includer, we'll only use expand_psymtab on the includer
6369 (making it a non-toplevel psymtab), while if we expand the includer via
6370 another path, we'll use read_symtab (making it a toplevel psymtab).
6371 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6372 psymtab, and trigger read_symtab on the includer here directly. */
6373 includer ()->read_symtab (objfile);
6374 }
6375
6376 void expand_psymtab (struct objfile *objfile) override
6377 {
6378 /* This is not called by read_symtab, and should not be called by any
6379 expand_dependencies. */
6380 gdb_assert (false);
6381 }
6382
6383 bool readin_p (struct objfile *objfile) const override
6384 {
6385 return includer ()->readin_p (objfile);
6386 }
6387
6388 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6389 {
6390 return nullptr;
6391 }
6392
6393 private:
6394 partial_symtab *includer () const
6395 {
6396 /* An include psymtab has exactly one dependency: the psymtab that
6397 includes it. */
6398 gdb_assert (this->number_of_dependencies == 1);
6399 return this->dependencies[0];
6400 }
6401 };
6402
6403 /* Allocate a new partial symtab for file named NAME and mark this new
6404 partial symtab as being an include of PST. */
6405
6406 static void
6407 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6408 struct objfile *objfile)
6409 {
6410 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6411
6412 if (!IS_ABSOLUTE_PATH (subpst->filename))
6413 subpst->dirname = pst->dirname;
6414
6415 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6416 subpst->dependencies[0] = pst;
6417 subpst->number_of_dependencies = 1;
6418 }
6419
6420 /* Read the Line Number Program data and extract the list of files
6421 included by the source file represented by PST. Build an include
6422 partial symtab for each of these included files. */
6423
6424 static void
6425 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6426 struct die_info *die,
6427 dwarf2_psymtab *pst)
6428 {
6429 line_header_up lh;
6430 struct attribute *attr;
6431
6432 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6433 if (attr != nullptr && attr->form_is_unsigned ())
6434 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
6435 if (lh == NULL)
6436 return; /* No linetable, so no includes. */
6437
6438 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6439 that we pass in the raw text_low here; that is ok because we're
6440 only decoding the line table to make include partial symtabs, and
6441 so the addresses aren't really used. */
6442 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6443 pst->raw_text_low (), 1);
6444 }
6445
6446 static hashval_t
6447 hash_signatured_type (const void *item)
6448 {
6449 const struct signatured_type *sig_type
6450 = (const struct signatured_type *) item;
6451
6452 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6453 return sig_type->signature;
6454 }
6455
6456 static int
6457 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6458 {
6459 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6460 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6461
6462 return lhs->signature == rhs->signature;
6463 }
6464
6465 /* Allocate a hash table for signatured types. */
6466
6467 static htab_up
6468 allocate_signatured_type_table ()
6469 {
6470 return htab_up (htab_create_alloc (41,
6471 hash_signatured_type,
6472 eq_signatured_type,
6473 NULL, xcalloc, xfree));
6474 }
6475
6476 /* A helper function to add a signatured type CU to a table. */
6477
6478 static int
6479 add_signatured_type_cu_to_table (void **slot, void *datum)
6480 {
6481 struct signatured_type *sigt = (struct signatured_type *) *slot;
6482 std::vector<signatured_type *> *all_type_units
6483 = (std::vector<signatured_type *> *) datum;
6484
6485 all_type_units->push_back (sigt);
6486
6487 return 1;
6488 }
6489
6490 /* A helper for create_debug_types_hash_table. Read types from SECTION
6491 and fill them into TYPES_HTAB. It will process only type units,
6492 therefore DW_UT_type. */
6493
6494 static void
6495 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
6496 struct dwo_file *dwo_file,
6497 dwarf2_section_info *section, htab_up &types_htab,
6498 rcuh_kind section_kind)
6499 {
6500 struct objfile *objfile = per_objfile->objfile;
6501 struct dwarf2_section_info *abbrev_section;
6502 bfd *abfd;
6503 const gdb_byte *info_ptr, *end_ptr;
6504
6505 abbrev_section = (dwo_file != NULL
6506 ? &dwo_file->sections.abbrev
6507 : &per_objfile->per_bfd->abbrev);
6508
6509 dwarf_read_debug_printf ("Reading %s for %s:",
6510 section->get_name (),
6511 abbrev_section->get_file_name ());
6512
6513 section->read (objfile);
6514 info_ptr = section->buffer;
6515
6516 if (info_ptr == NULL)
6517 return;
6518
6519 /* We can't set abfd until now because the section may be empty or
6520 not present, in which case the bfd is unknown. */
6521 abfd = section->get_bfd_owner ();
6522
6523 /* We don't use cutu_reader here because we don't need to read
6524 any dies: the signature is in the header. */
6525
6526 end_ptr = info_ptr + section->size;
6527 while (info_ptr < end_ptr)
6528 {
6529 struct signatured_type *sig_type;
6530 struct dwo_unit *dwo_tu;
6531 void **slot;
6532 const gdb_byte *ptr = info_ptr;
6533 struct comp_unit_head header;
6534 unsigned int length;
6535
6536 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6537
6538 /* Initialize it due to a false compiler warning. */
6539 header.signature = -1;
6540 header.type_cu_offset_in_tu = (cu_offset) -1;
6541
6542 /* We need to read the type's signature in order to build the hash
6543 table, but we don't need anything else just yet. */
6544
6545 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
6546 abbrev_section, ptr, section_kind);
6547
6548 length = header.get_length ();
6549
6550 /* Skip dummy type units. */
6551 if (ptr >= info_ptr + length
6552 || peek_abbrev_code (abfd, ptr) == 0
6553 || (header.unit_type != DW_UT_type
6554 && header.unit_type != DW_UT_split_type))
6555 {
6556 info_ptr += length;
6557 continue;
6558 }
6559
6560 if (types_htab == NULL)
6561 {
6562 if (dwo_file)
6563 types_htab = allocate_dwo_unit_table ();
6564 else
6565 types_htab = allocate_signatured_type_table ();
6566 }
6567
6568 if (dwo_file)
6569 {
6570 sig_type = NULL;
6571 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
6572 dwo_tu->dwo_file = dwo_file;
6573 dwo_tu->signature = header.signature;
6574 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6575 dwo_tu->section = section;
6576 dwo_tu->sect_off = sect_off;
6577 dwo_tu->length = length;
6578 }
6579 else
6580 {
6581 /* N.B.: type_offset is not usable if this type uses a DWO file.
6582 The real type_offset is in the DWO file. */
6583 dwo_tu = NULL;
6584 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6585 sig_type->signature = header.signature;
6586 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6587 sig_type->per_cu.is_debug_types = 1;
6588 sig_type->per_cu.section = section;
6589 sig_type->per_cu.sect_off = sect_off;
6590 sig_type->per_cu.length = length;
6591 }
6592
6593 slot = htab_find_slot (types_htab.get (),
6594 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6595 INSERT);
6596 gdb_assert (slot != NULL);
6597 if (*slot != NULL)
6598 {
6599 sect_offset dup_sect_off;
6600
6601 if (dwo_file)
6602 {
6603 const struct dwo_unit *dup_tu
6604 = (const struct dwo_unit *) *slot;
6605
6606 dup_sect_off = dup_tu->sect_off;
6607 }
6608 else
6609 {
6610 const struct signatured_type *dup_tu
6611 = (const struct signatured_type *) *slot;
6612
6613 dup_sect_off = dup_tu->per_cu.sect_off;
6614 }
6615
6616 complaint (_("debug type entry at offset %s is duplicate to"
6617 " the entry at offset %s, signature %s"),
6618 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6619 hex_string (header.signature));
6620 }
6621 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6622
6623 dwarf_read_debug_printf_v (" offset %s, signature %s",
6624 sect_offset_str (sect_off),
6625 hex_string (header.signature));
6626
6627 info_ptr += length;
6628 }
6629 }
6630
6631 /* Create the hash table of all entries in the .debug_types
6632 (or .debug_types.dwo) section(s).
6633 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6634 otherwise it is NULL.
6635
6636 The result is a pointer to the hash table or NULL if there are no types.
6637
6638 Note: This function processes DWO files only, not DWP files. */
6639
6640 static void
6641 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
6642 struct dwo_file *dwo_file,
6643 gdb::array_view<dwarf2_section_info> type_sections,
6644 htab_up &types_htab)
6645 {
6646 for (dwarf2_section_info &section : type_sections)
6647 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6648 rcuh_kind::TYPE);
6649 }
6650
6651 /* Create the hash table of all entries in the .debug_types section,
6652 and initialize all_type_units.
6653 The result is zero if there is an error (e.g. missing .debug_types section),
6654 otherwise non-zero. */
6655
6656 static int
6657 create_all_type_units (dwarf2_per_objfile *per_objfile)
6658 {
6659 htab_up types_htab;
6660
6661 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6662 types_htab, rcuh_kind::COMPILE);
6663 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6664 types_htab);
6665 if (types_htab == NULL)
6666 {
6667 per_objfile->per_bfd->signatured_types = NULL;
6668 return 0;
6669 }
6670
6671 per_objfile->per_bfd->signatured_types = std::move (types_htab);
6672
6673 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6674 per_objfile->per_bfd->all_type_units.reserve
6675 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
6676
6677 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
6678 add_signatured_type_cu_to_table,
6679 &per_objfile->per_bfd->all_type_units);
6680
6681 return 1;
6682 }
6683
6684 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6685 If SLOT is non-NULL, it is the entry to use in the hash table.
6686 Otherwise we find one. */
6687
6688 static struct signatured_type *
6689 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6690 {
6691 if (per_objfile->per_bfd->all_type_units.size ()
6692 == per_objfile->per_bfd->all_type_units.capacity ())
6693 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6694
6695 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6696
6697 per_objfile->resize_symtabs ();
6698
6699 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6700 sig_type->signature = sig;
6701 sig_type->per_cu.is_debug_types = 1;
6702 if (per_objfile->per_bfd->using_index)
6703 {
6704 sig_type->per_cu.v.quick =
6705 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6706 struct dwarf2_per_cu_quick_data);
6707 }
6708
6709 if (slot == NULL)
6710 {
6711 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6712 sig_type, INSERT);
6713 }
6714 gdb_assert (*slot == NULL);
6715 *slot = sig_type;
6716 /* The rest of sig_type must be filled in by the caller. */
6717 return sig_type;
6718 }
6719
6720 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6721 Fill in SIG_ENTRY with DWO_ENTRY. */
6722
6723 static void
6724 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
6725 struct signatured_type *sig_entry,
6726 struct dwo_unit *dwo_entry)
6727 {
6728 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6729
6730 /* Make sure we're not clobbering something we don't expect to. */
6731 gdb_assert (! sig_entry->per_cu.queued);
6732 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
6733 if (per_bfd->using_index)
6734 {
6735 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6736 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6737 }
6738 else
6739 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6740 gdb_assert (sig_entry->signature == dwo_entry->signature);
6741 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6742 gdb_assert (sig_entry->type_unit_group == NULL);
6743 gdb_assert (sig_entry->dwo_unit == NULL);
6744
6745 sig_entry->per_cu.section = dwo_entry->section;
6746 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6747 sig_entry->per_cu.length = dwo_entry->length;
6748 sig_entry->per_cu.reading_dwo_directly = 1;
6749 sig_entry->per_cu.per_bfd = per_bfd;
6750 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6751 sig_entry->dwo_unit = dwo_entry;
6752 }
6753
6754 /* Subroutine of lookup_signatured_type.
6755 If we haven't read the TU yet, create the signatured_type data structure
6756 for a TU to be read in directly from a DWO file, bypassing the stub.
6757 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6758 using .gdb_index, then when reading a CU we want to stay in the DWO file
6759 containing that CU. Otherwise we could end up reading several other DWO
6760 files (due to comdat folding) to process the transitive closure of all the
6761 mentioned TUs, and that can be slow. The current DWO file will have every
6762 type signature that it needs.
6763 We only do this for .gdb_index because in the psymtab case we already have
6764 to read all the DWOs to build the type unit groups. */
6765
6766 static struct signatured_type *
6767 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6768 {
6769 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6770 struct dwo_file *dwo_file;
6771 struct dwo_unit find_dwo_entry, *dwo_entry;
6772 struct signatured_type find_sig_entry, *sig_entry;
6773 void **slot;
6774
6775 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6776
6777 /* If TU skeletons have been removed then we may not have read in any
6778 TUs yet. */
6779 if (per_objfile->per_bfd->signatured_types == NULL)
6780 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6781
6782 /* We only ever need to read in one copy of a signatured type.
6783 Use the global signatured_types array to do our own comdat-folding
6784 of types. If this is the first time we're reading this TU, and
6785 the TU has an entry in .gdb_index, replace the recorded data from
6786 .gdb_index with this TU. */
6787
6788 find_sig_entry.signature = sig;
6789 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6790 &find_sig_entry, INSERT);
6791 sig_entry = (struct signatured_type *) *slot;
6792
6793 /* We can get here with the TU already read, *or* in the process of being
6794 read. Don't reassign the global entry to point to this DWO if that's
6795 the case. Also note that if the TU is already being read, it may not
6796 have come from a DWO, the program may be a mix of Fission-compiled
6797 code and non-Fission-compiled code. */
6798
6799 /* Have we already tried to read this TU?
6800 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6801 needn't exist in the global table yet). */
6802 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6803 return sig_entry;
6804
6805 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6806 dwo_unit of the TU itself. */
6807 dwo_file = cu->dwo_unit->dwo_file;
6808
6809 /* Ok, this is the first time we're reading this TU. */
6810 if (dwo_file->tus == NULL)
6811 return NULL;
6812 find_dwo_entry.signature = sig;
6813 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6814 &find_dwo_entry);
6815 if (dwo_entry == NULL)
6816 return NULL;
6817
6818 /* If the global table doesn't have an entry for this TU, add one. */
6819 if (sig_entry == NULL)
6820 sig_entry = add_type_unit (per_objfile, sig, slot);
6821
6822 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6823 sig_entry->per_cu.tu_read = 1;
6824 return sig_entry;
6825 }
6826
6827 /* Subroutine of lookup_signatured_type.
6828 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6829 then try the DWP file. If the TU stub (skeleton) has been removed then
6830 it won't be in .gdb_index. */
6831
6832 static struct signatured_type *
6833 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6834 {
6835 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6836 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6837 struct dwo_unit *dwo_entry;
6838 struct signatured_type find_sig_entry, *sig_entry;
6839 void **slot;
6840
6841 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6842 gdb_assert (dwp_file != NULL);
6843
6844 /* If TU skeletons have been removed then we may not have read in any
6845 TUs yet. */
6846 if (per_objfile->per_bfd->signatured_types == NULL)
6847 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6848
6849 find_sig_entry.signature = sig;
6850 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6851 &find_sig_entry, INSERT);
6852 sig_entry = (struct signatured_type *) *slot;
6853
6854 /* Have we already tried to read this TU?
6855 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6856 needn't exist in the global table yet). */
6857 if (sig_entry != NULL)
6858 return sig_entry;
6859
6860 if (dwp_file->tus == NULL)
6861 return NULL;
6862 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6863 1 /* is_debug_types */);
6864 if (dwo_entry == NULL)
6865 return NULL;
6866
6867 sig_entry = add_type_unit (per_objfile, sig, slot);
6868 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6869
6870 return sig_entry;
6871 }
6872
6873 /* Lookup a signature based type for DW_FORM_ref_sig8.
6874 Returns NULL if signature SIG is not present in the table.
6875 It is up to the caller to complain about this. */
6876
6877 static struct signatured_type *
6878 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6879 {
6880 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6881
6882 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6883 {
6884 /* We're in a DWO/DWP file, and we're using .gdb_index.
6885 These cases require special processing. */
6886 if (get_dwp_file (per_objfile) == NULL)
6887 return lookup_dwo_signatured_type (cu, sig);
6888 else
6889 return lookup_dwp_signatured_type (cu, sig);
6890 }
6891 else
6892 {
6893 struct signatured_type find_entry, *entry;
6894
6895 if (per_objfile->per_bfd->signatured_types == NULL)
6896 return NULL;
6897 find_entry.signature = sig;
6898 entry = ((struct signatured_type *)
6899 htab_find (per_objfile->per_bfd->signatured_types.get (),
6900 &find_entry));
6901 return entry;
6902 }
6903 }
6904
6905 /* Low level DIE reading support. */
6906
6907 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6908
6909 static void
6910 init_cu_die_reader (struct die_reader_specs *reader,
6911 struct dwarf2_cu *cu,
6912 struct dwarf2_section_info *section,
6913 struct dwo_file *dwo_file,
6914 struct abbrev_table *abbrev_table)
6915 {
6916 gdb_assert (section->readin && section->buffer != NULL);
6917 reader->abfd = section->get_bfd_owner ();
6918 reader->cu = cu;
6919 reader->dwo_file = dwo_file;
6920 reader->die_section = section;
6921 reader->buffer = section->buffer;
6922 reader->buffer_end = section->buffer + section->size;
6923 reader->abbrev_table = abbrev_table;
6924 }
6925
6926 /* Subroutine of cutu_reader to simplify it.
6927 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6928 There's just a lot of work to do, and cutu_reader is big enough
6929 already.
6930
6931 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6932 from it to the DIE in the DWO. If NULL we are skipping the stub.
6933 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6934 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6935 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6936 STUB_COMP_DIR may be non-NULL.
6937 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6938 are filled in with the info of the DIE from the DWO file.
6939 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6940 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6941 kept around for at least as long as *RESULT_READER.
6942
6943 The result is non-zero if a valid (non-dummy) DIE was found. */
6944
6945 static int
6946 read_cutu_die_from_dwo (dwarf2_cu *cu,
6947 struct dwo_unit *dwo_unit,
6948 struct die_info *stub_comp_unit_die,
6949 const char *stub_comp_dir,
6950 struct die_reader_specs *result_reader,
6951 const gdb_byte **result_info_ptr,
6952 struct die_info **result_comp_unit_die,
6953 abbrev_table_up *result_dwo_abbrev_table)
6954 {
6955 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6956 dwarf2_per_cu_data *per_cu = cu->per_cu;
6957 struct objfile *objfile = per_objfile->objfile;
6958 bfd *abfd;
6959 const gdb_byte *begin_info_ptr, *info_ptr;
6960 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6961 int i,num_extra_attrs;
6962 struct dwarf2_section_info *dwo_abbrev_section;
6963 struct die_info *comp_unit_die;
6964
6965 /* At most one of these may be provided. */
6966 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6967
6968 /* These attributes aren't processed until later:
6969 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6970 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6971 referenced later. However, these attributes are found in the stub
6972 which we won't have later. In order to not impose this complication
6973 on the rest of the code, we read them here and copy them to the
6974 DWO CU/TU die. */
6975
6976 stmt_list = NULL;
6977 low_pc = NULL;
6978 high_pc = NULL;
6979 ranges = NULL;
6980 comp_dir = NULL;
6981
6982 if (stub_comp_unit_die != NULL)
6983 {
6984 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6985 DWO file. */
6986 if (!per_cu->is_debug_types)
6987 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6988 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6989 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6990 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6991 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6992
6993 cu->addr_base = stub_comp_unit_die->addr_base ();
6994
6995 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6996 We need the value before we can process DW_AT_ranges values from the
6997 DWO. */
6998 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6999
7000 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
7001 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
7002 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
7003 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
7004 section. */
7005 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
7006 }
7007 else if (stub_comp_dir != NULL)
7008 {
7009 /* Reconstruct the comp_dir attribute to simplify the code below. */
7010 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
7011 comp_dir->name = DW_AT_comp_dir;
7012 comp_dir->form = DW_FORM_string;
7013 comp_dir->set_string_noncanonical (stub_comp_dir);
7014 }
7015
7016 /* Set up for reading the DWO CU/TU. */
7017 cu->dwo_unit = dwo_unit;
7018 dwarf2_section_info *section = dwo_unit->section;
7019 section->read (objfile);
7020 abfd = section->get_bfd_owner ();
7021 begin_info_ptr = info_ptr = (section->buffer
7022 + to_underlying (dwo_unit->sect_off));
7023 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7024
7025 if (per_cu->is_debug_types)
7026 {
7027 signatured_type *sig_type = (struct signatured_type *) per_cu;
7028
7029 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7030 section, dwo_abbrev_section,
7031 info_ptr, rcuh_kind::TYPE);
7032 /* This is not an assert because it can be caused by bad debug info. */
7033 if (sig_type->signature != cu->header.signature)
7034 {
7035 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7036 " TU at offset %s [in module %s]"),
7037 hex_string (sig_type->signature),
7038 hex_string (cu->header.signature),
7039 sect_offset_str (dwo_unit->sect_off),
7040 bfd_get_filename (abfd));
7041 }
7042 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7043 /* For DWOs coming from DWP files, we don't know the CU length
7044 nor the type's offset in the TU until now. */
7045 dwo_unit->length = cu->header.get_length ();
7046 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7047
7048 /* Establish the type offset that can be used to lookup the type.
7049 For DWO files, we don't know it until now. */
7050 sig_type->type_offset_in_section
7051 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7052 }
7053 else
7054 {
7055 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7056 section, dwo_abbrev_section,
7057 info_ptr, rcuh_kind::COMPILE);
7058 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7059 /* For DWOs coming from DWP files, we don't know the CU length
7060 until now. */
7061 dwo_unit->length = cu->header.get_length ();
7062 }
7063
7064 dwo_abbrev_section->read (objfile);
7065 *result_dwo_abbrev_table
7066 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
7067 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7068 result_dwo_abbrev_table->get ());
7069
7070 /* Read in the die, but leave space to copy over the attributes
7071 from the stub. This has the benefit of simplifying the rest of
7072 the code - all the work to maintain the illusion of a single
7073 DW_TAG_{compile,type}_unit DIE is done here. */
7074 num_extra_attrs = ((stmt_list != NULL)
7075 + (low_pc != NULL)
7076 + (high_pc != NULL)
7077 + (ranges != NULL)
7078 + (comp_dir != NULL));
7079 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7080 num_extra_attrs);
7081
7082 /* Copy over the attributes from the stub to the DIE we just read in. */
7083 comp_unit_die = *result_comp_unit_die;
7084 i = comp_unit_die->num_attrs;
7085 if (stmt_list != NULL)
7086 comp_unit_die->attrs[i++] = *stmt_list;
7087 if (low_pc != NULL)
7088 comp_unit_die->attrs[i++] = *low_pc;
7089 if (high_pc != NULL)
7090 comp_unit_die->attrs[i++] = *high_pc;
7091 if (ranges != NULL)
7092 comp_unit_die->attrs[i++] = *ranges;
7093 if (comp_dir != NULL)
7094 comp_unit_die->attrs[i++] = *comp_dir;
7095 comp_unit_die->num_attrs += num_extra_attrs;
7096
7097 if (dwarf_die_debug)
7098 {
7099 fprintf_unfiltered (gdb_stdlog,
7100 "Read die from %s@0x%x of %s:\n",
7101 section->get_name (),
7102 (unsigned) (begin_info_ptr - section->buffer),
7103 bfd_get_filename (abfd));
7104 dump_die (comp_unit_die, dwarf_die_debug);
7105 }
7106
7107 /* Skip dummy compilation units. */
7108 if (info_ptr >= begin_info_ptr + dwo_unit->length
7109 || peek_abbrev_code (abfd, info_ptr) == 0)
7110 return 0;
7111
7112 *result_info_ptr = info_ptr;
7113 return 1;
7114 }
7115
7116 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7117 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7118 signature is part of the header. */
7119 static gdb::optional<ULONGEST>
7120 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7121 {
7122 if (cu->header.version >= 5)
7123 return cu->header.signature;
7124 struct attribute *attr;
7125 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7126 if (attr == nullptr || !attr->form_is_unsigned ())
7127 return gdb::optional<ULONGEST> ();
7128 return attr->as_unsigned ();
7129 }
7130
7131 /* Subroutine of cutu_reader to simplify it.
7132 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7133 Returns NULL if the specified DWO unit cannot be found. */
7134
7135 static struct dwo_unit *
7136 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
7137 {
7138 dwarf2_per_cu_data *per_cu = cu->per_cu;
7139 struct dwo_unit *dwo_unit;
7140 const char *comp_dir;
7141
7142 gdb_assert (cu != NULL);
7143
7144 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7145 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7146 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7147
7148 if (per_cu->is_debug_types)
7149 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
7150 else
7151 {
7152 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7153
7154 if (!signature.has_value ())
7155 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7156 " [in module %s]"),
7157 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
7158
7159 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
7160 }
7161
7162 return dwo_unit;
7163 }
7164
7165 /* Subroutine of cutu_reader to simplify it.
7166 See it for a description of the parameters.
7167 Read a TU directly from a DWO file, bypassing the stub. */
7168
7169 void
7170 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
7171 dwarf2_per_objfile *per_objfile,
7172 dwarf2_cu *existing_cu)
7173 {
7174 struct signatured_type *sig_type;
7175
7176 /* Verify we can do the following downcast, and that we have the
7177 data we need. */
7178 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7179 sig_type = (struct signatured_type *) this_cu;
7180 gdb_assert (sig_type->dwo_unit != NULL);
7181
7182 dwarf2_cu *cu;
7183
7184 if (existing_cu != nullptr)
7185 {
7186 cu = existing_cu;
7187 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
7188 /* There's no need to do the rereading_dwo_cu handling that
7189 cutu_reader does since we don't read the stub. */
7190 }
7191 else
7192 {
7193 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7194 in per_objfile yet. */
7195 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7196 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7197 cu = m_new_cu.get ();
7198 }
7199
7200 /* A future optimization, if needed, would be to use an existing
7201 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7202 could share abbrev tables. */
7203
7204 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
7205 NULL /* stub_comp_unit_die */,
7206 sig_type->dwo_unit->dwo_file->comp_dir,
7207 this, &info_ptr,
7208 &comp_unit_die,
7209 &m_dwo_abbrev_table) == 0)
7210 {
7211 /* Dummy die. */
7212 dummy_p = true;
7213 }
7214 }
7215
7216 /* Initialize a CU (or TU) and read its DIEs.
7217 If the CU defers to a DWO file, read the DWO file as well.
7218
7219 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7220 Otherwise the table specified in the comp unit header is read in and used.
7221 This is an optimization for when we already have the abbrev table.
7222
7223 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7224 allocated. */
7225
7226 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7227 dwarf2_per_objfile *per_objfile,
7228 struct abbrev_table *abbrev_table,
7229 dwarf2_cu *existing_cu,
7230 bool skip_partial)
7231 : die_reader_specs {},
7232 m_this_cu (this_cu)
7233 {
7234 struct objfile *objfile = per_objfile->objfile;
7235 struct dwarf2_section_info *section = this_cu->section;
7236 bfd *abfd = section->get_bfd_owner ();
7237 const gdb_byte *begin_info_ptr;
7238 struct signatured_type *sig_type = NULL;
7239 struct dwarf2_section_info *abbrev_section;
7240 /* Non-zero if CU currently points to a DWO file and we need to
7241 reread it. When this happens we need to reread the skeleton die
7242 before we can reread the DWO file (this only applies to CUs, not TUs). */
7243 int rereading_dwo_cu = 0;
7244
7245 if (dwarf_die_debug)
7246 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7247 this_cu->is_debug_types ? "type" : "comp",
7248 sect_offset_str (this_cu->sect_off));
7249
7250 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7251 file (instead of going through the stub), short-circuit all of this. */
7252 if (this_cu->reading_dwo_directly)
7253 {
7254 /* Narrow down the scope of possibilities to have to understand. */
7255 gdb_assert (this_cu->is_debug_types);
7256 gdb_assert (abbrev_table == NULL);
7257 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
7258 return;
7259 }
7260
7261 /* This is cheap if the section is already read in. */
7262 section->read (objfile);
7263
7264 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7265
7266 abbrev_section = get_abbrev_section_for_cu (this_cu);
7267
7268 dwarf2_cu *cu;
7269
7270 if (existing_cu != nullptr)
7271 {
7272 cu = existing_cu;
7273 /* If this CU is from a DWO file we need to start over, we need to
7274 refetch the attributes from the skeleton CU.
7275 This could be optimized by retrieving those attributes from when we
7276 were here the first time: the previous comp_unit_die was stored in
7277 comp_unit_obstack. But there's no data yet that we need this
7278 optimization. */
7279 if (cu->dwo_unit != NULL)
7280 rereading_dwo_cu = 1;
7281 }
7282 else
7283 {
7284 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7285 in per_objfile yet. */
7286 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7287 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7288 cu = m_new_cu.get ();
7289 }
7290
7291 /* Get the header. */
7292 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7293 {
7294 /* We already have the header, there's no need to read it in again. */
7295 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7296 }
7297 else
7298 {
7299 if (this_cu->is_debug_types)
7300 {
7301 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7302 section, abbrev_section,
7303 info_ptr, rcuh_kind::TYPE);
7304
7305 /* Since per_cu is the first member of struct signatured_type,
7306 we can go from a pointer to one to a pointer to the other. */
7307 sig_type = (struct signatured_type *) this_cu;
7308 gdb_assert (sig_type->signature == cu->header.signature);
7309 gdb_assert (sig_type->type_offset_in_tu
7310 == cu->header.type_cu_offset_in_tu);
7311 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7312
7313 /* LENGTH has not been set yet for type units if we're
7314 using .gdb_index. */
7315 this_cu->length = cu->header.get_length ();
7316
7317 /* Establish the type offset that can be used to lookup the type. */
7318 sig_type->type_offset_in_section =
7319 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7320
7321 this_cu->dwarf_version = cu->header.version;
7322 }
7323 else
7324 {
7325 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7326 section, abbrev_section,
7327 info_ptr,
7328 rcuh_kind::COMPILE);
7329
7330 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7331 if (this_cu->length == 0)
7332 this_cu->length = cu->header.get_length ();
7333 else
7334 gdb_assert (this_cu->length == cu->header.get_length ());
7335 this_cu->dwarf_version = cu->header.version;
7336 }
7337 }
7338
7339 /* Skip dummy compilation units. */
7340 if (info_ptr >= begin_info_ptr + this_cu->length
7341 || peek_abbrev_code (abfd, info_ptr) == 0)
7342 {
7343 dummy_p = true;
7344 return;
7345 }
7346
7347 /* If we don't have them yet, read the abbrevs for this compilation unit.
7348 And if we need to read them now, make sure they're freed when we're
7349 done. */
7350 if (abbrev_table != NULL)
7351 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7352 else
7353 {
7354 abbrev_section->read (objfile);
7355 m_abbrev_table_holder
7356 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
7357 abbrev_table = m_abbrev_table_holder.get ();
7358 }
7359
7360 /* Read the top level CU/TU die. */
7361 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7362 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7363
7364 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7365 {
7366 dummy_p = true;
7367 return;
7368 }
7369
7370 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7371 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7372 table from the DWO file and pass the ownership over to us. It will be
7373 referenced from READER, so we must make sure to free it after we're done
7374 with READER.
7375
7376 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7377 DWO CU, that this test will fail (the attribute will not be present). */
7378 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7379 if (dwo_name != nullptr)
7380 {
7381 struct dwo_unit *dwo_unit;
7382 struct die_info *dwo_comp_unit_die;
7383
7384 if (comp_unit_die->has_children)
7385 {
7386 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7387 " has children (offset %s) [in module %s]"),
7388 sect_offset_str (this_cu->sect_off),
7389 bfd_get_filename (abfd));
7390 }
7391 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
7392 if (dwo_unit != NULL)
7393 {
7394 if (read_cutu_die_from_dwo (cu, dwo_unit,
7395 comp_unit_die, NULL,
7396 this, &info_ptr,
7397 &dwo_comp_unit_die,
7398 &m_dwo_abbrev_table) == 0)
7399 {
7400 /* Dummy die. */
7401 dummy_p = true;
7402 return;
7403 }
7404 comp_unit_die = dwo_comp_unit_die;
7405 }
7406 else
7407 {
7408 /* Yikes, we couldn't find the rest of the DIE, we only have
7409 the stub. A complaint has already been logged. There's
7410 not much more we can do except pass on the stub DIE to
7411 die_reader_func. We don't want to throw an error on bad
7412 debug info. */
7413 }
7414 }
7415 }
7416
7417 void
7418 cutu_reader::keep ()
7419 {
7420 /* Done, clean up. */
7421 gdb_assert (!dummy_p);
7422 if (m_new_cu != NULL)
7423 {
7424 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7425 now. */
7426 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7427 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
7428 }
7429 }
7430
7431 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7432 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7433 assumed to have already done the lookup to find the DWO file).
7434
7435 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7436 THIS_CU->is_debug_types, but nothing else.
7437
7438 We fill in THIS_CU->length.
7439
7440 THIS_CU->cu is always freed when done.
7441 This is done in order to not leave THIS_CU->cu in a state where we have
7442 to care whether it refers to the "main" CU or the DWO CU.
7443
7444 When parent_cu is passed, it is used to provide a default value for
7445 str_offsets_base and addr_base from the parent. */
7446
7447 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7448 dwarf2_per_objfile *per_objfile,
7449 struct dwarf2_cu *parent_cu,
7450 struct dwo_file *dwo_file)
7451 : die_reader_specs {},
7452 m_this_cu (this_cu)
7453 {
7454 struct objfile *objfile = per_objfile->objfile;
7455 struct dwarf2_section_info *section = this_cu->section;
7456 bfd *abfd = section->get_bfd_owner ();
7457 struct dwarf2_section_info *abbrev_section;
7458 const gdb_byte *begin_info_ptr, *info_ptr;
7459
7460 if (dwarf_die_debug)
7461 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7462 this_cu->is_debug_types ? "type" : "comp",
7463 sect_offset_str (this_cu->sect_off));
7464
7465 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7466
7467 abbrev_section = (dwo_file != NULL
7468 ? &dwo_file->sections.abbrev
7469 : get_abbrev_section_for_cu (this_cu));
7470
7471 /* This is cheap if the section is already read in. */
7472 section->read (objfile);
7473
7474 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7475
7476 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7477 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7478 section, abbrev_section, info_ptr,
7479 (this_cu->is_debug_types
7480 ? rcuh_kind::TYPE
7481 : rcuh_kind::COMPILE));
7482
7483 if (parent_cu != nullptr)
7484 {
7485 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7486 m_new_cu->addr_base = parent_cu->addr_base;
7487 }
7488 this_cu->length = m_new_cu->header.get_length ();
7489
7490 /* Skip dummy compilation units. */
7491 if (info_ptr >= begin_info_ptr + this_cu->length
7492 || peek_abbrev_code (abfd, info_ptr) == 0)
7493 {
7494 dummy_p = true;
7495 return;
7496 }
7497
7498 abbrev_section->read (objfile);
7499 m_abbrev_table_holder
7500 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
7501
7502 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7503 m_abbrev_table_holder.get ());
7504 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7505 }
7506
7507 \f
7508 /* Type Unit Groups.
7509
7510 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7511 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7512 so that all types coming from the same compilation (.o file) are grouped
7513 together. A future step could be to put the types in the same symtab as
7514 the CU the types ultimately came from. */
7515
7516 static hashval_t
7517 hash_type_unit_group (const void *item)
7518 {
7519 const struct type_unit_group *tu_group
7520 = (const struct type_unit_group *) item;
7521
7522 return hash_stmt_list_entry (&tu_group->hash);
7523 }
7524
7525 static int
7526 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7527 {
7528 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7529 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7530
7531 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7532 }
7533
7534 /* Allocate a hash table for type unit groups. */
7535
7536 static htab_up
7537 allocate_type_unit_groups_table ()
7538 {
7539 return htab_up (htab_create_alloc (3,
7540 hash_type_unit_group,
7541 eq_type_unit_group,
7542 NULL, xcalloc, xfree));
7543 }
7544
7545 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7546 partial symtabs. We combine several TUs per psymtab to not let the size
7547 of any one psymtab grow too big. */
7548 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7549 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7550
7551 /* Helper routine for get_type_unit_group.
7552 Create the type_unit_group object used to hold one or more TUs. */
7553
7554 static struct type_unit_group *
7555 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7556 {
7557 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7558 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7559 struct dwarf2_per_cu_data *per_cu;
7560 struct type_unit_group *tu_group;
7561
7562 tu_group = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, type_unit_group);
7563 per_cu = &tu_group->per_cu;
7564 per_cu->per_bfd = per_bfd;
7565
7566 if (per_bfd->using_index)
7567 {
7568 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7569 struct dwarf2_per_cu_quick_data);
7570 }
7571 else
7572 {
7573 unsigned int line_offset = to_underlying (line_offset_struct);
7574 dwarf2_psymtab *pst;
7575 std::string name;
7576
7577 /* Give the symtab a useful name for debug purposes. */
7578 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7579 name = string_printf ("<type_units_%d>",
7580 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7581 else
7582 name = string_printf ("<type_units_at_0x%x>", line_offset);
7583
7584 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
7585 pst->anonymous = true;
7586 }
7587
7588 tu_group->hash.dwo_unit = cu->dwo_unit;
7589 tu_group->hash.line_sect_off = line_offset_struct;
7590
7591 return tu_group;
7592 }
7593
7594 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7595 STMT_LIST is a DW_AT_stmt_list attribute. */
7596
7597 static struct type_unit_group *
7598 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7599 {
7600 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7601 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7602 struct type_unit_group *tu_group;
7603 void **slot;
7604 unsigned int line_offset;
7605 struct type_unit_group type_unit_group_for_lookup;
7606
7607 if (per_objfile->per_bfd->type_unit_groups == NULL)
7608 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7609
7610 /* Do we need to create a new group, or can we use an existing one? */
7611
7612 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
7613 {
7614 line_offset = stmt_list->as_unsigned ();
7615 ++tu_stats->nr_symtab_sharers;
7616 }
7617 else
7618 {
7619 /* Ugh, no stmt_list. Rare, but we have to handle it.
7620 We can do various things here like create one group per TU or
7621 spread them over multiple groups to split up the expansion work.
7622 To avoid worst case scenarios (too many groups or too large groups)
7623 we, umm, group them in bunches. */
7624 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7625 | (tu_stats->nr_stmt_less_type_units
7626 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7627 ++tu_stats->nr_stmt_less_type_units;
7628 }
7629
7630 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7631 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7632 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
7633 &type_unit_group_for_lookup, INSERT);
7634 if (*slot != NULL)
7635 {
7636 tu_group = (struct type_unit_group *) *slot;
7637 gdb_assert (tu_group != NULL);
7638 }
7639 else
7640 {
7641 sect_offset line_offset_struct = (sect_offset) line_offset;
7642 tu_group = create_type_unit_group (cu, line_offset_struct);
7643 *slot = tu_group;
7644 ++tu_stats->nr_symtabs;
7645 }
7646
7647 return tu_group;
7648 }
7649 \f
7650 /* Partial symbol tables. */
7651
7652 /* Create a psymtab named NAME and assign it to PER_CU.
7653
7654 The caller must fill in the following details:
7655 dirname, textlow, texthigh. */
7656
7657 static dwarf2_psymtab *
7658 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7659 dwarf2_per_objfile *per_objfile,
7660 const char *name)
7661 {
7662 struct objfile *objfile = per_objfile->objfile;
7663 dwarf2_psymtab *pst;
7664
7665 pst = new dwarf2_psymtab (name, objfile, per_cu);
7666
7667 pst->psymtabs_addrmap_supported = true;
7668
7669 /* This is the glue that links PST into GDB's symbol API. */
7670 per_cu->v.psymtab = pst;
7671
7672 return pst;
7673 }
7674
7675 /* DIE reader function for process_psymtab_comp_unit. */
7676
7677 static void
7678 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7679 const gdb_byte *info_ptr,
7680 struct die_info *comp_unit_die,
7681 enum language pretend_language)
7682 {
7683 struct dwarf2_cu *cu = reader->cu;
7684 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7685 struct objfile *objfile = per_objfile->objfile;
7686 struct gdbarch *gdbarch = objfile->arch ();
7687 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7688 CORE_ADDR baseaddr;
7689 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7690 dwarf2_psymtab *pst;
7691 enum pc_bounds_kind cu_bounds_kind;
7692 const char *filename;
7693
7694 gdb_assert (! per_cu->is_debug_types);
7695
7696 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7697
7698 /* Allocate a new partial symbol table structure. */
7699 gdb::unique_xmalloc_ptr<char> debug_filename;
7700 static const char artificial[] = "<artificial>";
7701 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7702 if (filename == NULL)
7703 filename = "";
7704 else if (strcmp (filename, artificial) == 0)
7705 {
7706 debug_filename.reset (concat (artificial, "@",
7707 sect_offset_str (per_cu->sect_off),
7708 (char *) NULL));
7709 filename = debug_filename.get ();
7710 }
7711
7712 pst = create_partial_symtab (per_cu, per_objfile, filename);
7713
7714 /* This must be done before calling dwarf2_build_include_psymtabs. */
7715 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7716
7717 baseaddr = objfile->text_section_offset ();
7718
7719 dwarf2_find_base_address (comp_unit_die, cu);
7720
7721 /* Possibly set the default values of LOWPC and HIGHPC from
7722 `DW_AT_ranges'. */
7723 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7724 &best_highpc, cu, pst);
7725 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7726 {
7727 CORE_ADDR low
7728 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7729 - baseaddr);
7730 CORE_ADDR high
7731 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7732 - baseaddr - 1);
7733 /* Store the contiguous range if it is not empty; it can be
7734 empty for CUs with no code. */
7735 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7736 low, high, pst);
7737 }
7738
7739 /* Check if comp unit has_children.
7740 If so, read the rest of the partial symbols from this comp unit.
7741 If not, there's no more debug_info for this comp unit. */
7742 if (comp_unit_die->has_children)
7743 {
7744 struct partial_die_info *first_die;
7745 CORE_ADDR lowpc, highpc;
7746
7747 lowpc = ((CORE_ADDR) -1);
7748 highpc = ((CORE_ADDR) 0);
7749
7750 first_die = load_partial_dies (reader, info_ptr, 1);
7751
7752 scan_partial_symbols (first_die, &lowpc, &highpc,
7753 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7754
7755 /* If we didn't find a lowpc, set it to highpc to avoid
7756 complaints from `maint check'. */
7757 if (lowpc == ((CORE_ADDR) -1))
7758 lowpc = highpc;
7759
7760 /* If the compilation unit didn't have an explicit address range,
7761 then use the information extracted from its child dies. */
7762 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7763 {
7764 best_lowpc = lowpc;
7765 best_highpc = highpc;
7766 }
7767 }
7768 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7769 best_lowpc + baseaddr)
7770 - baseaddr);
7771 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7772 best_highpc + baseaddr)
7773 - baseaddr);
7774
7775 pst->end ();
7776
7777 if (!cu->per_cu->imported_symtabs_empty ())
7778 {
7779 int i;
7780 int len = cu->per_cu->imported_symtabs_size ();
7781
7782 /* Fill in 'dependencies' here; we fill in 'users' in a
7783 post-pass. */
7784 pst->number_of_dependencies = len;
7785 pst->dependencies
7786 = objfile->partial_symtabs->allocate_dependencies (len);
7787 for (i = 0; i < len; ++i)
7788 {
7789 pst->dependencies[i]
7790 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7791 }
7792
7793 cu->per_cu->imported_symtabs_free ();
7794 }
7795
7796 /* Get the list of files included in the current compilation unit,
7797 and build a psymtab for each of them. */
7798 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7799
7800 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7801 ", %d global, %d static syms",
7802 per_cu->is_debug_types ? "type" : "comp",
7803 sect_offset_str (per_cu->sect_off),
7804 paddress (gdbarch, pst->text_low (objfile)),
7805 paddress (gdbarch, pst->text_high (objfile)),
7806 (int) pst->global_psymbols.size (),
7807 (int) pst->static_psymbols.size ());
7808 }
7809
7810 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7811 Process compilation unit THIS_CU for a psymtab. */
7812
7813 static void
7814 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7815 dwarf2_per_objfile *per_objfile,
7816 bool want_partial_unit,
7817 enum language pretend_language)
7818 {
7819 /* If this compilation unit was already read in, free the
7820 cached copy in order to read it in again. This is
7821 necessary because we skipped some symbols when we first
7822 read in the compilation unit (see load_partial_dies).
7823 This problem could be avoided, but the benefit is unclear. */
7824 per_objfile->remove_cu (this_cu);
7825
7826 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7827
7828 switch (reader.comp_unit_die->tag)
7829 {
7830 case DW_TAG_compile_unit:
7831 this_cu->unit_type = DW_UT_compile;
7832 break;
7833 case DW_TAG_partial_unit:
7834 this_cu->unit_type = DW_UT_partial;
7835 break;
7836 case DW_TAG_type_unit:
7837 this_cu->unit_type = DW_UT_type;
7838 break;
7839 default:
7840 abort ();
7841 }
7842
7843 if (reader.dummy_p)
7844 {
7845 /* Nothing. */
7846 }
7847 else if (this_cu->is_debug_types)
7848 build_type_psymtabs_reader (&reader, reader.info_ptr,
7849 reader.comp_unit_die);
7850 else if (want_partial_unit
7851 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7852 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7853 reader.comp_unit_die,
7854 pretend_language);
7855
7856 this_cu->lang = reader.cu->language;
7857
7858 /* Age out any secondary CUs. */
7859 per_objfile->age_comp_units ();
7860 }
7861
7862 /* Reader function for build_type_psymtabs. */
7863
7864 static void
7865 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7866 const gdb_byte *info_ptr,
7867 struct die_info *type_unit_die)
7868 {
7869 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7870 struct dwarf2_cu *cu = reader->cu;
7871 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7872 struct signatured_type *sig_type;
7873 struct type_unit_group *tu_group;
7874 struct attribute *attr;
7875 struct partial_die_info *first_die;
7876 CORE_ADDR lowpc, highpc;
7877 dwarf2_psymtab *pst;
7878
7879 gdb_assert (per_cu->is_debug_types);
7880 sig_type = (struct signatured_type *) per_cu;
7881
7882 if (! type_unit_die->has_children)
7883 return;
7884
7885 attr = type_unit_die->attr (DW_AT_stmt_list);
7886 tu_group = get_type_unit_group (cu, attr);
7887
7888 if (tu_group->tus == nullptr)
7889 tu_group->tus = new std::vector<signatured_type *>;
7890 tu_group->tus->push_back (sig_type);
7891
7892 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7893 pst = create_partial_symtab (per_cu, per_objfile, "");
7894 pst->anonymous = true;
7895
7896 first_die = load_partial_dies (reader, info_ptr, 1);
7897
7898 lowpc = (CORE_ADDR) -1;
7899 highpc = (CORE_ADDR) 0;
7900 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7901
7902 pst->end ();
7903 }
7904
7905 /* Struct used to sort TUs by their abbreviation table offset. */
7906
7907 struct tu_abbrev_offset
7908 {
7909 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7910 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7911 {}
7912
7913 signatured_type *sig_type;
7914 sect_offset abbrev_offset;
7915 };
7916
7917 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7918
7919 static bool
7920 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7921 const struct tu_abbrev_offset &b)
7922 {
7923 return a.abbrev_offset < b.abbrev_offset;
7924 }
7925
7926 /* Efficiently read all the type units.
7927 This does the bulk of the work for build_type_psymtabs.
7928
7929 The efficiency is because we sort TUs by the abbrev table they use and
7930 only read each abbrev table once. In one program there are 200K TUs
7931 sharing 8K abbrev tables.
7932
7933 The main purpose of this function is to support building the
7934 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7935 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7936 can collapse the search space by grouping them by stmt_list.
7937 The savings can be significant, in the same program from above the 200K TUs
7938 share 8K stmt_list tables.
7939
7940 FUNC is expected to call get_type_unit_group, which will create the
7941 struct type_unit_group if necessary and add it to
7942 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7943
7944 static void
7945 build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
7946 {
7947 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7948 abbrev_table_up abbrev_table;
7949 sect_offset abbrev_offset;
7950
7951 /* It's up to the caller to not call us multiple times. */
7952 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7953
7954 if (per_objfile->per_bfd->all_type_units.empty ())
7955 return;
7956
7957 /* TUs typically share abbrev tables, and there can be way more TUs than
7958 abbrev tables. Sort by abbrev table to reduce the number of times we
7959 read each abbrev table in.
7960 Alternatives are to punt or to maintain a cache of abbrev tables.
7961 This is simpler and efficient enough for now.
7962
7963 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7964 symtab to use). Typically TUs with the same abbrev offset have the same
7965 stmt_list value too so in practice this should work well.
7966
7967 The basic algorithm here is:
7968
7969 sort TUs by abbrev table
7970 for each TU with same abbrev table:
7971 read abbrev table if first user
7972 read TU top level DIE
7973 [IWBN if DWO skeletons had DW_AT_stmt_list]
7974 call FUNC */
7975
7976 dwarf_read_debug_printf ("Building type unit groups ...");
7977
7978 /* Sort in a separate table to maintain the order of all_type_units
7979 for .gdb_index: TU indices directly index all_type_units. */
7980 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7981 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
7982
7983 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
7984 sorted_by_abbrev.emplace_back
7985 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
7986 sig_type->per_cu.sect_off));
7987
7988 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7989 sort_tu_by_abbrev_offset);
7990
7991 abbrev_offset = (sect_offset) ~(unsigned) 0;
7992
7993 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7994 {
7995 /* Switch to the next abbrev table if necessary. */
7996 if (abbrev_table == NULL
7997 || tu.abbrev_offset != abbrev_offset)
7998 {
7999 abbrev_offset = tu.abbrev_offset;
8000 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
8001 abbrev_table =
8002 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
8003 ++tu_stats->nr_uniq_abbrev_tables;
8004 }
8005
8006 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
8007 abbrev_table.get (), nullptr, false);
8008 if (!reader.dummy_p)
8009 build_type_psymtabs_reader (&reader, reader.info_ptr,
8010 reader.comp_unit_die);
8011 }
8012 }
8013
8014 /* Print collected type unit statistics. */
8015
8016 static void
8017 print_tu_stats (dwarf2_per_objfile *per_objfile)
8018 {
8019 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
8020
8021 dwarf_read_debug_printf ("Type unit statistics:");
8022 dwarf_read_debug_printf (" %zu TUs",
8023 per_objfile->per_bfd->all_type_units.size ());
8024 dwarf_read_debug_printf (" %d uniq abbrev tables",
8025 tu_stats->nr_uniq_abbrev_tables);
8026 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
8027 tu_stats->nr_symtabs);
8028 dwarf_read_debug_printf (" %d symtab sharers",
8029 tu_stats->nr_symtab_sharers);
8030 dwarf_read_debug_printf (" %d type units without a stmt_list",
8031 tu_stats->nr_stmt_less_type_units);
8032 dwarf_read_debug_printf (" %d all_type_units reallocs",
8033 tu_stats->nr_all_type_units_reallocs);
8034 }
8035
8036 /* Traversal function for build_type_psymtabs. */
8037
8038 static int
8039 build_type_psymtab_dependencies (void **slot, void *info)
8040 {
8041 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
8042 struct objfile *objfile = per_objfile->objfile;
8043 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8044 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8045 dwarf2_psymtab *pst = per_cu->v.psymtab;
8046 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
8047 int i;
8048
8049 gdb_assert (len > 0);
8050 gdb_assert (per_cu->type_unit_group_p ());
8051
8052 pst->number_of_dependencies = len;
8053 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
8054 for (i = 0; i < len; ++i)
8055 {
8056 struct signatured_type *iter = tu_group->tus->at (i);
8057 gdb_assert (iter->per_cu.is_debug_types);
8058 pst->dependencies[i] = iter->per_cu.v.psymtab;
8059 iter->type_unit_group = tu_group;
8060 }
8061
8062 delete tu_group->tus;
8063 tu_group->tus = nullptr;
8064
8065 return 1;
8066 }
8067
8068 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8069 Build partial symbol tables for the .debug_types comp-units. */
8070
8071 static void
8072 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
8073 {
8074 if (! create_all_type_units (per_objfile))
8075 return;
8076
8077 build_type_psymtabs_1 (per_objfile);
8078 }
8079
8080 /* Traversal function for process_skeletonless_type_unit.
8081 Read a TU in a DWO file and build partial symbols for it. */
8082
8083 static int
8084 process_skeletonless_type_unit (void **slot, void *info)
8085 {
8086 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8087 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
8088 struct signatured_type find_entry, *entry;
8089
8090 /* If this TU doesn't exist in the global table, add it and read it in. */
8091
8092 if (per_objfile->per_bfd->signatured_types == NULL)
8093 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
8094
8095 find_entry.signature = dwo_unit->signature;
8096 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
8097 &find_entry, INSERT);
8098 /* If we've already seen this type there's nothing to do. What's happening
8099 is we're doing our own version of comdat-folding here. */
8100 if (*slot != NULL)
8101 return 1;
8102
8103 /* This does the job that create_all_type_units would have done for
8104 this TU. */
8105 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
8106 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
8107 *slot = entry;
8108
8109 /* This does the job that build_type_psymtabs_1 would have done. */
8110 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
8111 if (!reader.dummy_p)
8112 build_type_psymtabs_reader (&reader, reader.info_ptr,
8113 reader.comp_unit_die);
8114
8115 return 1;
8116 }
8117
8118 /* Traversal function for process_skeletonless_type_units. */
8119
8120 static int
8121 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8122 {
8123 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8124
8125 if (dwo_file->tus != NULL)
8126 htab_traverse_noresize (dwo_file->tus.get (),
8127 process_skeletonless_type_unit, info);
8128
8129 return 1;
8130 }
8131
8132 /* Scan all TUs of DWO files, verifying we've processed them.
8133 This is needed in case a TU was emitted without its skeleton.
8134 Note: This can't be done until we know what all the DWO files are. */
8135
8136 static void
8137 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
8138 {
8139 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8140 if (get_dwp_file (per_objfile) == NULL
8141 && per_objfile->per_bfd->dwo_files != NULL)
8142 {
8143 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
8144 process_dwo_file_for_skeletonless_type_units,
8145 per_objfile);
8146 }
8147 }
8148
8149 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8150
8151 static void
8152 set_partial_user (dwarf2_per_objfile *per_objfile)
8153 {
8154 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
8155 {
8156 dwarf2_psymtab *pst = per_cu->v.psymtab;
8157
8158 if (pst == NULL)
8159 continue;
8160
8161 for (int j = 0; j < pst->number_of_dependencies; ++j)
8162 {
8163 /* Set the 'user' field only if it is not already set. */
8164 if (pst->dependencies[j]->user == NULL)
8165 pst->dependencies[j]->user = pst;
8166 }
8167 }
8168 }
8169
8170 /* Build the partial symbol table by doing a quick pass through the
8171 .debug_info and .debug_abbrev sections. */
8172
8173 static void
8174 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
8175 {
8176 struct objfile *objfile = per_objfile->objfile;
8177
8178 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
8179 objfile_name (objfile));
8180
8181 scoped_restore restore_reading_psyms
8182 = make_scoped_restore (&per_objfile->per_bfd->reading_partial_symbols,
8183 true);
8184
8185 per_objfile->per_bfd->info.read (objfile);
8186
8187 /* Any cached compilation units will be linked by the per-objfile
8188 read_in_chain. Make sure to free them when we're done. */
8189 free_cached_comp_units freer (per_objfile);
8190
8191 build_type_psymtabs (per_objfile);
8192
8193 create_all_comp_units (per_objfile);
8194
8195 /* Create a temporary address map on a temporary obstack. We later
8196 copy this to the final obstack. */
8197 auto_obstack temp_obstack;
8198
8199 scoped_restore save_psymtabs_addrmap
8200 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8201 addrmap_create_mutable (&temp_obstack));
8202
8203 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
8204 {
8205 if (per_cu->v.psymtab != NULL)
8206 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8207 continue;
8208 process_psymtab_comp_unit (per_cu, per_objfile, false,
8209 language_minimal);
8210 }
8211
8212 /* This has to wait until we read the CUs, we need the list of DWOs. */
8213 process_skeletonless_type_units (per_objfile);
8214
8215 /* Now that all TUs have been processed we can fill in the dependencies. */
8216 if (per_objfile->per_bfd->type_unit_groups != NULL)
8217 {
8218 htab_traverse_noresize (per_objfile->per_bfd->type_unit_groups.get (),
8219 build_type_psymtab_dependencies, per_objfile);
8220 }
8221
8222 if (dwarf_read_debug > 0)
8223 print_tu_stats (per_objfile);
8224
8225 set_partial_user (per_objfile);
8226
8227 objfile->partial_symtabs->psymtabs_addrmap
8228 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8229 objfile->partial_symtabs->obstack ());
8230 /* At this point we want to keep the address map. */
8231 save_psymtabs_addrmap.release ();
8232
8233 dwarf_read_debug_printf ("Done building psymtabs of %s",
8234 objfile_name (objfile));
8235 }
8236
8237 /* Load the partial DIEs for a secondary CU into memory.
8238 This is also used when rereading a primary CU with load_all_dies. */
8239
8240 static void
8241 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
8242 dwarf2_per_objfile *per_objfile,
8243 dwarf2_cu *existing_cu)
8244 {
8245 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
8246
8247 if (!reader.dummy_p)
8248 {
8249 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8250 language_minimal);
8251
8252 /* Check if comp unit has_children.
8253 If so, read the rest of the partial symbols from this comp unit.
8254 If not, there's no more debug_info for this comp unit. */
8255 if (reader.comp_unit_die->has_children)
8256 load_partial_dies (&reader, reader.info_ptr, 0);
8257
8258 reader.keep ();
8259 }
8260 }
8261
8262 static void
8263 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
8264 struct dwarf2_section_info *section,
8265 struct dwarf2_section_info *abbrev_section,
8266 unsigned int is_dwz)
8267 {
8268 const gdb_byte *info_ptr;
8269 struct objfile *objfile = per_objfile->objfile;
8270
8271 dwarf_read_debug_printf ("Reading %s for %s",
8272 section->get_name (),
8273 section->get_file_name ());
8274
8275 section->read (objfile);
8276
8277 info_ptr = section->buffer;
8278
8279 while (info_ptr < section->buffer + section->size)
8280 {
8281 struct dwarf2_per_cu_data *this_cu;
8282
8283 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8284
8285 comp_unit_head cu_header;
8286 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
8287 abbrev_section, info_ptr,
8288 rcuh_kind::COMPILE);
8289
8290 /* Save the compilation unit for later lookup. */
8291 if (cu_header.unit_type != DW_UT_type)
8292 this_cu = per_objfile->per_bfd->allocate_per_cu ();
8293 else
8294 {
8295 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
8296 sig_type->signature = cu_header.signature;
8297 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8298 this_cu = &sig_type->per_cu;
8299 }
8300 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8301 this_cu->sect_off = sect_off;
8302 this_cu->length = cu_header.length + cu_header.initial_length_size;
8303 this_cu->is_dwz = is_dwz;
8304 this_cu->section = section;
8305
8306 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8307
8308 info_ptr = info_ptr + this_cu->length;
8309 }
8310 }
8311
8312 /* Create a list of all compilation units in OBJFILE.
8313 This is only done for -readnow and building partial symtabs. */
8314
8315 static void
8316 create_all_comp_units (dwarf2_per_objfile *per_objfile)
8317 {
8318 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8319 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8320 &per_objfile->per_bfd->abbrev, 0);
8321
8322 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8323 if (dwz != NULL)
8324 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
8325 }
8326
8327 /* Process all loaded DIEs for compilation unit CU, starting at
8328 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8329 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8330 DW_AT_ranges). See the comments of add_partial_subprogram on how
8331 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8332
8333 static void
8334 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8335 CORE_ADDR *highpc, int set_addrmap,
8336 struct dwarf2_cu *cu)
8337 {
8338 struct partial_die_info *pdi;
8339
8340 /* Now, march along the PDI's, descending into ones which have
8341 interesting children but skipping the children of the other ones,
8342 until we reach the end of the compilation unit. */
8343
8344 pdi = first_die;
8345
8346 while (pdi != NULL)
8347 {
8348 pdi->fixup (cu);
8349
8350 /* Anonymous namespaces or modules have no name but have interesting
8351 children, so we need to look at them. Ditto for anonymous
8352 enums. */
8353
8354 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
8355 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8356 || pdi->tag == DW_TAG_imported_unit
8357 || pdi->tag == DW_TAG_inlined_subroutine)
8358 {
8359 switch (pdi->tag)
8360 {
8361 case DW_TAG_subprogram:
8362 case DW_TAG_inlined_subroutine:
8363 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8364 if (cu->language == language_cplus)
8365 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8366 set_addrmap, cu);
8367 break;
8368 case DW_TAG_constant:
8369 case DW_TAG_variable:
8370 case DW_TAG_typedef:
8371 case DW_TAG_union_type:
8372 if (!pdi->is_declaration
8373 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8374 {
8375 add_partial_symbol (pdi, cu);
8376 }
8377 break;
8378 case DW_TAG_class_type:
8379 case DW_TAG_interface_type:
8380 case DW_TAG_structure_type:
8381 if (!pdi->is_declaration)
8382 {
8383 add_partial_symbol (pdi, cu);
8384 }
8385 if ((cu->language == language_rust
8386 || cu->language == language_cplus) && pdi->has_children)
8387 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8388 set_addrmap, cu);
8389 break;
8390 case DW_TAG_enumeration_type:
8391 if (!pdi->is_declaration)
8392 add_partial_enumeration (pdi, cu);
8393 break;
8394 case DW_TAG_base_type:
8395 case DW_TAG_subrange_type:
8396 /* File scope base type definitions are added to the partial
8397 symbol table. */
8398 add_partial_symbol (pdi, cu);
8399 break;
8400 case DW_TAG_namespace:
8401 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8402 break;
8403 case DW_TAG_module:
8404 if (!pdi->is_declaration)
8405 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8406 break;
8407 case DW_TAG_imported_unit:
8408 {
8409 struct dwarf2_per_cu_data *per_cu;
8410
8411 /* For now we don't handle imported units in type units. */
8412 if (cu->per_cu->is_debug_types)
8413 {
8414 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8415 " supported in type units [in module %s]"),
8416 objfile_name (cu->per_objfile->objfile));
8417 }
8418
8419 per_cu = dwarf2_find_containing_comp_unit
8420 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8421
8422 /* Go read the partial unit, if needed. */
8423 if (per_cu->v.psymtab == NULL)
8424 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8425 cu->language);
8426
8427 cu->per_cu->imported_symtabs_push (per_cu);
8428 }
8429 break;
8430 case DW_TAG_imported_declaration:
8431 add_partial_symbol (pdi, cu);
8432 break;
8433 default:
8434 break;
8435 }
8436 }
8437
8438 /* If the die has a sibling, skip to the sibling. */
8439
8440 pdi = pdi->die_sibling;
8441 }
8442 }
8443
8444 /* Functions used to compute the fully scoped name of a partial DIE.
8445
8446 Normally, this is simple. For C++, the parent DIE's fully scoped
8447 name is concatenated with "::" and the partial DIE's name.
8448 Enumerators are an exception; they use the scope of their parent
8449 enumeration type, i.e. the name of the enumeration type is not
8450 prepended to the enumerator.
8451
8452 There are two complexities. One is DW_AT_specification; in this
8453 case "parent" means the parent of the target of the specification,
8454 instead of the direct parent of the DIE. The other is compilers
8455 which do not emit DW_TAG_namespace; in this case we try to guess
8456 the fully qualified name of structure types from their members'
8457 linkage names. This must be done using the DIE's children rather
8458 than the children of any DW_AT_specification target. We only need
8459 to do this for structures at the top level, i.e. if the target of
8460 any DW_AT_specification (if any; otherwise the DIE itself) does not
8461 have a parent. */
8462
8463 /* Compute the scope prefix associated with PDI's parent, in
8464 compilation unit CU. The result will be allocated on CU's
8465 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8466 field. NULL is returned if no prefix is necessary. */
8467 static const char *
8468 partial_die_parent_scope (struct partial_die_info *pdi,
8469 struct dwarf2_cu *cu)
8470 {
8471 const char *grandparent_scope;
8472 struct partial_die_info *parent, *real_pdi;
8473
8474 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8475 then this means the parent of the specification DIE. */
8476
8477 real_pdi = pdi;
8478 while (real_pdi->has_specification)
8479 {
8480 auto res = find_partial_die (real_pdi->spec_offset,
8481 real_pdi->spec_is_dwz, cu);
8482 real_pdi = res.pdi;
8483 cu = res.cu;
8484 }
8485
8486 parent = real_pdi->die_parent;
8487 if (parent == NULL)
8488 return NULL;
8489
8490 if (parent->scope_set)
8491 return parent->scope;
8492
8493 parent->fixup (cu);
8494
8495 grandparent_scope = partial_die_parent_scope (parent, cu);
8496
8497 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8498 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8499 Work around this problem here. */
8500 if (cu->language == language_cplus
8501 && parent->tag == DW_TAG_namespace
8502 && strcmp (parent->name (cu), "::") == 0
8503 && grandparent_scope == NULL)
8504 {
8505 parent->scope = NULL;
8506 parent->scope_set = 1;
8507 return NULL;
8508 }
8509
8510 /* Nested subroutines in Fortran get a prefix. */
8511 if (pdi->tag == DW_TAG_enumerator)
8512 /* Enumerators should not get the name of the enumeration as a prefix. */
8513 parent->scope = grandparent_scope;
8514 else if (parent->tag == DW_TAG_namespace
8515 || parent->tag == DW_TAG_module
8516 || parent->tag == DW_TAG_structure_type
8517 || parent->tag == DW_TAG_class_type
8518 || parent->tag == DW_TAG_interface_type
8519 || parent->tag == DW_TAG_union_type
8520 || parent->tag == DW_TAG_enumeration_type
8521 || (cu->language == language_fortran
8522 && parent->tag == DW_TAG_subprogram
8523 && pdi->tag == DW_TAG_subprogram))
8524 {
8525 if (grandparent_scope == NULL)
8526 parent->scope = parent->name (cu);
8527 else
8528 parent->scope = typename_concat (&cu->comp_unit_obstack,
8529 grandparent_scope,
8530 parent->name (cu), 0, cu);
8531 }
8532 else
8533 {
8534 /* FIXME drow/2004-04-01: What should we be doing with
8535 function-local names? For partial symbols, we should probably be
8536 ignoring them. */
8537 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8538 dwarf_tag_name (parent->tag),
8539 sect_offset_str (pdi->sect_off));
8540 parent->scope = grandparent_scope;
8541 }
8542
8543 parent->scope_set = 1;
8544 return parent->scope;
8545 }
8546
8547 /* Return the fully scoped name associated with PDI, from compilation unit
8548 CU. The result will be allocated with malloc. */
8549
8550 static gdb::unique_xmalloc_ptr<char>
8551 partial_die_full_name (struct partial_die_info *pdi,
8552 struct dwarf2_cu *cu)
8553 {
8554 const char *parent_scope;
8555
8556 /* If this is a template instantiation, we can not work out the
8557 template arguments from partial DIEs. So, unfortunately, we have
8558 to go through the full DIEs. At least any work we do building
8559 types here will be reused if full symbols are loaded later. */
8560 if (pdi->has_template_arguments)
8561 {
8562 pdi->fixup (cu);
8563
8564 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
8565 {
8566 struct die_info *die;
8567 struct attribute attr;
8568 struct dwarf2_cu *ref_cu = cu;
8569
8570 /* DW_FORM_ref_addr is using section offset. */
8571 attr.name = (enum dwarf_attribute) 0;
8572 attr.form = DW_FORM_ref_addr;
8573 attr.u.unsnd = to_underlying (pdi->sect_off);
8574 die = follow_die_ref (NULL, &attr, &ref_cu);
8575
8576 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8577 }
8578 }
8579
8580 parent_scope = partial_die_parent_scope (pdi, cu);
8581 if (parent_scope == NULL)
8582 return NULL;
8583 else
8584 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8585 pdi->name (cu),
8586 0, cu));
8587 }
8588
8589 static void
8590 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8591 {
8592 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8593 struct objfile *objfile = per_objfile->objfile;
8594 struct gdbarch *gdbarch = objfile->arch ();
8595 CORE_ADDR addr = 0;
8596 const char *actual_name = NULL;
8597 CORE_ADDR baseaddr;
8598
8599 baseaddr = objfile->text_section_offset ();
8600
8601 gdb::unique_xmalloc_ptr<char> built_actual_name
8602 = partial_die_full_name (pdi, cu);
8603 if (built_actual_name != NULL)
8604 actual_name = built_actual_name.get ();
8605
8606 if (actual_name == NULL)
8607 actual_name = pdi->name (cu);
8608
8609 partial_symbol psymbol;
8610 memset (&psymbol, 0, sizeof (psymbol));
8611 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8612 psymbol.ginfo.set_section_index (-1);
8613
8614 /* The code below indicates that the psymbol should be installed by
8615 setting this. */
8616 gdb::optional<psymbol_placement> where;
8617
8618 switch (pdi->tag)
8619 {
8620 case DW_TAG_inlined_subroutine:
8621 case DW_TAG_subprogram:
8622 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8623 - baseaddr);
8624 if (pdi->is_external
8625 || cu->language == language_ada
8626 || (cu->language == language_fortran
8627 && pdi->die_parent != NULL
8628 && pdi->die_parent->tag == DW_TAG_subprogram))
8629 {
8630 /* Normally, only "external" DIEs are part of the global scope.
8631 But in Ada and Fortran, we want to be able to access nested
8632 procedures globally. So all Ada and Fortran subprograms are
8633 stored in the global scope. */
8634 where = psymbol_placement::GLOBAL;
8635 }
8636 else
8637 where = psymbol_placement::STATIC;
8638
8639 psymbol.domain = VAR_DOMAIN;
8640 psymbol.aclass = LOC_BLOCK;
8641 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8642 psymbol.ginfo.value.address = addr;
8643
8644 if (pdi->main_subprogram && actual_name != NULL)
8645 set_objfile_main_name (objfile, actual_name, cu->language);
8646 break;
8647 case DW_TAG_constant:
8648 psymbol.domain = VAR_DOMAIN;
8649 psymbol.aclass = LOC_STATIC;
8650 where = (pdi->is_external
8651 ? psymbol_placement::GLOBAL
8652 : psymbol_placement::STATIC);
8653 break;
8654 case DW_TAG_variable:
8655 if (pdi->d.locdesc)
8656 addr = decode_locdesc (pdi->d.locdesc, cu);
8657
8658 if (pdi->d.locdesc
8659 && addr == 0
8660 && !per_objfile->per_bfd->has_section_at_zero)
8661 {
8662 /* A global or static variable may also have been stripped
8663 out by the linker if unused, in which case its address
8664 will be nullified; do not add such variables into partial
8665 symbol table then. */
8666 }
8667 else if (pdi->is_external)
8668 {
8669 /* Global Variable.
8670 Don't enter into the minimal symbol tables as there is
8671 a minimal symbol table entry from the ELF symbols already.
8672 Enter into partial symbol table if it has a location
8673 descriptor or a type.
8674 If the location descriptor is missing, new_symbol will create
8675 a LOC_UNRESOLVED symbol, the address of the variable will then
8676 be determined from the minimal symbol table whenever the variable
8677 is referenced.
8678 The address for the partial symbol table entry is not
8679 used by GDB, but it comes in handy for debugging partial symbol
8680 table building. */
8681
8682 if (pdi->d.locdesc || pdi->has_type)
8683 {
8684 psymbol.domain = VAR_DOMAIN;
8685 psymbol.aclass = LOC_STATIC;
8686 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8687 psymbol.ginfo.value.address = addr;
8688 where = psymbol_placement::GLOBAL;
8689 }
8690 }
8691 else
8692 {
8693 int has_loc = pdi->d.locdesc != NULL;
8694
8695 /* Static Variable. Skip symbols whose value we cannot know (those
8696 without location descriptors or constant values). */
8697 if (!has_loc && !pdi->has_const_value)
8698 return;
8699
8700 psymbol.domain = VAR_DOMAIN;
8701 psymbol.aclass = LOC_STATIC;
8702 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8703 if (has_loc)
8704 psymbol.ginfo.value.address = addr;
8705 where = psymbol_placement::STATIC;
8706 }
8707 break;
8708 case DW_TAG_array_type:
8709 case DW_TAG_typedef:
8710 case DW_TAG_base_type:
8711 case DW_TAG_subrange_type:
8712 psymbol.domain = VAR_DOMAIN;
8713 psymbol.aclass = LOC_TYPEDEF;
8714 where = psymbol_placement::STATIC;
8715 break;
8716 case DW_TAG_imported_declaration:
8717 case DW_TAG_namespace:
8718 psymbol.domain = VAR_DOMAIN;
8719 psymbol.aclass = LOC_TYPEDEF;
8720 where = psymbol_placement::GLOBAL;
8721 break;
8722 case DW_TAG_module:
8723 /* With Fortran 77 there might be a "BLOCK DATA" module
8724 available without any name. If so, we skip the module as it
8725 doesn't bring any value. */
8726 if (actual_name != nullptr)
8727 {
8728 psymbol.domain = MODULE_DOMAIN;
8729 psymbol.aclass = LOC_TYPEDEF;
8730 where = psymbol_placement::GLOBAL;
8731 }
8732 break;
8733 case DW_TAG_class_type:
8734 case DW_TAG_interface_type:
8735 case DW_TAG_structure_type:
8736 case DW_TAG_union_type:
8737 case DW_TAG_enumeration_type:
8738 /* Skip external references. The DWARF standard says in the section
8739 about "Structure, Union, and Class Type Entries": "An incomplete
8740 structure, union or class type is represented by a structure,
8741 union or class entry that does not have a byte size attribute
8742 and that has a DW_AT_declaration attribute." */
8743 if (!pdi->has_byte_size && pdi->is_declaration)
8744 return;
8745
8746 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8747 static vs. global. */
8748 psymbol.domain = STRUCT_DOMAIN;
8749 psymbol.aclass = LOC_TYPEDEF;
8750 where = (cu->language == language_cplus
8751 ? psymbol_placement::GLOBAL
8752 : psymbol_placement::STATIC);
8753 break;
8754 case DW_TAG_enumerator:
8755 psymbol.domain = VAR_DOMAIN;
8756 psymbol.aclass = LOC_CONST;
8757 where = (cu->language == language_cplus
8758 ? psymbol_placement::GLOBAL
8759 : psymbol_placement::STATIC);
8760 break;
8761 default:
8762 break;
8763 }
8764
8765 if (where.has_value ())
8766 {
8767 if (built_actual_name != nullptr)
8768 actual_name = objfile->intern (actual_name);
8769 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8770 psymbol.ginfo.set_linkage_name (actual_name);
8771 else
8772 {
8773 psymbol.ginfo.set_demangled_name (actual_name,
8774 &objfile->objfile_obstack);
8775 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8776 }
8777 cu->per_cu->v.psymtab->add_psymbol (psymbol, *where, objfile);
8778 }
8779 }
8780
8781 /* Read a partial die corresponding to a namespace; also, add a symbol
8782 corresponding to that namespace to the symbol table. NAMESPACE is
8783 the name of the enclosing namespace. */
8784
8785 static void
8786 add_partial_namespace (struct partial_die_info *pdi,
8787 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8788 int set_addrmap, struct dwarf2_cu *cu)
8789 {
8790 /* Add a symbol for the namespace. */
8791
8792 add_partial_symbol (pdi, cu);
8793
8794 /* Now scan partial symbols in that namespace. */
8795
8796 if (pdi->has_children)
8797 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8798 }
8799
8800 /* Read a partial die corresponding to a Fortran module. */
8801
8802 static void
8803 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8804 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8805 {
8806 /* Add a symbol for the namespace. */
8807
8808 add_partial_symbol (pdi, cu);
8809
8810 /* Now scan partial symbols in that module. */
8811
8812 if (pdi->has_children)
8813 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8814 }
8815
8816 /* Read a partial die corresponding to a subprogram or an inlined
8817 subprogram and create a partial symbol for that subprogram.
8818 When the CU language allows it, this routine also defines a partial
8819 symbol for each nested subprogram that this subprogram contains.
8820 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8821 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8822
8823 PDI may also be a lexical block, in which case we simply search
8824 recursively for subprograms defined inside that lexical block.
8825 Again, this is only performed when the CU language allows this
8826 type of definitions. */
8827
8828 static void
8829 add_partial_subprogram (struct partial_die_info *pdi,
8830 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8831 int set_addrmap, struct dwarf2_cu *cu)
8832 {
8833 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8834 {
8835 if (pdi->has_pc_info)
8836 {
8837 if (pdi->lowpc < *lowpc)
8838 *lowpc = pdi->lowpc;
8839 if (pdi->highpc > *highpc)
8840 *highpc = pdi->highpc;
8841 if (set_addrmap)
8842 {
8843 struct objfile *objfile = cu->per_objfile->objfile;
8844 struct gdbarch *gdbarch = objfile->arch ();
8845 CORE_ADDR baseaddr;
8846 CORE_ADDR this_highpc;
8847 CORE_ADDR this_lowpc;
8848
8849 baseaddr = objfile->text_section_offset ();
8850 this_lowpc
8851 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8852 pdi->lowpc + baseaddr)
8853 - baseaddr);
8854 this_highpc
8855 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8856 pdi->highpc + baseaddr)
8857 - baseaddr);
8858 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8859 this_lowpc, this_highpc - 1,
8860 cu->per_cu->v.psymtab);
8861 }
8862 }
8863
8864 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8865 {
8866 if (!pdi->is_declaration)
8867 /* Ignore subprogram DIEs that do not have a name, they are
8868 illegal. Do not emit a complaint at this point, we will
8869 do so when we convert this psymtab into a symtab. */
8870 if (pdi->name (cu))
8871 add_partial_symbol (pdi, cu);
8872 }
8873 }
8874
8875 if (! pdi->has_children)
8876 return;
8877
8878 if (cu->language == language_ada || cu->language == language_fortran)
8879 {
8880 pdi = pdi->die_child;
8881 while (pdi != NULL)
8882 {
8883 pdi->fixup (cu);
8884 if (pdi->tag == DW_TAG_subprogram
8885 || pdi->tag == DW_TAG_inlined_subroutine
8886 || pdi->tag == DW_TAG_lexical_block)
8887 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8888 pdi = pdi->die_sibling;
8889 }
8890 }
8891 }
8892
8893 /* Read a partial die corresponding to an enumeration type. */
8894
8895 static void
8896 add_partial_enumeration (struct partial_die_info *enum_pdi,
8897 struct dwarf2_cu *cu)
8898 {
8899 struct partial_die_info *pdi;
8900
8901 if (enum_pdi->name (cu) != NULL)
8902 add_partial_symbol (enum_pdi, cu);
8903
8904 pdi = enum_pdi->die_child;
8905 while (pdi)
8906 {
8907 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8908 complaint (_("malformed enumerator DIE ignored"));
8909 else
8910 add_partial_symbol (pdi, cu);
8911 pdi = pdi->die_sibling;
8912 }
8913 }
8914
8915 /* Return the initial uleb128 in the die at INFO_PTR. */
8916
8917 static unsigned int
8918 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8919 {
8920 unsigned int bytes_read;
8921
8922 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8923 }
8924
8925 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8926 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8927
8928 Return the corresponding abbrev, or NULL if the number is zero (indicating
8929 an empty DIE). In either case *BYTES_READ will be set to the length of
8930 the initial number. */
8931
8932 static struct abbrev_info *
8933 peek_die_abbrev (const die_reader_specs &reader,
8934 const gdb_byte *info_ptr, unsigned int *bytes_read)
8935 {
8936 dwarf2_cu *cu = reader.cu;
8937 bfd *abfd = cu->per_objfile->objfile->obfd;
8938 unsigned int abbrev_number
8939 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8940
8941 if (abbrev_number == 0)
8942 return NULL;
8943
8944 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8945 if (!abbrev)
8946 {
8947 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8948 " at offset %s [in module %s]"),
8949 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8950 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8951 }
8952
8953 return abbrev;
8954 }
8955
8956 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8957 Returns a pointer to the end of a series of DIEs, terminated by an empty
8958 DIE. Any children of the skipped DIEs will also be skipped. */
8959
8960 static const gdb_byte *
8961 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8962 {
8963 while (1)
8964 {
8965 unsigned int bytes_read;
8966 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8967
8968 if (abbrev == NULL)
8969 return info_ptr + bytes_read;
8970 else
8971 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8972 }
8973 }
8974
8975 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8976 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8977 abbrev corresponding to that skipped uleb128 should be passed in
8978 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8979 children. */
8980
8981 static const gdb_byte *
8982 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8983 struct abbrev_info *abbrev)
8984 {
8985 unsigned int bytes_read;
8986 struct attribute attr;
8987 bfd *abfd = reader->abfd;
8988 struct dwarf2_cu *cu = reader->cu;
8989 const gdb_byte *buffer = reader->buffer;
8990 const gdb_byte *buffer_end = reader->buffer_end;
8991 unsigned int form, i;
8992
8993 for (i = 0; i < abbrev->num_attrs; i++)
8994 {
8995 /* The only abbrev we care about is DW_AT_sibling. */
8996 if (abbrev->attrs[i].name == DW_AT_sibling)
8997 {
8998 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8999 if (attr.form == DW_FORM_ref_addr)
9000 complaint (_("ignoring absolute DW_AT_sibling"));
9001 else
9002 {
9003 sect_offset off = attr.get_ref_die_offset ();
9004 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9005
9006 if (sibling_ptr < info_ptr)
9007 complaint (_("DW_AT_sibling points backwards"));
9008 else if (sibling_ptr > reader->buffer_end)
9009 reader->die_section->overflow_complaint ();
9010 else
9011 return sibling_ptr;
9012 }
9013 }
9014
9015 /* If it isn't DW_AT_sibling, skip this attribute. */
9016 form = abbrev->attrs[i].form;
9017 skip_attribute:
9018 switch (form)
9019 {
9020 case DW_FORM_ref_addr:
9021 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9022 and later it is offset sized. */
9023 if (cu->header.version == 2)
9024 info_ptr += cu->header.addr_size;
9025 else
9026 info_ptr += cu->header.offset_size;
9027 break;
9028 case DW_FORM_GNU_ref_alt:
9029 info_ptr += cu->header.offset_size;
9030 break;
9031 case DW_FORM_addr:
9032 info_ptr += cu->header.addr_size;
9033 break;
9034 case DW_FORM_data1:
9035 case DW_FORM_ref1:
9036 case DW_FORM_flag:
9037 case DW_FORM_strx1:
9038 info_ptr += 1;
9039 break;
9040 case DW_FORM_flag_present:
9041 case DW_FORM_implicit_const:
9042 break;
9043 case DW_FORM_data2:
9044 case DW_FORM_ref2:
9045 case DW_FORM_strx2:
9046 info_ptr += 2;
9047 break;
9048 case DW_FORM_strx3:
9049 info_ptr += 3;
9050 break;
9051 case DW_FORM_data4:
9052 case DW_FORM_ref4:
9053 case DW_FORM_strx4:
9054 info_ptr += 4;
9055 break;
9056 case DW_FORM_data8:
9057 case DW_FORM_ref8:
9058 case DW_FORM_ref_sig8:
9059 info_ptr += 8;
9060 break;
9061 case DW_FORM_data16:
9062 info_ptr += 16;
9063 break;
9064 case DW_FORM_string:
9065 read_direct_string (abfd, info_ptr, &bytes_read);
9066 info_ptr += bytes_read;
9067 break;
9068 case DW_FORM_sec_offset:
9069 case DW_FORM_strp:
9070 case DW_FORM_GNU_strp_alt:
9071 info_ptr += cu->header.offset_size;
9072 break;
9073 case DW_FORM_exprloc:
9074 case DW_FORM_block:
9075 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9076 info_ptr += bytes_read;
9077 break;
9078 case DW_FORM_block1:
9079 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9080 break;
9081 case DW_FORM_block2:
9082 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9083 break;
9084 case DW_FORM_block4:
9085 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9086 break;
9087 case DW_FORM_addrx:
9088 case DW_FORM_strx:
9089 case DW_FORM_sdata:
9090 case DW_FORM_udata:
9091 case DW_FORM_ref_udata:
9092 case DW_FORM_GNU_addr_index:
9093 case DW_FORM_GNU_str_index:
9094 case DW_FORM_rnglistx:
9095 case DW_FORM_loclistx:
9096 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9097 break;
9098 case DW_FORM_indirect:
9099 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9100 info_ptr += bytes_read;
9101 /* We need to continue parsing from here, so just go back to
9102 the top. */
9103 goto skip_attribute;
9104
9105 default:
9106 error (_("Dwarf Error: Cannot handle %s "
9107 "in DWARF reader [in module %s]"),
9108 dwarf_form_name (form),
9109 bfd_get_filename (abfd));
9110 }
9111 }
9112
9113 if (abbrev->has_children)
9114 return skip_children (reader, info_ptr);
9115 else
9116 return info_ptr;
9117 }
9118
9119 /* Locate ORIG_PDI's sibling.
9120 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9121
9122 static const gdb_byte *
9123 locate_pdi_sibling (const struct die_reader_specs *reader,
9124 struct partial_die_info *orig_pdi,
9125 const gdb_byte *info_ptr)
9126 {
9127 /* Do we know the sibling already? */
9128
9129 if (orig_pdi->sibling)
9130 return orig_pdi->sibling;
9131
9132 /* Are there any children to deal with? */
9133
9134 if (!orig_pdi->has_children)
9135 return info_ptr;
9136
9137 /* Skip the children the long way. */
9138
9139 return skip_children (reader, info_ptr);
9140 }
9141
9142 /* Expand this partial symbol table into a full symbol table. SELF is
9143 not NULL. */
9144
9145 void
9146 dwarf2_psymtab::read_symtab (struct objfile *objfile)
9147 {
9148 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9149
9150 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
9151
9152 /* If this psymtab is constructed from a debug-only objfile, the
9153 has_section_at_zero flag will not necessarily be correct. We
9154 can get the correct value for this flag by looking at the data
9155 associated with the (presumably stripped) associated objfile. */
9156 if (objfile->separate_debug_objfile_backlink)
9157 {
9158 dwarf2_per_objfile *per_objfile_backlink
9159 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9160
9161 per_objfile->per_bfd->has_section_at_zero
9162 = per_objfile_backlink->per_bfd->has_section_at_zero;
9163 }
9164
9165 expand_psymtab (objfile);
9166
9167 process_cu_includes (per_objfile);
9168 }
9169 \f
9170 /* Reading in full CUs. */
9171
9172 /* Add PER_CU to the queue. */
9173
9174 static void
9175 queue_comp_unit (dwarf2_per_cu_data *per_cu,
9176 dwarf2_per_objfile *per_objfile,
9177 enum language pretend_language)
9178 {
9179 per_cu->queued = 1;
9180
9181 gdb_assert (per_objfile->per_bfd->queue.has_value ());
9182 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
9183 }
9184
9185 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
9186
9187 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9188 dependency.
9189
9190 Return true if maybe_queue_comp_unit requires the caller to load the CU's
9191 DIEs, false otherwise.
9192
9193 Explanation: there is an invariant that if a CU is queued for expansion
9194 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
9195 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
9196 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
9197 are not yet loaded, the the caller must load the CU's DIEs to ensure the
9198 invariant is respected.
9199
9200 The caller is therefore not required to load the CU's DIEs (we return false)
9201 if:
9202
9203 - the CU is already expanded, and therefore does not get enqueued
9204 - the CU gets enqueued for expansion, but its DIEs are already loaded
9205
9206 Note that the caller should not use this function's return value as an
9207 indicator of whether the CU's DIEs are loaded right now, it should check
9208 that by calling `dwarf2_per_objfile::get_cu` instead. */
9209
9210 static int
9211 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9212 dwarf2_per_cu_data *per_cu,
9213 dwarf2_per_objfile *per_objfile,
9214 enum language pretend_language)
9215 {
9216 /* We may arrive here during partial symbol reading, if we need full
9217 DIEs to process an unusual case (e.g. template arguments). Do
9218 not queue PER_CU, just tell our caller to load its DIEs. */
9219 if (per_cu->per_bfd->reading_partial_symbols)
9220 {
9221 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9222
9223 if (cu == NULL || cu->dies == NULL)
9224 return 1;
9225 return 0;
9226 }
9227
9228 /* Mark the dependence relation so that we don't flush PER_CU
9229 too early. */
9230 if (dependent_cu != NULL)
9231 dwarf2_add_dependence (dependent_cu, per_cu);
9232
9233 /* If it's already on the queue, we have nothing to do. */
9234 if (per_cu->queued)
9235 {
9236 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
9237 loaded. */
9238 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
9239
9240 /* If the CU is queued for expansion, it should not already be
9241 expanded. */
9242 gdb_assert (!per_objfile->symtab_set_p (per_cu));
9243
9244 /* The DIEs are already loaded, the caller doesn't need to do it. */
9245 return 0;
9246 }
9247
9248 bool queued = false;
9249 if (!per_objfile->symtab_set_p (per_cu))
9250 {
9251 /* Add it to the queue. */
9252 queue_comp_unit (per_cu, per_objfile, pretend_language);
9253 queued = true;
9254 }
9255
9256 /* If the compilation unit is already loaded, just mark it as
9257 used. */
9258 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9259 if (cu != nullptr)
9260 cu->last_used = 0;
9261
9262 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
9263 and the DIEs are not already loaded. */
9264 return queued && cu == nullptr;
9265 }
9266
9267 /* Process the queue. */
9268
9269 static void
9270 process_queue (dwarf2_per_objfile *per_objfile)
9271 {
9272 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
9273 objfile_name (per_objfile->objfile));
9274
9275 /* The queue starts out with one item, but following a DIE reference
9276 may load a new CU, adding it to the end of the queue. */
9277 while (!per_objfile->per_bfd->queue->empty ())
9278 {
9279 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
9280 dwarf2_per_cu_data *per_cu = item.per_cu;
9281
9282 if (!per_objfile->symtab_set_p (per_cu))
9283 {
9284 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9285
9286 /* Skip dummy CUs. */
9287 if (cu != nullptr)
9288 {
9289 unsigned int debug_print_threshold;
9290 char buf[100];
9291
9292 if (per_cu->is_debug_types)
9293 {
9294 struct signatured_type *sig_type =
9295 (struct signatured_type *) per_cu;
9296
9297 sprintf (buf, "TU %s at offset %s",
9298 hex_string (sig_type->signature),
9299 sect_offset_str (per_cu->sect_off));
9300 /* There can be 100s of TUs.
9301 Only print them in verbose mode. */
9302 debug_print_threshold = 2;
9303 }
9304 else
9305 {
9306 sprintf (buf, "CU at offset %s",
9307 sect_offset_str (per_cu->sect_off));
9308 debug_print_threshold = 1;
9309 }
9310
9311 if (dwarf_read_debug >= debug_print_threshold)
9312 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
9313
9314 if (per_cu->is_debug_types)
9315 process_full_type_unit (cu, item.pretend_language);
9316 else
9317 process_full_comp_unit (cu, item.pretend_language);
9318
9319 if (dwarf_read_debug >= debug_print_threshold)
9320 dwarf_read_debug_printf ("Done expanding %s", buf);
9321 }
9322 }
9323
9324 per_cu->queued = 0;
9325 per_objfile->per_bfd->queue->pop ();
9326 }
9327
9328 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
9329 objfile_name (per_objfile->objfile));
9330 }
9331
9332 /* Read in full symbols for PST, and anything it depends on. */
9333
9334 void
9335 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9336 {
9337 gdb_assert (!readin_p (objfile));
9338
9339 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9340 free_cached_comp_units freer (per_objfile);
9341 expand_dependencies (objfile);
9342
9343 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9344 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9345 }
9346
9347 /* See psympriv.h. */
9348
9349 bool
9350 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9351 {
9352 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9353 return per_objfile->symtab_set_p (per_cu_data);
9354 }
9355
9356 /* See psympriv.h. */
9357
9358 compunit_symtab *
9359 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9360 {
9361 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9362 return per_objfile->get_symtab (per_cu_data);
9363 }
9364
9365 /* Trivial hash function for die_info: the hash value of a DIE
9366 is its offset in .debug_info for this objfile. */
9367
9368 static hashval_t
9369 die_hash (const void *item)
9370 {
9371 const struct die_info *die = (const struct die_info *) item;
9372
9373 return to_underlying (die->sect_off);
9374 }
9375
9376 /* Trivial comparison function for die_info structures: two DIEs
9377 are equal if they have the same offset. */
9378
9379 static int
9380 die_eq (const void *item_lhs, const void *item_rhs)
9381 {
9382 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9383 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9384
9385 return die_lhs->sect_off == die_rhs->sect_off;
9386 }
9387
9388 /* Load the DIEs associated with PER_CU into memory.
9389
9390 In some cases, the caller, while reading partial symbols, will need to load
9391 the full symbols for the CU for some reason. It will already have a
9392 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
9393 rather than creating a new one. */
9394
9395 static void
9396 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9397 dwarf2_per_objfile *per_objfile,
9398 dwarf2_cu *existing_cu,
9399 bool skip_partial,
9400 enum language pretend_language)
9401 {
9402 gdb_assert (! this_cu->is_debug_types);
9403
9404 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
9405 if (reader.dummy_p)
9406 return;
9407
9408 struct dwarf2_cu *cu = reader.cu;
9409 const gdb_byte *info_ptr = reader.info_ptr;
9410
9411 gdb_assert (cu->die_hash == NULL);
9412 cu->die_hash =
9413 htab_create_alloc_ex (cu->header.length / 12,
9414 die_hash,
9415 die_eq,
9416 NULL,
9417 &cu->comp_unit_obstack,
9418 hashtab_obstack_allocate,
9419 dummy_obstack_deallocate);
9420
9421 if (reader.comp_unit_die->has_children)
9422 reader.comp_unit_die->child
9423 = read_die_and_siblings (&reader, reader.info_ptr,
9424 &info_ptr, reader.comp_unit_die);
9425 cu->dies = reader.comp_unit_die;
9426 /* comp_unit_die is not stored in die_hash, no need. */
9427
9428 /* We try not to read any attributes in this function, because not
9429 all CUs needed for references have been loaded yet, and symbol
9430 table processing isn't initialized. But we have to set the CU language,
9431 or we won't be able to build types correctly.
9432 Similarly, if we do not read the producer, we can not apply
9433 producer-specific interpretation. */
9434 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9435
9436 reader.keep ();
9437 }
9438
9439 /* Add a DIE to the delayed physname list. */
9440
9441 static void
9442 add_to_method_list (struct type *type, int fnfield_index, int index,
9443 const char *name, struct die_info *die,
9444 struct dwarf2_cu *cu)
9445 {
9446 struct delayed_method_info mi;
9447 mi.type = type;
9448 mi.fnfield_index = fnfield_index;
9449 mi.index = index;
9450 mi.name = name;
9451 mi.die = die;
9452 cu->method_list.push_back (mi);
9453 }
9454
9455 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9456 "const" / "volatile". If so, decrements LEN by the length of the
9457 modifier and return true. Otherwise return false. */
9458
9459 template<size_t N>
9460 static bool
9461 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9462 {
9463 size_t mod_len = sizeof (mod) - 1;
9464 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9465 {
9466 len -= mod_len;
9467 return true;
9468 }
9469 return false;
9470 }
9471
9472 /* Compute the physnames of any methods on the CU's method list.
9473
9474 The computation of method physnames is delayed in order to avoid the
9475 (bad) condition that one of the method's formal parameters is of an as yet
9476 incomplete type. */
9477
9478 static void
9479 compute_delayed_physnames (struct dwarf2_cu *cu)
9480 {
9481 /* Only C++ delays computing physnames. */
9482 if (cu->method_list.empty ())
9483 return;
9484 gdb_assert (cu->language == language_cplus);
9485
9486 for (const delayed_method_info &mi : cu->method_list)
9487 {
9488 const char *physname;
9489 struct fn_fieldlist *fn_flp
9490 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9491 physname = dwarf2_physname (mi.name, mi.die, cu);
9492 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9493 = physname ? physname : "";
9494
9495 /* Since there's no tag to indicate whether a method is a
9496 const/volatile overload, extract that information out of the
9497 demangled name. */
9498 if (physname != NULL)
9499 {
9500 size_t len = strlen (physname);
9501
9502 while (1)
9503 {
9504 if (physname[len] == ')') /* shortcut */
9505 break;
9506 else if (check_modifier (physname, len, " const"))
9507 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9508 else if (check_modifier (physname, len, " volatile"))
9509 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9510 else
9511 break;
9512 }
9513 }
9514 }
9515
9516 /* The list is no longer needed. */
9517 cu->method_list.clear ();
9518 }
9519
9520 /* Go objects should be embedded in a DW_TAG_module DIE,
9521 and it's not clear if/how imported objects will appear.
9522 To keep Go support simple until that's worked out,
9523 go back through what we've read and create something usable.
9524 We could do this while processing each DIE, and feels kinda cleaner,
9525 but that way is more invasive.
9526 This is to, for example, allow the user to type "p var" or "b main"
9527 without having to specify the package name, and allow lookups
9528 of module.object to work in contexts that use the expression
9529 parser. */
9530
9531 static void
9532 fixup_go_packaging (struct dwarf2_cu *cu)
9533 {
9534 gdb::unique_xmalloc_ptr<char> package_name;
9535 struct pending *list;
9536 int i;
9537
9538 for (list = *cu->get_builder ()->get_global_symbols ();
9539 list != NULL;
9540 list = list->next)
9541 {
9542 for (i = 0; i < list->nsyms; ++i)
9543 {
9544 struct symbol *sym = list->symbol[i];
9545
9546 if (sym->language () == language_go
9547 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9548 {
9549 gdb::unique_xmalloc_ptr<char> this_package_name
9550 (go_symbol_package_name (sym));
9551
9552 if (this_package_name == NULL)
9553 continue;
9554 if (package_name == NULL)
9555 package_name = std::move (this_package_name);
9556 else
9557 {
9558 struct objfile *objfile = cu->per_objfile->objfile;
9559 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9560 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9561 (symbol_symtab (sym) != NULL
9562 ? symtab_to_filename_for_display
9563 (symbol_symtab (sym))
9564 : objfile_name (objfile)),
9565 this_package_name.get (), package_name.get ());
9566 }
9567 }
9568 }
9569 }
9570
9571 if (package_name != NULL)
9572 {
9573 struct objfile *objfile = cu->per_objfile->objfile;
9574 const char *saved_package_name = objfile->intern (package_name.get ());
9575 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9576 saved_package_name);
9577 struct symbol *sym;
9578
9579 sym = new (&objfile->objfile_obstack) symbol;
9580 sym->set_language (language_go, &objfile->objfile_obstack);
9581 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9582 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9583 e.g., "main" finds the "main" module and not C's main(). */
9584 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9585 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9586 SYMBOL_TYPE (sym) = type;
9587
9588 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9589 }
9590 }
9591
9592 /* Allocate a fully-qualified name consisting of the two parts on the
9593 obstack. */
9594
9595 static const char *
9596 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9597 {
9598 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9599 }
9600
9601 /* A helper that allocates a variant part to attach to a Rust enum
9602 type. OBSTACK is where the results should be allocated. TYPE is
9603 the type we're processing. DISCRIMINANT_INDEX is the index of the
9604 discriminant. It must be the index of one of the fields of TYPE,
9605 or -1 to mean there is no discriminant (univariant enum).
9606 DEFAULT_INDEX is the index of the default field; or -1 if there is
9607 no default. RANGES is indexed by "effective" field number (the
9608 field index, but omitting the discriminant and default fields) and
9609 must hold the discriminant values used by the variants. Note that
9610 RANGES must have a lifetime at least as long as OBSTACK -- either
9611 already allocated on it, or static. */
9612
9613 static void
9614 alloc_rust_variant (struct obstack *obstack, struct type *type,
9615 int discriminant_index, int default_index,
9616 gdb::array_view<discriminant_range> ranges)
9617 {
9618 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9619 gdb_assert (discriminant_index == -1
9620 || (discriminant_index >= 0
9621 && discriminant_index < type->num_fields ()));
9622 gdb_assert (default_index == -1
9623 || (default_index >= 0 && default_index < type->num_fields ()));
9624
9625 /* We have one variant for each non-discriminant field. */
9626 int n_variants = type->num_fields ();
9627 if (discriminant_index != -1)
9628 --n_variants;
9629
9630 variant *variants = new (obstack) variant[n_variants];
9631 int var_idx = 0;
9632 int range_idx = 0;
9633 for (int i = 0; i < type->num_fields (); ++i)
9634 {
9635 if (i == discriminant_index)
9636 continue;
9637
9638 variants[var_idx].first_field = i;
9639 variants[var_idx].last_field = i + 1;
9640
9641 /* The default field does not need a range, but other fields do.
9642 We skipped the discriminant above. */
9643 if (i != default_index)
9644 {
9645 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9646 ++range_idx;
9647 }
9648
9649 ++var_idx;
9650 }
9651
9652 gdb_assert (range_idx == ranges.size ());
9653 gdb_assert (var_idx == n_variants);
9654
9655 variant_part *part = new (obstack) variant_part;
9656 part->discriminant_index = discriminant_index;
9657 /* If there is no discriminant, then whether it is signed is of no
9658 consequence. */
9659 part->is_unsigned
9660 = (discriminant_index == -1
9661 ? false
9662 : type->field (discriminant_index).type ()->is_unsigned ());
9663 part->variants = gdb::array_view<variant> (variants, n_variants);
9664
9665 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9666 gdb::array_view<variant_part> *prop_value
9667 = new (storage) gdb::array_view<variant_part> (part, 1);
9668
9669 struct dynamic_prop prop;
9670 prop.set_variant_parts (prop_value);
9671
9672 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9673 }
9674
9675 /* Some versions of rustc emitted enums in an unusual way.
9676
9677 Ordinary enums were emitted as unions. The first element of each
9678 structure in the union was named "RUST$ENUM$DISR". This element
9679 held the discriminant.
9680
9681 These versions of Rust also implemented the "non-zero"
9682 optimization. When the enum had two values, and one is empty and
9683 the other holds a pointer that cannot be zero, the pointer is used
9684 as the discriminant, with a zero value meaning the empty variant.
9685 Here, the union's first member is of the form
9686 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9687 where the fieldnos are the indices of the fields that should be
9688 traversed in order to find the field (which may be several fields deep)
9689 and the variantname is the name of the variant of the case when the
9690 field is zero.
9691
9692 This function recognizes whether TYPE is of one of these forms,
9693 and, if so, smashes it to be a variant type. */
9694
9695 static void
9696 quirk_rust_enum (struct type *type, struct objfile *objfile)
9697 {
9698 gdb_assert (type->code () == TYPE_CODE_UNION);
9699
9700 /* We don't need to deal with empty enums. */
9701 if (type->num_fields () == 0)
9702 return;
9703
9704 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9705 if (type->num_fields () == 1
9706 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9707 {
9708 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9709
9710 /* Decode the field name to find the offset of the
9711 discriminant. */
9712 ULONGEST bit_offset = 0;
9713 struct type *field_type = type->field (0).type ();
9714 while (name[0] >= '0' && name[0] <= '9')
9715 {
9716 char *tail;
9717 unsigned long index = strtoul (name, &tail, 10);
9718 name = tail;
9719 if (*name != '$'
9720 || index >= field_type->num_fields ()
9721 || (TYPE_FIELD_LOC_KIND (field_type, index)
9722 != FIELD_LOC_KIND_BITPOS))
9723 {
9724 complaint (_("Could not parse Rust enum encoding string \"%s\""
9725 "[in module %s]"),
9726 TYPE_FIELD_NAME (type, 0),
9727 objfile_name (objfile));
9728 return;
9729 }
9730 ++name;
9731
9732 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9733 field_type = field_type->field (index).type ();
9734 }
9735
9736 /* Smash this type to be a structure type. We have to do this
9737 because the type has already been recorded. */
9738 type->set_code (TYPE_CODE_STRUCT);
9739 type->set_num_fields (3);
9740 /* Save the field we care about. */
9741 struct field saved_field = type->field (0);
9742 type->set_fields
9743 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9744
9745 /* Put the discriminant at index 0. */
9746 type->field (0).set_type (field_type);
9747 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9748 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9749 SET_FIELD_BITPOS (type->field (0), bit_offset);
9750
9751 /* The order of fields doesn't really matter, so put the real
9752 field at index 1 and the data-less field at index 2. */
9753 type->field (1) = saved_field;
9754 TYPE_FIELD_NAME (type, 1)
9755 = rust_last_path_segment (type->field (1).type ()->name ());
9756 type->field (1).type ()->set_name
9757 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9758 TYPE_FIELD_NAME (type, 1)));
9759
9760 const char *dataless_name
9761 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9762 name);
9763 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9764 dataless_name);
9765 type->field (2).set_type (dataless_type);
9766 /* NAME points into the original discriminant name, which
9767 already has the correct lifetime. */
9768 TYPE_FIELD_NAME (type, 2) = name;
9769 SET_FIELD_BITPOS (type->field (2), 0);
9770
9771 /* Indicate that this is a variant type. */
9772 static discriminant_range ranges[1] = { { 0, 0 } };
9773 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9774 }
9775 /* A union with a single anonymous field is probably an old-style
9776 univariant enum. */
9777 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9778 {
9779 /* Smash this type to be a structure type. We have to do this
9780 because the type has already been recorded. */
9781 type->set_code (TYPE_CODE_STRUCT);
9782
9783 struct type *field_type = type->field (0).type ();
9784 const char *variant_name
9785 = rust_last_path_segment (field_type->name ());
9786 TYPE_FIELD_NAME (type, 0) = variant_name;
9787 field_type->set_name
9788 (rust_fully_qualify (&objfile->objfile_obstack,
9789 type->name (), variant_name));
9790
9791 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
9792 }
9793 else
9794 {
9795 struct type *disr_type = nullptr;
9796 for (int i = 0; i < type->num_fields (); ++i)
9797 {
9798 disr_type = type->field (i).type ();
9799
9800 if (disr_type->code () != TYPE_CODE_STRUCT)
9801 {
9802 /* All fields of a true enum will be structs. */
9803 return;
9804 }
9805 else if (disr_type->num_fields () == 0)
9806 {
9807 /* Could be data-less variant, so keep going. */
9808 disr_type = nullptr;
9809 }
9810 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9811 "RUST$ENUM$DISR") != 0)
9812 {
9813 /* Not a Rust enum. */
9814 return;
9815 }
9816 else
9817 {
9818 /* Found one. */
9819 break;
9820 }
9821 }
9822
9823 /* If we got here without a discriminant, then it's probably
9824 just a union. */
9825 if (disr_type == nullptr)
9826 return;
9827
9828 /* Smash this type to be a structure type. We have to do this
9829 because the type has already been recorded. */
9830 type->set_code (TYPE_CODE_STRUCT);
9831
9832 /* Make space for the discriminant field. */
9833 struct field *disr_field = &disr_type->field (0);
9834 field *new_fields
9835 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9836 * sizeof (struct field)));
9837 memcpy (new_fields + 1, type->fields (),
9838 type->num_fields () * sizeof (struct field));
9839 type->set_fields (new_fields);
9840 type->set_num_fields (type->num_fields () + 1);
9841
9842 /* Install the discriminant at index 0 in the union. */
9843 type->field (0) = *disr_field;
9844 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9845 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9846
9847 /* We need a way to find the correct discriminant given a
9848 variant name. For convenience we build a map here. */
9849 struct type *enum_type = disr_field->type ();
9850 std::unordered_map<std::string, ULONGEST> discriminant_map;
9851 for (int i = 0; i < enum_type->num_fields (); ++i)
9852 {
9853 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9854 {
9855 const char *name
9856 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9857 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9858 }
9859 }
9860
9861 int n_fields = type->num_fields ();
9862 /* We don't need a range entry for the discriminant, but we do
9863 need one for every other field, as there is no default
9864 variant. */
9865 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9866 discriminant_range,
9867 n_fields - 1);
9868 /* Skip the discriminant here. */
9869 for (int i = 1; i < n_fields; ++i)
9870 {
9871 /* Find the final word in the name of this variant's type.
9872 That name can be used to look up the correct
9873 discriminant. */
9874 const char *variant_name
9875 = rust_last_path_segment (type->field (i).type ()->name ());
9876
9877 auto iter = discriminant_map.find (variant_name);
9878 if (iter != discriminant_map.end ())
9879 {
9880 ranges[i - 1].low = iter->second;
9881 ranges[i - 1].high = iter->second;
9882 }
9883
9884 /* In Rust, each element should have the size of the
9885 enclosing enum. */
9886 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9887
9888 /* Remove the discriminant field, if it exists. */
9889 struct type *sub_type = type->field (i).type ();
9890 if (sub_type->num_fields () > 0)
9891 {
9892 sub_type->set_num_fields (sub_type->num_fields () - 1);
9893 sub_type->set_fields (sub_type->fields () + 1);
9894 }
9895 TYPE_FIELD_NAME (type, i) = variant_name;
9896 sub_type->set_name
9897 (rust_fully_qualify (&objfile->objfile_obstack,
9898 type->name (), variant_name));
9899 }
9900
9901 /* Indicate that this is a variant type. */
9902 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9903 gdb::array_view<discriminant_range> (ranges,
9904 n_fields - 1));
9905 }
9906 }
9907
9908 /* Rewrite some Rust unions to be structures with variants parts. */
9909
9910 static void
9911 rust_union_quirks (struct dwarf2_cu *cu)
9912 {
9913 gdb_assert (cu->language == language_rust);
9914 for (type *type_ : cu->rust_unions)
9915 quirk_rust_enum (type_, cu->per_objfile->objfile);
9916 /* We don't need this any more. */
9917 cu->rust_unions.clear ();
9918 }
9919
9920 /* See read.h. */
9921
9922 type_unit_group_unshareable *
9923 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9924 {
9925 auto iter = this->m_type_units.find (tu_group);
9926 if (iter != this->m_type_units.end ())
9927 return iter->second.get ();
9928
9929 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9930 type_unit_group_unshareable *result = uniq.get ();
9931 this->m_type_units[tu_group] = std::move (uniq);
9932 return result;
9933 }
9934
9935 struct type *
9936 dwarf2_per_objfile::get_type_for_signatured_type
9937 (signatured_type *sig_type) const
9938 {
9939 auto iter = this->m_type_map.find (sig_type);
9940 if (iter == this->m_type_map.end ())
9941 return nullptr;
9942
9943 return iter->second;
9944 }
9945
9946 void dwarf2_per_objfile::set_type_for_signatured_type
9947 (signatured_type *sig_type, struct type *type)
9948 {
9949 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9950
9951 this->m_type_map[sig_type] = type;
9952 }
9953
9954 /* A helper function for computing the list of all symbol tables
9955 included by PER_CU. */
9956
9957 static void
9958 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9959 htab_t all_children, htab_t all_type_symtabs,
9960 dwarf2_per_cu_data *per_cu,
9961 dwarf2_per_objfile *per_objfile,
9962 struct compunit_symtab *immediate_parent)
9963 {
9964 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9965 if (*slot != NULL)
9966 {
9967 /* This inclusion and its children have been processed. */
9968 return;
9969 }
9970
9971 *slot = per_cu;
9972
9973 /* Only add a CU if it has a symbol table. */
9974 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9975 if (cust != NULL)
9976 {
9977 /* If this is a type unit only add its symbol table if we haven't
9978 seen it yet (type unit per_cu's can share symtabs). */
9979 if (per_cu->is_debug_types)
9980 {
9981 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9982 if (*slot == NULL)
9983 {
9984 *slot = cust;
9985 result->push_back (cust);
9986 if (cust->user == NULL)
9987 cust->user = immediate_parent;
9988 }
9989 }
9990 else
9991 {
9992 result->push_back (cust);
9993 if (cust->user == NULL)
9994 cust->user = immediate_parent;
9995 }
9996 }
9997
9998 if (!per_cu->imported_symtabs_empty ())
9999 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10000 {
10001 recursively_compute_inclusions (result, all_children,
10002 all_type_symtabs, ptr, per_objfile,
10003 cust);
10004 }
10005 }
10006
10007 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10008 PER_CU. */
10009
10010 static void
10011 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
10012 dwarf2_per_objfile *per_objfile)
10013 {
10014 gdb_assert (! per_cu->is_debug_types);
10015
10016 if (!per_cu->imported_symtabs_empty ())
10017 {
10018 int len;
10019 std::vector<compunit_symtab *> result_symtabs;
10020 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
10021
10022 /* If we don't have a symtab, we can just skip this case. */
10023 if (cust == NULL)
10024 return;
10025
10026 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
10027 htab_eq_pointer,
10028 NULL, xcalloc, xfree));
10029 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
10030 htab_eq_pointer,
10031 NULL, xcalloc, xfree));
10032
10033 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10034 {
10035 recursively_compute_inclusions (&result_symtabs, all_children.get (),
10036 all_type_symtabs.get (), ptr,
10037 per_objfile, cust);
10038 }
10039
10040 /* Now we have a transitive closure of all the included symtabs. */
10041 len = result_symtabs.size ();
10042 cust->includes
10043 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
10044 struct compunit_symtab *, len + 1);
10045 memcpy (cust->includes, result_symtabs.data (),
10046 len * sizeof (compunit_symtab *));
10047 cust->includes[len] = NULL;
10048 }
10049 }
10050
10051 /* Compute the 'includes' field for the symtabs of all the CUs we just
10052 read. */
10053
10054 static void
10055 process_cu_includes (dwarf2_per_objfile *per_objfile)
10056 {
10057 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
10058 {
10059 if (! iter->is_debug_types)
10060 compute_compunit_symtab_includes (iter, per_objfile);
10061 }
10062
10063 per_objfile->per_bfd->just_read_cus.clear ();
10064 }
10065
10066 /* Generate full symbol information for CU, whose DIEs have
10067 already been loaded into memory. */
10068
10069 static void
10070 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10071 {
10072 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10073 struct objfile *objfile = per_objfile->objfile;
10074 struct gdbarch *gdbarch = objfile->arch ();
10075 CORE_ADDR lowpc, highpc;
10076 struct compunit_symtab *cust;
10077 CORE_ADDR baseaddr;
10078 struct block *static_block;
10079 CORE_ADDR addr;
10080
10081 baseaddr = objfile->text_section_offset ();
10082
10083 /* Clear the list here in case something was left over. */
10084 cu->method_list.clear ();
10085
10086 cu->language = pretend_language;
10087 cu->language_defn = language_def (cu->language);
10088
10089 dwarf2_find_base_address (cu->dies, cu);
10090
10091 /* Do line number decoding in read_file_scope () */
10092 process_die (cu->dies, cu);
10093
10094 /* For now fudge the Go package. */
10095 if (cu->language == language_go)
10096 fixup_go_packaging (cu);
10097
10098 /* Now that we have processed all the DIEs in the CU, all the types
10099 should be complete, and it should now be safe to compute all of the
10100 physnames. */
10101 compute_delayed_physnames (cu);
10102
10103 if (cu->language == language_rust)
10104 rust_union_quirks (cu);
10105
10106 /* Some compilers don't define a DW_AT_high_pc attribute for the
10107 compilation unit. If the DW_AT_high_pc is missing, synthesize
10108 it, by scanning the DIE's below the compilation unit. */
10109 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10110
10111 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10112 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10113
10114 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10115 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10116 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10117 addrmap to help ensure it has an accurate map of pc values belonging to
10118 this comp unit. */
10119 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10120
10121 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10122 SECT_OFF_TEXT (objfile),
10123 0);
10124
10125 if (cust != NULL)
10126 {
10127 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10128
10129 /* Set symtab language to language from DW_AT_language. If the
10130 compilation is from a C file generated by language preprocessors, do
10131 not set the language if it was already deduced by start_subfile. */
10132 if (!(cu->language == language_c
10133 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10134 COMPUNIT_FILETABS (cust)->language = cu->language;
10135
10136 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10137 produce DW_AT_location with location lists but it can be possibly
10138 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10139 there were bugs in prologue debug info, fixed later in GCC-4.5
10140 by "unwind info for epilogues" patch (which is not directly related).
10141
10142 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10143 needed, it would be wrong due to missing DW_AT_producer there.
10144
10145 Still one can confuse GDB by using non-standard GCC compilation
10146 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10147 */
10148 if (cu->has_loclist && gcc_4_minor >= 5)
10149 cust->locations_valid = 1;
10150
10151 if (gcc_4_minor >= 5)
10152 cust->epilogue_unwind_valid = 1;
10153
10154 cust->call_site_htab = cu->call_site_htab;
10155 }
10156
10157 per_objfile->set_symtab (cu->per_cu, cust);
10158
10159 /* Push it for inclusion processing later. */
10160 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
10161
10162 /* Not needed any more. */
10163 cu->reset_builder ();
10164 }
10165
10166 /* Generate full symbol information for type unit CU, whose DIEs have
10167 already been loaded into memory. */
10168
10169 static void
10170 process_full_type_unit (dwarf2_cu *cu,
10171 enum language pretend_language)
10172 {
10173 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10174 struct objfile *objfile = per_objfile->objfile;
10175 struct compunit_symtab *cust;
10176 struct signatured_type *sig_type;
10177
10178 gdb_assert (cu->per_cu->is_debug_types);
10179 sig_type = (struct signatured_type *) cu->per_cu;
10180
10181 /* Clear the list here in case something was left over. */
10182 cu->method_list.clear ();
10183
10184 cu->language = pretend_language;
10185 cu->language_defn = language_def (cu->language);
10186
10187 /* The symbol tables are set up in read_type_unit_scope. */
10188 process_die (cu->dies, cu);
10189
10190 /* For now fudge the Go package. */
10191 if (cu->language == language_go)
10192 fixup_go_packaging (cu);
10193
10194 /* Now that we have processed all the DIEs in the CU, all the types
10195 should be complete, and it should now be safe to compute all of the
10196 physnames. */
10197 compute_delayed_physnames (cu);
10198
10199 if (cu->language == language_rust)
10200 rust_union_quirks (cu);
10201
10202 /* TUs share symbol tables.
10203 If this is the first TU to use this symtab, complete the construction
10204 of it with end_expandable_symtab. Otherwise, complete the addition of
10205 this TU's symbols to the existing symtab. */
10206 type_unit_group_unshareable *tug_unshare =
10207 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
10208 if (tug_unshare->compunit_symtab == NULL)
10209 {
10210 buildsym_compunit *builder = cu->get_builder ();
10211 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10212 tug_unshare->compunit_symtab = cust;
10213
10214 if (cust != NULL)
10215 {
10216 /* Set symtab language to language from DW_AT_language. If the
10217 compilation is from a C file generated by language preprocessors,
10218 do not set the language if it was already deduced by
10219 start_subfile. */
10220 if (!(cu->language == language_c
10221 && COMPUNIT_FILETABS (cust)->language != language_c))
10222 COMPUNIT_FILETABS (cust)->language = cu->language;
10223 }
10224 }
10225 else
10226 {
10227 cu->get_builder ()->augment_type_symtab ();
10228 cust = tug_unshare->compunit_symtab;
10229 }
10230
10231 per_objfile->set_symtab (cu->per_cu, cust);
10232
10233 /* Not needed any more. */
10234 cu->reset_builder ();
10235 }
10236
10237 /* Process an imported unit DIE. */
10238
10239 static void
10240 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10241 {
10242 struct attribute *attr;
10243
10244 /* For now we don't handle imported units in type units. */
10245 if (cu->per_cu->is_debug_types)
10246 {
10247 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10248 " supported in type units [in module %s]"),
10249 objfile_name (cu->per_objfile->objfile));
10250 }
10251
10252 attr = dwarf2_attr (die, DW_AT_import, cu);
10253 if (attr != NULL)
10254 {
10255 sect_offset sect_off = attr->get_ref_die_offset ();
10256 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10257 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10258 dwarf2_per_cu_data *per_cu
10259 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
10260
10261 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10262 into another compilation unit, at root level. Regard this as a hint,
10263 and ignore it. */
10264 if (die->parent && die->parent->parent == NULL
10265 && per_cu->unit_type == DW_UT_compile
10266 && per_cu->lang == language_cplus)
10267 return;
10268
10269 /* If necessary, add it to the queue and load its DIEs. */
10270 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
10271 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
10272 false, cu->language);
10273
10274 cu->per_cu->imported_symtabs_push (per_cu);
10275 }
10276 }
10277
10278 /* RAII object that represents a process_die scope: i.e.,
10279 starts/finishes processing a DIE. */
10280 class process_die_scope
10281 {
10282 public:
10283 process_die_scope (die_info *die, dwarf2_cu *cu)
10284 : m_die (die), m_cu (cu)
10285 {
10286 /* We should only be processing DIEs not already in process. */
10287 gdb_assert (!m_die->in_process);
10288 m_die->in_process = true;
10289 }
10290
10291 ~process_die_scope ()
10292 {
10293 m_die->in_process = false;
10294
10295 /* If we're done processing the DIE for the CU that owns the line
10296 header, we don't need the line header anymore. */
10297 if (m_cu->line_header_die_owner == m_die)
10298 {
10299 delete m_cu->line_header;
10300 m_cu->line_header = NULL;
10301 m_cu->line_header_die_owner = NULL;
10302 }
10303 }
10304
10305 private:
10306 die_info *m_die;
10307 dwarf2_cu *m_cu;
10308 };
10309
10310 /* Process a die and its children. */
10311
10312 static void
10313 process_die (struct die_info *die, struct dwarf2_cu *cu)
10314 {
10315 process_die_scope scope (die, cu);
10316
10317 switch (die->tag)
10318 {
10319 case DW_TAG_padding:
10320 break;
10321 case DW_TAG_compile_unit:
10322 case DW_TAG_partial_unit:
10323 read_file_scope (die, cu);
10324 break;
10325 case DW_TAG_type_unit:
10326 read_type_unit_scope (die, cu);
10327 break;
10328 case DW_TAG_subprogram:
10329 /* Nested subprograms in Fortran get a prefix. */
10330 if (cu->language == language_fortran
10331 && die->parent != NULL
10332 && die->parent->tag == DW_TAG_subprogram)
10333 cu->processing_has_namespace_info = true;
10334 /* Fall through. */
10335 case DW_TAG_inlined_subroutine:
10336 read_func_scope (die, cu);
10337 break;
10338 case DW_TAG_lexical_block:
10339 case DW_TAG_try_block:
10340 case DW_TAG_catch_block:
10341 read_lexical_block_scope (die, cu);
10342 break;
10343 case DW_TAG_call_site:
10344 case DW_TAG_GNU_call_site:
10345 read_call_site_scope (die, cu);
10346 break;
10347 case DW_TAG_class_type:
10348 case DW_TAG_interface_type:
10349 case DW_TAG_structure_type:
10350 case DW_TAG_union_type:
10351 process_structure_scope (die, cu);
10352 break;
10353 case DW_TAG_enumeration_type:
10354 process_enumeration_scope (die, cu);
10355 break;
10356
10357 /* These dies have a type, but processing them does not create
10358 a symbol or recurse to process the children. Therefore we can
10359 read them on-demand through read_type_die. */
10360 case DW_TAG_subroutine_type:
10361 case DW_TAG_set_type:
10362 case DW_TAG_pointer_type:
10363 case DW_TAG_ptr_to_member_type:
10364 case DW_TAG_reference_type:
10365 case DW_TAG_rvalue_reference_type:
10366 case DW_TAG_string_type:
10367 break;
10368
10369 case DW_TAG_array_type:
10370 /* We only need to handle this case for Ada -- in other
10371 languages, it's normal for the compiler to emit a typedef
10372 instead. */
10373 if (cu->language != language_ada)
10374 break;
10375 /* FALLTHROUGH */
10376 case DW_TAG_base_type:
10377 case DW_TAG_subrange_type:
10378 case DW_TAG_typedef:
10379 /* Add a typedef symbol for the type definition, if it has a
10380 DW_AT_name. */
10381 new_symbol (die, read_type_die (die, cu), cu);
10382 break;
10383 case DW_TAG_common_block:
10384 read_common_block (die, cu);
10385 break;
10386 case DW_TAG_common_inclusion:
10387 break;
10388 case DW_TAG_namespace:
10389 cu->processing_has_namespace_info = true;
10390 read_namespace (die, cu);
10391 break;
10392 case DW_TAG_module:
10393 cu->processing_has_namespace_info = true;
10394 read_module (die, cu);
10395 break;
10396 case DW_TAG_imported_declaration:
10397 cu->processing_has_namespace_info = true;
10398 if (read_namespace_alias (die, cu))
10399 break;
10400 /* The declaration is not a global namespace alias. */
10401 /* Fall through. */
10402 case DW_TAG_imported_module:
10403 cu->processing_has_namespace_info = true;
10404 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10405 || cu->language != language_fortran))
10406 complaint (_("Tag '%s' has unexpected children"),
10407 dwarf_tag_name (die->tag));
10408 read_import_statement (die, cu);
10409 break;
10410
10411 case DW_TAG_imported_unit:
10412 process_imported_unit_die (die, cu);
10413 break;
10414
10415 case DW_TAG_variable:
10416 read_variable (die, cu);
10417 break;
10418
10419 default:
10420 new_symbol (die, NULL, cu);
10421 break;
10422 }
10423 }
10424 \f
10425 /* DWARF name computation. */
10426
10427 /* A helper function for dwarf2_compute_name which determines whether DIE
10428 needs to have the name of the scope prepended to the name listed in the
10429 die. */
10430
10431 static int
10432 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10433 {
10434 struct attribute *attr;
10435
10436 switch (die->tag)
10437 {
10438 case DW_TAG_namespace:
10439 case DW_TAG_typedef:
10440 case DW_TAG_class_type:
10441 case DW_TAG_interface_type:
10442 case DW_TAG_structure_type:
10443 case DW_TAG_union_type:
10444 case DW_TAG_enumeration_type:
10445 case DW_TAG_enumerator:
10446 case DW_TAG_subprogram:
10447 case DW_TAG_inlined_subroutine:
10448 case DW_TAG_member:
10449 case DW_TAG_imported_declaration:
10450 return 1;
10451
10452 case DW_TAG_variable:
10453 case DW_TAG_constant:
10454 /* We only need to prefix "globally" visible variables. These include
10455 any variable marked with DW_AT_external or any variable that
10456 lives in a namespace. [Variables in anonymous namespaces
10457 require prefixing, but they are not DW_AT_external.] */
10458
10459 if (dwarf2_attr (die, DW_AT_specification, cu))
10460 {
10461 struct dwarf2_cu *spec_cu = cu;
10462
10463 return die_needs_namespace (die_specification (die, &spec_cu),
10464 spec_cu);
10465 }
10466
10467 attr = dwarf2_attr (die, DW_AT_external, cu);
10468 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10469 && die->parent->tag != DW_TAG_module)
10470 return 0;
10471 /* A variable in a lexical block of some kind does not need a
10472 namespace, even though in C++ such variables may be external
10473 and have a mangled name. */
10474 if (die->parent->tag == DW_TAG_lexical_block
10475 || die->parent->tag == DW_TAG_try_block
10476 || die->parent->tag == DW_TAG_catch_block
10477 || die->parent->tag == DW_TAG_subprogram)
10478 return 0;
10479 return 1;
10480
10481 default:
10482 return 0;
10483 }
10484 }
10485
10486 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10487 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10488 defined for the given DIE. */
10489
10490 static struct attribute *
10491 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10492 {
10493 struct attribute *attr;
10494
10495 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10496 if (attr == NULL)
10497 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10498
10499 return attr;
10500 }
10501
10502 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10503 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10504 defined for the given DIE. */
10505
10506 static const char *
10507 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10508 {
10509 const char *linkage_name;
10510
10511 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10512 if (linkage_name == NULL)
10513 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10514
10515 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10516 See https://github.com/rust-lang/rust/issues/32925. */
10517 if (cu->language == language_rust && linkage_name != NULL
10518 && strchr (linkage_name, '{') != NULL)
10519 linkage_name = NULL;
10520
10521 return linkage_name;
10522 }
10523
10524 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10525 compute the physname for the object, which include a method's:
10526 - formal parameters (C++),
10527 - receiver type (Go),
10528
10529 The term "physname" is a bit confusing.
10530 For C++, for example, it is the demangled name.
10531 For Go, for example, it's the mangled name.
10532
10533 For Ada, return the DIE's linkage name rather than the fully qualified
10534 name. PHYSNAME is ignored..
10535
10536 The result is allocated on the objfile->per_bfd's obstack and
10537 canonicalized. */
10538
10539 static const char *
10540 dwarf2_compute_name (const char *name,
10541 struct die_info *die, struct dwarf2_cu *cu,
10542 int physname)
10543 {
10544 struct objfile *objfile = cu->per_objfile->objfile;
10545
10546 if (name == NULL)
10547 name = dwarf2_name (die, cu);
10548
10549 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10550 but otherwise compute it by typename_concat inside GDB.
10551 FIXME: Actually this is not really true, or at least not always true.
10552 It's all very confusing. compute_and_set_names doesn't try to demangle
10553 Fortran names because there is no mangling standard. So new_symbol
10554 will set the demangled name to the result of dwarf2_full_name, and it is
10555 the demangled name that GDB uses if it exists. */
10556 if (cu->language == language_ada
10557 || (cu->language == language_fortran && physname))
10558 {
10559 /* For Ada unit, we prefer the linkage name over the name, as
10560 the former contains the exported name, which the user expects
10561 to be able to reference. Ideally, we want the user to be able
10562 to reference this entity using either natural or linkage name,
10563 but we haven't started looking at this enhancement yet. */
10564 const char *linkage_name = dw2_linkage_name (die, cu);
10565
10566 if (linkage_name != NULL)
10567 return linkage_name;
10568 }
10569
10570 /* These are the only languages we know how to qualify names in. */
10571 if (name != NULL
10572 && (cu->language == language_cplus
10573 || cu->language == language_fortran || cu->language == language_d
10574 || cu->language == language_rust))
10575 {
10576 if (die_needs_namespace (die, cu))
10577 {
10578 const char *prefix;
10579 const char *canonical_name = NULL;
10580
10581 string_file buf;
10582
10583 prefix = determine_prefix (die, cu);
10584 if (*prefix != '\0')
10585 {
10586 gdb::unique_xmalloc_ptr<char> prefixed_name
10587 (typename_concat (NULL, prefix, name, physname, cu));
10588
10589 buf.puts (prefixed_name.get ());
10590 }
10591 else
10592 buf.puts (name);
10593
10594 /* Template parameters may be specified in the DIE's DW_AT_name, or
10595 as children with DW_TAG_template_type_param or
10596 DW_TAG_value_type_param. If the latter, add them to the name
10597 here. If the name already has template parameters, then
10598 skip this step; some versions of GCC emit both, and
10599 it is more efficient to use the pre-computed name.
10600
10601 Something to keep in mind about this process: it is very
10602 unlikely, or in some cases downright impossible, to produce
10603 something that will match the mangled name of a function.
10604 If the definition of the function has the same debug info,
10605 we should be able to match up with it anyway. But fallbacks
10606 using the minimal symbol, for instance to find a method
10607 implemented in a stripped copy of libstdc++, will not work.
10608 If we do not have debug info for the definition, we will have to
10609 match them up some other way.
10610
10611 When we do name matching there is a related problem with function
10612 templates; two instantiated function templates are allowed to
10613 differ only by their return types, which we do not add here. */
10614
10615 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10616 {
10617 struct attribute *attr;
10618 struct die_info *child;
10619 int first = 1;
10620 const language_defn *cplus_lang = language_def (cu->language);
10621
10622 die->building_fullname = 1;
10623
10624 for (child = die->child; child != NULL; child = child->sibling)
10625 {
10626 struct type *type;
10627 LONGEST value;
10628 const gdb_byte *bytes;
10629 struct dwarf2_locexpr_baton *baton;
10630 struct value *v;
10631
10632 if (child->tag != DW_TAG_template_type_param
10633 && child->tag != DW_TAG_template_value_param)
10634 continue;
10635
10636 if (first)
10637 {
10638 buf.puts ("<");
10639 first = 0;
10640 }
10641 else
10642 buf.puts (", ");
10643
10644 attr = dwarf2_attr (child, DW_AT_type, cu);
10645 if (attr == NULL)
10646 {
10647 complaint (_("template parameter missing DW_AT_type"));
10648 buf.puts ("UNKNOWN_TYPE");
10649 continue;
10650 }
10651 type = die_type (child, cu);
10652
10653 if (child->tag == DW_TAG_template_type_param)
10654 {
10655 cplus_lang->print_type (type, "", &buf, -1, 0,
10656 &type_print_raw_options);
10657 continue;
10658 }
10659
10660 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10661 if (attr == NULL)
10662 {
10663 complaint (_("template parameter missing "
10664 "DW_AT_const_value"));
10665 buf.puts ("UNKNOWN_VALUE");
10666 continue;
10667 }
10668
10669 dwarf2_const_value_attr (attr, type, name,
10670 &cu->comp_unit_obstack, cu,
10671 &value, &bytes, &baton);
10672
10673 if (type->has_no_signedness ())
10674 /* GDB prints characters as NUMBER 'CHAR'. If that's
10675 changed, this can use value_print instead. */
10676 cplus_lang->printchar (value, type, &buf);
10677 else
10678 {
10679 struct value_print_options opts;
10680
10681 if (baton != NULL)
10682 v = dwarf2_evaluate_loc_desc (type, NULL,
10683 baton->data,
10684 baton->size,
10685 baton->per_cu,
10686 baton->per_objfile);
10687 else if (bytes != NULL)
10688 {
10689 v = allocate_value (type);
10690 memcpy (value_contents_writeable (v), bytes,
10691 TYPE_LENGTH (type));
10692 }
10693 else
10694 v = value_from_longest (type, value);
10695
10696 /* Specify decimal so that we do not depend on
10697 the radix. */
10698 get_formatted_print_options (&opts, 'd');
10699 opts.raw = 1;
10700 value_print (v, &buf, &opts);
10701 release_value (v);
10702 }
10703 }
10704
10705 die->building_fullname = 0;
10706
10707 if (!first)
10708 {
10709 /* Close the argument list, with a space if necessary
10710 (nested templates). */
10711 if (!buf.empty () && buf.string ().back () == '>')
10712 buf.puts (" >");
10713 else
10714 buf.puts (">");
10715 }
10716 }
10717
10718 /* For C++ methods, append formal parameter type
10719 information, if PHYSNAME. */
10720
10721 if (physname && die->tag == DW_TAG_subprogram
10722 && cu->language == language_cplus)
10723 {
10724 struct type *type = read_type_die (die, cu);
10725
10726 c_type_print_args (type, &buf, 1, cu->language,
10727 &type_print_raw_options);
10728
10729 if (cu->language == language_cplus)
10730 {
10731 /* Assume that an artificial first parameter is
10732 "this", but do not crash if it is not. RealView
10733 marks unnamed (and thus unused) parameters as
10734 artificial; there is no way to differentiate
10735 the two cases. */
10736 if (type->num_fields () > 0
10737 && TYPE_FIELD_ARTIFICIAL (type, 0)
10738 && type->field (0).type ()->code () == TYPE_CODE_PTR
10739 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
10740 buf.puts (" const");
10741 }
10742 }
10743
10744 const std::string &intermediate_name = buf.string ();
10745
10746 if (cu->language == language_cplus)
10747 canonical_name
10748 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10749 objfile);
10750
10751 /* If we only computed INTERMEDIATE_NAME, or if
10752 INTERMEDIATE_NAME is already canonical, then we need to
10753 intern it. */
10754 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10755 name = objfile->intern (intermediate_name);
10756 else
10757 name = canonical_name;
10758 }
10759 }
10760
10761 return name;
10762 }
10763
10764 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10765 If scope qualifiers are appropriate they will be added. The result
10766 will be allocated on the storage_obstack, or NULL if the DIE does
10767 not have a name. NAME may either be from a previous call to
10768 dwarf2_name or NULL.
10769
10770 The output string will be canonicalized (if C++). */
10771
10772 static const char *
10773 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10774 {
10775 return dwarf2_compute_name (name, die, cu, 0);
10776 }
10777
10778 /* Construct a physname for the given DIE in CU. NAME may either be
10779 from a previous call to dwarf2_name or NULL. The result will be
10780 allocated on the objfile_objstack or NULL if the DIE does not have a
10781 name.
10782
10783 The output string will be canonicalized (if C++). */
10784
10785 static const char *
10786 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10787 {
10788 struct objfile *objfile = cu->per_objfile->objfile;
10789 const char *retval, *mangled = NULL, *canon = NULL;
10790 int need_copy = 1;
10791
10792 /* In this case dwarf2_compute_name is just a shortcut not building anything
10793 on its own. */
10794 if (!die_needs_namespace (die, cu))
10795 return dwarf2_compute_name (name, die, cu, 1);
10796
10797 if (cu->language != language_rust)
10798 mangled = dw2_linkage_name (die, cu);
10799
10800 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10801 has computed. */
10802 gdb::unique_xmalloc_ptr<char> demangled;
10803 if (mangled != NULL)
10804 {
10805
10806 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
10807 {
10808 /* Do nothing (do not demangle the symbol name). */
10809 }
10810 else
10811 {
10812 /* Use DMGL_RET_DROP for C++ template functions to suppress
10813 their return type. It is easier for GDB users to search
10814 for such functions as `name(params)' than `long name(params)'.
10815 In such case the minimal symbol names do not match the full
10816 symbol names but for template functions there is never a need
10817 to look up their definition from their declaration so
10818 the only disadvantage remains the minimal symbol variant
10819 `long name(params)' does not have the proper inferior type. */
10820 demangled.reset (gdb_demangle (mangled,
10821 (DMGL_PARAMS | DMGL_ANSI
10822 | DMGL_RET_DROP)));
10823 }
10824 if (demangled)
10825 canon = demangled.get ();
10826 else
10827 {
10828 canon = mangled;
10829 need_copy = 0;
10830 }
10831 }
10832
10833 if (canon == NULL || check_physname)
10834 {
10835 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10836
10837 if (canon != NULL && strcmp (physname, canon) != 0)
10838 {
10839 /* It may not mean a bug in GDB. The compiler could also
10840 compute DW_AT_linkage_name incorrectly. But in such case
10841 GDB would need to be bug-to-bug compatible. */
10842
10843 complaint (_("Computed physname <%s> does not match demangled <%s> "
10844 "(from linkage <%s>) - DIE at %s [in module %s]"),
10845 physname, canon, mangled, sect_offset_str (die->sect_off),
10846 objfile_name (objfile));
10847
10848 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10849 is available here - over computed PHYSNAME. It is safer
10850 against both buggy GDB and buggy compilers. */
10851
10852 retval = canon;
10853 }
10854 else
10855 {
10856 retval = physname;
10857 need_copy = 0;
10858 }
10859 }
10860 else
10861 retval = canon;
10862
10863 if (need_copy)
10864 retval = objfile->intern (retval);
10865
10866 return retval;
10867 }
10868
10869 /* Inspect DIE in CU for a namespace alias. If one exists, record
10870 a new symbol for it.
10871
10872 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10873
10874 static int
10875 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10876 {
10877 struct attribute *attr;
10878
10879 /* If the die does not have a name, this is not a namespace
10880 alias. */
10881 attr = dwarf2_attr (die, DW_AT_name, cu);
10882 if (attr != NULL)
10883 {
10884 int num;
10885 struct die_info *d = die;
10886 struct dwarf2_cu *imported_cu = cu;
10887
10888 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10889 keep inspecting DIEs until we hit the underlying import. */
10890 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10891 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10892 {
10893 attr = dwarf2_attr (d, DW_AT_import, cu);
10894 if (attr == NULL)
10895 break;
10896
10897 d = follow_die_ref (d, attr, &imported_cu);
10898 if (d->tag != DW_TAG_imported_declaration)
10899 break;
10900 }
10901
10902 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10903 {
10904 complaint (_("DIE at %s has too many recursively imported "
10905 "declarations"), sect_offset_str (d->sect_off));
10906 return 0;
10907 }
10908
10909 if (attr != NULL)
10910 {
10911 struct type *type;
10912 sect_offset sect_off = attr->get_ref_die_offset ();
10913
10914 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10915 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10916 {
10917 /* This declaration is a global namespace alias. Add
10918 a symbol for it whose type is the aliased namespace. */
10919 new_symbol (die, type, cu);
10920 return 1;
10921 }
10922 }
10923 }
10924
10925 return 0;
10926 }
10927
10928 /* Return the using directives repository (global or local?) to use in the
10929 current context for CU.
10930
10931 For Ada, imported declarations can materialize renamings, which *may* be
10932 global. However it is impossible (for now?) in DWARF to distinguish
10933 "external" imported declarations and "static" ones. As all imported
10934 declarations seem to be static in all other languages, make them all CU-wide
10935 global only in Ada. */
10936
10937 static struct using_direct **
10938 using_directives (struct dwarf2_cu *cu)
10939 {
10940 if (cu->language == language_ada
10941 && cu->get_builder ()->outermost_context_p ())
10942 return cu->get_builder ()->get_global_using_directives ();
10943 else
10944 return cu->get_builder ()->get_local_using_directives ();
10945 }
10946
10947 /* Read the import statement specified by the given die and record it. */
10948
10949 static void
10950 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10951 {
10952 struct objfile *objfile = cu->per_objfile->objfile;
10953 struct attribute *import_attr;
10954 struct die_info *imported_die, *child_die;
10955 struct dwarf2_cu *imported_cu;
10956 const char *imported_name;
10957 const char *imported_name_prefix;
10958 const char *canonical_name;
10959 const char *import_alias;
10960 const char *imported_declaration = NULL;
10961 const char *import_prefix;
10962 std::vector<const char *> excludes;
10963
10964 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10965 if (import_attr == NULL)
10966 {
10967 complaint (_("Tag '%s' has no DW_AT_import"),
10968 dwarf_tag_name (die->tag));
10969 return;
10970 }
10971
10972 imported_cu = cu;
10973 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10974 imported_name = dwarf2_name (imported_die, imported_cu);
10975 if (imported_name == NULL)
10976 {
10977 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10978
10979 The import in the following code:
10980 namespace A
10981 {
10982 typedef int B;
10983 }
10984
10985 int main ()
10986 {
10987 using A::B;
10988 B b;
10989 return b;
10990 }
10991
10992 ...
10993 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10994 <52> DW_AT_decl_file : 1
10995 <53> DW_AT_decl_line : 6
10996 <54> DW_AT_import : <0x75>
10997 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10998 <59> DW_AT_name : B
10999 <5b> DW_AT_decl_file : 1
11000 <5c> DW_AT_decl_line : 2
11001 <5d> DW_AT_type : <0x6e>
11002 ...
11003 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11004 <76> DW_AT_byte_size : 4
11005 <77> DW_AT_encoding : 5 (signed)
11006
11007 imports the wrong die ( 0x75 instead of 0x58 ).
11008 This case will be ignored until the gcc bug is fixed. */
11009 return;
11010 }
11011
11012 /* Figure out the local name after import. */
11013 import_alias = dwarf2_name (die, cu);
11014
11015 /* Figure out where the statement is being imported to. */
11016 import_prefix = determine_prefix (die, cu);
11017
11018 /* Figure out what the scope of the imported die is and prepend it
11019 to the name of the imported die. */
11020 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11021
11022 if (imported_die->tag != DW_TAG_namespace
11023 && imported_die->tag != DW_TAG_module)
11024 {
11025 imported_declaration = imported_name;
11026 canonical_name = imported_name_prefix;
11027 }
11028 else if (strlen (imported_name_prefix) > 0)
11029 canonical_name = obconcat (&objfile->objfile_obstack,
11030 imported_name_prefix,
11031 (cu->language == language_d ? "." : "::"),
11032 imported_name, (char *) NULL);
11033 else
11034 canonical_name = imported_name;
11035
11036 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11037 for (child_die = die->child; child_die && child_die->tag;
11038 child_die = child_die->sibling)
11039 {
11040 /* DWARF-4: A Fortran use statement with a “rename list” may be
11041 represented by an imported module entry with an import attribute
11042 referring to the module and owned entries corresponding to those
11043 entities that are renamed as part of being imported. */
11044
11045 if (child_die->tag != DW_TAG_imported_declaration)
11046 {
11047 complaint (_("child DW_TAG_imported_declaration expected "
11048 "- DIE at %s [in module %s]"),
11049 sect_offset_str (child_die->sect_off),
11050 objfile_name (objfile));
11051 continue;
11052 }
11053
11054 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11055 if (import_attr == NULL)
11056 {
11057 complaint (_("Tag '%s' has no DW_AT_import"),
11058 dwarf_tag_name (child_die->tag));
11059 continue;
11060 }
11061
11062 imported_cu = cu;
11063 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11064 &imported_cu);
11065 imported_name = dwarf2_name (imported_die, imported_cu);
11066 if (imported_name == NULL)
11067 {
11068 complaint (_("child DW_TAG_imported_declaration has unknown "
11069 "imported name - DIE at %s [in module %s]"),
11070 sect_offset_str (child_die->sect_off),
11071 objfile_name (objfile));
11072 continue;
11073 }
11074
11075 excludes.push_back (imported_name);
11076
11077 process_die (child_die, cu);
11078 }
11079
11080 add_using_directive (using_directives (cu),
11081 import_prefix,
11082 canonical_name,
11083 import_alias,
11084 imported_declaration,
11085 excludes,
11086 0,
11087 &objfile->objfile_obstack);
11088 }
11089
11090 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11091 types, but gives them a size of zero. Starting with version 14,
11092 ICC is compatible with GCC. */
11093
11094 static bool
11095 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11096 {
11097 if (!cu->checked_producer)
11098 check_producer (cu);
11099
11100 return cu->producer_is_icc_lt_14;
11101 }
11102
11103 /* ICC generates a DW_AT_type for C void functions. This was observed on
11104 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11105 which says that void functions should not have a DW_AT_type. */
11106
11107 static bool
11108 producer_is_icc (struct dwarf2_cu *cu)
11109 {
11110 if (!cu->checked_producer)
11111 check_producer (cu);
11112
11113 return cu->producer_is_icc;
11114 }
11115
11116 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11117 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11118 this, it was first present in GCC release 4.3.0. */
11119
11120 static bool
11121 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11122 {
11123 if (!cu->checked_producer)
11124 check_producer (cu);
11125
11126 return cu->producer_is_gcc_lt_4_3;
11127 }
11128
11129 static file_and_directory
11130 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11131 {
11132 file_and_directory res;
11133
11134 /* Find the filename. Do not use dwarf2_name here, since the filename
11135 is not a source language identifier. */
11136 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11137 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11138
11139 if (res.comp_dir == NULL
11140 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11141 && IS_ABSOLUTE_PATH (res.name))
11142 {
11143 res.comp_dir_storage = ldirname (res.name);
11144 if (!res.comp_dir_storage.empty ())
11145 res.comp_dir = res.comp_dir_storage.c_str ();
11146 }
11147 if (res.comp_dir != NULL)
11148 {
11149 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11150 directory, get rid of it. */
11151 const char *cp = strchr (res.comp_dir, ':');
11152
11153 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11154 res.comp_dir = cp + 1;
11155 }
11156
11157 if (res.name == NULL)
11158 res.name = "<unknown>";
11159
11160 return res;
11161 }
11162
11163 /* Handle DW_AT_stmt_list for a compilation unit.
11164 DIE is the DW_TAG_compile_unit die for CU.
11165 COMP_DIR is the compilation directory. LOWPC is passed to
11166 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11167
11168 static void
11169 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11170 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11171 {
11172 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11173 struct attribute *attr;
11174 struct line_header line_header_local;
11175 hashval_t line_header_local_hash;
11176 void **slot;
11177 int decode_mapping;
11178
11179 gdb_assert (! cu->per_cu->is_debug_types);
11180
11181 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11182 if (attr == NULL || !attr->form_is_unsigned ())
11183 return;
11184
11185 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
11186
11187 /* The line header hash table is only created if needed (it exists to
11188 prevent redundant reading of the line table for partial_units).
11189 If we're given a partial_unit, we'll need it. If we're given a
11190 compile_unit, then use the line header hash table if it's already
11191 created, but don't create one just yet. */
11192
11193 if (per_objfile->line_header_hash == NULL
11194 && die->tag == DW_TAG_partial_unit)
11195 {
11196 per_objfile->line_header_hash
11197 .reset (htab_create_alloc (127, line_header_hash_voidp,
11198 line_header_eq_voidp,
11199 free_line_header_voidp,
11200 xcalloc, xfree));
11201 }
11202
11203 line_header_local.sect_off = line_offset;
11204 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11205 line_header_local_hash = line_header_hash (&line_header_local);
11206 if (per_objfile->line_header_hash != NULL)
11207 {
11208 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11209 &line_header_local,
11210 line_header_local_hash, NO_INSERT);
11211
11212 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11213 is not present in *SLOT (since if there is something in *SLOT then
11214 it will be for a partial_unit). */
11215 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11216 {
11217 gdb_assert (*slot != NULL);
11218 cu->line_header = (struct line_header *) *slot;
11219 return;
11220 }
11221 }
11222
11223 /* dwarf_decode_line_header does not yet provide sufficient information.
11224 We always have to call also dwarf_decode_lines for it. */
11225 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11226 if (lh == NULL)
11227 return;
11228
11229 cu->line_header = lh.release ();
11230 cu->line_header_die_owner = die;
11231
11232 if (per_objfile->line_header_hash == NULL)
11233 slot = NULL;
11234 else
11235 {
11236 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11237 &line_header_local,
11238 line_header_local_hash, INSERT);
11239 gdb_assert (slot != NULL);
11240 }
11241 if (slot != NULL && *slot == NULL)
11242 {
11243 /* This newly decoded line number information unit will be owned
11244 by line_header_hash hash table. */
11245 *slot = cu->line_header;
11246 cu->line_header_die_owner = NULL;
11247 }
11248 else
11249 {
11250 /* We cannot free any current entry in (*slot) as that struct line_header
11251 may be already used by multiple CUs. Create only temporary decoded
11252 line_header for this CU - it may happen at most once for each line
11253 number information unit. And if we're not using line_header_hash
11254 then this is what we want as well. */
11255 gdb_assert (die->tag != DW_TAG_partial_unit);
11256 }
11257 decode_mapping = (die->tag != DW_TAG_partial_unit);
11258 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11259 decode_mapping);
11260
11261 }
11262
11263 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11264
11265 static void
11266 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11267 {
11268 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11269 struct objfile *objfile = per_objfile->objfile;
11270 struct gdbarch *gdbarch = objfile->arch ();
11271 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11272 CORE_ADDR highpc = ((CORE_ADDR) 0);
11273 struct attribute *attr;
11274 struct die_info *child_die;
11275 CORE_ADDR baseaddr;
11276
11277 prepare_one_comp_unit (cu, die, cu->language);
11278 baseaddr = objfile->text_section_offset ();
11279
11280 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11281
11282 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11283 from finish_block. */
11284 if (lowpc == ((CORE_ADDR) -1))
11285 lowpc = highpc;
11286 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11287
11288 file_and_directory fnd = find_file_and_directory (die, cu);
11289
11290 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11291 standardised yet. As a workaround for the language detection we fall
11292 back to the DW_AT_producer string. */
11293 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11294 cu->language = language_opencl;
11295
11296 /* Similar hack for Go. */
11297 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11298 set_cu_language (DW_LANG_Go, cu);
11299
11300 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11301
11302 /* Decode line number information if present. We do this before
11303 processing child DIEs, so that the line header table is available
11304 for DW_AT_decl_file. */
11305 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11306
11307 /* Process all dies in compilation unit. */
11308 if (die->child != NULL)
11309 {
11310 child_die = die->child;
11311 while (child_die && child_die->tag)
11312 {
11313 process_die (child_die, cu);
11314 child_die = child_die->sibling;
11315 }
11316 }
11317
11318 /* Decode macro information, if present. Dwarf 2 macro information
11319 refers to information in the line number info statement program
11320 header, so we can only read it if we've read the header
11321 successfully. */
11322 attr = dwarf2_attr (die, DW_AT_macros, cu);
11323 if (attr == NULL)
11324 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11325 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
11326 {
11327 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11328 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11329
11330 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
11331 }
11332 else
11333 {
11334 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11335 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
11336 {
11337 unsigned int macro_offset = attr->as_unsigned ();
11338
11339 dwarf_decode_macros (cu, macro_offset, 0);
11340 }
11341 }
11342 }
11343
11344 void
11345 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11346 {
11347 struct type_unit_group *tu_group;
11348 int first_time;
11349 struct attribute *attr;
11350 unsigned int i;
11351 struct signatured_type *sig_type;
11352
11353 gdb_assert (per_cu->is_debug_types);
11354 sig_type = (struct signatured_type *) per_cu;
11355
11356 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11357
11358 /* If we're using .gdb_index (includes -readnow) then
11359 per_cu->type_unit_group may not have been set up yet. */
11360 if (sig_type->type_unit_group == NULL)
11361 sig_type->type_unit_group = get_type_unit_group (this, attr);
11362 tu_group = sig_type->type_unit_group;
11363
11364 /* If we've already processed this stmt_list there's no real need to
11365 do it again, we could fake it and just recreate the part we need
11366 (file name,index -> symtab mapping). If data shows this optimization
11367 is useful we can do it then. */
11368 type_unit_group_unshareable *tug_unshare
11369 = per_objfile->get_type_unit_group_unshareable (tu_group);
11370 first_time = tug_unshare->compunit_symtab == NULL;
11371
11372 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11373 debug info. */
11374 line_header_up lh;
11375 if (attr != NULL && attr->form_is_unsigned ())
11376 {
11377 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
11378 lh = dwarf_decode_line_header (line_offset, this);
11379 }
11380 if (lh == NULL)
11381 {
11382 if (first_time)
11383 start_symtab ("", NULL, 0);
11384 else
11385 {
11386 gdb_assert (tug_unshare->symtabs == NULL);
11387 gdb_assert (m_builder == nullptr);
11388 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11389 m_builder.reset (new struct buildsym_compunit
11390 (COMPUNIT_OBJFILE (cust), "",
11391 COMPUNIT_DIRNAME (cust),
11392 compunit_language (cust),
11393 0, cust));
11394 list_in_scope = get_builder ()->get_file_symbols ();
11395 }
11396 return;
11397 }
11398
11399 line_header = lh.release ();
11400 line_header_die_owner = die;
11401
11402 if (first_time)
11403 {
11404 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11405
11406 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11407 still initializing it, and our caller (a few levels up)
11408 process_full_type_unit still needs to know if this is the first
11409 time. */
11410
11411 tug_unshare->symtabs
11412 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11413 struct symtab *, line_header->file_names_size ());
11414
11415 auto &file_names = line_header->file_names ();
11416 for (i = 0; i < file_names.size (); ++i)
11417 {
11418 file_entry &fe = file_names[i];
11419 dwarf2_start_subfile (this, fe.name,
11420 fe.include_dir (line_header));
11421 buildsym_compunit *b = get_builder ();
11422 if (b->get_current_subfile ()->symtab == NULL)
11423 {
11424 /* NOTE: start_subfile will recognize when it's been
11425 passed a file it has already seen. So we can't
11426 assume there's a simple mapping from
11427 cu->line_header->file_names to subfiles, plus
11428 cu->line_header->file_names may contain dups. */
11429 b->get_current_subfile ()->symtab
11430 = allocate_symtab (cust, b->get_current_subfile ()->name);
11431 }
11432
11433 fe.symtab = b->get_current_subfile ()->symtab;
11434 tug_unshare->symtabs[i] = fe.symtab;
11435 }
11436 }
11437 else
11438 {
11439 gdb_assert (m_builder == nullptr);
11440 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11441 m_builder.reset (new struct buildsym_compunit
11442 (COMPUNIT_OBJFILE (cust), "",
11443 COMPUNIT_DIRNAME (cust),
11444 compunit_language (cust),
11445 0, cust));
11446 list_in_scope = get_builder ()->get_file_symbols ();
11447
11448 auto &file_names = line_header->file_names ();
11449 for (i = 0; i < file_names.size (); ++i)
11450 {
11451 file_entry &fe = file_names[i];
11452 fe.symtab = tug_unshare->symtabs[i];
11453 }
11454 }
11455
11456 /* The main symtab is allocated last. Type units don't have DW_AT_name
11457 so they don't have a "real" (so to speak) symtab anyway.
11458 There is later code that will assign the main symtab to all symbols
11459 that don't have one. We need to handle the case of a symbol with a
11460 missing symtab (DW_AT_decl_file) anyway. */
11461 }
11462
11463 /* Process DW_TAG_type_unit.
11464 For TUs we want to skip the first top level sibling if it's not the
11465 actual type being defined by this TU. In this case the first top
11466 level sibling is there to provide context only. */
11467
11468 static void
11469 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11470 {
11471 struct die_info *child_die;
11472
11473 prepare_one_comp_unit (cu, die, language_minimal);
11474
11475 /* Initialize (or reinitialize) the machinery for building symtabs.
11476 We do this before processing child DIEs, so that the line header table
11477 is available for DW_AT_decl_file. */
11478 cu->setup_type_unit_groups (die);
11479
11480 if (die->child != NULL)
11481 {
11482 child_die = die->child;
11483 while (child_die && child_die->tag)
11484 {
11485 process_die (child_die, cu);
11486 child_die = child_die->sibling;
11487 }
11488 }
11489 }
11490 \f
11491 /* DWO/DWP files.
11492
11493 http://gcc.gnu.org/wiki/DebugFission
11494 http://gcc.gnu.org/wiki/DebugFissionDWP
11495
11496 To simplify handling of both DWO files ("object" files with the DWARF info)
11497 and DWP files (a file with the DWOs packaged up into one file), we treat
11498 DWP files as having a collection of virtual DWO files. */
11499
11500 static hashval_t
11501 hash_dwo_file (const void *item)
11502 {
11503 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11504 hashval_t hash;
11505
11506 hash = htab_hash_string (dwo_file->dwo_name);
11507 if (dwo_file->comp_dir != NULL)
11508 hash += htab_hash_string (dwo_file->comp_dir);
11509 return hash;
11510 }
11511
11512 static int
11513 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11514 {
11515 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11516 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11517
11518 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11519 return 0;
11520 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11521 return lhs->comp_dir == rhs->comp_dir;
11522 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11523 }
11524
11525 /* Allocate a hash table for DWO files. */
11526
11527 static htab_up
11528 allocate_dwo_file_hash_table ()
11529 {
11530 auto delete_dwo_file = [] (void *item)
11531 {
11532 struct dwo_file *dwo_file = (struct dwo_file *) item;
11533
11534 delete dwo_file;
11535 };
11536
11537 return htab_up (htab_create_alloc (41,
11538 hash_dwo_file,
11539 eq_dwo_file,
11540 delete_dwo_file,
11541 xcalloc, xfree));
11542 }
11543
11544 /* Lookup DWO file DWO_NAME. */
11545
11546 static void **
11547 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
11548 const char *dwo_name,
11549 const char *comp_dir)
11550 {
11551 struct dwo_file find_entry;
11552 void **slot;
11553
11554 if (per_objfile->per_bfd->dwo_files == NULL)
11555 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11556
11557 find_entry.dwo_name = dwo_name;
11558 find_entry.comp_dir = comp_dir;
11559 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
11560 INSERT);
11561
11562 return slot;
11563 }
11564
11565 static hashval_t
11566 hash_dwo_unit (const void *item)
11567 {
11568 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11569
11570 /* This drops the top 32 bits of the id, but is ok for a hash. */
11571 return dwo_unit->signature;
11572 }
11573
11574 static int
11575 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11576 {
11577 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11578 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11579
11580 /* The signature is assumed to be unique within the DWO file.
11581 So while object file CU dwo_id's always have the value zero,
11582 that's OK, assuming each object file DWO file has only one CU,
11583 and that's the rule for now. */
11584 return lhs->signature == rhs->signature;
11585 }
11586
11587 /* Allocate a hash table for DWO CUs,TUs.
11588 There is one of these tables for each of CUs,TUs for each DWO file. */
11589
11590 static htab_up
11591 allocate_dwo_unit_table ()
11592 {
11593 /* Start out with a pretty small number.
11594 Generally DWO files contain only one CU and maybe some TUs. */
11595 return htab_up (htab_create_alloc (3,
11596 hash_dwo_unit,
11597 eq_dwo_unit,
11598 NULL, xcalloc, xfree));
11599 }
11600
11601 /* die_reader_func for create_dwo_cu. */
11602
11603 static void
11604 create_dwo_cu_reader (const struct die_reader_specs *reader,
11605 const gdb_byte *info_ptr,
11606 struct die_info *comp_unit_die,
11607 struct dwo_file *dwo_file,
11608 struct dwo_unit *dwo_unit)
11609 {
11610 struct dwarf2_cu *cu = reader->cu;
11611 sect_offset sect_off = cu->per_cu->sect_off;
11612 struct dwarf2_section_info *section = cu->per_cu->section;
11613
11614 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11615 if (!signature.has_value ())
11616 {
11617 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11618 " its dwo_id [in module %s]"),
11619 sect_offset_str (sect_off), dwo_file->dwo_name);
11620 return;
11621 }
11622
11623 dwo_unit->dwo_file = dwo_file;
11624 dwo_unit->signature = *signature;
11625 dwo_unit->section = section;
11626 dwo_unit->sect_off = sect_off;
11627 dwo_unit->length = cu->per_cu->length;
11628
11629 dwarf_read_debug_printf (" offset %s, dwo_id %s",
11630 sect_offset_str (sect_off),
11631 hex_string (dwo_unit->signature));
11632 }
11633
11634 /* Create the dwo_units for the CUs in a DWO_FILE.
11635 Note: This function processes DWO files only, not DWP files. */
11636
11637 static void
11638 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
11639 dwarf2_cu *cu, struct dwo_file &dwo_file,
11640 dwarf2_section_info &section, htab_up &cus_htab)
11641 {
11642 struct objfile *objfile = per_objfile->objfile;
11643 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
11644 const gdb_byte *info_ptr, *end_ptr;
11645
11646 section.read (objfile);
11647 info_ptr = section.buffer;
11648
11649 if (info_ptr == NULL)
11650 return;
11651
11652 dwarf_read_debug_printf ("Reading %s for %s:",
11653 section.get_name (),
11654 section.get_file_name ());
11655
11656 end_ptr = info_ptr + section.size;
11657 while (info_ptr < end_ptr)
11658 {
11659 struct dwarf2_per_cu_data per_cu;
11660 struct dwo_unit read_unit {};
11661 struct dwo_unit *dwo_unit;
11662 void **slot;
11663 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11664
11665 memset (&per_cu, 0, sizeof (per_cu));
11666 per_cu.per_bfd = per_bfd;
11667 per_cu.is_debug_types = 0;
11668 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11669 per_cu.section = &section;
11670
11671 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
11672 if (!reader.dummy_p)
11673 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11674 &dwo_file, &read_unit);
11675 info_ptr += per_cu.length;
11676
11677 // If the unit could not be parsed, skip it.
11678 if (read_unit.dwo_file == NULL)
11679 continue;
11680
11681 if (cus_htab == NULL)
11682 cus_htab = allocate_dwo_unit_table ();
11683
11684 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11685 struct dwo_unit);
11686 *dwo_unit = read_unit;
11687 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11688 gdb_assert (slot != NULL);
11689 if (*slot != NULL)
11690 {
11691 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11692 sect_offset dup_sect_off = dup_cu->sect_off;
11693
11694 complaint (_("debug cu entry at offset %s is duplicate to"
11695 " the entry at offset %s, signature %s"),
11696 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11697 hex_string (dwo_unit->signature));
11698 }
11699 *slot = (void *)dwo_unit;
11700 }
11701 }
11702
11703 /* DWP file .debug_{cu,tu}_index section format:
11704 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11705 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11706
11707 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11708 officially standard DWP format was published with DWARF v5 and is called
11709 Version 5. There are no versions 3 or 4.
11710
11711 DWP Version 1:
11712
11713 Both index sections have the same format, and serve to map a 64-bit
11714 signature to a set of section numbers. Each section begins with a header,
11715 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11716 indexes, and a pool of 32-bit section numbers. The index sections will be
11717 aligned at 8-byte boundaries in the file.
11718
11719 The index section header consists of:
11720
11721 V, 32 bit version number
11722 -, 32 bits unused
11723 N, 32 bit number of compilation units or type units in the index
11724 M, 32 bit number of slots in the hash table
11725
11726 Numbers are recorded using the byte order of the application binary.
11727
11728 The hash table begins at offset 16 in the section, and consists of an array
11729 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11730 order of the application binary). Unused slots in the hash table are 0.
11731 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11732
11733 The parallel table begins immediately after the hash table
11734 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11735 array of 32-bit indexes (using the byte order of the application binary),
11736 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11737 table contains a 32-bit index into the pool of section numbers. For unused
11738 hash table slots, the corresponding entry in the parallel table will be 0.
11739
11740 The pool of section numbers begins immediately following the hash table
11741 (at offset 16 + 12 * M from the beginning of the section). The pool of
11742 section numbers consists of an array of 32-bit words (using the byte order
11743 of the application binary). Each item in the array is indexed starting
11744 from 0. The hash table entry provides the index of the first section
11745 number in the set. Additional section numbers in the set follow, and the
11746 set is terminated by a 0 entry (section number 0 is not used in ELF).
11747
11748 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11749 section must be the first entry in the set, and the .debug_abbrev.dwo must
11750 be the second entry. Other members of the set may follow in any order.
11751
11752 ---
11753
11754 DWP Versions 2 and 5:
11755
11756 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
11757 and the entries in the index tables are now offsets into these sections.
11758 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11759 section.
11760
11761 Index Section Contents:
11762 Header
11763 Hash Table of Signatures dwp_hash_table.hash_table
11764 Parallel Table of Indices dwp_hash_table.unit_table
11765 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11766 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
11767
11768 The index section header consists of:
11769
11770 V, 32 bit version number
11771 L, 32 bit number of columns in the table of section offsets
11772 N, 32 bit number of compilation units or type units in the index
11773 M, 32 bit number of slots in the hash table
11774
11775 Numbers are recorded using the byte order of the application binary.
11776
11777 The hash table has the same format as version 1.
11778 The parallel table of indices has the same format as version 1,
11779 except that the entries are origin-1 indices into the table of sections
11780 offsets and the table of section sizes.
11781
11782 The table of offsets begins immediately following the parallel table
11783 (at offset 16 + 12 * M from the beginning of the section). The table is
11784 a two-dimensional array of 32-bit words (using the byte order of the
11785 application binary), with L columns and N+1 rows, in row-major order.
11786 Each row in the array is indexed starting from 0. The first row provides
11787 a key to the remaining rows: each column in this row provides an identifier
11788 for a debug section, and the offsets in the same column of subsequent rows
11789 refer to that section. The section identifiers for Version 2 are:
11790
11791 DW_SECT_INFO 1 .debug_info.dwo
11792 DW_SECT_TYPES 2 .debug_types.dwo
11793 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11794 DW_SECT_LINE 4 .debug_line.dwo
11795 DW_SECT_LOC 5 .debug_loc.dwo
11796 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11797 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11798 DW_SECT_MACRO 8 .debug_macro.dwo
11799
11800 The section identifiers for Version 5 are:
11801
11802 DW_SECT_INFO_V5 1 .debug_info.dwo
11803 DW_SECT_RESERVED_V5 2 --
11804 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11805 DW_SECT_LINE_V5 4 .debug_line.dwo
11806 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11807 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11808 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11809 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11810
11811 The offsets provided by the CU and TU index sections are the base offsets
11812 for the contributions made by each CU or TU to the corresponding section
11813 in the package file. Each CU and TU header contains an abbrev_offset
11814 field, used to find the abbreviations table for that CU or TU within the
11815 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11816 be interpreted as relative to the base offset given in the index section.
11817 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11818 should be interpreted as relative to the base offset for .debug_line.dwo,
11819 and offsets into other debug sections obtained from DWARF attributes should
11820 also be interpreted as relative to the corresponding base offset.
11821
11822 The table of sizes begins immediately following the table of offsets.
11823 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11824 with L columns and N rows, in row-major order. Each row in the array is
11825 indexed starting from 1 (row 0 is shared by the two tables).
11826
11827 ---
11828
11829 Hash table lookup is handled the same in version 1 and 2:
11830
11831 We assume that N and M will not exceed 2^32 - 1.
11832 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11833
11834 Given a 64-bit compilation unit signature or a type signature S, an entry
11835 in the hash table is located as follows:
11836
11837 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11838 the low-order k bits all set to 1.
11839
11840 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11841
11842 3) If the hash table entry at index H matches the signature, use that
11843 entry. If the hash table entry at index H is unused (all zeroes),
11844 terminate the search: the signature is not present in the table.
11845
11846 4) Let H = (H + H') modulo M. Repeat at Step 3.
11847
11848 Because M > N and H' and M are relatively prime, the search is guaranteed
11849 to stop at an unused slot or find the match. */
11850
11851 /* Create a hash table to map DWO IDs to their CU/TU entry in
11852 .debug_{info,types}.dwo in DWP_FILE.
11853 Returns NULL if there isn't one.
11854 Note: This function processes DWP files only, not DWO files. */
11855
11856 static struct dwp_hash_table *
11857 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11858 struct dwp_file *dwp_file, int is_debug_types)
11859 {
11860 struct objfile *objfile = per_objfile->objfile;
11861 bfd *dbfd = dwp_file->dbfd.get ();
11862 const gdb_byte *index_ptr, *index_end;
11863 struct dwarf2_section_info *index;
11864 uint32_t version, nr_columns, nr_units, nr_slots;
11865 struct dwp_hash_table *htab;
11866
11867 if (is_debug_types)
11868 index = &dwp_file->sections.tu_index;
11869 else
11870 index = &dwp_file->sections.cu_index;
11871
11872 if (index->empty ())
11873 return NULL;
11874 index->read (objfile);
11875
11876 index_ptr = index->buffer;
11877 index_end = index_ptr + index->size;
11878
11879 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11880 For now it's safe to just read 4 bytes (particularly as it's difficult to
11881 tell if you're dealing with Version 5 before you've read the version). */
11882 version = read_4_bytes (dbfd, index_ptr);
11883 index_ptr += 4;
11884 if (version == 2 || version == 5)
11885 nr_columns = read_4_bytes (dbfd, index_ptr);
11886 else
11887 nr_columns = 0;
11888 index_ptr += 4;
11889 nr_units = read_4_bytes (dbfd, index_ptr);
11890 index_ptr += 4;
11891 nr_slots = read_4_bytes (dbfd, index_ptr);
11892 index_ptr += 4;
11893
11894 if (version != 1 && version != 2 && version != 5)
11895 {
11896 error (_("Dwarf Error: unsupported DWP file version (%s)"
11897 " [in module %s]"),
11898 pulongest (version), dwp_file->name);
11899 }
11900 if (nr_slots != (nr_slots & -nr_slots))
11901 {
11902 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11903 " is not power of 2 [in module %s]"),
11904 pulongest (nr_slots), dwp_file->name);
11905 }
11906
11907 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11908 htab->version = version;
11909 htab->nr_columns = nr_columns;
11910 htab->nr_units = nr_units;
11911 htab->nr_slots = nr_slots;
11912 htab->hash_table = index_ptr;
11913 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11914
11915 /* Exit early if the table is empty. */
11916 if (nr_slots == 0 || nr_units == 0
11917 || (version == 2 && nr_columns == 0)
11918 || (version == 5 && nr_columns == 0))
11919 {
11920 /* All must be zero. */
11921 if (nr_slots != 0 || nr_units != 0
11922 || (version == 2 && nr_columns != 0)
11923 || (version == 5 && nr_columns != 0))
11924 {
11925 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11926 " all zero [in modules %s]"),
11927 dwp_file->name);
11928 }
11929 return htab;
11930 }
11931
11932 if (version == 1)
11933 {
11934 htab->section_pool.v1.indices =
11935 htab->unit_table + sizeof (uint32_t) * nr_slots;
11936 /* It's harder to decide whether the section is too small in v1.
11937 V1 is deprecated anyway so we punt. */
11938 }
11939 else if (version == 2)
11940 {
11941 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11942 int *ids = htab->section_pool.v2.section_ids;
11943 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11944 /* Reverse map for error checking. */
11945 int ids_seen[DW_SECT_MAX + 1];
11946 int i;
11947
11948 if (nr_columns < 2)
11949 {
11950 error (_("Dwarf Error: bad DWP hash table, too few columns"
11951 " in section table [in module %s]"),
11952 dwp_file->name);
11953 }
11954 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11955 {
11956 error (_("Dwarf Error: bad DWP hash table, too many columns"
11957 " in section table [in module %s]"),
11958 dwp_file->name);
11959 }
11960 memset (ids, 255, sizeof_ids);
11961 memset (ids_seen, 255, sizeof (ids_seen));
11962 for (i = 0; i < nr_columns; ++i)
11963 {
11964 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11965
11966 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11967 {
11968 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11969 " in section table [in module %s]"),
11970 id, dwp_file->name);
11971 }
11972 if (ids_seen[id] != -1)
11973 {
11974 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11975 " id %d in section table [in module %s]"),
11976 id, dwp_file->name);
11977 }
11978 ids_seen[id] = i;
11979 ids[i] = id;
11980 }
11981 /* Must have exactly one info or types section. */
11982 if (((ids_seen[DW_SECT_INFO] != -1)
11983 + (ids_seen[DW_SECT_TYPES] != -1))
11984 != 1)
11985 {
11986 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11987 " DWO info/types section [in module %s]"),
11988 dwp_file->name);
11989 }
11990 /* Must have an abbrev section. */
11991 if (ids_seen[DW_SECT_ABBREV] == -1)
11992 {
11993 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11994 " section [in module %s]"),
11995 dwp_file->name);
11996 }
11997 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11998 htab->section_pool.v2.sizes =
11999 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12000 * nr_units * nr_columns);
12001 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12002 * nr_units * nr_columns))
12003 > index_end)
12004 {
12005 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12006 " [in module %s]"),
12007 dwp_file->name);
12008 }
12009 }
12010 else /* version == 5 */
12011 {
12012 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12013 int *ids = htab->section_pool.v5.section_ids;
12014 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
12015 /* Reverse map for error checking. */
12016 int ids_seen[DW_SECT_MAX_V5 + 1];
12017
12018 if (nr_columns < 2)
12019 {
12020 error (_("Dwarf Error: bad DWP hash table, too few columns"
12021 " in section table [in module %s]"),
12022 dwp_file->name);
12023 }
12024 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
12025 {
12026 error (_("Dwarf Error: bad DWP hash table, too many columns"
12027 " in section table [in module %s]"),
12028 dwp_file->name);
12029 }
12030 memset (ids, 255, sizeof_ids);
12031 memset (ids_seen, 255, sizeof (ids_seen));
12032 for (int i = 0; i < nr_columns; ++i)
12033 {
12034 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12035
12036 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
12037 {
12038 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12039 " in section table [in module %s]"),
12040 id, dwp_file->name);
12041 }
12042 if (ids_seen[id] != -1)
12043 {
12044 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12045 " id %d in section table [in module %s]"),
12046 id, dwp_file->name);
12047 }
12048 ids_seen[id] = i;
12049 ids[i] = id;
12050 }
12051 /* Must have seen an info section. */
12052 if (ids_seen[DW_SECT_INFO_V5] == -1)
12053 {
12054 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12055 " DWO info/types section [in module %s]"),
12056 dwp_file->name);
12057 }
12058 /* Must have an abbrev section. */
12059 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
12060 {
12061 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12062 " section [in module %s]"),
12063 dwp_file->name);
12064 }
12065 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12066 htab->section_pool.v5.sizes
12067 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
12068 * nr_units * nr_columns);
12069 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
12070 * nr_units * nr_columns))
12071 > index_end)
12072 {
12073 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12074 " [in module %s]"),
12075 dwp_file->name);
12076 }
12077 }
12078
12079 return htab;
12080 }
12081
12082 /* Update SECTIONS with the data from SECTP.
12083
12084 This function is like the other "locate" section routines, but in
12085 this context the sections to read comes from the DWP V1 hash table,
12086 not the full ELF section table.
12087
12088 The result is non-zero for success, or zero if an error was found. */
12089
12090 static int
12091 locate_v1_virtual_dwo_sections (asection *sectp,
12092 struct virtual_v1_dwo_sections *sections)
12093 {
12094 const struct dwop_section_names *names = &dwop_section_names;
12095
12096 if (names->abbrev_dwo.matches (sectp->name))
12097 {
12098 /* There can be only one. */
12099 if (sections->abbrev.s.section != NULL)
12100 return 0;
12101 sections->abbrev.s.section = sectp;
12102 sections->abbrev.size = bfd_section_size (sectp);
12103 }
12104 else if (names->info_dwo.matches (sectp->name)
12105 || names->types_dwo.matches (sectp->name))
12106 {
12107 /* There can be only one. */
12108 if (sections->info_or_types.s.section != NULL)
12109 return 0;
12110 sections->info_or_types.s.section = sectp;
12111 sections->info_or_types.size = bfd_section_size (sectp);
12112 }
12113 else if (names->line_dwo.matches (sectp->name))
12114 {
12115 /* There can be only one. */
12116 if (sections->line.s.section != NULL)
12117 return 0;
12118 sections->line.s.section = sectp;
12119 sections->line.size = bfd_section_size (sectp);
12120 }
12121 else if (names->loc_dwo.matches (sectp->name))
12122 {
12123 /* There can be only one. */
12124 if (sections->loc.s.section != NULL)
12125 return 0;
12126 sections->loc.s.section = sectp;
12127 sections->loc.size = bfd_section_size (sectp);
12128 }
12129 else if (names->macinfo_dwo.matches (sectp->name))
12130 {
12131 /* There can be only one. */
12132 if (sections->macinfo.s.section != NULL)
12133 return 0;
12134 sections->macinfo.s.section = sectp;
12135 sections->macinfo.size = bfd_section_size (sectp);
12136 }
12137 else if (names->macro_dwo.matches (sectp->name))
12138 {
12139 /* There can be only one. */
12140 if (sections->macro.s.section != NULL)
12141 return 0;
12142 sections->macro.s.section = sectp;
12143 sections->macro.size = bfd_section_size (sectp);
12144 }
12145 else if (names->str_offsets_dwo.matches (sectp->name))
12146 {
12147 /* There can be only one. */
12148 if (sections->str_offsets.s.section != NULL)
12149 return 0;
12150 sections->str_offsets.s.section = sectp;
12151 sections->str_offsets.size = bfd_section_size (sectp);
12152 }
12153 else
12154 {
12155 /* No other kind of section is valid. */
12156 return 0;
12157 }
12158
12159 return 1;
12160 }
12161
12162 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12163 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12164 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12165 This is for DWP version 1 files. */
12166
12167 static struct dwo_unit *
12168 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
12169 struct dwp_file *dwp_file,
12170 uint32_t unit_index,
12171 const char *comp_dir,
12172 ULONGEST signature, int is_debug_types)
12173 {
12174 const struct dwp_hash_table *dwp_htab =
12175 is_debug_types ? dwp_file->tus : dwp_file->cus;
12176 bfd *dbfd = dwp_file->dbfd.get ();
12177 const char *kind = is_debug_types ? "TU" : "CU";
12178 struct dwo_file *dwo_file;
12179 struct dwo_unit *dwo_unit;
12180 struct virtual_v1_dwo_sections sections;
12181 void **dwo_file_slot;
12182 int i;
12183
12184 gdb_assert (dwp_file->version == 1);
12185
12186 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
12187 kind, pulongest (unit_index), hex_string (signature),
12188 dwp_file->name);
12189
12190 /* Fetch the sections of this DWO unit.
12191 Put a limit on the number of sections we look for so that bad data
12192 doesn't cause us to loop forever. */
12193
12194 #define MAX_NR_V1_DWO_SECTIONS \
12195 (1 /* .debug_info or .debug_types */ \
12196 + 1 /* .debug_abbrev */ \
12197 + 1 /* .debug_line */ \
12198 + 1 /* .debug_loc */ \
12199 + 1 /* .debug_str_offsets */ \
12200 + 1 /* .debug_macro or .debug_macinfo */ \
12201 + 1 /* trailing zero */)
12202
12203 memset (&sections, 0, sizeof (sections));
12204
12205 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12206 {
12207 asection *sectp;
12208 uint32_t section_nr =
12209 read_4_bytes (dbfd,
12210 dwp_htab->section_pool.v1.indices
12211 + (unit_index + i) * sizeof (uint32_t));
12212
12213 if (section_nr == 0)
12214 break;
12215 if (section_nr >= dwp_file->num_sections)
12216 {
12217 error (_("Dwarf Error: bad DWP hash table, section number too large"
12218 " [in module %s]"),
12219 dwp_file->name);
12220 }
12221
12222 sectp = dwp_file->elf_sections[section_nr];
12223 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12224 {
12225 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12226 " [in module %s]"),
12227 dwp_file->name);
12228 }
12229 }
12230
12231 if (i < 2
12232 || sections.info_or_types.empty ()
12233 || sections.abbrev.empty ())
12234 {
12235 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12236 " [in module %s]"),
12237 dwp_file->name);
12238 }
12239 if (i == MAX_NR_V1_DWO_SECTIONS)
12240 {
12241 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12242 " [in module %s]"),
12243 dwp_file->name);
12244 }
12245
12246 /* It's easier for the rest of the code if we fake a struct dwo_file and
12247 have dwo_unit "live" in that. At least for now.
12248
12249 The DWP file can be made up of a random collection of CUs and TUs.
12250 However, for each CU + set of TUs that came from the same original DWO
12251 file, we can combine them back into a virtual DWO file to save space
12252 (fewer struct dwo_file objects to allocate). Remember that for really
12253 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12254
12255 std::string virtual_dwo_name =
12256 string_printf ("virtual-dwo/%d-%d-%d-%d",
12257 sections.abbrev.get_id (),
12258 sections.line.get_id (),
12259 sections.loc.get_id (),
12260 sections.str_offsets.get_id ());
12261 /* Can we use an existing virtual DWO file? */
12262 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12263 comp_dir);
12264 /* Create one if necessary. */
12265 if (*dwo_file_slot == NULL)
12266 {
12267 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12268 virtual_dwo_name.c_str ());
12269
12270 dwo_file = new struct dwo_file;
12271 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12272 dwo_file->comp_dir = comp_dir;
12273 dwo_file->sections.abbrev = sections.abbrev;
12274 dwo_file->sections.line = sections.line;
12275 dwo_file->sections.loc = sections.loc;
12276 dwo_file->sections.macinfo = sections.macinfo;
12277 dwo_file->sections.macro = sections.macro;
12278 dwo_file->sections.str_offsets = sections.str_offsets;
12279 /* The "str" section is global to the entire DWP file. */
12280 dwo_file->sections.str = dwp_file->sections.str;
12281 /* The info or types section is assigned below to dwo_unit,
12282 there's no need to record it in dwo_file.
12283 Also, we can't simply record type sections in dwo_file because
12284 we record a pointer into the vector in dwo_unit. As we collect more
12285 types we'll grow the vector and eventually have to reallocate space
12286 for it, invalidating all copies of pointers into the previous
12287 contents. */
12288 *dwo_file_slot = dwo_file;
12289 }
12290 else
12291 {
12292 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12293 virtual_dwo_name.c_str ());
12294
12295 dwo_file = (struct dwo_file *) *dwo_file_slot;
12296 }
12297
12298 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12299 dwo_unit->dwo_file = dwo_file;
12300 dwo_unit->signature = signature;
12301 dwo_unit->section =
12302 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12303 *dwo_unit->section = sections.info_or_types;
12304 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12305
12306 return dwo_unit;
12307 }
12308
12309 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
12310 simplify them. Given a pointer to the containing section SECTION, and
12311 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
12312 virtual section of just that piece. */
12313
12314 static struct dwarf2_section_info
12315 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
12316 struct dwarf2_section_info *section,
12317 bfd_size_type offset, bfd_size_type size)
12318 {
12319 struct dwarf2_section_info result;
12320 asection *sectp;
12321
12322 gdb_assert (section != NULL);
12323 gdb_assert (!section->is_virtual);
12324
12325 memset (&result, 0, sizeof (result));
12326 result.s.containing_section = section;
12327 result.is_virtual = true;
12328
12329 if (size == 0)
12330 return result;
12331
12332 sectp = section->get_bfd_section ();
12333
12334 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12335 bounds of the real section. This is a pretty-rare event, so just
12336 flag an error (easier) instead of a warning and trying to cope. */
12337 if (sectp == NULL
12338 || offset + size > bfd_section_size (sectp))
12339 {
12340 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
12341 " in section %s [in module %s]"),
12342 sectp ? bfd_section_name (sectp) : "<unknown>",
12343 objfile_name (per_objfile->objfile));
12344 }
12345
12346 result.virtual_offset = offset;
12347 result.size = size;
12348 return result;
12349 }
12350
12351 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12352 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12353 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12354 This is for DWP version 2 files. */
12355
12356 static struct dwo_unit *
12357 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
12358 struct dwp_file *dwp_file,
12359 uint32_t unit_index,
12360 const char *comp_dir,
12361 ULONGEST signature, int is_debug_types)
12362 {
12363 const struct dwp_hash_table *dwp_htab =
12364 is_debug_types ? dwp_file->tus : dwp_file->cus;
12365 bfd *dbfd = dwp_file->dbfd.get ();
12366 const char *kind = is_debug_types ? "TU" : "CU";
12367 struct dwo_file *dwo_file;
12368 struct dwo_unit *dwo_unit;
12369 struct virtual_v2_or_v5_dwo_sections sections;
12370 void **dwo_file_slot;
12371 int i;
12372
12373 gdb_assert (dwp_file->version == 2);
12374
12375 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
12376 kind, pulongest (unit_index), hex_string (signature),
12377 dwp_file->name);
12378
12379 /* Fetch the section offsets of this DWO unit. */
12380
12381 memset (&sections, 0, sizeof (sections));
12382
12383 for (i = 0; i < dwp_htab->nr_columns; ++i)
12384 {
12385 uint32_t offset = read_4_bytes (dbfd,
12386 dwp_htab->section_pool.v2.offsets
12387 + (((unit_index - 1) * dwp_htab->nr_columns
12388 + i)
12389 * sizeof (uint32_t)));
12390 uint32_t size = read_4_bytes (dbfd,
12391 dwp_htab->section_pool.v2.sizes
12392 + (((unit_index - 1) * dwp_htab->nr_columns
12393 + i)
12394 * sizeof (uint32_t)));
12395
12396 switch (dwp_htab->section_pool.v2.section_ids[i])
12397 {
12398 case DW_SECT_INFO:
12399 case DW_SECT_TYPES:
12400 sections.info_or_types_offset = offset;
12401 sections.info_or_types_size = size;
12402 break;
12403 case DW_SECT_ABBREV:
12404 sections.abbrev_offset = offset;
12405 sections.abbrev_size = size;
12406 break;
12407 case DW_SECT_LINE:
12408 sections.line_offset = offset;
12409 sections.line_size = size;
12410 break;
12411 case DW_SECT_LOC:
12412 sections.loc_offset = offset;
12413 sections.loc_size = size;
12414 break;
12415 case DW_SECT_STR_OFFSETS:
12416 sections.str_offsets_offset = offset;
12417 sections.str_offsets_size = size;
12418 break;
12419 case DW_SECT_MACINFO:
12420 sections.macinfo_offset = offset;
12421 sections.macinfo_size = size;
12422 break;
12423 case DW_SECT_MACRO:
12424 sections.macro_offset = offset;
12425 sections.macro_size = size;
12426 break;
12427 }
12428 }
12429
12430 /* It's easier for the rest of the code if we fake a struct dwo_file and
12431 have dwo_unit "live" in that. At least for now.
12432
12433 The DWP file can be made up of a random collection of CUs and TUs.
12434 However, for each CU + set of TUs that came from the same original DWO
12435 file, we can combine them back into a virtual DWO file to save space
12436 (fewer struct dwo_file objects to allocate). Remember that for really
12437 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12438
12439 std::string virtual_dwo_name =
12440 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12441 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12442 (long) (sections.line_size ? sections.line_offset : 0),
12443 (long) (sections.loc_size ? sections.loc_offset : 0),
12444 (long) (sections.str_offsets_size
12445 ? sections.str_offsets_offset : 0));
12446 /* Can we use an existing virtual DWO file? */
12447 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12448 comp_dir);
12449 /* Create one if necessary. */
12450 if (*dwo_file_slot == NULL)
12451 {
12452 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12453 virtual_dwo_name.c_str ());
12454
12455 dwo_file = new struct dwo_file;
12456 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12457 dwo_file->comp_dir = comp_dir;
12458 dwo_file->sections.abbrev =
12459 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12460 sections.abbrev_offset,
12461 sections.abbrev_size);
12462 dwo_file->sections.line =
12463 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12464 sections.line_offset,
12465 sections.line_size);
12466 dwo_file->sections.loc =
12467 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12468 sections.loc_offset, sections.loc_size);
12469 dwo_file->sections.macinfo =
12470 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12471 sections.macinfo_offset,
12472 sections.macinfo_size);
12473 dwo_file->sections.macro =
12474 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12475 sections.macro_offset,
12476 sections.macro_size);
12477 dwo_file->sections.str_offsets =
12478 create_dwp_v2_or_v5_section (per_objfile,
12479 &dwp_file->sections.str_offsets,
12480 sections.str_offsets_offset,
12481 sections.str_offsets_size);
12482 /* The "str" section is global to the entire DWP file. */
12483 dwo_file->sections.str = dwp_file->sections.str;
12484 /* The info or types section is assigned below to dwo_unit,
12485 there's no need to record it in dwo_file.
12486 Also, we can't simply record type sections in dwo_file because
12487 we record a pointer into the vector in dwo_unit. As we collect more
12488 types we'll grow the vector and eventually have to reallocate space
12489 for it, invalidating all copies of pointers into the previous
12490 contents. */
12491 *dwo_file_slot = dwo_file;
12492 }
12493 else
12494 {
12495 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12496 virtual_dwo_name.c_str ());
12497
12498 dwo_file = (struct dwo_file *) *dwo_file_slot;
12499 }
12500
12501 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12502 dwo_unit->dwo_file = dwo_file;
12503 dwo_unit->signature = signature;
12504 dwo_unit->section =
12505 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12506 *dwo_unit->section = create_dwp_v2_or_v5_section
12507 (per_objfile,
12508 is_debug_types
12509 ? &dwp_file->sections.types
12510 : &dwp_file->sections.info,
12511 sections.info_or_types_offset,
12512 sections.info_or_types_size);
12513 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12514
12515 return dwo_unit;
12516 }
12517
12518 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12519 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12520 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12521 This is for DWP version 5 files. */
12522
12523 static struct dwo_unit *
12524 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12525 struct dwp_file *dwp_file,
12526 uint32_t unit_index,
12527 const char *comp_dir,
12528 ULONGEST signature, int is_debug_types)
12529 {
12530 const struct dwp_hash_table *dwp_htab
12531 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12532 bfd *dbfd = dwp_file->dbfd.get ();
12533 const char *kind = is_debug_types ? "TU" : "CU";
12534 struct dwo_file *dwo_file;
12535 struct dwo_unit *dwo_unit;
12536 struct virtual_v2_or_v5_dwo_sections sections {};
12537 void **dwo_file_slot;
12538
12539 gdb_assert (dwp_file->version == 5);
12540
12541 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
12542 kind, pulongest (unit_index), hex_string (signature),
12543 dwp_file->name);
12544
12545 /* Fetch the section offsets of this DWO unit. */
12546
12547 /* memset (&sections, 0, sizeof (sections)); */
12548
12549 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12550 {
12551 uint32_t offset = read_4_bytes (dbfd,
12552 dwp_htab->section_pool.v5.offsets
12553 + (((unit_index - 1)
12554 * dwp_htab->nr_columns
12555 + i)
12556 * sizeof (uint32_t)));
12557 uint32_t size = read_4_bytes (dbfd,
12558 dwp_htab->section_pool.v5.sizes
12559 + (((unit_index - 1) * dwp_htab->nr_columns
12560 + i)
12561 * sizeof (uint32_t)));
12562
12563 switch (dwp_htab->section_pool.v5.section_ids[i])
12564 {
12565 case DW_SECT_ABBREV_V5:
12566 sections.abbrev_offset = offset;
12567 sections.abbrev_size = size;
12568 break;
12569 case DW_SECT_INFO_V5:
12570 sections.info_or_types_offset = offset;
12571 sections.info_or_types_size = size;
12572 break;
12573 case DW_SECT_LINE_V5:
12574 sections.line_offset = offset;
12575 sections.line_size = size;
12576 break;
12577 case DW_SECT_LOCLISTS_V5:
12578 sections.loclists_offset = offset;
12579 sections.loclists_size = size;
12580 break;
12581 case DW_SECT_MACRO_V5:
12582 sections.macro_offset = offset;
12583 sections.macro_size = size;
12584 break;
12585 case DW_SECT_RNGLISTS_V5:
12586 sections.rnglists_offset = offset;
12587 sections.rnglists_size = size;
12588 break;
12589 case DW_SECT_STR_OFFSETS_V5:
12590 sections.str_offsets_offset = offset;
12591 sections.str_offsets_size = size;
12592 break;
12593 case DW_SECT_RESERVED_V5:
12594 default:
12595 break;
12596 }
12597 }
12598
12599 /* It's easier for the rest of the code if we fake a struct dwo_file and
12600 have dwo_unit "live" in that. At least for now.
12601
12602 The DWP file can be made up of a random collection of CUs and TUs.
12603 However, for each CU + set of TUs that came from the same original DWO
12604 file, we can combine them back into a virtual DWO file to save space
12605 (fewer struct dwo_file objects to allocate). Remember that for really
12606 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12607
12608 std::string virtual_dwo_name =
12609 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
12610 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12611 (long) (sections.line_size ? sections.line_offset : 0),
12612 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12613 (long) (sections.str_offsets_size
12614 ? sections.str_offsets_offset : 0),
12615 (long) (sections.macro_size ? sections.macro_offset : 0),
12616 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
12617 /* Can we use an existing virtual DWO file? */
12618 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
12619 virtual_dwo_name.c_str (),
12620 comp_dir);
12621 /* Create one if necessary. */
12622 if (*dwo_file_slot == NULL)
12623 {
12624 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12625 virtual_dwo_name.c_str ());
12626
12627 dwo_file = new struct dwo_file;
12628 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12629 dwo_file->comp_dir = comp_dir;
12630 dwo_file->sections.abbrev =
12631 create_dwp_v2_or_v5_section (per_objfile,
12632 &dwp_file->sections.abbrev,
12633 sections.abbrev_offset,
12634 sections.abbrev_size);
12635 dwo_file->sections.line =
12636 create_dwp_v2_or_v5_section (per_objfile,
12637 &dwp_file->sections.line,
12638 sections.line_offset, sections.line_size);
12639 dwo_file->sections.macro =
12640 create_dwp_v2_or_v5_section (per_objfile,
12641 &dwp_file->sections.macro,
12642 sections.macro_offset,
12643 sections.macro_size);
12644 dwo_file->sections.loclists =
12645 create_dwp_v2_or_v5_section (per_objfile,
12646 &dwp_file->sections.loclists,
12647 sections.loclists_offset,
12648 sections.loclists_size);
12649 dwo_file->sections.rnglists =
12650 create_dwp_v2_or_v5_section (per_objfile,
12651 &dwp_file->sections.rnglists,
12652 sections.rnglists_offset,
12653 sections.rnglists_size);
12654 dwo_file->sections.str_offsets =
12655 create_dwp_v2_or_v5_section (per_objfile,
12656 &dwp_file->sections.str_offsets,
12657 sections.str_offsets_offset,
12658 sections.str_offsets_size);
12659 /* The "str" section is global to the entire DWP file. */
12660 dwo_file->sections.str = dwp_file->sections.str;
12661 /* The info or types section is assigned below to dwo_unit,
12662 there's no need to record it in dwo_file.
12663 Also, we can't simply record type sections in dwo_file because
12664 we record a pointer into the vector in dwo_unit. As we collect more
12665 types we'll grow the vector and eventually have to reallocate space
12666 for it, invalidating all copies of pointers into the previous
12667 contents. */
12668 *dwo_file_slot = dwo_file;
12669 }
12670 else
12671 {
12672 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12673 virtual_dwo_name.c_str ());
12674
12675 dwo_file = (struct dwo_file *) *dwo_file_slot;
12676 }
12677
12678 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12679 dwo_unit->dwo_file = dwo_file;
12680 dwo_unit->signature = signature;
12681 dwo_unit->section
12682 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12683 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
12684 &dwp_file->sections.info,
12685 sections.info_or_types_offset,
12686 sections.info_or_types_size);
12687 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12688
12689 return dwo_unit;
12690 }
12691
12692 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12693 Returns NULL if the signature isn't found. */
12694
12695 static struct dwo_unit *
12696 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12697 struct dwp_file *dwp_file, const char *comp_dir,
12698 ULONGEST signature, int is_debug_types)
12699 {
12700 const struct dwp_hash_table *dwp_htab =
12701 is_debug_types ? dwp_file->tus : dwp_file->cus;
12702 bfd *dbfd = dwp_file->dbfd.get ();
12703 uint32_t mask = dwp_htab->nr_slots - 1;
12704 uint32_t hash = signature & mask;
12705 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12706 unsigned int i;
12707 void **slot;
12708 struct dwo_unit find_dwo_cu;
12709
12710 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12711 find_dwo_cu.signature = signature;
12712 slot = htab_find_slot (is_debug_types
12713 ? dwp_file->loaded_tus.get ()
12714 : dwp_file->loaded_cus.get (),
12715 &find_dwo_cu, INSERT);
12716
12717 if (*slot != NULL)
12718 return (struct dwo_unit *) *slot;
12719
12720 /* Use a for loop so that we don't loop forever on bad debug info. */
12721 for (i = 0; i < dwp_htab->nr_slots; ++i)
12722 {
12723 ULONGEST signature_in_table;
12724
12725 signature_in_table =
12726 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12727 if (signature_in_table == signature)
12728 {
12729 uint32_t unit_index =
12730 read_4_bytes (dbfd,
12731 dwp_htab->unit_table + hash * sizeof (uint32_t));
12732
12733 if (dwp_file->version == 1)
12734 {
12735 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12736 unit_index, comp_dir,
12737 signature, is_debug_types);
12738 }
12739 else if (dwp_file->version == 2)
12740 {
12741 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12742 unit_index, comp_dir,
12743 signature, is_debug_types);
12744 }
12745 else /* version == 5 */
12746 {
12747 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12748 unit_index, comp_dir,
12749 signature, is_debug_types);
12750 }
12751 return (struct dwo_unit *) *slot;
12752 }
12753 if (signature_in_table == 0)
12754 return NULL;
12755 hash = (hash + hash2) & mask;
12756 }
12757
12758 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12759 " [in module %s]"),
12760 dwp_file->name);
12761 }
12762
12763 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12764 Open the file specified by FILE_NAME and hand it off to BFD for
12765 preliminary analysis. Return a newly initialized bfd *, which
12766 includes a canonicalized copy of FILE_NAME.
12767 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12768 SEARCH_CWD is true if the current directory is to be searched.
12769 It will be searched before debug-file-directory.
12770 If successful, the file is added to the bfd include table of the
12771 objfile's bfd (see gdb_bfd_record_inclusion).
12772 If unable to find/open the file, return NULL.
12773 NOTE: This function is derived from symfile_bfd_open. */
12774
12775 static gdb_bfd_ref_ptr
12776 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12777 const char *file_name, int is_dwp, int search_cwd)
12778 {
12779 int desc;
12780 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12781 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12782 to debug_file_directory. */
12783 const char *search_path;
12784 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12785
12786 gdb::unique_xmalloc_ptr<char> search_path_holder;
12787 if (search_cwd)
12788 {
12789 if (*debug_file_directory != '\0')
12790 {
12791 search_path_holder.reset (concat (".", dirname_separator_string,
12792 debug_file_directory,
12793 (char *) NULL));
12794 search_path = search_path_holder.get ();
12795 }
12796 else
12797 search_path = ".";
12798 }
12799 else
12800 search_path = debug_file_directory;
12801
12802 openp_flags flags = OPF_RETURN_REALPATH;
12803 if (is_dwp)
12804 flags |= OPF_SEARCH_IN_PATH;
12805
12806 gdb::unique_xmalloc_ptr<char> absolute_name;
12807 desc = openp (search_path, flags, file_name,
12808 O_RDONLY | O_BINARY, &absolute_name);
12809 if (desc < 0)
12810 return NULL;
12811
12812 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12813 gnutarget, desc));
12814 if (sym_bfd == NULL)
12815 return NULL;
12816 bfd_set_cacheable (sym_bfd.get (), 1);
12817
12818 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12819 return NULL;
12820
12821 /* Success. Record the bfd as having been included by the objfile's bfd.
12822 This is important because things like demangled_names_hash lives in the
12823 objfile's per_bfd space and may have references to things like symbol
12824 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12825 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12826
12827 return sym_bfd;
12828 }
12829
12830 /* Try to open DWO file FILE_NAME.
12831 COMP_DIR is the DW_AT_comp_dir attribute.
12832 The result is the bfd handle of the file.
12833 If there is a problem finding or opening the file, return NULL.
12834 Upon success, the canonicalized path of the file is stored in the bfd,
12835 same as symfile_bfd_open. */
12836
12837 static gdb_bfd_ref_ptr
12838 open_dwo_file (dwarf2_per_objfile *per_objfile,
12839 const char *file_name, const char *comp_dir)
12840 {
12841 if (IS_ABSOLUTE_PATH (file_name))
12842 return try_open_dwop_file (per_objfile, file_name,
12843 0 /*is_dwp*/, 0 /*search_cwd*/);
12844
12845 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12846
12847 if (comp_dir != NULL)
12848 {
12849 gdb::unique_xmalloc_ptr<char> path_to_try
12850 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12851
12852 /* NOTE: If comp_dir is a relative path, this will also try the
12853 search path, which seems useful. */
12854 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12855 0 /*is_dwp*/,
12856 1 /*search_cwd*/));
12857 if (abfd != NULL)
12858 return abfd;
12859 }
12860
12861 /* That didn't work, try debug-file-directory, which, despite its name,
12862 is a list of paths. */
12863
12864 if (*debug_file_directory == '\0')
12865 return NULL;
12866
12867 return try_open_dwop_file (per_objfile, file_name,
12868 0 /*is_dwp*/, 1 /*search_cwd*/);
12869 }
12870
12871 /* This function is mapped across the sections and remembers the offset and
12872 size of each of the DWO debugging sections we are interested in. */
12873
12874 static void
12875 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12876 dwo_sections *dwo_sections)
12877 {
12878 const struct dwop_section_names *names = &dwop_section_names;
12879
12880 if (names->abbrev_dwo.matches (sectp->name))
12881 {
12882 dwo_sections->abbrev.s.section = sectp;
12883 dwo_sections->abbrev.size = bfd_section_size (sectp);
12884 }
12885 else if (names->info_dwo.matches (sectp->name))
12886 {
12887 dwo_sections->info.s.section = sectp;
12888 dwo_sections->info.size = bfd_section_size (sectp);
12889 }
12890 else if (names->line_dwo.matches (sectp->name))
12891 {
12892 dwo_sections->line.s.section = sectp;
12893 dwo_sections->line.size = bfd_section_size (sectp);
12894 }
12895 else if (names->loc_dwo.matches (sectp->name))
12896 {
12897 dwo_sections->loc.s.section = sectp;
12898 dwo_sections->loc.size = bfd_section_size (sectp);
12899 }
12900 else if (names->loclists_dwo.matches (sectp->name))
12901 {
12902 dwo_sections->loclists.s.section = sectp;
12903 dwo_sections->loclists.size = bfd_section_size (sectp);
12904 }
12905 else if (names->macinfo_dwo.matches (sectp->name))
12906 {
12907 dwo_sections->macinfo.s.section = sectp;
12908 dwo_sections->macinfo.size = bfd_section_size (sectp);
12909 }
12910 else if (names->macro_dwo.matches (sectp->name))
12911 {
12912 dwo_sections->macro.s.section = sectp;
12913 dwo_sections->macro.size = bfd_section_size (sectp);
12914 }
12915 else if (names->rnglists_dwo.matches (sectp->name))
12916 {
12917 dwo_sections->rnglists.s.section = sectp;
12918 dwo_sections->rnglists.size = bfd_section_size (sectp);
12919 }
12920 else if (names->str_dwo.matches (sectp->name))
12921 {
12922 dwo_sections->str.s.section = sectp;
12923 dwo_sections->str.size = bfd_section_size (sectp);
12924 }
12925 else if (names->str_offsets_dwo.matches (sectp->name))
12926 {
12927 dwo_sections->str_offsets.s.section = sectp;
12928 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12929 }
12930 else if (names->types_dwo.matches (sectp->name))
12931 {
12932 struct dwarf2_section_info type_section;
12933
12934 memset (&type_section, 0, sizeof (type_section));
12935 type_section.s.section = sectp;
12936 type_section.size = bfd_section_size (sectp);
12937 dwo_sections->types.push_back (type_section);
12938 }
12939 }
12940
12941 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12942 by PER_CU. This is for the non-DWP case.
12943 The result is NULL if DWO_NAME can't be found. */
12944
12945 static struct dwo_file *
12946 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12947 const char *comp_dir)
12948 {
12949 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12950
12951 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12952 if (dbfd == NULL)
12953 {
12954 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12955
12956 return NULL;
12957 }
12958
12959 dwo_file_up dwo_file (new struct dwo_file);
12960 dwo_file->dwo_name = dwo_name;
12961 dwo_file->comp_dir = comp_dir;
12962 dwo_file->dbfd = std::move (dbfd);
12963
12964 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12965 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12966 &dwo_file->sections);
12967
12968 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12969 dwo_file->cus);
12970
12971 if (cu->per_cu->dwarf_version < 5)
12972 {
12973 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12974 dwo_file->sections.types, dwo_file->tus);
12975 }
12976 else
12977 {
12978 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12979 &dwo_file->sections.info, dwo_file->tus,
12980 rcuh_kind::COMPILE);
12981 }
12982
12983 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
12984
12985 return dwo_file.release ();
12986 }
12987
12988 /* This function is mapped across the sections and remembers the offset and
12989 size of each of the DWP debugging sections common to version 1 and 2 that
12990 we are interested in. */
12991
12992 static void
12993 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12994 dwp_file *dwp_file)
12995 {
12996 const struct dwop_section_names *names = &dwop_section_names;
12997 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12998
12999 /* Record the ELF section number for later lookup: this is what the
13000 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13001 gdb_assert (elf_section_nr < dwp_file->num_sections);
13002 dwp_file->elf_sections[elf_section_nr] = sectp;
13003
13004 /* Look for specific sections that we need. */
13005 if (names->str_dwo.matches (sectp->name))
13006 {
13007 dwp_file->sections.str.s.section = sectp;
13008 dwp_file->sections.str.size = bfd_section_size (sectp);
13009 }
13010 else if (names->cu_index.matches (sectp->name))
13011 {
13012 dwp_file->sections.cu_index.s.section = sectp;
13013 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
13014 }
13015 else if (names->tu_index.matches (sectp->name))
13016 {
13017 dwp_file->sections.tu_index.s.section = sectp;
13018 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
13019 }
13020 }
13021
13022 /* This function is mapped across the sections and remembers the offset and
13023 size of each of the DWP version 2 debugging sections that we are interested
13024 in. This is split into a separate function because we don't know if we
13025 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
13026
13027 static void
13028 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13029 {
13030 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13031 const struct dwop_section_names *names = &dwop_section_names;
13032 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13033
13034 /* Record the ELF section number for later lookup: this is what the
13035 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13036 gdb_assert (elf_section_nr < dwp_file->num_sections);
13037 dwp_file->elf_sections[elf_section_nr] = sectp;
13038
13039 /* Look for specific sections that we need. */
13040 if (names->abbrev_dwo.matches (sectp->name))
13041 {
13042 dwp_file->sections.abbrev.s.section = sectp;
13043 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13044 }
13045 else if (names->info_dwo.matches (sectp->name))
13046 {
13047 dwp_file->sections.info.s.section = sectp;
13048 dwp_file->sections.info.size = bfd_section_size (sectp);
13049 }
13050 else if (names->line_dwo.matches (sectp->name))
13051 {
13052 dwp_file->sections.line.s.section = sectp;
13053 dwp_file->sections.line.size = bfd_section_size (sectp);
13054 }
13055 else if (names->loc_dwo.matches (sectp->name))
13056 {
13057 dwp_file->sections.loc.s.section = sectp;
13058 dwp_file->sections.loc.size = bfd_section_size (sectp);
13059 }
13060 else if (names->macinfo_dwo.matches (sectp->name))
13061 {
13062 dwp_file->sections.macinfo.s.section = sectp;
13063 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
13064 }
13065 else if (names->macro_dwo.matches (sectp->name))
13066 {
13067 dwp_file->sections.macro.s.section = sectp;
13068 dwp_file->sections.macro.size = bfd_section_size (sectp);
13069 }
13070 else if (names->str_offsets_dwo.matches (sectp->name))
13071 {
13072 dwp_file->sections.str_offsets.s.section = sectp;
13073 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13074 }
13075 else if (names->types_dwo.matches (sectp->name))
13076 {
13077 dwp_file->sections.types.s.section = sectp;
13078 dwp_file->sections.types.size = bfd_section_size (sectp);
13079 }
13080 }
13081
13082 /* This function is mapped across the sections and remembers the offset and
13083 size of each of the DWP version 5 debugging sections that we are interested
13084 in. This is split into a separate function because we don't know if we
13085 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
13086
13087 static void
13088 dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13089 {
13090 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13091 const struct dwop_section_names *names = &dwop_section_names;
13092 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13093
13094 /* Record the ELF section number for later lookup: this is what the
13095 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13096 gdb_assert (elf_section_nr < dwp_file->num_sections);
13097 dwp_file->elf_sections[elf_section_nr] = sectp;
13098
13099 /* Look for specific sections that we need. */
13100 if (names->abbrev_dwo.matches (sectp->name))
13101 {
13102 dwp_file->sections.abbrev.s.section = sectp;
13103 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13104 }
13105 else if (names->info_dwo.matches (sectp->name))
13106 {
13107 dwp_file->sections.info.s.section = sectp;
13108 dwp_file->sections.info.size = bfd_section_size (sectp);
13109 }
13110 else if (names->line_dwo.matches (sectp->name))
13111 {
13112 dwp_file->sections.line.s.section = sectp;
13113 dwp_file->sections.line.size = bfd_section_size (sectp);
13114 }
13115 else if (names->loclists_dwo.matches (sectp->name))
13116 {
13117 dwp_file->sections.loclists.s.section = sectp;
13118 dwp_file->sections.loclists.size = bfd_section_size (sectp);
13119 }
13120 else if (names->macro_dwo.matches (sectp->name))
13121 {
13122 dwp_file->sections.macro.s.section = sectp;
13123 dwp_file->sections.macro.size = bfd_section_size (sectp);
13124 }
13125 else if (names->rnglists_dwo.matches (sectp->name))
13126 {
13127 dwp_file->sections.rnglists.s.section = sectp;
13128 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
13129 }
13130 else if (names->str_offsets_dwo.matches (sectp->name))
13131 {
13132 dwp_file->sections.str_offsets.s.section = sectp;
13133 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13134 }
13135 }
13136
13137 /* Hash function for dwp_file loaded CUs/TUs. */
13138
13139 static hashval_t
13140 hash_dwp_loaded_cutus (const void *item)
13141 {
13142 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13143
13144 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13145 return dwo_unit->signature;
13146 }
13147
13148 /* Equality function for dwp_file loaded CUs/TUs. */
13149
13150 static int
13151 eq_dwp_loaded_cutus (const void *a, const void *b)
13152 {
13153 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13154 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13155
13156 return dua->signature == dub->signature;
13157 }
13158
13159 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13160
13161 static htab_up
13162 allocate_dwp_loaded_cutus_table ()
13163 {
13164 return htab_up (htab_create_alloc (3,
13165 hash_dwp_loaded_cutus,
13166 eq_dwp_loaded_cutus,
13167 NULL, xcalloc, xfree));
13168 }
13169
13170 /* Try to open DWP file FILE_NAME.
13171 The result is the bfd handle of the file.
13172 If there is a problem finding or opening the file, return NULL.
13173 Upon success, the canonicalized path of the file is stored in the bfd,
13174 same as symfile_bfd_open. */
13175
13176 static gdb_bfd_ref_ptr
13177 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
13178 {
13179 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
13180 1 /*is_dwp*/,
13181 1 /*search_cwd*/));
13182 if (abfd != NULL)
13183 return abfd;
13184
13185 /* Work around upstream bug 15652.
13186 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13187 [Whether that's a "bug" is debatable, but it is getting in our way.]
13188 We have no real idea where the dwp file is, because gdb's realpath-ing
13189 of the executable's path may have discarded the needed info.
13190 [IWBN if the dwp file name was recorded in the executable, akin to
13191 .gnu_debuglink, but that doesn't exist yet.]
13192 Strip the directory from FILE_NAME and search again. */
13193 if (*debug_file_directory != '\0')
13194 {
13195 /* Don't implicitly search the current directory here.
13196 If the user wants to search "." to handle this case,
13197 it must be added to debug-file-directory. */
13198 return try_open_dwop_file (per_objfile, lbasename (file_name),
13199 1 /*is_dwp*/,
13200 0 /*search_cwd*/);
13201 }
13202
13203 return NULL;
13204 }
13205
13206 /* Initialize the use of the DWP file for the current objfile.
13207 By convention the name of the DWP file is ${objfile}.dwp.
13208 The result is NULL if it can't be found. */
13209
13210 static std::unique_ptr<struct dwp_file>
13211 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
13212 {
13213 struct objfile *objfile = per_objfile->objfile;
13214
13215 /* Try to find first .dwp for the binary file before any symbolic links
13216 resolving. */
13217
13218 /* If the objfile is a debug file, find the name of the real binary
13219 file and get the name of dwp file from there. */
13220 std::string dwp_name;
13221 if (objfile->separate_debug_objfile_backlink != NULL)
13222 {
13223 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13224 const char *backlink_basename = lbasename (backlink->original_name);
13225
13226 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13227 }
13228 else
13229 dwp_name = objfile->original_name;
13230
13231 dwp_name += ".dwp";
13232
13233 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
13234 if (dbfd == NULL
13235 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13236 {
13237 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13238 dwp_name = objfile_name (objfile);
13239 dwp_name += ".dwp";
13240 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
13241 }
13242
13243 if (dbfd == NULL)
13244 {
13245 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
13246
13247 return std::unique_ptr<dwp_file> ();
13248 }
13249
13250 const char *name = bfd_get_filename (dbfd.get ());
13251 std::unique_ptr<struct dwp_file> dwp_file
13252 (new struct dwp_file (name, std::move (dbfd)));
13253
13254 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13255 dwp_file->elf_sections =
13256 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
13257 dwp_file->num_sections, asection *);
13258
13259 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13260 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
13261 dwp_file.get ());
13262
13263 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
13264
13265 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
13266
13267 /* The DWP file version is stored in the hash table. Oh well. */
13268 if (dwp_file->cus && dwp_file->tus
13269 && dwp_file->cus->version != dwp_file->tus->version)
13270 {
13271 /* Technically speaking, we should try to limp along, but this is
13272 pretty bizarre. We use pulongest here because that's the established
13273 portability solution (e.g, we cannot use %u for uint32_t). */
13274 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13275 " TU version %s [in DWP file %s]"),
13276 pulongest (dwp_file->cus->version),
13277 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13278 }
13279
13280 if (dwp_file->cus)
13281 dwp_file->version = dwp_file->cus->version;
13282 else if (dwp_file->tus)
13283 dwp_file->version = dwp_file->tus->version;
13284 else
13285 dwp_file->version = 2;
13286
13287 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13288 {
13289 if (dwp_file->version == 2)
13290 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
13291 dwp_file.get ());
13292 else
13293 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
13294 dwp_file.get ());
13295 }
13296
13297 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
13298 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
13299
13300 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
13301 dwarf_read_debug_printf (" %s CUs, %s TUs",
13302 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13303 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13304
13305 return dwp_file;
13306 }
13307
13308 /* Wrapper around open_and_init_dwp_file, only open it once. */
13309
13310 static struct dwp_file *
13311 get_dwp_file (dwarf2_per_objfile *per_objfile)
13312 {
13313 if (!per_objfile->per_bfd->dwp_checked)
13314 {
13315 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
13316 per_objfile->per_bfd->dwp_checked = 1;
13317 }
13318 return per_objfile->per_bfd->dwp_file.get ();
13319 }
13320
13321 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13322 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13323 or in the DWP file for the objfile, referenced by THIS_UNIT.
13324 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13325 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13326
13327 This is called, for example, when wanting to read a variable with a
13328 complex location. Therefore we don't want to do file i/o for every call.
13329 Therefore we don't want to look for a DWO file on every call.
13330 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13331 then we check if we've already seen DWO_NAME, and only THEN do we check
13332 for a DWO file.
13333
13334 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13335 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13336
13337 static struct dwo_unit *
13338 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13339 ULONGEST signature, int is_debug_types)
13340 {
13341 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13342 struct objfile *objfile = per_objfile->objfile;
13343 const char *kind = is_debug_types ? "TU" : "CU";
13344 void **dwo_file_slot;
13345 struct dwo_file *dwo_file;
13346 struct dwp_file *dwp_file;
13347
13348 /* First see if there's a DWP file.
13349 If we have a DWP file but didn't find the DWO inside it, don't
13350 look for the original DWO file. It makes gdb behave differently
13351 depending on whether one is debugging in the build tree. */
13352
13353 dwp_file = get_dwp_file (per_objfile);
13354 if (dwp_file != NULL)
13355 {
13356 const struct dwp_hash_table *dwp_htab =
13357 is_debug_types ? dwp_file->tus : dwp_file->cus;
13358
13359 if (dwp_htab != NULL)
13360 {
13361 struct dwo_unit *dwo_cutu =
13362 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
13363 is_debug_types);
13364
13365 if (dwo_cutu != NULL)
13366 {
13367 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
13368 kind, hex_string (signature),
13369 host_address_to_string (dwo_cutu));
13370
13371 return dwo_cutu;
13372 }
13373 }
13374 }
13375 else
13376 {
13377 /* No DWP file, look for the DWO file. */
13378
13379 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
13380 if (*dwo_file_slot == NULL)
13381 {
13382 /* Read in the file and build a table of the CUs/TUs it contains. */
13383 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
13384 }
13385 /* NOTE: This will be NULL if unable to open the file. */
13386 dwo_file = (struct dwo_file *) *dwo_file_slot;
13387
13388 if (dwo_file != NULL)
13389 {
13390 struct dwo_unit *dwo_cutu = NULL;
13391
13392 if (is_debug_types && dwo_file->tus)
13393 {
13394 struct dwo_unit find_dwo_cutu;
13395
13396 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13397 find_dwo_cutu.signature = signature;
13398 dwo_cutu
13399 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
13400 &find_dwo_cutu);
13401 }
13402 else if (!is_debug_types && dwo_file->cus)
13403 {
13404 struct dwo_unit find_dwo_cutu;
13405
13406 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13407 find_dwo_cutu.signature = signature;
13408 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
13409 &find_dwo_cutu);
13410 }
13411
13412 if (dwo_cutu != NULL)
13413 {
13414 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
13415 kind, dwo_name, hex_string (signature),
13416 host_address_to_string (dwo_cutu));
13417
13418 return dwo_cutu;
13419 }
13420 }
13421 }
13422
13423 /* We didn't find it. This could mean a dwo_id mismatch, or
13424 someone deleted the DWO/DWP file, or the search path isn't set up
13425 correctly to find the file. */
13426
13427 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
13428 kind, dwo_name, hex_string (signature));
13429
13430 /* This is a warning and not a complaint because it can be caused by
13431 pilot error (e.g., user accidentally deleting the DWO). */
13432 {
13433 /* Print the name of the DWP file if we looked there, helps the user
13434 better diagnose the problem. */
13435 std::string dwp_text;
13436
13437 if (dwp_file != NULL)
13438 dwp_text = string_printf (" [in DWP file %s]",
13439 lbasename (dwp_file->name));
13440
13441 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13442 " [in module %s]"),
13443 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13444 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
13445 }
13446 return NULL;
13447 }
13448
13449 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13450 See lookup_dwo_cutu_unit for details. */
13451
13452 static struct dwo_unit *
13453 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13454 ULONGEST signature)
13455 {
13456 gdb_assert (!cu->per_cu->is_debug_types);
13457
13458 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
13459 }
13460
13461 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13462 See lookup_dwo_cutu_unit for details. */
13463
13464 static struct dwo_unit *
13465 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
13466 {
13467 gdb_assert (cu->per_cu->is_debug_types);
13468
13469 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13470
13471 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
13472 }
13473
13474 /* Traversal function for queue_and_load_all_dwo_tus. */
13475
13476 static int
13477 queue_and_load_dwo_tu (void **slot, void *info)
13478 {
13479 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13480 dwarf2_cu *cu = (dwarf2_cu *) info;
13481 ULONGEST signature = dwo_unit->signature;
13482 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
13483
13484 if (sig_type != NULL)
13485 {
13486 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13487
13488 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13489 a real dependency of PER_CU on SIG_TYPE. That is detected later
13490 while processing PER_CU. */
13491 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
13492 load_full_type_unit (sig_cu, cu->per_objfile);
13493 cu->per_cu->imported_symtabs_push (sig_cu);
13494 }
13495
13496 return 1;
13497 }
13498
13499 /* Queue all TUs contained in the DWO of CU to be read in.
13500 The DWO may have the only definition of the type, though it may not be
13501 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13502 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13503
13504 static void
13505 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
13506 {
13507 struct dwo_unit *dwo_unit;
13508 struct dwo_file *dwo_file;
13509
13510 gdb_assert (cu != nullptr);
13511 gdb_assert (!cu->per_cu->is_debug_types);
13512 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
13513
13514 dwo_unit = cu->dwo_unit;
13515 gdb_assert (dwo_unit != NULL);
13516
13517 dwo_file = dwo_unit->dwo_file;
13518 if (dwo_file->tus != NULL)
13519 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
13520 }
13521
13522 /* Read in various DIEs. */
13523
13524 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13525 Inherit only the children of the DW_AT_abstract_origin DIE not being
13526 already referenced by DW_AT_abstract_origin from the children of the
13527 current DIE. */
13528
13529 static void
13530 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13531 {
13532 struct die_info *child_die;
13533 sect_offset *offsetp;
13534 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13535 struct die_info *origin_die;
13536 /* Iterator of the ORIGIN_DIE children. */
13537 struct die_info *origin_child_die;
13538 struct attribute *attr;
13539 struct dwarf2_cu *origin_cu;
13540 struct pending **origin_previous_list_in_scope;
13541
13542 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13543 if (!attr)
13544 return;
13545
13546 /* Note that following die references may follow to a die in a
13547 different cu. */
13548
13549 origin_cu = cu;
13550 origin_die = follow_die_ref (die, attr, &origin_cu);
13551
13552 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13553 symbols in. */
13554 origin_previous_list_in_scope = origin_cu->list_in_scope;
13555 origin_cu->list_in_scope = cu->list_in_scope;
13556
13557 if (die->tag != origin_die->tag
13558 && !(die->tag == DW_TAG_inlined_subroutine
13559 && origin_die->tag == DW_TAG_subprogram))
13560 complaint (_("DIE %s and its abstract origin %s have different tags"),
13561 sect_offset_str (die->sect_off),
13562 sect_offset_str (origin_die->sect_off));
13563
13564 std::vector<sect_offset> offsets;
13565
13566 for (child_die = die->child;
13567 child_die && child_die->tag;
13568 child_die = child_die->sibling)
13569 {
13570 struct die_info *child_origin_die;
13571 struct dwarf2_cu *child_origin_cu;
13572
13573 /* We are trying to process concrete instance entries:
13574 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13575 it's not relevant to our analysis here. i.e. detecting DIEs that are
13576 present in the abstract instance but not referenced in the concrete
13577 one. */
13578 if (child_die->tag == DW_TAG_call_site
13579 || child_die->tag == DW_TAG_GNU_call_site)
13580 continue;
13581
13582 /* For each CHILD_DIE, find the corresponding child of
13583 ORIGIN_DIE. If there is more than one layer of
13584 DW_AT_abstract_origin, follow them all; there shouldn't be,
13585 but GCC versions at least through 4.4 generate this (GCC PR
13586 40573). */
13587 child_origin_die = child_die;
13588 child_origin_cu = cu;
13589 while (1)
13590 {
13591 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13592 child_origin_cu);
13593 if (attr == NULL)
13594 break;
13595 child_origin_die = follow_die_ref (child_origin_die, attr,
13596 &child_origin_cu);
13597 }
13598
13599 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13600 counterpart may exist. */
13601 if (child_origin_die != child_die)
13602 {
13603 if (child_die->tag != child_origin_die->tag
13604 && !(child_die->tag == DW_TAG_inlined_subroutine
13605 && child_origin_die->tag == DW_TAG_subprogram))
13606 complaint (_("Child DIE %s and its abstract origin %s have "
13607 "different tags"),
13608 sect_offset_str (child_die->sect_off),
13609 sect_offset_str (child_origin_die->sect_off));
13610 if (child_origin_die->parent != origin_die)
13611 complaint (_("Child DIE %s and its abstract origin %s have "
13612 "different parents"),
13613 sect_offset_str (child_die->sect_off),
13614 sect_offset_str (child_origin_die->sect_off));
13615 else
13616 offsets.push_back (child_origin_die->sect_off);
13617 }
13618 }
13619 std::sort (offsets.begin (), offsets.end ());
13620 sect_offset *offsets_end = offsets.data () + offsets.size ();
13621 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13622 if (offsetp[-1] == *offsetp)
13623 complaint (_("Multiple children of DIE %s refer "
13624 "to DIE %s as their abstract origin"),
13625 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13626
13627 offsetp = offsets.data ();
13628 origin_child_die = origin_die->child;
13629 while (origin_child_die && origin_child_die->tag)
13630 {
13631 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13632 while (offsetp < offsets_end
13633 && *offsetp < origin_child_die->sect_off)
13634 offsetp++;
13635 if (offsetp >= offsets_end
13636 || *offsetp > origin_child_die->sect_off)
13637 {
13638 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13639 Check whether we're already processing ORIGIN_CHILD_DIE.
13640 This can happen with mutually referenced abstract_origins.
13641 PR 16581. */
13642 if (!origin_child_die->in_process)
13643 process_die (origin_child_die, origin_cu);
13644 }
13645 origin_child_die = origin_child_die->sibling;
13646 }
13647 origin_cu->list_in_scope = origin_previous_list_in_scope;
13648
13649 if (cu != origin_cu)
13650 compute_delayed_physnames (origin_cu);
13651 }
13652
13653 static void
13654 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13655 {
13656 struct objfile *objfile = cu->per_objfile->objfile;
13657 struct gdbarch *gdbarch = objfile->arch ();
13658 struct context_stack *newobj;
13659 CORE_ADDR lowpc;
13660 CORE_ADDR highpc;
13661 struct die_info *child_die;
13662 struct attribute *attr, *call_line, *call_file;
13663 const char *name;
13664 CORE_ADDR baseaddr;
13665 struct block *block;
13666 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13667 std::vector<struct symbol *> template_args;
13668 struct template_symbol *templ_func = NULL;
13669
13670 if (inlined_func)
13671 {
13672 /* If we do not have call site information, we can't show the
13673 caller of this inlined function. That's too confusing, so
13674 only use the scope for local variables. */
13675 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13676 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13677 if (call_line == NULL || call_file == NULL)
13678 {
13679 read_lexical_block_scope (die, cu);
13680 return;
13681 }
13682 }
13683
13684 baseaddr = objfile->text_section_offset ();
13685
13686 name = dwarf2_name (die, cu);
13687
13688 /* Ignore functions with missing or empty names. These are actually
13689 illegal according to the DWARF standard. */
13690 if (name == NULL)
13691 {
13692 complaint (_("missing name for subprogram DIE at %s"),
13693 sect_offset_str (die->sect_off));
13694 return;
13695 }
13696
13697 /* Ignore functions with missing or invalid low and high pc attributes. */
13698 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13699 <= PC_BOUNDS_INVALID)
13700 {
13701 attr = dwarf2_attr (die, DW_AT_external, cu);
13702 if (attr == nullptr || !attr->as_boolean ())
13703 complaint (_("cannot get low and high bounds "
13704 "for subprogram DIE at %s"),
13705 sect_offset_str (die->sect_off));
13706 return;
13707 }
13708
13709 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13710 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13711
13712 /* If we have any template arguments, then we must allocate a
13713 different sort of symbol. */
13714 for (child_die = die->child; child_die; child_die = child_die->sibling)
13715 {
13716 if (child_die->tag == DW_TAG_template_type_param
13717 || child_die->tag == DW_TAG_template_value_param)
13718 {
13719 templ_func = new (&objfile->objfile_obstack) template_symbol;
13720 templ_func->subclass = SYMBOL_TEMPLATE;
13721 break;
13722 }
13723 }
13724
13725 newobj = cu->get_builder ()->push_context (0, lowpc);
13726 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13727 (struct symbol *) templ_func);
13728
13729 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13730 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13731 cu->language);
13732
13733 /* If there is a location expression for DW_AT_frame_base, record
13734 it. */
13735 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13736 if (attr != nullptr)
13737 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13738
13739 /* If there is a location for the static link, record it. */
13740 newobj->static_link = NULL;
13741 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13742 if (attr != nullptr)
13743 {
13744 newobj->static_link
13745 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13746 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13747 cu->addr_type ());
13748 }
13749
13750 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13751
13752 if (die->child != NULL)
13753 {
13754 child_die = die->child;
13755 while (child_die && child_die->tag)
13756 {
13757 if (child_die->tag == DW_TAG_template_type_param
13758 || child_die->tag == DW_TAG_template_value_param)
13759 {
13760 struct symbol *arg = new_symbol (child_die, NULL, cu);
13761
13762 if (arg != NULL)
13763 template_args.push_back (arg);
13764 }
13765 else
13766 process_die (child_die, cu);
13767 child_die = child_die->sibling;
13768 }
13769 }
13770
13771 inherit_abstract_dies (die, cu);
13772
13773 /* If we have a DW_AT_specification, we might need to import using
13774 directives from the context of the specification DIE. See the
13775 comment in determine_prefix. */
13776 if (cu->language == language_cplus
13777 && dwarf2_attr (die, DW_AT_specification, cu))
13778 {
13779 struct dwarf2_cu *spec_cu = cu;
13780 struct die_info *spec_die = die_specification (die, &spec_cu);
13781
13782 while (spec_die)
13783 {
13784 child_die = spec_die->child;
13785 while (child_die && child_die->tag)
13786 {
13787 if (child_die->tag == DW_TAG_imported_module)
13788 process_die (child_die, spec_cu);
13789 child_die = child_die->sibling;
13790 }
13791
13792 /* In some cases, GCC generates specification DIEs that
13793 themselves contain DW_AT_specification attributes. */
13794 spec_die = die_specification (spec_die, &spec_cu);
13795 }
13796 }
13797
13798 struct context_stack cstk = cu->get_builder ()->pop_context ();
13799 /* Make a block for the local symbols within. */
13800 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13801 cstk.static_link, lowpc, highpc);
13802
13803 /* For C++, set the block's scope. */
13804 if ((cu->language == language_cplus
13805 || cu->language == language_fortran
13806 || cu->language == language_d
13807 || cu->language == language_rust)
13808 && cu->processing_has_namespace_info)
13809 block_set_scope (block, determine_prefix (die, cu),
13810 &objfile->objfile_obstack);
13811
13812 /* If we have address ranges, record them. */
13813 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13814
13815 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13816
13817 /* Attach template arguments to function. */
13818 if (!template_args.empty ())
13819 {
13820 gdb_assert (templ_func != NULL);
13821
13822 templ_func->n_template_arguments = template_args.size ();
13823 templ_func->template_arguments
13824 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13825 templ_func->n_template_arguments);
13826 memcpy (templ_func->template_arguments,
13827 template_args.data (),
13828 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13829
13830 /* Make sure that the symtab is set on the new symbols. Even
13831 though they don't appear in this symtab directly, other parts
13832 of gdb assume that symbols do, and this is reasonably
13833 true. */
13834 for (symbol *sym : template_args)
13835 symbol_set_symtab (sym, symbol_symtab (templ_func));
13836 }
13837
13838 /* In C++, we can have functions nested inside functions (e.g., when
13839 a function declares a class that has methods). This means that
13840 when we finish processing a function scope, we may need to go
13841 back to building a containing block's symbol lists. */
13842 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13843 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13844
13845 /* If we've finished processing a top-level function, subsequent
13846 symbols go in the file symbol list. */
13847 if (cu->get_builder ()->outermost_context_p ())
13848 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13849 }
13850
13851 /* Process all the DIES contained within a lexical block scope. Start
13852 a new scope, process the dies, and then close the scope. */
13853
13854 static void
13855 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13856 {
13857 struct objfile *objfile = cu->per_objfile->objfile;
13858 struct gdbarch *gdbarch = objfile->arch ();
13859 CORE_ADDR lowpc, highpc;
13860 struct die_info *child_die;
13861 CORE_ADDR baseaddr;
13862
13863 baseaddr = objfile->text_section_offset ();
13864
13865 /* Ignore blocks with missing or invalid low and high pc attributes. */
13866 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13867 as multiple lexical blocks? Handling children in a sane way would
13868 be nasty. Might be easier to properly extend generic blocks to
13869 describe ranges. */
13870 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13871 {
13872 case PC_BOUNDS_NOT_PRESENT:
13873 /* DW_TAG_lexical_block has no attributes, process its children as if
13874 there was no wrapping by that DW_TAG_lexical_block.
13875 GCC does no longer produces such DWARF since GCC r224161. */
13876 for (child_die = die->child;
13877 child_die != NULL && child_die->tag;
13878 child_die = child_die->sibling)
13879 {
13880 /* We might already be processing this DIE. This can happen
13881 in an unusual circumstance -- where a subroutine A
13882 appears lexically in another subroutine B, but A actually
13883 inlines B. The recursion is broken here, rather than in
13884 inherit_abstract_dies, because it seems better to simply
13885 drop concrete children here. */
13886 if (!child_die->in_process)
13887 process_die (child_die, cu);
13888 }
13889 return;
13890 case PC_BOUNDS_INVALID:
13891 return;
13892 }
13893 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13894 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13895
13896 cu->get_builder ()->push_context (0, lowpc);
13897 if (die->child != NULL)
13898 {
13899 child_die = die->child;
13900 while (child_die && child_die->tag)
13901 {
13902 process_die (child_die, cu);
13903 child_die = child_die->sibling;
13904 }
13905 }
13906 inherit_abstract_dies (die, cu);
13907 struct context_stack cstk = cu->get_builder ()->pop_context ();
13908
13909 if (*cu->get_builder ()->get_local_symbols () != NULL
13910 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13911 {
13912 struct block *block
13913 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13914 cstk.start_addr, highpc);
13915
13916 /* Note that recording ranges after traversing children, as we
13917 do here, means that recording a parent's ranges entails
13918 walking across all its children's ranges as they appear in
13919 the address map, which is quadratic behavior.
13920
13921 It would be nicer to record the parent's ranges before
13922 traversing its children, simply overriding whatever you find
13923 there. But since we don't even decide whether to create a
13924 block until after we've traversed its children, that's hard
13925 to do. */
13926 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13927 }
13928 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13929 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13930 }
13931
13932 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13933
13934 static void
13935 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13936 {
13937 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13938 struct objfile *objfile = per_objfile->objfile;
13939 struct gdbarch *gdbarch = objfile->arch ();
13940 CORE_ADDR pc, baseaddr;
13941 struct attribute *attr;
13942 struct call_site *call_site, call_site_local;
13943 void **slot;
13944 int nparams;
13945 struct die_info *child_die;
13946
13947 baseaddr = objfile->text_section_offset ();
13948
13949 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13950 if (attr == NULL)
13951 {
13952 /* This was a pre-DWARF-5 GNU extension alias
13953 for DW_AT_call_return_pc. */
13954 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13955 }
13956 if (!attr)
13957 {
13958 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13959 "DIE %s [in module %s]"),
13960 sect_offset_str (die->sect_off), objfile_name (objfile));
13961 return;
13962 }
13963 pc = attr->as_address () + baseaddr;
13964 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13965
13966 if (cu->call_site_htab == NULL)
13967 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13968 NULL, &objfile->objfile_obstack,
13969 hashtab_obstack_allocate, NULL);
13970 call_site_local.pc = pc;
13971 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13972 if (*slot != NULL)
13973 {
13974 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13975 "DIE %s [in module %s]"),
13976 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13977 objfile_name (objfile));
13978 return;
13979 }
13980
13981 /* Count parameters at the caller. */
13982
13983 nparams = 0;
13984 for (child_die = die->child; child_die && child_die->tag;
13985 child_die = child_die->sibling)
13986 {
13987 if (child_die->tag != DW_TAG_call_site_parameter
13988 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13989 {
13990 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13991 "DW_TAG_call_site child DIE %s [in module %s]"),
13992 child_die->tag, sect_offset_str (child_die->sect_off),
13993 objfile_name (objfile));
13994 continue;
13995 }
13996
13997 nparams++;
13998 }
13999
14000 call_site
14001 = ((struct call_site *)
14002 obstack_alloc (&objfile->objfile_obstack,
14003 sizeof (*call_site)
14004 + (sizeof (*call_site->parameter) * (nparams - 1))));
14005 *slot = call_site;
14006 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14007 call_site->pc = pc;
14008
14009 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14010 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14011 {
14012 struct die_info *func_die;
14013
14014 /* Skip also over DW_TAG_inlined_subroutine. */
14015 for (func_die = die->parent;
14016 func_die && func_die->tag != DW_TAG_subprogram
14017 && func_die->tag != DW_TAG_subroutine_type;
14018 func_die = func_die->parent);
14019
14020 /* DW_AT_call_all_calls is a superset
14021 of DW_AT_call_all_tail_calls. */
14022 if (func_die
14023 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14024 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14025 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14026 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14027 {
14028 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14029 not complete. But keep CALL_SITE for look ups via call_site_htab,
14030 both the initial caller containing the real return address PC and
14031 the final callee containing the current PC of a chain of tail
14032 calls do not need to have the tail call list complete. But any
14033 function candidate for a virtual tail call frame searched via
14034 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14035 determined unambiguously. */
14036 }
14037 else
14038 {
14039 struct type *func_type = NULL;
14040
14041 if (func_die)
14042 func_type = get_die_type (func_die, cu);
14043 if (func_type != NULL)
14044 {
14045 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
14046
14047 /* Enlist this call site to the function. */
14048 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14049 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14050 }
14051 else
14052 complaint (_("Cannot find function owning DW_TAG_call_site "
14053 "DIE %s [in module %s]"),
14054 sect_offset_str (die->sect_off), objfile_name (objfile));
14055 }
14056 }
14057
14058 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14059 if (attr == NULL)
14060 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14061 if (attr == NULL)
14062 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14063 if (attr == NULL)
14064 {
14065 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14066 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14067 }
14068 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14069 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
14070 /* Keep NULL DWARF_BLOCK. */;
14071 else if (attr->form_is_block ())
14072 {
14073 struct dwarf2_locexpr_baton *dlbaton;
14074 struct dwarf_block *block = attr->as_block ();
14075
14076 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14077 dlbaton->data = block->data;
14078 dlbaton->size = block->size;
14079 dlbaton->per_objfile = per_objfile;
14080 dlbaton->per_cu = cu->per_cu;
14081
14082 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14083 }
14084 else if (attr->form_is_ref ())
14085 {
14086 struct dwarf2_cu *target_cu = cu;
14087 struct die_info *target_die;
14088
14089 target_die = follow_die_ref (die, attr, &target_cu);
14090 gdb_assert (target_cu->per_objfile->objfile == objfile);
14091 if (die_is_declaration (target_die, target_cu))
14092 {
14093 const char *target_physname;
14094
14095 /* Prefer the mangled name; otherwise compute the demangled one. */
14096 target_physname = dw2_linkage_name (target_die, target_cu);
14097 if (target_physname == NULL)
14098 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14099 if (target_physname == NULL)
14100 complaint (_("DW_AT_call_target target DIE has invalid "
14101 "physname, for referencing DIE %s [in module %s]"),
14102 sect_offset_str (die->sect_off), objfile_name (objfile));
14103 else
14104 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14105 }
14106 else
14107 {
14108 CORE_ADDR lowpc;
14109
14110 /* DW_AT_entry_pc should be preferred. */
14111 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14112 <= PC_BOUNDS_INVALID)
14113 complaint (_("DW_AT_call_target target DIE has invalid "
14114 "low pc, for referencing DIE %s [in module %s]"),
14115 sect_offset_str (die->sect_off), objfile_name (objfile));
14116 else
14117 {
14118 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14119 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14120 }
14121 }
14122 }
14123 else
14124 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14125 "block nor reference, for DIE %s [in module %s]"),
14126 sect_offset_str (die->sect_off), objfile_name (objfile));
14127
14128 call_site->per_cu = cu->per_cu;
14129 call_site->per_objfile = per_objfile;
14130
14131 for (child_die = die->child;
14132 child_die && child_die->tag;
14133 child_die = child_die->sibling)
14134 {
14135 struct call_site_parameter *parameter;
14136 struct attribute *loc, *origin;
14137
14138 if (child_die->tag != DW_TAG_call_site_parameter
14139 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14140 {
14141 /* Already printed the complaint above. */
14142 continue;
14143 }
14144
14145 gdb_assert (call_site->parameter_count < nparams);
14146 parameter = &call_site->parameter[call_site->parameter_count];
14147
14148 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14149 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14150 register is contained in DW_AT_call_value. */
14151
14152 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14153 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14154 if (origin == NULL)
14155 {
14156 /* This was a pre-DWARF-5 GNU extension alias
14157 for DW_AT_call_parameter. */
14158 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14159 }
14160 if (loc == NULL && origin != NULL && origin->form_is_ref ())
14161 {
14162 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14163
14164 sect_offset sect_off = origin->get_ref_die_offset ();
14165 if (!cu->header.offset_in_cu_p (sect_off))
14166 {
14167 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14168 binding can be done only inside one CU. Such referenced DIE
14169 therefore cannot be even moved to DW_TAG_partial_unit. */
14170 complaint (_("DW_AT_call_parameter offset is not in CU for "
14171 "DW_TAG_call_site child DIE %s [in module %s]"),
14172 sect_offset_str (child_die->sect_off),
14173 objfile_name (objfile));
14174 continue;
14175 }
14176 parameter->u.param_cu_off
14177 = (cu_offset) (sect_off - cu->header.sect_off);
14178 }
14179 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
14180 {
14181 complaint (_("No DW_FORM_block* DW_AT_location for "
14182 "DW_TAG_call_site child DIE %s [in module %s]"),
14183 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14184 continue;
14185 }
14186 else
14187 {
14188 struct dwarf_block *block = loc->as_block ();
14189
14190 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14191 (block->data, &block->data[block->size]);
14192 if (parameter->u.dwarf_reg != -1)
14193 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14194 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
14195 &block->data[block->size],
14196 &parameter->u.fb_offset))
14197 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14198 else
14199 {
14200 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14201 "for DW_FORM_block* DW_AT_location is supported for "
14202 "DW_TAG_call_site child DIE %s "
14203 "[in module %s]"),
14204 sect_offset_str (child_die->sect_off),
14205 objfile_name (objfile));
14206 continue;
14207 }
14208 }
14209
14210 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14211 if (attr == NULL)
14212 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14213 if (attr == NULL || !attr->form_is_block ())
14214 {
14215 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14216 "DW_TAG_call_site child DIE %s [in module %s]"),
14217 sect_offset_str (child_die->sect_off),
14218 objfile_name (objfile));
14219 continue;
14220 }
14221
14222 struct dwarf_block *block = attr->as_block ();
14223 parameter->value = block->data;
14224 parameter->value_size = block->size;
14225
14226 /* Parameters are not pre-cleared by memset above. */
14227 parameter->data_value = NULL;
14228 parameter->data_value_size = 0;
14229 call_site->parameter_count++;
14230
14231 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14232 if (attr == NULL)
14233 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14234 if (attr != nullptr)
14235 {
14236 if (!attr->form_is_block ())
14237 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14238 "DW_TAG_call_site child DIE %s [in module %s]"),
14239 sect_offset_str (child_die->sect_off),
14240 objfile_name (objfile));
14241 else
14242 {
14243 block = attr->as_block ();
14244 parameter->data_value = block->data;
14245 parameter->data_value_size = block->size;
14246 }
14247 }
14248 }
14249 }
14250
14251 /* Helper function for read_variable. If DIE represents a virtual
14252 table, then return the type of the concrete object that is
14253 associated with the virtual table. Otherwise, return NULL. */
14254
14255 static struct type *
14256 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14257 {
14258 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14259 if (attr == NULL)
14260 return NULL;
14261
14262 /* Find the type DIE. */
14263 struct die_info *type_die = NULL;
14264 struct dwarf2_cu *type_cu = cu;
14265
14266 if (attr->form_is_ref ())
14267 type_die = follow_die_ref (die, attr, &type_cu);
14268 if (type_die == NULL)
14269 return NULL;
14270
14271 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14272 return NULL;
14273 return die_containing_type (type_die, type_cu);
14274 }
14275
14276 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14277
14278 static void
14279 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14280 {
14281 struct rust_vtable_symbol *storage = NULL;
14282
14283 if (cu->language == language_rust)
14284 {
14285 struct type *containing_type = rust_containing_type (die, cu);
14286
14287 if (containing_type != NULL)
14288 {
14289 struct objfile *objfile = cu->per_objfile->objfile;
14290
14291 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
14292 storage->concrete_type = containing_type;
14293 storage->subclass = SYMBOL_RUST_VTABLE;
14294 }
14295 }
14296
14297 struct symbol *res = new_symbol (die, NULL, cu, storage);
14298 struct attribute *abstract_origin
14299 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14300 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14301 if (res == NULL && loc && abstract_origin)
14302 {
14303 /* We have a variable without a name, but with a location and an abstract
14304 origin. This may be a concrete instance of an abstract variable
14305 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14306 later. */
14307 struct dwarf2_cu *origin_cu = cu;
14308 struct die_info *origin_die
14309 = follow_die_ref (die, abstract_origin, &origin_cu);
14310 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14311 per_objfile->per_bfd->abstract_to_concrete
14312 [origin_die->sect_off].push_back (die->sect_off);
14313 }
14314 }
14315
14316 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14317 reading .debug_rnglists.
14318 Callback's type should be:
14319 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14320 Return true if the attributes are present and valid, otherwise,
14321 return false. */
14322
14323 template <typename Callback>
14324 static bool
14325 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14326 dwarf_tag tag, Callback &&callback)
14327 {
14328 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14329 struct objfile *objfile = per_objfile->objfile;
14330 bfd *obfd = objfile->obfd;
14331 /* Base address selection entry. */
14332 gdb::optional<CORE_ADDR> base;
14333 const gdb_byte *buffer;
14334 CORE_ADDR baseaddr;
14335 bool overflow = false;
14336 ULONGEST addr_index;
14337 struct dwarf2_section_info *rnglists_section;
14338
14339 base = cu->base_address;
14340 rnglists_section = cu_debug_rnglists_section (cu, tag);
14341 rnglists_section->read (objfile);
14342
14343 if (offset >= rnglists_section->size)
14344 {
14345 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14346 offset);
14347 return false;
14348 }
14349 buffer = rnglists_section->buffer + offset;
14350
14351 baseaddr = objfile->text_section_offset ();
14352
14353 while (1)
14354 {
14355 /* Initialize it due to a false compiler warning. */
14356 CORE_ADDR range_beginning = 0, range_end = 0;
14357 const gdb_byte *buf_end = (rnglists_section->buffer
14358 + rnglists_section->size);
14359 unsigned int bytes_read;
14360
14361 if (buffer == buf_end)
14362 {
14363 overflow = true;
14364 break;
14365 }
14366 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14367 switch (rlet)
14368 {
14369 case DW_RLE_end_of_list:
14370 break;
14371 case DW_RLE_base_address:
14372 if (buffer + cu->header.addr_size > buf_end)
14373 {
14374 overflow = true;
14375 break;
14376 }
14377 base = cu->header.read_address (obfd, buffer, &bytes_read);
14378 buffer += bytes_read;
14379 break;
14380 case DW_RLE_base_addressx:
14381 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14382 buffer += bytes_read;
14383 base = read_addr_index (cu, addr_index);
14384 break;
14385 case DW_RLE_start_length:
14386 if (buffer + cu->header.addr_size > buf_end)
14387 {
14388 overflow = true;
14389 break;
14390 }
14391 range_beginning = cu->header.read_address (obfd, buffer,
14392 &bytes_read);
14393 buffer += bytes_read;
14394 range_end = (range_beginning
14395 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14396 buffer += bytes_read;
14397 if (buffer > buf_end)
14398 {
14399 overflow = true;
14400 break;
14401 }
14402 break;
14403 case DW_RLE_startx_length:
14404 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14405 buffer += bytes_read;
14406 range_beginning = read_addr_index (cu, addr_index);
14407 if (buffer > buf_end)
14408 {
14409 overflow = true;
14410 break;
14411 }
14412 range_end = (range_beginning
14413 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14414 buffer += bytes_read;
14415 break;
14416 case DW_RLE_offset_pair:
14417 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14418 buffer += bytes_read;
14419 if (buffer > buf_end)
14420 {
14421 overflow = true;
14422 break;
14423 }
14424 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14425 buffer += bytes_read;
14426 if (buffer > buf_end)
14427 {
14428 overflow = true;
14429 break;
14430 }
14431 break;
14432 case DW_RLE_start_end:
14433 if (buffer + 2 * cu->header.addr_size > buf_end)
14434 {
14435 overflow = true;
14436 break;
14437 }
14438 range_beginning = cu->header.read_address (obfd, buffer,
14439 &bytes_read);
14440 buffer += bytes_read;
14441 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
14442 buffer += bytes_read;
14443 break;
14444 case DW_RLE_startx_endx:
14445 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14446 buffer += bytes_read;
14447 range_beginning = read_addr_index (cu, addr_index);
14448 if (buffer > buf_end)
14449 {
14450 overflow = true;
14451 break;
14452 }
14453 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14454 buffer += bytes_read;
14455 range_end = read_addr_index (cu, addr_index);
14456 break;
14457 default:
14458 complaint (_("Invalid .debug_rnglists data (no base address)"));
14459 return false;
14460 }
14461 if (rlet == DW_RLE_end_of_list || overflow)
14462 break;
14463 if (rlet == DW_RLE_base_address)
14464 continue;
14465
14466 if (range_beginning > range_end)
14467 {
14468 /* Inverted range entries are invalid. */
14469 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14470 return false;
14471 }
14472
14473 /* Empty range entries have no effect. */
14474 if (range_beginning == range_end)
14475 continue;
14476
14477 /* Only DW_RLE_offset_pair needs the base address added. */
14478 if (rlet == DW_RLE_offset_pair)
14479 {
14480 if (!base.has_value ())
14481 {
14482 /* We have no valid base address for the DW_RLE_offset_pair. */
14483 complaint (_("Invalid .debug_rnglists data (no base address for "
14484 "DW_RLE_offset_pair)"));
14485 return false;
14486 }
14487
14488 range_beginning += *base;
14489 range_end += *base;
14490 }
14491
14492 /* A not-uncommon case of bad debug info.
14493 Don't pollute the addrmap with bad data. */
14494 if (range_beginning + baseaddr == 0
14495 && !per_objfile->per_bfd->has_section_at_zero)
14496 {
14497 complaint (_(".debug_rnglists entry has start address of zero"
14498 " [in module %s]"), objfile_name (objfile));
14499 continue;
14500 }
14501
14502 callback (range_beginning, range_end);
14503 }
14504
14505 if (overflow)
14506 {
14507 complaint (_("Offset %d is not terminated "
14508 "for DW_AT_ranges attribute"),
14509 offset);
14510 return false;
14511 }
14512
14513 return true;
14514 }
14515
14516 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14517 Callback's type should be:
14518 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14519 Return 1 if the attributes are present and valid, otherwise, return 0. */
14520
14521 template <typename Callback>
14522 static int
14523 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
14524 Callback &&callback)
14525 {
14526 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14527 struct objfile *objfile = per_objfile->objfile;
14528 struct comp_unit_head *cu_header = &cu->header;
14529 bfd *obfd = objfile->obfd;
14530 unsigned int addr_size = cu_header->addr_size;
14531 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14532 /* Base address selection entry. */
14533 gdb::optional<CORE_ADDR> base;
14534 unsigned int dummy;
14535 const gdb_byte *buffer;
14536 CORE_ADDR baseaddr;
14537
14538 if (cu_header->version >= 5)
14539 return dwarf2_rnglists_process (offset, cu, tag, callback);
14540
14541 base = cu->base_address;
14542
14543 per_objfile->per_bfd->ranges.read (objfile);
14544 if (offset >= per_objfile->per_bfd->ranges.size)
14545 {
14546 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14547 offset);
14548 return 0;
14549 }
14550 buffer = per_objfile->per_bfd->ranges.buffer + offset;
14551
14552 baseaddr = objfile->text_section_offset ();
14553
14554 while (1)
14555 {
14556 CORE_ADDR range_beginning, range_end;
14557
14558 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
14559 buffer += addr_size;
14560 range_end = cu->header.read_address (obfd, buffer, &dummy);
14561 buffer += addr_size;
14562 offset += 2 * addr_size;
14563
14564 /* An end of list marker is a pair of zero addresses. */
14565 if (range_beginning == 0 && range_end == 0)
14566 /* Found the end of list entry. */
14567 break;
14568
14569 /* Each base address selection entry is a pair of 2 values.
14570 The first is the largest possible address, the second is
14571 the base address. Check for a base address here. */
14572 if ((range_beginning & mask) == mask)
14573 {
14574 /* If we found the largest possible address, then we already
14575 have the base address in range_end. */
14576 base = range_end;
14577 continue;
14578 }
14579
14580 if (!base.has_value ())
14581 {
14582 /* We have no valid base address for the ranges
14583 data. */
14584 complaint (_("Invalid .debug_ranges data (no base address)"));
14585 return 0;
14586 }
14587
14588 if (range_beginning > range_end)
14589 {
14590 /* Inverted range entries are invalid. */
14591 complaint (_("Invalid .debug_ranges data (inverted range)"));
14592 return 0;
14593 }
14594
14595 /* Empty range entries have no effect. */
14596 if (range_beginning == range_end)
14597 continue;
14598
14599 range_beginning += *base;
14600 range_end += *base;
14601
14602 /* A not-uncommon case of bad debug info.
14603 Don't pollute the addrmap with bad data. */
14604 if (range_beginning + baseaddr == 0
14605 && !per_objfile->per_bfd->has_section_at_zero)
14606 {
14607 complaint (_(".debug_ranges entry has start address of zero"
14608 " [in module %s]"), objfile_name (objfile));
14609 continue;
14610 }
14611
14612 callback (range_beginning, range_end);
14613 }
14614
14615 return 1;
14616 }
14617
14618 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14619 Return 1 if the attributes are present and valid, otherwise, return 0.
14620 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14621
14622 static int
14623 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14624 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14625 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
14626 {
14627 struct objfile *objfile = cu->per_objfile->objfile;
14628 struct gdbarch *gdbarch = objfile->arch ();
14629 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14630 int low_set = 0;
14631 CORE_ADDR low = 0;
14632 CORE_ADDR high = 0;
14633 int retval;
14634
14635 retval = dwarf2_ranges_process (offset, cu, tag,
14636 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14637 {
14638 if (ranges_pst != NULL)
14639 {
14640 CORE_ADDR lowpc;
14641 CORE_ADDR highpc;
14642
14643 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14644 range_beginning + baseaddr)
14645 - baseaddr);
14646 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14647 range_end + baseaddr)
14648 - baseaddr);
14649 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14650 lowpc, highpc - 1, ranges_pst);
14651 }
14652
14653 /* FIXME: This is recording everything as a low-high
14654 segment of consecutive addresses. We should have a
14655 data structure for discontiguous block ranges
14656 instead. */
14657 if (! low_set)
14658 {
14659 low = range_beginning;
14660 high = range_end;
14661 low_set = 1;
14662 }
14663 else
14664 {
14665 if (range_beginning < low)
14666 low = range_beginning;
14667 if (range_end > high)
14668 high = range_end;
14669 }
14670 });
14671 if (!retval)
14672 return 0;
14673
14674 if (! low_set)
14675 /* If the first entry is an end-of-list marker, the range
14676 describes an empty scope, i.e. no instructions. */
14677 return 0;
14678
14679 if (low_return)
14680 *low_return = low;
14681 if (high_return)
14682 *high_return = high;
14683 return 1;
14684 }
14685
14686 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14687 definition for the return value. *LOWPC and *HIGHPC are set iff
14688 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14689
14690 static enum pc_bounds_kind
14691 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14692 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14693 dwarf2_psymtab *pst)
14694 {
14695 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14696 struct attribute *attr;
14697 struct attribute *attr_high;
14698 CORE_ADDR low = 0;
14699 CORE_ADDR high = 0;
14700 enum pc_bounds_kind ret;
14701
14702 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14703 if (attr_high)
14704 {
14705 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14706 if (attr != nullptr)
14707 {
14708 low = attr->as_address ();
14709 high = attr_high->as_address ();
14710 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14711 high += low;
14712 }
14713 else
14714 /* Found high w/o low attribute. */
14715 return PC_BOUNDS_INVALID;
14716
14717 /* Found consecutive range of addresses. */
14718 ret = PC_BOUNDS_HIGH_LOW;
14719 }
14720 else
14721 {
14722 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14723 if (attr != nullptr && attr->form_is_unsigned ())
14724 {
14725 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14726 on DWARF version). */
14727 ULONGEST ranges_offset = attr->as_unsigned ();
14728
14729 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14730 this value. */
14731 if (die->tag != DW_TAG_compile_unit)
14732 ranges_offset += cu->gnu_ranges_base;
14733
14734 /* Value of the DW_AT_ranges attribute is the offset in the
14735 .debug_ranges section. */
14736 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14737 die->tag))
14738 return PC_BOUNDS_INVALID;
14739 /* Found discontinuous range of addresses. */
14740 ret = PC_BOUNDS_RANGES;
14741 }
14742 else
14743 return PC_BOUNDS_NOT_PRESENT;
14744 }
14745
14746 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14747 if (high <= low)
14748 return PC_BOUNDS_INVALID;
14749
14750 /* When using the GNU linker, .gnu.linkonce. sections are used to
14751 eliminate duplicate copies of functions and vtables and such.
14752 The linker will arbitrarily choose one and discard the others.
14753 The AT_*_pc values for such functions refer to local labels in
14754 these sections. If the section from that file was discarded, the
14755 labels are not in the output, so the relocs get a value of 0.
14756 If this is a discarded function, mark the pc bounds as invalid,
14757 so that GDB will ignore it. */
14758 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14759 return PC_BOUNDS_INVALID;
14760
14761 *lowpc = low;
14762 if (highpc)
14763 *highpc = high;
14764 return ret;
14765 }
14766
14767 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14768 its low and high PC addresses. Do nothing if these addresses could not
14769 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14770 and HIGHPC to the high address if greater than HIGHPC. */
14771
14772 static void
14773 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14774 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14775 struct dwarf2_cu *cu)
14776 {
14777 CORE_ADDR low, high;
14778 struct die_info *child = die->child;
14779
14780 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14781 {
14782 *lowpc = std::min (*lowpc, low);
14783 *highpc = std::max (*highpc, high);
14784 }
14785
14786 /* If the language does not allow nested subprograms (either inside
14787 subprograms or lexical blocks), we're done. */
14788 if (cu->language != language_ada)
14789 return;
14790
14791 /* Check all the children of the given DIE. If it contains nested
14792 subprograms, then check their pc bounds. Likewise, we need to
14793 check lexical blocks as well, as they may also contain subprogram
14794 definitions. */
14795 while (child && child->tag)
14796 {
14797 if (child->tag == DW_TAG_subprogram
14798 || child->tag == DW_TAG_lexical_block)
14799 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14800 child = child->sibling;
14801 }
14802 }
14803
14804 /* Get the low and high pc's represented by the scope DIE, and store
14805 them in *LOWPC and *HIGHPC. If the correct values can't be
14806 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14807
14808 static void
14809 get_scope_pc_bounds (struct die_info *die,
14810 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14811 struct dwarf2_cu *cu)
14812 {
14813 CORE_ADDR best_low = (CORE_ADDR) -1;
14814 CORE_ADDR best_high = (CORE_ADDR) 0;
14815 CORE_ADDR current_low, current_high;
14816
14817 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14818 >= PC_BOUNDS_RANGES)
14819 {
14820 best_low = current_low;
14821 best_high = current_high;
14822 }
14823 else
14824 {
14825 struct die_info *child = die->child;
14826
14827 while (child && child->tag)
14828 {
14829 switch (child->tag) {
14830 case DW_TAG_subprogram:
14831 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14832 break;
14833 case DW_TAG_namespace:
14834 case DW_TAG_module:
14835 /* FIXME: carlton/2004-01-16: Should we do this for
14836 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14837 that current GCC's always emit the DIEs corresponding
14838 to definitions of methods of classes as children of a
14839 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14840 the DIEs giving the declarations, which could be
14841 anywhere). But I don't see any reason why the
14842 standards says that they have to be there. */
14843 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14844
14845 if (current_low != ((CORE_ADDR) -1))
14846 {
14847 best_low = std::min (best_low, current_low);
14848 best_high = std::max (best_high, current_high);
14849 }
14850 break;
14851 default:
14852 /* Ignore. */
14853 break;
14854 }
14855
14856 child = child->sibling;
14857 }
14858 }
14859
14860 *lowpc = best_low;
14861 *highpc = best_high;
14862 }
14863
14864 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14865 in DIE. */
14866
14867 static void
14868 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14869 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14870 {
14871 struct objfile *objfile = cu->per_objfile->objfile;
14872 struct gdbarch *gdbarch = objfile->arch ();
14873 struct attribute *attr;
14874 struct attribute *attr_high;
14875
14876 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14877 if (attr_high)
14878 {
14879 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14880 if (attr != nullptr)
14881 {
14882 CORE_ADDR low = attr->as_address ();
14883 CORE_ADDR high = attr_high->as_address ();
14884
14885 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14886 high += low;
14887
14888 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14889 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14890 cu->get_builder ()->record_block_range (block, low, high - 1);
14891 }
14892 }
14893
14894 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14895 if (attr != nullptr && attr->form_is_unsigned ())
14896 {
14897 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14898 on DWARF version). */
14899 ULONGEST ranges_offset = attr->as_unsigned ();
14900
14901 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14902 this value. */
14903 if (die->tag != DW_TAG_compile_unit)
14904 ranges_offset += cu->gnu_ranges_base;
14905
14906 std::vector<blockrange> blockvec;
14907 dwarf2_ranges_process (ranges_offset, cu, die->tag,
14908 [&] (CORE_ADDR start, CORE_ADDR end)
14909 {
14910 start += baseaddr;
14911 end += baseaddr;
14912 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14913 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14914 cu->get_builder ()->record_block_range (block, start, end - 1);
14915 blockvec.emplace_back (start, end);
14916 });
14917
14918 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14919 }
14920 }
14921
14922 /* Check whether the producer field indicates either of GCC < 4.6, or the
14923 Intel C/C++ compiler, and cache the result in CU. */
14924
14925 static void
14926 check_producer (struct dwarf2_cu *cu)
14927 {
14928 int major, minor;
14929
14930 if (cu->producer == NULL)
14931 {
14932 /* For unknown compilers expect their behavior is DWARF version
14933 compliant.
14934
14935 GCC started to support .debug_types sections by -gdwarf-4 since
14936 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14937 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14938 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14939 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14940 }
14941 else if (producer_is_gcc (cu->producer, &major, &minor))
14942 {
14943 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14944 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14945 }
14946 else if (producer_is_icc (cu->producer, &major, &minor))
14947 {
14948 cu->producer_is_icc = true;
14949 cu->producer_is_icc_lt_14 = major < 14;
14950 }
14951 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14952 cu->producer_is_codewarrior = true;
14953 else
14954 {
14955 /* For other non-GCC compilers, expect their behavior is DWARF version
14956 compliant. */
14957 }
14958
14959 cu->checked_producer = true;
14960 }
14961
14962 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14963 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14964 during 4.6.0 experimental. */
14965
14966 static bool
14967 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14968 {
14969 if (!cu->checked_producer)
14970 check_producer (cu);
14971
14972 return cu->producer_is_gxx_lt_4_6;
14973 }
14974
14975
14976 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14977 with incorrect is_stmt attributes. */
14978
14979 static bool
14980 producer_is_codewarrior (struct dwarf2_cu *cu)
14981 {
14982 if (!cu->checked_producer)
14983 check_producer (cu);
14984
14985 return cu->producer_is_codewarrior;
14986 }
14987
14988 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
14989 If that attribute is not available, return the appropriate
14990 default. */
14991
14992 static enum dwarf_access_attribute
14993 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14994 {
14995 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14996 if (attr != nullptr)
14997 {
14998 LONGEST value = attr->constant_value (-1);
14999 if (value == DW_ACCESS_public
15000 || value == DW_ACCESS_protected
15001 || value == DW_ACCESS_private)
15002 return (dwarf_access_attribute) value;
15003 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
15004 plongest (value));
15005 }
15006
15007 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
15008 {
15009 /* The default DWARF 2 accessibility for members is public, the default
15010 accessibility for inheritance is private. */
15011
15012 if (die->tag != DW_TAG_inheritance)
15013 return DW_ACCESS_public;
15014 else
15015 return DW_ACCESS_private;
15016 }
15017 else
15018 {
15019 /* DWARF 3+ defines the default accessibility a different way. The same
15020 rules apply now for DW_TAG_inheritance as for the members and it only
15021 depends on the container kind. */
15022
15023 if (die->parent->tag == DW_TAG_class_type)
15024 return DW_ACCESS_private;
15025 else
15026 return DW_ACCESS_public;
15027 }
15028 }
15029
15030 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15031 offset. If the attribute was not found return 0, otherwise return
15032 1. If it was found but could not properly be handled, set *OFFSET
15033 to 0. */
15034
15035 static int
15036 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15037 LONGEST *offset)
15038 {
15039 struct attribute *attr;
15040
15041 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15042 if (attr != NULL)
15043 {
15044 *offset = 0;
15045
15046 /* Note that we do not check for a section offset first here.
15047 This is because DW_AT_data_member_location is new in DWARF 4,
15048 so if we see it, we can assume that a constant form is really
15049 a constant and not a section offset. */
15050 if (attr->form_is_constant ())
15051 *offset = attr->constant_value (0);
15052 else if (attr->form_is_section_offset ())
15053 dwarf2_complex_location_expr_complaint ();
15054 else if (attr->form_is_block ())
15055 *offset = decode_locdesc (attr->as_block (), cu);
15056 else
15057 dwarf2_complex_location_expr_complaint ();
15058
15059 return 1;
15060 }
15061
15062 return 0;
15063 }
15064
15065 /* Look for DW_AT_data_member_location and store the results in FIELD. */
15066
15067 static void
15068 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15069 struct field *field)
15070 {
15071 struct attribute *attr;
15072
15073 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15074 if (attr != NULL)
15075 {
15076 if (attr->form_is_constant ())
15077 {
15078 LONGEST offset = attr->constant_value (0);
15079 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
15080 }
15081 else if (attr->form_is_section_offset ())
15082 dwarf2_complex_location_expr_complaint ();
15083 else if (attr->form_is_block ())
15084 {
15085 bool handled;
15086 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
15087 if (handled)
15088 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
15089 else
15090 {
15091 dwarf2_per_objfile *per_objfile = cu->per_objfile;
15092 struct objfile *objfile = per_objfile->objfile;
15093 struct dwarf2_locexpr_baton *dlbaton
15094 = XOBNEW (&objfile->objfile_obstack,
15095 struct dwarf2_locexpr_baton);
15096 dlbaton->data = attr->as_block ()->data;
15097 dlbaton->size = attr->as_block ()->size;
15098 /* When using this baton, we want to compute the address
15099 of the field, not the value. This is why
15100 is_reference is set to false here. */
15101 dlbaton->is_reference = false;
15102 dlbaton->per_objfile = per_objfile;
15103 dlbaton->per_cu = cu->per_cu;
15104
15105 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
15106 }
15107 }
15108 else
15109 dwarf2_complex_location_expr_complaint ();
15110 }
15111 }
15112
15113 /* Add an aggregate field to the field list. */
15114
15115 static void
15116 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15117 struct dwarf2_cu *cu)
15118 {
15119 struct objfile *objfile = cu->per_objfile->objfile;
15120 struct gdbarch *gdbarch = objfile->arch ();
15121 struct nextfield *new_field;
15122 struct attribute *attr;
15123 struct field *fp;
15124 const char *fieldname = "";
15125
15126 if (die->tag == DW_TAG_inheritance)
15127 {
15128 fip->baseclasses.emplace_back ();
15129 new_field = &fip->baseclasses.back ();
15130 }
15131 else
15132 {
15133 fip->fields.emplace_back ();
15134 new_field = &fip->fields.back ();
15135 }
15136
15137 new_field->offset = die->sect_off;
15138
15139 new_field->accessibility = dwarf2_access_attribute (die, cu);
15140 if (new_field->accessibility != DW_ACCESS_public)
15141 fip->non_public_fields = true;
15142
15143 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15144 if (attr != nullptr)
15145 new_field->virtuality = attr->as_virtuality ();
15146 else
15147 new_field->virtuality = DW_VIRTUALITY_none;
15148
15149 fp = &new_field->field;
15150
15151 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15152 {
15153 /* Data member other than a C++ static data member. */
15154
15155 /* Get type of field. */
15156 fp->set_type (die_type (die, cu));
15157
15158 SET_FIELD_BITPOS (*fp, 0);
15159
15160 /* Get bit size of field (zero if none). */
15161 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15162 if (attr != nullptr)
15163 {
15164 FIELD_BITSIZE (*fp) = attr->constant_value (0);
15165 }
15166 else
15167 {
15168 FIELD_BITSIZE (*fp) = 0;
15169 }
15170
15171 /* Get bit offset of field. */
15172 handle_data_member_location (die, cu, fp);
15173 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15174 if (attr != nullptr && attr->form_is_constant ())
15175 {
15176 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
15177 {
15178 /* For big endian bits, the DW_AT_bit_offset gives the
15179 additional bit offset from the MSB of the containing
15180 anonymous object to the MSB of the field. We don't
15181 have to do anything special since we don't need to
15182 know the size of the anonymous object. */
15183 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15184 + attr->constant_value (0)));
15185 }
15186 else
15187 {
15188 /* For little endian bits, compute the bit offset to the
15189 MSB of the anonymous object, subtract off the number of
15190 bits from the MSB of the field to the MSB of the
15191 object, and then subtract off the number of bits of
15192 the field itself. The result is the bit offset of
15193 the LSB of the field. */
15194 int anonymous_size;
15195 int bit_offset = attr->constant_value (0);
15196
15197 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15198 if (attr != nullptr && attr->form_is_constant ())
15199 {
15200 /* The size of the anonymous object containing
15201 the bit field is explicit, so use the
15202 indicated size (in bytes). */
15203 anonymous_size = attr->constant_value (0);
15204 }
15205 else
15206 {
15207 /* The size of the anonymous object containing
15208 the bit field must be inferred from the type
15209 attribute of the data member containing the
15210 bit field. */
15211 anonymous_size = TYPE_LENGTH (fp->type ());
15212 }
15213 SET_FIELD_BITPOS (*fp,
15214 (FIELD_BITPOS (*fp)
15215 + anonymous_size * bits_per_byte
15216 - bit_offset - FIELD_BITSIZE (*fp)));
15217 }
15218 }
15219 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15220 if (attr != NULL)
15221 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15222 + attr->constant_value (0)));
15223
15224 /* Get name of field. */
15225 fieldname = dwarf2_name (die, cu);
15226 if (fieldname == NULL)
15227 fieldname = "";
15228
15229 /* The name is already allocated along with this objfile, so we don't
15230 need to duplicate it for the type. */
15231 fp->name = fieldname;
15232
15233 /* Change accessibility for artificial fields (e.g. virtual table
15234 pointer or virtual base class pointer) to private. */
15235 if (dwarf2_attr (die, DW_AT_artificial, cu))
15236 {
15237 FIELD_ARTIFICIAL (*fp) = 1;
15238 new_field->accessibility = DW_ACCESS_private;
15239 fip->non_public_fields = true;
15240 }
15241 }
15242 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15243 {
15244 /* C++ static member. */
15245
15246 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15247 is a declaration, but all versions of G++ as of this writing
15248 (so through at least 3.2.1) incorrectly generate
15249 DW_TAG_variable tags. */
15250
15251 const char *physname;
15252
15253 /* Get name of field. */
15254 fieldname = dwarf2_name (die, cu);
15255 if (fieldname == NULL)
15256 return;
15257
15258 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15259 if (attr
15260 /* Only create a symbol if this is an external value.
15261 new_symbol checks this and puts the value in the global symbol
15262 table, which we want. If it is not external, new_symbol
15263 will try to put the value in cu->list_in_scope which is wrong. */
15264 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15265 {
15266 /* A static const member, not much different than an enum as far as
15267 we're concerned, except that we can support more types. */
15268 new_symbol (die, NULL, cu);
15269 }
15270
15271 /* Get physical name. */
15272 physname = dwarf2_physname (fieldname, die, cu);
15273
15274 /* The name is already allocated along with this objfile, so we don't
15275 need to duplicate it for the type. */
15276 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15277 fp->set_type (die_type (die, cu));
15278 FIELD_NAME (*fp) = fieldname;
15279 }
15280 else if (die->tag == DW_TAG_inheritance)
15281 {
15282 /* C++ base class field. */
15283 handle_data_member_location (die, cu, fp);
15284 FIELD_BITSIZE (*fp) = 0;
15285 fp->set_type (die_type (die, cu));
15286 FIELD_NAME (*fp) = fp->type ()->name ();
15287 }
15288 else
15289 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15290 }
15291
15292 /* Can the type given by DIE define another type? */
15293
15294 static bool
15295 type_can_define_types (const struct die_info *die)
15296 {
15297 switch (die->tag)
15298 {
15299 case DW_TAG_typedef:
15300 case DW_TAG_class_type:
15301 case DW_TAG_structure_type:
15302 case DW_TAG_union_type:
15303 case DW_TAG_enumeration_type:
15304 return true;
15305
15306 default:
15307 return false;
15308 }
15309 }
15310
15311 /* Add a type definition defined in the scope of the FIP's class. */
15312
15313 static void
15314 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15315 struct dwarf2_cu *cu)
15316 {
15317 struct decl_field fp;
15318 memset (&fp, 0, sizeof (fp));
15319
15320 gdb_assert (type_can_define_types (die));
15321
15322 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15323 fp.name = dwarf2_name (die, cu);
15324 fp.type = read_type_die (die, cu);
15325
15326 /* Save accessibility. */
15327 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15328 switch (accessibility)
15329 {
15330 case DW_ACCESS_public:
15331 /* The assumed value if neither private nor protected. */
15332 break;
15333 case DW_ACCESS_private:
15334 fp.is_private = 1;
15335 break;
15336 case DW_ACCESS_protected:
15337 fp.is_protected = 1;
15338 break;
15339 }
15340
15341 if (die->tag == DW_TAG_typedef)
15342 fip->typedef_field_list.push_back (fp);
15343 else
15344 fip->nested_types_list.push_back (fp);
15345 }
15346
15347 /* A convenience typedef that's used when finding the discriminant
15348 field for a variant part. */
15349 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
15350 offset_map_type;
15351
15352 /* Compute the discriminant range for a given variant. OBSTACK is
15353 where the results will be stored. VARIANT is the variant to
15354 process. IS_UNSIGNED indicates whether the discriminant is signed
15355 or unsigned. */
15356
15357 static const gdb::array_view<discriminant_range>
15358 convert_variant_range (struct obstack *obstack, const variant_field &variant,
15359 bool is_unsigned)
15360 {
15361 std::vector<discriminant_range> ranges;
15362
15363 if (variant.default_branch)
15364 return {};
15365
15366 if (variant.discr_list_data == nullptr)
15367 {
15368 discriminant_range r
15369 = {variant.discriminant_value, variant.discriminant_value};
15370 ranges.push_back (r);
15371 }
15372 else
15373 {
15374 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15375 variant.discr_list_data->size);
15376 while (!data.empty ())
15377 {
15378 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15379 {
15380 complaint (_("invalid discriminant marker: %d"), data[0]);
15381 break;
15382 }
15383 bool is_range = data[0] == DW_DSC_range;
15384 data = data.slice (1);
15385
15386 ULONGEST low, high;
15387 unsigned int bytes_read;
15388
15389 if (data.empty ())
15390 {
15391 complaint (_("DW_AT_discr_list missing low value"));
15392 break;
15393 }
15394 if (is_unsigned)
15395 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15396 else
15397 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15398 &bytes_read);
15399 data = data.slice (bytes_read);
15400
15401 if (is_range)
15402 {
15403 if (data.empty ())
15404 {
15405 complaint (_("DW_AT_discr_list missing high value"));
15406 break;
15407 }
15408 if (is_unsigned)
15409 high = read_unsigned_leb128 (nullptr, data.data (),
15410 &bytes_read);
15411 else
15412 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15413 &bytes_read);
15414 data = data.slice (bytes_read);
15415 }
15416 else
15417 high = low;
15418
15419 ranges.push_back ({ low, high });
15420 }
15421 }
15422
15423 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15424 ranges.size ());
15425 std::copy (ranges.begin (), ranges.end (), result);
15426 return gdb::array_view<discriminant_range> (result, ranges.size ());
15427 }
15428
15429 static const gdb::array_view<variant_part> create_variant_parts
15430 (struct obstack *obstack,
15431 const offset_map_type &offset_map,
15432 struct field_info *fi,
15433 const std::vector<variant_part_builder> &variant_parts);
15434
15435 /* Fill in a "struct variant" for a given variant field. RESULT is
15436 the variant to fill in. OBSTACK is where any needed allocations
15437 will be done. OFFSET_MAP holds the mapping from section offsets to
15438 fields for the type. FI describes the fields of the type we're
15439 processing. FIELD is the variant field we're converting. */
15440
15441 static void
15442 create_one_variant (variant &result, struct obstack *obstack,
15443 const offset_map_type &offset_map,
15444 struct field_info *fi, const variant_field &field)
15445 {
15446 result.discriminants = convert_variant_range (obstack, field, false);
15447 result.first_field = field.first_field + fi->baseclasses.size ();
15448 result.last_field = field.last_field + fi->baseclasses.size ();
15449 result.parts = create_variant_parts (obstack, offset_map, fi,
15450 field.variant_parts);
15451 }
15452
15453 /* Fill in a "struct variant_part" for a given variant part. RESULT
15454 is the variant part to fill in. OBSTACK is where any needed
15455 allocations will be done. OFFSET_MAP holds the mapping from
15456 section offsets to fields for the type. FI describes the fields of
15457 the type we're processing. BUILDER is the variant part to be
15458 converted. */
15459
15460 static void
15461 create_one_variant_part (variant_part &result,
15462 struct obstack *obstack,
15463 const offset_map_type &offset_map,
15464 struct field_info *fi,
15465 const variant_part_builder &builder)
15466 {
15467 auto iter = offset_map.find (builder.discriminant_offset);
15468 if (iter == offset_map.end ())
15469 {
15470 result.discriminant_index = -1;
15471 /* Doesn't matter. */
15472 result.is_unsigned = false;
15473 }
15474 else
15475 {
15476 result.discriminant_index = iter->second;
15477 result.is_unsigned
15478 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
15479 }
15480
15481 size_t n = builder.variants.size ();
15482 variant *output = new (obstack) variant[n];
15483 for (size_t i = 0; i < n; ++i)
15484 create_one_variant (output[i], obstack, offset_map, fi,
15485 builder.variants[i]);
15486
15487 result.variants = gdb::array_view<variant> (output, n);
15488 }
15489
15490 /* Create a vector of variant parts that can be attached to a type.
15491 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15492 holds the mapping from section offsets to fields for the type. FI
15493 describes the fields of the type we're processing. VARIANT_PARTS
15494 is the vector to convert. */
15495
15496 static const gdb::array_view<variant_part>
15497 create_variant_parts (struct obstack *obstack,
15498 const offset_map_type &offset_map,
15499 struct field_info *fi,
15500 const std::vector<variant_part_builder> &variant_parts)
15501 {
15502 if (variant_parts.empty ())
15503 return {};
15504
15505 size_t n = variant_parts.size ();
15506 variant_part *result = new (obstack) variant_part[n];
15507 for (size_t i = 0; i < n; ++i)
15508 create_one_variant_part (result[i], obstack, offset_map, fi,
15509 variant_parts[i]);
15510
15511 return gdb::array_view<variant_part> (result, n);
15512 }
15513
15514 /* Compute the variant part vector for FIP, attaching it to TYPE when
15515 done. */
15516
15517 static void
15518 add_variant_property (struct field_info *fip, struct type *type,
15519 struct dwarf2_cu *cu)
15520 {
15521 /* Map section offsets of fields to their field index. Note the
15522 field index here does not take the number of baseclasses into
15523 account. */
15524 offset_map_type offset_map;
15525 for (int i = 0; i < fip->fields.size (); ++i)
15526 offset_map[fip->fields[i].offset] = i;
15527
15528 struct objfile *objfile = cu->per_objfile->objfile;
15529 gdb::array_view<variant_part> parts
15530 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15531 fip->variant_parts);
15532
15533 struct dynamic_prop prop;
15534 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15535 obstack_copy (&objfile->objfile_obstack, &parts,
15536 sizeof (parts)));
15537
15538 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
15539 }
15540
15541 /* Create the vector of fields, and attach it to the type. */
15542
15543 static void
15544 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15545 struct dwarf2_cu *cu)
15546 {
15547 int nfields = fip->nfields ();
15548
15549 /* Record the field count, allocate space for the array of fields,
15550 and create blank accessibility bitfields if necessary. */
15551 type->set_num_fields (nfields);
15552 type->set_fields
15553 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
15554
15555 if (fip->non_public_fields && cu->language != language_ada)
15556 {
15557 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15558
15559 TYPE_FIELD_PRIVATE_BITS (type) =
15560 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15561 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15562
15563 TYPE_FIELD_PROTECTED_BITS (type) =
15564 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15565 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15566
15567 TYPE_FIELD_IGNORE_BITS (type) =
15568 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15569 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15570 }
15571
15572 /* If the type has baseclasses, allocate and clear a bit vector for
15573 TYPE_FIELD_VIRTUAL_BITS. */
15574 if (!fip->baseclasses.empty () && cu->language != language_ada)
15575 {
15576 int num_bytes = B_BYTES (fip->baseclasses.size ());
15577 unsigned char *pointer;
15578
15579 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15580 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15581 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15582 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15583 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15584 }
15585
15586 if (!fip->variant_parts.empty ())
15587 add_variant_property (fip, type, cu);
15588
15589 /* Copy the saved-up fields into the field vector. */
15590 for (int i = 0; i < nfields; ++i)
15591 {
15592 struct nextfield &field
15593 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15594 : fip->fields[i - fip->baseclasses.size ()]);
15595
15596 type->field (i) = field.field;
15597 switch (field.accessibility)
15598 {
15599 case DW_ACCESS_private:
15600 if (cu->language != language_ada)
15601 SET_TYPE_FIELD_PRIVATE (type, i);
15602 break;
15603
15604 case DW_ACCESS_protected:
15605 if (cu->language != language_ada)
15606 SET_TYPE_FIELD_PROTECTED (type, i);
15607 break;
15608
15609 case DW_ACCESS_public:
15610 break;
15611
15612 default:
15613 /* Unknown accessibility. Complain and treat it as public. */
15614 {
15615 complaint (_("unsupported accessibility %d"),
15616 field.accessibility);
15617 }
15618 break;
15619 }
15620 if (i < fip->baseclasses.size ())
15621 {
15622 switch (field.virtuality)
15623 {
15624 case DW_VIRTUALITY_virtual:
15625 case DW_VIRTUALITY_pure_virtual:
15626 if (cu->language == language_ada)
15627 error (_("unexpected virtuality in component of Ada type"));
15628 SET_TYPE_FIELD_VIRTUAL (type, i);
15629 break;
15630 }
15631 }
15632 }
15633 }
15634
15635 /* Return true if this member function is a constructor, false
15636 otherwise. */
15637
15638 static int
15639 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15640 {
15641 const char *fieldname;
15642 const char *type_name;
15643 int len;
15644
15645 if (die->parent == NULL)
15646 return 0;
15647
15648 if (die->parent->tag != DW_TAG_structure_type
15649 && die->parent->tag != DW_TAG_union_type
15650 && die->parent->tag != DW_TAG_class_type)
15651 return 0;
15652
15653 fieldname = dwarf2_name (die, cu);
15654 type_name = dwarf2_name (die->parent, cu);
15655 if (fieldname == NULL || type_name == NULL)
15656 return 0;
15657
15658 len = strlen (fieldname);
15659 return (strncmp (fieldname, type_name, len) == 0
15660 && (type_name[len] == '\0' || type_name[len] == '<'));
15661 }
15662
15663 /* Add a member function to the proper fieldlist. */
15664
15665 static void
15666 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15667 struct type *type, struct dwarf2_cu *cu)
15668 {
15669 struct objfile *objfile = cu->per_objfile->objfile;
15670 struct attribute *attr;
15671 int i;
15672 struct fnfieldlist *flp = nullptr;
15673 struct fn_field *fnp;
15674 const char *fieldname;
15675 struct type *this_type;
15676
15677 if (cu->language == language_ada)
15678 error (_("unexpected member function in Ada type"));
15679
15680 /* Get name of member function. */
15681 fieldname = dwarf2_name (die, cu);
15682 if (fieldname == NULL)
15683 return;
15684
15685 /* Look up member function name in fieldlist. */
15686 for (i = 0; i < fip->fnfieldlists.size (); i++)
15687 {
15688 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15689 {
15690 flp = &fip->fnfieldlists[i];
15691 break;
15692 }
15693 }
15694
15695 /* Create a new fnfieldlist if necessary. */
15696 if (flp == nullptr)
15697 {
15698 fip->fnfieldlists.emplace_back ();
15699 flp = &fip->fnfieldlists.back ();
15700 flp->name = fieldname;
15701 i = fip->fnfieldlists.size () - 1;
15702 }
15703
15704 /* Create a new member function field and add it to the vector of
15705 fnfieldlists. */
15706 flp->fnfields.emplace_back ();
15707 fnp = &flp->fnfields.back ();
15708
15709 /* Delay processing of the physname until later. */
15710 if (cu->language == language_cplus)
15711 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15712 die, cu);
15713 else
15714 {
15715 const char *physname = dwarf2_physname (fieldname, die, cu);
15716 fnp->physname = physname ? physname : "";
15717 }
15718
15719 fnp->type = alloc_type (objfile);
15720 this_type = read_type_die (die, cu);
15721 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15722 {
15723 int nparams = this_type->num_fields ();
15724
15725 /* TYPE is the domain of this method, and THIS_TYPE is the type
15726 of the method itself (TYPE_CODE_METHOD). */
15727 smash_to_method_type (fnp->type, type,
15728 TYPE_TARGET_TYPE (this_type),
15729 this_type->fields (),
15730 this_type->num_fields (),
15731 this_type->has_varargs ());
15732
15733 /* Handle static member functions.
15734 Dwarf2 has no clean way to discern C++ static and non-static
15735 member functions. G++ helps GDB by marking the first
15736 parameter for non-static member functions (which is the this
15737 pointer) as artificial. We obtain this information from
15738 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15739 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15740 fnp->voffset = VOFFSET_STATIC;
15741 }
15742 else
15743 complaint (_("member function type missing for '%s'"),
15744 dwarf2_full_name (fieldname, die, cu));
15745
15746 /* Get fcontext from DW_AT_containing_type if present. */
15747 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15748 fnp->fcontext = die_containing_type (die, cu);
15749
15750 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15751 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15752
15753 /* Get accessibility. */
15754 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15755 switch (accessibility)
15756 {
15757 case DW_ACCESS_private:
15758 fnp->is_private = 1;
15759 break;
15760 case DW_ACCESS_protected:
15761 fnp->is_protected = 1;
15762 break;
15763 }
15764
15765 /* Check for artificial methods. */
15766 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15767 if (attr && attr->as_boolean ())
15768 fnp->is_artificial = 1;
15769
15770 /* Check for defaulted methods. */
15771 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15772 if (attr != nullptr)
15773 fnp->defaulted = attr->defaulted ();
15774
15775 /* Check for deleted methods. */
15776 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15777 if (attr != nullptr && attr->as_boolean ())
15778 fnp->is_deleted = 1;
15779
15780 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15781
15782 /* Get index in virtual function table if it is a virtual member
15783 function. For older versions of GCC, this is an offset in the
15784 appropriate virtual table, as specified by DW_AT_containing_type.
15785 For everyone else, it is an expression to be evaluated relative
15786 to the object address. */
15787
15788 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15789 if (attr != nullptr)
15790 {
15791 if (attr->form_is_block () && attr->as_block ()->size > 0)
15792 {
15793 struct dwarf_block *block = attr->as_block ();
15794
15795 if (block->data[0] == DW_OP_constu)
15796 {
15797 /* Old-style GCC. */
15798 fnp->voffset = decode_locdesc (block, cu) + 2;
15799 }
15800 else if (block->data[0] == DW_OP_deref
15801 || (block->size > 1
15802 && block->data[0] == DW_OP_deref_size
15803 && block->data[1] == cu->header.addr_size))
15804 {
15805 fnp->voffset = decode_locdesc (block, cu);
15806 if ((fnp->voffset % cu->header.addr_size) != 0)
15807 dwarf2_complex_location_expr_complaint ();
15808 else
15809 fnp->voffset /= cu->header.addr_size;
15810 fnp->voffset += 2;
15811 }
15812 else
15813 dwarf2_complex_location_expr_complaint ();
15814
15815 if (!fnp->fcontext)
15816 {
15817 /* If there is no `this' field and no DW_AT_containing_type,
15818 we cannot actually find a base class context for the
15819 vtable! */
15820 if (this_type->num_fields () == 0
15821 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15822 {
15823 complaint (_("cannot determine context for virtual member "
15824 "function \"%s\" (offset %s)"),
15825 fieldname, sect_offset_str (die->sect_off));
15826 }
15827 else
15828 {
15829 fnp->fcontext
15830 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15831 }
15832 }
15833 }
15834 else if (attr->form_is_section_offset ())
15835 {
15836 dwarf2_complex_location_expr_complaint ();
15837 }
15838 else
15839 {
15840 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15841 fieldname);
15842 }
15843 }
15844 else
15845 {
15846 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15847 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
15848 {
15849 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15850 complaint (_("Member function \"%s\" (offset %s) is virtual "
15851 "but the vtable offset is not specified"),
15852 fieldname, sect_offset_str (die->sect_off));
15853 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15854 TYPE_CPLUS_DYNAMIC (type) = 1;
15855 }
15856 }
15857 }
15858
15859 /* Create the vector of member function fields, and attach it to the type. */
15860
15861 static void
15862 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15863 struct dwarf2_cu *cu)
15864 {
15865 if (cu->language == language_ada)
15866 error (_("unexpected member functions in Ada type"));
15867
15868 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15869 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15870 TYPE_ALLOC (type,
15871 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15872
15873 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15874 {
15875 struct fnfieldlist &nf = fip->fnfieldlists[i];
15876 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15877
15878 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15879 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15880 fn_flp->fn_fields = (struct fn_field *)
15881 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15882
15883 for (int k = 0; k < nf.fnfields.size (); ++k)
15884 fn_flp->fn_fields[k] = nf.fnfields[k];
15885 }
15886
15887 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15888 }
15889
15890 /* Returns non-zero if NAME is the name of a vtable member in CU's
15891 language, zero otherwise. */
15892 static int
15893 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15894 {
15895 static const char vptr[] = "_vptr";
15896
15897 /* Look for the C++ form of the vtable. */
15898 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15899 return 1;
15900
15901 return 0;
15902 }
15903
15904 /* GCC outputs unnamed structures that are really pointers to member
15905 functions, with the ABI-specified layout. If TYPE describes
15906 such a structure, smash it into a member function type.
15907
15908 GCC shouldn't do this; it should just output pointer to member DIEs.
15909 This is GCC PR debug/28767. */
15910
15911 static void
15912 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15913 {
15914 struct type *pfn_type, *self_type, *new_type;
15915
15916 /* Check for a structure with no name and two children. */
15917 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15918 return;
15919
15920 /* Check for __pfn and __delta members. */
15921 if (TYPE_FIELD_NAME (type, 0) == NULL
15922 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15923 || TYPE_FIELD_NAME (type, 1) == NULL
15924 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15925 return;
15926
15927 /* Find the type of the method. */
15928 pfn_type = type->field (0).type ();
15929 if (pfn_type == NULL
15930 || pfn_type->code () != TYPE_CODE_PTR
15931 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15932 return;
15933
15934 /* Look for the "this" argument. */
15935 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15936 if (pfn_type->num_fields () == 0
15937 /* || pfn_type->field (0).type () == NULL */
15938 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15939 return;
15940
15941 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15942 new_type = alloc_type (objfile);
15943 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15944 pfn_type->fields (), pfn_type->num_fields (),
15945 pfn_type->has_varargs ());
15946 smash_to_methodptr_type (type, new_type);
15947 }
15948
15949 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15950 requires rewriting, then copy it and return the updated copy.
15951 Otherwise return nullptr. */
15952
15953 static struct type *
15954 rewrite_array_type (struct type *type)
15955 {
15956 if (type->code () != TYPE_CODE_ARRAY)
15957 return nullptr;
15958
15959 struct type *index_type = type->index_type ();
15960 range_bounds *current_bounds = index_type->bounds ();
15961
15962 /* Handle multi-dimensional arrays. */
15963 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15964 if (new_target == nullptr)
15965 {
15966 /* Maybe we don't need to rewrite this array. */
15967 if (current_bounds->low.kind () == PROP_CONST
15968 && current_bounds->high.kind () == PROP_CONST)
15969 return nullptr;
15970 }
15971
15972 /* Either the target type was rewritten, or the bounds have to be
15973 updated. Either way we want to copy the type and update
15974 everything. */
15975 struct type *copy = copy_type (type);
15976 int nfields = copy->num_fields ();
15977 field *new_fields
15978 = ((struct field *) TYPE_ZALLOC (copy,
15979 nfields * sizeof (struct field)));
15980 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15981 copy->set_fields (new_fields);
15982 if (new_target != nullptr)
15983 TYPE_TARGET_TYPE (copy) = new_target;
15984
15985 struct type *index_copy = copy_type (index_type);
15986 range_bounds *bounds
15987 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15988 sizeof (range_bounds));
15989 *bounds = *current_bounds;
15990 bounds->low.set_const_val (1);
15991 bounds->high.set_const_val (0);
15992 index_copy->set_bounds (bounds);
15993 copy->set_index_type (index_copy);
15994
15995 return copy;
15996 }
15997
15998 /* While some versions of GCC will generate complicated DWARF for an
15999 array (see quirk_ada_thick_pointer), more recent versions were
16000 modified to emit an explicit thick pointer structure. However, in
16001 this case, the array still has DWARF expressions for its ranges,
16002 and these must be ignored. */
16003
16004 static void
16005 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
16006 struct type *type)
16007 {
16008 gdb_assert (cu->language == language_ada);
16009
16010 /* Check for a structure with two children. */
16011 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
16012 return;
16013
16014 /* Check for P_ARRAY and P_BOUNDS members. */
16015 if (TYPE_FIELD_NAME (type, 0) == NULL
16016 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
16017 || TYPE_FIELD_NAME (type, 1) == NULL
16018 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
16019 return;
16020
16021 /* Make sure we're looking at a pointer to an array. */
16022 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
16023 return;
16024
16025 /* The Ada code already knows how to handle these types, so all that
16026 we need to do is turn the bounds into static bounds. However, we
16027 don't want to rewrite existing array or index types in-place,
16028 because those may be referenced in other contexts where this
16029 rewriting is undesirable. */
16030 struct type *new_ary_type
16031 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
16032 if (new_ary_type != nullptr)
16033 type->field (0).set_type (lookup_pointer_type (new_ary_type));
16034 }
16035
16036 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
16037 appropriate error checking and issuing complaints if there is a
16038 problem. */
16039
16040 static ULONGEST
16041 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
16042 {
16043 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
16044
16045 if (attr == nullptr)
16046 return 0;
16047
16048 if (!attr->form_is_constant ())
16049 {
16050 complaint (_("DW_AT_alignment must have constant form"
16051 " - DIE at %s [in module %s]"),
16052 sect_offset_str (die->sect_off),
16053 objfile_name (cu->per_objfile->objfile));
16054 return 0;
16055 }
16056
16057 LONGEST val = attr->constant_value (0);
16058 if (val < 0)
16059 {
16060 complaint (_("DW_AT_alignment value must not be negative"
16061 " - DIE at %s [in module %s]"),
16062 sect_offset_str (die->sect_off),
16063 objfile_name (cu->per_objfile->objfile));
16064 return 0;
16065 }
16066 ULONGEST align = val;
16067
16068 if (align == 0)
16069 {
16070 complaint (_("DW_AT_alignment value must not be zero"
16071 " - DIE at %s [in module %s]"),
16072 sect_offset_str (die->sect_off),
16073 objfile_name (cu->per_objfile->objfile));
16074 return 0;
16075 }
16076 if ((align & (align - 1)) != 0)
16077 {
16078 complaint (_("DW_AT_alignment value must be a power of 2"
16079 " - DIE at %s [in module %s]"),
16080 sect_offset_str (die->sect_off),
16081 objfile_name (cu->per_objfile->objfile));
16082 return 0;
16083 }
16084
16085 return align;
16086 }
16087
16088 /* If the DIE has a DW_AT_alignment attribute, use its value to set
16089 the alignment for TYPE. */
16090
16091 static void
16092 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
16093 struct type *type)
16094 {
16095 if (!set_type_align (type, get_alignment (cu, die)))
16096 complaint (_("DW_AT_alignment value too large"
16097 " - DIE at %s [in module %s]"),
16098 sect_offset_str (die->sect_off),
16099 objfile_name (cu->per_objfile->objfile));
16100 }
16101
16102 /* Check if the given VALUE is a valid enum dwarf_calling_convention
16103 constant for a type, according to DWARF5 spec, Table 5.5. */
16104
16105 static bool
16106 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
16107 {
16108 switch (value)
16109 {
16110 case DW_CC_normal:
16111 case DW_CC_pass_by_reference:
16112 case DW_CC_pass_by_value:
16113 return true;
16114
16115 default:
16116 complaint (_("unrecognized DW_AT_calling_convention value "
16117 "(%s) for a type"), pulongest (value));
16118 return false;
16119 }
16120 }
16121
16122 /* Check if the given VALUE is a valid enum dwarf_calling_convention
16123 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
16124 also according to GNU-specific values (see include/dwarf2.h). */
16125
16126 static bool
16127 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
16128 {
16129 switch (value)
16130 {
16131 case DW_CC_normal:
16132 case DW_CC_program:
16133 case DW_CC_nocall:
16134 return true;
16135
16136 case DW_CC_GNU_renesas_sh:
16137 case DW_CC_GNU_borland_fastcall_i386:
16138 case DW_CC_GDB_IBM_OpenCL:
16139 return true;
16140
16141 default:
16142 complaint (_("unrecognized DW_AT_calling_convention value "
16143 "(%s) for a subroutine"), pulongest (value));
16144 return false;
16145 }
16146 }
16147
16148 /* Called when we find the DIE that starts a structure or union scope
16149 (definition) to create a type for the structure or union. Fill in
16150 the type's name and general properties; the members will not be
16151 processed until process_structure_scope. A symbol table entry for
16152 the type will also not be done until process_structure_scope (assuming
16153 the type has a name).
16154
16155 NOTE: we need to call these functions regardless of whether or not the
16156 DIE has a DW_AT_name attribute, since it might be an anonymous
16157 structure or union. This gets the type entered into our set of
16158 user defined types. */
16159
16160 static struct type *
16161 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
16162 {
16163 struct objfile *objfile = cu->per_objfile->objfile;
16164 struct type *type;
16165 struct attribute *attr;
16166 const char *name;
16167
16168 /* If the definition of this type lives in .debug_types, read that type.
16169 Don't follow DW_AT_specification though, that will take us back up
16170 the chain and we want to go down. */
16171 attr = die->attr (DW_AT_signature);
16172 if (attr != nullptr)
16173 {
16174 type = get_DW_AT_signature_type (die, attr, cu);
16175
16176 /* The type's CU may not be the same as CU.
16177 Ensure TYPE is recorded with CU in die_type_hash. */
16178 return set_die_type (die, type, cu);
16179 }
16180
16181 type = alloc_type (objfile);
16182 INIT_CPLUS_SPECIFIC (type);
16183
16184 name = dwarf2_name (die, cu);
16185 if (name != NULL)
16186 {
16187 if (cu->language == language_cplus
16188 || cu->language == language_d
16189 || cu->language == language_rust)
16190 {
16191 const char *full_name = dwarf2_full_name (name, die, cu);
16192
16193 /* dwarf2_full_name might have already finished building the DIE's
16194 type. If so, there is no need to continue. */
16195 if (get_die_type (die, cu) != NULL)
16196 return get_die_type (die, cu);
16197
16198 type->set_name (full_name);
16199 }
16200 else
16201 {
16202 /* The name is already allocated along with this objfile, so
16203 we don't need to duplicate it for the type. */
16204 type->set_name (name);
16205 }
16206 }
16207
16208 if (die->tag == DW_TAG_structure_type)
16209 {
16210 type->set_code (TYPE_CODE_STRUCT);
16211 }
16212 else if (die->tag == DW_TAG_union_type)
16213 {
16214 type->set_code (TYPE_CODE_UNION);
16215 }
16216 else
16217 {
16218 type->set_code (TYPE_CODE_STRUCT);
16219 }
16220
16221 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16222 TYPE_DECLARED_CLASS (type) = 1;
16223
16224 /* Store the calling convention in the type if it's available in
16225 the die. Otherwise the calling convention remains set to
16226 the default value DW_CC_normal. */
16227 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16228 if (attr != nullptr
16229 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
16230 {
16231 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16232 TYPE_CPLUS_CALLING_CONVENTION (type)
16233 = (enum dwarf_calling_convention) (attr->constant_value (0));
16234 }
16235
16236 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16237 if (attr != nullptr)
16238 {
16239 if (attr->form_is_constant ())
16240 TYPE_LENGTH (type) = attr->constant_value (0);
16241 else
16242 {
16243 struct dynamic_prop prop;
16244 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
16245 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
16246 TYPE_LENGTH (type) = 0;
16247 }
16248 }
16249 else
16250 {
16251 TYPE_LENGTH (type) = 0;
16252 }
16253
16254 maybe_set_alignment (cu, die, type);
16255
16256 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
16257 {
16258 /* ICC<14 does not output the required DW_AT_declaration on
16259 incomplete types, but gives them a size of zero. */
16260 type->set_is_stub (true);
16261 }
16262 else
16263 type->set_stub_is_supported (true);
16264
16265 if (die_is_declaration (die, cu))
16266 type->set_is_stub (true);
16267 else if (attr == NULL && die->child == NULL
16268 && producer_is_realview (cu->producer))
16269 /* RealView does not output the required DW_AT_declaration
16270 on incomplete types. */
16271 type->set_is_stub (true);
16272
16273 /* We need to add the type field to the die immediately so we don't
16274 infinitely recurse when dealing with pointers to the structure
16275 type within the structure itself. */
16276 set_die_type (die, type, cu);
16277
16278 /* set_die_type should be already done. */
16279 set_descriptive_type (type, die, cu);
16280
16281 return type;
16282 }
16283
16284 static void handle_struct_member_die
16285 (struct die_info *child_die,
16286 struct type *type,
16287 struct field_info *fi,
16288 std::vector<struct symbol *> *template_args,
16289 struct dwarf2_cu *cu);
16290
16291 /* A helper for handle_struct_member_die that handles
16292 DW_TAG_variant_part. */
16293
16294 static void
16295 handle_variant_part (struct die_info *die, struct type *type,
16296 struct field_info *fi,
16297 std::vector<struct symbol *> *template_args,
16298 struct dwarf2_cu *cu)
16299 {
16300 variant_part_builder *new_part;
16301 if (fi->current_variant_part == nullptr)
16302 {
16303 fi->variant_parts.emplace_back ();
16304 new_part = &fi->variant_parts.back ();
16305 }
16306 else if (!fi->current_variant_part->processing_variant)
16307 {
16308 complaint (_("nested DW_TAG_variant_part seen "
16309 "- DIE at %s [in module %s]"),
16310 sect_offset_str (die->sect_off),
16311 objfile_name (cu->per_objfile->objfile));
16312 return;
16313 }
16314 else
16315 {
16316 variant_field &current = fi->current_variant_part->variants.back ();
16317 current.variant_parts.emplace_back ();
16318 new_part = &current.variant_parts.back ();
16319 }
16320
16321 /* When we recurse, we want callees to add to this new variant
16322 part. */
16323 scoped_restore save_current_variant_part
16324 = make_scoped_restore (&fi->current_variant_part, new_part);
16325
16326 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16327 if (discr == NULL)
16328 {
16329 /* It's a univariant form, an extension we support. */
16330 }
16331 else if (discr->form_is_ref ())
16332 {
16333 struct dwarf2_cu *target_cu = cu;
16334 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16335
16336 new_part->discriminant_offset = target_die->sect_off;
16337 }
16338 else
16339 {
16340 complaint (_("DW_AT_discr does not have DIE reference form"
16341 " - DIE at %s [in module %s]"),
16342 sect_offset_str (die->sect_off),
16343 objfile_name (cu->per_objfile->objfile));
16344 }
16345
16346 for (die_info *child_die = die->child;
16347 child_die != NULL;
16348 child_die = child_die->sibling)
16349 handle_struct_member_die (child_die, type, fi, template_args, cu);
16350 }
16351
16352 /* A helper for handle_struct_member_die that handles
16353 DW_TAG_variant. */
16354
16355 static void
16356 handle_variant (struct die_info *die, struct type *type,
16357 struct field_info *fi,
16358 std::vector<struct symbol *> *template_args,
16359 struct dwarf2_cu *cu)
16360 {
16361 if (fi->current_variant_part == nullptr)
16362 {
16363 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
16364 "- DIE at %s [in module %s]"),
16365 sect_offset_str (die->sect_off),
16366 objfile_name (cu->per_objfile->objfile));
16367 return;
16368 }
16369 if (fi->current_variant_part->processing_variant)
16370 {
16371 complaint (_("nested DW_TAG_variant seen "
16372 "- DIE at %s [in module %s]"),
16373 sect_offset_str (die->sect_off),
16374 objfile_name (cu->per_objfile->objfile));
16375 return;
16376 }
16377
16378 scoped_restore save_processing_variant
16379 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16380 true);
16381
16382 fi->current_variant_part->variants.emplace_back ();
16383 variant_field &variant = fi->current_variant_part->variants.back ();
16384 variant.first_field = fi->fields.size ();
16385
16386 /* In a variant we want to get the discriminant and also add a
16387 field for our sole member child. */
16388 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
16389 if (discr == nullptr || !discr->form_is_constant ())
16390 {
16391 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
16392 if (discr == nullptr || discr->as_block ()->size == 0)
16393 variant.default_branch = true;
16394 else
16395 variant.discr_list_data = discr->as_block ();
16396 }
16397 else
16398 variant.discriminant_value = discr->constant_value (0);
16399
16400 for (die_info *variant_child = die->child;
16401 variant_child != NULL;
16402 variant_child = variant_child->sibling)
16403 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16404
16405 variant.last_field = fi->fields.size ();
16406 }
16407
16408 /* A helper for process_structure_scope that handles a single member
16409 DIE. */
16410
16411 static void
16412 handle_struct_member_die (struct die_info *child_die, struct type *type,
16413 struct field_info *fi,
16414 std::vector<struct symbol *> *template_args,
16415 struct dwarf2_cu *cu)
16416 {
16417 if (child_die->tag == DW_TAG_member
16418 || child_die->tag == DW_TAG_variable)
16419 {
16420 /* NOTE: carlton/2002-11-05: A C++ static data member
16421 should be a DW_TAG_member that is a declaration, but
16422 all versions of G++ as of this writing (so through at
16423 least 3.2.1) incorrectly generate DW_TAG_variable
16424 tags for them instead. */
16425 dwarf2_add_field (fi, child_die, cu);
16426 }
16427 else if (child_die->tag == DW_TAG_subprogram)
16428 {
16429 /* Rust doesn't have member functions in the C++ sense.
16430 However, it does emit ordinary functions as children
16431 of a struct DIE. */
16432 if (cu->language == language_rust)
16433 read_func_scope (child_die, cu);
16434 else
16435 {
16436 /* C++ member function. */
16437 dwarf2_add_member_fn (fi, child_die, type, cu);
16438 }
16439 }
16440 else if (child_die->tag == DW_TAG_inheritance)
16441 {
16442 /* C++ base class field. */
16443 dwarf2_add_field (fi, child_die, cu);
16444 }
16445 else if (type_can_define_types (child_die))
16446 dwarf2_add_type_defn (fi, child_die, cu);
16447 else if (child_die->tag == DW_TAG_template_type_param
16448 || child_die->tag == DW_TAG_template_value_param)
16449 {
16450 struct symbol *arg = new_symbol (child_die, NULL, cu);
16451
16452 if (arg != NULL)
16453 template_args->push_back (arg);
16454 }
16455 else if (child_die->tag == DW_TAG_variant_part)
16456 handle_variant_part (child_die, type, fi, template_args, cu);
16457 else if (child_die->tag == DW_TAG_variant)
16458 handle_variant (child_die, type, fi, template_args, cu);
16459 }
16460
16461 /* Finish creating a structure or union type, including filling in
16462 its members and creating a symbol for it. */
16463
16464 static void
16465 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16466 {
16467 struct objfile *objfile = cu->per_objfile->objfile;
16468 struct die_info *child_die;
16469 struct type *type;
16470
16471 type = get_die_type (die, cu);
16472 if (type == NULL)
16473 type = read_structure_type (die, cu);
16474
16475 bool has_template_parameters = false;
16476 if (die->child != NULL && ! die_is_declaration (die, cu))
16477 {
16478 struct field_info fi;
16479 std::vector<struct symbol *> template_args;
16480
16481 child_die = die->child;
16482
16483 while (child_die && child_die->tag)
16484 {
16485 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16486 child_die = child_die->sibling;
16487 }
16488
16489 /* Attach template arguments to type. */
16490 if (!template_args.empty ())
16491 {
16492 has_template_parameters = true;
16493 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16494 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16495 TYPE_TEMPLATE_ARGUMENTS (type)
16496 = XOBNEWVEC (&objfile->objfile_obstack,
16497 struct symbol *,
16498 TYPE_N_TEMPLATE_ARGUMENTS (type));
16499 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16500 template_args.data (),
16501 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16502 * sizeof (struct symbol *)));
16503 }
16504
16505 /* Attach fields and member functions to the type. */
16506 if (fi.nfields () > 0)
16507 dwarf2_attach_fields_to_type (&fi, type, cu);
16508 if (!fi.fnfieldlists.empty ())
16509 {
16510 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16511
16512 /* Get the type which refers to the base class (possibly this
16513 class itself) which contains the vtable pointer for the current
16514 class from the DW_AT_containing_type attribute. This use of
16515 DW_AT_containing_type is a GNU extension. */
16516
16517 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16518 {
16519 struct type *t = die_containing_type (die, cu);
16520
16521 set_type_vptr_basetype (type, t);
16522 if (type == t)
16523 {
16524 int i;
16525
16526 /* Our own class provides vtbl ptr. */
16527 for (i = t->num_fields () - 1;
16528 i >= TYPE_N_BASECLASSES (t);
16529 --i)
16530 {
16531 const char *fieldname = TYPE_FIELD_NAME (t, i);
16532
16533 if (is_vtable_name (fieldname, cu))
16534 {
16535 set_type_vptr_fieldno (type, i);
16536 break;
16537 }
16538 }
16539
16540 /* Complain if virtual function table field not found. */
16541 if (i < TYPE_N_BASECLASSES (t))
16542 complaint (_("virtual function table pointer "
16543 "not found when defining class '%s'"),
16544 type->name () ? type->name () : "");
16545 }
16546 else
16547 {
16548 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16549 }
16550 }
16551 else if (cu->producer
16552 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16553 {
16554 /* The IBM XLC compiler does not provide direct indication
16555 of the containing type, but the vtable pointer is
16556 always named __vfp. */
16557
16558 int i;
16559
16560 for (i = type->num_fields () - 1;
16561 i >= TYPE_N_BASECLASSES (type);
16562 --i)
16563 {
16564 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16565 {
16566 set_type_vptr_fieldno (type, i);
16567 set_type_vptr_basetype (type, type);
16568 break;
16569 }
16570 }
16571 }
16572 }
16573
16574 /* Copy fi.typedef_field_list linked list elements content into the
16575 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16576 if (!fi.typedef_field_list.empty ())
16577 {
16578 int count = fi.typedef_field_list.size ();
16579
16580 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16581 TYPE_TYPEDEF_FIELD_ARRAY (type)
16582 = ((struct decl_field *)
16583 TYPE_ALLOC (type,
16584 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16585 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16586
16587 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16588 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16589 }
16590
16591 /* Copy fi.nested_types_list linked list elements content into the
16592 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16593 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16594 {
16595 int count = fi.nested_types_list.size ();
16596
16597 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16598 TYPE_NESTED_TYPES_ARRAY (type)
16599 = ((struct decl_field *)
16600 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16601 TYPE_NESTED_TYPES_COUNT (type) = count;
16602
16603 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16604 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16605 }
16606 }
16607
16608 quirk_gcc_member_function_pointer (type, objfile);
16609 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16610 cu->rust_unions.push_back (type);
16611 else if (cu->language == language_ada)
16612 quirk_ada_thick_pointer_struct (die, cu, type);
16613
16614 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16615 snapshots) has been known to create a die giving a declaration
16616 for a class that has, as a child, a die giving a definition for a
16617 nested class. So we have to process our children even if the
16618 current die is a declaration. Normally, of course, a declaration
16619 won't have any children at all. */
16620
16621 child_die = die->child;
16622
16623 while (child_die != NULL && child_die->tag)
16624 {
16625 if (child_die->tag == DW_TAG_member
16626 || child_die->tag == DW_TAG_variable
16627 || child_die->tag == DW_TAG_inheritance
16628 || child_die->tag == DW_TAG_template_value_param
16629 || child_die->tag == DW_TAG_template_type_param)
16630 {
16631 /* Do nothing. */
16632 }
16633 else
16634 process_die (child_die, cu);
16635
16636 child_die = child_die->sibling;
16637 }
16638
16639 /* Do not consider external references. According to the DWARF standard,
16640 these DIEs are identified by the fact that they have no byte_size
16641 attribute, and a declaration attribute. */
16642 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16643 || !die_is_declaration (die, cu)
16644 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16645 {
16646 struct symbol *sym = new_symbol (die, type, cu);
16647
16648 if (has_template_parameters)
16649 {
16650 struct symtab *symtab;
16651 if (sym != nullptr)
16652 symtab = symbol_symtab (sym);
16653 else if (cu->line_header != nullptr)
16654 {
16655 /* Any related symtab will do. */
16656 symtab
16657 = cu->line_header->file_names ()[0].symtab;
16658 }
16659 else
16660 {
16661 symtab = nullptr;
16662 complaint (_("could not find suitable "
16663 "symtab for template parameter"
16664 " - DIE at %s [in module %s]"),
16665 sect_offset_str (die->sect_off),
16666 objfile_name (objfile));
16667 }
16668
16669 if (symtab != nullptr)
16670 {
16671 /* Make sure that the symtab is set on the new symbols.
16672 Even though they don't appear in this symtab directly,
16673 other parts of gdb assume that symbols do, and this is
16674 reasonably true. */
16675 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16676 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16677 }
16678 }
16679 }
16680 }
16681
16682 /* Assuming DIE is an enumeration type, and TYPE is its associated
16683 type, update TYPE using some information only available in DIE's
16684 children. In particular, the fields are computed. */
16685
16686 static void
16687 update_enumeration_type_from_children (struct die_info *die,
16688 struct type *type,
16689 struct dwarf2_cu *cu)
16690 {
16691 struct die_info *child_die;
16692 int unsigned_enum = 1;
16693 int flag_enum = 1;
16694
16695 auto_obstack obstack;
16696 std::vector<struct field> fields;
16697
16698 for (child_die = die->child;
16699 child_die != NULL && child_die->tag;
16700 child_die = child_die->sibling)
16701 {
16702 struct attribute *attr;
16703 LONGEST value;
16704 const gdb_byte *bytes;
16705 struct dwarf2_locexpr_baton *baton;
16706 const char *name;
16707
16708 if (child_die->tag != DW_TAG_enumerator)
16709 continue;
16710
16711 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16712 if (attr == NULL)
16713 continue;
16714
16715 name = dwarf2_name (child_die, cu);
16716 if (name == NULL)
16717 name = "<anonymous enumerator>";
16718
16719 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16720 &value, &bytes, &baton);
16721 if (value < 0)
16722 {
16723 unsigned_enum = 0;
16724 flag_enum = 0;
16725 }
16726 else
16727 {
16728 if (count_one_bits_ll (value) >= 2)
16729 flag_enum = 0;
16730 }
16731
16732 fields.emplace_back ();
16733 struct field &field = fields.back ();
16734 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16735 SET_FIELD_ENUMVAL (field, value);
16736 }
16737
16738 if (!fields.empty ())
16739 {
16740 type->set_num_fields (fields.size ());
16741 type->set_fields
16742 ((struct field *)
16743 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16744 memcpy (type->fields (), fields.data (),
16745 sizeof (struct field) * fields.size ());
16746 }
16747
16748 if (unsigned_enum)
16749 type->set_is_unsigned (true);
16750
16751 if (flag_enum)
16752 TYPE_FLAG_ENUM (type) = 1;
16753 }
16754
16755 /* Given a DW_AT_enumeration_type die, set its type. We do not
16756 complete the type's fields yet, or create any symbols. */
16757
16758 static struct type *
16759 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16760 {
16761 struct objfile *objfile = cu->per_objfile->objfile;
16762 struct type *type;
16763 struct attribute *attr;
16764 const char *name;
16765
16766 /* If the definition of this type lives in .debug_types, read that type.
16767 Don't follow DW_AT_specification though, that will take us back up
16768 the chain and we want to go down. */
16769 attr = die->attr (DW_AT_signature);
16770 if (attr != nullptr)
16771 {
16772 type = get_DW_AT_signature_type (die, attr, cu);
16773
16774 /* The type's CU may not be the same as CU.
16775 Ensure TYPE is recorded with CU in die_type_hash. */
16776 return set_die_type (die, type, cu);
16777 }
16778
16779 type = alloc_type (objfile);
16780
16781 type->set_code (TYPE_CODE_ENUM);
16782 name = dwarf2_full_name (NULL, die, cu);
16783 if (name != NULL)
16784 type->set_name (name);
16785
16786 attr = dwarf2_attr (die, DW_AT_type, cu);
16787 if (attr != NULL)
16788 {
16789 struct type *underlying_type = die_type (die, cu);
16790
16791 TYPE_TARGET_TYPE (type) = underlying_type;
16792 }
16793
16794 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16795 if (attr != nullptr)
16796 {
16797 TYPE_LENGTH (type) = attr->constant_value (0);
16798 }
16799 else
16800 {
16801 TYPE_LENGTH (type) = 0;
16802 }
16803
16804 maybe_set_alignment (cu, die, type);
16805
16806 /* The enumeration DIE can be incomplete. In Ada, any type can be
16807 declared as private in the package spec, and then defined only
16808 inside the package body. Such types are known as Taft Amendment
16809 Types. When another package uses such a type, an incomplete DIE
16810 may be generated by the compiler. */
16811 if (die_is_declaration (die, cu))
16812 type->set_is_stub (true);
16813
16814 /* If this type has an underlying type that is not a stub, then we
16815 may use its attributes. We always use the "unsigned" attribute
16816 in this situation, because ordinarily we guess whether the type
16817 is unsigned -- but the guess can be wrong and the underlying type
16818 can tell us the reality. However, we defer to a local size
16819 attribute if one exists, because this lets the compiler override
16820 the underlying type if needed. */
16821 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16822 {
16823 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16824 underlying_type = check_typedef (underlying_type);
16825
16826 type->set_is_unsigned (underlying_type->is_unsigned ());
16827
16828 if (TYPE_LENGTH (type) == 0)
16829 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16830
16831 if (TYPE_RAW_ALIGN (type) == 0
16832 && TYPE_RAW_ALIGN (underlying_type) != 0)
16833 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16834 }
16835
16836 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16837
16838 set_die_type (die, type, cu);
16839
16840 /* Finish the creation of this type by using the enum's children.
16841 Note that, as usual, this must come after set_die_type to avoid
16842 infinite recursion when trying to compute the names of the
16843 enumerators. */
16844 update_enumeration_type_from_children (die, type, cu);
16845
16846 return type;
16847 }
16848
16849 /* Given a pointer to a die which begins an enumeration, process all
16850 the dies that define the members of the enumeration, and create the
16851 symbol for the enumeration type.
16852
16853 NOTE: We reverse the order of the element list. */
16854
16855 static void
16856 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16857 {
16858 struct type *this_type;
16859
16860 this_type = get_die_type (die, cu);
16861 if (this_type == NULL)
16862 this_type = read_enumeration_type (die, cu);
16863
16864 if (die->child != NULL)
16865 {
16866 struct die_info *child_die;
16867 const char *name;
16868
16869 child_die = die->child;
16870 while (child_die && child_die->tag)
16871 {
16872 if (child_die->tag != DW_TAG_enumerator)
16873 {
16874 process_die (child_die, cu);
16875 }
16876 else
16877 {
16878 name = dwarf2_name (child_die, cu);
16879 if (name)
16880 new_symbol (child_die, this_type, cu);
16881 }
16882
16883 child_die = child_die->sibling;
16884 }
16885 }
16886
16887 /* If we are reading an enum from a .debug_types unit, and the enum
16888 is a declaration, and the enum is not the signatured type in the
16889 unit, then we do not want to add a symbol for it. Adding a
16890 symbol would in some cases obscure the true definition of the
16891 enum, giving users an incomplete type when the definition is
16892 actually available. Note that we do not want to do this for all
16893 enums which are just declarations, because C++0x allows forward
16894 enum declarations. */
16895 if (cu->per_cu->is_debug_types
16896 && die_is_declaration (die, cu))
16897 {
16898 struct signatured_type *sig_type;
16899
16900 sig_type = (struct signatured_type *) cu->per_cu;
16901 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16902 if (sig_type->type_offset_in_section != die->sect_off)
16903 return;
16904 }
16905
16906 new_symbol (die, this_type, cu);
16907 }
16908
16909 /* Helper function for quirk_ada_thick_pointer that examines a bounds
16910 expression for an index type and finds the corresponding field
16911 offset in the hidden "P_BOUNDS" structure. Returns true on success
16912 and updates *FIELD, false if it fails to recognize an
16913 expression. */
16914
16915 static bool
16916 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16917 int *bounds_offset, struct field *field,
16918 struct dwarf2_cu *cu)
16919 {
16920 struct attribute *attr = dwarf2_attr (die, name, cu);
16921 if (attr == nullptr || !attr->form_is_block ())
16922 return false;
16923
16924 const struct dwarf_block *block = attr->as_block ();
16925 const gdb_byte *start = block->data;
16926 const gdb_byte *end = block->data + block->size;
16927
16928 /* The expression to recognize generally looks like:
16929
16930 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16931 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16932
16933 However, the second "plus_uconst" may be missing:
16934
16935 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16936 DW_OP_deref_size: 4)
16937
16938 This happens when the field is at the start of the structure.
16939
16940 Also, the final deref may not be sized:
16941
16942 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16943 DW_OP_deref)
16944
16945 This happens when the size of the index type happens to be the
16946 same as the architecture's word size. This can occur with or
16947 without the second plus_uconst. */
16948
16949 if (end - start < 2)
16950 return false;
16951 if (*start++ != DW_OP_push_object_address)
16952 return false;
16953 if (*start++ != DW_OP_plus_uconst)
16954 return false;
16955
16956 uint64_t this_bound_off;
16957 start = gdb_read_uleb128 (start, end, &this_bound_off);
16958 if (start == nullptr || (int) this_bound_off != this_bound_off)
16959 return false;
16960 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16961 is consistent among all bounds. */
16962 if (*bounds_offset == -1)
16963 *bounds_offset = this_bound_off;
16964 else if (*bounds_offset != this_bound_off)
16965 return false;
16966
16967 if (start == end || *start++ != DW_OP_deref)
16968 return false;
16969
16970 int offset = 0;
16971 if (start ==end)
16972 return false;
16973 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16974 {
16975 /* This means an offset of 0. */
16976 }
16977 else if (*start++ != DW_OP_plus_uconst)
16978 return false;
16979 else
16980 {
16981 /* The size is the parameter to DW_OP_plus_uconst. */
16982 uint64_t val;
16983 start = gdb_read_uleb128 (start, end, &val);
16984 if (start == nullptr)
16985 return false;
16986 if ((int) val != val)
16987 return false;
16988 offset = val;
16989 }
16990
16991 if (start == end)
16992 return false;
16993
16994 uint64_t size;
16995 if (*start == DW_OP_deref_size)
16996 {
16997 start = gdb_read_uleb128 (start + 1, end, &size);
16998 if (start == nullptr)
16999 return false;
17000 }
17001 else if (*start == DW_OP_deref)
17002 {
17003 size = cu->header.addr_size;
17004 ++start;
17005 }
17006 else
17007 return false;
17008
17009 SET_FIELD_BITPOS (*field, 8 * offset);
17010 if (size != TYPE_LENGTH (field->type ()))
17011 FIELD_BITSIZE (*field) = 8 * size;
17012
17013 return true;
17014 }
17015
17016 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
17017 some kinds of Ada arrays:
17018
17019 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
17020 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
17021 <11e0> DW_AT_data_location: 2 byte block: 97 6
17022 (DW_OP_push_object_address; DW_OP_deref)
17023 <11e3> DW_AT_type : <0x1173>
17024 <11e7> DW_AT_sibling : <0x1201>
17025 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
17026 <11ec> DW_AT_type : <0x1206>
17027 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
17028 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
17029 DW_OP_deref_size: 4)
17030 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
17031 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
17032 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
17033
17034 This actually represents a "thick pointer", which is a structure
17035 with two elements: one that is a pointer to the array data, and one
17036 that is a pointer to another structure; this second structure holds
17037 the array bounds.
17038
17039 This returns a new type on success, or nullptr if this didn't
17040 recognize the type. */
17041
17042 static struct type *
17043 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
17044 struct type *type)
17045 {
17046 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
17047 /* So far we've only seen this with block form. */
17048 if (attr == nullptr || !attr->form_is_block ())
17049 return nullptr;
17050
17051 /* Note that this will fail if the structure layout is changed by
17052 the compiler. However, we have no good way to recognize some
17053 other layout, because we don't know what expression the compiler
17054 might choose to emit should this happen. */
17055 struct dwarf_block *blk = attr->as_block ();
17056 if (blk->size != 2
17057 || blk->data[0] != DW_OP_push_object_address
17058 || blk->data[1] != DW_OP_deref)
17059 return nullptr;
17060
17061 int bounds_offset = -1;
17062 int max_align = -1;
17063 std::vector<struct field> range_fields;
17064 for (struct die_info *child_die = die->child;
17065 child_die;
17066 child_die = child_die->sibling)
17067 {
17068 if (child_die->tag == DW_TAG_subrange_type)
17069 {
17070 struct type *underlying = read_subrange_index_type (child_die, cu);
17071
17072 int this_align = type_align (underlying);
17073 if (this_align > max_align)
17074 max_align = this_align;
17075
17076 range_fields.emplace_back ();
17077 range_fields.emplace_back ();
17078
17079 struct field &lower = range_fields[range_fields.size () - 2];
17080 struct field &upper = range_fields[range_fields.size () - 1];
17081
17082 lower.set_type (underlying);
17083 FIELD_ARTIFICIAL (lower) = 1;
17084
17085 upper.set_type (underlying);
17086 FIELD_ARTIFICIAL (upper) = 1;
17087
17088 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
17089 &bounds_offset, &lower, cu)
17090 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
17091 &bounds_offset, &upper, cu))
17092 return nullptr;
17093 }
17094 }
17095
17096 /* This shouldn't really happen, but double-check that we found
17097 where the bounds are stored. */
17098 if (bounds_offset == -1)
17099 return nullptr;
17100
17101 struct objfile *objfile = cu->per_objfile->objfile;
17102 for (int i = 0; i < range_fields.size (); i += 2)
17103 {
17104 char name[20];
17105
17106 /* Set the name of each field in the bounds. */
17107 xsnprintf (name, sizeof (name), "LB%d", i / 2);
17108 FIELD_NAME (range_fields[i]) = objfile->intern (name);
17109 xsnprintf (name, sizeof (name), "UB%d", i / 2);
17110 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
17111 }
17112
17113 struct type *bounds = alloc_type (objfile);
17114 bounds->set_code (TYPE_CODE_STRUCT);
17115
17116 bounds->set_num_fields (range_fields.size ());
17117 bounds->set_fields
17118 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
17119 * sizeof (struct field))));
17120 memcpy (bounds->fields (), range_fields.data (),
17121 bounds->num_fields () * sizeof (struct field));
17122
17123 int last_fieldno = range_fields.size () - 1;
17124 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
17125 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
17126 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
17127
17128 /* Rewrite the existing array type in place. Specifically, we
17129 remove any dynamic properties we might have read, and we replace
17130 the index types. */
17131 struct type *iter = type;
17132 for (int i = 0; i < range_fields.size (); i += 2)
17133 {
17134 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
17135 iter->main_type->dyn_prop_list = nullptr;
17136 iter->set_index_type
17137 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
17138 iter = TYPE_TARGET_TYPE (iter);
17139 }
17140
17141 struct type *result = alloc_type (objfile);
17142 result->set_code (TYPE_CODE_STRUCT);
17143
17144 result->set_num_fields (2);
17145 result->set_fields
17146 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
17147 * sizeof (struct field))));
17148
17149 /* The names are chosen to coincide with what the compiler does with
17150 -fgnat-encodings=all, which the Ada code in gdb already
17151 understands. */
17152 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
17153 result->field (0).set_type (lookup_pointer_type (type));
17154
17155 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
17156 result->field (1).set_type (lookup_pointer_type (bounds));
17157 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
17158
17159 result->set_name (type->name ());
17160 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
17161 + TYPE_LENGTH (result->field (1).type ()));
17162
17163 return result;
17164 }
17165
17166 /* Extract all information from a DW_TAG_array_type DIE and put it in
17167 the DIE's type field. For now, this only handles one dimensional
17168 arrays. */
17169
17170 static struct type *
17171 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
17172 {
17173 struct objfile *objfile = cu->per_objfile->objfile;
17174 struct die_info *child_die;
17175 struct type *type;
17176 struct type *element_type, *range_type, *index_type;
17177 struct attribute *attr;
17178 const char *name;
17179 struct dynamic_prop *byte_stride_prop = NULL;
17180 unsigned int bit_stride = 0;
17181
17182 element_type = die_type (die, cu);
17183
17184 /* The die_type call above may have already set the type for this DIE. */
17185 type = get_die_type (die, cu);
17186 if (type)
17187 return type;
17188
17189 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
17190 if (attr != NULL)
17191 {
17192 int stride_ok;
17193 struct type *prop_type = cu->addr_sized_int_type (false);
17194
17195 byte_stride_prop
17196 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
17197 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
17198 prop_type);
17199 if (!stride_ok)
17200 {
17201 complaint (_("unable to read array DW_AT_byte_stride "
17202 " - DIE at %s [in module %s]"),
17203 sect_offset_str (die->sect_off),
17204 objfile_name (cu->per_objfile->objfile));
17205 /* Ignore this attribute. We will likely not be able to print
17206 arrays of this type correctly, but there is little we can do
17207 to help if we cannot read the attribute's value. */
17208 byte_stride_prop = NULL;
17209 }
17210 }
17211
17212 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
17213 if (attr != NULL)
17214 bit_stride = attr->constant_value (0);
17215
17216 /* Irix 6.2 native cc creates array types without children for
17217 arrays with unspecified length. */
17218 if (die->child == NULL)
17219 {
17220 index_type = objfile_type (objfile)->builtin_int;
17221 range_type = create_static_range_type (NULL, index_type, 0, -1);
17222 type = create_array_type_with_stride (NULL, element_type, range_type,
17223 byte_stride_prop, bit_stride);
17224 return set_die_type (die, type, cu);
17225 }
17226
17227 std::vector<struct type *> range_types;
17228 child_die = die->child;
17229 while (child_die && child_die->tag)
17230 {
17231 if (child_die->tag == DW_TAG_subrange_type)
17232 {
17233 struct type *child_type = read_type_die (child_die, cu);
17234
17235 if (child_type != NULL)
17236 {
17237 /* The range type was succesfully read. Save it for the
17238 array type creation. */
17239 range_types.push_back (child_type);
17240 }
17241 }
17242 child_die = child_die->sibling;
17243 }
17244
17245 if (range_types.empty ())
17246 {
17247 complaint (_("unable to find array range - DIE at %s [in module %s]"),
17248 sect_offset_str (die->sect_off),
17249 objfile_name (cu->per_objfile->objfile));
17250 return NULL;
17251 }
17252
17253 /* Dwarf2 dimensions are output from left to right, create the
17254 necessary array types in backwards order. */
17255
17256 type = element_type;
17257
17258 if (read_array_order (die, cu) == DW_ORD_col_major)
17259 {
17260 int i = 0;
17261
17262 while (i < range_types.size ())
17263 {
17264 type = create_array_type_with_stride (NULL, type, range_types[i++],
17265 byte_stride_prop, bit_stride);
17266 bit_stride = 0;
17267 byte_stride_prop = nullptr;
17268 }
17269 }
17270 else
17271 {
17272 size_t ndim = range_types.size ();
17273 while (ndim-- > 0)
17274 {
17275 type = create_array_type_with_stride (NULL, type, range_types[ndim],
17276 byte_stride_prop, bit_stride);
17277 bit_stride = 0;
17278 byte_stride_prop = nullptr;
17279 }
17280 }
17281
17282 gdb_assert (type != element_type);
17283
17284 /* Understand Dwarf2 support for vector types (like they occur on
17285 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
17286 array type. This is not part of the Dwarf2/3 standard yet, but a
17287 custom vendor extension. The main difference between a regular
17288 array and the vector variant is that vectors are passed by value
17289 to functions. */
17290 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
17291 if (attr != nullptr)
17292 make_vector_type (type);
17293
17294 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
17295 implementation may choose to implement triple vectors using this
17296 attribute. */
17297 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17298 if (attr != nullptr && attr->form_is_unsigned ())
17299 {
17300 if (attr->as_unsigned () >= TYPE_LENGTH (type))
17301 TYPE_LENGTH (type) = attr->as_unsigned ();
17302 else
17303 complaint (_("DW_AT_byte_size for array type smaller "
17304 "than the total size of elements"));
17305 }
17306
17307 name = dwarf2_name (die, cu);
17308 if (name)
17309 type->set_name (name);
17310
17311 maybe_set_alignment (cu, die, type);
17312
17313 struct type *replacement_type = nullptr;
17314 if (cu->language == language_ada)
17315 {
17316 replacement_type = quirk_ada_thick_pointer (die, cu, type);
17317 if (replacement_type != nullptr)
17318 type = replacement_type;
17319 }
17320
17321 /* Install the type in the die. */
17322 set_die_type (die, type, cu, replacement_type != nullptr);
17323
17324 /* set_die_type should be already done. */
17325 set_descriptive_type (type, die, cu);
17326
17327 return type;
17328 }
17329
17330 static enum dwarf_array_dim_ordering
17331 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
17332 {
17333 struct attribute *attr;
17334
17335 attr = dwarf2_attr (die, DW_AT_ordering, cu);
17336
17337 if (attr != nullptr)
17338 {
17339 LONGEST val = attr->constant_value (-1);
17340 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
17341 return (enum dwarf_array_dim_ordering) val;
17342 }
17343
17344 /* GNU F77 is a special case, as at 08/2004 array type info is the
17345 opposite order to the dwarf2 specification, but data is still
17346 laid out as per normal fortran.
17347
17348 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
17349 version checking. */
17350
17351 if (cu->language == language_fortran
17352 && cu->producer && strstr (cu->producer, "GNU F77"))
17353 {
17354 return DW_ORD_row_major;
17355 }
17356
17357 switch (cu->language_defn->array_ordering ())
17358 {
17359 case array_column_major:
17360 return DW_ORD_col_major;
17361 case array_row_major:
17362 default:
17363 return DW_ORD_row_major;
17364 };
17365 }
17366
17367 /* Extract all information from a DW_TAG_set_type DIE and put it in
17368 the DIE's type field. */
17369
17370 static struct type *
17371 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
17372 {
17373 struct type *domain_type, *set_type;
17374 struct attribute *attr;
17375
17376 domain_type = die_type (die, cu);
17377
17378 /* The die_type call above may have already set the type for this DIE. */
17379 set_type = get_die_type (die, cu);
17380 if (set_type)
17381 return set_type;
17382
17383 set_type = create_set_type (NULL, domain_type);
17384
17385 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17386 if (attr != nullptr && attr->form_is_unsigned ())
17387 TYPE_LENGTH (set_type) = attr->as_unsigned ();
17388
17389 maybe_set_alignment (cu, die, set_type);
17390
17391 return set_die_type (die, set_type, cu);
17392 }
17393
17394 /* A helper for read_common_block that creates a locexpr baton.
17395 SYM is the symbol which we are marking as computed.
17396 COMMON_DIE is the DIE for the common block.
17397 COMMON_LOC is the location expression attribute for the common
17398 block itself.
17399 MEMBER_LOC is the location expression attribute for the particular
17400 member of the common block that we are processing.
17401 CU is the CU from which the above come. */
17402
17403 static void
17404 mark_common_block_symbol_computed (struct symbol *sym,
17405 struct die_info *common_die,
17406 struct attribute *common_loc,
17407 struct attribute *member_loc,
17408 struct dwarf2_cu *cu)
17409 {
17410 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17411 struct objfile *objfile = per_objfile->objfile;
17412 struct dwarf2_locexpr_baton *baton;
17413 gdb_byte *ptr;
17414 unsigned int cu_off;
17415 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
17416 LONGEST offset = 0;
17417
17418 gdb_assert (common_loc && member_loc);
17419 gdb_assert (common_loc->form_is_block ());
17420 gdb_assert (member_loc->form_is_block ()
17421 || member_loc->form_is_constant ());
17422
17423 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
17424 baton->per_objfile = per_objfile;
17425 baton->per_cu = cu->per_cu;
17426 gdb_assert (baton->per_cu);
17427
17428 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17429
17430 if (member_loc->form_is_constant ())
17431 {
17432 offset = member_loc->constant_value (0);
17433 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17434 }
17435 else
17436 baton->size += member_loc->as_block ()->size;
17437
17438 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
17439 baton->data = ptr;
17440
17441 *ptr++ = DW_OP_call4;
17442 cu_off = common_die->sect_off - cu->per_cu->sect_off;
17443 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17444 ptr += 4;
17445
17446 if (member_loc->form_is_constant ())
17447 {
17448 *ptr++ = DW_OP_addr;
17449 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17450 ptr += cu->header.addr_size;
17451 }
17452 else
17453 {
17454 /* We have to copy the data here, because DW_OP_call4 will only
17455 use a DW_AT_location attribute. */
17456 struct dwarf_block *block = member_loc->as_block ();
17457 memcpy (ptr, block->data, block->size);
17458 ptr += block->size;
17459 }
17460
17461 *ptr++ = DW_OP_plus;
17462 gdb_assert (ptr - baton->data == baton->size);
17463
17464 SYMBOL_LOCATION_BATON (sym) = baton;
17465 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
17466 }
17467
17468 /* Create appropriate locally-scoped variables for all the
17469 DW_TAG_common_block entries. Also create a struct common_block
17470 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
17471 is used to separate the common blocks name namespace from regular
17472 variable names. */
17473
17474 static void
17475 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
17476 {
17477 struct attribute *attr;
17478
17479 attr = dwarf2_attr (die, DW_AT_location, cu);
17480 if (attr != nullptr)
17481 {
17482 /* Support the .debug_loc offsets. */
17483 if (attr->form_is_block ())
17484 {
17485 /* Ok. */
17486 }
17487 else if (attr->form_is_section_offset ())
17488 {
17489 dwarf2_complex_location_expr_complaint ();
17490 attr = NULL;
17491 }
17492 else
17493 {
17494 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17495 "common block member");
17496 attr = NULL;
17497 }
17498 }
17499
17500 if (die->child != NULL)
17501 {
17502 struct objfile *objfile = cu->per_objfile->objfile;
17503 struct die_info *child_die;
17504 size_t n_entries = 0, size;
17505 struct common_block *common_block;
17506 struct symbol *sym;
17507
17508 for (child_die = die->child;
17509 child_die && child_die->tag;
17510 child_die = child_die->sibling)
17511 ++n_entries;
17512
17513 size = (sizeof (struct common_block)
17514 + (n_entries - 1) * sizeof (struct symbol *));
17515 common_block
17516 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17517 size);
17518 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17519 common_block->n_entries = 0;
17520
17521 for (child_die = die->child;
17522 child_die && child_die->tag;
17523 child_die = child_die->sibling)
17524 {
17525 /* Create the symbol in the DW_TAG_common_block block in the current
17526 symbol scope. */
17527 sym = new_symbol (child_die, NULL, cu);
17528 if (sym != NULL)
17529 {
17530 struct attribute *member_loc;
17531
17532 common_block->contents[common_block->n_entries++] = sym;
17533
17534 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17535 cu);
17536 if (member_loc)
17537 {
17538 /* GDB has handled this for a long time, but it is
17539 not specified by DWARF. It seems to have been
17540 emitted by gfortran at least as recently as:
17541 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17542 complaint (_("Variable in common block has "
17543 "DW_AT_data_member_location "
17544 "- DIE at %s [in module %s]"),
17545 sect_offset_str (child_die->sect_off),
17546 objfile_name (objfile));
17547
17548 if (member_loc->form_is_section_offset ())
17549 dwarf2_complex_location_expr_complaint ();
17550 else if (member_loc->form_is_constant ()
17551 || member_loc->form_is_block ())
17552 {
17553 if (attr != nullptr)
17554 mark_common_block_symbol_computed (sym, die, attr,
17555 member_loc, cu);
17556 }
17557 else
17558 dwarf2_complex_location_expr_complaint ();
17559 }
17560 }
17561 }
17562
17563 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17564 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
17565 }
17566 }
17567
17568 /* Create a type for a C++ namespace. */
17569
17570 static struct type *
17571 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
17572 {
17573 struct objfile *objfile = cu->per_objfile->objfile;
17574 const char *previous_prefix, *name;
17575 int is_anonymous;
17576 struct type *type;
17577
17578 /* For extensions, reuse the type of the original namespace. */
17579 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17580 {
17581 struct die_info *ext_die;
17582 struct dwarf2_cu *ext_cu = cu;
17583
17584 ext_die = dwarf2_extension (die, &ext_cu);
17585 type = read_type_die (ext_die, ext_cu);
17586
17587 /* EXT_CU may not be the same as CU.
17588 Ensure TYPE is recorded with CU in die_type_hash. */
17589 return set_die_type (die, type, cu);
17590 }
17591
17592 name = namespace_name (die, &is_anonymous, cu);
17593
17594 /* Now build the name of the current namespace. */
17595
17596 previous_prefix = determine_prefix (die, cu);
17597 if (previous_prefix[0] != '\0')
17598 name = typename_concat (&objfile->objfile_obstack,
17599 previous_prefix, name, 0, cu);
17600
17601 /* Create the type. */
17602 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17603
17604 return set_die_type (die, type, cu);
17605 }
17606
17607 /* Read a namespace scope. */
17608
17609 static void
17610 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17611 {
17612 struct objfile *objfile = cu->per_objfile->objfile;
17613 int is_anonymous;
17614
17615 /* Add a symbol associated to this if we haven't seen the namespace
17616 before. Also, add a using directive if it's an anonymous
17617 namespace. */
17618
17619 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17620 {
17621 struct type *type;
17622
17623 type = read_type_die (die, cu);
17624 new_symbol (die, type, cu);
17625
17626 namespace_name (die, &is_anonymous, cu);
17627 if (is_anonymous)
17628 {
17629 const char *previous_prefix = determine_prefix (die, cu);
17630
17631 std::vector<const char *> excludes;
17632 add_using_directive (using_directives (cu),
17633 previous_prefix, type->name (), NULL,
17634 NULL, excludes, 0, &objfile->objfile_obstack);
17635 }
17636 }
17637
17638 if (die->child != NULL)
17639 {
17640 struct die_info *child_die = die->child;
17641
17642 while (child_die && child_die->tag)
17643 {
17644 process_die (child_die, cu);
17645 child_die = child_die->sibling;
17646 }
17647 }
17648 }
17649
17650 /* Read a Fortran module as type. This DIE can be only a declaration used for
17651 imported module. Still we need that type as local Fortran "use ... only"
17652 declaration imports depend on the created type in determine_prefix. */
17653
17654 static struct type *
17655 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17656 {
17657 struct objfile *objfile = cu->per_objfile->objfile;
17658 const char *module_name;
17659 struct type *type;
17660
17661 module_name = dwarf2_name (die, cu);
17662 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17663
17664 return set_die_type (die, type, cu);
17665 }
17666
17667 /* Read a Fortran module. */
17668
17669 static void
17670 read_module (struct die_info *die, struct dwarf2_cu *cu)
17671 {
17672 struct die_info *child_die = die->child;
17673 struct type *type;
17674
17675 type = read_type_die (die, cu);
17676 new_symbol (die, type, cu);
17677
17678 while (child_die && child_die->tag)
17679 {
17680 process_die (child_die, cu);
17681 child_die = child_die->sibling;
17682 }
17683 }
17684
17685 /* Return the name of the namespace represented by DIE. Set
17686 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17687 namespace. */
17688
17689 static const char *
17690 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17691 {
17692 struct die_info *current_die;
17693 const char *name = NULL;
17694
17695 /* Loop through the extensions until we find a name. */
17696
17697 for (current_die = die;
17698 current_die != NULL;
17699 current_die = dwarf2_extension (die, &cu))
17700 {
17701 /* We don't use dwarf2_name here so that we can detect the absence
17702 of a name -> anonymous namespace. */
17703 name = dwarf2_string_attr (die, DW_AT_name, cu);
17704
17705 if (name != NULL)
17706 break;
17707 }
17708
17709 /* Is it an anonymous namespace? */
17710
17711 *is_anonymous = (name == NULL);
17712 if (*is_anonymous)
17713 name = CP_ANONYMOUS_NAMESPACE_STR;
17714
17715 return name;
17716 }
17717
17718 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17719 the user defined type vector. */
17720
17721 static struct type *
17722 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17723 {
17724 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
17725 struct comp_unit_head *cu_header = &cu->header;
17726 struct type *type;
17727 struct attribute *attr_byte_size;
17728 struct attribute *attr_address_class;
17729 int byte_size, addr_class;
17730 struct type *target_type;
17731
17732 target_type = die_type (die, cu);
17733
17734 /* The die_type call above may have already set the type for this DIE. */
17735 type = get_die_type (die, cu);
17736 if (type)
17737 return type;
17738
17739 type = lookup_pointer_type (target_type);
17740
17741 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17742 if (attr_byte_size)
17743 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
17744 else
17745 byte_size = cu_header->addr_size;
17746
17747 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17748 if (attr_address_class)
17749 addr_class = attr_address_class->constant_value (DW_ADDR_none);
17750 else
17751 addr_class = DW_ADDR_none;
17752
17753 ULONGEST alignment = get_alignment (cu, die);
17754
17755 /* If the pointer size, alignment, or address class is different
17756 than the default, create a type variant marked as such and set
17757 the length accordingly. */
17758 if (TYPE_LENGTH (type) != byte_size
17759 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17760 && alignment != TYPE_RAW_ALIGN (type))
17761 || addr_class != DW_ADDR_none)
17762 {
17763 if (gdbarch_address_class_type_flags_p (gdbarch))
17764 {
17765 type_instance_flags type_flags
17766 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17767 addr_class);
17768 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17769 == 0);
17770 type = make_type_with_address_space (type, type_flags);
17771 }
17772 else if (TYPE_LENGTH (type) != byte_size)
17773 {
17774 complaint (_("invalid pointer size %d"), byte_size);
17775 }
17776 else if (TYPE_RAW_ALIGN (type) != alignment)
17777 {
17778 complaint (_("Invalid DW_AT_alignment"
17779 " - DIE at %s [in module %s]"),
17780 sect_offset_str (die->sect_off),
17781 objfile_name (cu->per_objfile->objfile));
17782 }
17783 else
17784 {
17785 /* Should we also complain about unhandled address classes? */
17786 }
17787 }
17788
17789 TYPE_LENGTH (type) = byte_size;
17790 set_type_align (type, alignment);
17791 return set_die_type (die, type, cu);
17792 }
17793
17794 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17795 the user defined type vector. */
17796
17797 static struct type *
17798 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17799 {
17800 struct type *type;
17801 struct type *to_type;
17802 struct type *domain;
17803
17804 to_type = die_type (die, cu);
17805 domain = die_containing_type (die, cu);
17806
17807 /* The calls above may have already set the type for this DIE. */
17808 type = get_die_type (die, cu);
17809 if (type)
17810 return type;
17811
17812 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17813 type = lookup_methodptr_type (to_type);
17814 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17815 {
17816 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17817
17818 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17819 to_type->fields (), to_type->num_fields (),
17820 to_type->has_varargs ());
17821 type = lookup_methodptr_type (new_type);
17822 }
17823 else
17824 type = lookup_memberptr_type (to_type, domain);
17825
17826 return set_die_type (die, type, cu);
17827 }
17828
17829 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17830 the user defined type vector. */
17831
17832 static struct type *
17833 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17834 enum type_code refcode)
17835 {
17836 struct comp_unit_head *cu_header = &cu->header;
17837 struct type *type, *target_type;
17838 struct attribute *attr;
17839
17840 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17841
17842 target_type = die_type (die, cu);
17843
17844 /* The die_type call above may have already set the type for this DIE. */
17845 type = get_die_type (die, cu);
17846 if (type)
17847 return type;
17848
17849 type = lookup_reference_type (target_type, refcode);
17850 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17851 if (attr != nullptr)
17852 {
17853 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
17854 }
17855 else
17856 {
17857 TYPE_LENGTH (type) = cu_header->addr_size;
17858 }
17859 maybe_set_alignment (cu, die, type);
17860 return set_die_type (die, type, cu);
17861 }
17862
17863 /* Add the given cv-qualifiers to the element type of the array. GCC
17864 outputs DWARF type qualifiers that apply to an array, not the
17865 element type. But GDB relies on the array element type to carry
17866 the cv-qualifiers. This mimics section 6.7.3 of the C99
17867 specification. */
17868
17869 static struct type *
17870 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17871 struct type *base_type, int cnst, int voltl)
17872 {
17873 struct type *el_type, *inner_array;
17874
17875 base_type = copy_type (base_type);
17876 inner_array = base_type;
17877
17878 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17879 {
17880 TYPE_TARGET_TYPE (inner_array) =
17881 copy_type (TYPE_TARGET_TYPE (inner_array));
17882 inner_array = TYPE_TARGET_TYPE (inner_array);
17883 }
17884
17885 el_type = TYPE_TARGET_TYPE (inner_array);
17886 cnst |= TYPE_CONST (el_type);
17887 voltl |= TYPE_VOLATILE (el_type);
17888 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17889
17890 return set_die_type (die, base_type, cu);
17891 }
17892
17893 static struct type *
17894 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17895 {
17896 struct type *base_type, *cv_type;
17897
17898 base_type = die_type (die, cu);
17899
17900 /* The die_type call above may have already set the type for this DIE. */
17901 cv_type = get_die_type (die, cu);
17902 if (cv_type)
17903 return cv_type;
17904
17905 /* In case the const qualifier is applied to an array type, the element type
17906 is so qualified, not the array type (section 6.7.3 of C99). */
17907 if (base_type->code () == TYPE_CODE_ARRAY)
17908 return add_array_cv_type (die, cu, base_type, 1, 0);
17909
17910 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17911 return set_die_type (die, cv_type, cu);
17912 }
17913
17914 static struct type *
17915 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17916 {
17917 struct type *base_type, *cv_type;
17918
17919 base_type = die_type (die, cu);
17920
17921 /* The die_type call above may have already set the type for this DIE. */
17922 cv_type = get_die_type (die, cu);
17923 if (cv_type)
17924 return cv_type;
17925
17926 /* In case the volatile qualifier is applied to an array type, the
17927 element type is so qualified, not the array type (section 6.7.3
17928 of C99). */
17929 if (base_type->code () == TYPE_CODE_ARRAY)
17930 return add_array_cv_type (die, cu, base_type, 0, 1);
17931
17932 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17933 return set_die_type (die, cv_type, cu);
17934 }
17935
17936 /* Handle DW_TAG_restrict_type. */
17937
17938 static struct type *
17939 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17940 {
17941 struct type *base_type, *cv_type;
17942
17943 base_type = die_type (die, cu);
17944
17945 /* The die_type call above may have already set the type for this DIE. */
17946 cv_type = get_die_type (die, cu);
17947 if (cv_type)
17948 return cv_type;
17949
17950 cv_type = make_restrict_type (base_type);
17951 return set_die_type (die, cv_type, cu);
17952 }
17953
17954 /* Handle DW_TAG_atomic_type. */
17955
17956 static struct type *
17957 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17958 {
17959 struct type *base_type, *cv_type;
17960
17961 base_type = die_type (die, cu);
17962
17963 /* The die_type call above may have already set the type for this DIE. */
17964 cv_type = get_die_type (die, cu);
17965 if (cv_type)
17966 return cv_type;
17967
17968 cv_type = make_atomic_type (base_type);
17969 return set_die_type (die, cv_type, cu);
17970 }
17971
17972 /* Extract all information from a DW_TAG_string_type DIE and add to
17973 the user defined type vector. It isn't really a user defined type,
17974 but it behaves like one, with other DIE's using an AT_user_def_type
17975 attribute to reference it. */
17976
17977 static struct type *
17978 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17979 {
17980 struct objfile *objfile = cu->per_objfile->objfile;
17981 struct gdbarch *gdbarch = objfile->arch ();
17982 struct type *type, *range_type, *index_type, *char_type;
17983 struct attribute *attr;
17984 struct dynamic_prop prop;
17985 bool length_is_constant = true;
17986 LONGEST length;
17987
17988 /* There are a couple of places where bit sizes might be made use of
17989 when parsing a DW_TAG_string_type, however, no producer that we know
17990 of make use of these. Handling bit sizes that are a multiple of the
17991 byte size is easy enough, but what about other bit sizes? Lets deal
17992 with that problem when we have to. Warn about these attributes being
17993 unsupported, then parse the type and ignore them like we always
17994 have. */
17995 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17996 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17997 {
17998 static bool warning_printed = false;
17999 if (!warning_printed)
18000 {
18001 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
18002 "currently supported on DW_TAG_string_type."));
18003 warning_printed = true;
18004 }
18005 }
18006
18007 attr = dwarf2_attr (die, DW_AT_string_length, cu);
18008 if (attr != nullptr && !attr->form_is_constant ())
18009 {
18010 /* The string length describes the location at which the length of
18011 the string can be found. The size of the length field can be
18012 specified with one of the attributes below. */
18013 struct type *prop_type;
18014 struct attribute *len
18015 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
18016 if (len == nullptr)
18017 len = dwarf2_attr (die, DW_AT_byte_size, cu);
18018 if (len != nullptr && len->form_is_constant ())
18019 {
18020 /* Pass 0 as the default as we know this attribute is constant
18021 and the default value will not be returned. */
18022 LONGEST sz = len->constant_value (0);
18023 prop_type = cu->per_objfile->int_type (sz, true);
18024 }
18025 else
18026 {
18027 /* If the size is not specified then we assume it is the size of
18028 an address on this target. */
18029 prop_type = cu->addr_sized_int_type (true);
18030 }
18031
18032 /* Convert the attribute into a dynamic property. */
18033 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
18034 length = 1;
18035 else
18036 length_is_constant = false;
18037 }
18038 else if (attr != nullptr)
18039 {
18040 /* This DW_AT_string_length just contains the length with no
18041 indirection. There's no need to create a dynamic property in this
18042 case. Pass 0 for the default value as we know it will not be
18043 returned in this case. */
18044 length = attr->constant_value (0);
18045 }
18046 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
18047 {
18048 /* We don't currently support non-constant byte sizes for strings. */
18049 length = attr->constant_value (1);
18050 }
18051 else
18052 {
18053 /* Use 1 as a fallback length if we have nothing else. */
18054 length = 1;
18055 }
18056
18057 index_type = objfile_type (objfile)->builtin_int;
18058 if (length_is_constant)
18059 range_type = create_static_range_type (NULL, index_type, 1, length);
18060 else
18061 {
18062 struct dynamic_prop low_bound;
18063
18064 low_bound.set_const_val (1);
18065 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
18066 }
18067 char_type = language_string_char_type (cu->language_defn, gdbarch);
18068 type = create_string_type (NULL, char_type, range_type);
18069
18070 return set_die_type (die, type, cu);
18071 }
18072
18073 /* Assuming that DIE corresponds to a function, returns nonzero
18074 if the function is prototyped. */
18075
18076 static int
18077 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
18078 {
18079 struct attribute *attr;
18080
18081 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
18082 if (attr && attr->as_boolean ())
18083 return 1;
18084
18085 /* The DWARF standard implies that the DW_AT_prototyped attribute
18086 is only meaningful for C, but the concept also extends to other
18087 languages that allow unprototyped functions (Eg: Objective C).
18088 For all other languages, assume that functions are always
18089 prototyped. */
18090 if (cu->language != language_c
18091 && cu->language != language_objc
18092 && cu->language != language_opencl)
18093 return 1;
18094
18095 /* RealView does not emit DW_AT_prototyped. We can not distinguish
18096 prototyped and unprototyped functions; default to prototyped,
18097 since that is more common in modern code (and RealView warns
18098 about unprototyped functions). */
18099 if (producer_is_realview (cu->producer))
18100 return 1;
18101
18102 return 0;
18103 }
18104
18105 /* Handle DIES due to C code like:
18106
18107 struct foo
18108 {
18109 int (*funcp)(int a, long l);
18110 int b;
18111 };
18112
18113 ('funcp' generates a DW_TAG_subroutine_type DIE). */
18114
18115 static struct type *
18116 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
18117 {
18118 struct objfile *objfile = cu->per_objfile->objfile;
18119 struct type *type; /* Type that this function returns. */
18120 struct type *ftype; /* Function that returns above type. */
18121 struct attribute *attr;
18122
18123 type = die_type (die, cu);
18124
18125 /* The die_type call above may have already set the type for this DIE. */
18126 ftype = get_die_type (die, cu);
18127 if (ftype)
18128 return ftype;
18129
18130 ftype = lookup_function_type (type);
18131
18132 if (prototyped_function_p (die, cu))
18133 ftype->set_is_prototyped (true);
18134
18135 /* Store the calling convention in the type if it's available in
18136 the subroutine die. Otherwise set the calling convention to
18137 the default value DW_CC_normal. */
18138 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
18139 if (attr != nullptr
18140 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
18141 TYPE_CALLING_CONVENTION (ftype)
18142 = (enum dwarf_calling_convention) attr->constant_value (0);
18143 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
18144 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
18145 else
18146 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
18147
18148 /* Record whether the function returns normally to its caller or not
18149 if the DWARF producer set that information. */
18150 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
18151 if (attr && attr->as_boolean ())
18152 TYPE_NO_RETURN (ftype) = 1;
18153
18154 /* We need to add the subroutine type to the die immediately so
18155 we don't infinitely recurse when dealing with parameters
18156 declared as the same subroutine type. */
18157 set_die_type (die, ftype, cu);
18158
18159 if (die->child != NULL)
18160 {
18161 struct type *void_type = objfile_type (objfile)->builtin_void;
18162 struct die_info *child_die;
18163 int nparams, iparams;
18164
18165 /* Count the number of parameters.
18166 FIXME: GDB currently ignores vararg functions, but knows about
18167 vararg member functions. */
18168 nparams = 0;
18169 child_die = die->child;
18170 while (child_die && child_die->tag)
18171 {
18172 if (child_die->tag == DW_TAG_formal_parameter)
18173 nparams++;
18174 else if (child_die->tag == DW_TAG_unspecified_parameters)
18175 ftype->set_has_varargs (true);
18176
18177 child_die = child_die->sibling;
18178 }
18179
18180 /* Allocate storage for parameters and fill them in. */
18181 ftype->set_num_fields (nparams);
18182 ftype->set_fields
18183 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
18184
18185 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
18186 even if we error out during the parameters reading below. */
18187 for (iparams = 0; iparams < nparams; iparams++)
18188 ftype->field (iparams).set_type (void_type);
18189
18190 iparams = 0;
18191 child_die = die->child;
18192 while (child_die && child_die->tag)
18193 {
18194 if (child_die->tag == DW_TAG_formal_parameter)
18195 {
18196 struct type *arg_type;
18197
18198 /* DWARF version 2 has no clean way to discern C++
18199 static and non-static member functions. G++ helps
18200 GDB by marking the first parameter for non-static
18201 member functions (which is the this pointer) as
18202 artificial. We pass this information to
18203 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
18204
18205 DWARF version 3 added DW_AT_object_pointer, which GCC
18206 4.5 does not yet generate. */
18207 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
18208 if (attr != nullptr)
18209 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
18210 else
18211 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
18212 arg_type = die_type (child_die, cu);
18213
18214 /* RealView does not mark THIS as const, which the testsuite
18215 expects. GCC marks THIS as const in method definitions,
18216 but not in the class specifications (GCC PR 43053). */
18217 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
18218 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
18219 {
18220 int is_this = 0;
18221 struct dwarf2_cu *arg_cu = cu;
18222 const char *name = dwarf2_name (child_die, cu);
18223
18224 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
18225 if (attr != nullptr)
18226 {
18227 /* If the compiler emits this, use it. */
18228 if (follow_die_ref (die, attr, &arg_cu) == child_die)
18229 is_this = 1;
18230 }
18231 else if (name && strcmp (name, "this") == 0)
18232 /* Function definitions will have the argument names. */
18233 is_this = 1;
18234 else if (name == NULL && iparams == 0)
18235 /* Declarations may not have the names, so like
18236 elsewhere in GDB, assume an artificial first
18237 argument is "this". */
18238 is_this = 1;
18239
18240 if (is_this)
18241 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
18242 arg_type, 0);
18243 }
18244
18245 ftype->field (iparams).set_type (arg_type);
18246 iparams++;
18247 }
18248 child_die = child_die->sibling;
18249 }
18250 }
18251
18252 return ftype;
18253 }
18254
18255 static struct type *
18256 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
18257 {
18258 struct objfile *objfile = cu->per_objfile->objfile;
18259 const char *name = NULL;
18260 struct type *this_type, *target_type;
18261
18262 name = dwarf2_full_name (NULL, die, cu);
18263 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
18264 this_type->set_target_is_stub (true);
18265 set_die_type (die, this_type, cu);
18266 target_type = die_type (die, cu);
18267 if (target_type != this_type)
18268 TYPE_TARGET_TYPE (this_type) = target_type;
18269 else
18270 {
18271 /* Self-referential typedefs are, it seems, not allowed by the DWARF
18272 spec and cause infinite loops in GDB. */
18273 complaint (_("Self-referential DW_TAG_typedef "
18274 "- DIE at %s [in module %s]"),
18275 sect_offset_str (die->sect_off), objfile_name (objfile));
18276 TYPE_TARGET_TYPE (this_type) = NULL;
18277 }
18278 if (name == NULL)
18279 {
18280 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
18281 anonymous typedefs, which is, strictly speaking, invalid DWARF.
18282 Handle these by just returning the target type, rather than
18283 constructing an anonymous typedef type and trying to handle this
18284 elsewhere. */
18285 set_die_type (die, target_type, cu);
18286 return target_type;
18287 }
18288 return this_type;
18289 }
18290
18291 /* Helper for get_dwarf2_rational_constant that computes the value of
18292 a given gmp_mpz given an attribute. */
18293
18294 static void
18295 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
18296 {
18297 /* GCC will sometimes emit a 16-byte constant value as a DWARF
18298 location expression that pushes an implicit value. */
18299 if (attr->form == DW_FORM_exprloc)
18300 {
18301 dwarf_block *blk = attr->as_block ();
18302 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
18303 {
18304 uint64_t len;
18305 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
18306 blk->data + blk->size,
18307 &len);
18308 if (ptr - blk->data + len <= blk->size)
18309 {
18310 mpz_import (value->val, len,
18311 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18312 1, 0, 0, ptr);
18313 return;
18314 }
18315 }
18316
18317 /* On failure set it to 1. */
18318 *value = gdb_mpz (1);
18319 }
18320 else if (attr->form_is_block ())
18321 {
18322 dwarf_block *blk = attr->as_block ();
18323 mpz_import (value->val, blk->size,
18324 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18325 1, 0, 0, blk->data);
18326 }
18327 else
18328 *value = gdb_mpz (attr->constant_value (1));
18329 }
18330
18331 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
18332 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
18333
18334 If the numerator and/or numerator attribute is missing,
18335 a complaint is filed, and NUMERATOR and DENOMINATOR are left
18336 untouched. */
18337
18338 static void
18339 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
18340 gdb_mpz *numerator, gdb_mpz *denominator)
18341 {
18342 struct attribute *num_attr, *denom_attr;
18343
18344 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
18345 if (num_attr == nullptr)
18346 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
18347 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18348
18349 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
18350 if (denom_attr == nullptr)
18351 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
18352 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18353
18354 if (num_attr == nullptr || denom_attr == nullptr)
18355 return;
18356
18357 get_mpz (cu, numerator, num_attr);
18358 get_mpz (cu, denominator, denom_attr);
18359 }
18360
18361 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
18362 rational constant, rather than a signed one.
18363
18364 If the rational constant has a negative value, a complaint
18365 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
18366
18367 static void
18368 get_dwarf2_unsigned_rational_constant (struct die_info *die,
18369 struct dwarf2_cu *cu,
18370 gdb_mpz *numerator,
18371 gdb_mpz *denominator)
18372 {
18373 gdb_mpz num (1);
18374 gdb_mpz denom (1);
18375
18376 get_dwarf2_rational_constant (die, cu, &num, &denom);
18377 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
18378 {
18379 mpz_neg (num.val, num.val);
18380 mpz_neg (denom.val, denom.val);
18381 }
18382 else if (mpz_sgn (num.val) == -1)
18383 {
18384 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
18385 " in DIE at %s"),
18386 sect_offset_str (die->sect_off));
18387 return;
18388 }
18389 else if (mpz_sgn (denom.val) == -1)
18390 {
18391 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
18392 " in DIE at %s"),
18393 sect_offset_str (die->sect_off));
18394 return;
18395 }
18396
18397 *numerator = std::move (num);
18398 *denominator = std::move (denom);
18399 }
18400
18401 /* Assuming that ENCODING is a string whose contents starting at the
18402 K'th character is "_nn" where "nn" is a decimal number, scan that
18403 number and set RESULT to the value. K is updated to point to the
18404 character immediately following the number.
18405
18406 If the string does not conform to the format described above, false
18407 is returned, and K may or may not be changed. */
18408
18409 static bool
18410 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
18411 {
18412 /* The next character should be an underscore ('_') followed
18413 by a digit. */
18414 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
18415 return false;
18416
18417 /* Skip the underscore. */
18418 k++;
18419 int start = k;
18420
18421 /* Determine the number of digits for our number. */
18422 while (isdigit (encoding[k]))
18423 k++;
18424 if (k == start)
18425 return false;
18426
18427 std::string copy (&encoding[start], k - start);
18428 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
18429 return false;
18430
18431 return true;
18432 }
18433
18434 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
18435 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
18436 DENOM, update OFFSET, and return true on success. Return false on
18437 failure. */
18438
18439 static bool
18440 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
18441 gdb_mpz *num, gdb_mpz *denom)
18442 {
18443 if (!ada_get_gnat_encoded_number (encoding, offset, num))
18444 return false;
18445 return ada_get_gnat_encoded_number (encoding, offset, denom);
18446 }
18447
18448 /* Assuming DIE corresponds to a fixed point type, finish the creation
18449 of the corresponding TYPE by setting its type-specific data. CU is
18450 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
18451 encodings. It is nullptr if the GNAT encoding should be
18452 ignored. */
18453
18454 static void
18455 finish_fixed_point_type (struct type *type, const char *suffix,
18456 struct die_info *die, struct dwarf2_cu *cu)
18457 {
18458 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
18459 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
18460
18461 /* If GNAT encodings are preferred, don't examine the
18462 attributes. */
18463 struct attribute *attr = nullptr;
18464 if (suffix == nullptr)
18465 {
18466 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
18467 if (attr == nullptr)
18468 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
18469 if (attr == nullptr)
18470 attr = dwarf2_attr (die, DW_AT_small, cu);
18471 }
18472
18473 /* Numerator and denominator of our fixed-point type's scaling factor.
18474 The default is a scaling factor of 1, which we use as a fallback
18475 when we are not able to decode it (problem with the debugging info,
18476 unsupported forms, bug in GDB, etc...). Using that as the default
18477 allows us to at least print the unscaled value, which might still
18478 be useful to a user. */
18479 gdb_mpz scale_num (1);
18480 gdb_mpz scale_denom (1);
18481
18482 if (attr == nullptr)
18483 {
18484 int offset = 0;
18485 if (suffix != nullptr
18486 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18487 &scale_denom)
18488 /* The number might be encoded as _nn_dd_nn_dd, where the
18489 second ratio is the 'small value. In this situation, we
18490 want the second value. */
18491 && (suffix[offset] != '_'
18492 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18493 &scale_denom)))
18494 {
18495 /* Found it. */
18496 }
18497 else
18498 {
18499 /* Scaling factor not found. Assume a scaling factor of 1,
18500 and hope for the best. At least the user will be able to
18501 see the encoded value. */
18502 scale_num = 1;
18503 scale_denom = 1;
18504 complaint (_("no scale found for fixed-point type (DIE at %s)"),
18505 sect_offset_str (die->sect_off));
18506 }
18507 }
18508 else if (attr->name == DW_AT_binary_scale)
18509 {
18510 LONGEST scale_exp = attr->constant_value (0);
18511 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18512
18513 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
18514 }
18515 else if (attr->name == DW_AT_decimal_scale)
18516 {
18517 LONGEST scale_exp = attr->constant_value (0);
18518 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18519
18520 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
18521 }
18522 else if (attr->name == DW_AT_small)
18523 {
18524 struct die_info *scale_die;
18525 struct dwarf2_cu *scale_cu = cu;
18526
18527 scale_die = follow_die_ref (die, attr, &scale_cu);
18528 if (scale_die->tag == DW_TAG_constant)
18529 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18530 &scale_num, &scale_denom);
18531 else
18532 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18533 " (DIE at %s)"),
18534 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18535 }
18536 else
18537 {
18538 complaint (_("unsupported scale attribute %s for fixed-point type"
18539 " (DIE at %s)"),
18540 dwarf_attr_name (attr->name),
18541 sect_offset_str (die->sect_off));
18542 }
18543
18544 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
18545 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
18546 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
18547 mpq_canonicalize (scaling_factor.val);
18548 }
18549
18550 /* The gnat-encoding suffix for fixed point. */
18551
18552 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
18553
18554 /* If NAME encodes an Ada fixed-point type, return a pointer to the
18555 "XF" suffix of the name. The text after this is what encodes the
18556 'small and 'delta information. Otherwise, return nullptr. */
18557
18558 static const char *
18559 gnat_encoded_fixed_point_type_info (const char *name)
18560 {
18561 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
18562 }
18563
18564 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18565 (which may be different from NAME) to the architecture back-end to allow
18566 it to guess the correct format if necessary. */
18567
18568 static struct type *
18569 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
18570 const char *name_hint, enum bfd_endian byte_order)
18571 {
18572 struct gdbarch *gdbarch = objfile->arch ();
18573 const struct floatformat **format;
18574 struct type *type;
18575
18576 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18577 if (format)
18578 type = init_float_type (objfile, bits, name, format, byte_order);
18579 else
18580 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18581
18582 return type;
18583 }
18584
18585 /* Allocate an integer type of size BITS and name NAME. */
18586
18587 static struct type *
18588 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18589 int bits, int unsigned_p, const char *name)
18590 {
18591 struct type *type;
18592
18593 /* Versions of Intel's C Compiler generate an integer type called "void"
18594 instead of using DW_TAG_unspecified_type. This has been seen on
18595 at least versions 14, 17, and 18. */
18596 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18597 && strcmp (name, "void") == 0)
18598 type = objfile_type (objfile)->builtin_void;
18599 else
18600 type = init_integer_type (objfile, bits, unsigned_p, name);
18601
18602 return type;
18603 }
18604
18605 /* Return true if DIE has a DW_AT_small attribute whose value is
18606 a constant rational, where both the numerator and denominator
18607 are equal to zero.
18608
18609 CU is the DIE's Compilation Unit. */
18610
18611 static bool
18612 has_zero_over_zero_small_attribute (struct die_info *die,
18613 struct dwarf2_cu *cu)
18614 {
18615 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18616 if (attr == nullptr)
18617 return false;
18618
18619 struct dwarf2_cu *scale_cu = cu;
18620 struct die_info *scale_die
18621 = follow_die_ref (die, attr, &scale_cu);
18622
18623 if (scale_die->tag != DW_TAG_constant)
18624 return false;
18625
18626 gdb_mpz num (1), denom (1);
18627 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
18628 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
18629 }
18630
18631 /* Initialise and return a floating point type of size BITS suitable for
18632 use as a component of a complex number. The NAME_HINT is passed through
18633 when initialising the floating point type and is the name of the complex
18634 type.
18635
18636 As DWARF doesn't currently provide an explicit name for the components
18637 of a complex number, but it can be helpful to have these components
18638 named, we try to select a suitable name based on the size of the
18639 component. */
18640 static struct type *
18641 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18642 struct objfile *objfile,
18643 int bits, const char *name_hint,
18644 enum bfd_endian byte_order)
18645 {
18646 gdbarch *gdbarch = objfile->arch ();
18647 struct type *tt = nullptr;
18648
18649 /* Try to find a suitable floating point builtin type of size BITS.
18650 We're going to use the name of this type as the name for the complex
18651 target type that we are about to create. */
18652 switch (cu->language)
18653 {
18654 case language_fortran:
18655 switch (bits)
18656 {
18657 case 32:
18658 tt = builtin_f_type (gdbarch)->builtin_real;
18659 break;
18660 case 64:
18661 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18662 break;
18663 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18664 case 128:
18665 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18666 break;
18667 }
18668 break;
18669 default:
18670 switch (bits)
18671 {
18672 case 32:
18673 tt = builtin_type (gdbarch)->builtin_float;
18674 break;
18675 case 64:
18676 tt = builtin_type (gdbarch)->builtin_double;
18677 break;
18678 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18679 case 128:
18680 tt = builtin_type (gdbarch)->builtin_long_double;
18681 break;
18682 }
18683 break;
18684 }
18685
18686 /* If the type we found doesn't match the size we were looking for, then
18687 pretend we didn't find a type at all, the complex target type we
18688 create will then be nameless. */
18689 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
18690 tt = nullptr;
18691
18692 const char *name = (tt == nullptr) ? nullptr : tt->name ();
18693 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
18694 }
18695
18696 /* Find a representation of a given base type and install
18697 it in the TYPE field of the die. */
18698
18699 static struct type *
18700 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
18701 {
18702 struct objfile *objfile = cu->per_objfile->objfile;
18703 struct type *type;
18704 struct attribute *attr;
18705 int encoding = 0, bits = 0;
18706 const char *name;
18707 gdbarch *arch;
18708
18709 attr = dwarf2_attr (die, DW_AT_encoding, cu);
18710 if (attr != nullptr && attr->form_is_constant ())
18711 encoding = attr->constant_value (0);
18712 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18713 if (attr != nullptr)
18714 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
18715 name = dwarf2_name (die, cu);
18716 if (!name)
18717 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
18718
18719 arch = objfile->arch ();
18720 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18721
18722 attr = dwarf2_attr (die, DW_AT_endianity, cu);
18723 if (attr != nullptr && attr->form_is_constant ())
18724 {
18725 int endianity = attr->constant_value (0);
18726
18727 switch (endianity)
18728 {
18729 case DW_END_big:
18730 byte_order = BFD_ENDIAN_BIG;
18731 break;
18732 case DW_END_little:
18733 byte_order = BFD_ENDIAN_LITTLE;
18734 break;
18735 default:
18736 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18737 break;
18738 }
18739 }
18740
18741 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18742 && cu->language == language_ada
18743 && has_zero_over_zero_small_attribute (die, cu))
18744 {
18745 /* brobecker/2018-02-24: This is a fixed point type for which
18746 the scaling factor is represented as fraction whose value
18747 does not make sense (zero divided by zero), so we should
18748 normally never see these. However, there is a small category
18749 of fixed point types for which GNAT is unable to provide
18750 the scaling factor via the standard DWARF mechanisms, and
18751 for which the info is provided via the GNAT encodings instead.
18752 This is likely what this DIE is about. */
18753 encoding = (encoding == DW_ATE_signed_fixed
18754 ? DW_ATE_signed
18755 : DW_ATE_unsigned);
18756 }
18757
18758 /* With GNAT encodings, fixed-point information will be encoded in
18759 the type name. Note that this can also occur with the above
18760 zero-over-zero case, which is why this is a separate "if" rather
18761 than an "else if". */
18762 const char *gnat_encoding_suffix = nullptr;
18763 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18764 && cu->language == language_ada
18765 && name != nullptr)
18766 {
18767 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18768 if (gnat_encoding_suffix != nullptr)
18769 {
18770 gdb_assert (startswith (gnat_encoding_suffix,
18771 GNAT_FIXED_POINT_SUFFIX));
18772 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18773 name, gnat_encoding_suffix - name);
18774 /* Use -1 here so that SUFFIX points at the "_" after the
18775 "XF". */
18776 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18777
18778 encoding = (encoding == DW_ATE_signed
18779 ? DW_ATE_signed_fixed
18780 : DW_ATE_unsigned_fixed);
18781 }
18782 }
18783
18784 switch (encoding)
18785 {
18786 case DW_ATE_address:
18787 /* Turn DW_ATE_address into a void * pointer. */
18788 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
18789 type = init_pointer_type (objfile, bits, name, type);
18790 break;
18791 case DW_ATE_boolean:
18792 type = init_boolean_type (objfile, bits, 1, name);
18793 break;
18794 case DW_ATE_complex_float:
18795 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18796 byte_order);
18797 if (type->code () == TYPE_CODE_ERROR)
18798 {
18799 if (name == nullptr)
18800 {
18801 struct obstack *obstack
18802 = &cu->per_objfile->objfile->objfile_obstack;
18803 name = obconcat (obstack, "_Complex ", type->name (),
18804 nullptr);
18805 }
18806 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18807 }
18808 else
18809 type = init_complex_type (name, type);
18810 break;
18811 case DW_ATE_decimal_float:
18812 type = init_decfloat_type (objfile, bits, name);
18813 break;
18814 case DW_ATE_float:
18815 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
18816 break;
18817 case DW_ATE_signed:
18818 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18819 break;
18820 case DW_ATE_unsigned:
18821 if (cu->language == language_fortran
18822 && name
18823 && startswith (name, "character("))
18824 type = init_character_type (objfile, bits, 1, name);
18825 else
18826 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18827 break;
18828 case DW_ATE_signed_char:
18829 if (cu->language == language_ada || cu->language == language_m2
18830 || cu->language == language_pascal
18831 || cu->language == language_fortran)
18832 type = init_character_type (objfile, bits, 0, name);
18833 else
18834 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18835 break;
18836 case DW_ATE_unsigned_char:
18837 if (cu->language == language_ada || cu->language == language_m2
18838 || cu->language == language_pascal
18839 || cu->language == language_fortran
18840 || cu->language == language_rust)
18841 type = init_character_type (objfile, bits, 1, name);
18842 else
18843 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18844 break;
18845 case DW_ATE_UTF:
18846 {
18847 if (bits == 16)
18848 type = builtin_type (arch)->builtin_char16;
18849 else if (bits == 32)
18850 type = builtin_type (arch)->builtin_char32;
18851 else
18852 {
18853 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
18854 bits);
18855 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18856 }
18857 return set_die_type (die, type, cu);
18858 }
18859 break;
18860 case DW_ATE_signed_fixed:
18861 type = init_fixed_point_type (objfile, bits, 0, name);
18862 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18863 break;
18864 case DW_ATE_unsigned_fixed:
18865 type = init_fixed_point_type (objfile, bits, 1, name);
18866 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18867 break;
18868
18869 default:
18870 complaint (_("unsupported DW_AT_encoding: '%s'"),
18871 dwarf_type_encoding_name (encoding));
18872 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18873 break;
18874 }
18875
18876 if (name && strcmp (name, "char") == 0)
18877 type->set_has_no_signedness (true);
18878
18879 maybe_set_alignment (cu, die, type);
18880
18881 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18882
18883 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18884 {
18885 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18886 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
18887 {
18888 unsigned real_bit_size = attr->as_unsigned ();
18889 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18890 /* Only use the attributes if they make sense together. */
18891 if (attr == nullptr
18892 || (attr->as_unsigned () + real_bit_size
18893 <= 8 * TYPE_LENGTH (type)))
18894 {
18895 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18896 = real_bit_size;
18897 if (attr != nullptr)
18898 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18899 = attr->as_unsigned ();
18900 }
18901 }
18902 }
18903
18904 return set_die_type (die, type, cu);
18905 }
18906
18907 /* Parse dwarf attribute if it's a block, reference or constant and put the
18908 resulting value of the attribute into struct bound_prop.
18909 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18910
18911 static int
18912 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18913 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18914 struct type *default_type)
18915 {
18916 struct dwarf2_property_baton *baton;
18917 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18918 struct objfile *objfile = per_objfile->objfile;
18919 struct obstack *obstack = &objfile->objfile_obstack;
18920
18921 gdb_assert (default_type != NULL);
18922
18923 if (attr == NULL || prop == NULL)
18924 return 0;
18925
18926 if (attr->form_is_block ())
18927 {
18928 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18929 baton->property_type = default_type;
18930 baton->locexpr.per_cu = cu->per_cu;
18931 baton->locexpr.per_objfile = per_objfile;
18932
18933 struct dwarf_block *block = attr->as_block ();
18934 baton->locexpr.size = block->size;
18935 baton->locexpr.data = block->data;
18936 switch (attr->name)
18937 {
18938 case DW_AT_string_length:
18939 baton->locexpr.is_reference = true;
18940 break;
18941 default:
18942 baton->locexpr.is_reference = false;
18943 break;
18944 }
18945
18946 prop->set_locexpr (baton);
18947 gdb_assert (prop->baton () != NULL);
18948 }
18949 else if (attr->form_is_ref ())
18950 {
18951 struct dwarf2_cu *target_cu = cu;
18952 struct die_info *target_die;
18953 struct attribute *target_attr;
18954
18955 target_die = follow_die_ref (die, attr, &target_cu);
18956 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18957 if (target_attr == NULL)
18958 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18959 target_cu);
18960 if (target_attr == NULL)
18961 return 0;
18962
18963 switch (target_attr->name)
18964 {
18965 case DW_AT_location:
18966 if (target_attr->form_is_section_offset ())
18967 {
18968 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18969 baton->property_type = die_type (target_die, target_cu);
18970 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18971 prop->set_loclist (baton);
18972 gdb_assert (prop->baton () != NULL);
18973 }
18974 else if (target_attr->form_is_block ())
18975 {
18976 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18977 baton->property_type = die_type (target_die, target_cu);
18978 baton->locexpr.per_cu = cu->per_cu;
18979 baton->locexpr.per_objfile = per_objfile;
18980 struct dwarf_block *block = target_attr->as_block ();
18981 baton->locexpr.size = block->size;
18982 baton->locexpr.data = block->data;
18983 baton->locexpr.is_reference = true;
18984 prop->set_locexpr (baton);
18985 gdb_assert (prop->baton () != NULL);
18986 }
18987 else
18988 {
18989 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18990 "dynamic property");
18991 return 0;
18992 }
18993 break;
18994 case DW_AT_data_member_location:
18995 {
18996 LONGEST offset;
18997
18998 if (!handle_data_member_location (target_die, target_cu,
18999 &offset))
19000 return 0;
19001
19002 baton = XOBNEW (obstack, struct dwarf2_property_baton);
19003 baton->property_type = read_type_die (target_die->parent,
19004 target_cu);
19005 baton->offset_info.offset = offset;
19006 baton->offset_info.type = die_type (target_die, target_cu);
19007 prop->set_addr_offset (baton);
19008 break;
19009 }
19010 }
19011 }
19012 else if (attr->form_is_constant ())
19013 prop->set_const_val (attr->constant_value (0));
19014 else
19015 {
19016 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
19017 dwarf2_name (die, cu));
19018 return 0;
19019 }
19020
19021 return 1;
19022 }
19023
19024 /* See read.h. */
19025
19026 struct type *
19027 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
19028 {
19029 struct type *int_type;
19030
19031 /* Helper macro to examine the various builtin types. */
19032 #define TRY_TYPE(F) \
19033 int_type = (unsigned_p \
19034 ? objfile_type (objfile)->builtin_unsigned_ ## F \
19035 : objfile_type (objfile)->builtin_ ## F); \
19036 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
19037 return int_type
19038
19039 TRY_TYPE (char);
19040 TRY_TYPE (short);
19041 TRY_TYPE (int);
19042 TRY_TYPE (long);
19043 TRY_TYPE (long_long);
19044
19045 #undef TRY_TYPE
19046
19047 gdb_assert_not_reached ("unable to find suitable integer type");
19048 }
19049
19050 /* See read.h. */
19051
19052 struct type *
19053 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
19054 {
19055 int addr_size = this->per_cu->addr_size ();
19056 return this->per_objfile->int_type (addr_size, unsigned_p);
19057 }
19058
19059 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
19060 present (which is valid) then compute the default type based on the
19061 compilation units address size. */
19062
19063 static struct type *
19064 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
19065 {
19066 struct type *index_type = die_type (die, cu);
19067
19068 /* Dwarf-2 specifications explicitly allows to create subrange types
19069 without specifying a base type.
19070 In that case, the base type must be set to the type of
19071 the lower bound, upper bound or count, in that order, if any of these
19072 three attributes references an object that has a type.
19073 If no base type is found, the Dwarf-2 specifications say that
19074 a signed integer type of size equal to the size of an address should
19075 be used.
19076 For the following C code: `extern char gdb_int [];'
19077 GCC produces an empty range DIE.
19078 FIXME: muller/2010-05-28: Possible references to object for low bound,
19079 high bound or count are not yet handled by this code. */
19080 if (index_type->code () == TYPE_CODE_VOID)
19081 index_type = cu->addr_sized_int_type (false);
19082
19083 return index_type;
19084 }
19085
19086 /* Read the given DW_AT_subrange DIE. */
19087
19088 static struct type *
19089 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
19090 {
19091 struct type *base_type, *orig_base_type;
19092 struct type *range_type;
19093 struct attribute *attr;
19094 struct dynamic_prop low, high;
19095 int low_default_is_valid;
19096 int high_bound_is_count = 0;
19097 const char *name;
19098 ULONGEST negative_mask;
19099
19100 orig_base_type = read_subrange_index_type (die, cu);
19101
19102 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
19103 whereas the real type might be. So, we use ORIG_BASE_TYPE when
19104 creating the range type, but we use the result of check_typedef
19105 when examining properties of the type. */
19106 base_type = check_typedef (orig_base_type);
19107
19108 /* The die_type call above may have already set the type for this DIE. */
19109 range_type = get_die_type (die, cu);
19110 if (range_type)
19111 return range_type;
19112
19113 high.set_const_val (0);
19114
19115 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
19116 omitting DW_AT_lower_bound. */
19117 switch (cu->language)
19118 {
19119 case language_c:
19120 case language_cplus:
19121 low.set_const_val (0);
19122 low_default_is_valid = 1;
19123 break;
19124 case language_fortran:
19125 low.set_const_val (1);
19126 low_default_is_valid = 1;
19127 break;
19128 case language_d:
19129 case language_objc:
19130 case language_rust:
19131 low.set_const_val (0);
19132 low_default_is_valid = (cu->header.version >= 4);
19133 break;
19134 case language_ada:
19135 case language_m2:
19136 case language_pascal:
19137 low.set_const_val (1);
19138 low_default_is_valid = (cu->header.version >= 4);
19139 break;
19140 default:
19141 low.set_const_val (0);
19142 low_default_is_valid = 0;
19143 break;
19144 }
19145
19146 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
19147 if (attr != nullptr)
19148 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
19149 else if (!low_default_is_valid)
19150 complaint (_("Missing DW_AT_lower_bound "
19151 "- DIE at %s [in module %s]"),
19152 sect_offset_str (die->sect_off),
19153 objfile_name (cu->per_objfile->objfile));
19154
19155 struct attribute *attr_ub, *attr_count;
19156 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
19157 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
19158 {
19159 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
19160 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
19161 {
19162 /* If bounds are constant do the final calculation here. */
19163 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
19164 high.set_const_val (low.const_val () + high.const_val () - 1);
19165 else
19166 high_bound_is_count = 1;
19167 }
19168 else
19169 {
19170 if (attr_ub != NULL)
19171 complaint (_("Unresolved DW_AT_upper_bound "
19172 "- DIE at %s [in module %s]"),
19173 sect_offset_str (die->sect_off),
19174 objfile_name (cu->per_objfile->objfile));
19175 if (attr_count != NULL)
19176 complaint (_("Unresolved DW_AT_count "
19177 "- DIE at %s [in module %s]"),
19178 sect_offset_str (die->sect_off),
19179 objfile_name (cu->per_objfile->objfile));
19180 }
19181 }
19182
19183 LONGEST bias = 0;
19184 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
19185 if (bias_attr != nullptr && bias_attr->form_is_constant ())
19186 bias = bias_attr->constant_value (0);
19187
19188 /* Normally, the DWARF producers are expected to use a signed
19189 constant form (Eg. DW_FORM_sdata) to express negative bounds.
19190 But this is unfortunately not always the case, as witnessed
19191 with GCC, for instance, where the ambiguous DW_FORM_dataN form
19192 is used instead. To work around that ambiguity, we treat
19193 the bounds as signed, and thus sign-extend their values, when
19194 the base type is signed. */
19195 negative_mask =
19196 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
19197 if (low.kind () == PROP_CONST
19198 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
19199 low.set_const_val (low.const_val () | negative_mask);
19200 if (high.kind () == PROP_CONST
19201 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
19202 high.set_const_val (high.const_val () | negative_mask);
19203
19204 /* Check for bit and byte strides. */
19205 struct dynamic_prop byte_stride_prop;
19206 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
19207 if (attr_byte_stride != nullptr)
19208 {
19209 struct type *prop_type = cu->addr_sized_int_type (false);
19210 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
19211 prop_type);
19212 }
19213
19214 struct dynamic_prop bit_stride_prop;
19215 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
19216 if (attr_bit_stride != nullptr)
19217 {
19218 /* It only makes sense to have either a bit or byte stride. */
19219 if (attr_byte_stride != nullptr)
19220 {
19221 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
19222 "- DIE at %s [in module %s]"),
19223 sect_offset_str (die->sect_off),
19224 objfile_name (cu->per_objfile->objfile));
19225 attr_bit_stride = nullptr;
19226 }
19227 else
19228 {
19229 struct type *prop_type = cu->addr_sized_int_type (false);
19230 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
19231 prop_type);
19232 }
19233 }
19234
19235 if (attr_byte_stride != nullptr
19236 || attr_bit_stride != nullptr)
19237 {
19238 bool byte_stride_p = (attr_byte_stride != nullptr);
19239 struct dynamic_prop *stride
19240 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
19241
19242 range_type
19243 = create_range_type_with_stride (NULL, orig_base_type, &low,
19244 &high, bias, stride, byte_stride_p);
19245 }
19246 else
19247 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
19248
19249 if (high_bound_is_count)
19250 range_type->bounds ()->flag_upper_bound_is_count = 1;
19251
19252 /* Ada expects an empty array on no boundary attributes. */
19253 if (attr == NULL && cu->language != language_ada)
19254 range_type->bounds ()->high.set_undefined ();
19255
19256 name = dwarf2_name (die, cu);
19257 if (name)
19258 range_type->set_name (name);
19259
19260 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
19261 if (attr != nullptr)
19262 TYPE_LENGTH (range_type) = attr->constant_value (0);
19263
19264 maybe_set_alignment (cu, die, range_type);
19265
19266 set_die_type (die, range_type, cu);
19267
19268 /* set_die_type should be already done. */
19269 set_descriptive_type (range_type, die, cu);
19270
19271 return range_type;
19272 }
19273
19274 static struct type *
19275 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
19276 {
19277 struct type *type;
19278
19279 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
19280 type->set_name (dwarf2_name (die, cu));
19281
19282 /* In Ada, an unspecified type is typically used when the description
19283 of the type is deferred to a different unit. When encountering
19284 such a type, we treat it as a stub, and try to resolve it later on,
19285 when needed. */
19286 if (cu->language == language_ada)
19287 type->set_is_stub (true);
19288
19289 return set_die_type (die, type, cu);
19290 }
19291
19292 /* Read a single die and all its descendents. Set the die's sibling
19293 field to NULL; set other fields in the die correctly, and set all
19294 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
19295 location of the info_ptr after reading all of those dies. PARENT
19296 is the parent of the die in question. */
19297
19298 static struct die_info *
19299 read_die_and_children (const struct die_reader_specs *reader,
19300 const gdb_byte *info_ptr,
19301 const gdb_byte **new_info_ptr,
19302 struct die_info *parent)
19303 {
19304 struct die_info *die;
19305 const gdb_byte *cur_ptr;
19306
19307 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
19308 if (die == NULL)
19309 {
19310 *new_info_ptr = cur_ptr;
19311 return NULL;
19312 }
19313 store_in_ref_table (die, reader->cu);
19314
19315 if (die->has_children)
19316 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
19317 else
19318 {
19319 die->child = NULL;
19320 *new_info_ptr = cur_ptr;
19321 }
19322
19323 die->sibling = NULL;
19324 die->parent = parent;
19325 return die;
19326 }
19327
19328 /* Read a die, all of its descendents, and all of its siblings; set
19329 all of the fields of all of the dies correctly. Arguments are as
19330 in read_die_and_children. */
19331
19332 static struct die_info *
19333 read_die_and_siblings_1 (const struct die_reader_specs *reader,
19334 const gdb_byte *info_ptr,
19335 const gdb_byte **new_info_ptr,
19336 struct die_info *parent)
19337 {
19338 struct die_info *first_die, *last_sibling;
19339 const gdb_byte *cur_ptr;
19340
19341 cur_ptr = info_ptr;
19342 first_die = last_sibling = NULL;
19343
19344 while (1)
19345 {
19346 struct die_info *die
19347 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
19348
19349 if (die == NULL)
19350 {
19351 *new_info_ptr = cur_ptr;
19352 return first_die;
19353 }
19354
19355 if (!first_die)
19356 first_die = die;
19357 else
19358 last_sibling->sibling = die;
19359
19360 last_sibling = die;
19361 }
19362 }
19363
19364 /* Read a die, all of its descendents, and all of its siblings; set
19365 all of the fields of all of the dies correctly. Arguments are as
19366 in read_die_and_children.
19367 This the main entry point for reading a DIE and all its children. */
19368
19369 static struct die_info *
19370 read_die_and_siblings (const struct die_reader_specs *reader,
19371 const gdb_byte *info_ptr,
19372 const gdb_byte **new_info_ptr,
19373 struct die_info *parent)
19374 {
19375 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
19376 new_info_ptr, parent);
19377
19378 if (dwarf_die_debug)
19379 {
19380 fprintf_unfiltered (gdb_stdlog,
19381 "Read die from %s@0x%x of %s:\n",
19382 reader->die_section->get_name (),
19383 (unsigned) (info_ptr - reader->die_section->buffer),
19384 bfd_get_filename (reader->abfd));
19385 dump_die (die, dwarf_die_debug);
19386 }
19387
19388 return die;
19389 }
19390
19391 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
19392 attributes.
19393 The caller is responsible for filling in the extra attributes
19394 and updating (*DIEP)->num_attrs.
19395 Set DIEP to point to a newly allocated die with its information,
19396 except for its child, sibling, and parent fields. */
19397
19398 static const gdb_byte *
19399 read_full_die_1 (const struct die_reader_specs *reader,
19400 struct die_info **diep, const gdb_byte *info_ptr,
19401 int num_extra_attrs)
19402 {
19403 unsigned int abbrev_number, bytes_read, i;
19404 struct abbrev_info *abbrev;
19405 struct die_info *die;
19406 struct dwarf2_cu *cu = reader->cu;
19407 bfd *abfd = reader->abfd;
19408
19409 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
19410 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19411 info_ptr += bytes_read;
19412 if (!abbrev_number)
19413 {
19414 *diep = NULL;
19415 return info_ptr;
19416 }
19417
19418 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
19419 if (!abbrev)
19420 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
19421 abbrev_number,
19422 bfd_get_filename (abfd));
19423
19424 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
19425 die->sect_off = sect_off;
19426 die->tag = abbrev->tag;
19427 die->abbrev = abbrev_number;
19428 die->has_children = abbrev->has_children;
19429
19430 /* Make the result usable.
19431 The caller needs to update num_attrs after adding the extra
19432 attributes. */
19433 die->num_attrs = abbrev->num_attrs;
19434
19435 bool any_need_reprocess = false;
19436 for (i = 0; i < abbrev->num_attrs; ++i)
19437 {
19438 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
19439 info_ptr);
19440 if (die->attrs[i].requires_reprocessing_p ())
19441 any_need_reprocess = true;
19442 }
19443
19444 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
19445 if (attr != nullptr && attr->form_is_unsigned ())
19446 cu->str_offsets_base = attr->as_unsigned ();
19447
19448 attr = die->attr (DW_AT_loclists_base);
19449 if (attr != nullptr)
19450 cu->loclist_base = attr->as_unsigned ();
19451
19452 auto maybe_addr_base = die->addr_base ();
19453 if (maybe_addr_base.has_value ())
19454 cu->addr_base = *maybe_addr_base;
19455
19456 attr = die->attr (DW_AT_rnglists_base);
19457 if (attr != nullptr)
19458 cu->rnglists_base = attr->as_unsigned ();
19459
19460 if (any_need_reprocess)
19461 {
19462 for (i = 0; i < abbrev->num_attrs; ++i)
19463 {
19464 if (die->attrs[i].requires_reprocessing_p ())
19465 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
19466 }
19467 }
19468 *diep = die;
19469 return info_ptr;
19470 }
19471
19472 /* Read a die and all its attributes.
19473 Set DIEP to point to a newly allocated die with its information,
19474 except for its child, sibling, and parent fields. */
19475
19476 static const gdb_byte *
19477 read_full_die (const struct die_reader_specs *reader,
19478 struct die_info **diep, const gdb_byte *info_ptr)
19479 {
19480 const gdb_byte *result;
19481
19482 result = read_full_die_1 (reader, diep, info_ptr, 0);
19483
19484 if (dwarf_die_debug)
19485 {
19486 fprintf_unfiltered (gdb_stdlog,
19487 "Read die from %s@0x%x of %s:\n",
19488 reader->die_section->get_name (),
19489 (unsigned) (info_ptr - reader->die_section->buffer),
19490 bfd_get_filename (reader->abfd));
19491 dump_die (*diep, dwarf_die_debug);
19492 }
19493
19494 return result;
19495 }
19496 \f
19497
19498 /* Returns nonzero if TAG represents a type that we might generate a partial
19499 symbol for. */
19500
19501 static int
19502 is_type_tag_for_partial (int tag, enum language lang)
19503 {
19504 switch (tag)
19505 {
19506 #if 0
19507 /* Some types that would be reasonable to generate partial symbols for,
19508 that we don't at present. Note that normally this does not
19509 matter, mainly because C compilers don't give names to these
19510 types, but instead emit DW_TAG_typedef. */
19511 case DW_TAG_file_type:
19512 case DW_TAG_ptr_to_member_type:
19513 case DW_TAG_set_type:
19514 case DW_TAG_string_type:
19515 case DW_TAG_subroutine_type:
19516 #endif
19517
19518 /* GNAT may emit an array with a name, but no typedef, so we
19519 need to make a symbol in this case. */
19520 case DW_TAG_array_type:
19521 return lang == language_ada;
19522
19523 case DW_TAG_base_type:
19524 case DW_TAG_class_type:
19525 case DW_TAG_interface_type:
19526 case DW_TAG_enumeration_type:
19527 case DW_TAG_structure_type:
19528 case DW_TAG_subrange_type:
19529 case DW_TAG_typedef:
19530 case DW_TAG_union_type:
19531 return 1;
19532 default:
19533 return 0;
19534 }
19535 }
19536
19537 /* Load all DIEs that are interesting for partial symbols into memory. */
19538
19539 static struct partial_die_info *
19540 load_partial_dies (const struct die_reader_specs *reader,
19541 const gdb_byte *info_ptr, int building_psymtab)
19542 {
19543 struct dwarf2_cu *cu = reader->cu;
19544 struct objfile *objfile = cu->per_objfile->objfile;
19545 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
19546 unsigned int bytes_read;
19547 unsigned int load_all = 0;
19548 int nesting_level = 1;
19549
19550 parent_die = NULL;
19551 last_die = NULL;
19552
19553 gdb_assert (cu->per_cu != NULL);
19554 if (cu->per_cu->load_all_dies)
19555 load_all = 1;
19556
19557 cu->partial_dies
19558 = htab_create_alloc_ex (cu->header.length / 12,
19559 partial_die_hash,
19560 partial_die_eq,
19561 NULL,
19562 &cu->comp_unit_obstack,
19563 hashtab_obstack_allocate,
19564 dummy_obstack_deallocate);
19565
19566 while (1)
19567 {
19568 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
19569
19570 /* A NULL abbrev means the end of a series of children. */
19571 if (abbrev == NULL)
19572 {
19573 if (--nesting_level == 0)
19574 return first_die;
19575
19576 info_ptr += bytes_read;
19577 last_die = parent_die;
19578 parent_die = parent_die->die_parent;
19579 continue;
19580 }
19581
19582 /* Check for template arguments. We never save these; if
19583 they're seen, we just mark the parent, and go on our way. */
19584 if (parent_die != NULL
19585 && cu->language == language_cplus
19586 && (abbrev->tag == DW_TAG_template_type_param
19587 || abbrev->tag == DW_TAG_template_value_param))
19588 {
19589 parent_die->has_template_arguments = 1;
19590
19591 if (!load_all)
19592 {
19593 /* We don't need a partial DIE for the template argument. */
19594 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19595 continue;
19596 }
19597 }
19598
19599 /* We only recurse into c++ subprograms looking for template arguments.
19600 Skip their other children. */
19601 if (!load_all
19602 && cu->language == language_cplus
19603 && parent_die != NULL
19604 && parent_die->tag == DW_TAG_subprogram
19605 && abbrev->tag != DW_TAG_inlined_subroutine)
19606 {
19607 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19608 continue;
19609 }
19610
19611 /* Check whether this DIE is interesting enough to save. Normally
19612 we would not be interested in members here, but there may be
19613 later variables referencing them via DW_AT_specification (for
19614 static members). */
19615 if (!load_all
19616 && !is_type_tag_for_partial (abbrev->tag, cu->language)
19617 && abbrev->tag != DW_TAG_constant
19618 && abbrev->tag != DW_TAG_enumerator
19619 && abbrev->tag != DW_TAG_subprogram
19620 && abbrev->tag != DW_TAG_inlined_subroutine
19621 && abbrev->tag != DW_TAG_lexical_block
19622 && abbrev->tag != DW_TAG_variable
19623 && abbrev->tag != DW_TAG_namespace
19624 && abbrev->tag != DW_TAG_module
19625 && abbrev->tag != DW_TAG_member
19626 && abbrev->tag != DW_TAG_imported_unit
19627 && abbrev->tag != DW_TAG_imported_declaration)
19628 {
19629 /* Otherwise we skip to the next sibling, if any. */
19630 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19631 continue;
19632 }
19633
19634 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19635 abbrev);
19636
19637 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
19638
19639 /* This two-pass algorithm for processing partial symbols has a
19640 high cost in cache pressure. Thus, handle some simple cases
19641 here which cover the majority of C partial symbols. DIEs
19642 which neither have specification tags in them, nor could have
19643 specification tags elsewhere pointing at them, can simply be
19644 processed and discarded.
19645
19646 This segment is also optional; scan_partial_symbols and
19647 add_partial_symbol will handle these DIEs if we chain
19648 them in normally. When compilers which do not emit large
19649 quantities of duplicate debug information are more common,
19650 this code can probably be removed. */
19651
19652 /* Any complete simple types at the top level (pretty much all
19653 of them, for a language without namespaces), can be processed
19654 directly. */
19655 if (parent_die == NULL
19656 && pdi.has_specification == 0
19657 && pdi.is_declaration == 0
19658 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19659 || pdi.tag == DW_TAG_base_type
19660 || pdi.tag == DW_TAG_array_type
19661 || pdi.tag == DW_TAG_subrange_type))
19662 {
19663 if (building_psymtab && pdi.raw_name != NULL)
19664 add_partial_symbol (&pdi, cu);
19665
19666 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19667 continue;
19668 }
19669
19670 /* The exception for DW_TAG_typedef with has_children above is
19671 a workaround of GCC PR debug/47510. In the case of this complaint
19672 type_name_or_error will error on such types later.
19673
19674 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19675 it could not find the child DIEs referenced later, this is checked
19676 above. In correct DWARF DW_TAG_typedef should have no children. */
19677
19678 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
19679 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
19680 "- DIE at %s [in module %s]"),
19681 sect_offset_str (pdi.sect_off), objfile_name (objfile));
19682
19683 /* If we're at the second level, and we're an enumerator, and
19684 our parent has no specification (meaning possibly lives in a
19685 namespace elsewhere), then we can add the partial symbol now
19686 instead of queueing it. */
19687 if (pdi.tag == DW_TAG_enumerator
19688 && parent_die != NULL
19689 && parent_die->die_parent == NULL
19690 && parent_die->tag == DW_TAG_enumeration_type
19691 && parent_die->has_specification == 0)
19692 {
19693 if (pdi.raw_name == NULL)
19694 complaint (_("malformed enumerator DIE ignored"));
19695 else if (building_psymtab)
19696 add_partial_symbol (&pdi, cu);
19697
19698 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19699 continue;
19700 }
19701
19702 struct partial_die_info *part_die
19703 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
19704
19705 /* We'll save this DIE so link it in. */
19706 part_die->die_parent = parent_die;
19707 part_die->die_sibling = NULL;
19708 part_die->die_child = NULL;
19709
19710 if (last_die && last_die == parent_die)
19711 last_die->die_child = part_die;
19712 else if (last_die)
19713 last_die->die_sibling = part_die;
19714
19715 last_die = part_die;
19716
19717 if (first_die == NULL)
19718 first_die = part_die;
19719
19720 /* Maybe add the DIE to the hash table. Not all DIEs that we
19721 find interesting need to be in the hash table, because we
19722 also have the parent/sibling/child chains; only those that we
19723 might refer to by offset later during partial symbol reading.
19724
19725 For now this means things that might have be the target of a
19726 DW_AT_specification, DW_AT_abstract_origin, or
19727 DW_AT_extension. DW_AT_extension will refer only to
19728 namespaces; DW_AT_abstract_origin refers to functions (and
19729 many things under the function DIE, but we do not recurse
19730 into function DIEs during partial symbol reading) and
19731 possibly variables as well; DW_AT_specification refers to
19732 declarations. Declarations ought to have the DW_AT_declaration
19733 flag. It happens that GCC forgets to put it in sometimes, but
19734 only for functions, not for types.
19735
19736 Adding more things than necessary to the hash table is harmless
19737 except for the performance cost. Adding too few will result in
19738 wasted time in find_partial_die, when we reread the compilation
19739 unit with load_all_dies set. */
19740
19741 if (load_all
19742 || abbrev->tag == DW_TAG_constant
19743 || abbrev->tag == DW_TAG_subprogram
19744 || abbrev->tag == DW_TAG_variable
19745 || abbrev->tag == DW_TAG_namespace
19746 || part_die->is_declaration)
19747 {
19748 void **slot;
19749
19750 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
19751 to_underlying (part_die->sect_off),
19752 INSERT);
19753 *slot = part_die;
19754 }
19755
19756 /* For some DIEs we want to follow their children (if any). For C
19757 we have no reason to follow the children of structures; for other
19758 languages we have to, so that we can get at method physnames
19759 to infer fully qualified class names, for DW_AT_specification,
19760 and for C++ template arguments. For C++, we also look one level
19761 inside functions to find template arguments (if the name of the
19762 function does not already contain the template arguments).
19763
19764 For Ada and Fortran, we need to scan the children of subprograms
19765 and lexical blocks as well because these languages allow the
19766 definition of nested entities that could be interesting for the
19767 debugger, such as nested subprograms for instance. */
19768 if (last_die->has_children
19769 && (load_all
19770 || last_die->tag == DW_TAG_namespace
19771 || last_die->tag == DW_TAG_module
19772 || last_die->tag == DW_TAG_enumeration_type
19773 || (cu->language == language_cplus
19774 && last_die->tag == DW_TAG_subprogram
19775 && (last_die->raw_name == NULL
19776 || strchr (last_die->raw_name, '<') == NULL))
19777 || (cu->language != language_c
19778 && (last_die->tag == DW_TAG_class_type
19779 || last_die->tag == DW_TAG_interface_type
19780 || last_die->tag == DW_TAG_structure_type
19781 || last_die->tag == DW_TAG_union_type))
19782 || ((cu->language == language_ada
19783 || cu->language == language_fortran)
19784 && (last_die->tag == DW_TAG_subprogram
19785 || last_die->tag == DW_TAG_lexical_block))))
19786 {
19787 nesting_level++;
19788 parent_die = last_die;
19789 continue;
19790 }
19791
19792 /* Otherwise we skip to the next sibling, if any. */
19793 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
19794
19795 /* Back to the top, do it again. */
19796 }
19797 }
19798
19799 partial_die_info::partial_die_info (sect_offset sect_off_,
19800 struct abbrev_info *abbrev)
19801 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19802 {
19803 }
19804
19805 /* See class definition. */
19806
19807 const char *
19808 partial_die_info::name (dwarf2_cu *cu)
19809 {
19810 if (!canonical_name && raw_name != nullptr)
19811 {
19812 struct objfile *objfile = cu->per_objfile->objfile;
19813 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19814 canonical_name = 1;
19815 }
19816
19817 return raw_name;
19818 }
19819
19820 /* Read a minimal amount of information into the minimal die structure.
19821 INFO_PTR should point just after the initial uleb128 of a DIE. */
19822
19823 const gdb_byte *
19824 partial_die_info::read (const struct die_reader_specs *reader,
19825 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
19826 {
19827 struct dwarf2_cu *cu = reader->cu;
19828 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19829 unsigned int i;
19830 int has_low_pc_attr = 0;
19831 int has_high_pc_attr = 0;
19832 int high_pc_relative = 0;
19833
19834 for (i = 0; i < abbrev.num_attrs; ++i)
19835 {
19836 attribute attr;
19837 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19838 /* String and address offsets that need to do the reprocessing have
19839 already been read at this point, so there is no need to wait until
19840 the loop terminates to do the reprocessing. */
19841 if (attr.requires_reprocessing_p ())
19842 read_attribute_reprocess (reader, &attr, tag);
19843 /* Store the data if it is of an attribute we want to keep in a
19844 partial symbol table. */
19845 switch (attr.name)
19846 {
19847 case DW_AT_name:
19848 switch (tag)
19849 {
19850 case DW_TAG_compile_unit:
19851 case DW_TAG_partial_unit:
19852 case DW_TAG_type_unit:
19853 /* Compilation units have a DW_AT_name that is a filename, not
19854 a source language identifier. */
19855 case DW_TAG_enumeration_type:
19856 case DW_TAG_enumerator:
19857 /* These tags always have simple identifiers already; no need
19858 to canonicalize them. */
19859 canonical_name = 1;
19860 raw_name = attr.as_string ();
19861 break;
19862 default:
19863 canonical_name = 0;
19864 raw_name = attr.as_string ();
19865 break;
19866 }
19867 break;
19868 case DW_AT_linkage_name:
19869 case DW_AT_MIPS_linkage_name:
19870 /* Note that both forms of linkage name might appear. We
19871 assume they will be the same, and we only store the last
19872 one we see. */
19873 linkage_name = attr.as_string ();
19874 break;
19875 case DW_AT_low_pc:
19876 has_low_pc_attr = 1;
19877 lowpc = attr.as_address ();
19878 break;
19879 case DW_AT_high_pc:
19880 has_high_pc_attr = 1;
19881 highpc = attr.as_address ();
19882 if (cu->header.version >= 4 && attr.form_is_constant ())
19883 high_pc_relative = 1;
19884 break;
19885 case DW_AT_location:
19886 /* Support the .debug_loc offsets. */
19887 if (attr.form_is_block ())
19888 {
19889 d.locdesc = attr.as_block ();
19890 }
19891 else if (attr.form_is_section_offset ())
19892 {
19893 dwarf2_complex_location_expr_complaint ();
19894 }
19895 else
19896 {
19897 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19898 "partial symbol information");
19899 }
19900 break;
19901 case DW_AT_external:
19902 is_external = attr.as_boolean ();
19903 break;
19904 case DW_AT_declaration:
19905 is_declaration = attr.as_boolean ();
19906 break;
19907 case DW_AT_type:
19908 has_type = 1;
19909 break;
19910 case DW_AT_abstract_origin:
19911 case DW_AT_specification:
19912 case DW_AT_extension:
19913 has_specification = 1;
19914 spec_offset = attr.get_ref_die_offset ();
19915 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19916 || cu->per_cu->is_dwz);
19917 break;
19918 case DW_AT_sibling:
19919 /* Ignore absolute siblings, they might point outside of
19920 the current compile unit. */
19921 if (attr.form == DW_FORM_ref_addr)
19922 complaint (_("ignoring absolute DW_AT_sibling"));
19923 else
19924 {
19925 const gdb_byte *buffer = reader->buffer;
19926 sect_offset off = attr.get_ref_die_offset ();
19927 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19928
19929 if (sibling_ptr < info_ptr)
19930 complaint (_("DW_AT_sibling points backwards"));
19931 else if (sibling_ptr > reader->buffer_end)
19932 reader->die_section->overflow_complaint ();
19933 else
19934 sibling = sibling_ptr;
19935 }
19936 break;
19937 case DW_AT_byte_size:
19938 has_byte_size = 1;
19939 break;
19940 case DW_AT_const_value:
19941 has_const_value = 1;
19942 break;
19943 case DW_AT_calling_convention:
19944 /* DWARF doesn't provide a way to identify a program's source-level
19945 entry point. DW_AT_calling_convention attributes are only meant
19946 to describe functions' calling conventions.
19947
19948 However, because it's a necessary piece of information in
19949 Fortran, and before DWARF 4 DW_CC_program was the only
19950 piece of debugging information whose definition refers to
19951 a 'main program' at all, several compilers marked Fortran
19952 main programs with DW_CC_program --- even when those
19953 functions use the standard calling conventions.
19954
19955 Although DWARF now specifies a way to provide this
19956 information, we support this practice for backward
19957 compatibility. */
19958 if (attr.constant_value (0) == DW_CC_program
19959 && cu->language == language_fortran)
19960 main_subprogram = 1;
19961 break;
19962 case DW_AT_inline:
19963 {
19964 LONGEST value = attr.constant_value (-1);
19965 if (value == DW_INL_inlined
19966 || value == DW_INL_declared_inlined)
19967 may_be_inlined = 1;
19968 }
19969 break;
19970
19971 case DW_AT_import:
19972 if (tag == DW_TAG_imported_unit)
19973 {
19974 d.sect_off = attr.get_ref_die_offset ();
19975 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19976 || cu->per_cu->is_dwz);
19977 }
19978 break;
19979
19980 case DW_AT_main_subprogram:
19981 main_subprogram = attr.as_boolean ();
19982 break;
19983
19984 case DW_AT_ranges:
19985 {
19986 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19987 on DWARF version). */
19988 ULONGEST ranges_offset = attr.as_unsigned ();
19989
19990 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19991 this value. */
19992 if (tag != DW_TAG_compile_unit)
19993 ranges_offset += cu->gnu_ranges_base;
19994
19995 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19996 nullptr, tag))
19997 has_pc_info = 1;
19998 }
19999 break;
20000
20001 default:
20002 break;
20003 }
20004 }
20005
20006 /* For Ada, if both the name and the linkage name appear, we prefer
20007 the latter. This lets "catch exception" work better, regardless
20008 of the order in which the name and linkage name were emitted.
20009 Really, though, this is just a workaround for the fact that gdb
20010 doesn't store both the name and the linkage name. */
20011 if (cu->language == language_ada && linkage_name != nullptr)
20012 raw_name = linkage_name;
20013
20014 if (high_pc_relative)
20015 highpc += lowpc;
20016
20017 if (has_low_pc_attr && has_high_pc_attr)
20018 {
20019 /* When using the GNU linker, .gnu.linkonce. sections are used to
20020 eliminate duplicate copies of functions and vtables and such.
20021 The linker will arbitrarily choose one and discard the others.
20022 The AT_*_pc values for such functions refer to local labels in
20023 these sections. If the section from that file was discarded, the
20024 labels are not in the output, so the relocs get a value of 0.
20025 If this is a discarded function, mark the pc bounds as invalid,
20026 so that GDB will ignore it. */
20027 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
20028 {
20029 struct objfile *objfile = per_objfile->objfile;
20030 struct gdbarch *gdbarch = objfile->arch ();
20031
20032 complaint (_("DW_AT_low_pc %s is zero "
20033 "for DIE at %s [in module %s]"),
20034 paddress (gdbarch, lowpc),
20035 sect_offset_str (sect_off),
20036 objfile_name (objfile));
20037 }
20038 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
20039 else if (lowpc >= highpc)
20040 {
20041 struct objfile *objfile = per_objfile->objfile;
20042 struct gdbarch *gdbarch = objfile->arch ();
20043
20044 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
20045 "for DIE at %s [in module %s]"),
20046 paddress (gdbarch, lowpc),
20047 paddress (gdbarch, highpc),
20048 sect_offset_str (sect_off),
20049 objfile_name (objfile));
20050 }
20051 else
20052 has_pc_info = 1;
20053 }
20054
20055 return info_ptr;
20056 }
20057
20058 /* Find a cached partial DIE at OFFSET in CU. */
20059
20060 struct partial_die_info *
20061 dwarf2_cu::find_partial_die (sect_offset sect_off)
20062 {
20063 struct partial_die_info *lookup_die = NULL;
20064 struct partial_die_info part_die (sect_off);
20065
20066 lookup_die = ((struct partial_die_info *)
20067 htab_find_with_hash (partial_dies, &part_die,
20068 to_underlying (sect_off)));
20069
20070 return lookup_die;
20071 }
20072
20073 /* Find a partial DIE at OFFSET, which may or may not be in CU,
20074 except in the case of .debug_types DIEs which do not reference
20075 outside their CU (they do however referencing other types via
20076 DW_FORM_ref_sig8). */
20077
20078 static const struct cu_partial_die_info
20079 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
20080 {
20081 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20082 struct objfile *objfile = per_objfile->objfile;
20083 struct partial_die_info *pd = NULL;
20084
20085 if (offset_in_dwz == cu->per_cu->is_dwz
20086 && cu->header.offset_in_cu_p (sect_off))
20087 {
20088 pd = cu->find_partial_die (sect_off);
20089 if (pd != NULL)
20090 return { cu, pd };
20091 /* We missed recording what we needed.
20092 Load all dies and try again. */
20093 }
20094 else
20095 {
20096 /* TUs don't reference other CUs/TUs (except via type signatures). */
20097 if (cu->per_cu->is_debug_types)
20098 {
20099 error (_("Dwarf Error: Type Unit at offset %s contains"
20100 " external reference to offset %s [in module %s].\n"),
20101 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
20102 bfd_get_filename (objfile->obfd));
20103 }
20104 dwarf2_per_cu_data *per_cu
20105 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
20106 per_objfile);
20107
20108 cu = per_objfile->get_cu (per_cu);
20109 if (cu == NULL || cu->partial_dies == NULL)
20110 load_partial_comp_unit (per_cu, per_objfile, nullptr);
20111
20112 cu = per_objfile->get_cu (per_cu);
20113
20114 cu->last_used = 0;
20115 pd = cu->find_partial_die (sect_off);
20116 }
20117
20118 /* If we didn't find it, and not all dies have been loaded,
20119 load them all and try again. */
20120
20121 if (pd == NULL && cu->per_cu->load_all_dies == 0)
20122 {
20123 cu->per_cu->load_all_dies = 1;
20124
20125 /* This is nasty. When we reread the DIEs, somewhere up the call chain
20126 THIS_CU->cu may already be in use. So we can't just free it and
20127 replace its DIEs with the ones we read in. Instead, we leave those
20128 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
20129 and clobber THIS_CU->cu->partial_dies with the hash table for the new
20130 set. */
20131 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
20132
20133 pd = cu->find_partial_die (sect_off);
20134 }
20135
20136 if (pd == NULL)
20137 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
20138 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
20139 return { cu, pd };
20140 }
20141
20142 /* See if we can figure out if the class lives in a namespace. We do
20143 this by looking for a member function; its demangled name will
20144 contain namespace info, if there is any. */
20145
20146 static void
20147 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
20148 struct dwarf2_cu *cu)
20149 {
20150 /* NOTE: carlton/2003-10-07: Getting the info this way changes
20151 what template types look like, because the demangler
20152 frequently doesn't give the same name as the debug info. We
20153 could fix this by only using the demangled name to get the
20154 prefix (but see comment in read_structure_type). */
20155
20156 struct partial_die_info *real_pdi;
20157 struct partial_die_info *child_pdi;
20158
20159 /* If this DIE (this DIE's specification, if any) has a parent, then
20160 we should not do this. We'll prepend the parent's fully qualified
20161 name when we create the partial symbol. */
20162
20163 real_pdi = struct_pdi;
20164 while (real_pdi->has_specification)
20165 {
20166 auto res = find_partial_die (real_pdi->spec_offset,
20167 real_pdi->spec_is_dwz, cu);
20168 real_pdi = res.pdi;
20169 cu = res.cu;
20170 }
20171
20172 if (real_pdi->die_parent != NULL)
20173 return;
20174
20175 for (child_pdi = struct_pdi->die_child;
20176 child_pdi != NULL;
20177 child_pdi = child_pdi->die_sibling)
20178 {
20179 if (child_pdi->tag == DW_TAG_subprogram
20180 && child_pdi->linkage_name != NULL)
20181 {
20182 gdb::unique_xmalloc_ptr<char> actual_class_name
20183 (cu->language_defn->class_name_from_physname
20184 (child_pdi->linkage_name));
20185 if (actual_class_name != NULL)
20186 {
20187 struct objfile *objfile = cu->per_objfile->objfile;
20188 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
20189 struct_pdi->canonical_name = 1;
20190 }
20191 break;
20192 }
20193 }
20194 }
20195
20196 /* Return true if a DIE with TAG may have the DW_AT_const_value
20197 attribute. */
20198
20199 static bool
20200 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
20201 {
20202 switch (tag)
20203 {
20204 case DW_TAG_constant:
20205 case DW_TAG_enumerator:
20206 case DW_TAG_formal_parameter:
20207 case DW_TAG_template_value_param:
20208 case DW_TAG_variable:
20209 return true;
20210 }
20211
20212 return false;
20213 }
20214
20215 void
20216 partial_die_info::fixup (struct dwarf2_cu *cu)
20217 {
20218 /* Once we've fixed up a die, there's no point in doing so again.
20219 This also avoids a memory leak if we were to call
20220 guess_partial_die_structure_name multiple times. */
20221 if (fixup_called)
20222 return;
20223
20224 /* If we found a reference attribute and the DIE has no name, try
20225 to find a name in the referred to DIE. */
20226
20227 if (raw_name == NULL && has_specification)
20228 {
20229 struct partial_die_info *spec_die;
20230
20231 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20232 spec_die = res.pdi;
20233 cu = res.cu;
20234
20235 spec_die->fixup (cu);
20236
20237 if (spec_die->raw_name)
20238 {
20239 raw_name = spec_die->raw_name;
20240 canonical_name = spec_die->canonical_name;
20241
20242 /* Copy DW_AT_external attribute if it is set. */
20243 if (spec_die->is_external)
20244 is_external = spec_die->is_external;
20245 }
20246 }
20247
20248 if (!has_const_value && has_specification
20249 && can_have_DW_AT_const_value_p (tag))
20250 {
20251 struct partial_die_info *spec_die;
20252
20253 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20254 spec_die = res.pdi;
20255 cu = res.cu;
20256
20257 spec_die->fixup (cu);
20258
20259 if (spec_die->has_const_value)
20260 {
20261 /* Copy DW_AT_const_value attribute if it is set. */
20262 has_const_value = spec_die->has_const_value;
20263 }
20264 }
20265
20266 /* Set default names for some unnamed DIEs. */
20267
20268 if (raw_name == NULL && tag == DW_TAG_namespace)
20269 {
20270 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
20271 canonical_name = 1;
20272 }
20273
20274 /* If there is no parent die to provide a namespace, and there are
20275 children, see if we can determine the namespace from their linkage
20276 name. */
20277 if (cu->language == language_cplus
20278 && !cu->per_objfile->per_bfd->types.empty ()
20279 && die_parent == NULL
20280 && has_children
20281 && (tag == DW_TAG_class_type
20282 || tag == DW_TAG_structure_type
20283 || tag == DW_TAG_union_type))
20284 guess_partial_die_structure_name (this, cu);
20285
20286 /* GCC might emit a nameless struct or union that has a linkage
20287 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20288 if (raw_name == NULL
20289 && (tag == DW_TAG_class_type
20290 || tag == DW_TAG_interface_type
20291 || tag == DW_TAG_structure_type
20292 || tag == DW_TAG_union_type)
20293 && linkage_name != NULL)
20294 {
20295 gdb::unique_xmalloc_ptr<char> demangled
20296 (gdb_demangle (linkage_name, DMGL_TYPES));
20297 if (demangled != nullptr)
20298 {
20299 const char *base;
20300
20301 /* Strip any leading namespaces/classes, keep only the base name.
20302 DW_AT_name for named DIEs does not contain the prefixes. */
20303 base = strrchr (demangled.get (), ':');
20304 if (base && base > demangled.get () && base[-1] == ':')
20305 base++;
20306 else
20307 base = demangled.get ();
20308
20309 struct objfile *objfile = cu->per_objfile->objfile;
20310 raw_name = objfile->intern (base);
20311 canonical_name = 1;
20312 }
20313 }
20314
20315 fixup_called = 1;
20316 }
20317
20318 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
20319 contents from the given SECTION in the HEADER.
20320
20321 HEADER_OFFSET is the offset of the header in the section. */
20322 static void
20323 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
20324 struct dwarf2_section_info *section,
20325 sect_offset header_offset)
20326 {
20327 unsigned int bytes_read;
20328 bfd *abfd = section->get_bfd_owner ();
20329 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
20330
20331 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
20332 info_ptr += bytes_read;
20333
20334 header->version = read_2_bytes (abfd, info_ptr);
20335 info_ptr += 2;
20336
20337 header->addr_size = read_1_byte (abfd, info_ptr);
20338 info_ptr += 1;
20339
20340 header->segment_collector_size = read_1_byte (abfd, info_ptr);
20341 info_ptr += 1;
20342
20343 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
20344 }
20345
20346 /* Return the DW_AT_loclists_base value for the CU. */
20347 static ULONGEST
20348 lookup_loclist_base (struct dwarf2_cu *cu)
20349 {
20350 /* For the .dwo unit, the loclist_base points to the first offset following
20351 the header. The header consists of the following entities-
20352 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
20353 bit format)
20354 2. version (2 bytes)
20355 3. address size (1 byte)
20356 4. segment selector size (1 byte)
20357 5. offset entry count (4 bytes)
20358 These sizes are derived as per the DWARFv5 standard. */
20359 if (cu->dwo_unit != nullptr)
20360 {
20361 if (cu->header.initial_length_size == 4)
20362 return LOCLIST_HEADER_SIZE32;
20363 return LOCLIST_HEADER_SIZE64;
20364 }
20365 return cu->loclist_base;
20366 }
20367
20368 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
20369 array of offsets in the .debug_loclists section. */
20370
20371 static sect_offset
20372 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
20373 {
20374 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20375 struct objfile *objfile = per_objfile->objfile;
20376 bfd *abfd = objfile->obfd;
20377 ULONGEST loclist_header_size =
20378 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
20379 : LOCLIST_HEADER_SIZE64);
20380 ULONGEST loclist_base = lookup_loclist_base (cu);
20381
20382 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
20383 ULONGEST start_offset =
20384 loclist_base + loclist_index * cu->header.offset_size;
20385
20386 /* Get loclists section. */
20387 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20388
20389 /* Read the loclists section content. */
20390 section->read (objfile);
20391 if (section->buffer == NULL)
20392 error (_("DW_FORM_loclistx used without .debug_loclists "
20393 "section [in module %s]"), objfile_name (objfile));
20394
20395 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
20396 so if loclist_base is smaller than the header size, we have a problem. */
20397 if (loclist_base < loclist_header_size)
20398 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
20399 objfile_name (objfile));
20400
20401 /* Read the header of the loclists contribution. */
20402 struct loclists_rnglists_header header;
20403 read_loclists_rnglists_header (&header, section,
20404 (sect_offset) (loclist_base - loclist_header_size));
20405
20406 /* Verify the loclist index is valid. */
20407 if (loclist_index >= header.offset_entry_count)
20408 error (_("DW_FORM_loclistx pointing outside of "
20409 ".debug_loclists offset array [in module %s]"),
20410 objfile_name (objfile));
20411
20412 /* Validate that reading won't go beyond the end of the section. */
20413 if (start_offset + cu->header.offset_size > section->size)
20414 error (_("Reading DW_FORM_loclistx index beyond end of"
20415 ".debug_loclists section [in module %s]"),
20416 objfile_name (objfile));
20417
20418 const gdb_byte *info_ptr = section->buffer + start_offset;
20419
20420 if (cu->header.offset_size == 4)
20421 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
20422 else
20423 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
20424 }
20425
20426 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
20427 array of offsets in the .debug_rnglists section. */
20428
20429 static sect_offset
20430 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
20431 dwarf_tag tag)
20432 {
20433 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20434 struct objfile *objfile = dwarf2_per_objfile->objfile;
20435 bfd *abfd = objfile->obfd;
20436 ULONGEST rnglist_header_size =
20437 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
20438 : RNGLIST_HEADER_SIZE64);
20439
20440 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
20441 .debug_rnglists.dwo section. The rnglists base given in the skeleton
20442 doesn't apply. */
20443 ULONGEST rnglist_base =
20444 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
20445
20446 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
20447 ULONGEST start_offset =
20448 rnglist_base + rnglist_index * cu->header.offset_size;
20449
20450 /* Get rnglists section. */
20451 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
20452
20453 /* Read the rnglists section content. */
20454 section->read (objfile);
20455 if (section->buffer == nullptr)
20456 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
20457 "[in module %s]"),
20458 objfile_name (objfile));
20459
20460 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
20461 so if rnglist_base is smaller than the header size, we have a problem. */
20462 if (rnglist_base < rnglist_header_size)
20463 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
20464 objfile_name (objfile));
20465
20466 /* Read the header of the rnglists contribution. */
20467 struct loclists_rnglists_header header;
20468 read_loclists_rnglists_header (&header, section,
20469 (sect_offset) (rnglist_base - rnglist_header_size));
20470
20471 /* Verify the rnglist index is valid. */
20472 if (rnglist_index >= header.offset_entry_count)
20473 error (_("DW_FORM_rnglistx index pointing outside of "
20474 ".debug_rnglists offset array [in module %s]"),
20475 objfile_name (objfile));
20476
20477 /* Validate that reading won't go beyond the end of the section. */
20478 if (start_offset + cu->header.offset_size > section->size)
20479 error (_("Reading DW_FORM_rnglistx index beyond end of"
20480 ".debug_rnglists section [in module %s]"),
20481 objfile_name (objfile));
20482
20483 const gdb_byte *info_ptr = section->buffer + start_offset;
20484
20485 if (cu->header.offset_size == 4)
20486 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
20487 else
20488 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
20489 }
20490
20491 /* Process the attributes that had to be skipped in the first round. These
20492 attributes are the ones that need str_offsets_base or addr_base attributes.
20493 They could not have been processed in the first round, because at the time
20494 the values of str_offsets_base or addr_base may not have been known. */
20495 static void
20496 read_attribute_reprocess (const struct die_reader_specs *reader,
20497 struct attribute *attr, dwarf_tag tag)
20498 {
20499 struct dwarf2_cu *cu = reader->cu;
20500 switch (attr->form)
20501 {
20502 case DW_FORM_addrx:
20503 case DW_FORM_GNU_addr_index:
20504 attr->set_address (read_addr_index (cu,
20505 attr->as_unsigned_reprocess ()));
20506 break;
20507 case DW_FORM_loclistx:
20508 {
20509 sect_offset loclists_sect_off
20510 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
20511
20512 attr->set_unsigned (to_underlying (loclists_sect_off));
20513 }
20514 break;
20515 case DW_FORM_rnglistx:
20516 {
20517 sect_offset rnglists_sect_off
20518 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
20519
20520 attr->set_unsigned (to_underlying (rnglists_sect_off));
20521 }
20522 break;
20523 case DW_FORM_strx:
20524 case DW_FORM_strx1:
20525 case DW_FORM_strx2:
20526 case DW_FORM_strx3:
20527 case DW_FORM_strx4:
20528 case DW_FORM_GNU_str_index:
20529 {
20530 unsigned int str_index = attr->as_unsigned_reprocess ();
20531 gdb_assert (!attr->canonical_string_p ());
20532 if (reader->dwo_file != NULL)
20533 attr->set_string_noncanonical (read_dwo_str_index (reader,
20534 str_index));
20535 else
20536 attr->set_string_noncanonical (read_stub_str_index (cu,
20537 str_index));
20538 break;
20539 }
20540 default:
20541 gdb_assert_not_reached (_("Unexpected DWARF form."));
20542 }
20543 }
20544
20545 /* Read an attribute value described by an attribute form. */
20546
20547 static const gdb_byte *
20548 read_attribute_value (const struct die_reader_specs *reader,
20549 struct attribute *attr, unsigned form,
20550 LONGEST implicit_const, const gdb_byte *info_ptr)
20551 {
20552 struct dwarf2_cu *cu = reader->cu;
20553 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20554 struct objfile *objfile = per_objfile->objfile;
20555 bfd *abfd = reader->abfd;
20556 struct comp_unit_head *cu_header = &cu->header;
20557 unsigned int bytes_read;
20558 struct dwarf_block *blk;
20559
20560 attr->form = (enum dwarf_form) form;
20561 switch (form)
20562 {
20563 case DW_FORM_ref_addr:
20564 if (cu->header.version == 2)
20565 attr->set_unsigned (cu->header.read_address (abfd, info_ptr,
20566 &bytes_read));
20567 else
20568 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20569 &bytes_read));
20570 info_ptr += bytes_read;
20571 break;
20572 case DW_FORM_GNU_ref_alt:
20573 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20574 &bytes_read));
20575 info_ptr += bytes_read;
20576 break;
20577 case DW_FORM_addr:
20578 {
20579 struct gdbarch *gdbarch = objfile->arch ();
20580 CORE_ADDR addr = cu->header.read_address (abfd, info_ptr, &bytes_read);
20581 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20582 attr->set_address (addr);
20583 info_ptr += bytes_read;
20584 }
20585 break;
20586 case DW_FORM_block2:
20587 blk = dwarf_alloc_block (cu);
20588 blk->size = read_2_bytes (abfd, info_ptr);
20589 info_ptr += 2;
20590 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20591 info_ptr += blk->size;
20592 attr->set_block (blk);
20593 break;
20594 case DW_FORM_block4:
20595 blk = dwarf_alloc_block (cu);
20596 blk->size = read_4_bytes (abfd, info_ptr);
20597 info_ptr += 4;
20598 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20599 info_ptr += blk->size;
20600 attr->set_block (blk);
20601 break;
20602 case DW_FORM_data2:
20603 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
20604 info_ptr += 2;
20605 break;
20606 case DW_FORM_data4:
20607 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
20608 info_ptr += 4;
20609 break;
20610 case DW_FORM_data8:
20611 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
20612 info_ptr += 8;
20613 break;
20614 case DW_FORM_data16:
20615 blk = dwarf_alloc_block (cu);
20616 blk->size = 16;
20617 blk->data = read_n_bytes (abfd, info_ptr, 16);
20618 info_ptr += 16;
20619 attr->set_block (blk);
20620 break;
20621 case DW_FORM_sec_offset:
20622 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20623 &bytes_read));
20624 info_ptr += bytes_read;
20625 break;
20626 case DW_FORM_loclistx:
20627 {
20628 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20629 &bytes_read));
20630 info_ptr += bytes_read;
20631 }
20632 break;
20633 case DW_FORM_string:
20634 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20635 &bytes_read));
20636 info_ptr += bytes_read;
20637 break;
20638 case DW_FORM_strp:
20639 if (!cu->per_cu->is_dwz)
20640 {
20641 attr->set_string_noncanonical
20642 (read_indirect_string (per_objfile,
20643 abfd, info_ptr, cu_header,
20644 &bytes_read));
20645 info_ptr += bytes_read;
20646 break;
20647 }
20648 /* FALLTHROUGH */
20649 case DW_FORM_line_strp:
20650 if (!cu->per_cu->is_dwz)
20651 {
20652 attr->set_string_noncanonical
20653 (per_objfile->read_line_string (info_ptr, cu_header,
20654 &bytes_read));
20655 info_ptr += bytes_read;
20656 break;
20657 }
20658 /* FALLTHROUGH */
20659 case DW_FORM_GNU_strp_alt:
20660 {
20661 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20662 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20663 &bytes_read);
20664
20665 attr->set_string_noncanonical
20666 (dwz->read_string (objfile, str_offset));
20667 info_ptr += bytes_read;
20668 }
20669 break;
20670 case DW_FORM_exprloc:
20671 case DW_FORM_block:
20672 blk = dwarf_alloc_block (cu);
20673 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20674 info_ptr += bytes_read;
20675 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20676 info_ptr += blk->size;
20677 attr->set_block (blk);
20678 break;
20679 case DW_FORM_block1:
20680 blk = dwarf_alloc_block (cu);
20681 blk->size = read_1_byte (abfd, info_ptr);
20682 info_ptr += 1;
20683 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20684 info_ptr += blk->size;
20685 attr->set_block (blk);
20686 break;
20687 case DW_FORM_data1:
20688 case DW_FORM_flag:
20689 attr->set_unsigned (read_1_byte (abfd, info_ptr));
20690 info_ptr += 1;
20691 break;
20692 case DW_FORM_flag_present:
20693 attr->set_unsigned (1);
20694 break;
20695 case DW_FORM_sdata:
20696 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
20697 info_ptr += bytes_read;
20698 break;
20699 case DW_FORM_rnglistx:
20700 {
20701 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20702 &bytes_read));
20703 info_ptr += bytes_read;
20704 }
20705 break;
20706 case DW_FORM_udata:
20707 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
20708 info_ptr += bytes_read;
20709 break;
20710 case DW_FORM_ref1:
20711 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20712 + read_1_byte (abfd, info_ptr)));
20713 info_ptr += 1;
20714 break;
20715 case DW_FORM_ref2:
20716 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20717 + read_2_bytes (abfd, info_ptr)));
20718 info_ptr += 2;
20719 break;
20720 case DW_FORM_ref4:
20721 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20722 + read_4_bytes (abfd, info_ptr)));
20723 info_ptr += 4;
20724 break;
20725 case DW_FORM_ref8:
20726 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20727 + read_8_bytes (abfd, info_ptr)));
20728 info_ptr += 8;
20729 break;
20730 case DW_FORM_ref_sig8:
20731 attr->set_signature (read_8_bytes (abfd, info_ptr));
20732 info_ptr += 8;
20733 break;
20734 case DW_FORM_ref_udata:
20735 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20736 + read_unsigned_leb128 (abfd, info_ptr,
20737 &bytes_read)));
20738 info_ptr += bytes_read;
20739 break;
20740 case DW_FORM_indirect:
20741 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20742 info_ptr += bytes_read;
20743 if (form == DW_FORM_implicit_const)
20744 {
20745 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20746 info_ptr += bytes_read;
20747 }
20748 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
20749 info_ptr);
20750 break;
20751 case DW_FORM_implicit_const:
20752 attr->set_signed (implicit_const);
20753 break;
20754 case DW_FORM_addrx:
20755 case DW_FORM_GNU_addr_index:
20756 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20757 &bytes_read));
20758 info_ptr += bytes_read;
20759 break;
20760 case DW_FORM_strx:
20761 case DW_FORM_strx1:
20762 case DW_FORM_strx2:
20763 case DW_FORM_strx3:
20764 case DW_FORM_strx4:
20765 case DW_FORM_GNU_str_index:
20766 {
20767 ULONGEST str_index;
20768 if (form == DW_FORM_strx1)
20769 {
20770 str_index = read_1_byte (abfd, info_ptr);
20771 info_ptr += 1;
20772 }
20773 else if (form == DW_FORM_strx2)
20774 {
20775 str_index = read_2_bytes (abfd, info_ptr);
20776 info_ptr += 2;
20777 }
20778 else if (form == DW_FORM_strx3)
20779 {
20780 str_index = read_3_bytes (abfd, info_ptr);
20781 info_ptr += 3;
20782 }
20783 else if (form == DW_FORM_strx4)
20784 {
20785 str_index = read_4_bytes (abfd, info_ptr);
20786 info_ptr += 4;
20787 }
20788 else
20789 {
20790 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20791 info_ptr += bytes_read;
20792 }
20793 attr->set_unsigned_reprocess (str_index);
20794 }
20795 break;
20796 default:
20797 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
20798 dwarf_form_name (form),
20799 bfd_get_filename (abfd));
20800 }
20801
20802 /* Super hack. */
20803 if (cu->per_cu->is_dwz && attr->form_is_ref ())
20804 attr->form = DW_FORM_GNU_ref_alt;
20805
20806 /* We have seen instances where the compiler tried to emit a byte
20807 size attribute of -1 which ended up being encoded as an unsigned
20808 0xffffffff. Although 0xffffffff is technically a valid size value,
20809 an object of this size seems pretty unlikely so we can relatively
20810 safely treat these cases as if the size attribute was invalid and
20811 treat them as zero by default. */
20812 if (attr->name == DW_AT_byte_size
20813 && form == DW_FORM_data4
20814 && attr->as_unsigned () >= 0xffffffff)
20815 {
20816 complaint
20817 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20818 hex_string (attr->as_unsigned ()));
20819 attr->set_unsigned (0);
20820 }
20821
20822 return info_ptr;
20823 }
20824
20825 /* Read an attribute described by an abbreviated attribute. */
20826
20827 static const gdb_byte *
20828 read_attribute (const struct die_reader_specs *reader,
20829 struct attribute *attr, const struct attr_abbrev *abbrev,
20830 const gdb_byte *info_ptr)
20831 {
20832 attr->name = abbrev->name;
20833 attr->string_is_canonical = 0;
20834 attr->requires_reprocessing = 0;
20835 return read_attribute_value (reader, attr, abbrev->form,
20836 abbrev->implicit_const, info_ptr);
20837 }
20838
20839 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20840
20841 static const char *
20842 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
20843 LONGEST str_offset)
20844 {
20845 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20846 str_offset, "DW_FORM_strp");
20847 }
20848
20849 /* Return pointer to string at .debug_str offset as read from BUF.
20850 BUF is assumed to be in a compilation unit described by CU_HEADER.
20851 Return *BYTES_READ_PTR count of bytes read from BUF. */
20852
20853 static const char *
20854 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
20855 const gdb_byte *buf,
20856 const struct comp_unit_head *cu_header,
20857 unsigned int *bytes_read_ptr)
20858 {
20859 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20860
20861 return read_indirect_string_at_offset (per_objfile, str_offset);
20862 }
20863
20864 /* See read.h. */
20865
20866 const char *
20867 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20868 const struct comp_unit_head *cu_header,
20869 unsigned int *bytes_read_ptr)
20870 {
20871 bfd *abfd = objfile->obfd;
20872 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20873
20874 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20875 }
20876
20877 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20878 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20879 ADDR_SIZE is the size of addresses from the CU header. */
20880
20881 static CORE_ADDR
20882 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20883 gdb::optional<ULONGEST> addr_base, int addr_size)
20884 {
20885 struct objfile *objfile = per_objfile->objfile;
20886 bfd *abfd = objfile->obfd;
20887 const gdb_byte *info_ptr;
20888 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20889
20890 per_objfile->per_bfd->addr.read (objfile);
20891 if (per_objfile->per_bfd->addr.buffer == NULL)
20892 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20893 objfile_name (objfile));
20894 if (addr_base_or_zero + addr_index * addr_size
20895 >= per_objfile->per_bfd->addr.size)
20896 error (_("DW_FORM_addr_index pointing outside of "
20897 ".debug_addr section [in module %s]"),
20898 objfile_name (objfile));
20899 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20900 + addr_index * addr_size);
20901 if (addr_size == 4)
20902 return bfd_get_32 (abfd, info_ptr);
20903 else
20904 return bfd_get_64 (abfd, info_ptr);
20905 }
20906
20907 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20908
20909 static CORE_ADDR
20910 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20911 {
20912 return read_addr_index_1 (cu->per_objfile, addr_index,
20913 cu->addr_base, cu->header.addr_size);
20914 }
20915
20916 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20917
20918 static CORE_ADDR
20919 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20920 unsigned int *bytes_read)
20921 {
20922 bfd *abfd = cu->per_objfile->objfile->obfd;
20923 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20924
20925 return read_addr_index (cu, addr_index);
20926 }
20927
20928 /* See read.h. */
20929
20930 CORE_ADDR
20931 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20932 dwarf2_per_objfile *per_objfile,
20933 unsigned int addr_index)
20934 {
20935 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20936 gdb::optional<ULONGEST> addr_base;
20937 int addr_size;
20938
20939 /* We need addr_base and addr_size.
20940 If we don't have PER_CU->cu, we have to get it.
20941 Nasty, but the alternative is storing the needed info in PER_CU,
20942 which at this point doesn't seem justified: it's not clear how frequently
20943 it would get used and it would increase the size of every PER_CU.
20944 Entry points like dwarf2_per_cu_addr_size do a similar thing
20945 so we're not in uncharted territory here.
20946 Alas we need to be a bit more complicated as addr_base is contained
20947 in the DIE.
20948
20949 We don't need to read the entire CU(/TU).
20950 We just need the header and top level die.
20951
20952 IWBN to use the aging mechanism to let us lazily later discard the CU.
20953 For now we skip this optimization. */
20954
20955 if (cu != NULL)
20956 {
20957 addr_base = cu->addr_base;
20958 addr_size = cu->header.addr_size;
20959 }
20960 else
20961 {
20962 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
20963 addr_base = reader.cu->addr_base;
20964 addr_size = reader.cu->header.addr_size;
20965 }
20966
20967 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
20968 }
20969
20970 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20971 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20972 DWO file. */
20973
20974 static const char *
20975 read_str_index (struct dwarf2_cu *cu,
20976 struct dwarf2_section_info *str_section,
20977 struct dwarf2_section_info *str_offsets_section,
20978 ULONGEST str_offsets_base, ULONGEST str_index)
20979 {
20980 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20981 struct objfile *objfile = per_objfile->objfile;
20982 const char *objf_name = objfile_name (objfile);
20983 bfd *abfd = objfile->obfd;
20984 const gdb_byte *info_ptr;
20985 ULONGEST str_offset;
20986 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20987
20988 str_section->read (objfile);
20989 str_offsets_section->read (objfile);
20990 if (str_section->buffer == NULL)
20991 error (_("%s used without %s section"
20992 " in CU at offset %s [in module %s]"),
20993 form_name, str_section->get_name (),
20994 sect_offset_str (cu->header.sect_off), objf_name);
20995 if (str_offsets_section->buffer == NULL)
20996 error (_("%s used without %s section"
20997 " in CU at offset %s [in module %s]"),
20998 form_name, str_section->get_name (),
20999 sect_offset_str (cu->header.sect_off), objf_name);
21000 info_ptr = (str_offsets_section->buffer
21001 + str_offsets_base
21002 + str_index * cu->header.offset_size);
21003 if (cu->header.offset_size == 4)
21004 str_offset = bfd_get_32 (abfd, info_ptr);
21005 else
21006 str_offset = bfd_get_64 (abfd, info_ptr);
21007 if (str_offset >= str_section->size)
21008 error (_("Offset from %s pointing outside of"
21009 " .debug_str.dwo section in CU at offset %s [in module %s]"),
21010 form_name, sect_offset_str (cu->header.sect_off), objf_name);
21011 return (const char *) (str_section->buffer + str_offset);
21012 }
21013
21014 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
21015
21016 static const char *
21017 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
21018 {
21019 ULONGEST str_offsets_base = reader->cu->header.version >= 5
21020 ? reader->cu->header.addr_size : 0;
21021 return read_str_index (reader->cu,
21022 &reader->dwo_file->sections.str,
21023 &reader->dwo_file->sections.str_offsets,
21024 str_offsets_base, str_index);
21025 }
21026
21027 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
21028
21029 static const char *
21030 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
21031 {
21032 struct objfile *objfile = cu->per_objfile->objfile;
21033 const char *objf_name = objfile_name (objfile);
21034 static const char form_name[] = "DW_FORM_GNU_str_index";
21035 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
21036
21037 if (!cu->str_offsets_base.has_value ())
21038 error (_("%s used in Fission stub without %s"
21039 " in CU at offset 0x%lx [in module %s]"),
21040 form_name, str_offsets_attr_name,
21041 (long) cu->header.offset_size, objf_name);
21042
21043 return read_str_index (cu,
21044 &cu->per_objfile->per_bfd->str,
21045 &cu->per_objfile->per_bfd->str_offsets,
21046 *cu->str_offsets_base, str_index);
21047 }
21048
21049 /* Return the length of an LEB128 number in BUF. */
21050
21051 static int
21052 leb128_size (const gdb_byte *buf)
21053 {
21054 const gdb_byte *begin = buf;
21055 gdb_byte byte;
21056
21057 while (1)
21058 {
21059 byte = *buf++;
21060 if ((byte & 128) == 0)
21061 return buf - begin;
21062 }
21063 }
21064
21065 static void
21066 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
21067 {
21068 switch (lang)
21069 {
21070 case DW_LANG_C89:
21071 case DW_LANG_C99:
21072 case DW_LANG_C11:
21073 case DW_LANG_C:
21074 case DW_LANG_UPC:
21075 cu->language = language_c;
21076 break;
21077 case DW_LANG_Java:
21078 case DW_LANG_C_plus_plus:
21079 case DW_LANG_C_plus_plus_11:
21080 case DW_LANG_C_plus_plus_14:
21081 cu->language = language_cplus;
21082 break;
21083 case DW_LANG_D:
21084 cu->language = language_d;
21085 break;
21086 case DW_LANG_Fortran77:
21087 case DW_LANG_Fortran90:
21088 case DW_LANG_Fortran95:
21089 case DW_LANG_Fortran03:
21090 case DW_LANG_Fortran08:
21091 cu->language = language_fortran;
21092 break;
21093 case DW_LANG_Go:
21094 cu->language = language_go;
21095 break;
21096 case DW_LANG_Mips_Assembler:
21097 cu->language = language_asm;
21098 break;
21099 case DW_LANG_Ada83:
21100 case DW_LANG_Ada95:
21101 cu->language = language_ada;
21102 break;
21103 case DW_LANG_Modula2:
21104 cu->language = language_m2;
21105 break;
21106 case DW_LANG_Pascal83:
21107 cu->language = language_pascal;
21108 break;
21109 case DW_LANG_ObjC:
21110 cu->language = language_objc;
21111 break;
21112 case DW_LANG_Rust:
21113 case DW_LANG_Rust_old:
21114 cu->language = language_rust;
21115 break;
21116 case DW_LANG_Cobol74:
21117 case DW_LANG_Cobol85:
21118 default:
21119 cu->language = language_minimal;
21120 break;
21121 }
21122 cu->language_defn = language_def (cu->language);
21123 }
21124
21125 /* Return the named attribute or NULL if not there. */
21126
21127 static struct attribute *
21128 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
21129 {
21130 for (;;)
21131 {
21132 unsigned int i;
21133 struct attribute *spec = NULL;
21134
21135 for (i = 0; i < die->num_attrs; ++i)
21136 {
21137 if (die->attrs[i].name == name)
21138 return &die->attrs[i];
21139 if (die->attrs[i].name == DW_AT_specification
21140 || die->attrs[i].name == DW_AT_abstract_origin)
21141 spec = &die->attrs[i];
21142 }
21143
21144 if (!spec)
21145 break;
21146
21147 die = follow_die_ref (die, spec, &cu);
21148 }
21149
21150 return NULL;
21151 }
21152
21153 /* Return the string associated with a string-typed attribute, or NULL if it
21154 is either not found or is of an incorrect type. */
21155
21156 static const char *
21157 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
21158 {
21159 struct attribute *attr;
21160 const char *str = NULL;
21161
21162 attr = dwarf2_attr (die, name, cu);
21163
21164 if (attr != NULL)
21165 {
21166 str = attr->as_string ();
21167 if (str == nullptr)
21168 complaint (_("string type expected for attribute %s for "
21169 "DIE at %s in module %s"),
21170 dwarf_attr_name (name), sect_offset_str (die->sect_off),
21171 objfile_name (cu->per_objfile->objfile));
21172 }
21173
21174 return str;
21175 }
21176
21177 /* Return the dwo name or NULL if not present. If present, it is in either
21178 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
21179 static const char *
21180 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
21181 {
21182 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
21183 if (dwo_name == nullptr)
21184 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
21185 return dwo_name;
21186 }
21187
21188 /* Return non-zero iff the attribute NAME is defined for the given DIE,
21189 and holds a non-zero value. This function should only be used for
21190 DW_FORM_flag or DW_FORM_flag_present attributes. */
21191
21192 static int
21193 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
21194 {
21195 struct attribute *attr = dwarf2_attr (die, name, cu);
21196
21197 return attr != nullptr && attr->as_boolean ();
21198 }
21199
21200 static int
21201 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
21202 {
21203 /* A DIE is a declaration if it has a DW_AT_declaration attribute
21204 which value is non-zero. However, we have to be careful with
21205 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
21206 (via dwarf2_flag_true_p) follows this attribute. So we may
21207 end up accidently finding a declaration attribute that belongs
21208 to a different DIE referenced by the specification attribute,
21209 even though the given DIE does not have a declaration attribute. */
21210 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
21211 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
21212 }
21213
21214 /* Return the die giving the specification for DIE, if there is
21215 one. *SPEC_CU is the CU containing DIE on input, and the CU
21216 containing the return value on output. If there is no
21217 specification, but there is an abstract origin, that is
21218 returned. */
21219
21220 static struct die_info *
21221 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
21222 {
21223 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
21224 *spec_cu);
21225
21226 if (spec_attr == NULL)
21227 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
21228
21229 if (spec_attr == NULL)
21230 return NULL;
21231 else
21232 return follow_die_ref (die, spec_attr, spec_cu);
21233 }
21234
21235 /* Stub for free_line_header to match void * callback types. */
21236
21237 static void
21238 free_line_header_voidp (void *arg)
21239 {
21240 struct line_header *lh = (struct line_header *) arg;
21241
21242 delete lh;
21243 }
21244
21245 /* A convenience function to find the proper .debug_line section for a CU. */
21246
21247 static struct dwarf2_section_info *
21248 get_debug_line_section (struct dwarf2_cu *cu)
21249 {
21250 struct dwarf2_section_info *section;
21251 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21252
21253 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
21254 DWO file. */
21255 if (cu->dwo_unit && cu->per_cu->is_debug_types)
21256 section = &cu->dwo_unit->dwo_file->sections.line;
21257 else if (cu->per_cu->is_dwz)
21258 {
21259 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
21260
21261 section = &dwz->line;
21262 }
21263 else
21264 section = &per_objfile->per_bfd->line;
21265
21266 return section;
21267 }
21268
21269 /* Read the statement program header starting at OFFSET in
21270 .debug_line, or .debug_line.dwo. Return a pointer
21271 to a struct line_header, allocated using xmalloc.
21272 Returns NULL if there is a problem reading the header, e.g., if it
21273 has a version we don't understand.
21274
21275 NOTE: the strings in the include directory and file name tables of
21276 the returned object point into the dwarf line section buffer,
21277 and must not be freed. */
21278
21279 static line_header_up
21280 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
21281 {
21282 struct dwarf2_section_info *section;
21283 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21284
21285 section = get_debug_line_section (cu);
21286 section->read (per_objfile->objfile);
21287 if (section->buffer == NULL)
21288 {
21289 if (cu->dwo_unit && cu->per_cu->is_debug_types)
21290 complaint (_("missing .debug_line.dwo section"));
21291 else
21292 complaint (_("missing .debug_line section"));
21293 return 0;
21294 }
21295
21296 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
21297 per_objfile, section, &cu->header);
21298 }
21299
21300 /* Subroutine of dwarf_decode_lines to simplify it.
21301 Return the file name of the psymtab for the given file_entry.
21302 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21303 If space for the result is malloc'd, *NAME_HOLDER will be set.
21304 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
21305
21306 static const char *
21307 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
21308 const dwarf2_psymtab *pst,
21309 const char *comp_dir,
21310 gdb::unique_xmalloc_ptr<char> *name_holder)
21311 {
21312 const char *include_name = fe.name;
21313 const char *include_name_to_compare = include_name;
21314 const char *pst_filename;
21315 int file_is_pst;
21316
21317 const char *dir_name = fe.include_dir (lh);
21318
21319 gdb::unique_xmalloc_ptr<char> hold_compare;
21320 if (!IS_ABSOLUTE_PATH (include_name)
21321 && (dir_name != NULL || comp_dir != NULL))
21322 {
21323 /* Avoid creating a duplicate psymtab for PST.
21324 We do this by comparing INCLUDE_NAME and PST_FILENAME.
21325 Before we do the comparison, however, we need to account
21326 for DIR_NAME and COMP_DIR.
21327 First prepend dir_name (if non-NULL). If we still don't
21328 have an absolute path prepend comp_dir (if non-NULL).
21329 However, the directory we record in the include-file's
21330 psymtab does not contain COMP_DIR (to match the
21331 corresponding symtab(s)).
21332
21333 Example:
21334
21335 bash$ cd /tmp
21336 bash$ gcc -g ./hello.c
21337 include_name = "hello.c"
21338 dir_name = "."
21339 DW_AT_comp_dir = comp_dir = "/tmp"
21340 DW_AT_name = "./hello.c"
21341
21342 */
21343
21344 if (dir_name != NULL)
21345 {
21346 name_holder->reset (concat (dir_name, SLASH_STRING,
21347 include_name, (char *) NULL));
21348 include_name = name_holder->get ();
21349 include_name_to_compare = include_name;
21350 }
21351 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
21352 {
21353 hold_compare.reset (concat (comp_dir, SLASH_STRING,
21354 include_name, (char *) NULL));
21355 include_name_to_compare = hold_compare.get ();
21356 }
21357 }
21358
21359 pst_filename = pst->filename;
21360 gdb::unique_xmalloc_ptr<char> copied_name;
21361 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
21362 {
21363 copied_name.reset (concat (pst->dirname, SLASH_STRING,
21364 pst_filename, (char *) NULL));
21365 pst_filename = copied_name.get ();
21366 }
21367
21368 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
21369
21370 if (file_is_pst)
21371 return NULL;
21372 return include_name;
21373 }
21374
21375 /* State machine to track the state of the line number program. */
21376
21377 class lnp_state_machine
21378 {
21379 public:
21380 /* Initialize a machine state for the start of a line number
21381 program. */
21382 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
21383 bool record_lines_p);
21384
21385 file_entry *current_file ()
21386 {
21387 /* lh->file_names is 0-based, but the file name numbers in the
21388 statement program are 1-based. */
21389 return m_line_header->file_name_at (m_file);
21390 }
21391
21392 /* Record the line in the state machine. END_SEQUENCE is true if
21393 we're processing the end of a sequence. */
21394 void record_line (bool end_sequence);
21395
21396 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
21397 nop-out rest of the lines in this sequence. */
21398 void check_line_address (struct dwarf2_cu *cu,
21399 const gdb_byte *line_ptr,
21400 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
21401
21402 void handle_set_discriminator (unsigned int discriminator)
21403 {
21404 m_discriminator = discriminator;
21405 m_line_has_non_zero_discriminator |= discriminator != 0;
21406 }
21407
21408 /* Handle DW_LNE_set_address. */
21409 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21410 {
21411 m_op_index = 0;
21412 address += baseaddr;
21413 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21414 }
21415
21416 /* Handle DW_LNS_advance_pc. */
21417 void handle_advance_pc (CORE_ADDR adjust);
21418
21419 /* Handle a special opcode. */
21420 void handle_special_opcode (unsigned char op_code);
21421
21422 /* Handle DW_LNS_advance_line. */
21423 void handle_advance_line (int line_delta)
21424 {
21425 advance_line (line_delta);
21426 }
21427
21428 /* Handle DW_LNS_set_file. */
21429 void handle_set_file (file_name_index file);
21430
21431 /* Handle DW_LNS_negate_stmt. */
21432 void handle_negate_stmt ()
21433 {
21434 m_is_stmt = !m_is_stmt;
21435 }
21436
21437 /* Handle DW_LNS_const_add_pc. */
21438 void handle_const_add_pc ();
21439
21440 /* Handle DW_LNS_fixed_advance_pc. */
21441 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21442 {
21443 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21444 m_op_index = 0;
21445 }
21446
21447 /* Handle DW_LNS_copy. */
21448 void handle_copy ()
21449 {
21450 record_line (false);
21451 m_discriminator = 0;
21452 }
21453
21454 /* Handle DW_LNE_end_sequence. */
21455 void handle_end_sequence ()
21456 {
21457 m_currently_recording_lines = true;
21458 }
21459
21460 private:
21461 /* Advance the line by LINE_DELTA. */
21462 void advance_line (int line_delta)
21463 {
21464 m_line += line_delta;
21465
21466 if (line_delta != 0)
21467 m_line_has_non_zero_discriminator = m_discriminator != 0;
21468 }
21469
21470 struct dwarf2_cu *m_cu;
21471
21472 gdbarch *m_gdbarch;
21473
21474 /* True if we're recording lines.
21475 Otherwise we're building partial symtabs and are just interested in
21476 finding include files mentioned by the line number program. */
21477 bool m_record_lines_p;
21478
21479 /* The line number header. */
21480 line_header *m_line_header;
21481
21482 /* These are part of the standard DWARF line number state machine,
21483 and initialized according to the DWARF spec. */
21484
21485 unsigned char m_op_index = 0;
21486 /* The line table index of the current file. */
21487 file_name_index m_file = 1;
21488 unsigned int m_line = 1;
21489
21490 /* These are initialized in the constructor. */
21491
21492 CORE_ADDR m_address;
21493 bool m_is_stmt;
21494 unsigned int m_discriminator;
21495
21496 /* Additional bits of state we need to track. */
21497
21498 /* The last file that we called dwarf2_start_subfile for.
21499 This is only used for TLLs. */
21500 unsigned int m_last_file = 0;
21501 /* The last file a line number was recorded for. */
21502 struct subfile *m_last_subfile = NULL;
21503
21504 /* The address of the last line entry. */
21505 CORE_ADDR m_last_address;
21506
21507 /* Set to true when a previous line at the same address (using
21508 m_last_address) had m_is_stmt true. This is reset to false when a
21509 line entry at a new address (m_address different to m_last_address) is
21510 processed. */
21511 bool m_stmt_at_address = false;
21512
21513 /* When true, record the lines we decode. */
21514 bool m_currently_recording_lines = false;
21515
21516 /* The last line number that was recorded, used to coalesce
21517 consecutive entries for the same line. This can happen, for
21518 example, when discriminators are present. PR 17276. */
21519 unsigned int m_last_line = 0;
21520 bool m_line_has_non_zero_discriminator = false;
21521 };
21522
21523 void
21524 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21525 {
21526 CORE_ADDR addr_adj = (((m_op_index + adjust)
21527 / m_line_header->maximum_ops_per_instruction)
21528 * m_line_header->minimum_instruction_length);
21529 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21530 m_op_index = ((m_op_index + adjust)
21531 % m_line_header->maximum_ops_per_instruction);
21532 }
21533
21534 void
21535 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21536 {
21537 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21538 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21539 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21540 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
21541 / m_line_header->maximum_ops_per_instruction)
21542 * m_line_header->minimum_instruction_length);
21543 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21544 m_op_index = ((m_op_index + adj_opcode_d)
21545 % m_line_header->maximum_ops_per_instruction);
21546
21547 int line_delta = m_line_header->line_base + adj_opcode_r;
21548 advance_line (line_delta);
21549 record_line (false);
21550 m_discriminator = 0;
21551 }
21552
21553 void
21554 lnp_state_machine::handle_set_file (file_name_index file)
21555 {
21556 m_file = file;
21557
21558 const file_entry *fe = current_file ();
21559 if (fe == NULL)
21560 dwarf2_debug_line_missing_file_complaint ();
21561 else if (m_record_lines_p)
21562 {
21563 const char *dir = fe->include_dir (m_line_header);
21564
21565 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21566 m_line_has_non_zero_discriminator = m_discriminator != 0;
21567 dwarf2_start_subfile (m_cu, fe->name, dir);
21568 }
21569 }
21570
21571 void
21572 lnp_state_machine::handle_const_add_pc ()
21573 {
21574 CORE_ADDR adjust
21575 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21576
21577 CORE_ADDR addr_adj
21578 = (((m_op_index + adjust)
21579 / m_line_header->maximum_ops_per_instruction)
21580 * m_line_header->minimum_instruction_length);
21581
21582 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21583 m_op_index = ((m_op_index + adjust)
21584 % m_line_header->maximum_ops_per_instruction);
21585 }
21586
21587 /* Return non-zero if we should add LINE to the line number table.
21588 LINE is the line to add, LAST_LINE is the last line that was added,
21589 LAST_SUBFILE is the subfile for LAST_LINE.
21590 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21591 had a non-zero discriminator.
21592
21593 We have to be careful in the presence of discriminators.
21594 E.g., for this line:
21595
21596 for (i = 0; i < 100000; i++);
21597
21598 clang can emit four line number entries for that one line,
21599 each with a different discriminator.
21600 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21601
21602 However, we want gdb to coalesce all four entries into one.
21603 Otherwise the user could stepi into the middle of the line and
21604 gdb would get confused about whether the pc really was in the
21605 middle of the line.
21606
21607 Things are further complicated by the fact that two consecutive
21608 line number entries for the same line is a heuristic used by gcc
21609 to denote the end of the prologue. So we can't just discard duplicate
21610 entries, we have to be selective about it. The heuristic we use is
21611 that we only collapse consecutive entries for the same line if at least
21612 one of those entries has a non-zero discriminator. PR 17276.
21613
21614 Note: Addresses in the line number state machine can never go backwards
21615 within one sequence, thus this coalescing is ok. */
21616
21617 static int
21618 dwarf_record_line_p (struct dwarf2_cu *cu,
21619 unsigned int line, unsigned int last_line,
21620 int line_has_non_zero_discriminator,
21621 struct subfile *last_subfile)
21622 {
21623 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21624 return 1;
21625 if (line != last_line)
21626 return 1;
21627 /* Same line for the same file that we've seen already.
21628 As a last check, for pr 17276, only record the line if the line
21629 has never had a non-zero discriminator. */
21630 if (!line_has_non_zero_discriminator)
21631 return 1;
21632 return 0;
21633 }
21634
21635 /* Use the CU's builder to record line number LINE beginning at
21636 address ADDRESS in the line table of subfile SUBFILE. */
21637
21638 static void
21639 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21640 unsigned int line, CORE_ADDR address, bool is_stmt,
21641 struct dwarf2_cu *cu)
21642 {
21643 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21644
21645 if (dwarf_line_debug)
21646 {
21647 fprintf_unfiltered (gdb_stdlog,
21648 "Recording line %u, file %s, address %s\n",
21649 line, lbasename (subfile->name),
21650 paddress (gdbarch, address));
21651 }
21652
21653 if (cu != nullptr)
21654 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
21655 }
21656
21657 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21658 Mark the end of a set of line number records.
21659 The arguments are the same as for dwarf_record_line_1.
21660 If SUBFILE is NULL the request is ignored. */
21661
21662 static void
21663 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21664 CORE_ADDR address, struct dwarf2_cu *cu)
21665 {
21666 if (subfile == NULL)
21667 return;
21668
21669 if (dwarf_line_debug)
21670 {
21671 fprintf_unfiltered (gdb_stdlog,
21672 "Finishing current line, file %s, address %s\n",
21673 lbasename (subfile->name),
21674 paddress (gdbarch, address));
21675 }
21676
21677 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
21678 }
21679
21680 void
21681 lnp_state_machine::record_line (bool end_sequence)
21682 {
21683 if (dwarf_line_debug)
21684 {
21685 fprintf_unfiltered (gdb_stdlog,
21686 "Processing actual line %u: file %u,"
21687 " address %s, is_stmt %u, discrim %u%s\n",
21688 m_line, m_file,
21689 paddress (m_gdbarch, m_address),
21690 m_is_stmt, m_discriminator,
21691 (end_sequence ? "\t(end sequence)" : ""));
21692 }
21693
21694 file_entry *fe = current_file ();
21695
21696 if (fe == NULL)
21697 dwarf2_debug_line_missing_file_complaint ();
21698 /* For now we ignore lines not starting on an instruction boundary.
21699 But not when processing end_sequence for compatibility with the
21700 previous version of the code. */
21701 else if (m_op_index == 0 || end_sequence)
21702 {
21703 fe->included_p = 1;
21704 if (m_record_lines_p)
21705 {
21706 /* When we switch files we insert an end maker in the first file,
21707 switch to the second file and add a new line entry. The
21708 problem is that the end marker inserted in the first file will
21709 discard any previous line entries at the same address. If the
21710 line entries in the first file are marked as is-stmt, while
21711 the new line in the second file is non-stmt, then this means
21712 the end marker will discard is-stmt lines so we can have a
21713 non-stmt line. This means that there are less addresses at
21714 which the user can insert a breakpoint.
21715
21716 To improve this we track the last address in m_last_address,
21717 and whether we have seen an is-stmt at this address. Then
21718 when switching files, if we have seen a stmt at the current
21719 address, and we are switching to create a non-stmt line, then
21720 discard the new line. */
21721 bool file_changed
21722 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21723 bool ignore_this_line
21724 = ((file_changed && !end_sequence && m_last_address == m_address
21725 && !m_is_stmt && m_stmt_at_address)
21726 || (!end_sequence && m_line == 0));
21727
21728 if ((file_changed && !ignore_this_line) || end_sequence)
21729 {
21730 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21731 m_currently_recording_lines ? m_cu : nullptr);
21732 }
21733
21734 if (!end_sequence && !ignore_this_line)
21735 {
21736 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21737
21738 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21739 m_line_has_non_zero_discriminator,
21740 m_last_subfile))
21741 {
21742 buildsym_compunit *builder = m_cu->get_builder ();
21743 dwarf_record_line_1 (m_gdbarch,
21744 builder->get_current_subfile (),
21745 m_line, m_address, is_stmt,
21746 m_currently_recording_lines ? m_cu : nullptr);
21747 }
21748 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21749 m_last_line = m_line;
21750 }
21751 }
21752 }
21753
21754 /* Track whether we have seen any m_is_stmt true at m_address in case we
21755 have multiple line table entries all at m_address. */
21756 if (m_last_address != m_address)
21757 {
21758 m_stmt_at_address = false;
21759 m_last_address = m_address;
21760 }
21761 m_stmt_at_address |= m_is_stmt;
21762 }
21763
21764 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21765 line_header *lh, bool record_lines_p)
21766 {
21767 m_cu = cu;
21768 m_gdbarch = arch;
21769 m_record_lines_p = record_lines_p;
21770 m_line_header = lh;
21771
21772 m_currently_recording_lines = true;
21773
21774 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21775 was a line entry for it so that the backend has a chance to adjust it
21776 and also record it in case it needs it. This is currently used by MIPS
21777 code, cf. `mips_adjust_dwarf2_line'. */
21778 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21779 m_is_stmt = lh->default_is_stmt;
21780 m_discriminator = 0;
21781
21782 m_last_address = m_address;
21783 m_stmt_at_address = false;
21784 }
21785
21786 void
21787 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21788 const gdb_byte *line_ptr,
21789 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21790 {
21791 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21792 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21793 located at 0x0. In this case, additionally check that if
21794 ADDRESS < UNRELOCATED_LOWPC. */
21795
21796 if ((address == 0 && address < unrelocated_lowpc)
21797 || address == (CORE_ADDR) -1)
21798 {
21799 /* This line table is for a function which has been
21800 GCd by the linker. Ignore it. PR gdb/12528 */
21801
21802 struct objfile *objfile = cu->per_objfile->objfile;
21803 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21804
21805 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21806 line_offset, objfile_name (objfile));
21807 m_currently_recording_lines = false;
21808 /* Note: m_currently_recording_lines is left as false until we see
21809 DW_LNE_end_sequence. */
21810 }
21811 }
21812
21813 /* Subroutine of dwarf_decode_lines to simplify it.
21814 Process the line number information in LH.
21815 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21816 program in order to set included_p for every referenced header. */
21817
21818 static void
21819 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21820 const int decode_for_pst_p, CORE_ADDR lowpc)
21821 {
21822 const gdb_byte *line_ptr, *extended_end;
21823 const gdb_byte *line_end;
21824 unsigned int bytes_read, extended_len;
21825 unsigned char op_code, extended_op;
21826 CORE_ADDR baseaddr;
21827 struct objfile *objfile = cu->per_objfile->objfile;
21828 bfd *abfd = objfile->obfd;
21829 struct gdbarch *gdbarch = objfile->arch ();
21830 /* True if we're recording line info (as opposed to building partial
21831 symtabs and just interested in finding include files mentioned by
21832 the line number program). */
21833 bool record_lines_p = !decode_for_pst_p;
21834
21835 baseaddr = objfile->text_section_offset ();
21836
21837 line_ptr = lh->statement_program_start;
21838 line_end = lh->statement_program_end;
21839
21840 /* Read the statement sequences until there's nothing left. */
21841 while (line_ptr < line_end)
21842 {
21843 /* The DWARF line number program state machine. Reset the state
21844 machine at the start of each sequence. */
21845 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21846 bool end_sequence = false;
21847
21848 if (record_lines_p)
21849 {
21850 /* Start a subfile for the current file of the state
21851 machine. */
21852 const file_entry *fe = state_machine.current_file ();
21853
21854 if (fe != NULL)
21855 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21856 }
21857
21858 /* Decode the table. */
21859 while (line_ptr < line_end && !end_sequence)
21860 {
21861 op_code = read_1_byte (abfd, line_ptr);
21862 line_ptr += 1;
21863
21864 if (op_code >= lh->opcode_base)
21865 {
21866 /* Special opcode. */
21867 state_machine.handle_special_opcode (op_code);
21868 }
21869 else switch (op_code)
21870 {
21871 case DW_LNS_extended_op:
21872 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21873 &bytes_read);
21874 line_ptr += bytes_read;
21875 extended_end = line_ptr + extended_len;
21876 extended_op = read_1_byte (abfd, line_ptr);
21877 line_ptr += 1;
21878 if (DW_LNE_lo_user <= extended_op
21879 && extended_op <= DW_LNE_hi_user)
21880 {
21881 /* Vendor extension, ignore. */
21882 line_ptr = extended_end;
21883 break;
21884 }
21885 switch (extended_op)
21886 {
21887 case DW_LNE_end_sequence:
21888 state_machine.handle_end_sequence ();
21889 end_sequence = true;
21890 break;
21891 case DW_LNE_set_address:
21892 {
21893 CORE_ADDR address
21894 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21895 line_ptr += bytes_read;
21896
21897 state_machine.check_line_address (cu, line_ptr,
21898 lowpc - baseaddr, address);
21899 state_machine.handle_set_address (baseaddr, address);
21900 }
21901 break;
21902 case DW_LNE_define_file:
21903 {
21904 const char *cur_file;
21905 unsigned int mod_time, length;
21906 dir_index dindex;
21907
21908 cur_file = read_direct_string (abfd, line_ptr,
21909 &bytes_read);
21910 line_ptr += bytes_read;
21911 dindex = (dir_index)
21912 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21913 line_ptr += bytes_read;
21914 mod_time =
21915 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21916 line_ptr += bytes_read;
21917 length =
21918 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21919 line_ptr += bytes_read;
21920 lh->add_file_name (cur_file, dindex, mod_time, length);
21921 }
21922 break;
21923 case DW_LNE_set_discriminator:
21924 {
21925 /* The discriminator is not interesting to the
21926 debugger; just ignore it. We still need to
21927 check its value though:
21928 if there are consecutive entries for the same
21929 (non-prologue) line we want to coalesce them.
21930 PR 17276. */
21931 unsigned int discr
21932 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21933 line_ptr += bytes_read;
21934
21935 state_machine.handle_set_discriminator (discr);
21936 }
21937 break;
21938 default:
21939 complaint (_("mangled .debug_line section"));
21940 return;
21941 }
21942 /* Make sure that we parsed the extended op correctly. If e.g.
21943 we expected a different address size than the producer used,
21944 we may have read the wrong number of bytes. */
21945 if (line_ptr != extended_end)
21946 {
21947 complaint (_("mangled .debug_line section"));
21948 return;
21949 }
21950 break;
21951 case DW_LNS_copy:
21952 state_machine.handle_copy ();
21953 break;
21954 case DW_LNS_advance_pc:
21955 {
21956 CORE_ADDR adjust
21957 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21958 line_ptr += bytes_read;
21959
21960 state_machine.handle_advance_pc (adjust);
21961 }
21962 break;
21963 case DW_LNS_advance_line:
21964 {
21965 int line_delta
21966 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21967 line_ptr += bytes_read;
21968
21969 state_machine.handle_advance_line (line_delta);
21970 }
21971 break;
21972 case DW_LNS_set_file:
21973 {
21974 file_name_index file
21975 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21976 &bytes_read);
21977 line_ptr += bytes_read;
21978
21979 state_machine.handle_set_file (file);
21980 }
21981 break;
21982 case DW_LNS_set_column:
21983 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21984 line_ptr += bytes_read;
21985 break;
21986 case DW_LNS_negate_stmt:
21987 state_machine.handle_negate_stmt ();
21988 break;
21989 case DW_LNS_set_basic_block:
21990 break;
21991 /* Add to the address register of the state machine the
21992 address increment value corresponding to special opcode
21993 255. I.e., this value is scaled by the minimum
21994 instruction length since special opcode 255 would have
21995 scaled the increment. */
21996 case DW_LNS_const_add_pc:
21997 state_machine.handle_const_add_pc ();
21998 break;
21999 case DW_LNS_fixed_advance_pc:
22000 {
22001 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
22002 line_ptr += 2;
22003
22004 state_machine.handle_fixed_advance_pc (addr_adj);
22005 }
22006 break;
22007 default:
22008 {
22009 /* Unknown standard opcode, ignore it. */
22010 int i;
22011
22012 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
22013 {
22014 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
22015 line_ptr += bytes_read;
22016 }
22017 }
22018 }
22019 }
22020
22021 if (!end_sequence)
22022 dwarf2_debug_line_missing_end_sequence_complaint ();
22023
22024 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
22025 in which case we still finish recording the last line). */
22026 state_machine.record_line (true);
22027 }
22028 }
22029
22030 /* Decode the Line Number Program (LNP) for the given line_header
22031 structure and CU. The actual information extracted and the type
22032 of structures created from the LNP depends on the value of PST.
22033
22034 1. If PST is NULL, then this procedure uses the data from the program
22035 to create all necessary symbol tables, and their linetables.
22036
22037 2. If PST is not NULL, this procedure reads the program to determine
22038 the list of files included by the unit represented by PST, and
22039 builds all the associated partial symbol tables.
22040
22041 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
22042 It is used for relative paths in the line table.
22043 NOTE: When processing partial symtabs (pst != NULL),
22044 comp_dir == pst->dirname.
22045
22046 NOTE: It is important that psymtabs have the same file name (via strcmp)
22047 as the corresponding symtab. Since COMP_DIR is not used in the name of the
22048 symtab we don't use it in the name of the psymtabs we create.
22049 E.g. expand_line_sal requires this when finding psymtabs to expand.
22050 A good testcase for this is mb-inline.exp.
22051
22052 LOWPC is the lowest address in CU (or 0 if not known).
22053
22054 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
22055 for its PC<->lines mapping information. Otherwise only the filename
22056 table is read in. */
22057
22058 static void
22059 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
22060 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
22061 CORE_ADDR lowpc, int decode_mapping)
22062 {
22063 struct objfile *objfile = cu->per_objfile->objfile;
22064 const int decode_for_pst_p = (pst != NULL);
22065
22066 if (decode_mapping)
22067 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
22068
22069 if (decode_for_pst_p)
22070 {
22071 /* Now that we're done scanning the Line Header Program, we can
22072 create the psymtab of each included file. */
22073 for (auto &file_entry : lh->file_names ())
22074 if (file_entry.included_p == 1)
22075 {
22076 gdb::unique_xmalloc_ptr<char> name_holder;
22077 const char *include_name =
22078 psymtab_include_file_name (lh, file_entry, pst,
22079 comp_dir, &name_holder);
22080 if (include_name != NULL)
22081 dwarf2_create_include_psymtab (include_name, pst, objfile);
22082 }
22083 }
22084 else
22085 {
22086 /* Make sure a symtab is created for every file, even files
22087 which contain only variables (i.e. no code with associated
22088 line numbers). */
22089 buildsym_compunit *builder = cu->get_builder ();
22090 struct compunit_symtab *cust = builder->get_compunit_symtab ();
22091
22092 for (auto &fe : lh->file_names ())
22093 {
22094 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
22095 if (builder->get_current_subfile ()->symtab == NULL)
22096 {
22097 builder->get_current_subfile ()->symtab
22098 = allocate_symtab (cust,
22099 builder->get_current_subfile ()->name);
22100 }
22101 fe.symtab = builder->get_current_subfile ()->symtab;
22102 }
22103 }
22104 }
22105
22106 /* Start a subfile for DWARF. FILENAME is the name of the file and
22107 DIRNAME the name of the source directory which contains FILENAME
22108 or NULL if not known.
22109 This routine tries to keep line numbers from identical absolute and
22110 relative file names in a common subfile.
22111
22112 Using the `list' example from the GDB testsuite, which resides in
22113 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
22114 of /srcdir/list0.c yields the following debugging information for list0.c:
22115
22116 DW_AT_name: /srcdir/list0.c
22117 DW_AT_comp_dir: /compdir
22118 files.files[0].name: list0.h
22119 files.files[0].dir: /srcdir
22120 files.files[1].name: list0.c
22121 files.files[1].dir: /srcdir
22122
22123 The line number information for list0.c has to end up in a single
22124 subfile, so that `break /srcdir/list0.c:1' works as expected.
22125 start_subfile will ensure that this happens provided that we pass the
22126 concatenation of files.files[1].dir and files.files[1].name as the
22127 subfile's name. */
22128
22129 static void
22130 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
22131 const char *dirname)
22132 {
22133 gdb::unique_xmalloc_ptr<char> copy;
22134
22135 /* In order not to lose the line information directory,
22136 we concatenate it to the filename when it makes sense.
22137 Note that the Dwarf3 standard says (speaking of filenames in line
22138 information): ``The directory index is ignored for file names
22139 that represent full path names''. Thus ignoring dirname in the
22140 `else' branch below isn't an issue. */
22141
22142 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
22143 {
22144 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
22145 filename = copy.get ();
22146 }
22147
22148 cu->get_builder ()->start_subfile (filename);
22149 }
22150
22151 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
22152 buildsym_compunit constructor. */
22153
22154 struct compunit_symtab *
22155 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
22156 CORE_ADDR low_pc)
22157 {
22158 gdb_assert (m_builder == nullptr);
22159
22160 m_builder.reset (new struct buildsym_compunit
22161 (this->per_objfile->objfile,
22162 name, comp_dir, language, low_pc));
22163
22164 list_in_scope = get_builder ()->get_file_symbols ();
22165
22166 get_builder ()->record_debugformat ("DWARF 2");
22167 get_builder ()->record_producer (producer);
22168
22169 processing_has_namespace_info = false;
22170
22171 return get_builder ()->get_compunit_symtab ();
22172 }
22173
22174 static void
22175 var_decode_location (struct attribute *attr, struct symbol *sym,
22176 struct dwarf2_cu *cu)
22177 {
22178 struct objfile *objfile = cu->per_objfile->objfile;
22179 struct comp_unit_head *cu_header = &cu->header;
22180
22181 /* NOTE drow/2003-01-30: There used to be a comment and some special
22182 code here to turn a symbol with DW_AT_external and a
22183 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
22184 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
22185 with some versions of binutils) where shared libraries could have
22186 relocations against symbols in their debug information - the
22187 minimal symbol would have the right address, but the debug info
22188 would not. It's no longer necessary, because we will explicitly
22189 apply relocations when we read in the debug information now. */
22190
22191 /* A DW_AT_location attribute with no contents indicates that a
22192 variable has been optimized away. */
22193 if (attr->form_is_block () && attr->as_block ()->size == 0)
22194 {
22195 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22196 return;
22197 }
22198
22199 /* Handle one degenerate form of location expression specially, to
22200 preserve GDB's previous behavior when section offsets are
22201 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
22202 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
22203
22204 if (attr->form_is_block ())
22205 {
22206 struct dwarf_block *block = attr->as_block ();
22207
22208 if ((block->data[0] == DW_OP_addr
22209 && block->size == 1 + cu_header->addr_size)
22210 || ((block->data[0] == DW_OP_GNU_addr_index
22211 || block->data[0] == DW_OP_addrx)
22212 && (block->size
22213 == 1 + leb128_size (&block->data[1]))))
22214 {
22215 unsigned int dummy;
22216
22217 if (block->data[0] == DW_OP_addr)
22218 SET_SYMBOL_VALUE_ADDRESS
22219 (sym, cu->header.read_address (objfile->obfd,
22220 block->data + 1,
22221 &dummy));
22222 else
22223 SET_SYMBOL_VALUE_ADDRESS
22224 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
22225 &dummy));
22226 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
22227 fixup_symbol_section (sym, objfile);
22228 SET_SYMBOL_VALUE_ADDRESS
22229 (sym,
22230 SYMBOL_VALUE_ADDRESS (sym)
22231 + objfile->section_offsets[sym->section_index ()]);
22232 return;
22233 }
22234 }
22235
22236 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
22237 expression evaluator, and use LOC_COMPUTED only when necessary
22238 (i.e. when the value of a register or memory location is
22239 referenced, or a thread-local block, etc.). Then again, it might
22240 not be worthwhile. I'm assuming that it isn't unless performance
22241 or memory numbers show me otherwise. */
22242
22243 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
22244
22245 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
22246 cu->has_loclist = true;
22247 }
22248
22249 /* Given a pointer to a DWARF information entry, figure out if we need
22250 to make a symbol table entry for it, and if so, create a new entry
22251 and return a pointer to it.
22252 If TYPE is NULL, determine symbol type from the die, otherwise
22253 used the passed type.
22254 If SPACE is not NULL, use it to hold the new symbol. If it is
22255 NULL, allocate a new symbol on the objfile's obstack. */
22256
22257 static struct symbol *
22258 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
22259 struct symbol *space)
22260 {
22261 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22262 struct objfile *objfile = per_objfile->objfile;
22263 struct gdbarch *gdbarch = objfile->arch ();
22264 struct symbol *sym = NULL;
22265 const char *name;
22266 struct attribute *attr = NULL;
22267 struct attribute *attr2 = NULL;
22268 CORE_ADDR baseaddr;
22269 struct pending **list_to_add = NULL;
22270
22271 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
22272
22273 baseaddr = objfile->text_section_offset ();
22274
22275 name = dwarf2_name (die, cu);
22276 if (name)
22277 {
22278 int suppress_add = 0;
22279
22280 if (space)
22281 sym = space;
22282 else
22283 sym = new (&objfile->objfile_obstack) symbol;
22284 OBJSTAT (objfile, n_syms++);
22285
22286 /* Cache this symbol's name and the name's demangled form (if any). */
22287 sym->set_language (cu->language, &objfile->objfile_obstack);
22288 /* Fortran does not have mangling standard and the mangling does differ
22289 between gfortran, iFort etc. */
22290 const char *physname
22291 = (cu->language == language_fortran
22292 ? dwarf2_full_name (name, die, cu)
22293 : dwarf2_physname (name, die, cu));
22294 const char *linkagename = dw2_linkage_name (die, cu);
22295
22296 if (linkagename == nullptr || cu->language == language_ada)
22297 sym->set_linkage_name (physname);
22298 else
22299 {
22300 sym->set_demangled_name (physname, &objfile->objfile_obstack);
22301 sym->set_linkage_name (linkagename);
22302 }
22303
22304 /* Default assumptions.
22305 Use the passed type or decode it from the die. */
22306 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22307 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22308 if (type != NULL)
22309 SYMBOL_TYPE (sym) = type;
22310 else
22311 SYMBOL_TYPE (sym) = die_type (die, cu);
22312 attr = dwarf2_attr (die,
22313 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
22314 cu);
22315 if (attr != nullptr)
22316 SYMBOL_LINE (sym) = attr->constant_value (0);
22317
22318 attr = dwarf2_attr (die,
22319 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
22320 cu);
22321 if (attr != nullptr && attr->is_nonnegative ())
22322 {
22323 file_name_index file_index
22324 = (file_name_index) attr->as_nonnegative ();
22325 struct file_entry *fe;
22326
22327 if (cu->line_header != NULL)
22328 fe = cu->line_header->file_name_at (file_index);
22329 else
22330 fe = NULL;
22331
22332 if (fe == NULL)
22333 complaint (_("file index out of range"));
22334 else
22335 symbol_set_symtab (sym, fe->symtab);
22336 }
22337
22338 switch (die->tag)
22339 {
22340 case DW_TAG_label:
22341 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
22342 if (attr != nullptr)
22343 {
22344 CORE_ADDR addr;
22345
22346 addr = attr->as_address ();
22347 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
22348 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
22349 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
22350 }
22351 else
22352 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22353 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
22354 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
22355 add_symbol_to_list (sym, cu->list_in_scope);
22356 break;
22357 case DW_TAG_subprogram:
22358 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22359 finish_block. */
22360 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
22361 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22362 if ((attr2 != nullptr && attr2->as_boolean ())
22363 || cu->language == language_ada
22364 || cu->language == language_fortran)
22365 {
22366 /* Subprograms marked external are stored as a global symbol.
22367 Ada and Fortran subprograms, whether marked external or
22368 not, are always stored as a global symbol, because we want
22369 to be able to access them globally. For instance, we want
22370 to be able to break on a nested subprogram without having
22371 to specify the context. */
22372 list_to_add = cu->get_builder ()->get_global_symbols ();
22373 }
22374 else
22375 {
22376 list_to_add = cu->list_in_scope;
22377 }
22378 break;
22379 case DW_TAG_inlined_subroutine:
22380 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22381 finish_block. */
22382 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
22383 SYMBOL_INLINED (sym) = 1;
22384 list_to_add = cu->list_in_scope;
22385 break;
22386 case DW_TAG_template_value_param:
22387 suppress_add = 1;
22388 /* Fall through. */
22389 case DW_TAG_constant:
22390 case DW_TAG_variable:
22391 case DW_TAG_member:
22392 /* Compilation with minimal debug info may result in
22393 variables with missing type entries. Change the
22394 misleading `void' type to something sensible. */
22395 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
22396 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
22397
22398 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22399 /* In the case of DW_TAG_member, we should only be called for
22400 static const members. */
22401 if (die->tag == DW_TAG_member)
22402 {
22403 /* dwarf2_add_field uses die_is_declaration,
22404 so we do the same. */
22405 gdb_assert (die_is_declaration (die, cu));
22406 gdb_assert (attr);
22407 }
22408 if (attr != nullptr)
22409 {
22410 dwarf2_const_value (attr, sym, cu);
22411 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22412 if (!suppress_add)
22413 {
22414 if (attr2 != nullptr && attr2->as_boolean ())
22415 list_to_add = cu->get_builder ()->get_global_symbols ();
22416 else
22417 list_to_add = cu->list_in_scope;
22418 }
22419 break;
22420 }
22421 attr = dwarf2_attr (die, DW_AT_location, cu);
22422 if (attr != nullptr)
22423 {
22424 var_decode_location (attr, sym, cu);
22425 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22426
22427 /* Fortran explicitly imports any global symbols to the local
22428 scope by DW_TAG_common_block. */
22429 if (cu->language == language_fortran && die->parent
22430 && die->parent->tag == DW_TAG_common_block)
22431 attr2 = NULL;
22432
22433 if (SYMBOL_CLASS (sym) == LOC_STATIC
22434 && SYMBOL_VALUE_ADDRESS (sym) == 0
22435 && !per_objfile->per_bfd->has_section_at_zero)
22436 {
22437 /* When a static variable is eliminated by the linker,
22438 the corresponding debug information is not stripped
22439 out, but the variable address is set to null;
22440 do not add such variables into symbol table. */
22441 }
22442 else if (attr2 != nullptr && attr2->as_boolean ())
22443 {
22444 if (SYMBOL_CLASS (sym) == LOC_STATIC
22445 && (objfile->flags & OBJF_MAINLINE) == 0
22446 && per_objfile->per_bfd->can_copy)
22447 {
22448 /* A global static variable might be subject to
22449 copy relocation. We first check for a local
22450 minsym, though, because maybe the symbol was
22451 marked hidden, in which case this would not
22452 apply. */
22453 bound_minimal_symbol found
22454 = (lookup_minimal_symbol_linkage
22455 (sym->linkage_name (), objfile));
22456 if (found.minsym != nullptr)
22457 sym->maybe_copied = 1;
22458 }
22459
22460 /* A variable with DW_AT_external is never static,
22461 but it may be block-scoped. */
22462 list_to_add
22463 = ((cu->list_in_scope
22464 == cu->get_builder ()->get_file_symbols ())
22465 ? cu->get_builder ()->get_global_symbols ()
22466 : cu->list_in_scope);
22467 }
22468 else
22469 list_to_add = cu->list_in_scope;
22470 }
22471 else
22472 {
22473 /* We do not know the address of this symbol.
22474 If it is an external symbol and we have type information
22475 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22476 The address of the variable will then be determined from
22477 the minimal symbol table whenever the variable is
22478 referenced. */
22479 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22480
22481 /* Fortran explicitly imports any global symbols to the local
22482 scope by DW_TAG_common_block. */
22483 if (cu->language == language_fortran && die->parent
22484 && die->parent->tag == DW_TAG_common_block)
22485 {
22486 /* SYMBOL_CLASS doesn't matter here because
22487 read_common_block is going to reset it. */
22488 if (!suppress_add)
22489 list_to_add = cu->list_in_scope;
22490 }
22491 else if (attr2 != nullptr && attr2->as_boolean ()
22492 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
22493 {
22494 /* A variable with DW_AT_external is never static, but it
22495 may be block-scoped. */
22496 list_to_add
22497 = ((cu->list_in_scope
22498 == cu->get_builder ()->get_file_symbols ())
22499 ? cu->get_builder ()->get_global_symbols ()
22500 : cu->list_in_scope);
22501
22502 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
22503 }
22504 else if (!die_is_declaration (die, cu))
22505 {
22506 /* Use the default LOC_OPTIMIZED_OUT class. */
22507 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
22508 if (!suppress_add)
22509 list_to_add = cu->list_in_scope;
22510 }
22511 }
22512 break;
22513 case DW_TAG_formal_parameter:
22514 {
22515 /* If we are inside a function, mark this as an argument. If
22516 not, we might be looking at an argument to an inlined function
22517 when we do not have enough information to show inlined frames;
22518 pretend it's a local variable in that case so that the user can
22519 still see it. */
22520 struct context_stack *curr
22521 = cu->get_builder ()->get_current_context_stack ();
22522 if (curr != nullptr && curr->name != nullptr)
22523 SYMBOL_IS_ARGUMENT (sym) = 1;
22524 attr = dwarf2_attr (die, DW_AT_location, cu);
22525 if (attr != nullptr)
22526 {
22527 var_decode_location (attr, sym, cu);
22528 }
22529 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22530 if (attr != nullptr)
22531 {
22532 dwarf2_const_value (attr, sym, cu);
22533 }
22534
22535 list_to_add = cu->list_in_scope;
22536 }
22537 break;
22538 case DW_TAG_unspecified_parameters:
22539 /* From varargs functions; gdb doesn't seem to have any
22540 interest in this information, so just ignore it for now.
22541 (FIXME?) */
22542 break;
22543 case DW_TAG_template_type_param:
22544 suppress_add = 1;
22545 /* Fall through. */
22546 case DW_TAG_class_type:
22547 case DW_TAG_interface_type:
22548 case DW_TAG_structure_type:
22549 case DW_TAG_union_type:
22550 case DW_TAG_set_type:
22551 case DW_TAG_enumeration_type:
22552 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22553 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
22554
22555 {
22556 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22557 really ever be static objects: otherwise, if you try
22558 to, say, break of a class's method and you're in a file
22559 which doesn't mention that class, it won't work unless
22560 the check for all static symbols in lookup_symbol_aux
22561 saves you. See the OtherFileClass tests in
22562 gdb.c++/namespace.exp. */
22563
22564 if (!suppress_add)
22565 {
22566 buildsym_compunit *builder = cu->get_builder ();
22567 list_to_add
22568 = (cu->list_in_scope == builder->get_file_symbols ()
22569 && cu->language == language_cplus
22570 ? builder->get_global_symbols ()
22571 : cu->list_in_scope);
22572
22573 /* The semantics of C++ state that "struct foo {
22574 ... }" also defines a typedef for "foo". */
22575 if (cu->language == language_cplus
22576 || cu->language == language_ada
22577 || cu->language == language_d
22578 || cu->language == language_rust)
22579 {
22580 /* The symbol's name is already allocated along
22581 with this objfile, so we don't need to
22582 duplicate it for the type. */
22583 if (SYMBOL_TYPE (sym)->name () == 0)
22584 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
22585 }
22586 }
22587 }
22588 break;
22589 case DW_TAG_typedef:
22590 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22591 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22592 list_to_add = cu->list_in_scope;
22593 break;
22594 case DW_TAG_array_type:
22595 case DW_TAG_base_type:
22596 case DW_TAG_subrange_type:
22597 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22598 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22599 list_to_add = cu->list_in_scope;
22600 break;
22601 case DW_TAG_enumerator:
22602 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22603 if (attr != nullptr)
22604 {
22605 dwarf2_const_value (attr, sym, cu);
22606 }
22607 {
22608 /* NOTE: carlton/2003-11-10: See comment above in the
22609 DW_TAG_class_type, etc. block. */
22610
22611 list_to_add
22612 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22613 && cu->language == language_cplus
22614 ? cu->get_builder ()->get_global_symbols ()
22615 : cu->list_in_scope);
22616 }
22617 break;
22618 case DW_TAG_imported_declaration:
22619 case DW_TAG_namespace:
22620 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22621 list_to_add = cu->get_builder ()->get_global_symbols ();
22622 break;
22623 case DW_TAG_module:
22624 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22625 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22626 list_to_add = cu->get_builder ()->get_global_symbols ();
22627 break;
22628 case DW_TAG_common_block:
22629 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22630 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22631 add_symbol_to_list (sym, cu->list_in_scope);
22632 break;
22633 default:
22634 /* Not a tag we recognize. Hopefully we aren't processing
22635 trash data, but since we must specifically ignore things
22636 we don't recognize, there is nothing else we should do at
22637 this point. */
22638 complaint (_("unsupported tag: '%s'"),
22639 dwarf_tag_name (die->tag));
22640 break;
22641 }
22642
22643 if (suppress_add)
22644 {
22645 sym->hash_next = objfile->template_symbols;
22646 objfile->template_symbols = sym;
22647 list_to_add = NULL;
22648 }
22649
22650 if (list_to_add != NULL)
22651 add_symbol_to_list (sym, list_to_add);
22652
22653 /* For the benefit of old versions of GCC, check for anonymous
22654 namespaces based on the demangled name. */
22655 if (!cu->processing_has_namespace_info
22656 && cu->language == language_cplus)
22657 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22658 }
22659 return (sym);
22660 }
22661
22662 /* Given an attr with a DW_FORM_dataN value in host byte order,
22663 zero-extend it as appropriate for the symbol's type. The DWARF
22664 standard (v4) is not entirely clear about the meaning of using
22665 DW_FORM_dataN for a constant with a signed type, where the type is
22666 wider than the data. The conclusion of a discussion on the DWARF
22667 list was that this is unspecified. We choose to always zero-extend
22668 because that is the interpretation long in use by GCC. */
22669
22670 static gdb_byte *
22671 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22672 struct dwarf2_cu *cu, LONGEST *value, int bits)
22673 {
22674 struct objfile *objfile = cu->per_objfile->objfile;
22675 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22676 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22677 LONGEST l = attr->constant_value (0);
22678
22679 if (bits < sizeof (*value) * 8)
22680 {
22681 l &= ((LONGEST) 1 << bits) - 1;
22682 *value = l;
22683 }
22684 else if (bits == sizeof (*value) * 8)
22685 *value = l;
22686 else
22687 {
22688 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22689 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22690 return bytes;
22691 }
22692
22693 return NULL;
22694 }
22695
22696 /* Read a constant value from an attribute. Either set *VALUE, or if
22697 the value does not fit in *VALUE, set *BYTES - either already
22698 allocated on the objfile obstack, or newly allocated on OBSTACK,
22699 or, set *BATON, if we translated the constant to a location
22700 expression. */
22701
22702 static void
22703 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22704 const char *name, struct obstack *obstack,
22705 struct dwarf2_cu *cu,
22706 LONGEST *value, const gdb_byte **bytes,
22707 struct dwarf2_locexpr_baton **baton)
22708 {
22709 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22710 struct objfile *objfile = per_objfile->objfile;
22711 struct comp_unit_head *cu_header = &cu->header;
22712 struct dwarf_block *blk;
22713 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22714 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22715
22716 *value = 0;
22717 *bytes = NULL;
22718 *baton = NULL;
22719
22720 switch (attr->form)
22721 {
22722 case DW_FORM_addr:
22723 case DW_FORM_addrx:
22724 case DW_FORM_GNU_addr_index:
22725 {
22726 gdb_byte *data;
22727
22728 if (TYPE_LENGTH (type) != cu_header->addr_size)
22729 dwarf2_const_value_length_mismatch_complaint (name,
22730 cu_header->addr_size,
22731 TYPE_LENGTH (type));
22732 /* Symbols of this form are reasonably rare, so we just
22733 piggyback on the existing location code rather than writing
22734 a new implementation of symbol_computed_ops. */
22735 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22736 (*baton)->per_objfile = per_objfile;
22737 (*baton)->per_cu = cu->per_cu;
22738 gdb_assert ((*baton)->per_cu);
22739
22740 (*baton)->size = 2 + cu_header->addr_size;
22741 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22742 (*baton)->data = data;
22743
22744 data[0] = DW_OP_addr;
22745 store_unsigned_integer (&data[1], cu_header->addr_size,
22746 byte_order, attr->as_address ());
22747 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22748 }
22749 break;
22750 case DW_FORM_string:
22751 case DW_FORM_strp:
22752 case DW_FORM_strx:
22753 case DW_FORM_GNU_str_index:
22754 case DW_FORM_GNU_strp_alt:
22755 /* The string is already allocated on the objfile obstack, point
22756 directly to it. */
22757 *bytes = (const gdb_byte *) attr->as_string ();
22758 break;
22759 case DW_FORM_block1:
22760 case DW_FORM_block2:
22761 case DW_FORM_block4:
22762 case DW_FORM_block:
22763 case DW_FORM_exprloc:
22764 case DW_FORM_data16:
22765 blk = attr->as_block ();
22766 if (TYPE_LENGTH (type) != blk->size)
22767 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22768 TYPE_LENGTH (type));
22769 *bytes = blk->data;
22770 break;
22771
22772 /* The DW_AT_const_value attributes are supposed to carry the
22773 symbol's value "represented as it would be on the target
22774 architecture." By the time we get here, it's already been
22775 converted to host endianness, so we just need to sign- or
22776 zero-extend it as appropriate. */
22777 case DW_FORM_data1:
22778 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22779 break;
22780 case DW_FORM_data2:
22781 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22782 break;
22783 case DW_FORM_data4:
22784 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22785 break;
22786 case DW_FORM_data8:
22787 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22788 break;
22789
22790 case DW_FORM_sdata:
22791 case DW_FORM_implicit_const:
22792 *value = attr->as_signed ();
22793 break;
22794
22795 case DW_FORM_udata:
22796 *value = attr->as_unsigned ();
22797 break;
22798
22799 default:
22800 complaint (_("unsupported const value attribute form: '%s'"),
22801 dwarf_form_name (attr->form));
22802 *value = 0;
22803 break;
22804 }
22805 }
22806
22807
22808 /* Copy constant value from an attribute to a symbol. */
22809
22810 static void
22811 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22812 struct dwarf2_cu *cu)
22813 {
22814 struct objfile *objfile = cu->per_objfile->objfile;
22815 LONGEST value;
22816 const gdb_byte *bytes;
22817 struct dwarf2_locexpr_baton *baton;
22818
22819 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22820 sym->print_name (),
22821 &objfile->objfile_obstack, cu,
22822 &value, &bytes, &baton);
22823
22824 if (baton != NULL)
22825 {
22826 SYMBOL_LOCATION_BATON (sym) = baton;
22827 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22828 }
22829 else if (bytes != NULL)
22830 {
22831 SYMBOL_VALUE_BYTES (sym) = bytes;
22832 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22833 }
22834 else
22835 {
22836 SYMBOL_VALUE (sym) = value;
22837 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22838 }
22839 }
22840
22841 /* Return the type of the die in question using its DW_AT_type attribute. */
22842
22843 static struct type *
22844 die_type (struct die_info *die, struct dwarf2_cu *cu)
22845 {
22846 struct attribute *type_attr;
22847
22848 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22849 if (!type_attr)
22850 {
22851 struct objfile *objfile = cu->per_objfile->objfile;
22852 /* A missing DW_AT_type represents a void type. */
22853 return objfile_type (objfile)->builtin_void;
22854 }
22855
22856 return lookup_die_type (die, type_attr, cu);
22857 }
22858
22859 /* True iff CU's producer generates GNAT Ada auxiliary information
22860 that allows to find parallel types through that information instead
22861 of having to do expensive parallel lookups by type name. */
22862
22863 static int
22864 need_gnat_info (struct dwarf2_cu *cu)
22865 {
22866 /* Assume that the Ada compiler was GNAT, which always produces
22867 the auxiliary information. */
22868 return (cu->language == language_ada);
22869 }
22870
22871 /* Return the auxiliary type of the die in question using its
22872 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22873 attribute is not present. */
22874
22875 static struct type *
22876 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22877 {
22878 struct attribute *type_attr;
22879
22880 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22881 if (!type_attr)
22882 return NULL;
22883
22884 return lookup_die_type (die, type_attr, cu);
22885 }
22886
22887 /* If DIE has a descriptive_type attribute, then set the TYPE's
22888 descriptive type accordingly. */
22889
22890 static void
22891 set_descriptive_type (struct type *type, struct die_info *die,
22892 struct dwarf2_cu *cu)
22893 {
22894 struct type *descriptive_type = die_descriptive_type (die, cu);
22895
22896 if (descriptive_type)
22897 {
22898 ALLOCATE_GNAT_AUX_TYPE (type);
22899 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22900 }
22901 }
22902
22903 /* Return the containing type of the die in question using its
22904 DW_AT_containing_type attribute. */
22905
22906 static struct type *
22907 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22908 {
22909 struct attribute *type_attr;
22910 struct objfile *objfile = cu->per_objfile->objfile;
22911
22912 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22913 if (!type_attr)
22914 error (_("Dwarf Error: Problem turning containing type into gdb type "
22915 "[in module %s]"), objfile_name (objfile));
22916
22917 return lookup_die_type (die, type_attr, cu);
22918 }
22919
22920 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22921
22922 static struct type *
22923 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22924 {
22925 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22926 struct objfile *objfile = per_objfile->objfile;
22927 char *saved;
22928
22929 std::string message
22930 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22931 objfile_name (objfile),
22932 sect_offset_str (cu->header.sect_off),
22933 sect_offset_str (die->sect_off));
22934 saved = obstack_strdup (&objfile->objfile_obstack, message);
22935
22936 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22937 }
22938
22939 /* Look up the type of DIE in CU using its type attribute ATTR.
22940 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22941 DW_AT_containing_type.
22942 If there is no type substitute an error marker. */
22943
22944 static struct type *
22945 lookup_die_type (struct die_info *die, const struct attribute *attr,
22946 struct dwarf2_cu *cu)
22947 {
22948 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22949 struct objfile *objfile = per_objfile->objfile;
22950 struct type *this_type;
22951
22952 gdb_assert (attr->name == DW_AT_type
22953 || attr->name == DW_AT_GNAT_descriptive_type
22954 || attr->name == DW_AT_containing_type);
22955
22956 /* First see if we have it cached. */
22957
22958 if (attr->form == DW_FORM_GNU_ref_alt)
22959 {
22960 struct dwarf2_per_cu_data *per_cu;
22961 sect_offset sect_off = attr->get_ref_die_offset ();
22962
22963 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22964 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
22965 }
22966 else if (attr->form_is_ref ())
22967 {
22968 sect_offset sect_off = attr->get_ref_die_offset ();
22969
22970 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
22971 }
22972 else if (attr->form == DW_FORM_ref_sig8)
22973 {
22974 ULONGEST signature = attr->as_signature ();
22975
22976 return get_signatured_type (die, signature, cu);
22977 }
22978 else
22979 {
22980 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22981 " at %s [in module %s]"),
22982 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22983 objfile_name (objfile));
22984 return build_error_marker_type (cu, die);
22985 }
22986
22987 /* If not cached we need to read it in. */
22988
22989 if (this_type == NULL)
22990 {
22991 struct die_info *type_die = NULL;
22992 struct dwarf2_cu *type_cu = cu;
22993
22994 if (attr->form_is_ref ())
22995 type_die = follow_die_ref (die, attr, &type_cu);
22996 if (type_die == NULL)
22997 return build_error_marker_type (cu, die);
22998 /* If we find the type now, it's probably because the type came
22999 from an inter-CU reference and the type's CU got expanded before
23000 ours. */
23001 this_type = read_type_die (type_die, type_cu);
23002 }
23003
23004 /* If we still don't have a type use an error marker. */
23005
23006 if (this_type == NULL)
23007 return build_error_marker_type (cu, die);
23008
23009 return this_type;
23010 }
23011
23012 /* Return the type in DIE, CU.
23013 Returns NULL for invalid types.
23014
23015 This first does a lookup in die_type_hash,
23016 and only reads the die in if necessary.
23017
23018 NOTE: This can be called when reading in partial or full symbols. */
23019
23020 static struct type *
23021 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
23022 {
23023 struct type *this_type;
23024
23025 this_type = get_die_type (die, cu);
23026 if (this_type)
23027 return this_type;
23028
23029 return read_type_die_1 (die, cu);
23030 }
23031
23032 /* Read the type in DIE, CU.
23033 Returns NULL for invalid types. */
23034
23035 static struct type *
23036 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
23037 {
23038 struct type *this_type = NULL;
23039
23040 switch (die->tag)
23041 {
23042 case DW_TAG_class_type:
23043 case DW_TAG_interface_type:
23044 case DW_TAG_structure_type:
23045 case DW_TAG_union_type:
23046 this_type = read_structure_type (die, cu);
23047 break;
23048 case DW_TAG_enumeration_type:
23049 this_type = read_enumeration_type (die, cu);
23050 break;
23051 case DW_TAG_subprogram:
23052 case DW_TAG_subroutine_type:
23053 case DW_TAG_inlined_subroutine:
23054 this_type = read_subroutine_type (die, cu);
23055 break;
23056 case DW_TAG_array_type:
23057 this_type = read_array_type (die, cu);
23058 break;
23059 case DW_TAG_set_type:
23060 this_type = read_set_type (die, cu);
23061 break;
23062 case DW_TAG_pointer_type:
23063 this_type = read_tag_pointer_type (die, cu);
23064 break;
23065 case DW_TAG_ptr_to_member_type:
23066 this_type = read_tag_ptr_to_member_type (die, cu);
23067 break;
23068 case DW_TAG_reference_type:
23069 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
23070 break;
23071 case DW_TAG_rvalue_reference_type:
23072 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
23073 break;
23074 case DW_TAG_const_type:
23075 this_type = read_tag_const_type (die, cu);
23076 break;
23077 case DW_TAG_volatile_type:
23078 this_type = read_tag_volatile_type (die, cu);
23079 break;
23080 case DW_TAG_restrict_type:
23081 this_type = read_tag_restrict_type (die, cu);
23082 break;
23083 case DW_TAG_string_type:
23084 this_type = read_tag_string_type (die, cu);
23085 break;
23086 case DW_TAG_typedef:
23087 this_type = read_typedef (die, cu);
23088 break;
23089 case DW_TAG_subrange_type:
23090 this_type = read_subrange_type (die, cu);
23091 break;
23092 case DW_TAG_base_type:
23093 this_type = read_base_type (die, cu);
23094 break;
23095 case DW_TAG_unspecified_type:
23096 this_type = read_unspecified_type (die, cu);
23097 break;
23098 case DW_TAG_namespace:
23099 this_type = read_namespace_type (die, cu);
23100 break;
23101 case DW_TAG_module:
23102 this_type = read_module_type (die, cu);
23103 break;
23104 case DW_TAG_atomic_type:
23105 this_type = read_tag_atomic_type (die, cu);
23106 break;
23107 default:
23108 complaint (_("unexpected tag in read_type_die: '%s'"),
23109 dwarf_tag_name (die->tag));
23110 break;
23111 }
23112
23113 return this_type;
23114 }
23115
23116 /* See if we can figure out if the class lives in a namespace. We do
23117 this by looking for a member function; its demangled name will
23118 contain namespace info, if there is any.
23119 Return the computed name or NULL.
23120 Space for the result is allocated on the objfile's obstack.
23121 This is the full-die version of guess_partial_die_structure_name.
23122 In this case we know DIE has no useful parent. */
23123
23124 static const char *
23125 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
23126 {
23127 struct die_info *spec_die;
23128 struct dwarf2_cu *spec_cu;
23129 struct die_info *child;
23130 struct objfile *objfile = cu->per_objfile->objfile;
23131
23132 spec_cu = cu;
23133 spec_die = die_specification (die, &spec_cu);
23134 if (spec_die != NULL)
23135 {
23136 die = spec_die;
23137 cu = spec_cu;
23138 }
23139
23140 for (child = die->child;
23141 child != NULL;
23142 child = child->sibling)
23143 {
23144 if (child->tag == DW_TAG_subprogram)
23145 {
23146 const char *linkage_name = dw2_linkage_name (child, cu);
23147
23148 if (linkage_name != NULL)
23149 {
23150 gdb::unique_xmalloc_ptr<char> actual_name
23151 (cu->language_defn->class_name_from_physname (linkage_name));
23152 const char *name = NULL;
23153
23154 if (actual_name != NULL)
23155 {
23156 const char *die_name = dwarf2_name (die, cu);
23157
23158 if (die_name != NULL
23159 && strcmp (die_name, actual_name.get ()) != 0)
23160 {
23161 /* Strip off the class name from the full name.
23162 We want the prefix. */
23163 int die_name_len = strlen (die_name);
23164 int actual_name_len = strlen (actual_name.get ());
23165 const char *ptr = actual_name.get ();
23166
23167 /* Test for '::' as a sanity check. */
23168 if (actual_name_len > die_name_len + 2
23169 && ptr[actual_name_len - die_name_len - 1] == ':')
23170 name = obstack_strndup (
23171 &objfile->per_bfd->storage_obstack,
23172 ptr, actual_name_len - die_name_len - 2);
23173 }
23174 }
23175 return name;
23176 }
23177 }
23178 }
23179
23180 return NULL;
23181 }
23182
23183 /* GCC might emit a nameless typedef that has a linkage name. Determine the
23184 prefix part in such case. See
23185 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23186
23187 static const char *
23188 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
23189 {
23190 struct attribute *attr;
23191 const char *base;
23192
23193 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
23194 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
23195 return NULL;
23196
23197 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
23198 return NULL;
23199
23200 attr = dw2_linkage_name_attr (die, cu);
23201 const char *attr_name = attr->as_string ();
23202 if (attr == NULL || attr_name == NULL)
23203 return NULL;
23204
23205 /* dwarf2_name had to be already called. */
23206 gdb_assert (attr->canonical_string_p ());
23207
23208 /* Strip the base name, keep any leading namespaces/classes. */
23209 base = strrchr (attr_name, ':');
23210 if (base == NULL || base == attr_name || base[-1] != ':')
23211 return "";
23212
23213 struct objfile *objfile = cu->per_objfile->objfile;
23214 return obstack_strndup (&objfile->per_bfd->storage_obstack,
23215 attr_name,
23216 &base[-1] - attr_name);
23217 }
23218
23219 /* Return the name of the namespace/class that DIE is defined within,
23220 or "" if we can't tell. The caller should not xfree the result.
23221
23222 For example, if we're within the method foo() in the following
23223 code:
23224
23225 namespace N {
23226 class C {
23227 void foo () {
23228 }
23229 };
23230 }
23231
23232 then determine_prefix on foo's die will return "N::C". */
23233
23234 static const char *
23235 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
23236 {
23237 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23238 struct die_info *parent, *spec_die;
23239 struct dwarf2_cu *spec_cu;
23240 struct type *parent_type;
23241 const char *retval;
23242
23243 if (cu->language != language_cplus
23244 && cu->language != language_fortran && cu->language != language_d
23245 && cu->language != language_rust)
23246 return "";
23247
23248 retval = anonymous_struct_prefix (die, cu);
23249 if (retval)
23250 return retval;
23251
23252 /* We have to be careful in the presence of DW_AT_specification.
23253 For example, with GCC 3.4, given the code
23254
23255 namespace N {
23256 void foo() {
23257 // Definition of N::foo.
23258 }
23259 }
23260
23261 then we'll have a tree of DIEs like this:
23262
23263 1: DW_TAG_compile_unit
23264 2: DW_TAG_namespace // N
23265 3: DW_TAG_subprogram // declaration of N::foo
23266 4: DW_TAG_subprogram // definition of N::foo
23267 DW_AT_specification // refers to die #3
23268
23269 Thus, when processing die #4, we have to pretend that we're in
23270 the context of its DW_AT_specification, namely the contex of die
23271 #3. */
23272 spec_cu = cu;
23273 spec_die = die_specification (die, &spec_cu);
23274 if (spec_die == NULL)
23275 parent = die->parent;
23276 else
23277 {
23278 parent = spec_die->parent;
23279 cu = spec_cu;
23280 }
23281
23282 if (parent == NULL)
23283 return "";
23284 else if (parent->building_fullname)
23285 {
23286 const char *name;
23287 const char *parent_name;
23288
23289 /* It has been seen on RealView 2.2 built binaries,
23290 DW_TAG_template_type_param types actually _defined_ as
23291 children of the parent class:
23292
23293 enum E {};
23294 template class <class Enum> Class{};
23295 Class<enum E> class_e;
23296
23297 1: DW_TAG_class_type (Class)
23298 2: DW_TAG_enumeration_type (E)
23299 3: DW_TAG_enumerator (enum1:0)
23300 3: DW_TAG_enumerator (enum2:1)
23301 ...
23302 2: DW_TAG_template_type_param
23303 DW_AT_type DW_FORM_ref_udata (E)
23304
23305 Besides being broken debug info, it can put GDB into an
23306 infinite loop. Consider:
23307
23308 When we're building the full name for Class<E>, we'll start
23309 at Class, and go look over its template type parameters,
23310 finding E. We'll then try to build the full name of E, and
23311 reach here. We're now trying to build the full name of E,
23312 and look over the parent DIE for containing scope. In the
23313 broken case, if we followed the parent DIE of E, we'd again
23314 find Class, and once again go look at its template type
23315 arguments, etc., etc. Simply don't consider such parent die
23316 as source-level parent of this die (it can't be, the language
23317 doesn't allow it), and break the loop here. */
23318 name = dwarf2_name (die, cu);
23319 parent_name = dwarf2_name (parent, cu);
23320 complaint (_("template param type '%s' defined within parent '%s'"),
23321 name ? name : "<unknown>",
23322 parent_name ? parent_name : "<unknown>");
23323 return "";
23324 }
23325 else
23326 switch (parent->tag)
23327 {
23328 case DW_TAG_namespace:
23329 parent_type = read_type_die (parent, cu);
23330 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
23331 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
23332 Work around this problem here. */
23333 if (cu->language == language_cplus
23334 && strcmp (parent_type->name (), "::") == 0)
23335 return "";
23336 /* We give a name to even anonymous namespaces. */
23337 return parent_type->name ();
23338 case DW_TAG_class_type:
23339 case DW_TAG_interface_type:
23340 case DW_TAG_structure_type:
23341 case DW_TAG_union_type:
23342 case DW_TAG_module:
23343 parent_type = read_type_die (parent, cu);
23344 if (parent_type->name () != NULL)
23345 return parent_type->name ();
23346 else
23347 /* An anonymous structure is only allowed non-static data
23348 members; no typedefs, no member functions, et cetera.
23349 So it does not need a prefix. */
23350 return "";
23351 case DW_TAG_compile_unit:
23352 case DW_TAG_partial_unit:
23353 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
23354 if (cu->language == language_cplus
23355 && !per_objfile->per_bfd->types.empty ()
23356 && die->child != NULL
23357 && (die->tag == DW_TAG_class_type
23358 || die->tag == DW_TAG_structure_type
23359 || die->tag == DW_TAG_union_type))
23360 {
23361 const char *name = guess_full_die_structure_name (die, cu);
23362 if (name != NULL)
23363 return name;
23364 }
23365 return "";
23366 case DW_TAG_subprogram:
23367 /* Nested subroutines in Fortran get a prefix with the name
23368 of the parent's subroutine. */
23369 if (cu->language == language_fortran)
23370 {
23371 if ((die->tag == DW_TAG_subprogram)
23372 && (dwarf2_name (parent, cu) != NULL))
23373 return dwarf2_name (parent, cu);
23374 }
23375 return determine_prefix (parent, cu);
23376 case DW_TAG_enumeration_type:
23377 parent_type = read_type_die (parent, cu);
23378 if (TYPE_DECLARED_CLASS (parent_type))
23379 {
23380 if (parent_type->name () != NULL)
23381 return parent_type->name ();
23382 return "";
23383 }
23384 /* Fall through. */
23385 default:
23386 return determine_prefix (parent, cu);
23387 }
23388 }
23389
23390 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
23391 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
23392 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
23393 an obconcat, otherwise allocate storage for the result. The CU argument is
23394 used to determine the language and hence, the appropriate separator. */
23395
23396 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
23397
23398 static char *
23399 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
23400 int physname, struct dwarf2_cu *cu)
23401 {
23402 const char *lead = "";
23403 const char *sep;
23404
23405 if (suffix == NULL || suffix[0] == '\0'
23406 || prefix == NULL || prefix[0] == '\0')
23407 sep = "";
23408 else if (cu->language == language_d)
23409 {
23410 /* For D, the 'main' function could be defined in any module, but it
23411 should never be prefixed. */
23412 if (strcmp (suffix, "D main") == 0)
23413 {
23414 prefix = "";
23415 sep = "";
23416 }
23417 else
23418 sep = ".";
23419 }
23420 else if (cu->language == language_fortran && physname)
23421 {
23422 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
23423 DW_AT_MIPS_linkage_name is preferred and used instead. */
23424
23425 lead = "__";
23426 sep = "_MOD_";
23427 }
23428 else
23429 sep = "::";
23430
23431 if (prefix == NULL)
23432 prefix = "";
23433 if (suffix == NULL)
23434 suffix = "";
23435
23436 if (obs == NULL)
23437 {
23438 char *retval
23439 = ((char *)
23440 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
23441
23442 strcpy (retval, lead);
23443 strcat (retval, prefix);
23444 strcat (retval, sep);
23445 strcat (retval, suffix);
23446 return retval;
23447 }
23448 else
23449 {
23450 /* We have an obstack. */
23451 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
23452 }
23453 }
23454
23455 /* Get name of a die, return NULL if not found. */
23456
23457 static const char *
23458 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
23459 struct objfile *objfile)
23460 {
23461 if (name && cu->language == language_cplus)
23462 {
23463 gdb::unique_xmalloc_ptr<char> canon_name
23464 = cp_canonicalize_string (name);
23465
23466 if (canon_name != nullptr)
23467 name = objfile->intern (canon_name.get ());
23468 }
23469
23470 return name;
23471 }
23472
23473 /* Get name of a die, return NULL if not found.
23474 Anonymous namespaces are converted to their magic string. */
23475
23476 static const char *
23477 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
23478 {
23479 struct attribute *attr;
23480 struct objfile *objfile = cu->per_objfile->objfile;
23481
23482 attr = dwarf2_attr (die, DW_AT_name, cu);
23483 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
23484 if (attr_name == nullptr
23485 && die->tag != DW_TAG_namespace
23486 && die->tag != DW_TAG_class_type
23487 && die->tag != DW_TAG_interface_type
23488 && die->tag != DW_TAG_structure_type
23489 && die->tag != DW_TAG_union_type)
23490 return NULL;
23491
23492 switch (die->tag)
23493 {
23494 case DW_TAG_compile_unit:
23495 case DW_TAG_partial_unit:
23496 /* Compilation units have a DW_AT_name that is a filename, not
23497 a source language identifier. */
23498 case DW_TAG_enumeration_type:
23499 case DW_TAG_enumerator:
23500 /* These tags always have simple identifiers already; no need
23501 to canonicalize them. */
23502 return attr_name;
23503
23504 case DW_TAG_namespace:
23505 if (attr_name != nullptr)
23506 return attr_name;
23507 return CP_ANONYMOUS_NAMESPACE_STR;
23508
23509 case DW_TAG_class_type:
23510 case DW_TAG_interface_type:
23511 case DW_TAG_structure_type:
23512 case DW_TAG_union_type:
23513 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23514 structures or unions. These were of the form "._%d" in GCC 4.1,
23515 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23516 and GCC 4.4. We work around this problem by ignoring these. */
23517 if (attr_name != nullptr
23518 && (startswith (attr_name, "._")
23519 || startswith (attr_name, "<anonymous")))
23520 return NULL;
23521
23522 /* GCC might emit a nameless typedef that has a linkage name. See
23523 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23524 if (!attr || attr_name == NULL)
23525 {
23526 attr = dw2_linkage_name_attr (die, cu);
23527 attr_name = attr == nullptr ? nullptr : attr->as_string ();
23528 if (attr == NULL || attr_name == NULL)
23529 return NULL;
23530
23531 /* Avoid demangling attr_name the second time on a second
23532 call for the same DIE. */
23533 if (!attr->canonical_string_p ())
23534 {
23535 gdb::unique_xmalloc_ptr<char> demangled
23536 (gdb_demangle (attr_name, DMGL_TYPES));
23537 if (demangled == nullptr)
23538 return nullptr;
23539
23540 attr->set_string_canonical (objfile->intern (demangled.get ()));
23541 attr_name = attr->as_string ();
23542 }
23543
23544 /* Strip any leading namespaces/classes, keep only the
23545 base name. DW_AT_name for named DIEs does not
23546 contain the prefixes. */
23547 const char *base = strrchr (attr_name, ':');
23548 if (base && base > attr_name && base[-1] == ':')
23549 return &base[1];
23550 else
23551 return attr_name;
23552 }
23553 break;
23554
23555 default:
23556 break;
23557 }
23558
23559 if (!attr->canonical_string_p ())
23560 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23561 objfile));
23562 return attr->as_string ();
23563 }
23564
23565 /* Return the die that this die in an extension of, or NULL if there
23566 is none. *EXT_CU is the CU containing DIE on input, and the CU
23567 containing the return value on output. */
23568
23569 static struct die_info *
23570 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23571 {
23572 struct attribute *attr;
23573
23574 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23575 if (attr == NULL)
23576 return NULL;
23577
23578 return follow_die_ref (die, attr, ext_cu);
23579 }
23580
23581 static void
23582 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23583 {
23584 unsigned int i;
23585
23586 print_spaces (indent, f);
23587 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23588 dwarf_tag_name (die->tag), die->abbrev,
23589 sect_offset_str (die->sect_off));
23590
23591 if (die->parent != NULL)
23592 {
23593 print_spaces (indent, f);
23594 fprintf_unfiltered (f, " parent at offset: %s\n",
23595 sect_offset_str (die->parent->sect_off));
23596 }
23597
23598 print_spaces (indent, f);
23599 fprintf_unfiltered (f, " has children: %s\n",
23600 dwarf_bool_name (die->child != NULL));
23601
23602 print_spaces (indent, f);
23603 fprintf_unfiltered (f, " attributes:\n");
23604
23605 for (i = 0; i < die->num_attrs; ++i)
23606 {
23607 print_spaces (indent, f);
23608 fprintf_unfiltered (f, " %s (%s) ",
23609 dwarf_attr_name (die->attrs[i].name),
23610 dwarf_form_name (die->attrs[i].form));
23611
23612 switch (die->attrs[i].form)
23613 {
23614 case DW_FORM_addr:
23615 case DW_FORM_addrx:
23616 case DW_FORM_GNU_addr_index:
23617 fprintf_unfiltered (f, "address: ");
23618 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
23619 break;
23620 case DW_FORM_block2:
23621 case DW_FORM_block4:
23622 case DW_FORM_block:
23623 case DW_FORM_block1:
23624 fprintf_unfiltered (f, "block: size %s",
23625 pulongest (die->attrs[i].as_block ()->size));
23626 break;
23627 case DW_FORM_exprloc:
23628 fprintf_unfiltered (f, "expression: size %s",
23629 pulongest (die->attrs[i].as_block ()->size));
23630 break;
23631 case DW_FORM_data16:
23632 fprintf_unfiltered (f, "constant of 16 bytes");
23633 break;
23634 case DW_FORM_ref_addr:
23635 fprintf_unfiltered (f, "ref address: ");
23636 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23637 break;
23638 case DW_FORM_GNU_ref_alt:
23639 fprintf_unfiltered (f, "alt ref address: ");
23640 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23641 break;
23642 case DW_FORM_ref1:
23643 case DW_FORM_ref2:
23644 case DW_FORM_ref4:
23645 case DW_FORM_ref8:
23646 case DW_FORM_ref_udata:
23647 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23648 (long) (die->attrs[i].as_unsigned ()));
23649 break;
23650 case DW_FORM_data1:
23651 case DW_FORM_data2:
23652 case DW_FORM_data4:
23653 case DW_FORM_data8:
23654 case DW_FORM_udata:
23655 fprintf_unfiltered (f, "constant: %s",
23656 pulongest (die->attrs[i].as_unsigned ()));
23657 break;
23658 case DW_FORM_sec_offset:
23659 fprintf_unfiltered (f, "section offset: %s",
23660 pulongest (die->attrs[i].as_unsigned ()));
23661 break;
23662 case DW_FORM_ref_sig8:
23663 fprintf_unfiltered (f, "signature: %s",
23664 hex_string (die->attrs[i].as_signature ()));
23665 break;
23666 case DW_FORM_string:
23667 case DW_FORM_strp:
23668 case DW_FORM_line_strp:
23669 case DW_FORM_strx:
23670 case DW_FORM_GNU_str_index:
23671 case DW_FORM_GNU_strp_alt:
23672 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23673 die->attrs[i].as_string ()
23674 ? die->attrs[i].as_string () : "",
23675 die->attrs[i].canonical_string_p () ? "is" : "not");
23676 break;
23677 case DW_FORM_flag:
23678 if (die->attrs[i].as_boolean ())
23679 fprintf_unfiltered (f, "flag: TRUE");
23680 else
23681 fprintf_unfiltered (f, "flag: FALSE");
23682 break;
23683 case DW_FORM_flag_present:
23684 fprintf_unfiltered (f, "flag: TRUE");
23685 break;
23686 case DW_FORM_indirect:
23687 /* The reader will have reduced the indirect form to
23688 the "base form" so this form should not occur. */
23689 fprintf_unfiltered (f,
23690 "unexpected attribute form: DW_FORM_indirect");
23691 break;
23692 case DW_FORM_sdata:
23693 case DW_FORM_implicit_const:
23694 fprintf_unfiltered (f, "constant: %s",
23695 plongest (die->attrs[i].as_signed ()));
23696 break;
23697 default:
23698 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23699 die->attrs[i].form);
23700 break;
23701 }
23702 fprintf_unfiltered (f, "\n");
23703 }
23704 }
23705
23706 static void
23707 dump_die_for_error (struct die_info *die)
23708 {
23709 dump_die_shallow (gdb_stderr, 0, die);
23710 }
23711
23712 static void
23713 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23714 {
23715 int indent = level * 4;
23716
23717 gdb_assert (die != NULL);
23718
23719 if (level >= max_level)
23720 return;
23721
23722 dump_die_shallow (f, indent, die);
23723
23724 if (die->child != NULL)
23725 {
23726 print_spaces (indent, f);
23727 fprintf_unfiltered (f, " Children:");
23728 if (level + 1 < max_level)
23729 {
23730 fprintf_unfiltered (f, "\n");
23731 dump_die_1 (f, level + 1, max_level, die->child);
23732 }
23733 else
23734 {
23735 fprintf_unfiltered (f,
23736 " [not printed, max nesting level reached]\n");
23737 }
23738 }
23739
23740 if (die->sibling != NULL && level > 0)
23741 {
23742 dump_die_1 (f, level, max_level, die->sibling);
23743 }
23744 }
23745
23746 /* This is called from the pdie macro in gdbinit.in.
23747 It's not static so gcc will keep a copy callable from gdb. */
23748
23749 void
23750 dump_die (struct die_info *die, int max_level)
23751 {
23752 dump_die_1 (gdb_stdlog, 0, max_level, die);
23753 }
23754
23755 static void
23756 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23757 {
23758 void **slot;
23759
23760 slot = htab_find_slot_with_hash (cu->die_hash, die,
23761 to_underlying (die->sect_off),
23762 INSERT);
23763
23764 *slot = die;
23765 }
23766
23767 /* Follow reference or signature attribute ATTR of SRC_DIE.
23768 On entry *REF_CU is the CU of SRC_DIE.
23769 On exit *REF_CU is the CU of the result. */
23770
23771 static struct die_info *
23772 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23773 struct dwarf2_cu **ref_cu)
23774 {
23775 struct die_info *die;
23776
23777 if (attr->form_is_ref ())
23778 die = follow_die_ref (src_die, attr, ref_cu);
23779 else if (attr->form == DW_FORM_ref_sig8)
23780 die = follow_die_sig (src_die, attr, ref_cu);
23781 else
23782 {
23783 dump_die_for_error (src_die);
23784 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23785 objfile_name ((*ref_cu)->per_objfile->objfile));
23786 }
23787
23788 return die;
23789 }
23790
23791 /* Follow reference OFFSET.
23792 On entry *REF_CU is the CU of the source die referencing OFFSET.
23793 On exit *REF_CU is the CU of the result.
23794 Returns NULL if OFFSET is invalid. */
23795
23796 static struct die_info *
23797 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23798 struct dwarf2_cu **ref_cu)
23799 {
23800 struct die_info temp_die;
23801 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23802 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23803
23804 gdb_assert (cu->per_cu != NULL);
23805
23806 target_cu = cu;
23807
23808 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23809 "source CU contains target offset: %d",
23810 sect_offset_str (cu->per_cu->sect_off),
23811 sect_offset_str (sect_off),
23812 cu->header.offset_in_cu_p (sect_off));
23813
23814 if (cu->per_cu->is_debug_types)
23815 {
23816 /* .debug_types CUs cannot reference anything outside their CU.
23817 If they need to, they have to reference a signatured type via
23818 DW_FORM_ref_sig8. */
23819 if (!cu->header.offset_in_cu_p (sect_off))
23820 return NULL;
23821 }
23822 else if (offset_in_dwz != cu->per_cu->is_dwz
23823 || !cu->header.offset_in_cu_p (sect_off))
23824 {
23825 struct dwarf2_per_cu_data *per_cu;
23826
23827 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23828 per_objfile);
23829
23830 dwarf_read_debug_printf_v ("target CU offset: %s, "
23831 "target CU DIEs loaded: %d",
23832 sect_offset_str (per_cu->sect_off),
23833 per_objfile->get_cu (per_cu) != nullptr);
23834
23835 /* If necessary, add it to the queue and load its DIEs.
23836
23837 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23838 it doesn't mean they are currently loaded. Since we require them
23839 to be loaded, we must check for ourselves. */
23840 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language)
23841 || per_objfile->get_cu (per_cu) == nullptr)
23842 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23843 false, cu->language);
23844
23845 target_cu = per_objfile->get_cu (per_cu);
23846 gdb_assert (target_cu != nullptr);
23847 }
23848 else if (cu->dies == NULL)
23849 {
23850 /* We're loading full DIEs during partial symbol reading. */
23851 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
23852 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23853 language_minimal);
23854 }
23855
23856 *ref_cu = target_cu;
23857 temp_die.sect_off = sect_off;
23858
23859 if (target_cu != cu)
23860 target_cu->ancestor = cu;
23861
23862 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23863 &temp_die,
23864 to_underlying (sect_off));
23865 }
23866
23867 /* Follow reference attribute ATTR of SRC_DIE.
23868 On entry *REF_CU is the CU of SRC_DIE.
23869 On exit *REF_CU is the CU of the result. */
23870
23871 static struct die_info *
23872 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23873 struct dwarf2_cu **ref_cu)
23874 {
23875 sect_offset sect_off = attr->get_ref_die_offset ();
23876 struct dwarf2_cu *cu = *ref_cu;
23877 struct die_info *die;
23878
23879 die = follow_die_offset (sect_off,
23880 (attr->form == DW_FORM_GNU_ref_alt
23881 || cu->per_cu->is_dwz),
23882 ref_cu);
23883 if (!die)
23884 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23885 "at %s [in module %s]"),
23886 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23887 objfile_name (cu->per_objfile->objfile));
23888
23889 return die;
23890 }
23891
23892 /* See read.h. */
23893
23894 struct dwarf2_locexpr_baton
23895 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23896 dwarf2_per_cu_data *per_cu,
23897 dwarf2_per_objfile *per_objfile,
23898 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23899 bool resolve_abstract_p)
23900 {
23901 struct die_info *die;
23902 struct attribute *attr;
23903 struct dwarf2_locexpr_baton retval;
23904 struct objfile *objfile = per_objfile->objfile;
23905
23906 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23907 if (cu == nullptr)
23908 cu = load_cu (per_cu, per_objfile, false);
23909
23910 if (cu == nullptr)
23911 {
23912 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23913 Instead just throw an error, not much else we can do. */
23914 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23915 sect_offset_str (sect_off), objfile_name (objfile));
23916 }
23917
23918 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23919 if (!die)
23920 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23921 sect_offset_str (sect_off), objfile_name (objfile));
23922
23923 attr = dwarf2_attr (die, DW_AT_location, cu);
23924 if (!attr && resolve_abstract_p
23925 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23926 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23927 {
23928 CORE_ADDR pc = get_frame_pc ();
23929 CORE_ADDR baseaddr = objfile->text_section_offset ();
23930 struct gdbarch *gdbarch = objfile->arch ();
23931
23932 for (const auto &cand_off
23933 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23934 {
23935 struct dwarf2_cu *cand_cu = cu;
23936 struct die_info *cand
23937 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23938 if (!cand
23939 || !cand->parent
23940 || cand->parent->tag != DW_TAG_subprogram)
23941 continue;
23942
23943 CORE_ADDR pc_low, pc_high;
23944 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23945 if (pc_low == ((CORE_ADDR) -1))
23946 continue;
23947 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23948 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23949 if (!(pc_low <= pc && pc < pc_high))
23950 continue;
23951
23952 die = cand;
23953 attr = dwarf2_attr (die, DW_AT_location, cu);
23954 break;
23955 }
23956 }
23957
23958 if (!attr)
23959 {
23960 /* DWARF: "If there is no such attribute, then there is no effect.".
23961 DATA is ignored if SIZE is 0. */
23962
23963 retval.data = NULL;
23964 retval.size = 0;
23965 }
23966 else if (attr->form_is_section_offset ())
23967 {
23968 struct dwarf2_loclist_baton loclist_baton;
23969 CORE_ADDR pc = get_frame_pc ();
23970 size_t size;
23971
23972 fill_in_loclist_baton (cu, &loclist_baton, attr);
23973
23974 retval.data = dwarf2_find_location_expression (&loclist_baton,
23975 &size, pc);
23976 retval.size = size;
23977 }
23978 else
23979 {
23980 if (!attr->form_is_block ())
23981 error (_("Dwarf Error: DIE at %s referenced in module %s "
23982 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23983 sect_offset_str (sect_off), objfile_name (objfile));
23984
23985 struct dwarf_block *block = attr->as_block ();
23986 retval.data = block->data;
23987 retval.size = block->size;
23988 }
23989 retval.per_objfile = per_objfile;
23990 retval.per_cu = cu->per_cu;
23991
23992 per_objfile->age_comp_units ();
23993
23994 return retval;
23995 }
23996
23997 /* See read.h. */
23998
23999 struct dwarf2_locexpr_baton
24000 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
24001 dwarf2_per_cu_data *per_cu,
24002 dwarf2_per_objfile *per_objfile,
24003 gdb::function_view<CORE_ADDR ()> get_frame_pc)
24004 {
24005 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
24006
24007 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
24008 get_frame_pc);
24009 }
24010
24011 /* Write a constant of a given type as target-ordered bytes into
24012 OBSTACK. */
24013
24014 static const gdb_byte *
24015 write_constant_as_bytes (struct obstack *obstack,
24016 enum bfd_endian byte_order,
24017 struct type *type,
24018 ULONGEST value,
24019 LONGEST *len)
24020 {
24021 gdb_byte *result;
24022
24023 *len = TYPE_LENGTH (type);
24024 result = (gdb_byte *) obstack_alloc (obstack, *len);
24025 store_unsigned_integer (result, *len, byte_order, value);
24026
24027 return result;
24028 }
24029
24030 /* See read.h. */
24031
24032 const gdb_byte *
24033 dwarf2_fetch_constant_bytes (sect_offset sect_off,
24034 dwarf2_per_cu_data *per_cu,
24035 dwarf2_per_objfile *per_objfile,
24036 obstack *obstack,
24037 LONGEST *len)
24038 {
24039 struct die_info *die;
24040 struct attribute *attr;
24041 const gdb_byte *result = NULL;
24042 struct type *type;
24043 LONGEST value;
24044 enum bfd_endian byte_order;
24045 struct objfile *objfile = per_objfile->objfile;
24046
24047 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
24048 if (cu == nullptr)
24049 cu = load_cu (per_cu, per_objfile, false);
24050
24051 if (cu == nullptr)
24052 {
24053 /* We shouldn't get here for a dummy CU, but don't crash on the user.
24054 Instead just throw an error, not much else we can do. */
24055 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
24056 sect_offset_str (sect_off), objfile_name (objfile));
24057 }
24058
24059 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
24060 if (!die)
24061 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
24062 sect_offset_str (sect_off), objfile_name (objfile));
24063
24064 attr = dwarf2_attr (die, DW_AT_const_value, cu);
24065 if (attr == NULL)
24066 return NULL;
24067
24068 byte_order = (bfd_big_endian (objfile->obfd)
24069 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
24070
24071 switch (attr->form)
24072 {
24073 case DW_FORM_addr:
24074 case DW_FORM_addrx:
24075 case DW_FORM_GNU_addr_index:
24076 {
24077 gdb_byte *tem;
24078
24079 *len = cu->header.addr_size;
24080 tem = (gdb_byte *) obstack_alloc (obstack, *len);
24081 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
24082 result = tem;
24083 }
24084 break;
24085 case DW_FORM_string:
24086 case DW_FORM_strp:
24087 case DW_FORM_strx:
24088 case DW_FORM_GNU_str_index:
24089 case DW_FORM_GNU_strp_alt:
24090 /* The string is already allocated on the objfile obstack, point
24091 directly to it. */
24092 {
24093 const char *attr_name = attr->as_string ();
24094 result = (const gdb_byte *) attr_name;
24095 *len = strlen (attr_name);
24096 }
24097 break;
24098 case DW_FORM_block1:
24099 case DW_FORM_block2:
24100 case DW_FORM_block4:
24101 case DW_FORM_block:
24102 case DW_FORM_exprloc:
24103 case DW_FORM_data16:
24104 {
24105 struct dwarf_block *block = attr->as_block ();
24106 result = block->data;
24107 *len = block->size;
24108 }
24109 break;
24110
24111 /* The DW_AT_const_value attributes are supposed to carry the
24112 symbol's value "represented as it would be on the target
24113 architecture." By the time we get here, it's already been
24114 converted to host endianness, so we just need to sign- or
24115 zero-extend it as appropriate. */
24116 case DW_FORM_data1:
24117 type = die_type (die, cu);
24118 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
24119 if (result == NULL)
24120 result = write_constant_as_bytes (obstack, byte_order,
24121 type, value, len);
24122 break;
24123 case DW_FORM_data2:
24124 type = die_type (die, cu);
24125 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
24126 if (result == NULL)
24127 result = write_constant_as_bytes (obstack, byte_order,
24128 type, value, len);
24129 break;
24130 case DW_FORM_data4:
24131 type = die_type (die, cu);
24132 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
24133 if (result == NULL)
24134 result = write_constant_as_bytes (obstack, byte_order,
24135 type, value, len);
24136 break;
24137 case DW_FORM_data8:
24138 type = die_type (die, cu);
24139 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
24140 if (result == NULL)
24141 result = write_constant_as_bytes (obstack, byte_order,
24142 type, value, len);
24143 break;
24144
24145 case DW_FORM_sdata:
24146 case DW_FORM_implicit_const:
24147 type = die_type (die, cu);
24148 result = write_constant_as_bytes (obstack, byte_order,
24149 type, attr->as_signed (), len);
24150 break;
24151
24152 case DW_FORM_udata:
24153 type = die_type (die, cu);
24154 result = write_constant_as_bytes (obstack, byte_order,
24155 type, attr->as_unsigned (), len);
24156 break;
24157
24158 default:
24159 complaint (_("unsupported const value attribute form: '%s'"),
24160 dwarf_form_name (attr->form));
24161 break;
24162 }
24163
24164 return result;
24165 }
24166
24167 /* See read.h. */
24168
24169 struct type *
24170 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
24171 dwarf2_per_cu_data *per_cu,
24172 dwarf2_per_objfile *per_objfile)
24173 {
24174 struct die_info *die;
24175
24176 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
24177 if (cu == nullptr)
24178 cu = load_cu (per_cu, per_objfile, false);
24179
24180 if (cu == nullptr)
24181 return nullptr;
24182
24183 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
24184 if (!die)
24185 return NULL;
24186
24187 return die_type (die, cu);
24188 }
24189
24190 /* See read.h. */
24191
24192 struct type *
24193 dwarf2_get_die_type (cu_offset die_offset,
24194 dwarf2_per_cu_data *per_cu,
24195 dwarf2_per_objfile *per_objfile)
24196 {
24197 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
24198 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
24199 }
24200
24201 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
24202 On entry *REF_CU is the CU of SRC_DIE.
24203 On exit *REF_CU is the CU of the result.
24204 Returns NULL if the referenced DIE isn't found. */
24205
24206 static struct die_info *
24207 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
24208 struct dwarf2_cu **ref_cu)
24209 {
24210 struct die_info temp_die;
24211 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
24212 struct die_info *die;
24213 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
24214
24215
24216 /* While it might be nice to assert sig_type->type == NULL here,
24217 we can get here for DW_AT_imported_declaration where we need
24218 the DIE not the type. */
24219
24220 /* If necessary, add it to the queue and load its DIEs.
24221
24222 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
24223 it doesn't mean they are currently loaded. Since we require them
24224 to be loaded, we must check for ourselves. */
24225 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
24226 language_minimal)
24227 || per_objfile->get_cu (&sig_type->per_cu) == nullptr)
24228 read_signatured_type (sig_type, per_objfile);
24229
24230 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
24231 gdb_assert (sig_cu != NULL);
24232 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
24233 temp_die.sect_off = sig_type->type_offset_in_section;
24234 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
24235 to_underlying (temp_die.sect_off));
24236 if (die)
24237 {
24238 /* For .gdb_index version 7 keep track of included TUs.
24239 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
24240 if (per_objfile->per_bfd->index_table != NULL
24241 && per_objfile->per_bfd->index_table->version <= 7)
24242 {
24243 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
24244 }
24245
24246 *ref_cu = sig_cu;
24247 if (sig_cu != cu)
24248 sig_cu->ancestor = cu;
24249
24250 return die;
24251 }
24252
24253 return NULL;
24254 }
24255
24256 /* Follow signatured type referenced by ATTR in SRC_DIE.
24257 On entry *REF_CU is the CU of SRC_DIE.
24258 On exit *REF_CU is the CU of the result.
24259 The result is the DIE of the type.
24260 If the referenced type cannot be found an error is thrown. */
24261
24262 static struct die_info *
24263 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
24264 struct dwarf2_cu **ref_cu)
24265 {
24266 ULONGEST signature = attr->as_signature ();
24267 struct signatured_type *sig_type;
24268 struct die_info *die;
24269
24270 gdb_assert (attr->form == DW_FORM_ref_sig8);
24271
24272 sig_type = lookup_signatured_type (*ref_cu, signature);
24273 /* sig_type will be NULL if the signatured type is missing from
24274 the debug info. */
24275 if (sig_type == NULL)
24276 {
24277 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24278 " from DIE at %s [in module %s]"),
24279 hex_string (signature), sect_offset_str (src_die->sect_off),
24280 objfile_name ((*ref_cu)->per_objfile->objfile));
24281 }
24282
24283 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
24284 if (die == NULL)
24285 {
24286 dump_die_for_error (src_die);
24287 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24288 " from DIE at %s [in module %s]"),
24289 hex_string (signature), sect_offset_str (src_die->sect_off),
24290 objfile_name ((*ref_cu)->per_objfile->objfile));
24291 }
24292
24293 return die;
24294 }
24295
24296 /* Get the type specified by SIGNATURE referenced in DIE/CU,
24297 reading in and processing the type unit if necessary. */
24298
24299 static struct type *
24300 get_signatured_type (struct die_info *die, ULONGEST signature,
24301 struct dwarf2_cu *cu)
24302 {
24303 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24304 struct signatured_type *sig_type;
24305 struct dwarf2_cu *type_cu;
24306 struct die_info *type_die;
24307 struct type *type;
24308
24309 sig_type = lookup_signatured_type (cu, signature);
24310 /* sig_type will be NULL if the signatured type is missing from
24311 the debug info. */
24312 if (sig_type == NULL)
24313 {
24314 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24315 " from DIE at %s [in module %s]"),
24316 hex_string (signature), sect_offset_str (die->sect_off),
24317 objfile_name (per_objfile->objfile));
24318 return build_error_marker_type (cu, die);
24319 }
24320
24321 /* If we already know the type we're done. */
24322 type = per_objfile->get_type_for_signatured_type (sig_type);
24323 if (type != nullptr)
24324 return type;
24325
24326 type_cu = cu;
24327 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24328 if (type_die != NULL)
24329 {
24330 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24331 is created. This is important, for example, because for c++ classes
24332 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24333 type = read_type_die (type_die, type_cu);
24334 if (type == NULL)
24335 {
24336 complaint (_("Dwarf Error: Cannot build signatured type %s"
24337 " referenced from DIE at %s [in module %s]"),
24338 hex_string (signature), sect_offset_str (die->sect_off),
24339 objfile_name (per_objfile->objfile));
24340 type = build_error_marker_type (cu, die);
24341 }
24342 }
24343 else
24344 {
24345 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24346 " from DIE at %s [in module %s]"),
24347 hex_string (signature), sect_offset_str (die->sect_off),
24348 objfile_name (per_objfile->objfile));
24349 type = build_error_marker_type (cu, die);
24350 }
24351
24352 per_objfile->set_type_for_signatured_type (sig_type, type);
24353
24354 return type;
24355 }
24356
24357 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24358 reading in and processing the type unit if necessary. */
24359
24360 static struct type *
24361 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
24362 struct dwarf2_cu *cu) /* ARI: editCase function */
24363 {
24364 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
24365 if (attr->form_is_ref ())
24366 {
24367 struct dwarf2_cu *type_cu = cu;
24368 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24369
24370 return read_type_die (type_die, type_cu);
24371 }
24372 else if (attr->form == DW_FORM_ref_sig8)
24373 {
24374 return get_signatured_type (die, attr->as_signature (), cu);
24375 }
24376 else
24377 {
24378 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24379
24380 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
24381 " at %s [in module %s]"),
24382 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
24383 objfile_name (per_objfile->objfile));
24384 return build_error_marker_type (cu, die);
24385 }
24386 }
24387
24388 /* Load the DIEs associated with type unit PER_CU into memory. */
24389
24390 static void
24391 load_full_type_unit (dwarf2_per_cu_data *per_cu,
24392 dwarf2_per_objfile *per_objfile)
24393 {
24394 struct signatured_type *sig_type;
24395
24396 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24397 gdb_assert (! per_cu->type_unit_group_p ());
24398
24399 /* We have the per_cu, but we need the signatured_type.
24400 Fortunately this is an easy translation. */
24401 gdb_assert (per_cu->is_debug_types);
24402 sig_type = (struct signatured_type *) per_cu;
24403
24404 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
24405
24406 read_signatured_type (sig_type, per_objfile);
24407
24408 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
24409 }
24410
24411 /* Read in a signatured type and build its CU and DIEs.
24412 If the type is a stub for the real type in a DWO file,
24413 read in the real type from the DWO file as well. */
24414
24415 static void
24416 read_signatured_type (signatured_type *sig_type,
24417 dwarf2_per_objfile *per_objfile)
24418 {
24419 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
24420
24421 gdb_assert (per_cu->is_debug_types);
24422 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
24423
24424 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
24425
24426 if (!reader.dummy_p)
24427 {
24428 struct dwarf2_cu *cu = reader.cu;
24429 const gdb_byte *info_ptr = reader.info_ptr;
24430
24431 gdb_assert (cu->die_hash == NULL);
24432 cu->die_hash =
24433 htab_create_alloc_ex (cu->header.length / 12,
24434 die_hash,
24435 die_eq,
24436 NULL,
24437 &cu->comp_unit_obstack,
24438 hashtab_obstack_allocate,
24439 dummy_obstack_deallocate);
24440
24441 if (reader.comp_unit_die->has_children)
24442 reader.comp_unit_die->child
24443 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24444 reader.comp_unit_die);
24445 cu->dies = reader.comp_unit_die;
24446 /* comp_unit_die is not stored in die_hash, no need. */
24447
24448 /* We try not to read any attributes in this function, because
24449 not all CUs needed for references have been loaded yet, and
24450 symbol table processing isn't initialized. But we have to
24451 set the CU language, or we won't be able to build types
24452 correctly. Similarly, if we do not read the producer, we can
24453 not apply producer-specific interpretation. */
24454 prepare_one_comp_unit (cu, cu->dies, language_minimal);
24455
24456 reader.keep ();
24457 }
24458
24459 sig_type->per_cu.tu_read = 1;
24460 }
24461
24462 /* Decode simple location descriptions.
24463 Given a pointer to a dwarf block that defines a location, compute
24464 the location and return the value. If COMPUTED is non-null, it is
24465 set to true to indicate that decoding was successful, and false
24466 otherwise. If COMPUTED is null, then this function may emit a
24467 complaint. */
24468
24469 static CORE_ADDR
24470 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
24471 {
24472 struct objfile *objfile = cu->per_objfile->objfile;
24473 size_t i;
24474 size_t size = blk->size;
24475 const gdb_byte *data = blk->data;
24476 CORE_ADDR stack[64];
24477 int stacki;
24478 unsigned int bytes_read, unsnd;
24479 gdb_byte op;
24480
24481 if (computed != nullptr)
24482 *computed = false;
24483
24484 i = 0;
24485 stacki = 0;
24486 stack[stacki] = 0;
24487 stack[++stacki] = 0;
24488
24489 while (i < size)
24490 {
24491 op = data[i++];
24492 switch (op)
24493 {
24494 case DW_OP_lit0:
24495 case DW_OP_lit1:
24496 case DW_OP_lit2:
24497 case DW_OP_lit3:
24498 case DW_OP_lit4:
24499 case DW_OP_lit5:
24500 case DW_OP_lit6:
24501 case DW_OP_lit7:
24502 case DW_OP_lit8:
24503 case DW_OP_lit9:
24504 case DW_OP_lit10:
24505 case DW_OP_lit11:
24506 case DW_OP_lit12:
24507 case DW_OP_lit13:
24508 case DW_OP_lit14:
24509 case DW_OP_lit15:
24510 case DW_OP_lit16:
24511 case DW_OP_lit17:
24512 case DW_OP_lit18:
24513 case DW_OP_lit19:
24514 case DW_OP_lit20:
24515 case DW_OP_lit21:
24516 case DW_OP_lit22:
24517 case DW_OP_lit23:
24518 case DW_OP_lit24:
24519 case DW_OP_lit25:
24520 case DW_OP_lit26:
24521 case DW_OP_lit27:
24522 case DW_OP_lit28:
24523 case DW_OP_lit29:
24524 case DW_OP_lit30:
24525 case DW_OP_lit31:
24526 stack[++stacki] = op - DW_OP_lit0;
24527 break;
24528
24529 case DW_OP_reg0:
24530 case DW_OP_reg1:
24531 case DW_OP_reg2:
24532 case DW_OP_reg3:
24533 case DW_OP_reg4:
24534 case DW_OP_reg5:
24535 case DW_OP_reg6:
24536 case DW_OP_reg7:
24537 case DW_OP_reg8:
24538 case DW_OP_reg9:
24539 case DW_OP_reg10:
24540 case DW_OP_reg11:
24541 case DW_OP_reg12:
24542 case DW_OP_reg13:
24543 case DW_OP_reg14:
24544 case DW_OP_reg15:
24545 case DW_OP_reg16:
24546 case DW_OP_reg17:
24547 case DW_OP_reg18:
24548 case DW_OP_reg19:
24549 case DW_OP_reg20:
24550 case DW_OP_reg21:
24551 case DW_OP_reg22:
24552 case DW_OP_reg23:
24553 case DW_OP_reg24:
24554 case DW_OP_reg25:
24555 case DW_OP_reg26:
24556 case DW_OP_reg27:
24557 case DW_OP_reg28:
24558 case DW_OP_reg29:
24559 case DW_OP_reg30:
24560 case DW_OP_reg31:
24561 stack[++stacki] = op - DW_OP_reg0;
24562 if (i < size)
24563 {
24564 if (computed == nullptr)
24565 dwarf2_complex_location_expr_complaint ();
24566 else
24567 return 0;
24568 }
24569 break;
24570
24571 case DW_OP_regx:
24572 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24573 i += bytes_read;
24574 stack[++stacki] = unsnd;
24575 if (i < size)
24576 {
24577 if (computed == nullptr)
24578 dwarf2_complex_location_expr_complaint ();
24579 else
24580 return 0;
24581 }
24582 break;
24583
24584 case DW_OP_addr:
24585 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24586 &bytes_read);
24587 i += bytes_read;
24588 break;
24589
24590 case DW_OP_const1u:
24591 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24592 i += 1;
24593 break;
24594
24595 case DW_OP_const1s:
24596 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24597 i += 1;
24598 break;
24599
24600 case DW_OP_const2u:
24601 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24602 i += 2;
24603 break;
24604
24605 case DW_OP_const2s:
24606 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24607 i += 2;
24608 break;
24609
24610 case DW_OP_const4u:
24611 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24612 i += 4;
24613 break;
24614
24615 case DW_OP_const4s:
24616 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24617 i += 4;
24618 break;
24619
24620 case DW_OP_const8u:
24621 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24622 i += 8;
24623 break;
24624
24625 case DW_OP_constu:
24626 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24627 &bytes_read);
24628 i += bytes_read;
24629 break;
24630
24631 case DW_OP_consts:
24632 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24633 i += bytes_read;
24634 break;
24635
24636 case DW_OP_dup:
24637 stack[stacki + 1] = stack[stacki];
24638 stacki++;
24639 break;
24640
24641 case DW_OP_plus:
24642 stack[stacki - 1] += stack[stacki];
24643 stacki--;
24644 break;
24645
24646 case DW_OP_plus_uconst:
24647 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24648 &bytes_read);
24649 i += bytes_read;
24650 break;
24651
24652 case DW_OP_minus:
24653 stack[stacki - 1] -= stack[stacki];
24654 stacki--;
24655 break;
24656
24657 case DW_OP_deref:
24658 /* If we're not the last op, then we definitely can't encode
24659 this using GDB's address_class enum. This is valid for partial
24660 global symbols, although the variable's address will be bogus
24661 in the psymtab. */
24662 if (i < size)
24663 {
24664 if (computed == nullptr)
24665 dwarf2_complex_location_expr_complaint ();
24666 else
24667 return 0;
24668 }
24669 break;
24670
24671 case DW_OP_GNU_push_tls_address:
24672 case DW_OP_form_tls_address:
24673 /* The top of the stack has the offset from the beginning
24674 of the thread control block at which the variable is located. */
24675 /* Nothing should follow this operator, so the top of stack would
24676 be returned. */
24677 /* This is valid for partial global symbols, but the variable's
24678 address will be bogus in the psymtab. Make it always at least
24679 non-zero to not look as a variable garbage collected by linker
24680 which have DW_OP_addr 0. */
24681 if (i < size)
24682 {
24683 if (computed == nullptr)
24684 dwarf2_complex_location_expr_complaint ();
24685 else
24686 return 0;
24687 }
24688 stack[stacki]++;
24689 break;
24690
24691 case DW_OP_GNU_uninit:
24692 if (computed != nullptr)
24693 return 0;
24694 break;
24695
24696 case DW_OP_addrx:
24697 case DW_OP_GNU_addr_index:
24698 case DW_OP_GNU_const_index:
24699 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24700 &bytes_read);
24701 i += bytes_read;
24702 break;
24703
24704 default:
24705 if (computed == nullptr)
24706 {
24707 const char *name = get_DW_OP_name (op);
24708
24709 if (name)
24710 complaint (_("unsupported stack op: '%s'"),
24711 name);
24712 else
24713 complaint (_("unsupported stack op: '%02x'"),
24714 op);
24715 }
24716
24717 return (stack[stacki]);
24718 }
24719
24720 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24721 outside of the allocated space. Also enforce minimum>0. */
24722 if (stacki >= ARRAY_SIZE (stack) - 1)
24723 {
24724 if (computed == nullptr)
24725 complaint (_("location description stack overflow"));
24726 return 0;
24727 }
24728
24729 if (stacki <= 0)
24730 {
24731 if (computed == nullptr)
24732 complaint (_("location description stack underflow"));
24733 return 0;
24734 }
24735 }
24736
24737 if (computed != nullptr)
24738 *computed = true;
24739 return (stack[stacki]);
24740 }
24741
24742 /* memory allocation interface */
24743
24744 static struct dwarf_block *
24745 dwarf_alloc_block (struct dwarf2_cu *cu)
24746 {
24747 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24748 }
24749
24750 static struct die_info *
24751 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24752 {
24753 struct die_info *die;
24754 size_t size = sizeof (struct die_info);
24755
24756 if (num_attrs > 1)
24757 size += (num_attrs - 1) * sizeof (struct attribute);
24758
24759 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24760 memset (die, 0, sizeof (struct die_info));
24761 return (die);
24762 }
24763
24764 \f
24765
24766 /* Macro support. */
24767
24768 /* An overload of dwarf_decode_macros that finds the correct section
24769 and ensures it is read in before calling the other overload. */
24770
24771 static void
24772 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24773 int section_is_gnu)
24774 {
24775 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24776 struct objfile *objfile = per_objfile->objfile;
24777 const struct line_header *lh = cu->line_header;
24778 unsigned int offset_size = cu->header.offset_size;
24779 struct dwarf2_section_info *section;
24780 const char *section_name;
24781
24782 if (cu->dwo_unit != nullptr)
24783 {
24784 if (section_is_gnu)
24785 {
24786 section = &cu->dwo_unit->dwo_file->sections.macro;
24787 section_name = ".debug_macro.dwo";
24788 }
24789 else
24790 {
24791 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24792 section_name = ".debug_macinfo.dwo";
24793 }
24794 }
24795 else
24796 {
24797 if (section_is_gnu)
24798 {
24799 section = &per_objfile->per_bfd->macro;
24800 section_name = ".debug_macro";
24801 }
24802 else
24803 {
24804 section = &per_objfile->per_bfd->macinfo;
24805 section_name = ".debug_macinfo";
24806 }
24807 }
24808
24809 section->read (objfile);
24810 if (section->buffer == nullptr)
24811 {
24812 complaint (_("missing %s section"), section_name);
24813 return;
24814 }
24815
24816 buildsym_compunit *builder = cu->get_builder ();
24817
24818 struct dwarf2_section_info *str_offsets_section;
24819 struct dwarf2_section_info *str_section;
24820 ULONGEST str_offsets_base;
24821
24822 if (cu->dwo_unit != nullptr)
24823 {
24824 str_offsets_section = &cu->dwo_unit->dwo_file
24825 ->sections.str_offsets;
24826 str_section = &cu->dwo_unit->dwo_file->sections.str;
24827 str_offsets_base = cu->header.addr_size;
24828 }
24829 else
24830 {
24831 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24832 str_section = &per_objfile->per_bfd->str;
24833 str_offsets_base = *cu->str_offsets_base;
24834 }
24835
24836 dwarf_decode_macros (per_objfile, builder, section, lh,
24837 offset_size, offset, str_section, str_offsets_section,
24838 str_offsets_base, section_is_gnu);
24839 }
24840
24841 /* Return the .debug_loc section to use for CU.
24842 For DWO files use .debug_loc.dwo. */
24843
24844 static struct dwarf2_section_info *
24845 cu_debug_loc_section (struct dwarf2_cu *cu)
24846 {
24847 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24848
24849 if (cu->dwo_unit)
24850 {
24851 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24852
24853 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24854 }
24855 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24856 : &per_objfile->per_bfd->loc);
24857 }
24858
24859 /* Return the .debug_rnglists section to use for CU. */
24860 static struct dwarf2_section_info *
24861 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24862 {
24863 if (cu->header.version < 5)
24864 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24865 cu->header.version);
24866 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24867
24868 /* Make sure we read the .debug_rnglists section from the file that
24869 contains the DW_AT_ranges attribute we are reading. Normally that
24870 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24871 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24872 program. */
24873 if (cu->dwo_unit != nullptr
24874 && tag != DW_TAG_compile_unit
24875 && tag != DW_TAG_skeleton_unit)
24876 {
24877 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24878
24879 if (sections->rnglists.size > 0)
24880 return &sections->rnglists;
24881 else
24882 error (_(".debug_rnglists section is missing from .dwo file."));
24883 }
24884 return &dwarf2_per_objfile->per_bfd->rnglists;
24885 }
24886
24887 /* A helper function that fills in a dwarf2_loclist_baton. */
24888
24889 static void
24890 fill_in_loclist_baton (struct dwarf2_cu *cu,
24891 struct dwarf2_loclist_baton *baton,
24892 const struct attribute *attr)
24893 {
24894 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24895 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24896
24897 section->read (per_objfile->objfile);
24898
24899 baton->per_objfile = per_objfile;
24900 baton->per_cu = cu->per_cu;
24901 gdb_assert (baton->per_cu);
24902 /* We don't know how long the location list is, but make sure we
24903 don't run off the edge of the section. */
24904 baton->size = section->size - attr->as_unsigned ();
24905 baton->data = section->buffer + attr->as_unsigned ();
24906 if (cu->base_address.has_value ())
24907 baton->base_address = *cu->base_address;
24908 else
24909 baton->base_address = 0;
24910 baton->from_dwo = cu->dwo_unit != NULL;
24911 }
24912
24913 static void
24914 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24915 struct dwarf2_cu *cu, int is_block)
24916 {
24917 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24918 struct objfile *objfile = per_objfile->objfile;
24919 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24920
24921 if (attr->form_is_section_offset ()
24922 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24923 the section. If so, fall through to the complaint in the
24924 other branch. */
24925 && attr->as_unsigned () < section->get_size (objfile))
24926 {
24927 struct dwarf2_loclist_baton *baton;
24928
24929 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24930
24931 fill_in_loclist_baton (cu, baton, attr);
24932
24933 if (!cu->base_address.has_value ())
24934 complaint (_("Location list used without "
24935 "specifying the CU base address."));
24936
24937 SYMBOL_ACLASS_INDEX (sym) = (is_block
24938 ? dwarf2_loclist_block_index
24939 : dwarf2_loclist_index);
24940 SYMBOL_LOCATION_BATON (sym) = baton;
24941 }
24942 else
24943 {
24944 struct dwarf2_locexpr_baton *baton;
24945
24946 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24947 baton->per_objfile = per_objfile;
24948 baton->per_cu = cu->per_cu;
24949 gdb_assert (baton->per_cu);
24950
24951 if (attr->form_is_block ())
24952 {
24953 /* Note that we're just copying the block's data pointer
24954 here, not the actual data. We're still pointing into the
24955 info_buffer for SYM's objfile; right now we never release
24956 that buffer, but when we do clean up properly this may
24957 need to change. */
24958 struct dwarf_block *block = attr->as_block ();
24959 baton->size = block->size;
24960 baton->data = block->data;
24961 }
24962 else
24963 {
24964 dwarf2_invalid_attrib_class_complaint ("location description",
24965 sym->natural_name ());
24966 baton->size = 0;
24967 }
24968
24969 SYMBOL_ACLASS_INDEX (sym) = (is_block
24970 ? dwarf2_locexpr_block_index
24971 : dwarf2_locexpr_index);
24972 SYMBOL_LOCATION_BATON (sym) = baton;
24973 }
24974 }
24975
24976 /* See read.h. */
24977
24978 const comp_unit_head *
24979 dwarf2_per_cu_data::get_header () const
24980 {
24981 if (!m_header_read_in)
24982 {
24983 const gdb_byte *info_ptr
24984 = this->section->buffer + to_underlying (this->sect_off);
24985
24986 memset (&m_header, 0, sizeof (m_header));
24987
24988 read_comp_unit_head (&m_header, info_ptr, this->section,
24989 rcuh_kind::COMPILE);
24990 }
24991
24992 return &m_header;
24993 }
24994
24995 /* See read.h. */
24996
24997 int
24998 dwarf2_per_cu_data::addr_size () const
24999 {
25000 return this->get_header ()->addr_size;
25001 }
25002
25003 /* See read.h. */
25004
25005 int
25006 dwarf2_per_cu_data::offset_size () const
25007 {
25008 return this->get_header ()->offset_size;
25009 }
25010
25011 /* See read.h. */
25012
25013 int
25014 dwarf2_per_cu_data::ref_addr_size () const
25015 {
25016 const comp_unit_head *header = this->get_header ();
25017
25018 if (header->version == 2)
25019 return header->addr_size;
25020 else
25021 return header->offset_size;
25022 }
25023
25024 /* See read.h. */
25025
25026 struct type *
25027 dwarf2_cu::addr_type () const
25028 {
25029 struct objfile *objfile = this->per_objfile->objfile;
25030 struct type *void_type = objfile_type (objfile)->builtin_void;
25031 struct type *addr_type = lookup_pointer_type (void_type);
25032 int addr_size = this->per_cu->addr_size ();
25033
25034 if (TYPE_LENGTH (addr_type) == addr_size)
25035 return addr_type;
25036
25037 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
25038 return addr_type;
25039 }
25040
25041 /* A helper function for dwarf2_find_containing_comp_unit that returns
25042 the index of the result, and that searches a vector. It will
25043 return a result even if the offset in question does not actually
25044 occur in any CU. This is separate so that it can be unit
25045 tested. */
25046
25047 static int
25048 dwarf2_find_containing_comp_unit
25049 (sect_offset sect_off,
25050 unsigned int offset_in_dwz,
25051 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
25052 {
25053 int low, high;
25054
25055 low = 0;
25056 high = all_comp_units.size () - 1;
25057 while (high > low)
25058 {
25059 struct dwarf2_per_cu_data *mid_cu;
25060 int mid = low + (high - low) / 2;
25061
25062 mid_cu = all_comp_units[mid];
25063 if (mid_cu->is_dwz > offset_in_dwz
25064 || (mid_cu->is_dwz == offset_in_dwz
25065 && mid_cu->sect_off + mid_cu->length > sect_off))
25066 high = mid;
25067 else
25068 low = mid + 1;
25069 }
25070 gdb_assert (low == high);
25071 return low;
25072 }
25073
25074 /* Locate the .debug_info compilation unit from CU's objfile which contains
25075 the DIE at OFFSET. Raises an error on failure. */
25076
25077 static struct dwarf2_per_cu_data *
25078 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25079 unsigned int offset_in_dwz,
25080 dwarf2_per_objfile *per_objfile)
25081 {
25082 int low = dwarf2_find_containing_comp_unit
25083 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
25084 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
25085
25086 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
25087 {
25088 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25089 error (_("Dwarf Error: could not find partial DIE containing "
25090 "offset %s [in module %s]"),
25091 sect_offset_str (sect_off),
25092 bfd_get_filename (per_objfile->objfile->obfd));
25093
25094 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
25095 <= sect_off);
25096 return per_objfile->per_bfd->all_comp_units[low-1];
25097 }
25098 else
25099 {
25100 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
25101 && sect_off >= this_cu->sect_off + this_cu->length)
25102 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25103 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25104 return this_cu;
25105 }
25106 }
25107
25108 #if GDB_SELF_TEST
25109
25110 namespace selftests {
25111 namespace find_containing_comp_unit {
25112
25113 static void
25114 run_test ()
25115 {
25116 struct dwarf2_per_cu_data one {};
25117 struct dwarf2_per_cu_data two {};
25118 struct dwarf2_per_cu_data three {};
25119 struct dwarf2_per_cu_data four {};
25120
25121 one.length = 5;
25122 two.sect_off = sect_offset (one.length);
25123 two.length = 7;
25124
25125 three.length = 5;
25126 three.is_dwz = 1;
25127 four.sect_off = sect_offset (three.length);
25128 four.length = 7;
25129 four.is_dwz = 1;
25130
25131 std::vector<dwarf2_per_cu_data *> units;
25132 units.push_back (&one);
25133 units.push_back (&two);
25134 units.push_back (&three);
25135 units.push_back (&four);
25136
25137 int result;
25138
25139 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
25140 SELF_CHECK (units[result] == &one);
25141 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
25142 SELF_CHECK (units[result] == &one);
25143 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
25144 SELF_CHECK (units[result] == &two);
25145
25146 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
25147 SELF_CHECK (units[result] == &three);
25148 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
25149 SELF_CHECK (units[result] == &three);
25150 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
25151 SELF_CHECK (units[result] == &four);
25152 }
25153
25154 }
25155 }
25156
25157 #endif /* GDB_SELF_TEST */
25158
25159 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
25160
25161 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
25162 dwarf2_per_objfile *per_objfile)
25163 : per_cu (per_cu),
25164 per_objfile (per_objfile),
25165 mark (false),
25166 has_loclist (false),
25167 checked_producer (false),
25168 producer_is_gxx_lt_4_6 (false),
25169 producer_is_gcc_lt_4_3 (false),
25170 producer_is_icc (false),
25171 producer_is_icc_lt_14 (false),
25172 producer_is_codewarrior (false),
25173 processing_has_namespace_info (false)
25174 {
25175 }
25176
25177 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25178
25179 static void
25180 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25181 enum language pretend_language)
25182 {
25183 struct attribute *attr;
25184
25185 /* Set the language we're debugging. */
25186 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25187 if (attr != nullptr)
25188 set_cu_language (attr->constant_value (0), cu);
25189 else
25190 {
25191 cu->language = pretend_language;
25192 cu->language_defn = language_def (cu->language);
25193 }
25194
25195 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25196 }
25197
25198 /* See read.h. */
25199
25200 dwarf2_cu *
25201 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
25202 {
25203 auto it = m_dwarf2_cus.find (per_cu);
25204 if (it == m_dwarf2_cus.end ())
25205 return nullptr;
25206
25207 return it->second;
25208 }
25209
25210 /* See read.h. */
25211
25212 void
25213 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
25214 {
25215 gdb_assert (this->get_cu (per_cu) == nullptr);
25216
25217 m_dwarf2_cus[per_cu] = cu;
25218 }
25219
25220 /* See read.h. */
25221
25222 void
25223 dwarf2_per_objfile::age_comp_units ()
25224 {
25225 dwarf_read_debug_printf_v ("running");
25226
25227 /* This is not expected to be called in the middle of CU expansion. There is
25228 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
25229 loaded in memory. Calling age_comp_units while the queue is in use could
25230 make us free the DIEs for a CU that is in the queue and therefore break
25231 that invariant. */
25232 gdb_assert (!this->per_bfd->queue.has_value ());
25233
25234 /* Start by clearing all marks. */
25235 for (auto pair : m_dwarf2_cus)
25236 pair.second->mark = false;
25237
25238 /* Traverse all CUs, mark them and their dependencies if used recently
25239 enough. */
25240 for (auto pair : m_dwarf2_cus)
25241 {
25242 dwarf2_cu *cu = pair.second;
25243
25244 cu->last_used++;
25245 if (cu->last_used <= dwarf_max_cache_age)
25246 dwarf2_mark (cu);
25247 }
25248
25249 /* Delete all CUs still not marked. */
25250 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
25251 {
25252 dwarf2_cu *cu = it->second;
25253
25254 if (!cu->mark)
25255 {
25256 dwarf_read_debug_printf_v ("deleting old CU %s",
25257 sect_offset_str (cu->per_cu->sect_off));
25258 delete cu;
25259 it = m_dwarf2_cus.erase (it);
25260 }
25261 else
25262 it++;
25263 }
25264 }
25265
25266 /* See read.h. */
25267
25268 void
25269 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
25270 {
25271 auto it = m_dwarf2_cus.find (per_cu);
25272 if (it == m_dwarf2_cus.end ())
25273 return;
25274
25275 delete it->second;
25276
25277 m_dwarf2_cus.erase (it);
25278 }
25279
25280 dwarf2_per_objfile::~dwarf2_per_objfile ()
25281 {
25282 remove_all_cus ();
25283 }
25284
25285 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25286 We store these in a hash table separate from the DIEs, and preserve them
25287 when the DIEs are flushed out of cache.
25288
25289 The CU "per_cu" pointer is needed because offset alone is not enough to
25290 uniquely identify the type. A file may have multiple .debug_types sections,
25291 or the type may come from a DWO file. Furthermore, while it's more logical
25292 to use per_cu->section+offset, with Fission the section with the data is in
25293 the DWO file but we don't know that section at the point we need it.
25294 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25295 because we can enter the lookup routine, get_die_type_at_offset, from
25296 outside this file, and thus won't necessarily have PER_CU->cu.
25297 Fortunately, PER_CU is stable for the life of the objfile. */
25298
25299 struct dwarf2_per_cu_offset_and_type
25300 {
25301 const struct dwarf2_per_cu_data *per_cu;
25302 sect_offset sect_off;
25303 struct type *type;
25304 };
25305
25306 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25307
25308 static hashval_t
25309 per_cu_offset_and_type_hash (const void *item)
25310 {
25311 const struct dwarf2_per_cu_offset_and_type *ofs
25312 = (const struct dwarf2_per_cu_offset_and_type *) item;
25313
25314 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25315 }
25316
25317 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25318
25319 static int
25320 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25321 {
25322 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25323 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25324 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25325 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25326
25327 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25328 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25329 }
25330
25331 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25332 table if necessary. For convenience, return TYPE.
25333
25334 The DIEs reading must have careful ordering to:
25335 * Not cause infinite loops trying to read in DIEs as a prerequisite for
25336 reading current DIE.
25337 * Not trying to dereference contents of still incompletely read in types
25338 while reading in other DIEs.
25339 * Enable referencing still incompletely read in types just by a pointer to
25340 the type without accessing its fields.
25341
25342 Therefore caller should follow these rules:
25343 * Try to fetch any prerequisite types we may need to build this DIE type
25344 before building the type and calling set_die_type.
25345 * After building type call set_die_type for current DIE as soon as
25346 possible before fetching more types to complete the current type.
25347 * Make the type as complete as possible before fetching more types. */
25348
25349 static struct type *
25350 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
25351 bool skip_data_location)
25352 {
25353 dwarf2_per_objfile *per_objfile = cu->per_objfile;
25354 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25355 struct objfile *objfile = per_objfile->objfile;
25356 struct attribute *attr;
25357 struct dynamic_prop prop;
25358
25359 /* For Ada types, make sure that the gnat-specific data is always
25360 initialized (if not already set). There are a few types where
25361 we should not be doing so, because the type-specific area is
25362 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25363 where the type-specific area is used to store the floatformat).
25364 But this is not a problem, because the gnat-specific information
25365 is actually not needed for these types. */
25366 if (need_gnat_info (cu)
25367 && type->code () != TYPE_CODE_FUNC
25368 && type->code () != TYPE_CODE_FLT
25369 && type->code () != TYPE_CODE_METHODPTR
25370 && type->code () != TYPE_CODE_MEMBERPTR
25371 && type->code () != TYPE_CODE_METHOD
25372 && type->code () != TYPE_CODE_FIXED_POINT
25373 && !HAVE_GNAT_AUX_INFO (type))
25374 INIT_GNAT_SPECIFIC (type);
25375
25376 /* Read DW_AT_allocated and set in type. */
25377 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25378 if (attr != NULL)
25379 {
25380 struct type *prop_type = cu->addr_sized_int_type (false);
25381 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25382 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
25383 }
25384
25385 /* Read DW_AT_associated and set in type. */
25386 attr = dwarf2_attr (die, DW_AT_associated, cu);
25387 if (attr != NULL)
25388 {
25389 struct type *prop_type = cu->addr_sized_int_type (false);
25390 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25391 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
25392 }
25393
25394 /* Read DW_AT_data_location and set in type. */
25395 if (!skip_data_location)
25396 {
25397 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25398 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
25399 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
25400 }
25401
25402 if (per_objfile->die_type_hash == NULL)
25403 per_objfile->die_type_hash
25404 = htab_up (htab_create_alloc (127,
25405 per_cu_offset_and_type_hash,
25406 per_cu_offset_and_type_eq,
25407 NULL, xcalloc, xfree));
25408
25409 ofs.per_cu = cu->per_cu;
25410 ofs.sect_off = die->sect_off;
25411 ofs.type = type;
25412 slot = (struct dwarf2_per_cu_offset_and_type **)
25413 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
25414 if (*slot)
25415 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25416 sect_offset_str (die->sect_off));
25417 *slot = XOBNEW (&objfile->objfile_obstack,
25418 struct dwarf2_per_cu_offset_and_type);
25419 **slot = ofs;
25420 return type;
25421 }
25422
25423 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25424 or return NULL if the die does not have a saved type. */
25425
25426 static struct type *
25427 get_die_type_at_offset (sect_offset sect_off,
25428 dwarf2_per_cu_data *per_cu,
25429 dwarf2_per_objfile *per_objfile)
25430 {
25431 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25432
25433 if (per_objfile->die_type_hash == NULL)
25434 return NULL;
25435
25436 ofs.per_cu = per_cu;
25437 ofs.sect_off = sect_off;
25438 slot = ((struct dwarf2_per_cu_offset_and_type *)
25439 htab_find (per_objfile->die_type_hash.get (), &ofs));
25440 if (slot)
25441 return slot->type;
25442 else
25443 return NULL;
25444 }
25445
25446 /* Look up the type for DIE in CU in die_type_hash,
25447 or return NULL if DIE does not have a saved type. */
25448
25449 static struct type *
25450 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25451 {
25452 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
25453 }
25454
25455 /* Add a dependence relationship from CU to REF_PER_CU. */
25456
25457 static void
25458 dwarf2_add_dependence (struct dwarf2_cu *cu,
25459 struct dwarf2_per_cu_data *ref_per_cu)
25460 {
25461 void **slot;
25462
25463 if (cu->dependencies == NULL)
25464 cu->dependencies
25465 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25466 NULL, &cu->comp_unit_obstack,
25467 hashtab_obstack_allocate,
25468 dummy_obstack_deallocate);
25469
25470 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25471 if (*slot == NULL)
25472 *slot = ref_per_cu;
25473 }
25474
25475 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25476 Set the mark field in every compilation unit in the
25477 cache that we must keep because we are keeping CU.
25478
25479 DATA is the dwarf2_per_objfile object in which to look up CUs. */
25480
25481 static int
25482 dwarf2_mark_helper (void **slot, void *data)
25483 {
25484 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
25485 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
25486 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
25487
25488 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25489 reading of the chain. As such dependencies remain valid it is not much
25490 useful to track and undo them during QUIT cleanups. */
25491 if (cu == nullptr)
25492 return 1;
25493
25494 if (cu->mark)
25495 return 1;
25496
25497 cu->mark = true;
25498
25499 if (cu->dependencies != nullptr)
25500 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
25501
25502 return 1;
25503 }
25504
25505 /* Set the mark field in CU and in every other compilation unit in the
25506 cache that we must keep because we are keeping CU. */
25507
25508 static void
25509 dwarf2_mark (struct dwarf2_cu *cu)
25510 {
25511 if (cu->mark)
25512 return;
25513
25514 cu->mark = true;
25515
25516 if (cu->dependencies != nullptr)
25517 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
25518 }
25519
25520 /* Trivial hash function for partial_die_info: the hash value of a DIE
25521 is its offset in .debug_info for this objfile. */
25522
25523 static hashval_t
25524 partial_die_hash (const void *item)
25525 {
25526 const struct partial_die_info *part_die
25527 = (const struct partial_die_info *) item;
25528
25529 return to_underlying (part_die->sect_off);
25530 }
25531
25532 /* Trivial comparison function for partial_die_info structures: two DIEs
25533 are equal if they have the same offset. */
25534
25535 static int
25536 partial_die_eq (const void *item_lhs, const void *item_rhs)
25537 {
25538 const struct partial_die_info *part_die_lhs
25539 = (const struct partial_die_info *) item_lhs;
25540 const struct partial_die_info *part_die_rhs
25541 = (const struct partial_die_info *) item_rhs;
25542
25543 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25544 }
25545
25546 struct cmd_list_element *set_dwarf_cmdlist;
25547 struct cmd_list_element *show_dwarf_cmdlist;
25548
25549 static void
25550 show_check_physname (struct ui_file *file, int from_tty,
25551 struct cmd_list_element *c, const char *value)
25552 {
25553 fprintf_filtered (file,
25554 _("Whether to check \"physname\" is %s.\n"),
25555 value);
25556 }
25557
25558 void _initialize_dwarf2_read ();
25559 void
25560 _initialize_dwarf2_read ()
25561 {
25562 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
25563 Set DWARF specific variables.\n\
25564 Configure DWARF variables such as the cache size."),
25565 &set_dwarf_cmdlist, "maintenance set dwarf ",
25566 0/*allow-unknown*/, &maintenance_set_cmdlist);
25567
25568 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
25569 Show DWARF specific variables.\n\
25570 Show DWARF variables such as the cache size."),
25571 &show_dwarf_cmdlist, "maintenance show dwarf ",
25572 0/*allow-unknown*/, &maintenance_show_cmdlist);
25573
25574 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25575 &dwarf_max_cache_age, _("\
25576 Set the upper bound on the age of cached DWARF compilation units."), _("\
25577 Show the upper bound on the age of cached DWARF compilation units."), _("\
25578 A higher limit means that cached compilation units will be stored\n\
25579 in memory longer, and more total memory will be used. Zero disables\n\
25580 caching, which can slow down startup."),
25581 NULL,
25582 show_dwarf_max_cache_age,
25583 &set_dwarf_cmdlist,
25584 &show_dwarf_cmdlist);
25585
25586 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25587 Set debugging of the DWARF reader."), _("\
25588 Show debugging of the DWARF reader."), _("\
25589 When enabled (non-zero), debugging messages are printed during DWARF\n\
25590 reading and symtab expansion. A value of 1 (one) provides basic\n\
25591 information. A value greater than 1 provides more verbose information."),
25592 NULL,
25593 NULL,
25594 &setdebuglist, &showdebuglist);
25595
25596 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25597 Set debugging of the DWARF DIE reader."), _("\
25598 Show debugging of the DWARF DIE reader."), _("\
25599 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25600 The value is the maximum depth to print."),
25601 NULL,
25602 NULL,
25603 &setdebuglist, &showdebuglist);
25604
25605 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25606 Set debugging of the dwarf line reader."), _("\
25607 Show debugging of the dwarf line reader."), _("\
25608 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25609 A value of 1 (one) provides basic information.\n\
25610 A value greater than 1 provides more verbose information."),
25611 NULL,
25612 NULL,
25613 &setdebuglist, &showdebuglist);
25614
25615 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25616 Set cross-checking of \"physname\" code against demangler."), _("\
25617 Show cross-checking of \"physname\" code against demangler."), _("\
25618 When enabled, GDB's internal \"physname\" code is checked against\n\
25619 the demangler."),
25620 NULL, show_check_physname,
25621 &setdebuglist, &showdebuglist);
25622
25623 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25624 no_class, &use_deprecated_index_sections, _("\
25625 Set whether to use deprecated gdb_index sections."), _("\
25626 Show whether to use deprecated gdb_index sections."), _("\
25627 When enabled, deprecated .gdb_index sections are used anyway.\n\
25628 Normally they are ignored either because of a missing feature or\n\
25629 performance issue.\n\
25630 Warning: This option must be enabled before gdb reads the file."),
25631 NULL,
25632 NULL,
25633 &setlist, &showlist);
25634
25635 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25636 &dwarf2_locexpr_funcs);
25637 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25638 &dwarf2_loclist_funcs);
25639
25640 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25641 &dwarf2_block_frame_base_locexpr_funcs);
25642 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25643 &dwarf2_block_frame_base_loclist_funcs);
25644
25645 #if GDB_SELF_TEST
25646 selftests::register_test ("dw2_expand_symtabs_matching",
25647 selftests::dw2_expand_symtabs_matching::run_test);
25648 selftests::register_test ("dwarf2_find_containing_comp_unit",
25649 selftests::find_containing_comp_unit::run_test);
25650 #endif
25651 }
This page took 0.601845 seconds and 5 git commands to generate.