Introduce dwarf2/public.h
[binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2021 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit.h"
36 #include "dwarf2/index-cache.h"
37 #include "dwarf2/index-common.h"
38 #include "dwarf2/leb.h"
39 #include "dwarf2/line-header.h"
40 #include "dwarf2/dwz.h"
41 #include "dwarf2/macro.h"
42 #include "dwarf2/die.h"
43 #include "dwarf2/sect-names.h"
44 #include "dwarf2/stringify.h"
45 #include "dwarf2/public.h"
46 #include "bfd.h"
47 #include "elf-bfd.h"
48 #include "symtab.h"
49 #include "gdbtypes.h"
50 #include "objfiles.h"
51 #include "dwarf2.h"
52 #include "buildsym.h"
53 #include "demangle.h"
54 #include "gdb-demangle.h"
55 #include "filenames.h" /* for DOSish file names */
56 #include "language.h"
57 #include "complaints.h"
58 #include "dwarf2/expr.h"
59 #include "dwarf2/loc.h"
60 #include "cp-support.h"
61 #include "hashtab.h"
62 #include "command.h"
63 #include "gdbcmd.h"
64 #include "block.h"
65 #include "addrmap.h"
66 #include "typeprint.h"
67 #include "psympriv.h"
68 #include "c-lang.h"
69 #include "go-lang.h"
70 #include "valprint.h"
71 #include "gdbcore.h" /* for gnutarget */
72 #include "gdb/gdb-index.h"
73 #include "gdb_bfd.h"
74 #include "f-lang.h"
75 #include "source.h"
76 #include "build-id.h"
77 #include "namespace.h"
78 #include "gdbsupport/function-view.h"
79 #include "gdbsupport/gdb_optional.h"
80 #include "gdbsupport/underlying.h"
81 #include "gdbsupport/hash_enum.h"
82 #include "filename-seen-cache.h"
83 #include "producer.h"
84 #include <fcntl.h>
85 #include <algorithm>
86 #include <unordered_map>
87 #include "gdbsupport/selftest.h"
88 #include "rust-lang.h"
89 #include "gdbsupport/pathstuff.h"
90 #include "count-one-bits.h"
91
92 /* When == 1, print basic high level tracing messages.
93 When > 1, be more verbose.
94 This is in contrast to the low level DIE reading of dwarf_die_debug. */
95 static unsigned int dwarf_read_debug = 0;
96
97 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
98
99 #define dwarf_read_debug_printf(fmt, ...) \
100 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
101 ##__VA_ARGS__)
102
103 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
104
105 #define dwarf_read_debug_printf_v(fmt, ...) \
106 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
107 ##__VA_ARGS__)
108
109 /* When non-zero, dump DIEs after they are read in. */
110 static unsigned int dwarf_die_debug = 0;
111
112 /* When non-zero, dump line number entries as they are read in. */
113 unsigned int dwarf_line_debug = 0;
114
115 /* When true, cross-check physname against demangler. */
116 static bool check_physname = false;
117
118 /* When true, do not reject deprecated .gdb_index sections. */
119 static bool use_deprecated_index_sections = false;
120
121 /* This is used to store the data that is always per objfile. */
122 static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
123
124 /* These are used to store the dwarf2_per_bfd objects.
125
126 objfiles having the same BFD, which doesn't require relocations, are going to
127 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
128
129 Other objfiles are not going to share a dwarf2_per_bfd with any other
130 objfiles, so they'll have their own version kept in the _objfile_data_key
131 version. */
132 static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
133 static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
134
135 /* The "aclass" indices for various kinds of computed DWARF symbols. */
136
137 static int dwarf2_locexpr_index;
138 static int dwarf2_loclist_index;
139 static int dwarf2_locexpr_block_index;
140 static int dwarf2_loclist_block_index;
141
142 /* Size of .debug_loclists section header for 32-bit DWARF format. */
143 #define LOCLIST_HEADER_SIZE32 12
144
145 /* Size of .debug_loclists section header for 64-bit DWARF format. */
146 #define LOCLIST_HEADER_SIZE64 20
147
148 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
149 #define RNGLIST_HEADER_SIZE32 12
150
151 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
152 #define RNGLIST_HEADER_SIZE64 20
153
154 /* An index into a (C++) symbol name component in a symbol name as
155 recorded in the mapped_index's symbol table. For each C++ symbol
156 in the symbol table, we record one entry for the start of each
157 component in the symbol in a table of name components, and then
158 sort the table, in order to be able to binary search symbol names,
159 ignoring leading namespaces, both completion and regular look up.
160 For example, for symbol "A::B::C", we'll have an entry that points
161 to "A::B::C", another that points to "B::C", and another for "C".
162 Note that function symbols in GDB index have no parameter
163 information, just the function/method names. You can convert a
164 name_component to a "const char *" using the
165 'mapped_index::symbol_name_at(offset_type)' method. */
166
167 struct name_component
168 {
169 /* Offset in the symbol name where the component starts. Stored as
170 a (32-bit) offset instead of a pointer to save memory and improve
171 locality on 64-bit architectures. */
172 offset_type name_offset;
173
174 /* The symbol's index in the symbol and constant pool tables of a
175 mapped_index. */
176 offset_type idx;
177 };
178
179 /* Base class containing bits shared by both .gdb_index and
180 .debug_name indexes. */
181
182 struct mapped_index_base
183 {
184 mapped_index_base () = default;
185 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
186
187 /* The name_component table (a sorted vector). See name_component's
188 description above. */
189 std::vector<name_component> name_components;
190
191 /* How NAME_COMPONENTS is sorted. */
192 enum case_sensitivity name_components_casing;
193
194 /* Return the number of names in the symbol table. */
195 virtual size_t symbol_name_count () const = 0;
196
197 /* Get the name of the symbol at IDX in the symbol table. */
198 virtual const char *symbol_name_at
199 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
200
201 /* Return whether the name at IDX in the symbol table should be
202 ignored. */
203 virtual bool symbol_name_slot_invalid (offset_type idx) const
204 {
205 return false;
206 }
207
208 /* Build the symbol name component sorted vector, if we haven't
209 yet. */
210 void build_name_components (dwarf2_per_objfile *per_objfile);
211
212 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
213 possible matches for LN_NO_PARAMS in the name component
214 vector. */
215 std::pair<std::vector<name_component>::const_iterator,
216 std::vector<name_component>::const_iterator>
217 find_name_components_bounds (const lookup_name_info &ln_no_params,
218 enum language lang,
219 dwarf2_per_objfile *per_objfile) const;
220
221 /* Prevent deleting/destroying via a base class pointer. */
222 protected:
223 ~mapped_index_base() = default;
224 };
225
226 /* A description of the mapped index. The file format is described in
227 a comment by the code that writes the index. */
228 struct mapped_index final : public mapped_index_base
229 {
230 /* A slot/bucket in the symbol table hash. */
231 struct symbol_table_slot
232 {
233 const offset_type name;
234 const offset_type vec;
235 };
236
237 /* Index data format version. */
238 int version = 0;
239
240 /* The address table data. */
241 gdb::array_view<const gdb_byte> address_table;
242
243 /* The symbol table, implemented as a hash table. */
244 gdb::array_view<symbol_table_slot> symbol_table;
245
246 /* A pointer to the constant pool. */
247 const char *constant_pool = nullptr;
248
249 bool symbol_name_slot_invalid (offset_type idx) const override
250 {
251 const auto &bucket = this->symbol_table[idx];
252 return bucket.name == 0 && bucket.vec == 0;
253 }
254
255 /* Convenience method to get at the name of the symbol at IDX in the
256 symbol table. */
257 const char *symbol_name_at
258 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
259 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
260
261 size_t symbol_name_count () const override
262 { return this->symbol_table.size (); }
263 };
264
265 /* A description of the mapped .debug_names.
266 Uninitialized map has CU_COUNT 0. */
267 struct mapped_debug_names final : public mapped_index_base
268 {
269 bfd_endian dwarf5_byte_order;
270 bool dwarf5_is_dwarf64;
271 bool augmentation_is_gdb;
272 uint8_t offset_size;
273 uint32_t cu_count = 0;
274 uint32_t tu_count, bucket_count, name_count;
275 const gdb_byte *cu_table_reordered, *tu_table_reordered;
276 const uint32_t *bucket_table_reordered, *hash_table_reordered;
277 const gdb_byte *name_table_string_offs_reordered;
278 const gdb_byte *name_table_entry_offs_reordered;
279 const gdb_byte *entry_pool;
280
281 struct index_val
282 {
283 ULONGEST dwarf_tag;
284 struct attr
285 {
286 /* Attribute name DW_IDX_*. */
287 ULONGEST dw_idx;
288
289 /* Attribute form DW_FORM_*. */
290 ULONGEST form;
291
292 /* Value if FORM is DW_FORM_implicit_const. */
293 LONGEST implicit_const;
294 };
295 std::vector<attr> attr_vec;
296 };
297
298 std::unordered_map<ULONGEST, index_val> abbrev_map;
299
300 const char *namei_to_name
301 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
302
303 /* Implementation of the mapped_index_base virtual interface, for
304 the name_components cache. */
305
306 const char *symbol_name_at
307 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
308 { return namei_to_name (idx, per_objfile); }
309
310 size_t symbol_name_count () const override
311 { return this->name_count; }
312 };
313
314 /* See dwarf2read.h. */
315
316 dwarf2_per_objfile *
317 get_dwarf2_per_objfile (struct objfile *objfile)
318 {
319 return dwarf2_objfile_data_key.get (objfile);
320 }
321
322 /* Default names of the debugging sections. */
323
324 /* Note that if the debugging section has been compressed, it might
325 have a name like .zdebug_info. */
326
327 const struct dwarf2_debug_sections dwarf2_elf_names =
328 {
329 { ".debug_info", ".zdebug_info" },
330 { ".debug_abbrev", ".zdebug_abbrev" },
331 { ".debug_line", ".zdebug_line" },
332 { ".debug_loc", ".zdebug_loc" },
333 { ".debug_loclists", ".zdebug_loclists" },
334 { ".debug_macinfo", ".zdebug_macinfo" },
335 { ".debug_macro", ".zdebug_macro" },
336 { ".debug_str", ".zdebug_str" },
337 { ".debug_str_offsets", ".zdebug_str_offsets" },
338 { ".debug_line_str", ".zdebug_line_str" },
339 { ".debug_ranges", ".zdebug_ranges" },
340 { ".debug_rnglists", ".zdebug_rnglists" },
341 { ".debug_types", ".zdebug_types" },
342 { ".debug_addr", ".zdebug_addr" },
343 { ".debug_frame", ".zdebug_frame" },
344 { ".eh_frame", NULL },
345 { ".gdb_index", ".zgdb_index" },
346 { ".debug_names", ".zdebug_names" },
347 { ".debug_aranges", ".zdebug_aranges" },
348 23
349 };
350
351 /* List of DWO/DWP sections. */
352
353 static const struct dwop_section_names
354 {
355 struct dwarf2_section_names abbrev_dwo;
356 struct dwarf2_section_names info_dwo;
357 struct dwarf2_section_names line_dwo;
358 struct dwarf2_section_names loc_dwo;
359 struct dwarf2_section_names loclists_dwo;
360 struct dwarf2_section_names macinfo_dwo;
361 struct dwarf2_section_names macro_dwo;
362 struct dwarf2_section_names rnglists_dwo;
363 struct dwarf2_section_names str_dwo;
364 struct dwarf2_section_names str_offsets_dwo;
365 struct dwarf2_section_names types_dwo;
366 struct dwarf2_section_names cu_index;
367 struct dwarf2_section_names tu_index;
368 }
369 dwop_section_names =
370 {
371 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
372 { ".debug_info.dwo", ".zdebug_info.dwo" },
373 { ".debug_line.dwo", ".zdebug_line.dwo" },
374 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
375 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
376 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
377 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
378 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
379 { ".debug_str.dwo", ".zdebug_str.dwo" },
380 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
381 { ".debug_types.dwo", ".zdebug_types.dwo" },
382 { ".debug_cu_index", ".zdebug_cu_index" },
383 { ".debug_tu_index", ".zdebug_tu_index" },
384 };
385
386 /* local data types */
387
388 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
389 begin with a header, which contains the following information. */
390 struct loclists_rnglists_header
391 {
392 /* A 4-byte or 12-byte length containing the length of the
393 set of entries for this compilation unit, not including the
394 length field itself. */
395 unsigned int length;
396
397 /* A 2-byte version identifier. */
398 short version;
399
400 /* A 1-byte unsigned integer containing the size in bytes of an address on
401 the target system. */
402 unsigned char addr_size;
403
404 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
405 on the target system. */
406 unsigned char segment_collector_size;
407
408 /* A 4-byte count of the number of offsets that follow the header. */
409 unsigned int offset_entry_count;
410 };
411
412 /* Type used for delaying computation of method physnames.
413 See comments for compute_delayed_physnames. */
414 struct delayed_method_info
415 {
416 /* The type to which the method is attached, i.e., its parent class. */
417 struct type *type;
418
419 /* The index of the method in the type's function fieldlists. */
420 int fnfield_index;
421
422 /* The index of the method in the fieldlist. */
423 int index;
424
425 /* The name of the DIE. */
426 const char *name;
427
428 /* The DIE associated with this method. */
429 struct die_info *die;
430 };
431
432 /* Internal state when decoding a particular compilation unit. */
433 struct dwarf2_cu
434 {
435 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
436 dwarf2_per_objfile *per_objfile);
437
438 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
439
440 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
441 Create the set of symtabs used by this TU, or if this TU is sharing
442 symtabs with another TU and the symtabs have already been created
443 then restore those symtabs in the line header.
444 We don't need the pc/line-number mapping for type units. */
445 void setup_type_unit_groups (struct die_info *die);
446
447 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
448 buildsym_compunit constructor. */
449 struct compunit_symtab *start_symtab (const char *name,
450 const char *comp_dir,
451 CORE_ADDR low_pc);
452
453 /* Reset the builder. */
454 void reset_builder () { m_builder.reset (); }
455
456 /* Return a type that is a generic pointer type, the size of which
457 matches the address size given in the compilation unit header for
458 this CU. */
459 struct type *addr_type () const;
460
461 /* Find an integer type the same size as the address size given in
462 the compilation unit header for this CU. UNSIGNED_P controls if
463 the integer is unsigned or not. */
464 struct type *addr_sized_int_type (bool unsigned_p) const;
465
466 /* The header of the compilation unit. */
467 struct comp_unit_head header {};
468
469 /* Base address of this compilation unit. */
470 gdb::optional<CORE_ADDR> base_address;
471
472 /* The language we are debugging. */
473 enum language language = language_unknown;
474 const struct language_defn *language_defn = nullptr;
475
476 const char *producer = nullptr;
477
478 private:
479 /* The symtab builder for this CU. This is only non-NULL when full
480 symbols are being read. */
481 std::unique_ptr<buildsym_compunit> m_builder;
482
483 public:
484 /* The generic symbol table building routines have separate lists for
485 file scope symbols and all all other scopes (local scopes). So
486 we need to select the right one to pass to add_symbol_to_list().
487 We do it by keeping a pointer to the correct list in list_in_scope.
488
489 FIXME: The original dwarf code just treated the file scope as the
490 first local scope, and all other local scopes as nested local
491 scopes, and worked fine. Check to see if we really need to
492 distinguish these in buildsym.c. */
493 struct pending **list_in_scope = nullptr;
494
495 /* Hash table holding all the loaded partial DIEs
496 with partial_die->offset.SECT_OFF as hash. */
497 htab_t partial_dies = nullptr;
498
499 /* Storage for things with the same lifetime as this read-in compilation
500 unit, including partial DIEs. */
501 auto_obstack comp_unit_obstack;
502
503 /* Backlink to our per_cu entry. */
504 struct dwarf2_per_cu_data *per_cu;
505
506 /* The dwarf2_per_objfile that owns this. */
507 dwarf2_per_objfile *per_objfile;
508
509 /* How many compilation units ago was this CU last referenced? */
510 int last_used = 0;
511
512 /* A hash table of DIE cu_offset for following references with
513 die_info->offset.sect_off as hash. */
514 htab_t die_hash = nullptr;
515
516 /* Full DIEs if read in. */
517 struct die_info *dies = nullptr;
518
519 /* A set of pointers to dwarf2_per_cu_data objects for compilation
520 units referenced by this one. Only set during full symbol processing;
521 partial symbol tables do not have dependencies. */
522 htab_t dependencies = nullptr;
523
524 /* Header data from the line table, during full symbol processing. */
525 struct line_header *line_header = nullptr;
526 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
527 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
528 this is the DW_TAG_compile_unit die for this CU. We'll hold on
529 to the line header as long as this DIE is being processed. See
530 process_die_scope. */
531 die_info *line_header_die_owner = nullptr;
532
533 /* A list of methods which need to have physnames computed
534 after all type information has been read. */
535 std::vector<delayed_method_info> method_list;
536
537 /* To be copied to symtab->call_site_htab. */
538 htab_t call_site_htab = nullptr;
539
540 /* Non-NULL if this CU came from a DWO file.
541 There is an invariant here that is important to remember:
542 Except for attributes copied from the top level DIE in the "main"
543 (or "stub") file in preparation for reading the DWO file
544 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
545 Either there isn't a DWO file (in which case this is NULL and the point
546 is moot), or there is and either we're not going to read it (in which
547 case this is NULL) or there is and we are reading it (in which case this
548 is non-NULL). */
549 struct dwo_unit *dwo_unit = nullptr;
550
551 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
552 Note this value comes from the Fission stub CU/TU's DIE. */
553 gdb::optional<ULONGEST> addr_base;
554
555 /* The DW_AT_GNU_ranges_base attribute, if present.
556
557 This is only relevant in the context of pre-DWARF 5 split units. In this
558 context, there is a .debug_ranges section in the linked executable,
559 containing all the ranges data for all the compilation units. Each
560 skeleton/stub unit has (if needed) a DW_AT_GNU_ranges_base attribute that
561 indicates the base of its contribution to that section. The DW_AT_ranges
562 attributes in the split-unit are of the form DW_FORM_sec_offset and point
563 into the .debug_ranges section of the linked file. However, they are not
564 "true" DW_FORM_sec_offset, because they are relative to the base of their
565 compilation unit's contribution, rather than relative to the beginning of
566 the section. The DW_AT_GNU_ranges_base value must be added to it to make
567 it relative to the beginning of the section.
568
569 Note that the value is zero when we are not in a pre-DWARF 5 split-unit
570 case, so this value can be added without needing to know whether we are in
571 this case or not.
572
573 N.B. If a DW_AT_ranges attribute is found on the DW_TAG_compile_unit in the
574 skeleton/stub, it must not have the base added, as it already points to the
575 right place. And since the DW_TAG_compile_unit DIE in the split-unit can't
576 have a DW_AT_ranges attribute, we can use the
577
578 die->tag != DW_AT_compile_unit
579
580 to determine whether the base should be added or not. */
581 ULONGEST gnu_ranges_base = 0;
582
583 /* The DW_AT_rnglists_base attribute, if present.
584
585 This is used when processing attributes of form DW_FORM_rnglistx in
586 non-split units. Attributes of this form found in a split unit don't
587 use it, as split-unit files have their own non-shared .debug_rnglists.dwo
588 section. */
589 ULONGEST rnglists_base = 0;
590
591 /* The DW_AT_loclists_base attribute if present. */
592 ULONGEST loclist_base = 0;
593
594 /* When reading debug info generated by older versions of rustc, we
595 have to rewrite some union types to be struct types with a
596 variant part. This rewriting must be done after the CU is fully
597 read in, because otherwise at the point of rewriting some struct
598 type might not have been fully processed. So, we keep a list of
599 all such types here and process them after expansion. */
600 std::vector<struct type *> rust_unions;
601
602 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
603 files, the value is implicitly zero. For DWARF 5 version DWO files, the
604 value is often implicit and is the size of the header of
605 .debug_str_offsets section (8 or 4, depending on the address size). */
606 gdb::optional<ULONGEST> str_offsets_base;
607
608 /* Mark used when releasing cached dies. */
609 bool mark : 1;
610
611 /* This CU references .debug_loc. See the symtab->locations_valid field.
612 This test is imperfect as there may exist optimized debug code not using
613 any location list and still facing inlining issues if handled as
614 unoptimized code. For a future better test see GCC PR other/32998. */
615 bool has_loclist : 1;
616
617 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
618 if all the producer_is_* fields are valid. This information is cached
619 because profiling CU expansion showed excessive time spent in
620 producer_is_gxx_lt_4_6. */
621 bool checked_producer : 1;
622 bool producer_is_gxx_lt_4_6 : 1;
623 bool producer_is_gcc_lt_4_3 : 1;
624 bool producer_is_icc : 1;
625 bool producer_is_icc_lt_14 : 1;
626 bool producer_is_codewarrior : 1;
627
628 /* When true, the file that we're processing is known to have
629 debugging info for C++ namespaces. GCC 3.3.x did not produce
630 this information, but later versions do. */
631
632 bool processing_has_namespace_info : 1;
633
634 struct partial_die_info *find_partial_die (sect_offset sect_off);
635
636 /* If this CU was inherited by another CU (via specification,
637 abstract_origin, etc), this is the ancestor CU. */
638 dwarf2_cu *ancestor;
639
640 /* Get the buildsym_compunit for this CU. */
641 buildsym_compunit *get_builder ()
642 {
643 /* If this CU has a builder associated with it, use that. */
644 if (m_builder != nullptr)
645 return m_builder.get ();
646
647 /* Otherwise, search ancestors for a valid builder. */
648 if (ancestor != nullptr)
649 return ancestor->get_builder ();
650
651 return nullptr;
652 }
653 };
654
655 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
656 This includes type_unit_group and quick_file_names. */
657
658 struct stmt_list_hash
659 {
660 /* The DWO unit this table is from or NULL if there is none. */
661 struct dwo_unit *dwo_unit;
662
663 /* Offset in .debug_line or .debug_line.dwo. */
664 sect_offset line_sect_off;
665 };
666
667 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
668 an object of this type. This contains elements of type unit groups
669 that can be shared across objfiles. The non-shareable parts are in
670 type_unit_group_unshareable. */
671
672 struct type_unit_group
673 {
674 /* dwarf2read.c's main "handle" on a TU symtab.
675 To simplify things we create an artificial CU that "includes" all the
676 type units using this stmt_list so that the rest of the code still has
677 a "per_cu" handle on the symtab. */
678 struct dwarf2_per_cu_data per_cu;
679
680 /* The TUs that share this DW_AT_stmt_list entry.
681 This is added to while parsing type units to build partial symtabs,
682 and is deleted afterwards and not used again. */
683 std::vector<signatured_type *> *tus;
684
685 /* The data used to construct the hash key. */
686 struct stmt_list_hash hash;
687 };
688
689 /* These sections are what may appear in a (real or virtual) DWO file. */
690
691 struct dwo_sections
692 {
693 struct dwarf2_section_info abbrev;
694 struct dwarf2_section_info line;
695 struct dwarf2_section_info loc;
696 struct dwarf2_section_info loclists;
697 struct dwarf2_section_info macinfo;
698 struct dwarf2_section_info macro;
699 struct dwarf2_section_info rnglists;
700 struct dwarf2_section_info str;
701 struct dwarf2_section_info str_offsets;
702 /* In the case of a virtual DWO file, these two are unused. */
703 struct dwarf2_section_info info;
704 std::vector<dwarf2_section_info> types;
705 };
706
707 /* CUs/TUs in DWP/DWO files. */
708
709 struct dwo_unit
710 {
711 /* Backlink to the containing struct dwo_file. */
712 struct dwo_file *dwo_file;
713
714 /* The "id" that distinguishes this CU/TU.
715 .debug_info calls this "dwo_id", .debug_types calls this "signature".
716 Since signatures came first, we stick with it for consistency. */
717 ULONGEST signature;
718
719 /* The section this CU/TU lives in, in the DWO file. */
720 struct dwarf2_section_info *section;
721
722 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
723 sect_offset sect_off;
724 unsigned int length;
725
726 /* For types, offset in the type's DIE of the type defined by this TU. */
727 cu_offset type_offset_in_tu;
728 };
729
730 /* include/dwarf2.h defines the DWP section codes.
731 It defines a max value but it doesn't define a min value, which we
732 use for error checking, so provide one. */
733
734 enum dwp_v2_section_ids
735 {
736 DW_SECT_MIN = 1
737 };
738
739 /* Data for one DWO file.
740
741 This includes virtual DWO files (a virtual DWO file is a DWO file as it
742 appears in a DWP file). DWP files don't really have DWO files per se -
743 comdat folding of types "loses" the DWO file they came from, and from
744 a high level view DWP files appear to contain a mass of random types.
745 However, to maintain consistency with the non-DWP case we pretend DWP
746 files contain virtual DWO files, and we assign each TU with one virtual
747 DWO file (generally based on the line and abbrev section offsets -
748 a heuristic that seems to work in practice). */
749
750 struct dwo_file
751 {
752 dwo_file () = default;
753 DISABLE_COPY_AND_ASSIGN (dwo_file);
754
755 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
756 For virtual DWO files the name is constructed from the section offsets
757 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
758 from related CU+TUs. */
759 const char *dwo_name = nullptr;
760
761 /* The DW_AT_comp_dir attribute. */
762 const char *comp_dir = nullptr;
763
764 /* The bfd, when the file is open. Otherwise this is NULL.
765 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
766 gdb_bfd_ref_ptr dbfd;
767
768 /* The sections that make up this DWO file.
769 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
770 sections (for lack of a better name). */
771 struct dwo_sections sections {};
772
773 /* The CUs in the file.
774 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
775 an extension to handle LLVM's Link Time Optimization output (where
776 multiple source files may be compiled into a single object/dwo pair). */
777 htab_up cus;
778
779 /* Table of TUs in the file.
780 Each element is a struct dwo_unit. */
781 htab_up tus;
782 };
783
784 /* These sections are what may appear in a DWP file. */
785
786 struct dwp_sections
787 {
788 /* These are used by all DWP versions (1, 2 and 5). */
789 struct dwarf2_section_info str;
790 struct dwarf2_section_info cu_index;
791 struct dwarf2_section_info tu_index;
792
793 /* These are only used by DWP version 2 and version 5 files.
794 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
795 sections are referenced by section number, and are not recorded here.
796 In DWP version 2 or 5 there is at most one copy of all these sections,
797 each section being (effectively) comprised of the concatenation of all of
798 the individual sections that exist in the version 1 format.
799 To keep the code simple we treat each of these concatenated pieces as a
800 section itself (a virtual section?). */
801 struct dwarf2_section_info abbrev;
802 struct dwarf2_section_info info;
803 struct dwarf2_section_info line;
804 struct dwarf2_section_info loc;
805 struct dwarf2_section_info loclists;
806 struct dwarf2_section_info macinfo;
807 struct dwarf2_section_info macro;
808 struct dwarf2_section_info rnglists;
809 struct dwarf2_section_info str_offsets;
810 struct dwarf2_section_info types;
811 };
812
813 /* These sections are what may appear in a virtual DWO file in DWP version 1.
814 A virtual DWO file is a DWO file as it appears in a DWP file. */
815
816 struct virtual_v1_dwo_sections
817 {
818 struct dwarf2_section_info abbrev;
819 struct dwarf2_section_info line;
820 struct dwarf2_section_info loc;
821 struct dwarf2_section_info macinfo;
822 struct dwarf2_section_info macro;
823 struct dwarf2_section_info str_offsets;
824 /* Each DWP hash table entry records one CU or one TU.
825 That is recorded here, and copied to dwo_unit.section. */
826 struct dwarf2_section_info info_or_types;
827 };
828
829 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
830 In version 2, the sections of the DWO files are concatenated together
831 and stored in one section of that name. Thus each ELF section contains
832 several "virtual" sections. */
833
834 struct virtual_v2_or_v5_dwo_sections
835 {
836 bfd_size_type abbrev_offset;
837 bfd_size_type abbrev_size;
838
839 bfd_size_type line_offset;
840 bfd_size_type line_size;
841
842 bfd_size_type loc_offset;
843 bfd_size_type loc_size;
844
845 bfd_size_type loclists_offset;
846 bfd_size_type loclists_size;
847
848 bfd_size_type macinfo_offset;
849 bfd_size_type macinfo_size;
850
851 bfd_size_type macro_offset;
852 bfd_size_type macro_size;
853
854 bfd_size_type rnglists_offset;
855 bfd_size_type rnglists_size;
856
857 bfd_size_type str_offsets_offset;
858 bfd_size_type str_offsets_size;
859
860 /* Each DWP hash table entry records one CU or one TU.
861 That is recorded here, and copied to dwo_unit.section. */
862 bfd_size_type info_or_types_offset;
863 bfd_size_type info_or_types_size;
864 };
865
866 /* Contents of DWP hash tables. */
867
868 struct dwp_hash_table
869 {
870 uint32_t version, nr_columns;
871 uint32_t nr_units, nr_slots;
872 const gdb_byte *hash_table, *unit_table;
873 union
874 {
875 struct
876 {
877 const gdb_byte *indices;
878 } v1;
879 struct
880 {
881 /* This is indexed by column number and gives the id of the section
882 in that column. */
883 #define MAX_NR_V2_DWO_SECTIONS \
884 (1 /* .debug_info or .debug_types */ \
885 + 1 /* .debug_abbrev */ \
886 + 1 /* .debug_line */ \
887 + 1 /* .debug_loc */ \
888 + 1 /* .debug_str_offsets */ \
889 + 1 /* .debug_macro or .debug_macinfo */)
890 int section_ids[MAX_NR_V2_DWO_SECTIONS];
891 const gdb_byte *offsets;
892 const gdb_byte *sizes;
893 } v2;
894 struct
895 {
896 /* This is indexed by column number and gives the id of the section
897 in that column. */
898 #define MAX_NR_V5_DWO_SECTIONS \
899 (1 /* .debug_info */ \
900 + 1 /* .debug_abbrev */ \
901 + 1 /* .debug_line */ \
902 + 1 /* .debug_loclists */ \
903 + 1 /* .debug_str_offsets */ \
904 + 1 /* .debug_macro */ \
905 + 1 /* .debug_rnglists */)
906 int section_ids[MAX_NR_V5_DWO_SECTIONS];
907 const gdb_byte *offsets;
908 const gdb_byte *sizes;
909 } v5;
910 } section_pool;
911 };
912
913 /* Data for one DWP file. */
914
915 struct dwp_file
916 {
917 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
918 : name (name_),
919 dbfd (std::move (abfd))
920 {
921 }
922
923 /* Name of the file. */
924 const char *name;
925
926 /* File format version. */
927 int version = 0;
928
929 /* The bfd. */
930 gdb_bfd_ref_ptr dbfd;
931
932 /* Section info for this file. */
933 struct dwp_sections sections {};
934
935 /* Table of CUs in the file. */
936 const struct dwp_hash_table *cus = nullptr;
937
938 /* Table of TUs in the file. */
939 const struct dwp_hash_table *tus = nullptr;
940
941 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
942 htab_up loaded_cus;
943 htab_up loaded_tus;
944
945 /* Table to map ELF section numbers to their sections.
946 This is only needed for the DWP V1 file format. */
947 unsigned int num_sections = 0;
948 asection **elf_sections = nullptr;
949 };
950
951 /* Struct used to pass misc. parameters to read_die_and_children, et
952 al. which are used for both .debug_info and .debug_types dies.
953 All parameters here are unchanging for the life of the call. This
954 struct exists to abstract away the constant parameters of die reading. */
955
956 struct die_reader_specs
957 {
958 /* The bfd of die_section. */
959 bfd *abfd;
960
961 /* The CU of the DIE we are parsing. */
962 struct dwarf2_cu *cu;
963
964 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
965 struct dwo_file *dwo_file;
966
967 /* The section the die comes from.
968 This is either .debug_info or .debug_types, or the .dwo variants. */
969 struct dwarf2_section_info *die_section;
970
971 /* die_section->buffer. */
972 const gdb_byte *buffer;
973
974 /* The end of the buffer. */
975 const gdb_byte *buffer_end;
976
977 /* The abbreviation table to use when reading the DIEs. */
978 struct abbrev_table *abbrev_table;
979 };
980
981 /* A subclass of die_reader_specs that holds storage and has complex
982 constructor and destructor behavior. */
983
984 class cutu_reader : public die_reader_specs
985 {
986 public:
987
988 cutu_reader (dwarf2_per_cu_data *this_cu,
989 dwarf2_per_objfile *per_objfile,
990 struct abbrev_table *abbrev_table,
991 dwarf2_cu *existing_cu,
992 bool skip_partial);
993
994 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
995 dwarf2_per_objfile *per_objfile,
996 struct dwarf2_cu *parent_cu = nullptr,
997 struct dwo_file *dwo_file = nullptr);
998
999 DISABLE_COPY_AND_ASSIGN (cutu_reader);
1000
1001 const gdb_byte *info_ptr = nullptr;
1002 struct die_info *comp_unit_die = nullptr;
1003 bool dummy_p = false;
1004
1005 /* Release the new CU, putting it on the chain. This cannot be done
1006 for dummy CUs. */
1007 void keep ();
1008
1009 private:
1010 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
1011 dwarf2_per_objfile *per_objfile,
1012 dwarf2_cu *existing_cu);
1013
1014 struct dwarf2_per_cu_data *m_this_cu;
1015 std::unique_ptr<dwarf2_cu> m_new_cu;
1016
1017 /* The ordinary abbreviation table. */
1018 abbrev_table_up m_abbrev_table_holder;
1019
1020 /* The DWO abbreviation table. */
1021 abbrev_table_up m_dwo_abbrev_table;
1022 };
1023
1024 /* When we construct a partial symbol table entry we only
1025 need this much information. */
1026 struct partial_die_info : public allocate_on_obstack
1027 {
1028 partial_die_info (sect_offset sect_off, const struct abbrev_info *abbrev);
1029
1030 /* Disable assign but still keep copy ctor, which is needed
1031 load_partial_dies. */
1032 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1033
1034 /* Adjust the partial die before generating a symbol for it. This
1035 function may set the is_external flag or change the DIE's
1036 name. */
1037 void fixup (struct dwarf2_cu *cu);
1038
1039 /* Read a minimal amount of information into the minimal die
1040 structure. */
1041 const gdb_byte *read (const struct die_reader_specs *reader,
1042 const struct abbrev_info &abbrev,
1043 const gdb_byte *info_ptr);
1044
1045 /* Compute the name of this partial DIE. This memoizes the
1046 result, so it is safe to call multiple times. */
1047 const char *name (dwarf2_cu *cu);
1048
1049 /* Offset of this DIE. */
1050 const sect_offset sect_off;
1051
1052 /* DWARF-2 tag for this DIE. */
1053 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1054
1055 /* Assorted flags describing the data found in this DIE. */
1056 const unsigned int has_children : 1;
1057
1058 unsigned int is_external : 1;
1059 unsigned int is_declaration : 1;
1060 unsigned int has_type : 1;
1061 unsigned int has_specification : 1;
1062 unsigned int has_pc_info : 1;
1063 unsigned int may_be_inlined : 1;
1064
1065 /* This DIE has been marked DW_AT_main_subprogram. */
1066 unsigned int main_subprogram : 1;
1067
1068 /* Flag set if the SCOPE field of this structure has been
1069 computed. */
1070 unsigned int scope_set : 1;
1071
1072 /* Flag set if the DIE has a byte_size attribute. */
1073 unsigned int has_byte_size : 1;
1074
1075 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1076 unsigned int has_const_value : 1;
1077
1078 /* Flag set if any of the DIE's children are template arguments. */
1079 unsigned int has_template_arguments : 1;
1080
1081 /* Flag set if fixup has been called on this die. */
1082 unsigned int fixup_called : 1;
1083
1084 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1085 unsigned int is_dwz : 1;
1086
1087 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1088 unsigned int spec_is_dwz : 1;
1089
1090 unsigned int canonical_name : 1;
1091
1092 /* The name of this DIE. Normally the value of DW_AT_name, but
1093 sometimes a default name for unnamed DIEs. */
1094 const char *raw_name = nullptr;
1095
1096 /* The linkage name, if present. */
1097 const char *linkage_name = nullptr;
1098
1099 /* The scope to prepend to our children. This is generally
1100 allocated on the comp_unit_obstack, so will disappear
1101 when this compilation unit leaves the cache. */
1102 const char *scope = nullptr;
1103
1104 /* Some data associated with the partial DIE. The tag determines
1105 which field is live. */
1106 union
1107 {
1108 /* The location description associated with this DIE, if any. */
1109 struct dwarf_block *locdesc;
1110 /* The offset of an import, for DW_TAG_imported_unit. */
1111 sect_offset sect_off;
1112 } d {};
1113
1114 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1115 CORE_ADDR lowpc = 0;
1116 CORE_ADDR highpc = 0;
1117
1118 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1119 DW_AT_sibling, if any. */
1120 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1121 could return DW_AT_sibling values to its caller load_partial_dies. */
1122 const gdb_byte *sibling = nullptr;
1123
1124 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1125 DW_AT_specification (or DW_AT_abstract_origin or
1126 DW_AT_extension). */
1127 sect_offset spec_offset {};
1128
1129 /* Pointers to this DIE's parent, first child, and next sibling,
1130 if any. */
1131 struct partial_die_info *die_parent = nullptr;
1132 struct partial_die_info *die_child = nullptr;
1133 struct partial_die_info *die_sibling = nullptr;
1134
1135 friend struct partial_die_info *
1136 dwarf2_cu::find_partial_die (sect_offset sect_off);
1137
1138 private:
1139 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1140 partial_die_info (sect_offset sect_off)
1141 : partial_die_info (sect_off, DW_TAG_padding, 0)
1142 {
1143 }
1144
1145 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1146 int has_children_)
1147 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1148 {
1149 is_external = 0;
1150 is_declaration = 0;
1151 has_type = 0;
1152 has_specification = 0;
1153 has_pc_info = 0;
1154 may_be_inlined = 0;
1155 main_subprogram = 0;
1156 scope_set = 0;
1157 has_byte_size = 0;
1158 has_const_value = 0;
1159 has_template_arguments = 0;
1160 fixup_called = 0;
1161 is_dwz = 0;
1162 spec_is_dwz = 0;
1163 canonical_name = 0;
1164 }
1165 };
1166
1167 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1168 but this would require a corresponding change in unpack_field_as_long
1169 and friends. */
1170 static int bits_per_byte = 8;
1171
1172 struct variant_part_builder;
1173
1174 /* When reading a variant, we track a bit more information about the
1175 field, and store it in an object of this type. */
1176
1177 struct variant_field
1178 {
1179 int first_field = -1;
1180 int last_field = -1;
1181
1182 /* A variant can contain other variant parts. */
1183 std::vector<variant_part_builder> variant_parts;
1184
1185 /* If we see a DW_TAG_variant, then this will be set if this is the
1186 default branch. */
1187 bool default_branch = false;
1188 /* If we see a DW_AT_discr_value, then this will be the discriminant
1189 value. */
1190 ULONGEST discriminant_value = 0;
1191 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1192 data. */
1193 struct dwarf_block *discr_list_data = nullptr;
1194 };
1195
1196 /* This represents a DW_TAG_variant_part. */
1197
1198 struct variant_part_builder
1199 {
1200 /* The offset of the discriminant field. */
1201 sect_offset discriminant_offset {};
1202
1203 /* Variants that are direct children of this variant part. */
1204 std::vector<variant_field> variants;
1205
1206 /* True if we're currently reading a variant. */
1207 bool processing_variant = false;
1208 };
1209
1210 struct nextfield
1211 {
1212 int accessibility = 0;
1213 int virtuality = 0;
1214 /* Variant parts need to find the discriminant, which is a DIE
1215 reference. We track the section offset of each field to make
1216 this link. */
1217 sect_offset offset;
1218 struct field field {};
1219 };
1220
1221 struct fnfieldlist
1222 {
1223 const char *name = nullptr;
1224 std::vector<struct fn_field> fnfields;
1225 };
1226
1227 /* The routines that read and process dies for a C struct or C++ class
1228 pass lists of data member fields and lists of member function fields
1229 in an instance of a field_info structure, as defined below. */
1230 struct field_info
1231 {
1232 /* List of data member and baseclasses fields. */
1233 std::vector<struct nextfield> fields;
1234 std::vector<struct nextfield> baseclasses;
1235
1236 /* Set if the accessibility of one of the fields is not public. */
1237 bool non_public_fields = false;
1238
1239 /* Member function fieldlist array, contains name of possibly overloaded
1240 member function, number of overloaded member functions and a pointer
1241 to the head of the member function field chain. */
1242 std::vector<struct fnfieldlist> fnfieldlists;
1243
1244 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1245 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1246 std::vector<struct decl_field> typedef_field_list;
1247
1248 /* Nested types defined by this class and the number of elements in this
1249 list. */
1250 std::vector<struct decl_field> nested_types_list;
1251
1252 /* If non-null, this is the variant part we are currently
1253 reading. */
1254 variant_part_builder *current_variant_part = nullptr;
1255 /* This holds all the top-level variant parts attached to the type
1256 we're reading. */
1257 std::vector<variant_part_builder> variant_parts;
1258
1259 /* Return the total number of fields (including baseclasses). */
1260 int nfields () const
1261 {
1262 return fields.size () + baseclasses.size ();
1263 }
1264 };
1265
1266 /* Loaded secondary compilation units are kept in memory until they
1267 have not been referenced for the processing of this many
1268 compilation units. Set this to zero to disable caching. Cache
1269 sizes of up to at least twenty will improve startup time for
1270 typical inter-CU-reference binaries, at an obvious memory cost. */
1271 static int dwarf_max_cache_age = 5;
1272 static void
1273 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1274 struct cmd_list_element *c, const char *value)
1275 {
1276 fprintf_filtered (file, _("The upper bound on the age of cached "
1277 "DWARF compilation units is %s.\n"),
1278 value);
1279 }
1280 \f
1281 /* local function prototypes */
1282
1283 static void dwarf2_find_base_address (struct die_info *die,
1284 struct dwarf2_cu *cu);
1285
1286 static dwarf2_psymtab *create_partial_symtab
1287 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1288 const char *name);
1289
1290 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1291 const gdb_byte *info_ptr,
1292 struct die_info *type_unit_die);
1293
1294 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
1295
1296 static void scan_partial_symbols (struct partial_die_info *,
1297 CORE_ADDR *, CORE_ADDR *,
1298 int, struct dwarf2_cu *);
1299
1300 static void add_partial_symbol (struct partial_die_info *,
1301 struct dwarf2_cu *);
1302
1303 static void add_partial_namespace (struct partial_die_info *pdi,
1304 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1305 int set_addrmap, struct dwarf2_cu *cu);
1306
1307 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1308 CORE_ADDR *highpc, int set_addrmap,
1309 struct dwarf2_cu *cu);
1310
1311 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1312 struct dwarf2_cu *cu);
1313
1314 static void add_partial_subprogram (struct partial_die_info *pdi,
1315 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1316 int need_pc, struct dwarf2_cu *cu);
1317
1318 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1319
1320 static struct partial_die_info *load_partial_dies
1321 (const struct die_reader_specs *, const gdb_byte *, int);
1322
1323 /* A pair of partial_die_info and compilation unit. */
1324 struct cu_partial_die_info
1325 {
1326 /* The compilation unit of the partial_die_info. */
1327 struct dwarf2_cu *cu;
1328 /* A partial_die_info. */
1329 struct partial_die_info *pdi;
1330
1331 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1332 : cu (cu),
1333 pdi (pdi)
1334 { /* Nothing. */ }
1335
1336 private:
1337 cu_partial_die_info () = delete;
1338 };
1339
1340 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1341 struct dwarf2_cu *);
1342
1343 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1344 struct attribute *,
1345 const struct attr_abbrev *,
1346 const gdb_byte *);
1347
1348 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1349 struct attribute *attr, dwarf_tag tag);
1350
1351 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1352
1353 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1354 dwarf2_section_info *, sect_offset);
1355
1356 static const char *read_indirect_string
1357 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
1358 const struct comp_unit_head *, unsigned int *);
1359
1360 static const char *read_indirect_string_at_offset
1361 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
1362
1363 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1364 const gdb_byte *,
1365 unsigned int *);
1366
1367 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1368 ULONGEST str_index);
1369
1370 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1371 ULONGEST str_index);
1372
1373 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1374
1375 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1376 struct dwarf2_cu *);
1377
1378 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1379 struct dwarf2_cu *cu);
1380
1381 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1382
1383 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1384 struct dwarf2_cu *cu);
1385
1386 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1387
1388 static struct die_info *die_specification (struct die_info *die,
1389 struct dwarf2_cu **);
1390
1391 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1392 struct dwarf2_cu *cu);
1393
1394 static void dwarf_decode_lines (struct line_header *, const char *,
1395 struct dwarf2_cu *, dwarf2_psymtab *,
1396 CORE_ADDR, int decode_mapping);
1397
1398 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1399 const char *);
1400
1401 static struct symbol *new_symbol (struct die_info *, struct type *,
1402 struct dwarf2_cu *, struct symbol * = NULL);
1403
1404 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1405 struct dwarf2_cu *);
1406
1407 static void dwarf2_const_value_attr (const struct attribute *attr,
1408 struct type *type,
1409 const char *name,
1410 struct obstack *obstack,
1411 struct dwarf2_cu *cu, LONGEST *value,
1412 const gdb_byte **bytes,
1413 struct dwarf2_locexpr_baton **baton);
1414
1415 static struct type *read_subrange_index_type (struct die_info *die,
1416 struct dwarf2_cu *cu);
1417
1418 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1419
1420 static int need_gnat_info (struct dwarf2_cu *);
1421
1422 static struct type *die_descriptive_type (struct die_info *,
1423 struct dwarf2_cu *);
1424
1425 static void set_descriptive_type (struct type *, struct die_info *,
1426 struct dwarf2_cu *);
1427
1428 static struct type *die_containing_type (struct die_info *,
1429 struct dwarf2_cu *);
1430
1431 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1432 struct dwarf2_cu *);
1433
1434 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1435
1436 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1437
1438 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1439
1440 static char *typename_concat (struct obstack *obs, const char *prefix,
1441 const char *suffix, int physname,
1442 struct dwarf2_cu *cu);
1443
1444 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1445
1446 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1447
1448 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1449
1450 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1451
1452 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1453
1454 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1455
1456 /* Return the .debug_loclists section to use for cu. */
1457 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1458
1459 /* Return the .debug_rnglists section to use for cu. */
1460 static struct dwarf2_section_info *cu_debug_rnglists_section
1461 (struct dwarf2_cu *cu, dwarf_tag tag);
1462
1463 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1464 values. Keep the items ordered with increasing constraints compliance. */
1465 enum pc_bounds_kind
1466 {
1467 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1468 PC_BOUNDS_NOT_PRESENT,
1469
1470 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1471 were present but they do not form a valid range of PC addresses. */
1472 PC_BOUNDS_INVALID,
1473
1474 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1475 PC_BOUNDS_RANGES,
1476
1477 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1478 PC_BOUNDS_HIGH_LOW,
1479 };
1480
1481 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1482 CORE_ADDR *, CORE_ADDR *,
1483 struct dwarf2_cu *,
1484 dwarf2_psymtab *);
1485
1486 static void get_scope_pc_bounds (struct die_info *,
1487 CORE_ADDR *, CORE_ADDR *,
1488 struct dwarf2_cu *);
1489
1490 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1491 CORE_ADDR, struct dwarf2_cu *);
1492
1493 static void dwarf2_add_field (struct field_info *, struct die_info *,
1494 struct dwarf2_cu *);
1495
1496 static void dwarf2_attach_fields_to_type (struct field_info *,
1497 struct type *, struct dwarf2_cu *);
1498
1499 static void dwarf2_add_member_fn (struct field_info *,
1500 struct die_info *, struct type *,
1501 struct dwarf2_cu *);
1502
1503 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1504 struct type *,
1505 struct dwarf2_cu *);
1506
1507 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1508
1509 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1510
1511 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1512
1513 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1514
1515 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1516
1517 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1518
1519 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1520
1521 static struct type *read_module_type (struct die_info *die,
1522 struct dwarf2_cu *cu);
1523
1524 static const char *namespace_name (struct die_info *die,
1525 int *is_anonymous, struct dwarf2_cu *);
1526
1527 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1528
1529 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1530 bool * = nullptr);
1531
1532 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1533 struct dwarf2_cu *);
1534
1535 static struct die_info *read_die_and_siblings_1
1536 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1537 struct die_info *);
1538
1539 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1540 const gdb_byte *info_ptr,
1541 const gdb_byte **new_info_ptr,
1542 struct die_info *parent);
1543
1544 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1545 struct die_info **, const gdb_byte *,
1546 int);
1547
1548 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1549 struct die_info **, const gdb_byte *);
1550
1551 static void process_die (struct die_info *, struct dwarf2_cu *);
1552
1553 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1554 struct objfile *);
1555
1556 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1557
1558 static const char *dwarf2_full_name (const char *name,
1559 struct die_info *die,
1560 struct dwarf2_cu *cu);
1561
1562 static const char *dwarf2_physname (const char *name, struct die_info *die,
1563 struct dwarf2_cu *cu);
1564
1565 static struct die_info *dwarf2_extension (struct die_info *die,
1566 struct dwarf2_cu **);
1567
1568 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1569
1570 static void dump_die_for_error (struct die_info *);
1571
1572 static void dump_die_1 (struct ui_file *, int level, int max_level,
1573 struct die_info *);
1574
1575 /*static*/ void dump_die (struct die_info *, int max_level);
1576
1577 static void store_in_ref_table (struct die_info *,
1578 struct dwarf2_cu *);
1579
1580 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1581 const struct attribute *,
1582 struct dwarf2_cu **);
1583
1584 static struct die_info *follow_die_ref (struct die_info *,
1585 const struct attribute *,
1586 struct dwarf2_cu **);
1587
1588 static struct die_info *follow_die_sig (struct die_info *,
1589 const struct attribute *,
1590 struct dwarf2_cu **);
1591
1592 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1593 struct dwarf2_cu *);
1594
1595 static struct type *get_DW_AT_signature_type (struct die_info *,
1596 const struct attribute *,
1597 struct dwarf2_cu *);
1598
1599 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1600 dwarf2_per_objfile *per_objfile);
1601
1602 static void read_signatured_type (signatured_type *sig_type,
1603 dwarf2_per_objfile *per_objfile);
1604
1605 static int attr_to_dynamic_prop (const struct attribute *attr,
1606 struct die_info *die, struct dwarf2_cu *cu,
1607 struct dynamic_prop *prop, struct type *type);
1608
1609 /* memory allocation interface */
1610
1611 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1612
1613 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1614
1615 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1616
1617 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1618 struct dwarf2_loclist_baton *baton,
1619 const struct attribute *attr);
1620
1621 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1622 struct symbol *sym,
1623 struct dwarf2_cu *cu,
1624 int is_block);
1625
1626 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1627 const gdb_byte *info_ptr,
1628 const struct abbrev_info *abbrev);
1629
1630 static hashval_t partial_die_hash (const void *item);
1631
1632 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1633
1634 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1635 (sect_offset sect_off, unsigned int offset_in_dwz,
1636 dwarf2_per_objfile *per_objfile);
1637
1638 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1639 struct die_info *comp_unit_die,
1640 enum language pretend_language);
1641
1642 static struct type *set_die_type (struct die_info *, struct type *,
1643 struct dwarf2_cu *, bool = false);
1644
1645 static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
1646
1647 static int create_all_type_units (dwarf2_per_objfile *per_objfile);
1648
1649 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1650 dwarf2_per_objfile *per_objfile,
1651 dwarf2_cu *existing_cu,
1652 bool skip_partial,
1653 enum language pretend_language);
1654
1655 static void process_full_comp_unit (dwarf2_cu *cu,
1656 enum language pretend_language);
1657
1658 static void process_full_type_unit (dwarf2_cu *cu,
1659 enum language pretend_language);
1660
1661 static void dwarf2_add_dependence (struct dwarf2_cu *,
1662 struct dwarf2_per_cu_data *);
1663
1664 static void dwarf2_mark (struct dwarf2_cu *);
1665
1666 static struct type *get_die_type_at_offset (sect_offset,
1667 dwarf2_per_cu_data *per_cu,
1668 dwarf2_per_objfile *per_objfile);
1669
1670 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1671
1672 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1673 dwarf2_per_objfile *per_objfile,
1674 enum language pretend_language);
1675
1676 static void process_queue (dwarf2_per_objfile *per_objfile);
1677
1678 /* Class, the destructor of which frees all allocated queue entries. This
1679 will only have work to do if an error was thrown while processing the
1680 dwarf. If no error was thrown then the queue entries should have all
1681 been processed, and freed, as we went along. */
1682
1683 class dwarf2_queue_guard
1684 {
1685 public:
1686 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1687 : m_per_objfile (per_objfile)
1688 {
1689 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1690
1691 m_per_objfile->per_bfd->queue.emplace ();
1692 }
1693
1694 /* Free any entries remaining on the queue. There should only be
1695 entries left if we hit an error while processing the dwarf. */
1696 ~dwarf2_queue_guard ()
1697 {
1698 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1699
1700 m_per_objfile->per_bfd->queue.reset ();
1701 }
1702
1703 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1704
1705 private:
1706 dwarf2_per_objfile *m_per_objfile;
1707 };
1708
1709 dwarf2_queue_item::~dwarf2_queue_item ()
1710 {
1711 /* Anything still marked queued is likely to be in an
1712 inconsistent state, so discard it. */
1713 if (per_cu->queued)
1714 {
1715 per_objfile->remove_cu (per_cu);
1716 per_cu->queued = 0;
1717 }
1718 }
1719
1720 /* The return type of find_file_and_directory. Note, the enclosed
1721 string pointers are only valid while this object is valid. */
1722
1723 struct file_and_directory
1724 {
1725 /* The filename. This is never NULL. */
1726 const char *name;
1727
1728 /* The compilation directory. NULL if not known. If we needed to
1729 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1730 points directly to the DW_AT_comp_dir string attribute owned by
1731 the obstack that owns the DIE. */
1732 const char *comp_dir;
1733
1734 /* If we needed to build a new string for comp_dir, this is what
1735 owns the storage. */
1736 std::string comp_dir_storage;
1737 };
1738
1739 static file_and_directory find_file_and_directory (struct die_info *die,
1740 struct dwarf2_cu *cu);
1741
1742 static htab_up allocate_signatured_type_table ();
1743
1744 static htab_up allocate_dwo_unit_table ();
1745
1746 static struct dwo_unit *lookup_dwo_unit_in_dwp
1747 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1748 const char *comp_dir, ULONGEST signature, int is_debug_types);
1749
1750 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1751
1752 static struct dwo_unit *lookup_dwo_comp_unit
1753 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1754 ULONGEST signature);
1755
1756 static struct dwo_unit *lookup_dwo_type_unit
1757 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1758
1759 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1760
1761 /* A unique pointer to a dwo_file. */
1762
1763 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1764
1765 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1766
1767 static void check_producer (struct dwarf2_cu *cu);
1768
1769 static void free_line_header_voidp (void *arg);
1770 \f
1771 /* Various complaints about symbol reading that don't abort the process. */
1772
1773 static void
1774 dwarf2_debug_line_missing_file_complaint (void)
1775 {
1776 complaint (_(".debug_line section has line data without a file"));
1777 }
1778
1779 static void
1780 dwarf2_debug_line_missing_end_sequence_complaint (void)
1781 {
1782 complaint (_(".debug_line section has line "
1783 "program sequence without an end"));
1784 }
1785
1786 static void
1787 dwarf2_complex_location_expr_complaint (void)
1788 {
1789 complaint (_("location expression too complex"));
1790 }
1791
1792 static void
1793 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1794 int arg3)
1795 {
1796 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1797 arg1, arg2, arg3);
1798 }
1799
1800 static void
1801 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1802 {
1803 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1804 arg1, arg2);
1805 }
1806
1807 /* Hash function for line_header_hash. */
1808
1809 static hashval_t
1810 line_header_hash (const struct line_header *ofs)
1811 {
1812 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1813 }
1814
1815 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1816
1817 static hashval_t
1818 line_header_hash_voidp (const void *item)
1819 {
1820 const struct line_header *ofs = (const struct line_header *) item;
1821
1822 return line_header_hash (ofs);
1823 }
1824
1825 /* Equality function for line_header_hash. */
1826
1827 static int
1828 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1829 {
1830 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1831 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1832
1833 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1834 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1835 }
1836
1837 \f
1838
1839 /* See declaration. */
1840
1841 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1842 bool can_copy_)
1843 : obfd (obfd),
1844 can_copy (can_copy_)
1845 {
1846 if (names == NULL)
1847 names = &dwarf2_elf_names;
1848
1849 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1850 locate_sections (obfd, sec, *names);
1851 }
1852
1853 dwarf2_per_bfd::~dwarf2_per_bfd ()
1854 {
1855 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1856 per_cu->imported_symtabs_free ();
1857
1858 for (signatured_type *sig_type : all_type_units)
1859 sig_type->per_cu.imported_symtabs_free ();
1860
1861 /* Everything else should be on this->obstack. */
1862 }
1863
1864 /* See read.h. */
1865
1866 void
1867 dwarf2_per_objfile::remove_all_cus ()
1868 {
1869 gdb_assert (!this->per_bfd->queue.has_value ());
1870
1871 for (auto pair : m_dwarf2_cus)
1872 delete pair.second;
1873
1874 m_dwarf2_cus.clear ();
1875 }
1876
1877 /* A helper class that calls free_cached_comp_units on
1878 destruction. */
1879
1880 class free_cached_comp_units
1881 {
1882 public:
1883
1884 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1885 : m_per_objfile (per_objfile)
1886 {
1887 }
1888
1889 ~free_cached_comp_units ()
1890 {
1891 m_per_objfile->remove_all_cus ();
1892 }
1893
1894 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1895
1896 private:
1897
1898 dwarf2_per_objfile *m_per_objfile;
1899 };
1900
1901 /* See read.h. */
1902
1903 bool
1904 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1905 {
1906 gdb_assert (per_cu->index < this->m_symtabs.size ());
1907
1908 return this->m_symtabs[per_cu->index] != nullptr;
1909 }
1910
1911 /* See read.h. */
1912
1913 compunit_symtab *
1914 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1915 {
1916 gdb_assert (per_cu->index < this->m_symtabs.size ());
1917
1918 return this->m_symtabs[per_cu->index];
1919 }
1920
1921 /* See read.h. */
1922
1923 void
1924 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1925 compunit_symtab *symtab)
1926 {
1927 gdb_assert (per_cu->index < this->m_symtabs.size ());
1928 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1929
1930 this->m_symtabs[per_cu->index] = symtab;
1931 }
1932
1933 /* Try to locate the sections we need for DWARF 2 debugging
1934 information and return true if we have enough to do something.
1935 NAMES points to the dwarf2 section names, or is NULL if the standard
1936 ELF names are used. CAN_COPY is true for formats where symbol
1937 interposition is possible and so symbol values must follow copy
1938 relocation rules. */
1939
1940 int
1941 dwarf2_has_info (struct objfile *objfile,
1942 const struct dwarf2_debug_sections *names,
1943 bool can_copy)
1944 {
1945 if (objfile->flags & OBJF_READNEVER)
1946 return 0;
1947
1948 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1949
1950 if (per_objfile == NULL)
1951 {
1952 dwarf2_per_bfd *per_bfd;
1953
1954 /* We can share a "dwarf2_per_bfd" with other objfiles if the BFD
1955 doesn't require relocations and if there aren't partial symbols
1956 from some other reader. */
1957 if (!objfile_has_partial_symbols (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 \f
2153 /* DWARF quick_symbols_functions support. */
2154
2155 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2156 unique line tables, so we maintain a separate table of all .debug_line
2157 derived entries to support the sharing.
2158 All the quick functions need is the list of file names. We discard the
2159 line_header when we're done and don't need to record it here. */
2160 struct quick_file_names
2161 {
2162 /* The data used to construct the hash key. */
2163 struct stmt_list_hash hash;
2164
2165 /* The number of entries in file_names, real_names. */
2166 unsigned int num_file_names;
2167
2168 /* The file names from the line table, after being run through
2169 file_full_name. */
2170 const char **file_names;
2171
2172 /* The file names from the line table after being run through
2173 gdb_realpath. These are computed lazily. */
2174 const char **real_names;
2175 };
2176
2177 /* When using the index (and thus not using psymtabs), each CU has an
2178 object of this type. This is used to hold information needed by
2179 the various "quick" methods. */
2180 struct dwarf2_per_cu_quick_data
2181 {
2182 /* The file table. This can be NULL if there was no file table
2183 or it's currently not read in.
2184 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2185 struct quick_file_names *file_names;
2186
2187 /* A temporary mark bit used when iterating over all CUs in
2188 expand_symtabs_matching. */
2189 unsigned int mark : 1;
2190
2191 /* True if we've tried to read the file table and found there isn't one.
2192 There will be no point in trying to read it again next time. */
2193 unsigned int no_file_data : 1;
2194 };
2195
2196 /* Utility hash function for a stmt_list_hash. */
2197
2198 static hashval_t
2199 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2200 {
2201 hashval_t v = 0;
2202
2203 if (stmt_list_hash->dwo_unit != NULL)
2204 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2205 v += to_underlying (stmt_list_hash->line_sect_off);
2206 return v;
2207 }
2208
2209 /* Utility equality function for a stmt_list_hash. */
2210
2211 static int
2212 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2213 const struct stmt_list_hash *rhs)
2214 {
2215 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2216 return 0;
2217 if (lhs->dwo_unit != NULL
2218 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2219 return 0;
2220
2221 return lhs->line_sect_off == rhs->line_sect_off;
2222 }
2223
2224 /* Hash function for a quick_file_names. */
2225
2226 static hashval_t
2227 hash_file_name_entry (const void *e)
2228 {
2229 const struct quick_file_names *file_data
2230 = (const struct quick_file_names *) e;
2231
2232 return hash_stmt_list_entry (&file_data->hash);
2233 }
2234
2235 /* Equality function for a quick_file_names. */
2236
2237 static int
2238 eq_file_name_entry (const void *a, const void *b)
2239 {
2240 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2241 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2242
2243 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2244 }
2245
2246 /* Delete function for a quick_file_names. */
2247
2248 static void
2249 delete_file_name_entry (void *e)
2250 {
2251 struct quick_file_names *file_data = (struct quick_file_names *) e;
2252 int i;
2253
2254 for (i = 0; i < file_data->num_file_names; ++i)
2255 {
2256 xfree ((void*) file_data->file_names[i]);
2257 if (file_data->real_names)
2258 xfree ((void*) file_data->real_names[i]);
2259 }
2260
2261 /* The space for the struct itself lives on the obstack, so we don't
2262 free it here. */
2263 }
2264
2265 /* Create a quick_file_names hash table. */
2266
2267 static htab_up
2268 create_quick_file_names_table (unsigned int nr_initial_entries)
2269 {
2270 return htab_up (htab_create_alloc (nr_initial_entries,
2271 hash_file_name_entry, eq_file_name_entry,
2272 delete_file_name_entry, xcalloc, xfree));
2273 }
2274
2275 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2276 function is unrelated to symtabs, symtab would have to be created afterwards.
2277 You should call age_cached_comp_units after processing the CU. */
2278
2279 static dwarf2_cu *
2280 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2281 bool skip_partial)
2282 {
2283 if (per_cu->is_debug_types)
2284 load_full_type_unit (per_cu, per_objfile);
2285 else
2286 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2287 skip_partial, language_minimal);
2288
2289 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2290 if (cu == nullptr)
2291 return nullptr; /* Dummy CU. */
2292
2293 dwarf2_find_base_address (cu->dies, cu);
2294
2295 return cu;
2296 }
2297
2298 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2299
2300 static void
2301 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2302 dwarf2_per_objfile *per_objfile, bool skip_partial)
2303 {
2304 /* Skip type_unit_groups, reading the type units they contain
2305 is handled elsewhere. */
2306 if (per_cu->type_unit_group_p ())
2307 return;
2308
2309 {
2310 /* The destructor of dwarf2_queue_guard frees any entries left on
2311 the queue. After this point we're guaranteed to leave this function
2312 with the dwarf queue empty. */
2313 dwarf2_queue_guard q_guard (per_objfile);
2314
2315 if (!per_objfile->symtab_set_p (per_cu))
2316 {
2317 queue_comp_unit (per_cu, per_objfile, language_minimal);
2318 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2319
2320 /* If we just loaded a CU from a DWO, and we're working with an index
2321 that may badly handle TUs, load all the TUs in that DWO as well.
2322 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2323 if (!per_cu->is_debug_types
2324 && cu != NULL
2325 && cu->dwo_unit != NULL
2326 && per_objfile->per_bfd->index_table != NULL
2327 && per_objfile->per_bfd->index_table->version <= 7
2328 /* DWP files aren't supported yet. */
2329 && get_dwp_file (per_objfile) == NULL)
2330 queue_and_load_all_dwo_tus (cu);
2331 }
2332
2333 process_queue (per_objfile);
2334 }
2335
2336 /* Age the cache, releasing compilation units that have not
2337 been used recently. */
2338 per_objfile->age_comp_units ();
2339 }
2340
2341 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2342 the per-objfile for which this symtab is instantiated.
2343
2344 Returns the resulting symbol table. */
2345
2346 static struct compunit_symtab *
2347 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2348 dwarf2_per_objfile *per_objfile,
2349 bool skip_partial)
2350 {
2351 gdb_assert (per_objfile->per_bfd->using_index);
2352
2353 if (!per_objfile->symtab_set_p (per_cu))
2354 {
2355 free_cached_comp_units freer (per_objfile);
2356 scoped_restore decrementer = increment_reading_symtab ();
2357 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2358 process_cu_includes (per_objfile);
2359 }
2360
2361 return per_objfile->get_symtab (per_cu);
2362 }
2363
2364 /* See declaration. */
2365
2366 dwarf2_per_cu_data *
2367 dwarf2_per_bfd::get_cutu (int index)
2368 {
2369 if (index >= this->all_comp_units.size ())
2370 {
2371 index -= this->all_comp_units.size ();
2372 gdb_assert (index < this->all_type_units.size ());
2373 return &this->all_type_units[index]->per_cu;
2374 }
2375
2376 return this->all_comp_units[index];
2377 }
2378
2379 /* See declaration. */
2380
2381 dwarf2_per_cu_data *
2382 dwarf2_per_bfd::get_cu (int index)
2383 {
2384 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2385
2386 return this->all_comp_units[index];
2387 }
2388
2389 /* See declaration. */
2390
2391 signatured_type *
2392 dwarf2_per_bfd::get_tu (int index)
2393 {
2394 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2395
2396 return this->all_type_units[index];
2397 }
2398
2399 /* See read.h. */
2400
2401 dwarf2_per_cu_data *
2402 dwarf2_per_bfd::allocate_per_cu ()
2403 {
2404 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2405 result->per_bfd = this;
2406 result->index = m_num_psymtabs++;
2407 return result;
2408 }
2409
2410 /* See read.h. */
2411
2412 signatured_type *
2413 dwarf2_per_bfd::allocate_signatured_type ()
2414 {
2415 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2416 result->per_cu.per_bfd = this;
2417 result->per_cu.index = m_num_psymtabs++;
2418 return result;
2419 }
2420
2421 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2422 obstack, and constructed with the specified field values. */
2423
2424 static dwarf2_per_cu_data *
2425 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2426 struct dwarf2_section_info *section,
2427 int is_dwz,
2428 sect_offset sect_off, ULONGEST length)
2429 {
2430 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
2431 the_cu->sect_off = sect_off;
2432 the_cu->length = length;
2433 the_cu->section = section;
2434 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2435 struct dwarf2_per_cu_quick_data);
2436 the_cu->is_dwz = is_dwz;
2437 return the_cu;
2438 }
2439
2440 /* A helper for create_cus_from_index that handles a given list of
2441 CUs. */
2442
2443 static void
2444 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2445 const gdb_byte *cu_list, offset_type n_elements,
2446 struct dwarf2_section_info *section,
2447 int is_dwz)
2448 {
2449 for (offset_type i = 0; i < n_elements; i += 2)
2450 {
2451 gdb_static_assert (sizeof (ULONGEST) >= 8);
2452
2453 sect_offset sect_off
2454 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2455 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2456 cu_list += 2 * 8;
2457
2458 dwarf2_per_cu_data *per_cu
2459 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2460 length);
2461 per_bfd->all_comp_units.push_back (per_cu);
2462 }
2463 }
2464
2465 /* Read the CU list from the mapped index, and use it to create all
2466 the CU objects for PER_BFD. */
2467
2468 static void
2469 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2470 const gdb_byte *cu_list, offset_type cu_list_elements,
2471 const gdb_byte *dwz_list, offset_type dwz_elements)
2472 {
2473 gdb_assert (per_bfd->all_comp_units.empty ());
2474 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2475
2476 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2477 &per_bfd->info, 0);
2478
2479 if (dwz_elements == 0)
2480 return;
2481
2482 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2483 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2484 &dwz->info, 1);
2485 }
2486
2487 /* Create the signatured type hash table from the index. */
2488
2489 static void
2490 create_signatured_type_table_from_index
2491 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2492 const gdb_byte *bytes, offset_type elements)
2493 {
2494 gdb_assert (per_bfd->all_type_units.empty ());
2495 per_bfd->all_type_units.reserve (elements / 3);
2496
2497 htab_up sig_types_hash = allocate_signatured_type_table ();
2498
2499 for (offset_type i = 0; i < elements; i += 3)
2500 {
2501 struct signatured_type *sig_type;
2502 ULONGEST signature;
2503 void **slot;
2504 cu_offset type_offset_in_tu;
2505
2506 gdb_static_assert (sizeof (ULONGEST) >= 8);
2507 sect_offset sect_off
2508 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2509 type_offset_in_tu
2510 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2511 BFD_ENDIAN_LITTLE);
2512 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2513 bytes += 3 * 8;
2514
2515 sig_type = per_bfd->allocate_signatured_type ();
2516 sig_type->signature = signature;
2517 sig_type->type_offset_in_tu = type_offset_in_tu;
2518 sig_type->per_cu.is_debug_types = 1;
2519 sig_type->per_cu.section = section;
2520 sig_type->per_cu.sect_off = sect_off;
2521 sig_type->per_cu.v.quick
2522 = OBSTACK_ZALLOC (&per_bfd->obstack,
2523 struct dwarf2_per_cu_quick_data);
2524
2525 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2526 *slot = sig_type;
2527
2528 per_bfd->all_type_units.push_back (sig_type);
2529 }
2530
2531 per_bfd->signatured_types = std::move (sig_types_hash);
2532 }
2533
2534 /* Create the signatured type hash table from .debug_names. */
2535
2536 static void
2537 create_signatured_type_table_from_debug_names
2538 (dwarf2_per_objfile *per_objfile,
2539 const mapped_debug_names &map,
2540 struct dwarf2_section_info *section,
2541 struct dwarf2_section_info *abbrev_section)
2542 {
2543 struct objfile *objfile = per_objfile->objfile;
2544
2545 section->read (objfile);
2546 abbrev_section->read (objfile);
2547
2548 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2549 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2550
2551 htab_up sig_types_hash = allocate_signatured_type_table ();
2552
2553 for (uint32_t i = 0; i < map.tu_count; ++i)
2554 {
2555 struct signatured_type *sig_type;
2556 void **slot;
2557
2558 sect_offset sect_off
2559 = (sect_offset) (extract_unsigned_integer
2560 (map.tu_table_reordered + i * map.offset_size,
2561 map.offset_size,
2562 map.dwarf5_byte_order));
2563
2564 comp_unit_head cu_header;
2565 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2566 abbrev_section,
2567 section->buffer + to_underlying (sect_off),
2568 rcuh_kind::TYPE);
2569
2570 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
2571 sig_type->signature = cu_header.signature;
2572 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2573 sig_type->per_cu.is_debug_types = 1;
2574 sig_type->per_cu.section = section;
2575 sig_type->per_cu.sect_off = sect_off;
2576 sig_type->per_cu.v.quick
2577 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
2578 struct dwarf2_per_cu_quick_data);
2579
2580 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2581 *slot = sig_type;
2582
2583 per_objfile->per_bfd->all_type_units.push_back (sig_type);
2584 }
2585
2586 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2587 }
2588
2589 /* Read the address map data from the mapped index, and use it to
2590 populate the objfile's psymtabs_addrmap. */
2591
2592 static void
2593 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2594 struct mapped_index *index)
2595 {
2596 struct objfile *objfile = per_objfile->objfile;
2597 struct gdbarch *gdbarch = objfile->arch ();
2598 const gdb_byte *iter, *end;
2599 struct addrmap *mutable_map;
2600 CORE_ADDR baseaddr;
2601
2602 auto_obstack temp_obstack;
2603
2604 mutable_map = addrmap_create_mutable (&temp_obstack);
2605
2606 iter = index->address_table.data ();
2607 end = iter + index->address_table.size ();
2608
2609 baseaddr = objfile->text_section_offset ();
2610
2611 while (iter < end)
2612 {
2613 ULONGEST hi, lo, cu_index;
2614 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2615 iter += 8;
2616 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2617 iter += 8;
2618 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2619 iter += 4;
2620
2621 if (lo > hi)
2622 {
2623 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2624 hex_string (lo), hex_string (hi));
2625 continue;
2626 }
2627
2628 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
2629 {
2630 complaint (_(".gdb_index address table has invalid CU number %u"),
2631 (unsigned) cu_index);
2632 continue;
2633 }
2634
2635 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2636 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2637 addrmap_set_empty (mutable_map, lo, hi - 1,
2638 per_objfile->per_bfd->get_cu (cu_index));
2639 }
2640
2641 objfile->partial_symtabs->psymtabs_addrmap
2642 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2643 }
2644
2645 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2646 populate the objfile's psymtabs_addrmap. */
2647
2648 static void
2649 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2650 struct dwarf2_section_info *section)
2651 {
2652 struct objfile *objfile = per_objfile->objfile;
2653 bfd *abfd = objfile->obfd;
2654 struct gdbarch *gdbarch = objfile->arch ();
2655 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2656
2657 auto_obstack temp_obstack;
2658 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2659
2660 std::unordered_map<sect_offset,
2661 dwarf2_per_cu_data *,
2662 gdb::hash_enum<sect_offset>>
2663 debug_info_offset_to_per_cu;
2664 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
2665 {
2666 const auto insertpair
2667 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2668 if (!insertpair.second)
2669 {
2670 warning (_("Section .debug_aranges in %s has duplicate "
2671 "debug_info_offset %s, ignoring .debug_aranges."),
2672 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2673 return;
2674 }
2675 }
2676
2677 section->read (objfile);
2678
2679 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2680
2681 const gdb_byte *addr = section->buffer;
2682
2683 while (addr < section->buffer + section->size)
2684 {
2685 const gdb_byte *const entry_addr = addr;
2686 unsigned int bytes_read;
2687
2688 const LONGEST entry_length = read_initial_length (abfd, addr,
2689 &bytes_read);
2690 addr += bytes_read;
2691
2692 const gdb_byte *const entry_end = addr + entry_length;
2693 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2694 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2695 if (addr + entry_length > section->buffer + section->size)
2696 {
2697 warning (_("Section .debug_aranges in %s entry at offset %s "
2698 "length %s exceeds section length %s, "
2699 "ignoring .debug_aranges."),
2700 objfile_name (objfile),
2701 plongest (entry_addr - section->buffer),
2702 plongest (bytes_read + entry_length),
2703 pulongest (section->size));
2704 return;
2705 }
2706
2707 /* The version number. */
2708 const uint16_t version = read_2_bytes (abfd, addr);
2709 addr += 2;
2710 if (version != 2)
2711 {
2712 warning (_("Section .debug_aranges in %s entry at offset %s "
2713 "has unsupported version %d, ignoring .debug_aranges."),
2714 objfile_name (objfile),
2715 plongest (entry_addr - section->buffer), version);
2716 return;
2717 }
2718
2719 const uint64_t debug_info_offset
2720 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2721 addr += offset_size;
2722 const auto per_cu_it
2723 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2724 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2725 {
2726 warning (_("Section .debug_aranges in %s entry at offset %s "
2727 "debug_info_offset %s does not exists, "
2728 "ignoring .debug_aranges."),
2729 objfile_name (objfile),
2730 plongest (entry_addr - section->buffer),
2731 pulongest (debug_info_offset));
2732 return;
2733 }
2734 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2735
2736 const uint8_t address_size = *addr++;
2737 if (address_size < 1 || address_size > 8)
2738 {
2739 warning (_("Section .debug_aranges in %s entry at offset %s "
2740 "address_size %u is invalid, ignoring .debug_aranges."),
2741 objfile_name (objfile),
2742 plongest (entry_addr - section->buffer), address_size);
2743 return;
2744 }
2745
2746 const uint8_t segment_selector_size = *addr++;
2747 if (segment_selector_size != 0)
2748 {
2749 warning (_("Section .debug_aranges in %s entry at offset %s "
2750 "segment_selector_size %u is not supported, "
2751 "ignoring .debug_aranges."),
2752 objfile_name (objfile),
2753 plongest (entry_addr - section->buffer),
2754 segment_selector_size);
2755 return;
2756 }
2757
2758 /* Must pad to an alignment boundary that is twice the address
2759 size. It is undocumented by the DWARF standard but GCC does
2760 use it. */
2761 for (size_t padding = ((-(addr - section->buffer))
2762 & (2 * address_size - 1));
2763 padding > 0; padding--)
2764 if (*addr++ != 0)
2765 {
2766 warning (_("Section .debug_aranges in %s entry at offset %s "
2767 "padding is not zero, ignoring .debug_aranges."),
2768 objfile_name (objfile),
2769 plongest (entry_addr - section->buffer));
2770 return;
2771 }
2772
2773 for (;;)
2774 {
2775 if (addr + 2 * address_size > entry_end)
2776 {
2777 warning (_("Section .debug_aranges in %s entry at offset %s "
2778 "address list is not properly terminated, "
2779 "ignoring .debug_aranges."),
2780 objfile_name (objfile),
2781 plongest (entry_addr - section->buffer));
2782 return;
2783 }
2784 ULONGEST start = extract_unsigned_integer (addr, address_size,
2785 dwarf5_byte_order);
2786 addr += address_size;
2787 ULONGEST length = extract_unsigned_integer (addr, address_size,
2788 dwarf5_byte_order);
2789 addr += address_size;
2790 if (start == 0 && length == 0)
2791 break;
2792 if (start == 0 && !per_objfile->per_bfd->has_section_at_zero)
2793 {
2794 /* Symbol was eliminated due to a COMDAT group. */
2795 continue;
2796 }
2797 ULONGEST end = start + length;
2798 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2799 - baseaddr);
2800 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2801 - baseaddr);
2802 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2803 }
2804 }
2805
2806 objfile->partial_symtabs->psymtabs_addrmap
2807 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2808 }
2809
2810 /* Find a slot in the mapped index INDEX for the object named NAME.
2811 If NAME is found, set *VEC_OUT to point to the CU vector in the
2812 constant pool and return true. If NAME cannot be found, return
2813 false. */
2814
2815 static bool
2816 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2817 offset_type **vec_out)
2818 {
2819 offset_type hash;
2820 offset_type slot, step;
2821 int (*cmp) (const char *, const char *);
2822
2823 gdb::unique_xmalloc_ptr<char> without_params;
2824 if (current_language->la_language == language_cplus
2825 || current_language->la_language == language_fortran
2826 || current_language->la_language == language_d)
2827 {
2828 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2829 not contain any. */
2830
2831 if (strchr (name, '(') != NULL)
2832 {
2833 without_params = cp_remove_params (name);
2834
2835 if (without_params != NULL)
2836 name = without_params.get ();
2837 }
2838 }
2839
2840 /* Index version 4 did not support case insensitive searches. But the
2841 indices for case insensitive languages are built in lowercase, therefore
2842 simulate our NAME being searched is also lowercased. */
2843 hash = mapped_index_string_hash ((index->version == 4
2844 && case_sensitivity == case_sensitive_off
2845 ? 5 : index->version),
2846 name);
2847
2848 slot = hash & (index->symbol_table.size () - 1);
2849 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2850 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2851
2852 for (;;)
2853 {
2854 const char *str;
2855
2856 const auto &bucket = index->symbol_table[slot];
2857 if (bucket.name == 0 && bucket.vec == 0)
2858 return false;
2859
2860 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2861 if (!cmp (name, str))
2862 {
2863 *vec_out = (offset_type *) (index->constant_pool
2864 + MAYBE_SWAP (bucket.vec));
2865 return true;
2866 }
2867
2868 slot = (slot + step) & (index->symbol_table.size () - 1);
2869 }
2870 }
2871
2872 /* A helper function that reads the .gdb_index from BUFFER and fills
2873 in MAP. FILENAME is the name of the file containing the data;
2874 it is used for error reporting. DEPRECATED_OK is true if it is
2875 ok to use deprecated sections.
2876
2877 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2878 out parameters that are filled in with information about the CU and
2879 TU lists in the section.
2880
2881 Returns true if all went well, false otherwise. */
2882
2883 static bool
2884 read_gdb_index_from_buffer (const char *filename,
2885 bool deprecated_ok,
2886 gdb::array_view<const gdb_byte> buffer,
2887 struct mapped_index *map,
2888 const gdb_byte **cu_list,
2889 offset_type *cu_list_elements,
2890 const gdb_byte **types_list,
2891 offset_type *types_list_elements)
2892 {
2893 const gdb_byte *addr = &buffer[0];
2894
2895 /* Version check. */
2896 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2897 /* Versions earlier than 3 emitted every copy of a psymbol. This
2898 causes the index to behave very poorly for certain requests. Version 3
2899 contained incomplete addrmap. So, it seems better to just ignore such
2900 indices. */
2901 if (version < 4)
2902 {
2903 static int warning_printed = 0;
2904 if (!warning_printed)
2905 {
2906 warning (_("Skipping obsolete .gdb_index section in %s."),
2907 filename);
2908 warning_printed = 1;
2909 }
2910 return 0;
2911 }
2912 /* Index version 4 uses a different hash function than index version
2913 5 and later.
2914
2915 Versions earlier than 6 did not emit psymbols for inlined
2916 functions. Using these files will cause GDB not to be able to
2917 set breakpoints on inlined functions by name, so we ignore these
2918 indices unless the user has done
2919 "set use-deprecated-index-sections on". */
2920 if (version < 6 && !deprecated_ok)
2921 {
2922 static int warning_printed = 0;
2923 if (!warning_printed)
2924 {
2925 warning (_("\
2926 Skipping deprecated .gdb_index section in %s.\n\
2927 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2928 to use the section anyway."),
2929 filename);
2930 warning_printed = 1;
2931 }
2932 return 0;
2933 }
2934 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2935 of the TU (for symbols coming from TUs),
2936 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2937 Plus gold-generated indices can have duplicate entries for global symbols,
2938 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2939 These are just performance bugs, and we can't distinguish gdb-generated
2940 indices from gold-generated ones, so issue no warning here. */
2941
2942 /* Indexes with higher version than the one supported by GDB may be no
2943 longer backward compatible. */
2944 if (version > 8)
2945 return 0;
2946
2947 map->version = version;
2948
2949 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
2950
2951 int i = 0;
2952 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2953 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2954 / 8);
2955 ++i;
2956
2957 *types_list = addr + MAYBE_SWAP (metadata[i]);
2958 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2959 - MAYBE_SWAP (metadata[i]))
2960 / 8);
2961 ++i;
2962
2963 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2964 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2965 map->address_table
2966 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2967 ++i;
2968
2969 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2970 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2971 map->symbol_table
2972 = gdb::array_view<mapped_index::symbol_table_slot>
2973 ((mapped_index::symbol_table_slot *) symbol_table,
2974 (mapped_index::symbol_table_slot *) symbol_table_end);
2975
2976 ++i;
2977 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
2978
2979 return 1;
2980 }
2981
2982 /* Callback types for dwarf2_read_gdb_index. */
2983
2984 typedef gdb::function_view
2985 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
2986 get_gdb_index_contents_ftype;
2987 typedef gdb::function_view
2988 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2989 get_gdb_index_contents_dwz_ftype;
2990
2991 /* Read .gdb_index. If everything went ok, initialize the "quick"
2992 elements of all the CUs and return 1. Otherwise, return 0. */
2993
2994 static int
2995 dwarf2_read_gdb_index
2996 (dwarf2_per_objfile *per_objfile,
2997 get_gdb_index_contents_ftype get_gdb_index_contents,
2998 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2999 {
3000 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3001 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3002 struct dwz_file *dwz;
3003 struct objfile *objfile = per_objfile->objfile;
3004 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
3005
3006 gdb::array_view<const gdb_byte> main_index_contents
3007 = get_gdb_index_contents (objfile, per_bfd);
3008
3009 if (main_index_contents.empty ())
3010 return 0;
3011
3012 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3013 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3014 use_deprecated_index_sections,
3015 main_index_contents, map.get (), &cu_list,
3016 &cu_list_elements, &types_list,
3017 &types_list_elements))
3018 return 0;
3019
3020 /* Don't use the index if it's empty. */
3021 if (map->symbol_table.empty ())
3022 return 0;
3023
3024 /* If there is a .dwz file, read it so we can get its CU list as
3025 well. */
3026 dwz = dwarf2_get_dwz_file (per_bfd);
3027 if (dwz != NULL)
3028 {
3029 struct mapped_index dwz_map;
3030 const gdb_byte *dwz_types_ignore;
3031 offset_type dwz_types_elements_ignore;
3032
3033 gdb::array_view<const gdb_byte> dwz_index_content
3034 = get_gdb_index_contents_dwz (objfile, dwz);
3035
3036 if (dwz_index_content.empty ())
3037 return 0;
3038
3039 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3040 1, dwz_index_content, &dwz_map,
3041 &dwz_list, &dwz_list_elements,
3042 &dwz_types_ignore,
3043 &dwz_types_elements_ignore))
3044 {
3045 warning (_("could not read '.gdb_index' section from %s; skipping"),
3046 bfd_get_filename (dwz->dwz_bfd.get ()));
3047 return 0;
3048 }
3049 }
3050
3051 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3052 dwz_list_elements);
3053
3054 if (types_list_elements)
3055 {
3056 /* We can only handle a single .debug_types when we have an
3057 index. */
3058 if (per_bfd->types.size () != 1)
3059 return 0;
3060
3061 dwarf2_section_info *section = &per_bfd->types[0];
3062
3063 create_signatured_type_table_from_index (per_bfd, section, types_list,
3064 types_list_elements);
3065 }
3066
3067 create_addrmap_from_index (per_objfile, map.get ());
3068
3069 per_bfd->index_table = std::move (map);
3070 per_bfd->using_index = 1;
3071 per_bfd->quick_file_names_table =
3072 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3073
3074 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
3075 objfiles using the same BFD. */
3076 gdb_assert (per_bfd->partial_symtabs == nullptr);
3077 per_bfd->partial_symtabs = objfile->partial_symtabs;
3078
3079 return 1;
3080 }
3081
3082 /* die_reader_func for dw2_get_file_names. */
3083
3084 static void
3085 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3086 struct die_info *comp_unit_die)
3087 {
3088 struct dwarf2_cu *cu = reader->cu;
3089 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3090 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3091 struct dwarf2_per_cu_data *lh_cu;
3092 struct attribute *attr;
3093 void **slot;
3094 struct quick_file_names *qfn;
3095
3096 gdb_assert (! this_cu->is_debug_types);
3097
3098 /* Our callers never want to match partial units -- instead they
3099 will match the enclosing full CU. */
3100 if (comp_unit_die->tag == DW_TAG_partial_unit)
3101 {
3102 this_cu->v.quick->no_file_data = 1;
3103 return;
3104 }
3105
3106 lh_cu = this_cu;
3107 slot = NULL;
3108
3109 line_header_up lh;
3110 sect_offset line_offset {};
3111
3112 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3113 if (attr != nullptr && attr->form_is_unsigned ())
3114 {
3115 struct quick_file_names find_entry;
3116
3117 line_offset = (sect_offset) attr->as_unsigned ();
3118
3119 /* We may have already read in this line header (TU line header sharing).
3120 If we have we're done. */
3121 find_entry.hash.dwo_unit = cu->dwo_unit;
3122 find_entry.hash.line_sect_off = line_offset;
3123 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
3124 &find_entry, INSERT);
3125 if (*slot != NULL)
3126 {
3127 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3128 return;
3129 }
3130
3131 lh = dwarf_decode_line_header (line_offset, cu);
3132 }
3133 if (lh == NULL)
3134 {
3135 lh_cu->v.quick->no_file_data = 1;
3136 return;
3137 }
3138
3139 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
3140 qfn->hash.dwo_unit = cu->dwo_unit;
3141 qfn->hash.line_sect_off = line_offset;
3142 gdb_assert (slot != NULL);
3143 *slot = qfn;
3144
3145 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3146
3147 int offset = 0;
3148 if (strcmp (fnd.name, "<unknown>") != 0)
3149 ++offset;
3150
3151 qfn->num_file_names = offset + lh->file_names_size ();
3152 qfn->file_names =
3153 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
3154 qfn->num_file_names);
3155 if (offset != 0)
3156 qfn->file_names[0] = xstrdup (fnd.name);
3157 for (int i = 0; i < lh->file_names_size (); ++i)
3158 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3159 fnd.comp_dir).release ();
3160 qfn->real_names = NULL;
3161
3162 lh_cu->v.quick->file_names = qfn;
3163 }
3164
3165 /* A helper for the "quick" functions which attempts to read the line
3166 table for THIS_CU. */
3167
3168 static struct quick_file_names *
3169 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3170 dwarf2_per_objfile *per_objfile)
3171 {
3172 /* This should never be called for TUs. */
3173 gdb_assert (! this_cu->is_debug_types);
3174 /* Nor type unit groups. */
3175 gdb_assert (! this_cu->type_unit_group_p ());
3176
3177 if (this_cu->v.quick->file_names != NULL)
3178 return this_cu->v.quick->file_names;
3179 /* If we know there is no line data, no point in looking again. */
3180 if (this_cu->v.quick->no_file_data)
3181 return NULL;
3182
3183 cutu_reader reader (this_cu, per_objfile);
3184 if (!reader.dummy_p)
3185 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
3186
3187 if (this_cu->v.quick->no_file_data)
3188 return NULL;
3189 return this_cu->v.quick->file_names;
3190 }
3191
3192 /* A helper for the "quick" functions which computes and caches the
3193 real path for a given file name from the line table. */
3194
3195 static const char *
3196 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3197 struct quick_file_names *qfn, int index)
3198 {
3199 if (qfn->real_names == NULL)
3200 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3201 qfn->num_file_names, const char *);
3202
3203 if (qfn->real_names[index] == NULL)
3204 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3205
3206 return qfn->real_names[index];
3207 }
3208
3209 static struct symtab *
3210 dw2_find_last_source_symtab (struct objfile *objfile)
3211 {
3212 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3213 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3214 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3215
3216 if (cust == NULL)
3217 return NULL;
3218
3219 return compunit_primary_filetab (cust);
3220 }
3221
3222 /* Traversal function for dw2_forget_cached_source_info. */
3223
3224 static int
3225 dw2_free_cached_file_names (void **slot, void *info)
3226 {
3227 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3228
3229 if (file_data->real_names)
3230 {
3231 int i;
3232
3233 for (i = 0; i < file_data->num_file_names; ++i)
3234 {
3235 xfree ((void*) file_data->real_names[i]);
3236 file_data->real_names[i] = NULL;
3237 }
3238 }
3239
3240 return 1;
3241 }
3242
3243 static void
3244 dw2_forget_cached_source_info (struct objfile *objfile)
3245 {
3246 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3247
3248 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
3249 dw2_free_cached_file_names, NULL);
3250 }
3251
3252 /* Helper function for dw2_map_symtabs_matching_filename that expands
3253 the symtabs and calls the iterator. */
3254
3255 static int
3256 dw2_map_expand_apply (struct objfile *objfile,
3257 struct dwarf2_per_cu_data *per_cu,
3258 const char *name, const char *real_path,
3259 gdb::function_view<bool (symtab *)> callback)
3260 {
3261 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3262
3263 /* Don't visit already-expanded CUs. */
3264 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3265 if (per_objfile->symtab_set_p (per_cu))
3266 return 0;
3267
3268 /* This may expand more than one symtab, and we want to iterate over
3269 all of them. */
3270 dw2_instantiate_symtab (per_cu, per_objfile, false);
3271
3272 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3273 last_made, callback);
3274 }
3275
3276 /* Implementation of the map_symtabs_matching_filename method. */
3277
3278 static bool
3279 dw2_map_symtabs_matching_filename
3280 (struct objfile *objfile, const char *name, const char *real_path,
3281 gdb::function_view<bool (symtab *)> callback)
3282 {
3283 const char *name_basename = lbasename (name);
3284 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3285
3286 /* The rule is CUs specify all the files, including those used by
3287 any TU, so there's no need to scan TUs here. */
3288
3289 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3290 {
3291 /* We only need to look at symtabs not already expanded. */
3292 if (per_objfile->symtab_set_p (per_cu))
3293 continue;
3294
3295 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3296 if (file_data == NULL)
3297 continue;
3298
3299 for (int j = 0; j < file_data->num_file_names; ++j)
3300 {
3301 const char *this_name = file_data->file_names[j];
3302 const char *this_real_name;
3303
3304 if (compare_filenames_for_search (this_name, name))
3305 {
3306 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3307 callback))
3308 return true;
3309 continue;
3310 }
3311
3312 /* Before we invoke realpath, which can get expensive when many
3313 files are involved, do a quick comparison of the basenames. */
3314 if (! basenames_may_differ
3315 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3316 continue;
3317
3318 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
3319 if (compare_filenames_for_search (this_real_name, name))
3320 {
3321 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3322 callback))
3323 return true;
3324 continue;
3325 }
3326
3327 if (real_path != NULL)
3328 {
3329 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3330 gdb_assert (IS_ABSOLUTE_PATH (name));
3331 if (this_real_name != NULL
3332 && FILENAME_CMP (real_path, this_real_name) == 0)
3333 {
3334 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3335 callback))
3336 return true;
3337 continue;
3338 }
3339 }
3340 }
3341 }
3342
3343 return false;
3344 }
3345
3346 /* Struct used to manage iterating over all CUs looking for a symbol. */
3347
3348 struct dw2_symtab_iterator
3349 {
3350 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3351 dwarf2_per_objfile *per_objfile;
3352 /* If set, only look for symbols that match that block. Valid values are
3353 GLOBAL_BLOCK and STATIC_BLOCK. */
3354 gdb::optional<block_enum> block_index;
3355 /* The kind of symbol we're looking for. */
3356 domain_enum domain;
3357 /* The list of CUs from the index entry of the symbol,
3358 or NULL if not found. */
3359 offset_type *vec;
3360 /* The next element in VEC to look at. */
3361 int next;
3362 /* The number of elements in VEC, or zero if there is no match. */
3363 int length;
3364 /* Have we seen a global version of the symbol?
3365 If so we can ignore all further global instances.
3366 This is to work around gold/15646, inefficient gold-generated
3367 indices. */
3368 int global_seen;
3369 };
3370
3371 /* Initialize the index symtab iterator ITER, common part. */
3372
3373 static void
3374 dw2_symtab_iter_init_common (struct dw2_symtab_iterator *iter,
3375 dwarf2_per_objfile *per_objfile,
3376 gdb::optional<block_enum> block_index,
3377 domain_enum domain)
3378 {
3379 iter->per_objfile = per_objfile;
3380 iter->block_index = block_index;
3381 iter->domain = domain;
3382 iter->next = 0;
3383 iter->global_seen = 0;
3384 iter->vec = NULL;
3385 iter->length = 0;
3386 }
3387
3388 /* Initialize the index symtab iterator ITER, const char *NAME variant. */
3389
3390 static void
3391 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3392 dwarf2_per_objfile *per_objfile,
3393 gdb::optional<block_enum> block_index,
3394 domain_enum domain,
3395 const char *name)
3396 {
3397 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3398
3399 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3400 /* index is NULL if OBJF_READNOW. */
3401 if (index == NULL)
3402 return;
3403
3404 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3405 iter->length = MAYBE_SWAP (*iter->vec);
3406 }
3407
3408 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3409
3410 static void
3411 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3412 dwarf2_per_objfile *per_objfile,
3413 gdb::optional<block_enum> block_index,
3414 domain_enum domain, offset_type namei)
3415 {
3416 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3417
3418 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3419 /* index is NULL if OBJF_READNOW. */
3420 if (index == NULL)
3421 return;
3422
3423 gdb_assert (!index->symbol_name_slot_invalid (namei));
3424 const auto &bucket = index->symbol_table[namei];
3425
3426 iter->vec = (offset_type *) (index->constant_pool
3427 + MAYBE_SWAP (bucket.vec));
3428 iter->length = MAYBE_SWAP (*iter->vec);
3429 }
3430
3431 /* Return the next matching CU or NULL if there are no more. */
3432
3433 static struct dwarf2_per_cu_data *
3434 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3435 {
3436 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3437
3438 for ( ; iter->next < iter->length; ++iter->next)
3439 {
3440 offset_type cu_index_and_attrs =
3441 MAYBE_SWAP (iter->vec[iter->next + 1]);
3442 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3443 gdb_index_symbol_kind symbol_kind =
3444 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3445 /* Only check the symbol attributes if they're present.
3446 Indices prior to version 7 don't record them,
3447 and indices >= 7 may elide them for certain symbols
3448 (gold does this). */
3449 int attrs_valid =
3450 (per_objfile->per_bfd->index_table->version >= 7
3451 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3452
3453 /* Don't crash on bad data. */
3454 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3455 + per_objfile->per_bfd->all_type_units.size ()))
3456 {
3457 complaint (_(".gdb_index entry has bad CU index"
3458 " [in module %s]"), objfile_name (per_objfile->objfile));
3459 continue;
3460 }
3461
3462 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3463
3464 /* Skip if already read in. */
3465 if (per_objfile->symtab_set_p (per_cu))
3466 continue;
3467
3468 /* Check static vs global. */
3469 if (attrs_valid)
3470 {
3471 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3472
3473 if (iter->block_index.has_value ())
3474 {
3475 bool want_static = *iter->block_index == STATIC_BLOCK;
3476
3477 if (is_static != want_static)
3478 continue;
3479 }
3480
3481 /* Work around gold/15646. */
3482 if (!is_static
3483 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3484 {
3485 if (iter->global_seen)
3486 continue;
3487
3488 iter->global_seen = 1;
3489 }
3490 }
3491
3492 /* Only check the symbol's kind if it has one. */
3493 if (attrs_valid)
3494 {
3495 switch (iter->domain)
3496 {
3497 case VAR_DOMAIN:
3498 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3499 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3500 /* Some types are also in VAR_DOMAIN. */
3501 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3502 continue;
3503 break;
3504 case STRUCT_DOMAIN:
3505 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3506 continue;
3507 break;
3508 case LABEL_DOMAIN:
3509 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3510 continue;
3511 break;
3512 case MODULE_DOMAIN:
3513 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3514 continue;
3515 break;
3516 default:
3517 break;
3518 }
3519 }
3520
3521 ++iter->next;
3522 return per_cu;
3523 }
3524
3525 return NULL;
3526 }
3527
3528 static struct compunit_symtab *
3529 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3530 const char *name, domain_enum domain)
3531 {
3532 struct compunit_symtab *stab_best = NULL;
3533 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3534
3535 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3536
3537 struct dw2_symtab_iterator iter;
3538 struct dwarf2_per_cu_data *per_cu;
3539
3540 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
3541
3542 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3543 {
3544 struct symbol *sym, *with_opaque = NULL;
3545 struct compunit_symtab *stab
3546 = dw2_instantiate_symtab (per_cu, per_objfile, false);
3547 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3548 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3549
3550 sym = block_find_symbol (block, name, domain,
3551 block_find_non_opaque_type_preferred,
3552 &with_opaque);
3553
3554 /* Some caution must be observed with overloaded functions
3555 and methods, since the index will not contain any overload
3556 information (but NAME might contain it). */
3557
3558 if (sym != NULL
3559 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3560 return stab;
3561 if (with_opaque != NULL
3562 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3563 stab_best = stab;
3564
3565 /* Keep looking through other CUs. */
3566 }
3567
3568 return stab_best;
3569 }
3570
3571 static void
3572 dw2_print_stats (struct objfile *objfile)
3573 {
3574 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3575 int total = (per_objfile->per_bfd->all_comp_units.size ()
3576 + per_objfile->per_bfd->all_type_units.size ());
3577 int count = 0;
3578
3579 for (int i = 0; i < total; ++i)
3580 {
3581 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3582
3583 if (!per_objfile->symtab_set_p (per_cu))
3584 ++count;
3585 }
3586 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3587 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3588 }
3589
3590 /* This dumps minimal information about the index.
3591 It is called via "mt print objfiles".
3592 One use is to verify .gdb_index has been loaded by the
3593 gdb.dwarf2/gdb-index.exp testcase. */
3594
3595 static void
3596 dw2_dump (struct objfile *objfile)
3597 {
3598 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3599
3600 gdb_assert (per_objfile->per_bfd->using_index);
3601 printf_filtered (".gdb_index:");
3602 if (per_objfile->per_bfd->index_table != NULL)
3603 {
3604 printf_filtered (" version %d\n",
3605 per_objfile->per_bfd->index_table->version);
3606 }
3607 else
3608 printf_filtered (" faked for \"readnow\"\n");
3609 printf_filtered ("\n");
3610 }
3611
3612 static void
3613 dw2_expand_symtabs_for_function (struct objfile *objfile,
3614 const char *func_name)
3615 {
3616 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3617
3618 struct dw2_symtab_iterator iter;
3619 struct dwarf2_per_cu_data *per_cu;
3620
3621 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
3622
3623 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3624 dw2_instantiate_symtab (per_cu, per_objfile, false);
3625
3626 }
3627
3628 static void
3629 dw2_expand_all_symtabs (struct objfile *objfile)
3630 {
3631 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3632 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3633 + per_objfile->per_bfd->all_type_units.size ());
3634
3635 for (int i = 0; i < total_units; ++i)
3636 {
3637 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3638
3639 /* We don't want to directly expand a partial CU, because if we
3640 read it with the wrong language, then assertion failures can
3641 be triggered later on. See PR symtab/23010. So, tell
3642 dw2_instantiate_symtab to skip partial CUs -- any important
3643 partial CU will be read via DW_TAG_imported_unit anyway. */
3644 dw2_instantiate_symtab (per_cu, per_objfile, true);
3645 }
3646 }
3647
3648 static void
3649 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3650 const char *fullname)
3651 {
3652 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3653
3654 /* We don't need to consider type units here.
3655 This is only called for examining code, e.g. expand_line_sal.
3656 There can be an order of magnitude (or more) more type units
3657 than comp units, and we avoid them if we can. */
3658
3659 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3660 {
3661 /* We only need to look at symtabs not already expanded. */
3662 if (per_objfile->symtab_set_p (per_cu))
3663 continue;
3664
3665 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3666 if (file_data == NULL)
3667 continue;
3668
3669 for (int j = 0; j < file_data->num_file_names; ++j)
3670 {
3671 const char *this_fullname = file_data->file_names[j];
3672
3673 if (filename_cmp (this_fullname, fullname) == 0)
3674 {
3675 dw2_instantiate_symtab (per_cu, per_objfile, false);
3676 break;
3677 }
3678 }
3679 }
3680 }
3681
3682 static void
3683 dw2_expand_symtabs_matching_symbol
3684 (mapped_index_base &index,
3685 const lookup_name_info &lookup_name_in,
3686 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3687 enum search_domain kind,
3688 gdb::function_view<bool (offset_type)> match_callback,
3689 dwarf2_per_objfile *per_objfile);
3690
3691 static void
3692 dw2_expand_symtabs_matching_one
3693 (dwarf2_per_cu_data *per_cu,
3694 dwarf2_per_objfile *per_objfile,
3695 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3696 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3697
3698 static void
3699 dw2_map_matching_symbols
3700 (struct objfile *objfile,
3701 const lookup_name_info &name, domain_enum domain,
3702 int global,
3703 gdb::function_view<symbol_found_callback_ftype> callback,
3704 symbol_compare_ftype *ordered_compare)
3705 {
3706 /* Used for Ada. */
3707 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3708
3709 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3710
3711 if (per_objfile->per_bfd->index_table != nullptr)
3712 {
3713 mapped_index &index = *per_objfile->per_bfd->index_table;
3714
3715 const char *match_name = name.ada ().lookup_name ().c_str ();
3716 auto matcher = [&] (const char *symname)
3717 {
3718 if (ordered_compare == nullptr)
3719 return true;
3720 return ordered_compare (symname, match_name) == 0;
3721 };
3722
3723 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3724 [&] (offset_type namei)
3725 {
3726 struct dw2_symtab_iterator iter;
3727 struct dwarf2_per_cu_data *per_cu;
3728
3729 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3730 namei);
3731 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3732 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3733 nullptr);
3734 return true;
3735 }, per_objfile);
3736 }
3737 else
3738 {
3739 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3740 proceed assuming all symtabs have been read in. */
3741 }
3742
3743 for (compunit_symtab *cust : objfile->compunits ())
3744 {
3745 const struct block *block;
3746
3747 if (cust == NULL)
3748 continue;
3749 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3750 if (!iterate_over_symbols_terminated (block, name,
3751 domain, callback))
3752 return;
3753 }
3754 }
3755
3756 /* Starting from a search name, return the string that finds the upper
3757 bound of all strings that start with SEARCH_NAME in a sorted name
3758 list. Returns the empty string to indicate that the upper bound is
3759 the end of the list. */
3760
3761 static std::string
3762 make_sort_after_prefix_name (const char *search_name)
3763 {
3764 /* When looking to complete "func", we find the upper bound of all
3765 symbols that start with "func" by looking for where we'd insert
3766 the closest string that would follow "func" in lexicographical
3767 order. Usually, that's "func"-with-last-character-incremented,
3768 i.e. "fund". Mind non-ASCII characters, though. Usually those
3769 will be UTF-8 multi-byte sequences, but we can't be certain.
3770 Especially mind the 0xff character, which is a valid character in
3771 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3772 rule out compilers allowing it in identifiers. Note that
3773 conveniently, strcmp/strcasecmp are specified to compare
3774 characters interpreted as unsigned char. So what we do is treat
3775 the whole string as a base 256 number composed of a sequence of
3776 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3777 to 0, and carries 1 to the following more-significant position.
3778 If the very first character in SEARCH_NAME ends up incremented
3779 and carries/overflows, then the upper bound is the end of the
3780 list. The string after the empty string is also the empty
3781 string.
3782
3783 Some examples of this operation:
3784
3785 SEARCH_NAME => "+1" RESULT
3786
3787 "abc" => "abd"
3788 "ab\xff" => "ac"
3789 "\xff" "a" "\xff" => "\xff" "b"
3790 "\xff" => ""
3791 "\xff\xff" => ""
3792 "" => ""
3793
3794 Then, with these symbols for example:
3795
3796 func
3797 func1
3798 fund
3799
3800 completing "func" looks for symbols between "func" and
3801 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3802 which finds "func" and "func1", but not "fund".
3803
3804 And with:
3805
3806 funcÿ (Latin1 'ÿ' [0xff])
3807 funcÿ1
3808 fund
3809
3810 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3811 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3812
3813 And with:
3814
3815 ÿÿ (Latin1 'ÿ' [0xff])
3816 ÿÿ1
3817
3818 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3819 the end of the list.
3820 */
3821 std::string after = search_name;
3822 while (!after.empty () && (unsigned char) after.back () == 0xff)
3823 after.pop_back ();
3824 if (!after.empty ())
3825 after.back () = (unsigned char) after.back () + 1;
3826 return after;
3827 }
3828
3829 /* See declaration. */
3830
3831 std::pair<std::vector<name_component>::const_iterator,
3832 std::vector<name_component>::const_iterator>
3833 mapped_index_base::find_name_components_bounds
3834 (const lookup_name_info &lookup_name_without_params, language lang,
3835 dwarf2_per_objfile *per_objfile) const
3836 {
3837 auto *name_cmp
3838 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3839
3840 const char *lang_name
3841 = lookup_name_without_params.language_lookup_name (lang);
3842
3843 /* Comparison function object for lower_bound that matches against a
3844 given symbol name. */
3845 auto lookup_compare_lower = [&] (const name_component &elem,
3846 const char *name)
3847 {
3848 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3849 const char *elem_name = elem_qualified + elem.name_offset;
3850 return name_cmp (elem_name, name) < 0;
3851 };
3852
3853 /* Comparison function object for upper_bound that matches against a
3854 given symbol name. */
3855 auto lookup_compare_upper = [&] (const char *name,
3856 const name_component &elem)
3857 {
3858 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3859 const char *elem_name = elem_qualified + elem.name_offset;
3860 return name_cmp (name, elem_name) < 0;
3861 };
3862
3863 auto begin = this->name_components.begin ();
3864 auto end = this->name_components.end ();
3865
3866 /* Find the lower bound. */
3867 auto lower = [&] ()
3868 {
3869 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3870 return begin;
3871 else
3872 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3873 } ();
3874
3875 /* Find the upper bound. */
3876 auto upper = [&] ()
3877 {
3878 if (lookup_name_without_params.completion_mode ())
3879 {
3880 /* In completion mode, we want UPPER to point past all
3881 symbols names that have the same prefix. I.e., with
3882 these symbols, and completing "func":
3883
3884 function << lower bound
3885 function1
3886 other_function << upper bound
3887
3888 We find the upper bound by looking for the insertion
3889 point of "func"-with-last-character-incremented,
3890 i.e. "fund". */
3891 std::string after = make_sort_after_prefix_name (lang_name);
3892 if (after.empty ())
3893 return end;
3894 return std::lower_bound (lower, end, after.c_str (),
3895 lookup_compare_lower);
3896 }
3897 else
3898 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3899 } ();
3900
3901 return {lower, upper};
3902 }
3903
3904 /* See declaration. */
3905
3906 void
3907 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
3908 {
3909 if (!this->name_components.empty ())
3910 return;
3911
3912 this->name_components_casing = case_sensitivity;
3913 auto *name_cmp
3914 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3915
3916 /* The code below only knows how to break apart components of C++
3917 symbol names (and other languages that use '::' as
3918 namespace/module separator) and Ada symbol names. */
3919 auto count = this->symbol_name_count ();
3920 for (offset_type idx = 0; idx < count; idx++)
3921 {
3922 if (this->symbol_name_slot_invalid (idx))
3923 continue;
3924
3925 const char *name = this->symbol_name_at (idx, per_objfile);
3926
3927 /* Add each name component to the name component table. */
3928 unsigned int previous_len = 0;
3929
3930 if (strstr (name, "::") != nullptr)
3931 {
3932 for (unsigned int current_len = cp_find_first_component (name);
3933 name[current_len] != '\0';
3934 current_len += cp_find_first_component (name + current_len))
3935 {
3936 gdb_assert (name[current_len] == ':');
3937 this->name_components.push_back ({previous_len, idx});
3938 /* Skip the '::'. */
3939 current_len += 2;
3940 previous_len = current_len;
3941 }
3942 }
3943 else
3944 {
3945 /* Handle the Ada encoded (aka mangled) form here. */
3946 for (const char *iter = strstr (name, "__");
3947 iter != nullptr;
3948 iter = strstr (iter, "__"))
3949 {
3950 this->name_components.push_back ({previous_len, idx});
3951 iter += 2;
3952 previous_len = iter - name;
3953 }
3954 }
3955
3956 this->name_components.push_back ({previous_len, idx});
3957 }
3958
3959 /* Sort name_components elements by name. */
3960 auto name_comp_compare = [&] (const name_component &left,
3961 const name_component &right)
3962 {
3963 const char *left_qualified
3964 = this->symbol_name_at (left.idx, per_objfile);
3965 const char *right_qualified
3966 = this->symbol_name_at (right.idx, per_objfile);
3967
3968 const char *left_name = left_qualified + left.name_offset;
3969 const char *right_name = right_qualified + right.name_offset;
3970
3971 return name_cmp (left_name, right_name) < 0;
3972 };
3973
3974 std::sort (this->name_components.begin (),
3975 this->name_components.end (),
3976 name_comp_compare);
3977 }
3978
3979 /* Helper for dw2_expand_symtabs_matching that works with a
3980 mapped_index_base instead of the containing objfile. This is split
3981 to a separate function in order to be able to unit test the
3982 name_components matching using a mock mapped_index_base. For each
3983 symbol name that matches, calls MATCH_CALLBACK, passing it the
3984 symbol's index in the mapped_index_base symbol table. */
3985
3986 static void
3987 dw2_expand_symtabs_matching_symbol
3988 (mapped_index_base &index,
3989 const lookup_name_info &lookup_name_in,
3990 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3991 enum search_domain kind,
3992 gdb::function_view<bool (offset_type)> match_callback,
3993 dwarf2_per_objfile *per_objfile)
3994 {
3995 lookup_name_info lookup_name_without_params
3996 = lookup_name_in.make_ignore_params ();
3997
3998 /* Build the symbol name component sorted vector, if we haven't
3999 yet. */
4000 index.build_name_components (per_objfile);
4001
4002 /* The same symbol may appear more than once in the range though.
4003 E.g., if we're looking for symbols that complete "w", and we have
4004 a symbol named "w1::w2", we'll find the two name components for
4005 that same symbol in the range. To be sure we only call the
4006 callback once per symbol, we first collect the symbol name
4007 indexes that matched in a temporary vector and ignore
4008 duplicates. */
4009 std::vector<offset_type> matches;
4010
4011 struct name_and_matcher
4012 {
4013 symbol_name_matcher_ftype *matcher;
4014 const char *name;
4015
4016 bool operator== (const name_and_matcher &other) const
4017 {
4018 return matcher == other.matcher && strcmp (name, other.name) == 0;
4019 }
4020 };
4021
4022 /* A vector holding all the different symbol name matchers, for all
4023 languages. */
4024 std::vector<name_and_matcher> matchers;
4025
4026 for (int i = 0; i < nr_languages; i++)
4027 {
4028 enum language lang_e = (enum language) i;
4029
4030 const language_defn *lang = language_def (lang_e);
4031 symbol_name_matcher_ftype *name_matcher
4032 = lang->get_symbol_name_matcher (lookup_name_without_params);
4033
4034 name_and_matcher key {
4035 name_matcher,
4036 lookup_name_without_params.language_lookup_name (lang_e)
4037 };
4038
4039 /* Don't insert the same comparison routine more than once.
4040 Note that we do this linear walk. This is not a problem in
4041 practice because the number of supported languages is
4042 low. */
4043 if (std::find (matchers.begin (), matchers.end (), key)
4044 != matchers.end ())
4045 continue;
4046 matchers.push_back (std::move (key));
4047
4048 auto bounds
4049 = index.find_name_components_bounds (lookup_name_without_params,
4050 lang_e, per_objfile);
4051
4052 /* Now for each symbol name in range, check to see if we have a name
4053 match, and if so, call the MATCH_CALLBACK callback. */
4054
4055 for (; bounds.first != bounds.second; ++bounds.first)
4056 {
4057 const char *qualified
4058 = index.symbol_name_at (bounds.first->idx, per_objfile);
4059
4060 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4061 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4062 continue;
4063
4064 matches.push_back (bounds.first->idx);
4065 }
4066 }
4067
4068 std::sort (matches.begin (), matches.end ());
4069
4070 /* Finally call the callback, once per match. */
4071 ULONGEST prev = -1;
4072 for (offset_type idx : matches)
4073 {
4074 if (prev != idx)
4075 {
4076 if (!match_callback (idx))
4077 break;
4078 prev = idx;
4079 }
4080 }
4081
4082 /* Above we use a type wider than idx's for 'prev', since 0 and
4083 (offset_type)-1 are both possible values. */
4084 static_assert (sizeof (prev) > sizeof (offset_type), "");
4085 }
4086
4087 #if GDB_SELF_TEST
4088
4089 namespace selftests { namespace dw2_expand_symtabs_matching {
4090
4091 /* A mock .gdb_index/.debug_names-like name index table, enough to
4092 exercise dw2_expand_symtabs_matching_symbol, which works with the
4093 mapped_index_base interface. Builds an index from the symbol list
4094 passed as parameter to the constructor. */
4095 class mock_mapped_index : public mapped_index_base
4096 {
4097 public:
4098 mock_mapped_index (gdb::array_view<const char *> symbols)
4099 : m_symbol_table (symbols)
4100 {}
4101
4102 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4103
4104 /* Return the number of names in the symbol table. */
4105 size_t symbol_name_count () const override
4106 {
4107 return m_symbol_table.size ();
4108 }
4109
4110 /* Get the name of the symbol at IDX in the symbol table. */
4111 const char *symbol_name_at
4112 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
4113 {
4114 return m_symbol_table[idx];
4115 }
4116
4117 private:
4118 gdb::array_view<const char *> m_symbol_table;
4119 };
4120
4121 /* Convenience function that converts a NULL pointer to a "<null>"
4122 string, to pass to print routines. */
4123
4124 static const char *
4125 string_or_null (const char *str)
4126 {
4127 return str != NULL ? str : "<null>";
4128 }
4129
4130 /* Check if a lookup_name_info built from
4131 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4132 index. EXPECTED_LIST is the list of expected matches, in expected
4133 matching order. If no match expected, then an empty list is
4134 specified. Returns true on success. On failure prints a warning
4135 indicating the file:line that failed, and returns false. */
4136
4137 static bool
4138 check_match (const char *file, int line,
4139 mock_mapped_index &mock_index,
4140 const char *name, symbol_name_match_type match_type,
4141 bool completion_mode,
4142 std::initializer_list<const char *> expected_list,
4143 dwarf2_per_objfile *per_objfile)
4144 {
4145 lookup_name_info lookup_name (name, match_type, completion_mode);
4146
4147 bool matched = true;
4148
4149 auto mismatch = [&] (const char *expected_str,
4150 const char *got)
4151 {
4152 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4153 "expected=\"%s\", got=\"%s\"\n"),
4154 file, line,
4155 (match_type == symbol_name_match_type::FULL
4156 ? "FULL" : "WILD"),
4157 name, string_or_null (expected_str), string_or_null (got));
4158 matched = false;
4159 };
4160
4161 auto expected_it = expected_list.begin ();
4162 auto expected_end = expected_list.end ();
4163
4164 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4165 NULL, ALL_DOMAIN,
4166 [&] (offset_type idx)
4167 {
4168 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
4169 const char *expected_str
4170 = expected_it == expected_end ? NULL : *expected_it++;
4171
4172 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4173 mismatch (expected_str, matched_name);
4174 return true;
4175 }, per_objfile);
4176
4177 const char *expected_str
4178 = expected_it == expected_end ? NULL : *expected_it++;
4179 if (expected_str != NULL)
4180 mismatch (expected_str, NULL);
4181
4182 return matched;
4183 }
4184
4185 /* The symbols added to the mock mapped_index for testing (in
4186 canonical form). */
4187 static const char *test_symbols[] = {
4188 "function",
4189 "std::bar",
4190 "std::zfunction",
4191 "std::zfunction2",
4192 "w1::w2",
4193 "ns::foo<char*>",
4194 "ns::foo<int>",
4195 "ns::foo<long>",
4196 "ns2::tmpl<int>::foo2",
4197 "(anonymous namespace)::A::B::C",
4198
4199 /* These are used to check that the increment-last-char in the
4200 matching algorithm for completion doesn't match "t1_fund" when
4201 completing "t1_func". */
4202 "t1_func",
4203 "t1_func1",
4204 "t1_fund",
4205 "t1_fund1",
4206
4207 /* A UTF-8 name with multi-byte sequences to make sure that
4208 cp-name-parser understands this as a single identifier ("função"
4209 is "function" in PT). */
4210 u8"u8função",
4211
4212 /* \377 (0xff) is Latin1 'ÿ'. */
4213 "yfunc\377",
4214
4215 /* \377 (0xff) is Latin1 'ÿ'. */
4216 "\377",
4217 "\377\377123",
4218
4219 /* A name with all sorts of complications. Starts with "z" to make
4220 it easier for the completion tests below. */
4221 #define Z_SYM_NAME \
4222 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4223 "::tuple<(anonymous namespace)::ui*, " \
4224 "std::default_delete<(anonymous namespace)::ui>, void>"
4225
4226 Z_SYM_NAME
4227 };
4228
4229 /* Returns true if the mapped_index_base::find_name_component_bounds
4230 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4231 in completion mode. */
4232
4233 static bool
4234 check_find_bounds_finds (mapped_index_base &index,
4235 const char *search_name,
4236 gdb::array_view<const char *> expected_syms,
4237 dwarf2_per_objfile *per_objfile)
4238 {
4239 lookup_name_info lookup_name (search_name,
4240 symbol_name_match_type::FULL, true);
4241
4242 auto bounds = index.find_name_components_bounds (lookup_name,
4243 language_cplus,
4244 per_objfile);
4245
4246 size_t distance = std::distance (bounds.first, bounds.second);
4247 if (distance != expected_syms.size ())
4248 return false;
4249
4250 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4251 {
4252 auto nc_elem = bounds.first + exp_elem;
4253 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
4254 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4255 return false;
4256 }
4257
4258 return true;
4259 }
4260
4261 /* Test the lower-level mapped_index::find_name_component_bounds
4262 method. */
4263
4264 static void
4265 test_mapped_index_find_name_component_bounds ()
4266 {
4267 mock_mapped_index mock_index (test_symbols);
4268
4269 mock_index.build_name_components (NULL /* per_objfile */);
4270
4271 /* Test the lower-level mapped_index::find_name_component_bounds
4272 method in completion mode. */
4273 {
4274 static const char *expected_syms[] = {
4275 "t1_func",
4276 "t1_func1",
4277 };
4278
4279 SELF_CHECK (check_find_bounds_finds
4280 (mock_index, "t1_func", expected_syms,
4281 NULL /* per_objfile */));
4282 }
4283
4284 /* Check that the increment-last-char in the name matching algorithm
4285 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4286 {
4287 static const char *expected_syms1[] = {
4288 "\377",
4289 "\377\377123",
4290 };
4291 SELF_CHECK (check_find_bounds_finds
4292 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
4293
4294 static const char *expected_syms2[] = {
4295 "\377\377123",
4296 };
4297 SELF_CHECK (check_find_bounds_finds
4298 (mock_index, "\377\377", expected_syms2,
4299 NULL /* per_objfile */));
4300 }
4301 }
4302
4303 /* Test dw2_expand_symtabs_matching_symbol. */
4304
4305 static void
4306 test_dw2_expand_symtabs_matching_symbol ()
4307 {
4308 mock_mapped_index mock_index (test_symbols);
4309
4310 /* We let all tests run until the end even if some fails, for debug
4311 convenience. */
4312 bool any_mismatch = false;
4313
4314 /* Create the expected symbols list (an initializer_list). Needed
4315 because lists have commas, and we need to pass them to CHECK,
4316 which is a macro. */
4317 #define EXPECT(...) { __VA_ARGS__ }
4318
4319 /* Wrapper for check_match that passes down the current
4320 __FILE__/__LINE__. */
4321 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4322 any_mismatch |= !check_match (__FILE__, __LINE__, \
4323 mock_index, \
4324 NAME, MATCH_TYPE, COMPLETION_MODE, \
4325 EXPECTED_LIST, NULL)
4326
4327 /* Identity checks. */
4328 for (const char *sym : test_symbols)
4329 {
4330 /* Should be able to match all existing symbols. */
4331 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4332 EXPECT (sym));
4333
4334 /* Should be able to match all existing symbols with
4335 parameters. */
4336 std::string with_params = std::string (sym) + "(int)";
4337 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4338 EXPECT (sym));
4339
4340 /* Should be able to match all existing symbols with
4341 parameters and qualifiers. */
4342 with_params = std::string (sym) + " ( int ) const";
4343 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4344 EXPECT (sym));
4345
4346 /* This should really find sym, but cp-name-parser.y doesn't
4347 know about lvalue/rvalue qualifiers yet. */
4348 with_params = std::string (sym) + " ( int ) &&";
4349 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4350 {});
4351 }
4352
4353 /* Check that the name matching algorithm for completion doesn't get
4354 confused with Latin1 'ÿ' / 0xff. */
4355 {
4356 static const char str[] = "\377";
4357 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4358 EXPECT ("\377", "\377\377123"));
4359 }
4360
4361 /* Check that the increment-last-char in the matching algorithm for
4362 completion doesn't match "t1_fund" when completing "t1_func". */
4363 {
4364 static const char str[] = "t1_func";
4365 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4366 EXPECT ("t1_func", "t1_func1"));
4367 }
4368
4369 /* Check that completion mode works at each prefix of the expected
4370 symbol name. */
4371 {
4372 static const char str[] = "function(int)";
4373 size_t len = strlen (str);
4374 std::string lookup;
4375
4376 for (size_t i = 1; i < len; i++)
4377 {
4378 lookup.assign (str, i);
4379 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4380 EXPECT ("function"));
4381 }
4382 }
4383
4384 /* While "w" is a prefix of both components, the match function
4385 should still only be called once. */
4386 {
4387 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4388 EXPECT ("w1::w2"));
4389 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4390 EXPECT ("w1::w2"));
4391 }
4392
4393 /* Same, with a "complicated" symbol. */
4394 {
4395 static const char str[] = Z_SYM_NAME;
4396 size_t len = strlen (str);
4397 std::string lookup;
4398
4399 for (size_t i = 1; i < len; i++)
4400 {
4401 lookup.assign (str, i);
4402 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4403 EXPECT (Z_SYM_NAME));
4404 }
4405 }
4406
4407 /* In FULL mode, an incomplete symbol doesn't match. */
4408 {
4409 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4410 {});
4411 }
4412
4413 /* A complete symbol with parameters matches any overload, since the
4414 index has no overload info. */
4415 {
4416 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4417 EXPECT ("std::zfunction", "std::zfunction2"));
4418 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4419 EXPECT ("std::zfunction", "std::zfunction2"));
4420 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4421 EXPECT ("std::zfunction", "std::zfunction2"));
4422 }
4423
4424 /* Check that whitespace is ignored appropriately. A symbol with a
4425 template argument list. */
4426 {
4427 static const char expected[] = "ns::foo<int>";
4428 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4429 EXPECT (expected));
4430 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4431 EXPECT (expected));
4432 }
4433
4434 /* Check that whitespace is ignored appropriately. A symbol with a
4435 template argument list that includes a pointer. */
4436 {
4437 static const char expected[] = "ns::foo<char*>";
4438 /* Try both completion and non-completion modes. */
4439 static const bool completion_mode[2] = {false, true};
4440 for (size_t i = 0; i < 2; i++)
4441 {
4442 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4443 completion_mode[i], EXPECT (expected));
4444 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4445 completion_mode[i], EXPECT (expected));
4446
4447 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4448 completion_mode[i], EXPECT (expected));
4449 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4450 completion_mode[i], EXPECT (expected));
4451 }
4452 }
4453
4454 {
4455 /* Check method qualifiers are ignored. */
4456 static const char expected[] = "ns::foo<char*>";
4457 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4458 symbol_name_match_type::FULL, true, EXPECT (expected));
4459 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4460 symbol_name_match_type::FULL, true, EXPECT (expected));
4461 CHECK_MATCH ("foo < char * > ( int ) const",
4462 symbol_name_match_type::WILD, true, EXPECT (expected));
4463 CHECK_MATCH ("foo < char * > ( int ) &&",
4464 symbol_name_match_type::WILD, true, EXPECT (expected));
4465 }
4466
4467 /* Test lookup names that don't match anything. */
4468 {
4469 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4470 {});
4471
4472 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4473 {});
4474 }
4475
4476 /* Some wild matching tests, exercising "(anonymous namespace)",
4477 which should not be confused with a parameter list. */
4478 {
4479 static const char *syms[] = {
4480 "A::B::C",
4481 "B::C",
4482 "C",
4483 "A :: B :: C ( int )",
4484 "B :: C ( int )",
4485 "C ( int )",
4486 };
4487
4488 for (const char *s : syms)
4489 {
4490 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4491 EXPECT ("(anonymous namespace)::A::B::C"));
4492 }
4493 }
4494
4495 {
4496 static const char expected[] = "ns2::tmpl<int>::foo2";
4497 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4498 EXPECT (expected));
4499 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4500 EXPECT (expected));
4501 }
4502
4503 SELF_CHECK (!any_mismatch);
4504
4505 #undef EXPECT
4506 #undef CHECK_MATCH
4507 }
4508
4509 static void
4510 run_test ()
4511 {
4512 test_mapped_index_find_name_component_bounds ();
4513 test_dw2_expand_symtabs_matching_symbol ();
4514 }
4515
4516 }} // namespace selftests::dw2_expand_symtabs_matching
4517
4518 #endif /* GDB_SELF_TEST */
4519
4520 /* If FILE_MATCHER is NULL or if PER_CU has
4521 dwarf2_per_cu_quick_data::MARK set (see
4522 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4523 EXPANSION_NOTIFY on it. */
4524
4525 static void
4526 dw2_expand_symtabs_matching_one
4527 (dwarf2_per_cu_data *per_cu,
4528 dwarf2_per_objfile *per_objfile,
4529 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4530 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4531 {
4532 if (file_matcher == NULL || per_cu->v.quick->mark)
4533 {
4534 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4535
4536 compunit_symtab *symtab
4537 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4538 gdb_assert (symtab != nullptr);
4539
4540 if (expansion_notify != NULL && symtab_was_null)
4541 expansion_notify (symtab);
4542 }
4543 }
4544
4545 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4546 matched, to expand corresponding CUs that were marked. IDX is the
4547 index of the symbol name that matched. */
4548
4549 static void
4550 dw2_expand_marked_cus
4551 (dwarf2_per_objfile *per_objfile, offset_type idx,
4552 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4553 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4554 search_domain kind)
4555 {
4556 offset_type *vec, vec_len, vec_idx;
4557 bool global_seen = false;
4558 mapped_index &index = *per_objfile->per_bfd->index_table;
4559
4560 vec = (offset_type *) (index.constant_pool
4561 + MAYBE_SWAP (index.symbol_table[idx].vec));
4562 vec_len = MAYBE_SWAP (vec[0]);
4563 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4564 {
4565 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4566 /* This value is only valid for index versions >= 7. */
4567 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4568 gdb_index_symbol_kind symbol_kind =
4569 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4570 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4571 /* Only check the symbol attributes if they're present.
4572 Indices prior to version 7 don't record them,
4573 and indices >= 7 may elide them for certain symbols
4574 (gold does this). */
4575 int attrs_valid =
4576 (index.version >= 7
4577 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4578
4579 /* Work around gold/15646. */
4580 if (attrs_valid
4581 && !is_static
4582 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4583 {
4584 if (global_seen)
4585 continue;
4586
4587 global_seen = true;
4588 }
4589
4590 /* Only check the symbol's kind if it has one. */
4591 if (attrs_valid)
4592 {
4593 switch (kind)
4594 {
4595 case VARIABLES_DOMAIN:
4596 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4597 continue;
4598 break;
4599 case FUNCTIONS_DOMAIN:
4600 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4601 continue;
4602 break;
4603 case TYPES_DOMAIN:
4604 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4605 continue;
4606 break;
4607 case MODULES_DOMAIN:
4608 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4609 continue;
4610 break;
4611 default:
4612 break;
4613 }
4614 }
4615
4616 /* Don't crash on bad data. */
4617 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4618 + per_objfile->per_bfd->all_type_units.size ()))
4619 {
4620 complaint (_(".gdb_index entry has bad CU index"
4621 " [in module %s]"), objfile_name (per_objfile->objfile));
4622 continue;
4623 }
4624
4625 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4626 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4627 expansion_notify);
4628 }
4629 }
4630
4631 /* If FILE_MATCHER is non-NULL, set all the
4632 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4633 that match FILE_MATCHER. */
4634
4635 static void
4636 dw_expand_symtabs_matching_file_matcher
4637 (dwarf2_per_objfile *per_objfile,
4638 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4639 {
4640 if (file_matcher == NULL)
4641 return;
4642
4643 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4644 htab_eq_pointer,
4645 NULL, xcalloc, xfree));
4646 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4647 htab_eq_pointer,
4648 NULL, xcalloc, xfree));
4649
4650 /* The rule is CUs specify all the files, including those used by
4651 any TU, so there's no need to scan TUs here. */
4652
4653 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4654 {
4655 QUIT;
4656
4657 per_cu->v.quick->mark = 0;
4658
4659 /* We only need to look at symtabs not already expanded. */
4660 if (per_objfile->symtab_set_p (per_cu))
4661 continue;
4662
4663 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4664 if (file_data == NULL)
4665 continue;
4666
4667 if (htab_find (visited_not_found.get (), file_data) != NULL)
4668 continue;
4669 else if (htab_find (visited_found.get (), file_data) != NULL)
4670 {
4671 per_cu->v.quick->mark = 1;
4672 continue;
4673 }
4674
4675 for (int j = 0; j < file_data->num_file_names; ++j)
4676 {
4677 const char *this_real_name;
4678
4679 if (file_matcher (file_data->file_names[j], false))
4680 {
4681 per_cu->v.quick->mark = 1;
4682 break;
4683 }
4684
4685 /* Before we invoke realpath, which can get expensive when many
4686 files are involved, do a quick comparison of the basenames. */
4687 if (!basenames_may_differ
4688 && !file_matcher (lbasename (file_data->file_names[j]),
4689 true))
4690 continue;
4691
4692 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4693 if (file_matcher (this_real_name, false))
4694 {
4695 per_cu->v.quick->mark = 1;
4696 break;
4697 }
4698 }
4699
4700 void **slot = htab_find_slot (per_cu->v.quick->mark
4701 ? visited_found.get ()
4702 : visited_not_found.get (),
4703 file_data, INSERT);
4704 *slot = file_data;
4705 }
4706 }
4707
4708 static void
4709 dw2_expand_symtabs_matching
4710 (struct objfile *objfile,
4711 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4712 const lookup_name_info *lookup_name,
4713 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4714 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4715 enum search_domain kind)
4716 {
4717 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4718
4719 /* index_table is NULL if OBJF_READNOW. */
4720 if (!per_objfile->per_bfd->index_table)
4721 return;
4722
4723 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4724
4725 if (symbol_matcher == NULL && lookup_name == NULL)
4726 {
4727 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4728 {
4729 QUIT;
4730
4731 dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4732 file_matcher, expansion_notify);
4733 }
4734 return;
4735 }
4736
4737 mapped_index &index = *per_objfile->per_bfd->index_table;
4738
4739 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4740 symbol_matcher,
4741 kind, [&] (offset_type idx)
4742 {
4743 dw2_expand_marked_cus (per_objfile, idx, file_matcher, expansion_notify,
4744 kind);
4745 return true;
4746 }, per_objfile);
4747 }
4748
4749 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4750 symtab. */
4751
4752 static struct compunit_symtab *
4753 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4754 CORE_ADDR pc)
4755 {
4756 int i;
4757
4758 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4759 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4760 return cust;
4761
4762 if (cust->includes == NULL)
4763 return NULL;
4764
4765 for (i = 0; cust->includes[i]; ++i)
4766 {
4767 struct compunit_symtab *s = cust->includes[i];
4768
4769 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4770 if (s != NULL)
4771 return s;
4772 }
4773
4774 return NULL;
4775 }
4776
4777 static struct compunit_symtab *
4778 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4779 struct bound_minimal_symbol msymbol,
4780 CORE_ADDR pc,
4781 struct obj_section *section,
4782 int warn_if_readin)
4783 {
4784 struct dwarf2_per_cu_data *data;
4785 struct compunit_symtab *result;
4786
4787 if (!objfile->partial_symtabs->psymtabs_addrmap)
4788 return NULL;
4789
4790 CORE_ADDR baseaddr = objfile->text_section_offset ();
4791 data = (struct dwarf2_per_cu_data *) addrmap_find
4792 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4793 if (!data)
4794 return NULL;
4795
4796 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4797 if (warn_if_readin && per_objfile->symtab_set_p (data))
4798 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4799 paddress (objfile->arch (), pc));
4800
4801 result = recursively_find_pc_sect_compunit_symtab
4802 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4803
4804 gdb_assert (result != NULL);
4805 return result;
4806 }
4807
4808 static void
4809 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4810 void *data, int need_fullname)
4811 {
4812 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4813
4814 if (!per_objfile->per_bfd->filenames_cache)
4815 {
4816 per_objfile->per_bfd->filenames_cache.emplace ();
4817
4818 htab_up visited (htab_create_alloc (10,
4819 htab_hash_pointer, htab_eq_pointer,
4820 NULL, xcalloc, xfree));
4821
4822 /* The rule is CUs specify all the files, including those used
4823 by any TU, so there's no need to scan TUs here. We can
4824 ignore file names coming from already-expanded CUs. */
4825
4826 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4827 {
4828 if (per_objfile->symtab_set_p (per_cu))
4829 {
4830 void **slot = htab_find_slot (visited.get (),
4831 per_cu->v.quick->file_names,
4832 INSERT);
4833
4834 *slot = per_cu->v.quick->file_names;
4835 }
4836 }
4837
4838 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4839 {
4840 /* We only need to look at symtabs not already expanded. */
4841 if (per_objfile->symtab_set_p (per_cu))
4842 continue;
4843
4844 quick_file_names *file_data
4845 = dw2_get_file_names (per_cu, per_objfile);
4846 if (file_data == NULL)
4847 continue;
4848
4849 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4850 if (*slot)
4851 {
4852 /* Already visited. */
4853 continue;
4854 }
4855 *slot = file_data;
4856
4857 for (int j = 0; j < file_data->num_file_names; ++j)
4858 {
4859 const char *filename = file_data->file_names[j];
4860 per_objfile->per_bfd->filenames_cache->seen (filename);
4861 }
4862 }
4863 }
4864
4865 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
4866 {
4867 gdb::unique_xmalloc_ptr<char> this_real_name;
4868
4869 if (need_fullname)
4870 this_real_name = gdb_realpath (filename);
4871 (*fun) (filename, this_real_name.get (), data);
4872 });
4873 }
4874
4875 static int
4876 dw2_has_symbols (struct objfile *objfile)
4877 {
4878 return 1;
4879 }
4880
4881 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4882 {
4883 dw2_has_symbols,
4884 dw2_find_last_source_symtab,
4885 dw2_forget_cached_source_info,
4886 dw2_map_symtabs_matching_filename,
4887 dw2_lookup_symbol,
4888 NULL,
4889 dw2_print_stats,
4890 dw2_dump,
4891 dw2_expand_symtabs_for_function,
4892 dw2_expand_all_symtabs,
4893 dw2_expand_symtabs_with_fullname,
4894 dw2_map_matching_symbols,
4895 dw2_expand_symtabs_matching,
4896 dw2_find_pc_sect_compunit_symtab,
4897 NULL,
4898 dw2_map_symbol_filenames
4899 };
4900
4901 /* DWARF-5 debug_names reader. */
4902
4903 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4904 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4905
4906 /* A helper function that reads the .debug_names section in SECTION
4907 and fills in MAP. FILENAME is the name of the file containing the
4908 section; it is used for error reporting.
4909
4910 Returns true if all went well, false otherwise. */
4911
4912 static bool
4913 read_debug_names_from_section (struct objfile *objfile,
4914 const char *filename,
4915 struct dwarf2_section_info *section,
4916 mapped_debug_names &map)
4917 {
4918 if (section->empty ())
4919 return false;
4920
4921 /* Older elfutils strip versions could keep the section in the main
4922 executable while splitting it for the separate debug info file. */
4923 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4924 return false;
4925
4926 section->read (objfile);
4927
4928 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4929
4930 const gdb_byte *addr = section->buffer;
4931
4932 bfd *const abfd = section->get_bfd_owner ();
4933
4934 unsigned int bytes_read;
4935 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4936 addr += bytes_read;
4937
4938 map.dwarf5_is_dwarf64 = bytes_read != 4;
4939 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4940 if (bytes_read + length != section->size)
4941 {
4942 /* There may be multiple per-CU indices. */
4943 warning (_("Section .debug_names in %s length %s does not match "
4944 "section length %s, ignoring .debug_names."),
4945 filename, plongest (bytes_read + length),
4946 pulongest (section->size));
4947 return false;
4948 }
4949
4950 /* The version number. */
4951 uint16_t version = read_2_bytes (abfd, addr);
4952 addr += 2;
4953 if (version != 5)
4954 {
4955 warning (_("Section .debug_names in %s has unsupported version %d, "
4956 "ignoring .debug_names."),
4957 filename, version);
4958 return false;
4959 }
4960
4961 /* Padding. */
4962 uint16_t padding = read_2_bytes (abfd, addr);
4963 addr += 2;
4964 if (padding != 0)
4965 {
4966 warning (_("Section .debug_names in %s has unsupported padding %d, "
4967 "ignoring .debug_names."),
4968 filename, padding);
4969 return false;
4970 }
4971
4972 /* comp_unit_count - The number of CUs in the CU list. */
4973 map.cu_count = read_4_bytes (abfd, addr);
4974 addr += 4;
4975
4976 /* local_type_unit_count - The number of TUs in the local TU
4977 list. */
4978 map.tu_count = read_4_bytes (abfd, addr);
4979 addr += 4;
4980
4981 /* foreign_type_unit_count - The number of TUs in the foreign TU
4982 list. */
4983 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4984 addr += 4;
4985 if (foreign_tu_count != 0)
4986 {
4987 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4988 "ignoring .debug_names."),
4989 filename, static_cast<unsigned long> (foreign_tu_count));
4990 return false;
4991 }
4992
4993 /* bucket_count - The number of hash buckets in the hash lookup
4994 table. */
4995 map.bucket_count = read_4_bytes (abfd, addr);
4996 addr += 4;
4997
4998 /* name_count - The number of unique names in the index. */
4999 map.name_count = read_4_bytes (abfd, addr);
5000 addr += 4;
5001
5002 /* abbrev_table_size - The size in bytes of the abbreviations
5003 table. */
5004 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5005 addr += 4;
5006
5007 /* augmentation_string_size - The size in bytes of the augmentation
5008 string. This value is rounded up to a multiple of 4. */
5009 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5010 addr += 4;
5011 map.augmentation_is_gdb = ((augmentation_string_size
5012 == sizeof (dwarf5_augmentation))
5013 && memcmp (addr, dwarf5_augmentation,
5014 sizeof (dwarf5_augmentation)) == 0);
5015 augmentation_string_size += (-augmentation_string_size) & 3;
5016 addr += augmentation_string_size;
5017
5018 /* List of CUs */
5019 map.cu_table_reordered = addr;
5020 addr += map.cu_count * map.offset_size;
5021
5022 /* List of Local TUs */
5023 map.tu_table_reordered = addr;
5024 addr += map.tu_count * map.offset_size;
5025
5026 /* Hash Lookup Table */
5027 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5028 addr += map.bucket_count * 4;
5029 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5030 addr += map.name_count * 4;
5031
5032 /* Name Table */
5033 map.name_table_string_offs_reordered = addr;
5034 addr += map.name_count * map.offset_size;
5035 map.name_table_entry_offs_reordered = addr;
5036 addr += map.name_count * map.offset_size;
5037
5038 const gdb_byte *abbrev_table_start = addr;
5039 for (;;)
5040 {
5041 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5042 addr += bytes_read;
5043 if (index_num == 0)
5044 break;
5045
5046 const auto insertpair
5047 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5048 if (!insertpair.second)
5049 {
5050 warning (_("Section .debug_names in %s has duplicate index %s, "
5051 "ignoring .debug_names."),
5052 filename, pulongest (index_num));
5053 return false;
5054 }
5055 mapped_debug_names::index_val &indexval = insertpair.first->second;
5056 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5057 addr += bytes_read;
5058
5059 for (;;)
5060 {
5061 mapped_debug_names::index_val::attr attr;
5062 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5063 addr += bytes_read;
5064 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5065 addr += bytes_read;
5066 if (attr.form == DW_FORM_implicit_const)
5067 {
5068 attr.implicit_const = read_signed_leb128 (abfd, addr,
5069 &bytes_read);
5070 addr += bytes_read;
5071 }
5072 if (attr.dw_idx == 0 && attr.form == 0)
5073 break;
5074 indexval.attr_vec.push_back (std::move (attr));
5075 }
5076 }
5077 if (addr != abbrev_table_start + abbrev_table_size)
5078 {
5079 warning (_("Section .debug_names in %s has abbreviation_table "
5080 "of size %s vs. written as %u, ignoring .debug_names."),
5081 filename, plongest (addr - abbrev_table_start),
5082 abbrev_table_size);
5083 return false;
5084 }
5085 map.entry_pool = addr;
5086
5087 return true;
5088 }
5089
5090 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5091 list. */
5092
5093 static void
5094 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
5095 const mapped_debug_names &map,
5096 dwarf2_section_info &section,
5097 bool is_dwz)
5098 {
5099 if (!map.augmentation_is_gdb)
5100 {
5101 for (uint32_t i = 0; i < map.cu_count; ++i)
5102 {
5103 sect_offset sect_off
5104 = (sect_offset) (extract_unsigned_integer
5105 (map.cu_table_reordered + i * map.offset_size,
5106 map.offset_size,
5107 map.dwarf5_byte_order));
5108 /* We don't know the length of the CU, because the CU list in a
5109 .debug_names index can be incomplete, so we can't use the start
5110 of the next CU as end of this CU. We create the CUs here with
5111 length 0, and in cutu_reader::cutu_reader we'll fill in the
5112 actual length. */
5113 dwarf2_per_cu_data *per_cu
5114 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5115 sect_off, 0);
5116 per_bfd->all_comp_units.push_back (per_cu);
5117 }
5118 return;
5119 }
5120
5121 sect_offset sect_off_prev;
5122 for (uint32_t i = 0; i <= map.cu_count; ++i)
5123 {
5124 sect_offset sect_off_next;
5125 if (i < map.cu_count)
5126 {
5127 sect_off_next
5128 = (sect_offset) (extract_unsigned_integer
5129 (map.cu_table_reordered + i * map.offset_size,
5130 map.offset_size,
5131 map.dwarf5_byte_order));
5132 }
5133 else
5134 sect_off_next = (sect_offset) section.size;
5135 if (i >= 1)
5136 {
5137 const ULONGEST length = sect_off_next - sect_off_prev;
5138 dwarf2_per_cu_data *per_cu
5139 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5140 sect_off_prev, length);
5141 per_bfd->all_comp_units.push_back (per_cu);
5142 }
5143 sect_off_prev = sect_off_next;
5144 }
5145 }
5146
5147 /* Read the CU list from the mapped index, and use it to create all
5148 the CU objects for this dwarf2_per_objfile. */
5149
5150 static void
5151 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
5152 const mapped_debug_names &map,
5153 const mapped_debug_names &dwz_map)
5154 {
5155 gdb_assert (per_bfd->all_comp_units.empty ());
5156 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5157
5158 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
5159 false /* is_dwz */);
5160
5161 if (dwz_map.cu_count == 0)
5162 return;
5163
5164 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5165 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
5166 true /* is_dwz */);
5167 }
5168
5169 /* Read .debug_names. If everything went ok, initialize the "quick"
5170 elements of all the CUs and return true. Otherwise, return false. */
5171
5172 static bool
5173 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
5174 {
5175 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5176 mapped_debug_names dwz_map;
5177 struct objfile *objfile = per_objfile->objfile;
5178 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5179
5180 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5181 &per_objfile->per_bfd->debug_names, *map))
5182 return false;
5183
5184 /* Don't use the index if it's empty. */
5185 if (map->name_count == 0)
5186 return false;
5187
5188 /* If there is a .dwz file, read it so we can get its CU list as
5189 well. */
5190 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5191 if (dwz != NULL)
5192 {
5193 if (!read_debug_names_from_section (objfile,
5194 bfd_get_filename (dwz->dwz_bfd.get ()),
5195 &dwz->debug_names, dwz_map))
5196 {
5197 warning (_("could not read '.debug_names' section from %s; skipping"),
5198 bfd_get_filename (dwz->dwz_bfd.get ()));
5199 return false;
5200 }
5201 }
5202
5203 create_cus_from_debug_names (per_bfd, *map, dwz_map);
5204
5205 if (map->tu_count != 0)
5206 {
5207 /* We can only handle a single .debug_types when we have an
5208 index. */
5209 if (per_bfd->types.size () != 1)
5210 return false;
5211
5212 dwarf2_section_info *section = &per_bfd->types[0];
5213
5214 create_signatured_type_table_from_debug_names
5215 (per_objfile, *map, section, &per_bfd->abbrev);
5216 }
5217
5218 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
5219
5220 per_bfd->debug_names_table = std::move (map);
5221 per_bfd->using_index = 1;
5222 per_bfd->quick_file_names_table =
5223 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
5224
5225 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
5226 objfiles using the same BFD. */
5227 gdb_assert (per_bfd->partial_symtabs == nullptr);
5228 per_bfd->partial_symtabs = objfile->partial_symtabs;
5229
5230 return true;
5231 }
5232
5233 /* Type used to manage iterating over all CUs looking for a symbol for
5234 .debug_names. */
5235
5236 class dw2_debug_names_iterator
5237 {
5238 public:
5239 dw2_debug_names_iterator (const mapped_debug_names &map,
5240 gdb::optional<block_enum> block_index,
5241 domain_enum domain,
5242 const char *name, dwarf2_per_objfile *per_objfile)
5243 : m_map (map), m_block_index (block_index), m_domain (domain),
5244 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5245 m_per_objfile (per_objfile)
5246 {}
5247
5248 dw2_debug_names_iterator (const mapped_debug_names &map,
5249 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
5250 : m_map (map),
5251 m_search (search),
5252 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5253 m_per_objfile (per_objfile)
5254 {}
5255
5256 dw2_debug_names_iterator (const mapped_debug_names &map,
5257 block_enum block_index, domain_enum domain,
5258 uint32_t namei, dwarf2_per_objfile *per_objfile)
5259 : m_map (map), m_block_index (block_index), m_domain (domain),
5260 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5261 m_per_objfile (per_objfile)
5262 {}
5263
5264 /* Return the next matching CU or NULL if there are no more. */
5265 dwarf2_per_cu_data *next ();
5266
5267 private:
5268 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5269 const char *name,
5270 dwarf2_per_objfile *per_objfile);
5271 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5272 uint32_t namei,
5273 dwarf2_per_objfile *per_objfile);
5274
5275 /* The internalized form of .debug_names. */
5276 const mapped_debug_names &m_map;
5277
5278 /* If set, only look for symbols that match that block. Valid values are
5279 GLOBAL_BLOCK and STATIC_BLOCK. */
5280 const gdb::optional<block_enum> m_block_index;
5281
5282 /* The kind of symbol we're looking for. */
5283 const domain_enum m_domain = UNDEF_DOMAIN;
5284 const search_domain m_search = ALL_DOMAIN;
5285
5286 /* The list of CUs from the index entry of the symbol, or NULL if
5287 not found. */
5288 const gdb_byte *m_addr;
5289
5290 dwarf2_per_objfile *m_per_objfile;
5291 };
5292
5293 const char *
5294 mapped_debug_names::namei_to_name
5295 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
5296 {
5297 const ULONGEST namei_string_offs
5298 = extract_unsigned_integer ((name_table_string_offs_reordered
5299 + namei * offset_size),
5300 offset_size,
5301 dwarf5_byte_order);
5302 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
5303 }
5304
5305 /* Find a slot in .debug_names for the object named NAME. If NAME is
5306 found, return pointer to its pool data. If NAME cannot be found,
5307 return NULL. */
5308
5309 const gdb_byte *
5310 dw2_debug_names_iterator::find_vec_in_debug_names
5311 (const mapped_debug_names &map, const char *name,
5312 dwarf2_per_objfile *per_objfile)
5313 {
5314 int (*cmp) (const char *, const char *);
5315
5316 gdb::unique_xmalloc_ptr<char> without_params;
5317 if (current_language->la_language == language_cplus
5318 || current_language->la_language == language_fortran
5319 || current_language->la_language == language_d)
5320 {
5321 /* NAME is already canonical. Drop any qualifiers as
5322 .debug_names does not contain any. */
5323
5324 if (strchr (name, '(') != NULL)
5325 {
5326 without_params = cp_remove_params (name);
5327 if (without_params != NULL)
5328 name = without_params.get ();
5329 }
5330 }
5331
5332 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5333
5334 const uint32_t full_hash = dwarf5_djb_hash (name);
5335 uint32_t namei
5336 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5337 (map.bucket_table_reordered
5338 + (full_hash % map.bucket_count)), 4,
5339 map.dwarf5_byte_order);
5340 if (namei == 0)
5341 return NULL;
5342 --namei;
5343 if (namei >= map.name_count)
5344 {
5345 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5346 "[in module %s]"),
5347 namei, map.name_count,
5348 objfile_name (per_objfile->objfile));
5349 return NULL;
5350 }
5351
5352 for (;;)
5353 {
5354 const uint32_t namei_full_hash
5355 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5356 (map.hash_table_reordered + namei), 4,
5357 map.dwarf5_byte_order);
5358 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5359 return NULL;
5360
5361 if (full_hash == namei_full_hash)
5362 {
5363 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5364
5365 #if 0 /* An expensive sanity check. */
5366 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5367 {
5368 complaint (_("Wrong .debug_names hash for string at index %u "
5369 "[in module %s]"),
5370 namei, objfile_name (dwarf2_per_objfile->objfile));
5371 return NULL;
5372 }
5373 #endif
5374
5375 if (cmp (namei_string, name) == 0)
5376 {
5377 const ULONGEST namei_entry_offs
5378 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5379 + namei * map.offset_size),
5380 map.offset_size, map.dwarf5_byte_order);
5381 return map.entry_pool + namei_entry_offs;
5382 }
5383 }
5384
5385 ++namei;
5386 if (namei >= map.name_count)
5387 return NULL;
5388 }
5389 }
5390
5391 const gdb_byte *
5392 dw2_debug_names_iterator::find_vec_in_debug_names
5393 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5394 {
5395 if (namei >= map.name_count)
5396 {
5397 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5398 "[in module %s]"),
5399 namei, map.name_count,
5400 objfile_name (per_objfile->objfile));
5401 return NULL;
5402 }
5403
5404 const ULONGEST namei_entry_offs
5405 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5406 + namei * map.offset_size),
5407 map.offset_size, map.dwarf5_byte_order);
5408 return map.entry_pool + namei_entry_offs;
5409 }
5410
5411 /* See dw2_debug_names_iterator. */
5412
5413 dwarf2_per_cu_data *
5414 dw2_debug_names_iterator::next ()
5415 {
5416 if (m_addr == NULL)
5417 return NULL;
5418
5419 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5420 struct objfile *objfile = m_per_objfile->objfile;
5421 bfd *const abfd = objfile->obfd;
5422
5423 again:
5424
5425 unsigned int bytes_read;
5426 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5427 m_addr += bytes_read;
5428 if (abbrev == 0)
5429 return NULL;
5430
5431 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5432 if (indexval_it == m_map.abbrev_map.cend ())
5433 {
5434 complaint (_("Wrong .debug_names undefined abbrev code %s "
5435 "[in module %s]"),
5436 pulongest (abbrev), objfile_name (objfile));
5437 return NULL;
5438 }
5439 const mapped_debug_names::index_val &indexval = indexval_it->second;
5440 enum class symbol_linkage {
5441 unknown,
5442 static_,
5443 extern_,
5444 } symbol_linkage_ = symbol_linkage::unknown;
5445 dwarf2_per_cu_data *per_cu = NULL;
5446 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5447 {
5448 ULONGEST ull;
5449 switch (attr.form)
5450 {
5451 case DW_FORM_implicit_const:
5452 ull = attr.implicit_const;
5453 break;
5454 case DW_FORM_flag_present:
5455 ull = 1;
5456 break;
5457 case DW_FORM_udata:
5458 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5459 m_addr += bytes_read;
5460 break;
5461 case DW_FORM_ref4:
5462 ull = read_4_bytes (abfd, m_addr);
5463 m_addr += 4;
5464 break;
5465 case DW_FORM_ref8:
5466 ull = read_8_bytes (abfd, m_addr);
5467 m_addr += 8;
5468 break;
5469 case DW_FORM_ref_sig8:
5470 ull = read_8_bytes (abfd, m_addr);
5471 m_addr += 8;
5472 break;
5473 default:
5474 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5475 dwarf_form_name (attr.form),
5476 objfile_name (objfile));
5477 return NULL;
5478 }
5479 switch (attr.dw_idx)
5480 {
5481 case DW_IDX_compile_unit:
5482 /* Don't crash on bad data. */
5483 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
5484 {
5485 complaint (_(".debug_names entry has bad CU index %s"
5486 " [in module %s]"),
5487 pulongest (ull),
5488 objfile_name (objfile));
5489 continue;
5490 }
5491 per_cu = per_bfd->get_cutu (ull);
5492 break;
5493 case DW_IDX_type_unit:
5494 /* Don't crash on bad data. */
5495 if (ull >= per_bfd->all_type_units.size ())
5496 {
5497 complaint (_(".debug_names entry has bad TU index %s"
5498 " [in module %s]"),
5499 pulongest (ull),
5500 objfile_name (objfile));
5501 continue;
5502 }
5503 per_cu = &per_bfd->get_tu (ull)->per_cu;
5504 break;
5505 case DW_IDX_die_offset:
5506 /* In a per-CU index (as opposed to a per-module index), index
5507 entries without CU attribute implicitly refer to the single CU. */
5508 if (per_cu == NULL)
5509 per_cu = per_bfd->get_cu (0);
5510 break;
5511 case DW_IDX_GNU_internal:
5512 if (!m_map.augmentation_is_gdb)
5513 break;
5514 symbol_linkage_ = symbol_linkage::static_;
5515 break;
5516 case DW_IDX_GNU_external:
5517 if (!m_map.augmentation_is_gdb)
5518 break;
5519 symbol_linkage_ = symbol_linkage::extern_;
5520 break;
5521 }
5522 }
5523
5524 /* Skip if already read in. */
5525 if (m_per_objfile->symtab_set_p (per_cu))
5526 goto again;
5527
5528 /* Check static vs global. */
5529 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5530 {
5531 const bool want_static = *m_block_index == STATIC_BLOCK;
5532 const bool symbol_is_static =
5533 symbol_linkage_ == symbol_linkage::static_;
5534 if (want_static != symbol_is_static)
5535 goto again;
5536 }
5537
5538 /* Match dw2_symtab_iter_next, symbol_kind
5539 and debug_names::psymbol_tag. */
5540 switch (m_domain)
5541 {
5542 case VAR_DOMAIN:
5543 switch (indexval.dwarf_tag)
5544 {
5545 case DW_TAG_variable:
5546 case DW_TAG_subprogram:
5547 /* Some types are also in VAR_DOMAIN. */
5548 case DW_TAG_typedef:
5549 case DW_TAG_structure_type:
5550 break;
5551 default:
5552 goto again;
5553 }
5554 break;
5555 case STRUCT_DOMAIN:
5556 switch (indexval.dwarf_tag)
5557 {
5558 case DW_TAG_typedef:
5559 case DW_TAG_structure_type:
5560 break;
5561 default:
5562 goto again;
5563 }
5564 break;
5565 case LABEL_DOMAIN:
5566 switch (indexval.dwarf_tag)
5567 {
5568 case 0:
5569 case DW_TAG_variable:
5570 break;
5571 default:
5572 goto again;
5573 }
5574 break;
5575 case MODULE_DOMAIN:
5576 switch (indexval.dwarf_tag)
5577 {
5578 case DW_TAG_module:
5579 break;
5580 default:
5581 goto again;
5582 }
5583 break;
5584 default:
5585 break;
5586 }
5587
5588 /* Match dw2_expand_symtabs_matching, symbol_kind and
5589 debug_names::psymbol_tag. */
5590 switch (m_search)
5591 {
5592 case VARIABLES_DOMAIN:
5593 switch (indexval.dwarf_tag)
5594 {
5595 case DW_TAG_variable:
5596 break;
5597 default:
5598 goto again;
5599 }
5600 break;
5601 case FUNCTIONS_DOMAIN:
5602 switch (indexval.dwarf_tag)
5603 {
5604 case DW_TAG_subprogram:
5605 break;
5606 default:
5607 goto again;
5608 }
5609 break;
5610 case TYPES_DOMAIN:
5611 switch (indexval.dwarf_tag)
5612 {
5613 case DW_TAG_typedef:
5614 case DW_TAG_structure_type:
5615 break;
5616 default:
5617 goto again;
5618 }
5619 break;
5620 case MODULES_DOMAIN:
5621 switch (indexval.dwarf_tag)
5622 {
5623 case DW_TAG_module:
5624 break;
5625 default:
5626 goto again;
5627 }
5628 default:
5629 break;
5630 }
5631
5632 return per_cu;
5633 }
5634
5635 static struct compunit_symtab *
5636 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5637 const char *name, domain_enum domain)
5638 {
5639 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5640
5641 const auto &mapp = per_objfile->per_bfd->debug_names_table;
5642 if (!mapp)
5643 {
5644 /* index is NULL if OBJF_READNOW. */
5645 return NULL;
5646 }
5647 const auto &map = *mapp;
5648
5649 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
5650
5651 struct compunit_symtab *stab_best = NULL;
5652 struct dwarf2_per_cu_data *per_cu;
5653 while ((per_cu = iter.next ()) != NULL)
5654 {
5655 struct symbol *sym, *with_opaque = NULL;
5656 compunit_symtab *stab
5657 = dw2_instantiate_symtab (per_cu, per_objfile, false);
5658 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5659 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5660
5661 sym = block_find_symbol (block, name, domain,
5662 block_find_non_opaque_type_preferred,
5663 &with_opaque);
5664
5665 /* Some caution must be observed with overloaded functions and
5666 methods, since the index will not contain any overload
5667 information (but NAME might contain it). */
5668
5669 if (sym != NULL
5670 && strcmp_iw (sym->search_name (), name) == 0)
5671 return stab;
5672 if (with_opaque != NULL
5673 && strcmp_iw (with_opaque->search_name (), name) == 0)
5674 stab_best = stab;
5675
5676 /* Keep looking through other CUs. */
5677 }
5678
5679 return stab_best;
5680 }
5681
5682 /* This dumps minimal information about .debug_names. It is called
5683 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5684 uses this to verify that .debug_names has been loaded. */
5685
5686 static void
5687 dw2_debug_names_dump (struct objfile *objfile)
5688 {
5689 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5690
5691 gdb_assert (per_objfile->per_bfd->using_index);
5692 printf_filtered (".debug_names:");
5693 if (per_objfile->per_bfd->debug_names_table)
5694 printf_filtered (" exists\n");
5695 else
5696 printf_filtered (" faked for \"readnow\"\n");
5697 printf_filtered ("\n");
5698 }
5699
5700 static void
5701 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5702 const char *func_name)
5703 {
5704 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5705
5706 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5707 if (per_objfile->per_bfd->debug_names_table)
5708 {
5709 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5710
5711 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
5712 per_objfile);
5713
5714 struct dwarf2_per_cu_data *per_cu;
5715 while ((per_cu = iter.next ()) != NULL)
5716 dw2_instantiate_symtab (per_cu, per_objfile, false);
5717 }
5718 }
5719
5720 static void
5721 dw2_debug_names_map_matching_symbols
5722 (struct objfile *objfile,
5723 const lookup_name_info &name, domain_enum domain,
5724 int global,
5725 gdb::function_view<symbol_found_callback_ftype> callback,
5726 symbol_compare_ftype *ordered_compare)
5727 {
5728 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5729
5730 /* debug_names_table is NULL if OBJF_READNOW. */
5731 if (!per_objfile->per_bfd->debug_names_table)
5732 return;
5733
5734 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5735 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5736
5737 const char *match_name = name.ada ().lookup_name ().c_str ();
5738 auto matcher = [&] (const char *symname)
5739 {
5740 if (ordered_compare == nullptr)
5741 return true;
5742 return ordered_compare (symname, match_name) == 0;
5743 };
5744
5745 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5746 [&] (offset_type namei)
5747 {
5748 /* The name was matched, now expand corresponding CUs that were
5749 marked. */
5750 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
5751 per_objfile);
5752
5753 struct dwarf2_per_cu_data *per_cu;
5754 while ((per_cu = iter.next ()) != NULL)
5755 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5756 nullptr);
5757 return true;
5758 }, per_objfile);
5759
5760 /* It's a shame we couldn't do this inside the
5761 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5762 that have already been expanded. Instead, this loop matches what
5763 the psymtab code does. */
5764 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5765 {
5766 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
5767 if (symtab != nullptr)
5768 {
5769 const struct block *block
5770 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5771 if (!iterate_over_symbols_terminated (block, name,
5772 domain, callback))
5773 break;
5774 }
5775 }
5776 }
5777
5778 static void
5779 dw2_debug_names_expand_symtabs_matching
5780 (struct objfile *objfile,
5781 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5782 const lookup_name_info *lookup_name,
5783 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5784 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5785 enum search_domain kind)
5786 {
5787 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5788
5789 /* debug_names_table is NULL if OBJF_READNOW. */
5790 if (!per_objfile->per_bfd->debug_names_table)
5791 return;
5792
5793 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5794
5795 if (symbol_matcher == NULL && lookup_name == NULL)
5796 {
5797 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5798 {
5799 QUIT;
5800
5801 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5802 expansion_notify);
5803 }
5804 return;
5805 }
5806
5807 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5808
5809 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5810 symbol_matcher,
5811 kind, [&] (offset_type namei)
5812 {
5813 /* The name was matched, now expand corresponding CUs that were
5814 marked. */
5815 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
5816
5817 struct dwarf2_per_cu_data *per_cu;
5818 while ((per_cu = iter.next ()) != NULL)
5819 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5820 expansion_notify);
5821 return true;
5822 }, per_objfile);
5823 }
5824
5825 const struct quick_symbol_functions dwarf2_debug_names_functions =
5826 {
5827 dw2_has_symbols,
5828 dw2_find_last_source_symtab,
5829 dw2_forget_cached_source_info,
5830 dw2_map_symtabs_matching_filename,
5831 dw2_debug_names_lookup_symbol,
5832 NULL,
5833 dw2_print_stats,
5834 dw2_debug_names_dump,
5835 dw2_debug_names_expand_symtabs_for_function,
5836 dw2_expand_all_symtabs,
5837 dw2_expand_symtabs_with_fullname,
5838 dw2_debug_names_map_matching_symbols,
5839 dw2_debug_names_expand_symtabs_matching,
5840 dw2_find_pc_sect_compunit_symtab,
5841 NULL,
5842 dw2_map_symbol_filenames
5843 };
5844
5845 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5846 to either a dwarf2_per_bfd or dwz_file object. */
5847
5848 template <typename T>
5849 static gdb::array_view<const gdb_byte>
5850 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5851 {
5852 dwarf2_section_info *section = &section_owner->gdb_index;
5853
5854 if (section->empty ())
5855 return {};
5856
5857 /* Older elfutils strip versions could keep the section in the main
5858 executable while splitting it for the separate debug info file. */
5859 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5860 return {};
5861
5862 section->read (obj);
5863
5864 /* dwarf2_section_info::size is a bfd_size_type, while
5865 gdb::array_view works with size_t. On 32-bit hosts, with
5866 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5867 is 32-bit. So we need an explicit narrowing conversion here.
5868 This is fine, because it's impossible to allocate or mmap an
5869 array/buffer larger than what size_t can represent. */
5870 return gdb::make_array_view (section->buffer, section->size);
5871 }
5872
5873 /* Lookup the index cache for the contents of the index associated to
5874 DWARF2_OBJ. */
5875
5876 static gdb::array_view<const gdb_byte>
5877 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5878 {
5879 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5880 if (build_id == nullptr)
5881 return {};
5882
5883 return global_index_cache.lookup_gdb_index (build_id,
5884 &dwarf2_per_bfd->index_cache_res);
5885 }
5886
5887 /* Same as the above, but for DWZ. */
5888
5889 static gdb::array_view<const gdb_byte>
5890 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5891 {
5892 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5893 if (build_id == nullptr)
5894 return {};
5895
5896 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5897 }
5898
5899 /* See symfile.h. */
5900
5901 bool
5902 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5903 {
5904 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5905 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5906
5907 dwarf_read_debug_printf ("called");
5908
5909 /* If we're about to read full symbols, don't bother with the
5910 indices. In this case we also don't care if some other debug
5911 format is making psymtabs, because they are all about to be
5912 expanded anyway. */
5913 if ((objfile->flags & OBJF_READNOW))
5914 {
5915 dwarf_read_debug_printf ("readnow requested");
5916
5917 /* When using READNOW, the using_index flag (set below) indicates that
5918 PER_BFD was already initialized, when we loaded some other objfile. */
5919 if (per_bfd->using_index)
5920 {
5921 dwarf_read_debug_printf ("using_index already set");
5922 *index_kind = dw_index_kind::GDB_INDEX;
5923 per_objfile->resize_symtabs ();
5924 return true;
5925 }
5926
5927 per_bfd->using_index = 1;
5928 create_all_comp_units (per_objfile);
5929 create_all_type_units (per_objfile);
5930 per_bfd->quick_file_names_table
5931 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
5932 per_objfile->resize_symtabs ();
5933
5934 for (int i = 0; i < (per_bfd->all_comp_units.size ()
5935 + per_bfd->all_type_units.size ()); ++i)
5936 {
5937 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
5938
5939 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
5940 struct dwarf2_per_cu_quick_data);
5941 }
5942
5943 /* Return 1 so that gdb sees the "quick" functions. However,
5944 these functions will be no-ops because we will have expanded
5945 all symtabs. */
5946 *index_kind = dw_index_kind::GDB_INDEX;
5947 return true;
5948 }
5949
5950 /* Was a debug names index already read when we processed an objfile sharing
5951 PER_BFD? */
5952 if (per_bfd->debug_names_table != nullptr)
5953 {
5954 dwarf_read_debug_printf ("re-using shared debug names table");
5955 *index_kind = dw_index_kind::DEBUG_NAMES;
5956 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
5957 per_objfile->resize_symtabs ();
5958 return true;
5959 }
5960
5961 /* Was a GDB index already read when we processed an objfile sharing
5962 PER_BFD? */
5963 if (per_bfd->index_table != nullptr)
5964 {
5965 dwarf_read_debug_printf ("re-using shared index table");
5966 *index_kind = dw_index_kind::GDB_INDEX;
5967 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
5968 per_objfile->resize_symtabs ();
5969 return true;
5970 }
5971
5972 /* There might already be partial symtabs built for this BFD. This happens
5973 when loading the same binary twice with the index-cache enabled. If so,
5974 don't try to read an index. The objfile / per_objfile initialization will
5975 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
5976 code path. */
5977 if (per_bfd->partial_symtabs != nullptr)
5978 {
5979 dwarf_read_debug_printf ("re-using shared partial symtabs");
5980 return false;
5981 }
5982
5983 if (dwarf2_read_debug_names (per_objfile))
5984 {
5985 dwarf_read_debug_printf ("found debug names");
5986 *index_kind = dw_index_kind::DEBUG_NAMES;
5987 per_objfile->resize_symtabs ();
5988 return true;
5989 }
5990
5991 if (dwarf2_read_gdb_index (per_objfile,
5992 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5993 get_gdb_index_contents_from_section<dwz_file>))
5994 {
5995 dwarf_read_debug_printf ("found gdb index from file");
5996 *index_kind = dw_index_kind::GDB_INDEX;
5997 per_objfile->resize_symtabs ();
5998 return true;
5999 }
6000
6001 /* ... otherwise, try to find the index in the index cache. */
6002 if (dwarf2_read_gdb_index (per_objfile,
6003 get_gdb_index_contents_from_cache,
6004 get_gdb_index_contents_from_cache_dwz))
6005 {
6006 dwarf_read_debug_printf ("found gdb index from cache");
6007 global_index_cache.hit ();
6008 *index_kind = dw_index_kind::GDB_INDEX;
6009 per_objfile->resize_symtabs ();
6010 return true;
6011 }
6012
6013 global_index_cache.miss ();
6014 return false;
6015 }
6016
6017 \f
6018
6019 /* Build a partial symbol table. */
6020
6021 void
6022 dwarf2_build_psymtabs (struct objfile *objfile)
6023 {
6024 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6025 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6026
6027 if (per_bfd->partial_symtabs != nullptr)
6028 {
6029 /* Partial symbols were already read, so now we can simply
6030 attach them. */
6031 objfile->partial_symtabs = per_bfd->partial_symtabs;
6032 per_objfile->resize_symtabs ();
6033 return;
6034 }
6035
6036 try
6037 {
6038 /* This isn't really ideal: all the data we allocate on the
6039 objfile's obstack is still uselessly kept around. However,
6040 freeing it seems unsafe. */
6041 psymtab_discarder psymtabs (objfile);
6042 dwarf2_build_psymtabs_hard (per_objfile);
6043 psymtabs.keep ();
6044
6045 per_objfile->resize_symtabs ();
6046
6047 /* (maybe) store an index in the cache. */
6048 global_index_cache.store (per_objfile);
6049 }
6050 catch (const gdb_exception_error &except)
6051 {
6052 exception_print (gdb_stderr, except);
6053 }
6054
6055 /* Finish by setting the local reference to partial symtabs, so that
6056 we don't try to read them again if reading another objfile with the same
6057 BFD. If we can't in fact share, this won't make a difference anyway as
6058 the dwarf2_per_bfd object won't be shared. */
6059 per_bfd->partial_symtabs = objfile->partial_symtabs;
6060 }
6061
6062 /* Find the base address of the compilation unit for range lists and
6063 location lists. It will normally be specified by DW_AT_low_pc.
6064 In DWARF-3 draft 4, the base address could be overridden by
6065 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6066 compilation units with discontinuous ranges. */
6067
6068 static void
6069 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6070 {
6071 struct attribute *attr;
6072
6073 cu->base_address.reset ();
6074
6075 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6076 if (attr != nullptr)
6077 cu->base_address = attr->as_address ();
6078 else
6079 {
6080 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6081 if (attr != nullptr)
6082 cu->base_address = attr->as_address ();
6083 }
6084 }
6085
6086 /* Helper function that returns the proper abbrev section for
6087 THIS_CU. */
6088
6089 static struct dwarf2_section_info *
6090 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6091 {
6092 struct dwarf2_section_info *abbrev;
6093 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6094
6095 if (this_cu->is_dwz)
6096 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
6097 else
6098 abbrev = &per_bfd->abbrev;
6099
6100 return abbrev;
6101 }
6102
6103 /* Fetch the abbreviation table offset from a comp or type unit header. */
6104
6105 static sect_offset
6106 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
6107 struct dwarf2_section_info *section,
6108 sect_offset sect_off)
6109 {
6110 bfd *abfd = section->get_bfd_owner ();
6111 const gdb_byte *info_ptr;
6112 unsigned int initial_length_size, offset_size;
6113 uint16_t version;
6114
6115 section->read (per_objfile->objfile);
6116 info_ptr = section->buffer + to_underlying (sect_off);
6117 read_initial_length (abfd, info_ptr, &initial_length_size);
6118 offset_size = initial_length_size == 4 ? 4 : 8;
6119 info_ptr += initial_length_size;
6120
6121 version = read_2_bytes (abfd, info_ptr);
6122 info_ptr += 2;
6123 if (version >= 5)
6124 {
6125 /* Skip unit type and address size. */
6126 info_ptr += 2;
6127 }
6128
6129 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6130 }
6131
6132 /* A partial symtab that is used only for include files. */
6133 struct dwarf2_include_psymtab : public partial_symtab
6134 {
6135 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6136 : partial_symtab (filename, objfile)
6137 {
6138 }
6139
6140 void read_symtab (struct objfile *objfile) override
6141 {
6142 /* It's an include file, no symbols to read for it.
6143 Everything is in the includer symtab. */
6144
6145 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6146 expansion of the includer psymtab. We use the dependencies[0] field to
6147 model the includer. But if we go the regular route of calling
6148 expand_psymtab here, and having expand_psymtab call expand_dependencies
6149 to expand the includer, we'll only use expand_psymtab on the includer
6150 (making it a non-toplevel psymtab), while if we expand the includer via
6151 another path, we'll use read_symtab (making it a toplevel psymtab).
6152 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6153 psymtab, and trigger read_symtab on the includer here directly. */
6154 includer ()->read_symtab (objfile);
6155 }
6156
6157 void expand_psymtab (struct objfile *objfile) override
6158 {
6159 /* This is not called by read_symtab, and should not be called by any
6160 expand_dependencies. */
6161 gdb_assert (false);
6162 }
6163
6164 bool readin_p (struct objfile *objfile) const override
6165 {
6166 return includer ()->readin_p (objfile);
6167 }
6168
6169 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6170 {
6171 return nullptr;
6172 }
6173
6174 private:
6175 partial_symtab *includer () const
6176 {
6177 /* An include psymtab has exactly one dependency: the psymtab that
6178 includes it. */
6179 gdb_assert (this->number_of_dependencies == 1);
6180 return this->dependencies[0];
6181 }
6182 };
6183
6184 /* Allocate a new partial symtab for file named NAME and mark this new
6185 partial symtab as being an include of PST. */
6186
6187 static void
6188 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6189 struct objfile *objfile)
6190 {
6191 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6192
6193 if (!IS_ABSOLUTE_PATH (subpst->filename))
6194 subpst->dirname = pst->dirname;
6195
6196 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6197 subpst->dependencies[0] = pst;
6198 subpst->number_of_dependencies = 1;
6199 }
6200
6201 /* Read the Line Number Program data and extract the list of files
6202 included by the source file represented by PST. Build an include
6203 partial symtab for each of these included files. */
6204
6205 static void
6206 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6207 struct die_info *die,
6208 dwarf2_psymtab *pst)
6209 {
6210 line_header_up lh;
6211 struct attribute *attr;
6212
6213 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6214 if (attr != nullptr && attr->form_is_unsigned ())
6215 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
6216 if (lh == NULL)
6217 return; /* No linetable, so no includes. */
6218
6219 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6220 that we pass in the raw text_low here; that is ok because we're
6221 only decoding the line table to make include partial symtabs, and
6222 so the addresses aren't really used. */
6223 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6224 pst->raw_text_low (), 1);
6225 }
6226
6227 static hashval_t
6228 hash_signatured_type (const void *item)
6229 {
6230 const struct signatured_type *sig_type
6231 = (const struct signatured_type *) item;
6232
6233 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6234 return sig_type->signature;
6235 }
6236
6237 static int
6238 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6239 {
6240 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6241 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6242
6243 return lhs->signature == rhs->signature;
6244 }
6245
6246 /* Allocate a hash table for signatured types. */
6247
6248 static htab_up
6249 allocate_signatured_type_table ()
6250 {
6251 return htab_up (htab_create_alloc (41,
6252 hash_signatured_type,
6253 eq_signatured_type,
6254 NULL, xcalloc, xfree));
6255 }
6256
6257 /* A helper function to add a signatured type CU to a table. */
6258
6259 static int
6260 add_signatured_type_cu_to_table (void **slot, void *datum)
6261 {
6262 struct signatured_type *sigt = (struct signatured_type *) *slot;
6263 std::vector<signatured_type *> *all_type_units
6264 = (std::vector<signatured_type *> *) datum;
6265
6266 all_type_units->push_back (sigt);
6267
6268 return 1;
6269 }
6270
6271 /* A helper for create_debug_types_hash_table. Read types from SECTION
6272 and fill them into TYPES_HTAB. It will process only type units,
6273 therefore DW_UT_type. */
6274
6275 static void
6276 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
6277 struct dwo_file *dwo_file,
6278 dwarf2_section_info *section, htab_up &types_htab,
6279 rcuh_kind section_kind)
6280 {
6281 struct objfile *objfile = per_objfile->objfile;
6282 struct dwarf2_section_info *abbrev_section;
6283 bfd *abfd;
6284 const gdb_byte *info_ptr, *end_ptr;
6285
6286 abbrev_section = (dwo_file != NULL
6287 ? &dwo_file->sections.abbrev
6288 : &per_objfile->per_bfd->abbrev);
6289
6290 dwarf_read_debug_printf ("Reading %s for %s",
6291 section->get_name (),
6292 abbrev_section->get_file_name ());
6293
6294 section->read (objfile);
6295 info_ptr = section->buffer;
6296
6297 if (info_ptr == NULL)
6298 return;
6299
6300 /* We can't set abfd until now because the section may be empty or
6301 not present, in which case the bfd is unknown. */
6302 abfd = section->get_bfd_owner ();
6303
6304 /* We don't use cutu_reader here because we don't need to read
6305 any dies: the signature is in the header. */
6306
6307 end_ptr = info_ptr + section->size;
6308 while (info_ptr < end_ptr)
6309 {
6310 struct signatured_type *sig_type;
6311 struct dwo_unit *dwo_tu;
6312 void **slot;
6313 const gdb_byte *ptr = info_ptr;
6314 struct comp_unit_head header;
6315 unsigned int length;
6316
6317 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6318
6319 /* Initialize it due to a false compiler warning. */
6320 header.signature = -1;
6321 header.type_cu_offset_in_tu = (cu_offset) -1;
6322
6323 /* We need to read the type's signature in order to build the hash
6324 table, but we don't need anything else just yet. */
6325
6326 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
6327 abbrev_section, ptr, section_kind);
6328
6329 length = header.get_length ();
6330
6331 /* Skip dummy type units. */
6332 if (ptr >= info_ptr + length
6333 || peek_abbrev_code (abfd, ptr) == 0
6334 || (header.unit_type != DW_UT_type
6335 && header.unit_type != DW_UT_split_type))
6336 {
6337 info_ptr += length;
6338 continue;
6339 }
6340
6341 if (types_htab == NULL)
6342 {
6343 if (dwo_file)
6344 types_htab = allocate_dwo_unit_table ();
6345 else
6346 types_htab = allocate_signatured_type_table ();
6347 }
6348
6349 if (dwo_file)
6350 {
6351 sig_type = NULL;
6352 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
6353 dwo_tu->dwo_file = dwo_file;
6354 dwo_tu->signature = header.signature;
6355 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6356 dwo_tu->section = section;
6357 dwo_tu->sect_off = sect_off;
6358 dwo_tu->length = length;
6359 }
6360 else
6361 {
6362 /* N.B.: type_offset is not usable if this type uses a DWO file.
6363 The real type_offset is in the DWO file. */
6364 dwo_tu = NULL;
6365 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6366 sig_type->signature = header.signature;
6367 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6368 sig_type->per_cu.is_debug_types = 1;
6369 sig_type->per_cu.section = section;
6370 sig_type->per_cu.sect_off = sect_off;
6371 sig_type->per_cu.length = length;
6372 }
6373
6374 slot = htab_find_slot (types_htab.get (),
6375 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6376 INSERT);
6377 gdb_assert (slot != NULL);
6378 if (*slot != NULL)
6379 {
6380 sect_offset dup_sect_off;
6381
6382 if (dwo_file)
6383 {
6384 const struct dwo_unit *dup_tu
6385 = (const struct dwo_unit *) *slot;
6386
6387 dup_sect_off = dup_tu->sect_off;
6388 }
6389 else
6390 {
6391 const struct signatured_type *dup_tu
6392 = (const struct signatured_type *) *slot;
6393
6394 dup_sect_off = dup_tu->per_cu.sect_off;
6395 }
6396
6397 complaint (_("debug type entry at offset %s is duplicate to"
6398 " the entry at offset %s, signature %s"),
6399 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6400 hex_string (header.signature));
6401 }
6402 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6403
6404 dwarf_read_debug_printf_v (" offset %s, signature %s",
6405 sect_offset_str (sect_off),
6406 hex_string (header.signature));
6407
6408 info_ptr += length;
6409 }
6410 }
6411
6412 /* Create the hash table of all entries in the .debug_types
6413 (or .debug_types.dwo) section(s).
6414 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6415 otherwise it is NULL.
6416
6417 The result is a pointer to the hash table or NULL if there are no types.
6418
6419 Note: This function processes DWO files only, not DWP files. */
6420
6421 static void
6422 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
6423 struct dwo_file *dwo_file,
6424 gdb::array_view<dwarf2_section_info> type_sections,
6425 htab_up &types_htab)
6426 {
6427 for (dwarf2_section_info &section : type_sections)
6428 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6429 rcuh_kind::TYPE);
6430 }
6431
6432 /* Create the hash table of all entries in the .debug_types section,
6433 and initialize all_type_units.
6434 The result is zero if there is an error (e.g. missing .debug_types section),
6435 otherwise non-zero. */
6436
6437 static int
6438 create_all_type_units (dwarf2_per_objfile *per_objfile)
6439 {
6440 htab_up types_htab;
6441
6442 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6443 types_htab, rcuh_kind::COMPILE);
6444 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6445 types_htab);
6446 if (types_htab == NULL)
6447 {
6448 per_objfile->per_bfd->signatured_types = NULL;
6449 return 0;
6450 }
6451
6452 per_objfile->per_bfd->signatured_types = std::move (types_htab);
6453
6454 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6455 per_objfile->per_bfd->all_type_units.reserve
6456 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
6457
6458 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
6459 add_signatured_type_cu_to_table,
6460 &per_objfile->per_bfd->all_type_units);
6461
6462 return 1;
6463 }
6464
6465 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6466 If SLOT is non-NULL, it is the entry to use in the hash table.
6467 Otherwise we find one. */
6468
6469 static struct signatured_type *
6470 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6471 {
6472 if (per_objfile->per_bfd->all_type_units.size ()
6473 == per_objfile->per_bfd->all_type_units.capacity ())
6474 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6475
6476 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6477
6478 per_objfile->resize_symtabs ();
6479
6480 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6481 sig_type->signature = sig;
6482 sig_type->per_cu.is_debug_types = 1;
6483 if (per_objfile->per_bfd->using_index)
6484 {
6485 sig_type->per_cu.v.quick =
6486 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6487 struct dwarf2_per_cu_quick_data);
6488 }
6489
6490 if (slot == NULL)
6491 {
6492 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6493 sig_type, INSERT);
6494 }
6495 gdb_assert (*slot == NULL);
6496 *slot = sig_type;
6497 /* The rest of sig_type must be filled in by the caller. */
6498 return sig_type;
6499 }
6500
6501 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6502 Fill in SIG_ENTRY with DWO_ENTRY. */
6503
6504 static void
6505 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
6506 struct signatured_type *sig_entry,
6507 struct dwo_unit *dwo_entry)
6508 {
6509 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6510
6511 /* Make sure we're not clobbering something we don't expect to. */
6512 gdb_assert (! sig_entry->per_cu.queued);
6513 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
6514 if (per_bfd->using_index)
6515 {
6516 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6517 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6518 }
6519 else
6520 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6521 gdb_assert (sig_entry->signature == dwo_entry->signature);
6522 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6523 gdb_assert (sig_entry->type_unit_group == NULL);
6524 gdb_assert (sig_entry->dwo_unit == NULL);
6525
6526 sig_entry->per_cu.section = dwo_entry->section;
6527 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6528 sig_entry->per_cu.length = dwo_entry->length;
6529 sig_entry->per_cu.reading_dwo_directly = 1;
6530 sig_entry->per_cu.per_bfd = per_bfd;
6531 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6532 sig_entry->dwo_unit = dwo_entry;
6533 }
6534
6535 /* Subroutine of lookup_signatured_type.
6536 If we haven't read the TU yet, create the signatured_type data structure
6537 for a TU to be read in directly from a DWO file, bypassing the stub.
6538 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6539 using .gdb_index, then when reading a CU we want to stay in the DWO file
6540 containing that CU. Otherwise we could end up reading several other DWO
6541 files (due to comdat folding) to process the transitive closure of all the
6542 mentioned TUs, and that can be slow. The current DWO file will have every
6543 type signature that it needs.
6544 We only do this for .gdb_index because in the psymtab case we already have
6545 to read all the DWOs to build the type unit groups. */
6546
6547 static struct signatured_type *
6548 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6549 {
6550 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6551 struct dwo_file *dwo_file;
6552 struct dwo_unit find_dwo_entry, *dwo_entry;
6553 struct signatured_type find_sig_entry, *sig_entry;
6554 void **slot;
6555
6556 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6557
6558 /* If TU skeletons have been removed then we may not have read in any
6559 TUs yet. */
6560 if (per_objfile->per_bfd->signatured_types == NULL)
6561 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6562
6563 /* We only ever need to read in one copy of a signatured type.
6564 Use the global signatured_types array to do our own comdat-folding
6565 of types. If this is the first time we're reading this TU, and
6566 the TU has an entry in .gdb_index, replace the recorded data from
6567 .gdb_index with this TU. */
6568
6569 find_sig_entry.signature = sig;
6570 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6571 &find_sig_entry, INSERT);
6572 sig_entry = (struct signatured_type *) *slot;
6573
6574 /* We can get here with the TU already read, *or* in the process of being
6575 read. Don't reassign the global entry to point to this DWO if that's
6576 the case. Also note that if the TU is already being read, it may not
6577 have come from a DWO, the program may be a mix of Fission-compiled
6578 code and non-Fission-compiled code. */
6579
6580 /* Have we already tried to read this TU?
6581 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6582 needn't exist in the global table yet). */
6583 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6584 return sig_entry;
6585
6586 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6587 dwo_unit of the TU itself. */
6588 dwo_file = cu->dwo_unit->dwo_file;
6589
6590 /* Ok, this is the first time we're reading this TU. */
6591 if (dwo_file->tus == NULL)
6592 return NULL;
6593 find_dwo_entry.signature = sig;
6594 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6595 &find_dwo_entry);
6596 if (dwo_entry == NULL)
6597 return NULL;
6598
6599 /* If the global table doesn't have an entry for this TU, add one. */
6600 if (sig_entry == NULL)
6601 sig_entry = add_type_unit (per_objfile, sig, slot);
6602
6603 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6604 sig_entry->per_cu.tu_read = 1;
6605 return sig_entry;
6606 }
6607
6608 /* Subroutine of lookup_signatured_type.
6609 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6610 then try the DWP file. If the TU stub (skeleton) has been removed then
6611 it won't be in .gdb_index. */
6612
6613 static struct signatured_type *
6614 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6615 {
6616 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6617 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6618 struct dwo_unit *dwo_entry;
6619 struct signatured_type find_sig_entry, *sig_entry;
6620 void **slot;
6621
6622 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6623 gdb_assert (dwp_file != NULL);
6624
6625 /* If TU skeletons have been removed then we may not have read in any
6626 TUs yet. */
6627 if (per_objfile->per_bfd->signatured_types == NULL)
6628 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6629
6630 find_sig_entry.signature = sig;
6631 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6632 &find_sig_entry, INSERT);
6633 sig_entry = (struct signatured_type *) *slot;
6634
6635 /* Have we already tried to read this TU?
6636 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6637 needn't exist in the global table yet). */
6638 if (sig_entry != NULL)
6639 return sig_entry;
6640
6641 if (dwp_file->tus == NULL)
6642 return NULL;
6643 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6644 1 /* is_debug_types */);
6645 if (dwo_entry == NULL)
6646 return NULL;
6647
6648 sig_entry = add_type_unit (per_objfile, sig, slot);
6649 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6650
6651 return sig_entry;
6652 }
6653
6654 /* Lookup a signature based type for DW_FORM_ref_sig8.
6655 Returns NULL if signature SIG is not present in the table.
6656 It is up to the caller to complain about this. */
6657
6658 static struct signatured_type *
6659 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6660 {
6661 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6662
6663 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6664 {
6665 /* We're in a DWO/DWP file, and we're using .gdb_index.
6666 These cases require special processing. */
6667 if (get_dwp_file (per_objfile) == NULL)
6668 return lookup_dwo_signatured_type (cu, sig);
6669 else
6670 return lookup_dwp_signatured_type (cu, sig);
6671 }
6672 else
6673 {
6674 struct signatured_type find_entry, *entry;
6675
6676 if (per_objfile->per_bfd->signatured_types == NULL)
6677 return NULL;
6678 find_entry.signature = sig;
6679 entry = ((struct signatured_type *)
6680 htab_find (per_objfile->per_bfd->signatured_types.get (),
6681 &find_entry));
6682 return entry;
6683 }
6684 }
6685
6686 /* Low level DIE reading support. */
6687
6688 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6689
6690 static void
6691 init_cu_die_reader (struct die_reader_specs *reader,
6692 struct dwarf2_cu *cu,
6693 struct dwarf2_section_info *section,
6694 struct dwo_file *dwo_file,
6695 struct abbrev_table *abbrev_table)
6696 {
6697 gdb_assert (section->readin && section->buffer != NULL);
6698 reader->abfd = section->get_bfd_owner ();
6699 reader->cu = cu;
6700 reader->dwo_file = dwo_file;
6701 reader->die_section = section;
6702 reader->buffer = section->buffer;
6703 reader->buffer_end = section->buffer + section->size;
6704 reader->abbrev_table = abbrev_table;
6705 }
6706
6707 /* Subroutine of cutu_reader to simplify it.
6708 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6709 There's just a lot of work to do, and cutu_reader is big enough
6710 already.
6711
6712 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6713 from it to the DIE in the DWO. If NULL we are skipping the stub.
6714 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6715 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6716 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6717 STUB_COMP_DIR may be non-NULL.
6718 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6719 are filled in with the info of the DIE from the DWO file.
6720 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6721 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6722 kept around for at least as long as *RESULT_READER.
6723
6724 The result is non-zero if a valid (non-dummy) DIE was found. */
6725
6726 static int
6727 read_cutu_die_from_dwo (dwarf2_cu *cu,
6728 struct dwo_unit *dwo_unit,
6729 struct die_info *stub_comp_unit_die,
6730 const char *stub_comp_dir,
6731 struct die_reader_specs *result_reader,
6732 const gdb_byte **result_info_ptr,
6733 struct die_info **result_comp_unit_die,
6734 abbrev_table_up *result_dwo_abbrev_table)
6735 {
6736 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6737 dwarf2_per_cu_data *per_cu = cu->per_cu;
6738 struct objfile *objfile = per_objfile->objfile;
6739 bfd *abfd;
6740 const gdb_byte *begin_info_ptr, *info_ptr;
6741 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6742 int i,num_extra_attrs;
6743 struct dwarf2_section_info *dwo_abbrev_section;
6744 struct die_info *comp_unit_die;
6745
6746 /* At most one of these may be provided. */
6747 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6748
6749 /* These attributes aren't processed until later:
6750 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6751 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6752 referenced later. However, these attributes are found in the stub
6753 which we won't have later. In order to not impose this complication
6754 on the rest of the code, we read them here and copy them to the
6755 DWO CU/TU die. */
6756
6757 stmt_list = NULL;
6758 low_pc = NULL;
6759 high_pc = NULL;
6760 ranges = NULL;
6761 comp_dir = NULL;
6762
6763 if (stub_comp_unit_die != NULL)
6764 {
6765 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6766 DWO file. */
6767 if (!per_cu->is_debug_types)
6768 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6769 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6770 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6771 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6772 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6773
6774 cu->addr_base = stub_comp_unit_die->addr_base ();
6775
6776 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6777 We need the value before we can process DW_AT_ranges values from the
6778 DWO. */
6779 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6780
6781 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6782 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6783 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6784 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6785 section. */
6786 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
6787 }
6788 else if (stub_comp_dir != NULL)
6789 {
6790 /* Reconstruct the comp_dir attribute to simplify the code below. */
6791 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
6792 comp_dir->name = DW_AT_comp_dir;
6793 comp_dir->form = DW_FORM_string;
6794 comp_dir->set_string_noncanonical (stub_comp_dir);
6795 }
6796
6797 /* Set up for reading the DWO CU/TU. */
6798 cu->dwo_unit = dwo_unit;
6799 dwarf2_section_info *section = dwo_unit->section;
6800 section->read (objfile);
6801 abfd = section->get_bfd_owner ();
6802 begin_info_ptr = info_ptr = (section->buffer
6803 + to_underlying (dwo_unit->sect_off));
6804 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6805
6806 if (per_cu->is_debug_types)
6807 {
6808 signatured_type *sig_type = (struct signatured_type *) per_cu;
6809
6810 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6811 section, dwo_abbrev_section,
6812 info_ptr, rcuh_kind::TYPE);
6813 /* This is not an assert because it can be caused by bad debug info. */
6814 if (sig_type->signature != cu->header.signature)
6815 {
6816 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6817 " TU at offset %s [in module %s]"),
6818 hex_string (sig_type->signature),
6819 hex_string (cu->header.signature),
6820 sect_offset_str (dwo_unit->sect_off),
6821 bfd_get_filename (abfd));
6822 }
6823 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6824 /* For DWOs coming from DWP files, we don't know the CU length
6825 nor the type's offset in the TU until now. */
6826 dwo_unit->length = cu->header.get_length ();
6827 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6828
6829 /* Establish the type offset that can be used to lookup the type.
6830 For DWO files, we don't know it until now. */
6831 sig_type->type_offset_in_section
6832 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6833 }
6834 else
6835 {
6836 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6837 section, dwo_abbrev_section,
6838 info_ptr, rcuh_kind::COMPILE);
6839 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6840 /* For DWOs coming from DWP files, we don't know the CU length
6841 until now. */
6842 dwo_unit->length = cu->header.get_length ();
6843 }
6844
6845 dwo_abbrev_section->read (objfile);
6846 *result_dwo_abbrev_table
6847 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
6848 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6849 result_dwo_abbrev_table->get ());
6850
6851 /* Read in the die, but leave space to copy over the attributes
6852 from the stub. This has the benefit of simplifying the rest of
6853 the code - all the work to maintain the illusion of a single
6854 DW_TAG_{compile,type}_unit DIE is done here. */
6855 num_extra_attrs = ((stmt_list != NULL)
6856 + (low_pc != NULL)
6857 + (high_pc != NULL)
6858 + (ranges != NULL)
6859 + (comp_dir != NULL));
6860 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6861 num_extra_attrs);
6862
6863 /* Copy over the attributes from the stub to the DIE we just read in. */
6864 comp_unit_die = *result_comp_unit_die;
6865 i = comp_unit_die->num_attrs;
6866 if (stmt_list != NULL)
6867 comp_unit_die->attrs[i++] = *stmt_list;
6868 if (low_pc != NULL)
6869 comp_unit_die->attrs[i++] = *low_pc;
6870 if (high_pc != NULL)
6871 comp_unit_die->attrs[i++] = *high_pc;
6872 if (ranges != NULL)
6873 comp_unit_die->attrs[i++] = *ranges;
6874 if (comp_dir != NULL)
6875 comp_unit_die->attrs[i++] = *comp_dir;
6876 comp_unit_die->num_attrs += num_extra_attrs;
6877
6878 if (dwarf_die_debug)
6879 {
6880 fprintf_unfiltered (gdb_stdlog,
6881 "Read die from %s@0x%x of %s:\n",
6882 section->get_name (),
6883 (unsigned) (begin_info_ptr - section->buffer),
6884 bfd_get_filename (abfd));
6885 dump_die (comp_unit_die, dwarf_die_debug);
6886 }
6887
6888 /* Skip dummy compilation units. */
6889 if (info_ptr >= begin_info_ptr + dwo_unit->length
6890 || peek_abbrev_code (abfd, info_ptr) == 0)
6891 return 0;
6892
6893 *result_info_ptr = info_ptr;
6894 return 1;
6895 }
6896
6897 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6898 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6899 signature is part of the header. */
6900 static gdb::optional<ULONGEST>
6901 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6902 {
6903 if (cu->header.version >= 5)
6904 return cu->header.signature;
6905 struct attribute *attr;
6906 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6907 if (attr == nullptr || !attr->form_is_unsigned ())
6908 return gdb::optional<ULONGEST> ();
6909 return attr->as_unsigned ();
6910 }
6911
6912 /* Subroutine of cutu_reader to simplify it.
6913 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6914 Returns NULL if the specified DWO unit cannot be found. */
6915
6916 static struct dwo_unit *
6917 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6918 {
6919 dwarf2_per_cu_data *per_cu = cu->per_cu;
6920 struct dwo_unit *dwo_unit;
6921 const char *comp_dir;
6922
6923 gdb_assert (cu != NULL);
6924
6925 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6926 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6927 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6928
6929 if (per_cu->is_debug_types)
6930 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6931 else
6932 {
6933 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6934
6935 if (!signature.has_value ())
6936 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6937 " [in module %s]"),
6938 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6939
6940 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6941 }
6942
6943 return dwo_unit;
6944 }
6945
6946 /* Subroutine of cutu_reader to simplify it.
6947 See it for a description of the parameters.
6948 Read a TU directly from a DWO file, bypassing the stub. */
6949
6950 void
6951 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6952 dwarf2_per_objfile *per_objfile,
6953 dwarf2_cu *existing_cu)
6954 {
6955 struct signatured_type *sig_type;
6956
6957 /* Verify we can do the following downcast, and that we have the
6958 data we need. */
6959 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6960 sig_type = (struct signatured_type *) this_cu;
6961 gdb_assert (sig_type->dwo_unit != NULL);
6962
6963 dwarf2_cu *cu;
6964
6965 if (existing_cu != nullptr)
6966 {
6967 cu = existing_cu;
6968 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6969 /* There's no need to do the rereading_dwo_cu handling that
6970 cutu_reader does since we don't read the stub. */
6971 }
6972 else
6973 {
6974 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6975 in per_objfile yet. */
6976 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6977 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6978 cu = m_new_cu.get ();
6979 }
6980
6981 /* A future optimization, if needed, would be to use an existing
6982 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6983 could share abbrev tables. */
6984
6985 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
6986 NULL /* stub_comp_unit_die */,
6987 sig_type->dwo_unit->dwo_file->comp_dir,
6988 this, &info_ptr,
6989 &comp_unit_die,
6990 &m_dwo_abbrev_table) == 0)
6991 {
6992 /* Dummy die. */
6993 dummy_p = true;
6994 }
6995 }
6996
6997 /* Initialize a CU (or TU) and read its DIEs.
6998 If the CU defers to a DWO file, read the DWO file as well.
6999
7000 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7001 Otherwise the table specified in the comp unit header is read in and used.
7002 This is an optimization for when we already have the abbrev table.
7003
7004 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7005 allocated. */
7006
7007 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7008 dwarf2_per_objfile *per_objfile,
7009 struct abbrev_table *abbrev_table,
7010 dwarf2_cu *existing_cu,
7011 bool skip_partial)
7012 : die_reader_specs {},
7013 m_this_cu (this_cu)
7014 {
7015 struct objfile *objfile = per_objfile->objfile;
7016 struct dwarf2_section_info *section = this_cu->section;
7017 bfd *abfd = section->get_bfd_owner ();
7018 const gdb_byte *begin_info_ptr;
7019 struct signatured_type *sig_type = NULL;
7020 struct dwarf2_section_info *abbrev_section;
7021 /* Non-zero if CU currently points to a DWO file and we need to
7022 reread it. When this happens we need to reread the skeleton die
7023 before we can reread the DWO file (this only applies to CUs, not TUs). */
7024 int rereading_dwo_cu = 0;
7025
7026 if (dwarf_die_debug)
7027 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7028 this_cu->is_debug_types ? "type" : "comp",
7029 sect_offset_str (this_cu->sect_off));
7030
7031 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7032 file (instead of going through the stub), short-circuit all of this. */
7033 if (this_cu->reading_dwo_directly)
7034 {
7035 /* Narrow down the scope of possibilities to have to understand. */
7036 gdb_assert (this_cu->is_debug_types);
7037 gdb_assert (abbrev_table == NULL);
7038 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
7039 return;
7040 }
7041
7042 /* This is cheap if the section is already read in. */
7043 section->read (objfile);
7044
7045 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7046
7047 abbrev_section = get_abbrev_section_for_cu (this_cu);
7048
7049 dwarf2_cu *cu;
7050
7051 if (existing_cu != nullptr)
7052 {
7053 cu = existing_cu;
7054 /* If this CU is from a DWO file we need to start over, we need to
7055 refetch the attributes from the skeleton CU.
7056 This could be optimized by retrieving those attributes from when we
7057 were here the first time: the previous comp_unit_die was stored in
7058 comp_unit_obstack. But there's no data yet that we need this
7059 optimization. */
7060 if (cu->dwo_unit != NULL)
7061 rereading_dwo_cu = 1;
7062 }
7063 else
7064 {
7065 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7066 in per_objfile yet. */
7067 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7068 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7069 cu = m_new_cu.get ();
7070 }
7071
7072 /* Get the header. */
7073 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7074 {
7075 /* We already have the header, there's no need to read it in again. */
7076 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7077 }
7078 else
7079 {
7080 if (this_cu->is_debug_types)
7081 {
7082 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7083 section, abbrev_section,
7084 info_ptr, rcuh_kind::TYPE);
7085
7086 /* Since per_cu is the first member of struct signatured_type,
7087 we can go from a pointer to one to a pointer to the other. */
7088 sig_type = (struct signatured_type *) this_cu;
7089 gdb_assert (sig_type->signature == cu->header.signature);
7090 gdb_assert (sig_type->type_offset_in_tu
7091 == cu->header.type_cu_offset_in_tu);
7092 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7093
7094 /* LENGTH has not been set yet for type units if we're
7095 using .gdb_index. */
7096 this_cu->length = cu->header.get_length ();
7097
7098 /* Establish the type offset that can be used to lookup the type. */
7099 sig_type->type_offset_in_section =
7100 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7101
7102 this_cu->dwarf_version = cu->header.version;
7103 }
7104 else
7105 {
7106 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7107 section, abbrev_section,
7108 info_ptr,
7109 rcuh_kind::COMPILE);
7110
7111 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7112 if (this_cu->length == 0)
7113 this_cu->length = cu->header.get_length ();
7114 else
7115 gdb_assert (this_cu->length == cu->header.get_length ());
7116 this_cu->dwarf_version = cu->header.version;
7117 }
7118 }
7119
7120 /* Skip dummy compilation units. */
7121 if (info_ptr >= begin_info_ptr + this_cu->length
7122 || peek_abbrev_code (abfd, info_ptr) == 0)
7123 {
7124 dummy_p = true;
7125 return;
7126 }
7127
7128 /* If we don't have them yet, read the abbrevs for this compilation unit.
7129 And if we need to read them now, make sure they're freed when we're
7130 done. */
7131 if (abbrev_table != NULL)
7132 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7133 else
7134 {
7135 abbrev_section->read (objfile);
7136 m_abbrev_table_holder
7137 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
7138 abbrev_table = m_abbrev_table_holder.get ();
7139 }
7140
7141 /* Read the top level CU/TU die. */
7142 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7143 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7144
7145 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7146 {
7147 dummy_p = true;
7148 return;
7149 }
7150
7151 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7152 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7153 table from the DWO file and pass the ownership over to us. It will be
7154 referenced from READER, so we must make sure to free it after we're done
7155 with READER.
7156
7157 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7158 DWO CU, that this test will fail (the attribute will not be present). */
7159 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7160 if (dwo_name != nullptr)
7161 {
7162 struct dwo_unit *dwo_unit;
7163 struct die_info *dwo_comp_unit_die;
7164
7165 if (comp_unit_die->has_children)
7166 {
7167 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7168 " has children (offset %s) [in module %s]"),
7169 sect_offset_str (this_cu->sect_off),
7170 bfd_get_filename (abfd));
7171 }
7172 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
7173 if (dwo_unit != NULL)
7174 {
7175 if (read_cutu_die_from_dwo (cu, dwo_unit,
7176 comp_unit_die, NULL,
7177 this, &info_ptr,
7178 &dwo_comp_unit_die,
7179 &m_dwo_abbrev_table) == 0)
7180 {
7181 /* Dummy die. */
7182 dummy_p = true;
7183 return;
7184 }
7185 comp_unit_die = dwo_comp_unit_die;
7186 }
7187 else
7188 {
7189 /* Yikes, we couldn't find the rest of the DIE, we only have
7190 the stub. A complaint has already been logged. There's
7191 not much more we can do except pass on the stub DIE to
7192 die_reader_func. We don't want to throw an error on bad
7193 debug info. */
7194 }
7195 }
7196 }
7197
7198 void
7199 cutu_reader::keep ()
7200 {
7201 /* Done, clean up. */
7202 gdb_assert (!dummy_p);
7203 if (m_new_cu != NULL)
7204 {
7205 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7206 now. */
7207 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7208 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
7209 }
7210 }
7211
7212 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7213 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7214 assumed to have already done the lookup to find the DWO file).
7215
7216 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7217 THIS_CU->is_debug_types, but nothing else.
7218
7219 We fill in THIS_CU->length.
7220
7221 THIS_CU->cu is always freed when done.
7222 This is done in order to not leave THIS_CU->cu in a state where we have
7223 to care whether it refers to the "main" CU or the DWO CU.
7224
7225 When parent_cu is passed, it is used to provide a default value for
7226 str_offsets_base and addr_base from the parent. */
7227
7228 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7229 dwarf2_per_objfile *per_objfile,
7230 struct dwarf2_cu *parent_cu,
7231 struct dwo_file *dwo_file)
7232 : die_reader_specs {},
7233 m_this_cu (this_cu)
7234 {
7235 struct objfile *objfile = per_objfile->objfile;
7236 struct dwarf2_section_info *section = this_cu->section;
7237 bfd *abfd = section->get_bfd_owner ();
7238 struct dwarf2_section_info *abbrev_section;
7239 const gdb_byte *begin_info_ptr, *info_ptr;
7240
7241 if (dwarf_die_debug)
7242 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7243 this_cu->is_debug_types ? "type" : "comp",
7244 sect_offset_str (this_cu->sect_off));
7245
7246 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7247
7248 abbrev_section = (dwo_file != NULL
7249 ? &dwo_file->sections.abbrev
7250 : get_abbrev_section_for_cu (this_cu));
7251
7252 /* This is cheap if the section is already read in. */
7253 section->read (objfile);
7254
7255 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7256
7257 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7258 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7259 section, abbrev_section, info_ptr,
7260 (this_cu->is_debug_types
7261 ? rcuh_kind::TYPE
7262 : rcuh_kind::COMPILE));
7263
7264 if (parent_cu != nullptr)
7265 {
7266 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7267 m_new_cu->addr_base = parent_cu->addr_base;
7268 }
7269 this_cu->length = m_new_cu->header.get_length ();
7270
7271 /* Skip dummy compilation units. */
7272 if (info_ptr >= begin_info_ptr + this_cu->length
7273 || peek_abbrev_code (abfd, info_ptr) == 0)
7274 {
7275 dummy_p = true;
7276 return;
7277 }
7278
7279 abbrev_section->read (objfile);
7280 m_abbrev_table_holder
7281 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
7282
7283 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7284 m_abbrev_table_holder.get ());
7285 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7286 }
7287
7288 \f
7289 /* Type Unit Groups.
7290
7291 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7292 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7293 so that all types coming from the same compilation (.o file) are grouped
7294 together. A future step could be to put the types in the same symtab as
7295 the CU the types ultimately came from. */
7296
7297 static hashval_t
7298 hash_type_unit_group (const void *item)
7299 {
7300 const struct type_unit_group *tu_group
7301 = (const struct type_unit_group *) item;
7302
7303 return hash_stmt_list_entry (&tu_group->hash);
7304 }
7305
7306 static int
7307 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7308 {
7309 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7310 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7311
7312 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7313 }
7314
7315 /* Allocate a hash table for type unit groups. */
7316
7317 static htab_up
7318 allocate_type_unit_groups_table ()
7319 {
7320 return htab_up (htab_create_alloc (3,
7321 hash_type_unit_group,
7322 eq_type_unit_group,
7323 NULL, xcalloc, xfree));
7324 }
7325
7326 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7327 partial symtabs. We combine several TUs per psymtab to not let the size
7328 of any one psymtab grow too big. */
7329 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7330 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7331
7332 /* Helper routine for get_type_unit_group.
7333 Create the type_unit_group object used to hold one or more TUs. */
7334
7335 static struct type_unit_group *
7336 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7337 {
7338 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7339 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7340 struct dwarf2_per_cu_data *per_cu;
7341 struct type_unit_group *tu_group;
7342
7343 tu_group = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, type_unit_group);
7344 per_cu = &tu_group->per_cu;
7345 per_cu->per_bfd = per_bfd;
7346
7347 if (per_bfd->using_index)
7348 {
7349 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7350 struct dwarf2_per_cu_quick_data);
7351 }
7352 else
7353 {
7354 unsigned int line_offset = to_underlying (line_offset_struct);
7355 dwarf2_psymtab *pst;
7356 std::string name;
7357
7358 /* Give the symtab a useful name for debug purposes. */
7359 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7360 name = string_printf ("<type_units_%d>",
7361 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7362 else
7363 name = string_printf ("<type_units_at_0x%x>", line_offset);
7364
7365 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
7366 pst->anonymous = true;
7367 }
7368
7369 tu_group->hash.dwo_unit = cu->dwo_unit;
7370 tu_group->hash.line_sect_off = line_offset_struct;
7371
7372 return tu_group;
7373 }
7374
7375 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7376 STMT_LIST is a DW_AT_stmt_list attribute. */
7377
7378 static struct type_unit_group *
7379 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7380 {
7381 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7382 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7383 struct type_unit_group *tu_group;
7384 void **slot;
7385 unsigned int line_offset;
7386 struct type_unit_group type_unit_group_for_lookup;
7387
7388 if (per_objfile->per_bfd->type_unit_groups == NULL)
7389 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7390
7391 /* Do we need to create a new group, or can we use an existing one? */
7392
7393 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
7394 {
7395 line_offset = stmt_list->as_unsigned ();
7396 ++tu_stats->nr_symtab_sharers;
7397 }
7398 else
7399 {
7400 /* Ugh, no stmt_list. Rare, but we have to handle it.
7401 We can do various things here like create one group per TU or
7402 spread them over multiple groups to split up the expansion work.
7403 To avoid worst case scenarios (too many groups or too large groups)
7404 we, umm, group them in bunches. */
7405 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7406 | (tu_stats->nr_stmt_less_type_units
7407 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7408 ++tu_stats->nr_stmt_less_type_units;
7409 }
7410
7411 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7412 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7413 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
7414 &type_unit_group_for_lookup, INSERT);
7415 if (*slot != NULL)
7416 {
7417 tu_group = (struct type_unit_group *) *slot;
7418 gdb_assert (tu_group != NULL);
7419 }
7420 else
7421 {
7422 sect_offset line_offset_struct = (sect_offset) line_offset;
7423 tu_group = create_type_unit_group (cu, line_offset_struct);
7424 *slot = tu_group;
7425 ++tu_stats->nr_symtabs;
7426 }
7427
7428 return tu_group;
7429 }
7430 \f
7431 /* Partial symbol tables. */
7432
7433 /* Create a psymtab named NAME and assign it to PER_CU.
7434
7435 The caller must fill in the following details:
7436 dirname, textlow, texthigh. */
7437
7438 static dwarf2_psymtab *
7439 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7440 dwarf2_per_objfile *per_objfile,
7441 const char *name)
7442 {
7443 struct objfile *objfile = per_objfile->objfile;
7444 dwarf2_psymtab *pst;
7445
7446 pst = new dwarf2_psymtab (name, objfile, per_cu);
7447
7448 pst->psymtabs_addrmap_supported = true;
7449
7450 /* This is the glue that links PST into GDB's symbol API. */
7451 per_cu->v.psymtab = pst;
7452
7453 return pst;
7454 }
7455
7456 /* DIE reader function for process_psymtab_comp_unit. */
7457
7458 static void
7459 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7460 const gdb_byte *info_ptr,
7461 struct die_info *comp_unit_die,
7462 enum language pretend_language)
7463 {
7464 struct dwarf2_cu *cu = reader->cu;
7465 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7466 struct objfile *objfile = per_objfile->objfile;
7467 struct gdbarch *gdbarch = objfile->arch ();
7468 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7469 CORE_ADDR baseaddr;
7470 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7471 dwarf2_psymtab *pst;
7472 enum pc_bounds_kind cu_bounds_kind;
7473 const char *filename;
7474
7475 gdb_assert (! per_cu->is_debug_types);
7476
7477 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7478
7479 /* Allocate a new partial symbol table structure. */
7480 gdb::unique_xmalloc_ptr<char> debug_filename;
7481 static const char artificial[] = "<artificial>";
7482 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7483 if (filename == NULL)
7484 filename = "";
7485 else if (strcmp (filename, artificial) == 0)
7486 {
7487 debug_filename.reset (concat (artificial, "@",
7488 sect_offset_str (per_cu->sect_off),
7489 (char *) NULL));
7490 filename = debug_filename.get ();
7491 }
7492
7493 pst = create_partial_symtab (per_cu, per_objfile, filename);
7494
7495 /* This must be done before calling dwarf2_build_include_psymtabs. */
7496 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7497
7498 baseaddr = objfile->text_section_offset ();
7499
7500 dwarf2_find_base_address (comp_unit_die, cu);
7501
7502 /* Possibly set the default values of LOWPC and HIGHPC from
7503 `DW_AT_ranges'. */
7504 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7505 &best_highpc, cu, pst);
7506 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7507 {
7508 CORE_ADDR low
7509 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7510 - baseaddr);
7511 CORE_ADDR high
7512 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7513 - baseaddr - 1);
7514 /* Store the contiguous range if it is not empty; it can be
7515 empty for CUs with no code. */
7516 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7517 low, high, pst);
7518 }
7519
7520 /* Check if comp unit has_children.
7521 If so, read the rest of the partial symbols from this comp unit.
7522 If not, there's no more debug_info for this comp unit. */
7523 if (comp_unit_die->has_children)
7524 {
7525 struct partial_die_info *first_die;
7526 CORE_ADDR lowpc, highpc;
7527
7528 lowpc = ((CORE_ADDR) -1);
7529 highpc = ((CORE_ADDR) 0);
7530
7531 first_die = load_partial_dies (reader, info_ptr, 1);
7532
7533 scan_partial_symbols (first_die, &lowpc, &highpc,
7534 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7535
7536 /* If we didn't find a lowpc, set it to highpc to avoid
7537 complaints from `maint check'. */
7538 if (lowpc == ((CORE_ADDR) -1))
7539 lowpc = highpc;
7540
7541 /* If the compilation unit didn't have an explicit address range,
7542 then use the information extracted from its child dies. */
7543 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7544 {
7545 best_lowpc = lowpc;
7546 best_highpc = highpc;
7547 }
7548 }
7549 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7550 best_lowpc + baseaddr)
7551 - baseaddr);
7552 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7553 best_highpc + baseaddr)
7554 - baseaddr);
7555
7556 pst->end ();
7557
7558 if (!cu->per_cu->imported_symtabs_empty ())
7559 {
7560 int i;
7561 int len = cu->per_cu->imported_symtabs_size ();
7562
7563 /* Fill in 'dependencies' here; we fill in 'users' in a
7564 post-pass. */
7565 pst->number_of_dependencies = len;
7566 pst->dependencies
7567 = objfile->partial_symtabs->allocate_dependencies (len);
7568 for (i = 0; i < len; ++i)
7569 {
7570 pst->dependencies[i]
7571 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7572 }
7573
7574 cu->per_cu->imported_symtabs_free ();
7575 }
7576
7577 /* Get the list of files included in the current compilation unit,
7578 and build a psymtab for each of them. */
7579 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7580
7581 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7582 ", %d global, %d static syms",
7583 per_cu->is_debug_types ? "type" : "comp",
7584 sect_offset_str (per_cu->sect_off),
7585 paddress (gdbarch, pst->text_low (objfile)),
7586 paddress (gdbarch, pst->text_high (objfile)),
7587 (int) pst->global_psymbols.size (),
7588 (int) pst->static_psymbols.size ());
7589 }
7590
7591 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7592 Process compilation unit THIS_CU for a psymtab. */
7593
7594 static void
7595 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7596 dwarf2_per_objfile *per_objfile,
7597 bool want_partial_unit,
7598 enum language pretend_language)
7599 {
7600 /* If this compilation unit was already read in, free the
7601 cached copy in order to read it in again. This is
7602 necessary because we skipped some symbols when we first
7603 read in the compilation unit (see load_partial_dies).
7604 This problem could be avoided, but the benefit is unclear. */
7605 per_objfile->remove_cu (this_cu);
7606
7607 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7608
7609 switch (reader.comp_unit_die->tag)
7610 {
7611 case DW_TAG_compile_unit:
7612 this_cu->unit_type = DW_UT_compile;
7613 break;
7614 case DW_TAG_partial_unit:
7615 this_cu->unit_type = DW_UT_partial;
7616 break;
7617 case DW_TAG_type_unit:
7618 this_cu->unit_type = DW_UT_type;
7619 break;
7620 default:
7621 abort ();
7622 }
7623
7624 if (reader.dummy_p)
7625 {
7626 /* Nothing. */
7627 }
7628 else if (this_cu->is_debug_types)
7629 build_type_psymtabs_reader (&reader, reader.info_ptr,
7630 reader.comp_unit_die);
7631 else if (want_partial_unit
7632 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7633 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7634 reader.comp_unit_die,
7635 pretend_language);
7636
7637 this_cu->lang = reader.cu->language;
7638
7639 /* Age out any secondary CUs. */
7640 per_objfile->age_comp_units ();
7641 }
7642
7643 /* Reader function for build_type_psymtabs. */
7644
7645 static void
7646 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7647 const gdb_byte *info_ptr,
7648 struct die_info *type_unit_die)
7649 {
7650 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7651 struct dwarf2_cu *cu = reader->cu;
7652 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7653 struct signatured_type *sig_type;
7654 struct type_unit_group *tu_group;
7655 struct attribute *attr;
7656 struct partial_die_info *first_die;
7657 CORE_ADDR lowpc, highpc;
7658 dwarf2_psymtab *pst;
7659
7660 gdb_assert (per_cu->is_debug_types);
7661 sig_type = (struct signatured_type *) per_cu;
7662
7663 if (! type_unit_die->has_children)
7664 return;
7665
7666 attr = type_unit_die->attr (DW_AT_stmt_list);
7667 tu_group = get_type_unit_group (cu, attr);
7668
7669 if (tu_group->tus == nullptr)
7670 tu_group->tus = new std::vector<signatured_type *>;
7671 tu_group->tus->push_back (sig_type);
7672
7673 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7674 pst = create_partial_symtab (per_cu, per_objfile, "");
7675 pst->anonymous = true;
7676
7677 first_die = load_partial_dies (reader, info_ptr, 1);
7678
7679 lowpc = (CORE_ADDR) -1;
7680 highpc = (CORE_ADDR) 0;
7681 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7682
7683 pst->end ();
7684 }
7685
7686 /* Struct used to sort TUs by their abbreviation table offset. */
7687
7688 struct tu_abbrev_offset
7689 {
7690 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7691 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7692 {}
7693
7694 signatured_type *sig_type;
7695 sect_offset abbrev_offset;
7696 };
7697
7698 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7699
7700 static bool
7701 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7702 const struct tu_abbrev_offset &b)
7703 {
7704 return a.abbrev_offset < b.abbrev_offset;
7705 }
7706
7707 /* Efficiently read all the type units.
7708 This does the bulk of the work for build_type_psymtabs.
7709
7710 The efficiency is because we sort TUs by the abbrev table they use and
7711 only read each abbrev table once. In one program there are 200K TUs
7712 sharing 8K abbrev tables.
7713
7714 The main purpose of this function is to support building the
7715 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7716 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7717 can collapse the search space by grouping them by stmt_list.
7718 The savings can be significant, in the same program from above the 200K TUs
7719 share 8K stmt_list tables.
7720
7721 FUNC is expected to call get_type_unit_group, which will create the
7722 struct type_unit_group if necessary and add it to
7723 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7724
7725 static void
7726 build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
7727 {
7728 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7729 abbrev_table_up abbrev_table;
7730 sect_offset abbrev_offset;
7731
7732 /* It's up to the caller to not call us multiple times. */
7733 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7734
7735 if (per_objfile->per_bfd->all_type_units.empty ())
7736 return;
7737
7738 /* TUs typically share abbrev tables, and there can be way more TUs than
7739 abbrev tables. Sort by abbrev table to reduce the number of times we
7740 read each abbrev table in.
7741 Alternatives are to punt or to maintain a cache of abbrev tables.
7742 This is simpler and efficient enough for now.
7743
7744 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7745 symtab to use). Typically TUs with the same abbrev offset have the same
7746 stmt_list value too so in practice this should work well.
7747
7748 The basic algorithm here is:
7749
7750 sort TUs by abbrev table
7751 for each TU with same abbrev table:
7752 read abbrev table if first user
7753 read TU top level DIE
7754 [IWBN if DWO skeletons had DW_AT_stmt_list]
7755 call FUNC */
7756
7757 dwarf_read_debug_printf ("Building type unit groups ...");
7758
7759 /* Sort in a separate table to maintain the order of all_type_units
7760 for .gdb_index: TU indices directly index all_type_units. */
7761 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7762 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
7763
7764 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
7765 sorted_by_abbrev.emplace_back
7766 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
7767 sig_type->per_cu.sect_off));
7768
7769 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7770 sort_tu_by_abbrev_offset);
7771
7772 abbrev_offset = (sect_offset) ~(unsigned) 0;
7773
7774 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7775 {
7776 /* Switch to the next abbrev table if necessary. */
7777 if (abbrev_table == NULL
7778 || tu.abbrev_offset != abbrev_offset)
7779 {
7780 abbrev_offset = tu.abbrev_offset;
7781 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
7782 abbrev_table =
7783 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
7784 ++tu_stats->nr_uniq_abbrev_tables;
7785 }
7786
7787 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
7788 abbrev_table.get (), nullptr, false);
7789 if (!reader.dummy_p)
7790 build_type_psymtabs_reader (&reader, reader.info_ptr,
7791 reader.comp_unit_die);
7792 }
7793 }
7794
7795 /* Print collected type unit statistics. */
7796
7797 static void
7798 print_tu_stats (dwarf2_per_objfile *per_objfile)
7799 {
7800 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7801
7802 dwarf_read_debug_printf ("Type unit statistics:");
7803 dwarf_read_debug_printf (" %zu TUs",
7804 per_objfile->per_bfd->all_type_units.size ());
7805 dwarf_read_debug_printf (" %d uniq abbrev tables",
7806 tu_stats->nr_uniq_abbrev_tables);
7807 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7808 tu_stats->nr_symtabs);
7809 dwarf_read_debug_printf (" %d symtab sharers",
7810 tu_stats->nr_symtab_sharers);
7811 dwarf_read_debug_printf (" %d type units without a stmt_list",
7812 tu_stats->nr_stmt_less_type_units);
7813 dwarf_read_debug_printf (" %d all_type_units reallocs",
7814 tu_stats->nr_all_type_units_reallocs);
7815 }
7816
7817 /* Traversal function for build_type_psymtabs. */
7818
7819 static int
7820 build_type_psymtab_dependencies (void **slot, void *info)
7821 {
7822 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7823 struct objfile *objfile = per_objfile->objfile;
7824 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7825 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7826 dwarf2_psymtab *pst = per_cu->v.psymtab;
7827 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7828 int i;
7829
7830 gdb_assert (len > 0);
7831 gdb_assert (per_cu->type_unit_group_p ());
7832
7833 pst->number_of_dependencies = len;
7834 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7835 for (i = 0; i < len; ++i)
7836 {
7837 struct signatured_type *iter = tu_group->tus->at (i);
7838 gdb_assert (iter->per_cu.is_debug_types);
7839 pst->dependencies[i] = iter->per_cu.v.psymtab;
7840 iter->type_unit_group = tu_group;
7841 }
7842
7843 delete tu_group->tus;
7844 tu_group->tus = nullptr;
7845
7846 return 1;
7847 }
7848
7849 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7850 Build partial symbol tables for the .debug_types comp-units. */
7851
7852 static void
7853 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
7854 {
7855 if (! create_all_type_units (per_objfile))
7856 return;
7857
7858 build_type_psymtabs_1 (per_objfile);
7859 }
7860
7861 /* Traversal function for process_skeletonless_type_unit.
7862 Read a TU in a DWO file and build partial symbols for it. */
7863
7864 static int
7865 process_skeletonless_type_unit (void **slot, void *info)
7866 {
7867 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7868 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7869 struct signatured_type find_entry, *entry;
7870
7871 /* If this TU doesn't exist in the global table, add it and read it in. */
7872
7873 if (per_objfile->per_bfd->signatured_types == NULL)
7874 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7875
7876 find_entry.signature = dwo_unit->signature;
7877 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
7878 &find_entry, INSERT);
7879 /* If we've already seen this type there's nothing to do. What's happening
7880 is we're doing our own version of comdat-folding here. */
7881 if (*slot != NULL)
7882 return 1;
7883
7884 /* This does the job that create_all_type_units would have done for
7885 this TU. */
7886 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
7887 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
7888 *slot = entry;
7889
7890 /* This does the job that build_type_psymtabs_1 would have done. */
7891 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
7892 if (!reader.dummy_p)
7893 build_type_psymtabs_reader (&reader, reader.info_ptr,
7894 reader.comp_unit_die);
7895
7896 return 1;
7897 }
7898
7899 /* Traversal function for process_skeletonless_type_units. */
7900
7901 static int
7902 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7903 {
7904 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7905
7906 if (dwo_file->tus != NULL)
7907 htab_traverse_noresize (dwo_file->tus.get (),
7908 process_skeletonless_type_unit, info);
7909
7910 return 1;
7911 }
7912
7913 /* Scan all TUs of DWO files, verifying we've processed them.
7914 This is needed in case a TU was emitted without its skeleton.
7915 Note: This can't be done until we know what all the DWO files are. */
7916
7917 static void
7918 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
7919 {
7920 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7921 if (get_dwp_file (per_objfile) == NULL
7922 && per_objfile->per_bfd->dwo_files != NULL)
7923 {
7924 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
7925 process_dwo_file_for_skeletonless_type_units,
7926 per_objfile);
7927 }
7928 }
7929
7930 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7931
7932 static void
7933 set_partial_user (dwarf2_per_objfile *per_objfile)
7934 {
7935 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
7936 {
7937 dwarf2_psymtab *pst = per_cu->v.psymtab;
7938
7939 if (pst == NULL)
7940 continue;
7941
7942 for (int j = 0; j < pst->number_of_dependencies; ++j)
7943 {
7944 /* Set the 'user' field only if it is not already set. */
7945 if (pst->dependencies[j]->user == NULL)
7946 pst->dependencies[j]->user = pst;
7947 }
7948 }
7949 }
7950
7951 /* Build the partial symbol table by doing a quick pass through the
7952 .debug_info and .debug_abbrev sections. */
7953
7954 static void
7955 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
7956 {
7957 struct objfile *objfile = per_objfile->objfile;
7958
7959 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7960 objfile_name (objfile));
7961
7962 scoped_restore restore_reading_psyms
7963 = make_scoped_restore (&per_objfile->per_bfd->reading_partial_symbols,
7964 true);
7965
7966 per_objfile->per_bfd->info.read (objfile);
7967
7968 /* Any cached compilation units will be linked by the per-objfile
7969 read_in_chain. Make sure to free them when we're done. */
7970 free_cached_comp_units freer (per_objfile);
7971
7972 build_type_psymtabs (per_objfile);
7973
7974 create_all_comp_units (per_objfile);
7975
7976 /* Create a temporary address map on a temporary obstack. We later
7977 copy this to the final obstack. */
7978 auto_obstack temp_obstack;
7979
7980 scoped_restore save_psymtabs_addrmap
7981 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7982 addrmap_create_mutable (&temp_obstack));
7983
7984 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
7985 {
7986 if (per_cu->v.psymtab != NULL)
7987 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7988 continue;
7989 process_psymtab_comp_unit (per_cu, per_objfile, false,
7990 language_minimal);
7991 }
7992
7993 /* This has to wait until we read the CUs, we need the list of DWOs. */
7994 process_skeletonless_type_units (per_objfile);
7995
7996 /* Now that all TUs have been processed we can fill in the dependencies. */
7997 if (per_objfile->per_bfd->type_unit_groups != NULL)
7998 {
7999 htab_traverse_noresize (per_objfile->per_bfd->type_unit_groups.get (),
8000 build_type_psymtab_dependencies, per_objfile);
8001 }
8002
8003 if (dwarf_read_debug > 0)
8004 print_tu_stats (per_objfile);
8005
8006 set_partial_user (per_objfile);
8007
8008 objfile->partial_symtabs->psymtabs_addrmap
8009 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8010 objfile->partial_symtabs->obstack ());
8011 /* At this point we want to keep the address map. */
8012 save_psymtabs_addrmap.release ();
8013
8014 dwarf_read_debug_printf ("Done building psymtabs of %s",
8015 objfile_name (objfile));
8016 }
8017
8018 /* Load the partial DIEs for a secondary CU into memory.
8019 This is also used when rereading a primary CU with load_all_dies. */
8020
8021 static void
8022 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
8023 dwarf2_per_objfile *per_objfile,
8024 dwarf2_cu *existing_cu)
8025 {
8026 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
8027
8028 if (!reader.dummy_p)
8029 {
8030 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8031 language_minimal);
8032
8033 /* Check if comp unit has_children.
8034 If so, read the rest of the partial symbols from this comp unit.
8035 If not, there's no more debug_info for this comp unit. */
8036 if (reader.comp_unit_die->has_children)
8037 load_partial_dies (&reader, reader.info_ptr, 0);
8038
8039 reader.keep ();
8040 }
8041 }
8042
8043 static void
8044 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
8045 struct dwarf2_section_info *section,
8046 struct dwarf2_section_info *abbrev_section,
8047 unsigned int is_dwz)
8048 {
8049 const gdb_byte *info_ptr;
8050 struct objfile *objfile = per_objfile->objfile;
8051
8052 dwarf_read_debug_printf ("Reading %s for %s",
8053 section->get_name (),
8054 section->get_file_name ());
8055
8056 section->read (objfile);
8057
8058 info_ptr = section->buffer;
8059
8060 while (info_ptr < section->buffer + section->size)
8061 {
8062 struct dwarf2_per_cu_data *this_cu;
8063
8064 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8065
8066 comp_unit_head cu_header;
8067 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
8068 abbrev_section, info_ptr,
8069 rcuh_kind::COMPILE);
8070
8071 /* Save the compilation unit for later lookup. */
8072 if (cu_header.unit_type != DW_UT_type)
8073 this_cu = per_objfile->per_bfd->allocate_per_cu ();
8074 else
8075 {
8076 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
8077 sig_type->signature = cu_header.signature;
8078 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8079 this_cu = &sig_type->per_cu;
8080 }
8081 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8082 this_cu->sect_off = sect_off;
8083 this_cu->length = cu_header.length + cu_header.initial_length_size;
8084 this_cu->is_dwz = is_dwz;
8085 this_cu->section = section;
8086
8087 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8088
8089 info_ptr = info_ptr + this_cu->length;
8090 }
8091 }
8092
8093 /* Create a list of all compilation units in OBJFILE.
8094 This is only done for -readnow and building partial symtabs. */
8095
8096 static void
8097 create_all_comp_units (dwarf2_per_objfile *per_objfile)
8098 {
8099 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8100 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8101 &per_objfile->per_bfd->abbrev, 0);
8102
8103 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8104 if (dwz != NULL)
8105 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
8106 }
8107
8108 /* Process all loaded DIEs for compilation unit CU, starting at
8109 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8110 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8111 DW_AT_ranges). See the comments of add_partial_subprogram on how
8112 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8113
8114 static void
8115 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8116 CORE_ADDR *highpc, int set_addrmap,
8117 struct dwarf2_cu *cu)
8118 {
8119 struct partial_die_info *pdi;
8120
8121 /* Now, march along the PDI's, descending into ones which have
8122 interesting children but skipping the children of the other ones,
8123 until we reach the end of the compilation unit. */
8124
8125 pdi = first_die;
8126
8127 while (pdi != NULL)
8128 {
8129 pdi->fixup (cu);
8130
8131 /* Anonymous namespaces or modules have no name but have interesting
8132 children, so we need to look at them. Ditto for anonymous
8133 enums. */
8134
8135 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
8136 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8137 || pdi->tag == DW_TAG_imported_unit
8138 || pdi->tag == DW_TAG_inlined_subroutine)
8139 {
8140 switch (pdi->tag)
8141 {
8142 case DW_TAG_subprogram:
8143 case DW_TAG_inlined_subroutine:
8144 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8145 if (cu->language == language_cplus)
8146 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8147 set_addrmap, cu);
8148 break;
8149 case DW_TAG_constant:
8150 case DW_TAG_variable:
8151 case DW_TAG_typedef:
8152 case DW_TAG_union_type:
8153 if (!pdi->is_declaration
8154 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8155 {
8156 add_partial_symbol (pdi, cu);
8157 }
8158 break;
8159 case DW_TAG_class_type:
8160 case DW_TAG_interface_type:
8161 case DW_TAG_structure_type:
8162 if (!pdi->is_declaration)
8163 {
8164 add_partial_symbol (pdi, cu);
8165 }
8166 if ((cu->language == language_rust
8167 || cu->language == language_cplus) && pdi->has_children)
8168 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8169 set_addrmap, cu);
8170 break;
8171 case DW_TAG_enumeration_type:
8172 if (!pdi->is_declaration)
8173 add_partial_enumeration (pdi, cu);
8174 break;
8175 case DW_TAG_base_type:
8176 case DW_TAG_subrange_type:
8177 /* File scope base type definitions are added to the partial
8178 symbol table. */
8179 add_partial_symbol (pdi, cu);
8180 break;
8181 case DW_TAG_namespace:
8182 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8183 break;
8184 case DW_TAG_module:
8185 if (!pdi->is_declaration)
8186 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8187 break;
8188 case DW_TAG_imported_unit:
8189 {
8190 struct dwarf2_per_cu_data *per_cu;
8191
8192 /* For now we don't handle imported units in type units. */
8193 if (cu->per_cu->is_debug_types)
8194 {
8195 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8196 " supported in type units [in module %s]"),
8197 objfile_name (cu->per_objfile->objfile));
8198 }
8199
8200 per_cu = dwarf2_find_containing_comp_unit
8201 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8202
8203 /* Go read the partial unit, if needed. */
8204 if (per_cu->v.psymtab == NULL)
8205 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8206 cu->language);
8207
8208 cu->per_cu->imported_symtabs_push (per_cu);
8209 }
8210 break;
8211 case DW_TAG_imported_declaration:
8212 add_partial_symbol (pdi, cu);
8213 break;
8214 default:
8215 break;
8216 }
8217 }
8218
8219 /* If the die has a sibling, skip to the sibling. */
8220
8221 pdi = pdi->die_sibling;
8222 }
8223 }
8224
8225 /* Functions used to compute the fully scoped name of a partial DIE.
8226
8227 Normally, this is simple. For C++, the parent DIE's fully scoped
8228 name is concatenated with "::" and the partial DIE's name.
8229 Enumerators are an exception; they use the scope of their parent
8230 enumeration type, i.e. the name of the enumeration type is not
8231 prepended to the enumerator.
8232
8233 There are two complexities. One is DW_AT_specification; in this
8234 case "parent" means the parent of the target of the specification,
8235 instead of the direct parent of the DIE. The other is compilers
8236 which do not emit DW_TAG_namespace; in this case we try to guess
8237 the fully qualified name of structure types from their members'
8238 linkage names. This must be done using the DIE's children rather
8239 than the children of any DW_AT_specification target. We only need
8240 to do this for structures at the top level, i.e. if the target of
8241 any DW_AT_specification (if any; otherwise the DIE itself) does not
8242 have a parent. */
8243
8244 /* Compute the scope prefix associated with PDI's parent, in
8245 compilation unit CU. The result will be allocated on CU's
8246 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8247 field. NULL is returned if no prefix is necessary. */
8248 static const char *
8249 partial_die_parent_scope (struct partial_die_info *pdi,
8250 struct dwarf2_cu *cu)
8251 {
8252 const char *grandparent_scope;
8253 struct partial_die_info *parent, *real_pdi;
8254
8255 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8256 then this means the parent of the specification DIE. */
8257
8258 real_pdi = pdi;
8259 while (real_pdi->has_specification)
8260 {
8261 auto res = find_partial_die (real_pdi->spec_offset,
8262 real_pdi->spec_is_dwz, cu);
8263 real_pdi = res.pdi;
8264 cu = res.cu;
8265 }
8266
8267 parent = real_pdi->die_parent;
8268 if (parent == NULL)
8269 return NULL;
8270
8271 if (parent->scope_set)
8272 return parent->scope;
8273
8274 parent->fixup (cu);
8275
8276 grandparent_scope = partial_die_parent_scope (parent, cu);
8277
8278 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8279 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8280 Work around this problem here. */
8281 if (cu->language == language_cplus
8282 && parent->tag == DW_TAG_namespace
8283 && strcmp (parent->name (cu), "::") == 0
8284 && grandparent_scope == NULL)
8285 {
8286 parent->scope = NULL;
8287 parent->scope_set = 1;
8288 return NULL;
8289 }
8290
8291 /* Nested subroutines in Fortran get a prefix. */
8292 if (pdi->tag == DW_TAG_enumerator)
8293 /* Enumerators should not get the name of the enumeration as a prefix. */
8294 parent->scope = grandparent_scope;
8295 else if (parent->tag == DW_TAG_namespace
8296 || parent->tag == DW_TAG_module
8297 || parent->tag == DW_TAG_structure_type
8298 || parent->tag == DW_TAG_class_type
8299 || parent->tag == DW_TAG_interface_type
8300 || parent->tag == DW_TAG_union_type
8301 || parent->tag == DW_TAG_enumeration_type
8302 || (cu->language == language_fortran
8303 && parent->tag == DW_TAG_subprogram
8304 && pdi->tag == DW_TAG_subprogram))
8305 {
8306 if (grandparent_scope == NULL)
8307 parent->scope = parent->name (cu);
8308 else
8309 parent->scope = typename_concat (&cu->comp_unit_obstack,
8310 grandparent_scope,
8311 parent->name (cu), 0, cu);
8312 }
8313 else
8314 {
8315 /* FIXME drow/2004-04-01: What should we be doing with
8316 function-local names? For partial symbols, we should probably be
8317 ignoring them. */
8318 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8319 dwarf_tag_name (parent->tag),
8320 sect_offset_str (pdi->sect_off));
8321 parent->scope = grandparent_scope;
8322 }
8323
8324 parent->scope_set = 1;
8325 return parent->scope;
8326 }
8327
8328 /* Return the fully scoped name associated with PDI, from compilation unit
8329 CU. The result will be allocated with malloc. */
8330
8331 static gdb::unique_xmalloc_ptr<char>
8332 partial_die_full_name (struct partial_die_info *pdi,
8333 struct dwarf2_cu *cu)
8334 {
8335 const char *parent_scope;
8336
8337 /* If this is a template instantiation, we can not work out the
8338 template arguments from partial DIEs. So, unfortunately, we have
8339 to go through the full DIEs. At least any work we do building
8340 types here will be reused if full symbols are loaded later. */
8341 if (pdi->has_template_arguments)
8342 {
8343 pdi->fixup (cu);
8344
8345 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
8346 {
8347 struct die_info *die;
8348 struct attribute attr;
8349 struct dwarf2_cu *ref_cu = cu;
8350
8351 /* DW_FORM_ref_addr is using section offset. */
8352 attr.name = (enum dwarf_attribute) 0;
8353 attr.form = DW_FORM_ref_addr;
8354 attr.u.unsnd = to_underlying (pdi->sect_off);
8355 die = follow_die_ref (NULL, &attr, &ref_cu);
8356
8357 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8358 }
8359 }
8360
8361 parent_scope = partial_die_parent_scope (pdi, cu);
8362 if (parent_scope == NULL)
8363 return NULL;
8364 else
8365 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8366 pdi->name (cu),
8367 0, cu));
8368 }
8369
8370 static void
8371 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8372 {
8373 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8374 struct objfile *objfile = per_objfile->objfile;
8375 struct gdbarch *gdbarch = objfile->arch ();
8376 CORE_ADDR addr = 0;
8377 const char *actual_name = NULL;
8378 CORE_ADDR baseaddr;
8379
8380 baseaddr = objfile->text_section_offset ();
8381
8382 gdb::unique_xmalloc_ptr<char> built_actual_name
8383 = partial_die_full_name (pdi, cu);
8384 if (built_actual_name != NULL)
8385 actual_name = built_actual_name.get ();
8386
8387 if (actual_name == NULL)
8388 actual_name = pdi->name (cu);
8389
8390 partial_symbol psymbol;
8391 memset (&psymbol, 0, sizeof (psymbol));
8392 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8393 psymbol.ginfo.set_section_index (-1);
8394
8395 /* The code below indicates that the psymbol should be installed by
8396 setting this. */
8397 gdb::optional<psymbol_placement> where;
8398
8399 switch (pdi->tag)
8400 {
8401 case DW_TAG_inlined_subroutine:
8402 case DW_TAG_subprogram:
8403 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8404 - baseaddr);
8405 if (pdi->is_external
8406 || cu->language == language_ada
8407 || (cu->language == language_fortran
8408 && pdi->die_parent != NULL
8409 && pdi->die_parent->tag == DW_TAG_subprogram))
8410 {
8411 /* Normally, only "external" DIEs are part of the global scope.
8412 But in Ada and Fortran, we want to be able to access nested
8413 procedures globally. So all Ada and Fortran subprograms are
8414 stored in the global scope. */
8415 where = psymbol_placement::GLOBAL;
8416 }
8417 else
8418 where = psymbol_placement::STATIC;
8419
8420 psymbol.domain = VAR_DOMAIN;
8421 psymbol.aclass = LOC_BLOCK;
8422 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8423 psymbol.ginfo.value.address = addr;
8424
8425 if (pdi->main_subprogram && actual_name != NULL)
8426 set_objfile_main_name (objfile, actual_name, cu->language);
8427 break;
8428 case DW_TAG_constant:
8429 psymbol.domain = VAR_DOMAIN;
8430 psymbol.aclass = LOC_STATIC;
8431 where = (pdi->is_external
8432 ? psymbol_placement::GLOBAL
8433 : psymbol_placement::STATIC);
8434 break;
8435 case DW_TAG_variable:
8436 if (pdi->d.locdesc)
8437 addr = decode_locdesc (pdi->d.locdesc, cu);
8438
8439 if (pdi->d.locdesc
8440 && addr == 0
8441 && !per_objfile->per_bfd->has_section_at_zero)
8442 {
8443 /* A global or static variable may also have been stripped
8444 out by the linker if unused, in which case its address
8445 will be nullified; do not add such variables into partial
8446 symbol table then. */
8447 }
8448 else if (pdi->is_external)
8449 {
8450 /* Global Variable.
8451 Don't enter into the minimal symbol tables as there is
8452 a minimal symbol table entry from the ELF symbols already.
8453 Enter into partial symbol table if it has a location
8454 descriptor or a type.
8455 If the location descriptor is missing, new_symbol will create
8456 a LOC_UNRESOLVED symbol, the address of the variable will then
8457 be determined from the minimal symbol table whenever the variable
8458 is referenced.
8459 The address for the partial symbol table entry is not
8460 used by GDB, but it comes in handy for debugging partial symbol
8461 table building. */
8462
8463 if (pdi->d.locdesc || pdi->has_type)
8464 {
8465 psymbol.domain = VAR_DOMAIN;
8466 psymbol.aclass = LOC_STATIC;
8467 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8468 psymbol.ginfo.value.address = addr;
8469 where = psymbol_placement::GLOBAL;
8470 }
8471 }
8472 else
8473 {
8474 int has_loc = pdi->d.locdesc != NULL;
8475
8476 /* Static Variable. Skip symbols whose value we cannot know (those
8477 without location descriptors or constant values). */
8478 if (!has_loc && !pdi->has_const_value)
8479 return;
8480
8481 psymbol.domain = VAR_DOMAIN;
8482 psymbol.aclass = LOC_STATIC;
8483 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8484 if (has_loc)
8485 psymbol.ginfo.value.address = addr;
8486 where = psymbol_placement::STATIC;
8487 }
8488 break;
8489 case DW_TAG_array_type:
8490 case DW_TAG_typedef:
8491 case DW_TAG_base_type:
8492 case DW_TAG_subrange_type:
8493 psymbol.domain = VAR_DOMAIN;
8494 psymbol.aclass = LOC_TYPEDEF;
8495 where = psymbol_placement::STATIC;
8496 break;
8497 case DW_TAG_imported_declaration:
8498 case DW_TAG_namespace:
8499 psymbol.domain = VAR_DOMAIN;
8500 psymbol.aclass = LOC_TYPEDEF;
8501 where = psymbol_placement::GLOBAL;
8502 break;
8503 case DW_TAG_module:
8504 /* With Fortran 77 there might be a "BLOCK DATA" module
8505 available without any name. If so, we skip the module as it
8506 doesn't bring any value. */
8507 if (actual_name != nullptr)
8508 {
8509 psymbol.domain = MODULE_DOMAIN;
8510 psymbol.aclass = LOC_TYPEDEF;
8511 where = psymbol_placement::GLOBAL;
8512 }
8513 break;
8514 case DW_TAG_class_type:
8515 case DW_TAG_interface_type:
8516 case DW_TAG_structure_type:
8517 case DW_TAG_union_type:
8518 case DW_TAG_enumeration_type:
8519 /* Skip external references. The DWARF standard says in the section
8520 about "Structure, Union, and Class Type Entries": "An incomplete
8521 structure, union or class type is represented by a structure,
8522 union or class entry that does not have a byte size attribute
8523 and that has a DW_AT_declaration attribute." */
8524 if (!pdi->has_byte_size && pdi->is_declaration)
8525 return;
8526
8527 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8528 static vs. global. */
8529 psymbol.domain = STRUCT_DOMAIN;
8530 psymbol.aclass = LOC_TYPEDEF;
8531 where = (cu->language == language_cplus
8532 ? psymbol_placement::GLOBAL
8533 : psymbol_placement::STATIC);
8534 break;
8535 case DW_TAG_enumerator:
8536 psymbol.domain = VAR_DOMAIN;
8537 psymbol.aclass = LOC_CONST;
8538 where = (cu->language == language_cplus
8539 ? psymbol_placement::GLOBAL
8540 : psymbol_placement::STATIC);
8541 break;
8542 default:
8543 break;
8544 }
8545
8546 if (where.has_value ())
8547 {
8548 if (built_actual_name != nullptr)
8549 actual_name = objfile->intern (actual_name);
8550 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8551 psymbol.ginfo.set_linkage_name (actual_name);
8552 else
8553 {
8554 psymbol.ginfo.set_demangled_name (actual_name,
8555 &objfile->objfile_obstack);
8556 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8557 }
8558 cu->per_cu->v.psymtab->add_psymbol (psymbol, *where, objfile);
8559 }
8560 }
8561
8562 /* Read a partial die corresponding to a namespace; also, add a symbol
8563 corresponding to that namespace to the symbol table. NAMESPACE is
8564 the name of the enclosing namespace. */
8565
8566 static void
8567 add_partial_namespace (struct partial_die_info *pdi,
8568 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8569 int set_addrmap, struct dwarf2_cu *cu)
8570 {
8571 /* Add a symbol for the namespace. */
8572
8573 add_partial_symbol (pdi, cu);
8574
8575 /* Now scan partial symbols in that namespace. */
8576
8577 if (pdi->has_children)
8578 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8579 }
8580
8581 /* Read a partial die corresponding to a Fortran module. */
8582
8583 static void
8584 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8585 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8586 {
8587 /* Add a symbol for the namespace. */
8588
8589 add_partial_symbol (pdi, cu);
8590
8591 /* Now scan partial symbols in that module. */
8592
8593 if (pdi->has_children)
8594 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8595 }
8596
8597 /* Read a partial die corresponding to a subprogram or an inlined
8598 subprogram and create a partial symbol for that subprogram.
8599 When the CU language allows it, this routine also defines a partial
8600 symbol for each nested subprogram that this subprogram contains.
8601 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8602 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8603
8604 PDI may also be a lexical block, in which case we simply search
8605 recursively for subprograms defined inside that lexical block.
8606 Again, this is only performed when the CU language allows this
8607 type of definitions. */
8608
8609 static void
8610 add_partial_subprogram (struct partial_die_info *pdi,
8611 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8612 int set_addrmap, struct dwarf2_cu *cu)
8613 {
8614 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8615 {
8616 if (pdi->has_pc_info)
8617 {
8618 if (pdi->lowpc < *lowpc)
8619 *lowpc = pdi->lowpc;
8620 if (pdi->highpc > *highpc)
8621 *highpc = pdi->highpc;
8622 if (set_addrmap)
8623 {
8624 struct objfile *objfile = cu->per_objfile->objfile;
8625 struct gdbarch *gdbarch = objfile->arch ();
8626 CORE_ADDR baseaddr;
8627 CORE_ADDR this_highpc;
8628 CORE_ADDR this_lowpc;
8629
8630 baseaddr = objfile->text_section_offset ();
8631 this_lowpc
8632 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8633 pdi->lowpc + baseaddr)
8634 - baseaddr);
8635 this_highpc
8636 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8637 pdi->highpc + baseaddr)
8638 - baseaddr);
8639 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8640 this_lowpc, this_highpc - 1,
8641 cu->per_cu->v.psymtab);
8642 }
8643 }
8644
8645 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8646 {
8647 if (!pdi->is_declaration)
8648 /* Ignore subprogram DIEs that do not have a name, they are
8649 illegal. Do not emit a complaint at this point, we will
8650 do so when we convert this psymtab into a symtab. */
8651 if (pdi->name (cu))
8652 add_partial_symbol (pdi, cu);
8653 }
8654 }
8655
8656 if (! pdi->has_children)
8657 return;
8658
8659 if (cu->language == language_ada || cu->language == language_fortran)
8660 {
8661 pdi = pdi->die_child;
8662 while (pdi != NULL)
8663 {
8664 pdi->fixup (cu);
8665 if (pdi->tag == DW_TAG_subprogram
8666 || pdi->tag == DW_TAG_inlined_subroutine
8667 || pdi->tag == DW_TAG_lexical_block)
8668 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8669 pdi = pdi->die_sibling;
8670 }
8671 }
8672 }
8673
8674 /* Read a partial die corresponding to an enumeration type. */
8675
8676 static void
8677 add_partial_enumeration (struct partial_die_info *enum_pdi,
8678 struct dwarf2_cu *cu)
8679 {
8680 struct partial_die_info *pdi;
8681
8682 if (enum_pdi->name (cu) != NULL)
8683 add_partial_symbol (enum_pdi, cu);
8684
8685 pdi = enum_pdi->die_child;
8686 while (pdi)
8687 {
8688 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8689 complaint (_("malformed enumerator DIE ignored"));
8690 else
8691 add_partial_symbol (pdi, cu);
8692 pdi = pdi->die_sibling;
8693 }
8694 }
8695
8696 /* Return the initial uleb128 in the die at INFO_PTR. */
8697
8698 static unsigned int
8699 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8700 {
8701 unsigned int bytes_read;
8702
8703 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8704 }
8705
8706 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8707 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8708
8709 Return the corresponding abbrev, or NULL if the number is zero (indicating
8710 an empty DIE). In either case *BYTES_READ will be set to the length of
8711 the initial number. */
8712
8713 static const struct abbrev_info *
8714 peek_die_abbrev (const die_reader_specs &reader,
8715 const gdb_byte *info_ptr, unsigned int *bytes_read)
8716 {
8717 dwarf2_cu *cu = reader.cu;
8718 bfd *abfd = reader.abfd;
8719 unsigned int abbrev_number
8720 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8721
8722 if (abbrev_number == 0)
8723 return NULL;
8724
8725 const abbrev_info *abbrev
8726 = reader.abbrev_table->lookup_abbrev (abbrev_number);
8727 if (!abbrev)
8728 {
8729 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8730 " at offset %s [in module %s]"),
8731 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8732 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8733 }
8734
8735 return abbrev;
8736 }
8737
8738 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8739 Returns a pointer to the end of a series of DIEs, terminated by an empty
8740 DIE. Any children of the skipped DIEs will also be skipped. */
8741
8742 static const gdb_byte *
8743 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8744 {
8745 while (1)
8746 {
8747 unsigned int bytes_read;
8748 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8749 &bytes_read);
8750
8751 if (abbrev == NULL)
8752 return info_ptr + bytes_read;
8753 else
8754 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8755 }
8756 }
8757
8758 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8759 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8760 abbrev corresponding to that skipped uleb128 should be passed in
8761 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8762 children. */
8763
8764 static const gdb_byte *
8765 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8766 const struct abbrev_info *abbrev)
8767 {
8768 unsigned int bytes_read;
8769 struct attribute attr;
8770 bfd *abfd = reader->abfd;
8771 struct dwarf2_cu *cu = reader->cu;
8772 const gdb_byte *buffer = reader->buffer;
8773 const gdb_byte *buffer_end = reader->buffer_end;
8774 unsigned int form, i;
8775
8776 for (i = 0; i < abbrev->num_attrs; i++)
8777 {
8778 /* The only abbrev we care about is DW_AT_sibling. */
8779 if (abbrev->attrs[i].name == DW_AT_sibling)
8780 {
8781 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8782 if (attr.form == DW_FORM_ref_addr)
8783 complaint (_("ignoring absolute DW_AT_sibling"));
8784 else
8785 {
8786 sect_offset off = attr.get_ref_die_offset ();
8787 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8788
8789 if (sibling_ptr < info_ptr)
8790 complaint (_("DW_AT_sibling points backwards"));
8791 else if (sibling_ptr > reader->buffer_end)
8792 reader->die_section->overflow_complaint ();
8793 else
8794 return sibling_ptr;
8795 }
8796 }
8797
8798 /* If it isn't DW_AT_sibling, skip this attribute. */
8799 form = abbrev->attrs[i].form;
8800 skip_attribute:
8801 switch (form)
8802 {
8803 case DW_FORM_ref_addr:
8804 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8805 and later it is offset sized. */
8806 if (cu->header.version == 2)
8807 info_ptr += cu->header.addr_size;
8808 else
8809 info_ptr += cu->header.offset_size;
8810 break;
8811 case DW_FORM_GNU_ref_alt:
8812 info_ptr += cu->header.offset_size;
8813 break;
8814 case DW_FORM_addr:
8815 info_ptr += cu->header.addr_size;
8816 break;
8817 case DW_FORM_data1:
8818 case DW_FORM_ref1:
8819 case DW_FORM_flag:
8820 case DW_FORM_strx1:
8821 info_ptr += 1;
8822 break;
8823 case DW_FORM_flag_present:
8824 case DW_FORM_implicit_const:
8825 break;
8826 case DW_FORM_data2:
8827 case DW_FORM_ref2:
8828 case DW_FORM_strx2:
8829 info_ptr += 2;
8830 break;
8831 case DW_FORM_strx3:
8832 info_ptr += 3;
8833 break;
8834 case DW_FORM_data4:
8835 case DW_FORM_ref4:
8836 case DW_FORM_strx4:
8837 info_ptr += 4;
8838 break;
8839 case DW_FORM_data8:
8840 case DW_FORM_ref8:
8841 case DW_FORM_ref_sig8:
8842 info_ptr += 8;
8843 break;
8844 case DW_FORM_data16:
8845 info_ptr += 16;
8846 break;
8847 case DW_FORM_string:
8848 read_direct_string (abfd, info_ptr, &bytes_read);
8849 info_ptr += bytes_read;
8850 break;
8851 case DW_FORM_sec_offset:
8852 case DW_FORM_strp:
8853 case DW_FORM_GNU_strp_alt:
8854 info_ptr += cu->header.offset_size;
8855 break;
8856 case DW_FORM_exprloc:
8857 case DW_FORM_block:
8858 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8859 info_ptr += bytes_read;
8860 break;
8861 case DW_FORM_block1:
8862 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8863 break;
8864 case DW_FORM_block2:
8865 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8866 break;
8867 case DW_FORM_block4:
8868 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8869 break;
8870 case DW_FORM_addrx:
8871 case DW_FORM_strx:
8872 case DW_FORM_sdata:
8873 case DW_FORM_udata:
8874 case DW_FORM_ref_udata:
8875 case DW_FORM_GNU_addr_index:
8876 case DW_FORM_GNU_str_index:
8877 case DW_FORM_rnglistx:
8878 case DW_FORM_loclistx:
8879 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8880 break;
8881 case DW_FORM_indirect:
8882 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8883 info_ptr += bytes_read;
8884 /* We need to continue parsing from here, so just go back to
8885 the top. */
8886 goto skip_attribute;
8887
8888 default:
8889 error (_("Dwarf Error: Cannot handle %s "
8890 "in DWARF reader [in module %s]"),
8891 dwarf_form_name (form),
8892 bfd_get_filename (abfd));
8893 }
8894 }
8895
8896 if (abbrev->has_children)
8897 return skip_children (reader, info_ptr);
8898 else
8899 return info_ptr;
8900 }
8901
8902 /* Locate ORIG_PDI's sibling.
8903 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8904
8905 static const gdb_byte *
8906 locate_pdi_sibling (const struct die_reader_specs *reader,
8907 struct partial_die_info *orig_pdi,
8908 const gdb_byte *info_ptr)
8909 {
8910 /* Do we know the sibling already? */
8911
8912 if (orig_pdi->sibling)
8913 return orig_pdi->sibling;
8914
8915 /* Are there any children to deal with? */
8916
8917 if (!orig_pdi->has_children)
8918 return info_ptr;
8919
8920 /* Skip the children the long way. */
8921
8922 return skip_children (reader, info_ptr);
8923 }
8924
8925 /* Expand this partial symbol table into a full symbol table. SELF is
8926 not NULL. */
8927
8928 void
8929 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8930 {
8931 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8932
8933 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
8934
8935 /* If this psymtab is constructed from a debug-only objfile, the
8936 has_section_at_zero flag will not necessarily be correct. We
8937 can get the correct value for this flag by looking at the data
8938 associated with the (presumably stripped) associated objfile. */
8939 if (objfile->separate_debug_objfile_backlink)
8940 {
8941 dwarf2_per_objfile *per_objfile_backlink
8942 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8943
8944 per_objfile->per_bfd->has_section_at_zero
8945 = per_objfile_backlink->per_bfd->has_section_at_zero;
8946 }
8947
8948 expand_psymtab (objfile);
8949
8950 process_cu_includes (per_objfile);
8951 }
8952 \f
8953 /* Reading in full CUs. */
8954
8955 /* Add PER_CU to the queue. */
8956
8957 static void
8958 queue_comp_unit (dwarf2_per_cu_data *per_cu,
8959 dwarf2_per_objfile *per_objfile,
8960 enum language pretend_language)
8961 {
8962 per_cu->queued = 1;
8963
8964 gdb_assert (per_objfile->per_bfd->queue.has_value ());
8965 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
8966 }
8967
8968 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
8969
8970 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8971 dependency.
8972
8973 Return true if maybe_queue_comp_unit requires the caller to load the CU's
8974 DIEs, false otherwise.
8975
8976 Explanation: there is an invariant that if a CU is queued for expansion
8977 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
8978 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
8979 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
8980 are not yet loaded, the the caller must load the CU's DIEs to ensure the
8981 invariant is respected.
8982
8983 The caller is therefore not required to load the CU's DIEs (we return false)
8984 if:
8985
8986 - the CU is already expanded, and therefore does not get enqueued
8987 - the CU gets enqueued for expansion, but its DIEs are already loaded
8988
8989 Note that the caller should not use this function's return value as an
8990 indicator of whether the CU's DIEs are loaded right now, it should check
8991 that by calling `dwarf2_per_objfile::get_cu` instead. */
8992
8993 static int
8994 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8995 dwarf2_per_cu_data *per_cu,
8996 dwarf2_per_objfile *per_objfile,
8997 enum language pretend_language)
8998 {
8999 /* We may arrive here during partial symbol reading, if we need full
9000 DIEs to process an unusual case (e.g. template arguments). Do
9001 not queue PER_CU, just tell our caller to load its DIEs. */
9002 if (per_cu->per_bfd->reading_partial_symbols)
9003 {
9004 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9005
9006 if (cu == NULL || cu->dies == NULL)
9007 return 1;
9008 return 0;
9009 }
9010
9011 /* Mark the dependence relation so that we don't flush PER_CU
9012 too early. */
9013 if (dependent_cu != NULL)
9014 dwarf2_add_dependence (dependent_cu, per_cu);
9015
9016 /* If it's already on the queue, we have nothing to do. */
9017 if (per_cu->queued)
9018 {
9019 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
9020 loaded. */
9021 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
9022
9023 /* If the CU is queued for expansion, it should not already be
9024 expanded. */
9025 gdb_assert (!per_objfile->symtab_set_p (per_cu));
9026
9027 /* The DIEs are already loaded, the caller doesn't need to do it. */
9028 return 0;
9029 }
9030
9031 bool queued = false;
9032 if (!per_objfile->symtab_set_p (per_cu))
9033 {
9034 /* Add it to the queue. */
9035 queue_comp_unit (per_cu, per_objfile, pretend_language);
9036 queued = true;
9037 }
9038
9039 /* If the compilation unit is already loaded, just mark it as
9040 used. */
9041 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9042 if (cu != nullptr)
9043 cu->last_used = 0;
9044
9045 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
9046 and the DIEs are not already loaded. */
9047 return queued && cu == nullptr;
9048 }
9049
9050 /* Process the queue. */
9051
9052 static void
9053 process_queue (dwarf2_per_objfile *per_objfile)
9054 {
9055 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
9056 objfile_name (per_objfile->objfile));
9057
9058 /* The queue starts out with one item, but following a DIE reference
9059 may load a new CU, adding it to the end of the queue. */
9060 while (!per_objfile->per_bfd->queue->empty ())
9061 {
9062 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
9063 dwarf2_per_cu_data *per_cu = item.per_cu;
9064
9065 if (!per_objfile->symtab_set_p (per_cu))
9066 {
9067 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9068
9069 /* Skip dummy CUs. */
9070 if (cu != nullptr)
9071 {
9072 unsigned int debug_print_threshold;
9073 char buf[100];
9074
9075 if (per_cu->is_debug_types)
9076 {
9077 struct signatured_type *sig_type =
9078 (struct signatured_type *) per_cu;
9079
9080 sprintf (buf, "TU %s at offset %s",
9081 hex_string (sig_type->signature),
9082 sect_offset_str (per_cu->sect_off));
9083 /* There can be 100s of TUs.
9084 Only print them in verbose mode. */
9085 debug_print_threshold = 2;
9086 }
9087 else
9088 {
9089 sprintf (buf, "CU at offset %s",
9090 sect_offset_str (per_cu->sect_off));
9091 debug_print_threshold = 1;
9092 }
9093
9094 if (dwarf_read_debug >= debug_print_threshold)
9095 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
9096
9097 if (per_cu->is_debug_types)
9098 process_full_type_unit (cu, item.pretend_language);
9099 else
9100 process_full_comp_unit (cu, item.pretend_language);
9101
9102 if (dwarf_read_debug >= debug_print_threshold)
9103 dwarf_read_debug_printf ("Done expanding %s", buf);
9104 }
9105 }
9106
9107 per_cu->queued = 0;
9108 per_objfile->per_bfd->queue->pop ();
9109 }
9110
9111 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
9112 objfile_name (per_objfile->objfile));
9113 }
9114
9115 /* Read in full symbols for PST, and anything it depends on. */
9116
9117 void
9118 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9119 {
9120 gdb_assert (!readin_p (objfile));
9121
9122 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9123 free_cached_comp_units freer (per_objfile);
9124 expand_dependencies (objfile);
9125
9126 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9127 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9128 }
9129
9130 /* See psympriv.h. */
9131
9132 bool
9133 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9134 {
9135 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9136 return per_objfile->symtab_set_p (per_cu_data);
9137 }
9138
9139 /* See psympriv.h. */
9140
9141 compunit_symtab *
9142 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9143 {
9144 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9145 return per_objfile->get_symtab (per_cu_data);
9146 }
9147
9148 /* Trivial hash function for die_info: the hash value of a DIE
9149 is its offset in .debug_info for this objfile. */
9150
9151 static hashval_t
9152 die_hash (const void *item)
9153 {
9154 const struct die_info *die = (const struct die_info *) item;
9155
9156 return to_underlying (die->sect_off);
9157 }
9158
9159 /* Trivial comparison function for die_info structures: two DIEs
9160 are equal if they have the same offset. */
9161
9162 static int
9163 die_eq (const void *item_lhs, const void *item_rhs)
9164 {
9165 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9166 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9167
9168 return die_lhs->sect_off == die_rhs->sect_off;
9169 }
9170
9171 /* Load the DIEs associated with PER_CU into memory.
9172
9173 In some cases, the caller, while reading partial symbols, will need to load
9174 the full symbols for the CU for some reason. It will already have a
9175 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
9176 rather than creating a new one. */
9177
9178 static void
9179 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9180 dwarf2_per_objfile *per_objfile,
9181 dwarf2_cu *existing_cu,
9182 bool skip_partial,
9183 enum language pretend_language)
9184 {
9185 gdb_assert (! this_cu->is_debug_types);
9186
9187 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
9188 if (reader.dummy_p)
9189 return;
9190
9191 struct dwarf2_cu *cu = reader.cu;
9192 const gdb_byte *info_ptr = reader.info_ptr;
9193
9194 gdb_assert (cu->die_hash == NULL);
9195 cu->die_hash =
9196 htab_create_alloc_ex (cu->header.length / 12,
9197 die_hash,
9198 die_eq,
9199 NULL,
9200 &cu->comp_unit_obstack,
9201 hashtab_obstack_allocate,
9202 dummy_obstack_deallocate);
9203
9204 if (reader.comp_unit_die->has_children)
9205 reader.comp_unit_die->child
9206 = read_die_and_siblings (&reader, reader.info_ptr,
9207 &info_ptr, reader.comp_unit_die);
9208 cu->dies = reader.comp_unit_die;
9209 /* comp_unit_die is not stored in die_hash, no need. */
9210
9211 /* We try not to read any attributes in this function, because not
9212 all CUs needed for references have been loaded yet, and symbol
9213 table processing isn't initialized. But we have to set the CU language,
9214 or we won't be able to build types correctly.
9215 Similarly, if we do not read the producer, we can not apply
9216 producer-specific interpretation. */
9217 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9218
9219 reader.keep ();
9220 }
9221
9222 /* Add a DIE to the delayed physname list. */
9223
9224 static void
9225 add_to_method_list (struct type *type, int fnfield_index, int index,
9226 const char *name, struct die_info *die,
9227 struct dwarf2_cu *cu)
9228 {
9229 struct delayed_method_info mi;
9230 mi.type = type;
9231 mi.fnfield_index = fnfield_index;
9232 mi.index = index;
9233 mi.name = name;
9234 mi.die = die;
9235 cu->method_list.push_back (mi);
9236 }
9237
9238 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9239 "const" / "volatile". If so, decrements LEN by the length of the
9240 modifier and return true. Otherwise return false. */
9241
9242 template<size_t N>
9243 static bool
9244 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9245 {
9246 size_t mod_len = sizeof (mod) - 1;
9247 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9248 {
9249 len -= mod_len;
9250 return true;
9251 }
9252 return false;
9253 }
9254
9255 /* Compute the physnames of any methods on the CU's method list.
9256
9257 The computation of method physnames is delayed in order to avoid the
9258 (bad) condition that one of the method's formal parameters is of an as yet
9259 incomplete type. */
9260
9261 static void
9262 compute_delayed_physnames (struct dwarf2_cu *cu)
9263 {
9264 /* Only C++ delays computing physnames. */
9265 if (cu->method_list.empty ())
9266 return;
9267 gdb_assert (cu->language == language_cplus);
9268
9269 for (const delayed_method_info &mi : cu->method_list)
9270 {
9271 const char *physname;
9272 struct fn_fieldlist *fn_flp
9273 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9274 physname = dwarf2_physname (mi.name, mi.die, cu);
9275 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9276 = physname ? physname : "";
9277
9278 /* Since there's no tag to indicate whether a method is a
9279 const/volatile overload, extract that information out of the
9280 demangled name. */
9281 if (physname != NULL)
9282 {
9283 size_t len = strlen (physname);
9284
9285 while (1)
9286 {
9287 if (physname[len] == ')') /* shortcut */
9288 break;
9289 else if (check_modifier (physname, len, " const"))
9290 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9291 else if (check_modifier (physname, len, " volatile"))
9292 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9293 else
9294 break;
9295 }
9296 }
9297 }
9298
9299 /* The list is no longer needed. */
9300 cu->method_list.clear ();
9301 }
9302
9303 /* Go objects should be embedded in a DW_TAG_module DIE,
9304 and it's not clear if/how imported objects will appear.
9305 To keep Go support simple until that's worked out,
9306 go back through what we've read and create something usable.
9307 We could do this while processing each DIE, and feels kinda cleaner,
9308 but that way is more invasive.
9309 This is to, for example, allow the user to type "p var" or "b main"
9310 without having to specify the package name, and allow lookups
9311 of module.object to work in contexts that use the expression
9312 parser. */
9313
9314 static void
9315 fixup_go_packaging (struct dwarf2_cu *cu)
9316 {
9317 gdb::unique_xmalloc_ptr<char> package_name;
9318 struct pending *list;
9319 int i;
9320
9321 for (list = *cu->get_builder ()->get_global_symbols ();
9322 list != NULL;
9323 list = list->next)
9324 {
9325 for (i = 0; i < list->nsyms; ++i)
9326 {
9327 struct symbol *sym = list->symbol[i];
9328
9329 if (sym->language () == language_go
9330 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9331 {
9332 gdb::unique_xmalloc_ptr<char> this_package_name
9333 (go_symbol_package_name (sym));
9334
9335 if (this_package_name == NULL)
9336 continue;
9337 if (package_name == NULL)
9338 package_name = std::move (this_package_name);
9339 else
9340 {
9341 struct objfile *objfile = cu->per_objfile->objfile;
9342 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9343 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9344 (symbol_symtab (sym) != NULL
9345 ? symtab_to_filename_for_display
9346 (symbol_symtab (sym))
9347 : objfile_name (objfile)),
9348 this_package_name.get (), package_name.get ());
9349 }
9350 }
9351 }
9352 }
9353
9354 if (package_name != NULL)
9355 {
9356 struct objfile *objfile = cu->per_objfile->objfile;
9357 const char *saved_package_name = objfile->intern (package_name.get ());
9358 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9359 saved_package_name);
9360 struct symbol *sym;
9361
9362 sym = new (&objfile->objfile_obstack) symbol;
9363 sym->set_language (language_go, &objfile->objfile_obstack);
9364 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9365 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9366 e.g., "main" finds the "main" module and not C's main(). */
9367 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9368 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9369 SYMBOL_TYPE (sym) = type;
9370
9371 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9372 }
9373 }
9374
9375 /* Allocate a fully-qualified name consisting of the two parts on the
9376 obstack. */
9377
9378 static const char *
9379 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9380 {
9381 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9382 }
9383
9384 /* A helper that allocates a variant part to attach to a Rust enum
9385 type. OBSTACK is where the results should be allocated. TYPE is
9386 the type we're processing. DISCRIMINANT_INDEX is the index of the
9387 discriminant. It must be the index of one of the fields of TYPE,
9388 or -1 to mean there is no discriminant (univariant enum).
9389 DEFAULT_INDEX is the index of the default field; or -1 if there is
9390 no default. RANGES is indexed by "effective" field number (the
9391 field index, but omitting the discriminant and default fields) and
9392 must hold the discriminant values used by the variants. Note that
9393 RANGES must have a lifetime at least as long as OBSTACK -- either
9394 already allocated on it, or static. */
9395
9396 static void
9397 alloc_rust_variant (struct obstack *obstack, struct type *type,
9398 int discriminant_index, int default_index,
9399 gdb::array_view<discriminant_range> ranges)
9400 {
9401 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9402 gdb_assert (discriminant_index == -1
9403 || (discriminant_index >= 0
9404 && discriminant_index < type->num_fields ()));
9405 gdb_assert (default_index == -1
9406 || (default_index >= 0 && default_index < type->num_fields ()));
9407
9408 /* We have one variant for each non-discriminant field. */
9409 int n_variants = type->num_fields ();
9410 if (discriminant_index != -1)
9411 --n_variants;
9412
9413 variant *variants = new (obstack) variant[n_variants];
9414 int var_idx = 0;
9415 int range_idx = 0;
9416 for (int i = 0; i < type->num_fields (); ++i)
9417 {
9418 if (i == discriminant_index)
9419 continue;
9420
9421 variants[var_idx].first_field = i;
9422 variants[var_idx].last_field = i + 1;
9423
9424 /* The default field does not need a range, but other fields do.
9425 We skipped the discriminant above. */
9426 if (i != default_index)
9427 {
9428 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9429 ++range_idx;
9430 }
9431
9432 ++var_idx;
9433 }
9434
9435 gdb_assert (range_idx == ranges.size ());
9436 gdb_assert (var_idx == n_variants);
9437
9438 variant_part *part = new (obstack) variant_part;
9439 part->discriminant_index = discriminant_index;
9440 /* If there is no discriminant, then whether it is signed is of no
9441 consequence. */
9442 part->is_unsigned
9443 = (discriminant_index == -1
9444 ? false
9445 : type->field (discriminant_index).type ()->is_unsigned ());
9446 part->variants = gdb::array_view<variant> (variants, n_variants);
9447
9448 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9449 gdb::array_view<variant_part> *prop_value
9450 = new (storage) gdb::array_view<variant_part> (part, 1);
9451
9452 struct dynamic_prop prop;
9453 prop.set_variant_parts (prop_value);
9454
9455 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9456 }
9457
9458 /* Some versions of rustc emitted enums in an unusual way.
9459
9460 Ordinary enums were emitted as unions. The first element of each
9461 structure in the union was named "RUST$ENUM$DISR". This element
9462 held the discriminant.
9463
9464 These versions of Rust also implemented the "non-zero"
9465 optimization. When the enum had two values, and one is empty and
9466 the other holds a pointer that cannot be zero, the pointer is used
9467 as the discriminant, with a zero value meaning the empty variant.
9468 Here, the union's first member is of the form
9469 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9470 where the fieldnos are the indices of the fields that should be
9471 traversed in order to find the field (which may be several fields deep)
9472 and the variantname is the name of the variant of the case when the
9473 field is zero.
9474
9475 This function recognizes whether TYPE is of one of these forms,
9476 and, if so, smashes it to be a variant type. */
9477
9478 static void
9479 quirk_rust_enum (struct type *type, struct objfile *objfile)
9480 {
9481 gdb_assert (type->code () == TYPE_CODE_UNION);
9482
9483 /* We don't need to deal with empty enums. */
9484 if (type->num_fields () == 0)
9485 return;
9486
9487 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9488 if (type->num_fields () == 1
9489 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9490 {
9491 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9492
9493 /* Decode the field name to find the offset of the
9494 discriminant. */
9495 ULONGEST bit_offset = 0;
9496 struct type *field_type = type->field (0).type ();
9497 while (name[0] >= '0' && name[0] <= '9')
9498 {
9499 char *tail;
9500 unsigned long index = strtoul (name, &tail, 10);
9501 name = tail;
9502 if (*name != '$'
9503 || index >= field_type->num_fields ()
9504 || (TYPE_FIELD_LOC_KIND (field_type, index)
9505 != FIELD_LOC_KIND_BITPOS))
9506 {
9507 complaint (_("Could not parse Rust enum encoding string \"%s\""
9508 "[in module %s]"),
9509 TYPE_FIELD_NAME (type, 0),
9510 objfile_name (objfile));
9511 return;
9512 }
9513 ++name;
9514
9515 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9516 field_type = field_type->field (index).type ();
9517 }
9518
9519 /* Smash this type to be a structure type. We have to do this
9520 because the type has already been recorded. */
9521 type->set_code (TYPE_CODE_STRUCT);
9522 type->set_num_fields (3);
9523 /* Save the field we care about. */
9524 struct field saved_field = type->field (0);
9525 type->set_fields
9526 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9527
9528 /* Put the discriminant at index 0. */
9529 type->field (0).set_type (field_type);
9530 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9531 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9532 SET_FIELD_BITPOS (type->field (0), bit_offset);
9533
9534 /* The order of fields doesn't really matter, so put the real
9535 field at index 1 and the data-less field at index 2. */
9536 type->field (1) = saved_field;
9537 TYPE_FIELD_NAME (type, 1)
9538 = rust_last_path_segment (type->field (1).type ()->name ());
9539 type->field (1).type ()->set_name
9540 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9541 TYPE_FIELD_NAME (type, 1)));
9542
9543 const char *dataless_name
9544 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9545 name);
9546 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9547 dataless_name);
9548 type->field (2).set_type (dataless_type);
9549 /* NAME points into the original discriminant name, which
9550 already has the correct lifetime. */
9551 TYPE_FIELD_NAME (type, 2) = name;
9552 SET_FIELD_BITPOS (type->field (2), 0);
9553
9554 /* Indicate that this is a variant type. */
9555 static discriminant_range ranges[1] = { { 0, 0 } };
9556 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9557 }
9558 /* A union with a single anonymous field is probably an old-style
9559 univariant enum. */
9560 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9561 {
9562 /* Smash this type to be a structure type. We have to do this
9563 because the type has already been recorded. */
9564 type->set_code (TYPE_CODE_STRUCT);
9565
9566 struct type *field_type = type->field (0).type ();
9567 const char *variant_name
9568 = rust_last_path_segment (field_type->name ());
9569 TYPE_FIELD_NAME (type, 0) = variant_name;
9570 field_type->set_name
9571 (rust_fully_qualify (&objfile->objfile_obstack,
9572 type->name (), variant_name));
9573
9574 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
9575 }
9576 else
9577 {
9578 struct type *disr_type = nullptr;
9579 for (int i = 0; i < type->num_fields (); ++i)
9580 {
9581 disr_type = type->field (i).type ();
9582
9583 if (disr_type->code () != TYPE_CODE_STRUCT)
9584 {
9585 /* All fields of a true enum will be structs. */
9586 return;
9587 }
9588 else if (disr_type->num_fields () == 0)
9589 {
9590 /* Could be data-less variant, so keep going. */
9591 disr_type = nullptr;
9592 }
9593 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9594 "RUST$ENUM$DISR") != 0)
9595 {
9596 /* Not a Rust enum. */
9597 return;
9598 }
9599 else
9600 {
9601 /* Found one. */
9602 break;
9603 }
9604 }
9605
9606 /* If we got here without a discriminant, then it's probably
9607 just a union. */
9608 if (disr_type == nullptr)
9609 return;
9610
9611 /* Smash this type to be a structure type. We have to do this
9612 because the type has already been recorded. */
9613 type->set_code (TYPE_CODE_STRUCT);
9614
9615 /* Make space for the discriminant field. */
9616 struct field *disr_field = &disr_type->field (0);
9617 field *new_fields
9618 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9619 * sizeof (struct field)));
9620 memcpy (new_fields + 1, type->fields (),
9621 type->num_fields () * sizeof (struct field));
9622 type->set_fields (new_fields);
9623 type->set_num_fields (type->num_fields () + 1);
9624
9625 /* Install the discriminant at index 0 in the union. */
9626 type->field (0) = *disr_field;
9627 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9628 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9629
9630 /* We need a way to find the correct discriminant given a
9631 variant name. For convenience we build a map here. */
9632 struct type *enum_type = disr_field->type ();
9633 std::unordered_map<std::string, ULONGEST> discriminant_map;
9634 for (int i = 0; i < enum_type->num_fields (); ++i)
9635 {
9636 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9637 {
9638 const char *name
9639 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9640 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9641 }
9642 }
9643
9644 int n_fields = type->num_fields ();
9645 /* We don't need a range entry for the discriminant, but we do
9646 need one for every other field, as there is no default
9647 variant. */
9648 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9649 discriminant_range,
9650 n_fields - 1);
9651 /* Skip the discriminant here. */
9652 for (int i = 1; i < n_fields; ++i)
9653 {
9654 /* Find the final word in the name of this variant's type.
9655 That name can be used to look up the correct
9656 discriminant. */
9657 const char *variant_name
9658 = rust_last_path_segment (type->field (i).type ()->name ());
9659
9660 auto iter = discriminant_map.find (variant_name);
9661 if (iter != discriminant_map.end ())
9662 {
9663 ranges[i - 1].low = iter->second;
9664 ranges[i - 1].high = iter->second;
9665 }
9666
9667 /* In Rust, each element should have the size of the
9668 enclosing enum. */
9669 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9670
9671 /* Remove the discriminant field, if it exists. */
9672 struct type *sub_type = type->field (i).type ();
9673 if (sub_type->num_fields () > 0)
9674 {
9675 sub_type->set_num_fields (sub_type->num_fields () - 1);
9676 sub_type->set_fields (sub_type->fields () + 1);
9677 }
9678 TYPE_FIELD_NAME (type, i) = variant_name;
9679 sub_type->set_name
9680 (rust_fully_qualify (&objfile->objfile_obstack,
9681 type->name (), variant_name));
9682 }
9683
9684 /* Indicate that this is a variant type. */
9685 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9686 gdb::array_view<discriminant_range> (ranges,
9687 n_fields - 1));
9688 }
9689 }
9690
9691 /* Rewrite some Rust unions to be structures with variants parts. */
9692
9693 static void
9694 rust_union_quirks (struct dwarf2_cu *cu)
9695 {
9696 gdb_assert (cu->language == language_rust);
9697 for (type *type_ : cu->rust_unions)
9698 quirk_rust_enum (type_, cu->per_objfile->objfile);
9699 /* We don't need this any more. */
9700 cu->rust_unions.clear ();
9701 }
9702
9703 /* See read.h. */
9704
9705 type_unit_group_unshareable *
9706 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9707 {
9708 auto iter = this->m_type_units.find (tu_group);
9709 if (iter != this->m_type_units.end ())
9710 return iter->second.get ();
9711
9712 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9713 type_unit_group_unshareable *result = uniq.get ();
9714 this->m_type_units[tu_group] = std::move (uniq);
9715 return result;
9716 }
9717
9718 struct type *
9719 dwarf2_per_objfile::get_type_for_signatured_type
9720 (signatured_type *sig_type) const
9721 {
9722 auto iter = this->m_type_map.find (sig_type);
9723 if (iter == this->m_type_map.end ())
9724 return nullptr;
9725
9726 return iter->second;
9727 }
9728
9729 void dwarf2_per_objfile::set_type_for_signatured_type
9730 (signatured_type *sig_type, struct type *type)
9731 {
9732 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9733
9734 this->m_type_map[sig_type] = type;
9735 }
9736
9737 /* A helper function for computing the list of all symbol tables
9738 included by PER_CU. */
9739
9740 static void
9741 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9742 htab_t all_children, htab_t all_type_symtabs,
9743 dwarf2_per_cu_data *per_cu,
9744 dwarf2_per_objfile *per_objfile,
9745 struct compunit_symtab *immediate_parent)
9746 {
9747 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9748 if (*slot != NULL)
9749 {
9750 /* This inclusion and its children have been processed. */
9751 return;
9752 }
9753
9754 *slot = per_cu;
9755
9756 /* Only add a CU if it has a symbol table. */
9757 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9758 if (cust != NULL)
9759 {
9760 /* If this is a type unit only add its symbol table if we haven't
9761 seen it yet (type unit per_cu's can share symtabs). */
9762 if (per_cu->is_debug_types)
9763 {
9764 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9765 if (*slot == NULL)
9766 {
9767 *slot = cust;
9768 result->push_back (cust);
9769 if (cust->user == NULL)
9770 cust->user = immediate_parent;
9771 }
9772 }
9773 else
9774 {
9775 result->push_back (cust);
9776 if (cust->user == NULL)
9777 cust->user = immediate_parent;
9778 }
9779 }
9780
9781 if (!per_cu->imported_symtabs_empty ())
9782 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9783 {
9784 recursively_compute_inclusions (result, all_children,
9785 all_type_symtabs, ptr, per_objfile,
9786 cust);
9787 }
9788 }
9789
9790 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9791 PER_CU. */
9792
9793 static void
9794 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9795 dwarf2_per_objfile *per_objfile)
9796 {
9797 gdb_assert (! per_cu->is_debug_types);
9798
9799 if (!per_cu->imported_symtabs_empty ())
9800 {
9801 int len;
9802 std::vector<compunit_symtab *> result_symtabs;
9803 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9804
9805 /* If we don't have a symtab, we can just skip this case. */
9806 if (cust == NULL)
9807 return;
9808
9809 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9810 htab_eq_pointer,
9811 NULL, xcalloc, xfree));
9812 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9813 htab_eq_pointer,
9814 NULL, xcalloc, xfree));
9815
9816 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9817 {
9818 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9819 all_type_symtabs.get (), ptr,
9820 per_objfile, cust);
9821 }
9822
9823 /* Now we have a transitive closure of all the included symtabs. */
9824 len = result_symtabs.size ();
9825 cust->includes
9826 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9827 struct compunit_symtab *, len + 1);
9828 memcpy (cust->includes, result_symtabs.data (),
9829 len * sizeof (compunit_symtab *));
9830 cust->includes[len] = NULL;
9831 }
9832 }
9833
9834 /* Compute the 'includes' field for the symtabs of all the CUs we just
9835 read. */
9836
9837 static void
9838 process_cu_includes (dwarf2_per_objfile *per_objfile)
9839 {
9840 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
9841 {
9842 if (! iter->is_debug_types)
9843 compute_compunit_symtab_includes (iter, per_objfile);
9844 }
9845
9846 per_objfile->per_bfd->just_read_cus.clear ();
9847 }
9848
9849 /* Generate full symbol information for CU, whose DIEs have
9850 already been loaded into memory. */
9851
9852 static void
9853 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9854 {
9855 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9856 struct objfile *objfile = per_objfile->objfile;
9857 struct gdbarch *gdbarch = objfile->arch ();
9858 CORE_ADDR lowpc, highpc;
9859 struct compunit_symtab *cust;
9860 CORE_ADDR baseaddr;
9861 struct block *static_block;
9862 CORE_ADDR addr;
9863
9864 baseaddr = objfile->text_section_offset ();
9865
9866 /* Clear the list here in case something was left over. */
9867 cu->method_list.clear ();
9868
9869 cu->language = pretend_language;
9870 cu->language_defn = language_def (cu->language);
9871
9872 dwarf2_find_base_address (cu->dies, cu);
9873
9874 /* Do line number decoding in read_file_scope () */
9875 process_die (cu->dies, cu);
9876
9877 /* For now fudge the Go package. */
9878 if (cu->language == language_go)
9879 fixup_go_packaging (cu);
9880
9881 /* Now that we have processed all the DIEs in the CU, all the types
9882 should be complete, and it should now be safe to compute all of the
9883 physnames. */
9884 compute_delayed_physnames (cu);
9885
9886 if (cu->language == language_rust)
9887 rust_union_quirks (cu);
9888
9889 /* Some compilers don't define a DW_AT_high_pc attribute for the
9890 compilation unit. If the DW_AT_high_pc is missing, synthesize
9891 it, by scanning the DIE's below the compilation unit. */
9892 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9893
9894 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9895 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9896
9897 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9898 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9899 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9900 addrmap to help ensure it has an accurate map of pc values belonging to
9901 this comp unit. */
9902 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9903
9904 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9905 SECT_OFF_TEXT (objfile),
9906 0);
9907
9908 if (cust != NULL)
9909 {
9910 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9911
9912 /* Set symtab language to language from DW_AT_language. If the
9913 compilation is from a C file generated by language preprocessors, do
9914 not set the language if it was already deduced by start_subfile. */
9915 if (!(cu->language == language_c
9916 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9917 COMPUNIT_FILETABS (cust)->language = cu->language;
9918
9919 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9920 produce DW_AT_location with location lists but it can be possibly
9921 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9922 there were bugs in prologue debug info, fixed later in GCC-4.5
9923 by "unwind info for epilogues" patch (which is not directly related).
9924
9925 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9926 needed, it would be wrong due to missing DW_AT_producer there.
9927
9928 Still one can confuse GDB by using non-standard GCC compilation
9929 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9930 */
9931 if (cu->has_loclist && gcc_4_minor >= 5)
9932 cust->locations_valid = 1;
9933
9934 if (gcc_4_minor >= 5)
9935 cust->epilogue_unwind_valid = 1;
9936
9937 cust->call_site_htab = cu->call_site_htab;
9938 }
9939
9940 per_objfile->set_symtab (cu->per_cu, cust);
9941
9942 /* Push it for inclusion processing later. */
9943 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
9944
9945 /* Not needed any more. */
9946 cu->reset_builder ();
9947 }
9948
9949 /* Generate full symbol information for type unit CU, whose DIEs have
9950 already been loaded into memory. */
9951
9952 static void
9953 process_full_type_unit (dwarf2_cu *cu,
9954 enum language pretend_language)
9955 {
9956 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9957 struct objfile *objfile = per_objfile->objfile;
9958 struct compunit_symtab *cust;
9959 struct signatured_type *sig_type;
9960
9961 gdb_assert (cu->per_cu->is_debug_types);
9962 sig_type = (struct signatured_type *) cu->per_cu;
9963
9964 /* Clear the list here in case something was left over. */
9965 cu->method_list.clear ();
9966
9967 cu->language = pretend_language;
9968 cu->language_defn = language_def (cu->language);
9969
9970 /* The symbol tables are set up in read_type_unit_scope. */
9971 process_die (cu->dies, cu);
9972
9973 /* For now fudge the Go package. */
9974 if (cu->language == language_go)
9975 fixup_go_packaging (cu);
9976
9977 /* Now that we have processed all the DIEs in the CU, all the types
9978 should be complete, and it should now be safe to compute all of the
9979 physnames. */
9980 compute_delayed_physnames (cu);
9981
9982 if (cu->language == language_rust)
9983 rust_union_quirks (cu);
9984
9985 /* TUs share symbol tables.
9986 If this is the first TU to use this symtab, complete the construction
9987 of it with end_expandable_symtab. Otherwise, complete the addition of
9988 this TU's symbols to the existing symtab. */
9989 type_unit_group_unshareable *tug_unshare =
9990 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
9991 if (tug_unshare->compunit_symtab == NULL)
9992 {
9993 buildsym_compunit *builder = cu->get_builder ();
9994 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9995 tug_unshare->compunit_symtab = cust;
9996
9997 if (cust != NULL)
9998 {
9999 /* Set symtab language to language from DW_AT_language. If the
10000 compilation is from a C file generated by language preprocessors,
10001 do not set the language if it was already deduced by
10002 start_subfile. */
10003 if (!(cu->language == language_c
10004 && COMPUNIT_FILETABS (cust)->language != language_c))
10005 COMPUNIT_FILETABS (cust)->language = cu->language;
10006 }
10007 }
10008 else
10009 {
10010 cu->get_builder ()->augment_type_symtab ();
10011 cust = tug_unshare->compunit_symtab;
10012 }
10013
10014 per_objfile->set_symtab (cu->per_cu, cust);
10015
10016 /* Not needed any more. */
10017 cu->reset_builder ();
10018 }
10019
10020 /* Process an imported unit DIE. */
10021
10022 static void
10023 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10024 {
10025 struct attribute *attr;
10026
10027 /* For now we don't handle imported units in type units. */
10028 if (cu->per_cu->is_debug_types)
10029 {
10030 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10031 " supported in type units [in module %s]"),
10032 objfile_name (cu->per_objfile->objfile));
10033 }
10034
10035 attr = dwarf2_attr (die, DW_AT_import, cu);
10036 if (attr != NULL)
10037 {
10038 sect_offset sect_off = attr->get_ref_die_offset ();
10039 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10040 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10041 dwarf2_per_cu_data *per_cu
10042 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
10043
10044 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10045 into another compilation unit, at root level. Regard this as a hint,
10046 and ignore it. */
10047 if (die->parent && die->parent->parent == NULL
10048 && per_cu->unit_type == DW_UT_compile
10049 && per_cu->lang == language_cplus)
10050 return;
10051
10052 /* If necessary, add it to the queue and load its DIEs. */
10053 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
10054 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
10055 false, cu->language);
10056
10057 cu->per_cu->imported_symtabs_push (per_cu);
10058 }
10059 }
10060
10061 /* RAII object that represents a process_die scope: i.e.,
10062 starts/finishes processing a DIE. */
10063 class process_die_scope
10064 {
10065 public:
10066 process_die_scope (die_info *die, dwarf2_cu *cu)
10067 : m_die (die), m_cu (cu)
10068 {
10069 /* We should only be processing DIEs not already in process. */
10070 gdb_assert (!m_die->in_process);
10071 m_die->in_process = true;
10072 }
10073
10074 ~process_die_scope ()
10075 {
10076 m_die->in_process = false;
10077
10078 /* If we're done processing the DIE for the CU that owns the line
10079 header, we don't need the line header anymore. */
10080 if (m_cu->line_header_die_owner == m_die)
10081 {
10082 delete m_cu->line_header;
10083 m_cu->line_header = NULL;
10084 m_cu->line_header_die_owner = NULL;
10085 }
10086 }
10087
10088 private:
10089 die_info *m_die;
10090 dwarf2_cu *m_cu;
10091 };
10092
10093 /* Process a die and its children. */
10094
10095 static void
10096 process_die (struct die_info *die, struct dwarf2_cu *cu)
10097 {
10098 process_die_scope scope (die, cu);
10099
10100 switch (die->tag)
10101 {
10102 case DW_TAG_padding:
10103 break;
10104 case DW_TAG_compile_unit:
10105 case DW_TAG_partial_unit:
10106 read_file_scope (die, cu);
10107 break;
10108 case DW_TAG_type_unit:
10109 read_type_unit_scope (die, cu);
10110 break;
10111 case DW_TAG_subprogram:
10112 /* Nested subprograms in Fortran get a prefix. */
10113 if (cu->language == language_fortran
10114 && die->parent != NULL
10115 && die->parent->tag == DW_TAG_subprogram)
10116 cu->processing_has_namespace_info = true;
10117 /* Fall through. */
10118 case DW_TAG_inlined_subroutine:
10119 read_func_scope (die, cu);
10120 break;
10121 case DW_TAG_lexical_block:
10122 case DW_TAG_try_block:
10123 case DW_TAG_catch_block:
10124 read_lexical_block_scope (die, cu);
10125 break;
10126 case DW_TAG_call_site:
10127 case DW_TAG_GNU_call_site:
10128 read_call_site_scope (die, cu);
10129 break;
10130 case DW_TAG_class_type:
10131 case DW_TAG_interface_type:
10132 case DW_TAG_structure_type:
10133 case DW_TAG_union_type:
10134 process_structure_scope (die, cu);
10135 break;
10136 case DW_TAG_enumeration_type:
10137 process_enumeration_scope (die, cu);
10138 break;
10139
10140 /* These dies have a type, but processing them does not create
10141 a symbol or recurse to process the children. Therefore we can
10142 read them on-demand through read_type_die. */
10143 case DW_TAG_subroutine_type:
10144 case DW_TAG_set_type:
10145 case DW_TAG_pointer_type:
10146 case DW_TAG_ptr_to_member_type:
10147 case DW_TAG_reference_type:
10148 case DW_TAG_rvalue_reference_type:
10149 case DW_TAG_string_type:
10150 break;
10151
10152 case DW_TAG_array_type:
10153 /* We only need to handle this case for Ada -- in other
10154 languages, it's normal for the compiler to emit a typedef
10155 instead. */
10156 if (cu->language != language_ada)
10157 break;
10158 /* FALLTHROUGH */
10159 case DW_TAG_base_type:
10160 case DW_TAG_subrange_type:
10161 case DW_TAG_typedef:
10162 /* Add a typedef symbol for the type definition, if it has a
10163 DW_AT_name. */
10164 new_symbol (die, read_type_die (die, cu), cu);
10165 break;
10166 case DW_TAG_common_block:
10167 read_common_block (die, cu);
10168 break;
10169 case DW_TAG_common_inclusion:
10170 break;
10171 case DW_TAG_namespace:
10172 cu->processing_has_namespace_info = true;
10173 read_namespace (die, cu);
10174 break;
10175 case DW_TAG_module:
10176 cu->processing_has_namespace_info = true;
10177 read_module (die, cu);
10178 break;
10179 case DW_TAG_imported_declaration:
10180 cu->processing_has_namespace_info = true;
10181 if (read_namespace_alias (die, cu))
10182 break;
10183 /* The declaration is not a global namespace alias. */
10184 /* Fall through. */
10185 case DW_TAG_imported_module:
10186 cu->processing_has_namespace_info = true;
10187 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10188 || cu->language != language_fortran))
10189 complaint (_("Tag '%s' has unexpected children"),
10190 dwarf_tag_name (die->tag));
10191 read_import_statement (die, cu);
10192 break;
10193
10194 case DW_TAG_imported_unit:
10195 process_imported_unit_die (die, cu);
10196 break;
10197
10198 case DW_TAG_variable:
10199 read_variable (die, cu);
10200 break;
10201
10202 default:
10203 new_symbol (die, NULL, cu);
10204 break;
10205 }
10206 }
10207 \f
10208 /* DWARF name computation. */
10209
10210 /* A helper function for dwarf2_compute_name which determines whether DIE
10211 needs to have the name of the scope prepended to the name listed in the
10212 die. */
10213
10214 static int
10215 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10216 {
10217 struct attribute *attr;
10218
10219 switch (die->tag)
10220 {
10221 case DW_TAG_namespace:
10222 case DW_TAG_typedef:
10223 case DW_TAG_class_type:
10224 case DW_TAG_interface_type:
10225 case DW_TAG_structure_type:
10226 case DW_TAG_union_type:
10227 case DW_TAG_enumeration_type:
10228 case DW_TAG_enumerator:
10229 case DW_TAG_subprogram:
10230 case DW_TAG_inlined_subroutine:
10231 case DW_TAG_member:
10232 case DW_TAG_imported_declaration:
10233 return 1;
10234
10235 case DW_TAG_variable:
10236 case DW_TAG_constant:
10237 /* We only need to prefix "globally" visible variables. These include
10238 any variable marked with DW_AT_external or any variable that
10239 lives in a namespace. [Variables in anonymous namespaces
10240 require prefixing, but they are not DW_AT_external.] */
10241
10242 if (dwarf2_attr (die, DW_AT_specification, cu))
10243 {
10244 struct dwarf2_cu *spec_cu = cu;
10245
10246 return die_needs_namespace (die_specification (die, &spec_cu),
10247 spec_cu);
10248 }
10249
10250 attr = dwarf2_attr (die, DW_AT_external, cu);
10251 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10252 && die->parent->tag != DW_TAG_module)
10253 return 0;
10254 /* A variable in a lexical block of some kind does not need a
10255 namespace, even though in C++ such variables may be external
10256 and have a mangled name. */
10257 if (die->parent->tag == DW_TAG_lexical_block
10258 || die->parent->tag == DW_TAG_try_block
10259 || die->parent->tag == DW_TAG_catch_block
10260 || die->parent->tag == DW_TAG_subprogram)
10261 return 0;
10262 return 1;
10263
10264 default:
10265 return 0;
10266 }
10267 }
10268
10269 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10270 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10271 defined for the given DIE. */
10272
10273 static struct attribute *
10274 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10275 {
10276 struct attribute *attr;
10277
10278 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10279 if (attr == NULL)
10280 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10281
10282 return attr;
10283 }
10284
10285 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10286 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10287 defined for the given DIE. */
10288
10289 static const char *
10290 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10291 {
10292 const char *linkage_name;
10293
10294 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10295 if (linkage_name == NULL)
10296 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10297
10298 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10299 See https://github.com/rust-lang/rust/issues/32925. */
10300 if (cu->language == language_rust && linkage_name != NULL
10301 && strchr (linkage_name, '{') != NULL)
10302 linkage_name = NULL;
10303
10304 return linkage_name;
10305 }
10306
10307 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10308 compute the physname for the object, which include a method's:
10309 - formal parameters (C++),
10310 - receiver type (Go),
10311
10312 The term "physname" is a bit confusing.
10313 For C++, for example, it is the demangled name.
10314 For Go, for example, it's the mangled name.
10315
10316 For Ada, return the DIE's linkage name rather than the fully qualified
10317 name. PHYSNAME is ignored..
10318
10319 The result is allocated on the objfile->per_bfd's obstack and
10320 canonicalized. */
10321
10322 static const char *
10323 dwarf2_compute_name (const char *name,
10324 struct die_info *die, struct dwarf2_cu *cu,
10325 int physname)
10326 {
10327 struct objfile *objfile = cu->per_objfile->objfile;
10328
10329 if (name == NULL)
10330 name = dwarf2_name (die, cu);
10331
10332 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10333 but otherwise compute it by typename_concat inside GDB.
10334 FIXME: Actually this is not really true, or at least not always true.
10335 It's all very confusing. compute_and_set_names doesn't try to demangle
10336 Fortran names because there is no mangling standard. So new_symbol
10337 will set the demangled name to the result of dwarf2_full_name, and it is
10338 the demangled name that GDB uses if it exists. */
10339 if (cu->language == language_ada
10340 || (cu->language == language_fortran && physname))
10341 {
10342 /* For Ada unit, we prefer the linkage name over the name, as
10343 the former contains the exported name, which the user expects
10344 to be able to reference. Ideally, we want the user to be able
10345 to reference this entity using either natural or linkage name,
10346 but we haven't started looking at this enhancement yet. */
10347 const char *linkage_name = dw2_linkage_name (die, cu);
10348
10349 if (linkage_name != NULL)
10350 return linkage_name;
10351 }
10352
10353 /* These are the only languages we know how to qualify names in. */
10354 if (name != NULL
10355 && (cu->language == language_cplus
10356 || cu->language == language_fortran || cu->language == language_d
10357 || cu->language == language_rust))
10358 {
10359 if (die_needs_namespace (die, cu))
10360 {
10361 const char *prefix;
10362 const char *canonical_name = NULL;
10363
10364 string_file buf;
10365
10366 prefix = determine_prefix (die, cu);
10367 if (*prefix != '\0')
10368 {
10369 gdb::unique_xmalloc_ptr<char> prefixed_name
10370 (typename_concat (NULL, prefix, name, physname, cu));
10371
10372 buf.puts (prefixed_name.get ());
10373 }
10374 else
10375 buf.puts (name);
10376
10377 /* Template parameters may be specified in the DIE's DW_AT_name, or
10378 as children with DW_TAG_template_type_param or
10379 DW_TAG_value_type_param. If the latter, add them to the name
10380 here. If the name already has template parameters, then
10381 skip this step; some versions of GCC emit both, and
10382 it is more efficient to use the pre-computed name.
10383
10384 Something to keep in mind about this process: it is very
10385 unlikely, or in some cases downright impossible, to produce
10386 something that will match the mangled name of a function.
10387 If the definition of the function has the same debug info,
10388 we should be able to match up with it anyway. But fallbacks
10389 using the minimal symbol, for instance to find a method
10390 implemented in a stripped copy of libstdc++, will not work.
10391 If we do not have debug info for the definition, we will have to
10392 match them up some other way.
10393
10394 When we do name matching there is a related problem with function
10395 templates; two instantiated function templates are allowed to
10396 differ only by their return types, which we do not add here. */
10397
10398 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10399 {
10400 struct attribute *attr;
10401 struct die_info *child;
10402 int first = 1;
10403 const language_defn *cplus_lang = language_def (cu->language);
10404
10405 die->building_fullname = 1;
10406
10407 for (child = die->child; child != NULL; child = child->sibling)
10408 {
10409 struct type *type;
10410 LONGEST value;
10411 const gdb_byte *bytes;
10412 struct dwarf2_locexpr_baton *baton;
10413 struct value *v;
10414
10415 if (child->tag != DW_TAG_template_type_param
10416 && child->tag != DW_TAG_template_value_param)
10417 continue;
10418
10419 if (first)
10420 {
10421 buf.puts ("<");
10422 first = 0;
10423 }
10424 else
10425 buf.puts (", ");
10426
10427 attr = dwarf2_attr (child, DW_AT_type, cu);
10428 if (attr == NULL)
10429 {
10430 complaint (_("template parameter missing DW_AT_type"));
10431 buf.puts ("UNKNOWN_TYPE");
10432 continue;
10433 }
10434 type = die_type (child, cu);
10435
10436 if (child->tag == DW_TAG_template_type_param)
10437 {
10438 cplus_lang->print_type (type, "", &buf, -1, 0,
10439 &type_print_raw_options);
10440 continue;
10441 }
10442
10443 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10444 if (attr == NULL)
10445 {
10446 complaint (_("template parameter missing "
10447 "DW_AT_const_value"));
10448 buf.puts ("UNKNOWN_VALUE");
10449 continue;
10450 }
10451
10452 dwarf2_const_value_attr (attr, type, name,
10453 &cu->comp_unit_obstack, cu,
10454 &value, &bytes, &baton);
10455
10456 if (type->has_no_signedness ())
10457 /* GDB prints characters as NUMBER 'CHAR'. If that's
10458 changed, this can use value_print instead. */
10459 cplus_lang->printchar (value, type, &buf);
10460 else
10461 {
10462 struct value_print_options opts;
10463
10464 if (baton != NULL)
10465 v = dwarf2_evaluate_loc_desc (type, NULL,
10466 baton->data,
10467 baton->size,
10468 baton->per_cu,
10469 baton->per_objfile);
10470 else if (bytes != NULL)
10471 {
10472 v = allocate_value (type);
10473 memcpy (value_contents_writeable (v), bytes,
10474 TYPE_LENGTH (type));
10475 }
10476 else
10477 v = value_from_longest (type, value);
10478
10479 /* Specify decimal so that we do not depend on
10480 the radix. */
10481 get_formatted_print_options (&opts, 'd');
10482 opts.raw = 1;
10483 value_print (v, &buf, &opts);
10484 release_value (v);
10485 }
10486 }
10487
10488 die->building_fullname = 0;
10489
10490 if (!first)
10491 {
10492 /* Close the argument list, with a space if necessary
10493 (nested templates). */
10494 if (!buf.empty () && buf.string ().back () == '>')
10495 buf.puts (" >");
10496 else
10497 buf.puts (">");
10498 }
10499 }
10500
10501 /* For C++ methods, append formal parameter type
10502 information, if PHYSNAME. */
10503
10504 if (physname && die->tag == DW_TAG_subprogram
10505 && cu->language == language_cplus)
10506 {
10507 struct type *type = read_type_die (die, cu);
10508
10509 c_type_print_args (type, &buf, 1, cu->language,
10510 &type_print_raw_options);
10511
10512 if (cu->language == language_cplus)
10513 {
10514 /* Assume that an artificial first parameter is
10515 "this", but do not crash if it is not. RealView
10516 marks unnamed (and thus unused) parameters as
10517 artificial; there is no way to differentiate
10518 the two cases. */
10519 if (type->num_fields () > 0
10520 && TYPE_FIELD_ARTIFICIAL (type, 0)
10521 && type->field (0).type ()->code () == TYPE_CODE_PTR
10522 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
10523 buf.puts (" const");
10524 }
10525 }
10526
10527 const std::string &intermediate_name = buf.string ();
10528
10529 if (cu->language == language_cplus)
10530 canonical_name
10531 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10532 objfile);
10533
10534 /* If we only computed INTERMEDIATE_NAME, or if
10535 INTERMEDIATE_NAME is already canonical, then we need to
10536 intern it. */
10537 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10538 name = objfile->intern (intermediate_name);
10539 else
10540 name = canonical_name;
10541 }
10542 }
10543
10544 return name;
10545 }
10546
10547 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10548 If scope qualifiers are appropriate they will be added. The result
10549 will be allocated on the storage_obstack, or NULL if the DIE does
10550 not have a name. NAME may either be from a previous call to
10551 dwarf2_name or NULL.
10552
10553 The output string will be canonicalized (if C++). */
10554
10555 static const char *
10556 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10557 {
10558 return dwarf2_compute_name (name, die, cu, 0);
10559 }
10560
10561 /* Construct a physname for the given DIE in CU. NAME may either be
10562 from a previous call to dwarf2_name or NULL. The result will be
10563 allocated on the objfile_objstack or NULL if the DIE does not have a
10564 name.
10565
10566 The output string will be canonicalized (if C++). */
10567
10568 static const char *
10569 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10570 {
10571 struct objfile *objfile = cu->per_objfile->objfile;
10572 const char *retval, *mangled = NULL, *canon = NULL;
10573 int need_copy = 1;
10574
10575 /* In this case dwarf2_compute_name is just a shortcut not building anything
10576 on its own. */
10577 if (!die_needs_namespace (die, cu))
10578 return dwarf2_compute_name (name, die, cu, 1);
10579
10580 if (cu->language != language_rust)
10581 mangled = dw2_linkage_name (die, cu);
10582
10583 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10584 has computed. */
10585 gdb::unique_xmalloc_ptr<char> demangled;
10586 if (mangled != NULL)
10587 {
10588
10589 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
10590 {
10591 /* Do nothing (do not demangle the symbol name). */
10592 }
10593 else
10594 {
10595 /* Use DMGL_RET_DROP for C++ template functions to suppress
10596 their return type. It is easier for GDB users to search
10597 for such functions as `name(params)' than `long name(params)'.
10598 In such case the minimal symbol names do not match the full
10599 symbol names but for template functions there is never a need
10600 to look up their definition from their declaration so
10601 the only disadvantage remains the minimal symbol variant
10602 `long name(params)' does not have the proper inferior type. */
10603 demangled.reset (gdb_demangle (mangled,
10604 (DMGL_PARAMS | DMGL_ANSI
10605 | DMGL_RET_DROP)));
10606 }
10607 if (demangled)
10608 canon = demangled.get ();
10609 else
10610 {
10611 canon = mangled;
10612 need_copy = 0;
10613 }
10614 }
10615
10616 if (canon == NULL || check_physname)
10617 {
10618 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10619
10620 if (canon != NULL && strcmp (physname, canon) != 0)
10621 {
10622 /* It may not mean a bug in GDB. The compiler could also
10623 compute DW_AT_linkage_name incorrectly. But in such case
10624 GDB would need to be bug-to-bug compatible. */
10625
10626 complaint (_("Computed physname <%s> does not match demangled <%s> "
10627 "(from linkage <%s>) - DIE at %s [in module %s]"),
10628 physname, canon, mangled, sect_offset_str (die->sect_off),
10629 objfile_name (objfile));
10630
10631 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10632 is available here - over computed PHYSNAME. It is safer
10633 against both buggy GDB and buggy compilers. */
10634
10635 retval = canon;
10636 }
10637 else
10638 {
10639 retval = physname;
10640 need_copy = 0;
10641 }
10642 }
10643 else
10644 retval = canon;
10645
10646 if (need_copy)
10647 retval = objfile->intern (retval);
10648
10649 return retval;
10650 }
10651
10652 /* Inspect DIE in CU for a namespace alias. If one exists, record
10653 a new symbol for it.
10654
10655 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10656
10657 static int
10658 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10659 {
10660 struct attribute *attr;
10661
10662 /* If the die does not have a name, this is not a namespace
10663 alias. */
10664 attr = dwarf2_attr (die, DW_AT_name, cu);
10665 if (attr != NULL)
10666 {
10667 int num;
10668 struct die_info *d = die;
10669 struct dwarf2_cu *imported_cu = cu;
10670
10671 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10672 keep inspecting DIEs until we hit the underlying import. */
10673 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10674 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10675 {
10676 attr = dwarf2_attr (d, DW_AT_import, cu);
10677 if (attr == NULL)
10678 break;
10679
10680 d = follow_die_ref (d, attr, &imported_cu);
10681 if (d->tag != DW_TAG_imported_declaration)
10682 break;
10683 }
10684
10685 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10686 {
10687 complaint (_("DIE at %s has too many recursively imported "
10688 "declarations"), sect_offset_str (d->sect_off));
10689 return 0;
10690 }
10691
10692 if (attr != NULL)
10693 {
10694 struct type *type;
10695 sect_offset sect_off = attr->get_ref_die_offset ();
10696
10697 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10698 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10699 {
10700 /* This declaration is a global namespace alias. Add
10701 a symbol for it whose type is the aliased namespace. */
10702 new_symbol (die, type, cu);
10703 return 1;
10704 }
10705 }
10706 }
10707
10708 return 0;
10709 }
10710
10711 /* Return the using directives repository (global or local?) to use in the
10712 current context for CU.
10713
10714 For Ada, imported declarations can materialize renamings, which *may* be
10715 global. However it is impossible (for now?) in DWARF to distinguish
10716 "external" imported declarations and "static" ones. As all imported
10717 declarations seem to be static in all other languages, make them all CU-wide
10718 global only in Ada. */
10719
10720 static struct using_direct **
10721 using_directives (struct dwarf2_cu *cu)
10722 {
10723 if (cu->language == language_ada
10724 && cu->get_builder ()->outermost_context_p ())
10725 return cu->get_builder ()->get_global_using_directives ();
10726 else
10727 return cu->get_builder ()->get_local_using_directives ();
10728 }
10729
10730 /* Read the import statement specified by the given die and record it. */
10731
10732 static void
10733 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10734 {
10735 struct objfile *objfile = cu->per_objfile->objfile;
10736 struct attribute *import_attr;
10737 struct die_info *imported_die, *child_die;
10738 struct dwarf2_cu *imported_cu;
10739 const char *imported_name;
10740 const char *imported_name_prefix;
10741 const char *canonical_name;
10742 const char *import_alias;
10743 const char *imported_declaration = NULL;
10744 const char *import_prefix;
10745 std::vector<const char *> excludes;
10746
10747 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10748 if (import_attr == NULL)
10749 {
10750 complaint (_("Tag '%s' has no DW_AT_import"),
10751 dwarf_tag_name (die->tag));
10752 return;
10753 }
10754
10755 imported_cu = cu;
10756 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10757 imported_name = dwarf2_name (imported_die, imported_cu);
10758 if (imported_name == NULL)
10759 {
10760 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10761
10762 The import in the following code:
10763 namespace A
10764 {
10765 typedef int B;
10766 }
10767
10768 int main ()
10769 {
10770 using A::B;
10771 B b;
10772 return b;
10773 }
10774
10775 ...
10776 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10777 <52> DW_AT_decl_file : 1
10778 <53> DW_AT_decl_line : 6
10779 <54> DW_AT_import : <0x75>
10780 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10781 <59> DW_AT_name : B
10782 <5b> DW_AT_decl_file : 1
10783 <5c> DW_AT_decl_line : 2
10784 <5d> DW_AT_type : <0x6e>
10785 ...
10786 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10787 <76> DW_AT_byte_size : 4
10788 <77> DW_AT_encoding : 5 (signed)
10789
10790 imports the wrong die ( 0x75 instead of 0x58 ).
10791 This case will be ignored until the gcc bug is fixed. */
10792 return;
10793 }
10794
10795 /* Figure out the local name after import. */
10796 import_alias = dwarf2_name (die, cu);
10797
10798 /* Figure out where the statement is being imported to. */
10799 import_prefix = determine_prefix (die, cu);
10800
10801 /* Figure out what the scope of the imported die is and prepend it
10802 to the name of the imported die. */
10803 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10804
10805 if (imported_die->tag != DW_TAG_namespace
10806 && imported_die->tag != DW_TAG_module)
10807 {
10808 imported_declaration = imported_name;
10809 canonical_name = imported_name_prefix;
10810 }
10811 else if (strlen (imported_name_prefix) > 0)
10812 canonical_name = obconcat (&objfile->objfile_obstack,
10813 imported_name_prefix,
10814 (cu->language == language_d ? "." : "::"),
10815 imported_name, (char *) NULL);
10816 else
10817 canonical_name = imported_name;
10818
10819 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10820 for (child_die = die->child; child_die && child_die->tag;
10821 child_die = child_die->sibling)
10822 {
10823 /* DWARF-4: A Fortran use statement with a “rename list” may be
10824 represented by an imported module entry with an import attribute
10825 referring to the module and owned entries corresponding to those
10826 entities that are renamed as part of being imported. */
10827
10828 if (child_die->tag != DW_TAG_imported_declaration)
10829 {
10830 complaint (_("child DW_TAG_imported_declaration expected "
10831 "- DIE at %s [in module %s]"),
10832 sect_offset_str (child_die->sect_off),
10833 objfile_name (objfile));
10834 continue;
10835 }
10836
10837 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10838 if (import_attr == NULL)
10839 {
10840 complaint (_("Tag '%s' has no DW_AT_import"),
10841 dwarf_tag_name (child_die->tag));
10842 continue;
10843 }
10844
10845 imported_cu = cu;
10846 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10847 &imported_cu);
10848 imported_name = dwarf2_name (imported_die, imported_cu);
10849 if (imported_name == NULL)
10850 {
10851 complaint (_("child DW_TAG_imported_declaration has unknown "
10852 "imported name - DIE at %s [in module %s]"),
10853 sect_offset_str (child_die->sect_off),
10854 objfile_name (objfile));
10855 continue;
10856 }
10857
10858 excludes.push_back (imported_name);
10859
10860 process_die (child_die, cu);
10861 }
10862
10863 add_using_directive (using_directives (cu),
10864 import_prefix,
10865 canonical_name,
10866 import_alias,
10867 imported_declaration,
10868 excludes,
10869 0,
10870 &objfile->objfile_obstack);
10871 }
10872
10873 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10874 types, but gives them a size of zero. Starting with version 14,
10875 ICC is compatible with GCC. */
10876
10877 static bool
10878 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10879 {
10880 if (!cu->checked_producer)
10881 check_producer (cu);
10882
10883 return cu->producer_is_icc_lt_14;
10884 }
10885
10886 /* ICC generates a DW_AT_type for C void functions. This was observed on
10887 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10888 which says that void functions should not have a DW_AT_type. */
10889
10890 static bool
10891 producer_is_icc (struct dwarf2_cu *cu)
10892 {
10893 if (!cu->checked_producer)
10894 check_producer (cu);
10895
10896 return cu->producer_is_icc;
10897 }
10898
10899 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10900 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10901 this, it was first present in GCC release 4.3.0. */
10902
10903 static bool
10904 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10905 {
10906 if (!cu->checked_producer)
10907 check_producer (cu);
10908
10909 return cu->producer_is_gcc_lt_4_3;
10910 }
10911
10912 static file_and_directory
10913 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10914 {
10915 file_and_directory res;
10916
10917 /* Find the filename. Do not use dwarf2_name here, since the filename
10918 is not a source language identifier. */
10919 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10920 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10921
10922 if (res.comp_dir == NULL
10923 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10924 && IS_ABSOLUTE_PATH (res.name))
10925 {
10926 res.comp_dir_storage = ldirname (res.name);
10927 if (!res.comp_dir_storage.empty ())
10928 res.comp_dir = res.comp_dir_storage.c_str ();
10929 }
10930 if (res.comp_dir != NULL)
10931 {
10932 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10933 directory, get rid of it. */
10934 const char *cp = strchr (res.comp_dir, ':');
10935
10936 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10937 res.comp_dir = cp + 1;
10938 }
10939
10940 if (res.name == NULL)
10941 res.name = "<unknown>";
10942
10943 return res;
10944 }
10945
10946 /* Handle DW_AT_stmt_list for a compilation unit.
10947 DIE is the DW_TAG_compile_unit die for CU.
10948 COMP_DIR is the compilation directory. LOWPC is passed to
10949 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10950
10951 static void
10952 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10953 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10954 {
10955 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10956 struct attribute *attr;
10957 struct line_header line_header_local;
10958 hashval_t line_header_local_hash;
10959 void **slot;
10960 int decode_mapping;
10961
10962 gdb_assert (! cu->per_cu->is_debug_types);
10963
10964 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10965 if (attr == NULL || !attr->form_is_unsigned ())
10966 return;
10967
10968 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
10969
10970 /* The line header hash table is only created if needed (it exists to
10971 prevent redundant reading of the line table for partial_units).
10972 If we're given a partial_unit, we'll need it. If we're given a
10973 compile_unit, then use the line header hash table if it's already
10974 created, but don't create one just yet. */
10975
10976 if (per_objfile->line_header_hash == NULL
10977 && die->tag == DW_TAG_partial_unit)
10978 {
10979 per_objfile->line_header_hash
10980 .reset (htab_create_alloc (127, line_header_hash_voidp,
10981 line_header_eq_voidp,
10982 free_line_header_voidp,
10983 xcalloc, xfree));
10984 }
10985
10986 line_header_local.sect_off = line_offset;
10987 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10988 line_header_local_hash = line_header_hash (&line_header_local);
10989 if (per_objfile->line_header_hash != NULL)
10990 {
10991 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10992 &line_header_local,
10993 line_header_local_hash, NO_INSERT);
10994
10995 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10996 is not present in *SLOT (since if there is something in *SLOT then
10997 it will be for a partial_unit). */
10998 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10999 {
11000 gdb_assert (*slot != NULL);
11001 cu->line_header = (struct line_header *) *slot;
11002 return;
11003 }
11004 }
11005
11006 /* dwarf_decode_line_header does not yet provide sufficient information.
11007 We always have to call also dwarf_decode_lines for it. */
11008 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11009 if (lh == NULL)
11010 return;
11011
11012 cu->line_header = lh.release ();
11013 cu->line_header_die_owner = die;
11014
11015 if (per_objfile->line_header_hash == NULL)
11016 slot = NULL;
11017 else
11018 {
11019 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11020 &line_header_local,
11021 line_header_local_hash, INSERT);
11022 gdb_assert (slot != NULL);
11023 }
11024 if (slot != NULL && *slot == NULL)
11025 {
11026 /* This newly decoded line number information unit will be owned
11027 by line_header_hash hash table. */
11028 *slot = cu->line_header;
11029 cu->line_header_die_owner = NULL;
11030 }
11031 else
11032 {
11033 /* We cannot free any current entry in (*slot) as that struct line_header
11034 may be already used by multiple CUs. Create only temporary decoded
11035 line_header for this CU - it may happen at most once for each line
11036 number information unit. And if we're not using line_header_hash
11037 then this is what we want as well. */
11038 gdb_assert (die->tag != DW_TAG_partial_unit);
11039 }
11040 decode_mapping = (die->tag != DW_TAG_partial_unit);
11041 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11042 decode_mapping);
11043
11044 }
11045
11046 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11047
11048 static void
11049 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11050 {
11051 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11052 struct objfile *objfile = per_objfile->objfile;
11053 struct gdbarch *gdbarch = objfile->arch ();
11054 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11055 CORE_ADDR highpc = ((CORE_ADDR) 0);
11056 struct attribute *attr;
11057 struct die_info *child_die;
11058 CORE_ADDR baseaddr;
11059
11060 prepare_one_comp_unit (cu, die, cu->language);
11061 baseaddr = objfile->text_section_offset ();
11062
11063 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11064
11065 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11066 from finish_block. */
11067 if (lowpc == ((CORE_ADDR) -1))
11068 lowpc = highpc;
11069 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11070
11071 file_and_directory fnd = find_file_and_directory (die, cu);
11072
11073 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11074 standardised yet. As a workaround for the language detection we fall
11075 back to the DW_AT_producer string. */
11076 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11077 cu->language = language_opencl;
11078
11079 /* Similar hack for Go. */
11080 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11081 set_cu_language (DW_LANG_Go, cu);
11082
11083 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11084
11085 /* Decode line number information if present. We do this before
11086 processing child DIEs, so that the line header table is available
11087 for DW_AT_decl_file. */
11088 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11089
11090 /* Process all dies in compilation unit. */
11091 if (die->child != NULL)
11092 {
11093 child_die = die->child;
11094 while (child_die && child_die->tag)
11095 {
11096 process_die (child_die, cu);
11097 child_die = child_die->sibling;
11098 }
11099 }
11100
11101 /* Decode macro information, if present. Dwarf 2 macro information
11102 refers to information in the line number info statement program
11103 header, so we can only read it if we've read the header
11104 successfully. */
11105 attr = dwarf2_attr (die, DW_AT_macros, cu);
11106 if (attr == NULL)
11107 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11108 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
11109 {
11110 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11111 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11112
11113 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
11114 }
11115 else
11116 {
11117 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11118 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
11119 {
11120 unsigned int macro_offset = attr->as_unsigned ();
11121
11122 dwarf_decode_macros (cu, macro_offset, 0);
11123 }
11124 }
11125 }
11126
11127 void
11128 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11129 {
11130 struct type_unit_group *tu_group;
11131 int first_time;
11132 struct attribute *attr;
11133 unsigned int i;
11134 struct signatured_type *sig_type;
11135
11136 gdb_assert (per_cu->is_debug_types);
11137 sig_type = (struct signatured_type *) per_cu;
11138
11139 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11140
11141 /* If we're using .gdb_index (includes -readnow) then
11142 per_cu->type_unit_group may not have been set up yet. */
11143 if (sig_type->type_unit_group == NULL)
11144 sig_type->type_unit_group = get_type_unit_group (this, attr);
11145 tu_group = sig_type->type_unit_group;
11146
11147 /* If we've already processed this stmt_list there's no real need to
11148 do it again, we could fake it and just recreate the part we need
11149 (file name,index -> symtab mapping). If data shows this optimization
11150 is useful we can do it then. */
11151 type_unit_group_unshareable *tug_unshare
11152 = per_objfile->get_type_unit_group_unshareable (tu_group);
11153 first_time = tug_unshare->compunit_symtab == NULL;
11154
11155 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11156 debug info. */
11157 line_header_up lh;
11158 if (attr != NULL && attr->form_is_unsigned ())
11159 {
11160 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
11161 lh = dwarf_decode_line_header (line_offset, this);
11162 }
11163 if (lh == NULL)
11164 {
11165 if (first_time)
11166 start_symtab ("", NULL, 0);
11167 else
11168 {
11169 gdb_assert (tug_unshare->symtabs == NULL);
11170 gdb_assert (m_builder == nullptr);
11171 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11172 m_builder.reset (new struct buildsym_compunit
11173 (COMPUNIT_OBJFILE (cust), "",
11174 COMPUNIT_DIRNAME (cust),
11175 compunit_language (cust),
11176 0, cust));
11177 list_in_scope = get_builder ()->get_file_symbols ();
11178 }
11179 return;
11180 }
11181
11182 line_header = lh.release ();
11183 line_header_die_owner = die;
11184
11185 if (first_time)
11186 {
11187 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11188
11189 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11190 still initializing it, and our caller (a few levels up)
11191 process_full_type_unit still needs to know if this is the first
11192 time. */
11193
11194 tug_unshare->symtabs
11195 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11196 struct symtab *, line_header->file_names_size ());
11197
11198 auto &file_names = line_header->file_names ();
11199 for (i = 0; i < file_names.size (); ++i)
11200 {
11201 file_entry &fe = file_names[i];
11202 dwarf2_start_subfile (this, fe.name,
11203 fe.include_dir (line_header));
11204 buildsym_compunit *b = get_builder ();
11205 if (b->get_current_subfile ()->symtab == NULL)
11206 {
11207 /* NOTE: start_subfile will recognize when it's been
11208 passed a file it has already seen. So we can't
11209 assume there's a simple mapping from
11210 cu->line_header->file_names to subfiles, plus
11211 cu->line_header->file_names may contain dups. */
11212 b->get_current_subfile ()->symtab
11213 = allocate_symtab (cust, b->get_current_subfile ()->name);
11214 }
11215
11216 fe.symtab = b->get_current_subfile ()->symtab;
11217 tug_unshare->symtabs[i] = fe.symtab;
11218 }
11219 }
11220 else
11221 {
11222 gdb_assert (m_builder == nullptr);
11223 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11224 m_builder.reset (new struct buildsym_compunit
11225 (COMPUNIT_OBJFILE (cust), "",
11226 COMPUNIT_DIRNAME (cust),
11227 compunit_language (cust),
11228 0, cust));
11229 list_in_scope = get_builder ()->get_file_symbols ();
11230
11231 auto &file_names = line_header->file_names ();
11232 for (i = 0; i < file_names.size (); ++i)
11233 {
11234 file_entry &fe = file_names[i];
11235 fe.symtab = tug_unshare->symtabs[i];
11236 }
11237 }
11238
11239 /* The main symtab is allocated last. Type units don't have DW_AT_name
11240 so they don't have a "real" (so to speak) symtab anyway.
11241 There is later code that will assign the main symtab to all symbols
11242 that don't have one. We need to handle the case of a symbol with a
11243 missing symtab (DW_AT_decl_file) anyway. */
11244 }
11245
11246 /* Process DW_TAG_type_unit.
11247 For TUs we want to skip the first top level sibling if it's not the
11248 actual type being defined by this TU. In this case the first top
11249 level sibling is there to provide context only. */
11250
11251 static void
11252 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11253 {
11254 struct die_info *child_die;
11255
11256 prepare_one_comp_unit (cu, die, language_minimal);
11257
11258 /* Initialize (or reinitialize) the machinery for building symtabs.
11259 We do this before processing child DIEs, so that the line header table
11260 is available for DW_AT_decl_file. */
11261 cu->setup_type_unit_groups (die);
11262
11263 if (die->child != NULL)
11264 {
11265 child_die = die->child;
11266 while (child_die && child_die->tag)
11267 {
11268 process_die (child_die, cu);
11269 child_die = child_die->sibling;
11270 }
11271 }
11272 }
11273 \f
11274 /* DWO/DWP files.
11275
11276 http://gcc.gnu.org/wiki/DebugFission
11277 http://gcc.gnu.org/wiki/DebugFissionDWP
11278
11279 To simplify handling of both DWO files ("object" files with the DWARF info)
11280 and DWP files (a file with the DWOs packaged up into one file), we treat
11281 DWP files as having a collection of virtual DWO files. */
11282
11283 static hashval_t
11284 hash_dwo_file (const void *item)
11285 {
11286 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11287 hashval_t hash;
11288
11289 hash = htab_hash_string (dwo_file->dwo_name);
11290 if (dwo_file->comp_dir != NULL)
11291 hash += htab_hash_string (dwo_file->comp_dir);
11292 return hash;
11293 }
11294
11295 static int
11296 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11297 {
11298 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11299 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11300
11301 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11302 return 0;
11303 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11304 return lhs->comp_dir == rhs->comp_dir;
11305 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11306 }
11307
11308 /* Allocate a hash table for DWO files. */
11309
11310 static htab_up
11311 allocate_dwo_file_hash_table ()
11312 {
11313 auto delete_dwo_file = [] (void *item)
11314 {
11315 struct dwo_file *dwo_file = (struct dwo_file *) item;
11316
11317 delete dwo_file;
11318 };
11319
11320 return htab_up (htab_create_alloc (41,
11321 hash_dwo_file,
11322 eq_dwo_file,
11323 delete_dwo_file,
11324 xcalloc, xfree));
11325 }
11326
11327 /* Lookup DWO file DWO_NAME. */
11328
11329 static void **
11330 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
11331 const char *dwo_name,
11332 const char *comp_dir)
11333 {
11334 struct dwo_file find_entry;
11335 void **slot;
11336
11337 if (per_objfile->per_bfd->dwo_files == NULL)
11338 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11339
11340 find_entry.dwo_name = dwo_name;
11341 find_entry.comp_dir = comp_dir;
11342 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
11343 INSERT);
11344
11345 return slot;
11346 }
11347
11348 static hashval_t
11349 hash_dwo_unit (const void *item)
11350 {
11351 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11352
11353 /* This drops the top 32 bits of the id, but is ok for a hash. */
11354 return dwo_unit->signature;
11355 }
11356
11357 static int
11358 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11359 {
11360 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11361 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11362
11363 /* The signature is assumed to be unique within the DWO file.
11364 So while object file CU dwo_id's always have the value zero,
11365 that's OK, assuming each object file DWO file has only one CU,
11366 and that's the rule for now. */
11367 return lhs->signature == rhs->signature;
11368 }
11369
11370 /* Allocate a hash table for DWO CUs,TUs.
11371 There is one of these tables for each of CUs,TUs for each DWO file. */
11372
11373 static htab_up
11374 allocate_dwo_unit_table ()
11375 {
11376 /* Start out with a pretty small number.
11377 Generally DWO files contain only one CU and maybe some TUs. */
11378 return htab_up (htab_create_alloc (3,
11379 hash_dwo_unit,
11380 eq_dwo_unit,
11381 NULL, xcalloc, xfree));
11382 }
11383
11384 /* die_reader_func for create_dwo_cu. */
11385
11386 static void
11387 create_dwo_cu_reader (const struct die_reader_specs *reader,
11388 const gdb_byte *info_ptr,
11389 struct die_info *comp_unit_die,
11390 struct dwo_file *dwo_file,
11391 struct dwo_unit *dwo_unit)
11392 {
11393 struct dwarf2_cu *cu = reader->cu;
11394 sect_offset sect_off = cu->per_cu->sect_off;
11395 struct dwarf2_section_info *section = cu->per_cu->section;
11396
11397 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11398 if (!signature.has_value ())
11399 {
11400 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11401 " its dwo_id [in module %s]"),
11402 sect_offset_str (sect_off), dwo_file->dwo_name);
11403 return;
11404 }
11405
11406 dwo_unit->dwo_file = dwo_file;
11407 dwo_unit->signature = *signature;
11408 dwo_unit->section = section;
11409 dwo_unit->sect_off = sect_off;
11410 dwo_unit->length = cu->per_cu->length;
11411
11412 dwarf_read_debug_printf (" offset %s, dwo_id %s",
11413 sect_offset_str (sect_off),
11414 hex_string (dwo_unit->signature));
11415 }
11416
11417 /* Create the dwo_units for the CUs in a DWO_FILE.
11418 Note: This function processes DWO files only, not DWP files. */
11419
11420 static void
11421 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
11422 dwarf2_cu *cu, struct dwo_file &dwo_file,
11423 dwarf2_section_info &section, htab_up &cus_htab)
11424 {
11425 struct objfile *objfile = per_objfile->objfile;
11426 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
11427 const gdb_byte *info_ptr, *end_ptr;
11428
11429 section.read (objfile);
11430 info_ptr = section.buffer;
11431
11432 if (info_ptr == NULL)
11433 return;
11434
11435 dwarf_read_debug_printf ("Reading %s for %s:",
11436 section.get_name (),
11437 section.get_file_name ());
11438
11439 end_ptr = info_ptr + section.size;
11440 while (info_ptr < end_ptr)
11441 {
11442 struct dwarf2_per_cu_data per_cu;
11443 struct dwo_unit read_unit {};
11444 struct dwo_unit *dwo_unit;
11445 void **slot;
11446 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11447
11448 memset (&per_cu, 0, sizeof (per_cu));
11449 per_cu.per_bfd = per_bfd;
11450 per_cu.is_debug_types = 0;
11451 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11452 per_cu.section = &section;
11453
11454 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
11455 if (!reader.dummy_p)
11456 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11457 &dwo_file, &read_unit);
11458 info_ptr += per_cu.length;
11459
11460 // If the unit could not be parsed, skip it.
11461 if (read_unit.dwo_file == NULL)
11462 continue;
11463
11464 if (cus_htab == NULL)
11465 cus_htab = allocate_dwo_unit_table ();
11466
11467 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11468 struct dwo_unit);
11469 *dwo_unit = read_unit;
11470 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11471 gdb_assert (slot != NULL);
11472 if (*slot != NULL)
11473 {
11474 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11475 sect_offset dup_sect_off = dup_cu->sect_off;
11476
11477 complaint (_("debug cu entry at offset %s is duplicate to"
11478 " the entry at offset %s, signature %s"),
11479 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11480 hex_string (dwo_unit->signature));
11481 }
11482 *slot = (void *)dwo_unit;
11483 }
11484 }
11485
11486 /* DWP file .debug_{cu,tu}_index section format:
11487 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11488 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11489
11490 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11491 officially standard DWP format was published with DWARF v5 and is called
11492 Version 5. There are no versions 3 or 4.
11493
11494 DWP Version 1:
11495
11496 Both index sections have the same format, and serve to map a 64-bit
11497 signature to a set of section numbers. Each section begins with a header,
11498 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11499 indexes, and a pool of 32-bit section numbers. The index sections will be
11500 aligned at 8-byte boundaries in the file.
11501
11502 The index section header consists of:
11503
11504 V, 32 bit version number
11505 -, 32 bits unused
11506 N, 32 bit number of compilation units or type units in the index
11507 M, 32 bit number of slots in the hash table
11508
11509 Numbers are recorded using the byte order of the application binary.
11510
11511 The hash table begins at offset 16 in the section, and consists of an array
11512 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11513 order of the application binary). Unused slots in the hash table are 0.
11514 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11515
11516 The parallel table begins immediately after the hash table
11517 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11518 array of 32-bit indexes (using the byte order of the application binary),
11519 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11520 table contains a 32-bit index into the pool of section numbers. For unused
11521 hash table slots, the corresponding entry in the parallel table will be 0.
11522
11523 The pool of section numbers begins immediately following the hash table
11524 (at offset 16 + 12 * M from the beginning of the section). The pool of
11525 section numbers consists of an array of 32-bit words (using the byte order
11526 of the application binary). Each item in the array is indexed starting
11527 from 0. The hash table entry provides the index of the first section
11528 number in the set. Additional section numbers in the set follow, and the
11529 set is terminated by a 0 entry (section number 0 is not used in ELF).
11530
11531 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11532 section must be the first entry in the set, and the .debug_abbrev.dwo must
11533 be the second entry. Other members of the set may follow in any order.
11534
11535 ---
11536
11537 DWP Versions 2 and 5:
11538
11539 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
11540 and the entries in the index tables are now offsets into these sections.
11541 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11542 section.
11543
11544 Index Section Contents:
11545 Header
11546 Hash Table of Signatures dwp_hash_table.hash_table
11547 Parallel Table of Indices dwp_hash_table.unit_table
11548 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11549 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
11550
11551 The index section header consists of:
11552
11553 V, 32 bit version number
11554 L, 32 bit number of columns in the table of section offsets
11555 N, 32 bit number of compilation units or type units in the index
11556 M, 32 bit number of slots in the hash table
11557
11558 Numbers are recorded using the byte order of the application binary.
11559
11560 The hash table has the same format as version 1.
11561 The parallel table of indices has the same format as version 1,
11562 except that the entries are origin-1 indices into the table of sections
11563 offsets and the table of section sizes.
11564
11565 The table of offsets begins immediately following the parallel table
11566 (at offset 16 + 12 * M from the beginning of the section). The table is
11567 a two-dimensional array of 32-bit words (using the byte order of the
11568 application binary), with L columns and N+1 rows, in row-major order.
11569 Each row in the array is indexed starting from 0. The first row provides
11570 a key to the remaining rows: each column in this row provides an identifier
11571 for a debug section, and the offsets in the same column of subsequent rows
11572 refer to that section. The section identifiers for Version 2 are:
11573
11574 DW_SECT_INFO 1 .debug_info.dwo
11575 DW_SECT_TYPES 2 .debug_types.dwo
11576 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11577 DW_SECT_LINE 4 .debug_line.dwo
11578 DW_SECT_LOC 5 .debug_loc.dwo
11579 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11580 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11581 DW_SECT_MACRO 8 .debug_macro.dwo
11582
11583 The section identifiers for Version 5 are:
11584
11585 DW_SECT_INFO_V5 1 .debug_info.dwo
11586 DW_SECT_RESERVED_V5 2 --
11587 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11588 DW_SECT_LINE_V5 4 .debug_line.dwo
11589 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11590 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11591 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11592 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11593
11594 The offsets provided by the CU and TU index sections are the base offsets
11595 for the contributions made by each CU or TU to the corresponding section
11596 in the package file. Each CU and TU header contains an abbrev_offset
11597 field, used to find the abbreviations table for that CU or TU within the
11598 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11599 be interpreted as relative to the base offset given in the index section.
11600 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11601 should be interpreted as relative to the base offset for .debug_line.dwo,
11602 and offsets into other debug sections obtained from DWARF attributes should
11603 also be interpreted as relative to the corresponding base offset.
11604
11605 The table of sizes begins immediately following the table of offsets.
11606 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11607 with L columns and N rows, in row-major order. Each row in the array is
11608 indexed starting from 1 (row 0 is shared by the two tables).
11609
11610 ---
11611
11612 Hash table lookup is handled the same in version 1 and 2:
11613
11614 We assume that N and M will not exceed 2^32 - 1.
11615 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11616
11617 Given a 64-bit compilation unit signature or a type signature S, an entry
11618 in the hash table is located as follows:
11619
11620 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11621 the low-order k bits all set to 1.
11622
11623 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11624
11625 3) If the hash table entry at index H matches the signature, use that
11626 entry. If the hash table entry at index H is unused (all zeroes),
11627 terminate the search: the signature is not present in the table.
11628
11629 4) Let H = (H + H') modulo M. Repeat at Step 3.
11630
11631 Because M > N and H' and M are relatively prime, the search is guaranteed
11632 to stop at an unused slot or find the match. */
11633
11634 /* Create a hash table to map DWO IDs to their CU/TU entry in
11635 .debug_{info,types}.dwo in DWP_FILE.
11636 Returns NULL if there isn't one.
11637 Note: This function processes DWP files only, not DWO files. */
11638
11639 static struct dwp_hash_table *
11640 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11641 struct dwp_file *dwp_file, int is_debug_types)
11642 {
11643 struct objfile *objfile = per_objfile->objfile;
11644 bfd *dbfd = dwp_file->dbfd.get ();
11645 const gdb_byte *index_ptr, *index_end;
11646 struct dwarf2_section_info *index;
11647 uint32_t version, nr_columns, nr_units, nr_slots;
11648 struct dwp_hash_table *htab;
11649
11650 if (is_debug_types)
11651 index = &dwp_file->sections.tu_index;
11652 else
11653 index = &dwp_file->sections.cu_index;
11654
11655 if (index->empty ())
11656 return NULL;
11657 index->read (objfile);
11658
11659 index_ptr = index->buffer;
11660 index_end = index_ptr + index->size;
11661
11662 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11663 For now it's safe to just read 4 bytes (particularly as it's difficult to
11664 tell if you're dealing with Version 5 before you've read the version). */
11665 version = read_4_bytes (dbfd, index_ptr);
11666 index_ptr += 4;
11667 if (version == 2 || version == 5)
11668 nr_columns = read_4_bytes (dbfd, index_ptr);
11669 else
11670 nr_columns = 0;
11671 index_ptr += 4;
11672 nr_units = read_4_bytes (dbfd, index_ptr);
11673 index_ptr += 4;
11674 nr_slots = read_4_bytes (dbfd, index_ptr);
11675 index_ptr += 4;
11676
11677 if (version != 1 && version != 2 && version != 5)
11678 {
11679 error (_("Dwarf Error: unsupported DWP file version (%s)"
11680 " [in module %s]"),
11681 pulongest (version), dwp_file->name);
11682 }
11683 if (nr_slots != (nr_slots & -nr_slots))
11684 {
11685 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11686 " is not power of 2 [in module %s]"),
11687 pulongest (nr_slots), dwp_file->name);
11688 }
11689
11690 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11691 htab->version = version;
11692 htab->nr_columns = nr_columns;
11693 htab->nr_units = nr_units;
11694 htab->nr_slots = nr_slots;
11695 htab->hash_table = index_ptr;
11696 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11697
11698 /* Exit early if the table is empty. */
11699 if (nr_slots == 0 || nr_units == 0
11700 || (version == 2 && nr_columns == 0)
11701 || (version == 5 && nr_columns == 0))
11702 {
11703 /* All must be zero. */
11704 if (nr_slots != 0 || nr_units != 0
11705 || (version == 2 && nr_columns != 0)
11706 || (version == 5 && nr_columns != 0))
11707 {
11708 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11709 " all zero [in modules %s]"),
11710 dwp_file->name);
11711 }
11712 return htab;
11713 }
11714
11715 if (version == 1)
11716 {
11717 htab->section_pool.v1.indices =
11718 htab->unit_table + sizeof (uint32_t) * nr_slots;
11719 /* It's harder to decide whether the section is too small in v1.
11720 V1 is deprecated anyway so we punt. */
11721 }
11722 else if (version == 2)
11723 {
11724 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11725 int *ids = htab->section_pool.v2.section_ids;
11726 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11727 /* Reverse map for error checking. */
11728 int ids_seen[DW_SECT_MAX + 1];
11729 int i;
11730
11731 if (nr_columns < 2)
11732 {
11733 error (_("Dwarf Error: bad DWP hash table, too few columns"
11734 " in section table [in module %s]"),
11735 dwp_file->name);
11736 }
11737 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11738 {
11739 error (_("Dwarf Error: bad DWP hash table, too many columns"
11740 " in section table [in module %s]"),
11741 dwp_file->name);
11742 }
11743 memset (ids, 255, sizeof_ids);
11744 memset (ids_seen, 255, sizeof (ids_seen));
11745 for (i = 0; i < nr_columns; ++i)
11746 {
11747 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11748
11749 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11750 {
11751 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11752 " in section table [in module %s]"),
11753 id, dwp_file->name);
11754 }
11755 if (ids_seen[id] != -1)
11756 {
11757 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11758 " id %d in section table [in module %s]"),
11759 id, dwp_file->name);
11760 }
11761 ids_seen[id] = i;
11762 ids[i] = id;
11763 }
11764 /* Must have exactly one info or types section. */
11765 if (((ids_seen[DW_SECT_INFO] != -1)
11766 + (ids_seen[DW_SECT_TYPES] != -1))
11767 != 1)
11768 {
11769 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11770 " DWO info/types section [in module %s]"),
11771 dwp_file->name);
11772 }
11773 /* Must have an abbrev section. */
11774 if (ids_seen[DW_SECT_ABBREV] == -1)
11775 {
11776 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11777 " section [in module %s]"),
11778 dwp_file->name);
11779 }
11780 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11781 htab->section_pool.v2.sizes =
11782 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11783 * nr_units * nr_columns);
11784 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11785 * nr_units * nr_columns))
11786 > index_end)
11787 {
11788 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11789 " [in module %s]"),
11790 dwp_file->name);
11791 }
11792 }
11793 else /* version == 5 */
11794 {
11795 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11796 int *ids = htab->section_pool.v5.section_ids;
11797 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11798 /* Reverse map for error checking. */
11799 int ids_seen[DW_SECT_MAX_V5 + 1];
11800
11801 if (nr_columns < 2)
11802 {
11803 error (_("Dwarf Error: bad DWP hash table, too few columns"
11804 " in section table [in module %s]"),
11805 dwp_file->name);
11806 }
11807 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11808 {
11809 error (_("Dwarf Error: bad DWP hash table, too many columns"
11810 " in section table [in module %s]"),
11811 dwp_file->name);
11812 }
11813 memset (ids, 255, sizeof_ids);
11814 memset (ids_seen, 255, sizeof (ids_seen));
11815 for (int i = 0; i < nr_columns; ++i)
11816 {
11817 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11818
11819 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11820 {
11821 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11822 " in section table [in module %s]"),
11823 id, dwp_file->name);
11824 }
11825 if (ids_seen[id] != -1)
11826 {
11827 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11828 " id %d in section table [in module %s]"),
11829 id, dwp_file->name);
11830 }
11831 ids_seen[id] = i;
11832 ids[i] = id;
11833 }
11834 /* Must have seen an info section. */
11835 if (ids_seen[DW_SECT_INFO_V5] == -1)
11836 {
11837 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11838 " DWO info/types section [in module %s]"),
11839 dwp_file->name);
11840 }
11841 /* Must have an abbrev section. */
11842 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11843 {
11844 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11845 " section [in module %s]"),
11846 dwp_file->name);
11847 }
11848 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11849 htab->section_pool.v5.sizes
11850 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11851 * nr_units * nr_columns);
11852 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11853 * nr_units * nr_columns))
11854 > index_end)
11855 {
11856 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11857 " [in module %s]"),
11858 dwp_file->name);
11859 }
11860 }
11861
11862 return htab;
11863 }
11864
11865 /* Update SECTIONS with the data from SECTP.
11866
11867 This function is like the other "locate" section routines, but in
11868 this context the sections to read comes from the DWP V1 hash table,
11869 not the full ELF section table.
11870
11871 The result is non-zero for success, or zero if an error was found. */
11872
11873 static int
11874 locate_v1_virtual_dwo_sections (asection *sectp,
11875 struct virtual_v1_dwo_sections *sections)
11876 {
11877 const struct dwop_section_names *names = &dwop_section_names;
11878
11879 if (names->abbrev_dwo.matches (sectp->name))
11880 {
11881 /* There can be only one. */
11882 if (sections->abbrev.s.section != NULL)
11883 return 0;
11884 sections->abbrev.s.section = sectp;
11885 sections->abbrev.size = bfd_section_size (sectp);
11886 }
11887 else if (names->info_dwo.matches (sectp->name)
11888 || names->types_dwo.matches (sectp->name))
11889 {
11890 /* There can be only one. */
11891 if (sections->info_or_types.s.section != NULL)
11892 return 0;
11893 sections->info_or_types.s.section = sectp;
11894 sections->info_or_types.size = bfd_section_size (sectp);
11895 }
11896 else if (names->line_dwo.matches (sectp->name))
11897 {
11898 /* There can be only one. */
11899 if (sections->line.s.section != NULL)
11900 return 0;
11901 sections->line.s.section = sectp;
11902 sections->line.size = bfd_section_size (sectp);
11903 }
11904 else if (names->loc_dwo.matches (sectp->name))
11905 {
11906 /* There can be only one. */
11907 if (sections->loc.s.section != NULL)
11908 return 0;
11909 sections->loc.s.section = sectp;
11910 sections->loc.size = bfd_section_size (sectp);
11911 }
11912 else if (names->macinfo_dwo.matches (sectp->name))
11913 {
11914 /* There can be only one. */
11915 if (sections->macinfo.s.section != NULL)
11916 return 0;
11917 sections->macinfo.s.section = sectp;
11918 sections->macinfo.size = bfd_section_size (sectp);
11919 }
11920 else if (names->macro_dwo.matches (sectp->name))
11921 {
11922 /* There can be only one. */
11923 if (sections->macro.s.section != NULL)
11924 return 0;
11925 sections->macro.s.section = sectp;
11926 sections->macro.size = bfd_section_size (sectp);
11927 }
11928 else if (names->str_offsets_dwo.matches (sectp->name))
11929 {
11930 /* There can be only one. */
11931 if (sections->str_offsets.s.section != NULL)
11932 return 0;
11933 sections->str_offsets.s.section = sectp;
11934 sections->str_offsets.size = bfd_section_size (sectp);
11935 }
11936 else
11937 {
11938 /* No other kind of section is valid. */
11939 return 0;
11940 }
11941
11942 return 1;
11943 }
11944
11945 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11946 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11947 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11948 This is for DWP version 1 files. */
11949
11950 static struct dwo_unit *
11951 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
11952 struct dwp_file *dwp_file,
11953 uint32_t unit_index,
11954 const char *comp_dir,
11955 ULONGEST signature, int is_debug_types)
11956 {
11957 const struct dwp_hash_table *dwp_htab =
11958 is_debug_types ? dwp_file->tus : dwp_file->cus;
11959 bfd *dbfd = dwp_file->dbfd.get ();
11960 const char *kind = is_debug_types ? "TU" : "CU";
11961 struct dwo_file *dwo_file;
11962 struct dwo_unit *dwo_unit;
11963 struct virtual_v1_dwo_sections sections;
11964 void **dwo_file_slot;
11965 int i;
11966
11967 gdb_assert (dwp_file->version == 1);
11968
11969 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
11970 kind, pulongest (unit_index), hex_string (signature),
11971 dwp_file->name);
11972
11973 /* Fetch the sections of this DWO unit.
11974 Put a limit on the number of sections we look for so that bad data
11975 doesn't cause us to loop forever. */
11976
11977 #define MAX_NR_V1_DWO_SECTIONS \
11978 (1 /* .debug_info or .debug_types */ \
11979 + 1 /* .debug_abbrev */ \
11980 + 1 /* .debug_line */ \
11981 + 1 /* .debug_loc */ \
11982 + 1 /* .debug_str_offsets */ \
11983 + 1 /* .debug_macro or .debug_macinfo */ \
11984 + 1 /* trailing zero */)
11985
11986 memset (&sections, 0, sizeof (sections));
11987
11988 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11989 {
11990 asection *sectp;
11991 uint32_t section_nr =
11992 read_4_bytes (dbfd,
11993 dwp_htab->section_pool.v1.indices
11994 + (unit_index + i) * sizeof (uint32_t));
11995
11996 if (section_nr == 0)
11997 break;
11998 if (section_nr >= dwp_file->num_sections)
11999 {
12000 error (_("Dwarf Error: bad DWP hash table, section number too large"
12001 " [in module %s]"),
12002 dwp_file->name);
12003 }
12004
12005 sectp = dwp_file->elf_sections[section_nr];
12006 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12007 {
12008 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12009 " [in module %s]"),
12010 dwp_file->name);
12011 }
12012 }
12013
12014 if (i < 2
12015 || sections.info_or_types.empty ()
12016 || sections.abbrev.empty ())
12017 {
12018 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12019 " [in module %s]"),
12020 dwp_file->name);
12021 }
12022 if (i == MAX_NR_V1_DWO_SECTIONS)
12023 {
12024 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12025 " [in module %s]"),
12026 dwp_file->name);
12027 }
12028
12029 /* It's easier for the rest of the code if we fake a struct dwo_file and
12030 have dwo_unit "live" in that. At least for now.
12031
12032 The DWP file can be made up of a random collection of CUs and TUs.
12033 However, for each CU + set of TUs that came from the same original DWO
12034 file, we can combine them back into a virtual DWO file to save space
12035 (fewer struct dwo_file objects to allocate). Remember that for really
12036 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12037
12038 std::string virtual_dwo_name =
12039 string_printf ("virtual-dwo/%d-%d-%d-%d",
12040 sections.abbrev.get_id (),
12041 sections.line.get_id (),
12042 sections.loc.get_id (),
12043 sections.str_offsets.get_id ());
12044 /* Can we use an existing virtual DWO file? */
12045 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12046 comp_dir);
12047 /* Create one if necessary. */
12048 if (*dwo_file_slot == NULL)
12049 {
12050 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12051 virtual_dwo_name.c_str ());
12052
12053 dwo_file = new struct dwo_file;
12054 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12055 dwo_file->comp_dir = comp_dir;
12056 dwo_file->sections.abbrev = sections.abbrev;
12057 dwo_file->sections.line = sections.line;
12058 dwo_file->sections.loc = sections.loc;
12059 dwo_file->sections.macinfo = sections.macinfo;
12060 dwo_file->sections.macro = sections.macro;
12061 dwo_file->sections.str_offsets = sections.str_offsets;
12062 /* The "str" section is global to the entire DWP file. */
12063 dwo_file->sections.str = dwp_file->sections.str;
12064 /* The info or types section is assigned below to dwo_unit,
12065 there's no need to record it in dwo_file.
12066 Also, we can't simply record type sections in dwo_file because
12067 we record a pointer into the vector in dwo_unit. As we collect more
12068 types we'll grow the vector and eventually have to reallocate space
12069 for it, invalidating all copies of pointers into the previous
12070 contents. */
12071 *dwo_file_slot = dwo_file;
12072 }
12073 else
12074 {
12075 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12076 virtual_dwo_name.c_str ());
12077
12078 dwo_file = (struct dwo_file *) *dwo_file_slot;
12079 }
12080
12081 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12082 dwo_unit->dwo_file = dwo_file;
12083 dwo_unit->signature = signature;
12084 dwo_unit->section =
12085 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12086 *dwo_unit->section = sections.info_or_types;
12087 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12088
12089 return dwo_unit;
12090 }
12091
12092 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
12093 simplify them. Given a pointer to the containing section SECTION, and
12094 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
12095 virtual section of just that piece. */
12096
12097 static struct dwarf2_section_info
12098 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
12099 struct dwarf2_section_info *section,
12100 bfd_size_type offset, bfd_size_type size)
12101 {
12102 struct dwarf2_section_info result;
12103 asection *sectp;
12104
12105 gdb_assert (section != NULL);
12106 gdb_assert (!section->is_virtual);
12107
12108 memset (&result, 0, sizeof (result));
12109 result.s.containing_section = section;
12110 result.is_virtual = true;
12111
12112 if (size == 0)
12113 return result;
12114
12115 sectp = section->get_bfd_section ();
12116
12117 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12118 bounds of the real section. This is a pretty-rare event, so just
12119 flag an error (easier) instead of a warning and trying to cope. */
12120 if (sectp == NULL
12121 || offset + size > bfd_section_size (sectp))
12122 {
12123 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
12124 " in section %s [in module %s]"),
12125 sectp ? bfd_section_name (sectp) : "<unknown>",
12126 objfile_name (per_objfile->objfile));
12127 }
12128
12129 result.virtual_offset = offset;
12130 result.size = size;
12131 return result;
12132 }
12133
12134 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12135 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12136 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12137 This is for DWP version 2 files. */
12138
12139 static struct dwo_unit *
12140 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
12141 struct dwp_file *dwp_file,
12142 uint32_t unit_index,
12143 const char *comp_dir,
12144 ULONGEST signature, int is_debug_types)
12145 {
12146 const struct dwp_hash_table *dwp_htab =
12147 is_debug_types ? dwp_file->tus : dwp_file->cus;
12148 bfd *dbfd = dwp_file->dbfd.get ();
12149 const char *kind = is_debug_types ? "TU" : "CU";
12150 struct dwo_file *dwo_file;
12151 struct dwo_unit *dwo_unit;
12152 struct virtual_v2_or_v5_dwo_sections sections;
12153 void **dwo_file_slot;
12154 int i;
12155
12156 gdb_assert (dwp_file->version == 2);
12157
12158 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
12159 kind, pulongest (unit_index), hex_string (signature),
12160 dwp_file->name);
12161
12162 /* Fetch the section offsets of this DWO unit. */
12163
12164 memset (&sections, 0, sizeof (sections));
12165
12166 for (i = 0; i < dwp_htab->nr_columns; ++i)
12167 {
12168 uint32_t offset = read_4_bytes (dbfd,
12169 dwp_htab->section_pool.v2.offsets
12170 + (((unit_index - 1) * dwp_htab->nr_columns
12171 + i)
12172 * sizeof (uint32_t)));
12173 uint32_t size = read_4_bytes (dbfd,
12174 dwp_htab->section_pool.v2.sizes
12175 + (((unit_index - 1) * dwp_htab->nr_columns
12176 + i)
12177 * sizeof (uint32_t)));
12178
12179 switch (dwp_htab->section_pool.v2.section_ids[i])
12180 {
12181 case DW_SECT_INFO:
12182 case DW_SECT_TYPES:
12183 sections.info_or_types_offset = offset;
12184 sections.info_or_types_size = size;
12185 break;
12186 case DW_SECT_ABBREV:
12187 sections.abbrev_offset = offset;
12188 sections.abbrev_size = size;
12189 break;
12190 case DW_SECT_LINE:
12191 sections.line_offset = offset;
12192 sections.line_size = size;
12193 break;
12194 case DW_SECT_LOC:
12195 sections.loc_offset = offset;
12196 sections.loc_size = size;
12197 break;
12198 case DW_SECT_STR_OFFSETS:
12199 sections.str_offsets_offset = offset;
12200 sections.str_offsets_size = size;
12201 break;
12202 case DW_SECT_MACINFO:
12203 sections.macinfo_offset = offset;
12204 sections.macinfo_size = size;
12205 break;
12206 case DW_SECT_MACRO:
12207 sections.macro_offset = offset;
12208 sections.macro_size = size;
12209 break;
12210 }
12211 }
12212
12213 /* It's easier for the rest of the code if we fake a struct dwo_file and
12214 have dwo_unit "live" in that. At least for now.
12215
12216 The DWP file can be made up of a random collection of CUs and TUs.
12217 However, for each CU + set of TUs that came from the same original DWO
12218 file, we can combine them back into a virtual DWO file to save space
12219 (fewer struct dwo_file objects to allocate). Remember that for really
12220 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12221
12222 std::string virtual_dwo_name =
12223 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12224 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12225 (long) (sections.line_size ? sections.line_offset : 0),
12226 (long) (sections.loc_size ? sections.loc_offset : 0),
12227 (long) (sections.str_offsets_size
12228 ? sections.str_offsets_offset : 0));
12229 /* Can we use an existing virtual DWO file? */
12230 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12231 comp_dir);
12232 /* Create one if necessary. */
12233 if (*dwo_file_slot == NULL)
12234 {
12235 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12236 virtual_dwo_name.c_str ());
12237
12238 dwo_file = new struct dwo_file;
12239 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12240 dwo_file->comp_dir = comp_dir;
12241 dwo_file->sections.abbrev =
12242 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12243 sections.abbrev_offset,
12244 sections.abbrev_size);
12245 dwo_file->sections.line =
12246 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12247 sections.line_offset,
12248 sections.line_size);
12249 dwo_file->sections.loc =
12250 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12251 sections.loc_offset, sections.loc_size);
12252 dwo_file->sections.macinfo =
12253 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12254 sections.macinfo_offset,
12255 sections.macinfo_size);
12256 dwo_file->sections.macro =
12257 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12258 sections.macro_offset,
12259 sections.macro_size);
12260 dwo_file->sections.str_offsets =
12261 create_dwp_v2_or_v5_section (per_objfile,
12262 &dwp_file->sections.str_offsets,
12263 sections.str_offsets_offset,
12264 sections.str_offsets_size);
12265 /* The "str" section is global to the entire DWP file. */
12266 dwo_file->sections.str = dwp_file->sections.str;
12267 /* The info or types section is assigned below to dwo_unit,
12268 there's no need to record it in dwo_file.
12269 Also, we can't simply record type sections in dwo_file because
12270 we record a pointer into the vector in dwo_unit. As we collect more
12271 types we'll grow the vector and eventually have to reallocate space
12272 for it, invalidating all copies of pointers into the previous
12273 contents. */
12274 *dwo_file_slot = dwo_file;
12275 }
12276 else
12277 {
12278 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12279 virtual_dwo_name.c_str ());
12280
12281 dwo_file = (struct dwo_file *) *dwo_file_slot;
12282 }
12283
12284 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12285 dwo_unit->dwo_file = dwo_file;
12286 dwo_unit->signature = signature;
12287 dwo_unit->section =
12288 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12289 *dwo_unit->section = create_dwp_v2_or_v5_section
12290 (per_objfile,
12291 is_debug_types
12292 ? &dwp_file->sections.types
12293 : &dwp_file->sections.info,
12294 sections.info_or_types_offset,
12295 sections.info_or_types_size);
12296 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12297
12298 return dwo_unit;
12299 }
12300
12301 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12302 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12303 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12304 This is for DWP version 5 files. */
12305
12306 static struct dwo_unit *
12307 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12308 struct dwp_file *dwp_file,
12309 uint32_t unit_index,
12310 const char *comp_dir,
12311 ULONGEST signature, int is_debug_types)
12312 {
12313 const struct dwp_hash_table *dwp_htab
12314 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12315 bfd *dbfd = dwp_file->dbfd.get ();
12316 const char *kind = is_debug_types ? "TU" : "CU";
12317 struct dwo_file *dwo_file;
12318 struct dwo_unit *dwo_unit;
12319 struct virtual_v2_or_v5_dwo_sections sections {};
12320 void **dwo_file_slot;
12321
12322 gdb_assert (dwp_file->version == 5);
12323
12324 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
12325 kind, pulongest (unit_index), hex_string (signature),
12326 dwp_file->name);
12327
12328 /* Fetch the section offsets of this DWO unit. */
12329
12330 /* memset (&sections, 0, sizeof (sections)); */
12331
12332 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12333 {
12334 uint32_t offset = read_4_bytes (dbfd,
12335 dwp_htab->section_pool.v5.offsets
12336 + (((unit_index - 1)
12337 * dwp_htab->nr_columns
12338 + i)
12339 * sizeof (uint32_t)));
12340 uint32_t size = read_4_bytes (dbfd,
12341 dwp_htab->section_pool.v5.sizes
12342 + (((unit_index - 1) * dwp_htab->nr_columns
12343 + i)
12344 * sizeof (uint32_t)));
12345
12346 switch (dwp_htab->section_pool.v5.section_ids[i])
12347 {
12348 case DW_SECT_ABBREV_V5:
12349 sections.abbrev_offset = offset;
12350 sections.abbrev_size = size;
12351 break;
12352 case DW_SECT_INFO_V5:
12353 sections.info_or_types_offset = offset;
12354 sections.info_or_types_size = size;
12355 break;
12356 case DW_SECT_LINE_V5:
12357 sections.line_offset = offset;
12358 sections.line_size = size;
12359 break;
12360 case DW_SECT_LOCLISTS_V5:
12361 sections.loclists_offset = offset;
12362 sections.loclists_size = size;
12363 break;
12364 case DW_SECT_MACRO_V5:
12365 sections.macro_offset = offset;
12366 sections.macro_size = size;
12367 break;
12368 case DW_SECT_RNGLISTS_V5:
12369 sections.rnglists_offset = offset;
12370 sections.rnglists_size = size;
12371 break;
12372 case DW_SECT_STR_OFFSETS_V5:
12373 sections.str_offsets_offset = offset;
12374 sections.str_offsets_size = size;
12375 break;
12376 case DW_SECT_RESERVED_V5:
12377 default:
12378 break;
12379 }
12380 }
12381
12382 /* It's easier for the rest of the code if we fake a struct dwo_file and
12383 have dwo_unit "live" in that. At least for now.
12384
12385 The DWP file can be made up of a random collection of CUs and TUs.
12386 However, for each CU + set of TUs that came from the same original DWO
12387 file, we can combine them back into a virtual DWO file to save space
12388 (fewer struct dwo_file objects to allocate). Remember that for really
12389 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12390
12391 std::string virtual_dwo_name =
12392 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
12393 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12394 (long) (sections.line_size ? sections.line_offset : 0),
12395 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12396 (long) (sections.str_offsets_size
12397 ? sections.str_offsets_offset : 0),
12398 (long) (sections.macro_size ? sections.macro_offset : 0),
12399 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
12400 /* Can we use an existing virtual DWO file? */
12401 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
12402 virtual_dwo_name.c_str (),
12403 comp_dir);
12404 /* Create one if necessary. */
12405 if (*dwo_file_slot == NULL)
12406 {
12407 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12408 virtual_dwo_name.c_str ());
12409
12410 dwo_file = new struct dwo_file;
12411 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12412 dwo_file->comp_dir = comp_dir;
12413 dwo_file->sections.abbrev =
12414 create_dwp_v2_or_v5_section (per_objfile,
12415 &dwp_file->sections.abbrev,
12416 sections.abbrev_offset,
12417 sections.abbrev_size);
12418 dwo_file->sections.line =
12419 create_dwp_v2_or_v5_section (per_objfile,
12420 &dwp_file->sections.line,
12421 sections.line_offset, sections.line_size);
12422 dwo_file->sections.macro =
12423 create_dwp_v2_or_v5_section (per_objfile,
12424 &dwp_file->sections.macro,
12425 sections.macro_offset,
12426 sections.macro_size);
12427 dwo_file->sections.loclists =
12428 create_dwp_v2_or_v5_section (per_objfile,
12429 &dwp_file->sections.loclists,
12430 sections.loclists_offset,
12431 sections.loclists_size);
12432 dwo_file->sections.rnglists =
12433 create_dwp_v2_or_v5_section (per_objfile,
12434 &dwp_file->sections.rnglists,
12435 sections.rnglists_offset,
12436 sections.rnglists_size);
12437 dwo_file->sections.str_offsets =
12438 create_dwp_v2_or_v5_section (per_objfile,
12439 &dwp_file->sections.str_offsets,
12440 sections.str_offsets_offset,
12441 sections.str_offsets_size);
12442 /* The "str" section is global to the entire DWP file. */
12443 dwo_file->sections.str = dwp_file->sections.str;
12444 /* The info or types section is assigned below to dwo_unit,
12445 there's no need to record it in dwo_file.
12446 Also, we can't simply record type sections in dwo_file because
12447 we record a pointer into the vector in dwo_unit. As we collect more
12448 types we'll grow the vector and eventually have to reallocate space
12449 for it, invalidating all copies of pointers into the previous
12450 contents. */
12451 *dwo_file_slot = dwo_file;
12452 }
12453 else
12454 {
12455 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12456 virtual_dwo_name.c_str ());
12457
12458 dwo_file = (struct dwo_file *) *dwo_file_slot;
12459 }
12460
12461 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12462 dwo_unit->dwo_file = dwo_file;
12463 dwo_unit->signature = signature;
12464 dwo_unit->section
12465 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12466 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
12467 &dwp_file->sections.info,
12468 sections.info_or_types_offset,
12469 sections.info_or_types_size);
12470 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12471
12472 return dwo_unit;
12473 }
12474
12475 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12476 Returns NULL if the signature isn't found. */
12477
12478 static struct dwo_unit *
12479 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12480 struct dwp_file *dwp_file, const char *comp_dir,
12481 ULONGEST signature, int is_debug_types)
12482 {
12483 const struct dwp_hash_table *dwp_htab =
12484 is_debug_types ? dwp_file->tus : dwp_file->cus;
12485 bfd *dbfd = dwp_file->dbfd.get ();
12486 uint32_t mask = dwp_htab->nr_slots - 1;
12487 uint32_t hash = signature & mask;
12488 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12489 unsigned int i;
12490 void **slot;
12491 struct dwo_unit find_dwo_cu;
12492
12493 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12494 find_dwo_cu.signature = signature;
12495 slot = htab_find_slot (is_debug_types
12496 ? dwp_file->loaded_tus.get ()
12497 : dwp_file->loaded_cus.get (),
12498 &find_dwo_cu, INSERT);
12499
12500 if (*slot != NULL)
12501 return (struct dwo_unit *) *slot;
12502
12503 /* Use a for loop so that we don't loop forever on bad debug info. */
12504 for (i = 0; i < dwp_htab->nr_slots; ++i)
12505 {
12506 ULONGEST signature_in_table;
12507
12508 signature_in_table =
12509 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12510 if (signature_in_table == signature)
12511 {
12512 uint32_t unit_index =
12513 read_4_bytes (dbfd,
12514 dwp_htab->unit_table + hash * sizeof (uint32_t));
12515
12516 if (dwp_file->version == 1)
12517 {
12518 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12519 unit_index, comp_dir,
12520 signature, is_debug_types);
12521 }
12522 else if (dwp_file->version == 2)
12523 {
12524 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12525 unit_index, comp_dir,
12526 signature, is_debug_types);
12527 }
12528 else /* version == 5 */
12529 {
12530 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12531 unit_index, comp_dir,
12532 signature, is_debug_types);
12533 }
12534 return (struct dwo_unit *) *slot;
12535 }
12536 if (signature_in_table == 0)
12537 return NULL;
12538 hash = (hash + hash2) & mask;
12539 }
12540
12541 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12542 " [in module %s]"),
12543 dwp_file->name);
12544 }
12545
12546 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12547 Open the file specified by FILE_NAME and hand it off to BFD for
12548 preliminary analysis. Return a newly initialized bfd *, which
12549 includes a canonicalized copy of FILE_NAME.
12550 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12551 SEARCH_CWD is true if the current directory is to be searched.
12552 It will be searched before debug-file-directory.
12553 If successful, the file is added to the bfd include table of the
12554 objfile's bfd (see gdb_bfd_record_inclusion).
12555 If unable to find/open the file, return NULL.
12556 NOTE: This function is derived from symfile_bfd_open. */
12557
12558 static gdb_bfd_ref_ptr
12559 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12560 const char *file_name, int is_dwp, int search_cwd)
12561 {
12562 int desc;
12563 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12564 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12565 to debug_file_directory. */
12566 const char *search_path;
12567 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12568
12569 gdb::unique_xmalloc_ptr<char> search_path_holder;
12570 if (search_cwd)
12571 {
12572 if (*debug_file_directory != '\0')
12573 {
12574 search_path_holder.reset (concat (".", dirname_separator_string,
12575 debug_file_directory,
12576 (char *) NULL));
12577 search_path = search_path_holder.get ();
12578 }
12579 else
12580 search_path = ".";
12581 }
12582 else
12583 search_path = debug_file_directory;
12584
12585 openp_flags flags = OPF_RETURN_REALPATH;
12586 if (is_dwp)
12587 flags |= OPF_SEARCH_IN_PATH;
12588
12589 gdb::unique_xmalloc_ptr<char> absolute_name;
12590 desc = openp (search_path, flags, file_name,
12591 O_RDONLY | O_BINARY, &absolute_name);
12592 if (desc < 0)
12593 return NULL;
12594
12595 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12596 gnutarget, desc));
12597 if (sym_bfd == NULL)
12598 return NULL;
12599 bfd_set_cacheable (sym_bfd.get (), 1);
12600
12601 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12602 return NULL;
12603
12604 /* Success. Record the bfd as having been included by the objfile's bfd.
12605 This is important because things like demangled_names_hash lives in the
12606 objfile's per_bfd space and may have references to things like symbol
12607 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12608 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12609
12610 return sym_bfd;
12611 }
12612
12613 /* Try to open DWO file FILE_NAME.
12614 COMP_DIR is the DW_AT_comp_dir attribute.
12615 The result is the bfd handle of the file.
12616 If there is a problem finding or opening the file, return NULL.
12617 Upon success, the canonicalized path of the file is stored in the bfd,
12618 same as symfile_bfd_open. */
12619
12620 static gdb_bfd_ref_ptr
12621 open_dwo_file (dwarf2_per_objfile *per_objfile,
12622 const char *file_name, const char *comp_dir)
12623 {
12624 if (IS_ABSOLUTE_PATH (file_name))
12625 return try_open_dwop_file (per_objfile, file_name,
12626 0 /*is_dwp*/, 0 /*search_cwd*/);
12627
12628 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12629
12630 if (comp_dir != NULL)
12631 {
12632 gdb::unique_xmalloc_ptr<char> path_to_try
12633 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12634
12635 /* NOTE: If comp_dir is a relative path, this will also try the
12636 search path, which seems useful. */
12637 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12638 0 /*is_dwp*/,
12639 1 /*search_cwd*/));
12640 if (abfd != NULL)
12641 return abfd;
12642 }
12643
12644 /* That didn't work, try debug-file-directory, which, despite its name,
12645 is a list of paths. */
12646
12647 if (*debug_file_directory == '\0')
12648 return NULL;
12649
12650 return try_open_dwop_file (per_objfile, file_name,
12651 0 /*is_dwp*/, 1 /*search_cwd*/);
12652 }
12653
12654 /* This function is mapped across the sections and remembers the offset and
12655 size of each of the DWO debugging sections we are interested in. */
12656
12657 static void
12658 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12659 dwo_sections *dwo_sections)
12660 {
12661 const struct dwop_section_names *names = &dwop_section_names;
12662
12663 if (names->abbrev_dwo.matches (sectp->name))
12664 {
12665 dwo_sections->abbrev.s.section = sectp;
12666 dwo_sections->abbrev.size = bfd_section_size (sectp);
12667 }
12668 else if (names->info_dwo.matches (sectp->name))
12669 {
12670 dwo_sections->info.s.section = sectp;
12671 dwo_sections->info.size = bfd_section_size (sectp);
12672 }
12673 else if (names->line_dwo.matches (sectp->name))
12674 {
12675 dwo_sections->line.s.section = sectp;
12676 dwo_sections->line.size = bfd_section_size (sectp);
12677 }
12678 else if (names->loc_dwo.matches (sectp->name))
12679 {
12680 dwo_sections->loc.s.section = sectp;
12681 dwo_sections->loc.size = bfd_section_size (sectp);
12682 }
12683 else if (names->loclists_dwo.matches (sectp->name))
12684 {
12685 dwo_sections->loclists.s.section = sectp;
12686 dwo_sections->loclists.size = bfd_section_size (sectp);
12687 }
12688 else if (names->macinfo_dwo.matches (sectp->name))
12689 {
12690 dwo_sections->macinfo.s.section = sectp;
12691 dwo_sections->macinfo.size = bfd_section_size (sectp);
12692 }
12693 else if (names->macro_dwo.matches (sectp->name))
12694 {
12695 dwo_sections->macro.s.section = sectp;
12696 dwo_sections->macro.size = bfd_section_size (sectp);
12697 }
12698 else if (names->rnglists_dwo.matches (sectp->name))
12699 {
12700 dwo_sections->rnglists.s.section = sectp;
12701 dwo_sections->rnglists.size = bfd_section_size (sectp);
12702 }
12703 else if (names->str_dwo.matches (sectp->name))
12704 {
12705 dwo_sections->str.s.section = sectp;
12706 dwo_sections->str.size = bfd_section_size (sectp);
12707 }
12708 else if (names->str_offsets_dwo.matches (sectp->name))
12709 {
12710 dwo_sections->str_offsets.s.section = sectp;
12711 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12712 }
12713 else if (names->types_dwo.matches (sectp->name))
12714 {
12715 struct dwarf2_section_info type_section;
12716
12717 memset (&type_section, 0, sizeof (type_section));
12718 type_section.s.section = sectp;
12719 type_section.size = bfd_section_size (sectp);
12720 dwo_sections->types.push_back (type_section);
12721 }
12722 }
12723
12724 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12725 by PER_CU. This is for the non-DWP case.
12726 The result is NULL if DWO_NAME can't be found. */
12727
12728 static struct dwo_file *
12729 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12730 const char *comp_dir)
12731 {
12732 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12733
12734 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12735 if (dbfd == NULL)
12736 {
12737 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12738
12739 return NULL;
12740 }
12741
12742 dwo_file_up dwo_file (new struct dwo_file);
12743 dwo_file->dwo_name = dwo_name;
12744 dwo_file->comp_dir = comp_dir;
12745 dwo_file->dbfd = std::move (dbfd);
12746
12747 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12748 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12749 &dwo_file->sections);
12750
12751 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12752 dwo_file->cus);
12753
12754 if (cu->per_cu->dwarf_version < 5)
12755 {
12756 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12757 dwo_file->sections.types, dwo_file->tus);
12758 }
12759 else
12760 {
12761 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12762 &dwo_file->sections.info, dwo_file->tus,
12763 rcuh_kind::COMPILE);
12764 }
12765
12766 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
12767
12768 return dwo_file.release ();
12769 }
12770
12771 /* This function is mapped across the sections and remembers the offset and
12772 size of each of the DWP debugging sections common to version 1 and 2 that
12773 we are interested in. */
12774
12775 static void
12776 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12777 dwp_file *dwp_file)
12778 {
12779 const struct dwop_section_names *names = &dwop_section_names;
12780 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12781
12782 /* Record the ELF section number for later lookup: this is what the
12783 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12784 gdb_assert (elf_section_nr < dwp_file->num_sections);
12785 dwp_file->elf_sections[elf_section_nr] = sectp;
12786
12787 /* Look for specific sections that we need. */
12788 if (names->str_dwo.matches (sectp->name))
12789 {
12790 dwp_file->sections.str.s.section = sectp;
12791 dwp_file->sections.str.size = bfd_section_size (sectp);
12792 }
12793 else if (names->cu_index.matches (sectp->name))
12794 {
12795 dwp_file->sections.cu_index.s.section = sectp;
12796 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12797 }
12798 else if (names->tu_index.matches (sectp->name))
12799 {
12800 dwp_file->sections.tu_index.s.section = sectp;
12801 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12802 }
12803 }
12804
12805 /* This function is mapped across the sections and remembers the offset and
12806 size of each of the DWP version 2 debugging sections that we are interested
12807 in. This is split into a separate function because we don't know if we
12808 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12809
12810 static void
12811 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12812 {
12813 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12814 const struct dwop_section_names *names = &dwop_section_names;
12815 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12816
12817 /* Record the ELF section number for later lookup: this is what the
12818 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12819 gdb_assert (elf_section_nr < dwp_file->num_sections);
12820 dwp_file->elf_sections[elf_section_nr] = sectp;
12821
12822 /* Look for specific sections that we need. */
12823 if (names->abbrev_dwo.matches (sectp->name))
12824 {
12825 dwp_file->sections.abbrev.s.section = sectp;
12826 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12827 }
12828 else if (names->info_dwo.matches (sectp->name))
12829 {
12830 dwp_file->sections.info.s.section = sectp;
12831 dwp_file->sections.info.size = bfd_section_size (sectp);
12832 }
12833 else if (names->line_dwo.matches (sectp->name))
12834 {
12835 dwp_file->sections.line.s.section = sectp;
12836 dwp_file->sections.line.size = bfd_section_size (sectp);
12837 }
12838 else if (names->loc_dwo.matches (sectp->name))
12839 {
12840 dwp_file->sections.loc.s.section = sectp;
12841 dwp_file->sections.loc.size = bfd_section_size (sectp);
12842 }
12843 else if (names->macinfo_dwo.matches (sectp->name))
12844 {
12845 dwp_file->sections.macinfo.s.section = sectp;
12846 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12847 }
12848 else if (names->macro_dwo.matches (sectp->name))
12849 {
12850 dwp_file->sections.macro.s.section = sectp;
12851 dwp_file->sections.macro.size = bfd_section_size (sectp);
12852 }
12853 else if (names->str_offsets_dwo.matches (sectp->name))
12854 {
12855 dwp_file->sections.str_offsets.s.section = sectp;
12856 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12857 }
12858 else if (names->types_dwo.matches (sectp->name))
12859 {
12860 dwp_file->sections.types.s.section = sectp;
12861 dwp_file->sections.types.size = bfd_section_size (sectp);
12862 }
12863 }
12864
12865 /* This function is mapped across the sections and remembers the offset and
12866 size of each of the DWP version 5 debugging sections that we are interested
12867 in. This is split into a separate function because we don't know if we
12868 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12869
12870 static void
12871 dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12872 {
12873 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12874 const struct dwop_section_names *names = &dwop_section_names;
12875 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12876
12877 /* Record the ELF section number for later lookup: this is what the
12878 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12879 gdb_assert (elf_section_nr < dwp_file->num_sections);
12880 dwp_file->elf_sections[elf_section_nr] = sectp;
12881
12882 /* Look for specific sections that we need. */
12883 if (names->abbrev_dwo.matches (sectp->name))
12884 {
12885 dwp_file->sections.abbrev.s.section = sectp;
12886 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12887 }
12888 else if (names->info_dwo.matches (sectp->name))
12889 {
12890 dwp_file->sections.info.s.section = sectp;
12891 dwp_file->sections.info.size = bfd_section_size (sectp);
12892 }
12893 else if (names->line_dwo.matches (sectp->name))
12894 {
12895 dwp_file->sections.line.s.section = sectp;
12896 dwp_file->sections.line.size = bfd_section_size (sectp);
12897 }
12898 else if (names->loclists_dwo.matches (sectp->name))
12899 {
12900 dwp_file->sections.loclists.s.section = sectp;
12901 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12902 }
12903 else if (names->macro_dwo.matches (sectp->name))
12904 {
12905 dwp_file->sections.macro.s.section = sectp;
12906 dwp_file->sections.macro.size = bfd_section_size (sectp);
12907 }
12908 else if (names->rnglists_dwo.matches (sectp->name))
12909 {
12910 dwp_file->sections.rnglists.s.section = sectp;
12911 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12912 }
12913 else if (names->str_offsets_dwo.matches (sectp->name))
12914 {
12915 dwp_file->sections.str_offsets.s.section = sectp;
12916 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12917 }
12918 }
12919
12920 /* Hash function for dwp_file loaded CUs/TUs. */
12921
12922 static hashval_t
12923 hash_dwp_loaded_cutus (const void *item)
12924 {
12925 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12926
12927 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12928 return dwo_unit->signature;
12929 }
12930
12931 /* Equality function for dwp_file loaded CUs/TUs. */
12932
12933 static int
12934 eq_dwp_loaded_cutus (const void *a, const void *b)
12935 {
12936 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12937 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12938
12939 return dua->signature == dub->signature;
12940 }
12941
12942 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12943
12944 static htab_up
12945 allocate_dwp_loaded_cutus_table ()
12946 {
12947 return htab_up (htab_create_alloc (3,
12948 hash_dwp_loaded_cutus,
12949 eq_dwp_loaded_cutus,
12950 NULL, xcalloc, xfree));
12951 }
12952
12953 /* Try to open DWP file FILE_NAME.
12954 The result is the bfd handle of the file.
12955 If there is a problem finding or opening the file, return NULL.
12956 Upon success, the canonicalized path of the file is stored in the bfd,
12957 same as symfile_bfd_open. */
12958
12959 static gdb_bfd_ref_ptr
12960 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
12961 {
12962 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
12963 1 /*is_dwp*/,
12964 1 /*search_cwd*/));
12965 if (abfd != NULL)
12966 return abfd;
12967
12968 /* Work around upstream bug 15652.
12969 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12970 [Whether that's a "bug" is debatable, but it is getting in our way.]
12971 We have no real idea where the dwp file is, because gdb's realpath-ing
12972 of the executable's path may have discarded the needed info.
12973 [IWBN if the dwp file name was recorded in the executable, akin to
12974 .gnu_debuglink, but that doesn't exist yet.]
12975 Strip the directory from FILE_NAME and search again. */
12976 if (*debug_file_directory != '\0')
12977 {
12978 /* Don't implicitly search the current directory here.
12979 If the user wants to search "." to handle this case,
12980 it must be added to debug-file-directory. */
12981 return try_open_dwop_file (per_objfile, lbasename (file_name),
12982 1 /*is_dwp*/,
12983 0 /*search_cwd*/);
12984 }
12985
12986 return NULL;
12987 }
12988
12989 /* Initialize the use of the DWP file for the current objfile.
12990 By convention the name of the DWP file is ${objfile}.dwp.
12991 The result is NULL if it can't be found. */
12992
12993 static std::unique_ptr<struct dwp_file>
12994 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
12995 {
12996 struct objfile *objfile = per_objfile->objfile;
12997
12998 /* Try to find first .dwp for the binary file before any symbolic links
12999 resolving. */
13000
13001 /* If the objfile is a debug file, find the name of the real binary
13002 file and get the name of dwp file from there. */
13003 std::string dwp_name;
13004 if (objfile->separate_debug_objfile_backlink != NULL)
13005 {
13006 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13007 const char *backlink_basename = lbasename (backlink->original_name);
13008
13009 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13010 }
13011 else
13012 dwp_name = objfile->original_name;
13013
13014 dwp_name += ".dwp";
13015
13016 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
13017 if (dbfd == NULL
13018 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13019 {
13020 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13021 dwp_name = objfile_name (objfile);
13022 dwp_name += ".dwp";
13023 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
13024 }
13025
13026 if (dbfd == NULL)
13027 {
13028 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
13029
13030 return std::unique_ptr<dwp_file> ();
13031 }
13032
13033 const char *name = bfd_get_filename (dbfd.get ());
13034 std::unique_ptr<struct dwp_file> dwp_file
13035 (new struct dwp_file (name, std::move (dbfd)));
13036
13037 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13038 dwp_file->elf_sections =
13039 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
13040 dwp_file->num_sections, asection *);
13041
13042 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13043 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
13044 dwp_file.get ());
13045
13046 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
13047
13048 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
13049
13050 /* The DWP file version is stored in the hash table. Oh well. */
13051 if (dwp_file->cus && dwp_file->tus
13052 && dwp_file->cus->version != dwp_file->tus->version)
13053 {
13054 /* Technically speaking, we should try to limp along, but this is
13055 pretty bizarre. We use pulongest here because that's the established
13056 portability solution (e.g, we cannot use %u for uint32_t). */
13057 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13058 " TU version %s [in DWP file %s]"),
13059 pulongest (dwp_file->cus->version),
13060 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13061 }
13062
13063 if (dwp_file->cus)
13064 dwp_file->version = dwp_file->cus->version;
13065 else if (dwp_file->tus)
13066 dwp_file->version = dwp_file->tus->version;
13067 else
13068 dwp_file->version = 2;
13069
13070 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13071 {
13072 if (dwp_file->version == 2)
13073 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
13074 dwp_file.get ());
13075 else
13076 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
13077 dwp_file.get ());
13078 }
13079
13080 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
13081 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
13082
13083 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
13084 dwarf_read_debug_printf (" %s CUs, %s TUs",
13085 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13086 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13087
13088 return dwp_file;
13089 }
13090
13091 /* Wrapper around open_and_init_dwp_file, only open it once. */
13092
13093 static struct dwp_file *
13094 get_dwp_file (dwarf2_per_objfile *per_objfile)
13095 {
13096 if (!per_objfile->per_bfd->dwp_checked)
13097 {
13098 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
13099 per_objfile->per_bfd->dwp_checked = 1;
13100 }
13101 return per_objfile->per_bfd->dwp_file.get ();
13102 }
13103
13104 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13105 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13106 or in the DWP file for the objfile, referenced by THIS_UNIT.
13107 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13108 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13109
13110 This is called, for example, when wanting to read a variable with a
13111 complex location. Therefore we don't want to do file i/o for every call.
13112 Therefore we don't want to look for a DWO file on every call.
13113 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13114 then we check if we've already seen DWO_NAME, and only THEN do we check
13115 for a DWO file.
13116
13117 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13118 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13119
13120 static struct dwo_unit *
13121 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13122 ULONGEST signature, int is_debug_types)
13123 {
13124 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13125 struct objfile *objfile = per_objfile->objfile;
13126 const char *kind = is_debug_types ? "TU" : "CU";
13127 void **dwo_file_slot;
13128 struct dwo_file *dwo_file;
13129 struct dwp_file *dwp_file;
13130
13131 /* First see if there's a DWP file.
13132 If we have a DWP file but didn't find the DWO inside it, don't
13133 look for the original DWO file. It makes gdb behave differently
13134 depending on whether one is debugging in the build tree. */
13135
13136 dwp_file = get_dwp_file (per_objfile);
13137 if (dwp_file != NULL)
13138 {
13139 const struct dwp_hash_table *dwp_htab =
13140 is_debug_types ? dwp_file->tus : dwp_file->cus;
13141
13142 if (dwp_htab != NULL)
13143 {
13144 struct dwo_unit *dwo_cutu =
13145 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
13146 is_debug_types);
13147
13148 if (dwo_cutu != NULL)
13149 {
13150 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
13151 kind, hex_string (signature),
13152 host_address_to_string (dwo_cutu));
13153
13154 return dwo_cutu;
13155 }
13156 }
13157 }
13158 else
13159 {
13160 /* No DWP file, look for the DWO file. */
13161
13162 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
13163 if (*dwo_file_slot == NULL)
13164 {
13165 /* Read in the file and build a table of the CUs/TUs it contains. */
13166 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
13167 }
13168 /* NOTE: This will be NULL if unable to open the file. */
13169 dwo_file = (struct dwo_file *) *dwo_file_slot;
13170
13171 if (dwo_file != NULL)
13172 {
13173 struct dwo_unit *dwo_cutu = NULL;
13174
13175 if (is_debug_types && dwo_file->tus)
13176 {
13177 struct dwo_unit find_dwo_cutu;
13178
13179 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13180 find_dwo_cutu.signature = signature;
13181 dwo_cutu
13182 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
13183 &find_dwo_cutu);
13184 }
13185 else if (!is_debug_types && dwo_file->cus)
13186 {
13187 struct dwo_unit find_dwo_cutu;
13188
13189 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13190 find_dwo_cutu.signature = signature;
13191 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
13192 &find_dwo_cutu);
13193 }
13194
13195 if (dwo_cutu != NULL)
13196 {
13197 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
13198 kind, dwo_name, hex_string (signature),
13199 host_address_to_string (dwo_cutu));
13200
13201 return dwo_cutu;
13202 }
13203 }
13204 }
13205
13206 /* We didn't find it. This could mean a dwo_id mismatch, or
13207 someone deleted the DWO/DWP file, or the search path isn't set up
13208 correctly to find the file. */
13209
13210 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
13211 kind, dwo_name, hex_string (signature));
13212
13213 /* This is a warning and not a complaint because it can be caused by
13214 pilot error (e.g., user accidentally deleting the DWO). */
13215 {
13216 /* Print the name of the DWP file if we looked there, helps the user
13217 better diagnose the problem. */
13218 std::string dwp_text;
13219
13220 if (dwp_file != NULL)
13221 dwp_text = string_printf (" [in DWP file %s]",
13222 lbasename (dwp_file->name));
13223
13224 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13225 " [in module %s]"),
13226 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13227 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
13228 }
13229 return NULL;
13230 }
13231
13232 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13233 See lookup_dwo_cutu_unit for details. */
13234
13235 static struct dwo_unit *
13236 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13237 ULONGEST signature)
13238 {
13239 gdb_assert (!cu->per_cu->is_debug_types);
13240
13241 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
13242 }
13243
13244 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13245 See lookup_dwo_cutu_unit for details. */
13246
13247 static struct dwo_unit *
13248 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
13249 {
13250 gdb_assert (cu->per_cu->is_debug_types);
13251
13252 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13253
13254 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
13255 }
13256
13257 /* Traversal function for queue_and_load_all_dwo_tus. */
13258
13259 static int
13260 queue_and_load_dwo_tu (void **slot, void *info)
13261 {
13262 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13263 dwarf2_cu *cu = (dwarf2_cu *) info;
13264 ULONGEST signature = dwo_unit->signature;
13265 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
13266
13267 if (sig_type != NULL)
13268 {
13269 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13270
13271 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13272 a real dependency of PER_CU on SIG_TYPE. That is detected later
13273 while processing PER_CU. */
13274 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
13275 load_full_type_unit (sig_cu, cu->per_objfile);
13276 cu->per_cu->imported_symtabs_push (sig_cu);
13277 }
13278
13279 return 1;
13280 }
13281
13282 /* Queue all TUs contained in the DWO of CU to be read in.
13283 The DWO may have the only definition of the type, though it may not be
13284 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13285 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13286
13287 static void
13288 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
13289 {
13290 struct dwo_unit *dwo_unit;
13291 struct dwo_file *dwo_file;
13292
13293 gdb_assert (cu != nullptr);
13294 gdb_assert (!cu->per_cu->is_debug_types);
13295 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
13296
13297 dwo_unit = cu->dwo_unit;
13298 gdb_assert (dwo_unit != NULL);
13299
13300 dwo_file = dwo_unit->dwo_file;
13301 if (dwo_file->tus != NULL)
13302 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
13303 }
13304
13305 /* Read in various DIEs. */
13306
13307 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13308 Inherit only the children of the DW_AT_abstract_origin DIE not being
13309 already referenced by DW_AT_abstract_origin from the children of the
13310 current DIE. */
13311
13312 static void
13313 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13314 {
13315 struct die_info *child_die;
13316 sect_offset *offsetp;
13317 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13318 struct die_info *origin_die;
13319 /* Iterator of the ORIGIN_DIE children. */
13320 struct die_info *origin_child_die;
13321 struct attribute *attr;
13322 struct dwarf2_cu *origin_cu;
13323 struct pending **origin_previous_list_in_scope;
13324
13325 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13326 if (!attr)
13327 return;
13328
13329 /* Note that following die references may follow to a die in a
13330 different cu. */
13331
13332 origin_cu = cu;
13333 origin_die = follow_die_ref (die, attr, &origin_cu);
13334
13335 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13336 symbols in. */
13337 origin_previous_list_in_scope = origin_cu->list_in_scope;
13338 origin_cu->list_in_scope = cu->list_in_scope;
13339
13340 if (die->tag != origin_die->tag
13341 && !(die->tag == DW_TAG_inlined_subroutine
13342 && origin_die->tag == DW_TAG_subprogram))
13343 complaint (_("DIE %s and its abstract origin %s have different tags"),
13344 sect_offset_str (die->sect_off),
13345 sect_offset_str (origin_die->sect_off));
13346
13347 std::vector<sect_offset> offsets;
13348
13349 for (child_die = die->child;
13350 child_die && child_die->tag;
13351 child_die = child_die->sibling)
13352 {
13353 struct die_info *child_origin_die;
13354 struct dwarf2_cu *child_origin_cu;
13355
13356 /* We are trying to process concrete instance entries:
13357 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13358 it's not relevant to our analysis here. i.e. detecting DIEs that are
13359 present in the abstract instance but not referenced in the concrete
13360 one. */
13361 if (child_die->tag == DW_TAG_call_site
13362 || child_die->tag == DW_TAG_GNU_call_site)
13363 continue;
13364
13365 /* For each CHILD_DIE, find the corresponding child of
13366 ORIGIN_DIE. If there is more than one layer of
13367 DW_AT_abstract_origin, follow them all; there shouldn't be,
13368 but GCC versions at least through 4.4 generate this (GCC PR
13369 40573). */
13370 child_origin_die = child_die;
13371 child_origin_cu = cu;
13372 while (1)
13373 {
13374 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13375 child_origin_cu);
13376 if (attr == NULL)
13377 break;
13378 child_origin_die = follow_die_ref (child_origin_die, attr,
13379 &child_origin_cu);
13380 }
13381
13382 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13383 counterpart may exist. */
13384 if (child_origin_die != child_die)
13385 {
13386 if (child_die->tag != child_origin_die->tag
13387 && !(child_die->tag == DW_TAG_inlined_subroutine
13388 && child_origin_die->tag == DW_TAG_subprogram))
13389 complaint (_("Child DIE %s and its abstract origin %s have "
13390 "different tags"),
13391 sect_offset_str (child_die->sect_off),
13392 sect_offset_str (child_origin_die->sect_off));
13393 if (child_origin_die->parent != origin_die)
13394 complaint (_("Child DIE %s and its abstract origin %s have "
13395 "different parents"),
13396 sect_offset_str (child_die->sect_off),
13397 sect_offset_str (child_origin_die->sect_off));
13398 else
13399 offsets.push_back (child_origin_die->sect_off);
13400 }
13401 }
13402 std::sort (offsets.begin (), offsets.end ());
13403 sect_offset *offsets_end = offsets.data () + offsets.size ();
13404 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13405 if (offsetp[-1] == *offsetp)
13406 complaint (_("Multiple children of DIE %s refer "
13407 "to DIE %s as their abstract origin"),
13408 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13409
13410 offsetp = offsets.data ();
13411 origin_child_die = origin_die->child;
13412 while (origin_child_die && origin_child_die->tag)
13413 {
13414 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13415 while (offsetp < offsets_end
13416 && *offsetp < origin_child_die->sect_off)
13417 offsetp++;
13418 if (offsetp >= offsets_end
13419 || *offsetp > origin_child_die->sect_off)
13420 {
13421 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13422 Check whether we're already processing ORIGIN_CHILD_DIE.
13423 This can happen with mutually referenced abstract_origins.
13424 PR 16581. */
13425 if (!origin_child_die->in_process)
13426 process_die (origin_child_die, origin_cu);
13427 }
13428 origin_child_die = origin_child_die->sibling;
13429 }
13430 origin_cu->list_in_scope = origin_previous_list_in_scope;
13431
13432 if (cu != origin_cu)
13433 compute_delayed_physnames (origin_cu);
13434 }
13435
13436 static void
13437 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13438 {
13439 struct objfile *objfile = cu->per_objfile->objfile;
13440 struct gdbarch *gdbarch = objfile->arch ();
13441 struct context_stack *newobj;
13442 CORE_ADDR lowpc;
13443 CORE_ADDR highpc;
13444 struct die_info *child_die;
13445 struct attribute *attr, *call_line, *call_file;
13446 const char *name;
13447 CORE_ADDR baseaddr;
13448 struct block *block;
13449 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13450 std::vector<struct symbol *> template_args;
13451 struct template_symbol *templ_func = NULL;
13452
13453 if (inlined_func)
13454 {
13455 /* If we do not have call site information, we can't show the
13456 caller of this inlined function. That's too confusing, so
13457 only use the scope for local variables. */
13458 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13459 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13460 if (call_line == NULL || call_file == NULL)
13461 {
13462 read_lexical_block_scope (die, cu);
13463 return;
13464 }
13465 }
13466
13467 baseaddr = objfile->text_section_offset ();
13468
13469 name = dwarf2_name (die, cu);
13470
13471 /* Ignore functions with missing or empty names. These are actually
13472 illegal according to the DWARF standard. */
13473 if (name == NULL)
13474 {
13475 complaint (_("missing name for subprogram DIE at %s"),
13476 sect_offset_str (die->sect_off));
13477 return;
13478 }
13479
13480 /* Ignore functions with missing or invalid low and high pc attributes. */
13481 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13482 <= PC_BOUNDS_INVALID)
13483 {
13484 attr = dwarf2_attr (die, DW_AT_external, cu);
13485 if (attr == nullptr || !attr->as_boolean ())
13486 complaint (_("cannot get low and high bounds "
13487 "for subprogram DIE at %s"),
13488 sect_offset_str (die->sect_off));
13489 return;
13490 }
13491
13492 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13493 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13494
13495 /* If we have any template arguments, then we must allocate a
13496 different sort of symbol. */
13497 for (child_die = die->child; child_die; child_die = child_die->sibling)
13498 {
13499 if (child_die->tag == DW_TAG_template_type_param
13500 || child_die->tag == DW_TAG_template_value_param)
13501 {
13502 templ_func = new (&objfile->objfile_obstack) template_symbol;
13503 templ_func->subclass = SYMBOL_TEMPLATE;
13504 break;
13505 }
13506 }
13507
13508 newobj = cu->get_builder ()->push_context (0, lowpc);
13509 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13510 (struct symbol *) templ_func);
13511
13512 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13513 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13514 cu->language);
13515
13516 /* If there is a location expression for DW_AT_frame_base, record
13517 it. */
13518 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13519 if (attr != nullptr)
13520 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13521
13522 /* If there is a location for the static link, record it. */
13523 newobj->static_link = NULL;
13524 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13525 if (attr != nullptr)
13526 {
13527 newobj->static_link
13528 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13529 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13530 cu->addr_type ());
13531 }
13532
13533 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13534
13535 if (die->child != NULL)
13536 {
13537 child_die = die->child;
13538 while (child_die && child_die->tag)
13539 {
13540 if (child_die->tag == DW_TAG_template_type_param
13541 || child_die->tag == DW_TAG_template_value_param)
13542 {
13543 struct symbol *arg = new_symbol (child_die, NULL, cu);
13544
13545 if (arg != NULL)
13546 template_args.push_back (arg);
13547 }
13548 else
13549 process_die (child_die, cu);
13550 child_die = child_die->sibling;
13551 }
13552 }
13553
13554 inherit_abstract_dies (die, cu);
13555
13556 /* If we have a DW_AT_specification, we might need to import using
13557 directives from the context of the specification DIE. See the
13558 comment in determine_prefix. */
13559 if (cu->language == language_cplus
13560 && dwarf2_attr (die, DW_AT_specification, cu))
13561 {
13562 struct dwarf2_cu *spec_cu = cu;
13563 struct die_info *spec_die = die_specification (die, &spec_cu);
13564
13565 while (spec_die)
13566 {
13567 child_die = spec_die->child;
13568 while (child_die && child_die->tag)
13569 {
13570 if (child_die->tag == DW_TAG_imported_module)
13571 process_die (child_die, spec_cu);
13572 child_die = child_die->sibling;
13573 }
13574
13575 /* In some cases, GCC generates specification DIEs that
13576 themselves contain DW_AT_specification attributes. */
13577 spec_die = die_specification (spec_die, &spec_cu);
13578 }
13579 }
13580
13581 struct context_stack cstk = cu->get_builder ()->pop_context ();
13582 /* Make a block for the local symbols within. */
13583 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13584 cstk.static_link, lowpc, highpc);
13585
13586 /* For C++, set the block's scope. */
13587 if ((cu->language == language_cplus
13588 || cu->language == language_fortran
13589 || cu->language == language_d
13590 || cu->language == language_rust)
13591 && cu->processing_has_namespace_info)
13592 block_set_scope (block, determine_prefix (die, cu),
13593 &objfile->objfile_obstack);
13594
13595 /* If we have address ranges, record them. */
13596 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13597
13598 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13599
13600 /* Attach template arguments to function. */
13601 if (!template_args.empty ())
13602 {
13603 gdb_assert (templ_func != NULL);
13604
13605 templ_func->n_template_arguments = template_args.size ();
13606 templ_func->template_arguments
13607 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13608 templ_func->n_template_arguments);
13609 memcpy (templ_func->template_arguments,
13610 template_args.data (),
13611 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13612
13613 /* Make sure that the symtab is set on the new symbols. Even
13614 though they don't appear in this symtab directly, other parts
13615 of gdb assume that symbols do, and this is reasonably
13616 true. */
13617 for (symbol *sym : template_args)
13618 symbol_set_symtab (sym, symbol_symtab (templ_func));
13619 }
13620
13621 /* In C++, we can have functions nested inside functions (e.g., when
13622 a function declares a class that has methods). This means that
13623 when we finish processing a function scope, we may need to go
13624 back to building a containing block's symbol lists. */
13625 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13626 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13627
13628 /* If we've finished processing a top-level function, subsequent
13629 symbols go in the file symbol list. */
13630 if (cu->get_builder ()->outermost_context_p ())
13631 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13632 }
13633
13634 /* Process all the DIES contained within a lexical block scope. Start
13635 a new scope, process the dies, and then close the scope. */
13636
13637 static void
13638 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13639 {
13640 struct objfile *objfile = cu->per_objfile->objfile;
13641 struct gdbarch *gdbarch = objfile->arch ();
13642 CORE_ADDR lowpc, highpc;
13643 struct die_info *child_die;
13644 CORE_ADDR baseaddr;
13645
13646 baseaddr = objfile->text_section_offset ();
13647
13648 /* Ignore blocks with missing or invalid low and high pc attributes. */
13649 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13650 as multiple lexical blocks? Handling children in a sane way would
13651 be nasty. Might be easier to properly extend generic blocks to
13652 describe ranges. */
13653 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13654 {
13655 case PC_BOUNDS_NOT_PRESENT:
13656 /* DW_TAG_lexical_block has no attributes, process its children as if
13657 there was no wrapping by that DW_TAG_lexical_block.
13658 GCC does no longer produces such DWARF since GCC r224161. */
13659 for (child_die = die->child;
13660 child_die != NULL && child_die->tag;
13661 child_die = child_die->sibling)
13662 {
13663 /* We might already be processing this DIE. This can happen
13664 in an unusual circumstance -- where a subroutine A
13665 appears lexically in another subroutine B, but A actually
13666 inlines B. The recursion is broken here, rather than in
13667 inherit_abstract_dies, because it seems better to simply
13668 drop concrete children here. */
13669 if (!child_die->in_process)
13670 process_die (child_die, cu);
13671 }
13672 return;
13673 case PC_BOUNDS_INVALID:
13674 return;
13675 }
13676 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13677 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13678
13679 cu->get_builder ()->push_context (0, lowpc);
13680 if (die->child != NULL)
13681 {
13682 child_die = die->child;
13683 while (child_die && child_die->tag)
13684 {
13685 process_die (child_die, cu);
13686 child_die = child_die->sibling;
13687 }
13688 }
13689 inherit_abstract_dies (die, cu);
13690 struct context_stack cstk = cu->get_builder ()->pop_context ();
13691
13692 if (*cu->get_builder ()->get_local_symbols () != NULL
13693 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13694 {
13695 struct block *block
13696 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13697 cstk.start_addr, highpc);
13698
13699 /* Note that recording ranges after traversing children, as we
13700 do here, means that recording a parent's ranges entails
13701 walking across all its children's ranges as they appear in
13702 the address map, which is quadratic behavior.
13703
13704 It would be nicer to record the parent's ranges before
13705 traversing its children, simply overriding whatever you find
13706 there. But since we don't even decide whether to create a
13707 block until after we've traversed its children, that's hard
13708 to do. */
13709 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13710 }
13711 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13712 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13713 }
13714
13715 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13716
13717 static void
13718 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13719 {
13720 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13721 struct objfile *objfile = per_objfile->objfile;
13722 struct gdbarch *gdbarch = objfile->arch ();
13723 CORE_ADDR pc, baseaddr;
13724 struct attribute *attr;
13725 struct call_site *call_site, call_site_local;
13726 void **slot;
13727 int nparams;
13728 struct die_info *child_die;
13729
13730 baseaddr = objfile->text_section_offset ();
13731
13732 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13733 if (attr == NULL)
13734 {
13735 /* This was a pre-DWARF-5 GNU extension alias
13736 for DW_AT_call_return_pc. */
13737 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13738 }
13739 if (!attr)
13740 {
13741 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13742 "DIE %s [in module %s]"),
13743 sect_offset_str (die->sect_off), objfile_name (objfile));
13744 return;
13745 }
13746 pc = attr->as_address () + baseaddr;
13747 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13748
13749 if (cu->call_site_htab == NULL)
13750 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13751 NULL, &objfile->objfile_obstack,
13752 hashtab_obstack_allocate, NULL);
13753 call_site_local.pc = pc;
13754 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13755 if (*slot != NULL)
13756 {
13757 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13758 "DIE %s [in module %s]"),
13759 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13760 objfile_name (objfile));
13761 return;
13762 }
13763
13764 /* Count parameters at the caller. */
13765
13766 nparams = 0;
13767 for (child_die = die->child; child_die && child_die->tag;
13768 child_die = child_die->sibling)
13769 {
13770 if (child_die->tag != DW_TAG_call_site_parameter
13771 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13772 {
13773 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13774 "DW_TAG_call_site child DIE %s [in module %s]"),
13775 child_die->tag, sect_offset_str (child_die->sect_off),
13776 objfile_name (objfile));
13777 continue;
13778 }
13779
13780 nparams++;
13781 }
13782
13783 call_site
13784 = ((struct call_site *)
13785 obstack_alloc (&objfile->objfile_obstack,
13786 sizeof (*call_site)
13787 + (sizeof (*call_site->parameter) * (nparams - 1))));
13788 *slot = call_site;
13789 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13790 call_site->pc = pc;
13791
13792 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13793 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13794 {
13795 struct die_info *func_die;
13796
13797 /* Skip also over DW_TAG_inlined_subroutine. */
13798 for (func_die = die->parent;
13799 func_die && func_die->tag != DW_TAG_subprogram
13800 && func_die->tag != DW_TAG_subroutine_type;
13801 func_die = func_die->parent);
13802
13803 /* DW_AT_call_all_calls is a superset
13804 of DW_AT_call_all_tail_calls. */
13805 if (func_die
13806 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13807 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13808 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13809 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13810 {
13811 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13812 not complete. But keep CALL_SITE for look ups via call_site_htab,
13813 both the initial caller containing the real return address PC and
13814 the final callee containing the current PC of a chain of tail
13815 calls do not need to have the tail call list complete. But any
13816 function candidate for a virtual tail call frame searched via
13817 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13818 determined unambiguously. */
13819 }
13820 else
13821 {
13822 struct type *func_type = NULL;
13823
13824 if (func_die)
13825 func_type = get_die_type (func_die, cu);
13826 if (func_type != NULL)
13827 {
13828 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13829
13830 /* Enlist this call site to the function. */
13831 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13832 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13833 }
13834 else
13835 complaint (_("Cannot find function owning DW_TAG_call_site "
13836 "DIE %s [in module %s]"),
13837 sect_offset_str (die->sect_off), objfile_name (objfile));
13838 }
13839 }
13840
13841 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13842 if (attr == NULL)
13843 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13844 if (attr == NULL)
13845 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13846 if (attr == NULL)
13847 {
13848 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13849 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13850 }
13851 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13852 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
13853 /* Keep NULL DWARF_BLOCK. */;
13854 else if (attr->form_is_block ())
13855 {
13856 struct dwarf2_locexpr_baton *dlbaton;
13857 struct dwarf_block *block = attr->as_block ();
13858
13859 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13860 dlbaton->data = block->data;
13861 dlbaton->size = block->size;
13862 dlbaton->per_objfile = per_objfile;
13863 dlbaton->per_cu = cu->per_cu;
13864
13865 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13866 }
13867 else if (attr->form_is_ref ())
13868 {
13869 struct dwarf2_cu *target_cu = cu;
13870 struct die_info *target_die;
13871
13872 target_die = follow_die_ref (die, attr, &target_cu);
13873 gdb_assert (target_cu->per_objfile->objfile == objfile);
13874 if (die_is_declaration (target_die, target_cu))
13875 {
13876 const char *target_physname;
13877
13878 /* Prefer the mangled name; otherwise compute the demangled one. */
13879 target_physname = dw2_linkage_name (target_die, target_cu);
13880 if (target_physname == NULL)
13881 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13882 if (target_physname == NULL)
13883 complaint (_("DW_AT_call_target target DIE has invalid "
13884 "physname, for referencing DIE %s [in module %s]"),
13885 sect_offset_str (die->sect_off), objfile_name (objfile));
13886 else
13887 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13888 }
13889 else
13890 {
13891 CORE_ADDR lowpc;
13892
13893 /* DW_AT_entry_pc should be preferred. */
13894 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13895 <= PC_BOUNDS_INVALID)
13896 complaint (_("DW_AT_call_target target DIE has invalid "
13897 "low pc, for referencing DIE %s [in module %s]"),
13898 sect_offset_str (die->sect_off), objfile_name (objfile));
13899 else
13900 {
13901 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13902 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13903 }
13904 }
13905 }
13906 else
13907 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13908 "block nor reference, for DIE %s [in module %s]"),
13909 sect_offset_str (die->sect_off), objfile_name (objfile));
13910
13911 call_site->per_cu = cu->per_cu;
13912 call_site->per_objfile = per_objfile;
13913
13914 for (child_die = die->child;
13915 child_die && child_die->tag;
13916 child_die = child_die->sibling)
13917 {
13918 struct call_site_parameter *parameter;
13919 struct attribute *loc, *origin;
13920
13921 if (child_die->tag != DW_TAG_call_site_parameter
13922 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13923 {
13924 /* Already printed the complaint above. */
13925 continue;
13926 }
13927
13928 gdb_assert (call_site->parameter_count < nparams);
13929 parameter = &call_site->parameter[call_site->parameter_count];
13930
13931 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13932 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13933 register is contained in DW_AT_call_value. */
13934
13935 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13936 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13937 if (origin == NULL)
13938 {
13939 /* This was a pre-DWARF-5 GNU extension alias
13940 for DW_AT_call_parameter. */
13941 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13942 }
13943 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13944 {
13945 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13946
13947 sect_offset sect_off = origin->get_ref_die_offset ();
13948 if (!cu->header.offset_in_cu_p (sect_off))
13949 {
13950 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13951 binding can be done only inside one CU. Such referenced DIE
13952 therefore cannot be even moved to DW_TAG_partial_unit. */
13953 complaint (_("DW_AT_call_parameter offset is not in CU for "
13954 "DW_TAG_call_site child DIE %s [in module %s]"),
13955 sect_offset_str (child_die->sect_off),
13956 objfile_name (objfile));
13957 continue;
13958 }
13959 parameter->u.param_cu_off
13960 = (cu_offset) (sect_off - cu->header.sect_off);
13961 }
13962 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13963 {
13964 complaint (_("No DW_FORM_block* DW_AT_location for "
13965 "DW_TAG_call_site child DIE %s [in module %s]"),
13966 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13967 continue;
13968 }
13969 else
13970 {
13971 struct dwarf_block *block = loc->as_block ();
13972
13973 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13974 (block->data, &block->data[block->size]);
13975 if (parameter->u.dwarf_reg != -1)
13976 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13977 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
13978 &block->data[block->size],
13979 &parameter->u.fb_offset))
13980 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13981 else
13982 {
13983 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13984 "for DW_FORM_block* DW_AT_location is supported for "
13985 "DW_TAG_call_site child DIE %s "
13986 "[in module %s]"),
13987 sect_offset_str (child_die->sect_off),
13988 objfile_name (objfile));
13989 continue;
13990 }
13991 }
13992
13993 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13994 if (attr == NULL)
13995 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13996 if (attr == NULL || !attr->form_is_block ())
13997 {
13998 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13999 "DW_TAG_call_site child DIE %s [in module %s]"),
14000 sect_offset_str (child_die->sect_off),
14001 objfile_name (objfile));
14002 continue;
14003 }
14004
14005 struct dwarf_block *block = attr->as_block ();
14006 parameter->value = block->data;
14007 parameter->value_size = block->size;
14008
14009 /* Parameters are not pre-cleared by memset above. */
14010 parameter->data_value = NULL;
14011 parameter->data_value_size = 0;
14012 call_site->parameter_count++;
14013
14014 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14015 if (attr == NULL)
14016 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14017 if (attr != nullptr)
14018 {
14019 if (!attr->form_is_block ())
14020 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14021 "DW_TAG_call_site child DIE %s [in module %s]"),
14022 sect_offset_str (child_die->sect_off),
14023 objfile_name (objfile));
14024 else
14025 {
14026 block = attr->as_block ();
14027 parameter->data_value = block->data;
14028 parameter->data_value_size = block->size;
14029 }
14030 }
14031 }
14032 }
14033
14034 /* Helper function for read_variable. If DIE represents a virtual
14035 table, then return the type of the concrete object that is
14036 associated with the virtual table. Otherwise, return NULL. */
14037
14038 static struct type *
14039 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14040 {
14041 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14042 if (attr == NULL)
14043 return NULL;
14044
14045 /* Find the type DIE. */
14046 struct die_info *type_die = NULL;
14047 struct dwarf2_cu *type_cu = cu;
14048
14049 if (attr->form_is_ref ())
14050 type_die = follow_die_ref (die, attr, &type_cu);
14051 if (type_die == NULL)
14052 return NULL;
14053
14054 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14055 return NULL;
14056 return die_containing_type (type_die, type_cu);
14057 }
14058
14059 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14060
14061 static void
14062 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14063 {
14064 struct rust_vtable_symbol *storage = NULL;
14065
14066 if (cu->language == language_rust)
14067 {
14068 struct type *containing_type = rust_containing_type (die, cu);
14069
14070 if (containing_type != NULL)
14071 {
14072 struct objfile *objfile = cu->per_objfile->objfile;
14073
14074 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
14075 storage->concrete_type = containing_type;
14076 storage->subclass = SYMBOL_RUST_VTABLE;
14077 }
14078 }
14079
14080 struct symbol *res = new_symbol (die, NULL, cu, storage);
14081 struct attribute *abstract_origin
14082 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14083 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14084 if (res == NULL && loc && abstract_origin)
14085 {
14086 /* We have a variable without a name, but with a location and an abstract
14087 origin. This may be a concrete instance of an abstract variable
14088 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14089 later. */
14090 struct dwarf2_cu *origin_cu = cu;
14091 struct die_info *origin_die
14092 = follow_die_ref (die, abstract_origin, &origin_cu);
14093 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14094 per_objfile->per_bfd->abstract_to_concrete
14095 [origin_die->sect_off].push_back (die->sect_off);
14096 }
14097 }
14098
14099 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14100 reading .debug_rnglists.
14101 Callback's type should be:
14102 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14103 Return true if the attributes are present and valid, otherwise,
14104 return false. */
14105
14106 template <typename Callback>
14107 static bool
14108 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14109 dwarf_tag tag, Callback &&callback)
14110 {
14111 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14112 struct objfile *objfile = per_objfile->objfile;
14113 bfd *obfd = objfile->obfd;
14114 /* Base address selection entry. */
14115 gdb::optional<CORE_ADDR> base;
14116 const gdb_byte *buffer;
14117 CORE_ADDR baseaddr;
14118 bool overflow = false;
14119 ULONGEST addr_index;
14120 struct dwarf2_section_info *rnglists_section;
14121
14122 base = cu->base_address;
14123 rnglists_section = cu_debug_rnglists_section (cu, tag);
14124 rnglists_section->read (objfile);
14125
14126 if (offset >= rnglists_section->size)
14127 {
14128 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14129 offset);
14130 return false;
14131 }
14132 buffer = rnglists_section->buffer + offset;
14133
14134 baseaddr = objfile->text_section_offset ();
14135
14136 while (1)
14137 {
14138 /* Initialize it due to a false compiler warning. */
14139 CORE_ADDR range_beginning = 0, range_end = 0;
14140 const gdb_byte *buf_end = (rnglists_section->buffer
14141 + rnglists_section->size);
14142 unsigned int bytes_read;
14143
14144 if (buffer == buf_end)
14145 {
14146 overflow = true;
14147 break;
14148 }
14149 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14150 switch (rlet)
14151 {
14152 case DW_RLE_end_of_list:
14153 break;
14154 case DW_RLE_base_address:
14155 if (buffer + cu->header.addr_size > buf_end)
14156 {
14157 overflow = true;
14158 break;
14159 }
14160 base = cu->header.read_address (obfd, buffer, &bytes_read);
14161 buffer += bytes_read;
14162 break;
14163 case DW_RLE_base_addressx:
14164 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14165 buffer += bytes_read;
14166 base = read_addr_index (cu, addr_index);
14167 break;
14168 case DW_RLE_start_length:
14169 if (buffer + cu->header.addr_size > buf_end)
14170 {
14171 overflow = true;
14172 break;
14173 }
14174 range_beginning = cu->header.read_address (obfd, buffer,
14175 &bytes_read);
14176 buffer += bytes_read;
14177 range_end = (range_beginning
14178 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14179 buffer += bytes_read;
14180 if (buffer > buf_end)
14181 {
14182 overflow = true;
14183 break;
14184 }
14185 break;
14186 case DW_RLE_startx_length:
14187 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14188 buffer += bytes_read;
14189 range_beginning = read_addr_index (cu, addr_index);
14190 if (buffer > buf_end)
14191 {
14192 overflow = true;
14193 break;
14194 }
14195 range_end = (range_beginning
14196 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14197 buffer += bytes_read;
14198 break;
14199 case DW_RLE_offset_pair:
14200 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14201 buffer += bytes_read;
14202 if (buffer > buf_end)
14203 {
14204 overflow = true;
14205 break;
14206 }
14207 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14208 buffer += bytes_read;
14209 if (buffer > buf_end)
14210 {
14211 overflow = true;
14212 break;
14213 }
14214 break;
14215 case DW_RLE_start_end:
14216 if (buffer + 2 * cu->header.addr_size > buf_end)
14217 {
14218 overflow = true;
14219 break;
14220 }
14221 range_beginning = cu->header.read_address (obfd, buffer,
14222 &bytes_read);
14223 buffer += bytes_read;
14224 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
14225 buffer += bytes_read;
14226 break;
14227 case DW_RLE_startx_endx:
14228 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14229 buffer += bytes_read;
14230 range_beginning = read_addr_index (cu, addr_index);
14231 if (buffer > buf_end)
14232 {
14233 overflow = true;
14234 break;
14235 }
14236 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14237 buffer += bytes_read;
14238 range_end = read_addr_index (cu, addr_index);
14239 break;
14240 default:
14241 complaint (_("Invalid .debug_rnglists data (no base address)"));
14242 return false;
14243 }
14244 if (rlet == DW_RLE_end_of_list || overflow)
14245 break;
14246 if (rlet == DW_RLE_base_address)
14247 continue;
14248
14249 if (range_beginning > range_end)
14250 {
14251 /* Inverted range entries are invalid. */
14252 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14253 return false;
14254 }
14255
14256 /* Empty range entries have no effect. */
14257 if (range_beginning == range_end)
14258 continue;
14259
14260 /* Only DW_RLE_offset_pair needs the base address added. */
14261 if (rlet == DW_RLE_offset_pair)
14262 {
14263 if (!base.has_value ())
14264 {
14265 /* We have no valid base address for the DW_RLE_offset_pair. */
14266 complaint (_("Invalid .debug_rnglists data (no base address for "
14267 "DW_RLE_offset_pair)"));
14268 return false;
14269 }
14270
14271 range_beginning += *base;
14272 range_end += *base;
14273 }
14274
14275 /* A not-uncommon case of bad debug info.
14276 Don't pollute the addrmap with bad data. */
14277 if (range_beginning + baseaddr == 0
14278 && !per_objfile->per_bfd->has_section_at_zero)
14279 {
14280 complaint (_(".debug_rnglists entry has start address of zero"
14281 " [in module %s]"), objfile_name (objfile));
14282 continue;
14283 }
14284
14285 callback (range_beginning, range_end);
14286 }
14287
14288 if (overflow)
14289 {
14290 complaint (_("Offset %d is not terminated "
14291 "for DW_AT_ranges attribute"),
14292 offset);
14293 return false;
14294 }
14295
14296 return true;
14297 }
14298
14299 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14300 Callback's type should be:
14301 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14302 Return 1 if the attributes are present and valid, otherwise, return 0. */
14303
14304 template <typename Callback>
14305 static int
14306 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
14307 Callback &&callback)
14308 {
14309 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14310 struct objfile *objfile = per_objfile->objfile;
14311 struct comp_unit_head *cu_header = &cu->header;
14312 bfd *obfd = objfile->obfd;
14313 unsigned int addr_size = cu_header->addr_size;
14314 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14315 /* Base address selection entry. */
14316 gdb::optional<CORE_ADDR> base;
14317 unsigned int dummy;
14318 const gdb_byte *buffer;
14319 CORE_ADDR baseaddr;
14320
14321 if (cu_header->version >= 5)
14322 return dwarf2_rnglists_process (offset, cu, tag, callback);
14323
14324 base = cu->base_address;
14325
14326 per_objfile->per_bfd->ranges.read (objfile);
14327 if (offset >= per_objfile->per_bfd->ranges.size)
14328 {
14329 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14330 offset);
14331 return 0;
14332 }
14333 buffer = per_objfile->per_bfd->ranges.buffer + offset;
14334
14335 baseaddr = objfile->text_section_offset ();
14336
14337 while (1)
14338 {
14339 CORE_ADDR range_beginning, range_end;
14340
14341 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
14342 buffer += addr_size;
14343 range_end = cu->header.read_address (obfd, buffer, &dummy);
14344 buffer += addr_size;
14345 offset += 2 * addr_size;
14346
14347 /* An end of list marker is a pair of zero addresses. */
14348 if (range_beginning == 0 && range_end == 0)
14349 /* Found the end of list entry. */
14350 break;
14351
14352 /* Each base address selection entry is a pair of 2 values.
14353 The first is the largest possible address, the second is
14354 the base address. Check for a base address here. */
14355 if ((range_beginning & mask) == mask)
14356 {
14357 /* If we found the largest possible address, then we already
14358 have the base address in range_end. */
14359 base = range_end;
14360 continue;
14361 }
14362
14363 if (!base.has_value ())
14364 {
14365 /* We have no valid base address for the ranges
14366 data. */
14367 complaint (_("Invalid .debug_ranges data (no base address)"));
14368 return 0;
14369 }
14370
14371 if (range_beginning > range_end)
14372 {
14373 /* Inverted range entries are invalid. */
14374 complaint (_("Invalid .debug_ranges data (inverted range)"));
14375 return 0;
14376 }
14377
14378 /* Empty range entries have no effect. */
14379 if (range_beginning == range_end)
14380 continue;
14381
14382 range_beginning += *base;
14383 range_end += *base;
14384
14385 /* A not-uncommon case of bad debug info.
14386 Don't pollute the addrmap with bad data. */
14387 if (range_beginning + baseaddr == 0
14388 && !per_objfile->per_bfd->has_section_at_zero)
14389 {
14390 complaint (_(".debug_ranges entry has start address of zero"
14391 " [in module %s]"), objfile_name (objfile));
14392 continue;
14393 }
14394
14395 callback (range_beginning, range_end);
14396 }
14397
14398 return 1;
14399 }
14400
14401 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14402 Return 1 if the attributes are present and valid, otherwise, return 0.
14403 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14404
14405 static int
14406 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14407 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14408 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
14409 {
14410 struct objfile *objfile = cu->per_objfile->objfile;
14411 struct gdbarch *gdbarch = objfile->arch ();
14412 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14413 int low_set = 0;
14414 CORE_ADDR low = 0;
14415 CORE_ADDR high = 0;
14416 int retval;
14417
14418 retval = dwarf2_ranges_process (offset, cu, tag,
14419 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14420 {
14421 if (ranges_pst != NULL)
14422 {
14423 CORE_ADDR lowpc;
14424 CORE_ADDR highpc;
14425
14426 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14427 range_beginning + baseaddr)
14428 - baseaddr);
14429 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14430 range_end + baseaddr)
14431 - baseaddr);
14432 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14433 lowpc, highpc - 1, ranges_pst);
14434 }
14435
14436 /* FIXME: This is recording everything as a low-high
14437 segment of consecutive addresses. We should have a
14438 data structure for discontiguous block ranges
14439 instead. */
14440 if (! low_set)
14441 {
14442 low = range_beginning;
14443 high = range_end;
14444 low_set = 1;
14445 }
14446 else
14447 {
14448 if (range_beginning < low)
14449 low = range_beginning;
14450 if (range_end > high)
14451 high = range_end;
14452 }
14453 });
14454 if (!retval)
14455 return 0;
14456
14457 if (! low_set)
14458 /* If the first entry is an end-of-list marker, the range
14459 describes an empty scope, i.e. no instructions. */
14460 return 0;
14461
14462 if (low_return)
14463 *low_return = low;
14464 if (high_return)
14465 *high_return = high;
14466 return 1;
14467 }
14468
14469 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14470 definition for the return value. *LOWPC and *HIGHPC are set iff
14471 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14472
14473 static enum pc_bounds_kind
14474 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14475 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14476 dwarf2_psymtab *pst)
14477 {
14478 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14479 struct attribute *attr;
14480 struct attribute *attr_high;
14481 CORE_ADDR low = 0;
14482 CORE_ADDR high = 0;
14483 enum pc_bounds_kind ret;
14484
14485 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14486 if (attr_high)
14487 {
14488 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14489 if (attr != nullptr)
14490 {
14491 low = attr->as_address ();
14492 high = attr_high->as_address ();
14493 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14494 high += low;
14495 }
14496 else
14497 /* Found high w/o low attribute. */
14498 return PC_BOUNDS_INVALID;
14499
14500 /* Found consecutive range of addresses. */
14501 ret = PC_BOUNDS_HIGH_LOW;
14502 }
14503 else
14504 {
14505 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14506 if (attr != nullptr && attr->form_is_unsigned ())
14507 {
14508 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14509 on DWARF version). */
14510 ULONGEST ranges_offset = attr->as_unsigned ();
14511
14512 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14513 this value. */
14514 if (die->tag != DW_TAG_compile_unit)
14515 ranges_offset += cu->gnu_ranges_base;
14516
14517 /* Value of the DW_AT_ranges attribute is the offset in the
14518 .debug_ranges section. */
14519 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14520 die->tag))
14521 return PC_BOUNDS_INVALID;
14522 /* Found discontinuous range of addresses. */
14523 ret = PC_BOUNDS_RANGES;
14524 }
14525 else
14526 return PC_BOUNDS_NOT_PRESENT;
14527 }
14528
14529 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14530 if (high <= low)
14531 return PC_BOUNDS_INVALID;
14532
14533 /* When using the GNU linker, .gnu.linkonce. sections are used to
14534 eliminate duplicate copies of functions and vtables and such.
14535 The linker will arbitrarily choose one and discard the others.
14536 The AT_*_pc values for such functions refer to local labels in
14537 these sections. If the section from that file was discarded, the
14538 labels are not in the output, so the relocs get a value of 0.
14539 If this is a discarded function, mark the pc bounds as invalid,
14540 so that GDB will ignore it. */
14541 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14542 return PC_BOUNDS_INVALID;
14543
14544 *lowpc = low;
14545 if (highpc)
14546 *highpc = high;
14547 return ret;
14548 }
14549
14550 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14551 its low and high PC addresses. Do nothing if these addresses could not
14552 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14553 and HIGHPC to the high address if greater than HIGHPC. */
14554
14555 static void
14556 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14557 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14558 struct dwarf2_cu *cu)
14559 {
14560 CORE_ADDR low, high;
14561 struct die_info *child = die->child;
14562
14563 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14564 {
14565 *lowpc = std::min (*lowpc, low);
14566 *highpc = std::max (*highpc, high);
14567 }
14568
14569 /* If the language does not allow nested subprograms (either inside
14570 subprograms or lexical blocks), we're done. */
14571 if (cu->language != language_ada)
14572 return;
14573
14574 /* Check all the children of the given DIE. If it contains nested
14575 subprograms, then check their pc bounds. Likewise, we need to
14576 check lexical blocks as well, as they may also contain subprogram
14577 definitions. */
14578 while (child && child->tag)
14579 {
14580 if (child->tag == DW_TAG_subprogram
14581 || child->tag == DW_TAG_lexical_block)
14582 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14583 child = child->sibling;
14584 }
14585 }
14586
14587 /* Get the low and high pc's represented by the scope DIE, and store
14588 them in *LOWPC and *HIGHPC. If the correct values can't be
14589 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14590
14591 static void
14592 get_scope_pc_bounds (struct die_info *die,
14593 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14594 struct dwarf2_cu *cu)
14595 {
14596 CORE_ADDR best_low = (CORE_ADDR) -1;
14597 CORE_ADDR best_high = (CORE_ADDR) 0;
14598 CORE_ADDR current_low, current_high;
14599
14600 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14601 >= PC_BOUNDS_RANGES)
14602 {
14603 best_low = current_low;
14604 best_high = current_high;
14605 }
14606 else
14607 {
14608 struct die_info *child = die->child;
14609
14610 while (child && child->tag)
14611 {
14612 switch (child->tag) {
14613 case DW_TAG_subprogram:
14614 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14615 break;
14616 case DW_TAG_namespace:
14617 case DW_TAG_module:
14618 /* FIXME: carlton/2004-01-16: Should we do this for
14619 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14620 that current GCC's always emit the DIEs corresponding
14621 to definitions of methods of classes as children of a
14622 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14623 the DIEs giving the declarations, which could be
14624 anywhere). But I don't see any reason why the
14625 standards says that they have to be there. */
14626 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14627
14628 if (current_low != ((CORE_ADDR) -1))
14629 {
14630 best_low = std::min (best_low, current_low);
14631 best_high = std::max (best_high, current_high);
14632 }
14633 break;
14634 default:
14635 /* Ignore. */
14636 break;
14637 }
14638
14639 child = child->sibling;
14640 }
14641 }
14642
14643 *lowpc = best_low;
14644 *highpc = best_high;
14645 }
14646
14647 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14648 in DIE. */
14649
14650 static void
14651 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14652 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14653 {
14654 struct objfile *objfile = cu->per_objfile->objfile;
14655 struct gdbarch *gdbarch = objfile->arch ();
14656 struct attribute *attr;
14657 struct attribute *attr_high;
14658
14659 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14660 if (attr_high)
14661 {
14662 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14663 if (attr != nullptr)
14664 {
14665 CORE_ADDR low = attr->as_address ();
14666 CORE_ADDR high = attr_high->as_address ();
14667
14668 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14669 high += low;
14670
14671 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14672 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14673 cu->get_builder ()->record_block_range (block, low, high - 1);
14674 }
14675 }
14676
14677 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14678 if (attr != nullptr && attr->form_is_unsigned ())
14679 {
14680 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14681 on DWARF version). */
14682 ULONGEST ranges_offset = attr->as_unsigned ();
14683
14684 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14685 this value. */
14686 if (die->tag != DW_TAG_compile_unit)
14687 ranges_offset += cu->gnu_ranges_base;
14688
14689 std::vector<blockrange> blockvec;
14690 dwarf2_ranges_process (ranges_offset, cu, die->tag,
14691 [&] (CORE_ADDR start, CORE_ADDR end)
14692 {
14693 start += baseaddr;
14694 end += baseaddr;
14695 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14696 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14697 cu->get_builder ()->record_block_range (block, start, end - 1);
14698 blockvec.emplace_back (start, end);
14699 });
14700
14701 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14702 }
14703 }
14704
14705 /* Check whether the producer field indicates either of GCC < 4.6, or the
14706 Intel C/C++ compiler, and cache the result in CU. */
14707
14708 static void
14709 check_producer (struct dwarf2_cu *cu)
14710 {
14711 int major, minor;
14712
14713 if (cu->producer == NULL)
14714 {
14715 /* For unknown compilers expect their behavior is DWARF version
14716 compliant.
14717
14718 GCC started to support .debug_types sections by -gdwarf-4 since
14719 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14720 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14721 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14722 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14723 }
14724 else if (producer_is_gcc (cu->producer, &major, &minor))
14725 {
14726 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14727 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14728 }
14729 else if (producer_is_icc (cu->producer, &major, &minor))
14730 {
14731 cu->producer_is_icc = true;
14732 cu->producer_is_icc_lt_14 = major < 14;
14733 }
14734 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14735 cu->producer_is_codewarrior = true;
14736 else
14737 {
14738 /* For other non-GCC compilers, expect their behavior is DWARF version
14739 compliant. */
14740 }
14741
14742 cu->checked_producer = true;
14743 }
14744
14745 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14746 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14747 during 4.6.0 experimental. */
14748
14749 static bool
14750 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14751 {
14752 if (!cu->checked_producer)
14753 check_producer (cu);
14754
14755 return cu->producer_is_gxx_lt_4_6;
14756 }
14757
14758
14759 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14760 with incorrect is_stmt attributes. */
14761
14762 static bool
14763 producer_is_codewarrior (struct dwarf2_cu *cu)
14764 {
14765 if (!cu->checked_producer)
14766 check_producer (cu);
14767
14768 return cu->producer_is_codewarrior;
14769 }
14770
14771 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
14772 If that attribute is not available, return the appropriate
14773 default. */
14774
14775 static enum dwarf_access_attribute
14776 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14777 {
14778 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14779 if (attr != nullptr)
14780 {
14781 LONGEST value = attr->constant_value (-1);
14782 if (value == DW_ACCESS_public
14783 || value == DW_ACCESS_protected
14784 || value == DW_ACCESS_private)
14785 return (dwarf_access_attribute) value;
14786 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14787 plongest (value));
14788 }
14789
14790 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14791 {
14792 /* The default DWARF 2 accessibility for members is public, the default
14793 accessibility for inheritance is private. */
14794
14795 if (die->tag != DW_TAG_inheritance)
14796 return DW_ACCESS_public;
14797 else
14798 return DW_ACCESS_private;
14799 }
14800 else
14801 {
14802 /* DWARF 3+ defines the default accessibility a different way. The same
14803 rules apply now for DW_TAG_inheritance as for the members and it only
14804 depends on the container kind. */
14805
14806 if (die->parent->tag == DW_TAG_class_type)
14807 return DW_ACCESS_private;
14808 else
14809 return DW_ACCESS_public;
14810 }
14811 }
14812
14813 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14814 offset. If the attribute was not found return 0, otherwise return
14815 1. If it was found but could not properly be handled, set *OFFSET
14816 to 0. */
14817
14818 static int
14819 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14820 LONGEST *offset)
14821 {
14822 struct attribute *attr;
14823
14824 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14825 if (attr != NULL)
14826 {
14827 *offset = 0;
14828
14829 /* Note that we do not check for a section offset first here.
14830 This is because DW_AT_data_member_location is new in DWARF 4,
14831 so if we see it, we can assume that a constant form is really
14832 a constant and not a section offset. */
14833 if (attr->form_is_constant ())
14834 *offset = attr->constant_value (0);
14835 else if (attr->form_is_section_offset ())
14836 dwarf2_complex_location_expr_complaint ();
14837 else if (attr->form_is_block ())
14838 *offset = decode_locdesc (attr->as_block (), cu);
14839 else
14840 dwarf2_complex_location_expr_complaint ();
14841
14842 return 1;
14843 }
14844
14845 return 0;
14846 }
14847
14848 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14849
14850 static void
14851 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14852 struct field *field)
14853 {
14854 struct attribute *attr;
14855
14856 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14857 if (attr != NULL)
14858 {
14859 if (attr->form_is_constant ())
14860 {
14861 LONGEST offset = attr->constant_value (0);
14862 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14863 }
14864 else if (attr->form_is_section_offset ())
14865 dwarf2_complex_location_expr_complaint ();
14866 else if (attr->form_is_block ())
14867 {
14868 bool handled;
14869 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
14870 if (handled)
14871 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14872 else
14873 {
14874 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14875 struct objfile *objfile = per_objfile->objfile;
14876 struct dwarf2_locexpr_baton *dlbaton
14877 = XOBNEW (&objfile->objfile_obstack,
14878 struct dwarf2_locexpr_baton);
14879 dlbaton->data = attr->as_block ()->data;
14880 dlbaton->size = attr->as_block ()->size;
14881 /* When using this baton, we want to compute the address
14882 of the field, not the value. This is why
14883 is_reference is set to false here. */
14884 dlbaton->is_reference = false;
14885 dlbaton->per_objfile = per_objfile;
14886 dlbaton->per_cu = cu->per_cu;
14887
14888 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14889 }
14890 }
14891 else
14892 dwarf2_complex_location_expr_complaint ();
14893 }
14894 }
14895
14896 /* Add an aggregate field to the field list. */
14897
14898 static void
14899 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14900 struct dwarf2_cu *cu)
14901 {
14902 struct objfile *objfile = cu->per_objfile->objfile;
14903 struct gdbarch *gdbarch = objfile->arch ();
14904 struct nextfield *new_field;
14905 struct attribute *attr;
14906 struct field *fp;
14907 const char *fieldname = "";
14908
14909 if (die->tag == DW_TAG_inheritance)
14910 {
14911 fip->baseclasses.emplace_back ();
14912 new_field = &fip->baseclasses.back ();
14913 }
14914 else
14915 {
14916 fip->fields.emplace_back ();
14917 new_field = &fip->fields.back ();
14918 }
14919
14920 new_field->offset = die->sect_off;
14921
14922 new_field->accessibility = dwarf2_access_attribute (die, cu);
14923 if (new_field->accessibility != DW_ACCESS_public)
14924 fip->non_public_fields = true;
14925
14926 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14927 if (attr != nullptr)
14928 new_field->virtuality = attr->as_virtuality ();
14929 else
14930 new_field->virtuality = DW_VIRTUALITY_none;
14931
14932 fp = &new_field->field;
14933
14934 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14935 {
14936 /* Data member other than a C++ static data member. */
14937
14938 /* Get type of field. */
14939 fp->set_type (die_type (die, cu));
14940
14941 SET_FIELD_BITPOS (*fp, 0);
14942
14943 /* Get bit size of field (zero if none). */
14944 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14945 if (attr != nullptr)
14946 {
14947 FIELD_BITSIZE (*fp) = attr->constant_value (0);
14948 }
14949 else
14950 {
14951 FIELD_BITSIZE (*fp) = 0;
14952 }
14953
14954 /* Get bit offset of field. */
14955 handle_data_member_location (die, cu, fp);
14956 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14957 if (attr != nullptr && attr->form_is_constant ())
14958 {
14959 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14960 {
14961 /* For big endian bits, the DW_AT_bit_offset gives the
14962 additional bit offset from the MSB of the containing
14963 anonymous object to the MSB of the field. We don't
14964 have to do anything special since we don't need to
14965 know the size of the anonymous object. */
14966 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14967 + attr->constant_value (0)));
14968 }
14969 else
14970 {
14971 /* For little endian bits, compute the bit offset to the
14972 MSB of the anonymous object, subtract off the number of
14973 bits from the MSB of the field to the MSB of the
14974 object, and then subtract off the number of bits of
14975 the field itself. The result is the bit offset of
14976 the LSB of the field. */
14977 int anonymous_size;
14978 int bit_offset = attr->constant_value (0);
14979
14980 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14981 if (attr != nullptr && attr->form_is_constant ())
14982 {
14983 /* The size of the anonymous object containing
14984 the bit field is explicit, so use the
14985 indicated size (in bytes). */
14986 anonymous_size = attr->constant_value (0);
14987 }
14988 else
14989 {
14990 /* The size of the anonymous object containing
14991 the bit field must be inferred from the type
14992 attribute of the data member containing the
14993 bit field. */
14994 anonymous_size = TYPE_LENGTH (fp->type ());
14995 }
14996 SET_FIELD_BITPOS (*fp,
14997 (FIELD_BITPOS (*fp)
14998 + anonymous_size * bits_per_byte
14999 - bit_offset - FIELD_BITSIZE (*fp)));
15000 }
15001 }
15002 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15003 if (attr != NULL)
15004 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15005 + attr->constant_value (0)));
15006
15007 /* Get name of field. */
15008 fieldname = dwarf2_name (die, cu);
15009 if (fieldname == NULL)
15010 fieldname = "";
15011
15012 /* The name is already allocated along with this objfile, so we don't
15013 need to duplicate it for the type. */
15014 fp->name = fieldname;
15015
15016 /* Change accessibility for artificial fields (e.g. virtual table
15017 pointer or virtual base class pointer) to private. */
15018 if (dwarf2_attr (die, DW_AT_artificial, cu))
15019 {
15020 FIELD_ARTIFICIAL (*fp) = 1;
15021 new_field->accessibility = DW_ACCESS_private;
15022 fip->non_public_fields = true;
15023 }
15024 }
15025 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15026 {
15027 /* C++ static member. */
15028
15029 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15030 is a declaration, but all versions of G++ as of this writing
15031 (so through at least 3.2.1) incorrectly generate
15032 DW_TAG_variable tags. */
15033
15034 const char *physname;
15035
15036 /* Get name of field. */
15037 fieldname = dwarf2_name (die, cu);
15038 if (fieldname == NULL)
15039 return;
15040
15041 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15042 if (attr
15043 /* Only create a symbol if this is an external value.
15044 new_symbol checks this and puts the value in the global symbol
15045 table, which we want. If it is not external, new_symbol
15046 will try to put the value in cu->list_in_scope which is wrong. */
15047 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15048 {
15049 /* A static const member, not much different than an enum as far as
15050 we're concerned, except that we can support more types. */
15051 new_symbol (die, NULL, cu);
15052 }
15053
15054 /* Get physical name. */
15055 physname = dwarf2_physname (fieldname, die, cu);
15056
15057 /* The name is already allocated along with this objfile, so we don't
15058 need to duplicate it for the type. */
15059 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15060 fp->set_type (die_type (die, cu));
15061 FIELD_NAME (*fp) = fieldname;
15062 }
15063 else if (die->tag == DW_TAG_inheritance)
15064 {
15065 /* C++ base class field. */
15066 handle_data_member_location (die, cu, fp);
15067 FIELD_BITSIZE (*fp) = 0;
15068 fp->set_type (die_type (die, cu));
15069 FIELD_NAME (*fp) = fp->type ()->name ();
15070 }
15071 else
15072 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15073 }
15074
15075 /* Can the type given by DIE define another type? */
15076
15077 static bool
15078 type_can_define_types (const struct die_info *die)
15079 {
15080 switch (die->tag)
15081 {
15082 case DW_TAG_typedef:
15083 case DW_TAG_class_type:
15084 case DW_TAG_structure_type:
15085 case DW_TAG_union_type:
15086 case DW_TAG_enumeration_type:
15087 return true;
15088
15089 default:
15090 return false;
15091 }
15092 }
15093
15094 /* Add a type definition defined in the scope of the FIP's class. */
15095
15096 static void
15097 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15098 struct dwarf2_cu *cu)
15099 {
15100 struct decl_field fp;
15101 memset (&fp, 0, sizeof (fp));
15102
15103 gdb_assert (type_can_define_types (die));
15104
15105 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15106 fp.name = dwarf2_name (die, cu);
15107 fp.type = read_type_die (die, cu);
15108
15109 /* Save accessibility. */
15110 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15111 switch (accessibility)
15112 {
15113 case DW_ACCESS_public:
15114 /* The assumed value if neither private nor protected. */
15115 break;
15116 case DW_ACCESS_private:
15117 fp.is_private = 1;
15118 break;
15119 case DW_ACCESS_protected:
15120 fp.is_protected = 1;
15121 break;
15122 }
15123
15124 if (die->tag == DW_TAG_typedef)
15125 fip->typedef_field_list.push_back (fp);
15126 else
15127 fip->nested_types_list.push_back (fp);
15128 }
15129
15130 /* A convenience typedef that's used when finding the discriminant
15131 field for a variant part. */
15132 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
15133 offset_map_type;
15134
15135 /* Compute the discriminant range for a given variant. OBSTACK is
15136 where the results will be stored. VARIANT is the variant to
15137 process. IS_UNSIGNED indicates whether the discriminant is signed
15138 or unsigned. */
15139
15140 static const gdb::array_view<discriminant_range>
15141 convert_variant_range (struct obstack *obstack, const variant_field &variant,
15142 bool is_unsigned)
15143 {
15144 std::vector<discriminant_range> ranges;
15145
15146 if (variant.default_branch)
15147 return {};
15148
15149 if (variant.discr_list_data == nullptr)
15150 {
15151 discriminant_range r
15152 = {variant.discriminant_value, variant.discriminant_value};
15153 ranges.push_back (r);
15154 }
15155 else
15156 {
15157 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15158 variant.discr_list_data->size);
15159 while (!data.empty ())
15160 {
15161 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15162 {
15163 complaint (_("invalid discriminant marker: %d"), data[0]);
15164 break;
15165 }
15166 bool is_range = data[0] == DW_DSC_range;
15167 data = data.slice (1);
15168
15169 ULONGEST low, high;
15170 unsigned int bytes_read;
15171
15172 if (data.empty ())
15173 {
15174 complaint (_("DW_AT_discr_list missing low value"));
15175 break;
15176 }
15177 if (is_unsigned)
15178 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15179 else
15180 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15181 &bytes_read);
15182 data = data.slice (bytes_read);
15183
15184 if (is_range)
15185 {
15186 if (data.empty ())
15187 {
15188 complaint (_("DW_AT_discr_list missing high value"));
15189 break;
15190 }
15191 if (is_unsigned)
15192 high = read_unsigned_leb128 (nullptr, data.data (),
15193 &bytes_read);
15194 else
15195 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15196 &bytes_read);
15197 data = data.slice (bytes_read);
15198 }
15199 else
15200 high = low;
15201
15202 ranges.push_back ({ low, high });
15203 }
15204 }
15205
15206 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15207 ranges.size ());
15208 std::copy (ranges.begin (), ranges.end (), result);
15209 return gdb::array_view<discriminant_range> (result, ranges.size ());
15210 }
15211
15212 static const gdb::array_view<variant_part> create_variant_parts
15213 (struct obstack *obstack,
15214 const offset_map_type &offset_map,
15215 struct field_info *fi,
15216 const std::vector<variant_part_builder> &variant_parts);
15217
15218 /* Fill in a "struct variant" for a given variant field. RESULT is
15219 the variant to fill in. OBSTACK is where any needed allocations
15220 will be done. OFFSET_MAP holds the mapping from section offsets to
15221 fields for the type. FI describes the fields of the type we're
15222 processing. FIELD is the variant field we're converting. */
15223
15224 static void
15225 create_one_variant (variant &result, struct obstack *obstack,
15226 const offset_map_type &offset_map,
15227 struct field_info *fi, const variant_field &field)
15228 {
15229 result.discriminants = convert_variant_range (obstack, field, false);
15230 result.first_field = field.first_field + fi->baseclasses.size ();
15231 result.last_field = field.last_field + fi->baseclasses.size ();
15232 result.parts = create_variant_parts (obstack, offset_map, fi,
15233 field.variant_parts);
15234 }
15235
15236 /* Fill in a "struct variant_part" for a given variant part. RESULT
15237 is the variant part to fill in. OBSTACK is where any needed
15238 allocations will be done. OFFSET_MAP holds the mapping from
15239 section offsets to fields for the type. FI describes the fields of
15240 the type we're processing. BUILDER is the variant part to be
15241 converted. */
15242
15243 static void
15244 create_one_variant_part (variant_part &result,
15245 struct obstack *obstack,
15246 const offset_map_type &offset_map,
15247 struct field_info *fi,
15248 const variant_part_builder &builder)
15249 {
15250 auto iter = offset_map.find (builder.discriminant_offset);
15251 if (iter == offset_map.end ())
15252 {
15253 result.discriminant_index = -1;
15254 /* Doesn't matter. */
15255 result.is_unsigned = false;
15256 }
15257 else
15258 {
15259 result.discriminant_index = iter->second;
15260 result.is_unsigned
15261 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
15262 }
15263
15264 size_t n = builder.variants.size ();
15265 variant *output = new (obstack) variant[n];
15266 for (size_t i = 0; i < n; ++i)
15267 create_one_variant (output[i], obstack, offset_map, fi,
15268 builder.variants[i]);
15269
15270 result.variants = gdb::array_view<variant> (output, n);
15271 }
15272
15273 /* Create a vector of variant parts that can be attached to a type.
15274 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15275 holds the mapping from section offsets to fields for the type. FI
15276 describes the fields of the type we're processing. VARIANT_PARTS
15277 is the vector to convert. */
15278
15279 static const gdb::array_view<variant_part>
15280 create_variant_parts (struct obstack *obstack,
15281 const offset_map_type &offset_map,
15282 struct field_info *fi,
15283 const std::vector<variant_part_builder> &variant_parts)
15284 {
15285 if (variant_parts.empty ())
15286 return {};
15287
15288 size_t n = variant_parts.size ();
15289 variant_part *result = new (obstack) variant_part[n];
15290 for (size_t i = 0; i < n; ++i)
15291 create_one_variant_part (result[i], obstack, offset_map, fi,
15292 variant_parts[i]);
15293
15294 return gdb::array_view<variant_part> (result, n);
15295 }
15296
15297 /* Compute the variant part vector for FIP, attaching it to TYPE when
15298 done. */
15299
15300 static void
15301 add_variant_property (struct field_info *fip, struct type *type,
15302 struct dwarf2_cu *cu)
15303 {
15304 /* Map section offsets of fields to their field index. Note the
15305 field index here does not take the number of baseclasses into
15306 account. */
15307 offset_map_type offset_map;
15308 for (int i = 0; i < fip->fields.size (); ++i)
15309 offset_map[fip->fields[i].offset] = i;
15310
15311 struct objfile *objfile = cu->per_objfile->objfile;
15312 gdb::array_view<variant_part> parts
15313 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15314 fip->variant_parts);
15315
15316 struct dynamic_prop prop;
15317 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15318 obstack_copy (&objfile->objfile_obstack, &parts,
15319 sizeof (parts)));
15320
15321 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
15322 }
15323
15324 /* Create the vector of fields, and attach it to the type. */
15325
15326 static void
15327 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15328 struct dwarf2_cu *cu)
15329 {
15330 int nfields = fip->nfields ();
15331
15332 /* Record the field count, allocate space for the array of fields,
15333 and create blank accessibility bitfields if necessary. */
15334 type->set_num_fields (nfields);
15335 type->set_fields
15336 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
15337
15338 if (fip->non_public_fields && cu->language != language_ada)
15339 {
15340 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15341
15342 TYPE_FIELD_PRIVATE_BITS (type) =
15343 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15344 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15345
15346 TYPE_FIELD_PROTECTED_BITS (type) =
15347 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15348 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15349
15350 TYPE_FIELD_IGNORE_BITS (type) =
15351 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15352 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15353 }
15354
15355 /* If the type has baseclasses, allocate and clear a bit vector for
15356 TYPE_FIELD_VIRTUAL_BITS. */
15357 if (!fip->baseclasses.empty () && cu->language != language_ada)
15358 {
15359 int num_bytes = B_BYTES (fip->baseclasses.size ());
15360 unsigned char *pointer;
15361
15362 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15363 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15364 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15365 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15366 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15367 }
15368
15369 if (!fip->variant_parts.empty ())
15370 add_variant_property (fip, type, cu);
15371
15372 /* Copy the saved-up fields into the field vector. */
15373 for (int i = 0; i < nfields; ++i)
15374 {
15375 struct nextfield &field
15376 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15377 : fip->fields[i - fip->baseclasses.size ()]);
15378
15379 type->field (i) = field.field;
15380 switch (field.accessibility)
15381 {
15382 case DW_ACCESS_private:
15383 if (cu->language != language_ada)
15384 SET_TYPE_FIELD_PRIVATE (type, i);
15385 break;
15386
15387 case DW_ACCESS_protected:
15388 if (cu->language != language_ada)
15389 SET_TYPE_FIELD_PROTECTED (type, i);
15390 break;
15391
15392 case DW_ACCESS_public:
15393 break;
15394
15395 default:
15396 /* Unknown accessibility. Complain and treat it as public. */
15397 {
15398 complaint (_("unsupported accessibility %d"),
15399 field.accessibility);
15400 }
15401 break;
15402 }
15403 if (i < fip->baseclasses.size ())
15404 {
15405 switch (field.virtuality)
15406 {
15407 case DW_VIRTUALITY_virtual:
15408 case DW_VIRTUALITY_pure_virtual:
15409 if (cu->language == language_ada)
15410 error (_("unexpected virtuality in component of Ada type"));
15411 SET_TYPE_FIELD_VIRTUAL (type, i);
15412 break;
15413 }
15414 }
15415 }
15416 }
15417
15418 /* Return true if this member function is a constructor, false
15419 otherwise. */
15420
15421 static int
15422 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15423 {
15424 const char *fieldname;
15425 const char *type_name;
15426 int len;
15427
15428 if (die->parent == NULL)
15429 return 0;
15430
15431 if (die->parent->tag != DW_TAG_structure_type
15432 && die->parent->tag != DW_TAG_union_type
15433 && die->parent->tag != DW_TAG_class_type)
15434 return 0;
15435
15436 fieldname = dwarf2_name (die, cu);
15437 type_name = dwarf2_name (die->parent, cu);
15438 if (fieldname == NULL || type_name == NULL)
15439 return 0;
15440
15441 len = strlen (fieldname);
15442 return (strncmp (fieldname, type_name, len) == 0
15443 && (type_name[len] == '\0' || type_name[len] == '<'));
15444 }
15445
15446 /* Add a member function to the proper fieldlist. */
15447
15448 static void
15449 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15450 struct type *type, struct dwarf2_cu *cu)
15451 {
15452 struct objfile *objfile = cu->per_objfile->objfile;
15453 struct attribute *attr;
15454 int i;
15455 struct fnfieldlist *flp = nullptr;
15456 struct fn_field *fnp;
15457 const char *fieldname;
15458 struct type *this_type;
15459
15460 if (cu->language == language_ada)
15461 error (_("unexpected member function in Ada type"));
15462
15463 /* Get name of member function. */
15464 fieldname = dwarf2_name (die, cu);
15465 if (fieldname == NULL)
15466 return;
15467
15468 /* Look up member function name in fieldlist. */
15469 for (i = 0; i < fip->fnfieldlists.size (); i++)
15470 {
15471 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15472 {
15473 flp = &fip->fnfieldlists[i];
15474 break;
15475 }
15476 }
15477
15478 /* Create a new fnfieldlist if necessary. */
15479 if (flp == nullptr)
15480 {
15481 fip->fnfieldlists.emplace_back ();
15482 flp = &fip->fnfieldlists.back ();
15483 flp->name = fieldname;
15484 i = fip->fnfieldlists.size () - 1;
15485 }
15486
15487 /* Create a new member function field and add it to the vector of
15488 fnfieldlists. */
15489 flp->fnfields.emplace_back ();
15490 fnp = &flp->fnfields.back ();
15491
15492 /* Delay processing of the physname until later. */
15493 if (cu->language == language_cplus)
15494 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15495 die, cu);
15496 else
15497 {
15498 const char *physname = dwarf2_physname (fieldname, die, cu);
15499 fnp->physname = physname ? physname : "";
15500 }
15501
15502 fnp->type = alloc_type (objfile);
15503 this_type = read_type_die (die, cu);
15504 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15505 {
15506 int nparams = this_type->num_fields ();
15507
15508 /* TYPE is the domain of this method, and THIS_TYPE is the type
15509 of the method itself (TYPE_CODE_METHOD). */
15510 smash_to_method_type (fnp->type, type,
15511 TYPE_TARGET_TYPE (this_type),
15512 this_type->fields (),
15513 this_type->num_fields (),
15514 this_type->has_varargs ());
15515
15516 /* Handle static member functions.
15517 Dwarf2 has no clean way to discern C++ static and non-static
15518 member functions. G++ helps GDB by marking the first
15519 parameter for non-static member functions (which is the this
15520 pointer) as artificial. We obtain this information from
15521 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15522 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15523 fnp->voffset = VOFFSET_STATIC;
15524 }
15525 else
15526 complaint (_("member function type missing for '%s'"),
15527 dwarf2_full_name (fieldname, die, cu));
15528
15529 /* Get fcontext from DW_AT_containing_type if present. */
15530 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15531 fnp->fcontext = die_containing_type (die, cu);
15532
15533 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15534 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15535
15536 /* Get accessibility. */
15537 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15538 switch (accessibility)
15539 {
15540 case DW_ACCESS_private:
15541 fnp->is_private = 1;
15542 break;
15543 case DW_ACCESS_protected:
15544 fnp->is_protected = 1;
15545 break;
15546 }
15547
15548 /* Check for artificial methods. */
15549 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15550 if (attr && attr->as_boolean ())
15551 fnp->is_artificial = 1;
15552
15553 /* Check for defaulted methods. */
15554 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15555 if (attr != nullptr)
15556 fnp->defaulted = attr->defaulted ();
15557
15558 /* Check for deleted methods. */
15559 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15560 if (attr != nullptr && attr->as_boolean ())
15561 fnp->is_deleted = 1;
15562
15563 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15564
15565 /* Get index in virtual function table if it is a virtual member
15566 function. For older versions of GCC, this is an offset in the
15567 appropriate virtual table, as specified by DW_AT_containing_type.
15568 For everyone else, it is an expression to be evaluated relative
15569 to the object address. */
15570
15571 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15572 if (attr != nullptr)
15573 {
15574 if (attr->form_is_block () && attr->as_block ()->size > 0)
15575 {
15576 struct dwarf_block *block = attr->as_block ();
15577
15578 if (block->data[0] == DW_OP_constu)
15579 {
15580 /* Old-style GCC. */
15581 fnp->voffset = decode_locdesc (block, cu) + 2;
15582 }
15583 else if (block->data[0] == DW_OP_deref
15584 || (block->size > 1
15585 && block->data[0] == DW_OP_deref_size
15586 && block->data[1] == cu->header.addr_size))
15587 {
15588 fnp->voffset = decode_locdesc (block, cu);
15589 if ((fnp->voffset % cu->header.addr_size) != 0)
15590 dwarf2_complex_location_expr_complaint ();
15591 else
15592 fnp->voffset /= cu->header.addr_size;
15593 fnp->voffset += 2;
15594 }
15595 else
15596 dwarf2_complex_location_expr_complaint ();
15597
15598 if (!fnp->fcontext)
15599 {
15600 /* If there is no `this' field and no DW_AT_containing_type,
15601 we cannot actually find a base class context for the
15602 vtable! */
15603 if (this_type->num_fields () == 0
15604 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15605 {
15606 complaint (_("cannot determine context for virtual member "
15607 "function \"%s\" (offset %s)"),
15608 fieldname, sect_offset_str (die->sect_off));
15609 }
15610 else
15611 {
15612 fnp->fcontext
15613 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15614 }
15615 }
15616 }
15617 else if (attr->form_is_section_offset ())
15618 {
15619 dwarf2_complex_location_expr_complaint ();
15620 }
15621 else
15622 {
15623 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15624 fieldname);
15625 }
15626 }
15627 else
15628 {
15629 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15630 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
15631 {
15632 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15633 complaint (_("Member function \"%s\" (offset %s) is virtual "
15634 "but the vtable offset is not specified"),
15635 fieldname, sect_offset_str (die->sect_off));
15636 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15637 TYPE_CPLUS_DYNAMIC (type) = 1;
15638 }
15639 }
15640 }
15641
15642 /* Create the vector of member function fields, and attach it to the type. */
15643
15644 static void
15645 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15646 struct dwarf2_cu *cu)
15647 {
15648 if (cu->language == language_ada)
15649 error (_("unexpected member functions in Ada type"));
15650
15651 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15652 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15653 TYPE_ALLOC (type,
15654 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15655
15656 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15657 {
15658 struct fnfieldlist &nf = fip->fnfieldlists[i];
15659 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15660
15661 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15662 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15663 fn_flp->fn_fields = (struct fn_field *)
15664 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15665
15666 for (int k = 0; k < nf.fnfields.size (); ++k)
15667 fn_flp->fn_fields[k] = nf.fnfields[k];
15668 }
15669
15670 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15671 }
15672
15673 /* Returns non-zero if NAME is the name of a vtable member in CU's
15674 language, zero otherwise. */
15675 static int
15676 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15677 {
15678 static const char vptr[] = "_vptr";
15679
15680 /* Look for the C++ form of the vtable. */
15681 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15682 return 1;
15683
15684 return 0;
15685 }
15686
15687 /* GCC outputs unnamed structures that are really pointers to member
15688 functions, with the ABI-specified layout. If TYPE describes
15689 such a structure, smash it into a member function type.
15690
15691 GCC shouldn't do this; it should just output pointer to member DIEs.
15692 This is GCC PR debug/28767. */
15693
15694 static void
15695 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15696 {
15697 struct type *pfn_type, *self_type, *new_type;
15698
15699 /* Check for a structure with no name and two children. */
15700 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15701 return;
15702
15703 /* Check for __pfn and __delta members. */
15704 if (TYPE_FIELD_NAME (type, 0) == NULL
15705 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15706 || TYPE_FIELD_NAME (type, 1) == NULL
15707 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15708 return;
15709
15710 /* Find the type of the method. */
15711 pfn_type = type->field (0).type ();
15712 if (pfn_type == NULL
15713 || pfn_type->code () != TYPE_CODE_PTR
15714 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15715 return;
15716
15717 /* Look for the "this" argument. */
15718 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15719 if (pfn_type->num_fields () == 0
15720 /* || pfn_type->field (0).type () == NULL */
15721 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15722 return;
15723
15724 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15725 new_type = alloc_type (objfile);
15726 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15727 pfn_type->fields (), pfn_type->num_fields (),
15728 pfn_type->has_varargs ());
15729 smash_to_methodptr_type (type, new_type);
15730 }
15731
15732 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15733 requires rewriting, then copy it and return the updated copy.
15734 Otherwise return nullptr. */
15735
15736 static struct type *
15737 rewrite_array_type (struct type *type)
15738 {
15739 if (type->code () != TYPE_CODE_ARRAY)
15740 return nullptr;
15741
15742 struct type *index_type = type->index_type ();
15743 range_bounds *current_bounds = index_type->bounds ();
15744
15745 /* Handle multi-dimensional arrays. */
15746 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15747 if (new_target == nullptr)
15748 {
15749 /* Maybe we don't need to rewrite this array. */
15750 if (current_bounds->low.kind () == PROP_CONST
15751 && current_bounds->high.kind () == PROP_CONST)
15752 return nullptr;
15753 }
15754
15755 /* Either the target type was rewritten, or the bounds have to be
15756 updated. Either way we want to copy the type and update
15757 everything. */
15758 struct type *copy = copy_type (type);
15759 int nfields = copy->num_fields ();
15760 field *new_fields
15761 = ((struct field *) TYPE_ZALLOC (copy,
15762 nfields * sizeof (struct field)));
15763 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15764 copy->set_fields (new_fields);
15765 if (new_target != nullptr)
15766 TYPE_TARGET_TYPE (copy) = new_target;
15767
15768 struct type *index_copy = copy_type (index_type);
15769 range_bounds *bounds
15770 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15771 sizeof (range_bounds));
15772 *bounds = *current_bounds;
15773 bounds->low.set_const_val (1);
15774 bounds->high.set_const_val (0);
15775 index_copy->set_bounds (bounds);
15776 copy->set_index_type (index_copy);
15777
15778 return copy;
15779 }
15780
15781 /* While some versions of GCC will generate complicated DWARF for an
15782 array (see quirk_ada_thick_pointer), more recent versions were
15783 modified to emit an explicit thick pointer structure. However, in
15784 this case, the array still has DWARF expressions for its ranges,
15785 and these must be ignored. */
15786
15787 static void
15788 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15789 struct type *type)
15790 {
15791 gdb_assert (cu->language == language_ada);
15792
15793 /* Check for a structure with two children. */
15794 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15795 return;
15796
15797 /* Check for P_ARRAY and P_BOUNDS members. */
15798 if (TYPE_FIELD_NAME (type, 0) == NULL
15799 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15800 || TYPE_FIELD_NAME (type, 1) == NULL
15801 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15802 return;
15803
15804 /* Make sure we're looking at a pointer to an array. */
15805 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15806 return;
15807
15808 /* The Ada code already knows how to handle these types, so all that
15809 we need to do is turn the bounds into static bounds. However, we
15810 don't want to rewrite existing array or index types in-place,
15811 because those may be referenced in other contexts where this
15812 rewriting is undesirable. */
15813 struct type *new_ary_type
15814 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15815 if (new_ary_type != nullptr)
15816 type->field (0).set_type (lookup_pointer_type (new_ary_type));
15817 }
15818
15819 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15820 appropriate error checking and issuing complaints if there is a
15821 problem. */
15822
15823 static ULONGEST
15824 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15825 {
15826 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15827
15828 if (attr == nullptr)
15829 return 0;
15830
15831 if (!attr->form_is_constant ())
15832 {
15833 complaint (_("DW_AT_alignment must have constant form"
15834 " - DIE at %s [in module %s]"),
15835 sect_offset_str (die->sect_off),
15836 objfile_name (cu->per_objfile->objfile));
15837 return 0;
15838 }
15839
15840 LONGEST val = attr->constant_value (0);
15841 if (val < 0)
15842 {
15843 complaint (_("DW_AT_alignment value must not be negative"
15844 " - DIE at %s [in module %s]"),
15845 sect_offset_str (die->sect_off),
15846 objfile_name (cu->per_objfile->objfile));
15847 return 0;
15848 }
15849 ULONGEST align = val;
15850
15851 if (align == 0)
15852 {
15853 complaint (_("DW_AT_alignment value must not be zero"
15854 " - DIE at %s [in module %s]"),
15855 sect_offset_str (die->sect_off),
15856 objfile_name (cu->per_objfile->objfile));
15857 return 0;
15858 }
15859 if ((align & (align - 1)) != 0)
15860 {
15861 complaint (_("DW_AT_alignment value must be a power of 2"
15862 " - DIE at %s [in module %s]"),
15863 sect_offset_str (die->sect_off),
15864 objfile_name (cu->per_objfile->objfile));
15865 return 0;
15866 }
15867
15868 return align;
15869 }
15870
15871 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15872 the alignment for TYPE. */
15873
15874 static void
15875 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15876 struct type *type)
15877 {
15878 if (!set_type_align (type, get_alignment (cu, die)))
15879 complaint (_("DW_AT_alignment value too large"
15880 " - DIE at %s [in module %s]"),
15881 sect_offset_str (die->sect_off),
15882 objfile_name (cu->per_objfile->objfile));
15883 }
15884
15885 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15886 constant for a type, according to DWARF5 spec, Table 5.5. */
15887
15888 static bool
15889 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15890 {
15891 switch (value)
15892 {
15893 case DW_CC_normal:
15894 case DW_CC_pass_by_reference:
15895 case DW_CC_pass_by_value:
15896 return true;
15897
15898 default:
15899 complaint (_("unrecognized DW_AT_calling_convention value "
15900 "(%s) for a type"), pulongest (value));
15901 return false;
15902 }
15903 }
15904
15905 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15906 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15907 also according to GNU-specific values (see include/dwarf2.h). */
15908
15909 static bool
15910 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15911 {
15912 switch (value)
15913 {
15914 case DW_CC_normal:
15915 case DW_CC_program:
15916 case DW_CC_nocall:
15917 return true;
15918
15919 case DW_CC_GNU_renesas_sh:
15920 case DW_CC_GNU_borland_fastcall_i386:
15921 case DW_CC_GDB_IBM_OpenCL:
15922 return true;
15923
15924 default:
15925 complaint (_("unrecognized DW_AT_calling_convention value "
15926 "(%s) for a subroutine"), pulongest (value));
15927 return false;
15928 }
15929 }
15930
15931 /* Called when we find the DIE that starts a structure or union scope
15932 (definition) to create a type for the structure or union. Fill in
15933 the type's name and general properties; the members will not be
15934 processed until process_structure_scope. A symbol table entry for
15935 the type will also not be done until process_structure_scope (assuming
15936 the type has a name).
15937
15938 NOTE: we need to call these functions regardless of whether or not the
15939 DIE has a DW_AT_name attribute, since it might be an anonymous
15940 structure or union. This gets the type entered into our set of
15941 user defined types. */
15942
15943 static struct type *
15944 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15945 {
15946 struct objfile *objfile = cu->per_objfile->objfile;
15947 struct type *type;
15948 struct attribute *attr;
15949 const char *name;
15950
15951 /* If the definition of this type lives in .debug_types, read that type.
15952 Don't follow DW_AT_specification though, that will take us back up
15953 the chain and we want to go down. */
15954 attr = die->attr (DW_AT_signature);
15955 if (attr != nullptr)
15956 {
15957 type = get_DW_AT_signature_type (die, attr, cu);
15958
15959 /* The type's CU may not be the same as CU.
15960 Ensure TYPE is recorded with CU in die_type_hash. */
15961 return set_die_type (die, type, cu);
15962 }
15963
15964 type = alloc_type (objfile);
15965 INIT_CPLUS_SPECIFIC (type);
15966
15967 name = dwarf2_name (die, cu);
15968 if (name != NULL)
15969 {
15970 if (cu->language == language_cplus
15971 || cu->language == language_d
15972 || cu->language == language_rust)
15973 {
15974 const char *full_name = dwarf2_full_name (name, die, cu);
15975
15976 /* dwarf2_full_name might have already finished building the DIE's
15977 type. If so, there is no need to continue. */
15978 if (get_die_type (die, cu) != NULL)
15979 return get_die_type (die, cu);
15980
15981 type->set_name (full_name);
15982 }
15983 else
15984 {
15985 /* The name is already allocated along with this objfile, so
15986 we don't need to duplicate it for the type. */
15987 type->set_name (name);
15988 }
15989 }
15990
15991 if (die->tag == DW_TAG_structure_type)
15992 {
15993 type->set_code (TYPE_CODE_STRUCT);
15994 }
15995 else if (die->tag == DW_TAG_union_type)
15996 {
15997 type->set_code (TYPE_CODE_UNION);
15998 }
15999 else
16000 {
16001 type->set_code (TYPE_CODE_STRUCT);
16002 }
16003
16004 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16005 TYPE_DECLARED_CLASS (type) = 1;
16006
16007 /* Store the calling convention in the type if it's available in
16008 the die. Otherwise the calling convention remains set to
16009 the default value DW_CC_normal. */
16010 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16011 if (attr != nullptr
16012 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
16013 {
16014 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16015 TYPE_CPLUS_CALLING_CONVENTION (type)
16016 = (enum dwarf_calling_convention) (attr->constant_value (0));
16017 }
16018
16019 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16020 if (attr != nullptr)
16021 {
16022 if (attr->form_is_constant ())
16023 TYPE_LENGTH (type) = attr->constant_value (0);
16024 else
16025 {
16026 struct dynamic_prop prop;
16027 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
16028 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
16029 TYPE_LENGTH (type) = 0;
16030 }
16031 }
16032 else
16033 {
16034 TYPE_LENGTH (type) = 0;
16035 }
16036
16037 maybe_set_alignment (cu, die, type);
16038
16039 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
16040 {
16041 /* ICC<14 does not output the required DW_AT_declaration on
16042 incomplete types, but gives them a size of zero. */
16043 type->set_is_stub (true);
16044 }
16045 else
16046 type->set_stub_is_supported (true);
16047
16048 if (die_is_declaration (die, cu))
16049 type->set_is_stub (true);
16050 else if (attr == NULL && die->child == NULL
16051 && producer_is_realview (cu->producer))
16052 /* RealView does not output the required DW_AT_declaration
16053 on incomplete types. */
16054 type->set_is_stub (true);
16055
16056 /* We need to add the type field to the die immediately so we don't
16057 infinitely recurse when dealing with pointers to the structure
16058 type within the structure itself. */
16059 set_die_type (die, type, cu);
16060
16061 /* set_die_type should be already done. */
16062 set_descriptive_type (type, die, cu);
16063
16064 return type;
16065 }
16066
16067 static void handle_struct_member_die
16068 (struct die_info *child_die,
16069 struct type *type,
16070 struct field_info *fi,
16071 std::vector<struct symbol *> *template_args,
16072 struct dwarf2_cu *cu);
16073
16074 /* A helper for handle_struct_member_die that handles
16075 DW_TAG_variant_part. */
16076
16077 static void
16078 handle_variant_part (struct die_info *die, struct type *type,
16079 struct field_info *fi,
16080 std::vector<struct symbol *> *template_args,
16081 struct dwarf2_cu *cu)
16082 {
16083 variant_part_builder *new_part;
16084 if (fi->current_variant_part == nullptr)
16085 {
16086 fi->variant_parts.emplace_back ();
16087 new_part = &fi->variant_parts.back ();
16088 }
16089 else if (!fi->current_variant_part->processing_variant)
16090 {
16091 complaint (_("nested DW_TAG_variant_part seen "
16092 "- DIE at %s [in module %s]"),
16093 sect_offset_str (die->sect_off),
16094 objfile_name (cu->per_objfile->objfile));
16095 return;
16096 }
16097 else
16098 {
16099 variant_field &current = fi->current_variant_part->variants.back ();
16100 current.variant_parts.emplace_back ();
16101 new_part = &current.variant_parts.back ();
16102 }
16103
16104 /* When we recurse, we want callees to add to this new variant
16105 part. */
16106 scoped_restore save_current_variant_part
16107 = make_scoped_restore (&fi->current_variant_part, new_part);
16108
16109 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16110 if (discr == NULL)
16111 {
16112 /* It's a univariant form, an extension we support. */
16113 }
16114 else if (discr->form_is_ref ())
16115 {
16116 struct dwarf2_cu *target_cu = cu;
16117 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16118
16119 new_part->discriminant_offset = target_die->sect_off;
16120 }
16121 else
16122 {
16123 complaint (_("DW_AT_discr does not have DIE reference form"
16124 " - DIE at %s [in module %s]"),
16125 sect_offset_str (die->sect_off),
16126 objfile_name (cu->per_objfile->objfile));
16127 }
16128
16129 for (die_info *child_die = die->child;
16130 child_die != NULL;
16131 child_die = child_die->sibling)
16132 handle_struct_member_die (child_die, type, fi, template_args, cu);
16133 }
16134
16135 /* A helper for handle_struct_member_die that handles
16136 DW_TAG_variant. */
16137
16138 static void
16139 handle_variant (struct die_info *die, struct type *type,
16140 struct field_info *fi,
16141 std::vector<struct symbol *> *template_args,
16142 struct dwarf2_cu *cu)
16143 {
16144 if (fi->current_variant_part == nullptr)
16145 {
16146 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
16147 "- DIE at %s [in module %s]"),
16148 sect_offset_str (die->sect_off),
16149 objfile_name (cu->per_objfile->objfile));
16150 return;
16151 }
16152 if (fi->current_variant_part->processing_variant)
16153 {
16154 complaint (_("nested DW_TAG_variant seen "
16155 "- DIE at %s [in module %s]"),
16156 sect_offset_str (die->sect_off),
16157 objfile_name (cu->per_objfile->objfile));
16158 return;
16159 }
16160
16161 scoped_restore save_processing_variant
16162 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16163 true);
16164
16165 fi->current_variant_part->variants.emplace_back ();
16166 variant_field &variant = fi->current_variant_part->variants.back ();
16167 variant.first_field = fi->fields.size ();
16168
16169 /* In a variant we want to get the discriminant and also add a
16170 field for our sole member child. */
16171 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
16172 if (discr == nullptr || !discr->form_is_constant ())
16173 {
16174 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
16175 if (discr == nullptr || discr->as_block ()->size == 0)
16176 variant.default_branch = true;
16177 else
16178 variant.discr_list_data = discr->as_block ();
16179 }
16180 else
16181 variant.discriminant_value = discr->constant_value (0);
16182
16183 for (die_info *variant_child = die->child;
16184 variant_child != NULL;
16185 variant_child = variant_child->sibling)
16186 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16187
16188 variant.last_field = fi->fields.size ();
16189 }
16190
16191 /* A helper for process_structure_scope that handles a single member
16192 DIE. */
16193
16194 static void
16195 handle_struct_member_die (struct die_info *child_die, struct type *type,
16196 struct field_info *fi,
16197 std::vector<struct symbol *> *template_args,
16198 struct dwarf2_cu *cu)
16199 {
16200 if (child_die->tag == DW_TAG_member
16201 || child_die->tag == DW_TAG_variable)
16202 {
16203 /* NOTE: carlton/2002-11-05: A C++ static data member
16204 should be a DW_TAG_member that is a declaration, but
16205 all versions of G++ as of this writing (so through at
16206 least 3.2.1) incorrectly generate DW_TAG_variable
16207 tags for them instead. */
16208 dwarf2_add_field (fi, child_die, cu);
16209 }
16210 else if (child_die->tag == DW_TAG_subprogram)
16211 {
16212 /* Rust doesn't have member functions in the C++ sense.
16213 However, it does emit ordinary functions as children
16214 of a struct DIE. */
16215 if (cu->language == language_rust)
16216 read_func_scope (child_die, cu);
16217 else
16218 {
16219 /* C++ member function. */
16220 dwarf2_add_member_fn (fi, child_die, type, cu);
16221 }
16222 }
16223 else if (child_die->tag == DW_TAG_inheritance)
16224 {
16225 /* C++ base class field. */
16226 dwarf2_add_field (fi, child_die, cu);
16227 }
16228 else if (type_can_define_types (child_die))
16229 dwarf2_add_type_defn (fi, child_die, cu);
16230 else if (child_die->tag == DW_TAG_template_type_param
16231 || child_die->tag == DW_TAG_template_value_param)
16232 {
16233 struct symbol *arg = new_symbol (child_die, NULL, cu);
16234
16235 if (arg != NULL)
16236 template_args->push_back (arg);
16237 }
16238 else if (child_die->tag == DW_TAG_variant_part)
16239 handle_variant_part (child_die, type, fi, template_args, cu);
16240 else if (child_die->tag == DW_TAG_variant)
16241 handle_variant (child_die, type, fi, template_args, cu);
16242 }
16243
16244 /* Finish creating a structure or union type, including filling in
16245 its members and creating a symbol for it. */
16246
16247 static void
16248 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16249 {
16250 struct objfile *objfile = cu->per_objfile->objfile;
16251 struct die_info *child_die;
16252 struct type *type;
16253
16254 type = get_die_type (die, cu);
16255 if (type == NULL)
16256 type = read_structure_type (die, cu);
16257
16258 bool has_template_parameters = false;
16259 if (die->child != NULL && ! die_is_declaration (die, cu))
16260 {
16261 struct field_info fi;
16262 std::vector<struct symbol *> template_args;
16263
16264 child_die = die->child;
16265
16266 while (child_die && child_die->tag)
16267 {
16268 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16269 child_die = child_die->sibling;
16270 }
16271
16272 /* Attach template arguments to type. */
16273 if (!template_args.empty ())
16274 {
16275 has_template_parameters = true;
16276 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16277 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16278 TYPE_TEMPLATE_ARGUMENTS (type)
16279 = XOBNEWVEC (&objfile->objfile_obstack,
16280 struct symbol *,
16281 TYPE_N_TEMPLATE_ARGUMENTS (type));
16282 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16283 template_args.data (),
16284 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16285 * sizeof (struct symbol *)));
16286 }
16287
16288 /* Attach fields and member functions to the type. */
16289 if (fi.nfields () > 0)
16290 dwarf2_attach_fields_to_type (&fi, type, cu);
16291 if (!fi.fnfieldlists.empty ())
16292 {
16293 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16294
16295 /* Get the type which refers to the base class (possibly this
16296 class itself) which contains the vtable pointer for the current
16297 class from the DW_AT_containing_type attribute. This use of
16298 DW_AT_containing_type is a GNU extension. */
16299
16300 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16301 {
16302 struct type *t = die_containing_type (die, cu);
16303
16304 set_type_vptr_basetype (type, t);
16305 if (type == t)
16306 {
16307 int i;
16308
16309 /* Our own class provides vtbl ptr. */
16310 for (i = t->num_fields () - 1;
16311 i >= TYPE_N_BASECLASSES (t);
16312 --i)
16313 {
16314 const char *fieldname = TYPE_FIELD_NAME (t, i);
16315
16316 if (is_vtable_name (fieldname, cu))
16317 {
16318 set_type_vptr_fieldno (type, i);
16319 break;
16320 }
16321 }
16322
16323 /* Complain if virtual function table field not found. */
16324 if (i < TYPE_N_BASECLASSES (t))
16325 complaint (_("virtual function table pointer "
16326 "not found when defining class '%s'"),
16327 type->name () ? type->name () : "");
16328 }
16329 else
16330 {
16331 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16332 }
16333 }
16334 else if (cu->producer
16335 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16336 {
16337 /* The IBM XLC compiler does not provide direct indication
16338 of the containing type, but the vtable pointer is
16339 always named __vfp. */
16340
16341 int i;
16342
16343 for (i = type->num_fields () - 1;
16344 i >= TYPE_N_BASECLASSES (type);
16345 --i)
16346 {
16347 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16348 {
16349 set_type_vptr_fieldno (type, i);
16350 set_type_vptr_basetype (type, type);
16351 break;
16352 }
16353 }
16354 }
16355 }
16356
16357 /* Copy fi.typedef_field_list linked list elements content into the
16358 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16359 if (!fi.typedef_field_list.empty ())
16360 {
16361 int count = fi.typedef_field_list.size ();
16362
16363 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16364 TYPE_TYPEDEF_FIELD_ARRAY (type)
16365 = ((struct decl_field *)
16366 TYPE_ALLOC (type,
16367 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16368 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16369
16370 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16371 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16372 }
16373
16374 /* Copy fi.nested_types_list linked list elements content into the
16375 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16376 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16377 {
16378 int count = fi.nested_types_list.size ();
16379
16380 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16381 TYPE_NESTED_TYPES_ARRAY (type)
16382 = ((struct decl_field *)
16383 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16384 TYPE_NESTED_TYPES_COUNT (type) = count;
16385
16386 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16387 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16388 }
16389 }
16390
16391 quirk_gcc_member_function_pointer (type, objfile);
16392 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16393 cu->rust_unions.push_back (type);
16394 else if (cu->language == language_ada)
16395 quirk_ada_thick_pointer_struct (die, cu, type);
16396
16397 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16398 snapshots) has been known to create a die giving a declaration
16399 for a class that has, as a child, a die giving a definition for a
16400 nested class. So we have to process our children even if the
16401 current die is a declaration. Normally, of course, a declaration
16402 won't have any children at all. */
16403
16404 child_die = die->child;
16405
16406 while (child_die != NULL && child_die->tag)
16407 {
16408 if (child_die->tag == DW_TAG_member
16409 || child_die->tag == DW_TAG_variable
16410 || child_die->tag == DW_TAG_inheritance
16411 || child_die->tag == DW_TAG_template_value_param
16412 || child_die->tag == DW_TAG_template_type_param)
16413 {
16414 /* Do nothing. */
16415 }
16416 else
16417 process_die (child_die, cu);
16418
16419 child_die = child_die->sibling;
16420 }
16421
16422 /* Do not consider external references. According to the DWARF standard,
16423 these DIEs are identified by the fact that they have no byte_size
16424 attribute, and a declaration attribute. */
16425 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16426 || !die_is_declaration (die, cu)
16427 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16428 {
16429 struct symbol *sym = new_symbol (die, type, cu);
16430
16431 if (has_template_parameters)
16432 {
16433 struct symtab *symtab;
16434 if (sym != nullptr)
16435 symtab = symbol_symtab (sym);
16436 else if (cu->line_header != nullptr)
16437 {
16438 /* Any related symtab will do. */
16439 symtab
16440 = cu->line_header->file_names ()[0].symtab;
16441 }
16442 else
16443 {
16444 symtab = nullptr;
16445 complaint (_("could not find suitable "
16446 "symtab for template parameter"
16447 " - DIE at %s [in module %s]"),
16448 sect_offset_str (die->sect_off),
16449 objfile_name (objfile));
16450 }
16451
16452 if (symtab != nullptr)
16453 {
16454 /* Make sure that the symtab is set on the new symbols.
16455 Even though they don't appear in this symtab directly,
16456 other parts of gdb assume that symbols do, and this is
16457 reasonably true. */
16458 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16459 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16460 }
16461 }
16462 }
16463 }
16464
16465 /* Assuming DIE is an enumeration type, and TYPE is its associated
16466 type, update TYPE using some information only available in DIE's
16467 children. In particular, the fields are computed. */
16468
16469 static void
16470 update_enumeration_type_from_children (struct die_info *die,
16471 struct type *type,
16472 struct dwarf2_cu *cu)
16473 {
16474 struct die_info *child_die;
16475 int unsigned_enum = 1;
16476 int flag_enum = 1;
16477
16478 auto_obstack obstack;
16479 std::vector<struct field> fields;
16480
16481 for (child_die = die->child;
16482 child_die != NULL && child_die->tag;
16483 child_die = child_die->sibling)
16484 {
16485 struct attribute *attr;
16486 LONGEST value;
16487 const gdb_byte *bytes;
16488 struct dwarf2_locexpr_baton *baton;
16489 const char *name;
16490
16491 if (child_die->tag != DW_TAG_enumerator)
16492 continue;
16493
16494 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16495 if (attr == NULL)
16496 continue;
16497
16498 name = dwarf2_name (child_die, cu);
16499 if (name == NULL)
16500 name = "<anonymous enumerator>";
16501
16502 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16503 &value, &bytes, &baton);
16504 if (value < 0)
16505 {
16506 unsigned_enum = 0;
16507 flag_enum = 0;
16508 }
16509 else
16510 {
16511 if (count_one_bits_ll (value) >= 2)
16512 flag_enum = 0;
16513 }
16514
16515 fields.emplace_back ();
16516 struct field &field = fields.back ();
16517 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16518 SET_FIELD_ENUMVAL (field, value);
16519 }
16520
16521 if (!fields.empty ())
16522 {
16523 type->set_num_fields (fields.size ());
16524 type->set_fields
16525 ((struct field *)
16526 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16527 memcpy (type->fields (), fields.data (),
16528 sizeof (struct field) * fields.size ());
16529 }
16530
16531 if (unsigned_enum)
16532 type->set_is_unsigned (true);
16533
16534 if (flag_enum)
16535 TYPE_FLAG_ENUM (type) = 1;
16536 }
16537
16538 /* Given a DW_AT_enumeration_type die, set its type. We do not
16539 complete the type's fields yet, or create any symbols. */
16540
16541 static struct type *
16542 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16543 {
16544 struct objfile *objfile = cu->per_objfile->objfile;
16545 struct type *type;
16546 struct attribute *attr;
16547 const char *name;
16548
16549 /* If the definition of this type lives in .debug_types, read that type.
16550 Don't follow DW_AT_specification though, that will take us back up
16551 the chain and we want to go down. */
16552 attr = die->attr (DW_AT_signature);
16553 if (attr != nullptr)
16554 {
16555 type = get_DW_AT_signature_type (die, attr, cu);
16556
16557 /* The type's CU may not be the same as CU.
16558 Ensure TYPE is recorded with CU in die_type_hash. */
16559 return set_die_type (die, type, cu);
16560 }
16561
16562 type = alloc_type (objfile);
16563
16564 type->set_code (TYPE_CODE_ENUM);
16565 name = dwarf2_full_name (NULL, die, cu);
16566 if (name != NULL)
16567 type->set_name (name);
16568
16569 attr = dwarf2_attr (die, DW_AT_type, cu);
16570 if (attr != NULL)
16571 {
16572 struct type *underlying_type = die_type (die, cu);
16573
16574 TYPE_TARGET_TYPE (type) = underlying_type;
16575 }
16576
16577 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16578 if (attr != nullptr)
16579 {
16580 TYPE_LENGTH (type) = attr->constant_value (0);
16581 }
16582 else
16583 {
16584 TYPE_LENGTH (type) = 0;
16585 }
16586
16587 maybe_set_alignment (cu, die, type);
16588
16589 /* The enumeration DIE can be incomplete. In Ada, any type can be
16590 declared as private in the package spec, and then defined only
16591 inside the package body. Such types are known as Taft Amendment
16592 Types. When another package uses such a type, an incomplete DIE
16593 may be generated by the compiler. */
16594 if (die_is_declaration (die, cu))
16595 type->set_is_stub (true);
16596
16597 /* If this type has an underlying type that is not a stub, then we
16598 may use its attributes. We always use the "unsigned" attribute
16599 in this situation, because ordinarily we guess whether the type
16600 is unsigned -- but the guess can be wrong and the underlying type
16601 can tell us the reality. However, we defer to a local size
16602 attribute if one exists, because this lets the compiler override
16603 the underlying type if needed. */
16604 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16605 {
16606 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16607 underlying_type = check_typedef (underlying_type);
16608
16609 type->set_is_unsigned (underlying_type->is_unsigned ());
16610
16611 if (TYPE_LENGTH (type) == 0)
16612 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16613
16614 if (TYPE_RAW_ALIGN (type) == 0
16615 && TYPE_RAW_ALIGN (underlying_type) != 0)
16616 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16617 }
16618
16619 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16620
16621 set_die_type (die, type, cu);
16622
16623 /* Finish the creation of this type by using the enum's children.
16624 Note that, as usual, this must come after set_die_type to avoid
16625 infinite recursion when trying to compute the names of the
16626 enumerators. */
16627 update_enumeration_type_from_children (die, type, cu);
16628
16629 return type;
16630 }
16631
16632 /* Given a pointer to a die which begins an enumeration, process all
16633 the dies that define the members of the enumeration, and create the
16634 symbol for the enumeration type.
16635
16636 NOTE: We reverse the order of the element list. */
16637
16638 static void
16639 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16640 {
16641 struct type *this_type;
16642
16643 this_type = get_die_type (die, cu);
16644 if (this_type == NULL)
16645 this_type = read_enumeration_type (die, cu);
16646
16647 if (die->child != NULL)
16648 {
16649 struct die_info *child_die;
16650 const char *name;
16651
16652 child_die = die->child;
16653 while (child_die && child_die->tag)
16654 {
16655 if (child_die->tag != DW_TAG_enumerator)
16656 {
16657 process_die (child_die, cu);
16658 }
16659 else
16660 {
16661 name = dwarf2_name (child_die, cu);
16662 if (name)
16663 new_symbol (child_die, this_type, cu);
16664 }
16665
16666 child_die = child_die->sibling;
16667 }
16668 }
16669
16670 /* If we are reading an enum from a .debug_types unit, and the enum
16671 is a declaration, and the enum is not the signatured type in the
16672 unit, then we do not want to add a symbol for it. Adding a
16673 symbol would in some cases obscure the true definition of the
16674 enum, giving users an incomplete type when the definition is
16675 actually available. Note that we do not want to do this for all
16676 enums which are just declarations, because C++0x allows forward
16677 enum declarations. */
16678 if (cu->per_cu->is_debug_types
16679 && die_is_declaration (die, cu))
16680 {
16681 struct signatured_type *sig_type;
16682
16683 sig_type = (struct signatured_type *) cu->per_cu;
16684 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16685 if (sig_type->type_offset_in_section != die->sect_off)
16686 return;
16687 }
16688
16689 new_symbol (die, this_type, cu);
16690 }
16691
16692 /* Helper function for quirk_ada_thick_pointer that examines a bounds
16693 expression for an index type and finds the corresponding field
16694 offset in the hidden "P_BOUNDS" structure. Returns true on success
16695 and updates *FIELD, false if it fails to recognize an
16696 expression. */
16697
16698 static bool
16699 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16700 int *bounds_offset, struct field *field,
16701 struct dwarf2_cu *cu)
16702 {
16703 struct attribute *attr = dwarf2_attr (die, name, cu);
16704 if (attr == nullptr || !attr->form_is_block ())
16705 return false;
16706
16707 const struct dwarf_block *block = attr->as_block ();
16708 const gdb_byte *start = block->data;
16709 const gdb_byte *end = block->data + block->size;
16710
16711 /* The expression to recognize generally looks like:
16712
16713 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16714 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16715
16716 However, the second "plus_uconst" may be missing:
16717
16718 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16719 DW_OP_deref_size: 4)
16720
16721 This happens when the field is at the start of the structure.
16722
16723 Also, the final deref may not be sized:
16724
16725 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16726 DW_OP_deref)
16727
16728 This happens when the size of the index type happens to be the
16729 same as the architecture's word size. This can occur with or
16730 without the second plus_uconst. */
16731
16732 if (end - start < 2)
16733 return false;
16734 if (*start++ != DW_OP_push_object_address)
16735 return false;
16736 if (*start++ != DW_OP_plus_uconst)
16737 return false;
16738
16739 uint64_t this_bound_off;
16740 start = gdb_read_uleb128 (start, end, &this_bound_off);
16741 if (start == nullptr || (int) this_bound_off != this_bound_off)
16742 return false;
16743 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16744 is consistent among all bounds. */
16745 if (*bounds_offset == -1)
16746 *bounds_offset = this_bound_off;
16747 else if (*bounds_offset != this_bound_off)
16748 return false;
16749
16750 if (start == end || *start++ != DW_OP_deref)
16751 return false;
16752
16753 int offset = 0;
16754 if (start ==end)
16755 return false;
16756 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16757 {
16758 /* This means an offset of 0. */
16759 }
16760 else if (*start++ != DW_OP_plus_uconst)
16761 return false;
16762 else
16763 {
16764 /* The size is the parameter to DW_OP_plus_uconst. */
16765 uint64_t val;
16766 start = gdb_read_uleb128 (start, end, &val);
16767 if (start == nullptr)
16768 return false;
16769 if ((int) val != val)
16770 return false;
16771 offset = val;
16772 }
16773
16774 if (start == end)
16775 return false;
16776
16777 uint64_t size;
16778 if (*start == DW_OP_deref_size)
16779 {
16780 start = gdb_read_uleb128 (start + 1, end, &size);
16781 if (start == nullptr)
16782 return false;
16783 }
16784 else if (*start == DW_OP_deref)
16785 {
16786 size = cu->header.addr_size;
16787 ++start;
16788 }
16789 else
16790 return false;
16791
16792 SET_FIELD_BITPOS (*field, 8 * offset);
16793 if (size != TYPE_LENGTH (field->type ()))
16794 FIELD_BITSIZE (*field) = 8 * size;
16795
16796 return true;
16797 }
16798
16799 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16800 some kinds of Ada arrays:
16801
16802 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16803 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16804 <11e0> DW_AT_data_location: 2 byte block: 97 6
16805 (DW_OP_push_object_address; DW_OP_deref)
16806 <11e3> DW_AT_type : <0x1173>
16807 <11e7> DW_AT_sibling : <0x1201>
16808 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16809 <11ec> DW_AT_type : <0x1206>
16810 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16811 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16812 DW_OP_deref_size: 4)
16813 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16814 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16815 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16816
16817 This actually represents a "thick pointer", which is a structure
16818 with two elements: one that is a pointer to the array data, and one
16819 that is a pointer to another structure; this second structure holds
16820 the array bounds.
16821
16822 This returns a new type on success, or nullptr if this didn't
16823 recognize the type. */
16824
16825 static struct type *
16826 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16827 struct type *type)
16828 {
16829 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16830 /* So far we've only seen this with block form. */
16831 if (attr == nullptr || !attr->form_is_block ())
16832 return nullptr;
16833
16834 /* Note that this will fail if the structure layout is changed by
16835 the compiler. However, we have no good way to recognize some
16836 other layout, because we don't know what expression the compiler
16837 might choose to emit should this happen. */
16838 struct dwarf_block *blk = attr->as_block ();
16839 if (blk->size != 2
16840 || blk->data[0] != DW_OP_push_object_address
16841 || blk->data[1] != DW_OP_deref)
16842 return nullptr;
16843
16844 int bounds_offset = -1;
16845 int max_align = -1;
16846 std::vector<struct field> range_fields;
16847 for (struct die_info *child_die = die->child;
16848 child_die;
16849 child_die = child_die->sibling)
16850 {
16851 if (child_die->tag == DW_TAG_subrange_type)
16852 {
16853 struct type *underlying = read_subrange_index_type (child_die, cu);
16854
16855 int this_align = type_align (underlying);
16856 if (this_align > max_align)
16857 max_align = this_align;
16858
16859 range_fields.emplace_back ();
16860 range_fields.emplace_back ();
16861
16862 struct field &lower = range_fields[range_fields.size () - 2];
16863 struct field &upper = range_fields[range_fields.size () - 1];
16864
16865 lower.set_type (underlying);
16866 FIELD_ARTIFICIAL (lower) = 1;
16867
16868 upper.set_type (underlying);
16869 FIELD_ARTIFICIAL (upper) = 1;
16870
16871 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16872 &bounds_offset, &lower, cu)
16873 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16874 &bounds_offset, &upper, cu))
16875 return nullptr;
16876 }
16877 }
16878
16879 /* This shouldn't really happen, but double-check that we found
16880 where the bounds are stored. */
16881 if (bounds_offset == -1)
16882 return nullptr;
16883
16884 struct objfile *objfile = cu->per_objfile->objfile;
16885 for (int i = 0; i < range_fields.size (); i += 2)
16886 {
16887 char name[20];
16888
16889 /* Set the name of each field in the bounds. */
16890 xsnprintf (name, sizeof (name), "LB%d", i / 2);
16891 FIELD_NAME (range_fields[i]) = objfile->intern (name);
16892 xsnprintf (name, sizeof (name), "UB%d", i / 2);
16893 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
16894 }
16895
16896 struct type *bounds = alloc_type (objfile);
16897 bounds->set_code (TYPE_CODE_STRUCT);
16898
16899 bounds->set_num_fields (range_fields.size ());
16900 bounds->set_fields
16901 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
16902 * sizeof (struct field))));
16903 memcpy (bounds->fields (), range_fields.data (),
16904 bounds->num_fields () * sizeof (struct field));
16905
16906 int last_fieldno = range_fields.size () - 1;
16907 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
16908 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
16909 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
16910
16911 /* Rewrite the existing array type in place. Specifically, we
16912 remove any dynamic properties we might have read, and we replace
16913 the index types. */
16914 struct type *iter = type;
16915 for (int i = 0; i < range_fields.size (); i += 2)
16916 {
16917 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
16918 iter->main_type->dyn_prop_list = nullptr;
16919 iter->set_index_type
16920 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
16921 iter = TYPE_TARGET_TYPE (iter);
16922 }
16923
16924 struct type *result = alloc_type (objfile);
16925 result->set_code (TYPE_CODE_STRUCT);
16926
16927 result->set_num_fields (2);
16928 result->set_fields
16929 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
16930 * sizeof (struct field))));
16931
16932 /* The names are chosen to coincide with what the compiler does with
16933 -fgnat-encodings=all, which the Ada code in gdb already
16934 understands. */
16935 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
16936 result->field (0).set_type (lookup_pointer_type (type));
16937
16938 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
16939 result->field (1).set_type (lookup_pointer_type (bounds));
16940 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
16941
16942 result->set_name (type->name ());
16943 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
16944 + TYPE_LENGTH (result->field (1).type ()));
16945
16946 return result;
16947 }
16948
16949 /* Extract all information from a DW_TAG_array_type DIE and put it in
16950 the DIE's type field. For now, this only handles one dimensional
16951 arrays. */
16952
16953 static struct type *
16954 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16955 {
16956 struct objfile *objfile = cu->per_objfile->objfile;
16957 struct die_info *child_die;
16958 struct type *type;
16959 struct type *element_type, *range_type, *index_type;
16960 struct attribute *attr;
16961 const char *name;
16962 struct dynamic_prop *byte_stride_prop = NULL;
16963 unsigned int bit_stride = 0;
16964
16965 element_type = die_type (die, cu);
16966
16967 /* The die_type call above may have already set the type for this DIE. */
16968 type = get_die_type (die, cu);
16969 if (type)
16970 return type;
16971
16972 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16973 if (attr != NULL)
16974 {
16975 int stride_ok;
16976 struct type *prop_type = cu->addr_sized_int_type (false);
16977
16978 byte_stride_prop
16979 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16980 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16981 prop_type);
16982 if (!stride_ok)
16983 {
16984 complaint (_("unable to read array DW_AT_byte_stride "
16985 " - DIE at %s [in module %s]"),
16986 sect_offset_str (die->sect_off),
16987 objfile_name (cu->per_objfile->objfile));
16988 /* Ignore this attribute. We will likely not be able to print
16989 arrays of this type correctly, but there is little we can do
16990 to help if we cannot read the attribute's value. */
16991 byte_stride_prop = NULL;
16992 }
16993 }
16994
16995 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16996 if (attr != NULL)
16997 bit_stride = attr->constant_value (0);
16998
16999 /* Irix 6.2 native cc creates array types without children for
17000 arrays with unspecified length. */
17001 if (die->child == NULL)
17002 {
17003 index_type = objfile_type (objfile)->builtin_int;
17004 range_type = create_static_range_type (NULL, index_type, 0, -1);
17005 type = create_array_type_with_stride (NULL, element_type, range_type,
17006 byte_stride_prop, bit_stride);
17007 return set_die_type (die, type, cu);
17008 }
17009
17010 std::vector<struct type *> range_types;
17011 child_die = die->child;
17012 while (child_die && child_die->tag)
17013 {
17014 if (child_die->tag == DW_TAG_subrange_type)
17015 {
17016 struct type *child_type = read_type_die (child_die, cu);
17017
17018 if (child_type != NULL)
17019 {
17020 /* The range type was succesfully read. Save it for the
17021 array type creation. */
17022 range_types.push_back (child_type);
17023 }
17024 }
17025 child_die = child_die->sibling;
17026 }
17027
17028 if (range_types.empty ())
17029 {
17030 complaint (_("unable to find array range - DIE at %s [in module %s]"),
17031 sect_offset_str (die->sect_off),
17032 objfile_name (cu->per_objfile->objfile));
17033 return NULL;
17034 }
17035
17036 /* Dwarf2 dimensions are output from left to right, create the
17037 necessary array types in backwards order. */
17038
17039 type = element_type;
17040
17041 if (read_array_order (die, cu) == DW_ORD_col_major)
17042 {
17043 int i = 0;
17044
17045 while (i < range_types.size ())
17046 {
17047 type = create_array_type_with_stride (NULL, type, range_types[i++],
17048 byte_stride_prop, bit_stride);
17049 bit_stride = 0;
17050 byte_stride_prop = nullptr;
17051 }
17052 }
17053 else
17054 {
17055 size_t ndim = range_types.size ();
17056 while (ndim-- > 0)
17057 {
17058 type = create_array_type_with_stride (NULL, type, range_types[ndim],
17059 byte_stride_prop, bit_stride);
17060 bit_stride = 0;
17061 byte_stride_prop = nullptr;
17062 }
17063 }
17064
17065 gdb_assert (type != element_type);
17066
17067 /* Understand Dwarf2 support for vector types (like they occur on
17068 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
17069 array type. This is not part of the Dwarf2/3 standard yet, but a
17070 custom vendor extension. The main difference between a regular
17071 array and the vector variant is that vectors are passed by value
17072 to functions. */
17073 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
17074 if (attr != nullptr)
17075 make_vector_type (type);
17076
17077 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
17078 implementation may choose to implement triple vectors using this
17079 attribute. */
17080 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17081 if (attr != nullptr && attr->form_is_unsigned ())
17082 {
17083 if (attr->as_unsigned () >= TYPE_LENGTH (type))
17084 TYPE_LENGTH (type) = attr->as_unsigned ();
17085 else
17086 complaint (_("DW_AT_byte_size for array type smaller "
17087 "than the total size of elements"));
17088 }
17089
17090 name = dwarf2_name (die, cu);
17091 if (name)
17092 type->set_name (name);
17093
17094 maybe_set_alignment (cu, die, type);
17095
17096 struct type *replacement_type = nullptr;
17097 if (cu->language == language_ada)
17098 {
17099 replacement_type = quirk_ada_thick_pointer (die, cu, type);
17100 if (replacement_type != nullptr)
17101 type = replacement_type;
17102 }
17103
17104 /* Install the type in the die. */
17105 set_die_type (die, type, cu, replacement_type != nullptr);
17106
17107 /* set_die_type should be already done. */
17108 set_descriptive_type (type, die, cu);
17109
17110 return type;
17111 }
17112
17113 static enum dwarf_array_dim_ordering
17114 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
17115 {
17116 struct attribute *attr;
17117
17118 attr = dwarf2_attr (die, DW_AT_ordering, cu);
17119
17120 if (attr != nullptr)
17121 {
17122 LONGEST val = attr->constant_value (-1);
17123 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
17124 return (enum dwarf_array_dim_ordering) val;
17125 }
17126
17127 /* GNU F77 is a special case, as at 08/2004 array type info is the
17128 opposite order to the dwarf2 specification, but data is still
17129 laid out as per normal fortran.
17130
17131 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
17132 version checking. */
17133
17134 if (cu->language == language_fortran
17135 && cu->producer && strstr (cu->producer, "GNU F77"))
17136 {
17137 return DW_ORD_row_major;
17138 }
17139
17140 switch (cu->language_defn->array_ordering ())
17141 {
17142 case array_column_major:
17143 return DW_ORD_col_major;
17144 case array_row_major:
17145 default:
17146 return DW_ORD_row_major;
17147 };
17148 }
17149
17150 /* Extract all information from a DW_TAG_set_type DIE and put it in
17151 the DIE's type field. */
17152
17153 static struct type *
17154 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
17155 {
17156 struct type *domain_type, *set_type;
17157 struct attribute *attr;
17158
17159 domain_type = die_type (die, cu);
17160
17161 /* The die_type call above may have already set the type for this DIE. */
17162 set_type = get_die_type (die, cu);
17163 if (set_type)
17164 return set_type;
17165
17166 set_type = create_set_type (NULL, domain_type);
17167
17168 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17169 if (attr != nullptr && attr->form_is_unsigned ())
17170 TYPE_LENGTH (set_type) = attr->as_unsigned ();
17171
17172 maybe_set_alignment (cu, die, set_type);
17173
17174 return set_die_type (die, set_type, cu);
17175 }
17176
17177 /* A helper for read_common_block that creates a locexpr baton.
17178 SYM is the symbol which we are marking as computed.
17179 COMMON_DIE is the DIE for the common block.
17180 COMMON_LOC is the location expression attribute for the common
17181 block itself.
17182 MEMBER_LOC is the location expression attribute for the particular
17183 member of the common block that we are processing.
17184 CU is the CU from which the above come. */
17185
17186 static void
17187 mark_common_block_symbol_computed (struct symbol *sym,
17188 struct die_info *common_die,
17189 struct attribute *common_loc,
17190 struct attribute *member_loc,
17191 struct dwarf2_cu *cu)
17192 {
17193 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17194 struct objfile *objfile = per_objfile->objfile;
17195 struct dwarf2_locexpr_baton *baton;
17196 gdb_byte *ptr;
17197 unsigned int cu_off;
17198 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
17199 LONGEST offset = 0;
17200
17201 gdb_assert (common_loc && member_loc);
17202 gdb_assert (common_loc->form_is_block ());
17203 gdb_assert (member_loc->form_is_block ()
17204 || member_loc->form_is_constant ());
17205
17206 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
17207 baton->per_objfile = per_objfile;
17208 baton->per_cu = cu->per_cu;
17209 gdb_assert (baton->per_cu);
17210
17211 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17212
17213 if (member_loc->form_is_constant ())
17214 {
17215 offset = member_loc->constant_value (0);
17216 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17217 }
17218 else
17219 baton->size += member_loc->as_block ()->size;
17220
17221 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
17222 baton->data = ptr;
17223
17224 *ptr++ = DW_OP_call4;
17225 cu_off = common_die->sect_off - cu->per_cu->sect_off;
17226 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17227 ptr += 4;
17228
17229 if (member_loc->form_is_constant ())
17230 {
17231 *ptr++ = DW_OP_addr;
17232 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17233 ptr += cu->header.addr_size;
17234 }
17235 else
17236 {
17237 /* We have to copy the data here, because DW_OP_call4 will only
17238 use a DW_AT_location attribute. */
17239 struct dwarf_block *block = member_loc->as_block ();
17240 memcpy (ptr, block->data, block->size);
17241 ptr += block->size;
17242 }
17243
17244 *ptr++ = DW_OP_plus;
17245 gdb_assert (ptr - baton->data == baton->size);
17246
17247 SYMBOL_LOCATION_BATON (sym) = baton;
17248 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
17249 }
17250
17251 /* Create appropriate locally-scoped variables for all the
17252 DW_TAG_common_block entries. Also create a struct common_block
17253 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
17254 is used to separate the common blocks name namespace from regular
17255 variable names. */
17256
17257 static void
17258 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
17259 {
17260 struct attribute *attr;
17261
17262 attr = dwarf2_attr (die, DW_AT_location, cu);
17263 if (attr != nullptr)
17264 {
17265 /* Support the .debug_loc offsets. */
17266 if (attr->form_is_block ())
17267 {
17268 /* Ok. */
17269 }
17270 else if (attr->form_is_section_offset ())
17271 {
17272 dwarf2_complex_location_expr_complaint ();
17273 attr = NULL;
17274 }
17275 else
17276 {
17277 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17278 "common block member");
17279 attr = NULL;
17280 }
17281 }
17282
17283 if (die->child != NULL)
17284 {
17285 struct objfile *objfile = cu->per_objfile->objfile;
17286 struct die_info *child_die;
17287 size_t n_entries = 0, size;
17288 struct common_block *common_block;
17289 struct symbol *sym;
17290
17291 for (child_die = die->child;
17292 child_die && child_die->tag;
17293 child_die = child_die->sibling)
17294 ++n_entries;
17295
17296 size = (sizeof (struct common_block)
17297 + (n_entries - 1) * sizeof (struct symbol *));
17298 common_block
17299 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17300 size);
17301 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17302 common_block->n_entries = 0;
17303
17304 for (child_die = die->child;
17305 child_die && child_die->tag;
17306 child_die = child_die->sibling)
17307 {
17308 /* Create the symbol in the DW_TAG_common_block block in the current
17309 symbol scope. */
17310 sym = new_symbol (child_die, NULL, cu);
17311 if (sym != NULL)
17312 {
17313 struct attribute *member_loc;
17314
17315 common_block->contents[common_block->n_entries++] = sym;
17316
17317 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17318 cu);
17319 if (member_loc)
17320 {
17321 /* GDB has handled this for a long time, but it is
17322 not specified by DWARF. It seems to have been
17323 emitted by gfortran at least as recently as:
17324 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17325 complaint (_("Variable in common block has "
17326 "DW_AT_data_member_location "
17327 "- DIE at %s [in module %s]"),
17328 sect_offset_str (child_die->sect_off),
17329 objfile_name (objfile));
17330
17331 if (member_loc->form_is_section_offset ())
17332 dwarf2_complex_location_expr_complaint ();
17333 else if (member_loc->form_is_constant ()
17334 || member_loc->form_is_block ())
17335 {
17336 if (attr != nullptr)
17337 mark_common_block_symbol_computed (sym, die, attr,
17338 member_loc, cu);
17339 }
17340 else
17341 dwarf2_complex_location_expr_complaint ();
17342 }
17343 }
17344 }
17345
17346 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17347 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
17348 }
17349 }
17350
17351 /* Create a type for a C++ namespace. */
17352
17353 static struct type *
17354 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
17355 {
17356 struct objfile *objfile = cu->per_objfile->objfile;
17357 const char *previous_prefix, *name;
17358 int is_anonymous;
17359 struct type *type;
17360
17361 /* For extensions, reuse the type of the original namespace. */
17362 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17363 {
17364 struct die_info *ext_die;
17365 struct dwarf2_cu *ext_cu = cu;
17366
17367 ext_die = dwarf2_extension (die, &ext_cu);
17368 type = read_type_die (ext_die, ext_cu);
17369
17370 /* EXT_CU may not be the same as CU.
17371 Ensure TYPE is recorded with CU in die_type_hash. */
17372 return set_die_type (die, type, cu);
17373 }
17374
17375 name = namespace_name (die, &is_anonymous, cu);
17376
17377 /* Now build the name of the current namespace. */
17378
17379 previous_prefix = determine_prefix (die, cu);
17380 if (previous_prefix[0] != '\0')
17381 name = typename_concat (&objfile->objfile_obstack,
17382 previous_prefix, name, 0, cu);
17383
17384 /* Create the type. */
17385 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17386
17387 return set_die_type (die, type, cu);
17388 }
17389
17390 /* Read a namespace scope. */
17391
17392 static void
17393 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17394 {
17395 struct objfile *objfile = cu->per_objfile->objfile;
17396 int is_anonymous;
17397
17398 /* Add a symbol associated to this if we haven't seen the namespace
17399 before. Also, add a using directive if it's an anonymous
17400 namespace. */
17401
17402 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17403 {
17404 struct type *type;
17405
17406 type = read_type_die (die, cu);
17407 new_symbol (die, type, cu);
17408
17409 namespace_name (die, &is_anonymous, cu);
17410 if (is_anonymous)
17411 {
17412 const char *previous_prefix = determine_prefix (die, cu);
17413
17414 std::vector<const char *> excludes;
17415 add_using_directive (using_directives (cu),
17416 previous_prefix, type->name (), NULL,
17417 NULL, excludes, 0, &objfile->objfile_obstack);
17418 }
17419 }
17420
17421 if (die->child != NULL)
17422 {
17423 struct die_info *child_die = die->child;
17424
17425 while (child_die && child_die->tag)
17426 {
17427 process_die (child_die, cu);
17428 child_die = child_die->sibling;
17429 }
17430 }
17431 }
17432
17433 /* Read a Fortran module as type. This DIE can be only a declaration used for
17434 imported module. Still we need that type as local Fortran "use ... only"
17435 declaration imports depend on the created type in determine_prefix. */
17436
17437 static struct type *
17438 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17439 {
17440 struct objfile *objfile = cu->per_objfile->objfile;
17441 const char *module_name;
17442 struct type *type;
17443
17444 module_name = dwarf2_name (die, cu);
17445 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17446
17447 return set_die_type (die, type, cu);
17448 }
17449
17450 /* Read a Fortran module. */
17451
17452 static void
17453 read_module (struct die_info *die, struct dwarf2_cu *cu)
17454 {
17455 struct die_info *child_die = die->child;
17456 struct type *type;
17457
17458 type = read_type_die (die, cu);
17459 new_symbol (die, type, cu);
17460
17461 while (child_die && child_die->tag)
17462 {
17463 process_die (child_die, cu);
17464 child_die = child_die->sibling;
17465 }
17466 }
17467
17468 /* Return the name of the namespace represented by DIE. Set
17469 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17470 namespace. */
17471
17472 static const char *
17473 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17474 {
17475 struct die_info *current_die;
17476 const char *name = NULL;
17477
17478 /* Loop through the extensions until we find a name. */
17479
17480 for (current_die = die;
17481 current_die != NULL;
17482 current_die = dwarf2_extension (die, &cu))
17483 {
17484 /* We don't use dwarf2_name here so that we can detect the absence
17485 of a name -> anonymous namespace. */
17486 name = dwarf2_string_attr (die, DW_AT_name, cu);
17487
17488 if (name != NULL)
17489 break;
17490 }
17491
17492 /* Is it an anonymous namespace? */
17493
17494 *is_anonymous = (name == NULL);
17495 if (*is_anonymous)
17496 name = CP_ANONYMOUS_NAMESPACE_STR;
17497
17498 return name;
17499 }
17500
17501 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17502 the user defined type vector. */
17503
17504 static struct type *
17505 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17506 {
17507 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
17508 struct comp_unit_head *cu_header = &cu->header;
17509 struct type *type;
17510 struct attribute *attr_byte_size;
17511 struct attribute *attr_address_class;
17512 int byte_size, addr_class;
17513 struct type *target_type;
17514
17515 target_type = die_type (die, cu);
17516
17517 /* The die_type call above may have already set the type for this DIE. */
17518 type = get_die_type (die, cu);
17519 if (type)
17520 return type;
17521
17522 type = lookup_pointer_type (target_type);
17523
17524 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17525 if (attr_byte_size)
17526 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
17527 else
17528 byte_size = cu_header->addr_size;
17529
17530 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17531 if (attr_address_class)
17532 addr_class = attr_address_class->constant_value (DW_ADDR_none);
17533 else
17534 addr_class = DW_ADDR_none;
17535
17536 ULONGEST alignment = get_alignment (cu, die);
17537
17538 /* If the pointer size, alignment, or address class is different
17539 than the default, create a type variant marked as such and set
17540 the length accordingly. */
17541 if (TYPE_LENGTH (type) != byte_size
17542 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17543 && alignment != TYPE_RAW_ALIGN (type))
17544 || addr_class != DW_ADDR_none)
17545 {
17546 if (gdbarch_address_class_type_flags_p (gdbarch))
17547 {
17548 type_instance_flags type_flags
17549 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17550 addr_class);
17551 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17552 == 0);
17553 type = make_type_with_address_space (type, type_flags);
17554 }
17555 else if (TYPE_LENGTH (type) != byte_size)
17556 {
17557 complaint (_("invalid pointer size %d"), byte_size);
17558 }
17559 else if (TYPE_RAW_ALIGN (type) != alignment)
17560 {
17561 complaint (_("Invalid DW_AT_alignment"
17562 " - DIE at %s [in module %s]"),
17563 sect_offset_str (die->sect_off),
17564 objfile_name (cu->per_objfile->objfile));
17565 }
17566 else
17567 {
17568 /* Should we also complain about unhandled address classes? */
17569 }
17570 }
17571
17572 TYPE_LENGTH (type) = byte_size;
17573 set_type_align (type, alignment);
17574 return set_die_type (die, type, cu);
17575 }
17576
17577 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17578 the user defined type vector. */
17579
17580 static struct type *
17581 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17582 {
17583 struct type *type;
17584 struct type *to_type;
17585 struct type *domain;
17586
17587 to_type = die_type (die, cu);
17588 domain = die_containing_type (die, cu);
17589
17590 /* The calls above may have already set the type for this DIE. */
17591 type = get_die_type (die, cu);
17592 if (type)
17593 return type;
17594
17595 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17596 type = lookup_methodptr_type (to_type);
17597 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17598 {
17599 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17600
17601 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17602 to_type->fields (), to_type->num_fields (),
17603 to_type->has_varargs ());
17604 type = lookup_methodptr_type (new_type);
17605 }
17606 else
17607 type = lookup_memberptr_type (to_type, domain);
17608
17609 return set_die_type (die, type, cu);
17610 }
17611
17612 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17613 the user defined type vector. */
17614
17615 static struct type *
17616 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17617 enum type_code refcode)
17618 {
17619 struct comp_unit_head *cu_header = &cu->header;
17620 struct type *type, *target_type;
17621 struct attribute *attr;
17622
17623 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17624
17625 target_type = die_type (die, cu);
17626
17627 /* The die_type call above may have already set the type for this DIE. */
17628 type = get_die_type (die, cu);
17629 if (type)
17630 return type;
17631
17632 type = lookup_reference_type (target_type, refcode);
17633 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17634 if (attr != nullptr)
17635 {
17636 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
17637 }
17638 else
17639 {
17640 TYPE_LENGTH (type) = cu_header->addr_size;
17641 }
17642 maybe_set_alignment (cu, die, type);
17643 return set_die_type (die, type, cu);
17644 }
17645
17646 /* Add the given cv-qualifiers to the element type of the array. GCC
17647 outputs DWARF type qualifiers that apply to an array, not the
17648 element type. But GDB relies on the array element type to carry
17649 the cv-qualifiers. This mimics section 6.7.3 of the C99
17650 specification. */
17651
17652 static struct type *
17653 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17654 struct type *base_type, int cnst, int voltl)
17655 {
17656 struct type *el_type, *inner_array;
17657
17658 base_type = copy_type (base_type);
17659 inner_array = base_type;
17660
17661 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17662 {
17663 TYPE_TARGET_TYPE (inner_array) =
17664 copy_type (TYPE_TARGET_TYPE (inner_array));
17665 inner_array = TYPE_TARGET_TYPE (inner_array);
17666 }
17667
17668 el_type = TYPE_TARGET_TYPE (inner_array);
17669 cnst |= TYPE_CONST (el_type);
17670 voltl |= TYPE_VOLATILE (el_type);
17671 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17672
17673 return set_die_type (die, base_type, cu);
17674 }
17675
17676 static struct type *
17677 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17678 {
17679 struct type *base_type, *cv_type;
17680
17681 base_type = die_type (die, cu);
17682
17683 /* The die_type call above may have already set the type for this DIE. */
17684 cv_type = get_die_type (die, cu);
17685 if (cv_type)
17686 return cv_type;
17687
17688 /* In case the const qualifier is applied to an array type, the element type
17689 is so qualified, not the array type (section 6.7.3 of C99). */
17690 if (base_type->code () == TYPE_CODE_ARRAY)
17691 return add_array_cv_type (die, cu, base_type, 1, 0);
17692
17693 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17694 return set_die_type (die, cv_type, cu);
17695 }
17696
17697 static struct type *
17698 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17699 {
17700 struct type *base_type, *cv_type;
17701
17702 base_type = die_type (die, cu);
17703
17704 /* The die_type call above may have already set the type for this DIE. */
17705 cv_type = get_die_type (die, cu);
17706 if (cv_type)
17707 return cv_type;
17708
17709 /* In case the volatile qualifier is applied to an array type, the
17710 element type is so qualified, not the array type (section 6.7.3
17711 of C99). */
17712 if (base_type->code () == TYPE_CODE_ARRAY)
17713 return add_array_cv_type (die, cu, base_type, 0, 1);
17714
17715 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17716 return set_die_type (die, cv_type, cu);
17717 }
17718
17719 /* Handle DW_TAG_restrict_type. */
17720
17721 static struct type *
17722 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17723 {
17724 struct type *base_type, *cv_type;
17725
17726 base_type = die_type (die, cu);
17727
17728 /* The die_type call above may have already set the type for this DIE. */
17729 cv_type = get_die_type (die, cu);
17730 if (cv_type)
17731 return cv_type;
17732
17733 cv_type = make_restrict_type (base_type);
17734 return set_die_type (die, cv_type, cu);
17735 }
17736
17737 /* Handle DW_TAG_atomic_type. */
17738
17739 static struct type *
17740 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17741 {
17742 struct type *base_type, *cv_type;
17743
17744 base_type = die_type (die, cu);
17745
17746 /* The die_type call above may have already set the type for this DIE. */
17747 cv_type = get_die_type (die, cu);
17748 if (cv_type)
17749 return cv_type;
17750
17751 cv_type = make_atomic_type (base_type);
17752 return set_die_type (die, cv_type, cu);
17753 }
17754
17755 /* Extract all information from a DW_TAG_string_type DIE and add to
17756 the user defined type vector. It isn't really a user defined type,
17757 but it behaves like one, with other DIE's using an AT_user_def_type
17758 attribute to reference it. */
17759
17760 static struct type *
17761 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17762 {
17763 struct objfile *objfile = cu->per_objfile->objfile;
17764 struct gdbarch *gdbarch = objfile->arch ();
17765 struct type *type, *range_type, *index_type, *char_type;
17766 struct attribute *attr;
17767 struct dynamic_prop prop;
17768 bool length_is_constant = true;
17769 LONGEST length;
17770
17771 /* There are a couple of places where bit sizes might be made use of
17772 when parsing a DW_TAG_string_type, however, no producer that we know
17773 of make use of these. Handling bit sizes that are a multiple of the
17774 byte size is easy enough, but what about other bit sizes? Lets deal
17775 with that problem when we have to. Warn about these attributes being
17776 unsupported, then parse the type and ignore them like we always
17777 have. */
17778 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17779 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17780 {
17781 static bool warning_printed = false;
17782 if (!warning_printed)
17783 {
17784 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17785 "currently supported on DW_TAG_string_type."));
17786 warning_printed = true;
17787 }
17788 }
17789
17790 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17791 if (attr != nullptr && !attr->form_is_constant ())
17792 {
17793 /* The string length describes the location at which the length of
17794 the string can be found. The size of the length field can be
17795 specified with one of the attributes below. */
17796 struct type *prop_type;
17797 struct attribute *len
17798 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17799 if (len == nullptr)
17800 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17801 if (len != nullptr && len->form_is_constant ())
17802 {
17803 /* Pass 0 as the default as we know this attribute is constant
17804 and the default value will not be returned. */
17805 LONGEST sz = len->constant_value (0);
17806 prop_type = cu->per_objfile->int_type (sz, true);
17807 }
17808 else
17809 {
17810 /* If the size is not specified then we assume it is the size of
17811 an address on this target. */
17812 prop_type = cu->addr_sized_int_type (true);
17813 }
17814
17815 /* Convert the attribute into a dynamic property. */
17816 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17817 length = 1;
17818 else
17819 length_is_constant = false;
17820 }
17821 else if (attr != nullptr)
17822 {
17823 /* This DW_AT_string_length just contains the length with no
17824 indirection. There's no need to create a dynamic property in this
17825 case. Pass 0 for the default value as we know it will not be
17826 returned in this case. */
17827 length = attr->constant_value (0);
17828 }
17829 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17830 {
17831 /* We don't currently support non-constant byte sizes for strings. */
17832 length = attr->constant_value (1);
17833 }
17834 else
17835 {
17836 /* Use 1 as a fallback length if we have nothing else. */
17837 length = 1;
17838 }
17839
17840 index_type = objfile_type (objfile)->builtin_int;
17841 if (length_is_constant)
17842 range_type = create_static_range_type (NULL, index_type, 1, length);
17843 else
17844 {
17845 struct dynamic_prop low_bound;
17846
17847 low_bound.set_const_val (1);
17848 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17849 }
17850 char_type = language_string_char_type (cu->language_defn, gdbarch);
17851 type = create_string_type (NULL, char_type, range_type);
17852
17853 return set_die_type (die, type, cu);
17854 }
17855
17856 /* Assuming that DIE corresponds to a function, returns nonzero
17857 if the function is prototyped. */
17858
17859 static int
17860 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17861 {
17862 struct attribute *attr;
17863
17864 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17865 if (attr && attr->as_boolean ())
17866 return 1;
17867
17868 /* The DWARF standard implies that the DW_AT_prototyped attribute
17869 is only meaningful for C, but the concept also extends to other
17870 languages that allow unprototyped functions (Eg: Objective C).
17871 For all other languages, assume that functions are always
17872 prototyped. */
17873 if (cu->language != language_c
17874 && cu->language != language_objc
17875 && cu->language != language_opencl)
17876 return 1;
17877
17878 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17879 prototyped and unprototyped functions; default to prototyped,
17880 since that is more common in modern code (and RealView warns
17881 about unprototyped functions). */
17882 if (producer_is_realview (cu->producer))
17883 return 1;
17884
17885 return 0;
17886 }
17887
17888 /* Handle DIES due to C code like:
17889
17890 struct foo
17891 {
17892 int (*funcp)(int a, long l);
17893 int b;
17894 };
17895
17896 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17897
17898 static struct type *
17899 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17900 {
17901 struct objfile *objfile = cu->per_objfile->objfile;
17902 struct type *type; /* Type that this function returns. */
17903 struct type *ftype; /* Function that returns above type. */
17904 struct attribute *attr;
17905
17906 type = die_type (die, cu);
17907
17908 /* The die_type call above may have already set the type for this DIE. */
17909 ftype = get_die_type (die, cu);
17910 if (ftype)
17911 return ftype;
17912
17913 ftype = lookup_function_type (type);
17914
17915 if (prototyped_function_p (die, cu))
17916 ftype->set_is_prototyped (true);
17917
17918 /* Store the calling convention in the type if it's available in
17919 the subroutine die. Otherwise set the calling convention to
17920 the default value DW_CC_normal. */
17921 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17922 if (attr != nullptr
17923 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
17924 TYPE_CALLING_CONVENTION (ftype)
17925 = (enum dwarf_calling_convention) attr->constant_value (0);
17926 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17927 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17928 else
17929 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17930
17931 /* Record whether the function returns normally to its caller or not
17932 if the DWARF producer set that information. */
17933 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17934 if (attr && attr->as_boolean ())
17935 TYPE_NO_RETURN (ftype) = 1;
17936
17937 /* We need to add the subroutine type to the die immediately so
17938 we don't infinitely recurse when dealing with parameters
17939 declared as the same subroutine type. */
17940 set_die_type (die, ftype, cu);
17941
17942 if (die->child != NULL)
17943 {
17944 struct type *void_type = objfile_type (objfile)->builtin_void;
17945 struct die_info *child_die;
17946 int nparams, iparams;
17947
17948 /* Count the number of parameters.
17949 FIXME: GDB currently ignores vararg functions, but knows about
17950 vararg member functions. */
17951 nparams = 0;
17952 child_die = die->child;
17953 while (child_die && child_die->tag)
17954 {
17955 if (child_die->tag == DW_TAG_formal_parameter)
17956 nparams++;
17957 else if (child_die->tag == DW_TAG_unspecified_parameters)
17958 ftype->set_has_varargs (true);
17959
17960 child_die = child_die->sibling;
17961 }
17962
17963 /* Allocate storage for parameters and fill them in. */
17964 ftype->set_num_fields (nparams);
17965 ftype->set_fields
17966 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17967
17968 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17969 even if we error out during the parameters reading below. */
17970 for (iparams = 0; iparams < nparams; iparams++)
17971 ftype->field (iparams).set_type (void_type);
17972
17973 iparams = 0;
17974 child_die = die->child;
17975 while (child_die && child_die->tag)
17976 {
17977 if (child_die->tag == DW_TAG_formal_parameter)
17978 {
17979 struct type *arg_type;
17980
17981 /* DWARF version 2 has no clean way to discern C++
17982 static and non-static member functions. G++ helps
17983 GDB by marking the first parameter for non-static
17984 member functions (which is the this pointer) as
17985 artificial. We pass this information to
17986 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17987
17988 DWARF version 3 added DW_AT_object_pointer, which GCC
17989 4.5 does not yet generate. */
17990 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17991 if (attr != nullptr)
17992 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
17993 else
17994 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17995 arg_type = die_type (child_die, cu);
17996
17997 /* RealView does not mark THIS as const, which the testsuite
17998 expects. GCC marks THIS as const in method definitions,
17999 but not in the class specifications (GCC PR 43053). */
18000 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
18001 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
18002 {
18003 int is_this = 0;
18004 struct dwarf2_cu *arg_cu = cu;
18005 const char *name = dwarf2_name (child_die, cu);
18006
18007 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
18008 if (attr != nullptr)
18009 {
18010 /* If the compiler emits this, use it. */
18011 if (follow_die_ref (die, attr, &arg_cu) == child_die)
18012 is_this = 1;
18013 }
18014 else if (name && strcmp (name, "this") == 0)
18015 /* Function definitions will have the argument names. */
18016 is_this = 1;
18017 else if (name == NULL && iparams == 0)
18018 /* Declarations may not have the names, so like
18019 elsewhere in GDB, assume an artificial first
18020 argument is "this". */
18021 is_this = 1;
18022
18023 if (is_this)
18024 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
18025 arg_type, 0);
18026 }
18027
18028 ftype->field (iparams).set_type (arg_type);
18029 iparams++;
18030 }
18031 child_die = child_die->sibling;
18032 }
18033 }
18034
18035 return ftype;
18036 }
18037
18038 static struct type *
18039 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
18040 {
18041 struct objfile *objfile = cu->per_objfile->objfile;
18042 const char *name = NULL;
18043 struct type *this_type, *target_type;
18044
18045 name = dwarf2_full_name (NULL, die, cu);
18046 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
18047 this_type->set_target_is_stub (true);
18048 set_die_type (die, this_type, cu);
18049 target_type = die_type (die, cu);
18050 if (target_type != this_type)
18051 TYPE_TARGET_TYPE (this_type) = target_type;
18052 else
18053 {
18054 /* Self-referential typedefs are, it seems, not allowed by the DWARF
18055 spec and cause infinite loops in GDB. */
18056 complaint (_("Self-referential DW_TAG_typedef "
18057 "- DIE at %s [in module %s]"),
18058 sect_offset_str (die->sect_off), objfile_name (objfile));
18059 TYPE_TARGET_TYPE (this_type) = NULL;
18060 }
18061 if (name == NULL)
18062 {
18063 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
18064 anonymous typedefs, which is, strictly speaking, invalid DWARF.
18065 Handle these by just returning the target type, rather than
18066 constructing an anonymous typedef type and trying to handle this
18067 elsewhere. */
18068 set_die_type (die, target_type, cu);
18069 return target_type;
18070 }
18071 return this_type;
18072 }
18073
18074 /* Helper for get_dwarf2_rational_constant that computes the value of
18075 a given gmp_mpz given an attribute. */
18076
18077 static void
18078 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
18079 {
18080 /* GCC will sometimes emit a 16-byte constant value as a DWARF
18081 location expression that pushes an implicit value. */
18082 if (attr->form == DW_FORM_exprloc)
18083 {
18084 dwarf_block *blk = attr->as_block ();
18085 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
18086 {
18087 uint64_t len;
18088 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
18089 blk->data + blk->size,
18090 &len);
18091 if (ptr - blk->data + len <= blk->size)
18092 {
18093 mpz_import (value->val, len,
18094 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18095 1, 0, 0, ptr);
18096 return;
18097 }
18098 }
18099
18100 /* On failure set it to 1. */
18101 *value = gdb_mpz (1);
18102 }
18103 else if (attr->form_is_block ())
18104 {
18105 dwarf_block *blk = attr->as_block ();
18106 mpz_import (value->val, blk->size,
18107 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18108 1, 0, 0, blk->data);
18109 }
18110 else
18111 *value = gdb_mpz (attr->constant_value (1));
18112 }
18113
18114 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
18115 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
18116
18117 If the numerator and/or numerator attribute is missing,
18118 a complaint is filed, and NUMERATOR and DENOMINATOR are left
18119 untouched. */
18120
18121 static void
18122 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
18123 gdb_mpz *numerator, gdb_mpz *denominator)
18124 {
18125 struct attribute *num_attr, *denom_attr;
18126
18127 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
18128 if (num_attr == nullptr)
18129 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
18130 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18131
18132 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
18133 if (denom_attr == nullptr)
18134 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
18135 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18136
18137 if (num_attr == nullptr || denom_attr == nullptr)
18138 return;
18139
18140 get_mpz (cu, numerator, num_attr);
18141 get_mpz (cu, denominator, denom_attr);
18142 }
18143
18144 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
18145 rational constant, rather than a signed one.
18146
18147 If the rational constant has a negative value, a complaint
18148 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
18149
18150 static void
18151 get_dwarf2_unsigned_rational_constant (struct die_info *die,
18152 struct dwarf2_cu *cu,
18153 gdb_mpz *numerator,
18154 gdb_mpz *denominator)
18155 {
18156 gdb_mpz num (1);
18157 gdb_mpz denom (1);
18158
18159 get_dwarf2_rational_constant (die, cu, &num, &denom);
18160 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
18161 {
18162 mpz_neg (num.val, num.val);
18163 mpz_neg (denom.val, denom.val);
18164 }
18165 else if (mpz_sgn (num.val) == -1)
18166 {
18167 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
18168 " in DIE at %s"),
18169 sect_offset_str (die->sect_off));
18170 return;
18171 }
18172 else if (mpz_sgn (denom.val) == -1)
18173 {
18174 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
18175 " in DIE at %s"),
18176 sect_offset_str (die->sect_off));
18177 return;
18178 }
18179
18180 *numerator = std::move (num);
18181 *denominator = std::move (denom);
18182 }
18183
18184 /* Assuming that ENCODING is a string whose contents starting at the
18185 K'th character is "_nn" where "nn" is a decimal number, scan that
18186 number and set RESULT to the value. K is updated to point to the
18187 character immediately following the number.
18188
18189 If the string does not conform to the format described above, false
18190 is returned, and K may or may not be changed. */
18191
18192 static bool
18193 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
18194 {
18195 /* The next character should be an underscore ('_') followed
18196 by a digit. */
18197 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
18198 return false;
18199
18200 /* Skip the underscore. */
18201 k++;
18202 int start = k;
18203
18204 /* Determine the number of digits for our number. */
18205 while (isdigit (encoding[k]))
18206 k++;
18207 if (k == start)
18208 return false;
18209
18210 std::string copy (&encoding[start], k - start);
18211 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
18212 return false;
18213
18214 return true;
18215 }
18216
18217 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
18218 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
18219 DENOM, update OFFSET, and return true on success. Return false on
18220 failure. */
18221
18222 static bool
18223 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
18224 gdb_mpz *num, gdb_mpz *denom)
18225 {
18226 if (!ada_get_gnat_encoded_number (encoding, offset, num))
18227 return false;
18228 return ada_get_gnat_encoded_number (encoding, offset, denom);
18229 }
18230
18231 /* Assuming DIE corresponds to a fixed point type, finish the creation
18232 of the corresponding TYPE by setting its type-specific data. CU is
18233 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
18234 encodings. It is nullptr if the GNAT encoding should be
18235 ignored. */
18236
18237 static void
18238 finish_fixed_point_type (struct type *type, const char *suffix,
18239 struct die_info *die, struct dwarf2_cu *cu)
18240 {
18241 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
18242 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
18243
18244 /* If GNAT encodings are preferred, don't examine the
18245 attributes. */
18246 struct attribute *attr = nullptr;
18247 if (suffix == nullptr)
18248 {
18249 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
18250 if (attr == nullptr)
18251 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
18252 if (attr == nullptr)
18253 attr = dwarf2_attr (die, DW_AT_small, cu);
18254 }
18255
18256 /* Numerator and denominator of our fixed-point type's scaling factor.
18257 The default is a scaling factor of 1, which we use as a fallback
18258 when we are not able to decode it (problem with the debugging info,
18259 unsupported forms, bug in GDB, etc...). Using that as the default
18260 allows us to at least print the unscaled value, which might still
18261 be useful to a user. */
18262 gdb_mpz scale_num (1);
18263 gdb_mpz scale_denom (1);
18264
18265 if (attr == nullptr)
18266 {
18267 int offset = 0;
18268 if (suffix != nullptr
18269 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18270 &scale_denom)
18271 /* The number might be encoded as _nn_dd_nn_dd, where the
18272 second ratio is the 'small value. In this situation, we
18273 want the second value. */
18274 && (suffix[offset] != '_'
18275 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18276 &scale_denom)))
18277 {
18278 /* Found it. */
18279 }
18280 else
18281 {
18282 /* Scaling factor not found. Assume a scaling factor of 1,
18283 and hope for the best. At least the user will be able to
18284 see the encoded value. */
18285 scale_num = 1;
18286 scale_denom = 1;
18287 complaint (_("no scale found for fixed-point type (DIE at %s)"),
18288 sect_offset_str (die->sect_off));
18289 }
18290 }
18291 else if (attr->name == DW_AT_binary_scale)
18292 {
18293 LONGEST scale_exp = attr->constant_value (0);
18294 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18295
18296 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
18297 }
18298 else if (attr->name == DW_AT_decimal_scale)
18299 {
18300 LONGEST scale_exp = attr->constant_value (0);
18301 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18302
18303 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
18304 }
18305 else if (attr->name == DW_AT_small)
18306 {
18307 struct die_info *scale_die;
18308 struct dwarf2_cu *scale_cu = cu;
18309
18310 scale_die = follow_die_ref (die, attr, &scale_cu);
18311 if (scale_die->tag == DW_TAG_constant)
18312 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18313 &scale_num, &scale_denom);
18314 else
18315 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18316 " (DIE at %s)"),
18317 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18318 }
18319 else
18320 {
18321 complaint (_("unsupported scale attribute %s for fixed-point type"
18322 " (DIE at %s)"),
18323 dwarf_attr_name (attr->name),
18324 sect_offset_str (die->sect_off));
18325 }
18326
18327 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
18328 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
18329 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
18330 mpq_canonicalize (scaling_factor.val);
18331 }
18332
18333 /* The gnat-encoding suffix for fixed point. */
18334
18335 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
18336
18337 /* If NAME encodes an Ada fixed-point type, return a pointer to the
18338 "XF" suffix of the name. The text after this is what encodes the
18339 'small and 'delta information. Otherwise, return nullptr. */
18340
18341 static const char *
18342 gnat_encoded_fixed_point_type_info (const char *name)
18343 {
18344 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
18345 }
18346
18347 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18348 (which may be different from NAME) to the architecture back-end to allow
18349 it to guess the correct format if necessary. */
18350
18351 static struct type *
18352 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
18353 const char *name_hint, enum bfd_endian byte_order)
18354 {
18355 struct gdbarch *gdbarch = objfile->arch ();
18356 const struct floatformat **format;
18357 struct type *type;
18358
18359 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18360 if (format)
18361 type = init_float_type (objfile, bits, name, format, byte_order);
18362 else
18363 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18364
18365 return type;
18366 }
18367
18368 /* Allocate an integer type of size BITS and name NAME. */
18369
18370 static struct type *
18371 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18372 int bits, int unsigned_p, const char *name)
18373 {
18374 struct type *type;
18375
18376 /* Versions of Intel's C Compiler generate an integer type called "void"
18377 instead of using DW_TAG_unspecified_type. This has been seen on
18378 at least versions 14, 17, and 18. */
18379 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18380 && strcmp (name, "void") == 0)
18381 type = objfile_type (objfile)->builtin_void;
18382 else
18383 type = init_integer_type (objfile, bits, unsigned_p, name);
18384
18385 return type;
18386 }
18387
18388 /* Return true if DIE has a DW_AT_small attribute whose value is
18389 a constant rational, where both the numerator and denominator
18390 are equal to zero.
18391
18392 CU is the DIE's Compilation Unit. */
18393
18394 static bool
18395 has_zero_over_zero_small_attribute (struct die_info *die,
18396 struct dwarf2_cu *cu)
18397 {
18398 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18399 if (attr == nullptr)
18400 return false;
18401
18402 struct dwarf2_cu *scale_cu = cu;
18403 struct die_info *scale_die
18404 = follow_die_ref (die, attr, &scale_cu);
18405
18406 if (scale_die->tag != DW_TAG_constant)
18407 return false;
18408
18409 gdb_mpz num (1), denom (1);
18410 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
18411 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
18412 }
18413
18414 /* Initialise and return a floating point type of size BITS suitable for
18415 use as a component of a complex number. The NAME_HINT is passed through
18416 when initialising the floating point type and is the name of the complex
18417 type.
18418
18419 As DWARF doesn't currently provide an explicit name for the components
18420 of a complex number, but it can be helpful to have these components
18421 named, we try to select a suitable name based on the size of the
18422 component. */
18423 static struct type *
18424 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18425 struct objfile *objfile,
18426 int bits, const char *name_hint,
18427 enum bfd_endian byte_order)
18428 {
18429 gdbarch *gdbarch = objfile->arch ();
18430 struct type *tt = nullptr;
18431
18432 /* Try to find a suitable floating point builtin type of size BITS.
18433 We're going to use the name of this type as the name for the complex
18434 target type that we are about to create. */
18435 switch (cu->language)
18436 {
18437 case language_fortran:
18438 switch (bits)
18439 {
18440 case 32:
18441 tt = builtin_f_type (gdbarch)->builtin_real;
18442 break;
18443 case 64:
18444 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18445 break;
18446 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18447 case 128:
18448 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18449 break;
18450 }
18451 break;
18452 default:
18453 switch (bits)
18454 {
18455 case 32:
18456 tt = builtin_type (gdbarch)->builtin_float;
18457 break;
18458 case 64:
18459 tt = builtin_type (gdbarch)->builtin_double;
18460 break;
18461 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18462 case 128:
18463 tt = builtin_type (gdbarch)->builtin_long_double;
18464 break;
18465 }
18466 break;
18467 }
18468
18469 /* If the type we found doesn't match the size we were looking for, then
18470 pretend we didn't find a type at all, the complex target type we
18471 create will then be nameless. */
18472 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
18473 tt = nullptr;
18474
18475 const char *name = (tt == nullptr) ? nullptr : tt->name ();
18476 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
18477 }
18478
18479 /* Find a representation of a given base type and install
18480 it in the TYPE field of the die. */
18481
18482 static struct type *
18483 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
18484 {
18485 struct objfile *objfile = cu->per_objfile->objfile;
18486 struct type *type;
18487 struct attribute *attr;
18488 int encoding = 0, bits = 0;
18489 const char *name;
18490 gdbarch *arch;
18491
18492 attr = dwarf2_attr (die, DW_AT_encoding, cu);
18493 if (attr != nullptr && attr->form_is_constant ())
18494 encoding = attr->constant_value (0);
18495 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18496 if (attr != nullptr)
18497 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
18498 name = dwarf2_name (die, cu);
18499 if (!name)
18500 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
18501
18502 arch = objfile->arch ();
18503 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18504
18505 attr = dwarf2_attr (die, DW_AT_endianity, cu);
18506 if (attr != nullptr && attr->form_is_constant ())
18507 {
18508 int endianity = attr->constant_value (0);
18509
18510 switch (endianity)
18511 {
18512 case DW_END_big:
18513 byte_order = BFD_ENDIAN_BIG;
18514 break;
18515 case DW_END_little:
18516 byte_order = BFD_ENDIAN_LITTLE;
18517 break;
18518 default:
18519 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18520 break;
18521 }
18522 }
18523
18524 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18525 && cu->language == language_ada
18526 && has_zero_over_zero_small_attribute (die, cu))
18527 {
18528 /* brobecker/2018-02-24: This is a fixed point type for which
18529 the scaling factor is represented as fraction whose value
18530 does not make sense (zero divided by zero), so we should
18531 normally never see these. However, there is a small category
18532 of fixed point types for which GNAT is unable to provide
18533 the scaling factor via the standard DWARF mechanisms, and
18534 for which the info is provided via the GNAT encodings instead.
18535 This is likely what this DIE is about. */
18536 encoding = (encoding == DW_ATE_signed_fixed
18537 ? DW_ATE_signed
18538 : DW_ATE_unsigned);
18539 }
18540
18541 /* With GNAT encodings, fixed-point information will be encoded in
18542 the type name. Note that this can also occur with the above
18543 zero-over-zero case, which is why this is a separate "if" rather
18544 than an "else if". */
18545 const char *gnat_encoding_suffix = nullptr;
18546 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18547 && cu->language == language_ada
18548 && name != nullptr)
18549 {
18550 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18551 if (gnat_encoding_suffix != nullptr)
18552 {
18553 gdb_assert (startswith (gnat_encoding_suffix,
18554 GNAT_FIXED_POINT_SUFFIX));
18555 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18556 name, gnat_encoding_suffix - name);
18557 /* Use -1 here so that SUFFIX points at the "_" after the
18558 "XF". */
18559 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18560
18561 encoding = (encoding == DW_ATE_signed
18562 ? DW_ATE_signed_fixed
18563 : DW_ATE_unsigned_fixed);
18564 }
18565 }
18566
18567 switch (encoding)
18568 {
18569 case DW_ATE_address:
18570 /* Turn DW_ATE_address into a void * pointer. */
18571 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
18572 type = init_pointer_type (objfile, bits, name, type);
18573 break;
18574 case DW_ATE_boolean:
18575 type = init_boolean_type (objfile, bits, 1, name);
18576 break;
18577 case DW_ATE_complex_float:
18578 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18579 byte_order);
18580 if (type->code () == TYPE_CODE_ERROR)
18581 {
18582 if (name == nullptr)
18583 {
18584 struct obstack *obstack
18585 = &cu->per_objfile->objfile->objfile_obstack;
18586 name = obconcat (obstack, "_Complex ", type->name (),
18587 nullptr);
18588 }
18589 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18590 }
18591 else
18592 type = init_complex_type (name, type);
18593 break;
18594 case DW_ATE_decimal_float:
18595 type = init_decfloat_type (objfile, bits, name);
18596 break;
18597 case DW_ATE_float:
18598 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
18599 break;
18600 case DW_ATE_signed:
18601 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18602 break;
18603 case DW_ATE_unsigned:
18604 if (cu->language == language_fortran
18605 && name
18606 && startswith (name, "character("))
18607 type = init_character_type (objfile, bits, 1, name);
18608 else
18609 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18610 break;
18611 case DW_ATE_signed_char:
18612 if (cu->language == language_ada || cu->language == language_m2
18613 || cu->language == language_pascal
18614 || cu->language == language_fortran)
18615 type = init_character_type (objfile, bits, 0, name);
18616 else
18617 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18618 break;
18619 case DW_ATE_unsigned_char:
18620 if (cu->language == language_ada || cu->language == language_m2
18621 || cu->language == language_pascal
18622 || cu->language == language_fortran
18623 || cu->language == language_rust)
18624 type = init_character_type (objfile, bits, 1, name);
18625 else
18626 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18627 break;
18628 case DW_ATE_UTF:
18629 {
18630 if (bits == 16)
18631 type = builtin_type (arch)->builtin_char16;
18632 else if (bits == 32)
18633 type = builtin_type (arch)->builtin_char32;
18634 else
18635 {
18636 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
18637 bits);
18638 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18639 }
18640 return set_die_type (die, type, cu);
18641 }
18642 break;
18643 case DW_ATE_signed_fixed:
18644 type = init_fixed_point_type (objfile, bits, 0, name);
18645 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18646 break;
18647 case DW_ATE_unsigned_fixed:
18648 type = init_fixed_point_type (objfile, bits, 1, name);
18649 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18650 break;
18651
18652 default:
18653 complaint (_("unsupported DW_AT_encoding: '%s'"),
18654 dwarf_type_encoding_name (encoding));
18655 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18656 break;
18657 }
18658
18659 if (name && strcmp (name, "char") == 0)
18660 type->set_has_no_signedness (true);
18661
18662 maybe_set_alignment (cu, die, type);
18663
18664 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18665
18666 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18667 {
18668 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18669 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
18670 {
18671 unsigned real_bit_size = attr->as_unsigned ();
18672 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18673 /* Only use the attributes if they make sense together. */
18674 if (attr == nullptr
18675 || (attr->as_unsigned () + real_bit_size
18676 <= 8 * TYPE_LENGTH (type)))
18677 {
18678 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18679 = real_bit_size;
18680 if (attr != nullptr)
18681 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18682 = attr->as_unsigned ();
18683 }
18684 }
18685 }
18686
18687 return set_die_type (die, type, cu);
18688 }
18689
18690 /* Parse dwarf attribute if it's a block, reference or constant and put the
18691 resulting value of the attribute into struct bound_prop.
18692 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18693
18694 static int
18695 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18696 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18697 struct type *default_type)
18698 {
18699 struct dwarf2_property_baton *baton;
18700 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18701 struct objfile *objfile = per_objfile->objfile;
18702 struct obstack *obstack = &objfile->objfile_obstack;
18703
18704 gdb_assert (default_type != NULL);
18705
18706 if (attr == NULL || prop == NULL)
18707 return 0;
18708
18709 if (attr->form_is_block ())
18710 {
18711 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18712 baton->property_type = default_type;
18713 baton->locexpr.per_cu = cu->per_cu;
18714 baton->locexpr.per_objfile = per_objfile;
18715
18716 struct dwarf_block *block = attr->as_block ();
18717 baton->locexpr.size = block->size;
18718 baton->locexpr.data = block->data;
18719 switch (attr->name)
18720 {
18721 case DW_AT_string_length:
18722 baton->locexpr.is_reference = true;
18723 break;
18724 default:
18725 baton->locexpr.is_reference = false;
18726 break;
18727 }
18728
18729 prop->set_locexpr (baton);
18730 gdb_assert (prop->baton () != NULL);
18731 }
18732 else if (attr->form_is_ref ())
18733 {
18734 struct dwarf2_cu *target_cu = cu;
18735 struct die_info *target_die;
18736 struct attribute *target_attr;
18737
18738 target_die = follow_die_ref (die, attr, &target_cu);
18739 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18740 if (target_attr == NULL)
18741 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18742 target_cu);
18743 if (target_attr == NULL)
18744 return 0;
18745
18746 switch (target_attr->name)
18747 {
18748 case DW_AT_location:
18749 if (target_attr->form_is_section_offset ())
18750 {
18751 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18752 baton->property_type = die_type (target_die, target_cu);
18753 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18754 prop->set_loclist (baton);
18755 gdb_assert (prop->baton () != NULL);
18756 }
18757 else if (target_attr->form_is_block ())
18758 {
18759 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18760 baton->property_type = die_type (target_die, target_cu);
18761 baton->locexpr.per_cu = cu->per_cu;
18762 baton->locexpr.per_objfile = per_objfile;
18763 struct dwarf_block *block = target_attr->as_block ();
18764 baton->locexpr.size = block->size;
18765 baton->locexpr.data = block->data;
18766 baton->locexpr.is_reference = true;
18767 prop->set_locexpr (baton);
18768 gdb_assert (prop->baton () != NULL);
18769 }
18770 else
18771 {
18772 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18773 "dynamic property");
18774 return 0;
18775 }
18776 break;
18777 case DW_AT_data_member_location:
18778 {
18779 LONGEST offset;
18780
18781 if (!handle_data_member_location (target_die, target_cu,
18782 &offset))
18783 return 0;
18784
18785 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18786 baton->property_type = read_type_die (target_die->parent,
18787 target_cu);
18788 baton->offset_info.offset = offset;
18789 baton->offset_info.type = die_type (target_die, target_cu);
18790 prop->set_addr_offset (baton);
18791 break;
18792 }
18793 }
18794 }
18795 else if (attr->form_is_constant ())
18796 prop->set_const_val (attr->constant_value (0));
18797 else
18798 {
18799 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18800 dwarf2_name (die, cu));
18801 return 0;
18802 }
18803
18804 return 1;
18805 }
18806
18807 /* See read.h. */
18808
18809 struct type *
18810 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
18811 {
18812 struct type *int_type;
18813
18814 /* Helper macro to examine the various builtin types. */
18815 #define TRY_TYPE(F) \
18816 int_type = (unsigned_p \
18817 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18818 : objfile_type (objfile)->builtin_ ## F); \
18819 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
18820 return int_type
18821
18822 TRY_TYPE (char);
18823 TRY_TYPE (short);
18824 TRY_TYPE (int);
18825 TRY_TYPE (long);
18826 TRY_TYPE (long_long);
18827
18828 #undef TRY_TYPE
18829
18830 gdb_assert_not_reached ("unable to find suitable integer type");
18831 }
18832
18833 /* See read.h. */
18834
18835 struct type *
18836 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
18837 {
18838 int addr_size = this->per_cu->addr_size ();
18839 return this->per_objfile->int_type (addr_size, unsigned_p);
18840 }
18841
18842 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18843 present (which is valid) then compute the default type based on the
18844 compilation units address size. */
18845
18846 static struct type *
18847 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18848 {
18849 struct type *index_type = die_type (die, cu);
18850
18851 /* Dwarf-2 specifications explicitly allows to create subrange types
18852 without specifying a base type.
18853 In that case, the base type must be set to the type of
18854 the lower bound, upper bound or count, in that order, if any of these
18855 three attributes references an object that has a type.
18856 If no base type is found, the Dwarf-2 specifications say that
18857 a signed integer type of size equal to the size of an address should
18858 be used.
18859 For the following C code: `extern char gdb_int [];'
18860 GCC produces an empty range DIE.
18861 FIXME: muller/2010-05-28: Possible references to object for low bound,
18862 high bound or count are not yet handled by this code. */
18863 if (index_type->code () == TYPE_CODE_VOID)
18864 index_type = cu->addr_sized_int_type (false);
18865
18866 return index_type;
18867 }
18868
18869 /* Read the given DW_AT_subrange DIE. */
18870
18871 static struct type *
18872 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18873 {
18874 struct type *base_type, *orig_base_type;
18875 struct type *range_type;
18876 struct attribute *attr;
18877 struct dynamic_prop low, high;
18878 int low_default_is_valid;
18879 int high_bound_is_count = 0;
18880 const char *name;
18881 ULONGEST negative_mask;
18882
18883 orig_base_type = read_subrange_index_type (die, cu);
18884
18885 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18886 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18887 creating the range type, but we use the result of check_typedef
18888 when examining properties of the type. */
18889 base_type = check_typedef (orig_base_type);
18890
18891 /* The die_type call above may have already set the type for this DIE. */
18892 range_type = get_die_type (die, cu);
18893 if (range_type)
18894 return range_type;
18895
18896 high.set_const_val (0);
18897
18898 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18899 omitting DW_AT_lower_bound. */
18900 switch (cu->language)
18901 {
18902 case language_c:
18903 case language_cplus:
18904 low.set_const_val (0);
18905 low_default_is_valid = 1;
18906 break;
18907 case language_fortran:
18908 low.set_const_val (1);
18909 low_default_is_valid = 1;
18910 break;
18911 case language_d:
18912 case language_objc:
18913 case language_rust:
18914 low.set_const_val (0);
18915 low_default_is_valid = (cu->header.version >= 4);
18916 break;
18917 case language_ada:
18918 case language_m2:
18919 case language_pascal:
18920 low.set_const_val (1);
18921 low_default_is_valid = (cu->header.version >= 4);
18922 break;
18923 default:
18924 low.set_const_val (0);
18925 low_default_is_valid = 0;
18926 break;
18927 }
18928
18929 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18930 if (attr != nullptr)
18931 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18932 else if (!low_default_is_valid)
18933 complaint (_("Missing DW_AT_lower_bound "
18934 "- DIE at %s [in module %s]"),
18935 sect_offset_str (die->sect_off),
18936 objfile_name (cu->per_objfile->objfile));
18937
18938 struct attribute *attr_ub, *attr_count;
18939 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18940 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18941 {
18942 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18943 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18944 {
18945 /* If bounds are constant do the final calculation here. */
18946 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18947 high.set_const_val (low.const_val () + high.const_val () - 1);
18948 else
18949 high_bound_is_count = 1;
18950 }
18951 else
18952 {
18953 if (attr_ub != NULL)
18954 complaint (_("Unresolved DW_AT_upper_bound "
18955 "- DIE at %s [in module %s]"),
18956 sect_offset_str (die->sect_off),
18957 objfile_name (cu->per_objfile->objfile));
18958 if (attr_count != NULL)
18959 complaint (_("Unresolved DW_AT_count "
18960 "- DIE at %s [in module %s]"),
18961 sect_offset_str (die->sect_off),
18962 objfile_name (cu->per_objfile->objfile));
18963 }
18964 }
18965
18966 LONGEST bias = 0;
18967 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18968 if (bias_attr != nullptr && bias_attr->form_is_constant ())
18969 bias = bias_attr->constant_value (0);
18970
18971 /* Normally, the DWARF producers are expected to use a signed
18972 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18973 But this is unfortunately not always the case, as witnessed
18974 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18975 is used instead. To work around that ambiguity, we treat
18976 the bounds as signed, and thus sign-extend their values, when
18977 the base type is signed. */
18978 negative_mask =
18979 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18980 if (low.kind () == PROP_CONST
18981 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
18982 low.set_const_val (low.const_val () | negative_mask);
18983 if (high.kind () == PROP_CONST
18984 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
18985 high.set_const_val (high.const_val () | negative_mask);
18986
18987 /* Check for bit and byte strides. */
18988 struct dynamic_prop byte_stride_prop;
18989 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18990 if (attr_byte_stride != nullptr)
18991 {
18992 struct type *prop_type = cu->addr_sized_int_type (false);
18993 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18994 prop_type);
18995 }
18996
18997 struct dynamic_prop bit_stride_prop;
18998 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18999 if (attr_bit_stride != nullptr)
19000 {
19001 /* It only makes sense to have either a bit or byte stride. */
19002 if (attr_byte_stride != nullptr)
19003 {
19004 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
19005 "- DIE at %s [in module %s]"),
19006 sect_offset_str (die->sect_off),
19007 objfile_name (cu->per_objfile->objfile));
19008 attr_bit_stride = nullptr;
19009 }
19010 else
19011 {
19012 struct type *prop_type = cu->addr_sized_int_type (false);
19013 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
19014 prop_type);
19015 }
19016 }
19017
19018 if (attr_byte_stride != nullptr
19019 || attr_bit_stride != nullptr)
19020 {
19021 bool byte_stride_p = (attr_byte_stride != nullptr);
19022 struct dynamic_prop *stride
19023 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
19024
19025 range_type
19026 = create_range_type_with_stride (NULL, orig_base_type, &low,
19027 &high, bias, stride, byte_stride_p);
19028 }
19029 else
19030 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
19031
19032 if (high_bound_is_count)
19033 range_type->bounds ()->flag_upper_bound_is_count = 1;
19034
19035 /* Ada expects an empty array on no boundary attributes. */
19036 if (attr == NULL && cu->language != language_ada)
19037 range_type->bounds ()->high.set_undefined ();
19038
19039 name = dwarf2_name (die, cu);
19040 if (name)
19041 range_type->set_name (name);
19042
19043 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
19044 if (attr != nullptr)
19045 TYPE_LENGTH (range_type) = attr->constant_value (0);
19046
19047 maybe_set_alignment (cu, die, range_type);
19048
19049 set_die_type (die, range_type, cu);
19050
19051 /* set_die_type should be already done. */
19052 set_descriptive_type (range_type, die, cu);
19053
19054 return range_type;
19055 }
19056
19057 static struct type *
19058 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
19059 {
19060 struct type *type;
19061
19062 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
19063 type->set_name (dwarf2_name (die, cu));
19064
19065 /* In Ada, an unspecified type is typically used when the description
19066 of the type is deferred to a different unit. When encountering
19067 such a type, we treat it as a stub, and try to resolve it later on,
19068 when needed. */
19069 if (cu->language == language_ada)
19070 type->set_is_stub (true);
19071
19072 return set_die_type (die, type, cu);
19073 }
19074
19075 /* Read a single die and all its descendents. Set the die's sibling
19076 field to NULL; set other fields in the die correctly, and set all
19077 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
19078 location of the info_ptr after reading all of those dies. PARENT
19079 is the parent of the die in question. */
19080
19081 static struct die_info *
19082 read_die_and_children (const struct die_reader_specs *reader,
19083 const gdb_byte *info_ptr,
19084 const gdb_byte **new_info_ptr,
19085 struct die_info *parent)
19086 {
19087 struct die_info *die;
19088 const gdb_byte *cur_ptr;
19089
19090 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
19091 if (die == NULL)
19092 {
19093 *new_info_ptr = cur_ptr;
19094 return NULL;
19095 }
19096 store_in_ref_table (die, reader->cu);
19097
19098 if (die->has_children)
19099 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
19100 else
19101 {
19102 die->child = NULL;
19103 *new_info_ptr = cur_ptr;
19104 }
19105
19106 die->sibling = NULL;
19107 die->parent = parent;
19108 return die;
19109 }
19110
19111 /* Read a die, all of its descendents, and all of its siblings; set
19112 all of the fields of all of the dies correctly. Arguments are as
19113 in read_die_and_children. */
19114
19115 static struct die_info *
19116 read_die_and_siblings_1 (const struct die_reader_specs *reader,
19117 const gdb_byte *info_ptr,
19118 const gdb_byte **new_info_ptr,
19119 struct die_info *parent)
19120 {
19121 struct die_info *first_die, *last_sibling;
19122 const gdb_byte *cur_ptr;
19123
19124 cur_ptr = info_ptr;
19125 first_die = last_sibling = NULL;
19126
19127 while (1)
19128 {
19129 struct die_info *die
19130 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
19131
19132 if (die == NULL)
19133 {
19134 *new_info_ptr = cur_ptr;
19135 return first_die;
19136 }
19137
19138 if (!first_die)
19139 first_die = die;
19140 else
19141 last_sibling->sibling = die;
19142
19143 last_sibling = die;
19144 }
19145 }
19146
19147 /* Read a die, all of its descendents, and all of its siblings; set
19148 all of the fields of all of the dies correctly. Arguments are as
19149 in read_die_and_children.
19150 This the main entry point for reading a DIE and all its children. */
19151
19152 static struct die_info *
19153 read_die_and_siblings (const struct die_reader_specs *reader,
19154 const gdb_byte *info_ptr,
19155 const gdb_byte **new_info_ptr,
19156 struct die_info *parent)
19157 {
19158 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
19159 new_info_ptr, parent);
19160
19161 if (dwarf_die_debug)
19162 {
19163 fprintf_unfiltered (gdb_stdlog,
19164 "Read die from %s@0x%x of %s:\n",
19165 reader->die_section->get_name (),
19166 (unsigned) (info_ptr - reader->die_section->buffer),
19167 bfd_get_filename (reader->abfd));
19168 dump_die (die, dwarf_die_debug);
19169 }
19170
19171 return die;
19172 }
19173
19174 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
19175 attributes.
19176 The caller is responsible for filling in the extra attributes
19177 and updating (*DIEP)->num_attrs.
19178 Set DIEP to point to a newly allocated die with its information,
19179 except for its child, sibling, and parent fields. */
19180
19181 static const gdb_byte *
19182 read_full_die_1 (const struct die_reader_specs *reader,
19183 struct die_info **diep, const gdb_byte *info_ptr,
19184 int num_extra_attrs)
19185 {
19186 unsigned int abbrev_number, bytes_read, i;
19187 const struct abbrev_info *abbrev;
19188 struct die_info *die;
19189 struct dwarf2_cu *cu = reader->cu;
19190 bfd *abfd = reader->abfd;
19191
19192 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
19193 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19194 info_ptr += bytes_read;
19195 if (!abbrev_number)
19196 {
19197 *diep = NULL;
19198 return info_ptr;
19199 }
19200
19201 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
19202 if (!abbrev)
19203 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
19204 abbrev_number,
19205 bfd_get_filename (abfd));
19206
19207 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
19208 die->sect_off = sect_off;
19209 die->tag = abbrev->tag;
19210 die->abbrev = abbrev_number;
19211 die->has_children = abbrev->has_children;
19212
19213 /* Make the result usable.
19214 The caller needs to update num_attrs after adding the extra
19215 attributes. */
19216 die->num_attrs = abbrev->num_attrs;
19217
19218 bool any_need_reprocess = false;
19219 for (i = 0; i < abbrev->num_attrs; ++i)
19220 {
19221 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
19222 info_ptr);
19223 if (die->attrs[i].requires_reprocessing_p ())
19224 any_need_reprocess = true;
19225 }
19226
19227 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
19228 if (attr != nullptr && attr->form_is_unsigned ())
19229 cu->str_offsets_base = attr->as_unsigned ();
19230
19231 attr = die->attr (DW_AT_loclists_base);
19232 if (attr != nullptr)
19233 cu->loclist_base = attr->as_unsigned ();
19234
19235 auto maybe_addr_base = die->addr_base ();
19236 if (maybe_addr_base.has_value ())
19237 cu->addr_base = *maybe_addr_base;
19238
19239 attr = die->attr (DW_AT_rnglists_base);
19240 if (attr != nullptr)
19241 cu->rnglists_base = attr->as_unsigned ();
19242
19243 if (any_need_reprocess)
19244 {
19245 for (i = 0; i < abbrev->num_attrs; ++i)
19246 {
19247 if (die->attrs[i].requires_reprocessing_p ())
19248 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
19249 }
19250 }
19251 *diep = die;
19252 return info_ptr;
19253 }
19254
19255 /* Read a die and all its attributes.
19256 Set DIEP to point to a newly allocated die with its information,
19257 except for its child, sibling, and parent fields. */
19258
19259 static const gdb_byte *
19260 read_full_die (const struct die_reader_specs *reader,
19261 struct die_info **diep, const gdb_byte *info_ptr)
19262 {
19263 const gdb_byte *result;
19264
19265 result = read_full_die_1 (reader, diep, info_ptr, 0);
19266
19267 if (dwarf_die_debug)
19268 {
19269 fprintf_unfiltered (gdb_stdlog,
19270 "Read die from %s@0x%x of %s:\n",
19271 reader->die_section->get_name (),
19272 (unsigned) (info_ptr - reader->die_section->buffer),
19273 bfd_get_filename (reader->abfd));
19274 dump_die (*diep, dwarf_die_debug);
19275 }
19276
19277 return result;
19278 }
19279 \f
19280
19281 /* Returns nonzero if TAG represents a type that we might generate a partial
19282 symbol for. */
19283
19284 static int
19285 is_type_tag_for_partial (int tag, enum language lang)
19286 {
19287 switch (tag)
19288 {
19289 #if 0
19290 /* Some types that would be reasonable to generate partial symbols for,
19291 that we don't at present. Note that normally this does not
19292 matter, mainly because C compilers don't give names to these
19293 types, but instead emit DW_TAG_typedef. */
19294 case DW_TAG_file_type:
19295 case DW_TAG_ptr_to_member_type:
19296 case DW_TAG_set_type:
19297 case DW_TAG_string_type:
19298 case DW_TAG_subroutine_type:
19299 #endif
19300
19301 /* GNAT may emit an array with a name, but no typedef, so we
19302 need to make a symbol in this case. */
19303 case DW_TAG_array_type:
19304 return lang == language_ada;
19305
19306 case DW_TAG_base_type:
19307 case DW_TAG_class_type:
19308 case DW_TAG_interface_type:
19309 case DW_TAG_enumeration_type:
19310 case DW_TAG_structure_type:
19311 case DW_TAG_subrange_type:
19312 case DW_TAG_typedef:
19313 case DW_TAG_union_type:
19314 return 1;
19315 default:
19316 return 0;
19317 }
19318 }
19319
19320 /* Load all DIEs that are interesting for partial symbols into memory. */
19321
19322 static struct partial_die_info *
19323 load_partial_dies (const struct die_reader_specs *reader,
19324 const gdb_byte *info_ptr, int building_psymtab)
19325 {
19326 struct dwarf2_cu *cu = reader->cu;
19327 struct objfile *objfile = cu->per_objfile->objfile;
19328 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
19329 unsigned int bytes_read;
19330 unsigned int load_all = 0;
19331 int nesting_level = 1;
19332
19333 parent_die = NULL;
19334 last_die = NULL;
19335
19336 gdb_assert (cu->per_cu != NULL);
19337 if (cu->per_cu->load_all_dies)
19338 load_all = 1;
19339
19340 cu->partial_dies
19341 = htab_create_alloc_ex (cu->header.length / 12,
19342 partial_die_hash,
19343 partial_die_eq,
19344 NULL,
19345 &cu->comp_unit_obstack,
19346 hashtab_obstack_allocate,
19347 dummy_obstack_deallocate);
19348
19349 while (1)
19350 {
19351 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
19352 &bytes_read);
19353
19354 /* A NULL abbrev means the end of a series of children. */
19355 if (abbrev == NULL)
19356 {
19357 if (--nesting_level == 0)
19358 return first_die;
19359
19360 info_ptr += bytes_read;
19361 last_die = parent_die;
19362 parent_die = parent_die->die_parent;
19363 continue;
19364 }
19365
19366 /* Check for template arguments. We never save these; if
19367 they're seen, we just mark the parent, and go on our way. */
19368 if (parent_die != NULL
19369 && cu->language == language_cplus
19370 && (abbrev->tag == DW_TAG_template_type_param
19371 || abbrev->tag == DW_TAG_template_value_param))
19372 {
19373 parent_die->has_template_arguments = 1;
19374
19375 if (!load_all)
19376 {
19377 /* We don't need a partial DIE for the template argument. */
19378 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19379 continue;
19380 }
19381 }
19382
19383 /* We only recurse into c++ subprograms looking for template arguments.
19384 Skip their other children. */
19385 if (!load_all
19386 && cu->language == language_cplus
19387 && parent_die != NULL
19388 && parent_die->tag == DW_TAG_subprogram
19389 && abbrev->tag != DW_TAG_inlined_subroutine)
19390 {
19391 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19392 continue;
19393 }
19394
19395 /* Check whether this DIE is interesting enough to save. Normally
19396 we would not be interested in members here, but there may be
19397 later variables referencing them via DW_AT_specification (for
19398 static members). */
19399 if (!load_all
19400 && !is_type_tag_for_partial (abbrev->tag, cu->language)
19401 && abbrev->tag != DW_TAG_constant
19402 && abbrev->tag != DW_TAG_enumerator
19403 && abbrev->tag != DW_TAG_subprogram
19404 && abbrev->tag != DW_TAG_inlined_subroutine
19405 && abbrev->tag != DW_TAG_lexical_block
19406 && abbrev->tag != DW_TAG_variable
19407 && abbrev->tag != DW_TAG_namespace
19408 && abbrev->tag != DW_TAG_module
19409 && abbrev->tag != DW_TAG_member
19410 && abbrev->tag != DW_TAG_imported_unit
19411 && abbrev->tag != DW_TAG_imported_declaration)
19412 {
19413 /* Otherwise we skip to the next sibling, if any. */
19414 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19415 continue;
19416 }
19417
19418 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19419 abbrev);
19420
19421 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
19422
19423 /* This two-pass algorithm for processing partial symbols has a
19424 high cost in cache pressure. Thus, handle some simple cases
19425 here which cover the majority of C partial symbols. DIEs
19426 which neither have specification tags in them, nor could have
19427 specification tags elsewhere pointing at them, can simply be
19428 processed and discarded.
19429
19430 This segment is also optional; scan_partial_symbols and
19431 add_partial_symbol will handle these DIEs if we chain
19432 them in normally. When compilers which do not emit large
19433 quantities of duplicate debug information are more common,
19434 this code can probably be removed. */
19435
19436 /* Any complete simple types at the top level (pretty much all
19437 of them, for a language without namespaces), can be processed
19438 directly. */
19439 if (parent_die == NULL
19440 && pdi.has_specification == 0
19441 && pdi.is_declaration == 0
19442 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19443 || pdi.tag == DW_TAG_base_type
19444 || pdi.tag == DW_TAG_array_type
19445 || pdi.tag == DW_TAG_subrange_type))
19446 {
19447 if (building_psymtab && pdi.raw_name != NULL)
19448 add_partial_symbol (&pdi, cu);
19449
19450 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19451 continue;
19452 }
19453
19454 /* The exception for DW_TAG_typedef with has_children above is
19455 a workaround of GCC PR debug/47510. In the case of this complaint
19456 type_name_or_error will error on such types later.
19457
19458 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19459 it could not find the child DIEs referenced later, this is checked
19460 above. In correct DWARF DW_TAG_typedef should have no children. */
19461
19462 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
19463 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
19464 "- DIE at %s [in module %s]"),
19465 sect_offset_str (pdi.sect_off), objfile_name (objfile));
19466
19467 /* If we're at the second level, and we're an enumerator, and
19468 our parent has no specification (meaning possibly lives in a
19469 namespace elsewhere), then we can add the partial symbol now
19470 instead of queueing it. */
19471 if (pdi.tag == DW_TAG_enumerator
19472 && parent_die != NULL
19473 && parent_die->die_parent == NULL
19474 && parent_die->tag == DW_TAG_enumeration_type
19475 && parent_die->has_specification == 0)
19476 {
19477 if (pdi.raw_name == NULL)
19478 complaint (_("malformed enumerator DIE ignored"));
19479 else if (building_psymtab)
19480 add_partial_symbol (&pdi, cu);
19481
19482 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19483 continue;
19484 }
19485
19486 struct partial_die_info *part_die
19487 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
19488
19489 /* We'll save this DIE so link it in. */
19490 part_die->die_parent = parent_die;
19491 part_die->die_sibling = NULL;
19492 part_die->die_child = NULL;
19493
19494 if (last_die && last_die == parent_die)
19495 last_die->die_child = part_die;
19496 else if (last_die)
19497 last_die->die_sibling = part_die;
19498
19499 last_die = part_die;
19500
19501 if (first_die == NULL)
19502 first_die = part_die;
19503
19504 /* Maybe add the DIE to the hash table. Not all DIEs that we
19505 find interesting need to be in the hash table, because we
19506 also have the parent/sibling/child chains; only those that we
19507 might refer to by offset later during partial symbol reading.
19508
19509 For now this means things that might have be the target of a
19510 DW_AT_specification, DW_AT_abstract_origin, or
19511 DW_AT_extension. DW_AT_extension will refer only to
19512 namespaces; DW_AT_abstract_origin refers to functions (and
19513 many things under the function DIE, but we do not recurse
19514 into function DIEs during partial symbol reading) and
19515 possibly variables as well; DW_AT_specification refers to
19516 declarations. Declarations ought to have the DW_AT_declaration
19517 flag. It happens that GCC forgets to put it in sometimes, but
19518 only for functions, not for types.
19519
19520 Adding more things than necessary to the hash table is harmless
19521 except for the performance cost. Adding too few will result in
19522 wasted time in find_partial_die, when we reread the compilation
19523 unit with load_all_dies set. */
19524
19525 if (load_all
19526 || abbrev->tag == DW_TAG_constant
19527 || abbrev->tag == DW_TAG_subprogram
19528 || abbrev->tag == DW_TAG_variable
19529 || abbrev->tag == DW_TAG_namespace
19530 || part_die->is_declaration)
19531 {
19532 void **slot;
19533
19534 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
19535 to_underlying (part_die->sect_off),
19536 INSERT);
19537 *slot = part_die;
19538 }
19539
19540 /* For some DIEs we want to follow their children (if any). For C
19541 we have no reason to follow the children of structures; for other
19542 languages we have to, so that we can get at method physnames
19543 to infer fully qualified class names, for DW_AT_specification,
19544 and for C++ template arguments. For C++, we also look one level
19545 inside functions to find template arguments (if the name of the
19546 function does not already contain the template arguments).
19547
19548 For Ada and Fortran, we need to scan the children of subprograms
19549 and lexical blocks as well because these languages allow the
19550 definition of nested entities that could be interesting for the
19551 debugger, such as nested subprograms for instance. */
19552 if (last_die->has_children
19553 && (load_all
19554 || last_die->tag == DW_TAG_namespace
19555 || last_die->tag == DW_TAG_module
19556 || last_die->tag == DW_TAG_enumeration_type
19557 || (cu->language == language_cplus
19558 && last_die->tag == DW_TAG_subprogram
19559 && (last_die->raw_name == NULL
19560 || strchr (last_die->raw_name, '<') == NULL))
19561 || (cu->language != language_c
19562 && (last_die->tag == DW_TAG_class_type
19563 || last_die->tag == DW_TAG_interface_type
19564 || last_die->tag == DW_TAG_structure_type
19565 || last_die->tag == DW_TAG_union_type))
19566 || ((cu->language == language_ada
19567 || cu->language == language_fortran)
19568 && (last_die->tag == DW_TAG_subprogram
19569 || last_die->tag == DW_TAG_lexical_block))))
19570 {
19571 nesting_level++;
19572 parent_die = last_die;
19573 continue;
19574 }
19575
19576 /* Otherwise we skip to the next sibling, if any. */
19577 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
19578
19579 /* Back to the top, do it again. */
19580 }
19581 }
19582
19583 partial_die_info::partial_die_info (sect_offset sect_off_,
19584 const struct abbrev_info *abbrev)
19585 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19586 {
19587 }
19588
19589 /* See class definition. */
19590
19591 const char *
19592 partial_die_info::name (dwarf2_cu *cu)
19593 {
19594 if (!canonical_name && raw_name != nullptr)
19595 {
19596 struct objfile *objfile = cu->per_objfile->objfile;
19597 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19598 canonical_name = 1;
19599 }
19600
19601 return raw_name;
19602 }
19603
19604 /* Read a minimal amount of information into the minimal die structure.
19605 INFO_PTR should point just after the initial uleb128 of a DIE. */
19606
19607 const gdb_byte *
19608 partial_die_info::read (const struct die_reader_specs *reader,
19609 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
19610 {
19611 struct dwarf2_cu *cu = reader->cu;
19612 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19613 unsigned int i;
19614 int has_low_pc_attr = 0;
19615 int has_high_pc_attr = 0;
19616 int high_pc_relative = 0;
19617
19618 for (i = 0; i < abbrev.num_attrs; ++i)
19619 {
19620 attribute attr;
19621 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19622 /* String and address offsets that need to do the reprocessing have
19623 already been read at this point, so there is no need to wait until
19624 the loop terminates to do the reprocessing. */
19625 if (attr.requires_reprocessing_p ())
19626 read_attribute_reprocess (reader, &attr, tag);
19627 /* Store the data if it is of an attribute we want to keep in a
19628 partial symbol table. */
19629 switch (attr.name)
19630 {
19631 case DW_AT_name:
19632 switch (tag)
19633 {
19634 case DW_TAG_compile_unit:
19635 case DW_TAG_partial_unit:
19636 case DW_TAG_type_unit:
19637 /* Compilation units have a DW_AT_name that is a filename, not
19638 a source language identifier. */
19639 case DW_TAG_enumeration_type:
19640 case DW_TAG_enumerator:
19641 /* These tags always have simple identifiers already; no need
19642 to canonicalize them. */
19643 canonical_name = 1;
19644 raw_name = attr.as_string ();
19645 break;
19646 default:
19647 canonical_name = 0;
19648 raw_name = attr.as_string ();
19649 break;
19650 }
19651 break;
19652 case DW_AT_linkage_name:
19653 case DW_AT_MIPS_linkage_name:
19654 /* Note that both forms of linkage name might appear. We
19655 assume they will be the same, and we only store the last
19656 one we see. */
19657 linkage_name = attr.as_string ();
19658 break;
19659 case DW_AT_low_pc:
19660 has_low_pc_attr = 1;
19661 lowpc = attr.as_address ();
19662 break;
19663 case DW_AT_high_pc:
19664 has_high_pc_attr = 1;
19665 highpc = attr.as_address ();
19666 if (cu->header.version >= 4 && attr.form_is_constant ())
19667 high_pc_relative = 1;
19668 break;
19669 case DW_AT_location:
19670 /* Support the .debug_loc offsets. */
19671 if (attr.form_is_block ())
19672 {
19673 d.locdesc = attr.as_block ();
19674 }
19675 else if (attr.form_is_section_offset ())
19676 {
19677 dwarf2_complex_location_expr_complaint ();
19678 }
19679 else
19680 {
19681 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19682 "partial symbol information");
19683 }
19684 break;
19685 case DW_AT_external:
19686 is_external = attr.as_boolean ();
19687 break;
19688 case DW_AT_declaration:
19689 is_declaration = attr.as_boolean ();
19690 break;
19691 case DW_AT_type:
19692 has_type = 1;
19693 break;
19694 case DW_AT_abstract_origin:
19695 case DW_AT_specification:
19696 case DW_AT_extension:
19697 has_specification = 1;
19698 spec_offset = attr.get_ref_die_offset ();
19699 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19700 || cu->per_cu->is_dwz);
19701 break;
19702 case DW_AT_sibling:
19703 /* Ignore absolute siblings, they might point outside of
19704 the current compile unit. */
19705 if (attr.form == DW_FORM_ref_addr)
19706 complaint (_("ignoring absolute DW_AT_sibling"));
19707 else
19708 {
19709 const gdb_byte *buffer = reader->buffer;
19710 sect_offset off = attr.get_ref_die_offset ();
19711 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19712
19713 if (sibling_ptr < info_ptr)
19714 complaint (_("DW_AT_sibling points backwards"));
19715 else if (sibling_ptr > reader->buffer_end)
19716 reader->die_section->overflow_complaint ();
19717 else
19718 sibling = sibling_ptr;
19719 }
19720 break;
19721 case DW_AT_byte_size:
19722 has_byte_size = 1;
19723 break;
19724 case DW_AT_const_value:
19725 has_const_value = 1;
19726 break;
19727 case DW_AT_calling_convention:
19728 /* DWARF doesn't provide a way to identify a program's source-level
19729 entry point. DW_AT_calling_convention attributes are only meant
19730 to describe functions' calling conventions.
19731
19732 However, because it's a necessary piece of information in
19733 Fortran, and before DWARF 4 DW_CC_program was the only
19734 piece of debugging information whose definition refers to
19735 a 'main program' at all, several compilers marked Fortran
19736 main programs with DW_CC_program --- even when those
19737 functions use the standard calling conventions.
19738
19739 Although DWARF now specifies a way to provide this
19740 information, we support this practice for backward
19741 compatibility. */
19742 if (attr.constant_value (0) == DW_CC_program
19743 && cu->language == language_fortran)
19744 main_subprogram = 1;
19745 break;
19746 case DW_AT_inline:
19747 {
19748 LONGEST value = attr.constant_value (-1);
19749 if (value == DW_INL_inlined
19750 || value == DW_INL_declared_inlined)
19751 may_be_inlined = 1;
19752 }
19753 break;
19754
19755 case DW_AT_import:
19756 if (tag == DW_TAG_imported_unit)
19757 {
19758 d.sect_off = attr.get_ref_die_offset ();
19759 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19760 || cu->per_cu->is_dwz);
19761 }
19762 break;
19763
19764 case DW_AT_main_subprogram:
19765 main_subprogram = attr.as_boolean ();
19766 break;
19767
19768 case DW_AT_ranges:
19769 {
19770 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19771 on DWARF version). */
19772 ULONGEST ranges_offset = attr.as_unsigned ();
19773
19774 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19775 this value. */
19776 if (tag != DW_TAG_compile_unit)
19777 ranges_offset += cu->gnu_ranges_base;
19778
19779 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19780 nullptr, tag))
19781 has_pc_info = 1;
19782 }
19783 break;
19784
19785 default:
19786 break;
19787 }
19788 }
19789
19790 /* For Ada, if both the name and the linkage name appear, we prefer
19791 the latter. This lets "catch exception" work better, regardless
19792 of the order in which the name and linkage name were emitted.
19793 Really, though, this is just a workaround for the fact that gdb
19794 doesn't store both the name and the linkage name. */
19795 if (cu->language == language_ada && linkage_name != nullptr)
19796 raw_name = linkage_name;
19797
19798 if (high_pc_relative)
19799 highpc += lowpc;
19800
19801 if (has_low_pc_attr && has_high_pc_attr)
19802 {
19803 /* When using the GNU linker, .gnu.linkonce. sections are used to
19804 eliminate duplicate copies of functions and vtables and such.
19805 The linker will arbitrarily choose one and discard the others.
19806 The AT_*_pc values for such functions refer to local labels in
19807 these sections. If the section from that file was discarded, the
19808 labels are not in the output, so the relocs get a value of 0.
19809 If this is a discarded function, mark the pc bounds as invalid,
19810 so that GDB will ignore it. */
19811 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
19812 {
19813 struct objfile *objfile = per_objfile->objfile;
19814 struct gdbarch *gdbarch = objfile->arch ();
19815
19816 complaint (_("DW_AT_low_pc %s is zero "
19817 "for DIE at %s [in module %s]"),
19818 paddress (gdbarch, lowpc),
19819 sect_offset_str (sect_off),
19820 objfile_name (objfile));
19821 }
19822 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19823 else if (lowpc >= highpc)
19824 {
19825 struct objfile *objfile = per_objfile->objfile;
19826 struct gdbarch *gdbarch = objfile->arch ();
19827
19828 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19829 "for DIE at %s [in module %s]"),
19830 paddress (gdbarch, lowpc),
19831 paddress (gdbarch, highpc),
19832 sect_offset_str (sect_off),
19833 objfile_name (objfile));
19834 }
19835 else
19836 has_pc_info = 1;
19837 }
19838
19839 return info_ptr;
19840 }
19841
19842 /* Find a cached partial DIE at OFFSET in CU. */
19843
19844 struct partial_die_info *
19845 dwarf2_cu::find_partial_die (sect_offset sect_off)
19846 {
19847 struct partial_die_info *lookup_die = NULL;
19848 struct partial_die_info part_die (sect_off);
19849
19850 lookup_die = ((struct partial_die_info *)
19851 htab_find_with_hash (partial_dies, &part_die,
19852 to_underlying (sect_off)));
19853
19854 return lookup_die;
19855 }
19856
19857 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19858 except in the case of .debug_types DIEs which do not reference
19859 outside their CU (they do however referencing other types via
19860 DW_FORM_ref_sig8). */
19861
19862 static const struct cu_partial_die_info
19863 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19864 {
19865 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19866 struct objfile *objfile = per_objfile->objfile;
19867 struct partial_die_info *pd = NULL;
19868
19869 if (offset_in_dwz == cu->per_cu->is_dwz
19870 && cu->header.offset_in_cu_p (sect_off))
19871 {
19872 pd = cu->find_partial_die (sect_off);
19873 if (pd != NULL)
19874 return { cu, pd };
19875 /* We missed recording what we needed.
19876 Load all dies and try again. */
19877 }
19878 else
19879 {
19880 /* TUs don't reference other CUs/TUs (except via type signatures). */
19881 if (cu->per_cu->is_debug_types)
19882 {
19883 error (_("Dwarf Error: Type Unit at offset %s contains"
19884 " external reference to offset %s [in module %s].\n"),
19885 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19886 bfd_get_filename (objfile->obfd));
19887 }
19888 dwarf2_per_cu_data *per_cu
19889 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19890 per_objfile);
19891
19892 cu = per_objfile->get_cu (per_cu);
19893 if (cu == NULL || cu->partial_dies == NULL)
19894 load_partial_comp_unit (per_cu, per_objfile, nullptr);
19895
19896 cu = per_objfile->get_cu (per_cu);
19897
19898 cu->last_used = 0;
19899 pd = cu->find_partial_die (sect_off);
19900 }
19901
19902 /* If we didn't find it, and not all dies have been loaded,
19903 load them all and try again. */
19904
19905 if (pd == NULL && cu->per_cu->load_all_dies == 0)
19906 {
19907 cu->per_cu->load_all_dies = 1;
19908
19909 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19910 THIS_CU->cu may already be in use. So we can't just free it and
19911 replace its DIEs with the ones we read in. Instead, we leave those
19912 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19913 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19914 set. */
19915 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
19916
19917 pd = cu->find_partial_die (sect_off);
19918 }
19919
19920 if (pd == NULL)
19921 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
19922 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19923 return { cu, pd };
19924 }
19925
19926 /* See if we can figure out if the class lives in a namespace. We do
19927 this by looking for a member function; its demangled name will
19928 contain namespace info, if there is any. */
19929
19930 static void
19931 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19932 struct dwarf2_cu *cu)
19933 {
19934 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19935 what template types look like, because the demangler
19936 frequently doesn't give the same name as the debug info. We
19937 could fix this by only using the demangled name to get the
19938 prefix (but see comment in read_structure_type). */
19939
19940 struct partial_die_info *real_pdi;
19941 struct partial_die_info *child_pdi;
19942
19943 /* If this DIE (this DIE's specification, if any) has a parent, then
19944 we should not do this. We'll prepend the parent's fully qualified
19945 name when we create the partial symbol. */
19946
19947 real_pdi = struct_pdi;
19948 while (real_pdi->has_specification)
19949 {
19950 auto res = find_partial_die (real_pdi->spec_offset,
19951 real_pdi->spec_is_dwz, cu);
19952 real_pdi = res.pdi;
19953 cu = res.cu;
19954 }
19955
19956 if (real_pdi->die_parent != NULL)
19957 return;
19958
19959 for (child_pdi = struct_pdi->die_child;
19960 child_pdi != NULL;
19961 child_pdi = child_pdi->die_sibling)
19962 {
19963 if (child_pdi->tag == DW_TAG_subprogram
19964 && child_pdi->linkage_name != NULL)
19965 {
19966 gdb::unique_xmalloc_ptr<char> actual_class_name
19967 (cu->language_defn->class_name_from_physname
19968 (child_pdi->linkage_name));
19969 if (actual_class_name != NULL)
19970 {
19971 struct objfile *objfile = cu->per_objfile->objfile;
19972 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19973 struct_pdi->canonical_name = 1;
19974 }
19975 break;
19976 }
19977 }
19978 }
19979
19980 /* Return true if a DIE with TAG may have the DW_AT_const_value
19981 attribute. */
19982
19983 static bool
19984 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19985 {
19986 switch (tag)
19987 {
19988 case DW_TAG_constant:
19989 case DW_TAG_enumerator:
19990 case DW_TAG_formal_parameter:
19991 case DW_TAG_template_value_param:
19992 case DW_TAG_variable:
19993 return true;
19994 }
19995
19996 return false;
19997 }
19998
19999 void
20000 partial_die_info::fixup (struct dwarf2_cu *cu)
20001 {
20002 /* Once we've fixed up a die, there's no point in doing so again.
20003 This also avoids a memory leak if we were to call
20004 guess_partial_die_structure_name multiple times. */
20005 if (fixup_called)
20006 return;
20007
20008 /* If we found a reference attribute and the DIE has no name, try
20009 to find a name in the referred to DIE. */
20010
20011 if (raw_name == NULL && has_specification)
20012 {
20013 struct partial_die_info *spec_die;
20014
20015 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20016 spec_die = res.pdi;
20017 cu = res.cu;
20018
20019 spec_die->fixup (cu);
20020
20021 if (spec_die->raw_name)
20022 {
20023 raw_name = spec_die->raw_name;
20024 canonical_name = spec_die->canonical_name;
20025
20026 /* Copy DW_AT_external attribute if it is set. */
20027 if (spec_die->is_external)
20028 is_external = spec_die->is_external;
20029 }
20030 }
20031
20032 if (!has_const_value && has_specification
20033 && can_have_DW_AT_const_value_p (tag))
20034 {
20035 struct partial_die_info *spec_die;
20036
20037 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20038 spec_die = res.pdi;
20039 cu = res.cu;
20040
20041 spec_die->fixup (cu);
20042
20043 if (spec_die->has_const_value)
20044 {
20045 /* Copy DW_AT_const_value attribute if it is set. */
20046 has_const_value = spec_die->has_const_value;
20047 }
20048 }
20049
20050 /* Set default names for some unnamed DIEs. */
20051
20052 if (raw_name == NULL && tag == DW_TAG_namespace)
20053 {
20054 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
20055 canonical_name = 1;
20056 }
20057
20058 /* If there is no parent die to provide a namespace, and there are
20059 children, see if we can determine the namespace from their linkage
20060 name. */
20061 if (cu->language == language_cplus
20062 && !cu->per_objfile->per_bfd->types.empty ()
20063 && die_parent == NULL
20064 && has_children
20065 && (tag == DW_TAG_class_type
20066 || tag == DW_TAG_structure_type
20067 || tag == DW_TAG_union_type))
20068 guess_partial_die_structure_name (this, cu);
20069
20070 /* GCC might emit a nameless struct or union that has a linkage
20071 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20072 if (raw_name == NULL
20073 && (tag == DW_TAG_class_type
20074 || tag == DW_TAG_interface_type
20075 || tag == DW_TAG_structure_type
20076 || tag == DW_TAG_union_type)
20077 && linkage_name != NULL)
20078 {
20079 gdb::unique_xmalloc_ptr<char> demangled
20080 (gdb_demangle (linkage_name, DMGL_TYPES));
20081 if (demangled != nullptr)
20082 {
20083 const char *base;
20084
20085 /* Strip any leading namespaces/classes, keep only the base name.
20086 DW_AT_name for named DIEs does not contain the prefixes. */
20087 base = strrchr (demangled.get (), ':');
20088 if (base && base > demangled.get () && base[-1] == ':')
20089 base++;
20090 else
20091 base = demangled.get ();
20092
20093 struct objfile *objfile = cu->per_objfile->objfile;
20094 raw_name = objfile->intern (base);
20095 canonical_name = 1;
20096 }
20097 }
20098
20099 fixup_called = 1;
20100 }
20101
20102 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
20103 contents from the given SECTION in the HEADER.
20104
20105 HEADER_OFFSET is the offset of the header in the section. */
20106 static void
20107 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
20108 struct dwarf2_section_info *section,
20109 sect_offset header_offset)
20110 {
20111 unsigned int bytes_read;
20112 bfd *abfd = section->get_bfd_owner ();
20113 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
20114
20115 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
20116 info_ptr += bytes_read;
20117
20118 header->version = read_2_bytes (abfd, info_ptr);
20119 info_ptr += 2;
20120
20121 header->addr_size = read_1_byte (abfd, info_ptr);
20122 info_ptr += 1;
20123
20124 header->segment_collector_size = read_1_byte (abfd, info_ptr);
20125 info_ptr += 1;
20126
20127 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
20128 }
20129
20130 /* Return the DW_AT_loclists_base value for the CU. */
20131 static ULONGEST
20132 lookup_loclist_base (struct dwarf2_cu *cu)
20133 {
20134 /* For the .dwo unit, the loclist_base points to the first offset following
20135 the header. The header consists of the following entities-
20136 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
20137 bit format)
20138 2. version (2 bytes)
20139 3. address size (1 byte)
20140 4. segment selector size (1 byte)
20141 5. offset entry count (4 bytes)
20142 These sizes are derived as per the DWARFv5 standard. */
20143 if (cu->dwo_unit != nullptr)
20144 {
20145 if (cu->header.initial_length_size == 4)
20146 return LOCLIST_HEADER_SIZE32;
20147 return LOCLIST_HEADER_SIZE64;
20148 }
20149 return cu->loclist_base;
20150 }
20151
20152 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
20153 array of offsets in the .debug_loclists section. */
20154
20155 static sect_offset
20156 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
20157 {
20158 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20159 struct objfile *objfile = per_objfile->objfile;
20160 bfd *abfd = objfile->obfd;
20161 ULONGEST loclist_header_size =
20162 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
20163 : LOCLIST_HEADER_SIZE64);
20164 ULONGEST loclist_base = lookup_loclist_base (cu);
20165
20166 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
20167 ULONGEST start_offset =
20168 loclist_base + loclist_index * cu->header.offset_size;
20169
20170 /* Get loclists section. */
20171 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20172
20173 /* Read the loclists section content. */
20174 section->read (objfile);
20175 if (section->buffer == NULL)
20176 error (_("DW_FORM_loclistx used without .debug_loclists "
20177 "section [in module %s]"), objfile_name (objfile));
20178
20179 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
20180 so if loclist_base is smaller than the header size, we have a problem. */
20181 if (loclist_base < loclist_header_size)
20182 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
20183 objfile_name (objfile));
20184
20185 /* Read the header of the loclists contribution. */
20186 struct loclists_rnglists_header header;
20187 read_loclists_rnglists_header (&header, section,
20188 (sect_offset) (loclist_base - loclist_header_size));
20189
20190 /* Verify the loclist index is valid. */
20191 if (loclist_index >= header.offset_entry_count)
20192 error (_("DW_FORM_loclistx pointing outside of "
20193 ".debug_loclists offset array [in module %s]"),
20194 objfile_name (objfile));
20195
20196 /* Validate that reading won't go beyond the end of the section. */
20197 if (start_offset + cu->header.offset_size > section->size)
20198 error (_("Reading DW_FORM_loclistx index beyond end of"
20199 ".debug_loclists section [in module %s]"),
20200 objfile_name (objfile));
20201
20202 const gdb_byte *info_ptr = section->buffer + start_offset;
20203
20204 if (cu->header.offset_size == 4)
20205 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
20206 else
20207 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
20208 }
20209
20210 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
20211 array of offsets in the .debug_rnglists section. */
20212
20213 static sect_offset
20214 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
20215 dwarf_tag tag)
20216 {
20217 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20218 struct objfile *objfile = dwarf2_per_objfile->objfile;
20219 bfd *abfd = objfile->obfd;
20220 ULONGEST rnglist_header_size =
20221 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
20222 : RNGLIST_HEADER_SIZE64);
20223
20224 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
20225 .debug_rnglists.dwo section. The rnglists base given in the skeleton
20226 doesn't apply. */
20227 ULONGEST rnglist_base =
20228 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
20229
20230 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
20231 ULONGEST start_offset =
20232 rnglist_base + rnglist_index * cu->header.offset_size;
20233
20234 /* Get rnglists section. */
20235 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
20236
20237 /* Read the rnglists section content. */
20238 section->read (objfile);
20239 if (section->buffer == nullptr)
20240 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
20241 "[in module %s]"),
20242 objfile_name (objfile));
20243
20244 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
20245 so if rnglist_base is smaller than the header size, we have a problem. */
20246 if (rnglist_base < rnglist_header_size)
20247 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
20248 objfile_name (objfile));
20249
20250 /* Read the header of the rnglists contribution. */
20251 struct loclists_rnglists_header header;
20252 read_loclists_rnglists_header (&header, section,
20253 (sect_offset) (rnglist_base - rnglist_header_size));
20254
20255 /* Verify the rnglist index is valid. */
20256 if (rnglist_index >= header.offset_entry_count)
20257 error (_("DW_FORM_rnglistx index pointing outside of "
20258 ".debug_rnglists offset array [in module %s]"),
20259 objfile_name (objfile));
20260
20261 /* Validate that reading won't go beyond the end of the section. */
20262 if (start_offset + cu->header.offset_size > section->size)
20263 error (_("Reading DW_FORM_rnglistx index beyond end of"
20264 ".debug_rnglists section [in module %s]"),
20265 objfile_name (objfile));
20266
20267 const gdb_byte *info_ptr = section->buffer + start_offset;
20268
20269 if (cu->header.offset_size == 4)
20270 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
20271 else
20272 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
20273 }
20274
20275 /* Process the attributes that had to be skipped in the first round. These
20276 attributes are the ones that need str_offsets_base or addr_base attributes.
20277 They could not have been processed in the first round, because at the time
20278 the values of str_offsets_base or addr_base may not have been known. */
20279 static void
20280 read_attribute_reprocess (const struct die_reader_specs *reader,
20281 struct attribute *attr, dwarf_tag tag)
20282 {
20283 struct dwarf2_cu *cu = reader->cu;
20284 switch (attr->form)
20285 {
20286 case DW_FORM_addrx:
20287 case DW_FORM_GNU_addr_index:
20288 attr->set_address (read_addr_index (cu,
20289 attr->as_unsigned_reprocess ()));
20290 break;
20291 case DW_FORM_loclistx:
20292 {
20293 sect_offset loclists_sect_off
20294 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
20295
20296 attr->set_unsigned (to_underlying (loclists_sect_off));
20297 }
20298 break;
20299 case DW_FORM_rnglistx:
20300 {
20301 sect_offset rnglists_sect_off
20302 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
20303
20304 attr->set_unsigned (to_underlying (rnglists_sect_off));
20305 }
20306 break;
20307 case DW_FORM_strx:
20308 case DW_FORM_strx1:
20309 case DW_FORM_strx2:
20310 case DW_FORM_strx3:
20311 case DW_FORM_strx4:
20312 case DW_FORM_GNU_str_index:
20313 {
20314 unsigned int str_index = attr->as_unsigned_reprocess ();
20315 gdb_assert (!attr->canonical_string_p ());
20316 if (reader->dwo_file != NULL)
20317 attr->set_string_noncanonical (read_dwo_str_index (reader,
20318 str_index));
20319 else
20320 attr->set_string_noncanonical (read_stub_str_index (cu,
20321 str_index));
20322 break;
20323 }
20324 default:
20325 gdb_assert_not_reached (_("Unexpected DWARF form."));
20326 }
20327 }
20328
20329 /* Read an attribute value described by an attribute form. */
20330
20331 static const gdb_byte *
20332 read_attribute_value (const struct die_reader_specs *reader,
20333 struct attribute *attr, unsigned form,
20334 LONGEST implicit_const, const gdb_byte *info_ptr)
20335 {
20336 struct dwarf2_cu *cu = reader->cu;
20337 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20338 struct objfile *objfile = per_objfile->objfile;
20339 bfd *abfd = reader->abfd;
20340 struct comp_unit_head *cu_header = &cu->header;
20341 unsigned int bytes_read;
20342 struct dwarf_block *blk;
20343
20344 attr->form = (enum dwarf_form) form;
20345 switch (form)
20346 {
20347 case DW_FORM_ref_addr:
20348 if (cu_header->version == 2)
20349 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
20350 &bytes_read));
20351 else
20352 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20353 &bytes_read));
20354 info_ptr += bytes_read;
20355 break;
20356 case DW_FORM_GNU_ref_alt:
20357 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20358 &bytes_read));
20359 info_ptr += bytes_read;
20360 break;
20361 case DW_FORM_addr:
20362 {
20363 struct gdbarch *gdbarch = objfile->arch ();
20364 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
20365 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20366 attr->set_address (addr);
20367 info_ptr += bytes_read;
20368 }
20369 break;
20370 case DW_FORM_block2:
20371 blk = dwarf_alloc_block (cu);
20372 blk->size = read_2_bytes (abfd, info_ptr);
20373 info_ptr += 2;
20374 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20375 info_ptr += blk->size;
20376 attr->set_block (blk);
20377 break;
20378 case DW_FORM_block4:
20379 blk = dwarf_alloc_block (cu);
20380 blk->size = read_4_bytes (abfd, info_ptr);
20381 info_ptr += 4;
20382 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20383 info_ptr += blk->size;
20384 attr->set_block (blk);
20385 break;
20386 case DW_FORM_data2:
20387 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
20388 info_ptr += 2;
20389 break;
20390 case DW_FORM_data4:
20391 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
20392 info_ptr += 4;
20393 break;
20394 case DW_FORM_data8:
20395 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
20396 info_ptr += 8;
20397 break;
20398 case DW_FORM_data16:
20399 blk = dwarf_alloc_block (cu);
20400 blk->size = 16;
20401 blk->data = read_n_bytes (abfd, info_ptr, 16);
20402 info_ptr += 16;
20403 attr->set_block (blk);
20404 break;
20405 case DW_FORM_sec_offset:
20406 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20407 &bytes_read));
20408 info_ptr += bytes_read;
20409 break;
20410 case DW_FORM_loclistx:
20411 {
20412 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20413 &bytes_read));
20414 info_ptr += bytes_read;
20415 }
20416 break;
20417 case DW_FORM_string:
20418 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20419 &bytes_read));
20420 info_ptr += bytes_read;
20421 break;
20422 case DW_FORM_strp:
20423 if (!cu->per_cu->is_dwz)
20424 {
20425 attr->set_string_noncanonical
20426 (read_indirect_string (per_objfile,
20427 abfd, info_ptr, cu_header,
20428 &bytes_read));
20429 info_ptr += bytes_read;
20430 break;
20431 }
20432 /* FALLTHROUGH */
20433 case DW_FORM_line_strp:
20434 if (!cu->per_cu->is_dwz)
20435 {
20436 attr->set_string_noncanonical
20437 (per_objfile->read_line_string (info_ptr, cu_header,
20438 &bytes_read));
20439 info_ptr += bytes_read;
20440 break;
20441 }
20442 /* FALLTHROUGH */
20443 case DW_FORM_GNU_strp_alt:
20444 {
20445 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20446 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20447 &bytes_read);
20448
20449 attr->set_string_noncanonical
20450 (dwz->read_string (objfile, str_offset));
20451 info_ptr += bytes_read;
20452 }
20453 break;
20454 case DW_FORM_exprloc:
20455 case DW_FORM_block:
20456 blk = dwarf_alloc_block (cu);
20457 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20458 info_ptr += bytes_read;
20459 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20460 info_ptr += blk->size;
20461 attr->set_block (blk);
20462 break;
20463 case DW_FORM_block1:
20464 blk = dwarf_alloc_block (cu);
20465 blk->size = read_1_byte (abfd, info_ptr);
20466 info_ptr += 1;
20467 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20468 info_ptr += blk->size;
20469 attr->set_block (blk);
20470 break;
20471 case DW_FORM_data1:
20472 case DW_FORM_flag:
20473 attr->set_unsigned (read_1_byte (abfd, info_ptr));
20474 info_ptr += 1;
20475 break;
20476 case DW_FORM_flag_present:
20477 attr->set_unsigned (1);
20478 break;
20479 case DW_FORM_sdata:
20480 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
20481 info_ptr += bytes_read;
20482 break;
20483 case DW_FORM_rnglistx:
20484 {
20485 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20486 &bytes_read));
20487 info_ptr += bytes_read;
20488 }
20489 break;
20490 case DW_FORM_udata:
20491 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
20492 info_ptr += bytes_read;
20493 break;
20494 case DW_FORM_ref1:
20495 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20496 + read_1_byte (abfd, info_ptr)));
20497 info_ptr += 1;
20498 break;
20499 case DW_FORM_ref2:
20500 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20501 + read_2_bytes (abfd, info_ptr)));
20502 info_ptr += 2;
20503 break;
20504 case DW_FORM_ref4:
20505 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20506 + read_4_bytes (abfd, info_ptr)));
20507 info_ptr += 4;
20508 break;
20509 case DW_FORM_ref8:
20510 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20511 + read_8_bytes (abfd, info_ptr)));
20512 info_ptr += 8;
20513 break;
20514 case DW_FORM_ref_sig8:
20515 attr->set_signature (read_8_bytes (abfd, info_ptr));
20516 info_ptr += 8;
20517 break;
20518 case DW_FORM_ref_udata:
20519 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20520 + read_unsigned_leb128 (abfd, info_ptr,
20521 &bytes_read)));
20522 info_ptr += bytes_read;
20523 break;
20524 case DW_FORM_indirect:
20525 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20526 info_ptr += bytes_read;
20527 if (form == DW_FORM_implicit_const)
20528 {
20529 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20530 info_ptr += bytes_read;
20531 }
20532 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
20533 info_ptr);
20534 break;
20535 case DW_FORM_implicit_const:
20536 attr->set_signed (implicit_const);
20537 break;
20538 case DW_FORM_addrx:
20539 case DW_FORM_GNU_addr_index:
20540 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20541 &bytes_read));
20542 info_ptr += bytes_read;
20543 break;
20544 case DW_FORM_strx:
20545 case DW_FORM_strx1:
20546 case DW_FORM_strx2:
20547 case DW_FORM_strx3:
20548 case DW_FORM_strx4:
20549 case DW_FORM_GNU_str_index:
20550 {
20551 ULONGEST str_index;
20552 if (form == DW_FORM_strx1)
20553 {
20554 str_index = read_1_byte (abfd, info_ptr);
20555 info_ptr += 1;
20556 }
20557 else if (form == DW_FORM_strx2)
20558 {
20559 str_index = read_2_bytes (abfd, info_ptr);
20560 info_ptr += 2;
20561 }
20562 else if (form == DW_FORM_strx3)
20563 {
20564 str_index = read_3_bytes (abfd, info_ptr);
20565 info_ptr += 3;
20566 }
20567 else if (form == DW_FORM_strx4)
20568 {
20569 str_index = read_4_bytes (abfd, info_ptr);
20570 info_ptr += 4;
20571 }
20572 else
20573 {
20574 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20575 info_ptr += bytes_read;
20576 }
20577 attr->set_unsigned_reprocess (str_index);
20578 }
20579 break;
20580 default:
20581 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
20582 dwarf_form_name (form),
20583 bfd_get_filename (abfd));
20584 }
20585
20586 /* Super hack. */
20587 if (cu->per_cu->is_dwz && attr->form_is_ref ())
20588 attr->form = DW_FORM_GNU_ref_alt;
20589
20590 /* We have seen instances where the compiler tried to emit a byte
20591 size attribute of -1 which ended up being encoded as an unsigned
20592 0xffffffff. Although 0xffffffff is technically a valid size value,
20593 an object of this size seems pretty unlikely so we can relatively
20594 safely treat these cases as if the size attribute was invalid and
20595 treat them as zero by default. */
20596 if (attr->name == DW_AT_byte_size
20597 && form == DW_FORM_data4
20598 && attr->as_unsigned () >= 0xffffffff)
20599 {
20600 complaint
20601 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20602 hex_string (attr->as_unsigned ()));
20603 attr->set_unsigned (0);
20604 }
20605
20606 return info_ptr;
20607 }
20608
20609 /* Read an attribute described by an abbreviated attribute. */
20610
20611 static const gdb_byte *
20612 read_attribute (const struct die_reader_specs *reader,
20613 struct attribute *attr, const struct attr_abbrev *abbrev,
20614 const gdb_byte *info_ptr)
20615 {
20616 attr->name = abbrev->name;
20617 attr->string_is_canonical = 0;
20618 attr->requires_reprocessing = 0;
20619 return read_attribute_value (reader, attr, abbrev->form,
20620 abbrev->implicit_const, info_ptr);
20621 }
20622
20623 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20624
20625 static const char *
20626 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
20627 LONGEST str_offset)
20628 {
20629 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20630 str_offset, "DW_FORM_strp");
20631 }
20632
20633 /* Return pointer to string at .debug_str offset as read from BUF.
20634 BUF is assumed to be in a compilation unit described by CU_HEADER.
20635 Return *BYTES_READ_PTR count of bytes read from BUF. */
20636
20637 static const char *
20638 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
20639 const gdb_byte *buf,
20640 const struct comp_unit_head *cu_header,
20641 unsigned int *bytes_read_ptr)
20642 {
20643 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20644
20645 return read_indirect_string_at_offset (per_objfile, str_offset);
20646 }
20647
20648 /* See read.h. */
20649
20650 const char *
20651 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20652 const struct comp_unit_head *cu_header,
20653 unsigned int *bytes_read_ptr)
20654 {
20655 bfd *abfd = objfile->obfd;
20656 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20657
20658 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20659 }
20660
20661 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20662 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20663 ADDR_SIZE is the size of addresses from the CU header. */
20664
20665 static CORE_ADDR
20666 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20667 gdb::optional<ULONGEST> addr_base, int addr_size)
20668 {
20669 struct objfile *objfile = per_objfile->objfile;
20670 bfd *abfd = objfile->obfd;
20671 const gdb_byte *info_ptr;
20672 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20673
20674 per_objfile->per_bfd->addr.read (objfile);
20675 if (per_objfile->per_bfd->addr.buffer == NULL)
20676 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20677 objfile_name (objfile));
20678 if (addr_base_or_zero + addr_index * addr_size
20679 >= per_objfile->per_bfd->addr.size)
20680 error (_("DW_FORM_addr_index pointing outside of "
20681 ".debug_addr section [in module %s]"),
20682 objfile_name (objfile));
20683 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20684 + addr_index * addr_size);
20685 if (addr_size == 4)
20686 return bfd_get_32 (abfd, info_ptr);
20687 else
20688 return bfd_get_64 (abfd, info_ptr);
20689 }
20690
20691 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20692
20693 static CORE_ADDR
20694 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20695 {
20696 return read_addr_index_1 (cu->per_objfile, addr_index,
20697 cu->addr_base, cu->header.addr_size);
20698 }
20699
20700 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20701
20702 static CORE_ADDR
20703 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20704 unsigned int *bytes_read)
20705 {
20706 bfd *abfd = cu->per_objfile->objfile->obfd;
20707 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20708
20709 return read_addr_index (cu, addr_index);
20710 }
20711
20712 /* See read.h. */
20713
20714 CORE_ADDR
20715 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20716 dwarf2_per_objfile *per_objfile,
20717 unsigned int addr_index)
20718 {
20719 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20720 gdb::optional<ULONGEST> addr_base;
20721 int addr_size;
20722
20723 /* We need addr_base and addr_size.
20724 If we don't have PER_CU->cu, we have to get it.
20725 Nasty, but the alternative is storing the needed info in PER_CU,
20726 which at this point doesn't seem justified: it's not clear how frequently
20727 it would get used and it would increase the size of every PER_CU.
20728 Entry points like dwarf2_per_cu_addr_size do a similar thing
20729 so we're not in uncharted territory here.
20730 Alas we need to be a bit more complicated as addr_base is contained
20731 in the DIE.
20732
20733 We don't need to read the entire CU(/TU).
20734 We just need the header and top level die.
20735
20736 IWBN to use the aging mechanism to let us lazily later discard the CU.
20737 For now we skip this optimization. */
20738
20739 if (cu != NULL)
20740 {
20741 addr_base = cu->addr_base;
20742 addr_size = cu->header.addr_size;
20743 }
20744 else
20745 {
20746 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
20747 addr_base = reader.cu->addr_base;
20748 addr_size = reader.cu->header.addr_size;
20749 }
20750
20751 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
20752 }
20753
20754 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20755 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20756 DWO file. */
20757
20758 static const char *
20759 read_str_index (struct dwarf2_cu *cu,
20760 struct dwarf2_section_info *str_section,
20761 struct dwarf2_section_info *str_offsets_section,
20762 ULONGEST str_offsets_base, ULONGEST str_index)
20763 {
20764 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20765 struct objfile *objfile = per_objfile->objfile;
20766 const char *objf_name = objfile_name (objfile);
20767 bfd *abfd = objfile->obfd;
20768 const gdb_byte *info_ptr;
20769 ULONGEST str_offset;
20770 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20771
20772 str_section->read (objfile);
20773 str_offsets_section->read (objfile);
20774 if (str_section->buffer == NULL)
20775 error (_("%s used without %s section"
20776 " in CU at offset %s [in module %s]"),
20777 form_name, str_section->get_name (),
20778 sect_offset_str (cu->header.sect_off), objf_name);
20779 if (str_offsets_section->buffer == NULL)
20780 error (_("%s used without %s section"
20781 " in CU at offset %s [in module %s]"),
20782 form_name, str_section->get_name (),
20783 sect_offset_str (cu->header.sect_off), objf_name);
20784 info_ptr = (str_offsets_section->buffer
20785 + str_offsets_base
20786 + str_index * cu->header.offset_size);
20787 if (cu->header.offset_size == 4)
20788 str_offset = bfd_get_32 (abfd, info_ptr);
20789 else
20790 str_offset = bfd_get_64 (abfd, info_ptr);
20791 if (str_offset >= str_section->size)
20792 error (_("Offset from %s pointing outside of"
20793 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20794 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20795 return (const char *) (str_section->buffer + str_offset);
20796 }
20797
20798 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20799
20800 static const char *
20801 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20802 {
20803 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20804 ? reader->cu->header.addr_size : 0;
20805 return read_str_index (reader->cu,
20806 &reader->dwo_file->sections.str,
20807 &reader->dwo_file->sections.str_offsets,
20808 str_offsets_base, str_index);
20809 }
20810
20811 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20812
20813 static const char *
20814 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20815 {
20816 struct objfile *objfile = cu->per_objfile->objfile;
20817 const char *objf_name = objfile_name (objfile);
20818 static const char form_name[] = "DW_FORM_GNU_str_index";
20819 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20820
20821 if (!cu->str_offsets_base.has_value ())
20822 error (_("%s used in Fission stub without %s"
20823 " in CU at offset 0x%lx [in module %s]"),
20824 form_name, str_offsets_attr_name,
20825 (long) cu->header.offset_size, objf_name);
20826
20827 return read_str_index (cu,
20828 &cu->per_objfile->per_bfd->str,
20829 &cu->per_objfile->per_bfd->str_offsets,
20830 *cu->str_offsets_base, str_index);
20831 }
20832
20833 /* Return the length of an LEB128 number in BUF. */
20834
20835 static int
20836 leb128_size (const gdb_byte *buf)
20837 {
20838 const gdb_byte *begin = buf;
20839 gdb_byte byte;
20840
20841 while (1)
20842 {
20843 byte = *buf++;
20844 if ((byte & 128) == 0)
20845 return buf - begin;
20846 }
20847 }
20848
20849 static void
20850 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20851 {
20852 switch (lang)
20853 {
20854 case DW_LANG_C89:
20855 case DW_LANG_C99:
20856 case DW_LANG_C11:
20857 case DW_LANG_C:
20858 case DW_LANG_UPC:
20859 cu->language = language_c;
20860 break;
20861 case DW_LANG_Java:
20862 case DW_LANG_C_plus_plus:
20863 case DW_LANG_C_plus_plus_11:
20864 case DW_LANG_C_plus_plus_14:
20865 cu->language = language_cplus;
20866 break;
20867 case DW_LANG_D:
20868 cu->language = language_d;
20869 break;
20870 case DW_LANG_Fortran77:
20871 case DW_LANG_Fortran90:
20872 case DW_LANG_Fortran95:
20873 case DW_LANG_Fortran03:
20874 case DW_LANG_Fortran08:
20875 cu->language = language_fortran;
20876 break;
20877 case DW_LANG_Go:
20878 cu->language = language_go;
20879 break;
20880 case DW_LANG_Mips_Assembler:
20881 cu->language = language_asm;
20882 break;
20883 case DW_LANG_Ada83:
20884 case DW_LANG_Ada95:
20885 cu->language = language_ada;
20886 break;
20887 case DW_LANG_Modula2:
20888 cu->language = language_m2;
20889 break;
20890 case DW_LANG_Pascal83:
20891 cu->language = language_pascal;
20892 break;
20893 case DW_LANG_ObjC:
20894 cu->language = language_objc;
20895 break;
20896 case DW_LANG_Rust:
20897 case DW_LANG_Rust_old:
20898 cu->language = language_rust;
20899 break;
20900 case DW_LANG_Cobol74:
20901 case DW_LANG_Cobol85:
20902 default:
20903 cu->language = language_minimal;
20904 break;
20905 }
20906 cu->language_defn = language_def (cu->language);
20907 }
20908
20909 /* Return the named attribute or NULL if not there. */
20910
20911 static struct attribute *
20912 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20913 {
20914 for (;;)
20915 {
20916 unsigned int i;
20917 struct attribute *spec = NULL;
20918
20919 for (i = 0; i < die->num_attrs; ++i)
20920 {
20921 if (die->attrs[i].name == name)
20922 return &die->attrs[i];
20923 if (die->attrs[i].name == DW_AT_specification
20924 || die->attrs[i].name == DW_AT_abstract_origin)
20925 spec = &die->attrs[i];
20926 }
20927
20928 if (!spec)
20929 break;
20930
20931 die = follow_die_ref (die, spec, &cu);
20932 }
20933
20934 return NULL;
20935 }
20936
20937 /* Return the string associated with a string-typed attribute, or NULL if it
20938 is either not found or is of an incorrect type. */
20939
20940 static const char *
20941 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20942 {
20943 struct attribute *attr;
20944 const char *str = NULL;
20945
20946 attr = dwarf2_attr (die, name, cu);
20947
20948 if (attr != NULL)
20949 {
20950 str = attr->as_string ();
20951 if (str == nullptr)
20952 complaint (_("string type expected for attribute %s for "
20953 "DIE at %s in module %s"),
20954 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20955 objfile_name (cu->per_objfile->objfile));
20956 }
20957
20958 return str;
20959 }
20960
20961 /* Return the dwo name or NULL if not present. If present, it is in either
20962 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20963 static const char *
20964 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20965 {
20966 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20967 if (dwo_name == nullptr)
20968 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20969 return dwo_name;
20970 }
20971
20972 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20973 and holds a non-zero value. This function should only be used for
20974 DW_FORM_flag or DW_FORM_flag_present attributes. */
20975
20976 static int
20977 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20978 {
20979 struct attribute *attr = dwarf2_attr (die, name, cu);
20980
20981 return attr != nullptr && attr->as_boolean ();
20982 }
20983
20984 static int
20985 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20986 {
20987 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20988 which value is non-zero. However, we have to be careful with
20989 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20990 (via dwarf2_flag_true_p) follows this attribute. So we may
20991 end up accidently finding a declaration attribute that belongs
20992 to a different DIE referenced by the specification attribute,
20993 even though the given DIE does not have a declaration attribute. */
20994 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20995 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20996 }
20997
20998 /* Return the die giving the specification for DIE, if there is
20999 one. *SPEC_CU is the CU containing DIE on input, and the CU
21000 containing the return value on output. If there is no
21001 specification, but there is an abstract origin, that is
21002 returned. */
21003
21004 static struct die_info *
21005 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
21006 {
21007 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
21008 *spec_cu);
21009
21010 if (spec_attr == NULL)
21011 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
21012
21013 if (spec_attr == NULL)
21014 return NULL;
21015 else
21016 return follow_die_ref (die, spec_attr, spec_cu);
21017 }
21018
21019 /* Stub for free_line_header to match void * callback types. */
21020
21021 static void
21022 free_line_header_voidp (void *arg)
21023 {
21024 struct line_header *lh = (struct line_header *) arg;
21025
21026 delete lh;
21027 }
21028
21029 /* A convenience function to find the proper .debug_line section for a CU. */
21030
21031 static struct dwarf2_section_info *
21032 get_debug_line_section (struct dwarf2_cu *cu)
21033 {
21034 struct dwarf2_section_info *section;
21035 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21036
21037 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
21038 DWO file. */
21039 if (cu->dwo_unit && cu->per_cu->is_debug_types)
21040 section = &cu->dwo_unit->dwo_file->sections.line;
21041 else if (cu->per_cu->is_dwz)
21042 {
21043 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
21044
21045 section = &dwz->line;
21046 }
21047 else
21048 section = &per_objfile->per_bfd->line;
21049
21050 return section;
21051 }
21052
21053 /* Read the statement program header starting at OFFSET in
21054 .debug_line, or .debug_line.dwo. Return a pointer
21055 to a struct line_header, allocated using xmalloc.
21056 Returns NULL if there is a problem reading the header, e.g., if it
21057 has a version we don't understand.
21058
21059 NOTE: the strings in the include directory and file name tables of
21060 the returned object point into the dwarf line section buffer,
21061 and must not be freed. */
21062
21063 static line_header_up
21064 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
21065 {
21066 struct dwarf2_section_info *section;
21067 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21068
21069 section = get_debug_line_section (cu);
21070 section->read (per_objfile->objfile);
21071 if (section->buffer == NULL)
21072 {
21073 if (cu->dwo_unit && cu->per_cu->is_debug_types)
21074 complaint (_("missing .debug_line.dwo section"));
21075 else
21076 complaint (_("missing .debug_line section"));
21077 return 0;
21078 }
21079
21080 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
21081 per_objfile, section, &cu->header);
21082 }
21083
21084 /* Subroutine of dwarf_decode_lines to simplify it.
21085 Return the file name of the psymtab for the given file_entry.
21086 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21087 If space for the result is malloc'd, *NAME_HOLDER will be set.
21088 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
21089
21090 static const char *
21091 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
21092 const dwarf2_psymtab *pst,
21093 const char *comp_dir,
21094 gdb::unique_xmalloc_ptr<char> *name_holder)
21095 {
21096 const char *include_name = fe.name;
21097 const char *include_name_to_compare = include_name;
21098 const char *pst_filename;
21099 int file_is_pst;
21100
21101 const char *dir_name = fe.include_dir (lh);
21102
21103 gdb::unique_xmalloc_ptr<char> hold_compare;
21104 if (!IS_ABSOLUTE_PATH (include_name)
21105 && (dir_name != NULL || comp_dir != NULL))
21106 {
21107 /* Avoid creating a duplicate psymtab for PST.
21108 We do this by comparing INCLUDE_NAME and PST_FILENAME.
21109 Before we do the comparison, however, we need to account
21110 for DIR_NAME and COMP_DIR.
21111 First prepend dir_name (if non-NULL). If we still don't
21112 have an absolute path prepend comp_dir (if non-NULL).
21113 However, the directory we record in the include-file's
21114 psymtab does not contain COMP_DIR (to match the
21115 corresponding symtab(s)).
21116
21117 Example:
21118
21119 bash$ cd /tmp
21120 bash$ gcc -g ./hello.c
21121 include_name = "hello.c"
21122 dir_name = "."
21123 DW_AT_comp_dir = comp_dir = "/tmp"
21124 DW_AT_name = "./hello.c"
21125
21126 */
21127
21128 if (dir_name != NULL)
21129 {
21130 name_holder->reset (concat (dir_name, SLASH_STRING,
21131 include_name, (char *) NULL));
21132 include_name = name_holder->get ();
21133 include_name_to_compare = include_name;
21134 }
21135 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
21136 {
21137 hold_compare.reset (concat (comp_dir, SLASH_STRING,
21138 include_name, (char *) NULL));
21139 include_name_to_compare = hold_compare.get ();
21140 }
21141 }
21142
21143 pst_filename = pst->filename;
21144 gdb::unique_xmalloc_ptr<char> copied_name;
21145 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
21146 {
21147 copied_name.reset (concat (pst->dirname, SLASH_STRING,
21148 pst_filename, (char *) NULL));
21149 pst_filename = copied_name.get ();
21150 }
21151
21152 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
21153
21154 if (file_is_pst)
21155 return NULL;
21156 return include_name;
21157 }
21158
21159 /* State machine to track the state of the line number program. */
21160
21161 class lnp_state_machine
21162 {
21163 public:
21164 /* Initialize a machine state for the start of a line number
21165 program. */
21166 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
21167 bool record_lines_p);
21168
21169 file_entry *current_file ()
21170 {
21171 /* lh->file_names is 0-based, but the file name numbers in the
21172 statement program are 1-based. */
21173 return m_line_header->file_name_at (m_file);
21174 }
21175
21176 /* Record the line in the state machine. END_SEQUENCE is true if
21177 we're processing the end of a sequence. */
21178 void record_line (bool end_sequence);
21179
21180 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
21181 nop-out rest of the lines in this sequence. */
21182 void check_line_address (struct dwarf2_cu *cu,
21183 const gdb_byte *line_ptr,
21184 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
21185
21186 void handle_set_discriminator (unsigned int discriminator)
21187 {
21188 m_discriminator = discriminator;
21189 m_line_has_non_zero_discriminator |= discriminator != 0;
21190 }
21191
21192 /* Handle DW_LNE_set_address. */
21193 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21194 {
21195 m_op_index = 0;
21196 address += baseaddr;
21197 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21198 }
21199
21200 /* Handle DW_LNS_advance_pc. */
21201 void handle_advance_pc (CORE_ADDR adjust);
21202
21203 /* Handle a special opcode. */
21204 void handle_special_opcode (unsigned char op_code);
21205
21206 /* Handle DW_LNS_advance_line. */
21207 void handle_advance_line (int line_delta)
21208 {
21209 advance_line (line_delta);
21210 }
21211
21212 /* Handle DW_LNS_set_file. */
21213 void handle_set_file (file_name_index file);
21214
21215 /* Handle DW_LNS_negate_stmt. */
21216 void handle_negate_stmt ()
21217 {
21218 m_is_stmt = !m_is_stmt;
21219 }
21220
21221 /* Handle DW_LNS_const_add_pc. */
21222 void handle_const_add_pc ();
21223
21224 /* Handle DW_LNS_fixed_advance_pc. */
21225 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21226 {
21227 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21228 m_op_index = 0;
21229 }
21230
21231 /* Handle DW_LNS_copy. */
21232 void handle_copy ()
21233 {
21234 record_line (false);
21235 m_discriminator = 0;
21236 }
21237
21238 /* Handle DW_LNE_end_sequence. */
21239 void handle_end_sequence ()
21240 {
21241 m_currently_recording_lines = true;
21242 }
21243
21244 private:
21245 /* Advance the line by LINE_DELTA. */
21246 void advance_line (int line_delta)
21247 {
21248 m_line += line_delta;
21249
21250 if (line_delta != 0)
21251 m_line_has_non_zero_discriminator = m_discriminator != 0;
21252 }
21253
21254 struct dwarf2_cu *m_cu;
21255
21256 gdbarch *m_gdbarch;
21257
21258 /* True if we're recording lines.
21259 Otherwise we're building partial symtabs and are just interested in
21260 finding include files mentioned by the line number program. */
21261 bool m_record_lines_p;
21262
21263 /* The line number header. */
21264 line_header *m_line_header;
21265
21266 /* These are part of the standard DWARF line number state machine,
21267 and initialized according to the DWARF spec. */
21268
21269 unsigned char m_op_index = 0;
21270 /* The line table index of the current file. */
21271 file_name_index m_file = 1;
21272 unsigned int m_line = 1;
21273
21274 /* These are initialized in the constructor. */
21275
21276 CORE_ADDR m_address;
21277 bool m_is_stmt;
21278 unsigned int m_discriminator;
21279
21280 /* Additional bits of state we need to track. */
21281
21282 /* The last file that we called dwarf2_start_subfile for.
21283 This is only used for TLLs. */
21284 unsigned int m_last_file = 0;
21285 /* The last file a line number was recorded for. */
21286 struct subfile *m_last_subfile = NULL;
21287
21288 /* The address of the last line entry. */
21289 CORE_ADDR m_last_address;
21290
21291 /* Set to true when a previous line at the same address (using
21292 m_last_address) had m_is_stmt true. This is reset to false when a
21293 line entry at a new address (m_address different to m_last_address) is
21294 processed. */
21295 bool m_stmt_at_address = false;
21296
21297 /* When true, record the lines we decode. */
21298 bool m_currently_recording_lines = false;
21299
21300 /* The last line number that was recorded, used to coalesce
21301 consecutive entries for the same line. This can happen, for
21302 example, when discriminators are present. PR 17276. */
21303 unsigned int m_last_line = 0;
21304 bool m_line_has_non_zero_discriminator = false;
21305 };
21306
21307 void
21308 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21309 {
21310 CORE_ADDR addr_adj = (((m_op_index + adjust)
21311 / m_line_header->maximum_ops_per_instruction)
21312 * m_line_header->minimum_instruction_length);
21313 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21314 m_op_index = ((m_op_index + adjust)
21315 % m_line_header->maximum_ops_per_instruction);
21316 }
21317
21318 void
21319 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21320 {
21321 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21322 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21323 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21324 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
21325 / m_line_header->maximum_ops_per_instruction)
21326 * m_line_header->minimum_instruction_length);
21327 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21328 m_op_index = ((m_op_index + adj_opcode_d)
21329 % m_line_header->maximum_ops_per_instruction);
21330
21331 int line_delta = m_line_header->line_base + adj_opcode_r;
21332 advance_line (line_delta);
21333 record_line (false);
21334 m_discriminator = 0;
21335 }
21336
21337 void
21338 lnp_state_machine::handle_set_file (file_name_index file)
21339 {
21340 m_file = file;
21341
21342 const file_entry *fe = current_file ();
21343 if (fe == NULL)
21344 dwarf2_debug_line_missing_file_complaint ();
21345 else if (m_record_lines_p)
21346 {
21347 const char *dir = fe->include_dir (m_line_header);
21348
21349 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21350 m_line_has_non_zero_discriminator = m_discriminator != 0;
21351 dwarf2_start_subfile (m_cu, fe->name, dir);
21352 }
21353 }
21354
21355 void
21356 lnp_state_machine::handle_const_add_pc ()
21357 {
21358 CORE_ADDR adjust
21359 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21360
21361 CORE_ADDR addr_adj
21362 = (((m_op_index + adjust)
21363 / m_line_header->maximum_ops_per_instruction)
21364 * m_line_header->minimum_instruction_length);
21365
21366 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21367 m_op_index = ((m_op_index + adjust)
21368 % m_line_header->maximum_ops_per_instruction);
21369 }
21370
21371 /* Return non-zero if we should add LINE to the line number table.
21372 LINE is the line to add, LAST_LINE is the last line that was added,
21373 LAST_SUBFILE is the subfile for LAST_LINE.
21374 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21375 had a non-zero discriminator.
21376
21377 We have to be careful in the presence of discriminators.
21378 E.g., for this line:
21379
21380 for (i = 0; i < 100000; i++);
21381
21382 clang can emit four line number entries for that one line,
21383 each with a different discriminator.
21384 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21385
21386 However, we want gdb to coalesce all four entries into one.
21387 Otherwise the user could stepi into the middle of the line and
21388 gdb would get confused about whether the pc really was in the
21389 middle of the line.
21390
21391 Things are further complicated by the fact that two consecutive
21392 line number entries for the same line is a heuristic used by gcc
21393 to denote the end of the prologue. So we can't just discard duplicate
21394 entries, we have to be selective about it. The heuristic we use is
21395 that we only collapse consecutive entries for the same line if at least
21396 one of those entries has a non-zero discriminator. PR 17276.
21397
21398 Note: Addresses in the line number state machine can never go backwards
21399 within one sequence, thus this coalescing is ok. */
21400
21401 static int
21402 dwarf_record_line_p (struct dwarf2_cu *cu,
21403 unsigned int line, unsigned int last_line,
21404 int line_has_non_zero_discriminator,
21405 struct subfile *last_subfile)
21406 {
21407 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21408 return 1;
21409 if (line != last_line)
21410 return 1;
21411 /* Same line for the same file that we've seen already.
21412 As a last check, for pr 17276, only record the line if the line
21413 has never had a non-zero discriminator. */
21414 if (!line_has_non_zero_discriminator)
21415 return 1;
21416 return 0;
21417 }
21418
21419 /* Use the CU's builder to record line number LINE beginning at
21420 address ADDRESS in the line table of subfile SUBFILE. */
21421
21422 static void
21423 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21424 unsigned int line, CORE_ADDR address, bool is_stmt,
21425 struct dwarf2_cu *cu)
21426 {
21427 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21428
21429 if (dwarf_line_debug)
21430 {
21431 fprintf_unfiltered (gdb_stdlog,
21432 "Recording line %u, file %s, address %s\n",
21433 line, lbasename (subfile->name),
21434 paddress (gdbarch, address));
21435 }
21436
21437 if (cu != nullptr)
21438 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
21439 }
21440
21441 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21442 Mark the end of a set of line number records.
21443 The arguments are the same as for dwarf_record_line_1.
21444 If SUBFILE is NULL the request is ignored. */
21445
21446 static void
21447 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21448 CORE_ADDR address, struct dwarf2_cu *cu)
21449 {
21450 if (subfile == NULL)
21451 return;
21452
21453 if (dwarf_line_debug)
21454 {
21455 fprintf_unfiltered (gdb_stdlog,
21456 "Finishing current line, file %s, address %s\n",
21457 lbasename (subfile->name),
21458 paddress (gdbarch, address));
21459 }
21460
21461 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
21462 }
21463
21464 void
21465 lnp_state_machine::record_line (bool end_sequence)
21466 {
21467 if (dwarf_line_debug)
21468 {
21469 fprintf_unfiltered (gdb_stdlog,
21470 "Processing actual line %u: file %u,"
21471 " address %s, is_stmt %u, discrim %u%s\n",
21472 m_line, m_file,
21473 paddress (m_gdbarch, m_address),
21474 m_is_stmt, m_discriminator,
21475 (end_sequence ? "\t(end sequence)" : ""));
21476 }
21477
21478 file_entry *fe = current_file ();
21479
21480 if (fe == NULL)
21481 dwarf2_debug_line_missing_file_complaint ();
21482 /* For now we ignore lines not starting on an instruction boundary.
21483 But not when processing end_sequence for compatibility with the
21484 previous version of the code. */
21485 else if (m_op_index == 0 || end_sequence)
21486 {
21487 fe->included_p = 1;
21488 if (m_record_lines_p)
21489 {
21490 /* When we switch files we insert an end maker in the first file,
21491 switch to the second file and add a new line entry. The
21492 problem is that the end marker inserted in the first file will
21493 discard any previous line entries at the same address. If the
21494 line entries in the first file are marked as is-stmt, while
21495 the new line in the second file is non-stmt, then this means
21496 the end marker will discard is-stmt lines so we can have a
21497 non-stmt line. This means that there are less addresses at
21498 which the user can insert a breakpoint.
21499
21500 To improve this we track the last address in m_last_address,
21501 and whether we have seen an is-stmt at this address. Then
21502 when switching files, if we have seen a stmt at the current
21503 address, and we are switching to create a non-stmt line, then
21504 discard the new line. */
21505 bool file_changed
21506 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21507 bool ignore_this_line
21508 = ((file_changed && !end_sequence && m_last_address == m_address
21509 && !m_is_stmt && m_stmt_at_address)
21510 || (!end_sequence && m_line == 0));
21511
21512 if ((file_changed && !ignore_this_line) || end_sequence)
21513 {
21514 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21515 m_currently_recording_lines ? m_cu : nullptr);
21516 }
21517
21518 if (!end_sequence && !ignore_this_line)
21519 {
21520 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21521
21522 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21523 m_line_has_non_zero_discriminator,
21524 m_last_subfile))
21525 {
21526 buildsym_compunit *builder = m_cu->get_builder ();
21527 dwarf_record_line_1 (m_gdbarch,
21528 builder->get_current_subfile (),
21529 m_line, m_address, is_stmt,
21530 m_currently_recording_lines ? m_cu : nullptr);
21531 }
21532 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21533 m_last_line = m_line;
21534 }
21535 }
21536 }
21537
21538 /* Track whether we have seen any m_is_stmt true at m_address in case we
21539 have multiple line table entries all at m_address. */
21540 if (m_last_address != m_address)
21541 {
21542 m_stmt_at_address = false;
21543 m_last_address = m_address;
21544 }
21545 m_stmt_at_address |= m_is_stmt;
21546 }
21547
21548 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21549 line_header *lh, bool record_lines_p)
21550 {
21551 m_cu = cu;
21552 m_gdbarch = arch;
21553 m_record_lines_p = record_lines_p;
21554 m_line_header = lh;
21555
21556 m_currently_recording_lines = true;
21557
21558 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21559 was a line entry for it so that the backend has a chance to adjust it
21560 and also record it in case it needs it. This is currently used by MIPS
21561 code, cf. `mips_adjust_dwarf2_line'. */
21562 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21563 m_is_stmt = lh->default_is_stmt;
21564 m_discriminator = 0;
21565
21566 m_last_address = m_address;
21567 m_stmt_at_address = false;
21568 }
21569
21570 void
21571 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21572 const gdb_byte *line_ptr,
21573 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21574 {
21575 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21576 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21577 located at 0x0. In this case, additionally check that if
21578 ADDRESS < UNRELOCATED_LOWPC. */
21579
21580 if ((address == 0 && address < unrelocated_lowpc)
21581 || address == (CORE_ADDR) -1)
21582 {
21583 /* This line table is for a function which has been
21584 GCd by the linker. Ignore it. PR gdb/12528 */
21585
21586 struct objfile *objfile = cu->per_objfile->objfile;
21587 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21588
21589 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21590 line_offset, objfile_name (objfile));
21591 m_currently_recording_lines = false;
21592 /* Note: m_currently_recording_lines is left as false until we see
21593 DW_LNE_end_sequence. */
21594 }
21595 }
21596
21597 /* Subroutine of dwarf_decode_lines to simplify it.
21598 Process the line number information in LH.
21599 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21600 program in order to set included_p for every referenced header. */
21601
21602 static void
21603 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21604 const int decode_for_pst_p, CORE_ADDR lowpc)
21605 {
21606 const gdb_byte *line_ptr, *extended_end;
21607 const gdb_byte *line_end;
21608 unsigned int bytes_read, extended_len;
21609 unsigned char op_code, extended_op;
21610 CORE_ADDR baseaddr;
21611 struct objfile *objfile = cu->per_objfile->objfile;
21612 bfd *abfd = objfile->obfd;
21613 struct gdbarch *gdbarch = objfile->arch ();
21614 /* True if we're recording line info (as opposed to building partial
21615 symtabs and just interested in finding include files mentioned by
21616 the line number program). */
21617 bool record_lines_p = !decode_for_pst_p;
21618
21619 baseaddr = objfile->text_section_offset ();
21620
21621 line_ptr = lh->statement_program_start;
21622 line_end = lh->statement_program_end;
21623
21624 /* Read the statement sequences until there's nothing left. */
21625 while (line_ptr < line_end)
21626 {
21627 /* The DWARF line number program state machine. Reset the state
21628 machine at the start of each sequence. */
21629 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21630 bool end_sequence = false;
21631
21632 if (record_lines_p)
21633 {
21634 /* Start a subfile for the current file of the state
21635 machine. */
21636 const file_entry *fe = state_machine.current_file ();
21637
21638 if (fe != NULL)
21639 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21640 }
21641
21642 /* Decode the table. */
21643 while (line_ptr < line_end && !end_sequence)
21644 {
21645 op_code = read_1_byte (abfd, line_ptr);
21646 line_ptr += 1;
21647
21648 if (op_code >= lh->opcode_base)
21649 {
21650 /* Special opcode. */
21651 state_machine.handle_special_opcode (op_code);
21652 }
21653 else switch (op_code)
21654 {
21655 case DW_LNS_extended_op:
21656 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21657 &bytes_read);
21658 line_ptr += bytes_read;
21659 extended_end = line_ptr + extended_len;
21660 extended_op = read_1_byte (abfd, line_ptr);
21661 line_ptr += 1;
21662 if (DW_LNE_lo_user <= extended_op
21663 && extended_op <= DW_LNE_hi_user)
21664 {
21665 /* Vendor extension, ignore. */
21666 line_ptr = extended_end;
21667 break;
21668 }
21669 switch (extended_op)
21670 {
21671 case DW_LNE_end_sequence:
21672 state_machine.handle_end_sequence ();
21673 end_sequence = true;
21674 break;
21675 case DW_LNE_set_address:
21676 {
21677 CORE_ADDR address
21678 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21679 line_ptr += bytes_read;
21680
21681 state_machine.check_line_address (cu, line_ptr,
21682 lowpc - baseaddr, address);
21683 state_machine.handle_set_address (baseaddr, address);
21684 }
21685 break;
21686 case DW_LNE_define_file:
21687 {
21688 const char *cur_file;
21689 unsigned int mod_time, length;
21690 dir_index dindex;
21691
21692 cur_file = read_direct_string (abfd, line_ptr,
21693 &bytes_read);
21694 line_ptr += bytes_read;
21695 dindex = (dir_index)
21696 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21697 line_ptr += bytes_read;
21698 mod_time =
21699 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21700 line_ptr += bytes_read;
21701 length =
21702 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21703 line_ptr += bytes_read;
21704 lh->add_file_name (cur_file, dindex, mod_time, length);
21705 }
21706 break;
21707 case DW_LNE_set_discriminator:
21708 {
21709 /* The discriminator is not interesting to the
21710 debugger; just ignore it. We still need to
21711 check its value though:
21712 if there are consecutive entries for the same
21713 (non-prologue) line we want to coalesce them.
21714 PR 17276. */
21715 unsigned int discr
21716 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21717 line_ptr += bytes_read;
21718
21719 state_machine.handle_set_discriminator (discr);
21720 }
21721 break;
21722 default:
21723 complaint (_("mangled .debug_line section"));
21724 return;
21725 }
21726 /* Make sure that we parsed the extended op correctly. If e.g.
21727 we expected a different address size than the producer used,
21728 we may have read the wrong number of bytes. */
21729 if (line_ptr != extended_end)
21730 {
21731 complaint (_("mangled .debug_line section"));
21732 return;
21733 }
21734 break;
21735 case DW_LNS_copy:
21736 state_machine.handle_copy ();
21737 break;
21738 case DW_LNS_advance_pc:
21739 {
21740 CORE_ADDR adjust
21741 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21742 line_ptr += bytes_read;
21743
21744 state_machine.handle_advance_pc (adjust);
21745 }
21746 break;
21747 case DW_LNS_advance_line:
21748 {
21749 int line_delta
21750 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21751 line_ptr += bytes_read;
21752
21753 state_machine.handle_advance_line (line_delta);
21754 }
21755 break;
21756 case DW_LNS_set_file:
21757 {
21758 file_name_index file
21759 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21760 &bytes_read);
21761 line_ptr += bytes_read;
21762
21763 state_machine.handle_set_file (file);
21764 }
21765 break;
21766 case DW_LNS_set_column:
21767 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21768 line_ptr += bytes_read;
21769 break;
21770 case DW_LNS_negate_stmt:
21771 state_machine.handle_negate_stmt ();
21772 break;
21773 case DW_LNS_set_basic_block:
21774 break;
21775 /* Add to the address register of the state machine the
21776 address increment value corresponding to special opcode
21777 255. I.e., this value is scaled by the minimum
21778 instruction length since special opcode 255 would have
21779 scaled the increment. */
21780 case DW_LNS_const_add_pc:
21781 state_machine.handle_const_add_pc ();
21782 break;
21783 case DW_LNS_fixed_advance_pc:
21784 {
21785 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21786 line_ptr += 2;
21787
21788 state_machine.handle_fixed_advance_pc (addr_adj);
21789 }
21790 break;
21791 default:
21792 {
21793 /* Unknown standard opcode, ignore it. */
21794 int i;
21795
21796 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21797 {
21798 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21799 line_ptr += bytes_read;
21800 }
21801 }
21802 }
21803 }
21804
21805 if (!end_sequence)
21806 dwarf2_debug_line_missing_end_sequence_complaint ();
21807
21808 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21809 in which case we still finish recording the last line). */
21810 state_machine.record_line (true);
21811 }
21812 }
21813
21814 /* Decode the Line Number Program (LNP) for the given line_header
21815 structure and CU. The actual information extracted and the type
21816 of structures created from the LNP depends on the value of PST.
21817
21818 1. If PST is NULL, then this procedure uses the data from the program
21819 to create all necessary symbol tables, and their linetables.
21820
21821 2. If PST is not NULL, this procedure reads the program to determine
21822 the list of files included by the unit represented by PST, and
21823 builds all the associated partial symbol tables.
21824
21825 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21826 It is used for relative paths in the line table.
21827 NOTE: When processing partial symtabs (pst != NULL),
21828 comp_dir == pst->dirname.
21829
21830 NOTE: It is important that psymtabs have the same file name (via strcmp)
21831 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21832 symtab we don't use it in the name of the psymtabs we create.
21833 E.g. expand_line_sal requires this when finding psymtabs to expand.
21834 A good testcase for this is mb-inline.exp.
21835
21836 LOWPC is the lowest address in CU (or 0 if not known).
21837
21838 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21839 for its PC<->lines mapping information. Otherwise only the filename
21840 table is read in. */
21841
21842 static void
21843 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21844 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21845 CORE_ADDR lowpc, int decode_mapping)
21846 {
21847 struct objfile *objfile = cu->per_objfile->objfile;
21848 const int decode_for_pst_p = (pst != NULL);
21849
21850 if (decode_mapping)
21851 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21852
21853 if (decode_for_pst_p)
21854 {
21855 /* Now that we're done scanning the Line Header Program, we can
21856 create the psymtab of each included file. */
21857 for (auto &file_entry : lh->file_names ())
21858 if (file_entry.included_p == 1)
21859 {
21860 gdb::unique_xmalloc_ptr<char> name_holder;
21861 const char *include_name =
21862 psymtab_include_file_name (lh, file_entry, pst,
21863 comp_dir, &name_holder);
21864 if (include_name != NULL)
21865 dwarf2_create_include_psymtab (include_name, pst, objfile);
21866 }
21867 }
21868 else
21869 {
21870 /* Make sure a symtab is created for every file, even files
21871 which contain only variables (i.e. no code with associated
21872 line numbers). */
21873 buildsym_compunit *builder = cu->get_builder ();
21874 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21875
21876 for (auto &fe : lh->file_names ())
21877 {
21878 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21879 if (builder->get_current_subfile ()->symtab == NULL)
21880 {
21881 builder->get_current_subfile ()->symtab
21882 = allocate_symtab (cust,
21883 builder->get_current_subfile ()->name);
21884 }
21885 fe.symtab = builder->get_current_subfile ()->symtab;
21886 }
21887 }
21888 }
21889
21890 /* Start a subfile for DWARF. FILENAME is the name of the file and
21891 DIRNAME the name of the source directory which contains FILENAME
21892 or NULL if not known.
21893 This routine tries to keep line numbers from identical absolute and
21894 relative file names in a common subfile.
21895
21896 Using the `list' example from the GDB testsuite, which resides in
21897 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21898 of /srcdir/list0.c yields the following debugging information for list0.c:
21899
21900 DW_AT_name: /srcdir/list0.c
21901 DW_AT_comp_dir: /compdir
21902 files.files[0].name: list0.h
21903 files.files[0].dir: /srcdir
21904 files.files[1].name: list0.c
21905 files.files[1].dir: /srcdir
21906
21907 The line number information for list0.c has to end up in a single
21908 subfile, so that `break /srcdir/list0.c:1' works as expected.
21909 start_subfile will ensure that this happens provided that we pass the
21910 concatenation of files.files[1].dir and files.files[1].name as the
21911 subfile's name. */
21912
21913 static void
21914 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21915 const char *dirname)
21916 {
21917 gdb::unique_xmalloc_ptr<char> copy;
21918
21919 /* In order not to lose the line information directory,
21920 we concatenate it to the filename when it makes sense.
21921 Note that the Dwarf3 standard says (speaking of filenames in line
21922 information): ``The directory index is ignored for file names
21923 that represent full path names''. Thus ignoring dirname in the
21924 `else' branch below isn't an issue. */
21925
21926 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21927 {
21928 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21929 filename = copy.get ();
21930 }
21931
21932 cu->get_builder ()->start_subfile (filename);
21933 }
21934
21935 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21936 buildsym_compunit constructor. */
21937
21938 struct compunit_symtab *
21939 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21940 CORE_ADDR low_pc)
21941 {
21942 gdb_assert (m_builder == nullptr);
21943
21944 m_builder.reset (new struct buildsym_compunit
21945 (this->per_objfile->objfile,
21946 name, comp_dir, language, low_pc));
21947
21948 list_in_scope = get_builder ()->get_file_symbols ();
21949
21950 get_builder ()->record_debugformat ("DWARF 2");
21951 get_builder ()->record_producer (producer);
21952
21953 processing_has_namespace_info = false;
21954
21955 return get_builder ()->get_compunit_symtab ();
21956 }
21957
21958 static void
21959 var_decode_location (struct attribute *attr, struct symbol *sym,
21960 struct dwarf2_cu *cu)
21961 {
21962 struct objfile *objfile = cu->per_objfile->objfile;
21963 struct comp_unit_head *cu_header = &cu->header;
21964
21965 /* NOTE drow/2003-01-30: There used to be a comment and some special
21966 code here to turn a symbol with DW_AT_external and a
21967 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21968 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21969 with some versions of binutils) where shared libraries could have
21970 relocations against symbols in their debug information - the
21971 minimal symbol would have the right address, but the debug info
21972 would not. It's no longer necessary, because we will explicitly
21973 apply relocations when we read in the debug information now. */
21974
21975 /* A DW_AT_location attribute with no contents indicates that a
21976 variable has been optimized away. */
21977 if (attr->form_is_block () && attr->as_block ()->size == 0)
21978 {
21979 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21980 return;
21981 }
21982
21983 /* Handle one degenerate form of location expression specially, to
21984 preserve GDB's previous behavior when section offsets are
21985 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21986 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21987
21988 if (attr->form_is_block ())
21989 {
21990 struct dwarf_block *block = attr->as_block ();
21991
21992 if ((block->data[0] == DW_OP_addr
21993 && block->size == 1 + cu_header->addr_size)
21994 || ((block->data[0] == DW_OP_GNU_addr_index
21995 || block->data[0] == DW_OP_addrx)
21996 && (block->size
21997 == 1 + leb128_size (&block->data[1]))))
21998 {
21999 unsigned int dummy;
22000
22001 if (block->data[0] == DW_OP_addr)
22002 SET_SYMBOL_VALUE_ADDRESS
22003 (sym, cu->header.read_address (objfile->obfd,
22004 block->data + 1,
22005 &dummy));
22006 else
22007 SET_SYMBOL_VALUE_ADDRESS
22008 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
22009 &dummy));
22010 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
22011 fixup_symbol_section (sym, objfile);
22012 SET_SYMBOL_VALUE_ADDRESS
22013 (sym,
22014 SYMBOL_VALUE_ADDRESS (sym)
22015 + objfile->section_offsets[sym->section_index ()]);
22016 return;
22017 }
22018 }
22019
22020 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
22021 expression evaluator, and use LOC_COMPUTED only when necessary
22022 (i.e. when the value of a register or memory location is
22023 referenced, or a thread-local block, etc.). Then again, it might
22024 not be worthwhile. I'm assuming that it isn't unless performance
22025 or memory numbers show me otherwise. */
22026
22027 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
22028
22029 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
22030 cu->has_loclist = true;
22031 }
22032
22033 /* Given a pointer to a DWARF information entry, figure out if we need
22034 to make a symbol table entry for it, and if so, create a new entry
22035 and return a pointer to it.
22036 If TYPE is NULL, determine symbol type from the die, otherwise
22037 used the passed type.
22038 If SPACE is not NULL, use it to hold the new symbol. If it is
22039 NULL, allocate a new symbol on the objfile's obstack. */
22040
22041 static struct symbol *
22042 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
22043 struct symbol *space)
22044 {
22045 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22046 struct objfile *objfile = per_objfile->objfile;
22047 struct gdbarch *gdbarch = objfile->arch ();
22048 struct symbol *sym = NULL;
22049 const char *name;
22050 struct attribute *attr = NULL;
22051 struct attribute *attr2 = NULL;
22052 CORE_ADDR baseaddr;
22053 struct pending **list_to_add = NULL;
22054
22055 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
22056
22057 baseaddr = objfile->text_section_offset ();
22058
22059 name = dwarf2_name (die, cu);
22060 if (name)
22061 {
22062 int suppress_add = 0;
22063
22064 if (space)
22065 sym = space;
22066 else
22067 sym = new (&objfile->objfile_obstack) symbol;
22068 OBJSTAT (objfile, n_syms++);
22069
22070 /* Cache this symbol's name and the name's demangled form (if any). */
22071 sym->set_language (cu->language, &objfile->objfile_obstack);
22072 /* Fortran does not have mangling standard and the mangling does differ
22073 between gfortran, iFort etc. */
22074 const char *physname
22075 = (cu->language == language_fortran
22076 ? dwarf2_full_name (name, die, cu)
22077 : dwarf2_physname (name, die, cu));
22078 const char *linkagename = dw2_linkage_name (die, cu);
22079
22080 if (linkagename == nullptr || cu->language == language_ada)
22081 sym->set_linkage_name (physname);
22082 else
22083 {
22084 sym->set_demangled_name (physname, &objfile->objfile_obstack);
22085 sym->set_linkage_name (linkagename);
22086 }
22087
22088 /* Default assumptions.
22089 Use the passed type or decode it from the die. */
22090 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22091 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22092 if (type != NULL)
22093 SYMBOL_TYPE (sym) = type;
22094 else
22095 SYMBOL_TYPE (sym) = die_type (die, cu);
22096 attr = dwarf2_attr (die,
22097 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
22098 cu);
22099 if (attr != nullptr)
22100 SYMBOL_LINE (sym) = attr->constant_value (0);
22101
22102 attr = dwarf2_attr (die,
22103 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
22104 cu);
22105 if (attr != nullptr && attr->is_nonnegative ())
22106 {
22107 file_name_index file_index
22108 = (file_name_index) attr->as_nonnegative ();
22109 struct file_entry *fe;
22110
22111 if (cu->line_header != NULL)
22112 fe = cu->line_header->file_name_at (file_index);
22113 else
22114 fe = NULL;
22115
22116 if (fe == NULL)
22117 complaint (_("file index out of range"));
22118 else
22119 symbol_set_symtab (sym, fe->symtab);
22120 }
22121
22122 switch (die->tag)
22123 {
22124 case DW_TAG_label:
22125 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
22126 if (attr != nullptr)
22127 {
22128 CORE_ADDR addr;
22129
22130 addr = attr->as_address ();
22131 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
22132 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
22133 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
22134 }
22135 else
22136 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22137 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
22138 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
22139 add_symbol_to_list (sym, cu->list_in_scope);
22140 break;
22141 case DW_TAG_subprogram:
22142 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22143 finish_block. */
22144 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
22145 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22146 if ((attr2 != nullptr && attr2->as_boolean ())
22147 || cu->language == language_ada
22148 || cu->language == language_fortran)
22149 {
22150 /* Subprograms marked external are stored as a global symbol.
22151 Ada and Fortran subprograms, whether marked external or
22152 not, are always stored as a global symbol, because we want
22153 to be able to access them globally. For instance, we want
22154 to be able to break on a nested subprogram without having
22155 to specify the context. */
22156 list_to_add = cu->get_builder ()->get_global_symbols ();
22157 }
22158 else
22159 {
22160 list_to_add = cu->list_in_scope;
22161 }
22162 break;
22163 case DW_TAG_inlined_subroutine:
22164 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22165 finish_block. */
22166 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
22167 SYMBOL_INLINED (sym) = 1;
22168 list_to_add = cu->list_in_scope;
22169 break;
22170 case DW_TAG_template_value_param:
22171 suppress_add = 1;
22172 /* Fall through. */
22173 case DW_TAG_constant:
22174 case DW_TAG_variable:
22175 case DW_TAG_member:
22176 /* Compilation with minimal debug info may result in
22177 variables with missing type entries. Change the
22178 misleading `void' type to something sensible. */
22179 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
22180 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
22181
22182 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22183 /* In the case of DW_TAG_member, we should only be called for
22184 static const members. */
22185 if (die->tag == DW_TAG_member)
22186 {
22187 /* dwarf2_add_field uses die_is_declaration,
22188 so we do the same. */
22189 gdb_assert (die_is_declaration (die, cu));
22190 gdb_assert (attr);
22191 }
22192 if (attr != nullptr)
22193 {
22194 dwarf2_const_value (attr, sym, cu);
22195 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22196 if (!suppress_add)
22197 {
22198 if (attr2 != nullptr && attr2->as_boolean ())
22199 list_to_add = cu->get_builder ()->get_global_symbols ();
22200 else
22201 list_to_add = cu->list_in_scope;
22202 }
22203 break;
22204 }
22205 attr = dwarf2_attr (die, DW_AT_location, cu);
22206 if (attr != nullptr)
22207 {
22208 var_decode_location (attr, sym, cu);
22209 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22210
22211 /* Fortran explicitly imports any global symbols to the local
22212 scope by DW_TAG_common_block. */
22213 if (cu->language == language_fortran && die->parent
22214 && die->parent->tag == DW_TAG_common_block)
22215 attr2 = NULL;
22216
22217 if (SYMBOL_CLASS (sym) == LOC_STATIC
22218 && SYMBOL_VALUE_ADDRESS (sym) == 0
22219 && !per_objfile->per_bfd->has_section_at_zero)
22220 {
22221 /* When a static variable is eliminated by the linker,
22222 the corresponding debug information is not stripped
22223 out, but the variable address is set to null;
22224 do not add such variables into symbol table. */
22225 }
22226 else if (attr2 != nullptr && attr2->as_boolean ())
22227 {
22228 if (SYMBOL_CLASS (sym) == LOC_STATIC
22229 && (objfile->flags & OBJF_MAINLINE) == 0
22230 && per_objfile->per_bfd->can_copy)
22231 {
22232 /* A global static variable might be subject to
22233 copy relocation. We first check for a local
22234 minsym, though, because maybe the symbol was
22235 marked hidden, in which case this would not
22236 apply. */
22237 bound_minimal_symbol found
22238 = (lookup_minimal_symbol_linkage
22239 (sym->linkage_name (), objfile));
22240 if (found.minsym != nullptr)
22241 sym->maybe_copied = 1;
22242 }
22243
22244 /* A variable with DW_AT_external is never static,
22245 but it may be block-scoped. */
22246 list_to_add
22247 = ((cu->list_in_scope
22248 == cu->get_builder ()->get_file_symbols ())
22249 ? cu->get_builder ()->get_global_symbols ()
22250 : cu->list_in_scope);
22251 }
22252 else
22253 list_to_add = cu->list_in_scope;
22254 }
22255 else
22256 {
22257 /* We do not know the address of this symbol.
22258 If it is an external symbol and we have type information
22259 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22260 The address of the variable will then be determined from
22261 the minimal symbol table whenever the variable is
22262 referenced. */
22263 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22264
22265 /* Fortran explicitly imports any global symbols to the local
22266 scope by DW_TAG_common_block. */
22267 if (cu->language == language_fortran && die->parent
22268 && die->parent->tag == DW_TAG_common_block)
22269 {
22270 /* SYMBOL_CLASS doesn't matter here because
22271 read_common_block is going to reset it. */
22272 if (!suppress_add)
22273 list_to_add = cu->list_in_scope;
22274 }
22275 else if (attr2 != nullptr && attr2->as_boolean ()
22276 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
22277 {
22278 /* A variable with DW_AT_external is never static, but it
22279 may be block-scoped. */
22280 list_to_add
22281 = ((cu->list_in_scope
22282 == cu->get_builder ()->get_file_symbols ())
22283 ? cu->get_builder ()->get_global_symbols ()
22284 : cu->list_in_scope);
22285
22286 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
22287 }
22288 else if (!die_is_declaration (die, cu))
22289 {
22290 /* Use the default LOC_OPTIMIZED_OUT class. */
22291 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
22292 if (!suppress_add)
22293 list_to_add = cu->list_in_scope;
22294 }
22295 }
22296 break;
22297 case DW_TAG_formal_parameter:
22298 {
22299 /* If we are inside a function, mark this as an argument. If
22300 not, we might be looking at an argument to an inlined function
22301 when we do not have enough information to show inlined frames;
22302 pretend it's a local variable in that case so that the user can
22303 still see it. */
22304 struct context_stack *curr
22305 = cu->get_builder ()->get_current_context_stack ();
22306 if (curr != nullptr && curr->name != nullptr)
22307 SYMBOL_IS_ARGUMENT (sym) = 1;
22308 attr = dwarf2_attr (die, DW_AT_location, cu);
22309 if (attr != nullptr)
22310 {
22311 var_decode_location (attr, sym, cu);
22312 }
22313 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22314 if (attr != nullptr)
22315 {
22316 dwarf2_const_value (attr, sym, cu);
22317 }
22318
22319 list_to_add = cu->list_in_scope;
22320 }
22321 break;
22322 case DW_TAG_unspecified_parameters:
22323 /* From varargs functions; gdb doesn't seem to have any
22324 interest in this information, so just ignore it for now.
22325 (FIXME?) */
22326 break;
22327 case DW_TAG_template_type_param:
22328 suppress_add = 1;
22329 /* Fall through. */
22330 case DW_TAG_class_type:
22331 case DW_TAG_interface_type:
22332 case DW_TAG_structure_type:
22333 case DW_TAG_union_type:
22334 case DW_TAG_set_type:
22335 case DW_TAG_enumeration_type:
22336 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22337 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
22338
22339 {
22340 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22341 really ever be static objects: otherwise, if you try
22342 to, say, break of a class's method and you're in a file
22343 which doesn't mention that class, it won't work unless
22344 the check for all static symbols in lookup_symbol_aux
22345 saves you. See the OtherFileClass tests in
22346 gdb.c++/namespace.exp. */
22347
22348 if (!suppress_add)
22349 {
22350 buildsym_compunit *builder = cu->get_builder ();
22351 list_to_add
22352 = (cu->list_in_scope == builder->get_file_symbols ()
22353 && cu->language == language_cplus
22354 ? builder->get_global_symbols ()
22355 : cu->list_in_scope);
22356
22357 /* The semantics of C++ state that "struct foo {
22358 ... }" also defines a typedef for "foo". */
22359 if (cu->language == language_cplus
22360 || cu->language == language_ada
22361 || cu->language == language_d
22362 || cu->language == language_rust)
22363 {
22364 /* The symbol's name is already allocated along
22365 with this objfile, so we don't need to
22366 duplicate it for the type. */
22367 if (SYMBOL_TYPE (sym)->name () == 0)
22368 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
22369 }
22370 }
22371 }
22372 break;
22373 case DW_TAG_typedef:
22374 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22375 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22376 list_to_add = cu->list_in_scope;
22377 break;
22378 case DW_TAG_array_type:
22379 case DW_TAG_base_type:
22380 case DW_TAG_subrange_type:
22381 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22382 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22383 list_to_add = cu->list_in_scope;
22384 break;
22385 case DW_TAG_enumerator:
22386 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22387 if (attr != nullptr)
22388 {
22389 dwarf2_const_value (attr, sym, cu);
22390 }
22391 {
22392 /* NOTE: carlton/2003-11-10: See comment above in the
22393 DW_TAG_class_type, etc. block. */
22394
22395 list_to_add
22396 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22397 && cu->language == language_cplus
22398 ? cu->get_builder ()->get_global_symbols ()
22399 : cu->list_in_scope);
22400 }
22401 break;
22402 case DW_TAG_imported_declaration:
22403 case DW_TAG_namespace:
22404 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22405 list_to_add = cu->get_builder ()->get_global_symbols ();
22406 break;
22407 case DW_TAG_module:
22408 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22409 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22410 list_to_add = cu->get_builder ()->get_global_symbols ();
22411 break;
22412 case DW_TAG_common_block:
22413 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22414 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22415 add_symbol_to_list (sym, cu->list_in_scope);
22416 break;
22417 default:
22418 /* Not a tag we recognize. Hopefully we aren't processing
22419 trash data, but since we must specifically ignore things
22420 we don't recognize, there is nothing else we should do at
22421 this point. */
22422 complaint (_("unsupported tag: '%s'"),
22423 dwarf_tag_name (die->tag));
22424 break;
22425 }
22426
22427 if (suppress_add)
22428 {
22429 sym->hash_next = objfile->template_symbols;
22430 objfile->template_symbols = sym;
22431 list_to_add = NULL;
22432 }
22433
22434 if (list_to_add != NULL)
22435 add_symbol_to_list (sym, list_to_add);
22436
22437 /* For the benefit of old versions of GCC, check for anonymous
22438 namespaces based on the demangled name. */
22439 if (!cu->processing_has_namespace_info
22440 && cu->language == language_cplus)
22441 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22442 }
22443 return (sym);
22444 }
22445
22446 /* Given an attr with a DW_FORM_dataN value in host byte order,
22447 zero-extend it as appropriate for the symbol's type. The DWARF
22448 standard (v4) is not entirely clear about the meaning of using
22449 DW_FORM_dataN for a constant with a signed type, where the type is
22450 wider than the data. The conclusion of a discussion on the DWARF
22451 list was that this is unspecified. We choose to always zero-extend
22452 because that is the interpretation long in use by GCC. */
22453
22454 static gdb_byte *
22455 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22456 struct dwarf2_cu *cu, LONGEST *value, int bits)
22457 {
22458 struct objfile *objfile = cu->per_objfile->objfile;
22459 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22460 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22461 LONGEST l = attr->constant_value (0);
22462
22463 if (bits < sizeof (*value) * 8)
22464 {
22465 l &= ((LONGEST) 1 << bits) - 1;
22466 *value = l;
22467 }
22468 else if (bits == sizeof (*value) * 8)
22469 *value = l;
22470 else
22471 {
22472 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22473 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22474 return bytes;
22475 }
22476
22477 return NULL;
22478 }
22479
22480 /* Read a constant value from an attribute. Either set *VALUE, or if
22481 the value does not fit in *VALUE, set *BYTES - either already
22482 allocated on the objfile obstack, or newly allocated on OBSTACK,
22483 or, set *BATON, if we translated the constant to a location
22484 expression. */
22485
22486 static void
22487 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22488 const char *name, struct obstack *obstack,
22489 struct dwarf2_cu *cu,
22490 LONGEST *value, const gdb_byte **bytes,
22491 struct dwarf2_locexpr_baton **baton)
22492 {
22493 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22494 struct objfile *objfile = per_objfile->objfile;
22495 struct comp_unit_head *cu_header = &cu->header;
22496 struct dwarf_block *blk;
22497 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22498 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22499
22500 *value = 0;
22501 *bytes = NULL;
22502 *baton = NULL;
22503
22504 switch (attr->form)
22505 {
22506 case DW_FORM_addr:
22507 case DW_FORM_addrx:
22508 case DW_FORM_GNU_addr_index:
22509 {
22510 gdb_byte *data;
22511
22512 if (TYPE_LENGTH (type) != cu_header->addr_size)
22513 dwarf2_const_value_length_mismatch_complaint (name,
22514 cu_header->addr_size,
22515 TYPE_LENGTH (type));
22516 /* Symbols of this form are reasonably rare, so we just
22517 piggyback on the existing location code rather than writing
22518 a new implementation of symbol_computed_ops. */
22519 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22520 (*baton)->per_objfile = per_objfile;
22521 (*baton)->per_cu = cu->per_cu;
22522 gdb_assert ((*baton)->per_cu);
22523
22524 (*baton)->size = 2 + cu_header->addr_size;
22525 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22526 (*baton)->data = data;
22527
22528 data[0] = DW_OP_addr;
22529 store_unsigned_integer (&data[1], cu_header->addr_size,
22530 byte_order, attr->as_address ());
22531 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22532 }
22533 break;
22534 case DW_FORM_string:
22535 case DW_FORM_strp:
22536 case DW_FORM_strx:
22537 case DW_FORM_GNU_str_index:
22538 case DW_FORM_GNU_strp_alt:
22539 /* The string is already allocated on the objfile obstack, point
22540 directly to it. */
22541 *bytes = (const gdb_byte *) attr->as_string ();
22542 break;
22543 case DW_FORM_block1:
22544 case DW_FORM_block2:
22545 case DW_FORM_block4:
22546 case DW_FORM_block:
22547 case DW_FORM_exprloc:
22548 case DW_FORM_data16:
22549 blk = attr->as_block ();
22550 if (TYPE_LENGTH (type) != blk->size)
22551 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22552 TYPE_LENGTH (type));
22553 *bytes = blk->data;
22554 break;
22555
22556 /* The DW_AT_const_value attributes are supposed to carry the
22557 symbol's value "represented as it would be on the target
22558 architecture." By the time we get here, it's already been
22559 converted to host endianness, so we just need to sign- or
22560 zero-extend it as appropriate. */
22561 case DW_FORM_data1:
22562 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22563 break;
22564 case DW_FORM_data2:
22565 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22566 break;
22567 case DW_FORM_data4:
22568 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22569 break;
22570 case DW_FORM_data8:
22571 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22572 break;
22573
22574 case DW_FORM_sdata:
22575 case DW_FORM_implicit_const:
22576 *value = attr->as_signed ();
22577 break;
22578
22579 case DW_FORM_udata:
22580 *value = attr->as_unsigned ();
22581 break;
22582
22583 default:
22584 complaint (_("unsupported const value attribute form: '%s'"),
22585 dwarf_form_name (attr->form));
22586 *value = 0;
22587 break;
22588 }
22589 }
22590
22591
22592 /* Copy constant value from an attribute to a symbol. */
22593
22594 static void
22595 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22596 struct dwarf2_cu *cu)
22597 {
22598 struct objfile *objfile = cu->per_objfile->objfile;
22599 LONGEST value;
22600 const gdb_byte *bytes;
22601 struct dwarf2_locexpr_baton *baton;
22602
22603 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22604 sym->print_name (),
22605 &objfile->objfile_obstack, cu,
22606 &value, &bytes, &baton);
22607
22608 if (baton != NULL)
22609 {
22610 SYMBOL_LOCATION_BATON (sym) = baton;
22611 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22612 }
22613 else if (bytes != NULL)
22614 {
22615 SYMBOL_VALUE_BYTES (sym) = bytes;
22616 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22617 }
22618 else
22619 {
22620 SYMBOL_VALUE (sym) = value;
22621 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22622 }
22623 }
22624
22625 /* Return the type of the die in question using its DW_AT_type attribute. */
22626
22627 static struct type *
22628 die_type (struct die_info *die, struct dwarf2_cu *cu)
22629 {
22630 struct attribute *type_attr;
22631
22632 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22633 if (!type_attr)
22634 {
22635 struct objfile *objfile = cu->per_objfile->objfile;
22636 /* A missing DW_AT_type represents a void type. */
22637 return objfile_type (objfile)->builtin_void;
22638 }
22639
22640 return lookup_die_type (die, type_attr, cu);
22641 }
22642
22643 /* True iff CU's producer generates GNAT Ada auxiliary information
22644 that allows to find parallel types through that information instead
22645 of having to do expensive parallel lookups by type name. */
22646
22647 static int
22648 need_gnat_info (struct dwarf2_cu *cu)
22649 {
22650 /* Assume that the Ada compiler was GNAT, which always produces
22651 the auxiliary information. */
22652 return (cu->language == language_ada);
22653 }
22654
22655 /* Return the auxiliary type of the die in question using its
22656 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22657 attribute is not present. */
22658
22659 static struct type *
22660 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22661 {
22662 struct attribute *type_attr;
22663
22664 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22665 if (!type_attr)
22666 return NULL;
22667
22668 return lookup_die_type (die, type_attr, cu);
22669 }
22670
22671 /* If DIE has a descriptive_type attribute, then set the TYPE's
22672 descriptive type accordingly. */
22673
22674 static void
22675 set_descriptive_type (struct type *type, struct die_info *die,
22676 struct dwarf2_cu *cu)
22677 {
22678 struct type *descriptive_type = die_descriptive_type (die, cu);
22679
22680 if (descriptive_type)
22681 {
22682 ALLOCATE_GNAT_AUX_TYPE (type);
22683 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22684 }
22685 }
22686
22687 /* Return the containing type of the die in question using its
22688 DW_AT_containing_type attribute. */
22689
22690 static struct type *
22691 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22692 {
22693 struct attribute *type_attr;
22694 struct objfile *objfile = cu->per_objfile->objfile;
22695
22696 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22697 if (!type_attr)
22698 error (_("Dwarf Error: Problem turning containing type into gdb type "
22699 "[in module %s]"), objfile_name (objfile));
22700
22701 return lookup_die_type (die, type_attr, cu);
22702 }
22703
22704 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22705
22706 static struct type *
22707 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22708 {
22709 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22710 struct objfile *objfile = per_objfile->objfile;
22711 char *saved;
22712
22713 std::string message
22714 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22715 objfile_name (objfile),
22716 sect_offset_str (cu->header.sect_off),
22717 sect_offset_str (die->sect_off));
22718 saved = obstack_strdup (&objfile->objfile_obstack, message);
22719
22720 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22721 }
22722
22723 /* Look up the type of DIE in CU using its type attribute ATTR.
22724 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22725 DW_AT_containing_type.
22726 If there is no type substitute an error marker. */
22727
22728 static struct type *
22729 lookup_die_type (struct die_info *die, const struct attribute *attr,
22730 struct dwarf2_cu *cu)
22731 {
22732 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22733 struct objfile *objfile = per_objfile->objfile;
22734 struct type *this_type;
22735
22736 gdb_assert (attr->name == DW_AT_type
22737 || attr->name == DW_AT_GNAT_descriptive_type
22738 || attr->name == DW_AT_containing_type);
22739
22740 /* First see if we have it cached. */
22741
22742 if (attr->form == DW_FORM_GNU_ref_alt)
22743 {
22744 struct dwarf2_per_cu_data *per_cu;
22745 sect_offset sect_off = attr->get_ref_die_offset ();
22746
22747 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22748 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
22749 }
22750 else if (attr->form_is_ref ())
22751 {
22752 sect_offset sect_off = attr->get_ref_die_offset ();
22753
22754 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
22755 }
22756 else if (attr->form == DW_FORM_ref_sig8)
22757 {
22758 ULONGEST signature = attr->as_signature ();
22759
22760 return get_signatured_type (die, signature, cu);
22761 }
22762 else
22763 {
22764 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22765 " at %s [in module %s]"),
22766 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22767 objfile_name (objfile));
22768 return build_error_marker_type (cu, die);
22769 }
22770
22771 /* If not cached we need to read it in. */
22772
22773 if (this_type == NULL)
22774 {
22775 struct die_info *type_die = NULL;
22776 struct dwarf2_cu *type_cu = cu;
22777
22778 if (attr->form_is_ref ())
22779 type_die = follow_die_ref (die, attr, &type_cu);
22780 if (type_die == NULL)
22781 return build_error_marker_type (cu, die);
22782 /* If we find the type now, it's probably because the type came
22783 from an inter-CU reference and the type's CU got expanded before
22784 ours. */
22785 this_type = read_type_die (type_die, type_cu);
22786 }
22787
22788 /* If we still don't have a type use an error marker. */
22789
22790 if (this_type == NULL)
22791 return build_error_marker_type (cu, die);
22792
22793 return this_type;
22794 }
22795
22796 /* Return the type in DIE, CU.
22797 Returns NULL for invalid types.
22798
22799 This first does a lookup in die_type_hash,
22800 and only reads the die in if necessary.
22801
22802 NOTE: This can be called when reading in partial or full symbols. */
22803
22804 static struct type *
22805 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22806 {
22807 struct type *this_type;
22808
22809 this_type = get_die_type (die, cu);
22810 if (this_type)
22811 return this_type;
22812
22813 return read_type_die_1 (die, cu);
22814 }
22815
22816 /* Read the type in DIE, CU.
22817 Returns NULL for invalid types. */
22818
22819 static struct type *
22820 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22821 {
22822 struct type *this_type = NULL;
22823
22824 switch (die->tag)
22825 {
22826 case DW_TAG_class_type:
22827 case DW_TAG_interface_type:
22828 case DW_TAG_structure_type:
22829 case DW_TAG_union_type:
22830 this_type = read_structure_type (die, cu);
22831 break;
22832 case DW_TAG_enumeration_type:
22833 this_type = read_enumeration_type (die, cu);
22834 break;
22835 case DW_TAG_subprogram:
22836 case DW_TAG_subroutine_type:
22837 case DW_TAG_inlined_subroutine:
22838 this_type = read_subroutine_type (die, cu);
22839 break;
22840 case DW_TAG_array_type:
22841 this_type = read_array_type (die, cu);
22842 break;
22843 case DW_TAG_set_type:
22844 this_type = read_set_type (die, cu);
22845 break;
22846 case DW_TAG_pointer_type:
22847 this_type = read_tag_pointer_type (die, cu);
22848 break;
22849 case DW_TAG_ptr_to_member_type:
22850 this_type = read_tag_ptr_to_member_type (die, cu);
22851 break;
22852 case DW_TAG_reference_type:
22853 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22854 break;
22855 case DW_TAG_rvalue_reference_type:
22856 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22857 break;
22858 case DW_TAG_const_type:
22859 this_type = read_tag_const_type (die, cu);
22860 break;
22861 case DW_TAG_volatile_type:
22862 this_type = read_tag_volatile_type (die, cu);
22863 break;
22864 case DW_TAG_restrict_type:
22865 this_type = read_tag_restrict_type (die, cu);
22866 break;
22867 case DW_TAG_string_type:
22868 this_type = read_tag_string_type (die, cu);
22869 break;
22870 case DW_TAG_typedef:
22871 this_type = read_typedef (die, cu);
22872 break;
22873 case DW_TAG_subrange_type:
22874 this_type = read_subrange_type (die, cu);
22875 break;
22876 case DW_TAG_base_type:
22877 this_type = read_base_type (die, cu);
22878 break;
22879 case DW_TAG_unspecified_type:
22880 this_type = read_unspecified_type (die, cu);
22881 break;
22882 case DW_TAG_namespace:
22883 this_type = read_namespace_type (die, cu);
22884 break;
22885 case DW_TAG_module:
22886 this_type = read_module_type (die, cu);
22887 break;
22888 case DW_TAG_atomic_type:
22889 this_type = read_tag_atomic_type (die, cu);
22890 break;
22891 default:
22892 complaint (_("unexpected tag in read_type_die: '%s'"),
22893 dwarf_tag_name (die->tag));
22894 break;
22895 }
22896
22897 return this_type;
22898 }
22899
22900 /* See if we can figure out if the class lives in a namespace. We do
22901 this by looking for a member function; its demangled name will
22902 contain namespace info, if there is any.
22903 Return the computed name or NULL.
22904 Space for the result is allocated on the objfile's obstack.
22905 This is the full-die version of guess_partial_die_structure_name.
22906 In this case we know DIE has no useful parent. */
22907
22908 static const char *
22909 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22910 {
22911 struct die_info *spec_die;
22912 struct dwarf2_cu *spec_cu;
22913 struct die_info *child;
22914 struct objfile *objfile = cu->per_objfile->objfile;
22915
22916 spec_cu = cu;
22917 spec_die = die_specification (die, &spec_cu);
22918 if (spec_die != NULL)
22919 {
22920 die = spec_die;
22921 cu = spec_cu;
22922 }
22923
22924 for (child = die->child;
22925 child != NULL;
22926 child = child->sibling)
22927 {
22928 if (child->tag == DW_TAG_subprogram)
22929 {
22930 const char *linkage_name = dw2_linkage_name (child, cu);
22931
22932 if (linkage_name != NULL)
22933 {
22934 gdb::unique_xmalloc_ptr<char> actual_name
22935 (cu->language_defn->class_name_from_physname (linkage_name));
22936 const char *name = NULL;
22937
22938 if (actual_name != NULL)
22939 {
22940 const char *die_name = dwarf2_name (die, cu);
22941
22942 if (die_name != NULL
22943 && strcmp (die_name, actual_name.get ()) != 0)
22944 {
22945 /* Strip off the class name from the full name.
22946 We want the prefix. */
22947 int die_name_len = strlen (die_name);
22948 int actual_name_len = strlen (actual_name.get ());
22949 const char *ptr = actual_name.get ();
22950
22951 /* Test for '::' as a sanity check. */
22952 if (actual_name_len > die_name_len + 2
22953 && ptr[actual_name_len - die_name_len - 1] == ':')
22954 name = obstack_strndup (
22955 &objfile->per_bfd->storage_obstack,
22956 ptr, actual_name_len - die_name_len - 2);
22957 }
22958 }
22959 return name;
22960 }
22961 }
22962 }
22963
22964 return NULL;
22965 }
22966
22967 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22968 prefix part in such case. See
22969 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22970
22971 static const char *
22972 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22973 {
22974 struct attribute *attr;
22975 const char *base;
22976
22977 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22978 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22979 return NULL;
22980
22981 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22982 return NULL;
22983
22984 attr = dw2_linkage_name_attr (die, cu);
22985 const char *attr_name = attr->as_string ();
22986 if (attr == NULL || attr_name == NULL)
22987 return NULL;
22988
22989 /* dwarf2_name had to be already called. */
22990 gdb_assert (attr->canonical_string_p ());
22991
22992 /* Strip the base name, keep any leading namespaces/classes. */
22993 base = strrchr (attr_name, ':');
22994 if (base == NULL || base == attr_name || base[-1] != ':')
22995 return "";
22996
22997 struct objfile *objfile = cu->per_objfile->objfile;
22998 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22999 attr_name,
23000 &base[-1] - attr_name);
23001 }
23002
23003 /* Return the name of the namespace/class that DIE is defined within,
23004 or "" if we can't tell. The caller should not xfree the result.
23005
23006 For example, if we're within the method foo() in the following
23007 code:
23008
23009 namespace N {
23010 class C {
23011 void foo () {
23012 }
23013 };
23014 }
23015
23016 then determine_prefix on foo's die will return "N::C". */
23017
23018 static const char *
23019 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
23020 {
23021 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23022 struct die_info *parent, *spec_die;
23023 struct dwarf2_cu *spec_cu;
23024 struct type *parent_type;
23025 const char *retval;
23026
23027 if (cu->language != language_cplus
23028 && cu->language != language_fortran && cu->language != language_d
23029 && cu->language != language_rust)
23030 return "";
23031
23032 retval = anonymous_struct_prefix (die, cu);
23033 if (retval)
23034 return retval;
23035
23036 /* We have to be careful in the presence of DW_AT_specification.
23037 For example, with GCC 3.4, given the code
23038
23039 namespace N {
23040 void foo() {
23041 // Definition of N::foo.
23042 }
23043 }
23044
23045 then we'll have a tree of DIEs like this:
23046
23047 1: DW_TAG_compile_unit
23048 2: DW_TAG_namespace // N
23049 3: DW_TAG_subprogram // declaration of N::foo
23050 4: DW_TAG_subprogram // definition of N::foo
23051 DW_AT_specification // refers to die #3
23052
23053 Thus, when processing die #4, we have to pretend that we're in
23054 the context of its DW_AT_specification, namely the contex of die
23055 #3. */
23056 spec_cu = cu;
23057 spec_die = die_specification (die, &spec_cu);
23058 if (spec_die == NULL)
23059 parent = die->parent;
23060 else
23061 {
23062 parent = spec_die->parent;
23063 cu = spec_cu;
23064 }
23065
23066 if (parent == NULL)
23067 return "";
23068 else if (parent->building_fullname)
23069 {
23070 const char *name;
23071 const char *parent_name;
23072
23073 /* It has been seen on RealView 2.2 built binaries,
23074 DW_TAG_template_type_param types actually _defined_ as
23075 children of the parent class:
23076
23077 enum E {};
23078 template class <class Enum> Class{};
23079 Class<enum E> class_e;
23080
23081 1: DW_TAG_class_type (Class)
23082 2: DW_TAG_enumeration_type (E)
23083 3: DW_TAG_enumerator (enum1:0)
23084 3: DW_TAG_enumerator (enum2:1)
23085 ...
23086 2: DW_TAG_template_type_param
23087 DW_AT_type DW_FORM_ref_udata (E)
23088
23089 Besides being broken debug info, it can put GDB into an
23090 infinite loop. Consider:
23091
23092 When we're building the full name for Class<E>, we'll start
23093 at Class, and go look over its template type parameters,
23094 finding E. We'll then try to build the full name of E, and
23095 reach here. We're now trying to build the full name of E,
23096 and look over the parent DIE for containing scope. In the
23097 broken case, if we followed the parent DIE of E, we'd again
23098 find Class, and once again go look at its template type
23099 arguments, etc., etc. Simply don't consider such parent die
23100 as source-level parent of this die (it can't be, the language
23101 doesn't allow it), and break the loop here. */
23102 name = dwarf2_name (die, cu);
23103 parent_name = dwarf2_name (parent, cu);
23104 complaint (_("template param type '%s' defined within parent '%s'"),
23105 name ? name : "<unknown>",
23106 parent_name ? parent_name : "<unknown>");
23107 return "";
23108 }
23109 else
23110 switch (parent->tag)
23111 {
23112 case DW_TAG_namespace:
23113 parent_type = read_type_die (parent, cu);
23114 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
23115 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
23116 Work around this problem here. */
23117 if (cu->language == language_cplus
23118 && strcmp (parent_type->name (), "::") == 0)
23119 return "";
23120 /* We give a name to even anonymous namespaces. */
23121 return parent_type->name ();
23122 case DW_TAG_class_type:
23123 case DW_TAG_interface_type:
23124 case DW_TAG_structure_type:
23125 case DW_TAG_union_type:
23126 case DW_TAG_module:
23127 parent_type = read_type_die (parent, cu);
23128 if (parent_type->name () != NULL)
23129 return parent_type->name ();
23130 else
23131 /* An anonymous structure is only allowed non-static data
23132 members; no typedefs, no member functions, et cetera.
23133 So it does not need a prefix. */
23134 return "";
23135 case DW_TAG_compile_unit:
23136 case DW_TAG_partial_unit:
23137 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
23138 if (cu->language == language_cplus
23139 && !per_objfile->per_bfd->types.empty ()
23140 && die->child != NULL
23141 && (die->tag == DW_TAG_class_type
23142 || die->tag == DW_TAG_structure_type
23143 || die->tag == DW_TAG_union_type))
23144 {
23145 const char *name = guess_full_die_structure_name (die, cu);
23146 if (name != NULL)
23147 return name;
23148 }
23149 return "";
23150 case DW_TAG_subprogram:
23151 /* Nested subroutines in Fortran get a prefix with the name
23152 of the parent's subroutine. */
23153 if (cu->language == language_fortran)
23154 {
23155 if ((die->tag == DW_TAG_subprogram)
23156 && (dwarf2_name (parent, cu) != NULL))
23157 return dwarf2_name (parent, cu);
23158 }
23159 return determine_prefix (parent, cu);
23160 case DW_TAG_enumeration_type:
23161 parent_type = read_type_die (parent, cu);
23162 if (TYPE_DECLARED_CLASS (parent_type))
23163 {
23164 if (parent_type->name () != NULL)
23165 return parent_type->name ();
23166 return "";
23167 }
23168 /* Fall through. */
23169 default:
23170 return determine_prefix (parent, cu);
23171 }
23172 }
23173
23174 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
23175 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
23176 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
23177 an obconcat, otherwise allocate storage for the result. The CU argument is
23178 used to determine the language and hence, the appropriate separator. */
23179
23180 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
23181
23182 static char *
23183 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
23184 int physname, struct dwarf2_cu *cu)
23185 {
23186 const char *lead = "";
23187 const char *sep;
23188
23189 if (suffix == NULL || suffix[0] == '\0'
23190 || prefix == NULL || prefix[0] == '\0')
23191 sep = "";
23192 else if (cu->language == language_d)
23193 {
23194 /* For D, the 'main' function could be defined in any module, but it
23195 should never be prefixed. */
23196 if (strcmp (suffix, "D main") == 0)
23197 {
23198 prefix = "";
23199 sep = "";
23200 }
23201 else
23202 sep = ".";
23203 }
23204 else if (cu->language == language_fortran && physname)
23205 {
23206 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
23207 DW_AT_MIPS_linkage_name is preferred and used instead. */
23208
23209 lead = "__";
23210 sep = "_MOD_";
23211 }
23212 else
23213 sep = "::";
23214
23215 if (prefix == NULL)
23216 prefix = "";
23217 if (suffix == NULL)
23218 suffix = "";
23219
23220 if (obs == NULL)
23221 {
23222 char *retval
23223 = ((char *)
23224 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
23225
23226 strcpy (retval, lead);
23227 strcat (retval, prefix);
23228 strcat (retval, sep);
23229 strcat (retval, suffix);
23230 return retval;
23231 }
23232 else
23233 {
23234 /* We have an obstack. */
23235 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
23236 }
23237 }
23238
23239 /* Get name of a die, return NULL if not found. */
23240
23241 static const char *
23242 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
23243 struct objfile *objfile)
23244 {
23245 if (name && cu->language == language_cplus)
23246 {
23247 gdb::unique_xmalloc_ptr<char> canon_name
23248 = cp_canonicalize_string (name);
23249
23250 if (canon_name != nullptr)
23251 name = objfile->intern (canon_name.get ());
23252 }
23253
23254 return name;
23255 }
23256
23257 /* Get name of a die, return NULL if not found.
23258 Anonymous namespaces are converted to their magic string. */
23259
23260 static const char *
23261 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
23262 {
23263 struct attribute *attr;
23264 struct objfile *objfile = cu->per_objfile->objfile;
23265
23266 attr = dwarf2_attr (die, DW_AT_name, cu);
23267 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
23268 if (attr_name == nullptr
23269 && die->tag != DW_TAG_namespace
23270 && die->tag != DW_TAG_class_type
23271 && die->tag != DW_TAG_interface_type
23272 && die->tag != DW_TAG_structure_type
23273 && die->tag != DW_TAG_union_type)
23274 return NULL;
23275
23276 switch (die->tag)
23277 {
23278 case DW_TAG_compile_unit:
23279 case DW_TAG_partial_unit:
23280 /* Compilation units have a DW_AT_name that is a filename, not
23281 a source language identifier. */
23282 case DW_TAG_enumeration_type:
23283 case DW_TAG_enumerator:
23284 /* These tags always have simple identifiers already; no need
23285 to canonicalize them. */
23286 return attr_name;
23287
23288 case DW_TAG_namespace:
23289 if (attr_name != nullptr)
23290 return attr_name;
23291 return CP_ANONYMOUS_NAMESPACE_STR;
23292
23293 case DW_TAG_class_type:
23294 case DW_TAG_interface_type:
23295 case DW_TAG_structure_type:
23296 case DW_TAG_union_type:
23297 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23298 structures or unions. These were of the form "._%d" in GCC 4.1,
23299 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23300 and GCC 4.4. We work around this problem by ignoring these. */
23301 if (attr_name != nullptr
23302 && (startswith (attr_name, "._")
23303 || startswith (attr_name, "<anonymous")))
23304 return NULL;
23305
23306 /* GCC might emit a nameless typedef that has a linkage name. See
23307 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23308 if (!attr || attr_name == NULL)
23309 {
23310 attr = dw2_linkage_name_attr (die, cu);
23311 attr_name = attr == nullptr ? nullptr : attr->as_string ();
23312 if (attr == NULL || attr_name == NULL)
23313 return NULL;
23314
23315 /* Avoid demangling attr_name the second time on a second
23316 call for the same DIE. */
23317 if (!attr->canonical_string_p ())
23318 {
23319 gdb::unique_xmalloc_ptr<char> demangled
23320 (gdb_demangle (attr_name, DMGL_TYPES));
23321 if (demangled == nullptr)
23322 return nullptr;
23323
23324 attr->set_string_canonical (objfile->intern (demangled.get ()));
23325 attr_name = attr->as_string ();
23326 }
23327
23328 /* Strip any leading namespaces/classes, keep only the
23329 base name. DW_AT_name for named DIEs does not
23330 contain the prefixes. */
23331 const char *base = strrchr (attr_name, ':');
23332 if (base && base > attr_name && base[-1] == ':')
23333 return &base[1];
23334 else
23335 return attr_name;
23336 }
23337 break;
23338
23339 default:
23340 break;
23341 }
23342
23343 if (!attr->canonical_string_p ())
23344 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23345 objfile));
23346 return attr->as_string ();
23347 }
23348
23349 /* Return the die that this die in an extension of, or NULL if there
23350 is none. *EXT_CU is the CU containing DIE on input, and the CU
23351 containing the return value on output. */
23352
23353 static struct die_info *
23354 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23355 {
23356 struct attribute *attr;
23357
23358 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23359 if (attr == NULL)
23360 return NULL;
23361
23362 return follow_die_ref (die, attr, ext_cu);
23363 }
23364
23365 static void
23366 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23367 {
23368 unsigned int i;
23369
23370 print_spaces (indent, f);
23371 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23372 dwarf_tag_name (die->tag), die->abbrev,
23373 sect_offset_str (die->sect_off));
23374
23375 if (die->parent != NULL)
23376 {
23377 print_spaces (indent, f);
23378 fprintf_unfiltered (f, " parent at offset: %s\n",
23379 sect_offset_str (die->parent->sect_off));
23380 }
23381
23382 print_spaces (indent, f);
23383 fprintf_unfiltered (f, " has children: %s\n",
23384 dwarf_bool_name (die->child != NULL));
23385
23386 print_spaces (indent, f);
23387 fprintf_unfiltered (f, " attributes:\n");
23388
23389 for (i = 0; i < die->num_attrs; ++i)
23390 {
23391 print_spaces (indent, f);
23392 fprintf_unfiltered (f, " %s (%s) ",
23393 dwarf_attr_name (die->attrs[i].name),
23394 dwarf_form_name (die->attrs[i].form));
23395
23396 switch (die->attrs[i].form)
23397 {
23398 case DW_FORM_addr:
23399 case DW_FORM_addrx:
23400 case DW_FORM_GNU_addr_index:
23401 fprintf_unfiltered (f, "address: ");
23402 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
23403 break;
23404 case DW_FORM_block2:
23405 case DW_FORM_block4:
23406 case DW_FORM_block:
23407 case DW_FORM_block1:
23408 fprintf_unfiltered (f, "block: size %s",
23409 pulongest (die->attrs[i].as_block ()->size));
23410 break;
23411 case DW_FORM_exprloc:
23412 fprintf_unfiltered (f, "expression: size %s",
23413 pulongest (die->attrs[i].as_block ()->size));
23414 break;
23415 case DW_FORM_data16:
23416 fprintf_unfiltered (f, "constant of 16 bytes");
23417 break;
23418 case DW_FORM_ref_addr:
23419 fprintf_unfiltered (f, "ref address: ");
23420 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23421 break;
23422 case DW_FORM_GNU_ref_alt:
23423 fprintf_unfiltered (f, "alt ref address: ");
23424 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23425 break;
23426 case DW_FORM_ref1:
23427 case DW_FORM_ref2:
23428 case DW_FORM_ref4:
23429 case DW_FORM_ref8:
23430 case DW_FORM_ref_udata:
23431 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23432 (long) (die->attrs[i].as_unsigned ()));
23433 break;
23434 case DW_FORM_data1:
23435 case DW_FORM_data2:
23436 case DW_FORM_data4:
23437 case DW_FORM_data8:
23438 case DW_FORM_udata:
23439 fprintf_unfiltered (f, "constant: %s",
23440 pulongest (die->attrs[i].as_unsigned ()));
23441 break;
23442 case DW_FORM_sec_offset:
23443 fprintf_unfiltered (f, "section offset: %s",
23444 pulongest (die->attrs[i].as_unsigned ()));
23445 break;
23446 case DW_FORM_ref_sig8:
23447 fprintf_unfiltered (f, "signature: %s",
23448 hex_string (die->attrs[i].as_signature ()));
23449 break;
23450 case DW_FORM_string:
23451 case DW_FORM_strp:
23452 case DW_FORM_line_strp:
23453 case DW_FORM_strx:
23454 case DW_FORM_GNU_str_index:
23455 case DW_FORM_GNU_strp_alt:
23456 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23457 die->attrs[i].as_string ()
23458 ? die->attrs[i].as_string () : "",
23459 die->attrs[i].canonical_string_p () ? "is" : "not");
23460 break;
23461 case DW_FORM_flag:
23462 if (die->attrs[i].as_boolean ())
23463 fprintf_unfiltered (f, "flag: TRUE");
23464 else
23465 fprintf_unfiltered (f, "flag: FALSE");
23466 break;
23467 case DW_FORM_flag_present:
23468 fprintf_unfiltered (f, "flag: TRUE");
23469 break;
23470 case DW_FORM_indirect:
23471 /* The reader will have reduced the indirect form to
23472 the "base form" so this form should not occur. */
23473 fprintf_unfiltered (f,
23474 "unexpected attribute form: DW_FORM_indirect");
23475 break;
23476 case DW_FORM_sdata:
23477 case DW_FORM_implicit_const:
23478 fprintf_unfiltered (f, "constant: %s",
23479 plongest (die->attrs[i].as_signed ()));
23480 break;
23481 default:
23482 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23483 die->attrs[i].form);
23484 break;
23485 }
23486 fprintf_unfiltered (f, "\n");
23487 }
23488 }
23489
23490 static void
23491 dump_die_for_error (struct die_info *die)
23492 {
23493 dump_die_shallow (gdb_stderr, 0, die);
23494 }
23495
23496 static void
23497 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23498 {
23499 int indent = level * 4;
23500
23501 gdb_assert (die != NULL);
23502
23503 if (level >= max_level)
23504 return;
23505
23506 dump_die_shallow (f, indent, die);
23507
23508 if (die->child != NULL)
23509 {
23510 print_spaces (indent, f);
23511 fprintf_unfiltered (f, " Children:");
23512 if (level + 1 < max_level)
23513 {
23514 fprintf_unfiltered (f, "\n");
23515 dump_die_1 (f, level + 1, max_level, die->child);
23516 }
23517 else
23518 {
23519 fprintf_unfiltered (f,
23520 " [not printed, max nesting level reached]\n");
23521 }
23522 }
23523
23524 if (die->sibling != NULL && level > 0)
23525 {
23526 dump_die_1 (f, level, max_level, die->sibling);
23527 }
23528 }
23529
23530 /* This is called from the pdie macro in gdbinit.in.
23531 It's not static so gcc will keep a copy callable from gdb. */
23532
23533 void
23534 dump_die (struct die_info *die, int max_level)
23535 {
23536 dump_die_1 (gdb_stdlog, 0, max_level, die);
23537 }
23538
23539 static void
23540 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23541 {
23542 void **slot;
23543
23544 slot = htab_find_slot_with_hash (cu->die_hash, die,
23545 to_underlying (die->sect_off),
23546 INSERT);
23547
23548 *slot = die;
23549 }
23550
23551 /* Follow reference or signature attribute ATTR of SRC_DIE.
23552 On entry *REF_CU is the CU of SRC_DIE.
23553 On exit *REF_CU is the CU of the result. */
23554
23555 static struct die_info *
23556 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23557 struct dwarf2_cu **ref_cu)
23558 {
23559 struct die_info *die;
23560
23561 if (attr->form_is_ref ())
23562 die = follow_die_ref (src_die, attr, ref_cu);
23563 else if (attr->form == DW_FORM_ref_sig8)
23564 die = follow_die_sig (src_die, attr, ref_cu);
23565 else
23566 {
23567 dump_die_for_error (src_die);
23568 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23569 objfile_name ((*ref_cu)->per_objfile->objfile));
23570 }
23571
23572 return die;
23573 }
23574
23575 /* Follow reference OFFSET.
23576 On entry *REF_CU is the CU of the source die referencing OFFSET.
23577 On exit *REF_CU is the CU of the result.
23578 Returns NULL if OFFSET is invalid. */
23579
23580 static struct die_info *
23581 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23582 struct dwarf2_cu **ref_cu)
23583 {
23584 struct die_info temp_die;
23585 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23586 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23587
23588 gdb_assert (cu->per_cu != NULL);
23589
23590 target_cu = cu;
23591
23592 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23593 "source CU contains target offset: %d",
23594 sect_offset_str (cu->per_cu->sect_off),
23595 sect_offset_str (sect_off),
23596 cu->header.offset_in_cu_p (sect_off));
23597
23598 if (cu->per_cu->is_debug_types)
23599 {
23600 /* .debug_types CUs cannot reference anything outside their CU.
23601 If they need to, they have to reference a signatured type via
23602 DW_FORM_ref_sig8. */
23603 if (!cu->header.offset_in_cu_p (sect_off))
23604 return NULL;
23605 }
23606 else if (offset_in_dwz != cu->per_cu->is_dwz
23607 || !cu->header.offset_in_cu_p (sect_off))
23608 {
23609 struct dwarf2_per_cu_data *per_cu;
23610
23611 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23612 per_objfile);
23613
23614 dwarf_read_debug_printf_v ("target CU offset: %s, "
23615 "target CU DIEs loaded: %d",
23616 sect_offset_str (per_cu->sect_off),
23617 per_objfile->get_cu (per_cu) != nullptr);
23618
23619 /* If necessary, add it to the queue and load its DIEs.
23620
23621 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23622 it doesn't mean they are currently loaded. Since we require them
23623 to be loaded, we must check for ourselves. */
23624 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language)
23625 || per_objfile->get_cu (per_cu) == nullptr)
23626 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23627 false, cu->language);
23628
23629 target_cu = per_objfile->get_cu (per_cu);
23630 gdb_assert (target_cu != nullptr);
23631 }
23632 else if (cu->dies == NULL)
23633 {
23634 /* We're loading full DIEs during partial symbol reading. */
23635 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
23636 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23637 language_minimal);
23638 }
23639
23640 *ref_cu = target_cu;
23641 temp_die.sect_off = sect_off;
23642
23643 if (target_cu != cu)
23644 target_cu->ancestor = cu;
23645
23646 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23647 &temp_die,
23648 to_underlying (sect_off));
23649 }
23650
23651 /* Follow reference attribute ATTR of SRC_DIE.
23652 On entry *REF_CU is the CU of SRC_DIE.
23653 On exit *REF_CU is the CU of the result. */
23654
23655 static struct die_info *
23656 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23657 struct dwarf2_cu **ref_cu)
23658 {
23659 sect_offset sect_off = attr->get_ref_die_offset ();
23660 struct dwarf2_cu *cu = *ref_cu;
23661 struct die_info *die;
23662
23663 die = follow_die_offset (sect_off,
23664 (attr->form == DW_FORM_GNU_ref_alt
23665 || cu->per_cu->is_dwz),
23666 ref_cu);
23667 if (!die)
23668 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23669 "at %s [in module %s]"),
23670 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23671 objfile_name (cu->per_objfile->objfile));
23672
23673 return die;
23674 }
23675
23676 /* See read.h. */
23677
23678 struct dwarf2_locexpr_baton
23679 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23680 dwarf2_per_cu_data *per_cu,
23681 dwarf2_per_objfile *per_objfile,
23682 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23683 bool resolve_abstract_p)
23684 {
23685 struct die_info *die;
23686 struct attribute *attr;
23687 struct dwarf2_locexpr_baton retval;
23688 struct objfile *objfile = per_objfile->objfile;
23689
23690 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23691 if (cu == nullptr)
23692 cu = load_cu (per_cu, per_objfile, false);
23693
23694 if (cu == nullptr)
23695 {
23696 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23697 Instead just throw an error, not much else we can do. */
23698 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23699 sect_offset_str (sect_off), objfile_name (objfile));
23700 }
23701
23702 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23703 if (!die)
23704 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23705 sect_offset_str (sect_off), objfile_name (objfile));
23706
23707 attr = dwarf2_attr (die, DW_AT_location, cu);
23708 if (!attr && resolve_abstract_p
23709 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23710 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23711 {
23712 CORE_ADDR pc = get_frame_pc ();
23713 CORE_ADDR baseaddr = objfile->text_section_offset ();
23714 struct gdbarch *gdbarch = objfile->arch ();
23715
23716 for (const auto &cand_off
23717 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23718 {
23719 struct dwarf2_cu *cand_cu = cu;
23720 struct die_info *cand
23721 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23722 if (!cand
23723 || !cand->parent
23724 || cand->parent->tag != DW_TAG_subprogram)
23725 continue;
23726
23727 CORE_ADDR pc_low, pc_high;
23728 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23729 if (pc_low == ((CORE_ADDR) -1))
23730 continue;
23731 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23732 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23733 if (!(pc_low <= pc && pc < pc_high))
23734 continue;
23735
23736 die = cand;
23737 attr = dwarf2_attr (die, DW_AT_location, cu);
23738 break;
23739 }
23740 }
23741
23742 if (!attr)
23743 {
23744 /* DWARF: "If there is no such attribute, then there is no effect.".
23745 DATA is ignored if SIZE is 0. */
23746
23747 retval.data = NULL;
23748 retval.size = 0;
23749 }
23750 else if (attr->form_is_section_offset ())
23751 {
23752 struct dwarf2_loclist_baton loclist_baton;
23753 CORE_ADDR pc = get_frame_pc ();
23754 size_t size;
23755
23756 fill_in_loclist_baton (cu, &loclist_baton, attr);
23757
23758 retval.data = dwarf2_find_location_expression (&loclist_baton,
23759 &size, pc);
23760 retval.size = size;
23761 }
23762 else
23763 {
23764 if (!attr->form_is_block ())
23765 error (_("Dwarf Error: DIE at %s referenced in module %s "
23766 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23767 sect_offset_str (sect_off), objfile_name (objfile));
23768
23769 struct dwarf_block *block = attr->as_block ();
23770 retval.data = block->data;
23771 retval.size = block->size;
23772 }
23773 retval.per_objfile = per_objfile;
23774 retval.per_cu = cu->per_cu;
23775
23776 per_objfile->age_comp_units ();
23777
23778 return retval;
23779 }
23780
23781 /* See read.h. */
23782
23783 struct dwarf2_locexpr_baton
23784 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23785 dwarf2_per_cu_data *per_cu,
23786 dwarf2_per_objfile *per_objfile,
23787 gdb::function_view<CORE_ADDR ()> get_frame_pc)
23788 {
23789 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23790
23791 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
23792 get_frame_pc);
23793 }
23794
23795 /* Write a constant of a given type as target-ordered bytes into
23796 OBSTACK. */
23797
23798 static const gdb_byte *
23799 write_constant_as_bytes (struct obstack *obstack,
23800 enum bfd_endian byte_order,
23801 struct type *type,
23802 ULONGEST value,
23803 LONGEST *len)
23804 {
23805 gdb_byte *result;
23806
23807 *len = TYPE_LENGTH (type);
23808 result = (gdb_byte *) obstack_alloc (obstack, *len);
23809 store_unsigned_integer (result, *len, byte_order, value);
23810
23811 return result;
23812 }
23813
23814 /* See read.h. */
23815
23816 const gdb_byte *
23817 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23818 dwarf2_per_cu_data *per_cu,
23819 dwarf2_per_objfile *per_objfile,
23820 obstack *obstack,
23821 LONGEST *len)
23822 {
23823 struct die_info *die;
23824 struct attribute *attr;
23825 const gdb_byte *result = NULL;
23826 struct type *type;
23827 LONGEST value;
23828 enum bfd_endian byte_order;
23829 struct objfile *objfile = per_objfile->objfile;
23830
23831 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23832 if (cu == nullptr)
23833 cu = load_cu (per_cu, per_objfile, false);
23834
23835 if (cu == nullptr)
23836 {
23837 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23838 Instead just throw an error, not much else we can do. */
23839 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23840 sect_offset_str (sect_off), objfile_name (objfile));
23841 }
23842
23843 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23844 if (!die)
23845 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23846 sect_offset_str (sect_off), objfile_name (objfile));
23847
23848 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23849 if (attr == NULL)
23850 return NULL;
23851
23852 byte_order = (bfd_big_endian (objfile->obfd)
23853 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23854
23855 switch (attr->form)
23856 {
23857 case DW_FORM_addr:
23858 case DW_FORM_addrx:
23859 case DW_FORM_GNU_addr_index:
23860 {
23861 gdb_byte *tem;
23862
23863 *len = cu->header.addr_size;
23864 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23865 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
23866 result = tem;
23867 }
23868 break;
23869 case DW_FORM_string:
23870 case DW_FORM_strp:
23871 case DW_FORM_strx:
23872 case DW_FORM_GNU_str_index:
23873 case DW_FORM_GNU_strp_alt:
23874 /* The string is already allocated on the objfile obstack, point
23875 directly to it. */
23876 {
23877 const char *attr_name = attr->as_string ();
23878 result = (const gdb_byte *) attr_name;
23879 *len = strlen (attr_name);
23880 }
23881 break;
23882 case DW_FORM_block1:
23883 case DW_FORM_block2:
23884 case DW_FORM_block4:
23885 case DW_FORM_block:
23886 case DW_FORM_exprloc:
23887 case DW_FORM_data16:
23888 {
23889 struct dwarf_block *block = attr->as_block ();
23890 result = block->data;
23891 *len = block->size;
23892 }
23893 break;
23894
23895 /* The DW_AT_const_value attributes are supposed to carry the
23896 symbol's value "represented as it would be on the target
23897 architecture." By the time we get here, it's already been
23898 converted to host endianness, so we just need to sign- or
23899 zero-extend it as appropriate. */
23900 case DW_FORM_data1:
23901 type = die_type (die, cu);
23902 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23903 if (result == NULL)
23904 result = write_constant_as_bytes (obstack, byte_order,
23905 type, value, len);
23906 break;
23907 case DW_FORM_data2:
23908 type = die_type (die, cu);
23909 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23910 if (result == NULL)
23911 result = write_constant_as_bytes (obstack, byte_order,
23912 type, value, len);
23913 break;
23914 case DW_FORM_data4:
23915 type = die_type (die, cu);
23916 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23917 if (result == NULL)
23918 result = write_constant_as_bytes (obstack, byte_order,
23919 type, value, len);
23920 break;
23921 case DW_FORM_data8:
23922 type = die_type (die, cu);
23923 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23924 if (result == NULL)
23925 result = write_constant_as_bytes (obstack, byte_order,
23926 type, value, len);
23927 break;
23928
23929 case DW_FORM_sdata:
23930 case DW_FORM_implicit_const:
23931 type = die_type (die, cu);
23932 result = write_constant_as_bytes (obstack, byte_order,
23933 type, attr->as_signed (), len);
23934 break;
23935
23936 case DW_FORM_udata:
23937 type = die_type (die, cu);
23938 result = write_constant_as_bytes (obstack, byte_order,
23939 type, attr->as_unsigned (), len);
23940 break;
23941
23942 default:
23943 complaint (_("unsupported const value attribute form: '%s'"),
23944 dwarf_form_name (attr->form));
23945 break;
23946 }
23947
23948 return result;
23949 }
23950
23951 /* See read.h. */
23952
23953 struct type *
23954 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23955 dwarf2_per_cu_data *per_cu,
23956 dwarf2_per_objfile *per_objfile)
23957 {
23958 struct die_info *die;
23959
23960 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23961 if (cu == nullptr)
23962 cu = load_cu (per_cu, per_objfile, false);
23963
23964 if (cu == nullptr)
23965 return nullptr;
23966
23967 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23968 if (!die)
23969 return NULL;
23970
23971 return die_type (die, cu);
23972 }
23973
23974 /* See read.h. */
23975
23976 struct type *
23977 dwarf2_get_die_type (cu_offset die_offset,
23978 dwarf2_per_cu_data *per_cu,
23979 dwarf2_per_objfile *per_objfile)
23980 {
23981 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23982 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
23983 }
23984
23985 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23986 On entry *REF_CU is the CU of SRC_DIE.
23987 On exit *REF_CU is the CU of the result.
23988 Returns NULL if the referenced DIE isn't found. */
23989
23990 static struct die_info *
23991 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23992 struct dwarf2_cu **ref_cu)
23993 {
23994 struct die_info temp_die;
23995 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23996 struct die_info *die;
23997 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
23998
23999
24000 /* While it might be nice to assert sig_type->type == NULL here,
24001 we can get here for DW_AT_imported_declaration where we need
24002 the DIE not the type. */
24003
24004 /* If necessary, add it to the queue and load its DIEs.
24005
24006 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
24007 it doesn't mean they are currently loaded. Since we require them
24008 to be loaded, we must check for ourselves. */
24009 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
24010 language_minimal)
24011 || per_objfile->get_cu (&sig_type->per_cu) == nullptr)
24012 read_signatured_type (sig_type, per_objfile);
24013
24014 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
24015 gdb_assert (sig_cu != NULL);
24016 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
24017 temp_die.sect_off = sig_type->type_offset_in_section;
24018 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
24019 to_underlying (temp_die.sect_off));
24020 if (die)
24021 {
24022 /* For .gdb_index version 7 keep track of included TUs.
24023 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
24024 if (per_objfile->per_bfd->index_table != NULL
24025 && per_objfile->per_bfd->index_table->version <= 7)
24026 {
24027 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
24028 }
24029
24030 *ref_cu = sig_cu;
24031 if (sig_cu != cu)
24032 sig_cu->ancestor = cu;
24033
24034 return die;
24035 }
24036
24037 return NULL;
24038 }
24039
24040 /* Follow signatured type referenced by ATTR in SRC_DIE.
24041 On entry *REF_CU is the CU of SRC_DIE.
24042 On exit *REF_CU is the CU of the result.
24043 The result is the DIE of the type.
24044 If the referenced type cannot be found an error is thrown. */
24045
24046 static struct die_info *
24047 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
24048 struct dwarf2_cu **ref_cu)
24049 {
24050 ULONGEST signature = attr->as_signature ();
24051 struct signatured_type *sig_type;
24052 struct die_info *die;
24053
24054 gdb_assert (attr->form == DW_FORM_ref_sig8);
24055
24056 sig_type = lookup_signatured_type (*ref_cu, signature);
24057 /* sig_type will be NULL if the signatured type is missing from
24058 the debug info. */
24059 if (sig_type == NULL)
24060 {
24061 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24062 " from DIE at %s [in module %s]"),
24063 hex_string (signature), sect_offset_str (src_die->sect_off),
24064 objfile_name ((*ref_cu)->per_objfile->objfile));
24065 }
24066
24067 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
24068 if (die == NULL)
24069 {
24070 dump_die_for_error (src_die);
24071 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24072 " from DIE at %s [in module %s]"),
24073 hex_string (signature), sect_offset_str (src_die->sect_off),
24074 objfile_name ((*ref_cu)->per_objfile->objfile));
24075 }
24076
24077 return die;
24078 }
24079
24080 /* Get the type specified by SIGNATURE referenced in DIE/CU,
24081 reading in and processing the type unit if necessary. */
24082
24083 static struct type *
24084 get_signatured_type (struct die_info *die, ULONGEST signature,
24085 struct dwarf2_cu *cu)
24086 {
24087 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24088 struct signatured_type *sig_type;
24089 struct dwarf2_cu *type_cu;
24090 struct die_info *type_die;
24091 struct type *type;
24092
24093 sig_type = lookup_signatured_type (cu, signature);
24094 /* sig_type will be NULL if the signatured type is missing from
24095 the debug info. */
24096 if (sig_type == NULL)
24097 {
24098 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24099 " from DIE at %s [in module %s]"),
24100 hex_string (signature), sect_offset_str (die->sect_off),
24101 objfile_name (per_objfile->objfile));
24102 return build_error_marker_type (cu, die);
24103 }
24104
24105 /* If we already know the type we're done. */
24106 type = per_objfile->get_type_for_signatured_type (sig_type);
24107 if (type != nullptr)
24108 return type;
24109
24110 type_cu = cu;
24111 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24112 if (type_die != NULL)
24113 {
24114 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24115 is created. This is important, for example, because for c++ classes
24116 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24117 type = read_type_die (type_die, type_cu);
24118 if (type == NULL)
24119 {
24120 complaint (_("Dwarf Error: Cannot build signatured type %s"
24121 " referenced from DIE at %s [in module %s]"),
24122 hex_string (signature), sect_offset_str (die->sect_off),
24123 objfile_name (per_objfile->objfile));
24124 type = build_error_marker_type (cu, die);
24125 }
24126 }
24127 else
24128 {
24129 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24130 " from DIE at %s [in module %s]"),
24131 hex_string (signature), sect_offset_str (die->sect_off),
24132 objfile_name (per_objfile->objfile));
24133 type = build_error_marker_type (cu, die);
24134 }
24135
24136 per_objfile->set_type_for_signatured_type (sig_type, type);
24137
24138 return type;
24139 }
24140
24141 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24142 reading in and processing the type unit if necessary. */
24143
24144 static struct type *
24145 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
24146 struct dwarf2_cu *cu) /* ARI: editCase function */
24147 {
24148 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
24149 if (attr->form_is_ref ())
24150 {
24151 struct dwarf2_cu *type_cu = cu;
24152 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24153
24154 return read_type_die (type_die, type_cu);
24155 }
24156 else if (attr->form == DW_FORM_ref_sig8)
24157 {
24158 return get_signatured_type (die, attr->as_signature (), cu);
24159 }
24160 else
24161 {
24162 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24163
24164 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
24165 " at %s [in module %s]"),
24166 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
24167 objfile_name (per_objfile->objfile));
24168 return build_error_marker_type (cu, die);
24169 }
24170 }
24171
24172 /* Load the DIEs associated with type unit PER_CU into memory. */
24173
24174 static void
24175 load_full_type_unit (dwarf2_per_cu_data *per_cu,
24176 dwarf2_per_objfile *per_objfile)
24177 {
24178 struct signatured_type *sig_type;
24179
24180 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24181 gdb_assert (! per_cu->type_unit_group_p ());
24182
24183 /* We have the per_cu, but we need the signatured_type.
24184 Fortunately this is an easy translation. */
24185 gdb_assert (per_cu->is_debug_types);
24186 sig_type = (struct signatured_type *) per_cu;
24187
24188 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
24189
24190 read_signatured_type (sig_type, per_objfile);
24191
24192 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
24193 }
24194
24195 /* Read in a signatured type and build its CU and DIEs.
24196 If the type is a stub for the real type in a DWO file,
24197 read in the real type from the DWO file as well. */
24198
24199 static void
24200 read_signatured_type (signatured_type *sig_type,
24201 dwarf2_per_objfile *per_objfile)
24202 {
24203 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
24204
24205 gdb_assert (per_cu->is_debug_types);
24206 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
24207
24208 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
24209
24210 if (!reader.dummy_p)
24211 {
24212 struct dwarf2_cu *cu = reader.cu;
24213 const gdb_byte *info_ptr = reader.info_ptr;
24214
24215 gdb_assert (cu->die_hash == NULL);
24216 cu->die_hash =
24217 htab_create_alloc_ex (cu->header.length / 12,
24218 die_hash,
24219 die_eq,
24220 NULL,
24221 &cu->comp_unit_obstack,
24222 hashtab_obstack_allocate,
24223 dummy_obstack_deallocate);
24224
24225 if (reader.comp_unit_die->has_children)
24226 reader.comp_unit_die->child
24227 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24228 reader.comp_unit_die);
24229 cu->dies = reader.comp_unit_die;
24230 /* comp_unit_die is not stored in die_hash, no need. */
24231
24232 /* We try not to read any attributes in this function, because
24233 not all CUs needed for references have been loaded yet, and
24234 symbol table processing isn't initialized. But we have to
24235 set the CU language, or we won't be able to build types
24236 correctly. Similarly, if we do not read the producer, we can
24237 not apply producer-specific interpretation. */
24238 prepare_one_comp_unit (cu, cu->dies, language_minimal);
24239
24240 reader.keep ();
24241 }
24242
24243 sig_type->per_cu.tu_read = 1;
24244 }
24245
24246 /* Decode simple location descriptions.
24247 Given a pointer to a dwarf block that defines a location, compute
24248 the location and return the value. If COMPUTED is non-null, it is
24249 set to true to indicate that decoding was successful, and false
24250 otherwise. If COMPUTED is null, then this function may emit a
24251 complaint. */
24252
24253 static CORE_ADDR
24254 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
24255 {
24256 struct objfile *objfile = cu->per_objfile->objfile;
24257 size_t i;
24258 size_t size = blk->size;
24259 const gdb_byte *data = blk->data;
24260 CORE_ADDR stack[64];
24261 int stacki;
24262 unsigned int bytes_read, unsnd;
24263 gdb_byte op;
24264
24265 if (computed != nullptr)
24266 *computed = false;
24267
24268 i = 0;
24269 stacki = 0;
24270 stack[stacki] = 0;
24271 stack[++stacki] = 0;
24272
24273 while (i < size)
24274 {
24275 op = data[i++];
24276 switch (op)
24277 {
24278 case DW_OP_lit0:
24279 case DW_OP_lit1:
24280 case DW_OP_lit2:
24281 case DW_OP_lit3:
24282 case DW_OP_lit4:
24283 case DW_OP_lit5:
24284 case DW_OP_lit6:
24285 case DW_OP_lit7:
24286 case DW_OP_lit8:
24287 case DW_OP_lit9:
24288 case DW_OP_lit10:
24289 case DW_OP_lit11:
24290 case DW_OP_lit12:
24291 case DW_OP_lit13:
24292 case DW_OP_lit14:
24293 case DW_OP_lit15:
24294 case DW_OP_lit16:
24295 case DW_OP_lit17:
24296 case DW_OP_lit18:
24297 case DW_OP_lit19:
24298 case DW_OP_lit20:
24299 case DW_OP_lit21:
24300 case DW_OP_lit22:
24301 case DW_OP_lit23:
24302 case DW_OP_lit24:
24303 case DW_OP_lit25:
24304 case DW_OP_lit26:
24305 case DW_OP_lit27:
24306 case DW_OP_lit28:
24307 case DW_OP_lit29:
24308 case DW_OP_lit30:
24309 case DW_OP_lit31:
24310 stack[++stacki] = op - DW_OP_lit0;
24311 break;
24312
24313 case DW_OP_reg0:
24314 case DW_OP_reg1:
24315 case DW_OP_reg2:
24316 case DW_OP_reg3:
24317 case DW_OP_reg4:
24318 case DW_OP_reg5:
24319 case DW_OP_reg6:
24320 case DW_OP_reg7:
24321 case DW_OP_reg8:
24322 case DW_OP_reg9:
24323 case DW_OP_reg10:
24324 case DW_OP_reg11:
24325 case DW_OP_reg12:
24326 case DW_OP_reg13:
24327 case DW_OP_reg14:
24328 case DW_OP_reg15:
24329 case DW_OP_reg16:
24330 case DW_OP_reg17:
24331 case DW_OP_reg18:
24332 case DW_OP_reg19:
24333 case DW_OP_reg20:
24334 case DW_OP_reg21:
24335 case DW_OP_reg22:
24336 case DW_OP_reg23:
24337 case DW_OP_reg24:
24338 case DW_OP_reg25:
24339 case DW_OP_reg26:
24340 case DW_OP_reg27:
24341 case DW_OP_reg28:
24342 case DW_OP_reg29:
24343 case DW_OP_reg30:
24344 case DW_OP_reg31:
24345 stack[++stacki] = op - DW_OP_reg0;
24346 if (i < size)
24347 {
24348 if (computed == nullptr)
24349 dwarf2_complex_location_expr_complaint ();
24350 else
24351 return 0;
24352 }
24353 break;
24354
24355 case DW_OP_regx:
24356 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24357 i += bytes_read;
24358 stack[++stacki] = unsnd;
24359 if (i < size)
24360 {
24361 if (computed == nullptr)
24362 dwarf2_complex_location_expr_complaint ();
24363 else
24364 return 0;
24365 }
24366 break;
24367
24368 case DW_OP_addr:
24369 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24370 &bytes_read);
24371 i += bytes_read;
24372 break;
24373
24374 case DW_OP_const1u:
24375 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24376 i += 1;
24377 break;
24378
24379 case DW_OP_const1s:
24380 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24381 i += 1;
24382 break;
24383
24384 case DW_OP_const2u:
24385 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24386 i += 2;
24387 break;
24388
24389 case DW_OP_const2s:
24390 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24391 i += 2;
24392 break;
24393
24394 case DW_OP_const4u:
24395 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24396 i += 4;
24397 break;
24398
24399 case DW_OP_const4s:
24400 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24401 i += 4;
24402 break;
24403
24404 case DW_OP_const8u:
24405 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24406 i += 8;
24407 break;
24408
24409 case DW_OP_constu:
24410 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24411 &bytes_read);
24412 i += bytes_read;
24413 break;
24414
24415 case DW_OP_consts:
24416 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24417 i += bytes_read;
24418 break;
24419
24420 case DW_OP_dup:
24421 stack[stacki + 1] = stack[stacki];
24422 stacki++;
24423 break;
24424
24425 case DW_OP_plus:
24426 stack[stacki - 1] += stack[stacki];
24427 stacki--;
24428 break;
24429
24430 case DW_OP_plus_uconst:
24431 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24432 &bytes_read);
24433 i += bytes_read;
24434 break;
24435
24436 case DW_OP_minus:
24437 stack[stacki - 1] -= stack[stacki];
24438 stacki--;
24439 break;
24440
24441 case DW_OP_deref:
24442 /* If we're not the last op, then we definitely can't encode
24443 this using GDB's address_class enum. This is valid for partial
24444 global symbols, although the variable's address will be bogus
24445 in the psymtab. */
24446 if (i < size)
24447 {
24448 if (computed == nullptr)
24449 dwarf2_complex_location_expr_complaint ();
24450 else
24451 return 0;
24452 }
24453 break;
24454
24455 case DW_OP_GNU_push_tls_address:
24456 case DW_OP_form_tls_address:
24457 /* The top of the stack has the offset from the beginning
24458 of the thread control block at which the variable is located. */
24459 /* Nothing should follow this operator, so the top of stack would
24460 be returned. */
24461 /* This is valid for partial global symbols, but the variable's
24462 address will be bogus in the psymtab. Make it always at least
24463 non-zero to not look as a variable garbage collected by linker
24464 which have DW_OP_addr 0. */
24465 if (i < size)
24466 {
24467 if (computed == nullptr)
24468 dwarf2_complex_location_expr_complaint ();
24469 else
24470 return 0;
24471 }
24472 stack[stacki]++;
24473 break;
24474
24475 case DW_OP_GNU_uninit:
24476 if (computed != nullptr)
24477 return 0;
24478 break;
24479
24480 case DW_OP_addrx:
24481 case DW_OP_GNU_addr_index:
24482 case DW_OP_GNU_const_index:
24483 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24484 &bytes_read);
24485 i += bytes_read;
24486 break;
24487
24488 default:
24489 if (computed == nullptr)
24490 {
24491 const char *name = get_DW_OP_name (op);
24492
24493 if (name)
24494 complaint (_("unsupported stack op: '%s'"),
24495 name);
24496 else
24497 complaint (_("unsupported stack op: '%02x'"),
24498 op);
24499 }
24500
24501 return (stack[stacki]);
24502 }
24503
24504 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24505 outside of the allocated space. Also enforce minimum>0. */
24506 if (stacki >= ARRAY_SIZE (stack) - 1)
24507 {
24508 if (computed == nullptr)
24509 complaint (_("location description stack overflow"));
24510 return 0;
24511 }
24512
24513 if (stacki <= 0)
24514 {
24515 if (computed == nullptr)
24516 complaint (_("location description stack underflow"));
24517 return 0;
24518 }
24519 }
24520
24521 if (computed != nullptr)
24522 *computed = true;
24523 return (stack[stacki]);
24524 }
24525
24526 /* memory allocation interface */
24527
24528 static struct dwarf_block *
24529 dwarf_alloc_block (struct dwarf2_cu *cu)
24530 {
24531 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24532 }
24533
24534 static struct die_info *
24535 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24536 {
24537 struct die_info *die;
24538 size_t size = sizeof (struct die_info);
24539
24540 if (num_attrs > 1)
24541 size += (num_attrs - 1) * sizeof (struct attribute);
24542
24543 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24544 memset (die, 0, sizeof (struct die_info));
24545 return (die);
24546 }
24547
24548 \f
24549
24550 /* Macro support. */
24551
24552 /* An overload of dwarf_decode_macros that finds the correct section
24553 and ensures it is read in before calling the other overload. */
24554
24555 static void
24556 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24557 int section_is_gnu)
24558 {
24559 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24560 struct objfile *objfile = per_objfile->objfile;
24561 const struct line_header *lh = cu->line_header;
24562 unsigned int offset_size = cu->header.offset_size;
24563 struct dwarf2_section_info *section;
24564 const char *section_name;
24565
24566 if (cu->dwo_unit != nullptr)
24567 {
24568 if (section_is_gnu)
24569 {
24570 section = &cu->dwo_unit->dwo_file->sections.macro;
24571 section_name = ".debug_macro.dwo";
24572 }
24573 else
24574 {
24575 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24576 section_name = ".debug_macinfo.dwo";
24577 }
24578 }
24579 else
24580 {
24581 if (section_is_gnu)
24582 {
24583 section = &per_objfile->per_bfd->macro;
24584 section_name = ".debug_macro";
24585 }
24586 else
24587 {
24588 section = &per_objfile->per_bfd->macinfo;
24589 section_name = ".debug_macinfo";
24590 }
24591 }
24592
24593 section->read (objfile);
24594 if (section->buffer == nullptr)
24595 {
24596 complaint (_("missing %s section"), section_name);
24597 return;
24598 }
24599
24600 buildsym_compunit *builder = cu->get_builder ();
24601
24602 struct dwarf2_section_info *str_offsets_section;
24603 struct dwarf2_section_info *str_section;
24604 ULONGEST str_offsets_base;
24605
24606 if (cu->dwo_unit != nullptr)
24607 {
24608 str_offsets_section = &cu->dwo_unit->dwo_file
24609 ->sections.str_offsets;
24610 str_section = &cu->dwo_unit->dwo_file->sections.str;
24611 str_offsets_base = cu->header.addr_size;
24612 }
24613 else
24614 {
24615 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24616 str_section = &per_objfile->per_bfd->str;
24617 str_offsets_base = *cu->str_offsets_base;
24618 }
24619
24620 dwarf_decode_macros (per_objfile, builder, section, lh,
24621 offset_size, offset, str_section, str_offsets_section,
24622 str_offsets_base, section_is_gnu);
24623 }
24624
24625 /* Return the .debug_loc section to use for CU.
24626 For DWO files use .debug_loc.dwo. */
24627
24628 static struct dwarf2_section_info *
24629 cu_debug_loc_section (struct dwarf2_cu *cu)
24630 {
24631 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24632
24633 if (cu->dwo_unit)
24634 {
24635 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24636
24637 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24638 }
24639 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24640 : &per_objfile->per_bfd->loc);
24641 }
24642
24643 /* Return the .debug_rnglists section to use for CU. */
24644 static struct dwarf2_section_info *
24645 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24646 {
24647 if (cu->header.version < 5)
24648 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24649 cu->header.version);
24650 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24651
24652 /* Make sure we read the .debug_rnglists section from the file that
24653 contains the DW_AT_ranges attribute we are reading. Normally that
24654 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24655 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24656 program. */
24657 if (cu->dwo_unit != nullptr
24658 && tag != DW_TAG_compile_unit
24659 && tag != DW_TAG_skeleton_unit)
24660 {
24661 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24662
24663 if (sections->rnglists.size > 0)
24664 return &sections->rnglists;
24665 else
24666 error (_(".debug_rnglists section is missing from .dwo file."));
24667 }
24668 return &dwarf2_per_objfile->per_bfd->rnglists;
24669 }
24670
24671 /* A helper function that fills in a dwarf2_loclist_baton. */
24672
24673 static void
24674 fill_in_loclist_baton (struct dwarf2_cu *cu,
24675 struct dwarf2_loclist_baton *baton,
24676 const struct attribute *attr)
24677 {
24678 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24679 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24680
24681 section->read (per_objfile->objfile);
24682
24683 baton->per_objfile = per_objfile;
24684 baton->per_cu = cu->per_cu;
24685 gdb_assert (baton->per_cu);
24686 /* We don't know how long the location list is, but make sure we
24687 don't run off the edge of the section. */
24688 baton->size = section->size - attr->as_unsigned ();
24689 baton->data = section->buffer + attr->as_unsigned ();
24690 if (cu->base_address.has_value ())
24691 baton->base_address = *cu->base_address;
24692 else
24693 baton->base_address = 0;
24694 baton->from_dwo = cu->dwo_unit != NULL;
24695 }
24696
24697 static void
24698 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24699 struct dwarf2_cu *cu, int is_block)
24700 {
24701 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24702 struct objfile *objfile = per_objfile->objfile;
24703 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24704
24705 if (attr->form_is_section_offset ()
24706 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24707 the section. If so, fall through to the complaint in the
24708 other branch. */
24709 && attr->as_unsigned () < section->get_size (objfile))
24710 {
24711 struct dwarf2_loclist_baton *baton;
24712
24713 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24714
24715 fill_in_loclist_baton (cu, baton, attr);
24716
24717 if (!cu->base_address.has_value ())
24718 complaint (_("Location list used without "
24719 "specifying the CU base address."));
24720
24721 SYMBOL_ACLASS_INDEX (sym) = (is_block
24722 ? dwarf2_loclist_block_index
24723 : dwarf2_loclist_index);
24724 SYMBOL_LOCATION_BATON (sym) = baton;
24725 }
24726 else
24727 {
24728 struct dwarf2_locexpr_baton *baton;
24729
24730 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24731 baton->per_objfile = per_objfile;
24732 baton->per_cu = cu->per_cu;
24733 gdb_assert (baton->per_cu);
24734
24735 if (attr->form_is_block ())
24736 {
24737 /* Note that we're just copying the block's data pointer
24738 here, not the actual data. We're still pointing into the
24739 info_buffer for SYM's objfile; right now we never release
24740 that buffer, but when we do clean up properly this may
24741 need to change. */
24742 struct dwarf_block *block = attr->as_block ();
24743 baton->size = block->size;
24744 baton->data = block->data;
24745 }
24746 else
24747 {
24748 dwarf2_invalid_attrib_class_complaint ("location description",
24749 sym->natural_name ());
24750 baton->size = 0;
24751 }
24752
24753 SYMBOL_ACLASS_INDEX (sym) = (is_block
24754 ? dwarf2_locexpr_block_index
24755 : dwarf2_locexpr_index);
24756 SYMBOL_LOCATION_BATON (sym) = baton;
24757 }
24758 }
24759
24760 /* See read.h. */
24761
24762 const comp_unit_head *
24763 dwarf2_per_cu_data::get_header () const
24764 {
24765 if (!m_header_read_in)
24766 {
24767 const gdb_byte *info_ptr
24768 = this->section->buffer + to_underlying (this->sect_off);
24769
24770 memset (&m_header, 0, sizeof (m_header));
24771
24772 read_comp_unit_head (&m_header, info_ptr, this->section,
24773 rcuh_kind::COMPILE);
24774
24775 m_header_read_in = true;
24776 }
24777
24778 return &m_header;
24779 }
24780
24781 /* See read.h. */
24782
24783 int
24784 dwarf2_per_cu_data::addr_size () const
24785 {
24786 return this->get_header ()->addr_size;
24787 }
24788
24789 /* See read.h. */
24790
24791 int
24792 dwarf2_per_cu_data::offset_size () const
24793 {
24794 return this->get_header ()->offset_size;
24795 }
24796
24797 /* See read.h. */
24798
24799 int
24800 dwarf2_per_cu_data::ref_addr_size () const
24801 {
24802 const comp_unit_head *header = this->get_header ();
24803
24804 if (header->version == 2)
24805 return header->addr_size;
24806 else
24807 return header->offset_size;
24808 }
24809
24810 /* See read.h. */
24811
24812 struct type *
24813 dwarf2_cu::addr_type () const
24814 {
24815 struct objfile *objfile = this->per_objfile->objfile;
24816 struct type *void_type = objfile_type (objfile)->builtin_void;
24817 struct type *addr_type = lookup_pointer_type (void_type);
24818 int addr_size = this->per_cu->addr_size ();
24819
24820 if (TYPE_LENGTH (addr_type) == addr_size)
24821 return addr_type;
24822
24823 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
24824 return addr_type;
24825 }
24826
24827 /* A helper function for dwarf2_find_containing_comp_unit that returns
24828 the index of the result, and that searches a vector. It will
24829 return a result even if the offset in question does not actually
24830 occur in any CU. This is separate so that it can be unit
24831 tested. */
24832
24833 static int
24834 dwarf2_find_containing_comp_unit
24835 (sect_offset sect_off,
24836 unsigned int offset_in_dwz,
24837 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
24838 {
24839 int low, high;
24840
24841 low = 0;
24842 high = all_comp_units.size () - 1;
24843 while (high > low)
24844 {
24845 struct dwarf2_per_cu_data *mid_cu;
24846 int mid = low + (high - low) / 2;
24847
24848 mid_cu = all_comp_units[mid];
24849 if (mid_cu->is_dwz > offset_in_dwz
24850 || (mid_cu->is_dwz == offset_in_dwz
24851 && mid_cu->sect_off + mid_cu->length > sect_off))
24852 high = mid;
24853 else
24854 low = mid + 1;
24855 }
24856 gdb_assert (low == high);
24857 return low;
24858 }
24859
24860 /* Locate the .debug_info compilation unit from CU's objfile which contains
24861 the DIE at OFFSET. Raises an error on failure. */
24862
24863 static struct dwarf2_per_cu_data *
24864 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24865 unsigned int offset_in_dwz,
24866 dwarf2_per_objfile *per_objfile)
24867 {
24868 int low = dwarf2_find_containing_comp_unit
24869 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24870 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
24871
24872 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24873 {
24874 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24875 error (_("Dwarf Error: could not find partial DIE containing "
24876 "offset %s [in module %s]"),
24877 sect_offset_str (sect_off),
24878 bfd_get_filename (per_objfile->objfile->obfd));
24879
24880 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
24881 <= sect_off);
24882 return per_objfile->per_bfd->all_comp_units[low-1];
24883 }
24884 else
24885 {
24886 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
24887 && sect_off >= this_cu->sect_off + this_cu->length)
24888 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24889 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24890 return this_cu;
24891 }
24892 }
24893
24894 #if GDB_SELF_TEST
24895
24896 namespace selftests {
24897 namespace find_containing_comp_unit {
24898
24899 static void
24900 run_test ()
24901 {
24902 struct dwarf2_per_cu_data one {};
24903 struct dwarf2_per_cu_data two {};
24904 struct dwarf2_per_cu_data three {};
24905 struct dwarf2_per_cu_data four {};
24906
24907 one.length = 5;
24908 two.sect_off = sect_offset (one.length);
24909 two.length = 7;
24910
24911 three.length = 5;
24912 three.is_dwz = 1;
24913 four.sect_off = sect_offset (three.length);
24914 four.length = 7;
24915 four.is_dwz = 1;
24916
24917 std::vector<dwarf2_per_cu_data *> units;
24918 units.push_back (&one);
24919 units.push_back (&two);
24920 units.push_back (&three);
24921 units.push_back (&four);
24922
24923 int result;
24924
24925 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24926 SELF_CHECK (units[result] == &one);
24927 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24928 SELF_CHECK (units[result] == &one);
24929 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24930 SELF_CHECK (units[result] == &two);
24931
24932 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24933 SELF_CHECK (units[result] == &three);
24934 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24935 SELF_CHECK (units[result] == &three);
24936 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24937 SELF_CHECK (units[result] == &four);
24938 }
24939
24940 }
24941 }
24942
24943 #endif /* GDB_SELF_TEST */
24944
24945 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
24946
24947 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
24948 dwarf2_per_objfile *per_objfile)
24949 : per_cu (per_cu),
24950 per_objfile (per_objfile),
24951 mark (false),
24952 has_loclist (false),
24953 checked_producer (false),
24954 producer_is_gxx_lt_4_6 (false),
24955 producer_is_gcc_lt_4_3 (false),
24956 producer_is_icc (false),
24957 producer_is_icc_lt_14 (false),
24958 producer_is_codewarrior (false),
24959 processing_has_namespace_info (false)
24960 {
24961 }
24962
24963 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24964
24965 static void
24966 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24967 enum language pretend_language)
24968 {
24969 struct attribute *attr;
24970
24971 /* Set the language we're debugging. */
24972 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24973 if (attr != nullptr)
24974 set_cu_language (attr->constant_value (0), cu);
24975 else
24976 {
24977 cu->language = pretend_language;
24978 cu->language_defn = language_def (cu->language);
24979 }
24980
24981 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24982 }
24983
24984 /* See read.h. */
24985
24986 dwarf2_cu *
24987 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
24988 {
24989 auto it = m_dwarf2_cus.find (per_cu);
24990 if (it == m_dwarf2_cus.end ())
24991 return nullptr;
24992
24993 return it->second;
24994 }
24995
24996 /* See read.h. */
24997
24998 void
24999 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
25000 {
25001 gdb_assert (this->get_cu (per_cu) == nullptr);
25002
25003 m_dwarf2_cus[per_cu] = cu;
25004 }
25005
25006 /* See read.h. */
25007
25008 void
25009 dwarf2_per_objfile::age_comp_units ()
25010 {
25011 dwarf_read_debug_printf_v ("running");
25012
25013 /* This is not expected to be called in the middle of CU expansion. There is
25014 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
25015 loaded in memory. Calling age_comp_units while the queue is in use could
25016 make us free the DIEs for a CU that is in the queue and therefore break
25017 that invariant. */
25018 gdb_assert (!this->per_bfd->queue.has_value ());
25019
25020 /* Start by clearing all marks. */
25021 for (auto pair : m_dwarf2_cus)
25022 pair.second->mark = false;
25023
25024 /* Traverse all CUs, mark them and their dependencies if used recently
25025 enough. */
25026 for (auto pair : m_dwarf2_cus)
25027 {
25028 dwarf2_cu *cu = pair.second;
25029
25030 cu->last_used++;
25031 if (cu->last_used <= dwarf_max_cache_age)
25032 dwarf2_mark (cu);
25033 }
25034
25035 /* Delete all CUs still not marked. */
25036 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
25037 {
25038 dwarf2_cu *cu = it->second;
25039
25040 if (!cu->mark)
25041 {
25042 dwarf_read_debug_printf_v ("deleting old CU %s",
25043 sect_offset_str (cu->per_cu->sect_off));
25044 delete cu;
25045 it = m_dwarf2_cus.erase (it);
25046 }
25047 else
25048 it++;
25049 }
25050 }
25051
25052 /* See read.h. */
25053
25054 void
25055 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
25056 {
25057 auto it = m_dwarf2_cus.find (per_cu);
25058 if (it == m_dwarf2_cus.end ())
25059 return;
25060
25061 delete it->second;
25062
25063 m_dwarf2_cus.erase (it);
25064 }
25065
25066 dwarf2_per_objfile::~dwarf2_per_objfile ()
25067 {
25068 remove_all_cus ();
25069 }
25070
25071 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25072 We store these in a hash table separate from the DIEs, and preserve them
25073 when the DIEs are flushed out of cache.
25074
25075 The CU "per_cu" pointer is needed because offset alone is not enough to
25076 uniquely identify the type. A file may have multiple .debug_types sections,
25077 or the type may come from a DWO file. Furthermore, while it's more logical
25078 to use per_cu->section+offset, with Fission the section with the data is in
25079 the DWO file but we don't know that section at the point we need it.
25080 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25081 because we can enter the lookup routine, get_die_type_at_offset, from
25082 outside this file, and thus won't necessarily have PER_CU->cu.
25083 Fortunately, PER_CU is stable for the life of the objfile. */
25084
25085 struct dwarf2_per_cu_offset_and_type
25086 {
25087 const struct dwarf2_per_cu_data *per_cu;
25088 sect_offset sect_off;
25089 struct type *type;
25090 };
25091
25092 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25093
25094 static hashval_t
25095 per_cu_offset_and_type_hash (const void *item)
25096 {
25097 const struct dwarf2_per_cu_offset_and_type *ofs
25098 = (const struct dwarf2_per_cu_offset_and_type *) item;
25099
25100 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25101 }
25102
25103 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25104
25105 static int
25106 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25107 {
25108 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25109 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25110 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25111 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25112
25113 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25114 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25115 }
25116
25117 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25118 table if necessary. For convenience, return TYPE.
25119
25120 The DIEs reading must have careful ordering to:
25121 * Not cause infinite loops trying to read in DIEs as a prerequisite for
25122 reading current DIE.
25123 * Not trying to dereference contents of still incompletely read in types
25124 while reading in other DIEs.
25125 * Enable referencing still incompletely read in types just by a pointer to
25126 the type without accessing its fields.
25127
25128 Therefore caller should follow these rules:
25129 * Try to fetch any prerequisite types we may need to build this DIE type
25130 before building the type and calling set_die_type.
25131 * After building type call set_die_type for current DIE as soon as
25132 possible before fetching more types to complete the current type.
25133 * Make the type as complete as possible before fetching more types. */
25134
25135 static struct type *
25136 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
25137 bool skip_data_location)
25138 {
25139 dwarf2_per_objfile *per_objfile = cu->per_objfile;
25140 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25141 struct objfile *objfile = per_objfile->objfile;
25142 struct attribute *attr;
25143 struct dynamic_prop prop;
25144
25145 /* For Ada types, make sure that the gnat-specific data is always
25146 initialized (if not already set). There are a few types where
25147 we should not be doing so, because the type-specific area is
25148 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25149 where the type-specific area is used to store the floatformat).
25150 But this is not a problem, because the gnat-specific information
25151 is actually not needed for these types. */
25152 if (need_gnat_info (cu)
25153 && type->code () != TYPE_CODE_FUNC
25154 && type->code () != TYPE_CODE_FLT
25155 && type->code () != TYPE_CODE_METHODPTR
25156 && type->code () != TYPE_CODE_MEMBERPTR
25157 && type->code () != TYPE_CODE_METHOD
25158 && type->code () != TYPE_CODE_FIXED_POINT
25159 && !HAVE_GNAT_AUX_INFO (type))
25160 INIT_GNAT_SPECIFIC (type);
25161
25162 /* Read DW_AT_allocated and set in type. */
25163 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25164 if (attr != NULL)
25165 {
25166 struct type *prop_type = cu->addr_sized_int_type (false);
25167 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25168 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
25169 }
25170
25171 /* Read DW_AT_associated and set in type. */
25172 attr = dwarf2_attr (die, DW_AT_associated, cu);
25173 if (attr != NULL)
25174 {
25175 struct type *prop_type = cu->addr_sized_int_type (false);
25176 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25177 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
25178 }
25179
25180 /* Read DW_AT_data_location and set in type. */
25181 if (!skip_data_location)
25182 {
25183 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25184 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
25185 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
25186 }
25187
25188 if (per_objfile->die_type_hash == NULL)
25189 per_objfile->die_type_hash
25190 = htab_up (htab_create_alloc (127,
25191 per_cu_offset_and_type_hash,
25192 per_cu_offset_and_type_eq,
25193 NULL, xcalloc, xfree));
25194
25195 ofs.per_cu = cu->per_cu;
25196 ofs.sect_off = die->sect_off;
25197 ofs.type = type;
25198 slot = (struct dwarf2_per_cu_offset_and_type **)
25199 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
25200 if (*slot)
25201 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25202 sect_offset_str (die->sect_off));
25203 *slot = XOBNEW (&objfile->objfile_obstack,
25204 struct dwarf2_per_cu_offset_and_type);
25205 **slot = ofs;
25206 return type;
25207 }
25208
25209 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25210 or return NULL if the die does not have a saved type. */
25211
25212 static struct type *
25213 get_die_type_at_offset (sect_offset sect_off,
25214 dwarf2_per_cu_data *per_cu,
25215 dwarf2_per_objfile *per_objfile)
25216 {
25217 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25218
25219 if (per_objfile->die_type_hash == NULL)
25220 return NULL;
25221
25222 ofs.per_cu = per_cu;
25223 ofs.sect_off = sect_off;
25224 slot = ((struct dwarf2_per_cu_offset_and_type *)
25225 htab_find (per_objfile->die_type_hash.get (), &ofs));
25226 if (slot)
25227 return slot->type;
25228 else
25229 return NULL;
25230 }
25231
25232 /* Look up the type for DIE in CU in die_type_hash,
25233 or return NULL if DIE does not have a saved type. */
25234
25235 static struct type *
25236 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25237 {
25238 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
25239 }
25240
25241 /* Add a dependence relationship from CU to REF_PER_CU. */
25242
25243 static void
25244 dwarf2_add_dependence (struct dwarf2_cu *cu,
25245 struct dwarf2_per_cu_data *ref_per_cu)
25246 {
25247 void **slot;
25248
25249 if (cu->dependencies == NULL)
25250 cu->dependencies
25251 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25252 NULL, &cu->comp_unit_obstack,
25253 hashtab_obstack_allocate,
25254 dummy_obstack_deallocate);
25255
25256 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25257 if (*slot == NULL)
25258 *slot = ref_per_cu;
25259 }
25260
25261 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25262 Set the mark field in every compilation unit in the
25263 cache that we must keep because we are keeping CU.
25264
25265 DATA is the dwarf2_per_objfile object in which to look up CUs. */
25266
25267 static int
25268 dwarf2_mark_helper (void **slot, void *data)
25269 {
25270 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
25271 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
25272 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
25273
25274 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25275 reading of the chain. As such dependencies remain valid it is not much
25276 useful to track and undo them during QUIT cleanups. */
25277 if (cu == nullptr)
25278 return 1;
25279
25280 if (cu->mark)
25281 return 1;
25282
25283 cu->mark = true;
25284
25285 if (cu->dependencies != nullptr)
25286 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
25287
25288 return 1;
25289 }
25290
25291 /* Set the mark field in CU and in every other compilation unit in the
25292 cache that we must keep because we are keeping CU. */
25293
25294 static void
25295 dwarf2_mark (struct dwarf2_cu *cu)
25296 {
25297 if (cu->mark)
25298 return;
25299
25300 cu->mark = true;
25301
25302 if (cu->dependencies != nullptr)
25303 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
25304 }
25305
25306 /* Trivial hash function for partial_die_info: the hash value of a DIE
25307 is its offset in .debug_info for this objfile. */
25308
25309 static hashval_t
25310 partial_die_hash (const void *item)
25311 {
25312 const struct partial_die_info *part_die
25313 = (const struct partial_die_info *) item;
25314
25315 return to_underlying (part_die->sect_off);
25316 }
25317
25318 /* Trivial comparison function for partial_die_info structures: two DIEs
25319 are equal if they have the same offset. */
25320
25321 static int
25322 partial_die_eq (const void *item_lhs, const void *item_rhs)
25323 {
25324 const struct partial_die_info *part_die_lhs
25325 = (const struct partial_die_info *) item_lhs;
25326 const struct partial_die_info *part_die_rhs
25327 = (const struct partial_die_info *) item_rhs;
25328
25329 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25330 }
25331
25332 struct cmd_list_element *set_dwarf_cmdlist;
25333 struct cmd_list_element *show_dwarf_cmdlist;
25334
25335 static void
25336 show_check_physname (struct ui_file *file, int from_tty,
25337 struct cmd_list_element *c, const char *value)
25338 {
25339 fprintf_filtered (file,
25340 _("Whether to check \"physname\" is %s.\n"),
25341 value);
25342 }
25343
25344 void _initialize_dwarf2_read ();
25345 void
25346 _initialize_dwarf2_read ()
25347 {
25348 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
25349 Set DWARF specific variables.\n\
25350 Configure DWARF variables such as the cache size."),
25351 &set_dwarf_cmdlist, "maintenance set dwarf ",
25352 0/*allow-unknown*/, &maintenance_set_cmdlist);
25353
25354 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
25355 Show DWARF specific variables.\n\
25356 Show DWARF variables such as the cache size."),
25357 &show_dwarf_cmdlist, "maintenance show dwarf ",
25358 0/*allow-unknown*/, &maintenance_show_cmdlist);
25359
25360 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25361 &dwarf_max_cache_age, _("\
25362 Set the upper bound on the age of cached DWARF compilation units."), _("\
25363 Show the upper bound on the age of cached DWARF compilation units."), _("\
25364 A higher limit means that cached compilation units will be stored\n\
25365 in memory longer, and more total memory will be used. Zero disables\n\
25366 caching, which can slow down startup."),
25367 NULL,
25368 show_dwarf_max_cache_age,
25369 &set_dwarf_cmdlist,
25370 &show_dwarf_cmdlist);
25371
25372 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25373 Set debugging of the DWARF reader."), _("\
25374 Show debugging of the DWARF reader."), _("\
25375 When enabled (non-zero), debugging messages are printed during DWARF\n\
25376 reading and symtab expansion. A value of 1 (one) provides basic\n\
25377 information. A value greater than 1 provides more verbose information."),
25378 NULL,
25379 NULL,
25380 &setdebuglist, &showdebuglist);
25381
25382 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25383 Set debugging of the DWARF DIE reader."), _("\
25384 Show debugging of the DWARF DIE reader."), _("\
25385 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25386 The value is the maximum depth to print."),
25387 NULL,
25388 NULL,
25389 &setdebuglist, &showdebuglist);
25390
25391 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25392 Set debugging of the dwarf line reader."), _("\
25393 Show debugging of the dwarf line reader."), _("\
25394 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25395 A value of 1 (one) provides basic information.\n\
25396 A value greater than 1 provides more verbose information."),
25397 NULL,
25398 NULL,
25399 &setdebuglist, &showdebuglist);
25400
25401 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25402 Set cross-checking of \"physname\" code against demangler."), _("\
25403 Show cross-checking of \"physname\" code against demangler."), _("\
25404 When enabled, GDB's internal \"physname\" code is checked against\n\
25405 the demangler."),
25406 NULL, show_check_physname,
25407 &setdebuglist, &showdebuglist);
25408
25409 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25410 no_class, &use_deprecated_index_sections, _("\
25411 Set whether to use deprecated gdb_index sections."), _("\
25412 Show whether to use deprecated gdb_index sections."), _("\
25413 When enabled, deprecated .gdb_index sections are used anyway.\n\
25414 Normally they are ignored either because of a missing feature or\n\
25415 performance issue.\n\
25416 Warning: This option must be enabled before gdb reads the file."),
25417 NULL,
25418 NULL,
25419 &setlist, &showlist);
25420
25421 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25422 &dwarf2_locexpr_funcs);
25423 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25424 &dwarf2_loclist_funcs);
25425
25426 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25427 &dwarf2_block_frame_base_locexpr_funcs);
25428 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25429 &dwarf2_block_frame_base_loclist_funcs);
25430
25431 #if GDB_SELF_TEST
25432 selftests::register_test ("dw2_expand_symtabs_matching",
25433 selftests::dw2_expand_symtabs_matching::run_test);
25434 selftests::register_test ("dwarf2_find_containing_comp_unit",
25435 selftests::find_containing_comp_unit::run_test);
25436 #endif
25437 }