Handle bit offset and bit size in base types
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2020 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/stringify.h"
44 #include "bfd.h"
45 #include "elf-bfd.h"
46 #include "symtab.h"
47 #include "gdbtypes.h"
48 #include "objfiles.h"
49 #include "dwarf2.h"
50 #include "buildsym.h"
51 #include "demangle.h"
52 #include "gdb-demangle.h"
53 #include "filenames.h" /* for DOSish file names */
54 #include "language.h"
55 #include "complaints.h"
56 #include "dwarf2/expr.h"
57 #include "dwarf2/loc.h"
58 #include "cp-support.h"
59 #include "hashtab.h"
60 #include "command.h"
61 #include "gdbcmd.h"
62 #include "block.h"
63 #include "addrmap.h"
64 #include "typeprint.h"
65 #include "psympriv.h"
66 #include "c-lang.h"
67 #include "go-lang.h"
68 #include "valprint.h"
69 #include "gdbcore.h" /* for gnutarget */
70 #include "gdb/gdb-index.h"
71 #include "gdb_bfd.h"
72 #include "f-lang.h"
73 #include "source.h"
74 #include "build-id.h"
75 #include "namespace.h"
76 #include "gdbsupport/function-view.h"
77 #include "gdbsupport/gdb_optional.h"
78 #include "gdbsupport/underlying.h"
79 #include "gdbsupport/hash_enum.h"
80 #include "filename-seen-cache.h"
81 #include "producer.h"
82 #include <fcntl.h>
83 #include <algorithm>
84 #include <unordered_map>
85 #include "gdbsupport/selftest.h"
86 #include "rust-lang.h"
87 #include "gdbsupport/pathstuff.h"
88 #include "count-one-bits.h"
89 #include "debuginfod-support.h"
90
91 /* When == 1, print basic high level tracing messages.
92 When > 1, be more verbose.
93 This is in contrast to the low level DIE reading of dwarf_die_debug. */
94 static unsigned int dwarf_read_debug = 0;
95
96 /* When non-zero, dump DIEs after they are read in. */
97 static unsigned int dwarf_die_debug = 0;
98
99 /* When non-zero, dump line number entries as they are read in. */
100 unsigned int dwarf_line_debug = 0;
101
102 /* When true, cross-check physname against demangler. */
103 static bool check_physname = false;
104
105 /* When true, do not reject deprecated .gdb_index sections. */
106 static bool use_deprecated_index_sections = false;
107
108 /* This is used to store the data that is always per objfile. */
109 static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
110
111 /* These are used to store the dwarf2_per_bfd objects.
112
113 objfiles having the same BFD, which doesn't require relocations, are going to
114 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
115
116 Other objfiles are not going to share a dwarf2_per_bfd with any other
117 objfiles, so they'll have their own version kept in the _objfile_data_key
118 version. */
119 static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
120 static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
121
122 /* The "aclass" indices for various kinds of computed DWARF symbols. */
123
124 static int dwarf2_locexpr_index;
125 static int dwarf2_loclist_index;
126 static int dwarf2_locexpr_block_index;
127 static int dwarf2_loclist_block_index;
128
129 /* Size of .debug_loclists section header for 32-bit DWARF format. */
130 #define LOCLIST_HEADER_SIZE32 12
131
132 /* Size of .debug_loclists section header for 64-bit DWARF format. */
133 #define LOCLIST_HEADER_SIZE64 20
134
135 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
136 #define RNGLIST_HEADER_SIZE32 12
137
138 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
139 #define RNGLIST_HEADER_SIZE64 20
140
141 /* An index into a (C++) symbol name component in a symbol name as
142 recorded in the mapped_index's symbol table. For each C++ symbol
143 in the symbol table, we record one entry for the start of each
144 component in the symbol in a table of name components, and then
145 sort the table, in order to be able to binary search symbol names,
146 ignoring leading namespaces, both completion and regular look up.
147 For example, for symbol "A::B::C", we'll have an entry that points
148 to "A::B::C", another that points to "B::C", and another for "C".
149 Note that function symbols in GDB index have no parameter
150 information, just the function/method names. You can convert a
151 name_component to a "const char *" using the
152 'mapped_index::symbol_name_at(offset_type)' method. */
153
154 struct name_component
155 {
156 /* Offset in the symbol name where the component starts. Stored as
157 a (32-bit) offset instead of a pointer to save memory and improve
158 locality on 64-bit architectures. */
159 offset_type name_offset;
160
161 /* The symbol's index in the symbol and constant pool tables of a
162 mapped_index. */
163 offset_type idx;
164 };
165
166 /* Base class containing bits shared by both .gdb_index and
167 .debug_name indexes. */
168
169 struct mapped_index_base
170 {
171 mapped_index_base () = default;
172 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
173
174 /* The name_component table (a sorted vector). See name_component's
175 description above. */
176 std::vector<name_component> name_components;
177
178 /* How NAME_COMPONENTS is sorted. */
179 enum case_sensitivity name_components_casing;
180
181 /* Return the number of names in the symbol table. */
182 virtual size_t symbol_name_count () const = 0;
183
184 /* Get the name of the symbol at IDX in the symbol table. */
185 virtual const char *symbol_name_at
186 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
187
188 /* Return whether the name at IDX in the symbol table should be
189 ignored. */
190 virtual bool symbol_name_slot_invalid (offset_type idx) const
191 {
192 return false;
193 }
194
195 /* Build the symbol name component sorted vector, if we haven't
196 yet. */
197 void build_name_components (dwarf2_per_objfile *per_objfile);
198
199 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
200 possible matches for LN_NO_PARAMS in the name component
201 vector. */
202 std::pair<std::vector<name_component>::const_iterator,
203 std::vector<name_component>::const_iterator>
204 find_name_components_bounds (const lookup_name_info &ln_no_params,
205 enum language lang,
206 dwarf2_per_objfile *per_objfile) const;
207
208 /* Prevent deleting/destroying via a base class pointer. */
209 protected:
210 ~mapped_index_base() = default;
211 };
212
213 /* A description of the mapped index. The file format is described in
214 a comment by the code that writes the index. */
215 struct mapped_index final : public mapped_index_base
216 {
217 /* A slot/bucket in the symbol table hash. */
218 struct symbol_table_slot
219 {
220 const offset_type name;
221 const offset_type vec;
222 };
223
224 /* Index data format version. */
225 int version = 0;
226
227 /* The address table data. */
228 gdb::array_view<const gdb_byte> address_table;
229
230 /* The symbol table, implemented as a hash table. */
231 gdb::array_view<symbol_table_slot> symbol_table;
232
233 /* A pointer to the constant pool. */
234 const char *constant_pool = nullptr;
235
236 bool symbol_name_slot_invalid (offset_type idx) const override
237 {
238 const auto &bucket = this->symbol_table[idx];
239 return bucket.name == 0 && bucket.vec == 0;
240 }
241
242 /* Convenience method to get at the name of the symbol at IDX in the
243 symbol table. */
244 const char *symbol_name_at
245 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
246 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
247
248 size_t symbol_name_count () const override
249 { return this->symbol_table.size (); }
250 };
251
252 /* A description of the mapped .debug_names.
253 Uninitialized map has CU_COUNT 0. */
254 struct mapped_debug_names final : public mapped_index_base
255 {
256 bfd_endian dwarf5_byte_order;
257 bool dwarf5_is_dwarf64;
258 bool augmentation_is_gdb;
259 uint8_t offset_size;
260 uint32_t cu_count = 0;
261 uint32_t tu_count, bucket_count, name_count;
262 const gdb_byte *cu_table_reordered, *tu_table_reordered;
263 const uint32_t *bucket_table_reordered, *hash_table_reordered;
264 const gdb_byte *name_table_string_offs_reordered;
265 const gdb_byte *name_table_entry_offs_reordered;
266 const gdb_byte *entry_pool;
267
268 struct index_val
269 {
270 ULONGEST dwarf_tag;
271 struct attr
272 {
273 /* Attribute name DW_IDX_*. */
274 ULONGEST dw_idx;
275
276 /* Attribute form DW_FORM_*. */
277 ULONGEST form;
278
279 /* Value if FORM is DW_FORM_implicit_const. */
280 LONGEST implicit_const;
281 };
282 std::vector<attr> attr_vec;
283 };
284
285 std::unordered_map<ULONGEST, index_val> abbrev_map;
286
287 const char *namei_to_name
288 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
289
290 /* Implementation of the mapped_index_base virtual interface, for
291 the name_components cache. */
292
293 const char *symbol_name_at
294 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
295 { return namei_to_name (idx, per_objfile); }
296
297 size_t symbol_name_count () const override
298 { return this->name_count; }
299 };
300
301 /* See dwarf2read.h. */
302
303 dwarf2_per_objfile *
304 get_dwarf2_per_objfile (struct objfile *objfile)
305 {
306 return dwarf2_objfile_data_key.get (objfile);
307 }
308
309 /* Default names of the debugging sections. */
310
311 /* Note that if the debugging section has been compressed, it might
312 have a name like .zdebug_info. */
313
314 static const struct dwarf2_debug_sections dwarf2_elf_names =
315 {
316 { ".debug_info", ".zdebug_info" },
317 { ".debug_abbrev", ".zdebug_abbrev" },
318 { ".debug_line", ".zdebug_line" },
319 { ".debug_loc", ".zdebug_loc" },
320 { ".debug_loclists", ".zdebug_loclists" },
321 { ".debug_macinfo", ".zdebug_macinfo" },
322 { ".debug_macro", ".zdebug_macro" },
323 { ".debug_str", ".zdebug_str" },
324 { ".debug_str_offsets", ".zdebug_str_offsets" },
325 { ".debug_line_str", ".zdebug_line_str" },
326 { ".debug_ranges", ".zdebug_ranges" },
327 { ".debug_rnglists", ".zdebug_rnglists" },
328 { ".debug_types", ".zdebug_types" },
329 { ".debug_addr", ".zdebug_addr" },
330 { ".debug_frame", ".zdebug_frame" },
331 { ".eh_frame", NULL },
332 { ".gdb_index", ".zgdb_index" },
333 { ".debug_names", ".zdebug_names" },
334 { ".debug_aranges", ".zdebug_aranges" },
335 23
336 };
337
338 /* List of DWO/DWP sections. */
339
340 static const struct dwop_section_names
341 {
342 struct dwarf2_section_names abbrev_dwo;
343 struct dwarf2_section_names info_dwo;
344 struct dwarf2_section_names line_dwo;
345 struct dwarf2_section_names loc_dwo;
346 struct dwarf2_section_names loclists_dwo;
347 struct dwarf2_section_names macinfo_dwo;
348 struct dwarf2_section_names macro_dwo;
349 struct dwarf2_section_names rnglists_dwo;
350 struct dwarf2_section_names str_dwo;
351 struct dwarf2_section_names str_offsets_dwo;
352 struct dwarf2_section_names types_dwo;
353 struct dwarf2_section_names cu_index;
354 struct dwarf2_section_names tu_index;
355 }
356 dwop_section_names =
357 {
358 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
359 { ".debug_info.dwo", ".zdebug_info.dwo" },
360 { ".debug_line.dwo", ".zdebug_line.dwo" },
361 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
362 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
363 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
364 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
365 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
366 { ".debug_str.dwo", ".zdebug_str.dwo" },
367 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
368 { ".debug_types.dwo", ".zdebug_types.dwo" },
369 { ".debug_cu_index", ".zdebug_cu_index" },
370 { ".debug_tu_index", ".zdebug_tu_index" },
371 };
372
373 /* local data types */
374
375 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
376 begin with a header, which contains the following information. */
377 struct loclists_rnglists_header
378 {
379 /* A 4-byte or 12-byte length containing the length of the
380 set of entries for this compilation unit, not including the
381 length field itself. */
382 unsigned int length;
383
384 /* A 2-byte version identifier. */
385 short version;
386
387 /* A 1-byte unsigned integer containing the size in bytes of an address on
388 the target system. */
389 unsigned char addr_size;
390
391 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
392 on the target system. */
393 unsigned char segment_collector_size;
394
395 /* A 4-byte count of the number of offsets that follow the header. */
396 unsigned int offset_entry_count;
397 };
398
399 /* Type used for delaying computation of method physnames.
400 See comments for compute_delayed_physnames. */
401 struct delayed_method_info
402 {
403 /* The type to which the method is attached, i.e., its parent class. */
404 struct type *type;
405
406 /* The index of the method in the type's function fieldlists. */
407 int fnfield_index;
408
409 /* The index of the method in the fieldlist. */
410 int index;
411
412 /* The name of the DIE. */
413 const char *name;
414
415 /* The DIE associated with this method. */
416 struct die_info *die;
417 };
418
419 /* Internal state when decoding a particular compilation unit. */
420 struct dwarf2_cu
421 {
422 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
423 dwarf2_per_objfile *per_objfile);
424
425 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
426
427 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
428 Create the set of symtabs used by this TU, or if this TU is sharing
429 symtabs with another TU and the symtabs have already been created
430 then restore those symtabs in the line header.
431 We don't need the pc/line-number mapping for type units. */
432 void setup_type_unit_groups (struct die_info *die);
433
434 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
435 buildsym_compunit constructor. */
436 struct compunit_symtab *start_symtab (const char *name,
437 const char *comp_dir,
438 CORE_ADDR low_pc);
439
440 /* Reset the builder. */
441 void reset_builder () { m_builder.reset (); }
442
443 /* Return a type that is a generic pointer type, the size of which
444 matches the address size given in the compilation unit header for
445 this CU. */
446 struct type *addr_type () const;
447
448 /* Find an integer type the same size as the address size given in
449 the compilation unit header for this CU. UNSIGNED_P controls if
450 the integer is unsigned or not. */
451 struct type *addr_sized_int_type (bool unsigned_p) const;
452
453 /* The header of the compilation unit. */
454 struct comp_unit_head header {};
455
456 /* Base address of this compilation unit. */
457 gdb::optional<CORE_ADDR> base_address;
458
459 /* The language we are debugging. */
460 enum language language = language_unknown;
461 const struct language_defn *language_defn = nullptr;
462
463 const char *producer = nullptr;
464
465 private:
466 /* The symtab builder for this CU. This is only non-NULL when full
467 symbols are being read. */
468 std::unique_ptr<buildsym_compunit> m_builder;
469
470 public:
471 /* The generic symbol table building routines have separate lists for
472 file scope symbols and all all other scopes (local scopes). So
473 we need to select the right one to pass to add_symbol_to_list().
474 We do it by keeping a pointer to the correct list in list_in_scope.
475
476 FIXME: The original dwarf code just treated the file scope as the
477 first local scope, and all other local scopes as nested local
478 scopes, and worked fine. Check to see if we really need to
479 distinguish these in buildsym.c. */
480 struct pending **list_in_scope = nullptr;
481
482 /* Hash table holding all the loaded partial DIEs
483 with partial_die->offset.SECT_OFF as hash. */
484 htab_t partial_dies = nullptr;
485
486 /* Storage for things with the same lifetime as this read-in compilation
487 unit, including partial DIEs. */
488 auto_obstack comp_unit_obstack;
489
490 /* Backlink to our per_cu entry. */
491 struct dwarf2_per_cu_data *per_cu;
492
493 /* The dwarf2_per_objfile that owns this. */
494 dwarf2_per_objfile *per_objfile;
495
496 /* How many compilation units ago was this CU last referenced? */
497 int last_used = 0;
498
499 /* A hash table of DIE cu_offset for following references with
500 die_info->offset.sect_off as hash. */
501 htab_t die_hash = nullptr;
502
503 /* Full DIEs if read in. */
504 struct die_info *dies = nullptr;
505
506 /* A set of pointers to dwarf2_per_cu_data objects for compilation
507 units referenced by this one. Only set during full symbol processing;
508 partial symbol tables do not have dependencies. */
509 htab_t dependencies = nullptr;
510
511 /* Header data from the line table, during full symbol processing. */
512 struct line_header *line_header = nullptr;
513 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
514 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
515 this is the DW_TAG_compile_unit die for this CU. We'll hold on
516 to the line header as long as this DIE is being processed. See
517 process_die_scope. */
518 die_info *line_header_die_owner = nullptr;
519
520 /* A list of methods which need to have physnames computed
521 after all type information has been read. */
522 std::vector<delayed_method_info> method_list;
523
524 /* To be copied to symtab->call_site_htab. */
525 htab_t call_site_htab = nullptr;
526
527 /* Non-NULL if this CU came from a DWO file.
528 There is an invariant here that is important to remember:
529 Except for attributes copied from the top level DIE in the "main"
530 (or "stub") file in preparation for reading the DWO file
531 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
532 Either there isn't a DWO file (in which case this is NULL and the point
533 is moot), or there is and either we're not going to read it (in which
534 case this is NULL) or there is and we are reading it (in which case this
535 is non-NULL). */
536 struct dwo_unit *dwo_unit = nullptr;
537
538 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
539 Note this value comes from the Fission stub CU/TU's DIE. */
540 gdb::optional<ULONGEST> addr_base;
541
542 /* The DW_AT_rnglists_base attribute if present.
543 Note this value comes from the Fission stub CU/TU's DIE.
544 Also note that the value is zero in the non-DWO case so this value can
545 be used without needing to know whether DWO files are in use or not.
546 N.B. This does not apply to DW_AT_ranges appearing in
547 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
548 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
549 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
550 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
551 ULONGEST ranges_base = 0;
552
553 /* The DW_AT_loclists_base attribute if present. */
554 ULONGEST loclist_base = 0;
555
556 /* When reading debug info generated by older versions of rustc, we
557 have to rewrite some union types to be struct types with a
558 variant part. This rewriting must be done after the CU is fully
559 read in, because otherwise at the point of rewriting some struct
560 type might not have been fully processed. So, we keep a list of
561 all such types here and process them after expansion. */
562 std::vector<struct type *> rust_unions;
563
564 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
565 files, the value is implicitly zero. For DWARF 5 version DWO files, the
566 value is often implicit and is the size of the header of
567 .debug_str_offsets section (8 or 4, depending on the address size). */
568 gdb::optional<ULONGEST> str_offsets_base;
569
570 /* Mark used when releasing cached dies. */
571 bool mark : 1;
572
573 /* This CU references .debug_loc. See the symtab->locations_valid field.
574 This test is imperfect as there may exist optimized debug code not using
575 any location list and still facing inlining issues if handled as
576 unoptimized code. For a future better test see GCC PR other/32998. */
577 bool has_loclist : 1;
578
579 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
580 if all the producer_is_* fields are valid. This information is cached
581 because profiling CU expansion showed excessive time spent in
582 producer_is_gxx_lt_4_6. */
583 bool checked_producer : 1;
584 bool producer_is_gxx_lt_4_6 : 1;
585 bool producer_is_gcc_lt_4_3 : 1;
586 bool producer_is_icc : 1;
587 bool producer_is_icc_lt_14 : 1;
588 bool producer_is_codewarrior : 1;
589
590 /* When true, the file that we're processing is known to have
591 debugging info for C++ namespaces. GCC 3.3.x did not produce
592 this information, but later versions do. */
593
594 bool processing_has_namespace_info : 1;
595
596 struct partial_die_info *find_partial_die (sect_offset sect_off);
597
598 /* If this CU was inherited by another CU (via specification,
599 abstract_origin, etc), this is the ancestor CU. */
600 dwarf2_cu *ancestor;
601
602 /* Get the buildsym_compunit for this CU. */
603 buildsym_compunit *get_builder ()
604 {
605 /* If this CU has a builder associated with it, use that. */
606 if (m_builder != nullptr)
607 return m_builder.get ();
608
609 /* Otherwise, search ancestors for a valid builder. */
610 if (ancestor != nullptr)
611 return ancestor->get_builder ();
612
613 return nullptr;
614 }
615 };
616
617 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
618 This includes type_unit_group and quick_file_names. */
619
620 struct stmt_list_hash
621 {
622 /* The DWO unit this table is from or NULL if there is none. */
623 struct dwo_unit *dwo_unit;
624
625 /* Offset in .debug_line or .debug_line.dwo. */
626 sect_offset line_sect_off;
627 };
628
629 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
630 an object of this type. This contains elements of type unit groups
631 that can be shared across objfiles. The non-shareable parts are in
632 type_unit_group_unshareable. */
633
634 struct type_unit_group
635 {
636 /* dwarf2read.c's main "handle" on a TU symtab.
637 To simplify things we create an artificial CU that "includes" all the
638 type units using this stmt_list so that the rest of the code still has
639 a "per_cu" handle on the symtab. */
640 struct dwarf2_per_cu_data per_cu;
641
642 /* The TUs that share this DW_AT_stmt_list entry.
643 This is added to while parsing type units to build partial symtabs,
644 and is deleted afterwards and not used again. */
645 std::vector<signatured_type *> *tus;
646
647 /* The data used to construct the hash key. */
648 struct stmt_list_hash hash;
649 };
650
651 /* These sections are what may appear in a (real or virtual) DWO file. */
652
653 struct dwo_sections
654 {
655 struct dwarf2_section_info abbrev;
656 struct dwarf2_section_info line;
657 struct dwarf2_section_info loc;
658 struct dwarf2_section_info loclists;
659 struct dwarf2_section_info macinfo;
660 struct dwarf2_section_info macro;
661 struct dwarf2_section_info rnglists;
662 struct dwarf2_section_info str;
663 struct dwarf2_section_info str_offsets;
664 /* In the case of a virtual DWO file, these two are unused. */
665 struct dwarf2_section_info info;
666 std::vector<dwarf2_section_info> types;
667 };
668
669 /* CUs/TUs in DWP/DWO files. */
670
671 struct dwo_unit
672 {
673 /* Backlink to the containing struct dwo_file. */
674 struct dwo_file *dwo_file;
675
676 /* The "id" that distinguishes this CU/TU.
677 .debug_info calls this "dwo_id", .debug_types calls this "signature".
678 Since signatures came first, we stick with it for consistency. */
679 ULONGEST signature;
680
681 /* The section this CU/TU lives in, in the DWO file. */
682 struct dwarf2_section_info *section;
683
684 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
685 sect_offset sect_off;
686 unsigned int length;
687
688 /* For types, offset in the type's DIE of the type defined by this TU. */
689 cu_offset type_offset_in_tu;
690 };
691
692 /* include/dwarf2.h defines the DWP section codes.
693 It defines a max value but it doesn't define a min value, which we
694 use for error checking, so provide one. */
695
696 enum dwp_v2_section_ids
697 {
698 DW_SECT_MIN = 1
699 };
700
701 /* Data for one DWO file.
702
703 This includes virtual DWO files (a virtual DWO file is a DWO file as it
704 appears in a DWP file). DWP files don't really have DWO files per se -
705 comdat folding of types "loses" the DWO file they came from, and from
706 a high level view DWP files appear to contain a mass of random types.
707 However, to maintain consistency with the non-DWP case we pretend DWP
708 files contain virtual DWO files, and we assign each TU with one virtual
709 DWO file (generally based on the line and abbrev section offsets -
710 a heuristic that seems to work in practice). */
711
712 struct dwo_file
713 {
714 dwo_file () = default;
715 DISABLE_COPY_AND_ASSIGN (dwo_file);
716
717 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
718 For virtual DWO files the name is constructed from the section offsets
719 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
720 from related CU+TUs. */
721 const char *dwo_name = nullptr;
722
723 /* The DW_AT_comp_dir attribute. */
724 const char *comp_dir = nullptr;
725
726 /* The bfd, when the file is open. Otherwise this is NULL.
727 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
728 gdb_bfd_ref_ptr dbfd;
729
730 /* The sections that make up this DWO file.
731 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
732 sections (for lack of a better name). */
733 struct dwo_sections sections {};
734
735 /* The CUs in the file.
736 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
737 an extension to handle LLVM's Link Time Optimization output (where
738 multiple source files may be compiled into a single object/dwo pair). */
739 htab_up cus;
740
741 /* Table of TUs in the file.
742 Each element is a struct dwo_unit. */
743 htab_up tus;
744 };
745
746 /* These sections are what may appear in a DWP file. */
747
748 struct dwp_sections
749 {
750 /* These are used by all DWP versions (1, 2 and 5). */
751 struct dwarf2_section_info str;
752 struct dwarf2_section_info cu_index;
753 struct dwarf2_section_info tu_index;
754
755 /* These are only used by DWP version 2 and version 5 files.
756 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
757 sections are referenced by section number, and are not recorded here.
758 In DWP version 2 or 5 there is at most one copy of all these sections,
759 each section being (effectively) comprised of the concatenation of all of
760 the individual sections that exist in the version 1 format.
761 To keep the code simple we treat each of these concatenated pieces as a
762 section itself (a virtual section?). */
763 struct dwarf2_section_info abbrev;
764 struct dwarf2_section_info info;
765 struct dwarf2_section_info line;
766 struct dwarf2_section_info loc;
767 struct dwarf2_section_info loclists;
768 struct dwarf2_section_info macinfo;
769 struct dwarf2_section_info macro;
770 struct dwarf2_section_info rnglists;
771 struct dwarf2_section_info str_offsets;
772 struct dwarf2_section_info types;
773 };
774
775 /* These sections are what may appear in a virtual DWO file in DWP version 1.
776 A virtual DWO file is a DWO file as it appears in a DWP file. */
777
778 struct virtual_v1_dwo_sections
779 {
780 struct dwarf2_section_info abbrev;
781 struct dwarf2_section_info line;
782 struct dwarf2_section_info loc;
783 struct dwarf2_section_info macinfo;
784 struct dwarf2_section_info macro;
785 struct dwarf2_section_info str_offsets;
786 /* Each DWP hash table entry records one CU or one TU.
787 That is recorded here, and copied to dwo_unit.section. */
788 struct dwarf2_section_info info_or_types;
789 };
790
791 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
792 In version 2, the sections of the DWO files are concatenated together
793 and stored in one section of that name. Thus each ELF section contains
794 several "virtual" sections. */
795
796 struct virtual_v2_or_v5_dwo_sections
797 {
798 bfd_size_type abbrev_offset;
799 bfd_size_type abbrev_size;
800
801 bfd_size_type line_offset;
802 bfd_size_type line_size;
803
804 bfd_size_type loc_offset;
805 bfd_size_type loc_size;
806
807 bfd_size_type loclists_offset;
808 bfd_size_type loclists_size;
809
810 bfd_size_type macinfo_offset;
811 bfd_size_type macinfo_size;
812
813 bfd_size_type macro_offset;
814 bfd_size_type macro_size;
815
816 bfd_size_type rnglists_offset;
817 bfd_size_type rnglists_size;
818
819 bfd_size_type str_offsets_offset;
820 bfd_size_type str_offsets_size;
821
822 /* Each DWP hash table entry records one CU or one TU.
823 That is recorded here, and copied to dwo_unit.section. */
824 bfd_size_type info_or_types_offset;
825 bfd_size_type info_or_types_size;
826 };
827
828 /* Contents of DWP hash tables. */
829
830 struct dwp_hash_table
831 {
832 uint32_t version, nr_columns;
833 uint32_t nr_units, nr_slots;
834 const gdb_byte *hash_table, *unit_table;
835 union
836 {
837 struct
838 {
839 const gdb_byte *indices;
840 } v1;
841 struct
842 {
843 /* This is indexed by column number and gives the id of the section
844 in that column. */
845 #define MAX_NR_V2_DWO_SECTIONS \
846 (1 /* .debug_info or .debug_types */ \
847 + 1 /* .debug_abbrev */ \
848 + 1 /* .debug_line */ \
849 + 1 /* .debug_loc */ \
850 + 1 /* .debug_str_offsets */ \
851 + 1 /* .debug_macro or .debug_macinfo */)
852 int section_ids[MAX_NR_V2_DWO_SECTIONS];
853 const gdb_byte *offsets;
854 const gdb_byte *sizes;
855 } v2;
856 struct
857 {
858 /* This is indexed by column number and gives the id of the section
859 in that column. */
860 #define MAX_NR_V5_DWO_SECTIONS \
861 (1 /* .debug_info */ \
862 + 1 /* .debug_abbrev */ \
863 + 1 /* .debug_line */ \
864 + 1 /* .debug_loclists */ \
865 + 1 /* .debug_str_offsets */ \
866 + 1 /* .debug_macro */ \
867 + 1 /* .debug_rnglists */)
868 int section_ids[MAX_NR_V5_DWO_SECTIONS];
869 const gdb_byte *offsets;
870 const gdb_byte *sizes;
871 } v5;
872 } section_pool;
873 };
874
875 /* Data for one DWP file. */
876
877 struct dwp_file
878 {
879 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
880 : name (name_),
881 dbfd (std::move (abfd))
882 {
883 }
884
885 /* Name of the file. */
886 const char *name;
887
888 /* File format version. */
889 int version = 0;
890
891 /* The bfd. */
892 gdb_bfd_ref_ptr dbfd;
893
894 /* Section info for this file. */
895 struct dwp_sections sections {};
896
897 /* Table of CUs in the file. */
898 const struct dwp_hash_table *cus = nullptr;
899
900 /* Table of TUs in the file. */
901 const struct dwp_hash_table *tus = nullptr;
902
903 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
904 htab_up loaded_cus;
905 htab_up loaded_tus;
906
907 /* Table to map ELF section numbers to their sections.
908 This is only needed for the DWP V1 file format. */
909 unsigned int num_sections = 0;
910 asection **elf_sections = nullptr;
911 };
912
913 /* Struct used to pass misc. parameters to read_die_and_children, et
914 al. which are used for both .debug_info and .debug_types dies.
915 All parameters here are unchanging for the life of the call. This
916 struct exists to abstract away the constant parameters of die reading. */
917
918 struct die_reader_specs
919 {
920 /* The bfd of die_section. */
921 bfd* abfd;
922
923 /* The CU of the DIE we are parsing. */
924 struct dwarf2_cu *cu;
925
926 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
927 struct dwo_file *dwo_file;
928
929 /* The section the die comes from.
930 This is either .debug_info or .debug_types, or the .dwo variants. */
931 struct dwarf2_section_info *die_section;
932
933 /* die_section->buffer. */
934 const gdb_byte *buffer;
935
936 /* The end of the buffer. */
937 const gdb_byte *buffer_end;
938
939 /* The abbreviation table to use when reading the DIEs. */
940 struct abbrev_table *abbrev_table;
941 };
942
943 /* A subclass of die_reader_specs that holds storage and has complex
944 constructor and destructor behavior. */
945
946 class cutu_reader : public die_reader_specs
947 {
948 public:
949
950 cutu_reader (dwarf2_per_cu_data *this_cu,
951 dwarf2_per_objfile *per_objfile,
952 struct abbrev_table *abbrev_table,
953 dwarf2_cu *existing_cu,
954 bool skip_partial);
955
956 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
957 dwarf2_per_objfile *per_objfile,
958 struct dwarf2_cu *parent_cu = nullptr,
959 struct dwo_file *dwo_file = nullptr);
960
961 DISABLE_COPY_AND_ASSIGN (cutu_reader);
962
963 const gdb_byte *info_ptr = nullptr;
964 struct die_info *comp_unit_die = nullptr;
965 bool dummy_p = false;
966
967 /* Release the new CU, putting it on the chain. This cannot be done
968 for dummy CUs. */
969 void keep ();
970
971 private:
972 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
973 dwarf2_per_objfile *per_objfile,
974 dwarf2_cu *existing_cu);
975
976 struct dwarf2_per_cu_data *m_this_cu;
977 std::unique_ptr<dwarf2_cu> m_new_cu;
978
979 /* The ordinary abbreviation table. */
980 abbrev_table_up m_abbrev_table_holder;
981
982 /* The DWO abbreviation table. */
983 abbrev_table_up m_dwo_abbrev_table;
984 };
985
986 /* When we construct a partial symbol table entry we only
987 need this much information. */
988 struct partial_die_info : public allocate_on_obstack
989 {
990 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
991
992 /* Disable assign but still keep copy ctor, which is needed
993 load_partial_dies. */
994 partial_die_info& operator=(const partial_die_info& rhs) = delete;
995
996 /* Adjust the partial die before generating a symbol for it. This
997 function may set the is_external flag or change the DIE's
998 name. */
999 void fixup (struct dwarf2_cu *cu);
1000
1001 /* Read a minimal amount of information into the minimal die
1002 structure. */
1003 const gdb_byte *read (const struct die_reader_specs *reader,
1004 const struct abbrev_info &abbrev,
1005 const gdb_byte *info_ptr);
1006
1007 /* Compute the name of this partial DIE. This memoizes the
1008 result, so it is safe to call multiple times. */
1009 const char *name (dwarf2_cu *cu);
1010
1011 /* Offset of this DIE. */
1012 const sect_offset sect_off;
1013
1014 /* DWARF-2 tag for this DIE. */
1015 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1016
1017 /* Assorted flags describing the data found in this DIE. */
1018 const unsigned int has_children : 1;
1019
1020 unsigned int is_external : 1;
1021 unsigned int is_declaration : 1;
1022 unsigned int has_type : 1;
1023 unsigned int has_specification : 1;
1024 unsigned int has_pc_info : 1;
1025 unsigned int may_be_inlined : 1;
1026
1027 /* This DIE has been marked DW_AT_main_subprogram. */
1028 unsigned int main_subprogram : 1;
1029
1030 /* Flag set if the SCOPE field of this structure has been
1031 computed. */
1032 unsigned int scope_set : 1;
1033
1034 /* Flag set if the DIE has a byte_size attribute. */
1035 unsigned int has_byte_size : 1;
1036
1037 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1038 unsigned int has_const_value : 1;
1039
1040 /* Flag set if any of the DIE's children are template arguments. */
1041 unsigned int has_template_arguments : 1;
1042
1043 /* Flag set if fixup has been called on this die. */
1044 unsigned int fixup_called : 1;
1045
1046 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1047 unsigned int is_dwz : 1;
1048
1049 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1050 unsigned int spec_is_dwz : 1;
1051
1052 unsigned int canonical_name : 1;
1053
1054 /* The name of this DIE. Normally the value of DW_AT_name, but
1055 sometimes a default name for unnamed DIEs. */
1056 const char *raw_name = nullptr;
1057
1058 /* The linkage name, if present. */
1059 const char *linkage_name = nullptr;
1060
1061 /* The scope to prepend to our children. This is generally
1062 allocated on the comp_unit_obstack, so will disappear
1063 when this compilation unit leaves the cache. */
1064 const char *scope = nullptr;
1065
1066 /* Some data associated with the partial DIE. The tag determines
1067 which field is live. */
1068 union
1069 {
1070 /* The location description associated with this DIE, if any. */
1071 struct dwarf_block *locdesc;
1072 /* The offset of an import, for DW_TAG_imported_unit. */
1073 sect_offset sect_off;
1074 } d {};
1075
1076 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1077 CORE_ADDR lowpc = 0;
1078 CORE_ADDR highpc = 0;
1079
1080 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1081 DW_AT_sibling, if any. */
1082 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1083 could return DW_AT_sibling values to its caller load_partial_dies. */
1084 const gdb_byte *sibling = nullptr;
1085
1086 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1087 DW_AT_specification (or DW_AT_abstract_origin or
1088 DW_AT_extension). */
1089 sect_offset spec_offset {};
1090
1091 /* Pointers to this DIE's parent, first child, and next sibling,
1092 if any. */
1093 struct partial_die_info *die_parent = nullptr;
1094 struct partial_die_info *die_child = nullptr;
1095 struct partial_die_info *die_sibling = nullptr;
1096
1097 friend struct partial_die_info *
1098 dwarf2_cu::find_partial_die (sect_offset sect_off);
1099
1100 private:
1101 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1102 partial_die_info (sect_offset sect_off)
1103 : partial_die_info (sect_off, DW_TAG_padding, 0)
1104 {
1105 }
1106
1107 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1108 int has_children_)
1109 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1110 {
1111 is_external = 0;
1112 is_declaration = 0;
1113 has_type = 0;
1114 has_specification = 0;
1115 has_pc_info = 0;
1116 may_be_inlined = 0;
1117 main_subprogram = 0;
1118 scope_set = 0;
1119 has_byte_size = 0;
1120 has_const_value = 0;
1121 has_template_arguments = 0;
1122 fixup_called = 0;
1123 is_dwz = 0;
1124 spec_is_dwz = 0;
1125 canonical_name = 0;
1126 }
1127 };
1128
1129 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1130 but this would require a corresponding change in unpack_field_as_long
1131 and friends. */
1132 static int bits_per_byte = 8;
1133
1134 struct variant_part_builder;
1135
1136 /* When reading a variant, we track a bit more information about the
1137 field, and store it in an object of this type. */
1138
1139 struct variant_field
1140 {
1141 int first_field = -1;
1142 int last_field = -1;
1143
1144 /* A variant can contain other variant parts. */
1145 std::vector<variant_part_builder> variant_parts;
1146
1147 /* If we see a DW_TAG_variant, then this will be set if this is the
1148 default branch. */
1149 bool default_branch = false;
1150 /* If we see a DW_AT_discr_value, then this will be the discriminant
1151 value. */
1152 ULONGEST discriminant_value = 0;
1153 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1154 data. */
1155 struct dwarf_block *discr_list_data = nullptr;
1156 };
1157
1158 /* This represents a DW_TAG_variant_part. */
1159
1160 struct variant_part_builder
1161 {
1162 /* The offset of the discriminant field. */
1163 sect_offset discriminant_offset {};
1164
1165 /* Variants that are direct children of this variant part. */
1166 std::vector<variant_field> variants;
1167
1168 /* True if we're currently reading a variant. */
1169 bool processing_variant = false;
1170 };
1171
1172 struct nextfield
1173 {
1174 int accessibility = 0;
1175 int virtuality = 0;
1176 /* Variant parts need to find the discriminant, which is a DIE
1177 reference. We track the section offset of each field to make
1178 this link. */
1179 sect_offset offset;
1180 struct field field {};
1181 };
1182
1183 struct fnfieldlist
1184 {
1185 const char *name = nullptr;
1186 std::vector<struct fn_field> fnfields;
1187 };
1188
1189 /* The routines that read and process dies for a C struct or C++ class
1190 pass lists of data member fields and lists of member function fields
1191 in an instance of a field_info structure, as defined below. */
1192 struct field_info
1193 {
1194 /* List of data member and baseclasses fields. */
1195 std::vector<struct nextfield> fields;
1196 std::vector<struct nextfield> baseclasses;
1197
1198 /* Set if the accessibility of one of the fields is not public. */
1199 bool non_public_fields = false;
1200
1201 /* Member function fieldlist array, contains name of possibly overloaded
1202 member function, number of overloaded member functions and a pointer
1203 to the head of the member function field chain. */
1204 std::vector<struct fnfieldlist> fnfieldlists;
1205
1206 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1207 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1208 std::vector<struct decl_field> typedef_field_list;
1209
1210 /* Nested types defined by this class and the number of elements in this
1211 list. */
1212 std::vector<struct decl_field> nested_types_list;
1213
1214 /* If non-null, this is the variant part we are currently
1215 reading. */
1216 variant_part_builder *current_variant_part = nullptr;
1217 /* This holds all the top-level variant parts attached to the type
1218 we're reading. */
1219 std::vector<variant_part_builder> variant_parts;
1220
1221 /* Return the total number of fields (including baseclasses). */
1222 int nfields () const
1223 {
1224 return fields.size () + baseclasses.size ();
1225 }
1226 };
1227
1228 /* Loaded secondary compilation units are kept in memory until they
1229 have not been referenced for the processing of this many
1230 compilation units. Set this to zero to disable caching. Cache
1231 sizes of up to at least twenty will improve startup time for
1232 typical inter-CU-reference binaries, at an obvious memory cost. */
1233 static int dwarf_max_cache_age = 5;
1234 static void
1235 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1236 struct cmd_list_element *c, const char *value)
1237 {
1238 fprintf_filtered (file, _("The upper bound on the age of cached "
1239 "DWARF compilation units is %s.\n"),
1240 value);
1241 }
1242 \f
1243 /* local function prototypes */
1244
1245 static void dwarf2_find_base_address (struct die_info *die,
1246 struct dwarf2_cu *cu);
1247
1248 static dwarf2_psymtab *create_partial_symtab
1249 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1250 const char *name);
1251
1252 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1253 const gdb_byte *info_ptr,
1254 struct die_info *type_unit_die);
1255
1256 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
1257
1258 static void scan_partial_symbols (struct partial_die_info *,
1259 CORE_ADDR *, CORE_ADDR *,
1260 int, struct dwarf2_cu *);
1261
1262 static void add_partial_symbol (struct partial_die_info *,
1263 struct dwarf2_cu *);
1264
1265 static void add_partial_namespace (struct partial_die_info *pdi,
1266 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1267 int set_addrmap, struct dwarf2_cu *cu);
1268
1269 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1270 CORE_ADDR *highpc, int set_addrmap,
1271 struct dwarf2_cu *cu);
1272
1273 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1274 struct dwarf2_cu *cu);
1275
1276 static void add_partial_subprogram (struct partial_die_info *pdi,
1277 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1278 int need_pc, struct dwarf2_cu *cu);
1279
1280 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1281
1282 static struct partial_die_info *load_partial_dies
1283 (const struct die_reader_specs *, const gdb_byte *, int);
1284
1285 /* A pair of partial_die_info and compilation unit. */
1286 struct cu_partial_die_info
1287 {
1288 /* The compilation unit of the partial_die_info. */
1289 struct dwarf2_cu *cu;
1290 /* A partial_die_info. */
1291 struct partial_die_info *pdi;
1292
1293 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1294 : cu (cu),
1295 pdi (pdi)
1296 { /* Nothing. */ }
1297
1298 private:
1299 cu_partial_die_info () = delete;
1300 };
1301
1302 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1303 struct dwarf2_cu *);
1304
1305 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1306 struct attribute *, struct attr_abbrev *,
1307 const gdb_byte *, bool *need_reprocess);
1308
1309 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1310 struct attribute *attr, dwarf_tag tag);
1311
1312 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1313
1314 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1315 dwarf2_section_info *, sect_offset);
1316
1317 static const char *read_indirect_string
1318 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
1319 const struct comp_unit_head *, unsigned int *);
1320
1321 static const char *read_indirect_string_at_offset
1322 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
1323
1324 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1325 const gdb_byte *,
1326 unsigned int *);
1327
1328 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1329 ULONGEST str_index);
1330
1331 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1332 ULONGEST str_index);
1333
1334 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1335
1336 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1337 struct dwarf2_cu *);
1338
1339 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1340 struct dwarf2_cu *cu);
1341
1342 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1343
1344 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1345 struct dwarf2_cu *cu);
1346
1347 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1348
1349 static struct die_info *die_specification (struct die_info *die,
1350 struct dwarf2_cu **);
1351
1352 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1353 struct dwarf2_cu *cu);
1354
1355 static void dwarf_decode_lines (struct line_header *, const char *,
1356 struct dwarf2_cu *, dwarf2_psymtab *,
1357 CORE_ADDR, int decode_mapping);
1358
1359 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1360 const char *);
1361
1362 static struct symbol *new_symbol (struct die_info *, struct type *,
1363 struct dwarf2_cu *, struct symbol * = NULL);
1364
1365 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1366 struct dwarf2_cu *);
1367
1368 static void dwarf2_const_value_attr (const struct attribute *attr,
1369 struct type *type,
1370 const char *name,
1371 struct obstack *obstack,
1372 struct dwarf2_cu *cu, LONGEST *value,
1373 const gdb_byte **bytes,
1374 struct dwarf2_locexpr_baton **baton);
1375
1376 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1377
1378 static int need_gnat_info (struct dwarf2_cu *);
1379
1380 static struct type *die_descriptive_type (struct die_info *,
1381 struct dwarf2_cu *);
1382
1383 static void set_descriptive_type (struct type *, struct die_info *,
1384 struct dwarf2_cu *);
1385
1386 static struct type *die_containing_type (struct die_info *,
1387 struct dwarf2_cu *);
1388
1389 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1390 struct dwarf2_cu *);
1391
1392 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1393
1394 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1395
1396 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1397
1398 static char *typename_concat (struct obstack *obs, const char *prefix,
1399 const char *suffix, int physname,
1400 struct dwarf2_cu *cu);
1401
1402 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1403
1404 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1405
1406 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1407
1408 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1409
1410 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1411
1412 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1413
1414 /* Return the .debug_loclists section to use for cu. */
1415 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1416
1417 /* Return the .debug_rnglists section to use for cu. */
1418 static struct dwarf2_section_info *cu_debug_rnglists_section
1419 (struct dwarf2_cu *cu, dwarf_tag tag);
1420
1421 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1422 values. Keep the items ordered with increasing constraints compliance. */
1423 enum pc_bounds_kind
1424 {
1425 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1426 PC_BOUNDS_NOT_PRESENT,
1427
1428 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1429 were present but they do not form a valid range of PC addresses. */
1430 PC_BOUNDS_INVALID,
1431
1432 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1433 PC_BOUNDS_RANGES,
1434
1435 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1436 PC_BOUNDS_HIGH_LOW,
1437 };
1438
1439 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1440 CORE_ADDR *, CORE_ADDR *,
1441 struct dwarf2_cu *,
1442 dwarf2_psymtab *);
1443
1444 static void get_scope_pc_bounds (struct die_info *,
1445 CORE_ADDR *, CORE_ADDR *,
1446 struct dwarf2_cu *);
1447
1448 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1449 CORE_ADDR, struct dwarf2_cu *);
1450
1451 static void dwarf2_add_field (struct field_info *, struct die_info *,
1452 struct dwarf2_cu *);
1453
1454 static void dwarf2_attach_fields_to_type (struct field_info *,
1455 struct type *, struct dwarf2_cu *);
1456
1457 static void dwarf2_add_member_fn (struct field_info *,
1458 struct die_info *, struct type *,
1459 struct dwarf2_cu *);
1460
1461 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1462 struct type *,
1463 struct dwarf2_cu *);
1464
1465 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1466
1467 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1468
1469 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1470
1471 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1472
1473 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1474
1475 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1476
1477 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1478
1479 static struct type *read_module_type (struct die_info *die,
1480 struct dwarf2_cu *cu);
1481
1482 static const char *namespace_name (struct die_info *die,
1483 int *is_anonymous, struct dwarf2_cu *);
1484
1485 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1486
1487 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1488 bool * = nullptr);
1489
1490 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1491 struct dwarf2_cu *);
1492
1493 static struct die_info *read_die_and_siblings_1
1494 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1495 struct die_info *);
1496
1497 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1498 const gdb_byte *info_ptr,
1499 const gdb_byte **new_info_ptr,
1500 struct die_info *parent);
1501
1502 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1503 struct die_info **, const gdb_byte *,
1504 int);
1505
1506 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1507 struct die_info **, const gdb_byte *);
1508
1509 static void process_die (struct die_info *, struct dwarf2_cu *);
1510
1511 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1512 struct objfile *);
1513
1514 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1515
1516 static const char *dwarf2_full_name (const char *name,
1517 struct die_info *die,
1518 struct dwarf2_cu *cu);
1519
1520 static const char *dwarf2_physname (const char *name, struct die_info *die,
1521 struct dwarf2_cu *cu);
1522
1523 static struct die_info *dwarf2_extension (struct die_info *die,
1524 struct dwarf2_cu **);
1525
1526 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1527
1528 static void dump_die_for_error (struct die_info *);
1529
1530 static void dump_die_1 (struct ui_file *, int level, int max_level,
1531 struct die_info *);
1532
1533 /*static*/ void dump_die (struct die_info *, int max_level);
1534
1535 static void store_in_ref_table (struct die_info *,
1536 struct dwarf2_cu *);
1537
1538 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1539 const struct attribute *,
1540 struct dwarf2_cu **);
1541
1542 static struct die_info *follow_die_ref (struct die_info *,
1543 const struct attribute *,
1544 struct dwarf2_cu **);
1545
1546 static struct die_info *follow_die_sig (struct die_info *,
1547 const struct attribute *,
1548 struct dwarf2_cu **);
1549
1550 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1551 struct dwarf2_cu *);
1552
1553 static struct type *get_DW_AT_signature_type (struct die_info *,
1554 const struct attribute *,
1555 struct dwarf2_cu *);
1556
1557 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1558 dwarf2_per_objfile *per_objfile);
1559
1560 static void read_signatured_type (signatured_type *sig_type,
1561 dwarf2_per_objfile *per_objfile);
1562
1563 static int attr_to_dynamic_prop (const struct attribute *attr,
1564 struct die_info *die, struct dwarf2_cu *cu,
1565 struct dynamic_prop *prop, struct type *type);
1566
1567 /* memory allocation interface */
1568
1569 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1570
1571 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1572
1573 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1574
1575 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1576 struct dwarf2_loclist_baton *baton,
1577 const struct attribute *attr);
1578
1579 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1580 struct symbol *sym,
1581 struct dwarf2_cu *cu,
1582 int is_block);
1583
1584 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1585 const gdb_byte *info_ptr,
1586 struct abbrev_info *abbrev);
1587
1588 static hashval_t partial_die_hash (const void *item);
1589
1590 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1591
1592 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1593 (sect_offset sect_off, unsigned int offset_in_dwz,
1594 dwarf2_per_objfile *per_objfile);
1595
1596 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1597 struct die_info *comp_unit_die,
1598 enum language pretend_language);
1599
1600 static struct type *set_die_type (struct die_info *, struct type *,
1601 struct dwarf2_cu *);
1602
1603 static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
1604
1605 static int create_all_type_units (dwarf2_per_objfile *per_objfile);
1606
1607 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1608 dwarf2_per_objfile *per_objfile,
1609 bool skip_partial,
1610 enum language pretend_language);
1611
1612 static void process_full_comp_unit (dwarf2_cu *cu,
1613 enum language pretend_language);
1614
1615 static void process_full_type_unit (dwarf2_cu *cu,
1616 enum language pretend_language);
1617
1618 static void dwarf2_add_dependence (struct dwarf2_cu *,
1619 struct dwarf2_per_cu_data *);
1620
1621 static void dwarf2_mark (struct dwarf2_cu *);
1622
1623 static struct type *get_die_type_at_offset (sect_offset,
1624 dwarf2_per_cu_data *per_cu,
1625 dwarf2_per_objfile *per_objfile);
1626
1627 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1628
1629 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1630 dwarf2_per_objfile *per_objfile,
1631 enum language pretend_language);
1632
1633 static void process_queue (dwarf2_per_objfile *per_objfile);
1634
1635 /* Class, the destructor of which frees all allocated queue entries. This
1636 will only have work to do if an error was thrown while processing the
1637 dwarf. If no error was thrown then the queue entries should have all
1638 been processed, and freed, as we went along. */
1639
1640 class dwarf2_queue_guard
1641 {
1642 public:
1643 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1644 : m_per_objfile (per_objfile)
1645 {
1646 }
1647
1648 /* Free any entries remaining on the queue. There should only be
1649 entries left if we hit an error while processing the dwarf. */
1650 ~dwarf2_queue_guard ()
1651 {
1652 /* Ensure that no memory is allocated by the queue. */
1653 std::queue<dwarf2_queue_item> empty;
1654 std::swap (m_per_objfile->per_bfd->queue, empty);
1655 }
1656
1657 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1658
1659 private:
1660 dwarf2_per_objfile *m_per_objfile;
1661 };
1662
1663 dwarf2_queue_item::~dwarf2_queue_item ()
1664 {
1665 /* Anything still marked queued is likely to be in an
1666 inconsistent state, so discard it. */
1667 if (per_cu->queued)
1668 {
1669 per_objfile->remove_cu (per_cu);
1670 per_cu->queued = 0;
1671 }
1672 }
1673
1674 /* The return type of find_file_and_directory. Note, the enclosed
1675 string pointers are only valid while this object is valid. */
1676
1677 struct file_and_directory
1678 {
1679 /* The filename. This is never NULL. */
1680 const char *name;
1681
1682 /* The compilation directory. NULL if not known. If we needed to
1683 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1684 points directly to the DW_AT_comp_dir string attribute owned by
1685 the obstack that owns the DIE. */
1686 const char *comp_dir;
1687
1688 /* If we needed to build a new string for comp_dir, this is what
1689 owns the storage. */
1690 std::string comp_dir_storage;
1691 };
1692
1693 static file_and_directory find_file_and_directory (struct die_info *die,
1694 struct dwarf2_cu *cu);
1695
1696 static htab_up allocate_signatured_type_table ();
1697
1698 static htab_up allocate_dwo_unit_table ();
1699
1700 static struct dwo_unit *lookup_dwo_unit_in_dwp
1701 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1702 const char *comp_dir, ULONGEST signature, int is_debug_types);
1703
1704 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1705
1706 static struct dwo_unit *lookup_dwo_comp_unit
1707 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1708 ULONGEST signature);
1709
1710 static struct dwo_unit *lookup_dwo_type_unit
1711 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1712
1713 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1714
1715 /* A unique pointer to a dwo_file. */
1716
1717 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1718
1719 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1720
1721 static void check_producer (struct dwarf2_cu *cu);
1722
1723 static void free_line_header_voidp (void *arg);
1724 \f
1725 /* Various complaints about symbol reading that don't abort the process. */
1726
1727 static void
1728 dwarf2_debug_line_missing_file_complaint (void)
1729 {
1730 complaint (_(".debug_line section has line data without a file"));
1731 }
1732
1733 static void
1734 dwarf2_debug_line_missing_end_sequence_complaint (void)
1735 {
1736 complaint (_(".debug_line section has line "
1737 "program sequence without an end"));
1738 }
1739
1740 static void
1741 dwarf2_complex_location_expr_complaint (void)
1742 {
1743 complaint (_("location expression too complex"));
1744 }
1745
1746 static void
1747 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1748 int arg3)
1749 {
1750 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1751 arg1, arg2, arg3);
1752 }
1753
1754 static void
1755 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1756 {
1757 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1758 arg1, arg2);
1759 }
1760
1761 /* Hash function for line_header_hash. */
1762
1763 static hashval_t
1764 line_header_hash (const struct line_header *ofs)
1765 {
1766 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1767 }
1768
1769 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1770
1771 static hashval_t
1772 line_header_hash_voidp (const void *item)
1773 {
1774 const struct line_header *ofs = (const struct line_header *) item;
1775
1776 return line_header_hash (ofs);
1777 }
1778
1779 /* Equality function for line_header_hash. */
1780
1781 static int
1782 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1783 {
1784 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1785 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1786
1787 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1788 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1789 }
1790
1791 \f
1792
1793 /* See declaration. */
1794
1795 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1796 bool can_copy_)
1797 : obfd (obfd),
1798 can_copy (can_copy_)
1799 {
1800 if (names == NULL)
1801 names = &dwarf2_elf_names;
1802
1803 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1804 locate_sections (obfd, sec, *names);
1805 }
1806
1807 dwarf2_per_bfd::~dwarf2_per_bfd ()
1808 {
1809 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1810 per_cu->imported_symtabs_free ();
1811
1812 for (signatured_type *sig_type : all_type_units)
1813 sig_type->per_cu.imported_symtabs_free ();
1814
1815 /* Everything else should be on this->obstack. */
1816 }
1817
1818 /* See read.h. */
1819
1820 void
1821 dwarf2_per_objfile::remove_all_cus ()
1822 {
1823 for (auto pair : m_dwarf2_cus)
1824 delete pair.second;
1825
1826 m_dwarf2_cus.clear ();
1827 }
1828
1829 /* A helper class that calls free_cached_comp_units on
1830 destruction. */
1831
1832 class free_cached_comp_units
1833 {
1834 public:
1835
1836 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1837 : m_per_objfile (per_objfile)
1838 {
1839 }
1840
1841 ~free_cached_comp_units ()
1842 {
1843 m_per_objfile->remove_all_cus ();
1844 }
1845
1846 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1847
1848 private:
1849
1850 dwarf2_per_objfile *m_per_objfile;
1851 };
1852
1853 /* See read.h. */
1854
1855 bool
1856 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1857 {
1858 gdb_assert (per_cu->index < this->m_symtabs.size ());
1859
1860 return this->m_symtabs[per_cu->index] != nullptr;
1861 }
1862
1863 /* See read.h. */
1864
1865 compunit_symtab *
1866 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1867 {
1868 gdb_assert (per_cu->index < this->m_symtabs.size ());
1869
1870 return this->m_symtabs[per_cu->index];
1871 }
1872
1873 /* See read.h. */
1874
1875 void
1876 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1877 compunit_symtab *symtab)
1878 {
1879 gdb_assert (per_cu->index < this->m_symtabs.size ());
1880 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1881
1882 this->m_symtabs[per_cu->index] = symtab;
1883 }
1884
1885 /* Try to locate the sections we need for DWARF 2 debugging
1886 information and return true if we have enough to do something.
1887 NAMES points to the dwarf2 section names, or is NULL if the standard
1888 ELF names are used. CAN_COPY is true for formats where symbol
1889 interposition is possible and so symbol values must follow copy
1890 relocation rules. */
1891
1892 int
1893 dwarf2_has_info (struct objfile *objfile,
1894 const struct dwarf2_debug_sections *names,
1895 bool can_copy)
1896 {
1897 if (objfile->flags & OBJF_READNEVER)
1898 return 0;
1899
1900 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1901
1902 if (per_objfile == NULL)
1903 {
1904 dwarf2_per_bfd *per_bfd;
1905
1906 /* We can share a "dwarf2_per_bfd" with other objfiles if the BFD
1907 doesn't require relocations and if there aren't partial symbols
1908 from some other reader. */
1909 if (!objfile_has_partial_symbols (objfile)
1910 && !gdb_bfd_requires_relocations (objfile->obfd))
1911 {
1912 /* See if one has been created for this BFD yet. */
1913 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1914
1915 if (per_bfd == nullptr)
1916 {
1917 /* No, create it now. */
1918 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1919 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1920 }
1921 }
1922 else
1923 {
1924 /* No sharing possible, create one specifically for this objfile. */
1925 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1926 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1927 }
1928
1929 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1930 }
1931
1932 return (!per_objfile->per_bfd->info.is_virtual
1933 && per_objfile->per_bfd->info.s.section != NULL
1934 && !per_objfile->per_bfd->abbrev.is_virtual
1935 && per_objfile->per_bfd->abbrev.s.section != NULL);
1936 }
1937
1938 /* When loading sections, we look either for uncompressed section or for
1939 compressed section names. */
1940
1941 static int
1942 section_is_p (const char *section_name,
1943 const struct dwarf2_section_names *names)
1944 {
1945 if (names->normal != NULL
1946 && strcmp (section_name, names->normal) == 0)
1947 return 1;
1948 if (names->compressed != NULL
1949 && strcmp (section_name, names->compressed) == 0)
1950 return 1;
1951 return 0;
1952 }
1953
1954 /* See declaration. */
1955
1956 void
1957 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1958 const dwarf2_debug_sections &names)
1959 {
1960 flagword aflag = bfd_section_flags (sectp);
1961
1962 if ((aflag & SEC_HAS_CONTENTS) == 0)
1963 {
1964 }
1965 else if (elf_section_data (sectp)->this_hdr.sh_size
1966 > bfd_get_file_size (abfd))
1967 {
1968 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1969 warning (_("Discarding section %s which has a section size (%s"
1970 ") larger than the file size [in module %s]"),
1971 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1972 bfd_get_filename (abfd));
1973 }
1974 else if (section_is_p (sectp->name, &names.info))
1975 {
1976 this->info.s.section = sectp;
1977 this->info.size = bfd_section_size (sectp);
1978 }
1979 else if (section_is_p (sectp->name, &names.abbrev))
1980 {
1981 this->abbrev.s.section = sectp;
1982 this->abbrev.size = bfd_section_size (sectp);
1983 }
1984 else if (section_is_p (sectp->name, &names.line))
1985 {
1986 this->line.s.section = sectp;
1987 this->line.size = bfd_section_size (sectp);
1988 }
1989 else if (section_is_p (sectp->name, &names.loc))
1990 {
1991 this->loc.s.section = sectp;
1992 this->loc.size = bfd_section_size (sectp);
1993 }
1994 else if (section_is_p (sectp->name, &names.loclists))
1995 {
1996 this->loclists.s.section = sectp;
1997 this->loclists.size = bfd_section_size (sectp);
1998 }
1999 else if (section_is_p (sectp->name, &names.macinfo))
2000 {
2001 this->macinfo.s.section = sectp;
2002 this->macinfo.size = bfd_section_size (sectp);
2003 }
2004 else if (section_is_p (sectp->name, &names.macro))
2005 {
2006 this->macro.s.section = sectp;
2007 this->macro.size = bfd_section_size (sectp);
2008 }
2009 else if (section_is_p (sectp->name, &names.str))
2010 {
2011 this->str.s.section = sectp;
2012 this->str.size = bfd_section_size (sectp);
2013 }
2014 else if (section_is_p (sectp->name, &names.str_offsets))
2015 {
2016 this->str_offsets.s.section = sectp;
2017 this->str_offsets.size = bfd_section_size (sectp);
2018 }
2019 else if (section_is_p (sectp->name, &names.line_str))
2020 {
2021 this->line_str.s.section = sectp;
2022 this->line_str.size = bfd_section_size (sectp);
2023 }
2024 else if (section_is_p (sectp->name, &names.addr))
2025 {
2026 this->addr.s.section = sectp;
2027 this->addr.size = bfd_section_size (sectp);
2028 }
2029 else if (section_is_p (sectp->name, &names.frame))
2030 {
2031 this->frame.s.section = sectp;
2032 this->frame.size = bfd_section_size (sectp);
2033 }
2034 else if (section_is_p (sectp->name, &names.eh_frame))
2035 {
2036 this->eh_frame.s.section = sectp;
2037 this->eh_frame.size = bfd_section_size (sectp);
2038 }
2039 else if (section_is_p (sectp->name, &names.ranges))
2040 {
2041 this->ranges.s.section = sectp;
2042 this->ranges.size = bfd_section_size (sectp);
2043 }
2044 else if (section_is_p (sectp->name, &names.rnglists))
2045 {
2046 this->rnglists.s.section = sectp;
2047 this->rnglists.size = bfd_section_size (sectp);
2048 }
2049 else if (section_is_p (sectp->name, &names.types))
2050 {
2051 struct dwarf2_section_info type_section;
2052
2053 memset (&type_section, 0, sizeof (type_section));
2054 type_section.s.section = sectp;
2055 type_section.size = bfd_section_size (sectp);
2056
2057 this->types.push_back (type_section);
2058 }
2059 else if (section_is_p (sectp->name, &names.gdb_index))
2060 {
2061 this->gdb_index.s.section = sectp;
2062 this->gdb_index.size = bfd_section_size (sectp);
2063 }
2064 else if (section_is_p (sectp->name, &names.debug_names))
2065 {
2066 this->debug_names.s.section = sectp;
2067 this->debug_names.size = bfd_section_size (sectp);
2068 }
2069 else if (section_is_p (sectp->name, &names.debug_aranges))
2070 {
2071 this->debug_aranges.s.section = sectp;
2072 this->debug_aranges.size = bfd_section_size (sectp);
2073 }
2074
2075 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2076 && bfd_section_vma (sectp) == 0)
2077 this->has_section_at_zero = true;
2078 }
2079
2080 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2081 SECTION_NAME. */
2082
2083 void
2084 dwarf2_get_section_info (struct objfile *objfile,
2085 enum dwarf2_section_enum sect,
2086 asection **sectp, const gdb_byte **bufp,
2087 bfd_size_type *sizep)
2088 {
2089 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2090 struct dwarf2_section_info *info;
2091
2092 /* We may see an objfile without any DWARF, in which case we just
2093 return nothing. */
2094 if (per_objfile == NULL)
2095 {
2096 *sectp = NULL;
2097 *bufp = NULL;
2098 *sizep = 0;
2099 return;
2100 }
2101 switch (sect)
2102 {
2103 case DWARF2_DEBUG_FRAME:
2104 info = &per_objfile->per_bfd->frame;
2105 break;
2106 case DWARF2_EH_FRAME:
2107 info = &per_objfile->per_bfd->eh_frame;
2108 break;
2109 default:
2110 gdb_assert_not_reached ("unexpected section");
2111 }
2112
2113 info->read (objfile);
2114
2115 *sectp = info->get_bfd_section ();
2116 *bufp = info->buffer;
2117 *sizep = info->size;
2118 }
2119
2120 /* A helper function to find the sections for a .dwz file. */
2121
2122 static void
2123 locate_dwz_sections (bfd *abfd, asection *sectp, dwz_file *dwz_file)
2124 {
2125 /* Note that we only support the standard ELF names, because .dwz
2126 is ELF-only (at the time of writing). */
2127 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2128 {
2129 dwz_file->abbrev.s.section = sectp;
2130 dwz_file->abbrev.size = bfd_section_size (sectp);
2131 }
2132 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2133 {
2134 dwz_file->info.s.section = sectp;
2135 dwz_file->info.size = bfd_section_size (sectp);
2136 }
2137 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2138 {
2139 dwz_file->str.s.section = sectp;
2140 dwz_file->str.size = bfd_section_size (sectp);
2141 }
2142 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2143 {
2144 dwz_file->line.s.section = sectp;
2145 dwz_file->line.size = bfd_section_size (sectp);
2146 }
2147 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2148 {
2149 dwz_file->macro.s.section = sectp;
2150 dwz_file->macro.size = bfd_section_size (sectp);
2151 }
2152 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2153 {
2154 dwz_file->gdb_index.s.section = sectp;
2155 dwz_file->gdb_index.size = bfd_section_size (sectp);
2156 }
2157 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2158 {
2159 dwz_file->debug_names.s.section = sectp;
2160 dwz_file->debug_names.size = bfd_section_size (sectp);
2161 }
2162 }
2163
2164 /* See dwarf2read.h. */
2165
2166 struct dwz_file *
2167 dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
2168 {
2169 const char *filename;
2170 bfd_size_type buildid_len_arg;
2171 size_t buildid_len;
2172 bfd_byte *buildid;
2173
2174 if (per_bfd->dwz_file != NULL)
2175 return per_bfd->dwz_file.get ();
2176
2177 bfd_set_error (bfd_error_no_error);
2178 gdb::unique_xmalloc_ptr<char> data
2179 (bfd_get_alt_debug_link_info (per_bfd->obfd,
2180 &buildid_len_arg, &buildid));
2181 if (data == NULL)
2182 {
2183 if (bfd_get_error () == bfd_error_no_error)
2184 return NULL;
2185 error (_("could not read '.gnu_debugaltlink' section: %s"),
2186 bfd_errmsg (bfd_get_error ()));
2187 }
2188
2189 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2190
2191 buildid_len = (size_t) buildid_len_arg;
2192
2193 filename = data.get ();
2194
2195 std::string abs_storage;
2196 if (!IS_ABSOLUTE_PATH (filename))
2197 {
2198 gdb::unique_xmalloc_ptr<char> abs
2199 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
2200
2201 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2202 filename = abs_storage.c_str ();
2203 }
2204
2205 /* First try the file name given in the section. If that doesn't
2206 work, try to use the build-id instead. */
2207 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
2208 if (dwz_bfd != NULL)
2209 {
2210 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2211 dwz_bfd.reset (nullptr);
2212 }
2213
2214 if (dwz_bfd == NULL)
2215 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2216
2217 if (dwz_bfd == nullptr)
2218 {
2219 gdb::unique_xmalloc_ptr<char> alt_filename;
2220 const char *origname = bfd_get_filename (per_bfd->obfd);
2221
2222 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2223 buildid_len,
2224 origname,
2225 &alt_filename));
2226
2227 if (fd.get () >= 0)
2228 {
2229 /* File successfully retrieved from server. */
2230 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
2231
2232 if (dwz_bfd == nullptr)
2233 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2234 alt_filename.get ());
2235 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2236 dwz_bfd.reset (nullptr);
2237 }
2238 }
2239
2240 if (dwz_bfd == NULL)
2241 error (_("could not find '.gnu_debugaltlink' file for %s"),
2242 bfd_get_filename (per_bfd->obfd));
2243
2244 std::unique_ptr<struct dwz_file> result
2245 (new struct dwz_file (std::move (dwz_bfd)));
2246
2247 for (asection *sec : gdb_bfd_sections (result->dwz_bfd))
2248 locate_dwz_sections (result->dwz_bfd.get (), sec, result.get ());
2249
2250 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2251 per_bfd->dwz_file = std::move (result);
2252 return per_bfd->dwz_file.get ();
2253 }
2254 \f
2255 /* DWARF quick_symbols_functions support. */
2256
2257 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2258 unique line tables, so we maintain a separate table of all .debug_line
2259 derived entries to support the sharing.
2260 All the quick functions need is the list of file names. We discard the
2261 line_header when we're done and don't need to record it here. */
2262 struct quick_file_names
2263 {
2264 /* The data used to construct the hash key. */
2265 struct stmt_list_hash hash;
2266
2267 /* The number of entries in file_names, real_names. */
2268 unsigned int num_file_names;
2269
2270 /* The file names from the line table, after being run through
2271 file_full_name. */
2272 const char **file_names;
2273
2274 /* The file names from the line table after being run through
2275 gdb_realpath. These are computed lazily. */
2276 const char **real_names;
2277 };
2278
2279 /* When using the index (and thus not using psymtabs), each CU has an
2280 object of this type. This is used to hold information needed by
2281 the various "quick" methods. */
2282 struct dwarf2_per_cu_quick_data
2283 {
2284 /* The file table. This can be NULL if there was no file table
2285 or it's currently not read in.
2286 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2287 struct quick_file_names *file_names;
2288
2289 /* A temporary mark bit used when iterating over all CUs in
2290 expand_symtabs_matching. */
2291 unsigned int mark : 1;
2292
2293 /* True if we've tried to read the file table and found there isn't one.
2294 There will be no point in trying to read it again next time. */
2295 unsigned int no_file_data : 1;
2296 };
2297
2298 /* Utility hash function for a stmt_list_hash. */
2299
2300 static hashval_t
2301 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2302 {
2303 hashval_t v = 0;
2304
2305 if (stmt_list_hash->dwo_unit != NULL)
2306 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2307 v += to_underlying (stmt_list_hash->line_sect_off);
2308 return v;
2309 }
2310
2311 /* Utility equality function for a stmt_list_hash. */
2312
2313 static int
2314 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2315 const struct stmt_list_hash *rhs)
2316 {
2317 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2318 return 0;
2319 if (lhs->dwo_unit != NULL
2320 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2321 return 0;
2322
2323 return lhs->line_sect_off == rhs->line_sect_off;
2324 }
2325
2326 /* Hash function for a quick_file_names. */
2327
2328 static hashval_t
2329 hash_file_name_entry (const void *e)
2330 {
2331 const struct quick_file_names *file_data
2332 = (const struct quick_file_names *) e;
2333
2334 return hash_stmt_list_entry (&file_data->hash);
2335 }
2336
2337 /* Equality function for a quick_file_names. */
2338
2339 static int
2340 eq_file_name_entry (const void *a, const void *b)
2341 {
2342 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2343 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2344
2345 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2346 }
2347
2348 /* Delete function for a quick_file_names. */
2349
2350 static void
2351 delete_file_name_entry (void *e)
2352 {
2353 struct quick_file_names *file_data = (struct quick_file_names *) e;
2354 int i;
2355
2356 for (i = 0; i < file_data->num_file_names; ++i)
2357 {
2358 xfree ((void*) file_data->file_names[i]);
2359 if (file_data->real_names)
2360 xfree ((void*) file_data->real_names[i]);
2361 }
2362
2363 /* The space for the struct itself lives on the obstack, so we don't
2364 free it here. */
2365 }
2366
2367 /* Create a quick_file_names hash table. */
2368
2369 static htab_up
2370 create_quick_file_names_table (unsigned int nr_initial_entries)
2371 {
2372 return htab_up (htab_create_alloc (nr_initial_entries,
2373 hash_file_name_entry, eq_file_name_entry,
2374 delete_file_name_entry, xcalloc, xfree));
2375 }
2376
2377 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2378 function is unrelated to symtabs, symtab would have to be created afterwards.
2379 You should call age_cached_comp_units after processing the CU. */
2380
2381 static dwarf2_cu *
2382 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2383 bool skip_partial)
2384 {
2385 if (per_cu->is_debug_types)
2386 load_full_type_unit (per_cu, per_objfile);
2387 else
2388 load_full_comp_unit (per_cu, per_objfile, skip_partial, language_minimal);
2389
2390 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2391 if (cu == nullptr)
2392 return nullptr; /* Dummy CU. */
2393
2394 dwarf2_find_base_address (cu->dies, cu);
2395
2396 return cu;
2397 }
2398
2399 /* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2400
2401 static void
2402 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2403 dwarf2_per_objfile *per_objfile, bool skip_partial)
2404 {
2405 /* Skip type_unit_groups, reading the type units they contain
2406 is handled elsewhere. */
2407 if (per_cu->type_unit_group_p ())
2408 return;
2409
2410 /* The destructor of dwarf2_queue_guard frees any entries left on
2411 the queue. After this point we're guaranteed to leave this function
2412 with the dwarf queue empty. */
2413 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2414
2415 if (!per_objfile->symtab_set_p (per_cu))
2416 {
2417 queue_comp_unit (per_cu, per_objfile, language_minimal);
2418 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2419
2420 /* If we just loaded a CU from a DWO, and we're working with an index
2421 that may badly handle TUs, load all the TUs in that DWO as well.
2422 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2423 if (!per_cu->is_debug_types
2424 && cu != NULL
2425 && cu->dwo_unit != NULL
2426 && per_objfile->per_bfd->index_table != NULL
2427 && per_objfile->per_bfd->index_table->version <= 7
2428 /* DWP files aren't supported yet. */
2429 && get_dwp_file (per_objfile) == NULL)
2430 queue_and_load_all_dwo_tus (cu);
2431 }
2432
2433 process_queue (per_objfile);
2434
2435 /* Age the cache, releasing compilation units that have not
2436 been used recently. */
2437 per_objfile->age_comp_units ();
2438 }
2439
2440 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2441 the per-objfile for which this symtab is instantiated.
2442
2443 Returns the resulting symbol table. */
2444
2445 static struct compunit_symtab *
2446 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2447 dwarf2_per_objfile *per_objfile,
2448 bool skip_partial)
2449 {
2450 gdb_assert (per_objfile->per_bfd->using_index);
2451
2452 if (!per_objfile->symtab_set_p (per_cu))
2453 {
2454 free_cached_comp_units freer (per_objfile);
2455 scoped_restore decrementer = increment_reading_symtab ();
2456 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2457 process_cu_includes (per_objfile);
2458 }
2459
2460 return per_objfile->get_symtab (per_cu);
2461 }
2462
2463 /* See declaration. */
2464
2465 dwarf2_per_cu_data *
2466 dwarf2_per_bfd::get_cutu (int index)
2467 {
2468 if (index >= this->all_comp_units.size ())
2469 {
2470 index -= this->all_comp_units.size ();
2471 gdb_assert (index < this->all_type_units.size ());
2472 return &this->all_type_units[index]->per_cu;
2473 }
2474
2475 return this->all_comp_units[index];
2476 }
2477
2478 /* See declaration. */
2479
2480 dwarf2_per_cu_data *
2481 dwarf2_per_bfd::get_cu (int index)
2482 {
2483 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2484
2485 return this->all_comp_units[index];
2486 }
2487
2488 /* See declaration. */
2489
2490 signatured_type *
2491 dwarf2_per_bfd::get_tu (int index)
2492 {
2493 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2494
2495 return this->all_type_units[index];
2496 }
2497
2498 /* See read.h. */
2499
2500 dwarf2_per_cu_data *
2501 dwarf2_per_bfd::allocate_per_cu ()
2502 {
2503 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2504 result->per_bfd = this;
2505 result->index = m_num_psymtabs++;
2506 return result;
2507 }
2508
2509 /* See read.h. */
2510
2511 signatured_type *
2512 dwarf2_per_bfd::allocate_signatured_type ()
2513 {
2514 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2515 result->per_cu.per_bfd = this;
2516 result->per_cu.index = m_num_psymtabs++;
2517 return result;
2518 }
2519
2520 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2521 obstack, and constructed with the specified field values. */
2522
2523 static dwarf2_per_cu_data *
2524 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2525 struct dwarf2_section_info *section,
2526 int is_dwz,
2527 sect_offset sect_off, ULONGEST length)
2528 {
2529 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
2530 the_cu->sect_off = sect_off;
2531 the_cu->length = length;
2532 the_cu->section = section;
2533 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2534 struct dwarf2_per_cu_quick_data);
2535 the_cu->is_dwz = is_dwz;
2536 return the_cu;
2537 }
2538
2539 /* A helper for create_cus_from_index that handles a given list of
2540 CUs. */
2541
2542 static void
2543 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2544 const gdb_byte *cu_list, offset_type n_elements,
2545 struct dwarf2_section_info *section,
2546 int is_dwz)
2547 {
2548 for (offset_type i = 0; i < n_elements; i += 2)
2549 {
2550 gdb_static_assert (sizeof (ULONGEST) >= 8);
2551
2552 sect_offset sect_off
2553 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2554 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2555 cu_list += 2 * 8;
2556
2557 dwarf2_per_cu_data *per_cu
2558 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2559 length);
2560 per_bfd->all_comp_units.push_back (per_cu);
2561 }
2562 }
2563
2564 /* Read the CU list from the mapped index, and use it to create all
2565 the CU objects for PER_BFD. */
2566
2567 static void
2568 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2569 const gdb_byte *cu_list, offset_type cu_list_elements,
2570 const gdb_byte *dwz_list, offset_type dwz_elements)
2571 {
2572 gdb_assert (per_bfd->all_comp_units.empty ());
2573 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2574
2575 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2576 &per_bfd->info, 0);
2577
2578 if (dwz_elements == 0)
2579 return;
2580
2581 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2582 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2583 &dwz->info, 1);
2584 }
2585
2586 /* Create the signatured type hash table from the index. */
2587
2588 static void
2589 create_signatured_type_table_from_index
2590 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2591 const gdb_byte *bytes, offset_type elements)
2592 {
2593 gdb_assert (per_bfd->all_type_units.empty ());
2594 per_bfd->all_type_units.reserve (elements / 3);
2595
2596 htab_up sig_types_hash = allocate_signatured_type_table ();
2597
2598 for (offset_type i = 0; i < elements; i += 3)
2599 {
2600 struct signatured_type *sig_type;
2601 ULONGEST signature;
2602 void **slot;
2603 cu_offset type_offset_in_tu;
2604
2605 gdb_static_assert (sizeof (ULONGEST) >= 8);
2606 sect_offset sect_off
2607 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2608 type_offset_in_tu
2609 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2610 BFD_ENDIAN_LITTLE);
2611 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2612 bytes += 3 * 8;
2613
2614 sig_type = per_bfd->allocate_signatured_type ();
2615 sig_type->signature = signature;
2616 sig_type->type_offset_in_tu = type_offset_in_tu;
2617 sig_type->per_cu.is_debug_types = 1;
2618 sig_type->per_cu.section = section;
2619 sig_type->per_cu.sect_off = sect_off;
2620 sig_type->per_cu.v.quick
2621 = OBSTACK_ZALLOC (&per_bfd->obstack,
2622 struct dwarf2_per_cu_quick_data);
2623
2624 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2625 *slot = sig_type;
2626
2627 per_bfd->all_type_units.push_back (sig_type);
2628 }
2629
2630 per_bfd->signatured_types = std::move (sig_types_hash);
2631 }
2632
2633 /* Create the signatured type hash table from .debug_names. */
2634
2635 static void
2636 create_signatured_type_table_from_debug_names
2637 (dwarf2_per_objfile *per_objfile,
2638 const mapped_debug_names &map,
2639 struct dwarf2_section_info *section,
2640 struct dwarf2_section_info *abbrev_section)
2641 {
2642 struct objfile *objfile = per_objfile->objfile;
2643
2644 section->read (objfile);
2645 abbrev_section->read (objfile);
2646
2647 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2648 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2649
2650 htab_up sig_types_hash = allocate_signatured_type_table ();
2651
2652 for (uint32_t i = 0; i < map.tu_count; ++i)
2653 {
2654 struct signatured_type *sig_type;
2655 void **slot;
2656
2657 sect_offset sect_off
2658 = (sect_offset) (extract_unsigned_integer
2659 (map.tu_table_reordered + i * map.offset_size,
2660 map.offset_size,
2661 map.dwarf5_byte_order));
2662
2663 comp_unit_head cu_header;
2664 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2665 abbrev_section,
2666 section->buffer + to_underlying (sect_off),
2667 rcuh_kind::TYPE);
2668
2669 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
2670 sig_type->signature = cu_header.signature;
2671 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2672 sig_type->per_cu.is_debug_types = 1;
2673 sig_type->per_cu.section = section;
2674 sig_type->per_cu.sect_off = sect_off;
2675 sig_type->per_cu.v.quick
2676 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
2677 struct dwarf2_per_cu_quick_data);
2678
2679 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2680 *slot = sig_type;
2681
2682 per_objfile->per_bfd->all_type_units.push_back (sig_type);
2683 }
2684
2685 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2686 }
2687
2688 /* Read the address map data from the mapped index, and use it to
2689 populate the objfile's psymtabs_addrmap. */
2690
2691 static void
2692 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2693 struct mapped_index *index)
2694 {
2695 struct objfile *objfile = per_objfile->objfile;
2696 struct gdbarch *gdbarch = objfile->arch ();
2697 const gdb_byte *iter, *end;
2698 struct addrmap *mutable_map;
2699 CORE_ADDR baseaddr;
2700
2701 auto_obstack temp_obstack;
2702
2703 mutable_map = addrmap_create_mutable (&temp_obstack);
2704
2705 iter = index->address_table.data ();
2706 end = iter + index->address_table.size ();
2707
2708 baseaddr = objfile->text_section_offset ();
2709
2710 while (iter < end)
2711 {
2712 ULONGEST hi, lo, cu_index;
2713 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2714 iter += 8;
2715 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2716 iter += 8;
2717 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2718 iter += 4;
2719
2720 if (lo > hi)
2721 {
2722 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2723 hex_string (lo), hex_string (hi));
2724 continue;
2725 }
2726
2727 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
2728 {
2729 complaint (_(".gdb_index address table has invalid CU number %u"),
2730 (unsigned) cu_index);
2731 continue;
2732 }
2733
2734 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2735 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2736 addrmap_set_empty (mutable_map, lo, hi - 1,
2737 per_objfile->per_bfd->get_cu (cu_index));
2738 }
2739
2740 objfile->partial_symtabs->psymtabs_addrmap
2741 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2742 }
2743
2744 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2745 populate the objfile's psymtabs_addrmap. */
2746
2747 static void
2748 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2749 struct dwarf2_section_info *section)
2750 {
2751 struct objfile *objfile = per_objfile->objfile;
2752 bfd *abfd = objfile->obfd;
2753 struct gdbarch *gdbarch = objfile->arch ();
2754 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2755
2756 auto_obstack temp_obstack;
2757 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2758
2759 std::unordered_map<sect_offset,
2760 dwarf2_per_cu_data *,
2761 gdb::hash_enum<sect_offset>>
2762 debug_info_offset_to_per_cu;
2763 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
2764 {
2765 const auto insertpair
2766 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2767 if (!insertpair.second)
2768 {
2769 warning (_("Section .debug_aranges in %s has duplicate "
2770 "debug_info_offset %s, ignoring .debug_aranges."),
2771 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2772 return;
2773 }
2774 }
2775
2776 section->read (objfile);
2777
2778 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2779
2780 const gdb_byte *addr = section->buffer;
2781
2782 while (addr < section->buffer + section->size)
2783 {
2784 const gdb_byte *const entry_addr = addr;
2785 unsigned int bytes_read;
2786
2787 const LONGEST entry_length = read_initial_length (abfd, addr,
2788 &bytes_read);
2789 addr += bytes_read;
2790
2791 const gdb_byte *const entry_end = addr + entry_length;
2792 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2793 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2794 if (addr + entry_length > section->buffer + section->size)
2795 {
2796 warning (_("Section .debug_aranges in %s entry at offset %s "
2797 "length %s exceeds section length %s, "
2798 "ignoring .debug_aranges."),
2799 objfile_name (objfile),
2800 plongest (entry_addr - section->buffer),
2801 plongest (bytes_read + entry_length),
2802 pulongest (section->size));
2803 return;
2804 }
2805
2806 /* The version number. */
2807 const uint16_t version = read_2_bytes (abfd, addr);
2808 addr += 2;
2809 if (version != 2)
2810 {
2811 warning (_("Section .debug_aranges in %s entry at offset %s "
2812 "has unsupported version %d, ignoring .debug_aranges."),
2813 objfile_name (objfile),
2814 plongest (entry_addr - section->buffer), version);
2815 return;
2816 }
2817
2818 const uint64_t debug_info_offset
2819 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2820 addr += offset_size;
2821 const auto per_cu_it
2822 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2823 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2824 {
2825 warning (_("Section .debug_aranges in %s entry at offset %s "
2826 "debug_info_offset %s does not exists, "
2827 "ignoring .debug_aranges."),
2828 objfile_name (objfile),
2829 plongest (entry_addr - section->buffer),
2830 pulongest (debug_info_offset));
2831 return;
2832 }
2833 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2834
2835 const uint8_t address_size = *addr++;
2836 if (address_size < 1 || address_size > 8)
2837 {
2838 warning (_("Section .debug_aranges in %s entry at offset %s "
2839 "address_size %u is invalid, ignoring .debug_aranges."),
2840 objfile_name (objfile),
2841 plongest (entry_addr - section->buffer), address_size);
2842 return;
2843 }
2844
2845 const uint8_t segment_selector_size = *addr++;
2846 if (segment_selector_size != 0)
2847 {
2848 warning (_("Section .debug_aranges in %s entry at offset %s "
2849 "segment_selector_size %u is not supported, "
2850 "ignoring .debug_aranges."),
2851 objfile_name (objfile),
2852 plongest (entry_addr - section->buffer),
2853 segment_selector_size);
2854 return;
2855 }
2856
2857 /* Must pad to an alignment boundary that is twice the address
2858 size. It is undocumented by the DWARF standard but GCC does
2859 use it. */
2860 for (size_t padding = ((-(addr - section->buffer))
2861 & (2 * address_size - 1));
2862 padding > 0; padding--)
2863 if (*addr++ != 0)
2864 {
2865 warning (_("Section .debug_aranges in %s entry at offset %s "
2866 "padding is not zero, ignoring .debug_aranges."),
2867 objfile_name (objfile),
2868 plongest (entry_addr - section->buffer));
2869 return;
2870 }
2871
2872 for (;;)
2873 {
2874 if (addr + 2 * address_size > entry_end)
2875 {
2876 warning (_("Section .debug_aranges in %s entry at offset %s "
2877 "address list is not properly terminated, "
2878 "ignoring .debug_aranges."),
2879 objfile_name (objfile),
2880 plongest (entry_addr - section->buffer));
2881 return;
2882 }
2883 ULONGEST start = extract_unsigned_integer (addr, address_size,
2884 dwarf5_byte_order);
2885 addr += address_size;
2886 ULONGEST length = extract_unsigned_integer (addr, address_size,
2887 dwarf5_byte_order);
2888 addr += address_size;
2889 if (start == 0 && length == 0)
2890 break;
2891 if (start == 0 && !per_objfile->per_bfd->has_section_at_zero)
2892 {
2893 /* Symbol was eliminated due to a COMDAT group. */
2894 continue;
2895 }
2896 ULONGEST end = start + length;
2897 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2898 - baseaddr);
2899 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2900 - baseaddr);
2901 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2902 }
2903 }
2904
2905 objfile->partial_symtabs->psymtabs_addrmap
2906 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2907 }
2908
2909 /* Find a slot in the mapped index INDEX for the object named NAME.
2910 If NAME is found, set *VEC_OUT to point to the CU vector in the
2911 constant pool and return true. If NAME cannot be found, return
2912 false. */
2913
2914 static bool
2915 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2916 offset_type **vec_out)
2917 {
2918 offset_type hash;
2919 offset_type slot, step;
2920 int (*cmp) (const char *, const char *);
2921
2922 gdb::unique_xmalloc_ptr<char> without_params;
2923 if (current_language->la_language == language_cplus
2924 || current_language->la_language == language_fortran
2925 || current_language->la_language == language_d)
2926 {
2927 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2928 not contain any. */
2929
2930 if (strchr (name, '(') != NULL)
2931 {
2932 without_params = cp_remove_params (name);
2933
2934 if (without_params != NULL)
2935 name = without_params.get ();
2936 }
2937 }
2938
2939 /* Index version 4 did not support case insensitive searches. But the
2940 indices for case insensitive languages are built in lowercase, therefore
2941 simulate our NAME being searched is also lowercased. */
2942 hash = mapped_index_string_hash ((index->version == 4
2943 && case_sensitivity == case_sensitive_off
2944 ? 5 : index->version),
2945 name);
2946
2947 slot = hash & (index->symbol_table.size () - 1);
2948 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2949 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2950
2951 for (;;)
2952 {
2953 const char *str;
2954
2955 const auto &bucket = index->symbol_table[slot];
2956 if (bucket.name == 0 && bucket.vec == 0)
2957 return false;
2958
2959 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2960 if (!cmp (name, str))
2961 {
2962 *vec_out = (offset_type *) (index->constant_pool
2963 + MAYBE_SWAP (bucket.vec));
2964 return true;
2965 }
2966
2967 slot = (slot + step) & (index->symbol_table.size () - 1);
2968 }
2969 }
2970
2971 /* A helper function that reads the .gdb_index from BUFFER and fills
2972 in MAP. FILENAME is the name of the file containing the data;
2973 it is used for error reporting. DEPRECATED_OK is true if it is
2974 ok to use deprecated sections.
2975
2976 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2977 out parameters that are filled in with information about the CU and
2978 TU lists in the section.
2979
2980 Returns true if all went well, false otherwise. */
2981
2982 static bool
2983 read_gdb_index_from_buffer (const char *filename,
2984 bool deprecated_ok,
2985 gdb::array_view<const gdb_byte> buffer,
2986 struct mapped_index *map,
2987 const gdb_byte **cu_list,
2988 offset_type *cu_list_elements,
2989 const gdb_byte **types_list,
2990 offset_type *types_list_elements)
2991 {
2992 const gdb_byte *addr = &buffer[0];
2993
2994 /* Version check. */
2995 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2996 /* Versions earlier than 3 emitted every copy of a psymbol. This
2997 causes the index to behave very poorly for certain requests. Version 3
2998 contained incomplete addrmap. So, it seems better to just ignore such
2999 indices. */
3000 if (version < 4)
3001 {
3002 static int warning_printed = 0;
3003 if (!warning_printed)
3004 {
3005 warning (_("Skipping obsolete .gdb_index section in %s."),
3006 filename);
3007 warning_printed = 1;
3008 }
3009 return 0;
3010 }
3011 /* Index version 4 uses a different hash function than index version
3012 5 and later.
3013
3014 Versions earlier than 6 did not emit psymbols for inlined
3015 functions. Using these files will cause GDB not to be able to
3016 set breakpoints on inlined functions by name, so we ignore these
3017 indices unless the user has done
3018 "set use-deprecated-index-sections on". */
3019 if (version < 6 && !deprecated_ok)
3020 {
3021 static int warning_printed = 0;
3022 if (!warning_printed)
3023 {
3024 warning (_("\
3025 Skipping deprecated .gdb_index section in %s.\n\
3026 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3027 to use the section anyway."),
3028 filename);
3029 warning_printed = 1;
3030 }
3031 return 0;
3032 }
3033 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3034 of the TU (for symbols coming from TUs),
3035 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3036 Plus gold-generated indices can have duplicate entries for global symbols,
3037 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3038 These are just performance bugs, and we can't distinguish gdb-generated
3039 indices from gold-generated ones, so issue no warning here. */
3040
3041 /* Indexes with higher version than the one supported by GDB may be no
3042 longer backward compatible. */
3043 if (version > 8)
3044 return 0;
3045
3046 map->version = version;
3047
3048 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3049
3050 int i = 0;
3051 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3052 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3053 / 8);
3054 ++i;
3055
3056 *types_list = addr + MAYBE_SWAP (metadata[i]);
3057 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3058 - MAYBE_SWAP (metadata[i]))
3059 / 8);
3060 ++i;
3061
3062 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3063 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3064 map->address_table
3065 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3066 ++i;
3067
3068 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3069 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3070 map->symbol_table
3071 = gdb::array_view<mapped_index::symbol_table_slot>
3072 ((mapped_index::symbol_table_slot *) symbol_table,
3073 (mapped_index::symbol_table_slot *) symbol_table_end);
3074
3075 ++i;
3076 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3077
3078 return 1;
3079 }
3080
3081 /* Callback types for dwarf2_read_gdb_index. */
3082
3083 typedef gdb::function_view
3084 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3085 get_gdb_index_contents_ftype;
3086 typedef gdb::function_view
3087 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3088 get_gdb_index_contents_dwz_ftype;
3089
3090 /* Read .gdb_index. If everything went ok, initialize the "quick"
3091 elements of all the CUs and return 1. Otherwise, return 0. */
3092
3093 static int
3094 dwarf2_read_gdb_index
3095 (dwarf2_per_objfile *per_objfile,
3096 get_gdb_index_contents_ftype get_gdb_index_contents,
3097 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3098 {
3099 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3100 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3101 struct dwz_file *dwz;
3102 struct objfile *objfile = per_objfile->objfile;
3103 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
3104
3105 gdb::array_view<const gdb_byte> main_index_contents
3106 = get_gdb_index_contents (objfile, per_bfd);
3107
3108 if (main_index_contents.empty ())
3109 return 0;
3110
3111 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3112 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3113 use_deprecated_index_sections,
3114 main_index_contents, map.get (), &cu_list,
3115 &cu_list_elements, &types_list,
3116 &types_list_elements))
3117 return 0;
3118
3119 /* Don't use the index if it's empty. */
3120 if (map->symbol_table.empty ())
3121 return 0;
3122
3123 /* If there is a .dwz file, read it so we can get its CU list as
3124 well. */
3125 dwz = dwarf2_get_dwz_file (per_bfd);
3126 if (dwz != NULL)
3127 {
3128 struct mapped_index dwz_map;
3129 const gdb_byte *dwz_types_ignore;
3130 offset_type dwz_types_elements_ignore;
3131
3132 gdb::array_view<const gdb_byte> dwz_index_content
3133 = get_gdb_index_contents_dwz (objfile, dwz);
3134
3135 if (dwz_index_content.empty ())
3136 return 0;
3137
3138 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3139 1, dwz_index_content, &dwz_map,
3140 &dwz_list, &dwz_list_elements,
3141 &dwz_types_ignore,
3142 &dwz_types_elements_ignore))
3143 {
3144 warning (_("could not read '.gdb_index' section from %s; skipping"),
3145 bfd_get_filename (dwz->dwz_bfd.get ()));
3146 return 0;
3147 }
3148 }
3149
3150 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3151 dwz_list_elements);
3152
3153 if (types_list_elements)
3154 {
3155 /* We can only handle a single .debug_types when we have an
3156 index. */
3157 if (per_bfd->types.size () != 1)
3158 return 0;
3159
3160 dwarf2_section_info *section = &per_bfd->types[0];
3161
3162 create_signatured_type_table_from_index (per_bfd, section, types_list,
3163 types_list_elements);
3164 }
3165
3166 create_addrmap_from_index (per_objfile, map.get ());
3167
3168 per_bfd->index_table = std::move (map);
3169 per_bfd->using_index = 1;
3170 per_bfd->quick_file_names_table =
3171 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3172
3173 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
3174 objfiles using the same BFD. */
3175 gdb_assert (per_bfd->partial_symtabs == nullptr);
3176 per_bfd->partial_symtabs = objfile->partial_symtabs;
3177
3178 return 1;
3179 }
3180
3181 /* die_reader_func for dw2_get_file_names. */
3182
3183 static void
3184 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3185 const gdb_byte *info_ptr,
3186 struct die_info *comp_unit_die)
3187 {
3188 struct dwarf2_cu *cu = reader->cu;
3189 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3190 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3191 struct dwarf2_per_cu_data *lh_cu;
3192 struct attribute *attr;
3193 void **slot;
3194 struct quick_file_names *qfn;
3195
3196 gdb_assert (! this_cu->is_debug_types);
3197
3198 /* Our callers never want to match partial units -- instead they
3199 will match the enclosing full CU. */
3200 if (comp_unit_die->tag == DW_TAG_partial_unit)
3201 {
3202 this_cu->v.quick->no_file_data = 1;
3203 return;
3204 }
3205
3206 lh_cu = this_cu;
3207 slot = NULL;
3208
3209 line_header_up lh;
3210 sect_offset line_offset {};
3211
3212 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3213 if (attr != nullptr)
3214 {
3215 struct quick_file_names find_entry;
3216
3217 line_offset = (sect_offset) DW_UNSND (attr);
3218
3219 /* We may have already read in this line header (TU line header sharing).
3220 If we have we're done. */
3221 find_entry.hash.dwo_unit = cu->dwo_unit;
3222 find_entry.hash.line_sect_off = line_offset;
3223 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
3224 &find_entry, INSERT);
3225 if (*slot != NULL)
3226 {
3227 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3228 return;
3229 }
3230
3231 lh = dwarf_decode_line_header (line_offset, cu);
3232 }
3233 if (lh == NULL)
3234 {
3235 lh_cu->v.quick->no_file_data = 1;
3236 return;
3237 }
3238
3239 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
3240 qfn->hash.dwo_unit = cu->dwo_unit;
3241 qfn->hash.line_sect_off = line_offset;
3242 gdb_assert (slot != NULL);
3243 *slot = qfn;
3244
3245 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3246
3247 int offset = 0;
3248 if (strcmp (fnd.name, "<unknown>") != 0)
3249 ++offset;
3250
3251 qfn->num_file_names = offset + lh->file_names_size ();
3252 qfn->file_names =
3253 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
3254 qfn->num_file_names);
3255 if (offset != 0)
3256 qfn->file_names[0] = xstrdup (fnd.name);
3257 for (int i = 0; i < lh->file_names_size (); ++i)
3258 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3259 fnd.comp_dir).release ();
3260 qfn->real_names = NULL;
3261
3262 lh_cu->v.quick->file_names = qfn;
3263 }
3264
3265 /* A helper for the "quick" functions which attempts to read the line
3266 table for THIS_CU. */
3267
3268 static struct quick_file_names *
3269 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3270 dwarf2_per_objfile *per_objfile)
3271 {
3272 /* This should never be called for TUs. */
3273 gdb_assert (! this_cu->is_debug_types);
3274 /* Nor type unit groups. */
3275 gdb_assert (! this_cu->type_unit_group_p ());
3276
3277 if (this_cu->v.quick->file_names != NULL)
3278 return this_cu->v.quick->file_names;
3279 /* If we know there is no line data, no point in looking again. */
3280 if (this_cu->v.quick->no_file_data)
3281 return NULL;
3282
3283 cutu_reader reader (this_cu, per_objfile);
3284 if (!reader.dummy_p)
3285 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3286
3287 if (this_cu->v.quick->no_file_data)
3288 return NULL;
3289 return this_cu->v.quick->file_names;
3290 }
3291
3292 /* A helper for the "quick" functions which computes and caches the
3293 real path for a given file name from the line table. */
3294
3295 static const char *
3296 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3297 struct quick_file_names *qfn, int index)
3298 {
3299 if (qfn->real_names == NULL)
3300 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3301 qfn->num_file_names, const char *);
3302
3303 if (qfn->real_names[index] == NULL)
3304 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3305
3306 return qfn->real_names[index];
3307 }
3308
3309 static struct symtab *
3310 dw2_find_last_source_symtab (struct objfile *objfile)
3311 {
3312 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3313 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3314 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3315
3316 if (cust == NULL)
3317 return NULL;
3318
3319 return compunit_primary_filetab (cust);
3320 }
3321
3322 /* Traversal function for dw2_forget_cached_source_info. */
3323
3324 static int
3325 dw2_free_cached_file_names (void **slot, void *info)
3326 {
3327 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3328
3329 if (file_data->real_names)
3330 {
3331 int i;
3332
3333 for (i = 0; i < file_data->num_file_names; ++i)
3334 {
3335 xfree ((void*) file_data->real_names[i]);
3336 file_data->real_names[i] = NULL;
3337 }
3338 }
3339
3340 return 1;
3341 }
3342
3343 static void
3344 dw2_forget_cached_source_info (struct objfile *objfile)
3345 {
3346 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3347
3348 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
3349 dw2_free_cached_file_names, NULL);
3350 }
3351
3352 /* Helper function for dw2_map_symtabs_matching_filename that expands
3353 the symtabs and calls the iterator. */
3354
3355 static int
3356 dw2_map_expand_apply (struct objfile *objfile,
3357 struct dwarf2_per_cu_data *per_cu,
3358 const char *name, const char *real_path,
3359 gdb::function_view<bool (symtab *)> callback)
3360 {
3361 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3362
3363 /* Don't visit already-expanded CUs. */
3364 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3365 if (per_objfile->symtab_set_p (per_cu))
3366 return 0;
3367
3368 /* This may expand more than one symtab, and we want to iterate over
3369 all of them. */
3370 dw2_instantiate_symtab (per_cu, per_objfile, false);
3371
3372 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3373 last_made, callback);
3374 }
3375
3376 /* Implementation of the map_symtabs_matching_filename method. */
3377
3378 static bool
3379 dw2_map_symtabs_matching_filename
3380 (struct objfile *objfile, const char *name, const char *real_path,
3381 gdb::function_view<bool (symtab *)> callback)
3382 {
3383 const char *name_basename = lbasename (name);
3384 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3385
3386 /* The rule is CUs specify all the files, including those used by
3387 any TU, so there's no need to scan TUs here. */
3388
3389 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3390 {
3391 /* We only need to look at symtabs not already expanded. */
3392 if (per_objfile->symtab_set_p (per_cu))
3393 continue;
3394
3395 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3396 if (file_data == NULL)
3397 continue;
3398
3399 for (int j = 0; j < file_data->num_file_names; ++j)
3400 {
3401 const char *this_name = file_data->file_names[j];
3402 const char *this_real_name;
3403
3404 if (compare_filenames_for_search (this_name, name))
3405 {
3406 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3407 callback))
3408 return true;
3409 continue;
3410 }
3411
3412 /* Before we invoke realpath, which can get expensive when many
3413 files are involved, do a quick comparison of the basenames. */
3414 if (! basenames_may_differ
3415 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3416 continue;
3417
3418 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
3419 if (compare_filenames_for_search (this_real_name, name))
3420 {
3421 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3422 callback))
3423 return true;
3424 continue;
3425 }
3426
3427 if (real_path != NULL)
3428 {
3429 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3430 gdb_assert (IS_ABSOLUTE_PATH (name));
3431 if (this_real_name != NULL
3432 && FILENAME_CMP (real_path, this_real_name) == 0)
3433 {
3434 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3435 callback))
3436 return true;
3437 continue;
3438 }
3439 }
3440 }
3441 }
3442
3443 return false;
3444 }
3445
3446 /* Struct used to manage iterating over all CUs looking for a symbol. */
3447
3448 struct dw2_symtab_iterator
3449 {
3450 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3451 dwarf2_per_objfile *per_objfile;
3452 /* If set, only look for symbols that match that block. Valid values are
3453 GLOBAL_BLOCK and STATIC_BLOCK. */
3454 gdb::optional<block_enum> block_index;
3455 /* The kind of symbol we're looking for. */
3456 domain_enum domain;
3457 /* The list of CUs from the index entry of the symbol,
3458 or NULL if not found. */
3459 offset_type *vec;
3460 /* The next element in VEC to look at. */
3461 int next;
3462 /* The number of elements in VEC, or zero if there is no match. */
3463 int length;
3464 /* Have we seen a global version of the symbol?
3465 If so we can ignore all further global instances.
3466 This is to work around gold/15646, inefficient gold-generated
3467 indices. */
3468 int global_seen;
3469 };
3470
3471 /* Initialize the index symtab iterator ITER, common part. */
3472
3473 static void
3474 dw2_symtab_iter_init_common (struct dw2_symtab_iterator *iter,
3475 dwarf2_per_objfile *per_objfile,
3476 gdb::optional<block_enum> block_index,
3477 domain_enum domain)
3478 {
3479 iter->per_objfile = per_objfile;
3480 iter->block_index = block_index;
3481 iter->domain = domain;
3482 iter->next = 0;
3483 iter->global_seen = 0;
3484 iter->vec = NULL;
3485 iter->length = 0;
3486 }
3487
3488 /* Initialize the index symtab iterator ITER, const char *NAME variant. */
3489
3490 static void
3491 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3492 dwarf2_per_objfile *per_objfile,
3493 gdb::optional<block_enum> block_index,
3494 domain_enum domain,
3495 const char *name)
3496 {
3497 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3498
3499 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3500 /* index is NULL if OBJF_READNOW. */
3501 if (index == NULL)
3502 return;
3503
3504 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3505 iter->length = MAYBE_SWAP (*iter->vec);
3506 }
3507
3508 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3509
3510 static void
3511 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3512 dwarf2_per_objfile *per_objfile,
3513 gdb::optional<block_enum> block_index,
3514 domain_enum domain, offset_type namei)
3515 {
3516 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3517
3518 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3519 /* index is NULL if OBJF_READNOW. */
3520 if (index == NULL)
3521 return;
3522
3523 gdb_assert (!index->symbol_name_slot_invalid (namei));
3524 const auto &bucket = index->symbol_table[namei];
3525
3526 iter->vec = (offset_type *) (index->constant_pool
3527 + MAYBE_SWAP (bucket.vec));
3528 iter->length = MAYBE_SWAP (*iter->vec);
3529 }
3530
3531 /* Return the next matching CU or NULL if there are no more. */
3532
3533 static struct dwarf2_per_cu_data *
3534 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3535 {
3536 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3537
3538 for ( ; iter->next < iter->length; ++iter->next)
3539 {
3540 offset_type cu_index_and_attrs =
3541 MAYBE_SWAP (iter->vec[iter->next + 1]);
3542 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3543 gdb_index_symbol_kind symbol_kind =
3544 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3545 /* Only check the symbol attributes if they're present.
3546 Indices prior to version 7 don't record them,
3547 and indices >= 7 may elide them for certain symbols
3548 (gold does this). */
3549 int attrs_valid =
3550 (per_objfile->per_bfd->index_table->version >= 7
3551 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3552
3553 /* Don't crash on bad data. */
3554 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3555 + per_objfile->per_bfd->all_type_units.size ()))
3556 {
3557 complaint (_(".gdb_index entry has bad CU index"
3558 " [in module %s]"), objfile_name (per_objfile->objfile));
3559 continue;
3560 }
3561
3562 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3563
3564 /* Skip if already read in. */
3565 if (per_objfile->symtab_set_p (per_cu))
3566 continue;
3567
3568 /* Check static vs global. */
3569 if (attrs_valid)
3570 {
3571 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3572
3573 if (iter->block_index.has_value ())
3574 {
3575 bool want_static = *iter->block_index == STATIC_BLOCK;
3576
3577 if (is_static != want_static)
3578 continue;
3579 }
3580
3581 /* Work around gold/15646. */
3582 if (!is_static
3583 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3584 {
3585 if (iter->global_seen)
3586 continue;
3587
3588 iter->global_seen = 1;
3589 }
3590 }
3591
3592 /* Only check the symbol's kind if it has one. */
3593 if (attrs_valid)
3594 {
3595 switch (iter->domain)
3596 {
3597 case VAR_DOMAIN:
3598 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3599 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3600 /* Some types are also in VAR_DOMAIN. */
3601 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3602 continue;
3603 break;
3604 case STRUCT_DOMAIN:
3605 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3606 continue;
3607 break;
3608 case LABEL_DOMAIN:
3609 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3610 continue;
3611 break;
3612 case MODULE_DOMAIN:
3613 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3614 continue;
3615 break;
3616 default:
3617 break;
3618 }
3619 }
3620
3621 ++iter->next;
3622 return per_cu;
3623 }
3624
3625 return NULL;
3626 }
3627
3628 static struct compunit_symtab *
3629 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3630 const char *name, domain_enum domain)
3631 {
3632 struct compunit_symtab *stab_best = NULL;
3633 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3634
3635 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3636
3637 struct dw2_symtab_iterator iter;
3638 struct dwarf2_per_cu_data *per_cu;
3639
3640 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
3641
3642 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3643 {
3644 struct symbol *sym, *with_opaque = NULL;
3645 struct compunit_symtab *stab
3646 = dw2_instantiate_symtab (per_cu, per_objfile, false);
3647 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3648 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3649
3650 sym = block_find_symbol (block, name, domain,
3651 block_find_non_opaque_type_preferred,
3652 &with_opaque);
3653
3654 /* Some caution must be observed with overloaded functions
3655 and methods, since the index will not contain any overload
3656 information (but NAME might contain it). */
3657
3658 if (sym != NULL
3659 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3660 return stab;
3661 if (with_opaque != NULL
3662 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3663 stab_best = stab;
3664
3665 /* Keep looking through other CUs. */
3666 }
3667
3668 return stab_best;
3669 }
3670
3671 static void
3672 dw2_print_stats (struct objfile *objfile)
3673 {
3674 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3675 int total = (per_objfile->per_bfd->all_comp_units.size ()
3676 + per_objfile->per_bfd->all_type_units.size ());
3677 int count = 0;
3678
3679 for (int i = 0; i < total; ++i)
3680 {
3681 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3682
3683 if (!per_objfile->symtab_set_p (per_cu))
3684 ++count;
3685 }
3686 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3687 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3688 }
3689
3690 /* This dumps minimal information about the index.
3691 It is called via "mt print objfiles".
3692 One use is to verify .gdb_index has been loaded by the
3693 gdb.dwarf2/gdb-index.exp testcase. */
3694
3695 static void
3696 dw2_dump (struct objfile *objfile)
3697 {
3698 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3699
3700 gdb_assert (per_objfile->per_bfd->using_index);
3701 printf_filtered (".gdb_index:");
3702 if (per_objfile->per_bfd->index_table != NULL)
3703 {
3704 printf_filtered (" version %d\n",
3705 per_objfile->per_bfd->index_table->version);
3706 }
3707 else
3708 printf_filtered (" faked for \"readnow\"\n");
3709 printf_filtered ("\n");
3710 }
3711
3712 static void
3713 dw2_expand_symtabs_for_function (struct objfile *objfile,
3714 const char *func_name)
3715 {
3716 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3717
3718 struct dw2_symtab_iterator iter;
3719 struct dwarf2_per_cu_data *per_cu;
3720
3721 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
3722
3723 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3724 dw2_instantiate_symtab (per_cu, per_objfile, false);
3725
3726 }
3727
3728 static void
3729 dw2_expand_all_symtabs (struct objfile *objfile)
3730 {
3731 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3732 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3733 + per_objfile->per_bfd->all_type_units.size ());
3734
3735 for (int i = 0; i < total_units; ++i)
3736 {
3737 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3738
3739 /* We don't want to directly expand a partial CU, because if we
3740 read it with the wrong language, then assertion failures can
3741 be triggered later on. See PR symtab/23010. So, tell
3742 dw2_instantiate_symtab to skip partial CUs -- any important
3743 partial CU will be read via DW_TAG_imported_unit anyway. */
3744 dw2_instantiate_symtab (per_cu, per_objfile, true);
3745 }
3746 }
3747
3748 static void
3749 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3750 const char *fullname)
3751 {
3752 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3753
3754 /* We don't need to consider type units here.
3755 This is only called for examining code, e.g. expand_line_sal.
3756 There can be an order of magnitude (or more) more type units
3757 than comp units, and we avoid them if we can. */
3758
3759 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3760 {
3761 /* We only need to look at symtabs not already expanded. */
3762 if (per_objfile->symtab_set_p (per_cu))
3763 continue;
3764
3765 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3766 if (file_data == NULL)
3767 continue;
3768
3769 for (int j = 0; j < file_data->num_file_names; ++j)
3770 {
3771 const char *this_fullname = file_data->file_names[j];
3772
3773 if (filename_cmp (this_fullname, fullname) == 0)
3774 {
3775 dw2_instantiate_symtab (per_cu, per_objfile, false);
3776 break;
3777 }
3778 }
3779 }
3780 }
3781
3782 static void
3783 dw2_expand_symtabs_matching_symbol
3784 (mapped_index_base &index,
3785 const lookup_name_info &lookup_name_in,
3786 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3787 enum search_domain kind,
3788 gdb::function_view<bool (offset_type)> match_callback,
3789 dwarf2_per_objfile *per_objfile);
3790
3791 static void
3792 dw2_expand_symtabs_matching_one
3793 (dwarf2_per_cu_data *per_cu,
3794 dwarf2_per_objfile *per_objfile,
3795 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3796 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3797
3798 static void
3799 dw2_map_matching_symbols
3800 (struct objfile *objfile,
3801 const lookup_name_info &name, domain_enum domain,
3802 int global,
3803 gdb::function_view<symbol_found_callback_ftype> callback,
3804 symbol_compare_ftype *ordered_compare)
3805 {
3806 /* Used for Ada. */
3807 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3808
3809 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3810
3811 if (per_objfile->per_bfd->index_table != nullptr)
3812 {
3813 mapped_index &index = *per_objfile->per_bfd->index_table;
3814
3815 const char *match_name = name.ada ().lookup_name ().c_str ();
3816 auto matcher = [&] (const char *symname)
3817 {
3818 if (ordered_compare == nullptr)
3819 return true;
3820 return ordered_compare (symname, match_name) == 0;
3821 };
3822
3823 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3824 [&] (offset_type namei)
3825 {
3826 struct dw2_symtab_iterator iter;
3827 struct dwarf2_per_cu_data *per_cu;
3828
3829 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3830 namei);
3831 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3832 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3833 nullptr);
3834 return true;
3835 }, per_objfile);
3836 }
3837 else
3838 {
3839 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3840 proceed assuming all symtabs have been read in. */
3841 }
3842
3843 for (compunit_symtab *cust : objfile->compunits ())
3844 {
3845 const struct block *block;
3846
3847 if (cust == NULL)
3848 continue;
3849 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3850 if (!iterate_over_symbols_terminated (block, name,
3851 domain, callback))
3852 return;
3853 }
3854 }
3855
3856 /* Starting from a search name, return the string that finds the upper
3857 bound of all strings that start with SEARCH_NAME in a sorted name
3858 list. Returns the empty string to indicate that the upper bound is
3859 the end of the list. */
3860
3861 static std::string
3862 make_sort_after_prefix_name (const char *search_name)
3863 {
3864 /* When looking to complete "func", we find the upper bound of all
3865 symbols that start with "func" by looking for where we'd insert
3866 the closest string that would follow "func" in lexicographical
3867 order. Usually, that's "func"-with-last-character-incremented,
3868 i.e. "fund". Mind non-ASCII characters, though. Usually those
3869 will be UTF-8 multi-byte sequences, but we can't be certain.
3870 Especially mind the 0xff character, which is a valid character in
3871 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3872 rule out compilers allowing it in identifiers. Note that
3873 conveniently, strcmp/strcasecmp are specified to compare
3874 characters interpreted as unsigned char. So what we do is treat
3875 the whole string as a base 256 number composed of a sequence of
3876 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3877 to 0, and carries 1 to the following more-significant position.
3878 If the very first character in SEARCH_NAME ends up incremented
3879 and carries/overflows, then the upper bound is the end of the
3880 list. The string after the empty string is also the empty
3881 string.
3882
3883 Some examples of this operation:
3884
3885 SEARCH_NAME => "+1" RESULT
3886
3887 "abc" => "abd"
3888 "ab\xff" => "ac"
3889 "\xff" "a" "\xff" => "\xff" "b"
3890 "\xff" => ""
3891 "\xff\xff" => ""
3892 "" => ""
3893
3894 Then, with these symbols for example:
3895
3896 func
3897 func1
3898 fund
3899
3900 completing "func" looks for symbols between "func" and
3901 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3902 which finds "func" and "func1", but not "fund".
3903
3904 And with:
3905
3906 funcÿ (Latin1 'ÿ' [0xff])
3907 funcÿ1
3908 fund
3909
3910 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3911 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3912
3913 And with:
3914
3915 ÿÿ (Latin1 'ÿ' [0xff])
3916 ÿÿ1
3917
3918 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3919 the end of the list.
3920 */
3921 std::string after = search_name;
3922 while (!after.empty () && (unsigned char) after.back () == 0xff)
3923 after.pop_back ();
3924 if (!after.empty ())
3925 after.back () = (unsigned char) after.back () + 1;
3926 return after;
3927 }
3928
3929 /* See declaration. */
3930
3931 std::pair<std::vector<name_component>::const_iterator,
3932 std::vector<name_component>::const_iterator>
3933 mapped_index_base::find_name_components_bounds
3934 (const lookup_name_info &lookup_name_without_params, language lang,
3935 dwarf2_per_objfile *per_objfile) const
3936 {
3937 auto *name_cmp
3938 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3939
3940 const char *lang_name
3941 = lookup_name_without_params.language_lookup_name (lang);
3942
3943 /* Comparison function object for lower_bound that matches against a
3944 given symbol name. */
3945 auto lookup_compare_lower = [&] (const name_component &elem,
3946 const char *name)
3947 {
3948 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3949 const char *elem_name = elem_qualified + elem.name_offset;
3950 return name_cmp (elem_name, name) < 0;
3951 };
3952
3953 /* Comparison function object for upper_bound that matches against a
3954 given symbol name. */
3955 auto lookup_compare_upper = [&] (const char *name,
3956 const name_component &elem)
3957 {
3958 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3959 const char *elem_name = elem_qualified + elem.name_offset;
3960 return name_cmp (name, elem_name) < 0;
3961 };
3962
3963 auto begin = this->name_components.begin ();
3964 auto end = this->name_components.end ();
3965
3966 /* Find the lower bound. */
3967 auto lower = [&] ()
3968 {
3969 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3970 return begin;
3971 else
3972 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3973 } ();
3974
3975 /* Find the upper bound. */
3976 auto upper = [&] ()
3977 {
3978 if (lookup_name_without_params.completion_mode ())
3979 {
3980 /* In completion mode, we want UPPER to point past all
3981 symbols names that have the same prefix. I.e., with
3982 these symbols, and completing "func":
3983
3984 function << lower bound
3985 function1
3986 other_function << upper bound
3987
3988 We find the upper bound by looking for the insertion
3989 point of "func"-with-last-character-incremented,
3990 i.e. "fund". */
3991 std::string after = make_sort_after_prefix_name (lang_name);
3992 if (after.empty ())
3993 return end;
3994 return std::lower_bound (lower, end, after.c_str (),
3995 lookup_compare_lower);
3996 }
3997 else
3998 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3999 } ();
4000
4001 return {lower, upper};
4002 }
4003
4004 /* See declaration. */
4005
4006 void
4007 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
4008 {
4009 if (!this->name_components.empty ())
4010 return;
4011
4012 this->name_components_casing = case_sensitivity;
4013 auto *name_cmp
4014 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4015
4016 /* The code below only knows how to break apart components of C++
4017 symbol names (and other languages that use '::' as
4018 namespace/module separator) and Ada symbol names. */
4019 auto count = this->symbol_name_count ();
4020 for (offset_type idx = 0; idx < count; idx++)
4021 {
4022 if (this->symbol_name_slot_invalid (idx))
4023 continue;
4024
4025 const char *name = this->symbol_name_at (idx, per_objfile);
4026
4027 /* Add each name component to the name component table. */
4028 unsigned int previous_len = 0;
4029
4030 if (strstr (name, "::") != nullptr)
4031 {
4032 for (unsigned int current_len = cp_find_first_component (name);
4033 name[current_len] != '\0';
4034 current_len += cp_find_first_component (name + current_len))
4035 {
4036 gdb_assert (name[current_len] == ':');
4037 this->name_components.push_back ({previous_len, idx});
4038 /* Skip the '::'. */
4039 current_len += 2;
4040 previous_len = current_len;
4041 }
4042 }
4043 else
4044 {
4045 /* Handle the Ada encoded (aka mangled) form here. */
4046 for (const char *iter = strstr (name, "__");
4047 iter != nullptr;
4048 iter = strstr (iter, "__"))
4049 {
4050 this->name_components.push_back ({previous_len, idx});
4051 iter += 2;
4052 previous_len = iter - name;
4053 }
4054 }
4055
4056 this->name_components.push_back ({previous_len, idx});
4057 }
4058
4059 /* Sort name_components elements by name. */
4060 auto name_comp_compare = [&] (const name_component &left,
4061 const name_component &right)
4062 {
4063 const char *left_qualified
4064 = this->symbol_name_at (left.idx, per_objfile);
4065 const char *right_qualified
4066 = this->symbol_name_at (right.idx, per_objfile);
4067
4068 const char *left_name = left_qualified + left.name_offset;
4069 const char *right_name = right_qualified + right.name_offset;
4070
4071 return name_cmp (left_name, right_name) < 0;
4072 };
4073
4074 std::sort (this->name_components.begin (),
4075 this->name_components.end (),
4076 name_comp_compare);
4077 }
4078
4079 /* Helper for dw2_expand_symtabs_matching that works with a
4080 mapped_index_base instead of the containing objfile. This is split
4081 to a separate function in order to be able to unit test the
4082 name_components matching using a mock mapped_index_base. For each
4083 symbol name that matches, calls MATCH_CALLBACK, passing it the
4084 symbol's index in the mapped_index_base symbol table. */
4085
4086 static void
4087 dw2_expand_symtabs_matching_symbol
4088 (mapped_index_base &index,
4089 const lookup_name_info &lookup_name_in,
4090 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4091 enum search_domain kind,
4092 gdb::function_view<bool (offset_type)> match_callback,
4093 dwarf2_per_objfile *per_objfile)
4094 {
4095 lookup_name_info lookup_name_without_params
4096 = lookup_name_in.make_ignore_params ();
4097
4098 /* Build the symbol name component sorted vector, if we haven't
4099 yet. */
4100 index.build_name_components (per_objfile);
4101
4102 /* The same symbol may appear more than once in the range though.
4103 E.g., if we're looking for symbols that complete "w", and we have
4104 a symbol named "w1::w2", we'll find the two name components for
4105 that same symbol in the range. To be sure we only call the
4106 callback once per symbol, we first collect the symbol name
4107 indexes that matched in a temporary vector and ignore
4108 duplicates. */
4109 std::vector<offset_type> matches;
4110
4111 struct name_and_matcher
4112 {
4113 symbol_name_matcher_ftype *matcher;
4114 const char *name;
4115
4116 bool operator== (const name_and_matcher &other) const
4117 {
4118 return matcher == other.matcher && strcmp (name, other.name) == 0;
4119 }
4120 };
4121
4122 /* A vector holding all the different symbol name matchers, for all
4123 languages. */
4124 std::vector<name_and_matcher> matchers;
4125
4126 for (int i = 0; i < nr_languages; i++)
4127 {
4128 enum language lang_e = (enum language) i;
4129
4130 const language_defn *lang = language_def (lang_e);
4131 symbol_name_matcher_ftype *name_matcher
4132 = lang->get_symbol_name_matcher (lookup_name_without_params);
4133
4134 name_and_matcher key {
4135 name_matcher,
4136 lookup_name_without_params.language_lookup_name (lang_e)
4137 };
4138
4139 /* Don't insert the same comparison routine more than once.
4140 Note that we do this linear walk. This is not a problem in
4141 practice because the number of supported languages is
4142 low. */
4143 if (std::find (matchers.begin (), matchers.end (), key)
4144 != matchers.end ())
4145 continue;
4146 matchers.push_back (std::move (key));
4147
4148 auto bounds
4149 = index.find_name_components_bounds (lookup_name_without_params,
4150 lang_e, per_objfile);
4151
4152 /* Now for each symbol name in range, check to see if we have a name
4153 match, and if so, call the MATCH_CALLBACK callback. */
4154
4155 for (; bounds.first != bounds.second; ++bounds.first)
4156 {
4157 const char *qualified
4158 = index.symbol_name_at (bounds.first->idx, per_objfile);
4159
4160 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4161 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4162 continue;
4163
4164 matches.push_back (bounds.first->idx);
4165 }
4166 }
4167
4168 std::sort (matches.begin (), matches.end ());
4169
4170 /* Finally call the callback, once per match. */
4171 ULONGEST prev = -1;
4172 for (offset_type idx : matches)
4173 {
4174 if (prev != idx)
4175 {
4176 if (!match_callback (idx))
4177 break;
4178 prev = idx;
4179 }
4180 }
4181
4182 /* Above we use a type wider than idx's for 'prev', since 0 and
4183 (offset_type)-1 are both possible values. */
4184 static_assert (sizeof (prev) > sizeof (offset_type), "");
4185 }
4186
4187 #if GDB_SELF_TEST
4188
4189 namespace selftests { namespace dw2_expand_symtabs_matching {
4190
4191 /* A mock .gdb_index/.debug_names-like name index table, enough to
4192 exercise dw2_expand_symtabs_matching_symbol, which works with the
4193 mapped_index_base interface. Builds an index from the symbol list
4194 passed as parameter to the constructor. */
4195 class mock_mapped_index : public mapped_index_base
4196 {
4197 public:
4198 mock_mapped_index (gdb::array_view<const char *> symbols)
4199 : m_symbol_table (symbols)
4200 {}
4201
4202 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4203
4204 /* Return the number of names in the symbol table. */
4205 size_t symbol_name_count () const override
4206 {
4207 return m_symbol_table.size ();
4208 }
4209
4210 /* Get the name of the symbol at IDX in the symbol table. */
4211 const char *symbol_name_at
4212 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
4213 {
4214 return m_symbol_table[idx];
4215 }
4216
4217 private:
4218 gdb::array_view<const char *> m_symbol_table;
4219 };
4220
4221 /* Convenience function that converts a NULL pointer to a "<null>"
4222 string, to pass to print routines. */
4223
4224 static const char *
4225 string_or_null (const char *str)
4226 {
4227 return str != NULL ? str : "<null>";
4228 }
4229
4230 /* Check if a lookup_name_info built from
4231 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4232 index. EXPECTED_LIST is the list of expected matches, in expected
4233 matching order. If no match expected, then an empty list is
4234 specified. Returns true on success. On failure prints a warning
4235 indicating the file:line that failed, and returns false. */
4236
4237 static bool
4238 check_match (const char *file, int line,
4239 mock_mapped_index &mock_index,
4240 const char *name, symbol_name_match_type match_type,
4241 bool completion_mode,
4242 std::initializer_list<const char *> expected_list,
4243 dwarf2_per_objfile *per_objfile)
4244 {
4245 lookup_name_info lookup_name (name, match_type, completion_mode);
4246
4247 bool matched = true;
4248
4249 auto mismatch = [&] (const char *expected_str,
4250 const char *got)
4251 {
4252 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4253 "expected=\"%s\", got=\"%s\"\n"),
4254 file, line,
4255 (match_type == symbol_name_match_type::FULL
4256 ? "FULL" : "WILD"),
4257 name, string_or_null (expected_str), string_or_null (got));
4258 matched = false;
4259 };
4260
4261 auto expected_it = expected_list.begin ();
4262 auto expected_end = expected_list.end ();
4263
4264 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4265 NULL, ALL_DOMAIN,
4266 [&] (offset_type idx)
4267 {
4268 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
4269 const char *expected_str
4270 = expected_it == expected_end ? NULL : *expected_it++;
4271
4272 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4273 mismatch (expected_str, matched_name);
4274 return true;
4275 }, per_objfile);
4276
4277 const char *expected_str
4278 = expected_it == expected_end ? NULL : *expected_it++;
4279 if (expected_str != NULL)
4280 mismatch (expected_str, NULL);
4281
4282 return matched;
4283 }
4284
4285 /* The symbols added to the mock mapped_index for testing (in
4286 canonical form). */
4287 static const char *test_symbols[] = {
4288 "function",
4289 "std::bar",
4290 "std::zfunction",
4291 "std::zfunction2",
4292 "w1::w2",
4293 "ns::foo<char*>",
4294 "ns::foo<int>",
4295 "ns::foo<long>",
4296 "ns2::tmpl<int>::foo2",
4297 "(anonymous namespace)::A::B::C",
4298
4299 /* These are used to check that the increment-last-char in the
4300 matching algorithm for completion doesn't match "t1_fund" when
4301 completing "t1_func". */
4302 "t1_func",
4303 "t1_func1",
4304 "t1_fund",
4305 "t1_fund1",
4306
4307 /* A UTF-8 name with multi-byte sequences to make sure that
4308 cp-name-parser understands this as a single identifier ("função"
4309 is "function" in PT). */
4310 u8"u8função",
4311
4312 /* \377 (0xff) is Latin1 'ÿ'. */
4313 "yfunc\377",
4314
4315 /* \377 (0xff) is Latin1 'ÿ'. */
4316 "\377",
4317 "\377\377123",
4318
4319 /* A name with all sorts of complications. Starts with "z" to make
4320 it easier for the completion tests below. */
4321 #define Z_SYM_NAME \
4322 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4323 "::tuple<(anonymous namespace)::ui*, " \
4324 "std::default_delete<(anonymous namespace)::ui>, void>"
4325
4326 Z_SYM_NAME
4327 };
4328
4329 /* Returns true if the mapped_index_base::find_name_component_bounds
4330 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4331 in completion mode. */
4332
4333 static bool
4334 check_find_bounds_finds (mapped_index_base &index,
4335 const char *search_name,
4336 gdb::array_view<const char *> expected_syms,
4337 dwarf2_per_objfile *per_objfile)
4338 {
4339 lookup_name_info lookup_name (search_name,
4340 symbol_name_match_type::FULL, true);
4341
4342 auto bounds = index.find_name_components_bounds (lookup_name,
4343 language_cplus,
4344 per_objfile);
4345
4346 size_t distance = std::distance (bounds.first, bounds.second);
4347 if (distance != expected_syms.size ())
4348 return false;
4349
4350 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4351 {
4352 auto nc_elem = bounds.first + exp_elem;
4353 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
4354 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4355 return false;
4356 }
4357
4358 return true;
4359 }
4360
4361 /* Test the lower-level mapped_index::find_name_component_bounds
4362 method. */
4363
4364 static void
4365 test_mapped_index_find_name_component_bounds ()
4366 {
4367 mock_mapped_index mock_index (test_symbols);
4368
4369 mock_index.build_name_components (NULL /* per_objfile */);
4370
4371 /* Test the lower-level mapped_index::find_name_component_bounds
4372 method in completion mode. */
4373 {
4374 static const char *expected_syms[] = {
4375 "t1_func",
4376 "t1_func1",
4377 };
4378
4379 SELF_CHECK (check_find_bounds_finds
4380 (mock_index, "t1_func", expected_syms,
4381 NULL /* per_objfile */));
4382 }
4383
4384 /* Check that the increment-last-char in the name matching algorithm
4385 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4386 {
4387 static const char *expected_syms1[] = {
4388 "\377",
4389 "\377\377123",
4390 };
4391 SELF_CHECK (check_find_bounds_finds
4392 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
4393
4394 static const char *expected_syms2[] = {
4395 "\377\377123",
4396 };
4397 SELF_CHECK (check_find_bounds_finds
4398 (mock_index, "\377\377", expected_syms2,
4399 NULL /* per_objfile */));
4400 }
4401 }
4402
4403 /* Test dw2_expand_symtabs_matching_symbol. */
4404
4405 static void
4406 test_dw2_expand_symtabs_matching_symbol ()
4407 {
4408 mock_mapped_index mock_index (test_symbols);
4409
4410 /* We let all tests run until the end even if some fails, for debug
4411 convenience. */
4412 bool any_mismatch = false;
4413
4414 /* Create the expected symbols list (an initializer_list). Needed
4415 because lists have commas, and we need to pass them to CHECK,
4416 which is a macro. */
4417 #define EXPECT(...) { __VA_ARGS__ }
4418
4419 /* Wrapper for check_match that passes down the current
4420 __FILE__/__LINE__. */
4421 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4422 any_mismatch |= !check_match (__FILE__, __LINE__, \
4423 mock_index, \
4424 NAME, MATCH_TYPE, COMPLETION_MODE, \
4425 EXPECTED_LIST, NULL)
4426
4427 /* Identity checks. */
4428 for (const char *sym : test_symbols)
4429 {
4430 /* Should be able to match all existing symbols. */
4431 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4432 EXPECT (sym));
4433
4434 /* Should be able to match all existing symbols with
4435 parameters. */
4436 std::string with_params = std::string (sym) + "(int)";
4437 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4438 EXPECT (sym));
4439
4440 /* Should be able to match all existing symbols with
4441 parameters and qualifiers. */
4442 with_params = std::string (sym) + " ( int ) const";
4443 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4444 EXPECT (sym));
4445
4446 /* This should really find sym, but cp-name-parser.y doesn't
4447 know about lvalue/rvalue qualifiers yet. */
4448 with_params = std::string (sym) + " ( int ) &&";
4449 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4450 {});
4451 }
4452
4453 /* Check that the name matching algorithm for completion doesn't get
4454 confused with Latin1 'ÿ' / 0xff. */
4455 {
4456 static const char str[] = "\377";
4457 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4458 EXPECT ("\377", "\377\377123"));
4459 }
4460
4461 /* Check that the increment-last-char in the matching algorithm for
4462 completion doesn't match "t1_fund" when completing "t1_func". */
4463 {
4464 static const char str[] = "t1_func";
4465 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4466 EXPECT ("t1_func", "t1_func1"));
4467 }
4468
4469 /* Check that completion mode works at each prefix of the expected
4470 symbol name. */
4471 {
4472 static const char str[] = "function(int)";
4473 size_t len = strlen (str);
4474 std::string lookup;
4475
4476 for (size_t i = 1; i < len; i++)
4477 {
4478 lookup.assign (str, i);
4479 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4480 EXPECT ("function"));
4481 }
4482 }
4483
4484 /* While "w" is a prefix of both components, the match function
4485 should still only be called once. */
4486 {
4487 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4488 EXPECT ("w1::w2"));
4489 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4490 EXPECT ("w1::w2"));
4491 }
4492
4493 /* Same, with a "complicated" symbol. */
4494 {
4495 static const char str[] = Z_SYM_NAME;
4496 size_t len = strlen (str);
4497 std::string lookup;
4498
4499 for (size_t i = 1; i < len; i++)
4500 {
4501 lookup.assign (str, i);
4502 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4503 EXPECT (Z_SYM_NAME));
4504 }
4505 }
4506
4507 /* In FULL mode, an incomplete symbol doesn't match. */
4508 {
4509 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4510 {});
4511 }
4512
4513 /* A complete symbol with parameters matches any overload, since the
4514 index has no overload info. */
4515 {
4516 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4517 EXPECT ("std::zfunction", "std::zfunction2"));
4518 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4519 EXPECT ("std::zfunction", "std::zfunction2"));
4520 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4521 EXPECT ("std::zfunction", "std::zfunction2"));
4522 }
4523
4524 /* Check that whitespace is ignored appropriately. A symbol with a
4525 template argument list. */
4526 {
4527 static const char expected[] = "ns::foo<int>";
4528 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4529 EXPECT (expected));
4530 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4531 EXPECT (expected));
4532 }
4533
4534 /* Check that whitespace is ignored appropriately. A symbol with a
4535 template argument list that includes a pointer. */
4536 {
4537 static const char expected[] = "ns::foo<char*>";
4538 /* Try both completion and non-completion modes. */
4539 static const bool completion_mode[2] = {false, true};
4540 for (size_t i = 0; i < 2; i++)
4541 {
4542 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4543 completion_mode[i], EXPECT (expected));
4544 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4545 completion_mode[i], EXPECT (expected));
4546
4547 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4548 completion_mode[i], EXPECT (expected));
4549 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4550 completion_mode[i], EXPECT (expected));
4551 }
4552 }
4553
4554 {
4555 /* Check method qualifiers are ignored. */
4556 static const char expected[] = "ns::foo<char*>";
4557 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4558 symbol_name_match_type::FULL, true, EXPECT (expected));
4559 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4560 symbol_name_match_type::FULL, true, EXPECT (expected));
4561 CHECK_MATCH ("foo < char * > ( int ) const",
4562 symbol_name_match_type::WILD, true, EXPECT (expected));
4563 CHECK_MATCH ("foo < char * > ( int ) &&",
4564 symbol_name_match_type::WILD, true, EXPECT (expected));
4565 }
4566
4567 /* Test lookup names that don't match anything. */
4568 {
4569 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4570 {});
4571
4572 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4573 {});
4574 }
4575
4576 /* Some wild matching tests, exercising "(anonymous namespace)",
4577 which should not be confused with a parameter list. */
4578 {
4579 static const char *syms[] = {
4580 "A::B::C",
4581 "B::C",
4582 "C",
4583 "A :: B :: C ( int )",
4584 "B :: C ( int )",
4585 "C ( int )",
4586 };
4587
4588 for (const char *s : syms)
4589 {
4590 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4591 EXPECT ("(anonymous namespace)::A::B::C"));
4592 }
4593 }
4594
4595 {
4596 static const char expected[] = "ns2::tmpl<int>::foo2";
4597 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4598 EXPECT (expected));
4599 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4600 EXPECT (expected));
4601 }
4602
4603 SELF_CHECK (!any_mismatch);
4604
4605 #undef EXPECT
4606 #undef CHECK_MATCH
4607 }
4608
4609 static void
4610 run_test ()
4611 {
4612 test_mapped_index_find_name_component_bounds ();
4613 test_dw2_expand_symtabs_matching_symbol ();
4614 }
4615
4616 }} // namespace selftests::dw2_expand_symtabs_matching
4617
4618 #endif /* GDB_SELF_TEST */
4619
4620 /* If FILE_MATCHER is NULL or if PER_CU has
4621 dwarf2_per_cu_quick_data::MARK set (see
4622 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4623 EXPANSION_NOTIFY on it. */
4624
4625 static void
4626 dw2_expand_symtabs_matching_one
4627 (dwarf2_per_cu_data *per_cu,
4628 dwarf2_per_objfile *per_objfile,
4629 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4630 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4631 {
4632 if (file_matcher == NULL || per_cu->v.quick->mark)
4633 {
4634 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4635
4636 compunit_symtab *symtab
4637 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4638 gdb_assert (symtab != nullptr);
4639
4640 if (expansion_notify != NULL && symtab_was_null)
4641 expansion_notify (symtab);
4642 }
4643 }
4644
4645 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4646 matched, to expand corresponding CUs that were marked. IDX is the
4647 index of the symbol name that matched. */
4648
4649 static void
4650 dw2_expand_marked_cus
4651 (dwarf2_per_objfile *per_objfile, offset_type idx,
4652 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4653 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4654 search_domain kind)
4655 {
4656 offset_type *vec, vec_len, vec_idx;
4657 bool global_seen = false;
4658 mapped_index &index = *per_objfile->per_bfd->index_table;
4659
4660 vec = (offset_type *) (index.constant_pool
4661 + MAYBE_SWAP (index.symbol_table[idx].vec));
4662 vec_len = MAYBE_SWAP (vec[0]);
4663 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4664 {
4665 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4666 /* This value is only valid for index versions >= 7. */
4667 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4668 gdb_index_symbol_kind symbol_kind =
4669 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4670 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4671 /* Only check the symbol attributes if they're present.
4672 Indices prior to version 7 don't record them,
4673 and indices >= 7 may elide them for certain symbols
4674 (gold does this). */
4675 int attrs_valid =
4676 (index.version >= 7
4677 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4678
4679 /* Work around gold/15646. */
4680 if (attrs_valid
4681 && !is_static
4682 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4683 {
4684 if (global_seen)
4685 continue;
4686
4687 global_seen = true;
4688 }
4689
4690 /* Only check the symbol's kind if it has one. */
4691 if (attrs_valid)
4692 {
4693 switch (kind)
4694 {
4695 case VARIABLES_DOMAIN:
4696 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4697 continue;
4698 break;
4699 case FUNCTIONS_DOMAIN:
4700 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4701 continue;
4702 break;
4703 case TYPES_DOMAIN:
4704 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4705 continue;
4706 break;
4707 case MODULES_DOMAIN:
4708 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4709 continue;
4710 break;
4711 default:
4712 break;
4713 }
4714 }
4715
4716 /* Don't crash on bad data. */
4717 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4718 + per_objfile->per_bfd->all_type_units.size ()))
4719 {
4720 complaint (_(".gdb_index entry has bad CU index"
4721 " [in module %s]"), objfile_name (per_objfile->objfile));
4722 continue;
4723 }
4724
4725 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4726 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4727 expansion_notify);
4728 }
4729 }
4730
4731 /* If FILE_MATCHER is non-NULL, set all the
4732 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4733 that match FILE_MATCHER. */
4734
4735 static void
4736 dw_expand_symtabs_matching_file_matcher
4737 (dwarf2_per_objfile *per_objfile,
4738 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4739 {
4740 if (file_matcher == NULL)
4741 return;
4742
4743 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4744 htab_eq_pointer,
4745 NULL, xcalloc, xfree));
4746 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4747 htab_eq_pointer,
4748 NULL, xcalloc, xfree));
4749
4750 /* The rule is CUs specify all the files, including those used by
4751 any TU, so there's no need to scan TUs here. */
4752
4753 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4754 {
4755 QUIT;
4756
4757 per_cu->v.quick->mark = 0;
4758
4759 /* We only need to look at symtabs not already expanded. */
4760 if (per_objfile->symtab_set_p (per_cu))
4761 continue;
4762
4763 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4764 if (file_data == NULL)
4765 continue;
4766
4767 if (htab_find (visited_not_found.get (), file_data) != NULL)
4768 continue;
4769 else if (htab_find (visited_found.get (), file_data) != NULL)
4770 {
4771 per_cu->v.quick->mark = 1;
4772 continue;
4773 }
4774
4775 for (int j = 0; j < file_data->num_file_names; ++j)
4776 {
4777 const char *this_real_name;
4778
4779 if (file_matcher (file_data->file_names[j], false))
4780 {
4781 per_cu->v.quick->mark = 1;
4782 break;
4783 }
4784
4785 /* Before we invoke realpath, which can get expensive when many
4786 files are involved, do a quick comparison of the basenames. */
4787 if (!basenames_may_differ
4788 && !file_matcher (lbasename (file_data->file_names[j]),
4789 true))
4790 continue;
4791
4792 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4793 if (file_matcher (this_real_name, false))
4794 {
4795 per_cu->v.quick->mark = 1;
4796 break;
4797 }
4798 }
4799
4800 void **slot = htab_find_slot (per_cu->v.quick->mark
4801 ? visited_found.get ()
4802 : visited_not_found.get (),
4803 file_data, INSERT);
4804 *slot = file_data;
4805 }
4806 }
4807
4808 static void
4809 dw2_expand_symtabs_matching
4810 (struct objfile *objfile,
4811 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4812 const lookup_name_info *lookup_name,
4813 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4814 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4815 enum search_domain kind)
4816 {
4817 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4818
4819 /* index_table is NULL if OBJF_READNOW. */
4820 if (!per_objfile->per_bfd->index_table)
4821 return;
4822
4823 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4824
4825 if (symbol_matcher == NULL && lookup_name == NULL)
4826 {
4827 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4828 {
4829 QUIT;
4830
4831 dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4832 file_matcher, expansion_notify);
4833 }
4834 return;
4835 }
4836
4837 mapped_index &index = *per_objfile->per_bfd->index_table;
4838
4839 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4840 symbol_matcher,
4841 kind, [&] (offset_type idx)
4842 {
4843 dw2_expand_marked_cus (per_objfile, idx, file_matcher, expansion_notify,
4844 kind);
4845 return true;
4846 }, per_objfile);
4847 }
4848
4849 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4850 symtab. */
4851
4852 static struct compunit_symtab *
4853 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4854 CORE_ADDR pc)
4855 {
4856 int i;
4857
4858 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4859 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4860 return cust;
4861
4862 if (cust->includes == NULL)
4863 return NULL;
4864
4865 for (i = 0; cust->includes[i]; ++i)
4866 {
4867 struct compunit_symtab *s = cust->includes[i];
4868
4869 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4870 if (s != NULL)
4871 return s;
4872 }
4873
4874 return NULL;
4875 }
4876
4877 static struct compunit_symtab *
4878 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4879 struct bound_minimal_symbol msymbol,
4880 CORE_ADDR pc,
4881 struct obj_section *section,
4882 int warn_if_readin)
4883 {
4884 struct dwarf2_per_cu_data *data;
4885 struct compunit_symtab *result;
4886
4887 if (!objfile->partial_symtabs->psymtabs_addrmap)
4888 return NULL;
4889
4890 CORE_ADDR baseaddr = objfile->text_section_offset ();
4891 data = (struct dwarf2_per_cu_data *) addrmap_find
4892 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4893 if (!data)
4894 return NULL;
4895
4896 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4897 if (warn_if_readin && per_objfile->symtab_set_p (data))
4898 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4899 paddress (objfile->arch (), pc));
4900
4901 result = recursively_find_pc_sect_compunit_symtab
4902 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4903
4904 gdb_assert (result != NULL);
4905 return result;
4906 }
4907
4908 static void
4909 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4910 void *data, int need_fullname)
4911 {
4912 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4913
4914 if (!per_objfile->per_bfd->filenames_cache)
4915 {
4916 per_objfile->per_bfd->filenames_cache.emplace ();
4917
4918 htab_up visited (htab_create_alloc (10,
4919 htab_hash_pointer, htab_eq_pointer,
4920 NULL, xcalloc, xfree));
4921
4922 /* The rule is CUs specify all the files, including those used
4923 by any TU, so there's no need to scan TUs here. We can
4924 ignore file names coming from already-expanded CUs. */
4925
4926 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4927 {
4928 if (per_objfile->symtab_set_p (per_cu))
4929 {
4930 void **slot = htab_find_slot (visited.get (),
4931 per_cu->v.quick->file_names,
4932 INSERT);
4933
4934 *slot = per_cu->v.quick->file_names;
4935 }
4936 }
4937
4938 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4939 {
4940 /* We only need to look at symtabs not already expanded. */
4941 if (per_objfile->symtab_set_p (per_cu))
4942 continue;
4943
4944 quick_file_names *file_data
4945 = dw2_get_file_names (per_cu, per_objfile);
4946 if (file_data == NULL)
4947 continue;
4948
4949 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4950 if (*slot)
4951 {
4952 /* Already visited. */
4953 continue;
4954 }
4955 *slot = file_data;
4956
4957 for (int j = 0; j < file_data->num_file_names; ++j)
4958 {
4959 const char *filename = file_data->file_names[j];
4960 per_objfile->per_bfd->filenames_cache->seen (filename);
4961 }
4962 }
4963 }
4964
4965 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
4966 {
4967 gdb::unique_xmalloc_ptr<char> this_real_name;
4968
4969 if (need_fullname)
4970 this_real_name = gdb_realpath (filename);
4971 (*fun) (filename, this_real_name.get (), data);
4972 });
4973 }
4974
4975 static int
4976 dw2_has_symbols (struct objfile *objfile)
4977 {
4978 return 1;
4979 }
4980
4981 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4982 {
4983 dw2_has_symbols,
4984 dw2_find_last_source_symtab,
4985 dw2_forget_cached_source_info,
4986 dw2_map_symtabs_matching_filename,
4987 dw2_lookup_symbol,
4988 NULL,
4989 dw2_print_stats,
4990 dw2_dump,
4991 dw2_expand_symtabs_for_function,
4992 dw2_expand_all_symtabs,
4993 dw2_expand_symtabs_with_fullname,
4994 dw2_map_matching_symbols,
4995 dw2_expand_symtabs_matching,
4996 dw2_find_pc_sect_compunit_symtab,
4997 NULL,
4998 dw2_map_symbol_filenames
4999 };
5000
5001 /* DWARF-5 debug_names reader. */
5002
5003 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5004 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5005
5006 /* A helper function that reads the .debug_names section in SECTION
5007 and fills in MAP. FILENAME is the name of the file containing the
5008 section; it is used for error reporting.
5009
5010 Returns true if all went well, false otherwise. */
5011
5012 static bool
5013 read_debug_names_from_section (struct objfile *objfile,
5014 const char *filename,
5015 struct dwarf2_section_info *section,
5016 mapped_debug_names &map)
5017 {
5018 if (section->empty ())
5019 return false;
5020
5021 /* Older elfutils strip versions could keep the section in the main
5022 executable while splitting it for the separate debug info file. */
5023 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5024 return false;
5025
5026 section->read (objfile);
5027
5028 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
5029
5030 const gdb_byte *addr = section->buffer;
5031
5032 bfd *const abfd = section->get_bfd_owner ();
5033
5034 unsigned int bytes_read;
5035 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5036 addr += bytes_read;
5037
5038 map.dwarf5_is_dwarf64 = bytes_read != 4;
5039 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5040 if (bytes_read + length != section->size)
5041 {
5042 /* There may be multiple per-CU indices. */
5043 warning (_("Section .debug_names in %s length %s does not match "
5044 "section length %s, ignoring .debug_names."),
5045 filename, plongest (bytes_read + length),
5046 pulongest (section->size));
5047 return false;
5048 }
5049
5050 /* The version number. */
5051 uint16_t version = read_2_bytes (abfd, addr);
5052 addr += 2;
5053 if (version != 5)
5054 {
5055 warning (_("Section .debug_names in %s has unsupported version %d, "
5056 "ignoring .debug_names."),
5057 filename, version);
5058 return false;
5059 }
5060
5061 /* Padding. */
5062 uint16_t padding = read_2_bytes (abfd, addr);
5063 addr += 2;
5064 if (padding != 0)
5065 {
5066 warning (_("Section .debug_names in %s has unsupported padding %d, "
5067 "ignoring .debug_names."),
5068 filename, padding);
5069 return false;
5070 }
5071
5072 /* comp_unit_count - The number of CUs in the CU list. */
5073 map.cu_count = read_4_bytes (abfd, addr);
5074 addr += 4;
5075
5076 /* local_type_unit_count - The number of TUs in the local TU
5077 list. */
5078 map.tu_count = read_4_bytes (abfd, addr);
5079 addr += 4;
5080
5081 /* foreign_type_unit_count - The number of TUs in the foreign TU
5082 list. */
5083 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5084 addr += 4;
5085 if (foreign_tu_count != 0)
5086 {
5087 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5088 "ignoring .debug_names."),
5089 filename, static_cast<unsigned long> (foreign_tu_count));
5090 return false;
5091 }
5092
5093 /* bucket_count - The number of hash buckets in the hash lookup
5094 table. */
5095 map.bucket_count = read_4_bytes (abfd, addr);
5096 addr += 4;
5097
5098 /* name_count - The number of unique names in the index. */
5099 map.name_count = read_4_bytes (abfd, addr);
5100 addr += 4;
5101
5102 /* abbrev_table_size - The size in bytes of the abbreviations
5103 table. */
5104 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5105 addr += 4;
5106
5107 /* augmentation_string_size - The size in bytes of the augmentation
5108 string. This value is rounded up to a multiple of 4. */
5109 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5110 addr += 4;
5111 map.augmentation_is_gdb = ((augmentation_string_size
5112 == sizeof (dwarf5_augmentation))
5113 && memcmp (addr, dwarf5_augmentation,
5114 sizeof (dwarf5_augmentation)) == 0);
5115 augmentation_string_size += (-augmentation_string_size) & 3;
5116 addr += augmentation_string_size;
5117
5118 /* List of CUs */
5119 map.cu_table_reordered = addr;
5120 addr += map.cu_count * map.offset_size;
5121
5122 /* List of Local TUs */
5123 map.tu_table_reordered = addr;
5124 addr += map.tu_count * map.offset_size;
5125
5126 /* Hash Lookup Table */
5127 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5128 addr += map.bucket_count * 4;
5129 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5130 addr += map.name_count * 4;
5131
5132 /* Name Table */
5133 map.name_table_string_offs_reordered = addr;
5134 addr += map.name_count * map.offset_size;
5135 map.name_table_entry_offs_reordered = addr;
5136 addr += map.name_count * map.offset_size;
5137
5138 const gdb_byte *abbrev_table_start = addr;
5139 for (;;)
5140 {
5141 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5142 addr += bytes_read;
5143 if (index_num == 0)
5144 break;
5145
5146 const auto insertpair
5147 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5148 if (!insertpair.second)
5149 {
5150 warning (_("Section .debug_names in %s has duplicate index %s, "
5151 "ignoring .debug_names."),
5152 filename, pulongest (index_num));
5153 return false;
5154 }
5155 mapped_debug_names::index_val &indexval = insertpair.first->second;
5156 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5157 addr += bytes_read;
5158
5159 for (;;)
5160 {
5161 mapped_debug_names::index_val::attr attr;
5162 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5163 addr += bytes_read;
5164 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5165 addr += bytes_read;
5166 if (attr.form == DW_FORM_implicit_const)
5167 {
5168 attr.implicit_const = read_signed_leb128 (abfd, addr,
5169 &bytes_read);
5170 addr += bytes_read;
5171 }
5172 if (attr.dw_idx == 0 && attr.form == 0)
5173 break;
5174 indexval.attr_vec.push_back (std::move (attr));
5175 }
5176 }
5177 if (addr != abbrev_table_start + abbrev_table_size)
5178 {
5179 warning (_("Section .debug_names in %s has abbreviation_table "
5180 "of size %s vs. written as %u, ignoring .debug_names."),
5181 filename, plongest (addr - abbrev_table_start),
5182 abbrev_table_size);
5183 return false;
5184 }
5185 map.entry_pool = addr;
5186
5187 return true;
5188 }
5189
5190 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5191 list. */
5192
5193 static void
5194 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
5195 const mapped_debug_names &map,
5196 dwarf2_section_info &section,
5197 bool is_dwz)
5198 {
5199 if (!map.augmentation_is_gdb)
5200 {
5201 for (uint32_t i = 0; i < map.cu_count; ++i)
5202 {
5203 sect_offset sect_off
5204 = (sect_offset) (extract_unsigned_integer
5205 (map.cu_table_reordered + i * map.offset_size,
5206 map.offset_size,
5207 map.dwarf5_byte_order));
5208 /* We don't know the length of the CU, because the CU list in a
5209 .debug_names index can be incomplete, so we can't use the start of
5210 the next CU as end of this CU. We create the CUs here with length 0,
5211 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5212 dwarf2_per_cu_data *per_cu
5213 = create_cu_from_index_list (per_bfd, &section, is_dwz, sect_off, 0);
5214 per_bfd->all_comp_units.push_back (per_cu);
5215 }
5216 }
5217
5218 sect_offset sect_off_prev;
5219 for (uint32_t i = 0; i <= map.cu_count; ++i)
5220 {
5221 sect_offset sect_off_next;
5222 if (i < map.cu_count)
5223 {
5224 sect_off_next
5225 = (sect_offset) (extract_unsigned_integer
5226 (map.cu_table_reordered + i * map.offset_size,
5227 map.offset_size,
5228 map.dwarf5_byte_order));
5229 }
5230 else
5231 sect_off_next = (sect_offset) section.size;
5232 if (i >= 1)
5233 {
5234 const ULONGEST length = sect_off_next - sect_off_prev;
5235 dwarf2_per_cu_data *per_cu
5236 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5237 sect_off_prev, length);
5238 per_bfd->all_comp_units.push_back (per_cu);
5239 }
5240 sect_off_prev = sect_off_next;
5241 }
5242 }
5243
5244 /* Read the CU list from the mapped index, and use it to create all
5245 the CU objects for this dwarf2_per_objfile. */
5246
5247 static void
5248 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
5249 const mapped_debug_names &map,
5250 const mapped_debug_names &dwz_map)
5251 {
5252 gdb_assert (per_bfd->all_comp_units.empty ());
5253 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5254
5255 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
5256 false /* is_dwz */);
5257
5258 if (dwz_map.cu_count == 0)
5259 return;
5260
5261 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5262 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
5263 true /* is_dwz */);
5264 }
5265
5266 /* Read .debug_names. If everything went ok, initialize the "quick"
5267 elements of all the CUs and return true. Otherwise, return false. */
5268
5269 static bool
5270 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
5271 {
5272 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5273 mapped_debug_names dwz_map;
5274 struct objfile *objfile = per_objfile->objfile;
5275 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5276
5277 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5278 &per_objfile->per_bfd->debug_names, *map))
5279 return false;
5280
5281 /* Don't use the index if it's empty. */
5282 if (map->name_count == 0)
5283 return false;
5284
5285 /* If there is a .dwz file, read it so we can get its CU list as
5286 well. */
5287 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5288 if (dwz != NULL)
5289 {
5290 if (!read_debug_names_from_section (objfile,
5291 bfd_get_filename (dwz->dwz_bfd.get ()),
5292 &dwz->debug_names, dwz_map))
5293 {
5294 warning (_("could not read '.debug_names' section from %s; skipping"),
5295 bfd_get_filename (dwz->dwz_bfd.get ()));
5296 return false;
5297 }
5298 }
5299
5300 create_cus_from_debug_names (per_bfd, *map, dwz_map);
5301
5302 if (map->tu_count != 0)
5303 {
5304 /* We can only handle a single .debug_types when we have an
5305 index. */
5306 if (per_bfd->types.size () != 1)
5307 return false;
5308
5309 dwarf2_section_info *section = &per_bfd->types[0];
5310
5311 create_signatured_type_table_from_debug_names
5312 (per_objfile, *map, section, &per_bfd->abbrev);
5313 }
5314
5315 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
5316
5317 per_bfd->debug_names_table = std::move (map);
5318 per_bfd->using_index = 1;
5319 per_bfd->quick_file_names_table =
5320 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
5321
5322 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
5323 objfiles using the same BFD. */
5324 gdb_assert (per_bfd->partial_symtabs == nullptr);
5325 per_bfd->partial_symtabs = objfile->partial_symtabs;
5326
5327 return true;
5328 }
5329
5330 /* Type used to manage iterating over all CUs looking for a symbol for
5331 .debug_names. */
5332
5333 class dw2_debug_names_iterator
5334 {
5335 public:
5336 dw2_debug_names_iterator (const mapped_debug_names &map,
5337 gdb::optional<block_enum> block_index,
5338 domain_enum domain,
5339 const char *name, dwarf2_per_objfile *per_objfile)
5340 : m_map (map), m_block_index (block_index), m_domain (domain),
5341 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5342 m_per_objfile (per_objfile)
5343 {}
5344
5345 dw2_debug_names_iterator (const mapped_debug_names &map,
5346 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
5347 : m_map (map),
5348 m_search (search),
5349 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5350 m_per_objfile (per_objfile)
5351 {}
5352
5353 dw2_debug_names_iterator (const mapped_debug_names &map,
5354 block_enum block_index, domain_enum domain,
5355 uint32_t namei, dwarf2_per_objfile *per_objfile)
5356 : m_map (map), m_block_index (block_index), m_domain (domain),
5357 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5358 m_per_objfile (per_objfile)
5359 {}
5360
5361 /* Return the next matching CU or NULL if there are no more. */
5362 dwarf2_per_cu_data *next ();
5363
5364 private:
5365 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5366 const char *name,
5367 dwarf2_per_objfile *per_objfile);
5368 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5369 uint32_t namei,
5370 dwarf2_per_objfile *per_objfile);
5371
5372 /* The internalized form of .debug_names. */
5373 const mapped_debug_names &m_map;
5374
5375 /* If set, only look for symbols that match that block. Valid values are
5376 GLOBAL_BLOCK and STATIC_BLOCK. */
5377 const gdb::optional<block_enum> m_block_index;
5378
5379 /* The kind of symbol we're looking for. */
5380 const domain_enum m_domain = UNDEF_DOMAIN;
5381 const search_domain m_search = ALL_DOMAIN;
5382
5383 /* The list of CUs from the index entry of the symbol, or NULL if
5384 not found. */
5385 const gdb_byte *m_addr;
5386
5387 dwarf2_per_objfile *m_per_objfile;
5388 };
5389
5390 const char *
5391 mapped_debug_names::namei_to_name
5392 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
5393 {
5394 const ULONGEST namei_string_offs
5395 = extract_unsigned_integer ((name_table_string_offs_reordered
5396 + namei * offset_size),
5397 offset_size,
5398 dwarf5_byte_order);
5399 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
5400 }
5401
5402 /* Find a slot in .debug_names for the object named NAME. If NAME is
5403 found, return pointer to its pool data. If NAME cannot be found,
5404 return NULL. */
5405
5406 const gdb_byte *
5407 dw2_debug_names_iterator::find_vec_in_debug_names
5408 (const mapped_debug_names &map, const char *name,
5409 dwarf2_per_objfile *per_objfile)
5410 {
5411 int (*cmp) (const char *, const char *);
5412
5413 gdb::unique_xmalloc_ptr<char> without_params;
5414 if (current_language->la_language == language_cplus
5415 || current_language->la_language == language_fortran
5416 || current_language->la_language == language_d)
5417 {
5418 /* NAME is already canonical. Drop any qualifiers as
5419 .debug_names does not contain any. */
5420
5421 if (strchr (name, '(') != NULL)
5422 {
5423 without_params = cp_remove_params (name);
5424 if (without_params != NULL)
5425 name = without_params.get ();
5426 }
5427 }
5428
5429 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5430
5431 const uint32_t full_hash = dwarf5_djb_hash (name);
5432 uint32_t namei
5433 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5434 (map.bucket_table_reordered
5435 + (full_hash % map.bucket_count)), 4,
5436 map.dwarf5_byte_order);
5437 if (namei == 0)
5438 return NULL;
5439 --namei;
5440 if (namei >= map.name_count)
5441 {
5442 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5443 "[in module %s]"),
5444 namei, map.name_count,
5445 objfile_name (per_objfile->objfile));
5446 return NULL;
5447 }
5448
5449 for (;;)
5450 {
5451 const uint32_t namei_full_hash
5452 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5453 (map.hash_table_reordered + namei), 4,
5454 map.dwarf5_byte_order);
5455 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5456 return NULL;
5457
5458 if (full_hash == namei_full_hash)
5459 {
5460 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5461
5462 #if 0 /* An expensive sanity check. */
5463 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5464 {
5465 complaint (_("Wrong .debug_names hash for string at index %u "
5466 "[in module %s]"),
5467 namei, objfile_name (dwarf2_per_objfile->objfile));
5468 return NULL;
5469 }
5470 #endif
5471
5472 if (cmp (namei_string, name) == 0)
5473 {
5474 const ULONGEST namei_entry_offs
5475 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5476 + namei * map.offset_size),
5477 map.offset_size, map.dwarf5_byte_order);
5478 return map.entry_pool + namei_entry_offs;
5479 }
5480 }
5481
5482 ++namei;
5483 if (namei >= map.name_count)
5484 return NULL;
5485 }
5486 }
5487
5488 const gdb_byte *
5489 dw2_debug_names_iterator::find_vec_in_debug_names
5490 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5491 {
5492 if (namei >= map.name_count)
5493 {
5494 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5495 "[in module %s]"),
5496 namei, map.name_count,
5497 objfile_name (per_objfile->objfile));
5498 return NULL;
5499 }
5500
5501 const ULONGEST namei_entry_offs
5502 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5503 + namei * map.offset_size),
5504 map.offset_size, map.dwarf5_byte_order);
5505 return map.entry_pool + namei_entry_offs;
5506 }
5507
5508 /* See dw2_debug_names_iterator. */
5509
5510 dwarf2_per_cu_data *
5511 dw2_debug_names_iterator::next ()
5512 {
5513 if (m_addr == NULL)
5514 return NULL;
5515
5516 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5517 struct objfile *objfile = m_per_objfile->objfile;
5518 bfd *const abfd = objfile->obfd;
5519
5520 again:
5521
5522 unsigned int bytes_read;
5523 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5524 m_addr += bytes_read;
5525 if (abbrev == 0)
5526 return NULL;
5527
5528 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5529 if (indexval_it == m_map.abbrev_map.cend ())
5530 {
5531 complaint (_("Wrong .debug_names undefined abbrev code %s "
5532 "[in module %s]"),
5533 pulongest (abbrev), objfile_name (objfile));
5534 return NULL;
5535 }
5536 const mapped_debug_names::index_val &indexval = indexval_it->second;
5537 enum class symbol_linkage {
5538 unknown,
5539 static_,
5540 extern_,
5541 } symbol_linkage_ = symbol_linkage::unknown;
5542 dwarf2_per_cu_data *per_cu = NULL;
5543 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5544 {
5545 ULONGEST ull;
5546 switch (attr.form)
5547 {
5548 case DW_FORM_implicit_const:
5549 ull = attr.implicit_const;
5550 break;
5551 case DW_FORM_flag_present:
5552 ull = 1;
5553 break;
5554 case DW_FORM_udata:
5555 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5556 m_addr += bytes_read;
5557 break;
5558 case DW_FORM_ref4:
5559 ull = read_4_bytes (abfd, m_addr);
5560 m_addr += 4;
5561 break;
5562 case DW_FORM_ref8:
5563 ull = read_8_bytes (abfd, m_addr);
5564 m_addr += 8;
5565 break;
5566 case DW_FORM_ref_sig8:
5567 ull = read_8_bytes (abfd, m_addr);
5568 m_addr += 8;
5569 break;
5570 default:
5571 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5572 dwarf_form_name (attr.form),
5573 objfile_name (objfile));
5574 return NULL;
5575 }
5576 switch (attr.dw_idx)
5577 {
5578 case DW_IDX_compile_unit:
5579 /* Don't crash on bad data. */
5580 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
5581 {
5582 complaint (_(".debug_names entry has bad CU index %s"
5583 " [in module %s]"),
5584 pulongest (ull),
5585 objfile_name (objfile));
5586 continue;
5587 }
5588 per_cu = per_bfd->get_cutu (ull);
5589 break;
5590 case DW_IDX_type_unit:
5591 /* Don't crash on bad data. */
5592 if (ull >= per_bfd->all_type_units.size ())
5593 {
5594 complaint (_(".debug_names entry has bad TU index %s"
5595 " [in module %s]"),
5596 pulongest (ull),
5597 objfile_name (objfile));
5598 continue;
5599 }
5600 per_cu = &per_bfd->get_tu (ull)->per_cu;
5601 break;
5602 case DW_IDX_die_offset:
5603 /* In a per-CU index (as opposed to a per-module index), index
5604 entries without CU attribute implicitly refer to the single CU. */
5605 if (per_cu == NULL)
5606 per_cu = per_bfd->get_cu (0);
5607 break;
5608 case DW_IDX_GNU_internal:
5609 if (!m_map.augmentation_is_gdb)
5610 break;
5611 symbol_linkage_ = symbol_linkage::static_;
5612 break;
5613 case DW_IDX_GNU_external:
5614 if (!m_map.augmentation_is_gdb)
5615 break;
5616 symbol_linkage_ = symbol_linkage::extern_;
5617 break;
5618 }
5619 }
5620
5621 /* Skip if already read in. */
5622 if (m_per_objfile->symtab_set_p (per_cu))
5623 goto again;
5624
5625 /* Check static vs global. */
5626 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5627 {
5628 const bool want_static = *m_block_index == STATIC_BLOCK;
5629 const bool symbol_is_static =
5630 symbol_linkage_ == symbol_linkage::static_;
5631 if (want_static != symbol_is_static)
5632 goto again;
5633 }
5634
5635 /* Match dw2_symtab_iter_next, symbol_kind
5636 and debug_names::psymbol_tag. */
5637 switch (m_domain)
5638 {
5639 case VAR_DOMAIN:
5640 switch (indexval.dwarf_tag)
5641 {
5642 case DW_TAG_variable:
5643 case DW_TAG_subprogram:
5644 /* Some types are also in VAR_DOMAIN. */
5645 case DW_TAG_typedef:
5646 case DW_TAG_structure_type:
5647 break;
5648 default:
5649 goto again;
5650 }
5651 break;
5652 case STRUCT_DOMAIN:
5653 switch (indexval.dwarf_tag)
5654 {
5655 case DW_TAG_typedef:
5656 case DW_TAG_structure_type:
5657 break;
5658 default:
5659 goto again;
5660 }
5661 break;
5662 case LABEL_DOMAIN:
5663 switch (indexval.dwarf_tag)
5664 {
5665 case 0:
5666 case DW_TAG_variable:
5667 break;
5668 default:
5669 goto again;
5670 }
5671 break;
5672 case MODULE_DOMAIN:
5673 switch (indexval.dwarf_tag)
5674 {
5675 case DW_TAG_module:
5676 break;
5677 default:
5678 goto again;
5679 }
5680 break;
5681 default:
5682 break;
5683 }
5684
5685 /* Match dw2_expand_symtabs_matching, symbol_kind and
5686 debug_names::psymbol_tag. */
5687 switch (m_search)
5688 {
5689 case VARIABLES_DOMAIN:
5690 switch (indexval.dwarf_tag)
5691 {
5692 case DW_TAG_variable:
5693 break;
5694 default:
5695 goto again;
5696 }
5697 break;
5698 case FUNCTIONS_DOMAIN:
5699 switch (indexval.dwarf_tag)
5700 {
5701 case DW_TAG_subprogram:
5702 break;
5703 default:
5704 goto again;
5705 }
5706 break;
5707 case TYPES_DOMAIN:
5708 switch (indexval.dwarf_tag)
5709 {
5710 case DW_TAG_typedef:
5711 case DW_TAG_structure_type:
5712 break;
5713 default:
5714 goto again;
5715 }
5716 break;
5717 case MODULES_DOMAIN:
5718 switch (indexval.dwarf_tag)
5719 {
5720 case DW_TAG_module:
5721 break;
5722 default:
5723 goto again;
5724 }
5725 default:
5726 break;
5727 }
5728
5729 return per_cu;
5730 }
5731
5732 static struct compunit_symtab *
5733 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5734 const char *name, domain_enum domain)
5735 {
5736 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5737
5738 const auto &mapp = per_objfile->per_bfd->debug_names_table;
5739 if (!mapp)
5740 {
5741 /* index is NULL if OBJF_READNOW. */
5742 return NULL;
5743 }
5744 const auto &map = *mapp;
5745
5746 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
5747
5748 struct compunit_symtab *stab_best = NULL;
5749 struct dwarf2_per_cu_data *per_cu;
5750 while ((per_cu = iter.next ()) != NULL)
5751 {
5752 struct symbol *sym, *with_opaque = NULL;
5753 compunit_symtab *stab
5754 = dw2_instantiate_symtab (per_cu, per_objfile, false);
5755 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5756 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5757
5758 sym = block_find_symbol (block, name, domain,
5759 block_find_non_opaque_type_preferred,
5760 &with_opaque);
5761
5762 /* Some caution must be observed with overloaded functions and
5763 methods, since the index will not contain any overload
5764 information (but NAME might contain it). */
5765
5766 if (sym != NULL
5767 && strcmp_iw (sym->search_name (), name) == 0)
5768 return stab;
5769 if (with_opaque != NULL
5770 && strcmp_iw (with_opaque->search_name (), name) == 0)
5771 stab_best = stab;
5772
5773 /* Keep looking through other CUs. */
5774 }
5775
5776 return stab_best;
5777 }
5778
5779 /* This dumps minimal information about .debug_names. It is called
5780 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5781 uses this to verify that .debug_names has been loaded. */
5782
5783 static void
5784 dw2_debug_names_dump (struct objfile *objfile)
5785 {
5786 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5787
5788 gdb_assert (per_objfile->per_bfd->using_index);
5789 printf_filtered (".debug_names:");
5790 if (per_objfile->per_bfd->debug_names_table)
5791 printf_filtered (" exists\n");
5792 else
5793 printf_filtered (" faked for \"readnow\"\n");
5794 printf_filtered ("\n");
5795 }
5796
5797 static void
5798 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5799 const char *func_name)
5800 {
5801 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5802
5803 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5804 if (per_objfile->per_bfd->debug_names_table)
5805 {
5806 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5807
5808 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
5809 per_objfile);
5810
5811 struct dwarf2_per_cu_data *per_cu;
5812 while ((per_cu = iter.next ()) != NULL)
5813 dw2_instantiate_symtab (per_cu, per_objfile, false);
5814 }
5815 }
5816
5817 static void
5818 dw2_debug_names_map_matching_symbols
5819 (struct objfile *objfile,
5820 const lookup_name_info &name, domain_enum domain,
5821 int global,
5822 gdb::function_view<symbol_found_callback_ftype> callback,
5823 symbol_compare_ftype *ordered_compare)
5824 {
5825 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5826
5827 /* debug_names_table is NULL if OBJF_READNOW. */
5828 if (!per_objfile->per_bfd->debug_names_table)
5829 return;
5830
5831 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5832 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5833
5834 const char *match_name = name.ada ().lookup_name ().c_str ();
5835 auto matcher = [&] (const char *symname)
5836 {
5837 if (ordered_compare == nullptr)
5838 return true;
5839 return ordered_compare (symname, match_name) == 0;
5840 };
5841
5842 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5843 [&] (offset_type namei)
5844 {
5845 /* The name was matched, now expand corresponding CUs that were
5846 marked. */
5847 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
5848 per_objfile);
5849
5850 struct dwarf2_per_cu_data *per_cu;
5851 while ((per_cu = iter.next ()) != NULL)
5852 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5853 nullptr);
5854 return true;
5855 }, per_objfile);
5856
5857 /* It's a shame we couldn't do this inside the
5858 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5859 that have already been expanded. Instead, this loop matches what
5860 the psymtab code does. */
5861 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5862 {
5863 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
5864 if (symtab != nullptr)
5865 {
5866 const struct block *block
5867 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5868 if (!iterate_over_symbols_terminated (block, name,
5869 domain, callback))
5870 break;
5871 }
5872 }
5873 }
5874
5875 static void
5876 dw2_debug_names_expand_symtabs_matching
5877 (struct objfile *objfile,
5878 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5879 const lookup_name_info *lookup_name,
5880 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5881 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5882 enum search_domain kind)
5883 {
5884 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5885
5886 /* debug_names_table is NULL if OBJF_READNOW. */
5887 if (!per_objfile->per_bfd->debug_names_table)
5888 return;
5889
5890 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5891
5892 if (symbol_matcher == NULL && lookup_name == NULL)
5893 {
5894 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5895 {
5896 QUIT;
5897
5898 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5899 expansion_notify);
5900 }
5901 return;
5902 }
5903
5904 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5905
5906 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5907 symbol_matcher,
5908 kind, [&] (offset_type namei)
5909 {
5910 /* The name was matched, now expand corresponding CUs that were
5911 marked. */
5912 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
5913
5914 struct dwarf2_per_cu_data *per_cu;
5915 while ((per_cu = iter.next ()) != NULL)
5916 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5917 expansion_notify);
5918 return true;
5919 }, per_objfile);
5920 }
5921
5922 const struct quick_symbol_functions dwarf2_debug_names_functions =
5923 {
5924 dw2_has_symbols,
5925 dw2_find_last_source_symtab,
5926 dw2_forget_cached_source_info,
5927 dw2_map_symtabs_matching_filename,
5928 dw2_debug_names_lookup_symbol,
5929 NULL,
5930 dw2_print_stats,
5931 dw2_debug_names_dump,
5932 dw2_debug_names_expand_symtabs_for_function,
5933 dw2_expand_all_symtabs,
5934 dw2_expand_symtabs_with_fullname,
5935 dw2_debug_names_map_matching_symbols,
5936 dw2_debug_names_expand_symtabs_matching,
5937 dw2_find_pc_sect_compunit_symtab,
5938 NULL,
5939 dw2_map_symbol_filenames
5940 };
5941
5942 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5943 to either a dwarf2_per_bfd or dwz_file object. */
5944
5945 template <typename T>
5946 static gdb::array_view<const gdb_byte>
5947 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5948 {
5949 dwarf2_section_info *section = &section_owner->gdb_index;
5950
5951 if (section->empty ())
5952 return {};
5953
5954 /* Older elfutils strip versions could keep the section in the main
5955 executable while splitting it for the separate debug info file. */
5956 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5957 return {};
5958
5959 section->read (obj);
5960
5961 /* dwarf2_section_info::size is a bfd_size_type, while
5962 gdb::array_view works with size_t. On 32-bit hosts, with
5963 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5964 is 32-bit. So we need an explicit narrowing conversion here.
5965 This is fine, because it's impossible to allocate or mmap an
5966 array/buffer larger than what size_t can represent. */
5967 return gdb::make_array_view (section->buffer, section->size);
5968 }
5969
5970 /* Lookup the index cache for the contents of the index associated to
5971 DWARF2_OBJ. */
5972
5973 static gdb::array_view<const gdb_byte>
5974 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5975 {
5976 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5977 if (build_id == nullptr)
5978 return {};
5979
5980 return global_index_cache.lookup_gdb_index (build_id,
5981 &dwarf2_per_bfd->index_cache_res);
5982 }
5983
5984 /* Same as the above, but for DWZ. */
5985
5986 static gdb::array_view<const gdb_byte>
5987 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5988 {
5989 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5990 if (build_id == nullptr)
5991 return {};
5992
5993 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5994 }
5995
5996 /* See symfile.h. */
5997
5998 bool
5999 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6000 {
6001 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6002 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6003
6004 /* If we're about to read full symbols, don't bother with the
6005 indices. In this case we also don't care if some other debug
6006 format is making psymtabs, because they are all about to be
6007 expanded anyway. */
6008 if ((objfile->flags & OBJF_READNOW))
6009 {
6010 /* When using READNOW, the using_index flag (set below) indicates that
6011 PER_BFD was already initialized, when we loaded some other objfile. */
6012 if (per_bfd->using_index)
6013 {
6014 *index_kind = dw_index_kind::GDB_INDEX;
6015 per_objfile->resize_symtabs ();
6016 return true;
6017 }
6018
6019 per_bfd->using_index = 1;
6020 create_all_comp_units (per_objfile);
6021 create_all_type_units (per_objfile);
6022 per_bfd->quick_file_names_table
6023 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
6024 per_objfile->resize_symtabs ();
6025
6026 for (int i = 0; i < (per_bfd->all_comp_units.size ()
6027 + per_bfd->all_type_units.size ()); ++i)
6028 {
6029 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
6030
6031 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6032 struct dwarf2_per_cu_quick_data);
6033 }
6034
6035 /* Return 1 so that gdb sees the "quick" functions. However,
6036 these functions will be no-ops because we will have expanded
6037 all symtabs. */
6038 *index_kind = dw_index_kind::GDB_INDEX;
6039 return true;
6040 }
6041
6042 /* Was a debug names index already read when we processed an objfile sharing
6043 PER_BFD? */
6044 if (per_bfd->debug_names_table != nullptr)
6045 {
6046 *index_kind = dw_index_kind::DEBUG_NAMES;
6047 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
6048 per_objfile->resize_symtabs ();
6049 return true;
6050 }
6051
6052 /* Was a GDB index already read when we processed an objfile sharing
6053 PER_BFD? */
6054 if (per_bfd->index_table != nullptr)
6055 {
6056 *index_kind = dw_index_kind::GDB_INDEX;
6057 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
6058 per_objfile->resize_symtabs ();
6059 return true;
6060 }
6061
6062 /* There might already be partial symtabs built for this BFD. This happens
6063 when loading the same binary twice with the index-cache enabled. If so,
6064 don't try to read an index. The objfile / per_objfile initialization will
6065 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
6066 code path. */
6067 if (per_bfd->partial_symtabs != nullptr)
6068 return false;
6069
6070 if (dwarf2_read_debug_names (per_objfile))
6071 {
6072 *index_kind = dw_index_kind::DEBUG_NAMES;
6073 per_objfile->resize_symtabs ();
6074 return true;
6075 }
6076
6077 if (dwarf2_read_gdb_index (per_objfile,
6078 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
6079 get_gdb_index_contents_from_section<dwz_file>))
6080 {
6081 *index_kind = dw_index_kind::GDB_INDEX;
6082 per_objfile->resize_symtabs ();
6083 return true;
6084 }
6085
6086 /* ... otherwise, try to find the index in the index cache. */
6087 if (dwarf2_read_gdb_index (per_objfile,
6088 get_gdb_index_contents_from_cache,
6089 get_gdb_index_contents_from_cache_dwz))
6090 {
6091 global_index_cache.hit ();
6092 *index_kind = dw_index_kind::GDB_INDEX;
6093 per_objfile->resize_symtabs ();
6094 return true;
6095 }
6096
6097 global_index_cache.miss ();
6098 return false;
6099 }
6100
6101 \f
6102
6103 /* Build a partial symbol table. */
6104
6105 void
6106 dwarf2_build_psymtabs (struct objfile *objfile)
6107 {
6108 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6109 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6110
6111 if (per_bfd->partial_symtabs != nullptr)
6112 {
6113 /* Partial symbols were already read, so now we can simply
6114 attach them. */
6115 objfile->partial_symtabs = per_bfd->partial_symtabs;
6116 per_objfile->resize_symtabs ();
6117 return;
6118 }
6119
6120 init_psymbol_list (objfile, 1024);
6121
6122 try
6123 {
6124 /* This isn't really ideal: all the data we allocate on the
6125 objfile's obstack is still uselessly kept around. However,
6126 freeing it seems unsafe. */
6127 psymtab_discarder psymtabs (objfile);
6128 dwarf2_build_psymtabs_hard (per_objfile);
6129 psymtabs.keep ();
6130
6131 per_objfile->resize_symtabs ();
6132
6133 /* (maybe) store an index in the cache. */
6134 global_index_cache.store (per_objfile);
6135 }
6136 catch (const gdb_exception_error &except)
6137 {
6138 exception_print (gdb_stderr, except);
6139 }
6140
6141 /* Finish by setting the local reference to partial symtabs, so that
6142 we don't try to read them again if reading another objfile with the same
6143 BFD. If we can't in fact share, this won't make a difference anyway as
6144 the dwarf2_per_bfd object won't be shared. */
6145 per_bfd->partial_symtabs = objfile->partial_symtabs;
6146 }
6147
6148 /* Find the base address of the compilation unit for range lists and
6149 location lists. It will normally be specified by DW_AT_low_pc.
6150 In DWARF-3 draft 4, the base address could be overridden by
6151 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6152 compilation units with discontinuous ranges. */
6153
6154 static void
6155 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6156 {
6157 struct attribute *attr;
6158
6159 cu->base_address.reset ();
6160
6161 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6162 if (attr != nullptr)
6163 cu->base_address = attr->value_as_address ();
6164 else
6165 {
6166 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6167 if (attr != nullptr)
6168 cu->base_address = attr->value_as_address ();
6169 }
6170 }
6171
6172 /* Helper function that returns the proper abbrev section for
6173 THIS_CU. */
6174
6175 static struct dwarf2_section_info *
6176 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6177 {
6178 struct dwarf2_section_info *abbrev;
6179 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6180
6181 if (this_cu->is_dwz)
6182 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
6183 else
6184 abbrev = &per_bfd->abbrev;
6185
6186 return abbrev;
6187 }
6188
6189 /* Fetch the abbreviation table offset from a comp or type unit header. */
6190
6191 static sect_offset
6192 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
6193 struct dwarf2_section_info *section,
6194 sect_offset sect_off)
6195 {
6196 bfd *abfd = section->get_bfd_owner ();
6197 const gdb_byte *info_ptr;
6198 unsigned int initial_length_size, offset_size;
6199 uint16_t version;
6200
6201 section->read (per_objfile->objfile);
6202 info_ptr = section->buffer + to_underlying (sect_off);
6203 read_initial_length (abfd, info_ptr, &initial_length_size);
6204 offset_size = initial_length_size == 4 ? 4 : 8;
6205 info_ptr += initial_length_size;
6206
6207 version = read_2_bytes (abfd, info_ptr);
6208 info_ptr += 2;
6209 if (version >= 5)
6210 {
6211 /* Skip unit type and address size. */
6212 info_ptr += 2;
6213 }
6214
6215 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6216 }
6217
6218 /* A partial symtab that is used only for include files. */
6219 struct dwarf2_include_psymtab : public partial_symtab
6220 {
6221 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6222 : partial_symtab (filename, objfile)
6223 {
6224 }
6225
6226 void read_symtab (struct objfile *objfile) override
6227 {
6228 /* It's an include file, no symbols to read for it.
6229 Everything is in the includer symtab. */
6230
6231 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6232 expansion of the includer psymtab. We use the dependencies[0] field to
6233 model the includer. But if we go the regular route of calling
6234 expand_psymtab here, and having expand_psymtab call expand_dependencies
6235 to expand the includer, we'll only use expand_psymtab on the includer
6236 (making it a non-toplevel psymtab), while if we expand the includer via
6237 another path, we'll use read_symtab (making it a toplevel psymtab).
6238 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6239 psymtab, and trigger read_symtab on the includer here directly. */
6240 includer ()->read_symtab (objfile);
6241 }
6242
6243 void expand_psymtab (struct objfile *objfile) override
6244 {
6245 /* This is not called by read_symtab, and should not be called by any
6246 expand_dependencies. */
6247 gdb_assert (false);
6248 }
6249
6250 bool readin_p (struct objfile *objfile) const override
6251 {
6252 return includer ()->readin_p (objfile);
6253 }
6254
6255 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6256 {
6257 return nullptr;
6258 }
6259
6260 private:
6261 partial_symtab *includer () const
6262 {
6263 /* An include psymtab has exactly one dependency: the psymtab that
6264 includes it. */
6265 gdb_assert (this->number_of_dependencies == 1);
6266 return this->dependencies[0];
6267 }
6268 };
6269
6270 /* Allocate a new partial symtab for file named NAME and mark this new
6271 partial symtab as being an include of PST. */
6272
6273 static void
6274 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6275 struct objfile *objfile)
6276 {
6277 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6278
6279 if (!IS_ABSOLUTE_PATH (subpst->filename))
6280 subpst->dirname = pst->dirname;
6281
6282 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6283 subpst->dependencies[0] = pst;
6284 subpst->number_of_dependencies = 1;
6285 }
6286
6287 /* Read the Line Number Program data and extract the list of files
6288 included by the source file represented by PST. Build an include
6289 partial symtab for each of these included files. */
6290
6291 static void
6292 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6293 struct die_info *die,
6294 dwarf2_psymtab *pst)
6295 {
6296 line_header_up lh;
6297 struct attribute *attr;
6298
6299 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6300 if (attr != nullptr)
6301 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6302 if (lh == NULL)
6303 return; /* No linetable, so no includes. */
6304
6305 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6306 that we pass in the raw text_low here; that is ok because we're
6307 only decoding the line table to make include partial symtabs, and
6308 so the addresses aren't really used. */
6309 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6310 pst->raw_text_low (), 1);
6311 }
6312
6313 static hashval_t
6314 hash_signatured_type (const void *item)
6315 {
6316 const struct signatured_type *sig_type
6317 = (const struct signatured_type *) item;
6318
6319 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6320 return sig_type->signature;
6321 }
6322
6323 static int
6324 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6325 {
6326 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6327 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6328
6329 return lhs->signature == rhs->signature;
6330 }
6331
6332 /* Allocate a hash table for signatured types. */
6333
6334 static htab_up
6335 allocate_signatured_type_table ()
6336 {
6337 return htab_up (htab_create_alloc (41,
6338 hash_signatured_type,
6339 eq_signatured_type,
6340 NULL, xcalloc, xfree));
6341 }
6342
6343 /* A helper function to add a signatured type CU to a table. */
6344
6345 static int
6346 add_signatured_type_cu_to_table (void **slot, void *datum)
6347 {
6348 struct signatured_type *sigt = (struct signatured_type *) *slot;
6349 std::vector<signatured_type *> *all_type_units
6350 = (std::vector<signatured_type *> *) datum;
6351
6352 all_type_units->push_back (sigt);
6353
6354 return 1;
6355 }
6356
6357 /* A helper for create_debug_types_hash_table. Read types from SECTION
6358 and fill them into TYPES_HTAB. It will process only type units,
6359 therefore DW_UT_type. */
6360
6361 static void
6362 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
6363 struct dwo_file *dwo_file,
6364 dwarf2_section_info *section, htab_up &types_htab,
6365 rcuh_kind section_kind)
6366 {
6367 struct objfile *objfile = per_objfile->objfile;
6368 struct dwarf2_section_info *abbrev_section;
6369 bfd *abfd;
6370 const gdb_byte *info_ptr, *end_ptr;
6371
6372 abbrev_section = (dwo_file != NULL
6373 ? &dwo_file->sections.abbrev
6374 : &per_objfile->per_bfd->abbrev);
6375
6376 if (dwarf_read_debug)
6377 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6378 section->get_name (),
6379 abbrev_section->get_file_name ());
6380
6381 section->read (objfile);
6382 info_ptr = section->buffer;
6383
6384 if (info_ptr == NULL)
6385 return;
6386
6387 /* We can't set abfd until now because the section may be empty or
6388 not present, in which case the bfd is unknown. */
6389 abfd = section->get_bfd_owner ();
6390
6391 /* We don't use cutu_reader here because we don't need to read
6392 any dies: the signature is in the header. */
6393
6394 end_ptr = info_ptr + section->size;
6395 while (info_ptr < end_ptr)
6396 {
6397 struct signatured_type *sig_type;
6398 struct dwo_unit *dwo_tu;
6399 void **slot;
6400 const gdb_byte *ptr = info_ptr;
6401 struct comp_unit_head header;
6402 unsigned int length;
6403
6404 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6405
6406 /* Initialize it due to a false compiler warning. */
6407 header.signature = -1;
6408 header.type_cu_offset_in_tu = (cu_offset) -1;
6409
6410 /* We need to read the type's signature in order to build the hash
6411 table, but we don't need anything else just yet. */
6412
6413 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
6414 abbrev_section, ptr, section_kind);
6415
6416 length = header.get_length ();
6417
6418 /* Skip dummy type units. */
6419 if (ptr >= info_ptr + length
6420 || peek_abbrev_code (abfd, ptr) == 0
6421 || (header.unit_type != DW_UT_type
6422 && header.unit_type != DW_UT_split_type))
6423 {
6424 info_ptr += length;
6425 continue;
6426 }
6427
6428 if (types_htab == NULL)
6429 {
6430 if (dwo_file)
6431 types_htab = allocate_dwo_unit_table ();
6432 else
6433 types_htab = allocate_signatured_type_table ();
6434 }
6435
6436 if (dwo_file)
6437 {
6438 sig_type = NULL;
6439 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
6440 dwo_tu->dwo_file = dwo_file;
6441 dwo_tu->signature = header.signature;
6442 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6443 dwo_tu->section = section;
6444 dwo_tu->sect_off = sect_off;
6445 dwo_tu->length = length;
6446 }
6447 else
6448 {
6449 /* N.B.: type_offset is not usable if this type uses a DWO file.
6450 The real type_offset is in the DWO file. */
6451 dwo_tu = NULL;
6452 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6453 sig_type->signature = header.signature;
6454 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6455 sig_type->per_cu.is_debug_types = 1;
6456 sig_type->per_cu.section = section;
6457 sig_type->per_cu.sect_off = sect_off;
6458 sig_type->per_cu.length = length;
6459 }
6460
6461 slot = htab_find_slot (types_htab.get (),
6462 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6463 INSERT);
6464 gdb_assert (slot != NULL);
6465 if (*slot != NULL)
6466 {
6467 sect_offset dup_sect_off;
6468
6469 if (dwo_file)
6470 {
6471 const struct dwo_unit *dup_tu
6472 = (const struct dwo_unit *) *slot;
6473
6474 dup_sect_off = dup_tu->sect_off;
6475 }
6476 else
6477 {
6478 const struct signatured_type *dup_tu
6479 = (const struct signatured_type *) *slot;
6480
6481 dup_sect_off = dup_tu->per_cu.sect_off;
6482 }
6483
6484 complaint (_("debug type entry at offset %s is duplicate to"
6485 " the entry at offset %s, signature %s"),
6486 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6487 hex_string (header.signature));
6488 }
6489 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6490
6491 if (dwarf_read_debug > 1)
6492 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6493 sect_offset_str (sect_off),
6494 hex_string (header.signature));
6495
6496 info_ptr += length;
6497 }
6498 }
6499
6500 /* Create the hash table of all entries in the .debug_types
6501 (or .debug_types.dwo) section(s).
6502 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6503 otherwise it is NULL.
6504
6505 The result is a pointer to the hash table or NULL if there are no types.
6506
6507 Note: This function processes DWO files only, not DWP files. */
6508
6509 static void
6510 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
6511 struct dwo_file *dwo_file,
6512 gdb::array_view<dwarf2_section_info> type_sections,
6513 htab_up &types_htab)
6514 {
6515 for (dwarf2_section_info &section : type_sections)
6516 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6517 rcuh_kind::TYPE);
6518 }
6519
6520 /* Create the hash table of all entries in the .debug_types section,
6521 and initialize all_type_units.
6522 The result is zero if there is an error (e.g. missing .debug_types section),
6523 otherwise non-zero. */
6524
6525 static int
6526 create_all_type_units (dwarf2_per_objfile *per_objfile)
6527 {
6528 htab_up types_htab;
6529
6530 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6531 types_htab, rcuh_kind::COMPILE);
6532 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6533 types_htab);
6534 if (types_htab == NULL)
6535 {
6536 per_objfile->per_bfd->signatured_types = NULL;
6537 return 0;
6538 }
6539
6540 per_objfile->per_bfd->signatured_types = std::move (types_htab);
6541
6542 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6543 per_objfile->per_bfd->all_type_units.reserve
6544 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
6545
6546 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
6547 add_signatured_type_cu_to_table,
6548 &per_objfile->per_bfd->all_type_units);
6549
6550 return 1;
6551 }
6552
6553 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6554 If SLOT is non-NULL, it is the entry to use in the hash table.
6555 Otherwise we find one. */
6556
6557 static struct signatured_type *
6558 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6559 {
6560 if (per_objfile->per_bfd->all_type_units.size ()
6561 == per_objfile->per_bfd->all_type_units.capacity ())
6562 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6563
6564 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6565
6566 per_objfile->resize_symtabs ();
6567
6568 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6569 sig_type->signature = sig;
6570 sig_type->per_cu.is_debug_types = 1;
6571 if (per_objfile->per_bfd->using_index)
6572 {
6573 sig_type->per_cu.v.quick =
6574 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6575 struct dwarf2_per_cu_quick_data);
6576 }
6577
6578 if (slot == NULL)
6579 {
6580 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6581 sig_type, INSERT);
6582 }
6583 gdb_assert (*slot == NULL);
6584 *slot = sig_type;
6585 /* The rest of sig_type must be filled in by the caller. */
6586 return sig_type;
6587 }
6588
6589 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6590 Fill in SIG_ENTRY with DWO_ENTRY. */
6591
6592 static void
6593 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
6594 struct signatured_type *sig_entry,
6595 struct dwo_unit *dwo_entry)
6596 {
6597 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6598
6599 /* Make sure we're not clobbering something we don't expect to. */
6600 gdb_assert (! sig_entry->per_cu.queued);
6601 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
6602 if (per_bfd->using_index)
6603 {
6604 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6605 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6606 }
6607 else
6608 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6609 gdb_assert (sig_entry->signature == dwo_entry->signature);
6610 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6611 gdb_assert (sig_entry->type_unit_group == NULL);
6612 gdb_assert (sig_entry->dwo_unit == NULL);
6613
6614 sig_entry->per_cu.section = dwo_entry->section;
6615 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6616 sig_entry->per_cu.length = dwo_entry->length;
6617 sig_entry->per_cu.reading_dwo_directly = 1;
6618 sig_entry->per_cu.per_bfd = per_bfd;
6619 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6620 sig_entry->dwo_unit = dwo_entry;
6621 }
6622
6623 /* Subroutine of lookup_signatured_type.
6624 If we haven't read the TU yet, create the signatured_type data structure
6625 for a TU to be read in directly from a DWO file, bypassing the stub.
6626 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6627 using .gdb_index, then when reading a CU we want to stay in the DWO file
6628 containing that CU. Otherwise we could end up reading several other DWO
6629 files (due to comdat folding) to process the transitive closure of all the
6630 mentioned TUs, and that can be slow. The current DWO file will have every
6631 type signature that it needs.
6632 We only do this for .gdb_index because in the psymtab case we already have
6633 to read all the DWOs to build the type unit groups. */
6634
6635 static struct signatured_type *
6636 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6637 {
6638 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6639 struct dwo_file *dwo_file;
6640 struct dwo_unit find_dwo_entry, *dwo_entry;
6641 struct signatured_type find_sig_entry, *sig_entry;
6642 void **slot;
6643
6644 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6645
6646 /* If TU skeletons have been removed then we may not have read in any
6647 TUs yet. */
6648 if (per_objfile->per_bfd->signatured_types == NULL)
6649 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6650
6651 /* We only ever need to read in one copy of a signatured type.
6652 Use the global signatured_types array to do our own comdat-folding
6653 of types. If this is the first time we're reading this TU, and
6654 the TU has an entry in .gdb_index, replace the recorded data from
6655 .gdb_index with this TU. */
6656
6657 find_sig_entry.signature = sig;
6658 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6659 &find_sig_entry, INSERT);
6660 sig_entry = (struct signatured_type *) *slot;
6661
6662 /* We can get here with the TU already read, *or* in the process of being
6663 read. Don't reassign the global entry to point to this DWO if that's
6664 the case. Also note that if the TU is already being read, it may not
6665 have come from a DWO, the program may be a mix of Fission-compiled
6666 code and non-Fission-compiled code. */
6667
6668 /* Have we already tried to read this TU?
6669 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6670 needn't exist in the global table yet). */
6671 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6672 return sig_entry;
6673
6674 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6675 dwo_unit of the TU itself. */
6676 dwo_file = cu->dwo_unit->dwo_file;
6677
6678 /* Ok, this is the first time we're reading this TU. */
6679 if (dwo_file->tus == NULL)
6680 return NULL;
6681 find_dwo_entry.signature = sig;
6682 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6683 &find_dwo_entry);
6684 if (dwo_entry == NULL)
6685 return NULL;
6686
6687 /* If the global table doesn't have an entry for this TU, add one. */
6688 if (sig_entry == NULL)
6689 sig_entry = add_type_unit (per_objfile, sig, slot);
6690
6691 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6692 sig_entry->per_cu.tu_read = 1;
6693 return sig_entry;
6694 }
6695
6696 /* Subroutine of lookup_signatured_type.
6697 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6698 then try the DWP file. If the TU stub (skeleton) has been removed then
6699 it won't be in .gdb_index. */
6700
6701 static struct signatured_type *
6702 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6703 {
6704 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6705 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6706 struct dwo_unit *dwo_entry;
6707 struct signatured_type find_sig_entry, *sig_entry;
6708 void **slot;
6709
6710 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6711 gdb_assert (dwp_file != NULL);
6712
6713 /* If TU skeletons have been removed then we may not have read in any
6714 TUs yet. */
6715 if (per_objfile->per_bfd->signatured_types == NULL)
6716 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6717
6718 find_sig_entry.signature = sig;
6719 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6720 &find_sig_entry, INSERT);
6721 sig_entry = (struct signatured_type *) *slot;
6722
6723 /* Have we already tried to read this TU?
6724 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6725 needn't exist in the global table yet). */
6726 if (sig_entry != NULL)
6727 return sig_entry;
6728
6729 if (dwp_file->tus == NULL)
6730 return NULL;
6731 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6732 1 /* is_debug_types */);
6733 if (dwo_entry == NULL)
6734 return NULL;
6735
6736 sig_entry = add_type_unit (per_objfile, sig, slot);
6737 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6738
6739 return sig_entry;
6740 }
6741
6742 /* Lookup a signature based type for DW_FORM_ref_sig8.
6743 Returns NULL if signature SIG is not present in the table.
6744 It is up to the caller to complain about this. */
6745
6746 static struct signatured_type *
6747 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6748 {
6749 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6750
6751 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6752 {
6753 /* We're in a DWO/DWP file, and we're using .gdb_index.
6754 These cases require special processing. */
6755 if (get_dwp_file (per_objfile) == NULL)
6756 return lookup_dwo_signatured_type (cu, sig);
6757 else
6758 return lookup_dwp_signatured_type (cu, sig);
6759 }
6760 else
6761 {
6762 struct signatured_type find_entry, *entry;
6763
6764 if (per_objfile->per_bfd->signatured_types == NULL)
6765 return NULL;
6766 find_entry.signature = sig;
6767 entry = ((struct signatured_type *)
6768 htab_find (per_objfile->per_bfd->signatured_types.get (),
6769 &find_entry));
6770 return entry;
6771 }
6772 }
6773
6774 /* Low level DIE reading support. */
6775
6776 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6777
6778 static void
6779 init_cu_die_reader (struct die_reader_specs *reader,
6780 struct dwarf2_cu *cu,
6781 struct dwarf2_section_info *section,
6782 struct dwo_file *dwo_file,
6783 struct abbrev_table *abbrev_table)
6784 {
6785 gdb_assert (section->readin && section->buffer != NULL);
6786 reader->abfd = section->get_bfd_owner ();
6787 reader->cu = cu;
6788 reader->dwo_file = dwo_file;
6789 reader->die_section = section;
6790 reader->buffer = section->buffer;
6791 reader->buffer_end = section->buffer + section->size;
6792 reader->abbrev_table = abbrev_table;
6793 }
6794
6795 /* Subroutine of cutu_reader to simplify it.
6796 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6797 There's just a lot of work to do, and cutu_reader is big enough
6798 already.
6799
6800 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6801 from it to the DIE in the DWO. If NULL we are skipping the stub.
6802 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6803 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6804 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6805 STUB_COMP_DIR may be non-NULL.
6806 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6807 are filled in with the info of the DIE from the DWO file.
6808 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6809 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6810 kept around for at least as long as *RESULT_READER.
6811
6812 The result is non-zero if a valid (non-dummy) DIE was found. */
6813
6814 static int
6815 read_cutu_die_from_dwo (dwarf2_cu *cu,
6816 struct dwo_unit *dwo_unit,
6817 struct die_info *stub_comp_unit_die,
6818 const char *stub_comp_dir,
6819 struct die_reader_specs *result_reader,
6820 const gdb_byte **result_info_ptr,
6821 struct die_info **result_comp_unit_die,
6822 abbrev_table_up *result_dwo_abbrev_table)
6823 {
6824 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6825 dwarf2_per_cu_data *per_cu = cu->per_cu;
6826 struct objfile *objfile = per_objfile->objfile;
6827 bfd *abfd;
6828 const gdb_byte *begin_info_ptr, *info_ptr;
6829 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6830 int i,num_extra_attrs;
6831 struct dwarf2_section_info *dwo_abbrev_section;
6832 struct die_info *comp_unit_die;
6833
6834 /* At most one of these may be provided. */
6835 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6836
6837 /* These attributes aren't processed until later:
6838 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6839 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6840 referenced later. However, these attributes are found in the stub
6841 which we won't have later. In order to not impose this complication
6842 on the rest of the code, we read them here and copy them to the
6843 DWO CU/TU die. */
6844
6845 stmt_list = NULL;
6846 low_pc = NULL;
6847 high_pc = NULL;
6848 ranges = NULL;
6849 comp_dir = NULL;
6850
6851 if (stub_comp_unit_die != NULL)
6852 {
6853 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6854 DWO file. */
6855 if (!per_cu->is_debug_types)
6856 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6857 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6858 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6859 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6860 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6861
6862 cu->addr_base = stub_comp_unit_die->addr_base ();
6863
6864 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6865 here (if needed). We need the value before we can process
6866 DW_AT_ranges. */
6867 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6868 }
6869 else if (stub_comp_dir != NULL)
6870 {
6871 /* Reconstruct the comp_dir attribute to simplify the code below. */
6872 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6873 comp_dir->name = DW_AT_comp_dir;
6874 comp_dir->form = DW_FORM_string;
6875 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6876 DW_STRING (comp_dir) = stub_comp_dir;
6877 }
6878
6879 /* Set up for reading the DWO CU/TU. */
6880 cu->dwo_unit = dwo_unit;
6881 dwarf2_section_info *section = dwo_unit->section;
6882 section->read (objfile);
6883 abfd = section->get_bfd_owner ();
6884 begin_info_ptr = info_ptr = (section->buffer
6885 + to_underlying (dwo_unit->sect_off));
6886 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6887
6888 if (per_cu->is_debug_types)
6889 {
6890 signatured_type *sig_type = (struct signatured_type *) per_cu;
6891
6892 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6893 section, dwo_abbrev_section,
6894 info_ptr, rcuh_kind::TYPE);
6895 /* This is not an assert because it can be caused by bad debug info. */
6896 if (sig_type->signature != cu->header.signature)
6897 {
6898 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6899 " TU at offset %s [in module %s]"),
6900 hex_string (sig_type->signature),
6901 hex_string (cu->header.signature),
6902 sect_offset_str (dwo_unit->sect_off),
6903 bfd_get_filename (abfd));
6904 }
6905 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6906 /* For DWOs coming from DWP files, we don't know the CU length
6907 nor the type's offset in the TU until now. */
6908 dwo_unit->length = cu->header.get_length ();
6909 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6910
6911 /* Establish the type offset that can be used to lookup the type.
6912 For DWO files, we don't know it until now. */
6913 sig_type->type_offset_in_section
6914 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6915 }
6916 else
6917 {
6918 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6919 section, dwo_abbrev_section,
6920 info_ptr, rcuh_kind::COMPILE);
6921 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6922 /* For DWOs coming from DWP files, we don't know the CU length
6923 until now. */
6924 dwo_unit->length = cu->header.get_length ();
6925 }
6926
6927 *result_dwo_abbrev_table
6928 = abbrev_table::read (objfile, dwo_abbrev_section,
6929 cu->header.abbrev_sect_off);
6930 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6931 result_dwo_abbrev_table->get ());
6932
6933 /* Read in the die, but leave space to copy over the attributes
6934 from the stub. This has the benefit of simplifying the rest of
6935 the code - all the work to maintain the illusion of a single
6936 DW_TAG_{compile,type}_unit DIE is done here. */
6937 num_extra_attrs = ((stmt_list != NULL)
6938 + (low_pc != NULL)
6939 + (high_pc != NULL)
6940 + (ranges != NULL)
6941 + (comp_dir != NULL));
6942 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6943 num_extra_attrs);
6944
6945 /* Copy over the attributes from the stub to the DIE we just read in. */
6946 comp_unit_die = *result_comp_unit_die;
6947 i = comp_unit_die->num_attrs;
6948 if (stmt_list != NULL)
6949 comp_unit_die->attrs[i++] = *stmt_list;
6950 if (low_pc != NULL)
6951 comp_unit_die->attrs[i++] = *low_pc;
6952 if (high_pc != NULL)
6953 comp_unit_die->attrs[i++] = *high_pc;
6954 if (ranges != NULL)
6955 comp_unit_die->attrs[i++] = *ranges;
6956 if (comp_dir != NULL)
6957 comp_unit_die->attrs[i++] = *comp_dir;
6958 comp_unit_die->num_attrs += num_extra_attrs;
6959
6960 if (dwarf_die_debug)
6961 {
6962 fprintf_unfiltered (gdb_stdlog,
6963 "Read die from %s@0x%x of %s:\n",
6964 section->get_name (),
6965 (unsigned) (begin_info_ptr - section->buffer),
6966 bfd_get_filename (abfd));
6967 dump_die (comp_unit_die, dwarf_die_debug);
6968 }
6969
6970 /* Skip dummy compilation units. */
6971 if (info_ptr >= begin_info_ptr + dwo_unit->length
6972 || peek_abbrev_code (abfd, info_ptr) == 0)
6973 return 0;
6974
6975 *result_info_ptr = info_ptr;
6976 return 1;
6977 }
6978
6979 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6980 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6981 signature is part of the header. */
6982 static gdb::optional<ULONGEST>
6983 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6984 {
6985 if (cu->header.version >= 5)
6986 return cu->header.signature;
6987 struct attribute *attr;
6988 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6989 if (attr == nullptr)
6990 return gdb::optional<ULONGEST> ();
6991 return DW_UNSND (attr);
6992 }
6993
6994 /* Subroutine of cutu_reader to simplify it.
6995 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6996 Returns NULL if the specified DWO unit cannot be found. */
6997
6998 static struct dwo_unit *
6999 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
7000 {
7001 dwarf2_per_cu_data *per_cu = cu->per_cu;
7002 struct dwo_unit *dwo_unit;
7003 const char *comp_dir;
7004
7005 gdb_assert (cu != NULL);
7006
7007 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7008 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7009 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7010
7011 if (per_cu->is_debug_types)
7012 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
7013 else
7014 {
7015 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7016
7017 if (!signature.has_value ())
7018 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7019 " [in module %s]"),
7020 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
7021
7022 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
7023 }
7024
7025 return dwo_unit;
7026 }
7027
7028 /* Subroutine of cutu_reader to simplify it.
7029 See it for a description of the parameters.
7030 Read a TU directly from a DWO file, bypassing the stub. */
7031
7032 void
7033 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
7034 dwarf2_per_objfile *per_objfile,
7035 dwarf2_cu *existing_cu)
7036 {
7037 struct signatured_type *sig_type;
7038
7039 /* Verify we can do the following downcast, and that we have the
7040 data we need. */
7041 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7042 sig_type = (struct signatured_type *) this_cu;
7043 gdb_assert (sig_type->dwo_unit != NULL);
7044
7045 dwarf2_cu *cu;
7046
7047 if (existing_cu != nullptr)
7048 {
7049 cu = existing_cu;
7050 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
7051 /* There's no need to do the rereading_dwo_cu handling that
7052 cutu_reader does since we don't read the stub. */
7053 }
7054 else
7055 {
7056 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7057 in per_objfile yet. */
7058 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7059 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7060 cu = m_new_cu.get ();
7061 }
7062
7063 /* A future optimization, if needed, would be to use an existing
7064 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7065 could share abbrev tables. */
7066
7067 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
7068 NULL /* stub_comp_unit_die */,
7069 sig_type->dwo_unit->dwo_file->comp_dir,
7070 this, &info_ptr,
7071 &comp_unit_die,
7072 &m_dwo_abbrev_table) == 0)
7073 {
7074 /* Dummy die. */
7075 dummy_p = true;
7076 }
7077 }
7078
7079 /* Initialize a CU (or TU) and read its DIEs.
7080 If the CU defers to a DWO file, read the DWO file as well.
7081
7082 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7083 Otherwise the table specified in the comp unit header is read in and used.
7084 This is an optimization for when we already have the abbrev table.
7085
7086 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7087 allocated. */
7088
7089 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7090 dwarf2_per_objfile *per_objfile,
7091 struct abbrev_table *abbrev_table,
7092 dwarf2_cu *existing_cu,
7093 bool skip_partial)
7094 : die_reader_specs {},
7095 m_this_cu (this_cu)
7096 {
7097 struct objfile *objfile = per_objfile->objfile;
7098 struct dwarf2_section_info *section = this_cu->section;
7099 bfd *abfd = section->get_bfd_owner ();
7100 const gdb_byte *begin_info_ptr;
7101 struct signatured_type *sig_type = NULL;
7102 struct dwarf2_section_info *abbrev_section;
7103 /* Non-zero if CU currently points to a DWO file and we need to
7104 reread it. When this happens we need to reread the skeleton die
7105 before we can reread the DWO file (this only applies to CUs, not TUs). */
7106 int rereading_dwo_cu = 0;
7107
7108 if (dwarf_die_debug)
7109 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7110 this_cu->is_debug_types ? "type" : "comp",
7111 sect_offset_str (this_cu->sect_off));
7112
7113 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7114 file (instead of going through the stub), short-circuit all of this. */
7115 if (this_cu->reading_dwo_directly)
7116 {
7117 /* Narrow down the scope of possibilities to have to understand. */
7118 gdb_assert (this_cu->is_debug_types);
7119 gdb_assert (abbrev_table == NULL);
7120 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
7121 return;
7122 }
7123
7124 /* This is cheap if the section is already read in. */
7125 section->read (objfile);
7126
7127 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7128
7129 abbrev_section = get_abbrev_section_for_cu (this_cu);
7130
7131 dwarf2_cu *cu;
7132
7133 if (existing_cu != nullptr)
7134 {
7135 cu = existing_cu;
7136 /* If this CU is from a DWO file we need to start over, we need to
7137 refetch the attributes from the skeleton CU.
7138 This could be optimized by retrieving those attributes from when we
7139 were here the first time: the previous comp_unit_die was stored in
7140 comp_unit_obstack. But there's no data yet that we need this
7141 optimization. */
7142 if (cu->dwo_unit != NULL)
7143 rereading_dwo_cu = 1;
7144 }
7145 else
7146 {
7147 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7148 in per_objfile yet. */
7149 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7150 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7151 cu = m_new_cu.get ();
7152 }
7153
7154 /* Get the header. */
7155 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7156 {
7157 /* We already have the header, there's no need to read it in again. */
7158 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7159 }
7160 else
7161 {
7162 if (this_cu->is_debug_types)
7163 {
7164 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7165 section, abbrev_section,
7166 info_ptr, rcuh_kind::TYPE);
7167
7168 /* Since per_cu is the first member of struct signatured_type,
7169 we can go from a pointer to one to a pointer to the other. */
7170 sig_type = (struct signatured_type *) this_cu;
7171 gdb_assert (sig_type->signature == cu->header.signature);
7172 gdb_assert (sig_type->type_offset_in_tu
7173 == cu->header.type_cu_offset_in_tu);
7174 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7175
7176 /* LENGTH has not been set yet for type units if we're
7177 using .gdb_index. */
7178 this_cu->length = cu->header.get_length ();
7179
7180 /* Establish the type offset that can be used to lookup the type. */
7181 sig_type->type_offset_in_section =
7182 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7183
7184 this_cu->dwarf_version = cu->header.version;
7185 }
7186 else
7187 {
7188 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7189 section, abbrev_section,
7190 info_ptr,
7191 rcuh_kind::COMPILE);
7192
7193 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7194 if (this_cu->length == 0)
7195 this_cu->length = cu->header.get_length ();
7196 else
7197 gdb_assert (this_cu->length == cu->header.get_length ());
7198 this_cu->dwarf_version = cu->header.version;
7199 }
7200 }
7201
7202 /* Skip dummy compilation units. */
7203 if (info_ptr >= begin_info_ptr + this_cu->length
7204 || peek_abbrev_code (abfd, info_ptr) == 0)
7205 {
7206 dummy_p = true;
7207 return;
7208 }
7209
7210 /* If we don't have them yet, read the abbrevs for this compilation unit.
7211 And if we need to read them now, make sure they're freed when we're
7212 done. */
7213 if (abbrev_table != NULL)
7214 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7215 else
7216 {
7217 m_abbrev_table_holder
7218 = abbrev_table::read (objfile, abbrev_section,
7219 cu->header.abbrev_sect_off);
7220 abbrev_table = m_abbrev_table_holder.get ();
7221 }
7222
7223 /* Read the top level CU/TU die. */
7224 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7225 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7226
7227 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7228 {
7229 dummy_p = true;
7230 return;
7231 }
7232
7233 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7234 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7235 table from the DWO file and pass the ownership over to us. It will be
7236 referenced from READER, so we must make sure to free it after we're done
7237 with READER.
7238
7239 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7240 DWO CU, that this test will fail (the attribute will not be present). */
7241 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7242 if (dwo_name != nullptr)
7243 {
7244 struct dwo_unit *dwo_unit;
7245 struct die_info *dwo_comp_unit_die;
7246
7247 if (comp_unit_die->has_children)
7248 {
7249 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7250 " has children (offset %s) [in module %s]"),
7251 sect_offset_str (this_cu->sect_off),
7252 bfd_get_filename (abfd));
7253 }
7254 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
7255 if (dwo_unit != NULL)
7256 {
7257 if (read_cutu_die_from_dwo (cu, dwo_unit,
7258 comp_unit_die, NULL,
7259 this, &info_ptr,
7260 &dwo_comp_unit_die,
7261 &m_dwo_abbrev_table) == 0)
7262 {
7263 /* Dummy die. */
7264 dummy_p = true;
7265 return;
7266 }
7267 comp_unit_die = dwo_comp_unit_die;
7268 }
7269 else
7270 {
7271 /* Yikes, we couldn't find the rest of the DIE, we only have
7272 the stub. A complaint has already been logged. There's
7273 not much more we can do except pass on the stub DIE to
7274 die_reader_func. We don't want to throw an error on bad
7275 debug info. */
7276 }
7277 }
7278 }
7279
7280 void
7281 cutu_reader::keep ()
7282 {
7283 /* Done, clean up. */
7284 gdb_assert (!dummy_p);
7285 if (m_new_cu != NULL)
7286 {
7287 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7288 now. */
7289 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7290 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
7291 }
7292 }
7293
7294 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7295 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7296 assumed to have already done the lookup to find the DWO file).
7297
7298 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7299 THIS_CU->is_debug_types, but nothing else.
7300
7301 We fill in THIS_CU->length.
7302
7303 THIS_CU->cu is always freed when done.
7304 This is done in order to not leave THIS_CU->cu in a state where we have
7305 to care whether it refers to the "main" CU or the DWO CU.
7306
7307 When parent_cu is passed, it is used to provide a default value for
7308 str_offsets_base and addr_base from the parent. */
7309
7310 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7311 dwarf2_per_objfile *per_objfile,
7312 struct dwarf2_cu *parent_cu,
7313 struct dwo_file *dwo_file)
7314 : die_reader_specs {},
7315 m_this_cu (this_cu)
7316 {
7317 struct objfile *objfile = per_objfile->objfile;
7318 struct dwarf2_section_info *section = this_cu->section;
7319 bfd *abfd = section->get_bfd_owner ();
7320 struct dwarf2_section_info *abbrev_section;
7321 const gdb_byte *begin_info_ptr, *info_ptr;
7322
7323 if (dwarf_die_debug)
7324 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7325 this_cu->is_debug_types ? "type" : "comp",
7326 sect_offset_str (this_cu->sect_off));
7327
7328 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7329
7330 abbrev_section = (dwo_file != NULL
7331 ? &dwo_file->sections.abbrev
7332 : get_abbrev_section_for_cu (this_cu));
7333
7334 /* This is cheap if the section is already read in. */
7335 section->read (objfile);
7336
7337 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7338
7339 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7340 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7341 section, abbrev_section, info_ptr,
7342 (this_cu->is_debug_types
7343 ? rcuh_kind::TYPE
7344 : rcuh_kind::COMPILE));
7345
7346 if (parent_cu != nullptr)
7347 {
7348 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7349 m_new_cu->addr_base = parent_cu->addr_base;
7350 }
7351 this_cu->length = m_new_cu->header.get_length ();
7352
7353 /* Skip dummy compilation units. */
7354 if (info_ptr >= begin_info_ptr + this_cu->length
7355 || peek_abbrev_code (abfd, info_ptr) == 0)
7356 {
7357 dummy_p = true;
7358 return;
7359 }
7360
7361 m_abbrev_table_holder
7362 = abbrev_table::read (objfile, abbrev_section,
7363 m_new_cu->header.abbrev_sect_off);
7364
7365 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7366 m_abbrev_table_holder.get ());
7367 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7368 }
7369
7370 \f
7371 /* Type Unit Groups.
7372
7373 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7374 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7375 so that all types coming from the same compilation (.o file) are grouped
7376 together. A future step could be to put the types in the same symtab as
7377 the CU the types ultimately came from. */
7378
7379 static hashval_t
7380 hash_type_unit_group (const void *item)
7381 {
7382 const struct type_unit_group *tu_group
7383 = (const struct type_unit_group *) item;
7384
7385 return hash_stmt_list_entry (&tu_group->hash);
7386 }
7387
7388 static int
7389 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7390 {
7391 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7392 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7393
7394 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7395 }
7396
7397 /* Allocate a hash table for type unit groups. */
7398
7399 static htab_up
7400 allocate_type_unit_groups_table ()
7401 {
7402 return htab_up (htab_create_alloc (3,
7403 hash_type_unit_group,
7404 eq_type_unit_group,
7405 NULL, xcalloc, xfree));
7406 }
7407
7408 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7409 partial symtabs. We combine several TUs per psymtab to not let the size
7410 of any one psymtab grow too big. */
7411 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7412 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7413
7414 /* Helper routine for get_type_unit_group.
7415 Create the type_unit_group object used to hold one or more TUs. */
7416
7417 static struct type_unit_group *
7418 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7419 {
7420 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7421 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7422 struct dwarf2_per_cu_data *per_cu;
7423 struct type_unit_group *tu_group;
7424
7425 tu_group = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, type_unit_group);
7426 per_cu = &tu_group->per_cu;
7427 per_cu->per_bfd = per_bfd;
7428
7429 if (per_bfd->using_index)
7430 {
7431 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7432 struct dwarf2_per_cu_quick_data);
7433 }
7434 else
7435 {
7436 unsigned int line_offset = to_underlying (line_offset_struct);
7437 dwarf2_psymtab *pst;
7438 std::string name;
7439
7440 /* Give the symtab a useful name for debug purposes. */
7441 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7442 name = string_printf ("<type_units_%d>",
7443 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7444 else
7445 name = string_printf ("<type_units_at_0x%x>", line_offset);
7446
7447 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
7448 pst->anonymous = true;
7449 }
7450
7451 tu_group->hash.dwo_unit = cu->dwo_unit;
7452 tu_group->hash.line_sect_off = line_offset_struct;
7453
7454 return tu_group;
7455 }
7456
7457 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7458 STMT_LIST is a DW_AT_stmt_list attribute. */
7459
7460 static struct type_unit_group *
7461 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7462 {
7463 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7464 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7465 struct type_unit_group *tu_group;
7466 void **slot;
7467 unsigned int line_offset;
7468 struct type_unit_group type_unit_group_for_lookup;
7469
7470 if (per_objfile->per_bfd->type_unit_groups == NULL)
7471 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7472
7473 /* Do we need to create a new group, or can we use an existing one? */
7474
7475 if (stmt_list)
7476 {
7477 line_offset = DW_UNSND (stmt_list);
7478 ++tu_stats->nr_symtab_sharers;
7479 }
7480 else
7481 {
7482 /* Ugh, no stmt_list. Rare, but we have to handle it.
7483 We can do various things here like create one group per TU or
7484 spread them over multiple groups to split up the expansion work.
7485 To avoid worst case scenarios (too many groups or too large groups)
7486 we, umm, group them in bunches. */
7487 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7488 | (tu_stats->nr_stmt_less_type_units
7489 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7490 ++tu_stats->nr_stmt_less_type_units;
7491 }
7492
7493 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7494 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7495 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
7496 &type_unit_group_for_lookup, INSERT);
7497 if (*slot != NULL)
7498 {
7499 tu_group = (struct type_unit_group *) *slot;
7500 gdb_assert (tu_group != NULL);
7501 }
7502 else
7503 {
7504 sect_offset line_offset_struct = (sect_offset) line_offset;
7505 tu_group = create_type_unit_group (cu, line_offset_struct);
7506 *slot = tu_group;
7507 ++tu_stats->nr_symtabs;
7508 }
7509
7510 return tu_group;
7511 }
7512 \f
7513 /* Partial symbol tables. */
7514
7515 /* Create a psymtab named NAME and assign it to PER_CU.
7516
7517 The caller must fill in the following details:
7518 dirname, textlow, texthigh. */
7519
7520 static dwarf2_psymtab *
7521 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7522 dwarf2_per_objfile *per_objfile,
7523 const char *name)
7524 {
7525 struct objfile *objfile = per_objfile->objfile;
7526 dwarf2_psymtab *pst;
7527
7528 pst = new dwarf2_psymtab (name, objfile, per_cu);
7529
7530 pst->psymtabs_addrmap_supported = true;
7531
7532 /* This is the glue that links PST into GDB's symbol API. */
7533 per_cu->v.psymtab = pst;
7534
7535 return pst;
7536 }
7537
7538 /* DIE reader function for process_psymtab_comp_unit. */
7539
7540 static void
7541 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7542 const gdb_byte *info_ptr,
7543 struct die_info *comp_unit_die,
7544 enum language pretend_language)
7545 {
7546 struct dwarf2_cu *cu = reader->cu;
7547 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7548 struct objfile *objfile = per_objfile->objfile;
7549 struct gdbarch *gdbarch = objfile->arch ();
7550 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7551 CORE_ADDR baseaddr;
7552 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7553 dwarf2_psymtab *pst;
7554 enum pc_bounds_kind cu_bounds_kind;
7555 const char *filename;
7556
7557 gdb_assert (! per_cu->is_debug_types);
7558
7559 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7560
7561 /* Allocate a new partial symbol table structure. */
7562 gdb::unique_xmalloc_ptr<char> debug_filename;
7563 static const char artificial[] = "<artificial>";
7564 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7565 if (filename == NULL)
7566 filename = "";
7567 else if (strcmp (filename, artificial) == 0)
7568 {
7569 debug_filename.reset (concat (artificial, "@",
7570 sect_offset_str (per_cu->sect_off),
7571 (char *) NULL));
7572 filename = debug_filename.get ();
7573 }
7574
7575 pst = create_partial_symtab (per_cu, per_objfile, filename);
7576
7577 /* This must be done before calling dwarf2_build_include_psymtabs. */
7578 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7579
7580 baseaddr = objfile->text_section_offset ();
7581
7582 dwarf2_find_base_address (comp_unit_die, cu);
7583
7584 /* Possibly set the default values of LOWPC and HIGHPC from
7585 `DW_AT_ranges'. */
7586 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7587 &best_highpc, cu, pst);
7588 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7589 {
7590 CORE_ADDR low
7591 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7592 - baseaddr);
7593 CORE_ADDR high
7594 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7595 - baseaddr - 1);
7596 /* Store the contiguous range if it is not empty; it can be
7597 empty for CUs with no code. */
7598 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7599 low, high, pst);
7600 }
7601
7602 /* Check if comp unit has_children.
7603 If so, read the rest of the partial symbols from this comp unit.
7604 If not, there's no more debug_info for this comp unit. */
7605 if (comp_unit_die->has_children)
7606 {
7607 struct partial_die_info *first_die;
7608 CORE_ADDR lowpc, highpc;
7609
7610 lowpc = ((CORE_ADDR) -1);
7611 highpc = ((CORE_ADDR) 0);
7612
7613 first_die = load_partial_dies (reader, info_ptr, 1);
7614
7615 scan_partial_symbols (first_die, &lowpc, &highpc,
7616 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7617
7618 /* If we didn't find a lowpc, set it to highpc to avoid
7619 complaints from `maint check'. */
7620 if (lowpc == ((CORE_ADDR) -1))
7621 lowpc = highpc;
7622
7623 /* If the compilation unit didn't have an explicit address range,
7624 then use the information extracted from its child dies. */
7625 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7626 {
7627 best_lowpc = lowpc;
7628 best_highpc = highpc;
7629 }
7630 }
7631 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7632 best_lowpc + baseaddr)
7633 - baseaddr);
7634 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7635 best_highpc + baseaddr)
7636 - baseaddr);
7637
7638 end_psymtab_common (objfile, pst);
7639
7640 if (!cu->per_cu->imported_symtabs_empty ())
7641 {
7642 int i;
7643 int len = cu->per_cu->imported_symtabs_size ();
7644
7645 /* Fill in 'dependencies' here; we fill in 'users' in a
7646 post-pass. */
7647 pst->number_of_dependencies = len;
7648 pst->dependencies
7649 = objfile->partial_symtabs->allocate_dependencies (len);
7650 for (i = 0; i < len; ++i)
7651 {
7652 pst->dependencies[i]
7653 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7654 }
7655
7656 cu->per_cu->imported_symtabs_free ();
7657 }
7658
7659 /* Get the list of files included in the current compilation unit,
7660 and build a psymtab for each of them. */
7661 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7662
7663 if (dwarf_read_debug)
7664 fprintf_unfiltered (gdb_stdlog,
7665 "Psymtab for %s unit @%s: %s - %s"
7666 ", %d global, %d static syms\n",
7667 per_cu->is_debug_types ? "type" : "comp",
7668 sect_offset_str (per_cu->sect_off),
7669 paddress (gdbarch, pst->text_low (objfile)),
7670 paddress (gdbarch, pst->text_high (objfile)),
7671 pst->n_global_syms, pst->n_static_syms);
7672 }
7673
7674 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7675 Process compilation unit THIS_CU for a psymtab. */
7676
7677 static void
7678 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7679 dwarf2_per_objfile *per_objfile,
7680 bool want_partial_unit,
7681 enum language pretend_language)
7682 {
7683 /* If this compilation unit was already read in, free the
7684 cached copy in order to read it in again. This is
7685 necessary because we skipped some symbols when we first
7686 read in the compilation unit (see load_partial_dies).
7687 This problem could be avoided, but the benefit is unclear. */
7688 per_objfile->remove_cu (this_cu);
7689
7690 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7691
7692 switch (reader.comp_unit_die->tag)
7693 {
7694 case DW_TAG_compile_unit:
7695 this_cu->unit_type = DW_UT_compile;
7696 break;
7697 case DW_TAG_partial_unit:
7698 this_cu->unit_type = DW_UT_partial;
7699 break;
7700 default:
7701 abort ();
7702 }
7703
7704 if (reader.dummy_p)
7705 {
7706 /* Nothing. */
7707 }
7708 else if (this_cu->is_debug_types)
7709 build_type_psymtabs_reader (&reader, reader.info_ptr,
7710 reader.comp_unit_die);
7711 else if (want_partial_unit
7712 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7713 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7714 reader.comp_unit_die,
7715 pretend_language);
7716
7717 this_cu->lang = reader.cu->language;
7718
7719 /* Age out any secondary CUs. */
7720 per_objfile->age_comp_units ();
7721 }
7722
7723 /* Reader function for build_type_psymtabs. */
7724
7725 static void
7726 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7727 const gdb_byte *info_ptr,
7728 struct die_info *type_unit_die)
7729 {
7730 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7731 struct objfile *objfile = per_objfile->objfile;
7732 struct dwarf2_cu *cu = reader->cu;
7733 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7734 struct signatured_type *sig_type;
7735 struct type_unit_group *tu_group;
7736 struct attribute *attr;
7737 struct partial_die_info *first_die;
7738 CORE_ADDR lowpc, highpc;
7739 dwarf2_psymtab *pst;
7740
7741 gdb_assert (per_cu->is_debug_types);
7742 sig_type = (struct signatured_type *) per_cu;
7743
7744 if (! type_unit_die->has_children)
7745 return;
7746
7747 attr = type_unit_die->attr (DW_AT_stmt_list);
7748 tu_group = get_type_unit_group (cu, attr);
7749
7750 if (tu_group->tus == nullptr)
7751 tu_group->tus = new std::vector<signatured_type *>;
7752 tu_group->tus->push_back (sig_type);
7753
7754 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7755 pst = create_partial_symtab (per_cu, per_objfile, "");
7756 pst->anonymous = true;
7757
7758 first_die = load_partial_dies (reader, info_ptr, 1);
7759
7760 lowpc = (CORE_ADDR) -1;
7761 highpc = (CORE_ADDR) 0;
7762 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7763
7764 end_psymtab_common (objfile, pst);
7765 }
7766
7767 /* Struct used to sort TUs by their abbreviation table offset. */
7768
7769 struct tu_abbrev_offset
7770 {
7771 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7772 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7773 {}
7774
7775 signatured_type *sig_type;
7776 sect_offset abbrev_offset;
7777 };
7778
7779 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7780
7781 static bool
7782 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7783 const struct tu_abbrev_offset &b)
7784 {
7785 return a.abbrev_offset < b.abbrev_offset;
7786 }
7787
7788 /* Efficiently read all the type units.
7789 This does the bulk of the work for build_type_psymtabs.
7790
7791 The efficiency is because we sort TUs by the abbrev table they use and
7792 only read each abbrev table once. In one program there are 200K TUs
7793 sharing 8K abbrev tables.
7794
7795 The main purpose of this function is to support building the
7796 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7797 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7798 can collapse the search space by grouping them by stmt_list.
7799 The savings can be significant, in the same program from above the 200K TUs
7800 share 8K stmt_list tables.
7801
7802 FUNC is expected to call get_type_unit_group, which will create the
7803 struct type_unit_group if necessary and add it to
7804 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7805
7806 static void
7807 build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
7808 {
7809 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7810 abbrev_table_up abbrev_table;
7811 sect_offset abbrev_offset;
7812
7813 /* It's up to the caller to not call us multiple times. */
7814 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7815
7816 if (per_objfile->per_bfd->all_type_units.empty ())
7817 return;
7818
7819 /* TUs typically share abbrev tables, and there can be way more TUs than
7820 abbrev tables. Sort by abbrev table to reduce the number of times we
7821 read each abbrev table in.
7822 Alternatives are to punt or to maintain a cache of abbrev tables.
7823 This is simpler and efficient enough for now.
7824
7825 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7826 symtab to use). Typically TUs with the same abbrev offset have the same
7827 stmt_list value too so in practice this should work well.
7828
7829 The basic algorithm here is:
7830
7831 sort TUs by abbrev table
7832 for each TU with same abbrev table:
7833 read abbrev table if first user
7834 read TU top level DIE
7835 [IWBN if DWO skeletons had DW_AT_stmt_list]
7836 call FUNC */
7837
7838 if (dwarf_read_debug)
7839 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7840
7841 /* Sort in a separate table to maintain the order of all_type_units
7842 for .gdb_index: TU indices directly index all_type_units. */
7843 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7844 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
7845
7846 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
7847 sorted_by_abbrev.emplace_back
7848 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
7849 sig_type->per_cu.sect_off));
7850
7851 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7852 sort_tu_by_abbrev_offset);
7853
7854 abbrev_offset = (sect_offset) ~(unsigned) 0;
7855
7856 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7857 {
7858 /* Switch to the next abbrev table if necessary. */
7859 if (abbrev_table == NULL
7860 || tu.abbrev_offset != abbrev_offset)
7861 {
7862 abbrev_offset = tu.abbrev_offset;
7863 abbrev_table =
7864 abbrev_table::read (per_objfile->objfile,
7865 &per_objfile->per_bfd->abbrev, abbrev_offset);
7866 ++tu_stats->nr_uniq_abbrev_tables;
7867 }
7868
7869 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
7870 abbrev_table.get (), nullptr, false);
7871 if (!reader.dummy_p)
7872 build_type_psymtabs_reader (&reader, reader.info_ptr,
7873 reader.comp_unit_die);
7874 }
7875 }
7876
7877 /* Print collected type unit statistics. */
7878
7879 static void
7880 print_tu_stats (dwarf2_per_objfile *per_objfile)
7881 {
7882 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7883
7884 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7885 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7886 per_objfile->per_bfd->all_type_units.size ());
7887 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7888 tu_stats->nr_uniq_abbrev_tables);
7889 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7890 tu_stats->nr_symtabs);
7891 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7892 tu_stats->nr_symtab_sharers);
7893 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7894 tu_stats->nr_stmt_less_type_units);
7895 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7896 tu_stats->nr_all_type_units_reallocs);
7897 }
7898
7899 /* Traversal function for build_type_psymtabs. */
7900
7901 static int
7902 build_type_psymtab_dependencies (void **slot, void *info)
7903 {
7904 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7905 struct objfile *objfile = per_objfile->objfile;
7906 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7907 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7908 dwarf2_psymtab *pst = per_cu->v.psymtab;
7909 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7910 int i;
7911
7912 gdb_assert (len > 0);
7913 gdb_assert (per_cu->type_unit_group_p ());
7914
7915 pst->number_of_dependencies = len;
7916 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7917 for (i = 0; i < len; ++i)
7918 {
7919 struct signatured_type *iter = tu_group->tus->at (i);
7920 gdb_assert (iter->per_cu.is_debug_types);
7921 pst->dependencies[i] = iter->per_cu.v.psymtab;
7922 iter->type_unit_group = tu_group;
7923 }
7924
7925 delete tu_group->tus;
7926 tu_group->tus = nullptr;
7927
7928 return 1;
7929 }
7930
7931 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7932 Build partial symbol tables for the .debug_types comp-units. */
7933
7934 static void
7935 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
7936 {
7937 if (! create_all_type_units (per_objfile))
7938 return;
7939
7940 build_type_psymtabs_1 (per_objfile);
7941 }
7942
7943 /* Traversal function for process_skeletonless_type_unit.
7944 Read a TU in a DWO file and build partial symbols for it. */
7945
7946 static int
7947 process_skeletonless_type_unit (void **slot, void *info)
7948 {
7949 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7950 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7951 struct signatured_type find_entry, *entry;
7952
7953 /* If this TU doesn't exist in the global table, add it and read it in. */
7954
7955 if (per_objfile->per_bfd->signatured_types == NULL)
7956 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7957
7958 find_entry.signature = dwo_unit->signature;
7959 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
7960 &find_entry, INSERT);
7961 /* If we've already seen this type there's nothing to do. What's happening
7962 is we're doing our own version of comdat-folding here. */
7963 if (*slot != NULL)
7964 return 1;
7965
7966 /* This does the job that create_all_type_units would have done for
7967 this TU. */
7968 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
7969 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
7970 *slot = entry;
7971
7972 /* This does the job that build_type_psymtabs_1 would have done. */
7973 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
7974 if (!reader.dummy_p)
7975 build_type_psymtabs_reader (&reader, reader.info_ptr,
7976 reader.comp_unit_die);
7977
7978 return 1;
7979 }
7980
7981 /* Traversal function for process_skeletonless_type_units. */
7982
7983 static int
7984 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7985 {
7986 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7987
7988 if (dwo_file->tus != NULL)
7989 htab_traverse_noresize (dwo_file->tus.get (),
7990 process_skeletonless_type_unit, info);
7991
7992 return 1;
7993 }
7994
7995 /* Scan all TUs of DWO files, verifying we've processed them.
7996 This is needed in case a TU was emitted without its skeleton.
7997 Note: This can't be done until we know what all the DWO files are. */
7998
7999 static void
8000 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
8001 {
8002 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8003 if (get_dwp_file (per_objfile) == NULL
8004 && per_objfile->per_bfd->dwo_files != NULL)
8005 {
8006 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
8007 process_dwo_file_for_skeletonless_type_units,
8008 per_objfile);
8009 }
8010 }
8011
8012 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8013
8014 static void
8015 set_partial_user (dwarf2_per_objfile *per_objfile)
8016 {
8017 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
8018 {
8019 dwarf2_psymtab *pst = per_cu->v.psymtab;
8020
8021 if (pst == NULL)
8022 continue;
8023
8024 for (int j = 0; j < pst->number_of_dependencies; ++j)
8025 {
8026 /* Set the 'user' field only if it is not already set. */
8027 if (pst->dependencies[j]->user == NULL)
8028 pst->dependencies[j]->user = pst;
8029 }
8030 }
8031 }
8032
8033 /* Build the partial symbol table by doing a quick pass through the
8034 .debug_info and .debug_abbrev sections. */
8035
8036 static void
8037 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
8038 {
8039 struct objfile *objfile = per_objfile->objfile;
8040
8041 if (dwarf_read_debug)
8042 {
8043 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8044 objfile_name (objfile));
8045 }
8046
8047 scoped_restore restore_reading_psyms
8048 = make_scoped_restore (&per_objfile->per_bfd->reading_partial_symbols,
8049 true);
8050
8051 per_objfile->per_bfd->info.read (objfile);
8052
8053 /* Any cached compilation units will be linked by the per-objfile
8054 read_in_chain. Make sure to free them when we're done. */
8055 free_cached_comp_units freer (per_objfile);
8056
8057 build_type_psymtabs (per_objfile);
8058
8059 create_all_comp_units (per_objfile);
8060
8061 /* Create a temporary address map on a temporary obstack. We later
8062 copy this to the final obstack. */
8063 auto_obstack temp_obstack;
8064
8065 scoped_restore save_psymtabs_addrmap
8066 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8067 addrmap_create_mutable (&temp_obstack));
8068
8069 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
8070 {
8071 if (per_cu->v.psymtab != NULL)
8072 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8073 continue;
8074 process_psymtab_comp_unit (per_cu, per_objfile, false,
8075 language_minimal);
8076 }
8077
8078 /* This has to wait until we read the CUs, we need the list of DWOs. */
8079 process_skeletonless_type_units (per_objfile);
8080
8081 /* Now that all TUs have been processed we can fill in the dependencies. */
8082 if (per_objfile->per_bfd->type_unit_groups != NULL)
8083 {
8084 htab_traverse_noresize (per_objfile->per_bfd->type_unit_groups.get (),
8085 build_type_psymtab_dependencies, per_objfile);
8086 }
8087
8088 if (dwarf_read_debug)
8089 print_tu_stats (per_objfile);
8090
8091 set_partial_user (per_objfile);
8092
8093 objfile->partial_symtabs->psymtabs_addrmap
8094 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8095 objfile->partial_symtabs->obstack ());
8096 /* At this point we want to keep the address map. */
8097 save_psymtabs_addrmap.release ();
8098
8099 if (dwarf_read_debug)
8100 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8101 objfile_name (objfile));
8102 }
8103
8104 /* Load the partial DIEs for a secondary CU into memory.
8105 This is also used when rereading a primary CU with load_all_dies. */
8106
8107 static void
8108 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
8109 dwarf2_per_objfile *per_objfile,
8110 dwarf2_cu *existing_cu)
8111 {
8112 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
8113
8114 if (!reader.dummy_p)
8115 {
8116 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8117 language_minimal);
8118
8119 /* Check if comp unit has_children.
8120 If so, read the rest of the partial symbols from this comp unit.
8121 If not, there's no more debug_info for this comp unit. */
8122 if (reader.comp_unit_die->has_children)
8123 load_partial_dies (&reader, reader.info_ptr, 0);
8124
8125 reader.keep ();
8126 }
8127 }
8128
8129 static void
8130 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
8131 struct dwarf2_section_info *section,
8132 struct dwarf2_section_info *abbrev_section,
8133 unsigned int is_dwz)
8134 {
8135 const gdb_byte *info_ptr;
8136 struct objfile *objfile = per_objfile->objfile;
8137
8138 if (dwarf_read_debug)
8139 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8140 section->get_name (),
8141 section->get_file_name ());
8142
8143 section->read (objfile);
8144
8145 info_ptr = section->buffer;
8146
8147 while (info_ptr < section->buffer + section->size)
8148 {
8149 struct dwarf2_per_cu_data *this_cu;
8150
8151 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8152
8153 comp_unit_head cu_header;
8154 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
8155 abbrev_section, info_ptr,
8156 rcuh_kind::COMPILE);
8157
8158 /* Save the compilation unit for later lookup. */
8159 if (cu_header.unit_type != DW_UT_type)
8160 this_cu = per_objfile->per_bfd->allocate_per_cu ();
8161 else
8162 {
8163 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
8164 sig_type->signature = cu_header.signature;
8165 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8166 this_cu = &sig_type->per_cu;
8167 }
8168 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8169 this_cu->sect_off = sect_off;
8170 this_cu->length = cu_header.length + cu_header.initial_length_size;
8171 this_cu->is_dwz = is_dwz;
8172 this_cu->section = section;
8173
8174 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8175
8176 info_ptr = info_ptr + this_cu->length;
8177 }
8178 }
8179
8180 /* Create a list of all compilation units in OBJFILE.
8181 This is only done for -readnow and building partial symtabs. */
8182
8183 static void
8184 create_all_comp_units (dwarf2_per_objfile *per_objfile)
8185 {
8186 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8187 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8188 &per_objfile->per_bfd->abbrev, 0);
8189
8190 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8191 if (dwz != NULL)
8192 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
8193 }
8194
8195 /* Process all loaded DIEs for compilation unit CU, starting at
8196 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8197 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8198 DW_AT_ranges). See the comments of add_partial_subprogram on how
8199 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8200
8201 static void
8202 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8203 CORE_ADDR *highpc, int set_addrmap,
8204 struct dwarf2_cu *cu)
8205 {
8206 struct partial_die_info *pdi;
8207
8208 /* Now, march along the PDI's, descending into ones which have
8209 interesting children but skipping the children of the other ones,
8210 until we reach the end of the compilation unit. */
8211
8212 pdi = first_die;
8213
8214 while (pdi != NULL)
8215 {
8216 pdi->fixup (cu);
8217
8218 /* Anonymous namespaces or modules have no name but have interesting
8219 children, so we need to look at them. Ditto for anonymous
8220 enums. */
8221
8222 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
8223 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8224 || pdi->tag == DW_TAG_imported_unit
8225 || pdi->tag == DW_TAG_inlined_subroutine)
8226 {
8227 switch (pdi->tag)
8228 {
8229 case DW_TAG_subprogram:
8230 case DW_TAG_inlined_subroutine:
8231 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8232 if (cu->language == language_cplus)
8233 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8234 set_addrmap, cu);
8235 break;
8236 case DW_TAG_constant:
8237 case DW_TAG_variable:
8238 case DW_TAG_typedef:
8239 case DW_TAG_union_type:
8240 if (!pdi->is_declaration
8241 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8242 {
8243 add_partial_symbol (pdi, cu);
8244 }
8245 break;
8246 case DW_TAG_class_type:
8247 case DW_TAG_interface_type:
8248 case DW_TAG_structure_type:
8249 if (!pdi->is_declaration)
8250 {
8251 add_partial_symbol (pdi, cu);
8252 }
8253 if ((cu->language == language_rust
8254 || cu->language == language_cplus) && pdi->has_children)
8255 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8256 set_addrmap, cu);
8257 break;
8258 case DW_TAG_enumeration_type:
8259 if (!pdi->is_declaration)
8260 add_partial_enumeration (pdi, cu);
8261 break;
8262 case DW_TAG_base_type:
8263 case DW_TAG_subrange_type:
8264 /* File scope base type definitions are added to the partial
8265 symbol table. */
8266 add_partial_symbol (pdi, cu);
8267 break;
8268 case DW_TAG_namespace:
8269 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8270 break;
8271 case DW_TAG_module:
8272 if (!pdi->is_declaration)
8273 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8274 break;
8275 case DW_TAG_imported_unit:
8276 {
8277 struct dwarf2_per_cu_data *per_cu;
8278
8279 /* For now we don't handle imported units in type units. */
8280 if (cu->per_cu->is_debug_types)
8281 {
8282 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8283 " supported in type units [in module %s]"),
8284 objfile_name (cu->per_objfile->objfile));
8285 }
8286
8287 per_cu = dwarf2_find_containing_comp_unit
8288 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8289
8290 /* Go read the partial unit, if needed. */
8291 if (per_cu->v.psymtab == NULL)
8292 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8293 cu->language);
8294
8295 cu->per_cu->imported_symtabs_push (per_cu);
8296 }
8297 break;
8298 case DW_TAG_imported_declaration:
8299 add_partial_symbol (pdi, cu);
8300 break;
8301 default:
8302 break;
8303 }
8304 }
8305
8306 /* If the die has a sibling, skip to the sibling. */
8307
8308 pdi = pdi->die_sibling;
8309 }
8310 }
8311
8312 /* Functions used to compute the fully scoped name of a partial DIE.
8313
8314 Normally, this is simple. For C++, the parent DIE's fully scoped
8315 name is concatenated with "::" and the partial DIE's name.
8316 Enumerators are an exception; they use the scope of their parent
8317 enumeration type, i.e. the name of the enumeration type is not
8318 prepended to the enumerator.
8319
8320 There are two complexities. One is DW_AT_specification; in this
8321 case "parent" means the parent of the target of the specification,
8322 instead of the direct parent of the DIE. The other is compilers
8323 which do not emit DW_TAG_namespace; in this case we try to guess
8324 the fully qualified name of structure types from their members'
8325 linkage names. This must be done using the DIE's children rather
8326 than the children of any DW_AT_specification target. We only need
8327 to do this for structures at the top level, i.e. if the target of
8328 any DW_AT_specification (if any; otherwise the DIE itself) does not
8329 have a parent. */
8330
8331 /* Compute the scope prefix associated with PDI's parent, in
8332 compilation unit CU. The result will be allocated on CU's
8333 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8334 field. NULL is returned if no prefix is necessary. */
8335 static const char *
8336 partial_die_parent_scope (struct partial_die_info *pdi,
8337 struct dwarf2_cu *cu)
8338 {
8339 const char *grandparent_scope;
8340 struct partial_die_info *parent, *real_pdi;
8341
8342 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8343 then this means the parent of the specification DIE. */
8344
8345 real_pdi = pdi;
8346 while (real_pdi->has_specification)
8347 {
8348 auto res = find_partial_die (real_pdi->spec_offset,
8349 real_pdi->spec_is_dwz, cu);
8350 real_pdi = res.pdi;
8351 cu = res.cu;
8352 }
8353
8354 parent = real_pdi->die_parent;
8355 if (parent == NULL)
8356 return NULL;
8357
8358 if (parent->scope_set)
8359 return parent->scope;
8360
8361 parent->fixup (cu);
8362
8363 grandparent_scope = partial_die_parent_scope (parent, cu);
8364
8365 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8366 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8367 Work around this problem here. */
8368 if (cu->language == language_cplus
8369 && parent->tag == DW_TAG_namespace
8370 && strcmp (parent->name (cu), "::") == 0
8371 && grandparent_scope == NULL)
8372 {
8373 parent->scope = NULL;
8374 parent->scope_set = 1;
8375 return NULL;
8376 }
8377
8378 /* Nested subroutines in Fortran get a prefix. */
8379 if (pdi->tag == DW_TAG_enumerator)
8380 /* Enumerators should not get the name of the enumeration as a prefix. */
8381 parent->scope = grandparent_scope;
8382 else if (parent->tag == DW_TAG_namespace
8383 || parent->tag == DW_TAG_module
8384 || parent->tag == DW_TAG_structure_type
8385 || parent->tag == DW_TAG_class_type
8386 || parent->tag == DW_TAG_interface_type
8387 || parent->tag == DW_TAG_union_type
8388 || parent->tag == DW_TAG_enumeration_type
8389 || (cu->language == language_fortran
8390 && parent->tag == DW_TAG_subprogram
8391 && pdi->tag == DW_TAG_subprogram))
8392 {
8393 if (grandparent_scope == NULL)
8394 parent->scope = parent->name (cu);
8395 else
8396 parent->scope = typename_concat (&cu->comp_unit_obstack,
8397 grandparent_scope,
8398 parent->name (cu), 0, cu);
8399 }
8400 else
8401 {
8402 /* FIXME drow/2004-04-01: What should we be doing with
8403 function-local names? For partial symbols, we should probably be
8404 ignoring them. */
8405 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8406 dwarf_tag_name (parent->tag),
8407 sect_offset_str (pdi->sect_off));
8408 parent->scope = grandparent_scope;
8409 }
8410
8411 parent->scope_set = 1;
8412 return parent->scope;
8413 }
8414
8415 /* Return the fully scoped name associated with PDI, from compilation unit
8416 CU. The result will be allocated with malloc. */
8417
8418 static gdb::unique_xmalloc_ptr<char>
8419 partial_die_full_name (struct partial_die_info *pdi,
8420 struct dwarf2_cu *cu)
8421 {
8422 const char *parent_scope;
8423
8424 /* If this is a template instantiation, we can not work out the
8425 template arguments from partial DIEs. So, unfortunately, we have
8426 to go through the full DIEs. At least any work we do building
8427 types here will be reused if full symbols are loaded later. */
8428 if (pdi->has_template_arguments)
8429 {
8430 pdi->fixup (cu);
8431
8432 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
8433 {
8434 struct die_info *die;
8435 struct attribute attr;
8436 struct dwarf2_cu *ref_cu = cu;
8437
8438 /* DW_FORM_ref_addr is using section offset. */
8439 attr.name = (enum dwarf_attribute) 0;
8440 attr.form = DW_FORM_ref_addr;
8441 attr.u.unsnd = to_underlying (pdi->sect_off);
8442 die = follow_die_ref (NULL, &attr, &ref_cu);
8443
8444 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8445 }
8446 }
8447
8448 parent_scope = partial_die_parent_scope (pdi, cu);
8449 if (parent_scope == NULL)
8450 return NULL;
8451 else
8452 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8453 pdi->name (cu),
8454 0, cu));
8455 }
8456
8457 static void
8458 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8459 {
8460 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8461 struct objfile *objfile = per_objfile->objfile;
8462 struct gdbarch *gdbarch = objfile->arch ();
8463 CORE_ADDR addr = 0;
8464 const char *actual_name = NULL;
8465 CORE_ADDR baseaddr;
8466
8467 baseaddr = objfile->text_section_offset ();
8468
8469 gdb::unique_xmalloc_ptr<char> built_actual_name
8470 = partial_die_full_name (pdi, cu);
8471 if (built_actual_name != NULL)
8472 actual_name = built_actual_name.get ();
8473
8474 if (actual_name == NULL)
8475 actual_name = pdi->name (cu);
8476
8477 partial_symbol psymbol;
8478 memset (&psymbol, 0, sizeof (psymbol));
8479 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8480 psymbol.ginfo.section = -1;
8481
8482 /* The code below indicates that the psymbol should be installed by
8483 setting this. */
8484 gdb::optional<psymbol_placement> where;
8485
8486 switch (pdi->tag)
8487 {
8488 case DW_TAG_inlined_subroutine:
8489 case DW_TAG_subprogram:
8490 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8491 - baseaddr);
8492 if (pdi->is_external
8493 || cu->language == language_ada
8494 || (cu->language == language_fortran
8495 && pdi->die_parent != NULL
8496 && pdi->die_parent->tag == DW_TAG_subprogram))
8497 {
8498 /* Normally, only "external" DIEs are part of the global scope.
8499 But in Ada and Fortran, we want to be able to access nested
8500 procedures globally. So all Ada and Fortran subprograms are
8501 stored in the global scope. */
8502 where = psymbol_placement::GLOBAL;
8503 }
8504 else
8505 where = psymbol_placement::STATIC;
8506
8507 psymbol.domain = VAR_DOMAIN;
8508 psymbol.aclass = LOC_BLOCK;
8509 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8510 psymbol.ginfo.value.address = addr;
8511
8512 if (pdi->main_subprogram && actual_name != NULL)
8513 set_objfile_main_name (objfile, actual_name, cu->language);
8514 break;
8515 case DW_TAG_constant:
8516 psymbol.domain = VAR_DOMAIN;
8517 psymbol.aclass = LOC_STATIC;
8518 where = (pdi->is_external
8519 ? psymbol_placement::GLOBAL
8520 : psymbol_placement::STATIC);
8521 break;
8522 case DW_TAG_variable:
8523 if (pdi->d.locdesc)
8524 addr = decode_locdesc (pdi->d.locdesc, cu);
8525
8526 if (pdi->d.locdesc
8527 && addr == 0
8528 && !per_objfile->per_bfd->has_section_at_zero)
8529 {
8530 /* A global or static variable may also have been stripped
8531 out by the linker if unused, in which case its address
8532 will be nullified; do not add such variables into partial
8533 symbol table then. */
8534 }
8535 else if (pdi->is_external)
8536 {
8537 /* Global Variable.
8538 Don't enter into the minimal symbol tables as there is
8539 a minimal symbol table entry from the ELF symbols already.
8540 Enter into partial symbol table if it has a location
8541 descriptor or a type.
8542 If the location descriptor is missing, new_symbol will create
8543 a LOC_UNRESOLVED symbol, the address of the variable will then
8544 be determined from the minimal symbol table whenever the variable
8545 is referenced.
8546 The address for the partial symbol table entry is not
8547 used by GDB, but it comes in handy for debugging partial symbol
8548 table building. */
8549
8550 if (pdi->d.locdesc || pdi->has_type)
8551 {
8552 psymbol.domain = VAR_DOMAIN;
8553 psymbol.aclass = LOC_STATIC;
8554 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8555 psymbol.ginfo.value.address = addr;
8556 where = psymbol_placement::GLOBAL;
8557 }
8558 }
8559 else
8560 {
8561 int has_loc = pdi->d.locdesc != NULL;
8562
8563 /* Static Variable. Skip symbols whose value we cannot know (those
8564 without location descriptors or constant values). */
8565 if (!has_loc && !pdi->has_const_value)
8566 return;
8567
8568 psymbol.domain = VAR_DOMAIN;
8569 psymbol.aclass = LOC_STATIC;
8570 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8571 if (has_loc)
8572 psymbol.ginfo.value.address = addr;
8573 where = psymbol_placement::STATIC;
8574 }
8575 break;
8576 case DW_TAG_typedef:
8577 case DW_TAG_base_type:
8578 case DW_TAG_subrange_type:
8579 psymbol.domain = VAR_DOMAIN;
8580 psymbol.aclass = LOC_TYPEDEF;
8581 where = psymbol_placement::STATIC;
8582 break;
8583 case DW_TAG_imported_declaration:
8584 case DW_TAG_namespace:
8585 psymbol.domain = VAR_DOMAIN;
8586 psymbol.aclass = LOC_TYPEDEF;
8587 where = psymbol_placement::GLOBAL;
8588 break;
8589 case DW_TAG_module:
8590 /* With Fortran 77 there might be a "BLOCK DATA" module
8591 available without any name. If so, we skip the module as it
8592 doesn't bring any value. */
8593 if (actual_name != nullptr)
8594 {
8595 psymbol.domain = MODULE_DOMAIN;
8596 psymbol.aclass = LOC_TYPEDEF;
8597 where = psymbol_placement::GLOBAL;
8598 }
8599 break;
8600 case DW_TAG_class_type:
8601 case DW_TAG_interface_type:
8602 case DW_TAG_structure_type:
8603 case DW_TAG_union_type:
8604 case DW_TAG_enumeration_type:
8605 /* Skip external references. The DWARF standard says in the section
8606 about "Structure, Union, and Class Type Entries": "An incomplete
8607 structure, union or class type is represented by a structure,
8608 union or class entry that does not have a byte size attribute
8609 and that has a DW_AT_declaration attribute." */
8610 if (!pdi->has_byte_size && pdi->is_declaration)
8611 return;
8612
8613 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8614 static vs. global. */
8615 psymbol.domain = STRUCT_DOMAIN;
8616 psymbol.aclass = LOC_TYPEDEF;
8617 where = (cu->language == language_cplus
8618 ? psymbol_placement::GLOBAL
8619 : psymbol_placement::STATIC);
8620 break;
8621 case DW_TAG_enumerator:
8622 psymbol.domain = VAR_DOMAIN;
8623 psymbol.aclass = LOC_CONST;
8624 where = (cu->language == language_cplus
8625 ? psymbol_placement::GLOBAL
8626 : psymbol_placement::STATIC);
8627 break;
8628 default:
8629 break;
8630 }
8631
8632 if (where.has_value ())
8633 {
8634 if (built_actual_name != nullptr)
8635 actual_name = objfile->intern (actual_name);
8636 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8637 psymbol.ginfo.set_linkage_name (actual_name);
8638 else
8639 {
8640 psymbol.ginfo.set_demangled_name (actual_name,
8641 &objfile->objfile_obstack);
8642 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8643 }
8644 add_psymbol_to_list (psymbol, *where, objfile);
8645 }
8646 }
8647
8648 /* Read a partial die corresponding to a namespace; also, add a symbol
8649 corresponding to that namespace to the symbol table. NAMESPACE is
8650 the name of the enclosing namespace. */
8651
8652 static void
8653 add_partial_namespace (struct partial_die_info *pdi,
8654 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8655 int set_addrmap, struct dwarf2_cu *cu)
8656 {
8657 /* Add a symbol for the namespace. */
8658
8659 add_partial_symbol (pdi, cu);
8660
8661 /* Now scan partial symbols in that namespace. */
8662
8663 if (pdi->has_children)
8664 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8665 }
8666
8667 /* Read a partial die corresponding to a Fortran module. */
8668
8669 static void
8670 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8671 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8672 {
8673 /* Add a symbol for the namespace. */
8674
8675 add_partial_symbol (pdi, cu);
8676
8677 /* Now scan partial symbols in that module. */
8678
8679 if (pdi->has_children)
8680 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8681 }
8682
8683 /* Read a partial die corresponding to a subprogram or an inlined
8684 subprogram and create a partial symbol for that subprogram.
8685 When the CU language allows it, this routine also defines a partial
8686 symbol for each nested subprogram that this subprogram contains.
8687 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8688 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8689
8690 PDI may also be a lexical block, in which case we simply search
8691 recursively for subprograms defined inside that lexical block.
8692 Again, this is only performed when the CU language allows this
8693 type of definitions. */
8694
8695 static void
8696 add_partial_subprogram (struct partial_die_info *pdi,
8697 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8698 int set_addrmap, struct dwarf2_cu *cu)
8699 {
8700 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8701 {
8702 if (pdi->has_pc_info)
8703 {
8704 if (pdi->lowpc < *lowpc)
8705 *lowpc = pdi->lowpc;
8706 if (pdi->highpc > *highpc)
8707 *highpc = pdi->highpc;
8708 if (set_addrmap)
8709 {
8710 struct objfile *objfile = cu->per_objfile->objfile;
8711 struct gdbarch *gdbarch = objfile->arch ();
8712 CORE_ADDR baseaddr;
8713 CORE_ADDR this_highpc;
8714 CORE_ADDR this_lowpc;
8715
8716 baseaddr = objfile->text_section_offset ();
8717 this_lowpc
8718 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8719 pdi->lowpc + baseaddr)
8720 - baseaddr);
8721 this_highpc
8722 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8723 pdi->highpc + baseaddr)
8724 - baseaddr);
8725 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8726 this_lowpc, this_highpc - 1,
8727 cu->per_cu->v.psymtab);
8728 }
8729 }
8730
8731 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8732 {
8733 if (!pdi->is_declaration)
8734 /* Ignore subprogram DIEs that do not have a name, they are
8735 illegal. Do not emit a complaint at this point, we will
8736 do so when we convert this psymtab into a symtab. */
8737 if (pdi->name (cu))
8738 add_partial_symbol (pdi, cu);
8739 }
8740 }
8741
8742 if (! pdi->has_children)
8743 return;
8744
8745 if (cu->language == language_ada || cu->language == language_fortran)
8746 {
8747 pdi = pdi->die_child;
8748 while (pdi != NULL)
8749 {
8750 pdi->fixup (cu);
8751 if (pdi->tag == DW_TAG_subprogram
8752 || pdi->tag == DW_TAG_inlined_subroutine
8753 || pdi->tag == DW_TAG_lexical_block)
8754 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8755 pdi = pdi->die_sibling;
8756 }
8757 }
8758 }
8759
8760 /* Read a partial die corresponding to an enumeration type. */
8761
8762 static void
8763 add_partial_enumeration (struct partial_die_info *enum_pdi,
8764 struct dwarf2_cu *cu)
8765 {
8766 struct partial_die_info *pdi;
8767
8768 if (enum_pdi->name (cu) != NULL)
8769 add_partial_symbol (enum_pdi, cu);
8770
8771 pdi = enum_pdi->die_child;
8772 while (pdi)
8773 {
8774 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8775 complaint (_("malformed enumerator DIE ignored"));
8776 else
8777 add_partial_symbol (pdi, cu);
8778 pdi = pdi->die_sibling;
8779 }
8780 }
8781
8782 /* Return the initial uleb128 in the die at INFO_PTR. */
8783
8784 static unsigned int
8785 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8786 {
8787 unsigned int bytes_read;
8788
8789 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8790 }
8791
8792 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8793 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8794
8795 Return the corresponding abbrev, or NULL if the number is zero (indicating
8796 an empty DIE). In either case *BYTES_READ will be set to the length of
8797 the initial number. */
8798
8799 static struct abbrev_info *
8800 peek_die_abbrev (const die_reader_specs &reader,
8801 const gdb_byte *info_ptr, unsigned int *bytes_read)
8802 {
8803 dwarf2_cu *cu = reader.cu;
8804 bfd *abfd = cu->per_objfile->objfile->obfd;
8805 unsigned int abbrev_number
8806 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8807
8808 if (abbrev_number == 0)
8809 return NULL;
8810
8811 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8812 if (!abbrev)
8813 {
8814 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8815 " at offset %s [in module %s]"),
8816 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8817 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8818 }
8819
8820 return abbrev;
8821 }
8822
8823 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8824 Returns a pointer to the end of a series of DIEs, terminated by an empty
8825 DIE. Any children of the skipped DIEs will also be skipped. */
8826
8827 static const gdb_byte *
8828 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8829 {
8830 while (1)
8831 {
8832 unsigned int bytes_read;
8833 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8834
8835 if (abbrev == NULL)
8836 return info_ptr + bytes_read;
8837 else
8838 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8839 }
8840 }
8841
8842 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8843 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8844 abbrev corresponding to that skipped uleb128 should be passed in
8845 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8846 children. */
8847
8848 static const gdb_byte *
8849 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8850 struct abbrev_info *abbrev)
8851 {
8852 unsigned int bytes_read;
8853 struct attribute attr;
8854 bfd *abfd = reader->abfd;
8855 struct dwarf2_cu *cu = reader->cu;
8856 const gdb_byte *buffer = reader->buffer;
8857 const gdb_byte *buffer_end = reader->buffer_end;
8858 unsigned int form, i;
8859
8860 for (i = 0; i < abbrev->num_attrs; i++)
8861 {
8862 /* The only abbrev we care about is DW_AT_sibling. */
8863 if (abbrev->attrs[i].name == DW_AT_sibling)
8864 {
8865 bool ignored;
8866 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8867 &ignored);
8868 if (attr.form == DW_FORM_ref_addr)
8869 complaint (_("ignoring absolute DW_AT_sibling"));
8870 else
8871 {
8872 sect_offset off = attr.get_ref_die_offset ();
8873 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8874
8875 if (sibling_ptr < info_ptr)
8876 complaint (_("DW_AT_sibling points backwards"));
8877 else if (sibling_ptr > reader->buffer_end)
8878 reader->die_section->overflow_complaint ();
8879 else
8880 return sibling_ptr;
8881 }
8882 }
8883
8884 /* If it isn't DW_AT_sibling, skip this attribute. */
8885 form = abbrev->attrs[i].form;
8886 skip_attribute:
8887 switch (form)
8888 {
8889 case DW_FORM_ref_addr:
8890 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8891 and later it is offset sized. */
8892 if (cu->header.version == 2)
8893 info_ptr += cu->header.addr_size;
8894 else
8895 info_ptr += cu->header.offset_size;
8896 break;
8897 case DW_FORM_GNU_ref_alt:
8898 info_ptr += cu->header.offset_size;
8899 break;
8900 case DW_FORM_addr:
8901 info_ptr += cu->header.addr_size;
8902 break;
8903 case DW_FORM_data1:
8904 case DW_FORM_ref1:
8905 case DW_FORM_flag:
8906 case DW_FORM_strx1:
8907 info_ptr += 1;
8908 break;
8909 case DW_FORM_flag_present:
8910 case DW_FORM_implicit_const:
8911 break;
8912 case DW_FORM_data2:
8913 case DW_FORM_ref2:
8914 case DW_FORM_strx2:
8915 info_ptr += 2;
8916 break;
8917 case DW_FORM_strx3:
8918 info_ptr += 3;
8919 break;
8920 case DW_FORM_data4:
8921 case DW_FORM_ref4:
8922 case DW_FORM_strx4:
8923 info_ptr += 4;
8924 break;
8925 case DW_FORM_data8:
8926 case DW_FORM_ref8:
8927 case DW_FORM_ref_sig8:
8928 info_ptr += 8;
8929 break;
8930 case DW_FORM_data16:
8931 info_ptr += 16;
8932 break;
8933 case DW_FORM_string:
8934 read_direct_string (abfd, info_ptr, &bytes_read);
8935 info_ptr += bytes_read;
8936 break;
8937 case DW_FORM_sec_offset:
8938 case DW_FORM_strp:
8939 case DW_FORM_GNU_strp_alt:
8940 info_ptr += cu->header.offset_size;
8941 break;
8942 case DW_FORM_exprloc:
8943 case DW_FORM_block:
8944 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8945 info_ptr += bytes_read;
8946 break;
8947 case DW_FORM_block1:
8948 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8949 break;
8950 case DW_FORM_block2:
8951 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8952 break;
8953 case DW_FORM_block4:
8954 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8955 break;
8956 case DW_FORM_addrx:
8957 case DW_FORM_strx:
8958 case DW_FORM_sdata:
8959 case DW_FORM_udata:
8960 case DW_FORM_ref_udata:
8961 case DW_FORM_GNU_addr_index:
8962 case DW_FORM_GNU_str_index:
8963 case DW_FORM_rnglistx:
8964 case DW_FORM_loclistx:
8965 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8966 break;
8967 case DW_FORM_indirect:
8968 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8969 info_ptr += bytes_read;
8970 /* We need to continue parsing from here, so just go back to
8971 the top. */
8972 goto skip_attribute;
8973
8974 default:
8975 error (_("Dwarf Error: Cannot handle %s "
8976 "in DWARF reader [in module %s]"),
8977 dwarf_form_name (form),
8978 bfd_get_filename (abfd));
8979 }
8980 }
8981
8982 if (abbrev->has_children)
8983 return skip_children (reader, info_ptr);
8984 else
8985 return info_ptr;
8986 }
8987
8988 /* Locate ORIG_PDI's sibling.
8989 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8990
8991 static const gdb_byte *
8992 locate_pdi_sibling (const struct die_reader_specs *reader,
8993 struct partial_die_info *orig_pdi,
8994 const gdb_byte *info_ptr)
8995 {
8996 /* Do we know the sibling already? */
8997
8998 if (orig_pdi->sibling)
8999 return orig_pdi->sibling;
9000
9001 /* Are there any children to deal with? */
9002
9003 if (!orig_pdi->has_children)
9004 return info_ptr;
9005
9006 /* Skip the children the long way. */
9007
9008 return skip_children (reader, info_ptr);
9009 }
9010
9011 /* Expand this partial symbol table into a full symbol table. SELF is
9012 not NULL. */
9013
9014 void
9015 dwarf2_psymtab::read_symtab (struct objfile *objfile)
9016 {
9017 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9018
9019 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
9020
9021 /* If this psymtab is constructed from a debug-only objfile, the
9022 has_section_at_zero flag will not necessarily be correct. We
9023 can get the correct value for this flag by looking at the data
9024 associated with the (presumably stripped) associated objfile. */
9025 if (objfile->separate_debug_objfile_backlink)
9026 {
9027 dwarf2_per_objfile *per_objfile_backlink
9028 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9029
9030 per_objfile->per_bfd->has_section_at_zero
9031 = per_objfile_backlink->per_bfd->has_section_at_zero;
9032 }
9033
9034 expand_psymtab (objfile);
9035
9036 process_cu_includes (per_objfile);
9037 }
9038 \f
9039 /* Reading in full CUs. */
9040
9041 /* Add PER_CU to the queue. */
9042
9043 static void
9044 queue_comp_unit (dwarf2_per_cu_data *per_cu,
9045 dwarf2_per_objfile *per_objfile,
9046 enum language pretend_language)
9047 {
9048 per_cu->queued = 1;
9049 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
9050 }
9051
9052 /* If PER_CU is not yet queued, add it to the queue.
9053 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9054 dependency.
9055 The result is non-zero if PER_CU was queued, otherwise the result is zero
9056 meaning either PER_CU is already queued or it is already loaded.
9057
9058 N.B. There is an invariant here that if a CU is queued then it is loaded.
9059 The caller is required to load PER_CU if we return non-zero. */
9060
9061 static int
9062 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9063 dwarf2_per_cu_data *per_cu,
9064 dwarf2_per_objfile *per_objfile,
9065 enum language pretend_language)
9066 {
9067 /* We may arrive here during partial symbol reading, if we need full
9068 DIEs to process an unusual case (e.g. template arguments). Do
9069 not queue PER_CU, just tell our caller to load its DIEs. */
9070 if (per_cu->per_bfd->reading_partial_symbols)
9071 {
9072 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9073
9074 if (cu == NULL || cu->dies == NULL)
9075 return 1;
9076 return 0;
9077 }
9078
9079 /* Mark the dependence relation so that we don't flush PER_CU
9080 too early. */
9081 if (dependent_cu != NULL)
9082 dwarf2_add_dependence (dependent_cu, per_cu);
9083
9084 /* If it's already on the queue, we have nothing to do. */
9085 if (per_cu->queued)
9086 return 0;
9087
9088 /* If the compilation unit is already loaded, just mark it as
9089 used. */
9090 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9091 if (cu != nullptr)
9092 {
9093 cu->last_used = 0;
9094 return 0;
9095 }
9096
9097 /* Add it to the queue. */
9098 queue_comp_unit (per_cu, per_objfile, pretend_language);
9099
9100 return 1;
9101 }
9102
9103 /* Process the queue. */
9104
9105 static void
9106 process_queue (dwarf2_per_objfile *per_objfile)
9107 {
9108 if (dwarf_read_debug)
9109 {
9110 fprintf_unfiltered (gdb_stdlog,
9111 "Expanding one or more symtabs of objfile %s ...\n",
9112 objfile_name (per_objfile->objfile));
9113 }
9114
9115 /* The queue starts out with one item, but following a DIE reference
9116 may load a new CU, adding it to the end of the queue. */
9117 while (!per_objfile->per_bfd->queue.empty ())
9118 {
9119 dwarf2_queue_item &item = per_objfile->per_bfd->queue.front ();
9120 dwarf2_per_cu_data *per_cu = item.per_cu;
9121
9122 if (!per_objfile->symtab_set_p (per_cu))
9123 {
9124 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9125
9126 /* Skip dummy CUs. */
9127 if (cu != nullptr)
9128 {
9129 unsigned int debug_print_threshold;
9130 char buf[100];
9131
9132 if (per_cu->is_debug_types)
9133 {
9134 struct signatured_type *sig_type =
9135 (struct signatured_type *) per_cu;
9136
9137 sprintf (buf, "TU %s at offset %s",
9138 hex_string (sig_type->signature),
9139 sect_offset_str (per_cu->sect_off));
9140 /* There can be 100s of TUs.
9141 Only print them in verbose mode. */
9142 debug_print_threshold = 2;
9143 }
9144 else
9145 {
9146 sprintf (buf, "CU at offset %s",
9147 sect_offset_str (per_cu->sect_off));
9148 debug_print_threshold = 1;
9149 }
9150
9151 if (dwarf_read_debug >= debug_print_threshold)
9152 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9153
9154 if (per_cu->is_debug_types)
9155 process_full_type_unit (cu, item.pretend_language);
9156 else
9157 process_full_comp_unit (cu, item.pretend_language);
9158
9159 if (dwarf_read_debug >= debug_print_threshold)
9160 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9161 }
9162 }
9163
9164 per_cu->queued = 0;
9165 per_objfile->per_bfd->queue.pop ();
9166 }
9167
9168 if (dwarf_read_debug)
9169 {
9170 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9171 objfile_name (per_objfile->objfile));
9172 }
9173 }
9174
9175 /* Read in full symbols for PST, and anything it depends on. */
9176
9177 void
9178 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9179 {
9180 gdb_assert (!readin_p (objfile));
9181
9182 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9183 free_cached_comp_units freer (per_objfile);
9184 expand_dependencies (objfile);
9185
9186 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9187 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9188 }
9189
9190 /* See psympriv.h. */
9191
9192 bool
9193 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9194 {
9195 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9196 return per_objfile->symtab_set_p (per_cu_data);
9197 }
9198
9199 /* See psympriv.h. */
9200
9201 compunit_symtab *
9202 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9203 {
9204 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9205 return per_objfile->get_symtab (per_cu_data);
9206 }
9207
9208 /* Trivial hash function for die_info: the hash value of a DIE
9209 is its offset in .debug_info for this objfile. */
9210
9211 static hashval_t
9212 die_hash (const void *item)
9213 {
9214 const struct die_info *die = (const struct die_info *) item;
9215
9216 return to_underlying (die->sect_off);
9217 }
9218
9219 /* Trivial comparison function for die_info structures: two DIEs
9220 are equal if they have the same offset. */
9221
9222 static int
9223 die_eq (const void *item_lhs, const void *item_rhs)
9224 {
9225 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9226 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9227
9228 return die_lhs->sect_off == die_rhs->sect_off;
9229 }
9230
9231 /* Load the DIEs associated with PER_CU into memory. */
9232
9233 static void
9234 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9235 dwarf2_per_objfile *per_objfile,
9236 bool skip_partial,
9237 enum language pretend_language)
9238 {
9239 gdb_assert (! this_cu->is_debug_types);
9240
9241 dwarf2_cu *existing_cu = per_objfile->get_cu (this_cu);
9242 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
9243 if (reader.dummy_p)
9244 return;
9245
9246 struct dwarf2_cu *cu = reader.cu;
9247 const gdb_byte *info_ptr = reader.info_ptr;
9248
9249 gdb_assert (cu->die_hash == NULL);
9250 cu->die_hash =
9251 htab_create_alloc_ex (cu->header.length / 12,
9252 die_hash,
9253 die_eq,
9254 NULL,
9255 &cu->comp_unit_obstack,
9256 hashtab_obstack_allocate,
9257 dummy_obstack_deallocate);
9258
9259 if (reader.comp_unit_die->has_children)
9260 reader.comp_unit_die->child
9261 = read_die_and_siblings (&reader, reader.info_ptr,
9262 &info_ptr, reader.comp_unit_die);
9263 cu->dies = reader.comp_unit_die;
9264 /* comp_unit_die is not stored in die_hash, no need. */
9265
9266 /* We try not to read any attributes in this function, because not
9267 all CUs needed for references have been loaded yet, and symbol
9268 table processing isn't initialized. But we have to set the CU language,
9269 or we won't be able to build types correctly.
9270 Similarly, if we do not read the producer, we can not apply
9271 producer-specific interpretation. */
9272 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9273
9274 reader.keep ();
9275 }
9276
9277 /* Add a DIE to the delayed physname list. */
9278
9279 static void
9280 add_to_method_list (struct type *type, int fnfield_index, int index,
9281 const char *name, struct die_info *die,
9282 struct dwarf2_cu *cu)
9283 {
9284 struct delayed_method_info mi;
9285 mi.type = type;
9286 mi.fnfield_index = fnfield_index;
9287 mi.index = index;
9288 mi.name = name;
9289 mi.die = die;
9290 cu->method_list.push_back (mi);
9291 }
9292
9293 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9294 "const" / "volatile". If so, decrements LEN by the length of the
9295 modifier and return true. Otherwise return false. */
9296
9297 template<size_t N>
9298 static bool
9299 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9300 {
9301 size_t mod_len = sizeof (mod) - 1;
9302 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9303 {
9304 len -= mod_len;
9305 return true;
9306 }
9307 return false;
9308 }
9309
9310 /* Compute the physnames of any methods on the CU's method list.
9311
9312 The computation of method physnames is delayed in order to avoid the
9313 (bad) condition that one of the method's formal parameters is of an as yet
9314 incomplete type. */
9315
9316 static void
9317 compute_delayed_physnames (struct dwarf2_cu *cu)
9318 {
9319 /* Only C++ delays computing physnames. */
9320 if (cu->method_list.empty ())
9321 return;
9322 gdb_assert (cu->language == language_cplus);
9323
9324 for (const delayed_method_info &mi : cu->method_list)
9325 {
9326 const char *physname;
9327 struct fn_fieldlist *fn_flp
9328 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9329 physname = dwarf2_physname (mi.name, mi.die, cu);
9330 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9331 = physname ? physname : "";
9332
9333 /* Since there's no tag to indicate whether a method is a
9334 const/volatile overload, extract that information out of the
9335 demangled name. */
9336 if (physname != NULL)
9337 {
9338 size_t len = strlen (physname);
9339
9340 while (1)
9341 {
9342 if (physname[len] == ')') /* shortcut */
9343 break;
9344 else if (check_modifier (physname, len, " const"))
9345 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9346 else if (check_modifier (physname, len, " volatile"))
9347 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9348 else
9349 break;
9350 }
9351 }
9352 }
9353
9354 /* The list is no longer needed. */
9355 cu->method_list.clear ();
9356 }
9357
9358 /* Go objects should be embedded in a DW_TAG_module DIE,
9359 and it's not clear if/how imported objects will appear.
9360 To keep Go support simple until that's worked out,
9361 go back through what we've read and create something usable.
9362 We could do this while processing each DIE, and feels kinda cleaner,
9363 but that way is more invasive.
9364 This is to, for example, allow the user to type "p var" or "b main"
9365 without having to specify the package name, and allow lookups
9366 of module.object to work in contexts that use the expression
9367 parser. */
9368
9369 static void
9370 fixup_go_packaging (struct dwarf2_cu *cu)
9371 {
9372 gdb::unique_xmalloc_ptr<char> package_name;
9373 struct pending *list;
9374 int i;
9375
9376 for (list = *cu->get_builder ()->get_global_symbols ();
9377 list != NULL;
9378 list = list->next)
9379 {
9380 for (i = 0; i < list->nsyms; ++i)
9381 {
9382 struct symbol *sym = list->symbol[i];
9383
9384 if (sym->language () == language_go
9385 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9386 {
9387 gdb::unique_xmalloc_ptr<char> this_package_name
9388 (go_symbol_package_name (sym));
9389
9390 if (this_package_name == NULL)
9391 continue;
9392 if (package_name == NULL)
9393 package_name = std::move (this_package_name);
9394 else
9395 {
9396 struct objfile *objfile = cu->per_objfile->objfile;
9397 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9398 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9399 (symbol_symtab (sym) != NULL
9400 ? symtab_to_filename_for_display
9401 (symbol_symtab (sym))
9402 : objfile_name (objfile)),
9403 this_package_name.get (), package_name.get ());
9404 }
9405 }
9406 }
9407 }
9408
9409 if (package_name != NULL)
9410 {
9411 struct objfile *objfile = cu->per_objfile->objfile;
9412 const char *saved_package_name = objfile->intern (package_name.get ());
9413 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9414 saved_package_name);
9415 struct symbol *sym;
9416
9417 sym = new (&objfile->objfile_obstack) symbol;
9418 sym->set_language (language_go, &objfile->objfile_obstack);
9419 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9420 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9421 e.g., "main" finds the "main" module and not C's main(). */
9422 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9423 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9424 SYMBOL_TYPE (sym) = type;
9425
9426 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9427 }
9428 }
9429
9430 /* Allocate a fully-qualified name consisting of the two parts on the
9431 obstack. */
9432
9433 static const char *
9434 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9435 {
9436 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9437 }
9438
9439 /* A helper that allocates a variant part to attach to a Rust enum
9440 type. OBSTACK is where the results should be allocated. TYPE is
9441 the type we're processing. DISCRIMINANT_INDEX is the index of the
9442 discriminant. It must be the index of one of the fields of TYPE,
9443 or -1 to mean there is no discriminant (univariant enum).
9444 DEFAULT_INDEX is the index of the default field; or -1 if there is
9445 no default. RANGES is indexed by "effective" field number (the
9446 field index, but omitting the discriminant and default fields) and
9447 must hold the discriminant values used by the variants. Note that
9448 RANGES must have a lifetime at least as long as OBSTACK -- either
9449 already allocated on it, or static. */
9450
9451 static void
9452 alloc_rust_variant (struct obstack *obstack, struct type *type,
9453 int discriminant_index, int default_index,
9454 gdb::array_view<discriminant_range> ranges)
9455 {
9456 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9457 gdb_assert (discriminant_index == -1
9458 || (discriminant_index >= 0
9459 && discriminant_index < type->num_fields ()));
9460 gdb_assert (default_index == -1
9461 || (default_index >= 0 && default_index < type->num_fields ()));
9462
9463 /* We have one variant for each non-discriminant field. */
9464 int n_variants = type->num_fields ();
9465 if (discriminant_index != -1)
9466 --n_variants;
9467
9468 variant *variants = new (obstack) variant[n_variants];
9469 int var_idx = 0;
9470 int range_idx = 0;
9471 for (int i = 0; i < type->num_fields (); ++i)
9472 {
9473 if (i == discriminant_index)
9474 continue;
9475
9476 variants[var_idx].first_field = i;
9477 variants[var_idx].last_field = i + 1;
9478
9479 /* The default field does not need a range, but other fields do.
9480 We skipped the discriminant above. */
9481 if (i != default_index)
9482 {
9483 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9484 ++range_idx;
9485 }
9486
9487 ++var_idx;
9488 }
9489
9490 gdb_assert (range_idx == ranges.size ());
9491 gdb_assert (var_idx == n_variants);
9492
9493 variant_part *part = new (obstack) variant_part;
9494 part->discriminant_index = discriminant_index;
9495 /* If there is no discriminant, then whether it is signed is of no
9496 consequence. */
9497 part->is_unsigned
9498 = (discriminant_index == -1
9499 ? false
9500 : type->field (discriminant_index).type ()->is_unsigned ());
9501 part->variants = gdb::array_view<variant> (variants, n_variants);
9502
9503 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9504 gdb::array_view<variant_part> *prop_value
9505 = new (storage) gdb::array_view<variant_part> (part, 1);
9506
9507 struct dynamic_prop prop;
9508 prop.set_variant_parts (prop_value);
9509
9510 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9511 }
9512
9513 /* Some versions of rustc emitted enums in an unusual way.
9514
9515 Ordinary enums were emitted as unions. The first element of each
9516 structure in the union was named "RUST$ENUM$DISR". This element
9517 held the discriminant.
9518
9519 These versions of Rust also implemented the "non-zero"
9520 optimization. When the enum had two values, and one is empty and
9521 the other holds a pointer that cannot be zero, the pointer is used
9522 as the discriminant, with a zero value meaning the empty variant.
9523 Here, the union's first member is of the form
9524 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9525 where the fieldnos are the indices of the fields that should be
9526 traversed in order to find the field (which may be several fields deep)
9527 and the variantname is the name of the variant of the case when the
9528 field is zero.
9529
9530 This function recognizes whether TYPE is of one of these forms,
9531 and, if so, smashes it to be a variant type. */
9532
9533 static void
9534 quirk_rust_enum (struct type *type, struct objfile *objfile)
9535 {
9536 gdb_assert (type->code () == TYPE_CODE_UNION);
9537
9538 /* We don't need to deal with empty enums. */
9539 if (type->num_fields () == 0)
9540 return;
9541
9542 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9543 if (type->num_fields () == 1
9544 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9545 {
9546 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9547
9548 /* Decode the field name to find the offset of the
9549 discriminant. */
9550 ULONGEST bit_offset = 0;
9551 struct type *field_type = type->field (0).type ();
9552 while (name[0] >= '0' && name[0] <= '9')
9553 {
9554 char *tail;
9555 unsigned long index = strtoul (name, &tail, 10);
9556 name = tail;
9557 if (*name != '$'
9558 || index >= field_type->num_fields ()
9559 || (TYPE_FIELD_LOC_KIND (field_type, index)
9560 != FIELD_LOC_KIND_BITPOS))
9561 {
9562 complaint (_("Could not parse Rust enum encoding string \"%s\""
9563 "[in module %s]"),
9564 TYPE_FIELD_NAME (type, 0),
9565 objfile_name (objfile));
9566 return;
9567 }
9568 ++name;
9569
9570 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9571 field_type = field_type->field (index).type ();
9572 }
9573
9574 /* Smash this type to be a structure type. We have to do this
9575 because the type has already been recorded. */
9576 type->set_code (TYPE_CODE_STRUCT);
9577 type->set_num_fields (3);
9578 /* Save the field we care about. */
9579 struct field saved_field = type->field (0);
9580 type->set_fields
9581 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9582
9583 /* Put the discriminant at index 0. */
9584 type->field (0).set_type (field_type);
9585 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9586 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9587 SET_FIELD_BITPOS (type->field (0), bit_offset);
9588
9589 /* The order of fields doesn't really matter, so put the real
9590 field at index 1 and the data-less field at index 2. */
9591 type->field (1) = saved_field;
9592 TYPE_FIELD_NAME (type, 1)
9593 = rust_last_path_segment (type->field (1).type ()->name ());
9594 type->field (1).type ()->set_name
9595 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9596 TYPE_FIELD_NAME (type, 1)));
9597
9598 const char *dataless_name
9599 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9600 name);
9601 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9602 dataless_name);
9603 type->field (2).set_type (dataless_type);
9604 /* NAME points into the original discriminant name, which
9605 already has the correct lifetime. */
9606 TYPE_FIELD_NAME (type, 2) = name;
9607 SET_FIELD_BITPOS (type->field (2), 0);
9608
9609 /* Indicate that this is a variant type. */
9610 static discriminant_range ranges[1] = { { 0, 0 } };
9611 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9612 }
9613 /* A union with a single anonymous field is probably an old-style
9614 univariant enum. */
9615 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9616 {
9617 /* Smash this type to be a structure type. We have to do this
9618 because the type has already been recorded. */
9619 type->set_code (TYPE_CODE_STRUCT);
9620
9621 struct type *field_type = type->field (0).type ();
9622 const char *variant_name
9623 = rust_last_path_segment (field_type->name ());
9624 TYPE_FIELD_NAME (type, 0) = variant_name;
9625 field_type->set_name
9626 (rust_fully_qualify (&objfile->objfile_obstack,
9627 type->name (), variant_name));
9628
9629 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
9630 }
9631 else
9632 {
9633 struct type *disr_type = nullptr;
9634 for (int i = 0; i < type->num_fields (); ++i)
9635 {
9636 disr_type = type->field (i).type ();
9637
9638 if (disr_type->code () != TYPE_CODE_STRUCT)
9639 {
9640 /* All fields of a true enum will be structs. */
9641 return;
9642 }
9643 else if (disr_type->num_fields () == 0)
9644 {
9645 /* Could be data-less variant, so keep going. */
9646 disr_type = nullptr;
9647 }
9648 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9649 "RUST$ENUM$DISR") != 0)
9650 {
9651 /* Not a Rust enum. */
9652 return;
9653 }
9654 else
9655 {
9656 /* Found one. */
9657 break;
9658 }
9659 }
9660
9661 /* If we got here without a discriminant, then it's probably
9662 just a union. */
9663 if (disr_type == nullptr)
9664 return;
9665
9666 /* Smash this type to be a structure type. We have to do this
9667 because the type has already been recorded. */
9668 type->set_code (TYPE_CODE_STRUCT);
9669
9670 /* Make space for the discriminant field. */
9671 struct field *disr_field = &disr_type->field (0);
9672 field *new_fields
9673 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9674 * sizeof (struct field)));
9675 memcpy (new_fields + 1, type->fields (),
9676 type->num_fields () * sizeof (struct field));
9677 type->set_fields (new_fields);
9678 type->set_num_fields (type->num_fields () + 1);
9679
9680 /* Install the discriminant at index 0 in the union. */
9681 type->field (0) = *disr_field;
9682 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9683 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9684
9685 /* We need a way to find the correct discriminant given a
9686 variant name. For convenience we build a map here. */
9687 struct type *enum_type = disr_field->type ();
9688 std::unordered_map<std::string, ULONGEST> discriminant_map;
9689 for (int i = 0; i < enum_type->num_fields (); ++i)
9690 {
9691 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9692 {
9693 const char *name
9694 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9695 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9696 }
9697 }
9698
9699 int n_fields = type->num_fields ();
9700 /* We don't need a range entry for the discriminant, but we do
9701 need one for every other field, as there is no default
9702 variant. */
9703 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9704 discriminant_range,
9705 n_fields - 1);
9706 /* Skip the discriminant here. */
9707 for (int i = 1; i < n_fields; ++i)
9708 {
9709 /* Find the final word in the name of this variant's type.
9710 That name can be used to look up the correct
9711 discriminant. */
9712 const char *variant_name
9713 = rust_last_path_segment (type->field (i).type ()->name ());
9714
9715 auto iter = discriminant_map.find (variant_name);
9716 if (iter != discriminant_map.end ())
9717 {
9718 ranges[i - 1].low = iter->second;
9719 ranges[i - 1].high = iter->second;
9720 }
9721
9722 /* In Rust, each element should have the size of the
9723 enclosing enum. */
9724 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9725
9726 /* Remove the discriminant field, if it exists. */
9727 struct type *sub_type = type->field (i).type ();
9728 if (sub_type->num_fields () > 0)
9729 {
9730 sub_type->set_num_fields (sub_type->num_fields () - 1);
9731 sub_type->set_fields (sub_type->fields () + 1);
9732 }
9733 TYPE_FIELD_NAME (type, i) = variant_name;
9734 sub_type->set_name
9735 (rust_fully_qualify (&objfile->objfile_obstack,
9736 type->name (), variant_name));
9737 }
9738
9739 /* Indicate that this is a variant type. */
9740 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9741 gdb::array_view<discriminant_range> (ranges,
9742 n_fields - 1));
9743 }
9744 }
9745
9746 /* Rewrite some Rust unions to be structures with variants parts. */
9747
9748 static void
9749 rust_union_quirks (struct dwarf2_cu *cu)
9750 {
9751 gdb_assert (cu->language == language_rust);
9752 for (type *type_ : cu->rust_unions)
9753 quirk_rust_enum (type_, cu->per_objfile->objfile);
9754 /* We don't need this any more. */
9755 cu->rust_unions.clear ();
9756 }
9757
9758 /* See read.h. */
9759
9760 type_unit_group_unshareable *
9761 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9762 {
9763 auto iter = this->m_type_units.find (tu_group);
9764 if (iter != this->m_type_units.end ())
9765 return iter->second.get ();
9766
9767 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9768 type_unit_group_unshareable *result = uniq.get ();
9769 this->m_type_units[tu_group] = std::move (uniq);
9770 return result;
9771 }
9772
9773 struct type *
9774 dwarf2_per_objfile::get_type_for_signatured_type
9775 (signatured_type *sig_type) const
9776 {
9777 auto iter = this->m_type_map.find (sig_type);
9778 if (iter == this->m_type_map.end ())
9779 return nullptr;
9780
9781 return iter->second;
9782 }
9783
9784 void dwarf2_per_objfile::set_type_for_signatured_type
9785 (signatured_type *sig_type, struct type *type)
9786 {
9787 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9788
9789 this->m_type_map[sig_type] = type;
9790 }
9791
9792 /* A helper function for computing the list of all symbol tables
9793 included by PER_CU. */
9794
9795 static void
9796 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9797 htab_t all_children, htab_t all_type_symtabs,
9798 dwarf2_per_cu_data *per_cu,
9799 dwarf2_per_objfile *per_objfile,
9800 struct compunit_symtab *immediate_parent)
9801 {
9802 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9803 if (*slot != NULL)
9804 {
9805 /* This inclusion and its children have been processed. */
9806 return;
9807 }
9808
9809 *slot = per_cu;
9810
9811 /* Only add a CU if it has a symbol table. */
9812 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9813 if (cust != NULL)
9814 {
9815 /* If this is a type unit only add its symbol table if we haven't
9816 seen it yet (type unit per_cu's can share symtabs). */
9817 if (per_cu->is_debug_types)
9818 {
9819 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9820 if (*slot == NULL)
9821 {
9822 *slot = cust;
9823 result->push_back (cust);
9824 if (cust->user == NULL)
9825 cust->user = immediate_parent;
9826 }
9827 }
9828 else
9829 {
9830 result->push_back (cust);
9831 if (cust->user == NULL)
9832 cust->user = immediate_parent;
9833 }
9834 }
9835
9836 if (!per_cu->imported_symtabs_empty ())
9837 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9838 {
9839 recursively_compute_inclusions (result, all_children,
9840 all_type_symtabs, ptr, per_objfile,
9841 cust);
9842 }
9843 }
9844
9845 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9846 PER_CU. */
9847
9848 static void
9849 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9850 dwarf2_per_objfile *per_objfile)
9851 {
9852 gdb_assert (! per_cu->is_debug_types);
9853
9854 if (!per_cu->imported_symtabs_empty ())
9855 {
9856 int len;
9857 std::vector<compunit_symtab *> result_symtabs;
9858 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9859
9860 /* If we don't have a symtab, we can just skip this case. */
9861 if (cust == NULL)
9862 return;
9863
9864 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9865 htab_eq_pointer,
9866 NULL, xcalloc, xfree));
9867 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9868 htab_eq_pointer,
9869 NULL, xcalloc, xfree));
9870
9871 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9872 {
9873 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9874 all_type_symtabs.get (), ptr,
9875 per_objfile, cust);
9876 }
9877
9878 /* Now we have a transitive closure of all the included symtabs. */
9879 len = result_symtabs.size ();
9880 cust->includes
9881 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9882 struct compunit_symtab *, len + 1);
9883 memcpy (cust->includes, result_symtabs.data (),
9884 len * sizeof (compunit_symtab *));
9885 cust->includes[len] = NULL;
9886 }
9887 }
9888
9889 /* Compute the 'includes' field for the symtabs of all the CUs we just
9890 read. */
9891
9892 static void
9893 process_cu_includes (dwarf2_per_objfile *per_objfile)
9894 {
9895 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
9896 {
9897 if (! iter->is_debug_types)
9898 compute_compunit_symtab_includes (iter, per_objfile);
9899 }
9900
9901 per_objfile->per_bfd->just_read_cus.clear ();
9902 }
9903
9904 /* Generate full symbol information for CU, whose DIEs have
9905 already been loaded into memory. */
9906
9907 static void
9908 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9909 {
9910 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9911 struct objfile *objfile = per_objfile->objfile;
9912 struct gdbarch *gdbarch = objfile->arch ();
9913 CORE_ADDR lowpc, highpc;
9914 struct compunit_symtab *cust;
9915 CORE_ADDR baseaddr;
9916 struct block *static_block;
9917 CORE_ADDR addr;
9918
9919 baseaddr = objfile->text_section_offset ();
9920
9921 /* Clear the list here in case something was left over. */
9922 cu->method_list.clear ();
9923
9924 cu->language = pretend_language;
9925 cu->language_defn = language_def (cu->language);
9926
9927 /* Do line number decoding in read_file_scope () */
9928 process_die (cu->dies, cu);
9929
9930 /* For now fudge the Go package. */
9931 if (cu->language == language_go)
9932 fixup_go_packaging (cu);
9933
9934 /* Now that we have processed all the DIEs in the CU, all the types
9935 should be complete, and it should now be safe to compute all of the
9936 physnames. */
9937 compute_delayed_physnames (cu);
9938
9939 if (cu->language == language_rust)
9940 rust_union_quirks (cu);
9941
9942 /* Some compilers don't define a DW_AT_high_pc attribute for the
9943 compilation unit. If the DW_AT_high_pc is missing, synthesize
9944 it, by scanning the DIE's below the compilation unit. */
9945 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9946
9947 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9948 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9949
9950 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9951 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9952 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9953 addrmap to help ensure it has an accurate map of pc values belonging to
9954 this comp unit. */
9955 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9956
9957 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9958 SECT_OFF_TEXT (objfile),
9959 0);
9960
9961 if (cust != NULL)
9962 {
9963 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9964
9965 /* Set symtab language to language from DW_AT_language. If the
9966 compilation is from a C file generated by language preprocessors, do
9967 not set the language if it was already deduced by start_subfile. */
9968 if (!(cu->language == language_c
9969 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9970 COMPUNIT_FILETABS (cust)->language = cu->language;
9971
9972 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9973 produce DW_AT_location with location lists but it can be possibly
9974 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9975 there were bugs in prologue debug info, fixed later in GCC-4.5
9976 by "unwind info for epilogues" patch (which is not directly related).
9977
9978 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9979 needed, it would be wrong due to missing DW_AT_producer there.
9980
9981 Still one can confuse GDB by using non-standard GCC compilation
9982 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9983 */
9984 if (cu->has_loclist && gcc_4_minor >= 5)
9985 cust->locations_valid = 1;
9986
9987 if (gcc_4_minor >= 5)
9988 cust->epilogue_unwind_valid = 1;
9989
9990 cust->call_site_htab = cu->call_site_htab;
9991 }
9992
9993 per_objfile->set_symtab (cu->per_cu, cust);
9994
9995 /* Push it for inclusion processing later. */
9996 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
9997
9998 /* Not needed any more. */
9999 cu->reset_builder ();
10000 }
10001
10002 /* Generate full symbol information for type unit CU, whose DIEs have
10003 already been loaded into memory. */
10004
10005 static void
10006 process_full_type_unit (dwarf2_cu *cu,
10007 enum language pretend_language)
10008 {
10009 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10010 struct objfile *objfile = per_objfile->objfile;
10011 struct compunit_symtab *cust;
10012 struct signatured_type *sig_type;
10013
10014 gdb_assert (cu->per_cu->is_debug_types);
10015 sig_type = (struct signatured_type *) cu->per_cu;
10016
10017 /* Clear the list here in case something was left over. */
10018 cu->method_list.clear ();
10019
10020 cu->language = pretend_language;
10021 cu->language_defn = language_def (cu->language);
10022
10023 /* The symbol tables are set up in read_type_unit_scope. */
10024 process_die (cu->dies, cu);
10025
10026 /* For now fudge the Go package. */
10027 if (cu->language == language_go)
10028 fixup_go_packaging (cu);
10029
10030 /* Now that we have processed all the DIEs in the CU, all the types
10031 should be complete, and it should now be safe to compute all of the
10032 physnames. */
10033 compute_delayed_physnames (cu);
10034
10035 if (cu->language == language_rust)
10036 rust_union_quirks (cu);
10037
10038 /* TUs share symbol tables.
10039 If this is the first TU to use this symtab, complete the construction
10040 of it with end_expandable_symtab. Otherwise, complete the addition of
10041 this TU's symbols to the existing symtab. */
10042 type_unit_group_unshareable *tug_unshare =
10043 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
10044 if (tug_unshare->compunit_symtab == NULL)
10045 {
10046 buildsym_compunit *builder = cu->get_builder ();
10047 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10048 tug_unshare->compunit_symtab = cust;
10049
10050 if (cust != NULL)
10051 {
10052 /* Set symtab language to language from DW_AT_language. If the
10053 compilation is from a C file generated by language preprocessors,
10054 do not set the language if it was already deduced by
10055 start_subfile. */
10056 if (!(cu->language == language_c
10057 && COMPUNIT_FILETABS (cust)->language != language_c))
10058 COMPUNIT_FILETABS (cust)->language = cu->language;
10059 }
10060 }
10061 else
10062 {
10063 cu->get_builder ()->augment_type_symtab ();
10064 cust = tug_unshare->compunit_symtab;
10065 }
10066
10067 per_objfile->set_symtab (cu->per_cu, cust);
10068
10069 /* Not needed any more. */
10070 cu->reset_builder ();
10071 }
10072
10073 /* Process an imported unit DIE. */
10074
10075 static void
10076 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10077 {
10078 struct attribute *attr;
10079
10080 /* For now we don't handle imported units in type units. */
10081 if (cu->per_cu->is_debug_types)
10082 {
10083 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10084 " supported in type units [in module %s]"),
10085 objfile_name (cu->per_objfile->objfile));
10086 }
10087
10088 attr = dwarf2_attr (die, DW_AT_import, cu);
10089 if (attr != NULL)
10090 {
10091 sect_offset sect_off = attr->get_ref_die_offset ();
10092 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10093 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10094 dwarf2_per_cu_data *per_cu
10095 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
10096
10097 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10098 into another compilation unit, at root level. Regard this as a hint,
10099 and ignore it. */
10100 if (die->parent && die->parent->parent == NULL
10101 && per_cu->unit_type == DW_UT_compile
10102 && per_cu->lang == language_cplus)
10103 return;
10104
10105 /* If necessary, add it to the queue and load its DIEs. */
10106 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
10107 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
10108
10109 cu->per_cu->imported_symtabs_push (per_cu);
10110 }
10111 }
10112
10113 /* RAII object that represents a process_die scope: i.e.,
10114 starts/finishes processing a DIE. */
10115 class process_die_scope
10116 {
10117 public:
10118 process_die_scope (die_info *die, dwarf2_cu *cu)
10119 : m_die (die), m_cu (cu)
10120 {
10121 /* We should only be processing DIEs not already in process. */
10122 gdb_assert (!m_die->in_process);
10123 m_die->in_process = true;
10124 }
10125
10126 ~process_die_scope ()
10127 {
10128 m_die->in_process = false;
10129
10130 /* If we're done processing the DIE for the CU that owns the line
10131 header, we don't need the line header anymore. */
10132 if (m_cu->line_header_die_owner == m_die)
10133 {
10134 delete m_cu->line_header;
10135 m_cu->line_header = NULL;
10136 m_cu->line_header_die_owner = NULL;
10137 }
10138 }
10139
10140 private:
10141 die_info *m_die;
10142 dwarf2_cu *m_cu;
10143 };
10144
10145 /* Process a die and its children. */
10146
10147 static void
10148 process_die (struct die_info *die, struct dwarf2_cu *cu)
10149 {
10150 process_die_scope scope (die, cu);
10151
10152 switch (die->tag)
10153 {
10154 case DW_TAG_padding:
10155 break;
10156 case DW_TAG_compile_unit:
10157 case DW_TAG_partial_unit:
10158 read_file_scope (die, cu);
10159 break;
10160 case DW_TAG_type_unit:
10161 read_type_unit_scope (die, cu);
10162 break;
10163 case DW_TAG_subprogram:
10164 /* Nested subprograms in Fortran get a prefix. */
10165 if (cu->language == language_fortran
10166 && die->parent != NULL
10167 && die->parent->tag == DW_TAG_subprogram)
10168 cu->processing_has_namespace_info = true;
10169 /* Fall through. */
10170 case DW_TAG_inlined_subroutine:
10171 read_func_scope (die, cu);
10172 break;
10173 case DW_TAG_lexical_block:
10174 case DW_TAG_try_block:
10175 case DW_TAG_catch_block:
10176 read_lexical_block_scope (die, cu);
10177 break;
10178 case DW_TAG_call_site:
10179 case DW_TAG_GNU_call_site:
10180 read_call_site_scope (die, cu);
10181 break;
10182 case DW_TAG_class_type:
10183 case DW_TAG_interface_type:
10184 case DW_TAG_structure_type:
10185 case DW_TAG_union_type:
10186 process_structure_scope (die, cu);
10187 break;
10188 case DW_TAG_enumeration_type:
10189 process_enumeration_scope (die, cu);
10190 break;
10191
10192 /* These dies have a type, but processing them does not create
10193 a symbol or recurse to process the children. Therefore we can
10194 read them on-demand through read_type_die. */
10195 case DW_TAG_subroutine_type:
10196 case DW_TAG_set_type:
10197 case DW_TAG_array_type:
10198 case DW_TAG_pointer_type:
10199 case DW_TAG_ptr_to_member_type:
10200 case DW_TAG_reference_type:
10201 case DW_TAG_rvalue_reference_type:
10202 case DW_TAG_string_type:
10203 break;
10204
10205 case DW_TAG_base_type:
10206 case DW_TAG_subrange_type:
10207 case DW_TAG_typedef:
10208 /* Add a typedef symbol for the type definition, if it has a
10209 DW_AT_name. */
10210 new_symbol (die, read_type_die (die, cu), cu);
10211 break;
10212 case DW_TAG_common_block:
10213 read_common_block (die, cu);
10214 break;
10215 case DW_TAG_common_inclusion:
10216 break;
10217 case DW_TAG_namespace:
10218 cu->processing_has_namespace_info = true;
10219 read_namespace (die, cu);
10220 break;
10221 case DW_TAG_module:
10222 cu->processing_has_namespace_info = true;
10223 read_module (die, cu);
10224 break;
10225 case DW_TAG_imported_declaration:
10226 cu->processing_has_namespace_info = true;
10227 if (read_namespace_alias (die, cu))
10228 break;
10229 /* The declaration is not a global namespace alias. */
10230 /* Fall through. */
10231 case DW_TAG_imported_module:
10232 cu->processing_has_namespace_info = true;
10233 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10234 || cu->language != language_fortran))
10235 complaint (_("Tag '%s' has unexpected children"),
10236 dwarf_tag_name (die->tag));
10237 read_import_statement (die, cu);
10238 break;
10239
10240 case DW_TAG_imported_unit:
10241 process_imported_unit_die (die, cu);
10242 break;
10243
10244 case DW_TAG_variable:
10245 read_variable (die, cu);
10246 break;
10247
10248 default:
10249 new_symbol (die, NULL, cu);
10250 break;
10251 }
10252 }
10253 \f
10254 /* DWARF name computation. */
10255
10256 /* A helper function for dwarf2_compute_name which determines whether DIE
10257 needs to have the name of the scope prepended to the name listed in the
10258 die. */
10259
10260 static int
10261 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10262 {
10263 struct attribute *attr;
10264
10265 switch (die->tag)
10266 {
10267 case DW_TAG_namespace:
10268 case DW_TAG_typedef:
10269 case DW_TAG_class_type:
10270 case DW_TAG_interface_type:
10271 case DW_TAG_structure_type:
10272 case DW_TAG_union_type:
10273 case DW_TAG_enumeration_type:
10274 case DW_TAG_enumerator:
10275 case DW_TAG_subprogram:
10276 case DW_TAG_inlined_subroutine:
10277 case DW_TAG_member:
10278 case DW_TAG_imported_declaration:
10279 return 1;
10280
10281 case DW_TAG_variable:
10282 case DW_TAG_constant:
10283 /* We only need to prefix "globally" visible variables. These include
10284 any variable marked with DW_AT_external or any variable that
10285 lives in a namespace. [Variables in anonymous namespaces
10286 require prefixing, but they are not DW_AT_external.] */
10287
10288 if (dwarf2_attr (die, DW_AT_specification, cu))
10289 {
10290 struct dwarf2_cu *spec_cu = cu;
10291
10292 return die_needs_namespace (die_specification (die, &spec_cu),
10293 spec_cu);
10294 }
10295
10296 attr = dwarf2_attr (die, DW_AT_external, cu);
10297 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10298 && die->parent->tag != DW_TAG_module)
10299 return 0;
10300 /* A variable in a lexical block of some kind does not need a
10301 namespace, even though in C++ such variables may be external
10302 and have a mangled name. */
10303 if (die->parent->tag == DW_TAG_lexical_block
10304 || die->parent->tag == DW_TAG_try_block
10305 || die->parent->tag == DW_TAG_catch_block
10306 || die->parent->tag == DW_TAG_subprogram)
10307 return 0;
10308 return 1;
10309
10310 default:
10311 return 0;
10312 }
10313 }
10314
10315 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10316 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10317 defined for the given DIE. */
10318
10319 static struct attribute *
10320 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10321 {
10322 struct attribute *attr;
10323
10324 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10325 if (attr == NULL)
10326 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10327
10328 return attr;
10329 }
10330
10331 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10332 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10333 defined for the given DIE. */
10334
10335 static const char *
10336 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10337 {
10338 const char *linkage_name;
10339
10340 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10341 if (linkage_name == NULL)
10342 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10343
10344 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10345 See https://github.com/rust-lang/rust/issues/32925. */
10346 if (cu->language == language_rust && linkage_name != NULL
10347 && strchr (linkage_name, '{') != NULL)
10348 linkage_name = NULL;
10349
10350 return linkage_name;
10351 }
10352
10353 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10354 compute the physname for the object, which include a method's:
10355 - formal parameters (C++),
10356 - receiver type (Go),
10357
10358 The term "physname" is a bit confusing.
10359 For C++, for example, it is the demangled name.
10360 For Go, for example, it's the mangled name.
10361
10362 For Ada, return the DIE's linkage name rather than the fully qualified
10363 name. PHYSNAME is ignored..
10364
10365 The result is allocated on the objfile->per_bfd's obstack and
10366 canonicalized. */
10367
10368 static const char *
10369 dwarf2_compute_name (const char *name,
10370 struct die_info *die, struct dwarf2_cu *cu,
10371 int physname)
10372 {
10373 struct objfile *objfile = cu->per_objfile->objfile;
10374
10375 if (name == NULL)
10376 name = dwarf2_name (die, cu);
10377
10378 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10379 but otherwise compute it by typename_concat inside GDB.
10380 FIXME: Actually this is not really true, or at least not always true.
10381 It's all very confusing. compute_and_set_names doesn't try to demangle
10382 Fortran names because there is no mangling standard. So new_symbol
10383 will set the demangled name to the result of dwarf2_full_name, and it is
10384 the demangled name that GDB uses if it exists. */
10385 if (cu->language == language_ada
10386 || (cu->language == language_fortran && physname))
10387 {
10388 /* For Ada unit, we prefer the linkage name over the name, as
10389 the former contains the exported name, which the user expects
10390 to be able to reference. Ideally, we want the user to be able
10391 to reference this entity using either natural or linkage name,
10392 but we haven't started looking at this enhancement yet. */
10393 const char *linkage_name = dw2_linkage_name (die, cu);
10394
10395 if (linkage_name != NULL)
10396 return linkage_name;
10397 }
10398
10399 /* These are the only languages we know how to qualify names in. */
10400 if (name != NULL
10401 && (cu->language == language_cplus
10402 || cu->language == language_fortran || cu->language == language_d
10403 || cu->language == language_rust))
10404 {
10405 if (die_needs_namespace (die, cu))
10406 {
10407 const char *prefix;
10408 const char *canonical_name = NULL;
10409
10410 string_file buf;
10411
10412 prefix = determine_prefix (die, cu);
10413 if (*prefix != '\0')
10414 {
10415 gdb::unique_xmalloc_ptr<char> prefixed_name
10416 (typename_concat (NULL, prefix, name, physname, cu));
10417
10418 buf.puts (prefixed_name.get ());
10419 }
10420 else
10421 buf.puts (name);
10422
10423 /* Template parameters may be specified in the DIE's DW_AT_name, or
10424 as children with DW_TAG_template_type_param or
10425 DW_TAG_value_type_param. If the latter, add them to the name
10426 here. If the name already has template parameters, then
10427 skip this step; some versions of GCC emit both, and
10428 it is more efficient to use the pre-computed name.
10429
10430 Something to keep in mind about this process: it is very
10431 unlikely, or in some cases downright impossible, to produce
10432 something that will match the mangled name of a function.
10433 If the definition of the function has the same debug info,
10434 we should be able to match up with it anyway. But fallbacks
10435 using the minimal symbol, for instance to find a method
10436 implemented in a stripped copy of libstdc++, will not work.
10437 If we do not have debug info for the definition, we will have to
10438 match them up some other way.
10439
10440 When we do name matching there is a related problem with function
10441 templates; two instantiated function templates are allowed to
10442 differ only by their return types, which we do not add here. */
10443
10444 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10445 {
10446 struct attribute *attr;
10447 struct die_info *child;
10448 int first = 1;
10449
10450 die->building_fullname = 1;
10451
10452 for (child = die->child; child != NULL; child = child->sibling)
10453 {
10454 struct type *type;
10455 LONGEST value;
10456 const gdb_byte *bytes;
10457 struct dwarf2_locexpr_baton *baton;
10458 struct value *v;
10459
10460 if (child->tag != DW_TAG_template_type_param
10461 && child->tag != DW_TAG_template_value_param)
10462 continue;
10463
10464 if (first)
10465 {
10466 buf.puts ("<");
10467 first = 0;
10468 }
10469 else
10470 buf.puts (", ");
10471
10472 attr = dwarf2_attr (child, DW_AT_type, cu);
10473 if (attr == NULL)
10474 {
10475 complaint (_("template parameter missing DW_AT_type"));
10476 buf.puts ("UNKNOWN_TYPE");
10477 continue;
10478 }
10479 type = die_type (child, cu);
10480
10481 if (child->tag == DW_TAG_template_type_param)
10482 {
10483 c_print_type (type, "", &buf, -1, 0, cu->language,
10484 &type_print_raw_options);
10485 continue;
10486 }
10487
10488 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10489 if (attr == NULL)
10490 {
10491 complaint (_("template parameter missing "
10492 "DW_AT_const_value"));
10493 buf.puts ("UNKNOWN_VALUE");
10494 continue;
10495 }
10496
10497 dwarf2_const_value_attr (attr, type, name,
10498 &cu->comp_unit_obstack, cu,
10499 &value, &bytes, &baton);
10500
10501 if (type->has_no_signedness ())
10502 /* GDB prints characters as NUMBER 'CHAR'. If that's
10503 changed, this can use value_print instead. */
10504 c_printchar (value, type, &buf);
10505 else
10506 {
10507 struct value_print_options opts;
10508
10509 if (baton != NULL)
10510 v = dwarf2_evaluate_loc_desc (type, NULL,
10511 baton->data,
10512 baton->size,
10513 baton->per_cu,
10514 baton->per_objfile);
10515 else if (bytes != NULL)
10516 {
10517 v = allocate_value (type);
10518 memcpy (value_contents_writeable (v), bytes,
10519 TYPE_LENGTH (type));
10520 }
10521 else
10522 v = value_from_longest (type, value);
10523
10524 /* Specify decimal so that we do not depend on
10525 the radix. */
10526 get_formatted_print_options (&opts, 'd');
10527 opts.raw = 1;
10528 value_print (v, &buf, &opts);
10529 release_value (v);
10530 }
10531 }
10532
10533 die->building_fullname = 0;
10534
10535 if (!first)
10536 {
10537 /* Close the argument list, with a space if necessary
10538 (nested templates). */
10539 if (!buf.empty () && buf.string ().back () == '>')
10540 buf.puts (" >");
10541 else
10542 buf.puts (">");
10543 }
10544 }
10545
10546 /* For C++ methods, append formal parameter type
10547 information, if PHYSNAME. */
10548
10549 if (physname && die->tag == DW_TAG_subprogram
10550 && cu->language == language_cplus)
10551 {
10552 struct type *type = read_type_die (die, cu);
10553
10554 c_type_print_args (type, &buf, 1, cu->language,
10555 &type_print_raw_options);
10556
10557 if (cu->language == language_cplus)
10558 {
10559 /* Assume that an artificial first parameter is
10560 "this", but do not crash if it is not. RealView
10561 marks unnamed (and thus unused) parameters as
10562 artificial; there is no way to differentiate
10563 the two cases. */
10564 if (type->num_fields () > 0
10565 && TYPE_FIELD_ARTIFICIAL (type, 0)
10566 && type->field (0).type ()->code () == TYPE_CODE_PTR
10567 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
10568 buf.puts (" const");
10569 }
10570 }
10571
10572 const std::string &intermediate_name = buf.string ();
10573
10574 if (cu->language == language_cplus)
10575 canonical_name
10576 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10577 objfile);
10578
10579 /* If we only computed INTERMEDIATE_NAME, or if
10580 INTERMEDIATE_NAME is already canonical, then we need to
10581 intern it. */
10582 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10583 name = objfile->intern (intermediate_name);
10584 else
10585 name = canonical_name;
10586 }
10587 }
10588
10589 return name;
10590 }
10591
10592 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10593 If scope qualifiers are appropriate they will be added. The result
10594 will be allocated on the storage_obstack, or NULL if the DIE does
10595 not have a name. NAME may either be from a previous call to
10596 dwarf2_name or NULL.
10597
10598 The output string will be canonicalized (if C++). */
10599
10600 static const char *
10601 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10602 {
10603 return dwarf2_compute_name (name, die, cu, 0);
10604 }
10605
10606 /* Construct a physname for the given DIE in CU. NAME may either be
10607 from a previous call to dwarf2_name or NULL. The result will be
10608 allocated on the objfile_objstack or NULL if the DIE does not have a
10609 name.
10610
10611 The output string will be canonicalized (if C++). */
10612
10613 static const char *
10614 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10615 {
10616 struct objfile *objfile = cu->per_objfile->objfile;
10617 const char *retval, *mangled = NULL, *canon = NULL;
10618 int need_copy = 1;
10619
10620 /* In this case dwarf2_compute_name is just a shortcut not building anything
10621 on its own. */
10622 if (!die_needs_namespace (die, cu))
10623 return dwarf2_compute_name (name, die, cu, 1);
10624
10625 if (cu->language != language_rust)
10626 mangled = dw2_linkage_name (die, cu);
10627
10628 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10629 has computed. */
10630 gdb::unique_xmalloc_ptr<char> demangled;
10631 if (mangled != NULL)
10632 {
10633
10634 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
10635 {
10636 /* Do nothing (do not demangle the symbol name). */
10637 }
10638 else
10639 {
10640 /* Use DMGL_RET_DROP for C++ template functions to suppress
10641 their return type. It is easier for GDB users to search
10642 for such functions as `name(params)' than `long name(params)'.
10643 In such case the minimal symbol names do not match the full
10644 symbol names but for template functions there is never a need
10645 to look up their definition from their declaration so
10646 the only disadvantage remains the minimal symbol variant
10647 `long name(params)' does not have the proper inferior type. */
10648 demangled.reset (gdb_demangle (mangled,
10649 (DMGL_PARAMS | DMGL_ANSI
10650 | DMGL_RET_DROP)));
10651 }
10652 if (demangled)
10653 canon = demangled.get ();
10654 else
10655 {
10656 canon = mangled;
10657 need_copy = 0;
10658 }
10659 }
10660
10661 if (canon == NULL || check_physname)
10662 {
10663 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10664
10665 if (canon != NULL && strcmp (physname, canon) != 0)
10666 {
10667 /* It may not mean a bug in GDB. The compiler could also
10668 compute DW_AT_linkage_name incorrectly. But in such case
10669 GDB would need to be bug-to-bug compatible. */
10670
10671 complaint (_("Computed physname <%s> does not match demangled <%s> "
10672 "(from linkage <%s>) - DIE at %s [in module %s]"),
10673 physname, canon, mangled, sect_offset_str (die->sect_off),
10674 objfile_name (objfile));
10675
10676 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10677 is available here - over computed PHYSNAME. It is safer
10678 against both buggy GDB and buggy compilers. */
10679
10680 retval = canon;
10681 }
10682 else
10683 {
10684 retval = physname;
10685 need_copy = 0;
10686 }
10687 }
10688 else
10689 retval = canon;
10690
10691 if (need_copy)
10692 retval = objfile->intern (retval);
10693
10694 return retval;
10695 }
10696
10697 /* Inspect DIE in CU for a namespace alias. If one exists, record
10698 a new symbol for it.
10699
10700 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10701
10702 static int
10703 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10704 {
10705 struct attribute *attr;
10706
10707 /* If the die does not have a name, this is not a namespace
10708 alias. */
10709 attr = dwarf2_attr (die, DW_AT_name, cu);
10710 if (attr != NULL)
10711 {
10712 int num;
10713 struct die_info *d = die;
10714 struct dwarf2_cu *imported_cu = cu;
10715
10716 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10717 keep inspecting DIEs until we hit the underlying import. */
10718 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10719 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10720 {
10721 attr = dwarf2_attr (d, DW_AT_import, cu);
10722 if (attr == NULL)
10723 break;
10724
10725 d = follow_die_ref (d, attr, &imported_cu);
10726 if (d->tag != DW_TAG_imported_declaration)
10727 break;
10728 }
10729
10730 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10731 {
10732 complaint (_("DIE at %s has too many recursively imported "
10733 "declarations"), sect_offset_str (d->sect_off));
10734 return 0;
10735 }
10736
10737 if (attr != NULL)
10738 {
10739 struct type *type;
10740 sect_offset sect_off = attr->get_ref_die_offset ();
10741
10742 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10743 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10744 {
10745 /* This declaration is a global namespace alias. Add
10746 a symbol for it whose type is the aliased namespace. */
10747 new_symbol (die, type, cu);
10748 return 1;
10749 }
10750 }
10751 }
10752
10753 return 0;
10754 }
10755
10756 /* Return the using directives repository (global or local?) to use in the
10757 current context for CU.
10758
10759 For Ada, imported declarations can materialize renamings, which *may* be
10760 global. However it is impossible (for now?) in DWARF to distinguish
10761 "external" imported declarations and "static" ones. As all imported
10762 declarations seem to be static in all other languages, make them all CU-wide
10763 global only in Ada. */
10764
10765 static struct using_direct **
10766 using_directives (struct dwarf2_cu *cu)
10767 {
10768 if (cu->language == language_ada
10769 && cu->get_builder ()->outermost_context_p ())
10770 return cu->get_builder ()->get_global_using_directives ();
10771 else
10772 return cu->get_builder ()->get_local_using_directives ();
10773 }
10774
10775 /* Read the import statement specified by the given die and record it. */
10776
10777 static void
10778 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10779 {
10780 struct objfile *objfile = cu->per_objfile->objfile;
10781 struct attribute *import_attr;
10782 struct die_info *imported_die, *child_die;
10783 struct dwarf2_cu *imported_cu;
10784 const char *imported_name;
10785 const char *imported_name_prefix;
10786 const char *canonical_name;
10787 const char *import_alias;
10788 const char *imported_declaration = NULL;
10789 const char *import_prefix;
10790 std::vector<const char *> excludes;
10791
10792 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10793 if (import_attr == NULL)
10794 {
10795 complaint (_("Tag '%s' has no DW_AT_import"),
10796 dwarf_tag_name (die->tag));
10797 return;
10798 }
10799
10800 imported_cu = cu;
10801 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10802 imported_name = dwarf2_name (imported_die, imported_cu);
10803 if (imported_name == NULL)
10804 {
10805 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10806
10807 The import in the following code:
10808 namespace A
10809 {
10810 typedef int B;
10811 }
10812
10813 int main ()
10814 {
10815 using A::B;
10816 B b;
10817 return b;
10818 }
10819
10820 ...
10821 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10822 <52> DW_AT_decl_file : 1
10823 <53> DW_AT_decl_line : 6
10824 <54> DW_AT_import : <0x75>
10825 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10826 <59> DW_AT_name : B
10827 <5b> DW_AT_decl_file : 1
10828 <5c> DW_AT_decl_line : 2
10829 <5d> DW_AT_type : <0x6e>
10830 ...
10831 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10832 <76> DW_AT_byte_size : 4
10833 <77> DW_AT_encoding : 5 (signed)
10834
10835 imports the wrong die ( 0x75 instead of 0x58 ).
10836 This case will be ignored until the gcc bug is fixed. */
10837 return;
10838 }
10839
10840 /* Figure out the local name after import. */
10841 import_alias = dwarf2_name (die, cu);
10842
10843 /* Figure out where the statement is being imported to. */
10844 import_prefix = determine_prefix (die, cu);
10845
10846 /* Figure out what the scope of the imported die is and prepend it
10847 to the name of the imported die. */
10848 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10849
10850 if (imported_die->tag != DW_TAG_namespace
10851 && imported_die->tag != DW_TAG_module)
10852 {
10853 imported_declaration = imported_name;
10854 canonical_name = imported_name_prefix;
10855 }
10856 else if (strlen (imported_name_prefix) > 0)
10857 canonical_name = obconcat (&objfile->objfile_obstack,
10858 imported_name_prefix,
10859 (cu->language == language_d ? "." : "::"),
10860 imported_name, (char *) NULL);
10861 else
10862 canonical_name = imported_name;
10863
10864 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10865 for (child_die = die->child; child_die && child_die->tag;
10866 child_die = child_die->sibling)
10867 {
10868 /* DWARF-4: A Fortran use statement with a “rename list” may be
10869 represented by an imported module entry with an import attribute
10870 referring to the module and owned entries corresponding to those
10871 entities that are renamed as part of being imported. */
10872
10873 if (child_die->tag != DW_TAG_imported_declaration)
10874 {
10875 complaint (_("child DW_TAG_imported_declaration expected "
10876 "- DIE at %s [in module %s]"),
10877 sect_offset_str (child_die->sect_off),
10878 objfile_name (objfile));
10879 continue;
10880 }
10881
10882 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10883 if (import_attr == NULL)
10884 {
10885 complaint (_("Tag '%s' has no DW_AT_import"),
10886 dwarf_tag_name (child_die->tag));
10887 continue;
10888 }
10889
10890 imported_cu = cu;
10891 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10892 &imported_cu);
10893 imported_name = dwarf2_name (imported_die, imported_cu);
10894 if (imported_name == NULL)
10895 {
10896 complaint (_("child DW_TAG_imported_declaration has unknown "
10897 "imported name - DIE at %s [in module %s]"),
10898 sect_offset_str (child_die->sect_off),
10899 objfile_name (objfile));
10900 continue;
10901 }
10902
10903 excludes.push_back (imported_name);
10904
10905 process_die (child_die, cu);
10906 }
10907
10908 add_using_directive (using_directives (cu),
10909 import_prefix,
10910 canonical_name,
10911 import_alias,
10912 imported_declaration,
10913 excludes,
10914 0,
10915 &objfile->objfile_obstack);
10916 }
10917
10918 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10919 types, but gives them a size of zero. Starting with version 14,
10920 ICC is compatible with GCC. */
10921
10922 static bool
10923 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10924 {
10925 if (!cu->checked_producer)
10926 check_producer (cu);
10927
10928 return cu->producer_is_icc_lt_14;
10929 }
10930
10931 /* ICC generates a DW_AT_type for C void functions. This was observed on
10932 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10933 which says that void functions should not have a DW_AT_type. */
10934
10935 static bool
10936 producer_is_icc (struct dwarf2_cu *cu)
10937 {
10938 if (!cu->checked_producer)
10939 check_producer (cu);
10940
10941 return cu->producer_is_icc;
10942 }
10943
10944 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10945 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10946 this, it was first present in GCC release 4.3.0. */
10947
10948 static bool
10949 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10950 {
10951 if (!cu->checked_producer)
10952 check_producer (cu);
10953
10954 return cu->producer_is_gcc_lt_4_3;
10955 }
10956
10957 static file_and_directory
10958 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10959 {
10960 file_and_directory res;
10961
10962 /* Find the filename. Do not use dwarf2_name here, since the filename
10963 is not a source language identifier. */
10964 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10965 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10966
10967 if (res.comp_dir == NULL
10968 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10969 && IS_ABSOLUTE_PATH (res.name))
10970 {
10971 res.comp_dir_storage = ldirname (res.name);
10972 if (!res.comp_dir_storage.empty ())
10973 res.comp_dir = res.comp_dir_storage.c_str ();
10974 }
10975 if (res.comp_dir != NULL)
10976 {
10977 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10978 directory, get rid of it. */
10979 const char *cp = strchr (res.comp_dir, ':');
10980
10981 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10982 res.comp_dir = cp + 1;
10983 }
10984
10985 if (res.name == NULL)
10986 res.name = "<unknown>";
10987
10988 return res;
10989 }
10990
10991 /* Handle DW_AT_stmt_list for a compilation unit.
10992 DIE is the DW_TAG_compile_unit die for CU.
10993 COMP_DIR is the compilation directory. LOWPC is passed to
10994 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10995
10996 static void
10997 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10998 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10999 {
11000 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11001 struct attribute *attr;
11002 struct line_header line_header_local;
11003 hashval_t line_header_local_hash;
11004 void **slot;
11005 int decode_mapping;
11006
11007 gdb_assert (! cu->per_cu->is_debug_types);
11008
11009 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11010 if (attr == NULL)
11011 return;
11012
11013 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11014
11015 /* The line header hash table is only created if needed (it exists to
11016 prevent redundant reading of the line table for partial_units).
11017 If we're given a partial_unit, we'll need it. If we're given a
11018 compile_unit, then use the line header hash table if it's already
11019 created, but don't create one just yet. */
11020
11021 if (per_objfile->line_header_hash == NULL
11022 && die->tag == DW_TAG_partial_unit)
11023 {
11024 per_objfile->line_header_hash
11025 .reset (htab_create_alloc (127, line_header_hash_voidp,
11026 line_header_eq_voidp,
11027 free_line_header_voidp,
11028 xcalloc, xfree));
11029 }
11030
11031 line_header_local.sect_off = line_offset;
11032 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11033 line_header_local_hash = line_header_hash (&line_header_local);
11034 if (per_objfile->line_header_hash != NULL)
11035 {
11036 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11037 &line_header_local,
11038 line_header_local_hash, NO_INSERT);
11039
11040 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11041 is not present in *SLOT (since if there is something in *SLOT then
11042 it will be for a partial_unit). */
11043 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11044 {
11045 gdb_assert (*slot != NULL);
11046 cu->line_header = (struct line_header *) *slot;
11047 return;
11048 }
11049 }
11050
11051 /* dwarf_decode_line_header does not yet provide sufficient information.
11052 We always have to call also dwarf_decode_lines for it. */
11053 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11054 if (lh == NULL)
11055 return;
11056
11057 cu->line_header = lh.release ();
11058 cu->line_header_die_owner = die;
11059
11060 if (per_objfile->line_header_hash == NULL)
11061 slot = NULL;
11062 else
11063 {
11064 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11065 &line_header_local,
11066 line_header_local_hash, INSERT);
11067 gdb_assert (slot != NULL);
11068 }
11069 if (slot != NULL && *slot == NULL)
11070 {
11071 /* This newly decoded line number information unit will be owned
11072 by line_header_hash hash table. */
11073 *slot = cu->line_header;
11074 cu->line_header_die_owner = NULL;
11075 }
11076 else
11077 {
11078 /* We cannot free any current entry in (*slot) as that struct line_header
11079 may be already used by multiple CUs. Create only temporary decoded
11080 line_header for this CU - it may happen at most once for each line
11081 number information unit. And if we're not using line_header_hash
11082 then this is what we want as well. */
11083 gdb_assert (die->tag != DW_TAG_partial_unit);
11084 }
11085 decode_mapping = (die->tag != DW_TAG_partial_unit);
11086 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11087 decode_mapping);
11088
11089 }
11090
11091 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11092
11093 static void
11094 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11095 {
11096 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11097 struct objfile *objfile = per_objfile->objfile;
11098 struct gdbarch *gdbarch = objfile->arch ();
11099 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11100 CORE_ADDR highpc = ((CORE_ADDR) 0);
11101 struct attribute *attr;
11102 struct die_info *child_die;
11103 CORE_ADDR baseaddr;
11104
11105 prepare_one_comp_unit (cu, die, cu->language);
11106 baseaddr = objfile->text_section_offset ();
11107
11108 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11109
11110 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11111 from finish_block. */
11112 if (lowpc == ((CORE_ADDR) -1))
11113 lowpc = highpc;
11114 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11115
11116 file_and_directory fnd = find_file_and_directory (die, cu);
11117
11118 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11119 standardised yet. As a workaround for the language detection we fall
11120 back to the DW_AT_producer string. */
11121 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11122 cu->language = language_opencl;
11123
11124 /* Similar hack for Go. */
11125 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11126 set_cu_language (DW_LANG_Go, cu);
11127
11128 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11129
11130 /* Decode line number information if present. We do this before
11131 processing child DIEs, so that the line header table is available
11132 for DW_AT_decl_file. */
11133 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11134
11135 /* Process all dies in compilation unit. */
11136 if (die->child != NULL)
11137 {
11138 child_die = die->child;
11139 while (child_die && child_die->tag)
11140 {
11141 process_die (child_die, cu);
11142 child_die = child_die->sibling;
11143 }
11144 }
11145
11146 /* Decode macro information, if present. Dwarf 2 macro information
11147 refers to information in the line number info statement program
11148 header, so we can only read it if we've read the header
11149 successfully. */
11150 attr = dwarf2_attr (die, DW_AT_macros, cu);
11151 if (attr == NULL)
11152 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11153 if (attr && cu->line_header)
11154 {
11155 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11156 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11157
11158 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11159 }
11160 else
11161 {
11162 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11163 if (attr && cu->line_header)
11164 {
11165 unsigned int macro_offset = DW_UNSND (attr);
11166
11167 dwarf_decode_macros (cu, macro_offset, 0);
11168 }
11169 }
11170 }
11171
11172 void
11173 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11174 {
11175 struct type_unit_group *tu_group;
11176 int first_time;
11177 struct attribute *attr;
11178 unsigned int i;
11179 struct signatured_type *sig_type;
11180
11181 gdb_assert (per_cu->is_debug_types);
11182 sig_type = (struct signatured_type *) per_cu;
11183
11184 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11185
11186 /* If we're using .gdb_index (includes -readnow) then
11187 per_cu->type_unit_group may not have been set up yet. */
11188 if (sig_type->type_unit_group == NULL)
11189 sig_type->type_unit_group = get_type_unit_group (this, attr);
11190 tu_group = sig_type->type_unit_group;
11191
11192 /* If we've already processed this stmt_list there's no real need to
11193 do it again, we could fake it and just recreate the part we need
11194 (file name,index -> symtab mapping). If data shows this optimization
11195 is useful we can do it then. */
11196 type_unit_group_unshareable *tug_unshare
11197 = per_objfile->get_type_unit_group_unshareable (tu_group);
11198 first_time = tug_unshare->compunit_symtab == NULL;
11199
11200 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11201 debug info. */
11202 line_header_up lh;
11203 if (attr != NULL)
11204 {
11205 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11206 lh = dwarf_decode_line_header (line_offset, this);
11207 }
11208 if (lh == NULL)
11209 {
11210 if (first_time)
11211 start_symtab ("", NULL, 0);
11212 else
11213 {
11214 gdb_assert (tug_unshare->symtabs == NULL);
11215 gdb_assert (m_builder == nullptr);
11216 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11217 m_builder.reset (new struct buildsym_compunit
11218 (COMPUNIT_OBJFILE (cust), "",
11219 COMPUNIT_DIRNAME (cust),
11220 compunit_language (cust),
11221 0, cust));
11222 list_in_scope = get_builder ()->get_file_symbols ();
11223 }
11224 return;
11225 }
11226
11227 line_header = lh.release ();
11228 line_header_die_owner = die;
11229
11230 if (first_time)
11231 {
11232 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11233
11234 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11235 still initializing it, and our caller (a few levels up)
11236 process_full_type_unit still needs to know if this is the first
11237 time. */
11238
11239 tug_unshare->symtabs
11240 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11241 struct symtab *, line_header->file_names_size ());
11242
11243 auto &file_names = line_header->file_names ();
11244 for (i = 0; i < file_names.size (); ++i)
11245 {
11246 file_entry &fe = file_names[i];
11247 dwarf2_start_subfile (this, fe.name,
11248 fe.include_dir (line_header));
11249 buildsym_compunit *b = get_builder ();
11250 if (b->get_current_subfile ()->symtab == NULL)
11251 {
11252 /* NOTE: start_subfile will recognize when it's been
11253 passed a file it has already seen. So we can't
11254 assume there's a simple mapping from
11255 cu->line_header->file_names to subfiles, plus
11256 cu->line_header->file_names may contain dups. */
11257 b->get_current_subfile ()->symtab
11258 = allocate_symtab (cust, b->get_current_subfile ()->name);
11259 }
11260
11261 fe.symtab = b->get_current_subfile ()->symtab;
11262 tug_unshare->symtabs[i] = fe.symtab;
11263 }
11264 }
11265 else
11266 {
11267 gdb_assert (m_builder == nullptr);
11268 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11269 m_builder.reset (new struct buildsym_compunit
11270 (COMPUNIT_OBJFILE (cust), "",
11271 COMPUNIT_DIRNAME (cust),
11272 compunit_language (cust),
11273 0, cust));
11274 list_in_scope = get_builder ()->get_file_symbols ();
11275
11276 auto &file_names = line_header->file_names ();
11277 for (i = 0; i < file_names.size (); ++i)
11278 {
11279 file_entry &fe = file_names[i];
11280 fe.symtab = tug_unshare->symtabs[i];
11281 }
11282 }
11283
11284 /* The main symtab is allocated last. Type units don't have DW_AT_name
11285 so they don't have a "real" (so to speak) symtab anyway.
11286 There is later code that will assign the main symtab to all symbols
11287 that don't have one. We need to handle the case of a symbol with a
11288 missing symtab (DW_AT_decl_file) anyway. */
11289 }
11290
11291 /* Process DW_TAG_type_unit.
11292 For TUs we want to skip the first top level sibling if it's not the
11293 actual type being defined by this TU. In this case the first top
11294 level sibling is there to provide context only. */
11295
11296 static void
11297 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11298 {
11299 struct die_info *child_die;
11300
11301 prepare_one_comp_unit (cu, die, language_minimal);
11302
11303 /* Initialize (or reinitialize) the machinery for building symtabs.
11304 We do this before processing child DIEs, so that the line header table
11305 is available for DW_AT_decl_file. */
11306 cu->setup_type_unit_groups (die);
11307
11308 if (die->child != NULL)
11309 {
11310 child_die = die->child;
11311 while (child_die && child_die->tag)
11312 {
11313 process_die (child_die, cu);
11314 child_die = child_die->sibling;
11315 }
11316 }
11317 }
11318 \f
11319 /* DWO/DWP files.
11320
11321 http://gcc.gnu.org/wiki/DebugFission
11322 http://gcc.gnu.org/wiki/DebugFissionDWP
11323
11324 To simplify handling of both DWO files ("object" files with the DWARF info)
11325 and DWP files (a file with the DWOs packaged up into one file), we treat
11326 DWP files as having a collection of virtual DWO files. */
11327
11328 static hashval_t
11329 hash_dwo_file (const void *item)
11330 {
11331 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11332 hashval_t hash;
11333
11334 hash = htab_hash_string (dwo_file->dwo_name);
11335 if (dwo_file->comp_dir != NULL)
11336 hash += htab_hash_string (dwo_file->comp_dir);
11337 return hash;
11338 }
11339
11340 static int
11341 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11342 {
11343 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11344 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11345
11346 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11347 return 0;
11348 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11349 return lhs->comp_dir == rhs->comp_dir;
11350 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11351 }
11352
11353 /* Allocate a hash table for DWO files. */
11354
11355 static htab_up
11356 allocate_dwo_file_hash_table ()
11357 {
11358 auto delete_dwo_file = [] (void *item)
11359 {
11360 struct dwo_file *dwo_file = (struct dwo_file *) item;
11361
11362 delete dwo_file;
11363 };
11364
11365 return htab_up (htab_create_alloc (41,
11366 hash_dwo_file,
11367 eq_dwo_file,
11368 delete_dwo_file,
11369 xcalloc, xfree));
11370 }
11371
11372 /* Lookup DWO file DWO_NAME. */
11373
11374 static void **
11375 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
11376 const char *dwo_name,
11377 const char *comp_dir)
11378 {
11379 struct dwo_file find_entry;
11380 void **slot;
11381
11382 if (per_objfile->per_bfd->dwo_files == NULL)
11383 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11384
11385 find_entry.dwo_name = dwo_name;
11386 find_entry.comp_dir = comp_dir;
11387 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
11388 INSERT);
11389
11390 return slot;
11391 }
11392
11393 static hashval_t
11394 hash_dwo_unit (const void *item)
11395 {
11396 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11397
11398 /* This drops the top 32 bits of the id, but is ok for a hash. */
11399 return dwo_unit->signature;
11400 }
11401
11402 static int
11403 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11404 {
11405 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11406 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11407
11408 /* The signature is assumed to be unique within the DWO file.
11409 So while object file CU dwo_id's always have the value zero,
11410 that's OK, assuming each object file DWO file has only one CU,
11411 and that's the rule for now. */
11412 return lhs->signature == rhs->signature;
11413 }
11414
11415 /* Allocate a hash table for DWO CUs,TUs.
11416 There is one of these tables for each of CUs,TUs for each DWO file. */
11417
11418 static htab_up
11419 allocate_dwo_unit_table ()
11420 {
11421 /* Start out with a pretty small number.
11422 Generally DWO files contain only one CU and maybe some TUs. */
11423 return htab_up (htab_create_alloc (3,
11424 hash_dwo_unit,
11425 eq_dwo_unit,
11426 NULL, xcalloc, xfree));
11427 }
11428
11429 /* die_reader_func for create_dwo_cu. */
11430
11431 static void
11432 create_dwo_cu_reader (const struct die_reader_specs *reader,
11433 const gdb_byte *info_ptr,
11434 struct die_info *comp_unit_die,
11435 struct dwo_file *dwo_file,
11436 struct dwo_unit *dwo_unit)
11437 {
11438 struct dwarf2_cu *cu = reader->cu;
11439 sect_offset sect_off = cu->per_cu->sect_off;
11440 struct dwarf2_section_info *section = cu->per_cu->section;
11441
11442 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11443 if (!signature.has_value ())
11444 {
11445 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11446 " its dwo_id [in module %s]"),
11447 sect_offset_str (sect_off), dwo_file->dwo_name);
11448 return;
11449 }
11450
11451 dwo_unit->dwo_file = dwo_file;
11452 dwo_unit->signature = *signature;
11453 dwo_unit->section = section;
11454 dwo_unit->sect_off = sect_off;
11455 dwo_unit->length = cu->per_cu->length;
11456
11457 if (dwarf_read_debug)
11458 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11459 sect_offset_str (sect_off),
11460 hex_string (dwo_unit->signature));
11461 }
11462
11463 /* Create the dwo_units for the CUs in a DWO_FILE.
11464 Note: This function processes DWO files only, not DWP files. */
11465
11466 static void
11467 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
11468 dwarf2_cu *cu, struct dwo_file &dwo_file,
11469 dwarf2_section_info &section, htab_up &cus_htab)
11470 {
11471 struct objfile *objfile = per_objfile->objfile;
11472 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
11473 const gdb_byte *info_ptr, *end_ptr;
11474
11475 section.read (objfile);
11476 info_ptr = section.buffer;
11477
11478 if (info_ptr == NULL)
11479 return;
11480
11481 if (dwarf_read_debug)
11482 {
11483 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11484 section.get_name (),
11485 section.get_file_name ());
11486 }
11487
11488 end_ptr = info_ptr + section.size;
11489 while (info_ptr < end_ptr)
11490 {
11491 struct dwarf2_per_cu_data per_cu;
11492 struct dwo_unit read_unit {};
11493 struct dwo_unit *dwo_unit;
11494 void **slot;
11495 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11496
11497 memset (&per_cu, 0, sizeof (per_cu));
11498 per_cu.per_bfd = per_bfd;
11499 per_cu.is_debug_types = 0;
11500 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11501 per_cu.section = &section;
11502
11503 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
11504 if (!reader.dummy_p)
11505 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11506 &dwo_file, &read_unit);
11507 info_ptr += per_cu.length;
11508
11509 // If the unit could not be parsed, skip it.
11510 if (read_unit.dwo_file == NULL)
11511 continue;
11512
11513 if (cus_htab == NULL)
11514 cus_htab = allocate_dwo_unit_table ();
11515
11516 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11517 struct dwo_unit);
11518 *dwo_unit = read_unit;
11519 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11520 gdb_assert (slot != NULL);
11521 if (*slot != NULL)
11522 {
11523 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11524 sect_offset dup_sect_off = dup_cu->sect_off;
11525
11526 complaint (_("debug cu entry at offset %s is duplicate to"
11527 " the entry at offset %s, signature %s"),
11528 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11529 hex_string (dwo_unit->signature));
11530 }
11531 *slot = (void *)dwo_unit;
11532 }
11533 }
11534
11535 /* DWP file .debug_{cu,tu}_index section format:
11536 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11537 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11538
11539 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11540 officially standard DWP format was published with DWARF v5 and is called
11541 Version 5. There are no versions 3 or 4.
11542
11543 DWP Version 1:
11544
11545 Both index sections have the same format, and serve to map a 64-bit
11546 signature to a set of section numbers. Each section begins with a header,
11547 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11548 indexes, and a pool of 32-bit section numbers. The index sections will be
11549 aligned at 8-byte boundaries in the file.
11550
11551 The index section header consists of:
11552
11553 V, 32 bit version number
11554 -, 32 bits unused
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 begins at offset 16 in the section, and consists of an array
11561 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11562 order of the application binary). Unused slots in the hash table are 0.
11563 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11564
11565 The parallel table begins immediately after the hash table
11566 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11567 array of 32-bit indexes (using the byte order of the application binary),
11568 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11569 table contains a 32-bit index into the pool of section numbers. For unused
11570 hash table slots, the corresponding entry in the parallel table will be 0.
11571
11572 The pool of section numbers begins immediately following the hash table
11573 (at offset 16 + 12 * M from the beginning of the section). The pool of
11574 section numbers consists of an array of 32-bit words (using the byte order
11575 of the application binary). Each item in the array is indexed starting
11576 from 0. The hash table entry provides the index of the first section
11577 number in the set. Additional section numbers in the set follow, and the
11578 set is terminated by a 0 entry (section number 0 is not used in ELF).
11579
11580 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11581 section must be the first entry in the set, and the .debug_abbrev.dwo must
11582 be the second entry. Other members of the set may follow in any order.
11583
11584 ---
11585
11586 DWP Versions 2 and 5:
11587
11588 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
11589 and the entries in the index tables are now offsets into these sections.
11590 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11591 section.
11592
11593 Index Section Contents:
11594 Header
11595 Hash Table of Signatures dwp_hash_table.hash_table
11596 Parallel Table of Indices dwp_hash_table.unit_table
11597 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11598 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
11599
11600 The index section header consists of:
11601
11602 V, 32 bit version number
11603 L, 32 bit number of columns in the table of section offsets
11604 N, 32 bit number of compilation units or type units in the index
11605 M, 32 bit number of slots in the hash table
11606
11607 Numbers are recorded using the byte order of the application binary.
11608
11609 The hash table has the same format as version 1.
11610 The parallel table of indices has the same format as version 1,
11611 except that the entries are origin-1 indices into the table of sections
11612 offsets and the table of section sizes.
11613
11614 The table of offsets begins immediately following the parallel table
11615 (at offset 16 + 12 * M from the beginning of the section). The table is
11616 a two-dimensional array of 32-bit words (using the byte order of the
11617 application binary), with L columns and N+1 rows, in row-major order.
11618 Each row in the array is indexed starting from 0. The first row provides
11619 a key to the remaining rows: each column in this row provides an identifier
11620 for a debug section, and the offsets in the same column of subsequent rows
11621 refer to that section. The section identifiers for Version 2 are:
11622
11623 DW_SECT_INFO 1 .debug_info.dwo
11624 DW_SECT_TYPES 2 .debug_types.dwo
11625 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11626 DW_SECT_LINE 4 .debug_line.dwo
11627 DW_SECT_LOC 5 .debug_loc.dwo
11628 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11629 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11630 DW_SECT_MACRO 8 .debug_macro.dwo
11631
11632 The section identifiers for Version 5 are:
11633
11634 DW_SECT_INFO_V5 1 .debug_info.dwo
11635 DW_SECT_RESERVED_V5 2 --
11636 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11637 DW_SECT_LINE_V5 4 .debug_line.dwo
11638 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11639 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11640 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11641 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11642
11643 The offsets provided by the CU and TU index sections are the base offsets
11644 for the contributions made by each CU or TU to the corresponding section
11645 in the package file. Each CU and TU header contains an abbrev_offset
11646 field, used to find the abbreviations table for that CU or TU within the
11647 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11648 be interpreted as relative to the base offset given in the index section.
11649 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11650 should be interpreted as relative to the base offset for .debug_line.dwo,
11651 and offsets into other debug sections obtained from DWARF attributes should
11652 also be interpreted as relative to the corresponding base offset.
11653
11654 The table of sizes begins immediately following the table of offsets.
11655 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11656 with L columns and N rows, in row-major order. Each row in the array is
11657 indexed starting from 1 (row 0 is shared by the two tables).
11658
11659 ---
11660
11661 Hash table lookup is handled the same in version 1 and 2:
11662
11663 We assume that N and M will not exceed 2^32 - 1.
11664 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11665
11666 Given a 64-bit compilation unit signature or a type signature S, an entry
11667 in the hash table is located as follows:
11668
11669 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11670 the low-order k bits all set to 1.
11671
11672 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11673
11674 3) If the hash table entry at index H matches the signature, use that
11675 entry. If the hash table entry at index H is unused (all zeroes),
11676 terminate the search: the signature is not present in the table.
11677
11678 4) Let H = (H + H') modulo M. Repeat at Step 3.
11679
11680 Because M > N and H' and M are relatively prime, the search is guaranteed
11681 to stop at an unused slot or find the match. */
11682
11683 /* Create a hash table to map DWO IDs to their CU/TU entry in
11684 .debug_{info,types}.dwo in DWP_FILE.
11685 Returns NULL if there isn't one.
11686 Note: This function processes DWP files only, not DWO files. */
11687
11688 static struct dwp_hash_table *
11689 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11690 struct dwp_file *dwp_file, int is_debug_types)
11691 {
11692 struct objfile *objfile = per_objfile->objfile;
11693 bfd *dbfd = dwp_file->dbfd.get ();
11694 const gdb_byte *index_ptr, *index_end;
11695 struct dwarf2_section_info *index;
11696 uint32_t version, nr_columns, nr_units, nr_slots;
11697 struct dwp_hash_table *htab;
11698
11699 if (is_debug_types)
11700 index = &dwp_file->sections.tu_index;
11701 else
11702 index = &dwp_file->sections.cu_index;
11703
11704 if (index->empty ())
11705 return NULL;
11706 index->read (objfile);
11707
11708 index_ptr = index->buffer;
11709 index_end = index_ptr + index->size;
11710
11711 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11712 For now it's safe to just read 4 bytes (particularly as it's difficult to
11713 tell if you're dealing with Version 5 before you've read the version). */
11714 version = read_4_bytes (dbfd, index_ptr);
11715 index_ptr += 4;
11716 if (version == 2 || version == 5)
11717 nr_columns = read_4_bytes (dbfd, index_ptr);
11718 else
11719 nr_columns = 0;
11720 index_ptr += 4;
11721 nr_units = read_4_bytes (dbfd, index_ptr);
11722 index_ptr += 4;
11723 nr_slots = read_4_bytes (dbfd, index_ptr);
11724 index_ptr += 4;
11725
11726 if (version != 1 && version != 2 && version != 5)
11727 {
11728 error (_("Dwarf Error: unsupported DWP file version (%s)"
11729 " [in module %s]"),
11730 pulongest (version), dwp_file->name);
11731 }
11732 if (nr_slots != (nr_slots & -nr_slots))
11733 {
11734 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11735 " is not power of 2 [in module %s]"),
11736 pulongest (nr_slots), dwp_file->name);
11737 }
11738
11739 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11740 htab->version = version;
11741 htab->nr_columns = nr_columns;
11742 htab->nr_units = nr_units;
11743 htab->nr_slots = nr_slots;
11744 htab->hash_table = index_ptr;
11745 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11746
11747 /* Exit early if the table is empty. */
11748 if (nr_slots == 0 || nr_units == 0
11749 || (version == 2 && nr_columns == 0)
11750 || (version == 5 && nr_columns == 0))
11751 {
11752 /* All must be zero. */
11753 if (nr_slots != 0 || nr_units != 0
11754 || (version == 2 && nr_columns != 0)
11755 || (version == 5 && nr_columns != 0))
11756 {
11757 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11758 " all zero [in modules %s]"),
11759 dwp_file->name);
11760 }
11761 return htab;
11762 }
11763
11764 if (version == 1)
11765 {
11766 htab->section_pool.v1.indices =
11767 htab->unit_table + sizeof (uint32_t) * nr_slots;
11768 /* It's harder to decide whether the section is too small in v1.
11769 V1 is deprecated anyway so we punt. */
11770 }
11771 else if (version == 2)
11772 {
11773 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11774 int *ids = htab->section_pool.v2.section_ids;
11775 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11776 /* Reverse map for error checking. */
11777 int ids_seen[DW_SECT_MAX + 1];
11778 int i;
11779
11780 if (nr_columns < 2)
11781 {
11782 error (_("Dwarf Error: bad DWP hash table, too few columns"
11783 " in section table [in module %s]"),
11784 dwp_file->name);
11785 }
11786 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11787 {
11788 error (_("Dwarf Error: bad DWP hash table, too many columns"
11789 " in section table [in module %s]"),
11790 dwp_file->name);
11791 }
11792 memset (ids, 255, sizeof_ids);
11793 memset (ids_seen, 255, sizeof (ids_seen));
11794 for (i = 0; i < nr_columns; ++i)
11795 {
11796 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11797
11798 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11799 {
11800 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11801 " in section table [in module %s]"),
11802 id, dwp_file->name);
11803 }
11804 if (ids_seen[id] != -1)
11805 {
11806 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11807 " id %d in section table [in module %s]"),
11808 id, dwp_file->name);
11809 }
11810 ids_seen[id] = i;
11811 ids[i] = id;
11812 }
11813 /* Must have exactly one info or types section. */
11814 if (((ids_seen[DW_SECT_INFO] != -1)
11815 + (ids_seen[DW_SECT_TYPES] != -1))
11816 != 1)
11817 {
11818 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11819 " DWO info/types section [in module %s]"),
11820 dwp_file->name);
11821 }
11822 /* Must have an abbrev section. */
11823 if (ids_seen[DW_SECT_ABBREV] == -1)
11824 {
11825 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11826 " section [in module %s]"),
11827 dwp_file->name);
11828 }
11829 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11830 htab->section_pool.v2.sizes =
11831 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11832 * nr_units * nr_columns);
11833 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11834 * nr_units * nr_columns))
11835 > index_end)
11836 {
11837 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11838 " [in module %s]"),
11839 dwp_file->name);
11840 }
11841 }
11842 else /* version == 5 */
11843 {
11844 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11845 int *ids = htab->section_pool.v5.section_ids;
11846 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11847 /* Reverse map for error checking. */
11848 int ids_seen[DW_SECT_MAX_V5 + 1];
11849
11850 if (nr_columns < 2)
11851 {
11852 error (_("Dwarf Error: bad DWP hash table, too few columns"
11853 " in section table [in module %s]"),
11854 dwp_file->name);
11855 }
11856 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11857 {
11858 error (_("Dwarf Error: bad DWP hash table, too many columns"
11859 " in section table [in module %s]"),
11860 dwp_file->name);
11861 }
11862 memset (ids, 255, sizeof_ids);
11863 memset (ids_seen, 255, sizeof (ids_seen));
11864 for (int i = 0; i < nr_columns; ++i)
11865 {
11866 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11867
11868 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11869 {
11870 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11871 " in section table [in module %s]"),
11872 id, dwp_file->name);
11873 }
11874 if (ids_seen[id] != -1)
11875 {
11876 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11877 " id %d in section table [in module %s]"),
11878 id, dwp_file->name);
11879 }
11880 ids_seen[id] = i;
11881 ids[i] = id;
11882 }
11883 /* Must have seen an info section. */
11884 if (ids_seen[DW_SECT_INFO_V5] == -1)
11885 {
11886 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11887 " DWO info/types section [in module %s]"),
11888 dwp_file->name);
11889 }
11890 /* Must have an abbrev section. */
11891 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11892 {
11893 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11894 " section [in module %s]"),
11895 dwp_file->name);
11896 }
11897 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11898 htab->section_pool.v5.sizes
11899 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11900 * nr_units * nr_columns);
11901 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11902 * nr_units * nr_columns))
11903 > index_end)
11904 {
11905 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11906 " [in module %s]"),
11907 dwp_file->name);
11908 }
11909 }
11910
11911 return htab;
11912 }
11913
11914 /* Update SECTIONS with the data from SECTP.
11915
11916 This function is like the other "locate" section routines, but in
11917 this context the sections to read comes from the DWP V1 hash table,
11918 not the full ELF section table.
11919
11920 The result is non-zero for success, or zero if an error was found. */
11921
11922 static int
11923 locate_v1_virtual_dwo_sections (asection *sectp,
11924 struct virtual_v1_dwo_sections *sections)
11925 {
11926 const struct dwop_section_names *names = &dwop_section_names;
11927
11928 if (section_is_p (sectp->name, &names->abbrev_dwo))
11929 {
11930 /* There can be only one. */
11931 if (sections->abbrev.s.section != NULL)
11932 return 0;
11933 sections->abbrev.s.section = sectp;
11934 sections->abbrev.size = bfd_section_size (sectp);
11935 }
11936 else if (section_is_p (sectp->name, &names->info_dwo)
11937 || section_is_p (sectp->name, &names->types_dwo))
11938 {
11939 /* There can be only one. */
11940 if (sections->info_or_types.s.section != NULL)
11941 return 0;
11942 sections->info_or_types.s.section = sectp;
11943 sections->info_or_types.size = bfd_section_size (sectp);
11944 }
11945 else if (section_is_p (sectp->name, &names->line_dwo))
11946 {
11947 /* There can be only one. */
11948 if (sections->line.s.section != NULL)
11949 return 0;
11950 sections->line.s.section = sectp;
11951 sections->line.size = bfd_section_size (sectp);
11952 }
11953 else if (section_is_p (sectp->name, &names->loc_dwo))
11954 {
11955 /* There can be only one. */
11956 if (sections->loc.s.section != NULL)
11957 return 0;
11958 sections->loc.s.section = sectp;
11959 sections->loc.size = bfd_section_size (sectp);
11960 }
11961 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11962 {
11963 /* There can be only one. */
11964 if (sections->macinfo.s.section != NULL)
11965 return 0;
11966 sections->macinfo.s.section = sectp;
11967 sections->macinfo.size = bfd_section_size (sectp);
11968 }
11969 else if (section_is_p (sectp->name, &names->macro_dwo))
11970 {
11971 /* There can be only one. */
11972 if (sections->macro.s.section != NULL)
11973 return 0;
11974 sections->macro.s.section = sectp;
11975 sections->macro.size = bfd_section_size (sectp);
11976 }
11977 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11978 {
11979 /* There can be only one. */
11980 if (sections->str_offsets.s.section != NULL)
11981 return 0;
11982 sections->str_offsets.s.section = sectp;
11983 sections->str_offsets.size = bfd_section_size (sectp);
11984 }
11985 else
11986 {
11987 /* No other kind of section is valid. */
11988 return 0;
11989 }
11990
11991 return 1;
11992 }
11993
11994 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11995 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11996 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11997 This is for DWP version 1 files. */
11998
11999 static struct dwo_unit *
12000 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
12001 struct dwp_file *dwp_file,
12002 uint32_t unit_index,
12003 const char *comp_dir,
12004 ULONGEST signature, int is_debug_types)
12005 {
12006 const struct dwp_hash_table *dwp_htab =
12007 is_debug_types ? dwp_file->tus : dwp_file->cus;
12008 bfd *dbfd = dwp_file->dbfd.get ();
12009 const char *kind = is_debug_types ? "TU" : "CU";
12010 struct dwo_file *dwo_file;
12011 struct dwo_unit *dwo_unit;
12012 struct virtual_v1_dwo_sections sections;
12013 void **dwo_file_slot;
12014 int i;
12015
12016 gdb_assert (dwp_file->version == 1);
12017
12018 if (dwarf_read_debug)
12019 {
12020 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12021 kind,
12022 pulongest (unit_index), hex_string (signature),
12023 dwp_file->name);
12024 }
12025
12026 /* Fetch the sections of this DWO unit.
12027 Put a limit on the number of sections we look for so that bad data
12028 doesn't cause us to loop forever. */
12029
12030 #define MAX_NR_V1_DWO_SECTIONS \
12031 (1 /* .debug_info or .debug_types */ \
12032 + 1 /* .debug_abbrev */ \
12033 + 1 /* .debug_line */ \
12034 + 1 /* .debug_loc */ \
12035 + 1 /* .debug_str_offsets */ \
12036 + 1 /* .debug_macro or .debug_macinfo */ \
12037 + 1 /* trailing zero */)
12038
12039 memset (&sections, 0, sizeof (sections));
12040
12041 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12042 {
12043 asection *sectp;
12044 uint32_t section_nr =
12045 read_4_bytes (dbfd,
12046 dwp_htab->section_pool.v1.indices
12047 + (unit_index + i) * sizeof (uint32_t));
12048
12049 if (section_nr == 0)
12050 break;
12051 if (section_nr >= dwp_file->num_sections)
12052 {
12053 error (_("Dwarf Error: bad DWP hash table, section number too large"
12054 " [in module %s]"),
12055 dwp_file->name);
12056 }
12057
12058 sectp = dwp_file->elf_sections[section_nr];
12059 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12060 {
12061 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12062 " [in module %s]"),
12063 dwp_file->name);
12064 }
12065 }
12066
12067 if (i < 2
12068 || sections.info_or_types.empty ()
12069 || sections.abbrev.empty ())
12070 {
12071 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12072 " [in module %s]"),
12073 dwp_file->name);
12074 }
12075 if (i == MAX_NR_V1_DWO_SECTIONS)
12076 {
12077 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12078 " [in module %s]"),
12079 dwp_file->name);
12080 }
12081
12082 /* It's easier for the rest of the code if we fake a struct dwo_file and
12083 have dwo_unit "live" in that. At least for now.
12084
12085 The DWP file can be made up of a random collection of CUs and TUs.
12086 However, for each CU + set of TUs that came from the same original DWO
12087 file, we can combine them back into a virtual DWO file to save space
12088 (fewer struct dwo_file objects to allocate). Remember that for really
12089 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12090
12091 std::string virtual_dwo_name =
12092 string_printf ("virtual-dwo/%d-%d-%d-%d",
12093 sections.abbrev.get_id (),
12094 sections.line.get_id (),
12095 sections.loc.get_id (),
12096 sections.str_offsets.get_id ());
12097 /* Can we use an existing virtual DWO file? */
12098 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12099 comp_dir);
12100 /* Create one if necessary. */
12101 if (*dwo_file_slot == NULL)
12102 {
12103 if (dwarf_read_debug)
12104 {
12105 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12106 virtual_dwo_name.c_str ());
12107 }
12108 dwo_file = new struct dwo_file;
12109 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12110 dwo_file->comp_dir = comp_dir;
12111 dwo_file->sections.abbrev = sections.abbrev;
12112 dwo_file->sections.line = sections.line;
12113 dwo_file->sections.loc = sections.loc;
12114 dwo_file->sections.macinfo = sections.macinfo;
12115 dwo_file->sections.macro = sections.macro;
12116 dwo_file->sections.str_offsets = sections.str_offsets;
12117 /* The "str" section is global to the entire DWP file. */
12118 dwo_file->sections.str = dwp_file->sections.str;
12119 /* The info or types section is assigned below to dwo_unit,
12120 there's no need to record it in dwo_file.
12121 Also, we can't simply record type sections in dwo_file because
12122 we record a pointer into the vector in dwo_unit. As we collect more
12123 types we'll grow the vector and eventually have to reallocate space
12124 for it, invalidating all copies of pointers into the previous
12125 contents. */
12126 *dwo_file_slot = dwo_file;
12127 }
12128 else
12129 {
12130 if (dwarf_read_debug)
12131 {
12132 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12133 virtual_dwo_name.c_str ());
12134 }
12135 dwo_file = (struct dwo_file *) *dwo_file_slot;
12136 }
12137
12138 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12139 dwo_unit->dwo_file = dwo_file;
12140 dwo_unit->signature = signature;
12141 dwo_unit->section =
12142 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12143 *dwo_unit->section = sections.info_or_types;
12144 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12145
12146 return dwo_unit;
12147 }
12148
12149 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
12150 simplify them. Given a pointer to the containing section SECTION, and
12151 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
12152 virtual section of just that piece. */
12153
12154 static struct dwarf2_section_info
12155 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
12156 struct dwarf2_section_info *section,
12157 bfd_size_type offset, bfd_size_type size)
12158 {
12159 struct dwarf2_section_info result;
12160 asection *sectp;
12161
12162 gdb_assert (section != NULL);
12163 gdb_assert (!section->is_virtual);
12164
12165 memset (&result, 0, sizeof (result));
12166 result.s.containing_section = section;
12167 result.is_virtual = true;
12168
12169 if (size == 0)
12170 return result;
12171
12172 sectp = section->get_bfd_section ();
12173
12174 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12175 bounds of the real section. This is a pretty-rare event, so just
12176 flag an error (easier) instead of a warning and trying to cope. */
12177 if (sectp == NULL
12178 || offset + size > bfd_section_size (sectp))
12179 {
12180 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
12181 " in section %s [in module %s]"),
12182 sectp ? bfd_section_name (sectp) : "<unknown>",
12183 objfile_name (per_objfile->objfile));
12184 }
12185
12186 result.virtual_offset = offset;
12187 result.size = size;
12188 return result;
12189 }
12190
12191 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12192 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12193 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12194 This is for DWP version 2 files. */
12195
12196 static struct dwo_unit *
12197 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
12198 struct dwp_file *dwp_file,
12199 uint32_t unit_index,
12200 const char *comp_dir,
12201 ULONGEST signature, int is_debug_types)
12202 {
12203 const struct dwp_hash_table *dwp_htab =
12204 is_debug_types ? dwp_file->tus : dwp_file->cus;
12205 bfd *dbfd = dwp_file->dbfd.get ();
12206 const char *kind = is_debug_types ? "TU" : "CU";
12207 struct dwo_file *dwo_file;
12208 struct dwo_unit *dwo_unit;
12209 struct virtual_v2_or_v5_dwo_sections sections;
12210 void **dwo_file_slot;
12211 int i;
12212
12213 gdb_assert (dwp_file->version == 2);
12214
12215 if (dwarf_read_debug)
12216 {
12217 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12218 kind,
12219 pulongest (unit_index), hex_string (signature),
12220 dwp_file->name);
12221 }
12222
12223 /* Fetch the section offsets of this DWO unit. */
12224
12225 memset (&sections, 0, sizeof (sections));
12226
12227 for (i = 0; i < dwp_htab->nr_columns; ++i)
12228 {
12229 uint32_t offset = read_4_bytes (dbfd,
12230 dwp_htab->section_pool.v2.offsets
12231 + (((unit_index - 1) * dwp_htab->nr_columns
12232 + i)
12233 * sizeof (uint32_t)));
12234 uint32_t size = read_4_bytes (dbfd,
12235 dwp_htab->section_pool.v2.sizes
12236 + (((unit_index - 1) * dwp_htab->nr_columns
12237 + i)
12238 * sizeof (uint32_t)));
12239
12240 switch (dwp_htab->section_pool.v2.section_ids[i])
12241 {
12242 case DW_SECT_INFO:
12243 case DW_SECT_TYPES:
12244 sections.info_or_types_offset = offset;
12245 sections.info_or_types_size = size;
12246 break;
12247 case DW_SECT_ABBREV:
12248 sections.abbrev_offset = offset;
12249 sections.abbrev_size = size;
12250 break;
12251 case DW_SECT_LINE:
12252 sections.line_offset = offset;
12253 sections.line_size = size;
12254 break;
12255 case DW_SECT_LOC:
12256 sections.loc_offset = offset;
12257 sections.loc_size = size;
12258 break;
12259 case DW_SECT_STR_OFFSETS:
12260 sections.str_offsets_offset = offset;
12261 sections.str_offsets_size = size;
12262 break;
12263 case DW_SECT_MACINFO:
12264 sections.macinfo_offset = offset;
12265 sections.macinfo_size = size;
12266 break;
12267 case DW_SECT_MACRO:
12268 sections.macro_offset = offset;
12269 sections.macro_size = size;
12270 break;
12271 }
12272 }
12273
12274 /* It's easier for the rest of the code if we fake a struct dwo_file and
12275 have dwo_unit "live" in that. At least for now.
12276
12277 The DWP file can be made up of a random collection of CUs and TUs.
12278 However, for each CU + set of TUs that came from the same original DWO
12279 file, we can combine them back into a virtual DWO file to save space
12280 (fewer struct dwo_file objects to allocate). Remember that for really
12281 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12282
12283 std::string virtual_dwo_name =
12284 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12285 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12286 (long) (sections.line_size ? sections.line_offset : 0),
12287 (long) (sections.loc_size ? sections.loc_offset : 0),
12288 (long) (sections.str_offsets_size
12289 ? sections.str_offsets_offset : 0));
12290 /* Can we use an existing virtual DWO file? */
12291 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12292 comp_dir);
12293 /* Create one if necessary. */
12294 if (*dwo_file_slot == NULL)
12295 {
12296 if (dwarf_read_debug)
12297 {
12298 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12299 virtual_dwo_name.c_str ());
12300 }
12301 dwo_file = new struct dwo_file;
12302 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12303 dwo_file->comp_dir = comp_dir;
12304 dwo_file->sections.abbrev =
12305 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12306 sections.abbrev_offset,
12307 sections.abbrev_size);
12308 dwo_file->sections.line =
12309 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12310 sections.line_offset,
12311 sections.line_size);
12312 dwo_file->sections.loc =
12313 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12314 sections.loc_offset, sections.loc_size);
12315 dwo_file->sections.macinfo =
12316 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12317 sections.macinfo_offset,
12318 sections.macinfo_size);
12319 dwo_file->sections.macro =
12320 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12321 sections.macro_offset,
12322 sections.macro_size);
12323 dwo_file->sections.str_offsets =
12324 create_dwp_v2_or_v5_section (per_objfile,
12325 &dwp_file->sections.str_offsets,
12326 sections.str_offsets_offset,
12327 sections.str_offsets_size);
12328 /* The "str" section is global to the entire DWP file. */
12329 dwo_file->sections.str = dwp_file->sections.str;
12330 /* The info or types section is assigned below to dwo_unit,
12331 there's no need to record it in dwo_file.
12332 Also, we can't simply record type sections in dwo_file because
12333 we record a pointer into the vector in dwo_unit. As we collect more
12334 types we'll grow the vector and eventually have to reallocate space
12335 for it, invalidating all copies of pointers into the previous
12336 contents. */
12337 *dwo_file_slot = dwo_file;
12338 }
12339 else
12340 {
12341 if (dwarf_read_debug)
12342 {
12343 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12344 virtual_dwo_name.c_str ());
12345 }
12346 dwo_file = (struct dwo_file *) *dwo_file_slot;
12347 }
12348
12349 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12350 dwo_unit->dwo_file = dwo_file;
12351 dwo_unit->signature = signature;
12352 dwo_unit->section =
12353 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12354 *dwo_unit->section = create_dwp_v2_or_v5_section
12355 (per_objfile,
12356 is_debug_types
12357 ? &dwp_file->sections.types
12358 : &dwp_file->sections.info,
12359 sections.info_or_types_offset,
12360 sections.info_or_types_size);
12361 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12362
12363 return dwo_unit;
12364 }
12365
12366 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12367 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12368 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12369 This is for DWP version 5 files. */
12370
12371 static struct dwo_unit *
12372 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12373 struct dwp_file *dwp_file,
12374 uint32_t unit_index,
12375 const char *comp_dir,
12376 ULONGEST signature, int is_debug_types)
12377 {
12378 const struct dwp_hash_table *dwp_htab
12379 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12380 bfd *dbfd = dwp_file->dbfd.get ();
12381 const char *kind = is_debug_types ? "TU" : "CU";
12382 struct dwo_file *dwo_file;
12383 struct dwo_unit *dwo_unit;
12384 struct virtual_v2_or_v5_dwo_sections sections {};
12385 void **dwo_file_slot;
12386
12387 gdb_assert (dwp_file->version == 5);
12388
12389 if (dwarf_read_debug)
12390 {
12391 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V5 file: %s\n",
12392 kind,
12393 pulongest (unit_index), hex_string (signature),
12394 dwp_file->name);
12395 }
12396
12397 /* Fetch the section offsets of this DWO unit. */
12398
12399 /* memset (&sections, 0, sizeof (sections)); */
12400
12401 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12402 {
12403 uint32_t offset = read_4_bytes (dbfd,
12404 dwp_htab->section_pool.v5.offsets
12405 + (((unit_index - 1)
12406 * dwp_htab->nr_columns
12407 + i)
12408 * sizeof (uint32_t)));
12409 uint32_t size = read_4_bytes (dbfd,
12410 dwp_htab->section_pool.v5.sizes
12411 + (((unit_index - 1) * dwp_htab->nr_columns
12412 + i)
12413 * sizeof (uint32_t)));
12414
12415 switch (dwp_htab->section_pool.v5.section_ids[i])
12416 {
12417 case DW_SECT_ABBREV_V5:
12418 sections.abbrev_offset = offset;
12419 sections.abbrev_size = size;
12420 break;
12421 case DW_SECT_INFO_V5:
12422 sections.info_or_types_offset = offset;
12423 sections.info_or_types_size = size;
12424 break;
12425 case DW_SECT_LINE_V5:
12426 sections.line_offset = offset;
12427 sections.line_size = size;
12428 break;
12429 case DW_SECT_LOCLISTS_V5:
12430 sections.loclists_offset = offset;
12431 sections.loclists_size = size;
12432 break;
12433 case DW_SECT_MACRO_V5:
12434 sections.macro_offset = offset;
12435 sections.macro_size = size;
12436 break;
12437 case DW_SECT_RNGLISTS_V5:
12438 sections.rnglists_offset = offset;
12439 sections.rnglists_size = size;
12440 break;
12441 case DW_SECT_STR_OFFSETS_V5:
12442 sections.str_offsets_offset = offset;
12443 sections.str_offsets_size = size;
12444 break;
12445 case DW_SECT_RESERVED_V5:
12446 default:
12447 break;
12448 }
12449 }
12450
12451 /* It's easier for the rest of the code if we fake a struct dwo_file and
12452 have dwo_unit "live" in that. At least for now.
12453
12454 The DWP file can be made up of a random collection of CUs and TUs.
12455 However, for each CU + set of TUs that came from the same original DWO
12456 file, we can combine them back into a virtual DWO file to save space
12457 (fewer struct dwo_file objects to allocate). Remember that for really
12458 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12459
12460 std::string virtual_dwo_name =
12461 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
12462 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12463 (long) (sections.line_size ? sections.line_offset : 0),
12464 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12465 (long) (sections.str_offsets_size
12466 ? sections.str_offsets_offset : 0),
12467 (long) (sections.macro_size ? sections.macro_offset : 0),
12468 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
12469 /* Can we use an existing virtual DWO file? */
12470 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
12471 virtual_dwo_name.c_str (),
12472 comp_dir);
12473 /* Create one if necessary. */
12474 if (*dwo_file_slot == NULL)
12475 {
12476 if (dwarf_read_debug)
12477 {
12478 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12479 virtual_dwo_name.c_str ());
12480 }
12481 dwo_file = new struct dwo_file;
12482 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12483 dwo_file->comp_dir = comp_dir;
12484 dwo_file->sections.abbrev =
12485 create_dwp_v2_or_v5_section (per_objfile,
12486 &dwp_file->sections.abbrev,
12487 sections.abbrev_offset,
12488 sections.abbrev_size);
12489 dwo_file->sections.line =
12490 create_dwp_v2_or_v5_section (per_objfile,
12491 &dwp_file->sections.line,
12492 sections.line_offset, sections.line_size);
12493 dwo_file->sections.macro =
12494 create_dwp_v2_or_v5_section (per_objfile,
12495 &dwp_file->sections.macro,
12496 sections.macro_offset,
12497 sections.macro_size);
12498 dwo_file->sections.loclists =
12499 create_dwp_v2_or_v5_section (per_objfile,
12500 &dwp_file->sections.loclists,
12501 sections.loclists_offset,
12502 sections.loclists_size);
12503 dwo_file->sections.rnglists =
12504 create_dwp_v2_or_v5_section (per_objfile,
12505 &dwp_file->sections.rnglists,
12506 sections.rnglists_offset,
12507 sections.rnglists_size);
12508 dwo_file->sections.str_offsets =
12509 create_dwp_v2_or_v5_section (per_objfile,
12510 &dwp_file->sections.str_offsets,
12511 sections.str_offsets_offset,
12512 sections.str_offsets_size);
12513 /* The "str" section is global to the entire DWP file. */
12514 dwo_file->sections.str = dwp_file->sections.str;
12515 /* The info or types section is assigned below to dwo_unit,
12516 there's no need to record it in dwo_file.
12517 Also, we can't simply record type sections in dwo_file because
12518 we record a pointer into the vector in dwo_unit. As we collect more
12519 types we'll grow the vector and eventually have to reallocate space
12520 for it, invalidating all copies of pointers into the previous
12521 contents. */
12522 *dwo_file_slot = dwo_file;
12523 }
12524 else
12525 {
12526 if (dwarf_read_debug)
12527 {
12528 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12529 virtual_dwo_name.c_str ());
12530 }
12531 dwo_file = (struct dwo_file *) *dwo_file_slot;
12532 }
12533
12534 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12535 dwo_unit->dwo_file = dwo_file;
12536 dwo_unit->signature = signature;
12537 dwo_unit->section
12538 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12539 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
12540 &dwp_file->sections.info,
12541 sections.info_or_types_offset,
12542 sections.info_or_types_size);
12543 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12544
12545 return dwo_unit;
12546 }
12547
12548 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12549 Returns NULL if the signature isn't found. */
12550
12551 static struct dwo_unit *
12552 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12553 struct dwp_file *dwp_file, const char *comp_dir,
12554 ULONGEST signature, int is_debug_types)
12555 {
12556 const struct dwp_hash_table *dwp_htab =
12557 is_debug_types ? dwp_file->tus : dwp_file->cus;
12558 bfd *dbfd = dwp_file->dbfd.get ();
12559 uint32_t mask = dwp_htab->nr_slots - 1;
12560 uint32_t hash = signature & mask;
12561 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12562 unsigned int i;
12563 void **slot;
12564 struct dwo_unit find_dwo_cu;
12565
12566 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12567 find_dwo_cu.signature = signature;
12568 slot = htab_find_slot (is_debug_types
12569 ? dwp_file->loaded_tus.get ()
12570 : dwp_file->loaded_cus.get (),
12571 &find_dwo_cu, INSERT);
12572
12573 if (*slot != NULL)
12574 return (struct dwo_unit *) *slot;
12575
12576 /* Use a for loop so that we don't loop forever on bad debug info. */
12577 for (i = 0; i < dwp_htab->nr_slots; ++i)
12578 {
12579 ULONGEST signature_in_table;
12580
12581 signature_in_table =
12582 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12583 if (signature_in_table == signature)
12584 {
12585 uint32_t unit_index =
12586 read_4_bytes (dbfd,
12587 dwp_htab->unit_table + hash * sizeof (uint32_t));
12588
12589 if (dwp_file->version == 1)
12590 {
12591 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12592 unit_index, comp_dir,
12593 signature, is_debug_types);
12594 }
12595 else if (dwp_file->version == 2)
12596 {
12597 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12598 unit_index, comp_dir,
12599 signature, is_debug_types);
12600 }
12601 else /* version == 5 */
12602 {
12603 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12604 unit_index, comp_dir,
12605 signature, is_debug_types);
12606 }
12607 return (struct dwo_unit *) *slot;
12608 }
12609 if (signature_in_table == 0)
12610 return NULL;
12611 hash = (hash + hash2) & mask;
12612 }
12613
12614 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12615 " [in module %s]"),
12616 dwp_file->name);
12617 }
12618
12619 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12620 Open the file specified by FILE_NAME and hand it off to BFD for
12621 preliminary analysis. Return a newly initialized bfd *, which
12622 includes a canonicalized copy of FILE_NAME.
12623 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12624 SEARCH_CWD is true if the current directory is to be searched.
12625 It will be searched before debug-file-directory.
12626 If successful, the file is added to the bfd include table of the
12627 objfile's bfd (see gdb_bfd_record_inclusion).
12628 If unable to find/open the file, return NULL.
12629 NOTE: This function is derived from symfile_bfd_open. */
12630
12631 static gdb_bfd_ref_ptr
12632 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12633 const char *file_name, int is_dwp, int search_cwd)
12634 {
12635 int desc;
12636 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12637 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12638 to debug_file_directory. */
12639 const char *search_path;
12640 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12641
12642 gdb::unique_xmalloc_ptr<char> search_path_holder;
12643 if (search_cwd)
12644 {
12645 if (*debug_file_directory != '\0')
12646 {
12647 search_path_holder.reset (concat (".", dirname_separator_string,
12648 debug_file_directory,
12649 (char *) NULL));
12650 search_path = search_path_holder.get ();
12651 }
12652 else
12653 search_path = ".";
12654 }
12655 else
12656 search_path = debug_file_directory;
12657
12658 openp_flags flags = OPF_RETURN_REALPATH;
12659 if (is_dwp)
12660 flags |= OPF_SEARCH_IN_PATH;
12661
12662 gdb::unique_xmalloc_ptr<char> absolute_name;
12663 desc = openp (search_path, flags, file_name,
12664 O_RDONLY | O_BINARY, &absolute_name);
12665 if (desc < 0)
12666 return NULL;
12667
12668 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12669 gnutarget, desc));
12670 if (sym_bfd == NULL)
12671 return NULL;
12672 bfd_set_cacheable (sym_bfd.get (), 1);
12673
12674 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12675 return NULL;
12676
12677 /* Success. Record the bfd as having been included by the objfile's bfd.
12678 This is important because things like demangled_names_hash lives in the
12679 objfile's per_bfd space and may have references to things like symbol
12680 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12681 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12682
12683 return sym_bfd;
12684 }
12685
12686 /* Try to open DWO file FILE_NAME.
12687 COMP_DIR is the DW_AT_comp_dir attribute.
12688 The result is the bfd handle of the file.
12689 If there is a problem finding or opening the file, return NULL.
12690 Upon success, the canonicalized path of the file is stored in the bfd,
12691 same as symfile_bfd_open. */
12692
12693 static gdb_bfd_ref_ptr
12694 open_dwo_file (dwarf2_per_objfile *per_objfile,
12695 const char *file_name, const char *comp_dir)
12696 {
12697 if (IS_ABSOLUTE_PATH (file_name))
12698 return try_open_dwop_file (per_objfile, file_name,
12699 0 /*is_dwp*/, 0 /*search_cwd*/);
12700
12701 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12702
12703 if (comp_dir != NULL)
12704 {
12705 gdb::unique_xmalloc_ptr<char> path_to_try
12706 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12707
12708 /* NOTE: If comp_dir is a relative path, this will also try the
12709 search path, which seems useful. */
12710 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12711 0 /*is_dwp*/,
12712 1 /*search_cwd*/));
12713 if (abfd != NULL)
12714 return abfd;
12715 }
12716
12717 /* That didn't work, try debug-file-directory, which, despite its name,
12718 is a list of paths. */
12719
12720 if (*debug_file_directory == '\0')
12721 return NULL;
12722
12723 return try_open_dwop_file (per_objfile, file_name,
12724 0 /*is_dwp*/, 1 /*search_cwd*/);
12725 }
12726
12727 /* This function is mapped across the sections and remembers the offset and
12728 size of each of the DWO debugging sections we are interested in. */
12729
12730 static void
12731 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12732 dwo_sections *dwo_sections)
12733 {
12734 const struct dwop_section_names *names = &dwop_section_names;
12735
12736 if (section_is_p (sectp->name, &names->abbrev_dwo))
12737 {
12738 dwo_sections->abbrev.s.section = sectp;
12739 dwo_sections->abbrev.size = bfd_section_size (sectp);
12740 }
12741 else if (section_is_p (sectp->name, &names->info_dwo))
12742 {
12743 dwo_sections->info.s.section = sectp;
12744 dwo_sections->info.size = bfd_section_size (sectp);
12745 }
12746 else if (section_is_p (sectp->name, &names->line_dwo))
12747 {
12748 dwo_sections->line.s.section = sectp;
12749 dwo_sections->line.size = bfd_section_size (sectp);
12750 }
12751 else if (section_is_p (sectp->name, &names->loc_dwo))
12752 {
12753 dwo_sections->loc.s.section = sectp;
12754 dwo_sections->loc.size = bfd_section_size (sectp);
12755 }
12756 else if (section_is_p (sectp->name, &names->loclists_dwo))
12757 {
12758 dwo_sections->loclists.s.section = sectp;
12759 dwo_sections->loclists.size = bfd_section_size (sectp);
12760 }
12761 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12762 {
12763 dwo_sections->macinfo.s.section = sectp;
12764 dwo_sections->macinfo.size = bfd_section_size (sectp);
12765 }
12766 else if (section_is_p (sectp->name, &names->macro_dwo))
12767 {
12768 dwo_sections->macro.s.section = sectp;
12769 dwo_sections->macro.size = bfd_section_size (sectp);
12770 }
12771 else if (section_is_p (sectp->name, &names->rnglists_dwo))
12772 {
12773 dwo_sections->rnglists.s.section = sectp;
12774 dwo_sections->rnglists.size = bfd_section_size (sectp);
12775 }
12776 else if (section_is_p (sectp->name, &names->str_dwo))
12777 {
12778 dwo_sections->str.s.section = sectp;
12779 dwo_sections->str.size = bfd_section_size (sectp);
12780 }
12781 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12782 {
12783 dwo_sections->str_offsets.s.section = sectp;
12784 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12785 }
12786 else if (section_is_p (sectp->name, &names->types_dwo))
12787 {
12788 struct dwarf2_section_info type_section;
12789
12790 memset (&type_section, 0, sizeof (type_section));
12791 type_section.s.section = sectp;
12792 type_section.size = bfd_section_size (sectp);
12793 dwo_sections->types.push_back (type_section);
12794 }
12795 }
12796
12797 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12798 by PER_CU. This is for the non-DWP case.
12799 The result is NULL if DWO_NAME can't be found. */
12800
12801 static struct dwo_file *
12802 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12803 const char *comp_dir)
12804 {
12805 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12806
12807 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12808 if (dbfd == NULL)
12809 {
12810 if (dwarf_read_debug)
12811 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12812 return NULL;
12813 }
12814
12815 dwo_file_up dwo_file (new struct dwo_file);
12816 dwo_file->dwo_name = dwo_name;
12817 dwo_file->comp_dir = comp_dir;
12818 dwo_file->dbfd = std::move (dbfd);
12819
12820 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12821 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12822 &dwo_file->sections);
12823
12824 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12825 dwo_file->cus);
12826
12827 if (cu->per_cu->dwarf_version < 5)
12828 {
12829 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12830 dwo_file->sections.types, dwo_file->tus);
12831 }
12832 else
12833 {
12834 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12835 &dwo_file->sections.info, dwo_file->tus,
12836 rcuh_kind::TYPE);
12837 }
12838
12839 if (dwarf_read_debug)
12840 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12841
12842 return dwo_file.release ();
12843 }
12844
12845 /* This function is mapped across the sections and remembers the offset and
12846 size of each of the DWP debugging sections common to version 1 and 2 that
12847 we are interested in. */
12848
12849 static void
12850 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12851 dwp_file *dwp_file)
12852 {
12853 const struct dwop_section_names *names = &dwop_section_names;
12854 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12855
12856 /* Record the ELF section number for later lookup: this is what the
12857 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12858 gdb_assert (elf_section_nr < dwp_file->num_sections);
12859 dwp_file->elf_sections[elf_section_nr] = sectp;
12860
12861 /* Look for specific sections that we need. */
12862 if (section_is_p (sectp->name, &names->str_dwo))
12863 {
12864 dwp_file->sections.str.s.section = sectp;
12865 dwp_file->sections.str.size = bfd_section_size (sectp);
12866 }
12867 else if (section_is_p (sectp->name, &names->cu_index))
12868 {
12869 dwp_file->sections.cu_index.s.section = sectp;
12870 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12871 }
12872 else if (section_is_p (sectp->name, &names->tu_index))
12873 {
12874 dwp_file->sections.tu_index.s.section = sectp;
12875 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12876 }
12877 }
12878
12879 /* This function is mapped across the sections and remembers the offset and
12880 size of each of the DWP version 2 debugging sections that we are interested
12881 in. This is split into a separate function because we don't know if we
12882 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12883
12884 static void
12885 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12886 {
12887 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12888 const struct dwop_section_names *names = &dwop_section_names;
12889 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12890
12891 /* Record the ELF section number for later lookup: this is what the
12892 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12893 gdb_assert (elf_section_nr < dwp_file->num_sections);
12894 dwp_file->elf_sections[elf_section_nr] = sectp;
12895
12896 /* Look for specific sections that we need. */
12897 if (section_is_p (sectp->name, &names->abbrev_dwo))
12898 {
12899 dwp_file->sections.abbrev.s.section = sectp;
12900 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12901 }
12902 else if (section_is_p (sectp->name, &names->info_dwo))
12903 {
12904 dwp_file->sections.info.s.section = sectp;
12905 dwp_file->sections.info.size = bfd_section_size (sectp);
12906 }
12907 else if (section_is_p (sectp->name, &names->line_dwo))
12908 {
12909 dwp_file->sections.line.s.section = sectp;
12910 dwp_file->sections.line.size = bfd_section_size (sectp);
12911 }
12912 else if (section_is_p (sectp->name, &names->loc_dwo))
12913 {
12914 dwp_file->sections.loc.s.section = sectp;
12915 dwp_file->sections.loc.size = bfd_section_size (sectp);
12916 }
12917 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12918 {
12919 dwp_file->sections.macinfo.s.section = sectp;
12920 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12921 }
12922 else if (section_is_p (sectp->name, &names->macro_dwo))
12923 {
12924 dwp_file->sections.macro.s.section = sectp;
12925 dwp_file->sections.macro.size = bfd_section_size (sectp);
12926 }
12927 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12928 {
12929 dwp_file->sections.str_offsets.s.section = sectp;
12930 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12931 }
12932 else if (section_is_p (sectp->name, &names->types_dwo))
12933 {
12934 dwp_file->sections.types.s.section = sectp;
12935 dwp_file->sections.types.size = bfd_section_size (sectp);
12936 }
12937 }
12938
12939 /* This function is mapped across the sections and remembers the offset and
12940 size of each of the DWP version 5 debugging sections that we are interested
12941 in. This is split into a separate function because we don't know if we
12942 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12943
12944 static void
12945 dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12946 {
12947 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12948 const struct dwop_section_names *names = &dwop_section_names;
12949 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12950
12951 /* Record the ELF section number for later lookup: this is what the
12952 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12953 gdb_assert (elf_section_nr < dwp_file->num_sections);
12954 dwp_file->elf_sections[elf_section_nr] = sectp;
12955
12956 /* Look for specific sections that we need. */
12957 if (section_is_p (sectp->name, &names->abbrev_dwo))
12958 {
12959 dwp_file->sections.abbrev.s.section = sectp;
12960 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12961 }
12962 else if (section_is_p (sectp->name, &names->info_dwo))
12963 {
12964 dwp_file->sections.info.s.section = sectp;
12965 dwp_file->sections.info.size = bfd_section_size (sectp);
12966 }
12967 else if (section_is_p (sectp->name, &names->line_dwo))
12968 {
12969 dwp_file->sections.line.s.section = sectp;
12970 dwp_file->sections.line.size = bfd_section_size (sectp);
12971 }
12972 else if (section_is_p (sectp->name, &names->loclists_dwo))
12973 {
12974 dwp_file->sections.loclists.s.section = sectp;
12975 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12976 }
12977 else if (section_is_p (sectp->name, &names->macro_dwo))
12978 {
12979 dwp_file->sections.macro.s.section = sectp;
12980 dwp_file->sections.macro.size = bfd_section_size (sectp);
12981 }
12982 else if (section_is_p (sectp->name, &names->rnglists_dwo))
12983 {
12984 dwp_file->sections.rnglists.s.section = sectp;
12985 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12986 }
12987 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12988 {
12989 dwp_file->sections.str_offsets.s.section = sectp;
12990 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12991 }
12992 }
12993
12994 /* Hash function for dwp_file loaded CUs/TUs. */
12995
12996 static hashval_t
12997 hash_dwp_loaded_cutus (const void *item)
12998 {
12999 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13000
13001 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13002 return dwo_unit->signature;
13003 }
13004
13005 /* Equality function for dwp_file loaded CUs/TUs. */
13006
13007 static int
13008 eq_dwp_loaded_cutus (const void *a, const void *b)
13009 {
13010 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13011 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13012
13013 return dua->signature == dub->signature;
13014 }
13015
13016 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13017
13018 static htab_up
13019 allocate_dwp_loaded_cutus_table ()
13020 {
13021 return htab_up (htab_create_alloc (3,
13022 hash_dwp_loaded_cutus,
13023 eq_dwp_loaded_cutus,
13024 NULL, xcalloc, xfree));
13025 }
13026
13027 /* Try to open DWP file FILE_NAME.
13028 The result is the bfd handle of the file.
13029 If there is a problem finding or opening the file, return NULL.
13030 Upon success, the canonicalized path of the file is stored in the bfd,
13031 same as symfile_bfd_open. */
13032
13033 static gdb_bfd_ref_ptr
13034 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
13035 {
13036 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
13037 1 /*is_dwp*/,
13038 1 /*search_cwd*/));
13039 if (abfd != NULL)
13040 return abfd;
13041
13042 /* Work around upstream bug 15652.
13043 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13044 [Whether that's a "bug" is debatable, but it is getting in our way.]
13045 We have no real idea where the dwp file is, because gdb's realpath-ing
13046 of the executable's path may have discarded the needed info.
13047 [IWBN if the dwp file name was recorded in the executable, akin to
13048 .gnu_debuglink, but that doesn't exist yet.]
13049 Strip the directory from FILE_NAME and search again. */
13050 if (*debug_file_directory != '\0')
13051 {
13052 /* Don't implicitly search the current directory here.
13053 If the user wants to search "." to handle this case,
13054 it must be added to debug-file-directory. */
13055 return try_open_dwop_file (per_objfile, lbasename (file_name),
13056 1 /*is_dwp*/,
13057 0 /*search_cwd*/);
13058 }
13059
13060 return NULL;
13061 }
13062
13063 /* Initialize the use of the DWP file for the current objfile.
13064 By convention the name of the DWP file is ${objfile}.dwp.
13065 The result is NULL if it can't be found. */
13066
13067 static std::unique_ptr<struct dwp_file>
13068 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
13069 {
13070 struct objfile *objfile = per_objfile->objfile;
13071
13072 /* Try to find first .dwp for the binary file before any symbolic links
13073 resolving. */
13074
13075 /* If the objfile is a debug file, find the name of the real binary
13076 file and get the name of dwp file from there. */
13077 std::string dwp_name;
13078 if (objfile->separate_debug_objfile_backlink != NULL)
13079 {
13080 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13081 const char *backlink_basename = lbasename (backlink->original_name);
13082
13083 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13084 }
13085 else
13086 dwp_name = objfile->original_name;
13087
13088 dwp_name += ".dwp";
13089
13090 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
13091 if (dbfd == NULL
13092 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13093 {
13094 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13095 dwp_name = objfile_name (objfile);
13096 dwp_name += ".dwp";
13097 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
13098 }
13099
13100 if (dbfd == NULL)
13101 {
13102 if (dwarf_read_debug)
13103 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13104 return std::unique_ptr<dwp_file> ();
13105 }
13106
13107 const char *name = bfd_get_filename (dbfd.get ());
13108 std::unique_ptr<struct dwp_file> dwp_file
13109 (new struct dwp_file (name, std::move (dbfd)));
13110
13111 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13112 dwp_file->elf_sections =
13113 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
13114 dwp_file->num_sections, asection *);
13115
13116 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13117 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
13118 dwp_file.get ());
13119
13120 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
13121
13122 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
13123
13124 /* The DWP file version is stored in the hash table. Oh well. */
13125 if (dwp_file->cus && dwp_file->tus
13126 && dwp_file->cus->version != dwp_file->tus->version)
13127 {
13128 /* Technically speaking, we should try to limp along, but this is
13129 pretty bizarre. We use pulongest here because that's the established
13130 portability solution (e.g, we cannot use %u for uint32_t). */
13131 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13132 " TU version %s [in DWP file %s]"),
13133 pulongest (dwp_file->cus->version),
13134 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13135 }
13136
13137 if (dwp_file->cus)
13138 dwp_file->version = dwp_file->cus->version;
13139 else if (dwp_file->tus)
13140 dwp_file->version = dwp_file->tus->version;
13141 else
13142 dwp_file->version = 2;
13143
13144 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13145 {
13146 if (dwp_file->version == 2)
13147 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
13148 dwp_file.get ());
13149 else
13150 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
13151 dwp_file.get ());
13152 }
13153
13154 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
13155 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
13156
13157 if (dwarf_read_debug)
13158 {
13159 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13160 fprintf_unfiltered (gdb_stdlog,
13161 " %s CUs, %s TUs\n",
13162 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13163 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13164 }
13165
13166 return dwp_file;
13167 }
13168
13169 /* Wrapper around open_and_init_dwp_file, only open it once. */
13170
13171 static struct dwp_file *
13172 get_dwp_file (dwarf2_per_objfile *per_objfile)
13173 {
13174 if (!per_objfile->per_bfd->dwp_checked)
13175 {
13176 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
13177 per_objfile->per_bfd->dwp_checked = 1;
13178 }
13179 return per_objfile->per_bfd->dwp_file.get ();
13180 }
13181
13182 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13183 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13184 or in the DWP file for the objfile, referenced by THIS_UNIT.
13185 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13186 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13187
13188 This is called, for example, when wanting to read a variable with a
13189 complex location. Therefore we don't want to do file i/o for every call.
13190 Therefore we don't want to look for a DWO file on every call.
13191 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13192 then we check if we've already seen DWO_NAME, and only THEN do we check
13193 for a DWO file.
13194
13195 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13196 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13197
13198 static struct dwo_unit *
13199 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13200 ULONGEST signature, int is_debug_types)
13201 {
13202 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13203 struct objfile *objfile = per_objfile->objfile;
13204 const char *kind = is_debug_types ? "TU" : "CU";
13205 void **dwo_file_slot;
13206 struct dwo_file *dwo_file;
13207 struct dwp_file *dwp_file;
13208
13209 /* First see if there's a DWP file.
13210 If we have a DWP file but didn't find the DWO inside it, don't
13211 look for the original DWO file. It makes gdb behave differently
13212 depending on whether one is debugging in the build tree. */
13213
13214 dwp_file = get_dwp_file (per_objfile);
13215 if (dwp_file != NULL)
13216 {
13217 const struct dwp_hash_table *dwp_htab =
13218 is_debug_types ? dwp_file->tus : dwp_file->cus;
13219
13220 if (dwp_htab != NULL)
13221 {
13222 struct dwo_unit *dwo_cutu =
13223 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
13224 is_debug_types);
13225
13226 if (dwo_cutu != NULL)
13227 {
13228 if (dwarf_read_debug)
13229 {
13230 fprintf_unfiltered (gdb_stdlog,
13231 "Virtual DWO %s %s found: @%s\n",
13232 kind, hex_string (signature),
13233 host_address_to_string (dwo_cutu));
13234 }
13235 return dwo_cutu;
13236 }
13237 }
13238 }
13239 else
13240 {
13241 /* No DWP file, look for the DWO file. */
13242
13243 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
13244 if (*dwo_file_slot == NULL)
13245 {
13246 /* Read in the file and build a table of the CUs/TUs it contains. */
13247 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
13248 }
13249 /* NOTE: This will be NULL if unable to open the file. */
13250 dwo_file = (struct dwo_file *) *dwo_file_slot;
13251
13252 if (dwo_file != NULL)
13253 {
13254 struct dwo_unit *dwo_cutu = NULL;
13255
13256 if (is_debug_types && dwo_file->tus)
13257 {
13258 struct dwo_unit find_dwo_cutu;
13259
13260 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13261 find_dwo_cutu.signature = signature;
13262 dwo_cutu
13263 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
13264 &find_dwo_cutu);
13265 }
13266 else if (!is_debug_types && dwo_file->cus)
13267 {
13268 struct dwo_unit find_dwo_cutu;
13269
13270 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13271 find_dwo_cutu.signature = signature;
13272 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
13273 &find_dwo_cutu);
13274 }
13275
13276 if (dwo_cutu != NULL)
13277 {
13278 if (dwarf_read_debug)
13279 {
13280 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13281 kind, dwo_name, hex_string (signature),
13282 host_address_to_string (dwo_cutu));
13283 }
13284 return dwo_cutu;
13285 }
13286 }
13287 }
13288
13289 /* We didn't find it. This could mean a dwo_id mismatch, or
13290 someone deleted the DWO/DWP file, or the search path isn't set up
13291 correctly to find the file. */
13292
13293 if (dwarf_read_debug)
13294 {
13295 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13296 kind, dwo_name, hex_string (signature));
13297 }
13298
13299 /* This is a warning and not a complaint because it can be caused by
13300 pilot error (e.g., user accidentally deleting the DWO). */
13301 {
13302 /* Print the name of the DWP file if we looked there, helps the user
13303 better diagnose the problem. */
13304 std::string dwp_text;
13305
13306 if (dwp_file != NULL)
13307 dwp_text = string_printf (" [in DWP file %s]",
13308 lbasename (dwp_file->name));
13309
13310 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13311 " [in module %s]"),
13312 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13313 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
13314 }
13315 return NULL;
13316 }
13317
13318 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13319 See lookup_dwo_cutu_unit for details. */
13320
13321 static struct dwo_unit *
13322 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13323 ULONGEST signature)
13324 {
13325 gdb_assert (!cu->per_cu->is_debug_types);
13326
13327 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
13328 }
13329
13330 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13331 See lookup_dwo_cutu_unit for details. */
13332
13333 static struct dwo_unit *
13334 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
13335 {
13336 gdb_assert (cu->per_cu->is_debug_types);
13337
13338 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13339
13340 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
13341 }
13342
13343 /* Traversal function for queue_and_load_all_dwo_tus. */
13344
13345 static int
13346 queue_and_load_dwo_tu (void **slot, void *info)
13347 {
13348 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13349 dwarf2_cu *cu = (dwarf2_cu *) info;
13350 ULONGEST signature = dwo_unit->signature;
13351 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
13352
13353 if (sig_type != NULL)
13354 {
13355 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13356
13357 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13358 a real dependency of PER_CU on SIG_TYPE. That is detected later
13359 while processing PER_CU. */
13360 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
13361 load_full_type_unit (sig_cu, cu->per_objfile);
13362 cu->per_cu->imported_symtabs_push (sig_cu);
13363 }
13364
13365 return 1;
13366 }
13367
13368 /* Queue all TUs contained in the DWO of CU to be read in.
13369 The DWO may have the only definition of the type, though it may not be
13370 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13371 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13372
13373 static void
13374 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
13375 {
13376 struct dwo_unit *dwo_unit;
13377 struct dwo_file *dwo_file;
13378
13379 gdb_assert (cu != nullptr);
13380 gdb_assert (!cu->per_cu->is_debug_types);
13381 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
13382
13383 dwo_unit = cu->dwo_unit;
13384 gdb_assert (dwo_unit != NULL);
13385
13386 dwo_file = dwo_unit->dwo_file;
13387 if (dwo_file->tus != NULL)
13388 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
13389 }
13390
13391 /* Read in various DIEs. */
13392
13393 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13394 Inherit only the children of the DW_AT_abstract_origin DIE not being
13395 already referenced by DW_AT_abstract_origin from the children of the
13396 current DIE. */
13397
13398 static void
13399 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13400 {
13401 struct die_info *child_die;
13402 sect_offset *offsetp;
13403 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13404 struct die_info *origin_die;
13405 /* Iterator of the ORIGIN_DIE children. */
13406 struct die_info *origin_child_die;
13407 struct attribute *attr;
13408 struct dwarf2_cu *origin_cu;
13409 struct pending **origin_previous_list_in_scope;
13410
13411 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13412 if (!attr)
13413 return;
13414
13415 /* Note that following die references may follow to a die in a
13416 different cu. */
13417
13418 origin_cu = cu;
13419 origin_die = follow_die_ref (die, attr, &origin_cu);
13420
13421 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13422 symbols in. */
13423 origin_previous_list_in_scope = origin_cu->list_in_scope;
13424 origin_cu->list_in_scope = cu->list_in_scope;
13425
13426 if (die->tag != origin_die->tag
13427 && !(die->tag == DW_TAG_inlined_subroutine
13428 && origin_die->tag == DW_TAG_subprogram))
13429 complaint (_("DIE %s and its abstract origin %s have different tags"),
13430 sect_offset_str (die->sect_off),
13431 sect_offset_str (origin_die->sect_off));
13432
13433 std::vector<sect_offset> offsets;
13434
13435 for (child_die = die->child;
13436 child_die && child_die->tag;
13437 child_die = child_die->sibling)
13438 {
13439 struct die_info *child_origin_die;
13440 struct dwarf2_cu *child_origin_cu;
13441
13442 /* We are trying to process concrete instance entries:
13443 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13444 it's not relevant to our analysis here. i.e. detecting DIEs that are
13445 present in the abstract instance but not referenced in the concrete
13446 one. */
13447 if (child_die->tag == DW_TAG_call_site
13448 || child_die->tag == DW_TAG_GNU_call_site)
13449 continue;
13450
13451 /* For each CHILD_DIE, find the corresponding child of
13452 ORIGIN_DIE. If there is more than one layer of
13453 DW_AT_abstract_origin, follow them all; there shouldn't be,
13454 but GCC versions at least through 4.4 generate this (GCC PR
13455 40573). */
13456 child_origin_die = child_die;
13457 child_origin_cu = cu;
13458 while (1)
13459 {
13460 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13461 child_origin_cu);
13462 if (attr == NULL)
13463 break;
13464 child_origin_die = follow_die_ref (child_origin_die, attr,
13465 &child_origin_cu);
13466 }
13467
13468 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13469 counterpart may exist. */
13470 if (child_origin_die != child_die)
13471 {
13472 if (child_die->tag != child_origin_die->tag
13473 && !(child_die->tag == DW_TAG_inlined_subroutine
13474 && child_origin_die->tag == DW_TAG_subprogram))
13475 complaint (_("Child DIE %s and its abstract origin %s have "
13476 "different tags"),
13477 sect_offset_str (child_die->sect_off),
13478 sect_offset_str (child_origin_die->sect_off));
13479 if (child_origin_die->parent != origin_die)
13480 complaint (_("Child DIE %s and its abstract origin %s have "
13481 "different parents"),
13482 sect_offset_str (child_die->sect_off),
13483 sect_offset_str (child_origin_die->sect_off));
13484 else
13485 offsets.push_back (child_origin_die->sect_off);
13486 }
13487 }
13488 std::sort (offsets.begin (), offsets.end ());
13489 sect_offset *offsets_end = offsets.data () + offsets.size ();
13490 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13491 if (offsetp[-1] == *offsetp)
13492 complaint (_("Multiple children of DIE %s refer "
13493 "to DIE %s as their abstract origin"),
13494 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13495
13496 offsetp = offsets.data ();
13497 origin_child_die = origin_die->child;
13498 while (origin_child_die && origin_child_die->tag)
13499 {
13500 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13501 while (offsetp < offsets_end
13502 && *offsetp < origin_child_die->sect_off)
13503 offsetp++;
13504 if (offsetp >= offsets_end
13505 || *offsetp > origin_child_die->sect_off)
13506 {
13507 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13508 Check whether we're already processing ORIGIN_CHILD_DIE.
13509 This can happen with mutually referenced abstract_origins.
13510 PR 16581. */
13511 if (!origin_child_die->in_process)
13512 process_die (origin_child_die, origin_cu);
13513 }
13514 origin_child_die = origin_child_die->sibling;
13515 }
13516 origin_cu->list_in_scope = origin_previous_list_in_scope;
13517
13518 if (cu != origin_cu)
13519 compute_delayed_physnames (origin_cu);
13520 }
13521
13522 static void
13523 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13524 {
13525 struct objfile *objfile = cu->per_objfile->objfile;
13526 struct gdbarch *gdbarch = objfile->arch ();
13527 struct context_stack *newobj;
13528 CORE_ADDR lowpc;
13529 CORE_ADDR highpc;
13530 struct die_info *child_die;
13531 struct attribute *attr, *call_line, *call_file;
13532 const char *name;
13533 CORE_ADDR baseaddr;
13534 struct block *block;
13535 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13536 std::vector<struct symbol *> template_args;
13537 struct template_symbol *templ_func = NULL;
13538
13539 if (inlined_func)
13540 {
13541 /* If we do not have call site information, we can't show the
13542 caller of this inlined function. That's too confusing, so
13543 only use the scope for local variables. */
13544 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13545 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13546 if (call_line == NULL || call_file == NULL)
13547 {
13548 read_lexical_block_scope (die, cu);
13549 return;
13550 }
13551 }
13552
13553 baseaddr = objfile->text_section_offset ();
13554
13555 name = dwarf2_name (die, cu);
13556
13557 /* Ignore functions with missing or empty names. These are actually
13558 illegal according to the DWARF standard. */
13559 if (name == NULL)
13560 {
13561 complaint (_("missing name for subprogram DIE at %s"),
13562 sect_offset_str (die->sect_off));
13563 return;
13564 }
13565
13566 /* Ignore functions with missing or invalid low and high pc attributes. */
13567 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13568 <= PC_BOUNDS_INVALID)
13569 {
13570 attr = dwarf2_attr (die, DW_AT_external, cu);
13571 if (!attr || !DW_UNSND (attr))
13572 complaint (_("cannot get low and high bounds "
13573 "for subprogram DIE at %s"),
13574 sect_offset_str (die->sect_off));
13575 return;
13576 }
13577
13578 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13579 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13580
13581 /* If we have any template arguments, then we must allocate a
13582 different sort of symbol. */
13583 for (child_die = die->child; child_die; child_die = child_die->sibling)
13584 {
13585 if (child_die->tag == DW_TAG_template_type_param
13586 || child_die->tag == DW_TAG_template_value_param)
13587 {
13588 templ_func = new (&objfile->objfile_obstack) template_symbol;
13589 templ_func->subclass = SYMBOL_TEMPLATE;
13590 break;
13591 }
13592 }
13593
13594 newobj = cu->get_builder ()->push_context (0, lowpc);
13595 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13596 (struct symbol *) templ_func);
13597
13598 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13599 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13600 cu->language);
13601
13602 /* If there is a location expression for DW_AT_frame_base, record
13603 it. */
13604 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13605 if (attr != nullptr)
13606 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13607
13608 /* If there is a location for the static link, record it. */
13609 newobj->static_link = NULL;
13610 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13611 if (attr != nullptr)
13612 {
13613 newobj->static_link
13614 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13615 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13616 cu->addr_type ());
13617 }
13618
13619 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13620
13621 if (die->child != NULL)
13622 {
13623 child_die = die->child;
13624 while (child_die && child_die->tag)
13625 {
13626 if (child_die->tag == DW_TAG_template_type_param
13627 || child_die->tag == DW_TAG_template_value_param)
13628 {
13629 struct symbol *arg = new_symbol (child_die, NULL, cu);
13630
13631 if (arg != NULL)
13632 template_args.push_back (arg);
13633 }
13634 else
13635 process_die (child_die, cu);
13636 child_die = child_die->sibling;
13637 }
13638 }
13639
13640 inherit_abstract_dies (die, cu);
13641
13642 /* If we have a DW_AT_specification, we might need to import using
13643 directives from the context of the specification DIE. See the
13644 comment in determine_prefix. */
13645 if (cu->language == language_cplus
13646 && dwarf2_attr (die, DW_AT_specification, cu))
13647 {
13648 struct dwarf2_cu *spec_cu = cu;
13649 struct die_info *spec_die = die_specification (die, &spec_cu);
13650
13651 while (spec_die)
13652 {
13653 child_die = spec_die->child;
13654 while (child_die && child_die->tag)
13655 {
13656 if (child_die->tag == DW_TAG_imported_module)
13657 process_die (child_die, spec_cu);
13658 child_die = child_die->sibling;
13659 }
13660
13661 /* In some cases, GCC generates specification DIEs that
13662 themselves contain DW_AT_specification attributes. */
13663 spec_die = die_specification (spec_die, &spec_cu);
13664 }
13665 }
13666
13667 struct context_stack cstk = cu->get_builder ()->pop_context ();
13668 /* Make a block for the local symbols within. */
13669 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13670 cstk.static_link, lowpc, highpc);
13671
13672 /* For C++, set the block's scope. */
13673 if ((cu->language == language_cplus
13674 || cu->language == language_fortran
13675 || cu->language == language_d
13676 || cu->language == language_rust)
13677 && cu->processing_has_namespace_info)
13678 block_set_scope (block, determine_prefix (die, cu),
13679 &objfile->objfile_obstack);
13680
13681 /* If we have address ranges, record them. */
13682 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13683
13684 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13685
13686 /* Attach template arguments to function. */
13687 if (!template_args.empty ())
13688 {
13689 gdb_assert (templ_func != NULL);
13690
13691 templ_func->n_template_arguments = template_args.size ();
13692 templ_func->template_arguments
13693 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13694 templ_func->n_template_arguments);
13695 memcpy (templ_func->template_arguments,
13696 template_args.data (),
13697 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13698
13699 /* Make sure that the symtab is set on the new symbols. Even
13700 though they don't appear in this symtab directly, other parts
13701 of gdb assume that symbols do, and this is reasonably
13702 true. */
13703 for (symbol *sym : template_args)
13704 symbol_set_symtab (sym, symbol_symtab (templ_func));
13705 }
13706
13707 /* In C++, we can have functions nested inside functions (e.g., when
13708 a function declares a class that has methods). This means that
13709 when we finish processing a function scope, we may need to go
13710 back to building a containing block's symbol lists. */
13711 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13712 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13713
13714 /* If we've finished processing a top-level function, subsequent
13715 symbols go in the file symbol list. */
13716 if (cu->get_builder ()->outermost_context_p ())
13717 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13718 }
13719
13720 /* Process all the DIES contained within a lexical block scope. Start
13721 a new scope, process the dies, and then close the scope. */
13722
13723 static void
13724 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13725 {
13726 struct objfile *objfile = cu->per_objfile->objfile;
13727 struct gdbarch *gdbarch = objfile->arch ();
13728 CORE_ADDR lowpc, highpc;
13729 struct die_info *child_die;
13730 CORE_ADDR baseaddr;
13731
13732 baseaddr = objfile->text_section_offset ();
13733
13734 /* Ignore blocks with missing or invalid low and high pc attributes. */
13735 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13736 as multiple lexical blocks? Handling children in a sane way would
13737 be nasty. Might be easier to properly extend generic blocks to
13738 describe ranges. */
13739 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13740 {
13741 case PC_BOUNDS_NOT_PRESENT:
13742 /* DW_TAG_lexical_block has no attributes, process its children as if
13743 there was no wrapping by that DW_TAG_lexical_block.
13744 GCC does no longer produces such DWARF since GCC r224161. */
13745 for (child_die = die->child;
13746 child_die != NULL && child_die->tag;
13747 child_die = child_die->sibling)
13748 {
13749 /* We might already be processing this DIE. This can happen
13750 in an unusual circumstance -- where a subroutine A
13751 appears lexically in another subroutine B, but A actually
13752 inlines B. The recursion is broken here, rather than in
13753 inherit_abstract_dies, because it seems better to simply
13754 drop concrete children here. */
13755 if (!child_die->in_process)
13756 process_die (child_die, cu);
13757 }
13758 return;
13759 case PC_BOUNDS_INVALID:
13760 return;
13761 }
13762 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13763 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13764
13765 cu->get_builder ()->push_context (0, lowpc);
13766 if (die->child != NULL)
13767 {
13768 child_die = die->child;
13769 while (child_die && child_die->tag)
13770 {
13771 process_die (child_die, cu);
13772 child_die = child_die->sibling;
13773 }
13774 }
13775 inherit_abstract_dies (die, cu);
13776 struct context_stack cstk = cu->get_builder ()->pop_context ();
13777
13778 if (*cu->get_builder ()->get_local_symbols () != NULL
13779 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13780 {
13781 struct block *block
13782 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13783 cstk.start_addr, highpc);
13784
13785 /* Note that recording ranges after traversing children, as we
13786 do here, means that recording a parent's ranges entails
13787 walking across all its children's ranges as they appear in
13788 the address map, which is quadratic behavior.
13789
13790 It would be nicer to record the parent's ranges before
13791 traversing its children, simply overriding whatever you find
13792 there. But since we don't even decide whether to create a
13793 block until after we've traversed its children, that's hard
13794 to do. */
13795 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13796 }
13797 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13798 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13799 }
13800
13801 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13802
13803 static void
13804 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13805 {
13806 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13807 struct objfile *objfile = per_objfile->objfile;
13808 struct gdbarch *gdbarch = objfile->arch ();
13809 CORE_ADDR pc, baseaddr;
13810 struct attribute *attr;
13811 struct call_site *call_site, call_site_local;
13812 void **slot;
13813 int nparams;
13814 struct die_info *child_die;
13815
13816 baseaddr = objfile->text_section_offset ();
13817
13818 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13819 if (attr == NULL)
13820 {
13821 /* This was a pre-DWARF-5 GNU extension alias
13822 for DW_AT_call_return_pc. */
13823 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13824 }
13825 if (!attr)
13826 {
13827 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13828 "DIE %s [in module %s]"),
13829 sect_offset_str (die->sect_off), objfile_name (objfile));
13830 return;
13831 }
13832 pc = attr->value_as_address () + baseaddr;
13833 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13834
13835 if (cu->call_site_htab == NULL)
13836 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13837 NULL, &objfile->objfile_obstack,
13838 hashtab_obstack_allocate, NULL);
13839 call_site_local.pc = pc;
13840 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13841 if (*slot != NULL)
13842 {
13843 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13844 "DIE %s [in module %s]"),
13845 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13846 objfile_name (objfile));
13847 return;
13848 }
13849
13850 /* Count parameters at the caller. */
13851
13852 nparams = 0;
13853 for (child_die = die->child; child_die && child_die->tag;
13854 child_die = child_die->sibling)
13855 {
13856 if (child_die->tag != DW_TAG_call_site_parameter
13857 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13858 {
13859 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13860 "DW_TAG_call_site child DIE %s [in module %s]"),
13861 child_die->tag, sect_offset_str (child_die->sect_off),
13862 objfile_name (objfile));
13863 continue;
13864 }
13865
13866 nparams++;
13867 }
13868
13869 call_site
13870 = ((struct call_site *)
13871 obstack_alloc (&objfile->objfile_obstack,
13872 sizeof (*call_site)
13873 + (sizeof (*call_site->parameter) * (nparams - 1))));
13874 *slot = call_site;
13875 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13876 call_site->pc = pc;
13877
13878 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13879 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13880 {
13881 struct die_info *func_die;
13882
13883 /* Skip also over DW_TAG_inlined_subroutine. */
13884 for (func_die = die->parent;
13885 func_die && func_die->tag != DW_TAG_subprogram
13886 && func_die->tag != DW_TAG_subroutine_type;
13887 func_die = func_die->parent);
13888
13889 /* DW_AT_call_all_calls is a superset
13890 of DW_AT_call_all_tail_calls. */
13891 if (func_die
13892 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13893 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13894 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13895 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13896 {
13897 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13898 not complete. But keep CALL_SITE for look ups via call_site_htab,
13899 both the initial caller containing the real return address PC and
13900 the final callee containing the current PC of a chain of tail
13901 calls do not need to have the tail call list complete. But any
13902 function candidate for a virtual tail call frame searched via
13903 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13904 determined unambiguously. */
13905 }
13906 else
13907 {
13908 struct type *func_type = NULL;
13909
13910 if (func_die)
13911 func_type = get_die_type (func_die, cu);
13912 if (func_type != NULL)
13913 {
13914 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13915
13916 /* Enlist this call site to the function. */
13917 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13918 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13919 }
13920 else
13921 complaint (_("Cannot find function owning DW_TAG_call_site "
13922 "DIE %s [in module %s]"),
13923 sect_offset_str (die->sect_off), objfile_name (objfile));
13924 }
13925 }
13926
13927 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13928 if (attr == NULL)
13929 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13930 if (attr == NULL)
13931 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13932 if (attr == NULL)
13933 {
13934 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13935 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13936 }
13937 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13938 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13939 /* Keep NULL DWARF_BLOCK. */;
13940 else if (attr->form_is_block ())
13941 {
13942 struct dwarf2_locexpr_baton *dlbaton;
13943
13944 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13945 dlbaton->data = DW_BLOCK (attr)->data;
13946 dlbaton->size = DW_BLOCK (attr)->size;
13947 dlbaton->per_objfile = per_objfile;
13948 dlbaton->per_cu = cu->per_cu;
13949
13950 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13951 }
13952 else if (attr->form_is_ref ())
13953 {
13954 struct dwarf2_cu *target_cu = cu;
13955 struct die_info *target_die;
13956
13957 target_die = follow_die_ref (die, attr, &target_cu);
13958 gdb_assert (target_cu->per_objfile->objfile == objfile);
13959 if (die_is_declaration (target_die, target_cu))
13960 {
13961 const char *target_physname;
13962
13963 /* Prefer the mangled name; otherwise compute the demangled one. */
13964 target_physname = dw2_linkage_name (target_die, target_cu);
13965 if (target_physname == NULL)
13966 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13967 if (target_physname == NULL)
13968 complaint (_("DW_AT_call_target target DIE has invalid "
13969 "physname, for referencing DIE %s [in module %s]"),
13970 sect_offset_str (die->sect_off), objfile_name (objfile));
13971 else
13972 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13973 }
13974 else
13975 {
13976 CORE_ADDR lowpc;
13977
13978 /* DW_AT_entry_pc should be preferred. */
13979 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13980 <= PC_BOUNDS_INVALID)
13981 complaint (_("DW_AT_call_target target DIE has invalid "
13982 "low pc, for referencing DIE %s [in module %s]"),
13983 sect_offset_str (die->sect_off), objfile_name (objfile));
13984 else
13985 {
13986 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13987 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13988 }
13989 }
13990 }
13991 else
13992 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13993 "block nor reference, for DIE %s [in module %s]"),
13994 sect_offset_str (die->sect_off), objfile_name (objfile));
13995
13996 call_site->per_cu = cu->per_cu;
13997 call_site->per_objfile = per_objfile;
13998
13999 for (child_die = die->child;
14000 child_die && child_die->tag;
14001 child_die = child_die->sibling)
14002 {
14003 struct call_site_parameter *parameter;
14004 struct attribute *loc, *origin;
14005
14006 if (child_die->tag != DW_TAG_call_site_parameter
14007 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14008 {
14009 /* Already printed the complaint above. */
14010 continue;
14011 }
14012
14013 gdb_assert (call_site->parameter_count < nparams);
14014 parameter = &call_site->parameter[call_site->parameter_count];
14015
14016 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14017 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14018 register is contained in DW_AT_call_value. */
14019
14020 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14021 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14022 if (origin == NULL)
14023 {
14024 /* This was a pre-DWARF-5 GNU extension alias
14025 for DW_AT_call_parameter. */
14026 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14027 }
14028 if (loc == NULL && origin != NULL && origin->form_is_ref ())
14029 {
14030 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14031
14032 sect_offset sect_off = origin->get_ref_die_offset ();
14033 if (!cu->header.offset_in_cu_p (sect_off))
14034 {
14035 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14036 binding can be done only inside one CU. Such referenced DIE
14037 therefore cannot be even moved to DW_TAG_partial_unit. */
14038 complaint (_("DW_AT_call_parameter offset is not in CU for "
14039 "DW_TAG_call_site child DIE %s [in module %s]"),
14040 sect_offset_str (child_die->sect_off),
14041 objfile_name (objfile));
14042 continue;
14043 }
14044 parameter->u.param_cu_off
14045 = (cu_offset) (sect_off - cu->header.sect_off);
14046 }
14047 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
14048 {
14049 complaint (_("No DW_FORM_block* DW_AT_location for "
14050 "DW_TAG_call_site child DIE %s [in module %s]"),
14051 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14052 continue;
14053 }
14054 else
14055 {
14056 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14057 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14058 if (parameter->u.dwarf_reg != -1)
14059 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14060 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14061 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14062 &parameter->u.fb_offset))
14063 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14064 else
14065 {
14066 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14067 "for DW_FORM_block* DW_AT_location is supported for "
14068 "DW_TAG_call_site child DIE %s "
14069 "[in module %s]"),
14070 sect_offset_str (child_die->sect_off),
14071 objfile_name (objfile));
14072 continue;
14073 }
14074 }
14075
14076 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14077 if (attr == NULL)
14078 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14079 if (attr == NULL || !attr->form_is_block ())
14080 {
14081 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14082 "DW_TAG_call_site child DIE %s [in module %s]"),
14083 sect_offset_str (child_die->sect_off),
14084 objfile_name (objfile));
14085 continue;
14086 }
14087 parameter->value = DW_BLOCK (attr)->data;
14088 parameter->value_size = DW_BLOCK (attr)->size;
14089
14090 /* Parameters are not pre-cleared by memset above. */
14091 parameter->data_value = NULL;
14092 parameter->data_value_size = 0;
14093 call_site->parameter_count++;
14094
14095 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14096 if (attr == NULL)
14097 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14098 if (attr != nullptr)
14099 {
14100 if (!attr->form_is_block ())
14101 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14102 "DW_TAG_call_site child DIE %s [in module %s]"),
14103 sect_offset_str (child_die->sect_off),
14104 objfile_name (objfile));
14105 else
14106 {
14107 parameter->data_value = DW_BLOCK (attr)->data;
14108 parameter->data_value_size = DW_BLOCK (attr)->size;
14109 }
14110 }
14111 }
14112 }
14113
14114 /* Helper function for read_variable. If DIE represents a virtual
14115 table, then return the type of the concrete object that is
14116 associated with the virtual table. Otherwise, return NULL. */
14117
14118 static struct type *
14119 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14120 {
14121 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14122 if (attr == NULL)
14123 return NULL;
14124
14125 /* Find the type DIE. */
14126 struct die_info *type_die = NULL;
14127 struct dwarf2_cu *type_cu = cu;
14128
14129 if (attr->form_is_ref ())
14130 type_die = follow_die_ref (die, attr, &type_cu);
14131 if (type_die == NULL)
14132 return NULL;
14133
14134 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14135 return NULL;
14136 return die_containing_type (type_die, type_cu);
14137 }
14138
14139 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14140
14141 static void
14142 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14143 {
14144 struct rust_vtable_symbol *storage = NULL;
14145
14146 if (cu->language == language_rust)
14147 {
14148 struct type *containing_type = rust_containing_type (die, cu);
14149
14150 if (containing_type != NULL)
14151 {
14152 struct objfile *objfile = cu->per_objfile->objfile;
14153
14154 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
14155 storage->concrete_type = containing_type;
14156 storage->subclass = SYMBOL_RUST_VTABLE;
14157 }
14158 }
14159
14160 struct symbol *res = new_symbol (die, NULL, cu, storage);
14161 struct attribute *abstract_origin
14162 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14163 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14164 if (res == NULL && loc && abstract_origin)
14165 {
14166 /* We have a variable without a name, but with a location and an abstract
14167 origin. This may be a concrete instance of an abstract variable
14168 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14169 later. */
14170 struct dwarf2_cu *origin_cu = cu;
14171 struct die_info *origin_die
14172 = follow_die_ref (die, abstract_origin, &origin_cu);
14173 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14174 per_objfile->per_bfd->abstract_to_concrete
14175 [origin_die->sect_off].push_back (die->sect_off);
14176 }
14177 }
14178
14179 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14180 reading .debug_rnglists.
14181 Callback's type should be:
14182 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14183 Return true if the attributes are present and valid, otherwise,
14184 return false. */
14185
14186 template <typename Callback>
14187 static bool
14188 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14189 dwarf_tag tag, Callback &&callback)
14190 {
14191 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14192 struct objfile *objfile = per_objfile->objfile;
14193 bfd *obfd = objfile->obfd;
14194 /* Base address selection entry. */
14195 gdb::optional<CORE_ADDR> base;
14196 const gdb_byte *buffer;
14197 CORE_ADDR baseaddr;
14198 bool overflow = false;
14199 ULONGEST addr_index;
14200 struct dwarf2_section_info *rnglists_section;
14201
14202 base = cu->base_address;
14203 rnglists_section = cu_debug_rnglists_section (cu, tag);
14204 rnglists_section->read (objfile);
14205
14206 if (offset >= rnglists_section->size)
14207 {
14208 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14209 offset);
14210 return false;
14211 }
14212 buffer = rnglists_section->buffer + offset;
14213
14214 baseaddr = objfile->text_section_offset ();
14215
14216 while (1)
14217 {
14218 /* Initialize it due to a false compiler warning. */
14219 CORE_ADDR range_beginning = 0, range_end = 0;
14220 const gdb_byte *buf_end = (rnglists_section->buffer
14221 + rnglists_section->size);
14222 unsigned int bytes_read;
14223
14224 if (buffer == buf_end)
14225 {
14226 overflow = true;
14227 break;
14228 }
14229 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14230 switch (rlet)
14231 {
14232 case DW_RLE_end_of_list:
14233 break;
14234 case DW_RLE_base_address:
14235 if (buffer + cu->header.addr_size > buf_end)
14236 {
14237 overflow = true;
14238 break;
14239 }
14240 base = cu->header.read_address (obfd, buffer, &bytes_read);
14241 buffer += bytes_read;
14242 break;
14243 case DW_RLE_base_addressx:
14244 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14245 buffer += bytes_read;
14246 base = read_addr_index (cu, addr_index);
14247 break;
14248 case DW_RLE_start_length:
14249 if (buffer + cu->header.addr_size > buf_end)
14250 {
14251 overflow = true;
14252 break;
14253 }
14254 range_beginning = cu->header.read_address (obfd, buffer,
14255 &bytes_read);
14256 buffer += bytes_read;
14257 range_end = (range_beginning
14258 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14259 buffer += bytes_read;
14260 if (buffer > buf_end)
14261 {
14262 overflow = true;
14263 break;
14264 }
14265 break;
14266 case DW_RLE_startx_length:
14267 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14268 buffer += bytes_read;
14269 range_beginning = read_addr_index (cu, addr_index);
14270 if (buffer > buf_end)
14271 {
14272 overflow = true;
14273 break;
14274 }
14275 range_end = (range_beginning
14276 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14277 buffer += bytes_read;
14278 break;
14279 case DW_RLE_offset_pair:
14280 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14281 buffer += bytes_read;
14282 if (buffer > buf_end)
14283 {
14284 overflow = true;
14285 break;
14286 }
14287 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14288 buffer += bytes_read;
14289 if (buffer > buf_end)
14290 {
14291 overflow = true;
14292 break;
14293 }
14294 break;
14295 case DW_RLE_start_end:
14296 if (buffer + 2 * cu->header.addr_size > buf_end)
14297 {
14298 overflow = true;
14299 break;
14300 }
14301 range_beginning = cu->header.read_address (obfd, buffer,
14302 &bytes_read);
14303 buffer += bytes_read;
14304 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
14305 buffer += bytes_read;
14306 break;
14307 case DW_RLE_startx_endx:
14308 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14309 buffer += bytes_read;
14310 range_beginning = read_addr_index (cu, addr_index);
14311 if (buffer > buf_end)
14312 {
14313 overflow = true;
14314 break;
14315 }
14316 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14317 buffer += bytes_read;
14318 range_end = read_addr_index (cu, addr_index);
14319 break;
14320 default:
14321 complaint (_("Invalid .debug_rnglists data (no base address)"));
14322 return false;
14323 }
14324 if (rlet == DW_RLE_end_of_list || overflow)
14325 break;
14326 if (rlet == DW_RLE_base_address)
14327 continue;
14328
14329 if (range_beginning > range_end)
14330 {
14331 /* Inverted range entries are invalid. */
14332 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14333 return false;
14334 }
14335
14336 /* Empty range entries have no effect. */
14337 if (range_beginning == range_end)
14338 continue;
14339
14340 /* Only DW_RLE_offset_pair needs the base address added. */
14341 if (rlet == DW_RLE_offset_pair)
14342 {
14343 if (!base.has_value ())
14344 {
14345 /* We have no valid base address for the DW_RLE_offset_pair. */
14346 complaint (_("Invalid .debug_rnglists data (no base address for "
14347 "DW_RLE_offset_pair)"));
14348 return false;
14349 }
14350
14351 range_beginning += *base;
14352 range_end += *base;
14353 }
14354
14355 /* A not-uncommon case of bad debug info.
14356 Don't pollute the addrmap with bad data. */
14357 if (range_beginning + baseaddr == 0
14358 && !per_objfile->per_bfd->has_section_at_zero)
14359 {
14360 complaint (_(".debug_rnglists entry has start address of zero"
14361 " [in module %s]"), objfile_name (objfile));
14362 continue;
14363 }
14364
14365 callback (range_beginning, range_end);
14366 }
14367
14368 if (overflow)
14369 {
14370 complaint (_("Offset %d is not terminated "
14371 "for DW_AT_ranges attribute"),
14372 offset);
14373 return false;
14374 }
14375
14376 return true;
14377 }
14378
14379 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14380 Callback's type should be:
14381 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14382 Return 1 if the attributes are present and valid, otherwise, return 0. */
14383
14384 template <typename Callback>
14385 static int
14386 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
14387 Callback &&callback)
14388 {
14389 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14390 struct objfile *objfile = per_objfile->objfile;
14391 struct comp_unit_head *cu_header = &cu->header;
14392 bfd *obfd = objfile->obfd;
14393 unsigned int addr_size = cu_header->addr_size;
14394 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14395 /* Base address selection entry. */
14396 gdb::optional<CORE_ADDR> base;
14397 unsigned int dummy;
14398 const gdb_byte *buffer;
14399 CORE_ADDR baseaddr;
14400
14401 if (cu_header->version >= 5)
14402 return dwarf2_rnglists_process (offset, cu, tag, callback);
14403
14404 base = cu->base_address;
14405
14406 per_objfile->per_bfd->ranges.read (objfile);
14407 if (offset >= per_objfile->per_bfd->ranges.size)
14408 {
14409 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14410 offset);
14411 return 0;
14412 }
14413 buffer = per_objfile->per_bfd->ranges.buffer + offset;
14414
14415 baseaddr = objfile->text_section_offset ();
14416
14417 while (1)
14418 {
14419 CORE_ADDR range_beginning, range_end;
14420
14421 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
14422 buffer += addr_size;
14423 range_end = cu->header.read_address (obfd, buffer, &dummy);
14424 buffer += addr_size;
14425 offset += 2 * addr_size;
14426
14427 /* An end of list marker is a pair of zero addresses. */
14428 if (range_beginning == 0 && range_end == 0)
14429 /* Found the end of list entry. */
14430 break;
14431
14432 /* Each base address selection entry is a pair of 2 values.
14433 The first is the largest possible address, the second is
14434 the base address. Check for a base address here. */
14435 if ((range_beginning & mask) == mask)
14436 {
14437 /* If we found the largest possible address, then we already
14438 have the base address in range_end. */
14439 base = range_end;
14440 continue;
14441 }
14442
14443 if (!base.has_value ())
14444 {
14445 /* We have no valid base address for the ranges
14446 data. */
14447 complaint (_("Invalid .debug_ranges data (no base address)"));
14448 return 0;
14449 }
14450
14451 if (range_beginning > range_end)
14452 {
14453 /* Inverted range entries are invalid. */
14454 complaint (_("Invalid .debug_ranges data (inverted range)"));
14455 return 0;
14456 }
14457
14458 /* Empty range entries have no effect. */
14459 if (range_beginning == range_end)
14460 continue;
14461
14462 range_beginning += *base;
14463 range_end += *base;
14464
14465 /* A not-uncommon case of bad debug info.
14466 Don't pollute the addrmap with bad data. */
14467 if (range_beginning + baseaddr == 0
14468 && !per_objfile->per_bfd->has_section_at_zero)
14469 {
14470 complaint (_(".debug_ranges entry has start address of zero"
14471 " [in module %s]"), objfile_name (objfile));
14472 continue;
14473 }
14474
14475 callback (range_beginning, range_end);
14476 }
14477
14478 return 1;
14479 }
14480
14481 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14482 Return 1 if the attributes are present and valid, otherwise, return 0.
14483 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14484
14485 static int
14486 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14487 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14488 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
14489 {
14490 struct objfile *objfile = cu->per_objfile->objfile;
14491 struct gdbarch *gdbarch = objfile->arch ();
14492 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14493 int low_set = 0;
14494 CORE_ADDR low = 0;
14495 CORE_ADDR high = 0;
14496 int retval;
14497
14498 retval = dwarf2_ranges_process (offset, cu, tag,
14499 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14500 {
14501 if (ranges_pst != NULL)
14502 {
14503 CORE_ADDR lowpc;
14504 CORE_ADDR highpc;
14505
14506 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14507 range_beginning + baseaddr)
14508 - baseaddr);
14509 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14510 range_end + baseaddr)
14511 - baseaddr);
14512 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14513 lowpc, highpc - 1, ranges_pst);
14514 }
14515
14516 /* FIXME: This is recording everything as a low-high
14517 segment of consecutive addresses. We should have a
14518 data structure for discontiguous block ranges
14519 instead. */
14520 if (! low_set)
14521 {
14522 low = range_beginning;
14523 high = range_end;
14524 low_set = 1;
14525 }
14526 else
14527 {
14528 if (range_beginning < low)
14529 low = range_beginning;
14530 if (range_end > high)
14531 high = range_end;
14532 }
14533 });
14534 if (!retval)
14535 return 0;
14536
14537 if (! low_set)
14538 /* If the first entry is an end-of-list marker, the range
14539 describes an empty scope, i.e. no instructions. */
14540 return 0;
14541
14542 if (low_return)
14543 *low_return = low;
14544 if (high_return)
14545 *high_return = high;
14546 return 1;
14547 }
14548
14549 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14550 definition for the return value. *LOWPC and *HIGHPC are set iff
14551 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14552
14553 static enum pc_bounds_kind
14554 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14555 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14556 dwarf2_psymtab *pst)
14557 {
14558 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14559 struct attribute *attr;
14560 struct attribute *attr_high;
14561 CORE_ADDR low = 0;
14562 CORE_ADDR high = 0;
14563 enum pc_bounds_kind ret;
14564
14565 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14566 if (attr_high)
14567 {
14568 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14569 if (attr != nullptr)
14570 {
14571 low = attr->value_as_address ();
14572 high = attr_high->value_as_address ();
14573 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14574 high += low;
14575 }
14576 else
14577 /* Found high w/o low attribute. */
14578 return PC_BOUNDS_INVALID;
14579
14580 /* Found consecutive range of addresses. */
14581 ret = PC_BOUNDS_HIGH_LOW;
14582 }
14583 else
14584 {
14585 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14586 if (attr != NULL)
14587 {
14588 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14589 We take advantage of the fact that DW_AT_ranges does not appear
14590 in DW_TAG_compile_unit of DWO files.
14591
14592 Attributes of the form DW_FORM_rnglistx have already had their
14593 value changed by read_rnglist_index and already include
14594 DW_AT_rnglists_base, so don't need to add the ranges base,
14595 either. */
14596 int need_ranges_base = (die->tag != DW_TAG_compile_unit
14597 && attr->form != DW_FORM_rnglistx);
14598 unsigned int ranges_offset = (DW_UNSND (attr)
14599 + (need_ranges_base
14600 ? cu->ranges_base
14601 : 0));
14602
14603 /* Value of the DW_AT_ranges attribute is the offset in the
14604 .debug_ranges section. */
14605 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14606 die->tag))
14607 return PC_BOUNDS_INVALID;
14608 /* Found discontinuous range of addresses. */
14609 ret = PC_BOUNDS_RANGES;
14610 }
14611 else
14612 return PC_BOUNDS_NOT_PRESENT;
14613 }
14614
14615 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14616 if (high <= low)
14617 return PC_BOUNDS_INVALID;
14618
14619 /* When using the GNU linker, .gnu.linkonce. sections are used to
14620 eliminate duplicate copies of functions and vtables and such.
14621 The linker will arbitrarily choose one and discard the others.
14622 The AT_*_pc values for such functions refer to local labels in
14623 these sections. If the section from that file was discarded, the
14624 labels are not in the output, so the relocs get a value of 0.
14625 If this is a discarded function, mark the pc bounds as invalid,
14626 so that GDB will ignore it. */
14627 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14628 return PC_BOUNDS_INVALID;
14629
14630 *lowpc = low;
14631 if (highpc)
14632 *highpc = high;
14633 return ret;
14634 }
14635
14636 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14637 its low and high PC addresses. Do nothing if these addresses could not
14638 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14639 and HIGHPC to the high address if greater than HIGHPC. */
14640
14641 static void
14642 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14643 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14644 struct dwarf2_cu *cu)
14645 {
14646 CORE_ADDR low, high;
14647 struct die_info *child = die->child;
14648
14649 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14650 {
14651 *lowpc = std::min (*lowpc, low);
14652 *highpc = std::max (*highpc, high);
14653 }
14654
14655 /* If the language does not allow nested subprograms (either inside
14656 subprograms or lexical blocks), we're done. */
14657 if (cu->language != language_ada)
14658 return;
14659
14660 /* Check all the children of the given DIE. If it contains nested
14661 subprograms, then check their pc bounds. Likewise, we need to
14662 check lexical blocks as well, as they may also contain subprogram
14663 definitions. */
14664 while (child && child->tag)
14665 {
14666 if (child->tag == DW_TAG_subprogram
14667 || child->tag == DW_TAG_lexical_block)
14668 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14669 child = child->sibling;
14670 }
14671 }
14672
14673 /* Get the low and high pc's represented by the scope DIE, and store
14674 them in *LOWPC and *HIGHPC. If the correct values can't be
14675 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14676
14677 static void
14678 get_scope_pc_bounds (struct die_info *die,
14679 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14680 struct dwarf2_cu *cu)
14681 {
14682 CORE_ADDR best_low = (CORE_ADDR) -1;
14683 CORE_ADDR best_high = (CORE_ADDR) 0;
14684 CORE_ADDR current_low, current_high;
14685
14686 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14687 >= PC_BOUNDS_RANGES)
14688 {
14689 best_low = current_low;
14690 best_high = current_high;
14691 }
14692 else
14693 {
14694 struct die_info *child = die->child;
14695
14696 while (child && child->tag)
14697 {
14698 switch (child->tag) {
14699 case DW_TAG_subprogram:
14700 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14701 break;
14702 case DW_TAG_namespace:
14703 case DW_TAG_module:
14704 /* FIXME: carlton/2004-01-16: Should we do this for
14705 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14706 that current GCC's always emit the DIEs corresponding
14707 to definitions of methods of classes as children of a
14708 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14709 the DIEs giving the declarations, which could be
14710 anywhere). But I don't see any reason why the
14711 standards says that they have to be there. */
14712 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14713
14714 if (current_low != ((CORE_ADDR) -1))
14715 {
14716 best_low = std::min (best_low, current_low);
14717 best_high = std::max (best_high, current_high);
14718 }
14719 break;
14720 default:
14721 /* Ignore. */
14722 break;
14723 }
14724
14725 child = child->sibling;
14726 }
14727 }
14728
14729 *lowpc = best_low;
14730 *highpc = best_high;
14731 }
14732
14733 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14734 in DIE. */
14735
14736 static void
14737 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14738 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14739 {
14740 struct objfile *objfile = cu->per_objfile->objfile;
14741 struct gdbarch *gdbarch = objfile->arch ();
14742 struct attribute *attr;
14743 struct attribute *attr_high;
14744
14745 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14746 if (attr_high)
14747 {
14748 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14749 if (attr != nullptr)
14750 {
14751 CORE_ADDR low = attr->value_as_address ();
14752 CORE_ADDR high = attr_high->value_as_address ();
14753
14754 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14755 high += low;
14756
14757 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14758 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14759 cu->get_builder ()->record_block_range (block, low, high - 1);
14760 }
14761 }
14762
14763 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14764 if (attr != nullptr)
14765 {
14766 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14767 We take advantage of the fact that DW_AT_ranges does not appear
14768 in DW_TAG_compile_unit of DWO files.
14769
14770 Attributes of the form DW_FORM_rnglistx have already had their
14771 value changed by read_rnglist_index and already include
14772 DW_AT_rnglists_base, so don't need to add the ranges base,
14773 either. */
14774 int need_ranges_base = (die->tag != DW_TAG_compile_unit
14775 && attr->form != DW_FORM_rnglistx);
14776
14777 /* The value of the DW_AT_ranges attribute is the offset of the
14778 address range list in the .debug_ranges section. */
14779 unsigned long offset = (DW_UNSND (attr)
14780 + (need_ranges_base ? cu->ranges_base : 0));
14781
14782 std::vector<blockrange> blockvec;
14783 dwarf2_ranges_process (offset, cu, die->tag,
14784 [&] (CORE_ADDR start, CORE_ADDR end)
14785 {
14786 start += baseaddr;
14787 end += baseaddr;
14788 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14789 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14790 cu->get_builder ()->record_block_range (block, start, end - 1);
14791 blockvec.emplace_back (start, end);
14792 });
14793
14794 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14795 }
14796 }
14797
14798 /* Check whether the producer field indicates either of GCC < 4.6, or the
14799 Intel C/C++ compiler, and cache the result in CU. */
14800
14801 static void
14802 check_producer (struct dwarf2_cu *cu)
14803 {
14804 int major, minor;
14805
14806 if (cu->producer == NULL)
14807 {
14808 /* For unknown compilers expect their behavior is DWARF version
14809 compliant.
14810
14811 GCC started to support .debug_types sections by -gdwarf-4 since
14812 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14813 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14814 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14815 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14816 }
14817 else if (producer_is_gcc (cu->producer, &major, &minor))
14818 {
14819 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14820 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14821 }
14822 else if (producer_is_icc (cu->producer, &major, &minor))
14823 {
14824 cu->producer_is_icc = true;
14825 cu->producer_is_icc_lt_14 = major < 14;
14826 }
14827 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14828 cu->producer_is_codewarrior = true;
14829 else
14830 {
14831 /* For other non-GCC compilers, expect their behavior is DWARF version
14832 compliant. */
14833 }
14834
14835 cu->checked_producer = true;
14836 }
14837
14838 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14839 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14840 during 4.6.0 experimental. */
14841
14842 static bool
14843 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14844 {
14845 if (!cu->checked_producer)
14846 check_producer (cu);
14847
14848 return cu->producer_is_gxx_lt_4_6;
14849 }
14850
14851
14852 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14853 with incorrect is_stmt attributes. */
14854
14855 static bool
14856 producer_is_codewarrior (struct dwarf2_cu *cu)
14857 {
14858 if (!cu->checked_producer)
14859 check_producer (cu);
14860
14861 return cu->producer_is_codewarrior;
14862 }
14863
14864 /* Return the default accessibility type if it is not overridden by
14865 DW_AT_accessibility. */
14866
14867 static enum dwarf_access_attribute
14868 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14869 {
14870 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14871 {
14872 /* The default DWARF 2 accessibility for members is public, the default
14873 accessibility for inheritance is private. */
14874
14875 if (die->tag != DW_TAG_inheritance)
14876 return DW_ACCESS_public;
14877 else
14878 return DW_ACCESS_private;
14879 }
14880 else
14881 {
14882 /* DWARF 3+ defines the default accessibility a different way. The same
14883 rules apply now for DW_TAG_inheritance as for the members and it only
14884 depends on the container kind. */
14885
14886 if (die->parent->tag == DW_TAG_class_type)
14887 return DW_ACCESS_private;
14888 else
14889 return DW_ACCESS_public;
14890 }
14891 }
14892
14893 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14894 offset. If the attribute was not found return 0, otherwise return
14895 1. If it was found but could not properly be handled, set *OFFSET
14896 to 0. */
14897
14898 static int
14899 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14900 LONGEST *offset)
14901 {
14902 struct attribute *attr;
14903
14904 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14905 if (attr != NULL)
14906 {
14907 *offset = 0;
14908
14909 /* Note that we do not check for a section offset first here.
14910 This is because DW_AT_data_member_location is new in DWARF 4,
14911 so if we see it, we can assume that a constant form is really
14912 a constant and not a section offset. */
14913 if (attr->form_is_constant ())
14914 *offset = attr->constant_value (0);
14915 else if (attr->form_is_section_offset ())
14916 dwarf2_complex_location_expr_complaint ();
14917 else if (attr->form_is_block ())
14918 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14919 else
14920 dwarf2_complex_location_expr_complaint ();
14921
14922 return 1;
14923 }
14924
14925 return 0;
14926 }
14927
14928 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14929
14930 static void
14931 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14932 struct field *field)
14933 {
14934 struct attribute *attr;
14935
14936 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14937 if (attr != NULL)
14938 {
14939 if (attr->form_is_constant ())
14940 {
14941 LONGEST offset = attr->constant_value (0);
14942 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14943 }
14944 else if (attr->form_is_section_offset ())
14945 dwarf2_complex_location_expr_complaint ();
14946 else if (attr->form_is_block ())
14947 {
14948 bool handled;
14949 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14950 if (handled)
14951 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14952 else
14953 {
14954 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14955 struct objfile *objfile = per_objfile->objfile;
14956 struct dwarf2_locexpr_baton *dlbaton
14957 = XOBNEW (&objfile->objfile_obstack,
14958 struct dwarf2_locexpr_baton);
14959 dlbaton->data = DW_BLOCK (attr)->data;
14960 dlbaton->size = DW_BLOCK (attr)->size;
14961 /* When using this baton, we want to compute the address
14962 of the field, not the value. This is why
14963 is_reference is set to false here. */
14964 dlbaton->is_reference = false;
14965 dlbaton->per_objfile = per_objfile;
14966 dlbaton->per_cu = cu->per_cu;
14967
14968 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14969 }
14970 }
14971 else
14972 dwarf2_complex_location_expr_complaint ();
14973 }
14974 }
14975
14976 /* Add an aggregate field to the field list. */
14977
14978 static void
14979 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14980 struct dwarf2_cu *cu)
14981 {
14982 struct objfile *objfile = cu->per_objfile->objfile;
14983 struct gdbarch *gdbarch = objfile->arch ();
14984 struct nextfield *new_field;
14985 struct attribute *attr;
14986 struct field *fp;
14987 const char *fieldname = "";
14988
14989 if (die->tag == DW_TAG_inheritance)
14990 {
14991 fip->baseclasses.emplace_back ();
14992 new_field = &fip->baseclasses.back ();
14993 }
14994 else
14995 {
14996 fip->fields.emplace_back ();
14997 new_field = &fip->fields.back ();
14998 }
14999
15000 new_field->offset = die->sect_off;
15001
15002 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15003 if (attr != nullptr)
15004 new_field->accessibility = DW_UNSND (attr);
15005 else
15006 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15007 if (new_field->accessibility != DW_ACCESS_public)
15008 fip->non_public_fields = true;
15009
15010 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15011 if (attr != nullptr)
15012 new_field->virtuality = DW_UNSND (attr);
15013 else
15014 new_field->virtuality = DW_VIRTUALITY_none;
15015
15016 fp = &new_field->field;
15017
15018 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15019 {
15020 /* Data member other than a C++ static data member. */
15021
15022 /* Get type of field. */
15023 fp->set_type (die_type (die, cu));
15024
15025 SET_FIELD_BITPOS (*fp, 0);
15026
15027 /* Get bit size of field (zero if none). */
15028 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15029 if (attr != nullptr)
15030 {
15031 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15032 }
15033 else
15034 {
15035 FIELD_BITSIZE (*fp) = 0;
15036 }
15037
15038 /* Get bit offset of field. */
15039 handle_data_member_location (die, cu, fp);
15040 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15041 if (attr != nullptr)
15042 {
15043 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
15044 {
15045 /* For big endian bits, the DW_AT_bit_offset gives the
15046 additional bit offset from the MSB of the containing
15047 anonymous object to the MSB of the field. We don't
15048 have to do anything special since we don't need to
15049 know the size of the anonymous object. */
15050 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15051 }
15052 else
15053 {
15054 /* For little endian bits, compute the bit offset to the
15055 MSB of the anonymous object, subtract off the number of
15056 bits from the MSB of the field to the MSB of the
15057 object, and then subtract off the number of bits of
15058 the field itself. The result is the bit offset of
15059 the LSB of the field. */
15060 int anonymous_size;
15061 int bit_offset = DW_UNSND (attr);
15062
15063 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15064 if (attr != nullptr)
15065 {
15066 /* The size of the anonymous object containing
15067 the bit field is explicit, so use the
15068 indicated size (in bytes). */
15069 anonymous_size = DW_UNSND (attr);
15070 }
15071 else
15072 {
15073 /* The size of the anonymous object containing
15074 the bit field must be inferred from the type
15075 attribute of the data member containing the
15076 bit field. */
15077 anonymous_size = TYPE_LENGTH (fp->type ());
15078 }
15079 SET_FIELD_BITPOS (*fp,
15080 (FIELD_BITPOS (*fp)
15081 + anonymous_size * bits_per_byte
15082 - bit_offset - FIELD_BITSIZE (*fp)));
15083 }
15084 }
15085 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15086 if (attr != NULL)
15087 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15088 + attr->constant_value (0)));
15089
15090 /* Get name of field. */
15091 fieldname = dwarf2_name (die, cu);
15092 if (fieldname == NULL)
15093 fieldname = "";
15094
15095 /* The name is already allocated along with this objfile, so we don't
15096 need to duplicate it for the type. */
15097 fp->name = fieldname;
15098
15099 /* Change accessibility for artificial fields (e.g. virtual table
15100 pointer or virtual base class pointer) to private. */
15101 if (dwarf2_attr (die, DW_AT_artificial, cu))
15102 {
15103 FIELD_ARTIFICIAL (*fp) = 1;
15104 new_field->accessibility = DW_ACCESS_private;
15105 fip->non_public_fields = true;
15106 }
15107 }
15108 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15109 {
15110 /* C++ static member. */
15111
15112 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15113 is a declaration, but all versions of G++ as of this writing
15114 (so through at least 3.2.1) incorrectly generate
15115 DW_TAG_variable tags. */
15116
15117 const char *physname;
15118
15119 /* Get name of field. */
15120 fieldname = dwarf2_name (die, cu);
15121 if (fieldname == NULL)
15122 return;
15123
15124 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15125 if (attr
15126 /* Only create a symbol if this is an external value.
15127 new_symbol checks this and puts the value in the global symbol
15128 table, which we want. If it is not external, new_symbol
15129 will try to put the value in cu->list_in_scope which is wrong. */
15130 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15131 {
15132 /* A static const member, not much different than an enum as far as
15133 we're concerned, except that we can support more types. */
15134 new_symbol (die, NULL, cu);
15135 }
15136
15137 /* Get physical name. */
15138 physname = dwarf2_physname (fieldname, die, cu);
15139
15140 /* The name is already allocated along with this objfile, so we don't
15141 need to duplicate it for the type. */
15142 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15143 fp->set_type (die_type (die, cu));
15144 FIELD_NAME (*fp) = fieldname;
15145 }
15146 else if (die->tag == DW_TAG_inheritance)
15147 {
15148 /* C++ base class field. */
15149 handle_data_member_location (die, cu, fp);
15150 FIELD_BITSIZE (*fp) = 0;
15151 fp->set_type (die_type (die, cu));
15152 FIELD_NAME (*fp) = fp->type ()->name ();
15153 }
15154 else
15155 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15156 }
15157
15158 /* Can the type given by DIE define another type? */
15159
15160 static bool
15161 type_can_define_types (const struct die_info *die)
15162 {
15163 switch (die->tag)
15164 {
15165 case DW_TAG_typedef:
15166 case DW_TAG_class_type:
15167 case DW_TAG_structure_type:
15168 case DW_TAG_union_type:
15169 case DW_TAG_enumeration_type:
15170 return true;
15171
15172 default:
15173 return false;
15174 }
15175 }
15176
15177 /* Add a type definition defined in the scope of the FIP's class. */
15178
15179 static void
15180 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15181 struct dwarf2_cu *cu)
15182 {
15183 struct decl_field fp;
15184 memset (&fp, 0, sizeof (fp));
15185
15186 gdb_assert (type_can_define_types (die));
15187
15188 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15189 fp.name = dwarf2_name (die, cu);
15190 fp.type = read_type_die (die, cu);
15191
15192 /* Save accessibility. */
15193 enum dwarf_access_attribute accessibility;
15194 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15195 if (attr != NULL)
15196 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15197 else
15198 accessibility = dwarf2_default_access_attribute (die, cu);
15199 switch (accessibility)
15200 {
15201 case DW_ACCESS_public:
15202 /* The assumed value if neither private nor protected. */
15203 break;
15204 case DW_ACCESS_private:
15205 fp.is_private = 1;
15206 break;
15207 case DW_ACCESS_protected:
15208 fp.is_protected = 1;
15209 break;
15210 default:
15211 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15212 }
15213
15214 if (die->tag == DW_TAG_typedef)
15215 fip->typedef_field_list.push_back (fp);
15216 else
15217 fip->nested_types_list.push_back (fp);
15218 }
15219
15220 /* A convenience typedef that's used when finding the discriminant
15221 field for a variant part. */
15222 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
15223 offset_map_type;
15224
15225 /* Compute the discriminant range for a given variant. OBSTACK is
15226 where the results will be stored. VARIANT is the variant to
15227 process. IS_UNSIGNED indicates whether the discriminant is signed
15228 or unsigned. */
15229
15230 static const gdb::array_view<discriminant_range>
15231 convert_variant_range (struct obstack *obstack, const variant_field &variant,
15232 bool is_unsigned)
15233 {
15234 std::vector<discriminant_range> ranges;
15235
15236 if (variant.default_branch)
15237 return {};
15238
15239 if (variant.discr_list_data == nullptr)
15240 {
15241 discriminant_range r
15242 = {variant.discriminant_value, variant.discriminant_value};
15243 ranges.push_back (r);
15244 }
15245 else
15246 {
15247 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15248 variant.discr_list_data->size);
15249 while (!data.empty ())
15250 {
15251 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15252 {
15253 complaint (_("invalid discriminant marker: %d"), data[0]);
15254 break;
15255 }
15256 bool is_range = data[0] == DW_DSC_range;
15257 data = data.slice (1);
15258
15259 ULONGEST low, high;
15260 unsigned int bytes_read;
15261
15262 if (data.empty ())
15263 {
15264 complaint (_("DW_AT_discr_list missing low value"));
15265 break;
15266 }
15267 if (is_unsigned)
15268 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15269 else
15270 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15271 &bytes_read);
15272 data = data.slice (bytes_read);
15273
15274 if (is_range)
15275 {
15276 if (data.empty ())
15277 {
15278 complaint (_("DW_AT_discr_list missing high value"));
15279 break;
15280 }
15281 if (is_unsigned)
15282 high = read_unsigned_leb128 (nullptr, data.data (),
15283 &bytes_read);
15284 else
15285 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15286 &bytes_read);
15287 data = data.slice (bytes_read);
15288 }
15289 else
15290 high = low;
15291
15292 ranges.push_back ({ low, high });
15293 }
15294 }
15295
15296 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15297 ranges.size ());
15298 std::copy (ranges.begin (), ranges.end (), result);
15299 return gdb::array_view<discriminant_range> (result, ranges.size ());
15300 }
15301
15302 static const gdb::array_view<variant_part> create_variant_parts
15303 (struct obstack *obstack,
15304 const offset_map_type &offset_map,
15305 struct field_info *fi,
15306 const std::vector<variant_part_builder> &variant_parts);
15307
15308 /* Fill in a "struct variant" for a given variant field. RESULT is
15309 the variant to fill in. OBSTACK is where any needed allocations
15310 will be done. OFFSET_MAP holds the mapping from section offsets to
15311 fields for the type. FI describes the fields of the type we're
15312 processing. FIELD is the variant field we're converting. */
15313
15314 static void
15315 create_one_variant (variant &result, struct obstack *obstack,
15316 const offset_map_type &offset_map,
15317 struct field_info *fi, const variant_field &field)
15318 {
15319 result.discriminants = convert_variant_range (obstack, field, false);
15320 result.first_field = field.first_field + fi->baseclasses.size ();
15321 result.last_field = field.last_field + fi->baseclasses.size ();
15322 result.parts = create_variant_parts (obstack, offset_map, fi,
15323 field.variant_parts);
15324 }
15325
15326 /* Fill in a "struct variant_part" for a given variant part. RESULT
15327 is the variant part to fill in. OBSTACK is where any needed
15328 allocations will be done. OFFSET_MAP holds the mapping from
15329 section offsets to fields for the type. FI describes the fields of
15330 the type we're processing. BUILDER is the variant part to be
15331 converted. */
15332
15333 static void
15334 create_one_variant_part (variant_part &result,
15335 struct obstack *obstack,
15336 const offset_map_type &offset_map,
15337 struct field_info *fi,
15338 const variant_part_builder &builder)
15339 {
15340 auto iter = offset_map.find (builder.discriminant_offset);
15341 if (iter == offset_map.end ())
15342 {
15343 result.discriminant_index = -1;
15344 /* Doesn't matter. */
15345 result.is_unsigned = false;
15346 }
15347 else
15348 {
15349 result.discriminant_index = iter->second;
15350 result.is_unsigned
15351 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
15352 }
15353
15354 size_t n = builder.variants.size ();
15355 variant *output = new (obstack) variant[n];
15356 for (size_t i = 0; i < n; ++i)
15357 create_one_variant (output[i], obstack, offset_map, fi,
15358 builder.variants[i]);
15359
15360 result.variants = gdb::array_view<variant> (output, n);
15361 }
15362
15363 /* Create a vector of variant parts that can be attached to a type.
15364 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15365 holds the mapping from section offsets to fields for the type. FI
15366 describes the fields of the type we're processing. VARIANT_PARTS
15367 is the vector to convert. */
15368
15369 static const gdb::array_view<variant_part>
15370 create_variant_parts (struct obstack *obstack,
15371 const offset_map_type &offset_map,
15372 struct field_info *fi,
15373 const std::vector<variant_part_builder> &variant_parts)
15374 {
15375 if (variant_parts.empty ())
15376 return {};
15377
15378 size_t n = variant_parts.size ();
15379 variant_part *result = new (obstack) variant_part[n];
15380 for (size_t i = 0; i < n; ++i)
15381 create_one_variant_part (result[i], obstack, offset_map, fi,
15382 variant_parts[i]);
15383
15384 return gdb::array_view<variant_part> (result, n);
15385 }
15386
15387 /* Compute the variant part vector for FIP, attaching it to TYPE when
15388 done. */
15389
15390 static void
15391 add_variant_property (struct field_info *fip, struct type *type,
15392 struct dwarf2_cu *cu)
15393 {
15394 /* Map section offsets of fields to their field index. Note the
15395 field index here does not take the number of baseclasses into
15396 account. */
15397 offset_map_type offset_map;
15398 for (int i = 0; i < fip->fields.size (); ++i)
15399 offset_map[fip->fields[i].offset] = i;
15400
15401 struct objfile *objfile = cu->per_objfile->objfile;
15402 gdb::array_view<variant_part> parts
15403 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15404 fip->variant_parts);
15405
15406 struct dynamic_prop prop;
15407 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15408 obstack_copy (&objfile->objfile_obstack, &parts,
15409 sizeof (parts)));
15410
15411 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
15412 }
15413
15414 /* Create the vector of fields, and attach it to the type. */
15415
15416 static void
15417 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15418 struct dwarf2_cu *cu)
15419 {
15420 int nfields = fip->nfields ();
15421
15422 /* Record the field count, allocate space for the array of fields,
15423 and create blank accessibility bitfields if necessary. */
15424 type->set_num_fields (nfields);
15425 type->set_fields
15426 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
15427
15428 if (fip->non_public_fields && cu->language != language_ada)
15429 {
15430 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15431
15432 TYPE_FIELD_PRIVATE_BITS (type) =
15433 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15434 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15435
15436 TYPE_FIELD_PROTECTED_BITS (type) =
15437 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15438 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15439
15440 TYPE_FIELD_IGNORE_BITS (type) =
15441 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15442 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15443 }
15444
15445 /* If the type has baseclasses, allocate and clear a bit vector for
15446 TYPE_FIELD_VIRTUAL_BITS. */
15447 if (!fip->baseclasses.empty () && cu->language != language_ada)
15448 {
15449 int num_bytes = B_BYTES (fip->baseclasses.size ());
15450 unsigned char *pointer;
15451
15452 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15453 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15454 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15455 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15456 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15457 }
15458
15459 if (!fip->variant_parts.empty ())
15460 add_variant_property (fip, type, cu);
15461
15462 /* Copy the saved-up fields into the field vector. */
15463 for (int i = 0; i < nfields; ++i)
15464 {
15465 struct nextfield &field
15466 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15467 : fip->fields[i - fip->baseclasses.size ()]);
15468
15469 type->field (i) = field.field;
15470 switch (field.accessibility)
15471 {
15472 case DW_ACCESS_private:
15473 if (cu->language != language_ada)
15474 SET_TYPE_FIELD_PRIVATE (type, i);
15475 break;
15476
15477 case DW_ACCESS_protected:
15478 if (cu->language != language_ada)
15479 SET_TYPE_FIELD_PROTECTED (type, i);
15480 break;
15481
15482 case DW_ACCESS_public:
15483 break;
15484
15485 default:
15486 /* Unknown accessibility. Complain and treat it as public. */
15487 {
15488 complaint (_("unsupported accessibility %d"),
15489 field.accessibility);
15490 }
15491 break;
15492 }
15493 if (i < fip->baseclasses.size ())
15494 {
15495 switch (field.virtuality)
15496 {
15497 case DW_VIRTUALITY_virtual:
15498 case DW_VIRTUALITY_pure_virtual:
15499 if (cu->language == language_ada)
15500 error (_("unexpected virtuality in component of Ada type"));
15501 SET_TYPE_FIELD_VIRTUAL (type, i);
15502 break;
15503 }
15504 }
15505 }
15506 }
15507
15508 /* Return true if this member function is a constructor, false
15509 otherwise. */
15510
15511 static int
15512 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15513 {
15514 const char *fieldname;
15515 const char *type_name;
15516 int len;
15517
15518 if (die->parent == NULL)
15519 return 0;
15520
15521 if (die->parent->tag != DW_TAG_structure_type
15522 && die->parent->tag != DW_TAG_union_type
15523 && die->parent->tag != DW_TAG_class_type)
15524 return 0;
15525
15526 fieldname = dwarf2_name (die, cu);
15527 type_name = dwarf2_name (die->parent, cu);
15528 if (fieldname == NULL || type_name == NULL)
15529 return 0;
15530
15531 len = strlen (fieldname);
15532 return (strncmp (fieldname, type_name, len) == 0
15533 && (type_name[len] == '\0' || type_name[len] == '<'));
15534 }
15535
15536 /* Check if the given VALUE is a recognized enum
15537 dwarf_defaulted_attribute constant according to DWARF5 spec,
15538 Table 7.24. */
15539
15540 static bool
15541 is_valid_DW_AT_defaulted (ULONGEST value)
15542 {
15543 switch (value)
15544 {
15545 case DW_DEFAULTED_no:
15546 case DW_DEFAULTED_in_class:
15547 case DW_DEFAULTED_out_of_class:
15548 return true;
15549 }
15550
15551 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
15552 return false;
15553 }
15554
15555 /* Add a member function to the proper fieldlist. */
15556
15557 static void
15558 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15559 struct type *type, struct dwarf2_cu *cu)
15560 {
15561 struct objfile *objfile = cu->per_objfile->objfile;
15562 struct attribute *attr;
15563 int i;
15564 struct fnfieldlist *flp = nullptr;
15565 struct fn_field *fnp;
15566 const char *fieldname;
15567 struct type *this_type;
15568 enum dwarf_access_attribute accessibility;
15569
15570 if (cu->language == language_ada)
15571 error (_("unexpected member function in Ada type"));
15572
15573 /* Get name of member function. */
15574 fieldname = dwarf2_name (die, cu);
15575 if (fieldname == NULL)
15576 return;
15577
15578 /* Look up member function name in fieldlist. */
15579 for (i = 0; i < fip->fnfieldlists.size (); i++)
15580 {
15581 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15582 {
15583 flp = &fip->fnfieldlists[i];
15584 break;
15585 }
15586 }
15587
15588 /* Create a new fnfieldlist if necessary. */
15589 if (flp == nullptr)
15590 {
15591 fip->fnfieldlists.emplace_back ();
15592 flp = &fip->fnfieldlists.back ();
15593 flp->name = fieldname;
15594 i = fip->fnfieldlists.size () - 1;
15595 }
15596
15597 /* Create a new member function field and add it to the vector of
15598 fnfieldlists. */
15599 flp->fnfields.emplace_back ();
15600 fnp = &flp->fnfields.back ();
15601
15602 /* Delay processing of the physname until later. */
15603 if (cu->language == language_cplus)
15604 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15605 die, cu);
15606 else
15607 {
15608 const char *physname = dwarf2_physname (fieldname, die, cu);
15609 fnp->physname = physname ? physname : "";
15610 }
15611
15612 fnp->type = alloc_type (objfile);
15613 this_type = read_type_die (die, cu);
15614 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15615 {
15616 int nparams = this_type->num_fields ();
15617
15618 /* TYPE is the domain of this method, and THIS_TYPE is the type
15619 of the method itself (TYPE_CODE_METHOD). */
15620 smash_to_method_type (fnp->type, type,
15621 TYPE_TARGET_TYPE (this_type),
15622 this_type->fields (),
15623 this_type->num_fields (),
15624 this_type->has_varargs ());
15625
15626 /* Handle static member functions.
15627 Dwarf2 has no clean way to discern C++ static and non-static
15628 member functions. G++ helps GDB by marking the first
15629 parameter for non-static member functions (which is the this
15630 pointer) as artificial. We obtain this information from
15631 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15632 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15633 fnp->voffset = VOFFSET_STATIC;
15634 }
15635 else
15636 complaint (_("member function type missing for '%s'"),
15637 dwarf2_full_name (fieldname, die, cu));
15638
15639 /* Get fcontext from DW_AT_containing_type if present. */
15640 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15641 fnp->fcontext = die_containing_type (die, cu);
15642
15643 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15644 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15645
15646 /* Get accessibility. */
15647 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15648 if (attr != nullptr)
15649 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15650 else
15651 accessibility = dwarf2_default_access_attribute (die, cu);
15652 switch (accessibility)
15653 {
15654 case DW_ACCESS_private:
15655 fnp->is_private = 1;
15656 break;
15657 case DW_ACCESS_protected:
15658 fnp->is_protected = 1;
15659 break;
15660 }
15661
15662 /* Check for artificial methods. */
15663 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15664 if (attr && DW_UNSND (attr) != 0)
15665 fnp->is_artificial = 1;
15666
15667 /* Check for defaulted methods. */
15668 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15669 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15670 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15671
15672 /* Check for deleted methods. */
15673 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15674 if (attr != nullptr && DW_UNSND (attr) != 0)
15675 fnp->is_deleted = 1;
15676
15677 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15678
15679 /* Get index in virtual function table if it is a virtual member
15680 function. For older versions of GCC, this is an offset in the
15681 appropriate virtual table, as specified by DW_AT_containing_type.
15682 For everyone else, it is an expression to be evaluated relative
15683 to the object address. */
15684
15685 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15686 if (attr != nullptr)
15687 {
15688 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
15689 {
15690 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15691 {
15692 /* Old-style GCC. */
15693 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15694 }
15695 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15696 || (DW_BLOCK (attr)->size > 1
15697 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15698 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15699 {
15700 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15701 if ((fnp->voffset % cu->header.addr_size) != 0)
15702 dwarf2_complex_location_expr_complaint ();
15703 else
15704 fnp->voffset /= cu->header.addr_size;
15705 fnp->voffset += 2;
15706 }
15707 else
15708 dwarf2_complex_location_expr_complaint ();
15709
15710 if (!fnp->fcontext)
15711 {
15712 /* If there is no `this' field and no DW_AT_containing_type,
15713 we cannot actually find a base class context for the
15714 vtable! */
15715 if (this_type->num_fields () == 0
15716 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15717 {
15718 complaint (_("cannot determine context for virtual member "
15719 "function \"%s\" (offset %s)"),
15720 fieldname, sect_offset_str (die->sect_off));
15721 }
15722 else
15723 {
15724 fnp->fcontext
15725 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15726 }
15727 }
15728 }
15729 else if (attr->form_is_section_offset ())
15730 {
15731 dwarf2_complex_location_expr_complaint ();
15732 }
15733 else
15734 {
15735 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15736 fieldname);
15737 }
15738 }
15739 else
15740 {
15741 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15742 if (attr && DW_UNSND (attr))
15743 {
15744 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15745 complaint (_("Member function \"%s\" (offset %s) is virtual "
15746 "but the vtable offset is not specified"),
15747 fieldname, sect_offset_str (die->sect_off));
15748 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15749 TYPE_CPLUS_DYNAMIC (type) = 1;
15750 }
15751 }
15752 }
15753
15754 /* Create the vector of member function fields, and attach it to the type. */
15755
15756 static void
15757 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15758 struct dwarf2_cu *cu)
15759 {
15760 if (cu->language == language_ada)
15761 error (_("unexpected member functions in Ada type"));
15762
15763 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15764 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15765 TYPE_ALLOC (type,
15766 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15767
15768 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15769 {
15770 struct fnfieldlist &nf = fip->fnfieldlists[i];
15771 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15772
15773 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15774 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15775 fn_flp->fn_fields = (struct fn_field *)
15776 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15777
15778 for (int k = 0; k < nf.fnfields.size (); ++k)
15779 fn_flp->fn_fields[k] = nf.fnfields[k];
15780 }
15781
15782 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15783 }
15784
15785 /* Returns non-zero if NAME is the name of a vtable member in CU's
15786 language, zero otherwise. */
15787 static int
15788 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15789 {
15790 static const char vptr[] = "_vptr";
15791
15792 /* Look for the C++ form of the vtable. */
15793 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15794 return 1;
15795
15796 return 0;
15797 }
15798
15799 /* GCC outputs unnamed structures that are really pointers to member
15800 functions, with the ABI-specified layout. If TYPE describes
15801 such a structure, smash it into a member function type.
15802
15803 GCC shouldn't do this; it should just output pointer to member DIEs.
15804 This is GCC PR debug/28767. */
15805
15806 static void
15807 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15808 {
15809 struct type *pfn_type, *self_type, *new_type;
15810
15811 /* Check for a structure with no name and two children. */
15812 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15813 return;
15814
15815 /* Check for __pfn and __delta members. */
15816 if (TYPE_FIELD_NAME (type, 0) == NULL
15817 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15818 || TYPE_FIELD_NAME (type, 1) == NULL
15819 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15820 return;
15821
15822 /* Find the type of the method. */
15823 pfn_type = type->field (0).type ();
15824 if (pfn_type == NULL
15825 || pfn_type->code () != TYPE_CODE_PTR
15826 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15827 return;
15828
15829 /* Look for the "this" argument. */
15830 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15831 if (pfn_type->num_fields () == 0
15832 /* || pfn_type->field (0).type () == NULL */
15833 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15834 return;
15835
15836 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15837 new_type = alloc_type (objfile);
15838 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15839 pfn_type->fields (), pfn_type->num_fields (),
15840 pfn_type->has_varargs ());
15841 smash_to_methodptr_type (type, new_type);
15842 }
15843
15844 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15845 appropriate error checking and issuing complaints if there is a
15846 problem. */
15847
15848 static ULONGEST
15849 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15850 {
15851 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15852
15853 if (attr == nullptr)
15854 return 0;
15855
15856 if (!attr->form_is_constant ())
15857 {
15858 complaint (_("DW_AT_alignment must have constant form"
15859 " - DIE at %s [in module %s]"),
15860 sect_offset_str (die->sect_off),
15861 objfile_name (cu->per_objfile->objfile));
15862 return 0;
15863 }
15864
15865 ULONGEST align;
15866 if (attr->form == DW_FORM_sdata)
15867 {
15868 LONGEST val = DW_SND (attr);
15869 if (val < 0)
15870 {
15871 complaint (_("DW_AT_alignment value must not be negative"
15872 " - DIE at %s [in module %s]"),
15873 sect_offset_str (die->sect_off),
15874 objfile_name (cu->per_objfile->objfile));
15875 return 0;
15876 }
15877 align = val;
15878 }
15879 else
15880 align = DW_UNSND (attr);
15881
15882 if (align == 0)
15883 {
15884 complaint (_("DW_AT_alignment value must not be zero"
15885 " - DIE at %s [in module %s]"),
15886 sect_offset_str (die->sect_off),
15887 objfile_name (cu->per_objfile->objfile));
15888 return 0;
15889 }
15890 if ((align & (align - 1)) != 0)
15891 {
15892 complaint (_("DW_AT_alignment value must be a power of 2"
15893 " - DIE at %s [in module %s]"),
15894 sect_offset_str (die->sect_off),
15895 objfile_name (cu->per_objfile->objfile));
15896 return 0;
15897 }
15898
15899 return align;
15900 }
15901
15902 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15903 the alignment for TYPE. */
15904
15905 static void
15906 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15907 struct type *type)
15908 {
15909 if (!set_type_align (type, get_alignment (cu, die)))
15910 complaint (_("DW_AT_alignment value too large"
15911 " - DIE at %s [in module %s]"),
15912 sect_offset_str (die->sect_off),
15913 objfile_name (cu->per_objfile->objfile));
15914 }
15915
15916 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15917 constant for a type, according to DWARF5 spec, Table 5.5. */
15918
15919 static bool
15920 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15921 {
15922 switch (value)
15923 {
15924 case DW_CC_normal:
15925 case DW_CC_pass_by_reference:
15926 case DW_CC_pass_by_value:
15927 return true;
15928
15929 default:
15930 complaint (_("unrecognized DW_AT_calling_convention value "
15931 "(%s) for a type"), pulongest (value));
15932 return false;
15933 }
15934 }
15935
15936 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15937 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15938 also according to GNU-specific values (see include/dwarf2.h). */
15939
15940 static bool
15941 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15942 {
15943 switch (value)
15944 {
15945 case DW_CC_normal:
15946 case DW_CC_program:
15947 case DW_CC_nocall:
15948 return true;
15949
15950 case DW_CC_GNU_renesas_sh:
15951 case DW_CC_GNU_borland_fastcall_i386:
15952 case DW_CC_GDB_IBM_OpenCL:
15953 return true;
15954
15955 default:
15956 complaint (_("unrecognized DW_AT_calling_convention value "
15957 "(%s) for a subroutine"), pulongest (value));
15958 return false;
15959 }
15960 }
15961
15962 /* Called when we find the DIE that starts a structure or union scope
15963 (definition) to create a type for the structure or union. Fill in
15964 the type's name and general properties; the members will not be
15965 processed until process_structure_scope. A symbol table entry for
15966 the type will also not be done until process_structure_scope (assuming
15967 the type has a name).
15968
15969 NOTE: we need to call these functions regardless of whether or not the
15970 DIE has a DW_AT_name attribute, since it might be an anonymous
15971 structure or union. This gets the type entered into our set of
15972 user defined types. */
15973
15974 static struct type *
15975 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15976 {
15977 struct objfile *objfile = cu->per_objfile->objfile;
15978 struct type *type;
15979 struct attribute *attr;
15980 const char *name;
15981
15982 /* If the definition of this type lives in .debug_types, read that type.
15983 Don't follow DW_AT_specification though, that will take us back up
15984 the chain and we want to go down. */
15985 attr = die->attr (DW_AT_signature);
15986 if (attr != nullptr)
15987 {
15988 type = get_DW_AT_signature_type (die, attr, cu);
15989
15990 /* The type's CU may not be the same as CU.
15991 Ensure TYPE is recorded with CU in die_type_hash. */
15992 return set_die_type (die, type, cu);
15993 }
15994
15995 type = alloc_type (objfile);
15996 INIT_CPLUS_SPECIFIC (type);
15997
15998 name = dwarf2_name (die, cu);
15999 if (name != NULL)
16000 {
16001 if (cu->language == language_cplus
16002 || cu->language == language_d
16003 || cu->language == language_rust)
16004 {
16005 const char *full_name = dwarf2_full_name (name, die, cu);
16006
16007 /* dwarf2_full_name might have already finished building the DIE's
16008 type. If so, there is no need to continue. */
16009 if (get_die_type (die, cu) != NULL)
16010 return get_die_type (die, cu);
16011
16012 type->set_name (full_name);
16013 }
16014 else
16015 {
16016 /* The name is already allocated along with this objfile, so
16017 we don't need to duplicate it for the type. */
16018 type->set_name (name);
16019 }
16020 }
16021
16022 if (die->tag == DW_TAG_structure_type)
16023 {
16024 type->set_code (TYPE_CODE_STRUCT);
16025 }
16026 else if (die->tag == DW_TAG_union_type)
16027 {
16028 type->set_code (TYPE_CODE_UNION);
16029 }
16030 else
16031 {
16032 type->set_code (TYPE_CODE_STRUCT);
16033 }
16034
16035 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16036 TYPE_DECLARED_CLASS (type) = 1;
16037
16038 /* Store the calling convention in the type if it's available in
16039 the die. Otherwise the calling convention remains set to
16040 the default value DW_CC_normal. */
16041 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16042 if (attr != nullptr
16043 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
16044 {
16045 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16046 TYPE_CPLUS_CALLING_CONVENTION (type)
16047 = (enum dwarf_calling_convention) (DW_UNSND (attr));
16048 }
16049
16050 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16051 if (attr != nullptr)
16052 {
16053 if (attr->form_is_constant ())
16054 TYPE_LENGTH (type) = DW_UNSND (attr);
16055 else
16056 {
16057 struct dynamic_prop prop;
16058 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
16059 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
16060 TYPE_LENGTH (type) = 0;
16061 }
16062 }
16063 else
16064 {
16065 TYPE_LENGTH (type) = 0;
16066 }
16067
16068 maybe_set_alignment (cu, die, type);
16069
16070 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
16071 {
16072 /* ICC<14 does not output the required DW_AT_declaration on
16073 incomplete types, but gives them a size of zero. */
16074 type->set_is_stub (true);
16075 }
16076 else
16077 type->set_stub_is_supported (true);
16078
16079 if (die_is_declaration (die, cu))
16080 type->set_is_stub (true);
16081 else if (attr == NULL && die->child == NULL
16082 && producer_is_realview (cu->producer))
16083 /* RealView does not output the required DW_AT_declaration
16084 on incomplete types. */
16085 type->set_is_stub (true);
16086
16087 /* We need to add the type field to the die immediately so we don't
16088 infinitely recurse when dealing with pointers to the structure
16089 type within the structure itself. */
16090 set_die_type (die, type, cu);
16091
16092 /* set_die_type should be already done. */
16093 set_descriptive_type (type, die, cu);
16094
16095 return type;
16096 }
16097
16098 static void handle_struct_member_die
16099 (struct die_info *child_die,
16100 struct type *type,
16101 struct field_info *fi,
16102 std::vector<struct symbol *> *template_args,
16103 struct dwarf2_cu *cu);
16104
16105 /* A helper for handle_struct_member_die that handles
16106 DW_TAG_variant_part. */
16107
16108 static void
16109 handle_variant_part (struct die_info *die, struct type *type,
16110 struct field_info *fi,
16111 std::vector<struct symbol *> *template_args,
16112 struct dwarf2_cu *cu)
16113 {
16114 variant_part_builder *new_part;
16115 if (fi->current_variant_part == nullptr)
16116 {
16117 fi->variant_parts.emplace_back ();
16118 new_part = &fi->variant_parts.back ();
16119 }
16120 else if (!fi->current_variant_part->processing_variant)
16121 {
16122 complaint (_("nested DW_TAG_variant_part seen "
16123 "- DIE at %s [in module %s]"),
16124 sect_offset_str (die->sect_off),
16125 objfile_name (cu->per_objfile->objfile));
16126 return;
16127 }
16128 else
16129 {
16130 variant_field &current = fi->current_variant_part->variants.back ();
16131 current.variant_parts.emplace_back ();
16132 new_part = &current.variant_parts.back ();
16133 }
16134
16135 /* When we recurse, we want callees to add to this new variant
16136 part. */
16137 scoped_restore save_current_variant_part
16138 = make_scoped_restore (&fi->current_variant_part, new_part);
16139
16140 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16141 if (discr == NULL)
16142 {
16143 /* It's a univariant form, an extension we support. */
16144 }
16145 else if (discr->form_is_ref ())
16146 {
16147 struct dwarf2_cu *target_cu = cu;
16148 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16149
16150 new_part->discriminant_offset = target_die->sect_off;
16151 }
16152 else
16153 {
16154 complaint (_("DW_AT_discr does not have DIE reference form"
16155 " - DIE at %s [in module %s]"),
16156 sect_offset_str (die->sect_off),
16157 objfile_name (cu->per_objfile->objfile));
16158 }
16159
16160 for (die_info *child_die = die->child;
16161 child_die != NULL;
16162 child_die = child_die->sibling)
16163 handle_struct_member_die (child_die, type, fi, template_args, cu);
16164 }
16165
16166 /* A helper for handle_struct_member_die that handles
16167 DW_TAG_variant. */
16168
16169 static void
16170 handle_variant (struct die_info *die, struct type *type,
16171 struct field_info *fi,
16172 std::vector<struct symbol *> *template_args,
16173 struct dwarf2_cu *cu)
16174 {
16175 if (fi->current_variant_part == nullptr)
16176 {
16177 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
16178 "- DIE at %s [in module %s]"),
16179 sect_offset_str (die->sect_off),
16180 objfile_name (cu->per_objfile->objfile));
16181 return;
16182 }
16183 if (fi->current_variant_part->processing_variant)
16184 {
16185 complaint (_("nested DW_TAG_variant seen "
16186 "- DIE at %s [in module %s]"),
16187 sect_offset_str (die->sect_off),
16188 objfile_name (cu->per_objfile->objfile));
16189 return;
16190 }
16191
16192 scoped_restore save_processing_variant
16193 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16194 true);
16195
16196 fi->current_variant_part->variants.emplace_back ();
16197 variant_field &variant = fi->current_variant_part->variants.back ();
16198 variant.first_field = fi->fields.size ();
16199
16200 /* In a variant we want to get the discriminant and also add a
16201 field for our sole member child. */
16202 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
16203 if (discr == nullptr)
16204 {
16205 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
16206 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
16207 variant.default_branch = true;
16208 else
16209 variant.discr_list_data = DW_BLOCK (discr);
16210 }
16211 else
16212 variant.discriminant_value = DW_UNSND (discr);
16213
16214 for (die_info *variant_child = die->child;
16215 variant_child != NULL;
16216 variant_child = variant_child->sibling)
16217 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16218
16219 variant.last_field = fi->fields.size ();
16220 }
16221
16222 /* A helper for process_structure_scope that handles a single member
16223 DIE. */
16224
16225 static void
16226 handle_struct_member_die (struct die_info *child_die, struct type *type,
16227 struct field_info *fi,
16228 std::vector<struct symbol *> *template_args,
16229 struct dwarf2_cu *cu)
16230 {
16231 if (child_die->tag == DW_TAG_member
16232 || child_die->tag == DW_TAG_variable)
16233 {
16234 /* NOTE: carlton/2002-11-05: A C++ static data member
16235 should be a DW_TAG_member that is a declaration, but
16236 all versions of G++ as of this writing (so through at
16237 least 3.2.1) incorrectly generate DW_TAG_variable
16238 tags for them instead. */
16239 dwarf2_add_field (fi, child_die, cu);
16240 }
16241 else if (child_die->tag == DW_TAG_subprogram)
16242 {
16243 /* Rust doesn't have member functions in the C++ sense.
16244 However, it does emit ordinary functions as children
16245 of a struct DIE. */
16246 if (cu->language == language_rust)
16247 read_func_scope (child_die, cu);
16248 else
16249 {
16250 /* C++ member function. */
16251 dwarf2_add_member_fn (fi, child_die, type, cu);
16252 }
16253 }
16254 else if (child_die->tag == DW_TAG_inheritance)
16255 {
16256 /* C++ base class field. */
16257 dwarf2_add_field (fi, child_die, cu);
16258 }
16259 else if (type_can_define_types (child_die))
16260 dwarf2_add_type_defn (fi, child_die, cu);
16261 else if (child_die->tag == DW_TAG_template_type_param
16262 || child_die->tag == DW_TAG_template_value_param)
16263 {
16264 struct symbol *arg = new_symbol (child_die, NULL, cu);
16265
16266 if (arg != NULL)
16267 template_args->push_back (arg);
16268 }
16269 else if (child_die->tag == DW_TAG_variant_part)
16270 handle_variant_part (child_die, type, fi, template_args, cu);
16271 else if (child_die->tag == DW_TAG_variant)
16272 handle_variant (child_die, type, fi, template_args, cu);
16273 }
16274
16275 /* Finish creating a structure or union type, including filling in
16276 its members and creating a symbol for it. */
16277
16278 static void
16279 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16280 {
16281 struct objfile *objfile = cu->per_objfile->objfile;
16282 struct die_info *child_die;
16283 struct type *type;
16284
16285 type = get_die_type (die, cu);
16286 if (type == NULL)
16287 type = read_structure_type (die, cu);
16288
16289 bool has_template_parameters = false;
16290 if (die->child != NULL && ! die_is_declaration (die, cu))
16291 {
16292 struct field_info fi;
16293 std::vector<struct symbol *> template_args;
16294
16295 child_die = die->child;
16296
16297 while (child_die && child_die->tag)
16298 {
16299 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16300 child_die = child_die->sibling;
16301 }
16302
16303 /* Attach template arguments to type. */
16304 if (!template_args.empty ())
16305 {
16306 has_template_parameters = true;
16307 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16308 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16309 TYPE_TEMPLATE_ARGUMENTS (type)
16310 = XOBNEWVEC (&objfile->objfile_obstack,
16311 struct symbol *,
16312 TYPE_N_TEMPLATE_ARGUMENTS (type));
16313 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16314 template_args.data (),
16315 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16316 * sizeof (struct symbol *)));
16317 }
16318
16319 /* Attach fields and member functions to the type. */
16320 if (fi.nfields () > 0)
16321 dwarf2_attach_fields_to_type (&fi, type, cu);
16322 if (!fi.fnfieldlists.empty ())
16323 {
16324 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16325
16326 /* Get the type which refers to the base class (possibly this
16327 class itself) which contains the vtable pointer for the current
16328 class from the DW_AT_containing_type attribute. This use of
16329 DW_AT_containing_type is a GNU extension. */
16330
16331 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16332 {
16333 struct type *t = die_containing_type (die, cu);
16334
16335 set_type_vptr_basetype (type, t);
16336 if (type == t)
16337 {
16338 int i;
16339
16340 /* Our own class provides vtbl ptr. */
16341 for (i = t->num_fields () - 1;
16342 i >= TYPE_N_BASECLASSES (t);
16343 --i)
16344 {
16345 const char *fieldname = TYPE_FIELD_NAME (t, i);
16346
16347 if (is_vtable_name (fieldname, cu))
16348 {
16349 set_type_vptr_fieldno (type, i);
16350 break;
16351 }
16352 }
16353
16354 /* Complain if virtual function table field not found. */
16355 if (i < TYPE_N_BASECLASSES (t))
16356 complaint (_("virtual function table pointer "
16357 "not found when defining class '%s'"),
16358 type->name () ? type->name () : "");
16359 }
16360 else
16361 {
16362 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16363 }
16364 }
16365 else if (cu->producer
16366 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16367 {
16368 /* The IBM XLC compiler does not provide direct indication
16369 of the containing type, but the vtable pointer is
16370 always named __vfp. */
16371
16372 int i;
16373
16374 for (i = type->num_fields () - 1;
16375 i >= TYPE_N_BASECLASSES (type);
16376 --i)
16377 {
16378 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16379 {
16380 set_type_vptr_fieldno (type, i);
16381 set_type_vptr_basetype (type, type);
16382 break;
16383 }
16384 }
16385 }
16386 }
16387
16388 /* Copy fi.typedef_field_list linked list elements content into the
16389 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16390 if (!fi.typedef_field_list.empty ())
16391 {
16392 int count = fi.typedef_field_list.size ();
16393
16394 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16395 TYPE_TYPEDEF_FIELD_ARRAY (type)
16396 = ((struct decl_field *)
16397 TYPE_ALLOC (type,
16398 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16399 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16400
16401 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16402 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16403 }
16404
16405 /* Copy fi.nested_types_list linked list elements content into the
16406 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16407 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16408 {
16409 int count = fi.nested_types_list.size ();
16410
16411 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16412 TYPE_NESTED_TYPES_ARRAY (type)
16413 = ((struct decl_field *)
16414 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16415 TYPE_NESTED_TYPES_COUNT (type) = count;
16416
16417 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16418 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16419 }
16420 }
16421
16422 quirk_gcc_member_function_pointer (type, objfile);
16423 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16424 cu->rust_unions.push_back (type);
16425
16426 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16427 snapshots) has been known to create a die giving a declaration
16428 for a class that has, as a child, a die giving a definition for a
16429 nested class. So we have to process our children even if the
16430 current die is a declaration. Normally, of course, a declaration
16431 won't have any children at all. */
16432
16433 child_die = die->child;
16434
16435 while (child_die != NULL && child_die->tag)
16436 {
16437 if (child_die->tag == DW_TAG_member
16438 || child_die->tag == DW_TAG_variable
16439 || child_die->tag == DW_TAG_inheritance
16440 || child_die->tag == DW_TAG_template_value_param
16441 || child_die->tag == DW_TAG_template_type_param)
16442 {
16443 /* Do nothing. */
16444 }
16445 else
16446 process_die (child_die, cu);
16447
16448 child_die = child_die->sibling;
16449 }
16450
16451 /* Do not consider external references. According to the DWARF standard,
16452 these DIEs are identified by the fact that they have no byte_size
16453 attribute, and a declaration attribute. */
16454 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16455 || !die_is_declaration (die, cu)
16456 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16457 {
16458 struct symbol *sym = new_symbol (die, type, cu);
16459
16460 if (has_template_parameters)
16461 {
16462 struct symtab *symtab;
16463 if (sym != nullptr)
16464 symtab = symbol_symtab (sym);
16465 else if (cu->line_header != nullptr)
16466 {
16467 /* Any related symtab will do. */
16468 symtab
16469 = cu->line_header->file_names ()[0].symtab;
16470 }
16471 else
16472 {
16473 symtab = nullptr;
16474 complaint (_("could not find suitable "
16475 "symtab for template parameter"
16476 " - DIE at %s [in module %s]"),
16477 sect_offset_str (die->sect_off),
16478 objfile_name (objfile));
16479 }
16480
16481 if (symtab != nullptr)
16482 {
16483 /* Make sure that the symtab is set on the new symbols.
16484 Even though they don't appear in this symtab directly,
16485 other parts of gdb assume that symbols do, and this is
16486 reasonably true. */
16487 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16488 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16489 }
16490 }
16491 }
16492 }
16493
16494 /* Assuming DIE is an enumeration type, and TYPE is its associated
16495 type, update TYPE using some information only available in DIE's
16496 children. In particular, the fields are computed. */
16497
16498 static void
16499 update_enumeration_type_from_children (struct die_info *die,
16500 struct type *type,
16501 struct dwarf2_cu *cu)
16502 {
16503 struct die_info *child_die;
16504 int unsigned_enum = 1;
16505 int flag_enum = 1;
16506
16507 auto_obstack obstack;
16508 std::vector<struct field> fields;
16509
16510 for (child_die = die->child;
16511 child_die != NULL && child_die->tag;
16512 child_die = child_die->sibling)
16513 {
16514 struct attribute *attr;
16515 LONGEST value;
16516 const gdb_byte *bytes;
16517 struct dwarf2_locexpr_baton *baton;
16518 const char *name;
16519
16520 if (child_die->tag != DW_TAG_enumerator)
16521 continue;
16522
16523 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16524 if (attr == NULL)
16525 continue;
16526
16527 name = dwarf2_name (child_die, cu);
16528 if (name == NULL)
16529 name = "<anonymous enumerator>";
16530
16531 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16532 &value, &bytes, &baton);
16533 if (value < 0)
16534 {
16535 unsigned_enum = 0;
16536 flag_enum = 0;
16537 }
16538 else
16539 {
16540 if (count_one_bits_ll (value) >= 2)
16541 flag_enum = 0;
16542 }
16543
16544 fields.emplace_back ();
16545 struct field &field = fields.back ();
16546 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16547 SET_FIELD_ENUMVAL (field, value);
16548 }
16549
16550 if (!fields.empty ())
16551 {
16552 type->set_num_fields (fields.size ());
16553 type->set_fields
16554 ((struct field *)
16555 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16556 memcpy (type->fields (), fields.data (),
16557 sizeof (struct field) * fields.size ());
16558 }
16559
16560 if (unsigned_enum)
16561 type->set_is_unsigned (true);
16562
16563 if (flag_enum)
16564 TYPE_FLAG_ENUM (type) = 1;
16565 }
16566
16567 /* Given a DW_AT_enumeration_type die, set its type. We do not
16568 complete the type's fields yet, or create any symbols. */
16569
16570 static struct type *
16571 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16572 {
16573 struct objfile *objfile = cu->per_objfile->objfile;
16574 struct type *type;
16575 struct attribute *attr;
16576 const char *name;
16577
16578 /* If the definition of this type lives in .debug_types, read that type.
16579 Don't follow DW_AT_specification though, that will take us back up
16580 the chain and we want to go down. */
16581 attr = die->attr (DW_AT_signature);
16582 if (attr != nullptr)
16583 {
16584 type = get_DW_AT_signature_type (die, attr, cu);
16585
16586 /* The type's CU may not be the same as CU.
16587 Ensure TYPE is recorded with CU in die_type_hash. */
16588 return set_die_type (die, type, cu);
16589 }
16590
16591 type = alloc_type (objfile);
16592
16593 type->set_code (TYPE_CODE_ENUM);
16594 name = dwarf2_full_name (NULL, die, cu);
16595 if (name != NULL)
16596 type->set_name (name);
16597
16598 attr = dwarf2_attr (die, DW_AT_type, cu);
16599 if (attr != NULL)
16600 {
16601 struct type *underlying_type = die_type (die, cu);
16602
16603 TYPE_TARGET_TYPE (type) = underlying_type;
16604 }
16605
16606 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16607 if (attr != nullptr)
16608 {
16609 TYPE_LENGTH (type) = DW_UNSND (attr);
16610 }
16611 else
16612 {
16613 TYPE_LENGTH (type) = 0;
16614 }
16615
16616 maybe_set_alignment (cu, die, type);
16617
16618 /* The enumeration DIE can be incomplete. In Ada, any type can be
16619 declared as private in the package spec, and then defined only
16620 inside the package body. Such types are known as Taft Amendment
16621 Types. When another package uses such a type, an incomplete DIE
16622 may be generated by the compiler. */
16623 if (die_is_declaration (die, cu))
16624 type->set_is_stub (true);
16625
16626 /* If this type has an underlying type that is not a stub, then we
16627 may use its attributes. We always use the "unsigned" attribute
16628 in this situation, because ordinarily we guess whether the type
16629 is unsigned -- but the guess can be wrong and the underlying type
16630 can tell us the reality. However, we defer to a local size
16631 attribute if one exists, because this lets the compiler override
16632 the underlying type if needed. */
16633 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16634 {
16635 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16636 underlying_type = check_typedef (underlying_type);
16637
16638 type->set_is_unsigned (underlying_type->is_unsigned ());
16639
16640 if (TYPE_LENGTH (type) == 0)
16641 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16642
16643 if (TYPE_RAW_ALIGN (type) == 0
16644 && TYPE_RAW_ALIGN (underlying_type) != 0)
16645 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16646 }
16647
16648 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16649
16650 set_die_type (die, type, cu);
16651
16652 /* Finish the creation of this type by using the enum's children.
16653 Note that, as usual, this must come after set_die_type to avoid
16654 infinite recursion when trying to compute the names of the
16655 enumerators. */
16656 update_enumeration_type_from_children (die, type, cu);
16657
16658 return type;
16659 }
16660
16661 /* Given a pointer to a die which begins an enumeration, process all
16662 the dies that define the members of the enumeration, and create the
16663 symbol for the enumeration type.
16664
16665 NOTE: We reverse the order of the element list. */
16666
16667 static void
16668 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16669 {
16670 struct type *this_type;
16671
16672 this_type = get_die_type (die, cu);
16673 if (this_type == NULL)
16674 this_type = read_enumeration_type (die, cu);
16675
16676 if (die->child != NULL)
16677 {
16678 struct die_info *child_die;
16679 const char *name;
16680
16681 child_die = die->child;
16682 while (child_die && child_die->tag)
16683 {
16684 if (child_die->tag != DW_TAG_enumerator)
16685 {
16686 process_die (child_die, cu);
16687 }
16688 else
16689 {
16690 name = dwarf2_name (child_die, cu);
16691 if (name)
16692 new_symbol (child_die, this_type, cu);
16693 }
16694
16695 child_die = child_die->sibling;
16696 }
16697 }
16698
16699 /* If we are reading an enum from a .debug_types unit, and the enum
16700 is a declaration, and the enum is not the signatured type in the
16701 unit, then we do not want to add a symbol for it. Adding a
16702 symbol would in some cases obscure the true definition of the
16703 enum, giving users an incomplete type when the definition is
16704 actually available. Note that we do not want to do this for all
16705 enums which are just declarations, because C++0x allows forward
16706 enum declarations. */
16707 if (cu->per_cu->is_debug_types
16708 && die_is_declaration (die, cu))
16709 {
16710 struct signatured_type *sig_type;
16711
16712 sig_type = (struct signatured_type *) cu->per_cu;
16713 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16714 if (sig_type->type_offset_in_section != die->sect_off)
16715 return;
16716 }
16717
16718 new_symbol (die, this_type, cu);
16719 }
16720
16721 /* Extract all information from a DW_TAG_array_type DIE and put it in
16722 the DIE's type field. For now, this only handles one dimensional
16723 arrays. */
16724
16725 static struct type *
16726 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16727 {
16728 struct objfile *objfile = cu->per_objfile->objfile;
16729 struct die_info *child_die;
16730 struct type *type;
16731 struct type *element_type, *range_type, *index_type;
16732 struct attribute *attr;
16733 const char *name;
16734 struct dynamic_prop *byte_stride_prop = NULL;
16735 unsigned int bit_stride = 0;
16736
16737 element_type = die_type (die, cu);
16738
16739 /* The die_type call above may have already set the type for this DIE. */
16740 type = get_die_type (die, cu);
16741 if (type)
16742 return type;
16743
16744 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16745 if (attr != NULL)
16746 {
16747 int stride_ok;
16748 struct type *prop_type = cu->addr_sized_int_type (false);
16749
16750 byte_stride_prop
16751 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16752 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16753 prop_type);
16754 if (!stride_ok)
16755 {
16756 complaint (_("unable to read array DW_AT_byte_stride "
16757 " - DIE at %s [in module %s]"),
16758 sect_offset_str (die->sect_off),
16759 objfile_name (cu->per_objfile->objfile));
16760 /* Ignore this attribute. We will likely not be able to print
16761 arrays of this type correctly, but there is little we can do
16762 to help if we cannot read the attribute's value. */
16763 byte_stride_prop = NULL;
16764 }
16765 }
16766
16767 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16768 if (attr != NULL)
16769 bit_stride = DW_UNSND (attr);
16770
16771 /* Irix 6.2 native cc creates array types without children for
16772 arrays with unspecified length. */
16773 if (die->child == NULL)
16774 {
16775 index_type = objfile_type (objfile)->builtin_int;
16776 range_type = create_static_range_type (NULL, index_type, 0, -1);
16777 type = create_array_type_with_stride (NULL, element_type, range_type,
16778 byte_stride_prop, bit_stride);
16779 return set_die_type (die, type, cu);
16780 }
16781
16782 std::vector<struct type *> range_types;
16783 child_die = die->child;
16784 while (child_die && child_die->tag)
16785 {
16786 if (child_die->tag == DW_TAG_subrange_type)
16787 {
16788 struct type *child_type = read_type_die (child_die, cu);
16789
16790 if (child_type != NULL)
16791 {
16792 /* The range type was succesfully read. Save it for the
16793 array type creation. */
16794 range_types.push_back (child_type);
16795 }
16796 }
16797 child_die = child_die->sibling;
16798 }
16799
16800 /* Dwarf2 dimensions are output from left to right, create the
16801 necessary array types in backwards order. */
16802
16803 type = element_type;
16804
16805 if (read_array_order (die, cu) == DW_ORD_col_major)
16806 {
16807 int i = 0;
16808
16809 while (i < range_types.size ())
16810 type = create_array_type_with_stride (NULL, type, range_types[i++],
16811 byte_stride_prop, bit_stride);
16812 }
16813 else
16814 {
16815 size_t ndim = range_types.size ();
16816 while (ndim-- > 0)
16817 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16818 byte_stride_prop, bit_stride);
16819 }
16820
16821 /* Understand Dwarf2 support for vector types (like they occur on
16822 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16823 array type. This is not part of the Dwarf2/3 standard yet, but a
16824 custom vendor extension. The main difference between a regular
16825 array and the vector variant is that vectors are passed by value
16826 to functions. */
16827 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16828 if (attr != nullptr)
16829 make_vector_type (type);
16830
16831 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16832 implementation may choose to implement triple vectors using this
16833 attribute. */
16834 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16835 if (attr != nullptr)
16836 {
16837 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16838 TYPE_LENGTH (type) = DW_UNSND (attr);
16839 else
16840 complaint (_("DW_AT_byte_size for array type smaller "
16841 "than the total size of elements"));
16842 }
16843
16844 name = dwarf2_name (die, cu);
16845 if (name)
16846 type->set_name (name);
16847
16848 maybe_set_alignment (cu, die, type);
16849
16850 /* Install the type in the die. */
16851 set_die_type (die, type, cu);
16852
16853 /* set_die_type should be already done. */
16854 set_descriptive_type (type, die, cu);
16855
16856 return type;
16857 }
16858
16859 static enum dwarf_array_dim_ordering
16860 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16861 {
16862 struct attribute *attr;
16863
16864 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16865
16866 if (attr != nullptr)
16867 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16868
16869 /* GNU F77 is a special case, as at 08/2004 array type info is the
16870 opposite order to the dwarf2 specification, but data is still
16871 laid out as per normal fortran.
16872
16873 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16874 version checking. */
16875
16876 if (cu->language == language_fortran
16877 && cu->producer && strstr (cu->producer, "GNU F77"))
16878 {
16879 return DW_ORD_row_major;
16880 }
16881
16882 switch (cu->language_defn->array_ordering ())
16883 {
16884 case array_column_major:
16885 return DW_ORD_col_major;
16886 case array_row_major:
16887 default:
16888 return DW_ORD_row_major;
16889 };
16890 }
16891
16892 /* Extract all information from a DW_TAG_set_type DIE and put it in
16893 the DIE's type field. */
16894
16895 static struct type *
16896 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16897 {
16898 struct type *domain_type, *set_type;
16899 struct attribute *attr;
16900
16901 domain_type = die_type (die, cu);
16902
16903 /* The die_type call above may have already set the type for this DIE. */
16904 set_type = get_die_type (die, cu);
16905 if (set_type)
16906 return set_type;
16907
16908 set_type = create_set_type (NULL, domain_type);
16909
16910 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16911 if (attr != nullptr)
16912 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16913
16914 maybe_set_alignment (cu, die, set_type);
16915
16916 return set_die_type (die, set_type, cu);
16917 }
16918
16919 /* A helper for read_common_block that creates a locexpr baton.
16920 SYM is the symbol which we are marking as computed.
16921 COMMON_DIE is the DIE for the common block.
16922 COMMON_LOC is the location expression attribute for the common
16923 block itself.
16924 MEMBER_LOC is the location expression attribute for the particular
16925 member of the common block that we are processing.
16926 CU is the CU from which the above come. */
16927
16928 static void
16929 mark_common_block_symbol_computed (struct symbol *sym,
16930 struct die_info *common_die,
16931 struct attribute *common_loc,
16932 struct attribute *member_loc,
16933 struct dwarf2_cu *cu)
16934 {
16935 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16936 struct objfile *objfile = per_objfile->objfile;
16937 struct dwarf2_locexpr_baton *baton;
16938 gdb_byte *ptr;
16939 unsigned int cu_off;
16940 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16941 LONGEST offset = 0;
16942
16943 gdb_assert (common_loc && member_loc);
16944 gdb_assert (common_loc->form_is_block ());
16945 gdb_assert (member_loc->form_is_block ()
16946 || member_loc->form_is_constant ());
16947
16948 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16949 baton->per_objfile = per_objfile;
16950 baton->per_cu = cu->per_cu;
16951 gdb_assert (baton->per_cu);
16952
16953 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16954
16955 if (member_loc->form_is_constant ())
16956 {
16957 offset = member_loc->constant_value (0);
16958 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16959 }
16960 else
16961 baton->size += DW_BLOCK (member_loc)->size;
16962
16963 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16964 baton->data = ptr;
16965
16966 *ptr++ = DW_OP_call4;
16967 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16968 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16969 ptr += 4;
16970
16971 if (member_loc->form_is_constant ())
16972 {
16973 *ptr++ = DW_OP_addr;
16974 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16975 ptr += cu->header.addr_size;
16976 }
16977 else
16978 {
16979 /* We have to copy the data here, because DW_OP_call4 will only
16980 use a DW_AT_location attribute. */
16981 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16982 ptr += DW_BLOCK (member_loc)->size;
16983 }
16984
16985 *ptr++ = DW_OP_plus;
16986 gdb_assert (ptr - baton->data == baton->size);
16987
16988 SYMBOL_LOCATION_BATON (sym) = baton;
16989 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16990 }
16991
16992 /* Create appropriate locally-scoped variables for all the
16993 DW_TAG_common_block entries. Also create a struct common_block
16994 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16995 is used to separate the common blocks name namespace from regular
16996 variable names. */
16997
16998 static void
16999 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
17000 {
17001 struct attribute *attr;
17002
17003 attr = dwarf2_attr (die, DW_AT_location, cu);
17004 if (attr != nullptr)
17005 {
17006 /* Support the .debug_loc offsets. */
17007 if (attr->form_is_block ())
17008 {
17009 /* Ok. */
17010 }
17011 else if (attr->form_is_section_offset ())
17012 {
17013 dwarf2_complex_location_expr_complaint ();
17014 attr = NULL;
17015 }
17016 else
17017 {
17018 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17019 "common block member");
17020 attr = NULL;
17021 }
17022 }
17023
17024 if (die->child != NULL)
17025 {
17026 struct objfile *objfile = cu->per_objfile->objfile;
17027 struct die_info *child_die;
17028 size_t n_entries = 0, size;
17029 struct common_block *common_block;
17030 struct symbol *sym;
17031
17032 for (child_die = die->child;
17033 child_die && child_die->tag;
17034 child_die = child_die->sibling)
17035 ++n_entries;
17036
17037 size = (sizeof (struct common_block)
17038 + (n_entries - 1) * sizeof (struct symbol *));
17039 common_block
17040 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17041 size);
17042 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17043 common_block->n_entries = 0;
17044
17045 for (child_die = die->child;
17046 child_die && child_die->tag;
17047 child_die = child_die->sibling)
17048 {
17049 /* Create the symbol in the DW_TAG_common_block block in the current
17050 symbol scope. */
17051 sym = new_symbol (child_die, NULL, cu);
17052 if (sym != NULL)
17053 {
17054 struct attribute *member_loc;
17055
17056 common_block->contents[common_block->n_entries++] = sym;
17057
17058 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17059 cu);
17060 if (member_loc)
17061 {
17062 /* GDB has handled this for a long time, but it is
17063 not specified by DWARF. It seems to have been
17064 emitted by gfortran at least as recently as:
17065 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17066 complaint (_("Variable in common block has "
17067 "DW_AT_data_member_location "
17068 "- DIE at %s [in module %s]"),
17069 sect_offset_str (child_die->sect_off),
17070 objfile_name (objfile));
17071
17072 if (member_loc->form_is_section_offset ())
17073 dwarf2_complex_location_expr_complaint ();
17074 else if (member_loc->form_is_constant ()
17075 || member_loc->form_is_block ())
17076 {
17077 if (attr != nullptr)
17078 mark_common_block_symbol_computed (sym, die, attr,
17079 member_loc, cu);
17080 }
17081 else
17082 dwarf2_complex_location_expr_complaint ();
17083 }
17084 }
17085 }
17086
17087 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17088 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
17089 }
17090 }
17091
17092 /* Create a type for a C++ namespace. */
17093
17094 static struct type *
17095 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
17096 {
17097 struct objfile *objfile = cu->per_objfile->objfile;
17098 const char *previous_prefix, *name;
17099 int is_anonymous;
17100 struct type *type;
17101
17102 /* For extensions, reuse the type of the original namespace. */
17103 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17104 {
17105 struct die_info *ext_die;
17106 struct dwarf2_cu *ext_cu = cu;
17107
17108 ext_die = dwarf2_extension (die, &ext_cu);
17109 type = read_type_die (ext_die, ext_cu);
17110
17111 /* EXT_CU may not be the same as CU.
17112 Ensure TYPE is recorded with CU in die_type_hash. */
17113 return set_die_type (die, type, cu);
17114 }
17115
17116 name = namespace_name (die, &is_anonymous, cu);
17117
17118 /* Now build the name of the current namespace. */
17119
17120 previous_prefix = determine_prefix (die, cu);
17121 if (previous_prefix[0] != '\0')
17122 name = typename_concat (&objfile->objfile_obstack,
17123 previous_prefix, name, 0, cu);
17124
17125 /* Create the type. */
17126 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17127
17128 return set_die_type (die, type, cu);
17129 }
17130
17131 /* Read a namespace scope. */
17132
17133 static void
17134 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17135 {
17136 struct objfile *objfile = cu->per_objfile->objfile;
17137 int is_anonymous;
17138
17139 /* Add a symbol associated to this if we haven't seen the namespace
17140 before. Also, add a using directive if it's an anonymous
17141 namespace. */
17142
17143 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17144 {
17145 struct type *type;
17146
17147 type = read_type_die (die, cu);
17148 new_symbol (die, type, cu);
17149
17150 namespace_name (die, &is_anonymous, cu);
17151 if (is_anonymous)
17152 {
17153 const char *previous_prefix = determine_prefix (die, cu);
17154
17155 std::vector<const char *> excludes;
17156 add_using_directive (using_directives (cu),
17157 previous_prefix, type->name (), NULL,
17158 NULL, excludes, 0, &objfile->objfile_obstack);
17159 }
17160 }
17161
17162 if (die->child != NULL)
17163 {
17164 struct die_info *child_die = die->child;
17165
17166 while (child_die && child_die->tag)
17167 {
17168 process_die (child_die, cu);
17169 child_die = child_die->sibling;
17170 }
17171 }
17172 }
17173
17174 /* Read a Fortran module as type. This DIE can be only a declaration used for
17175 imported module. Still we need that type as local Fortran "use ... only"
17176 declaration imports depend on the created type in determine_prefix. */
17177
17178 static struct type *
17179 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17180 {
17181 struct objfile *objfile = cu->per_objfile->objfile;
17182 const char *module_name;
17183 struct type *type;
17184
17185 module_name = dwarf2_name (die, cu);
17186 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17187
17188 return set_die_type (die, type, cu);
17189 }
17190
17191 /* Read a Fortran module. */
17192
17193 static void
17194 read_module (struct die_info *die, struct dwarf2_cu *cu)
17195 {
17196 struct die_info *child_die = die->child;
17197 struct type *type;
17198
17199 type = read_type_die (die, cu);
17200 new_symbol (die, type, cu);
17201
17202 while (child_die && child_die->tag)
17203 {
17204 process_die (child_die, cu);
17205 child_die = child_die->sibling;
17206 }
17207 }
17208
17209 /* Return the name of the namespace represented by DIE. Set
17210 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17211 namespace. */
17212
17213 static const char *
17214 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17215 {
17216 struct die_info *current_die;
17217 const char *name = NULL;
17218
17219 /* Loop through the extensions until we find a name. */
17220
17221 for (current_die = die;
17222 current_die != NULL;
17223 current_die = dwarf2_extension (die, &cu))
17224 {
17225 /* We don't use dwarf2_name here so that we can detect the absence
17226 of a name -> anonymous namespace. */
17227 name = dwarf2_string_attr (die, DW_AT_name, cu);
17228
17229 if (name != NULL)
17230 break;
17231 }
17232
17233 /* Is it an anonymous namespace? */
17234
17235 *is_anonymous = (name == NULL);
17236 if (*is_anonymous)
17237 name = CP_ANONYMOUS_NAMESPACE_STR;
17238
17239 return name;
17240 }
17241
17242 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17243 the user defined type vector. */
17244
17245 static struct type *
17246 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17247 {
17248 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
17249 struct comp_unit_head *cu_header = &cu->header;
17250 struct type *type;
17251 struct attribute *attr_byte_size;
17252 struct attribute *attr_address_class;
17253 int byte_size, addr_class;
17254 struct type *target_type;
17255
17256 target_type = die_type (die, cu);
17257
17258 /* The die_type call above may have already set the type for this DIE. */
17259 type = get_die_type (die, cu);
17260 if (type)
17261 return type;
17262
17263 type = lookup_pointer_type (target_type);
17264
17265 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17266 if (attr_byte_size)
17267 byte_size = DW_UNSND (attr_byte_size);
17268 else
17269 byte_size = cu_header->addr_size;
17270
17271 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17272 if (attr_address_class)
17273 addr_class = DW_UNSND (attr_address_class);
17274 else
17275 addr_class = DW_ADDR_none;
17276
17277 ULONGEST alignment = get_alignment (cu, die);
17278
17279 /* If the pointer size, alignment, or address class is different
17280 than the default, create a type variant marked as such and set
17281 the length accordingly. */
17282 if (TYPE_LENGTH (type) != byte_size
17283 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17284 && alignment != TYPE_RAW_ALIGN (type))
17285 || addr_class != DW_ADDR_none)
17286 {
17287 if (gdbarch_address_class_type_flags_p (gdbarch))
17288 {
17289 type_instance_flags type_flags
17290 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17291 addr_class);
17292 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17293 == 0);
17294 type = make_type_with_address_space (type, type_flags);
17295 }
17296 else if (TYPE_LENGTH (type) != byte_size)
17297 {
17298 complaint (_("invalid pointer size %d"), byte_size);
17299 }
17300 else if (TYPE_RAW_ALIGN (type) != alignment)
17301 {
17302 complaint (_("Invalid DW_AT_alignment"
17303 " - DIE at %s [in module %s]"),
17304 sect_offset_str (die->sect_off),
17305 objfile_name (cu->per_objfile->objfile));
17306 }
17307 else
17308 {
17309 /* Should we also complain about unhandled address classes? */
17310 }
17311 }
17312
17313 TYPE_LENGTH (type) = byte_size;
17314 set_type_align (type, alignment);
17315 return set_die_type (die, type, cu);
17316 }
17317
17318 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17319 the user defined type vector. */
17320
17321 static struct type *
17322 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17323 {
17324 struct type *type;
17325 struct type *to_type;
17326 struct type *domain;
17327
17328 to_type = die_type (die, cu);
17329 domain = die_containing_type (die, cu);
17330
17331 /* The calls above may have already set the type for this DIE. */
17332 type = get_die_type (die, cu);
17333 if (type)
17334 return type;
17335
17336 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17337 type = lookup_methodptr_type (to_type);
17338 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17339 {
17340 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17341
17342 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17343 to_type->fields (), to_type->num_fields (),
17344 to_type->has_varargs ());
17345 type = lookup_methodptr_type (new_type);
17346 }
17347 else
17348 type = lookup_memberptr_type (to_type, domain);
17349
17350 return set_die_type (die, type, cu);
17351 }
17352
17353 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17354 the user defined type vector. */
17355
17356 static struct type *
17357 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17358 enum type_code refcode)
17359 {
17360 struct comp_unit_head *cu_header = &cu->header;
17361 struct type *type, *target_type;
17362 struct attribute *attr;
17363
17364 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17365
17366 target_type = die_type (die, cu);
17367
17368 /* The die_type call above may have already set the type for this DIE. */
17369 type = get_die_type (die, cu);
17370 if (type)
17371 return type;
17372
17373 type = lookup_reference_type (target_type, refcode);
17374 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17375 if (attr != nullptr)
17376 {
17377 TYPE_LENGTH (type) = DW_UNSND (attr);
17378 }
17379 else
17380 {
17381 TYPE_LENGTH (type) = cu_header->addr_size;
17382 }
17383 maybe_set_alignment (cu, die, type);
17384 return set_die_type (die, type, cu);
17385 }
17386
17387 /* Add the given cv-qualifiers to the element type of the array. GCC
17388 outputs DWARF type qualifiers that apply to an array, not the
17389 element type. But GDB relies on the array element type to carry
17390 the cv-qualifiers. This mimics section 6.7.3 of the C99
17391 specification. */
17392
17393 static struct type *
17394 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17395 struct type *base_type, int cnst, int voltl)
17396 {
17397 struct type *el_type, *inner_array;
17398
17399 base_type = copy_type (base_type);
17400 inner_array = base_type;
17401
17402 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17403 {
17404 TYPE_TARGET_TYPE (inner_array) =
17405 copy_type (TYPE_TARGET_TYPE (inner_array));
17406 inner_array = TYPE_TARGET_TYPE (inner_array);
17407 }
17408
17409 el_type = TYPE_TARGET_TYPE (inner_array);
17410 cnst |= TYPE_CONST (el_type);
17411 voltl |= TYPE_VOLATILE (el_type);
17412 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17413
17414 return set_die_type (die, base_type, cu);
17415 }
17416
17417 static struct type *
17418 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17419 {
17420 struct type *base_type, *cv_type;
17421
17422 base_type = die_type (die, cu);
17423
17424 /* The die_type call above may have already set the type for this DIE. */
17425 cv_type = get_die_type (die, cu);
17426 if (cv_type)
17427 return cv_type;
17428
17429 /* In case the const qualifier is applied to an array type, the element type
17430 is so qualified, not the array type (section 6.7.3 of C99). */
17431 if (base_type->code () == TYPE_CODE_ARRAY)
17432 return add_array_cv_type (die, cu, base_type, 1, 0);
17433
17434 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17435 return set_die_type (die, cv_type, cu);
17436 }
17437
17438 static struct type *
17439 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17440 {
17441 struct type *base_type, *cv_type;
17442
17443 base_type = die_type (die, cu);
17444
17445 /* The die_type call above may have already set the type for this DIE. */
17446 cv_type = get_die_type (die, cu);
17447 if (cv_type)
17448 return cv_type;
17449
17450 /* In case the volatile qualifier is applied to an array type, the
17451 element type is so qualified, not the array type (section 6.7.3
17452 of C99). */
17453 if (base_type->code () == TYPE_CODE_ARRAY)
17454 return add_array_cv_type (die, cu, base_type, 0, 1);
17455
17456 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17457 return set_die_type (die, cv_type, cu);
17458 }
17459
17460 /* Handle DW_TAG_restrict_type. */
17461
17462 static struct type *
17463 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17464 {
17465 struct type *base_type, *cv_type;
17466
17467 base_type = die_type (die, cu);
17468
17469 /* The die_type call above may have already set the type for this DIE. */
17470 cv_type = get_die_type (die, cu);
17471 if (cv_type)
17472 return cv_type;
17473
17474 cv_type = make_restrict_type (base_type);
17475 return set_die_type (die, cv_type, cu);
17476 }
17477
17478 /* Handle DW_TAG_atomic_type. */
17479
17480 static struct type *
17481 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17482 {
17483 struct type *base_type, *cv_type;
17484
17485 base_type = die_type (die, cu);
17486
17487 /* The die_type call above may have already set the type for this DIE. */
17488 cv_type = get_die_type (die, cu);
17489 if (cv_type)
17490 return cv_type;
17491
17492 cv_type = make_atomic_type (base_type);
17493 return set_die_type (die, cv_type, cu);
17494 }
17495
17496 /* Extract all information from a DW_TAG_string_type DIE and add to
17497 the user defined type vector. It isn't really a user defined type,
17498 but it behaves like one, with other DIE's using an AT_user_def_type
17499 attribute to reference it. */
17500
17501 static struct type *
17502 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17503 {
17504 struct objfile *objfile = cu->per_objfile->objfile;
17505 struct gdbarch *gdbarch = objfile->arch ();
17506 struct type *type, *range_type, *index_type, *char_type;
17507 struct attribute *attr;
17508 struct dynamic_prop prop;
17509 bool length_is_constant = true;
17510 LONGEST length;
17511
17512 /* There are a couple of places where bit sizes might be made use of
17513 when parsing a DW_TAG_string_type, however, no producer that we know
17514 of make use of these. Handling bit sizes that are a multiple of the
17515 byte size is easy enough, but what about other bit sizes? Lets deal
17516 with that problem when we have to. Warn about these attributes being
17517 unsupported, then parse the type and ignore them like we always
17518 have. */
17519 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17520 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17521 {
17522 static bool warning_printed = false;
17523 if (!warning_printed)
17524 {
17525 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17526 "currently supported on DW_TAG_string_type."));
17527 warning_printed = true;
17528 }
17529 }
17530
17531 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17532 if (attr != nullptr && !attr->form_is_constant ())
17533 {
17534 /* The string length describes the location at which the length of
17535 the string can be found. The size of the length field can be
17536 specified with one of the attributes below. */
17537 struct type *prop_type;
17538 struct attribute *len
17539 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17540 if (len == nullptr)
17541 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17542 if (len != nullptr && len->form_is_constant ())
17543 {
17544 /* Pass 0 as the default as we know this attribute is constant
17545 and the default value will not be returned. */
17546 LONGEST sz = len->constant_value (0);
17547 prop_type = cu->per_objfile->int_type (sz, true);
17548 }
17549 else
17550 {
17551 /* If the size is not specified then we assume it is the size of
17552 an address on this target. */
17553 prop_type = cu->addr_sized_int_type (true);
17554 }
17555
17556 /* Convert the attribute into a dynamic property. */
17557 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17558 length = 1;
17559 else
17560 length_is_constant = false;
17561 }
17562 else if (attr != nullptr)
17563 {
17564 /* This DW_AT_string_length just contains the length with no
17565 indirection. There's no need to create a dynamic property in this
17566 case. Pass 0 for the default value as we know it will not be
17567 returned in this case. */
17568 length = attr->constant_value (0);
17569 }
17570 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17571 {
17572 /* We don't currently support non-constant byte sizes for strings. */
17573 length = attr->constant_value (1);
17574 }
17575 else
17576 {
17577 /* Use 1 as a fallback length if we have nothing else. */
17578 length = 1;
17579 }
17580
17581 index_type = objfile_type (objfile)->builtin_int;
17582 if (length_is_constant)
17583 range_type = create_static_range_type (NULL, index_type, 1, length);
17584 else
17585 {
17586 struct dynamic_prop low_bound;
17587
17588 low_bound.set_const_val (1);
17589 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17590 }
17591 char_type = language_string_char_type (cu->language_defn, gdbarch);
17592 type = create_string_type (NULL, char_type, range_type);
17593
17594 return set_die_type (die, type, cu);
17595 }
17596
17597 /* Assuming that DIE corresponds to a function, returns nonzero
17598 if the function is prototyped. */
17599
17600 static int
17601 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17602 {
17603 struct attribute *attr;
17604
17605 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17606 if (attr && (DW_UNSND (attr) != 0))
17607 return 1;
17608
17609 /* The DWARF standard implies that the DW_AT_prototyped attribute
17610 is only meaningful for C, but the concept also extends to other
17611 languages that allow unprototyped functions (Eg: Objective C).
17612 For all other languages, assume that functions are always
17613 prototyped. */
17614 if (cu->language != language_c
17615 && cu->language != language_objc
17616 && cu->language != language_opencl)
17617 return 1;
17618
17619 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17620 prototyped and unprototyped functions; default to prototyped,
17621 since that is more common in modern code (and RealView warns
17622 about unprototyped functions). */
17623 if (producer_is_realview (cu->producer))
17624 return 1;
17625
17626 return 0;
17627 }
17628
17629 /* Handle DIES due to C code like:
17630
17631 struct foo
17632 {
17633 int (*funcp)(int a, long l);
17634 int b;
17635 };
17636
17637 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17638
17639 static struct type *
17640 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17641 {
17642 struct objfile *objfile = cu->per_objfile->objfile;
17643 struct type *type; /* Type that this function returns. */
17644 struct type *ftype; /* Function that returns above type. */
17645 struct attribute *attr;
17646
17647 type = die_type (die, cu);
17648
17649 /* The die_type call above may have already set the type for this DIE. */
17650 ftype = get_die_type (die, cu);
17651 if (ftype)
17652 return ftype;
17653
17654 ftype = lookup_function_type (type);
17655
17656 if (prototyped_function_p (die, cu))
17657 ftype->set_is_prototyped (true);
17658
17659 /* Store the calling convention in the type if it's available in
17660 the subroutine die. Otherwise set the calling convention to
17661 the default value DW_CC_normal. */
17662 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17663 if (attr != nullptr
17664 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17665 TYPE_CALLING_CONVENTION (ftype)
17666 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17667 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17668 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17669 else
17670 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17671
17672 /* Record whether the function returns normally to its caller or not
17673 if the DWARF producer set that information. */
17674 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17675 if (attr && (DW_UNSND (attr) != 0))
17676 TYPE_NO_RETURN (ftype) = 1;
17677
17678 /* We need to add the subroutine type to the die immediately so
17679 we don't infinitely recurse when dealing with parameters
17680 declared as the same subroutine type. */
17681 set_die_type (die, ftype, cu);
17682
17683 if (die->child != NULL)
17684 {
17685 struct type *void_type = objfile_type (objfile)->builtin_void;
17686 struct die_info *child_die;
17687 int nparams, iparams;
17688
17689 /* Count the number of parameters.
17690 FIXME: GDB currently ignores vararg functions, but knows about
17691 vararg member functions. */
17692 nparams = 0;
17693 child_die = die->child;
17694 while (child_die && child_die->tag)
17695 {
17696 if (child_die->tag == DW_TAG_formal_parameter)
17697 nparams++;
17698 else if (child_die->tag == DW_TAG_unspecified_parameters)
17699 ftype->set_has_varargs (true);
17700
17701 child_die = child_die->sibling;
17702 }
17703
17704 /* Allocate storage for parameters and fill them in. */
17705 ftype->set_num_fields (nparams);
17706 ftype->set_fields
17707 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17708
17709 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17710 even if we error out during the parameters reading below. */
17711 for (iparams = 0; iparams < nparams; iparams++)
17712 ftype->field (iparams).set_type (void_type);
17713
17714 iparams = 0;
17715 child_die = die->child;
17716 while (child_die && child_die->tag)
17717 {
17718 if (child_die->tag == DW_TAG_formal_parameter)
17719 {
17720 struct type *arg_type;
17721
17722 /* DWARF version 2 has no clean way to discern C++
17723 static and non-static member functions. G++ helps
17724 GDB by marking the first parameter for non-static
17725 member functions (which is the this pointer) as
17726 artificial. We pass this information to
17727 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17728
17729 DWARF version 3 added DW_AT_object_pointer, which GCC
17730 4.5 does not yet generate. */
17731 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17732 if (attr != nullptr)
17733 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17734 else
17735 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17736 arg_type = die_type (child_die, cu);
17737
17738 /* RealView does not mark THIS as const, which the testsuite
17739 expects. GCC marks THIS as const in method definitions,
17740 but not in the class specifications (GCC PR 43053). */
17741 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17742 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17743 {
17744 int is_this = 0;
17745 struct dwarf2_cu *arg_cu = cu;
17746 const char *name = dwarf2_name (child_die, cu);
17747
17748 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17749 if (attr != nullptr)
17750 {
17751 /* If the compiler emits this, use it. */
17752 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17753 is_this = 1;
17754 }
17755 else if (name && strcmp (name, "this") == 0)
17756 /* Function definitions will have the argument names. */
17757 is_this = 1;
17758 else if (name == NULL && iparams == 0)
17759 /* Declarations may not have the names, so like
17760 elsewhere in GDB, assume an artificial first
17761 argument is "this". */
17762 is_this = 1;
17763
17764 if (is_this)
17765 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17766 arg_type, 0);
17767 }
17768
17769 ftype->field (iparams).set_type (arg_type);
17770 iparams++;
17771 }
17772 child_die = child_die->sibling;
17773 }
17774 }
17775
17776 return ftype;
17777 }
17778
17779 static struct type *
17780 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17781 {
17782 struct objfile *objfile = cu->per_objfile->objfile;
17783 const char *name = NULL;
17784 struct type *this_type, *target_type;
17785
17786 name = dwarf2_full_name (NULL, die, cu);
17787 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17788 this_type->set_target_is_stub (true);
17789 set_die_type (die, this_type, cu);
17790 target_type = die_type (die, cu);
17791 if (target_type != this_type)
17792 TYPE_TARGET_TYPE (this_type) = target_type;
17793 else
17794 {
17795 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17796 spec and cause infinite loops in GDB. */
17797 complaint (_("Self-referential DW_TAG_typedef "
17798 "- DIE at %s [in module %s]"),
17799 sect_offset_str (die->sect_off), objfile_name (objfile));
17800 TYPE_TARGET_TYPE (this_type) = NULL;
17801 }
17802 if (name == NULL)
17803 {
17804 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17805 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17806 Handle these by just returning the target type, rather than
17807 constructing an anonymous typedef type and trying to handle this
17808 elsewhere. */
17809 set_die_type (die, target_type, cu);
17810 return target_type;
17811 }
17812 return this_type;
17813 }
17814
17815 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17816 (which may be different from NAME) to the architecture back-end to allow
17817 it to guess the correct format if necessary. */
17818
17819 static struct type *
17820 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17821 const char *name_hint, enum bfd_endian byte_order)
17822 {
17823 struct gdbarch *gdbarch = objfile->arch ();
17824 const struct floatformat **format;
17825 struct type *type;
17826
17827 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17828 if (format)
17829 type = init_float_type (objfile, bits, name, format, byte_order);
17830 else
17831 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17832
17833 return type;
17834 }
17835
17836 /* Allocate an integer type of size BITS and name NAME. */
17837
17838 static struct type *
17839 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17840 int bits, int unsigned_p, const char *name)
17841 {
17842 struct type *type;
17843
17844 /* Versions of Intel's C Compiler generate an integer type called "void"
17845 instead of using DW_TAG_unspecified_type. This has been seen on
17846 at least versions 14, 17, and 18. */
17847 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17848 && strcmp (name, "void") == 0)
17849 type = objfile_type (objfile)->builtin_void;
17850 else
17851 type = init_integer_type (objfile, bits, unsigned_p, name);
17852
17853 return type;
17854 }
17855
17856 /* Initialise and return a floating point type of size BITS suitable for
17857 use as a component of a complex number. The NAME_HINT is passed through
17858 when initialising the floating point type and is the name of the complex
17859 type.
17860
17861 As DWARF doesn't currently provide an explicit name for the components
17862 of a complex number, but it can be helpful to have these components
17863 named, we try to select a suitable name based on the size of the
17864 component. */
17865 static struct type *
17866 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17867 struct objfile *objfile,
17868 int bits, const char *name_hint,
17869 enum bfd_endian byte_order)
17870 {
17871 gdbarch *gdbarch = objfile->arch ();
17872 struct type *tt = nullptr;
17873
17874 /* Try to find a suitable floating point builtin type of size BITS.
17875 We're going to use the name of this type as the name for the complex
17876 target type that we are about to create. */
17877 switch (cu->language)
17878 {
17879 case language_fortran:
17880 switch (bits)
17881 {
17882 case 32:
17883 tt = builtin_f_type (gdbarch)->builtin_real;
17884 break;
17885 case 64:
17886 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17887 break;
17888 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17889 case 128:
17890 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17891 break;
17892 }
17893 break;
17894 default:
17895 switch (bits)
17896 {
17897 case 32:
17898 tt = builtin_type (gdbarch)->builtin_float;
17899 break;
17900 case 64:
17901 tt = builtin_type (gdbarch)->builtin_double;
17902 break;
17903 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17904 case 128:
17905 tt = builtin_type (gdbarch)->builtin_long_double;
17906 break;
17907 }
17908 break;
17909 }
17910
17911 /* If the type we found doesn't match the size we were looking for, then
17912 pretend we didn't find a type at all, the complex target type we
17913 create will then be nameless. */
17914 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17915 tt = nullptr;
17916
17917 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17918 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17919 }
17920
17921 /* Find a representation of a given base type and install
17922 it in the TYPE field of the die. */
17923
17924 static struct type *
17925 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17926 {
17927 struct objfile *objfile = cu->per_objfile->objfile;
17928 struct type *type;
17929 struct attribute *attr;
17930 int encoding = 0, bits = 0;
17931 const char *name;
17932 gdbarch *arch;
17933
17934 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17935 if (attr != nullptr)
17936 encoding = DW_UNSND (attr);
17937 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17938 if (attr != nullptr)
17939 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17940 name = dwarf2_name (die, cu);
17941 if (!name)
17942 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17943
17944 arch = objfile->arch ();
17945 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17946
17947 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17948 if (attr)
17949 {
17950 int endianity = DW_UNSND (attr);
17951
17952 switch (endianity)
17953 {
17954 case DW_END_big:
17955 byte_order = BFD_ENDIAN_BIG;
17956 break;
17957 case DW_END_little:
17958 byte_order = BFD_ENDIAN_LITTLE;
17959 break;
17960 default:
17961 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17962 break;
17963 }
17964 }
17965
17966 switch (encoding)
17967 {
17968 case DW_ATE_address:
17969 /* Turn DW_ATE_address into a void * pointer. */
17970 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17971 type = init_pointer_type (objfile, bits, name, type);
17972 break;
17973 case DW_ATE_boolean:
17974 type = init_boolean_type (objfile, bits, 1, name);
17975 break;
17976 case DW_ATE_complex_float:
17977 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17978 byte_order);
17979 if (type->code () == TYPE_CODE_ERROR)
17980 {
17981 if (name == nullptr)
17982 {
17983 struct obstack *obstack
17984 = &cu->per_objfile->objfile->objfile_obstack;
17985 name = obconcat (obstack, "_Complex ", type->name (),
17986 nullptr);
17987 }
17988 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17989 }
17990 else
17991 type = init_complex_type (name, type);
17992 break;
17993 case DW_ATE_decimal_float:
17994 type = init_decfloat_type (objfile, bits, name);
17995 break;
17996 case DW_ATE_float:
17997 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17998 break;
17999 case DW_ATE_signed:
18000 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18001 break;
18002 case DW_ATE_unsigned:
18003 if (cu->language == language_fortran
18004 && name
18005 && startswith (name, "character("))
18006 type = init_character_type (objfile, bits, 1, name);
18007 else
18008 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18009 break;
18010 case DW_ATE_signed_char:
18011 if (cu->language == language_ada || cu->language == language_m2
18012 || cu->language == language_pascal
18013 || cu->language == language_fortran)
18014 type = init_character_type (objfile, bits, 0, name);
18015 else
18016 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18017 break;
18018 case DW_ATE_unsigned_char:
18019 if (cu->language == language_ada || cu->language == language_m2
18020 || cu->language == language_pascal
18021 || cu->language == language_fortran
18022 || cu->language == language_rust)
18023 type = init_character_type (objfile, bits, 1, name);
18024 else
18025 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18026 break;
18027 case DW_ATE_UTF:
18028 {
18029 if (bits == 16)
18030 type = builtin_type (arch)->builtin_char16;
18031 else if (bits == 32)
18032 type = builtin_type (arch)->builtin_char32;
18033 else
18034 {
18035 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
18036 bits);
18037 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18038 }
18039 return set_die_type (die, type, cu);
18040 }
18041 break;
18042
18043 default:
18044 complaint (_("unsupported DW_AT_encoding: '%s'"),
18045 dwarf_type_encoding_name (encoding));
18046 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18047 break;
18048 }
18049
18050 if (name && strcmp (name, "char") == 0)
18051 type->set_has_no_signedness (true);
18052
18053 maybe_set_alignment (cu, die, type);
18054
18055 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18056
18057 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18058 {
18059 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18060 if (attr != nullptr && DW_UNSND (attr) <= 8 * TYPE_LENGTH (type))
18061 {
18062 unsigned real_bit_size = DW_UNSND (attr);
18063 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18064 /* Only use the attributes if they make sense together. */
18065 if (attr == nullptr
18066 || DW_UNSND (attr) + real_bit_size <= 8 * TYPE_LENGTH (type))
18067 {
18068 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18069 = real_bit_size;
18070 if (attr != nullptr)
18071 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18072 = DW_UNSND (attr);
18073 }
18074 }
18075 }
18076
18077 return set_die_type (die, type, cu);
18078 }
18079
18080 /* Parse dwarf attribute if it's a block, reference or constant and put the
18081 resulting value of the attribute into struct bound_prop.
18082 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18083
18084 static int
18085 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18086 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18087 struct type *default_type)
18088 {
18089 struct dwarf2_property_baton *baton;
18090 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18091 struct objfile *objfile = per_objfile->objfile;
18092 struct obstack *obstack = &objfile->objfile_obstack;
18093
18094 gdb_assert (default_type != NULL);
18095
18096 if (attr == NULL || prop == NULL)
18097 return 0;
18098
18099 if (attr->form_is_block ())
18100 {
18101 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18102 baton->property_type = default_type;
18103 baton->locexpr.per_cu = cu->per_cu;
18104 baton->locexpr.per_objfile = per_objfile;
18105 baton->locexpr.size = DW_BLOCK (attr)->size;
18106 baton->locexpr.data = DW_BLOCK (attr)->data;
18107 switch (attr->name)
18108 {
18109 case DW_AT_string_length:
18110 baton->locexpr.is_reference = true;
18111 break;
18112 default:
18113 baton->locexpr.is_reference = false;
18114 break;
18115 }
18116
18117 prop->set_locexpr (baton);
18118 gdb_assert (prop->baton () != NULL);
18119 }
18120 else if (attr->form_is_ref ())
18121 {
18122 struct dwarf2_cu *target_cu = cu;
18123 struct die_info *target_die;
18124 struct attribute *target_attr;
18125
18126 target_die = follow_die_ref (die, attr, &target_cu);
18127 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18128 if (target_attr == NULL)
18129 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18130 target_cu);
18131 if (target_attr == NULL)
18132 return 0;
18133
18134 switch (target_attr->name)
18135 {
18136 case DW_AT_location:
18137 if (target_attr->form_is_section_offset ())
18138 {
18139 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18140 baton->property_type = die_type (target_die, target_cu);
18141 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18142 prop->set_loclist (baton);
18143 gdb_assert (prop->baton () != NULL);
18144 }
18145 else if (target_attr->form_is_block ())
18146 {
18147 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18148 baton->property_type = die_type (target_die, target_cu);
18149 baton->locexpr.per_cu = cu->per_cu;
18150 baton->locexpr.per_objfile = per_objfile;
18151 baton->locexpr.size = DW_BLOCK (target_attr)->size;
18152 baton->locexpr.data = DW_BLOCK (target_attr)->data;
18153 baton->locexpr.is_reference = true;
18154 prop->set_locexpr (baton);
18155 gdb_assert (prop->baton () != NULL);
18156 }
18157 else
18158 {
18159 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18160 "dynamic property");
18161 return 0;
18162 }
18163 break;
18164 case DW_AT_data_member_location:
18165 {
18166 LONGEST offset;
18167
18168 if (!handle_data_member_location (target_die, target_cu,
18169 &offset))
18170 return 0;
18171
18172 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18173 baton->property_type = read_type_die (target_die->parent,
18174 target_cu);
18175 baton->offset_info.offset = offset;
18176 baton->offset_info.type = die_type (target_die, target_cu);
18177 prop->set_addr_offset (baton);
18178 break;
18179 }
18180 }
18181 }
18182 else if (attr->form_is_constant ())
18183 prop->set_const_val (attr->constant_value (0));
18184 else
18185 {
18186 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18187 dwarf2_name (die, cu));
18188 return 0;
18189 }
18190
18191 return 1;
18192 }
18193
18194 /* See read.h. */
18195
18196 struct type *
18197 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
18198 {
18199 struct type *int_type;
18200
18201 /* Helper macro to examine the various builtin types. */
18202 #define TRY_TYPE(F) \
18203 int_type = (unsigned_p \
18204 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18205 : objfile_type (objfile)->builtin_ ## F); \
18206 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
18207 return int_type
18208
18209 TRY_TYPE (char);
18210 TRY_TYPE (short);
18211 TRY_TYPE (int);
18212 TRY_TYPE (long);
18213 TRY_TYPE (long_long);
18214
18215 #undef TRY_TYPE
18216
18217 gdb_assert_not_reached ("unable to find suitable integer type");
18218 }
18219
18220 /* See read.h. */
18221
18222 struct type *
18223 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
18224 {
18225 int addr_size = this->per_cu->addr_size ();
18226 return this->per_objfile->int_type (addr_size, unsigned_p);
18227 }
18228
18229 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18230 present (which is valid) then compute the default type based on the
18231 compilation units address size. */
18232
18233 static struct type *
18234 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18235 {
18236 struct type *index_type = die_type (die, cu);
18237
18238 /* Dwarf-2 specifications explicitly allows to create subrange types
18239 without specifying a base type.
18240 In that case, the base type must be set to the type of
18241 the lower bound, upper bound or count, in that order, if any of these
18242 three attributes references an object that has a type.
18243 If no base type is found, the Dwarf-2 specifications say that
18244 a signed integer type of size equal to the size of an address should
18245 be used.
18246 For the following C code: `extern char gdb_int [];'
18247 GCC produces an empty range DIE.
18248 FIXME: muller/2010-05-28: Possible references to object for low bound,
18249 high bound or count are not yet handled by this code. */
18250 if (index_type->code () == TYPE_CODE_VOID)
18251 index_type = cu->addr_sized_int_type (false);
18252
18253 return index_type;
18254 }
18255
18256 /* Read the given DW_AT_subrange DIE. */
18257
18258 static struct type *
18259 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18260 {
18261 struct type *base_type, *orig_base_type;
18262 struct type *range_type;
18263 struct attribute *attr;
18264 struct dynamic_prop low, high;
18265 int low_default_is_valid;
18266 int high_bound_is_count = 0;
18267 const char *name;
18268 ULONGEST negative_mask;
18269
18270 orig_base_type = read_subrange_index_type (die, cu);
18271
18272 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18273 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18274 creating the range type, but we use the result of check_typedef
18275 when examining properties of the type. */
18276 base_type = check_typedef (orig_base_type);
18277
18278 /* The die_type call above may have already set the type for this DIE. */
18279 range_type = get_die_type (die, cu);
18280 if (range_type)
18281 return range_type;
18282
18283 high.set_const_val (0);
18284
18285 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18286 omitting DW_AT_lower_bound. */
18287 switch (cu->language)
18288 {
18289 case language_c:
18290 case language_cplus:
18291 low.set_const_val (0);
18292 low_default_is_valid = 1;
18293 break;
18294 case language_fortran:
18295 low.set_const_val (1);
18296 low_default_is_valid = 1;
18297 break;
18298 case language_d:
18299 case language_objc:
18300 case language_rust:
18301 low.set_const_val (0);
18302 low_default_is_valid = (cu->header.version >= 4);
18303 break;
18304 case language_ada:
18305 case language_m2:
18306 case language_pascal:
18307 low.set_const_val (1);
18308 low_default_is_valid = (cu->header.version >= 4);
18309 break;
18310 default:
18311 low.set_const_val (0);
18312 low_default_is_valid = 0;
18313 break;
18314 }
18315
18316 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18317 if (attr != nullptr)
18318 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18319 else if (!low_default_is_valid)
18320 complaint (_("Missing DW_AT_lower_bound "
18321 "- DIE at %s [in module %s]"),
18322 sect_offset_str (die->sect_off),
18323 objfile_name (cu->per_objfile->objfile));
18324
18325 struct attribute *attr_ub, *attr_count;
18326 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18327 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18328 {
18329 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18330 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18331 {
18332 /* If bounds are constant do the final calculation here. */
18333 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18334 high.set_const_val (low.const_val () + high.const_val () - 1);
18335 else
18336 high_bound_is_count = 1;
18337 }
18338 else
18339 {
18340 if (attr_ub != NULL)
18341 complaint (_("Unresolved DW_AT_upper_bound "
18342 "- DIE at %s [in module %s]"),
18343 sect_offset_str (die->sect_off),
18344 objfile_name (cu->per_objfile->objfile));
18345 if (attr_count != NULL)
18346 complaint (_("Unresolved DW_AT_count "
18347 "- DIE at %s [in module %s]"),
18348 sect_offset_str (die->sect_off),
18349 objfile_name (cu->per_objfile->objfile));
18350 }
18351 }
18352
18353 LONGEST bias = 0;
18354 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18355 if (bias_attr != nullptr && bias_attr->form_is_constant ())
18356 bias = bias_attr->constant_value (0);
18357
18358 /* Normally, the DWARF producers are expected to use a signed
18359 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18360 But this is unfortunately not always the case, as witnessed
18361 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18362 is used instead. To work around that ambiguity, we treat
18363 the bounds as signed, and thus sign-extend their values, when
18364 the base type is signed. */
18365 negative_mask =
18366 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18367 if (low.kind () == PROP_CONST
18368 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
18369 low.set_const_val (low.const_val () | negative_mask);
18370 if (high.kind () == PROP_CONST
18371 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
18372 high.set_const_val (high.const_val () | negative_mask);
18373
18374 /* Check for bit and byte strides. */
18375 struct dynamic_prop byte_stride_prop;
18376 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18377 if (attr_byte_stride != nullptr)
18378 {
18379 struct type *prop_type = cu->addr_sized_int_type (false);
18380 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18381 prop_type);
18382 }
18383
18384 struct dynamic_prop bit_stride_prop;
18385 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18386 if (attr_bit_stride != nullptr)
18387 {
18388 /* It only makes sense to have either a bit or byte stride. */
18389 if (attr_byte_stride != nullptr)
18390 {
18391 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18392 "- DIE at %s [in module %s]"),
18393 sect_offset_str (die->sect_off),
18394 objfile_name (cu->per_objfile->objfile));
18395 attr_bit_stride = nullptr;
18396 }
18397 else
18398 {
18399 struct type *prop_type = cu->addr_sized_int_type (false);
18400 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18401 prop_type);
18402 }
18403 }
18404
18405 if (attr_byte_stride != nullptr
18406 || attr_bit_stride != nullptr)
18407 {
18408 bool byte_stride_p = (attr_byte_stride != nullptr);
18409 struct dynamic_prop *stride
18410 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18411
18412 range_type
18413 = create_range_type_with_stride (NULL, orig_base_type, &low,
18414 &high, bias, stride, byte_stride_p);
18415 }
18416 else
18417 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18418
18419 if (high_bound_is_count)
18420 range_type->bounds ()->flag_upper_bound_is_count = 1;
18421
18422 /* Ada expects an empty array on no boundary attributes. */
18423 if (attr == NULL && cu->language != language_ada)
18424 range_type->bounds ()->high.set_undefined ();
18425
18426 name = dwarf2_name (die, cu);
18427 if (name)
18428 range_type->set_name (name);
18429
18430 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18431 if (attr != nullptr)
18432 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18433
18434 maybe_set_alignment (cu, die, range_type);
18435
18436 set_die_type (die, range_type, cu);
18437
18438 /* set_die_type should be already done. */
18439 set_descriptive_type (range_type, die, cu);
18440
18441 return range_type;
18442 }
18443
18444 static struct type *
18445 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18446 {
18447 struct type *type;
18448
18449 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
18450 type->set_name (dwarf2_name (die, cu));
18451
18452 /* In Ada, an unspecified type is typically used when the description
18453 of the type is deferred to a different unit. When encountering
18454 such a type, we treat it as a stub, and try to resolve it later on,
18455 when needed. */
18456 if (cu->language == language_ada)
18457 type->set_is_stub (true);
18458
18459 return set_die_type (die, type, cu);
18460 }
18461
18462 /* Read a single die and all its descendents. Set the die's sibling
18463 field to NULL; set other fields in the die correctly, and set all
18464 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18465 location of the info_ptr after reading all of those dies. PARENT
18466 is the parent of the die in question. */
18467
18468 static struct die_info *
18469 read_die_and_children (const struct die_reader_specs *reader,
18470 const gdb_byte *info_ptr,
18471 const gdb_byte **new_info_ptr,
18472 struct die_info *parent)
18473 {
18474 struct die_info *die;
18475 const gdb_byte *cur_ptr;
18476
18477 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
18478 if (die == NULL)
18479 {
18480 *new_info_ptr = cur_ptr;
18481 return NULL;
18482 }
18483 store_in_ref_table (die, reader->cu);
18484
18485 if (die->has_children)
18486 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18487 else
18488 {
18489 die->child = NULL;
18490 *new_info_ptr = cur_ptr;
18491 }
18492
18493 die->sibling = NULL;
18494 die->parent = parent;
18495 return die;
18496 }
18497
18498 /* Read a die, all of its descendents, and all of its siblings; set
18499 all of the fields of all of the dies correctly. Arguments are as
18500 in read_die_and_children. */
18501
18502 static struct die_info *
18503 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18504 const gdb_byte *info_ptr,
18505 const gdb_byte **new_info_ptr,
18506 struct die_info *parent)
18507 {
18508 struct die_info *first_die, *last_sibling;
18509 const gdb_byte *cur_ptr;
18510
18511 cur_ptr = info_ptr;
18512 first_die = last_sibling = NULL;
18513
18514 while (1)
18515 {
18516 struct die_info *die
18517 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18518
18519 if (die == NULL)
18520 {
18521 *new_info_ptr = cur_ptr;
18522 return first_die;
18523 }
18524
18525 if (!first_die)
18526 first_die = die;
18527 else
18528 last_sibling->sibling = die;
18529
18530 last_sibling = die;
18531 }
18532 }
18533
18534 /* Read a die, all of its descendents, and all of its siblings; set
18535 all of the fields of all of the dies correctly. Arguments are as
18536 in read_die_and_children.
18537 This the main entry point for reading a DIE and all its children. */
18538
18539 static struct die_info *
18540 read_die_and_siblings (const struct die_reader_specs *reader,
18541 const gdb_byte *info_ptr,
18542 const gdb_byte **new_info_ptr,
18543 struct die_info *parent)
18544 {
18545 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18546 new_info_ptr, parent);
18547
18548 if (dwarf_die_debug)
18549 {
18550 fprintf_unfiltered (gdb_stdlog,
18551 "Read die from %s@0x%x of %s:\n",
18552 reader->die_section->get_name (),
18553 (unsigned) (info_ptr - reader->die_section->buffer),
18554 bfd_get_filename (reader->abfd));
18555 dump_die (die, dwarf_die_debug);
18556 }
18557
18558 return die;
18559 }
18560
18561 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18562 attributes.
18563 The caller is responsible for filling in the extra attributes
18564 and updating (*DIEP)->num_attrs.
18565 Set DIEP to point to a newly allocated die with its information,
18566 except for its child, sibling, and parent fields. */
18567
18568 static const gdb_byte *
18569 read_full_die_1 (const struct die_reader_specs *reader,
18570 struct die_info **diep, const gdb_byte *info_ptr,
18571 int num_extra_attrs)
18572 {
18573 unsigned int abbrev_number, bytes_read, i;
18574 struct abbrev_info *abbrev;
18575 struct die_info *die;
18576 struct dwarf2_cu *cu = reader->cu;
18577 bfd *abfd = reader->abfd;
18578
18579 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18580 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18581 info_ptr += bytes_read;
18582 if (!abbrev_number)
18583 {
18584 *diep = NULL;
18585 return info_ptr;
18586 }
18587
18588 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18589 if (!abbrev)
18590 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18591 abbrev_number,
18592 bfd_get_filename (abfd));
18593
18594 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18595 die->sect_off = sect_off;
18596 die->tag = abbrev->tag;
18597 die->abbrev = abbrev_number;
18598 die->has_children = abbrev->has_children;
18599
18600 /* Make the result usable.
18601 The caller needs to update num_attrs after adding the extra
18602 attributes. */
18603 die->num_attrs = abbrev->num_attrs;
18604
18605 std::vector<int> indexes_that_need_reprocess;
18606 for (i = 0; i < abbrev->num_attrs; ++i)
18607 {
18608 bool need_reprocess;
18609 info_ptr =
18610 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18611 info_ptr, &need_reprocess);
18612 if (need_reprocess)
18613 indexes_that_need_reprocess.push_back (i);
18614 }
18615
18616 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18617 if (attr != nullptr)
18618 cu->str_offsets_base = DW_UNSND (attr);
18619
18620 attr = die->attr (DW_AT_loclists_base);
18621 if (attr != nullptr)
18622 cu->loclist_base = DW_UNSND (attr);
18623
18624 auto maybe_addr_base = die->addr_base ();
18625 if (maybe_addr_base.has_value ())
18626 cu->addr_base = *maybe_addr_base;
18627
18628 attr = die->attr (DW_AT_rnglists_base);
18629 if (attr != nullptr)
18630 cu->ranges_base = DW_UNSND (attr);
18631
18632 for (int index : indexes_that_need_reprocess)
18633 read_attribute_reprocess (reader, &die->attrs[index], die->tag);
18634 *diep = die;
18635 return info_ptr;
18636 }
18637
18638 /* Read a die and all its attributes.
18639 Set DIEP to point to a newly allocated die with its information,
18640 except for its child, sibling, and parent fields. */
18641
18642 static const gdb_byte *
18643 read_full_die (const struct die_reader_specs *reader,
18644 struct die_info **diep, const gdb_byte *info_ptr)
18645 {
18646 const gdb_byte *result;
18647
18648 result = read_full_die_1 (reader, diep, info_ptr, 0);
18649
18650 if (dwarf_die_debug)
18651 {
18652 fprintf_unfiltered (gdb_stdlog,
18653 "Read die from %s@0x%x of %s:\n",
18654 reader->die_section->get_name (),
18655 (unsigned) (info_ptr - reader->die_section->buffer),
18656 bfd_get_filename (reader->abfd));
18657 dump_die (*diep, dwarf_die_debug);
18658 }
18659
18660 return result;
18661 }
18662 \f
18663
18664 /* Returns nonzero if TAG represents a type that we might generate a partial
18665 symbol for. */
18666
18667 static int
18668 is_type_tag_for_partial (int tag)
18669 {
18670 switch (tag)
18671 {
18672 #if 0
18673 /* Some types that would be reasonable to generate partial symbols for,
18674 that we don't at present. */
18675 case DW_TAG_array_type:
18676 case DW_TAG_file_type:
18677 case DW_TAG_ptr_to_member_type:
18678 case DW_TAG_set_type:
18679 case DW_TAG_string_type:
18680 case DW_TAG_subroutine_type:
18681 #endif
18682 case DW_TAG_base_type:
18683 case DW_TAG_class_type:
18684 case DW_TAG_interface_type:
18685 case DW_TAG_enumeration_type:
18686 case DW_TAG_structure_type:
18687 case DW_TAG_subrange_type:
18688 case DW_TAG_typedef:
18689 case DW_TAG_union_type:
18690 return 1;
18691 default:
18692 return 0;
18693 }
18694 }
18695
18696 /* Load all DIEs that are interesting for partial symbols into memory. */
18697
18698 static struct partial_die_info *
18699 load_partial_dies (const struct die_reader_specs *reader,
18700 const gdb_byte *info_ptr, int building_psymtab)
18701 {
18702 struct dwarf2_cu *cu = reader->cu;
18703 struct objfile *objfile = cu->per_objfile->objfile;
18704 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18705 unsigned int bytes_read;
18706 unsigned int load_all = 0;
18707 int nesting_level = 1;
18708
18709 parent_die = NULL;
18710 last_die = NULL;
18711
18712 gdb_assert (cu->per_cu != NULL);
18713 if (cu->per_cu->load_all_dies)
18714 load_all = 1;
18715
18716 cu->partial_dies
18717 = htab_create_alloc_ex (cu->header.length / 12,
18718 partial_die_hash,
18719 partial_die_eq,
18720 NULL,
18721 &cu->comp_unit_obstack,
18722 hashtab_obstack_allocate,
18723 dummy_obstack_deallocate);
18724
18725 while (1)
18726 {
18727 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18728
18729 /* A NULL abbrev means the end of a series of children. */
18730 if (abbrev == NULL)
18731 {
18732 if (--nesting_level == 0)
18733 return first_die;
18734
18735 info_ptr += bytes_read;
18736 last_die = parent_die;
18737 parent_die = parent_die->die_parent;
18738 continue;
18739 }
18740
18741 /* Check for template arguments. We never save these; if
18742 they're seen, we just mark the parent, and go on our way. */
18743 if (parent_die != NULL
18744 && cu->language == language_cplus
18745 && (abbrev->tag == DW_TAG_template_type_param
18746 || abbrev->tag == DW_TAG_template_value_param))
18747 {
18748 parent_die->has_template_arguments = 1;
18749
18750 if (!load_all)
18751 {
18752 /* We don't need a partial DIE for the template argument. */
18753 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18754 continue;
18755 }
18756 }
18757
18758 /* We only recurse into c++ subprograms looking for template arguments.
18759 Skip their other children. */
18760 if (!load_all
18761 && cu->language == language_cplus
18762 && parent_die != NULL
18763 && parent_die->tag == DW_TAG_subprogram
18764 && abbrev->tag != DW_TAG_inlined_subroutine)
18765 {
18766 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18767 continue;
18768 }
18769
18770 /* Check whether this DIE is interesting enough to save. Normally
18771 we would not be interested in members here, but there may be
18772 later variables referencing them via DW_AT_specification (for
18773 static members). */
18774 if (!load_all
18775 && !is_type_tag_for_partial (abbrev->tag)
18776 && abbrev->tag != DW_TAG_constant
18777 && abbrev->tag != DW_TAG_enumerator
18778 && abbrev->tag != DW_TAG_subprogram
18779 && abbrev->tag != DW_TAG_inlined_subroutine
18780 && abbrev->tag != DW_TAG_lexical_block
18781 && abbrev->tag != DW_TAG_variable
18782 && abbrev->tag != DW_TAG_namespace
18783 && abbrev->tag != DW_TAG_module
18784 && abbrev->tag != DW_TAG_member
18785 && abbrev->tag != DW_TAG_imported_unit
18786 && abbrev->tag != DW_TAG_imported_declaration)
18787 {
18788 /* Otherwise we skip to the next sibling, if any. */
18789 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18790 continue;
18791 }
18792
18793 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18794 abbrev);
18795
18796 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18797
18798 /* This two-pass algorithm for processing partial symbols has a
18799 high cost in cache pressure. Thus, handle some simple cases
18800 here which cover the majority of C partial symbols. DIEs
18801 which neither have specification tags in them, nor could have
18802 specification tags elsewhere pointing at them, can simply be
18803 processed and discarded.
18804
18805 This segment is also optional; scan_partial_symbols and
18806 add_partial_symbol will handle these DIEs if we chain
18807 them in normally. When compilers which do not emit large
18808 quantities of duplicate debug information are more common,
18809 this code can probably be removed. */
18810
18811 /* Any complete simple types at the top level (pretty much all
18812 of them, for a language without namespaces), can be processed
18813 directly. */
18814 if (parent_die == NULL
18815 && pdi.has_specification == 0
18816 && pdi.is_declaration == 0
18817 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18818 || pdi.tag == DW_TAG_base_type
18819 || pdi.tag == DW_TAG_subrange_type))
18820 {
18821 if (building_psymtab && pdi.raw_name != NULL)
18822 add_partial_symbol (&pdi, cu);
18823
18824 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18825 continue;
18826 }
18827
18828 /* The exception for DW_TAG_typedef with has_children above is
18829 a workaround of GCC PR debug/47510. In the case of this complaint
18830 type_name_or_error will error on such types later.
18831
18832 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18833 it could not find the child DIEs referenced later, this is checked
18834 above. In correct DWARF DW_TAG_typedef should have no children. */
18835
18836 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18837 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18838 "- DIE at %s [in module %s]"),
18839 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18840
18841 /* If we're at the second level, and we're an enumerator, and
18842 our parent has no specification (meaning possibly lives in a
18843 namespace elsewhere), then we can add the partial symbol now
18844 instead of queueing it. */
18845 if (pdi.tag == DW_TAG_enumerator
18846 && parent_die != NULL
18847 && parent_die->die_parent == NULL
18848 && parent_die->tag == DW_TAG_enumeration_type
18849 && parent_die->has_specification == 0)
18850 {
18851 if (pdi.raw_name == NULL)
18852 complaint (_("malformed enumerator DIE ignored"));
18853 else if (building_psymtab)
18854 add_partial_symbol (&pdi, cu);
18855
18856 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18857 continue;
18858 }
18859
18860 struct partial_die_info *part_die
18861 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18862
18863 /* We'll save this DIE so link it in. */
18864 part_die->die_parent = parent_die;
18865 part_die->die_sibling = NULL;
18866 part_die->die_child = NULL;
18867
18868 if (last_die && last_die == parent_die)
18869 last_die->die_child = part_die;
18870 else if (last_die)
18871 last_die->die_sibling = part_die;
18872
18873 last_die = part_die;
18874
18875 if (first_die == NULL)
18876 first_die = part_die;
18877
18878 /* Maybe add the DIE to the hash table. Not all DIEs that we
18879 find interesting need to be in the hash table, because we
18880 also have the parent/sibling/child chains; only those that we
18881 might refer to by offset later during partial symbol reading.
18882
18883 For now this means things that might have be the target of a
18884 DW_AT_specification, DW_AT_abstract_origin, or
18885 DW_AT_extension. DW_AT_extension will refer only to
18886 namespaces; DW_AT_abstract_origin refers to functions (and
18887 many things under the function DIE, but we do not recurse
18888 into function DIEs during partial symbol reading) and
18889 possibly variables as well; DW_AT_specification refers to
18890 declarations. Declarations ought to have the DW_AT_declaration
18891 flag. It happens that GCC forgets to put it in sometimes, but
18892 only for functions, not for types.
18893
18894 Adding more things than necessary to the hash table is harmless
18895 except for the performance cost. Adding too few will result in
18896 wasted time in find_partial_die, when we reread the compilation
18897 unit with load_all_dies set. */
18898
18899 if (load_all
18900 || abbrev->tag == DW_TAG_constant
18901 || abbrev->tag == DW_TAG_subprogram
18902 || abbrev->tag == DW_TAG_variable
18903 || abbrev->tag == DW_TAG_namespace
18904 || part_die->is_declaration)
18905 {
18906 void **slot;
18907
18908 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18909 to_underlying (part_die->sect_off),
18910 INSERT);
18911 *slot = part_die;
18912 }
18913
18914 /* For some DIEs we want to follow their children (if any). For C
18915 we have no reason to follow the children of structures; for other
18916 languages we have to, so that we can get at method physnames
18917 to infer fully qualified class names, for DW_AT_specification,
18918 and for C++ template arguments. For C++, we also look one level
18919 inside functions to find template arguments (if the name of the
18920 function does not already contain the template arguments).
18921
18922 For Ada and Fortran, we need to scan the children of subprograms
18923 and lexical blocks as well because these languages allow the
18924 definition of nested entities that could be interesting for the
18925 debugger, such as nested subprograms for instance. */
18926 if (last_die->has_children
18927 && (load_all
18928 || last_die->tag == DW_TAG_namespace
18929 || last_die->tag == DW_TAG_module
18930 || last_die->tag == DW_TAG_enumeration_type
18931 || (cu->language == language_cplus
18932 && last_die->tag == DW_TAG_subprogram
18933 && (last_die->raw_name == NULL
18934 || strchr (last_die->raw_name, '<') == NULL))
18935 || (cu->language != language_c
18936 && (last_die->tag == DW_TAG_class_type
18937 || last_die->tag == DW_TAG_interface_type
18938 || last_die->tag == DW_TAG_structure_type
18939 || last_die->tag == DW_TAG_union_type))
18940 || ((cu->language == language_ada
18941 || cu->language == language_fortran)
18942 && (last_die->tag == DW_TAG_subprogram
18943 || last_die->tag == DW_TAG_lexical_block))))
18944 {
18945 nesting_level++;
18946 parent_die = last_die;
18947 continue;
18948 }
18949
18950 /* Otherwise we skip to the next sibling, if any. */
18951 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18952
18953 /* Back to the top, do it again. */
18954 }
18955 }
18956
18957 partial_die_info::partial_die_info (sect_offset sect_off_,
18958 struct abbrev_info *abbrev)
18959 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18960 {
18961 }
18962
18963 /* See class definition. */
18964
18965 const char *
18966 partial_die_info::name (dwarf2_cu *cu)
18967 {
18968 if (!canonical_name && raw_name != nullptr)
18969 {
18970 struct objfile *objfile = cu->per_objfile->objfile;
18971 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
18972 canonical_name = 1;
18973 }
18974
18975 return raw_name;
18976 }
18977
18978 /* Read a minimal amount of information into the minimal die structure.
18979 INFO_PTR should point just after the initial uleb128 of a DIE. */
18980
18981 const gdb_byte *
18982 partial_die_info::read (const struct die_reader_specs *reader,
18983 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18984 {
18985 struct dwarf2_cu *cu = reader->cu;
18986 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18987 unsigned int i;
18988 int has_low_pc_attr = 0;
18989 int has_high_pc_attr = 0;
18990 int high_pc_relative = 0;
18991
18992 for (i = 0; i < abbrev.num_attrs; ++i)
18993 {
18994 attribute attr;
18995 bool need_reprocess;
18996 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18997 info_ptr, &need_reprocess);
18998 /* String and address offsets that need to do the reprocessing have
18999 already been read at this point, so there is no need to wait until
19000 the loop terminates to do the reprocessing. */
19001 if (need_reprocess)
19002 read_attribute_reprocess (reader, &attr, tag);
19003 /* Store the data if it is of an attribute we want to keep in a
19004 partial symbol table. */
19005 switch (attr.name)
19006 {
19007 case DW_AT_name:
19008 switch (tag)
19009 {
19010 case DW_TAG_compile_unit:
19011 case DW_TAG_partial_unit:
19012 case DW_TAG_type_unit:
19013 /* Compilation units have a DW_AT_name that is a filename, not
19014 a source language identifier. */
19015 case DW_TAG_enumeration_type:
19016 case DW_TAG_enumerator:
19017 /* These tags always have simple identifiers already; no need
19018 to canonicalize them. */
19019 canonical_name = 1;
19020 raw_name = DW_STRING (&attr);
19021 break;
19022 default:
19023 canonical_name = 0;
19024 raw_name = DW_STRING (&attr);
19025 break;
19026 }
19027 break;
19028 case DW_AT_linkage_name:
19029 case DW_AT_MIPS_linkage_name:
19030 /* Note that both forms of linkage name might appear. We
19031 assume they will be the same, and we only store the last
19032 one we see. */
19033 linkage_name = attr.value_as_string ();
19034 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
19035 See https://github.com/rust-lang/rust/issues/32925. */
19036 if (cu->language == language_rust && linkage_name != NULL
19037 && strchr (linkage_name, '{') != NULL)
19038 linkage_name = NULL;
19039 break;
19040 case DW_AT_low_pc:
19041 has_low_pc_attr = 1;
19042 lowpc = attr.value_as_address ();
19043 break;
19044 case DW_AT_high_pc:
19045 has_high_pc_attr = 1;
19046 highpc = attr.value_as_address ();
19047 if (cu->header.version >= 4 && attr.form_is_constant ())
19048 high_pc_relative = 1;
19049 break;
19050 case DW_AT_location:
19051 /* Support the .debug_loc offsets. */
19052 if (attr.form_is_block ())
19053 {
19054 d.locdesc = DW_BLOCK (&attr);
19055 }
19056 else if (attr.form_is_section_offset ())
19057 {
19058 dwarf2_complex_location_expr_complaint ();
19059 }
19060 else
19061 {
19062 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19063 "partial symbol information");
19064 }
19065 break;
19066 case DW_AT_external:
19067 is_external = DW_UNSND (&attr);
19068 break;
19069 case DW_AT_declaration:
19070 is_declaration = DW_UNSND (&attr);
19071 break;
19072 case DW_AT_type:
19073 has_type = 1;
19074 break;
19075 case DW_AT_abstract_origin:
19076 case DW_AT_specification:
19077 case DW_AT_extension:
19078 has_specification = 1;
19079 spec_offset = attr.get_ref_die_offset ();
19080 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19081 || cu->per_cu->is_dwz);
19082 break;
19083 case DW_AT_sibling:
19084 /* Ignore absolute siblings, they might point outside of
19085 the current compile unit. */
19086 if (attr.form == DW_FORM_ref_addr)
19087 complaint (_("ignoring absolute DW_AT_sibling"));
19088 else
19089 {
19090 const gdb_byte *buffer = reader->buffer;
19091 sect_offset off = attr.get_ref_die_offset ();
19092 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19093
19094 if (sibling_ptr < info_ptr)
19095 complaint (_("DW_AT_sibling points backwards"));
19096 else if (sibling_ptr > reader->buffer_end)
19097 reader->die_section->overflow_complaint ();
19098 else
19099 sibling = sibling_ptr;
19100 }
19101 break;
19102 case DW_AT_byte_size:
19103 has_byte_size = 1;
19104 break;
19105 case DW_AT_const_value:
19106 has_const_value = 1;
19107 break;
19108 case DW_AT_calling_convention:
19109 /* DWARF doesn't provide a way to identify a program's source-level
19110 entry point. DW_AT_calling_convention attributes are only meant
19111 to describe functions' calling conventions.
19112
19113 However, because it's a necessary piece of information in
19114 Fortran, and before DWARF 4 DW_CC_program was the only
19115 piece of debugging information whose definition refers to
19116 a 'main program' at all, several compilers marked Fortran
19117 main programs with DW_CC_program --- even when those
19118 functions use the standard calling conventions.
19119
19120 Although DWARF now specifies a way to provide this
19121 information, we support this practice for backward
19122 compatibility. */
19123 if (DW_UNSND (&attr) == DW_CC_program
19124 && cu->language == language_fortran)
19125 main_subprogram = 1;
19126 break;
19127 case DW_AT_inline:
19128 if (DW_UNSND (&attr) == DW_INL_inlined
19129 || DW_UNSND (&attr) == DW_INL_declared_inlined)
19130 may_be_inlined = 1;
19131 break;
19132
19133 case DW_AT_import:
19134 if (tag == DW_TAG_imported_unit)
19135 {
19136 d.sect_off = attr.get_ref_die_offset ();
19137 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19138 || cu->per_cu->is_dwz);
19139 }
19140 break;
19141
19142 case DW_AT_main_subprogram:
19143 main_subprogram = DW_UNSND (&attr);
19144 break;
19145
19146 case DW_AT_ranges:
19147 {
19148 /* DW_AT_rnglists_base does not apply to DIEs from the DWO
19149 skeleton. We take advantage of the fact the DW_AT_ranges
19150 does not appear in DW_TAG_compile_unit of DWO files.
19151
19152 Attributes of the form DW_FORM_rnglistx have already had
19153 their value changed by read_rnglist_index and already
19154 include DW_AT_rnglists_base, so don't need to add the ranges
19155 base, either. */
19156 int need_ranges_base = (tag != DW_TAG_compile_unit
19157 && attr.form != DW_FORM_rnglistx);
19158 unsigned int ranges_offset = (DW_UNSND (&attr)
19159 + (need_ranges_base
19160 ? cu->ranges_base
19161 : 0));
19162
19163 /* Value of the DW_AT_ranges attribute is the offset in the
19164 .debug_ranges section. */
19165 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19166 nullptr, tag))
19167 has_pc_info = 1;
19168 }
19169 break;
19170
19171 default:
19172 break;
19173 }
19174 }
19175
19176 /* For Ada, if both the name and the linkage name appear, we prefer
19177 the latter. This lets "catch exception" work better, regardless
19178 of the order in which the name and linkage name were emitted.
19179 Really, though, this is just a workaround for the fact that gdb
19180 doesn't store both the name and the linkage name. */
19181 if (cu->language == language_ada && linkage_name != nullptr)
19182 raw_name = linkage_name;
19183
19184 if (high_pc_relative)
19185 highpc += lowpc;
19186
19187 if (has_low_pc_attr && has_high_pc_attr)
19188 {
19189 /* When using the GNU linker, .gnu.linkonce. sections are used to
19190 eliminate duplicate copies of functions and vtables and such.
19191 The linker will arbitrarily choose one and discard the others.
19192 The AT_*_pc values for such functions refer to local labels in
19193 these sections. If the section from that file was discarded, the
19194 labels are not in the output, so the relocs get a value of 0.
19195 If this is a discarded function, mark the pc bounds as invalid,
19196 so that GDB will ignore it. */
19197 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
19198 {
19199 struct objfile *objfile = per_objfile->objfile;
19200 struct gdbarch *gdbarch = objfile->arch ();
19201
19202 complaint (_("DW_AT_low_pc %s is zero "
19203 "for DIE at %s [in module %s]"),
19204 paddress (gdbarch, lowpc),
19205 sect_offset_str (sect_off),
19206 objfile_name (objfile));
19207 }
19208 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19209 else if (lowpc >= highpc)
19210 {
19211 struct objfile *objfile = per_objfile->objfile;
19212 struct gdbarch *gdbarch = objfile->arch ();
19213
19214 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19215 "for DIE at %s [in module %s]"),
19216 paddress (gdbarch, lowpc),
19217 paddress (gdbarch, highpc),
19218 sect_offset_str (sect_off),
19219 objfile_name (objfile));
19220 }
19221 else
19222 has_pc_info = 1;
19223 }
19224
19225 return info_ptr;
19226 }
19227
19228 /* Find a cached partial DIE at OFFSET in CU. */
19229
19230 struct partial_die_info *
19231 dwarf2_cu::find_partial_die (sect_offset sect_off)
19232 {
19233 struct partial_die_info *lookup_die = NULL;
19234 struct partial_die_info part_die (sect_off);
19235
19236 lookup_die = ((struct partial_die_info *)
19237 htab_find_with_hash (partial_dies, &part_die,
19238 to_underlying (sect_off)));
19239
19240 return lookup_die;
19241 }
19242
19243 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19244 except in the case of .debug_types DIEs which do not reference
19245 outside their CU (they do however referencing other types via
19246 DW_FORM_ref_sig8). */
19247
19248 static const struct cu_partial_die_info
19249 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19250 {
19251 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19252 struct objfile *objfile = per_objfile->objfile;
19253 struct partial_die_info *pd = NULL;
19254
19255 if (offset_in_dwz == cu->per_cu->is_dwz
19256 && cu->header.offset_in_cu_p (sect_off))
19257 {
19258 pd = cu->find_partial_die (sect_off);
19259 if (pd != NULL)
19260 return { cu, pd };
19261 /* We missed recording what we needed.
19262 Load all dies and try again. */
19263 }
19264 else
19265 {
19266 /* TUs don't reference other CUs/TUs (except via type signatures). */
19267 if (cu->per_cu->is_debug_types)
19268 {
19269 error (_("Dwarf Error: Type Unit at offset %s contains"
19270 " external reference to offset %s [in module %s].\n"),
19271 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19272 bfd_get_filename (objfile->obfd));
19273 }
19274 dwarf2_per_cu_data *per_cu
19275 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19276 per_objfile);
19277
19278 cu = per_objfile->get_cu (per_cu);
19279 if (cu == NULL || cu->partial_dies == NULL)
19280 load_partial_comp_unit (per_cu, per_objfile, nullptr);
19281
19282 cu = per_objfile->get_cu (per_cu);
19283
19284 cu->last_used = 0;
19285 pd = cu->find_partial_die (sect_off);
19286 }
19287
19288 /* If we didn't find it, and not all dies have been loaded,
19289 load them all and try again. */
19290
19291 if (pd == NULL && cu->per_cu->load_all_dies == 0)
19292 {
19293 cu->per_cu->load_all_dies = 1;
19294
19295 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19296 THIS_CU->cu may already be in use. So we can't just free it and
19297 replace its DIEs with the ones we read in. Instead, we leave those
19298 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19299 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19300 set. */
19301 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
19302
19303 pd = cu->find_partial_die (sect_off);
19304 }
19305
19306 if (pd == NULL)
19307 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
19308 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19309 return { cu, pd };
19310 }
19311
19312 /* See if we can figure out if the class lives in a namespace. We do
19313 this by looking for a member function; its demangled name will
19314 contain namespace info, if there is any. */
19315
19316 static void
19317 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19318 struct dwarf2_cu *cu)
19319 {
19320 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19321 what template types look like, because the demangler
19322 frequently doesn't give the same name as the debug info. We
19323 could fix this by only using the demangled name to get the
19324 prefix (but see comment in read_structure_type). */
19325
19326 struct partial_die_info *real_pdi;
19327 struct partial_die_info *child_pdi;
19328
19329 /* If this DIE (this DIE's specification, if any) has a parent, then
19330 we should not do this. We'll prepend the parent's fully qualified
19331 name when we create the partial symbol. */
19332
19333 real_pdi = struct_pdi;
19334 while (real_pdi->has_specification)
19335 {
19336 auto res = find_partial_die (real_pdi->spec_offset,
19337 real_pdi->spec_is_dwz, cu);
19338 real_pdi = res.pdi;
19339 cu = res.cu;
19340 }
19341
19342 if (real_pdi->die_parent != NULL)
19343 return;
19344
19345 for (child_pdi = struct_pdi->die_child;
19346 child_pdi != NULL;
19347 child_pdi = child_pdi->die_sibling)
19348 {
19349 if (child_pdi->tag == DW_TAG_subprogram
19350 && child_pdi->linkage_name != NULL)
19351 {
19352 gdb::unique_xmalloc_ptr<char> actual_class_name
19353 (cu->language_defn->class_name_from_physname
19354 (child_pdi->linkage_name));
19355 if (actual_class_name != NULL)
19356 {
19357 struct objfile *objfile = cu->per_objfile->objfile;
19358 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19359 struct_pdi->canonical_name = 1;
19360 }
19361 break;
19362 }
19363 }
19364 }
19365
19366 /* Return true if a DIE with TAG may have the DW_AT_const_value
19367 attribute. */
19368
19369 static bool
19370 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19371 {
19372 switch (tag)
19373 {
19374 case DW_TAG_constant:
19375 case DW_TAG_enumerator:
19376 case DW_TAG_formal_parameter:
19377 case DW_TAG_template_value_param:
19378 case DW_TAG_variable:
19379 return true;
19380 }
19381
19382 return false;
19383 }
19384
19385 void
19386 partial_die_info::fixup (struct dwarf2_cu *cu)
19387 {
19388 /* Once we've fixed up a die, there's no point in doing so again.
19389 This also avoids a memory leak if we were to call
19390 guess_partial_die_structure_name multiple times. */
19391 if (fixup_called)
19392 return;
19393
19394 /* If we found a reference attribute and the DIE has no name, try
19395 to find a name in the referred to DIE. */
19396
19397 if (raw_name == NULL && has_specification)
19398 {
19399 struct partial_die_info *spec_die;
19400
19401 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19402 spec_die = res.pdi;
19403 cu = res.cu;
19404
19405 spec_die->fixup (cu);
19406
19407 if (spec_die->raw_name)
19408 {
19409 raw_name = spec_die->raw_name;
19410 canonical_name = spec_die->canonical_name;
19411
19412 /* Copy DW_AT_external attribute if it is set. */
19413 if (spec_die->is_external)
19414 is_external = spec_die->is_external;
19415 }
19416 }
19417
19418 if (!has_const_value && has_specification
19419 && can_have_DW_AT_const_value_p (tag))
19420 {
19421 struct partial_die_info *spec_die;
19422
19423 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19424 spec_die = res.pdi;
19425 cu = res.cu;
19426
19427 spec_die->fixup (cu);
19428
19429 if (spec_die->has_const_value)
19430 {
19431 /* Copy DW_AT_const_value attribute if it is set. */
19432 has_const_value = spec_die->has_const_value;
19433 }
19434 }
19435
19436 /* Set default names for some unnamed DIEs. */
19437
19438 if (raw_name == NULL && tag == DW_TAG_namespace)
19439 {
19440 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
19441 canonical_name = 1;
19442 }
19443
19444 /* If there is no parent die to provide a namespace, and there are
19445 children, see if we can determine the namespace from their linkage
19446 name. */
19447 if (cu->language == language_cplus
19448 && !cu->per_objfile->per_bfd->types.empty ()
19449 && die_parent == NULL
19450 && has_children
19451 && (tag == DW_TAG_class_type
19452 || tag == DW_TAG_structure_type
19453 || tag == DW_TAG_union_type))
19454 guess_partial_die_structure_name (this, cu);
19455
19456 /* GCC might emit a nameless struct or union that has a linkage
19457 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19458 if (raw_name == NULL
19459 && (tag == DW_TAG_class_type
19460 || tag == DW_TAG_interface_type
19461 || tag == DW_TAG_structure_type
19462 || tag == DW_TAG_union_type)
19463 && linkage_name != NULL)
19464 {
19465 gdb::unique_xmalloc_ptr<char> demangled
19466 (gdb_demangle (linkage_name, DMGL_TYPES));
19467 if (demangled != nullptr)
19468 {
19469 const char *base;
19470
19471 /* Strip any leading namespaces/classes, keep only the base name.
19472 DW_AT_name for named DIEs does not contain the prefixes. */
19473 base = strrchr (demangled.get (), ':');
19474 if (base && base > demangled.get () && base[-1] == ':')
19475 base++;
19476 else
19477 base = demangled.get ();
19478
19479 struct objfile *objfile = cu->per_objfile->objfile;
19480 raw_name = objfile->intern (base);
19481 canonical_name = 1;
19482 }
19483 }
19484
19485 fixup_called = 1;
19486 }
19487
19488 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
19489 contents from the given SECTION in the HEADER. */
19490 static void
19491 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
19492 struct dwarf2_section_info *section)
19493 {
19494 unsigned int bytes_read;
19495 bfd *abfd = section->get_bfd_owner ();
19496 const gdb_byte *info_ptr = section->buffer;
19497 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19498 info_ptr += bytes_read;
19499 header->version = read_2_bytes (abfd, info_ptr);
19500 info_ptr += 2;
19501 header->addr_size = read_1_byte (abfd, info_ptr);
19502 info_ptr += 1;
19503 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19504 info_ptr += 1;
19505 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19506 }
19507
19508 /* Return the DW_AT_loclists_base value for the CU. */
19509 static ULONGEST
19510 lookup_loclist_base (struct dwarf2_cu *cu)
19511 {
19512 /* For the .dwo unit, the loclist_base points to the first offset following
19513 the header. The header consists of the following entities-
19514 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19515 bit format)
19516 2. version (2 bytes)
19517 3. address size (1 byte)
19518 4. segment selector size (1 byte)
19519 5. offset entry count (4 bytes)
19520 These sizes are derived as per the DWARFv5 standard. */
19521 if (cu->dwo_unit != nullptr)
19522 {
19523 if (cu->header.initial_length_size == 4)
19524 return LOCLIST_HEADER_SIZE32;
19525 return LOCLIST_HEADER_SIZE64;
19526 }
19527 return cu->loclist_base;
19528 }
19529
19530 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19531 array of offsets in the .debug_loclists section. */
19532 static CORE_ADDR
19533 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19534 {
19535 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19536 struct objfile *objfile = per_objfile->objfile;
19537 bfd *abfd = objfile->obfd;
19538 ULONGEST loclist_base = lookup_loclist_base (cu);
19539 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19540
19541 section->read (objfile);
19542 if (section->buffer == NULL)
19543 complaint (_("DW_FORM_loclistx used without .debug_loclists "
19544 "section [in module %s]"), objfile_name (objfile));
19545 struct loclists_rnglists_header header;
19546 read_loclists_rnglists_header (&header, section);
19547 if (loclist_index >= header.offset_entry_count)
19548 complaint (_("DW_FORM_loclistx pointing outside of "
19549 ".debug_loclists offset array [in module %s]"),
19550 objfile_name (objfile));
19551 if (loclist_base + loclist_index * cu->header.offset_size
19552 >= section->size)
19553 complaint (_("DW_FORM_loclistx pointing outside of "
19554 ".debug_loclists section [in module %s]"),
19555 objfile_name (objfile));
19556 const gdb_byte *info_ptr
19557 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
19558
19559 if (cu->header.offset_size == 4)
19560 return bfd_get_32 (abfd, info_ptr) + loclist_base;
19561 else
19562 return bfd_get_64 (abfd, info_ptr) + loclist_base;
19563 }
19564
19565 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
19566 array of offsets in the .debug_rnglists section. */
19567 static CORE_ADDR
19568 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
19569 dwarf_tag tag)
19570 {
19571 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19572 struct objfile *objfile = dwarf2_per_objfile->objfile;
19573 bfd *abfd = objfile->obfd;
19574 ULONGEST rnglist_header_size =
19575 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
19576 : RNGLIST_HEADER_SIZE64);
19577 ULONGEST rnglist_base =
19578 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->ranges_base;
19579 ULONGEST start_offset =
19580 rnglist_base + rnglist_index * cu->header.offset_size;
19581
19582 /* Get rnglists section. */
19583 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
19584
19585 /* Read the rnglists section content. */
19586 section->read (objfile);
19587 if (section->buffer == nullptr)
19588 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
19589 "[in module %s]"),
19590 objfile_name (objfile));
19591
19592 /* Verify the rnglist index is valid. */
19593 struct loclists_rnglists_header header;
19594 read_loclists_rnglists_header (&header, section);
19595 if (rnglist_index >= header.offset_entry_count)
19596 error (_("DW_FORM_rnglistx index pointing outside of "
19597 ".debug_rnglists offset array [in module %s]"),
19598 objfile_name (objfile));
19599
19600 /* Validate that the offset is within the section's range. */
19601 if (start_offset >= section->size)
19602 error (_("DW_FORM_rnglistx pointing outside of "
19603 ".debug_rnglists section [in module %s]"),
19604 objfile_name (objfile));
19605
19606 /* Validate that reading won't go beyond the end of the section. */
19607 if (start_offset + cu->header.offset_size > rnglist_base + section->size)
19608 error (_("Reading DW_FORM_rnglistx index beyond end of"
19609 ".debug_rnglists section [in module %s]"),
19610 objfile_name (objfile));
19611
19612 const gdb_byte *info_ptr = section->buffer + start_offset;
19613
19614 if (cu->header.offset_size == 4)
19615 return read_4_bytes (abfd, info_ptr) + rnglist_base;
19616 else
19617 return read_8_bytes (abfd, info_ptr) + rnglist_base;
19618 }
19619
19620 /* Process the attributes that had to be skipped in the first round. These
19621 attributes are the ones that need str_offsets_base or addr_base attributes.
19622 They could not have been processed in the first round, because at the time
19623 the values of str_offsets_base or addr_base may not have been known. */
19624 static void
19625 read_attribute_reprocess (const struct die_reader_specs *reader,
19626 struct attribute *attr, dwarf_tag tag)
19627 {
19628 struct dwarf2_cu *cu = reader->cu;
19629 switch (attr->form)
19630 {
19631 case DW_FORM_addrx:
19632 case DW_FORM_GNU_addr_index:
19633 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
19634 break;
19635 case DW_FORM_loclistx:
19636 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
19637 break;
19638 case DW_FORM_rnglistx:
19639 DW_UNSND (attr) = read_rnglist_index (cu, DW_UNSND (attr), tag);
19640 break;
19641 case DW_FORM_strx:
19642 case DW_FORM_strx1:
19643 case DW_FORM_strx2:
19644 case DW_FORM_strx3:
19645 case DW_FORM_strx4:
19646 case DW_FORM_GNU_str_index:
19647 {
19648 unsigned int str_index = DW_UNSND (attr);
19649 if (reader->dwo_file != NULL)
19650 {
19651 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19652 DW_STRING_IS_CANONICAL (attr) = 0;
19653 }
19654 else
19655 {
19656 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19657 DW_STRING_IS_CANONICAL (attr) = 0;
19658 }
19659 break;
19660 }
19661 default:
19662 gdb_assert_not_reached (_("Unexpected DWARF form."));
19663 }
19664 }
19665
19666 /* Read an attribute value described by an attribute form. */
19667
19668 static const gdb_byte *
19669 read_attribute_value (const struct die_reader_specs *reader,
19670 struct attribute *attr, unsigned form,
19671 LONGEST implicit_const, const gdb_byte *info_ptr,
19672 bool *need_reprocess)
19673 {
19674 struct dwarf2_cu *cu = reader->cu;
19675 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19676 struct objfile *objfile = per_objfile->objfile;
19677 bfd *abfd = reader->abfd;
19678 struct comp_unit_head *cu_header = &cu->header;
19679 unsigned int bytes_read;
19680 struct dwarf_block *blk;
19681 *need_reprocess = false;
19682
19683 attr->form = (enum dwarf_form) form;
19684 switch (form)
19685 {
19686 case DW_FORM_ref_addr:
19687 if (cu->header.version == 2)
19688 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19689 &bytes_read);
19690 else
19691 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19692 &bytes_read);
19693 info_ptr += bytes_read;
19694 break;
19695 case DW_FORM_GNU_ref_alt:
19696 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19697 info_ptr += bytes_read;
19698 break;
19699 case DW_FORM_addr:
19700 {
19701 struct gdbarch *gdbarch = objfile->arch ();
19702 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19703 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19704 info_ptr += bytes_read;
19705 }
19706 break;
19707 case DW_FORM_block2:
19708 blk = dwarf_alloc_block (cu);
19709 blk->size = read_2_bytes (abfd, info_ptr);
19710 info_ptr += 2;
19711 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19712 info_ptr += blk->size;
19713 DW_BLOCK (attr) = blk;
19714 break;
19715 case DW_FORM_block4:
19716 blk = dwarf_alloc_block (cu);
19717 blk->size = read_4_bytes (abfd, info_ptr);
19718 info_ptr += 4;
19719 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19720 info_ptr += blk->size;
19721 DW_BLOCK (attr) = blk;
19722 break;
19723 case DW_FORM_data2:
19724 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19725 info_ptr += 2;
19726 break;
19727 case DW_FORM_data4:
19728 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19729 info_ptr += 4;
19730 break;
19731 case DW_FORM_data8:
19732 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19733 info_ptr += 8;
19734 break;
19735 case DW_FORM_data16:
19736 blk = dwarf_alloc_block (cu);
19737 blk->size = 16;
19738 blk->data = read_n_bytes (abfd, info_ptr, 16);
19739 info_ptr += 16;
19740 DW_BLOCK (attr) = blk;
19741 break;
19742 case DW_FORM_sec_offset:
19743 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19744 info_ptr += bytes_read;
19745 break;
19746 case DW_FORM_loclistx:
19747 {
19748 *need_reprocess = true;
19749 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19750 info_ptr += bytes_read;
19751 }
19752 break;
19753 case DW_FORM_string:
19754 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19755 DW_STRING_IS_CANONICAL (attr) = 0;
19756 info_ptr += bytes_read;
19757 break;
19758 case DW_FORM_strp:
19759 if (!cu->per_cu->is_dwz)
19760 {
19761 DW_STRING (attr) = read_indirect_string (per_objfile,
19762 abfd, info_ptr, cu_header,
19763 &bytes_read);
19764 DW_STRING_IS_CANONICAL (attr) = 0;
19765 info_ptr += bytes_read;
19766 break;
19767 }
19768 /* FALLTHROUGH */
19769 case DW_FORM_line_strp:
19770 if (!cu->per_cu->is_dwz)
19771 {
19772 DW_STRING (attr) = per_objfile->read_line_string (info_ptr, cu_header,
19773 &bytes_read);
19774 DW_STRING_IS_CANONICAL (attr) = 0;
19775 info_ptr += bytes_read;
19776 break;
19777 }
19778 /* FALLTHROUGH */
19779 case DW_FORM_GNU_strp_alt:
19780 {
19781 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
19782 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19783 &bytes_read);
19784
19785 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
19786 DW_STRING_IS_CANONICAL (attr) = 0;
19787 info_ptr += bytes_read;
19788 }
19789 break;
19790 case DW_FORM_exprloc:
19791 case DW_FORM_block:
19792 blk = dwarf_alloc_block (cu);
19793 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19794 info_ptr += bytes_read;
19795 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19796 info_ptr += blk->size;
19797 DW_BLOCK (attr) = blk;
19798 break;
19799 case DW_FORM_block1:
19800 blk = dwarf_alloc_block (cu);
19801 blk->size = read_1_byte (abfd, info_ptr);
19802 info_ptr += 1;
19803 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19804 info_ptr += blk->size;
19805 DW_BLOCK (attr) = blk;
19806 break;
19807 case DW_FORM_data1:
19808 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19809 info_ptr += 1;
19810 break;
19811 case DW_FORM_flag:
19812 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19813 info_ptr += 1;
19814 break;
19815 case DW_FORM_flag_present:
19816 DW_UNSND (attr) = 1;
19817 break;
19818 case DW_FORM_sdata:
19819 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19820 info_ptr += bytes_read;
19821 break;
19822 case DW_FORM_rnglistx:
19823 *need_reprocess = true;
19824 /* FALLTHROUGH */
19825 case DW_FORM_udata:
19826 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19827 info_ptr += bytes_read;
19828 break;
19829 case DW_FORM_ref1:
19830 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19831 + read_1_byte (abfd, info_ptr));
19832 info_ptr += 1;
19833 break;
19834 case DW_FORM_ref2:
19835 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19836 + read_2_bytes (abfd, info_ptr));
19837 info_ptr += 2;
19838 break;
19839 case DW_FORM_ref4:
19840 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19841 + read_4_bytes (abfd, info_ptr));
19842 info_ptr += 4;
19843 break;
19844 case DW_FORM_ref8:
19845 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19846 + read_8_bytes (abfd, info_ptr));
19847 info_ptr += 8;
19848 break;
19849 case DW_FORM_ref_sig8:
19850 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19851 info_ptr += 8;
19852 break;
19853 case DW_FORM_ref_udata:
19854 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19855 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19856 info_ptr += bytes_read;
19857 break;
19858 case DW_FORM_indirect:
19859 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19860 info_ptr += bytes_read;
19861 if (form == DW_FORM_implicit_const)
19862 {
19863 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19864 info_ptr += bytes_read;
19865 }
19866 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19867 info_ptr, need_reprocess);
19868 break;
19869 case DW_FORM_implicit_const:
19870 DW_SND (attr) = implicit_const;
19871 break;
19872 case DW_FORM_addrx:
19873 case DW_FORM_GNU_addr_index:
19874 *need_reprocess = true;
19875 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19876 info_ptr += bytes_read;
19877 break;
19878 case DW_FORM_strx:
19879 case DW_FORM_strx1:
19880 case DW_FORM_strx2:
19881 case DW_FORM_strx3:
19882 case DW_FORM_strx4:
19883 case DW_FORM_GNU_str_index:
19884 {
19885 ULONGEST str_index;
19886 if (form == DW_FORM_strx1)
19887 {
19888 str_index = read_1_byte (abfd, info_ptr);
19889 info_ptr += 1;
19890 }
19891 else if (form == DW_FORM_strx2)
19892 {
19893 str_index = read_2_bytes (abfd, info_ptr);
19894 info_ptr += 2;
19895 }
19896 else if (form == DW_FORM_strx3)
19897 {
19898 str_index = read_3_bytes (abfd, info_ptr);
19899 info_ptr += 3;
19900 }
19901 else if (form == DW_FORM_strx4)
19902 {
19903 str_index = read_4_bytes (abfd, info_ptr);
19904 info_ptr += 4;
19905 }
19906 else
19907 {
19908 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19909 info_ptr += bytes_read;
19910 }
19911 *need_reprocess = true;
19912 DW_UNSND (attr) = str_index;
19913 }
19914 break;
19915 default:
19916 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19917 dwarf_form_name (form),
19918 bfd_get_filename (abfd));
19919 }
19920
19921 /* Super hack. */
19922 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19923 attr->form = DW_FORM_GNU_ref_alt;
19924
19925 /* We have seen instances where the compiler tried to emit a byte
19926 size attribute of -1 which ended up being encoded as an unsigned
19927 0xffffffff. Although 0xffffffff is technically a valid size value,
19928 an object of this size seems pretty unlikely so we can relatively
19929 safely treat these cases as if the size attribute was invalid and
19930 treat them as zero by default. */
19931 if (attr->name == DW_AT_byte_size
19932 && form == DW_FORM_data4
19933 && DW_UNSND (attr) >= 0xffffffff)
19934 {
19935 complaint
19936 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19937 hex_string (DW_UNSND (attr)));
19938 DW_UNSND (attr) = 0;
19939 }
19940
19941 return info_ptr;
19942 }
19943
19944 /* Read an attribute described by an abbreviated attribute. */
19945
19946 static const gdb_byte *
19947 read_attribute (const struct die_reader_specs *reader,
19948 struct attribute *attr, struct attr_abbrev *abbrev,
19949 const gdb_byte *info_ptr, bool *need_reprocess)
19950 {
19951 attr->name = abbrev->name;
19952 return read_attribute_value (reader, attr, abbrev->form,
19953 abbrev->implicit_const, info_ptr,
19954 need_reprocess);
19955 }
19956
19957 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19958
19959 static const char *
19960 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
19961 LONGEST str_offset)
19962 {
19963 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
19964 str_offset, "DW_FORM_strp");
19965 }
19966
19967 /* Return pointer to string at .debug_str offset as read from BUF.
19968 BUF is assumed to be in a compilation unit described by CU_HEADER.
19969 Return *BYTES_READ_PTR count of bytes read from BUF. */
19970
19971 static const char *
19972 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
19973 const gdb_byte *buf,
19974 const struct comp_unit_head *cu_header,
19975 unsigned int *bytes_read_ptr)
19976 {
19977 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19978
19979 return read_indirect_string_at_offset (per_objfile, str_offset);
19980 }
19981
19982 /* See read.h. */
19983
19984 const char *
19985 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19986 const struct comp_unit_head *cu_header,
19987 unsigned int *bytes_read_ptr)
19988 {
19989 bfd *abfd = objfile->obfd;
19990 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19991
19992 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19993 }
19994
19995 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19996 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19997 ADDR_SIZE is the size of addresses from the CU header. */
19998
19999 static CORE_ADDR
20000 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20001 gdb::optional<ULONGEST> addr_base, int addr_size)
20002 {
20003 struct objfile *objfile = per_objfile->objfile;
20004 bfd *abfd = objfile->obfd;
20005 const gdb_byte *info_ptr;
20006 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20007
20008 per_objfile->per_bfd->addr.read (objfile);
20009 if (per_objfile->per_bfd->addr.buffer == NULL)
20010 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20011 objfile_name (objfile));
20012 if (addr_base_or_zero + addr_index * addr_size
20013 >= per_objfile->per_bfd->addr.size)
20014 error (_("DW_FORM_addr_index pointing outside of "
20015 ".debug_addr section [in module %s]"),
20016 objfile_name (objfile));
20017 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20018 + addr_index * addr_size);
20019 if (addr_size == 4)
20020 return bfd_get_32 (abfd, info_ptr);
20021 else
20022 return bfd_get_64 (abfd, info_ptr);
20023 }
20024
20025 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20026
20027 static CORE_ADDR
20028 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20029 {
20030 return read_addr_index_1 (cu->per_objfile, addr_index,
20031 cu->addr_base, cu->header.addr_size);
20032 }
20033
20034 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20035
20036 static CORE_ADDR
20037 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20038 unsigned int *bytes_read)
20039 {
20040 bfd *abfd = cu->per_objfile->objfile->obfd;
20041 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20042
20043 return read_addr_index (cu, addr_index);
20044 }
20045
20046 /* See read.h. */
20047
20048 CORE_ADDR
20049 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20050 dwarf2_per_objfile *per_objfile,
20051 unsigned int addr_index)
20052 {
20053 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20054 gdb::optional<ULONGEST> addr_base;
20055 int addr_size;
20056
20057 /* We need addr_base and addr_size.
20058 If we don't have PER_CU->cu, we have to get it.
20059 Nasty, but the alternative is storing the needed info in PER_CU,
20060 which at this point doesn't seem justified: it's not clear how frequently
20061 it would get used and it would increase the size of every PER_CU.
20062 Entry points like dwarf2_per_cu_addr_size do a similar thing
20063 so we're not in uncharted territory here.
20064 Alas we need to be a bit more complicated as addr_base is contained
20065 in the DIE.
20066
20067 We don't need to read the entire CU(/TU).
20068 We just need the header and top level die.
20069
20070 IWBN to use the aging mechanism to let us lazily later discard the CU.
20071 For now we skip this optimization. */
20072
20073 if (cu != NULL)
20074 {
20075 addr_base = cu->addr_base;
20076 addr_size = cu->header.addr_size;
20077 }
20078 else
20079 {
20080 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
20081 addr_base = reader.cu->addr_base;
20082 addr_size = reader.cu->header.addr_size;
20083 }
20084
20085 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
20086 }
20087
20088 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20089 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20090 DWO file. */
20091
20092 static const char *
20093 read_str_index (struct dwarf2_cu *cu,
20094 struct dwarf2_section_info *str_section,
20095 struct dwarf2_section_info *str_offsets_section,
20096 ULONGEST str_offsets_base, ULONGEST str_index)
20097 {
20098 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20099 struct objfile *objfile = per_objfile->objfile;
20100 const char *objf_name = objfile_name (objfile);
20101 bfd *abfd = objfile->obfd;
20102 const gdb_byte *info_ptr;
20103 ULONGEST str_offset;
20104 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20105
20106 str_section->read (objfile);
20107 str_offsets_section->read (objfile);
20108 if (str_section->buffer == NULL)
20109 error (_("%s used without %s section"
20110 " in CU at offset %s [in module %s]"),
20111 form_name, str_section->get_name (),
20112 sect_offset_str (cu->header.sect_off), objf_name);
20113 if (str_offsets_section->buffer == NULL)
20114 error (_("%s used without %s section"
20115 " in CU at offset %s [in module %s]"),
20116 form_name, str_section->get_name (),
20117 sect_offset_str (cu->header.sect_off), objf_name);
20118 info_ptr = (str_offsets_section->buffer
20119 + str_offsets_base
20120 + str_index * cu->header.offset_size);
20121 if (cu->header.offset_size == 4)
20122 str_offset = bfd_get_32 (abfd, info_ptr);
20123 else
20124 str_offset = bfd_get_64 (abfd, info_ptr);
20125 if (str_offset >= str_section->size)
20126 error (_("Offset from %s pointing outside of"
20127 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20128 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20129 return (const char *) (str_section->buffer + str_offset);
20130 }
20131
20132 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20133
20134 static const char *
20135 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20136 {
20137 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20138 ? reader->cu->header.addr_size : 0;
20139 return read_str_index (reader->cu,
20140 &reader->dwo_file->sections.str,
20141 &reader->dwo_file->sections.str_offsets,
20142 str_offsets_base, str_index);
20143 }
20144
20145 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20146
20147 static const char *
20148 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20149 {
20150 struct objfile *objfile = cu->per_objfile->objfile;
20151 const char *objf_name = objfile_name (objfile);
20152 static const char form_name[] = "DW_FORM_GNU_str_index";
20153 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20154
20155 if (!cu->str_offsets_base.has_value ())
20156 error (_("%s used in Fission stub without %s"
20157 " in CU at offset 0x%lx [in module %s]"),
20158 form_name, str_offsets_attr_name,
20159 (long) cu->header.offset_size, objf_name);
20160
20161 return read_str_index (cu,
20162 &cu->per_objfile->per_bfd->str,
20163 &cu->per_objfile->per_bfd->str_offsets,
20164 *cu->str_offsets_base, str_index);
20165 }
20166
20167 /* Return the length of an LEB128 number in BUF. */
20168
20169 static int
20170 leb128_size (const gdb_byte *buf)
20171 {
20172 const gdb_byte *begin = buf;
20173 gdb_byte byte;
20174
20175 while (1)
20176 {
20177 byte = *buf++;
20178 if ((byte & 128) == 0)
20179 return buf - begin;
20180 }
20181 }
20182
20183 static void
20184 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20185 {
20186 switch (lang)
20187 {
20188 case DW_LANG_C89:
20189 case DW_LANG_C99:
20190 case DW_LANG_C11:
20191 case DW_LANG_C:
20192 case DW_LANG_UPC:
20193 cu->language = language_c;
20194 break;
20195 case DW_LANG_Java:
20196 case DW_LANG_C_plus_plus:
20197 case DW_LANG_C_plus_plus_11:
20198 case DW_LANG_C_plus_plus_14:
20199 cu->language = language_cplus;
20200 break;
20201 case DW_LANG_D:
20202 cu->language = language_d;
20203 break;
20204 case DW_LANG_Fortran77:
20205 case DW_LANG_Fortran90:
20206 case DW_LANG_Fortran95:
20207 case DW_LANG_Fortran03:
20208 case DW_LANG_Fortran08:
20209 cu->language = language_fortran;
20210 break;
20211 case DW_LANG_Go:
20212 cu->language = language_go;
20213 break;
20214 case DW_LANG_Mips_Assembler:
20215 cu->language = language_asm;
20216 break;
20217 case DW_LANG_Ada83:
20218 case DW_LANG_Ada95:
20219 cu->language = language_ada;
20220 break;
20221 case DW_LANG_Modula2:
20222 cu->language = language_m2;
20223 break;
20224 case DW_LANG_Pascal83:
20225 cu->language = language_pascal;
20226 break;
20227 case DW_LANG_ObjC:
20228 cu->language = language_objc;
20229 break;
20230 case DW_LANG_Rust:
20231 case DW_LANG_Rust_old:
20232 cu->language = language_rust;
20233 break;
20234 case DW_LANG_Cobol74:
20235 case DW_LANG_Cobol85:
20236 default:
20237 cu->language = language_minimal;
20238 break;
20239 }
20240 cu->language_defn = language_def (cu->language);
20241 }
20242
20243 /* Return the named attribute or NULL if not there. */
20244
20245 static struct attribute *
20246 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20247 {
20248 for (;;)
20249 {
20250 unsigned int i;
20251 struct attribute *spec = NULL;
20252
20253 for (i = 0; i < die->num_attrs; ++i)
20254 {
20255 if (die->attrs[i].name == name)
20256 return &die->attrs[i];
20257 if (die->attrs[i].name == DW_AT_specification
20258 || die->attrs[i].name == DW_AT_abstract_origin)
20259 spec = &die->attrs[i];
20260 }
20261
20262 if (!spec)
20263 break;
20264
20265 die = follow_die_ref (die, spec, &cu);
20266 }
20267
20268 return NULL;
20269 }
20270
20271 /* Return the string associated with a string-typed attribute, or NULL if it
20272 is either not found or is of an incorrect type. */
20273
20274 static const char *
20275 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20276 {
20277 struct attribute *attr;
20278 const char *str = NULL;
20279
20280 attr = dwarf2_attr (die, name, cu);
20281
20282 if (attr != NULL)
20283 {
20284 str = attr->value_as_string ();
20285 if (str == nullptr)
20286 complaint (_("string type expected for attribute %s for "
20287 "DIE at %s in module %s"),
20288 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20289 objfile_name (cu->per_objfile->objfile));
20290 }
20291
20292 return str;
20293 }
20294
20295 /* Return the dwo name or NULL if not present. If present, it is in either
20296 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20297 static const char *
20298 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20299 {
20300 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20301 if (dwo_name == nullptr)
20302 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20303 return dwo_name;
20304 }
20305
20306 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20307 and holds a non-zero value. This function should only be used for
20308 DW_FORM_flag or DW_FORM_flag_present attributes. */
20309
20310 static int
20311 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20312 {
20313 struct attribute *attr = dwarf2_attr (die, name, cu);
20314
20315 return (attr && DW_UNSND (attr));
20316 }
20317
20318 static int
20319 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20320 {
20321 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20322 which value is non-zero. However, we have to be careful with
20323 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20324 (via dwarf2_flag_true_p) follows this attribute. So we may
20325 end up accidently finding a declaration attribute that belongs
20326 to a different DIE referenced by the specification attribute,
20327 even though the given DIE does not have a declaration attribute. */
20328 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20329 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20330 }
20331
20332 /* Return the die giving the specification for DIE, if there is
20333 one. *SPEC_CU is the CU containing DIE on input, and the CU
20334 containing the return value on output. If there is no
20335 specification, but there is an abstract origin, that is
20336 returned. */
20337
20338 static struct die_info *
20339 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20340 {
20341 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20342 *spec_cu);
20343
20344 if (spec_attr == NULL)
20345 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20346
20347 if (spec_attr == NULL)
20348 return NULL;
20349 else
20350 return follow_die_ref (die, spec_attr, spec_cu);
20351 }
20352
20353 /* Stub for free_line_header to match void * callback types. */
20354
20355 static void
20356 free_line_header_voidp (void *arg)
20357 {
20358 struct line_header *lh = (struct line_header *) arg;
20359
20360 delete lh;
20361 }
20362
20363 /* A convenience function to find the proper .debug_line section for a CU. */
20364
20365 static struct dwarf2_section_info *
20366 get_debug_line_section (struct dwarf2_cu *cu)
20367 {
20368 struct dwarf2_section_info *section;
20369 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20370
20371 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20372 DWO file. */
20373 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20374 section = &cu->dwo_unit->dwo_file->sections.line;
20375 else if (cu->per_cu->is_dwz)
20376 {
20377 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
20378
20379 section = &dwz->line;
20380 }
20381 else
20382 section = &per_objfile->per_bfd->line;
20383
20384 return section;
20385 }
20386
20387 /* Read the statement program header starting at OFFSET in
20388 .debug_line, or .debug_line.dwo. Return a pointer
20389 to a struct line_header, allocated using xmalloc.
20390 Returns NULL if there is a problem reading the header, e.g., if it
20391 has a version we don't understand.
20392
20393 NOTE: the strings in the include directory and file name tables of
20394 the returned object point into the dwarf line section buffer,
20395 and must not be freed. */
20396
20397 static line_header_up
20398 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20399 {
20400 struct dwarf2_section_info *section;
20401 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20402
20403 section = get_debug_line_section (cu);
20404 section->read (per_objfile->objfile);
20405 if (section->buffer == NULL)
20406 {
20407 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20408 complaint (_("missing .debug_line.dwo section"));
20409 else
20410 complaint (_("missing .debug_line section"));
20411 return 0;
20412 }
20413
20414 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
20415 per_objfile, section, &cu->header);
20416 }
20417
20418 /* Subroutine of dwarf_decode_lines to simplify it.
20419 Return the file name of the psymtab for the given file_entry.
20420 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20421 If space for the result is malloc'd, *NAME_HOLDER will be set.
20422 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20423
20424 static const char *
20425 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
20426 const dwarf2_psymtab *pst,
20427 const char *comp_dir,
20428 gdb::unique_xmalloc_ptr<char> *name_holder)
20429 {
20430 const char *include_name = fe.name;
20431 const char *include_name_to_compare = include_name;
20432 const char *pst_filename;
20433 int file_is_pst;
20434
20435 const char *dir_name = fe.include_dir (lh);
20436
20437 gdb::unique_xmalloc_ptr<char> hold_compare;
20438 if (!IS_ABSOLUTE_PATH (include_name)
20439 && (dir_name != NULL || comp_dir != NULL))
20440 {
20441 /* Avoid creating a duplicate psymtab for PST.
20442 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20443 Before we do the comparison, however, we need to account
20444 for DIR_NAME and COMP_DIR.
20445 First prepend dir_name (if non-NULL). If we still don't
20446 have an absolute path prepend comp_dir (if non-NULL).
20447 However, the directory we record in the include-file's
20448 psymtab does not contain COMP_DIR (to match the
20449 corresponding symtab(s)).
20450
20451 Example:
20452
20453 bash$ cd /tmp
20454 bash$ gcc -g ./hello.c
20455 include_name = "hello.c"
20456 dir_name = "."
20457 DW_AT_comp_dir = comp_dir = "/tmp"
20458 DW_AT_name = "./hello.c"
20459
20460 */
20461
20462 if (dir_name != NULL)
20463 {
20464 name_holder->reset (concat (dir_name, SLASH_STRING,
20465 include_name, (char *) NULL));
20466 include_name = name_holder->get ();
20467 include_name_to_compare = include_name;
20468 }
20469 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20470 {
20471 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20472 include_name, (char *) NULL));
20473 include_name_to_compare = hold_compare.get ();
20474 }
20475 }
20476
20477 pst_filename = pst->filename;
20478 gdb::unique_xmalloc_ptr<char> copied_name;
20479 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20480 {
20481 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20482 pst_filename, (char *) NULL));
20483 pst_filename = copied_name.get ();
20484 }
20485
20486 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20487
20488 if (file_is_pst)
20489 return NULL;
20490 return include_name;
20491 }
20492
20493 /* State machine to track the state of the line number program. */
20494
20495 class lnp_state_machine
20496 {
20497 public:
20498 /* Initialize a machine state for the start of a line number
20499 program. */
20500 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20501 bool record_lines_p);
20502
20503 file_entry *current_file ()
20504 {
20505 /* lh->file_names is 0-based, but the file name numbers in the
20506 statement program are 1-based. */
20507 return m_line_header->file_name_at (m_file);
20508 }
20509
20510 /* Record the line in the state machine. END_SEQUENCE is true if
20511 we're processing the end of a sequence. */
20512 void record_line (bool end_sequence);
20513
20514 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
20515 nop-out rest of the lines in this sequence. */
20516 void check_line_address (struct dwarf2_cu *cu,
20517 const gdb_byte *line_ptr,
20518 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20519
20520 void handle_set_discriminator (unsigned int discriminator)
20521 {
20522 m_discriminator = discriminator;
20523 m_line_has_non_zero_discriminator |= discriminator != 0;
20524 }
20525
20526 /* Handle DW_LNE_set_address. */
20527 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20528 {
20529 m_op_index = 0;
20530 address += baseaddr;
20531 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20532 }
20533
20534 /* Handle DW_LNS_advance_pc. */
20535 void handle_advance_pc (CORE_ADDR adjust);
20536
20537 /* Handle a special opcode. */
20538 void handle_special_opcode (unsigned char op_code);
20539
20540 /* Handle DW_LNS_advance_line. */
20541 void handle_advance_line (int line_delta)
20542 {
20543 advance_line (line_delta);
20544 }
20545
20546 /* Handle DW_LNS_set_file. */
20547 void handle_set_file (file_name_index file);
20548
20549 /* Handle DW_LNS_negate_stmt. */
20550 void handle_negate_stmt ()
20551 {
20552 m_is_stmt = !m_is_stmt;
20553 }
20554
20555 /* Handle DW_LNS_const_add_pc. */
20556 void handle_const_add_pc ();
20557
20558 /* Handle DW_LNS_fixed_advance_pc. */
20559 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20560 {
20561 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20562 m_op_index = 0;
20563 }
20564
20565 /* Handle DW_LNS_copy. */
20566 void handle_copy ()
20567 {
20568 record_line (false);
20569 m_discriminator = 0;
20570 }
20571
20572 /* Handle DW_LNE_end_sequence. */
20573 void handle_end_sequence ()
20574 {
20575 m_currently_recording_lines = true;
20576 }
20577
20578 private:
20579 /* Advance the line by LINE_DELTA. */
20580 void advance_line (int line_delta)
20581 {
20582 m_line += line_delta;
20583
20584 if (line_delta != 0)
20585 m_line_has_non_zero_discriminator = m_discriminator != 0;
20586 }
20587
20588 struct dwarf2_cu *m_cu;
20589
20590 gdbarch *m_gdbarch;
20591
20592 /* True if we're recording lines.
20593 Otherwise we're building partial symtabs and are just interested in
20594 finding include files mentioned by the line number program. */
20595 bool m_record_lines_p;
20596
20597 /* The line number header. */
20598 line_header *m_line_header;
20599
20600 /* These are part of the standard DWARF line number state machine,
20601 and initialized according to the DWARF spec. */
20602
20603 unsigned char m_op_index = 0;
20604 /* The line table index of the current file. */
20605 file_name_index m_file = 1;
20606 unsigned int m_line = 1;
20607
20608 /* These are initialized in the constructor. */
20609
20610 CORE_ADDR m_address;
20611 bool m_is_stmt;
20612 unsigned int m_discriminator;
20613
20614 /* Additional bits of state we need to track. */
20615
20616 /* The last file that we called dwarf2_start_subfile for.
20617 This is only used for TLLs. */
20618 unsigned int m_last_file = 0;
20619 /* The last file a line number was recorded for. */
20620 struct subfile *m_last_subfile = NULL;
20621
20622 /* The address of the last line entry. */
20623 CORE_ADDR m_last_address;
20624
20625 /* Set to true when a previous line at the same address (using
20626 m_last_address) had m_is_stmt true. This is reset to false when a
20627 line entry at a new address (m_address different to m_last_address) is
20628 processed. */
20629 bool m_stmt_at_address = false;
20630
20631 /* When true, record the lines we decode. */
20632 bool m_currently_recording_lines = false;
20633
20634 /* The last line number that was recorded, used to coalesce
20635 consecutive entries for the same line. This can happen, for
20636 example, when discriminators are present. PR 17276. */
20637 unsigned int m_last_line = 0;
20638 bool m_line_has_non_zero_discriminator = false;
20639 };
20640
20641 void
20642 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20643 {
20644 CORE_ADDR addr_adj = (((m_op_index + adjust)
20645 / m_line_header->maximum_ops_per_instruction)
20646 * m_line_header->minimum_instruction_length);
20647 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20648 m_op_index = ((m_op_index + adjust)
20649 % m_line_header->maximum_ops_per_instruction);
20650 }
20651
20652 void
20653 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20654 {
20655 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20656 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20657 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20658 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
20659 / m_line_header->maximum_ops_per_instruction)
20660 * m_line_header->minimum_instruction_length);
20661 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20662 m_op_index = ((m_op_index + adj_opcode_d)
20663 % m_line_header->maximum_ops_per_instruction);
20664
20665 int line_delta = m_line_header->line_base + adj_opcode_r;
20666 advance_line (line_delta);
20667 record_line (false);
20668 m_discriminator = 0;
20669 }
20670
20671 void
20672 lnp_state_machine::handle_set_file (file_name_index file)
20673 {
20674 m_file = file;
20675
20676 const file_entry *fe = current_file ();
20677 if (fe == NULL)
20678 dwarf2_debug_line_missing_file_complaint ();
20679 else if (m_record_lines_p)
20680 {
20681 const char *dir = fe->include_dir (m_line_header);
20682
20683 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20684 m_line_has_non_zero_discriminator = m_discriminator != 0;
20685 dwarf2_start_subfile (m_cu, fe->name, dir);
20686 }
20687 }
20688
20689 void
20690 lnp_state_machine::handle_const_add_pc ()
20691 {
20692 CORE_ADDR adjust
20693 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20694
20695 CORE_ADDR addr_adj
20696 = (((m_op_index + adjust)
20697 / m_line_header->maximum_ops_per_instruction)
20698 * m_line_header->minimum_instruction_length);
20699
20700 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20701 m_op_index = ((m_op_index + adjust)
20702 % m_line_header->maximum_ops_per_instruction);
20703 }
20704
20705 /* Return non-zero if we should add LINE to the line number table.
20706 LINE is the line to add, LAST_LINE is the last line that was added,
20707 LAST_SUBFILE is the subfile for LAST_LINE.
20708 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20709 had a non-zero discriminator.
20710
20711 We have to be careful in the presence of discriminators.
20712 E.g., for this line:
20713
20714 for (i = 0; i < 100000; i++);
20715
20716 clang can emit four line number entries for that one line,
20717 each with a different discriminator.
20718 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20719
20720 However, we want gdb to coalesce all four entries into one.
20721 Otherwise the user could stepi into the middle of the line and
20722 gdb would get confused about whether the pc really was in the
20723 middle of the line.
20724
20725 Things are further complicated by the fact that two consecutive
20726 line number entries for the same line is a heuristic used by gcc
20727 to denote the end of the prologue. So we can't just discard duplicate
20728 entries, we have to be selective about it. The heuristic we use is
20729 that we only collapse consecutive entries for the same line if at least
20730 one of those entries has a non-zero discriminator. PR 17276.
20731
20732 Note: Addresses in the line number state machine can never go backwards
20733 within one sequence, thus this coalescing is ok. */
20734
20735 static int
20736 dwarf_record_line_p (struct dwarf2_cu *cu,
20737 unsigned int line, unsigned int last_line,
20738 int line_has_non_zero_discriminator,
20739 struct subfile *last_subfile)
20740 {
20741 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20742 return 1;
20743 if (line != last_line)
20744 return 1;
20745 /* Same line for the same file that we've seen already.
20746 As a last check, for pr 17276, only record the line if the line
20747 has never had a non-zero discriminator. */
20748 if (!line_has_non_zero_discriminator)
20749 return 1;
20750 return 0;
20751 }
20752
20753 /* Use the CU's builder to record line number LINE beginning at
20754 address ADDRESS in the line table of subfile SUBFILE. */
20755
20756 static void
20757 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20758 unsigned int line, CORE_ADDR address, bool is_stmt,
20759 struct dwarf2_cu *cu)
20760 {
20761 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20762
20763 if (dwarf_line_debug)
20764 {
20765 fprintf_unfiltered (gdb_stdlog,
20766 "Recording line %u, file %s, address %s\n",
20767 line, lbasename (subfile->name),
20768 paddress (gdbarch, address));
20769 }
20770
20771 if (cu != nullptr)
20772 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
20773 }
20774
20775 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20776 Mark the end of a set of line number records.
20777 The arguments are the same as for dwarf_record_line_1.
20778 If SUBFILE is NULL the request is ignored. */
20779
20780 static void
20781 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20782 CORE_ADDR address, struct dwarf2_cu *cu)
20783 {
20784 if (subfile == NULL)
20785 return;
20786
20787 if (dwarf_line_debug)
20788 {
20789 fprintf_unfiltered (gdb_stdlog,
20790 "Finishing current line, file %s, address %s\n",
20791 lbasename (subfile->name),
20792 paddress (gdbarch, address));
20793 }
20794
20795 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
20796 }
20797
20798 void
20799 lnp_state_machine::record_line (bool end_sequence)
20800 {
20801 if (dwarf_line_debug)
20802 {
20803 fprintf_unfiltered (gdb_stdlog,
20804 "Processing actual line %u: file %u,"
20805 " address %s, is_stmt %u, discrim %u%s\n",
20806 m_line, m_file,
20807 paddress (m_gdbarch, m_address),
20808 m_is_stmt, m_discriminator,
20809 (end_sequence ? "\t(end sequence)" : ""));
20810 }
20811
20812 file_entry *fe = current_file ();
20813
20814 if (fe == NULL)
20815 dwarf2_debug_line_missing_file_complaint ();
20816 /* For now we ignore lines not starting on an instruction boundary.
20817 But not when processing end_sequence for compatibility with the
20818 previous version of the code. */
20819 else if (m_op_index == 0 || end_sequence)
20820 {
20821 fe->included_p = 1;
20822 if (m_record_lines_p)
20823 {
20824 /* When we switch files we insert an end maker in the first file,
20825 switch to the second file and add a new line entry. The
20826 problem is that the end marker inserted in the first file will
20827 discard any previous line entries at the same address. If the
20828 line entries in the first file are marked as is-stmt, while
20829 the new line in the second file is non-stmt, then this means
20830 the end marker will discard is-stmt lines so we can have a
20831 non-stmt line. This means that there are less addresses at
20832 which the user can insert a breakpoint.
20833
20834 To improve this we track the last address in m_last_address,
20835 and whether we have seen an is-stmt at this address. Then
20836 when switching files, if we have seen a stmt at the current
20837 address, and we are switching to create a non-stmt line, then
20838 discard the new line. */
20839 bool file_changed
20840 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
20841 bool ignore_this_line
20842 = ((file_changed && !end_sequence && m_last_address == m_address
20843 && !m_is_stmt && m_stmt_at_address)
20844 || (!end_sequence && m_line == 0));
20845
20846 if ((file_changed && !ignore_this_line) || end_sequence)
20847 {
20848 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20849 m_currently_recording_lines ? m_cu : nullptr);
20850 }
20851
20852 if (!end_sequence && !ignore_this_line)
20853 {
20854 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20855
20856 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20857 m_line_has_non_zero_discriminator,
20858 m_last_subfile))
20859 {
20860 buildsym_compunit *builder = m_cu->get_builder ();
20861 dwarf_record_line_1 (m_gdbarch,
20862 builder->get_current_subfile (),
20863 m_line, m_address, is_stmt,
20864 m_currently_recording_lines ? m_cu : nullptr);
20865 }
20866 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20867 m_last_line = m_line;
20868 }
20869 }
20870 }
20871
20872 /* Track whether we have seen any m_is_stmt true at m_address in case we
20873 have multiple line table entries all at m_address. */
20874 if (m_last_address != m_address)
20875 {
20876 m_stmt_at_address = false;
20877 m_last_address = m_address;
20878 }
20879 m_stmt_at_address |= m_is_stmt;
20880 }
20881
20882 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20883 line_header *lh, bool record_lines_p)
20884 {
20885 m_cu = cu;
20886 m_gdbarch = arch;
20887 m_record_lines_p = record_lines_p;
20888 m_line_header = lh;
20889
20890 m_currently_recording_lines = true;
20891
20892 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20893 was a line entry for it so that the backend has a chance to adjust it
20894 and also record it in case it needs it. This is currently used by MIPS
20895 code, cf. `mips_adjust_dwarf2_line'. */
20896 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20897 m_is_stmt = lh->default_is_stmt;
20898 m_discriminator = 0;
20899
20900 m_last_address = m_address;
20901 m_stmt_at_address = false;
20902 }
20903
20904 void
20905 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20906 const gdb_byte *line_ptr,
20907 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20908 {
20909 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
20910 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
20911 located at 0x0. In this case, additionally check that if
20912 ADDRESS < UNRELOCATED_LOWPC. */
20913
20914 if ((address == 0 && address < unrelocated_lowpc)
20915 || address == (CORE_ADDR) -1)
20916 {
20917 /* This line table is for a function which has been
20918 GCd by the linker. Ignore it. PR gdb/12528 */
20919
20920 struct objfile *objfile = cu->per_objfile->objfile;
20921 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20922
20923 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20924 line_offset, objfile_name (objfile));
20925 m_currently_recording_lines = false;
20926 /* Note: m_currently_recording_lines is left as false until we see
20927 DW_LNE_end_sequence. */
20928 }
20929 }
20930
20931 /* Subroutine of dwarf_decode_lines to simplify it.
20932 Process the line number information in LH.
20933 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20934 program in order to set included_p for every referenced header. */
20935
20936 static void
20937 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20938 const int decode_for_pst_p, CORE_ADDR lowpc)
20939 {
20940 const gdb_byte *line_ptr, *extended_end;
20941 const gdb_byte *line_end;
20942 unsigned int bytes_read, extended_len;
20943 unsigned char op_code, extended_op;
20944 CORE_ADDR baseaddr;
20945 struct objfile *objfile = cu->per_objfile->objfile;
20946 bfd *abfd = objfile->obfd;
20947 struct gdbarch *gdbarch = objfile->arch ();
20948 /* True if we're recording line info (as opposed to building partial
20949 symtabs and just interested in finding include files mentioned by
20950 the line number program). */
20951 bool record_lines_p = !decode_for_pst_p;
20952
20953 baseaddr = objfile->text_section_offset ();
20954
20955 line_ptr = lh->statement_program_start;
20956 line_end = lh->statement_program_end;
20957
20958 /* Read the statement sequences until there's nothing left. */
20959 while (line_ptr < line_end)
20960 {
20961 /* The DWARF line number program state machine. Reset the state
20962 machine at the start of each sequence. */
20963 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20964 bool end_sequence = false;
20965
20966 if (record_lines_p)
20967 {
20968 /* Start a subfile for the current file of the state
20969 machine. */
20970 const file_entry *fe = state_machine.current_file ();
20971
20972 if (fe != NULL)
20973 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20974 }
20975
20976 /* Decode the table. */
20977 while (line_ptr < line_end && !end_sequence)
20978 {
20979 op_code = read_1_byte (abfd, line_ptr);
20980 line_ptr += 1;
20981
20982 if (op_code >= lh->opcode_base)
20983 {
20984 /* Special opcode. */
20985 state_machine.handle_special_opcode (op_code);
20986 }
20987 else switch (op_code)
20988 {
20989 case DW_LNS_extended_op:
20990 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20991 &bytes_read);
20992 line_ptr += bytes_read;
20993 extended_end = line_ptr + extended_len;
20994 extended_op = read_1_byte (abfd, line_ptr);
20995 line_ptr += 1;
20996 if (DW_LNE_lo_user <= extended_op
20997 && extended_op <= DW_LNE_hi_user)
20998 {
20999 /* Vendor extension, ignore. */
21000 line_ptr = extended_end;
21001 break;
21002 }
21003 switch (extended_op)
21004 {
21005 case DW_LNE_end_sequence:
21006 state_machine.handle_end_sequence ();
21007 end_sequence = true;
21008 break;
21009 case DW_LNE_set_address:
21010 {
21011 CORE_ADDR address
21012 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21013 line_ptr += bytes_read;
21014
21015 state_machine.check_line_address (cu, line_ptr,
21016 lowpc - baseaddr, address);
21017 state_machine.handle_set_address (baseaddr, address);
21018 }
21019 break;
21020 case DW_LNE_define_file:
21021 {
21022 const char *cur_file;
21023 unsigned int mod_time, length;
21024 dir_index dindex;
21025
21026 cur_file = read_direct_string (abfd, line_ptr,
21027 &bytes_read);
21028 line_ptr += bytes_read;
21029 dindex = (dir_index)
21030 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21031 line_ptr += bytes_read;
21032 mod_time =
21033 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21034 line_ptr += bytes_read;
21035 length =
21036 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21037 line_ptr += bytes_read;
21038 lh->add_file_name (cur_file, dindex, mod_time, length);
21039 }
21040 break;
21041 case DW_LNE_set_discriminator:
21042 {
21043 /* The discriminator is not interesting to the
21044 debugger; just ignore it. We still need to
21045 check its value though:
21046 if there are consecutive entries for the same
21047 (non-prologue) line we want to coalesce them.
21048 PR 17276. */
21049 unsigned int discr
21050 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21051 line_ptr += bytes_read;
21052
21053 state_machine.handle_set_discriminator (discr);
21054 }
21055 break;
21056 default:
21057 complaint (_("mangled .debug_line section"));
21058 return;
21059 }
21060 /* Make sure that we parsed the extended op correctly. If e.g.
21061 we expected a different address size than the producer used,
21062 we may have read the wrong number of bytes. */
21063 if (line_ptr != extended_end)
21064 {
21065 complaint (_("mangled .debug_line section"));
21066 return;
21067 }
21068 break;
21069 case DW_LNS_copy:
21070 state_machine.handle_copy ();
21071 break;
21072 case DW_LNS_advance_pc:
21073 {
21074 CORE_ADDR adjust
21075 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21076 line_ptr += bytes_read;
21077
21078 state_machine.handle_advance_pc (adjust);
21079 }
21080 break;
21081 case DW_LNS_advance_line:
21082 {
21083 int line_delta
21084 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21085 line_ptr += bytes_read;
21086
21087 state_machine.handle_advance_line (line_delta);
21088 }
21089 break;
21090 case DW_LNS_set_file:
21091 {
21092 file_name_index file
21093 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21094 &bytes_read);
21095 line_ptr += bytes_read;
21096
21097 state_machine.handle_set_file (file);
21098 }
21099 break;
21100 case DW_LNS_set_column:
21101 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21102 line_ptr += bytes_read;
21103 break;
21104 case DW_LNS_negate_stmt:
21105 state_machine.handle_negate_stmt ();
21106 break;
21107 case DW_LNS_set_basic_block:
21108 break;
21109 /* Add to the address register of the state machine the
21110 address increment value corresponding to special opcode
21111 255. I.e., this value is scaled by the minimum
21112 instruction length since special opcode 255 would have
21113 scaled the increment. */
21114 case DW_LNS_const_add_pc:
21115 state_machine.handle_const_add_pc ();
21116 break;
21117 case DW_LNS_fixed_advance_pc:
21118 {
21119 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21120 line_ptr += 2;
21121
21122 state_machine.handle_fixed_advance_pc (addr_adj);
21123 }
21124 break;
21125 default:
21126 {
21127 /* Unknown standard opcode, ignore it. */
21128 int i;
21129
21130 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21131 {
21132 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21133 line_ptr += bytes_read;
21134 }
21135 }
21136 }
21137 }
21138
21139 if (!end_sequence)
21140 dwarf2_debug_line_missing_end_sequence_complaint ();
21141
21142 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21143 in which case we still finish recording the last line). */
21144 state_machine.record_line (true);
21145 }
21146 }
21147
21148 /* Decode the Line Number Program (LNP) for the given line_header
21149 structure and CU. The actual information extracted and the type
21150 of structures created from the LNP depends on the value of PST.
21151
21152 1. If PST is NULL, then this procedure uses the data from the program
21153 to create all necessary symbol tables, and their linetables.
21154
21155 2. If PST is not NULL, this procedure reads the program to determine
21156 the list of files included by the unit represented by PST, and
21157 builds all the associated partial symbol tables.
21158
21159 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21160 It is used for relative paths in the line table.
21161 NOTE: When processing partial symtabs (pst != NULL),
21162 comp_dir == pst->dirname.
21163
21164 NOTE: It is important that psymtabs have the same file name (via strcmp)
21165 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21166 symtab we don't use it in the name of the psymtabs we create.
21167 E.g. expand_line_sal requires this when finding psymtabs to expand.
21168 A good testcase for this is mb-inline.exp.
21169
21170 LOWPC is the lowest address in CU (or 0 if not known).
21171
21172 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21173 for its PC<->lines mapping information. Otherwise only the filename
21174 table is read in. */
21175
21176 static void
21177 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21178 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21179 CORE_ADDR lowpc, int decode_mapping)
21180 {
21181 struct objfile *objfile = cu->per_objfile->objfile;
21182 const int decode_for_pst_p = (pst != NULL);
21183
21184 if (decode_mapping)
21185 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21186
21187 if (decode_for_pst_p)
21188 {
21189 /* Now that we're done scanning the Line Header Program, we can
21190 create the psymtab of each included file. */
21191 for (auto &file_entry : lh->file_names ())
21192 if (file_entry.included_p == 1)
21193 {
21194 gdb::unique_xmalloc_ptr<char> name_holder;
21195 const char *include_name =
21196 psymtab_include_file_name (lh, file_entry, pst,
21197 comp_dir, &name_holder);
21198 if (include_name != NULL)
21199 dwarf2_create_include_psymtab (include_name, pst, objfile);
21200 }
21201 }
21202 else
21203 {
21204 /* Make sure a symtab is created for every file, even files
21205 which contain only variables (i.e. no code with associated
21206 line numbers). */
21207 buildsym_compunit *builder = cu->get_builder ();
21208 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21209
21210 for (auto &fe : lh->file_names ())
21211 {
21212 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21213 if (builder->get_current_subfile ()->symtab == NULL)
21214 {
21215 builder->get_current_subfile ()->symtab
21216 = allocate_symtab (cust,
21217 builder->get_current_subfile ()->name);
21218 }
21219 fe.symtab = builder->get_current_subfile ()->symtab;
21220 }
21221 }
21222 }
21223
21224 /* Start a subfile for DWARF. FILENAME is the name of the file and
21225 DIRNAME the name of the source directory which contains FILENAME
21226 or NULL if not known.
21227 This routine tries to keep line numbers from identical absolute and
21228 relative file names in a common subfile.
21229
21230 Using the `list' example from the GDB testsuite, which resides in
21231 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21232 of /srcdir/list0.c yields the following debugging information for list0.c:
21233
21234 DW_AT_name: /srcdir/list0.c
21235 DW_AT_comp_dir: /compdir
21236 files.files[0].name: list0.h
21237 files.files[0].dir: /srcdir
21238 files.files[1].name: list0.c
21239 files.files[1].dir: /srcdir
21240
21241 The line number information for list0.c has to end up in a single
21242 subfile, so that `break /srcdir/list0.c:1' works as expected.
21243 start_subfile will ensure that this happens provided that we pass the
21244 concatenation of files.files[1].dir and files.files[1].name as the
21245 subfile's name. */
21246
21247 static void
21248 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21249 const char *dirname)
21250 {
21251 gdb::unique_xmalloc_ptr<char> copy;
21252
21253 /* In order not to lose the line information directory,
21254 we concatenate it to the filename when it makes sense.
21255 Note that the Dwarf3 standard says (speaking of filenames in line
21256 information): ``The directory index is ignored for file names
21257 that represent full path names''. Thus ignoring dirname in the
21258 `else' branch below isn't an issue. */
21259
21260 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21261 {
21262 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21263 filename = copy.get ();
21264 }
21265
21266 cu->get_builder ()->start_subfile (filename);
21267 }
21268
21269 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21270 buildsym_compunit constructor. */
21271
21272 struct compunit_symtab *
21273 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21274 CORE_ADDR low_pc)
21275 {
21276 gdb_assert (m_builder == nullptr);
21277
21278 m_builder.reset (new struct buildsym_compunit
21279 (this->per_objfile->objfile,
21280 name, comp_dir, language, low_pc));
21281
21282 list_in_scope = get_builder ()->get_file_symbols ();
21283
21284 get_builder ()->record_debugformat ("DWARF 2");
21285 get_builder ()->record_producer (producer);
21286
21287 processing_has_namespace_info = false;
21288
21289 return get_builder ()->get_compunit_symtab ();
21290 }
21291
21292 static void
21293 var_decode_location (struct attribute *attr, struct symbol *sym,
21294 struct dwarf2_cu *cu)
21295 {
21296 struct objfile *objfile = cu->per_objfile->objfile;
21297 struct comp_unit_head *cu_header = &cu->header;
21298
21299 /* NOTE drow/2003-01-30: There used to be a comment and some special
21300 code here to turn a symbol with DW_AT_external and a
21301 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21302 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21303 with some versions of binutils) where shared libraries could have
21304 relocations against symbols in their debug information - the
21305 minimal symbol would have the right address, but the debug info
21306 would not. It's no longer necessary, because we will explicitly
21307 apply relocations when we read in the debug information now. */
21308
21309 /* A DW_AT_location attribute with no contents indicates that a
21310 variable has been optimized away. */
21311 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
21312 {
21313 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21314 return;
21315 }
21316
21317 /* Handle one degenerate form of location expression specially, to
21318 preserve GDB's previous behavior when section offsets are
21319 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21320 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21321
21322 if (attr->form_is_block ()
21323 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21324 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21325 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21326 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
21327 && (DW_BLOCK (attr)->size
21328 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21329 {
21330 unsigned int dummy;
21331
21332 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21333 SET_SYMBOL_VALUE_ADDRESS
21334 (sym, cu->header.read_address (objfile->obfd,
21335 DW_BLOCK (attr)->data + 1,
21336 &dummy));
21337 else
21338 SET_SYMBOL_VALUE_ADDRESS
21339 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21340 &dummy));
21341 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21342 fixup_symbol_section (sym, objfile);
21343 SET_SYMBOL_VALUE_ADDRESS
21344 (sym,
21345 SYMBOL_VALUE_ADDRESS (sym)
21346 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
21347 return;
21348 }
21349
21350 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21351 expression evaluator, and use LOC_COMPUTED only when necessary
21352 (i.e. when the value of a register or memory location is
21353 referenced, or a thread-local block, etc.). Then again, it might
21354 not be worthwhile. I'm assuming that it isn't unless performance
21355 or memory numbers show me otherwise. */
21356
21357 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21358
21359 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21360 cu->has_loclist = true;
21361 }
21362
21363 /* Given a pointer to a DWARF information entry, figure out if we need
21364 to make a symbol table entry for it, and if so, create a new entry
21365 and return a pointer to it.
21366 If TYPE is NULL, determine symbol type from the die, otherwise
21367 used the passed type.
21368 If SPACE is not NULL, use it to hold the new symbol. If it is
21369 NULL, allocate a new symbol on the objfile's obstack. */
21370
21371 static struct symbol *
21372 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21373 struct symbol *space)
21374 {
21375 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21376 struct objfile *objfile = per_objfile->objfile;
21377 struct gdbarch *gdbarch = objfile->arch ();
21378 struct symbol *sym = NULL;
21379 const char *name;
21380 struct attribute *attr = NULL;
21381 struct attribute *attr2 = NULL;
21382 CORE_ADDR baseaddr;
21383 struct pending **list_to_add = NULL;
21384
21385 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21386
21387 baseaddr = objfile->text_section_offset ();
21388
21389 name = dwarf2_name (die, cu);
21390 if (name)
21391 {
21392 int suppress_add = 0;
21393
21394 if (space)
21395 sym = space;
21396 else
21397 sym = new (&objfile->objfile_obstack) symbol;
21398 OBJSTAT (objfile, n_syms++);
21399
21400 /* Cache this symbol's name and the name's demangled form (if any). */
21401 sym->set_language (cu->language, &objfile->objfile_obstack);
21402 /* Fortran does not have mangling standard and the mangling does differ
21403 between gfortran, iFort etc. */
21404 const char *physname
21405 = (cu->language == language_fortran
21406 ? dwarf2_full_name (name, die, cu)
21407 : dwarf2_physname (name, die, cu));
21408 const char *linkagename = dw2_linkage_name (die, cu);
21409
21410 if (linkagename == nullptr || cu->language == language_ada)
21411 sym->set_linkage_name (physname);
21412 else
21413 {
21414 sym->set_demangled_name (physname, &objfile->objfile_obstack);
21415 sym->set_linkage_name (linkagename);
21416 }
21417
21418 /* Default assumptions.
21419 Use the passed type or decode it from the die. */
21420 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21421 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21422 if (type != NULL)
21423 SYMBOL_TYPE (sym) = type;
21424 else
21425 SYMBOL_TYPE (sym) = die_type (die, cu);
21426 attr = dwarf2_attr (die,
21427 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21428 cu);
21429 if (attr != nullptr)
21430 {
21431 SYMBOL_LINE (sym) = DW_UNSND (attr);
21432 }
21433
21434 attr = dwarf2_attr (die,
21435 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21436 cu);
21437 if (attr != nullptr)
21438 {
21439 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21440 struct file_entry *fe;
21441
21442 if (cu->line_header != NULL)
21443 fe = cu->line_header->file_name_at (file_index);
21444 else
21445 fe = NULL;
21446
21447 if (fe == NULL)
21448 complaint (_("file index out of range"));
21449 else
21450 symbol_set_symtab (sym, fe->symtab);
21451 }
21452
21453 switch (die->tag)
21454 {
21455 case DW_TAG_label:
21456 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21457 if (attr != nullptr)
21458 {
21459 CORE_ADDR addr;
21460
21461 addr = attr->value_as_address ();
21462 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21463 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21464 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21465 }
21466 else
21467 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21468 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21469 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21470 add_symbol_to_list (sym, cu->list_in_scope);
21471 break;
21472 case DW_TAG_subprogram:
21473 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21474 finish_block. */
21475 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21476 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21477 if ((attr2 && (DW_UNSND (attr2) != 0))
21478 || cu->language == language_ada
21479 || cu->language == language_fortran)
21480 {
21481 /* Subprograms marked external are stored as a global symbol.
21482 Ada and Fortran subprograms, whether marked external or
21483 not, are always stored as a global symbol, because we want
21484 to be able to access them globally. For instance, we want
21485 to be able to break on a nested subprogram without having
21486 to specify the context. */
21487 list_to_add = cu->get_builder ()->get_global_symbols ();
21488 }
21489 else
21490 {
21491 list_to_add = cu->list_in_scope;
21492 }
21493 break;
21494 case DW_TAG_inlined_subroutine:
21495 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21496 finish_block. */
21497 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21498 SYMBOL_INLINED (sym) = 1;
21499 list_to_add = cu->list_in_scope;
21500 break;
21501 case DW_TAG_template_value_param:
21502 suppress_add = 1;
21503 /* Fall through. */
21504 case DW_TAG_constant:
21505 case DW_TAG_variable:
21506 case DW_TAG_member:
21507 /* Compilation with minimal debug info may result in
21508 variables with missing type entries. Change the
21509 misleading `void' type to something sensible. */
21510 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
21511 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21512
21513 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21514 /* In the case of DW_TAG_member, we should only be called for
21515 static const members. */
21516 if (die->tag == DW_TAG_member)
21517 {
21518 /* dwarf2_add_field uses die_is_declaration,
21519 so we do the same. */
21520 gdb_assert (die_is_declaration (die, cu));
21521 gdb_assert (attr);
21522 }
21523 if (attr != nullptr)
21524 {
21525 dwarf2_const_value (attr, sym, cu);
21526 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21527 if (!suppress_add)
21528 {
21529 if (attr2 && (DW_UNSND (attr2) != 0))
21530 list_to_add = cu->get_builder ()->get_global_symbols ();
21531 else
21532 list_to_add = cu->list_in_scope;
21533 }
21534 break;
21535 }
21536 attr = dwarf2_attr (die, DW_AT_location, cu);
21537 if (attr != nullptr)
21538 {
21539 var_decode_location (attr, sym, cu);
21540 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21541
21542 /* Fortran explicitly imports any global symbols to the local
21543 scope by DW_TAG_common_block. */
21544 if (cu->language == language_fortran && die->parent
21545 && die->parent->tag == DW_TAG_common_block)
21546 attr2 = NULL;
21547
21548 if (SYMBOL_CLASS (sym) == LOC_STATIC
21549 && SYMBOL_VALUE_ADDRESS (sym) == 0
21550 && !per_objfile->per_bfd->has_section_at_zero)
21551 {
21552 /* When a static variable is eliminated by the linker,
21553 the corresponding debug information is not stripped
21554 out, but the variable address is set to null;
21555 do not add such variables into symbol table. */
21556 }
21557 else if (attr2 && (DW_UNSND (attr2) != 0))
21558 {
21559 if (SYMBOL_CLASS (sym) == LOC_STATIC
21560 && (objfile->flags & OBJF_MAINLINE) == 0
21561 && per_objfile->per_bfd->can_copy)
21562 {
21563 /* A global static variable might be subject to
21564 copy relocation. We first check for a local
21565 minsym, though, because maybe the symbol was
21566 marked hidden, in which case this would not
21567 apply. */
21568 bound_minimal_symbol found
21569 = (lookup_minimal_symbol_linkage
21570 (sym->linkage_name (), objfile));
21571 if (found.minsym != nullptr)
21572 sym->maybe_copied = 1;
21573 }
21574
21575 /* A variable with DW_AT_external is never static,
21576 but it may be block-scoped. */
21577 list_to_add
21578 = ((cu->list_in_scope
21579 == cu->get_builder ()->get_file_symbols ())
21580 ? cu->get_builder ()->get_global_symbols ()
21581 : cu->list_in_scope);
21582 }
21583 else
21584 list_to_add = cu->list_in_scope;
21585 }
21586 else
21587 {
21588 /* We do not know the address of this symbol.
21589 If it is an external symbol and we have type information
21590 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21591 The address of the variable will then be determined from
21592 the minimal symbol table whenever the variable is
21593 referenced. */
21594 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21595
21596 /* Fortran explicitly imports any global symbols to the local
21597 scope by DW_TAG_common_block. */
21598 if (cu->language == language_fortran && die->parent
21599 && die->parent->tag == DW_TAG_common_block)
21600 {
21601 /* SYMBOL_CLASS doesn't matter here because
21602 read_common_block is going to reset it. */
21603 if (!suppress_add)
21604 list_to_add = cu->list_in_scope;
21605 }
21606 else if (attr2 && (DW_UNSND (attr2) != 0)
21607 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21608 {
21609 /* A variable with DW_AT_external is never static, but it
21610 may be block-scoped. */
21611 list_to_add
21612 = ((cu->list_in_scope
21613 == cu->get_builder ()->get_file_symbols ())
21614 ? cu->get_builder ()->get_global_symbols ()
21615 : cu->list_in_scope);
21616
21617 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21618 }
21619 else if (!die_is_declaration (die, cu))
21620 {
21621 /* Use the default LOC_OPTIMIZED_OUT class. */
21622 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21623 if (!suppress_add)
21624 list_to_add = cu->list_in_scope;
21625 }
21626 }
21627 break;
21628 case DW_TAG_formal_parameter:
21629 {
21630 /* If we are inside a function, mark this as an argument. If
21631 not, we might be looking at an argument to an inlined function
21632 when we do not have enough information to show inlined frames;
21633 pretend it's a local variable in that case so that the user can
21634 still see it. */
21635 struct context_stack *curr
21636 = cu->get_builder ()->get_current_context_stack ();
21637 if (curr != nullptr && curr->name != nullptr)
21638 SYMBOL_IS_ARGUMENT (sym) = 1;
21639 attr = dwarf2_attr (die, DW_AT_location, cu);
21640 if (attr != nullptr)
21641 {
21642 var_decode_location (attr, sym, cu);
21643 }
21644 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21645 if (attr != nullptr)
21646 {
21647 dwarf2_const_value (attr, sym, cu);
21648 }
21649
21650 list_to_add = cu->list_in_scope;
21651 }
21652 break;
21653 case DW_TAG_unspecified_parameters:
21654 /* From varargs functions; gdb doesn't seem to have any
21655 interest in this information, so just ignore it for now.
21656 (FIXME?) */
21657 break;
21658 case DW_TAG_template_type_param:
21659 suppress_add = 1;
21660 /* Fall through. */
21661 case DW_TAG_class_type:
21662 case DW_TAG_interface_type:
21663 case DW_TAG_structure_type:
21664 case DW_TAG_union_type:
21665 case DW_TAG_set_type:
21666 case DW_TAG_enumeration_type:
21667 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21668 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21669
21670 {
21671 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21672 really ever be static objects: otherwise, if you try
21673 to, say, break of a class's method and you're in a file
21674 which doesn't mention that class, it won't work unless
21675 the check for all static symbols in lookup_symbol_aux
21676 saves you. See the OtherFileClass tests in
21677 gdb.c++/namespace.exp. */
21678
21679 if (!suppress_add)
21680 {
21681 buildsym_compunit *builder = cu->get_builder ();
21682 list_to_add
21683 = (cu->list_in_scope == builder->get_file_symbols ()
21684 && cu->language == language_cplus
21685 ? builder->get_global_symbols ()
21686 : cu->list_in_scope);
21687
21688 /* The semantics of C++ state that "struct foo {
21689 ... }" also defines a typedef for "foo". */
21690 if (cu->language == language_cplus
21691 || cu->language == language_ada
21692 || cu->language == language_d
21693 || cu->language == language_rust)
21694 {
21695 /* The symbol's name is already allocated along
21696 with this objfile, so we don't need to
21697 duplicate it for the type. */
21698 if (SYMBOL_TYPE (sym)->name () == 0)
21699 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
21700 }
21701 }
21702 }
21703 break;
21704 case DW_TAG_typedef:
21705 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21706 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21707 list_to_add = cu->list_in_scope;
21708 break;
21709 case DW_TAG_base_type:
21710 case DW_TAG_subrange_type:
21711 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21712 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21713 list_to_add = cu->list_in_scope;
21714 break;
21715 case DW_TAG_enumerator:
21716 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21717 if (attr != nullptr)
21718 {
21719 dwarf2_const_value (attr, sym, cu);
21720 }
21721 {
21722 /* NOTE: carlton/2003-11-10: See comment above in the
21723 DW_TAG_class_type, etc. block. */
21724
21725 list_to_add
21726 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21727 && cu->language == language_cplus
21728 ? cu->get_builder ()->get_global_symbols ()
21729 : cu->list_in_scope);
21730 }
21731 break;
21732 case DW_TAG_imported_declaration:
21733 case DW_TAG_namespace:
21734 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21735 list_to_add = cu->get_builder ()->get_global_symbols ();
21736 break;
21737 case DW_TAG_module:
21738 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21739 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21740 list_to_add = cu->get_builder ()->get_global_symbols ();
21741 break;
21742 case DW_TAG_common_block:
21743 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21744 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21745 add_symbol_to_list (sym, cu->list_in_scope);
21746 break;
21747 default:
21748 /* Not a tag we recognize. Hopefully we aren't processing
21749 trash data, but since we must specifically ignore things
21750 we don't recognize, there is nothing else we should do at
21751 this point. */
21752 complaint (_("unsupported tag: '%s'"),
21753 dwarf_tag_name (die->tag));
21754 break;
21755 }
21756
21757 if (suppress_add)
21758 {
21759 sym->hash_next = objfile->template_symbols;
21760 objfile->template_symbols = sym;
21761 list_to_add = NULL;
21762 }
21763
21764 if (list_to_add != NULL)
21765 add_symbol_to_list (sym, list_to_add);
21766
21767 /* For the benefit of old versions of GCC, check for anonymous
21768 namespaces based on the demangled name. */
21769 if (!cu->processing_has_namespace_info
21770 && cu->language == language_cplus)
21771 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21772 }
21773 return (sym);
21774 }
21775
21776 /* Given an attr with a DW_FORM_dataN value in host byte order,
21777 zero-extend it as appropriate for the symbol's type. The DWARF
21778 standard (v4) is not entirely clear about the meaning of using
21779 DW_FORM_dataN for a constant with a signed type, where the type is
21780 wider than the data. The conclusion of a discussion on the DWARF
21781 list was that this is unspecified. We choose to always zero-extend
21782 because that is the interpretation long in use by GCC. */
21783
21784 static gdb_byte *
21785 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21786 struct dwarf2_cu *cu, LONGEST *value, int bits)
21787 {
21788 struct objfile *objfile = cu->per_objfile->objfile;
21789 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21790 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21791 LONGEST l = DW_UNSND (attr);
21792
21793 if (bits < sizeof (*value) * 8)
21794 {
21795 l &= ((LONGEST) 1 << bits) - 1;
21796 *value = l;
21797 }
21798 else if (bits == sizeof (*value) * 8)
21799 *value = l;
21800 else
21801 {
21802 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21803 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21804 return bytes;
21805 }
21806
21807 return NULL;
21808 }
21809
21810 /* Read a constant value from an attribute. Either set *VALUE, or if
21811 the value does not fit in *VALUE, set *BYTES - either already
21812 allocated on the objfile obstack, or newly allocated on OBSTACK,
21813 or, set *BATON, if we translated the constant to a location
21814 expression. */
21815
21816 static void
21817 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21818 const char *name, struct obstack *obstack,
21819 struct dwarf2_cu *cu,
21820 LONGEST *value, const gdb_byte **bytes,
21821 struct dwarf2_locexpr_baton **baton)
21822 {
21823 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21824 struct objfile *objfile = per_objfile->objfile;
21825 struct comp_unit_head *cu_header = &cu->header;
21826 struct dwarf_block *blk;
21827 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21828 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21829
21830 *value = 0;
21831 *bytes = NULL;
21832 *baton = NULL;
21833
21834 switch (attr->form)
21835 {
21836 case DW_FORM_addr:
21837 case DW_FORM_addrx:
21838 case DW_FORM_GNU_addr_index:
21839 {
21840 gdb_byte *data;
21841
21842 if (TYPE_LENGTH (type) != cu_header->addr_size)
21843 dwarf2_const_value_length_mismatch_complaint (name,
21844 cu_header->addr_size,
21845 TYPE_LENGTH (type));
21846 /* Symbols of this form are reasonably rare, so we just
21847 piggyback on the existing location code rather than writing
21848 a new implementation of symbol_computed_ops. */
21849 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21850 (*baton)->per_objfile = per_objfile;
21851 (*baton)->per_cu = cu->per_cu;
21852 gdb_assert ((*baton)->per_cu);
21853
21854 (*baton)->size = 2 + cu_header->addr_size;
21855 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21856 (*baton)->data = data;
21857
21858 data[0] = DW_OP_addr;
21859 store_unsigned_integer (&data[1], cu_header->addr_size,
21860 byte_order, DW_ADDR (attr));
21861 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21862 }
21863 break;
21864 case DW_FORM_string:
21865 case DW_FORM_strp:
21866 case DW_FORM_strx:
21867 case DW_FORM_GNU_str_index:
21868 case DW_FORM_GNU_strp_alt:
21869 /* DW_STRING is already allocated on the objfile obstack, point
21870 directly to it. */
21871 *bytes = (const gdb_byte *) DW_STRING (attr);
21872 break;
21873 case DW_FORM_block1:
21874 case DW_FORM_block2:
21875 case DW_FORM_block4:
21876 case DW_FORM_block:
21877 case DW_FORM_exprloc:
21878 case DW_FORM_data16:
21879 blk = DW_BLOCK (attr);
21880 if (TYPE_LENGTH (type) != blk->size)
21881 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21882 TYPE_LENGTH (type));
21883 *bytes = blk->data;
21884 break;
21885
21886 /* The DW_AT_const_value attributes are supposed to carry the
21887 symbol's value "represented as it would be on the target
21888 architecture." By the time we get here, it's already been
21889 converted to host endianness, so we just need to sign- or
21890 zero-extend it as appropriate. */
21891 case DW_FORM_data1:
21892 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21893 break;
21894 case DW_FORM_data2:
21895 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21896 break;
21897 case DW_FORM_data4:
21898 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21899 break;
21900 case DW_FORM_data8:
21901 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21902 break;
21903
21904 case DW_FORM_sdata:
21905 case DW_FORM_implicit_const:
21906 *value = DW_SND (attr);
21907 break;
21908
21909 case DW_FORM_udata:
21910 *value = DW_UNSND (attr);
21911 break;
21912
21913 default:
21914 complaint (_("unsupported const value attribute form: '%s'"),
21915 dwarf_form_name (attr->form));
21916 *value = 0;
21917 break;
21918 }
21919 }
21920
21921
21922 /* Copy constant value from an attribute to a symbol. */
21923
21924 static void
21925 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21926 struct dwarf2_cu *cu)
21927 {
21928 struct objfile *objfile = cu->per_objfile->objfile;
21929 LONGEST value;
21930 const gdb_byte *bytes;
21931 struct dwarf2_locexpr_baton *baton;
21932
21933 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21934 sym->print_name (),
21935 &objfile->objfile_obstack, cu,
21936 &value, &bytes, &baton);
21937
21938 if (baton != NULL)
21939 {
21940 SYMBOL_LOCATION_BATON (sym) = baton;
21941 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21942 }
21943 else if (bytes != NULL)
21944 {
21945 SYMBOL_VALUE_BYTES (sym) = bytes;
21946 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21947 }
21948 else
21949 {
21950 SYMBOL_VALUE (sym) = value;
21951 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21952 }
21953 }
21954
21955 /* Return the type of the die in question using its DW_AT_type attribute. */
21956
21957 static struct type *
21958 die_type (struct die_info *die, struct dwarf2_cu *cu)
21959 {
21960 struct attribute *type_attr;
21961
21962 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21963 if (!type_attr)
21964 {
21965 struct objfile *objfile = cu->per_objfile->objfile;
21966 /* A missing DW_AT_type represents a void type. */
21967 return objfile_type (objfile)->builtin_void;
21968 }
21969
21970 return lookup_die_type (die, type_attr, cu);
21971 }
21972
21973 /* True iff CU's producer generates GNAT Ada auxiliary information
21974 that allows to find parallel types through that information instead
21975 of having to do expensive parallel lookups by type name. */
21976
21977 static int
21978 need_gnat_info (struct dwarf2_cu *cu)
21979 {
21980 /* Assume that the Ada compiler was GNAT, which always produces
21981 the auxiliary information. */
21982 return (cu->language == language_ada);
21983 }
21984
21985 /* Return the auxiliary type of the die in question using its
21986 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21987 attribute is not present. */
21988
21989 static struct type *
21990 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21991 {
21992 struct attribute *type_attr;
21993
21994 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21995 if (!type_attr)
21996 return NULL;
21997
21998 return lookup_die_type (die, type_attr, cu);
21999 }
22000
22001 /* If DIE has a descriptive_type attribute, then set the TYPE's
22002 descriptive type accordingly. */
22003
22004 static void
22005 set_descriptive_type (struct type *type, struct die_info *die,
22006 struct dwarf2_cu *cu)
22007 {
22008 struct type *descriptive_type = die_descriptive_type (die, cu);
22009
22010 if (descriptive_type)
22011 {
22012 ALLOCATE_GNAT_AUX_TYPE (type);
22013 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22014 }
22015 }
22016
22017 /* Return the containing type of the die in question using its
22018 DW_AT_containing_type attribute. */
22019
22020 static struct type *
22021 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22022 {
22023 struct attribute *type_attr;
22024 struct objfile *objfile = cu->per_objfile->objfile;
22025
22026 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22027 if (!type_attr)
22028 error (_("Dwarf Error: Problem turning containing type into gdb type "
22029 "[in module %s]"), objfile_name (objfile));
22030
22031 return lookup_die_type (die, type_attr, cu);
22032 }
22033
22034 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22035
22036 static struct type *
22037 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22038 {
22039 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22040 struct objfile *objfile = per_objfile->objfile;
22041 char *saved;
22042
22043 std::string message
22044 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22045 objfile_name (objfile),
22046 sect_offset_str (cu->header.sect_off),
22047 sect_offset_str (die->sect_off));
22048 saved = obstack_strdup (&objfile->objfile_obstack, message);
22049
22050 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22051 }
22052
22053 /* Look up the type of DIE in CU using its type attribute ATTR.
22054 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22055 DW_AT_containing_type.
22056 If there is no type substitute an error marker. */
22057
22058 static struct type *
22059 lookup_die_type (struct die_info *die, const struct attribute *attr,
22060 struct dwarf2_cu *cu)
22061 {
22062 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22063 struct objfile *objfile = per_objfile->objfile;
22064 struct type *this_type;
22065
22066 gdb_assert (attr->name == DW_AT_type
22067 || attr->name == DW_AT_GNAT_descriptive_type
22068 || attr->name == DW_AT_containing_type);
22069
22070 /* First see if we have it cached. */
22071
22072 if (attr->form == DW_FORM_GNU_ref_alt)
22073 {
22074 struct dwarf2_per_cu_data *per_cu;
22075 sect_offset sect_off = attr->get_ref_die_offset ();
22076
22077 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22078 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
22079 }
22080 else if (attr->form_is_ref ())
22081 {
22082 sect_offset sect_off = attr->get_ref_die_offset ();
22083
22084 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
22085 }
22086 else if (attr->form == DW_FORM_ref_sig8)
22087 {
22088 ULONGEST signature = DW_SIGNATURE (attr);
22089
22090 return get_signatured_type (die, signature, cu);
22091 }
22092 else
22093 {
22094 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22095 " at %s [in module %s]"),
22096 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22097 objfile_name (objfile));
22098 return build_error_marker_type (cu, die);
22099 }
22100
22101 /* If not cached we need to read it in. */
22102
22103 if (this_type == NULL)
22104 {
22105 struct die_info *type_die = NULL;
22106 struct dwarf2_cu *type_cu = cu;
22107
22108 if (attr->form_is_ref ())
22109 type_die = follow_die_ref (die, attr, &type_cu);
22110 if (type_die == NULL)
22111 return build_error_marker_type (cu, die);
22112 /* If we find the type now, it's probably because the type came
22113 from an inter-CU reference and the type's CU got expanded before
22114 ours. */
22115 this_type = read_type_die (type_die, type_cu);
22116 }
22117
22118 /* If we still don't have a type use an error marker. */
22119
22120 if (this_type == NULL)
22121 return build_error_marker_type (cu, die);
22122
22123 return this_type;
22124 }
22125
22126 /* Return the type in DIE, CU.
22127 Returns NULL for invalid types.
22128
22129 This first does a lookup in die_type_hash,
22130 and only reads the die in if necessary.
22131
22132 NOTE: This can be called when reading in partial or full symbols. */
22133
22134 static struct type *
22135 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22136 {
22137 struct type *this_type;
22138
22139 this_type = get_die_type (die, cu);
22140 if (this_type)
22141 return this_type;
22142
22143 return read_type_die_1 (die, cu);
22144 }
22145
22146 /* Read the type in DIE, CU.
22147 Returns NULL for invalid types. */
22148
22149 static struct type *
22150 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22151 {
22152 struct type *this_type = NULL;
22153
22154 switch (die->tag)
22155 {
22156 case DW_TAG_class_type:
22157 case DW_TAG_interface_type:
22158 case DW_TAG_structure_type:
22159 case DW_TAG_union_type:
22160 this_type = read_structure_type (die, cu);
22161 break;
22162 case DW_TAG_enumeration_type:
22163 this_type = read_enumeration_type (die, cu);
22164 break;
22165 case DW_TAG_subprogram:
22166 case DW_TAG_subroutine_type:
22167 case DW_TAG_inlined_subroutine:
22168 this_type = read_subroutine_type (die, cu);
22169 break;
22170 case DW_TAG_array_type:
22171 this_type = read_array_type (die, cu);
22172 break;
22173 case DW_TAG_set_type:
22174 this_type = read_set_type (die, cu);
22175 break;
22176 case DW_TAG_pointer_type:
22177 this_type = read_tag_pointer_type (die, cu);
22178 break;
22179 case DW_TAG_ptr_to_member_type:
22180 this_type = read_tag_ptr_to_member_type (die, cu);
22181 break;
22182 case DW_TAG_reference_type:
22183 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22184 break;
22185 case DW_TAG_rvalue_reference_type:
22186 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22187 break;
22188 case DW_TAG_const_type:
22189 this_type = read_tag_const_type (die, cu);
22190 break;
22191 case DW_TAG_volatile_type:
22192 this_type = read_tag_volatile_type (die, cu);
22193 break;
22194 case DW_TAG_restrict_type:
22195 this_type = read_tag_restrict_type (die, cu);
22196 break;
22197 case DW_TAG_string_type:
22198 this_type = read_tag_string_type (die, cu);
22199 break;
22200 case DW_TAG_typedef:
22201 this_type = read_typedef (die, cu);
22202 break;
22203 case DW_TAG_subrange_type:
22204 this_type = read_subrange_type (die, cu);
22205 break;
22206 case DW_TAG_base_type:
22207 this_type = read_base_type (die, cu);
22208 break;
22209 case DW_TAG_unspecified_type:
22210 this_type = read_unspecified_type (die, cu);
22211 break;
22212 case DW_TAG_namespace:
22213 this_type = read_namespace_type (die, cu);
22214 break;
22215 case DW_TAG_module:
22216 this_type = read_module_type (die, cu);
22217 break;
22218 case DW_TAG_atomic_type:
22219 this_type = read_tag_atomic_type (die, cu);
22220 break;
22221 default:
22222 complaint (_("unexpected tag in read_type_die: '%s'"),
22223 dwarf_tag_name (die->tag));
22224 break;
22225 }
22226
22227 return this_type;
22228 }
22229
22230 /* See if we can figure out if the class lives in a namespace. We do
22231 this by looking for a member function; its demangled name will
22232 contain namespace info, if there is any.
22233 Return the computed name or NULL.
22234 Space for the result is allocated on the objfile's obstack.
22235 This is the full-die version of guess_partial_die_structure_name.
22236 In this case we know DIE has no useful parent. */
22237
22238 static const char *
22239 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22240 {
22241 struct die_info *spec_die;
22242 struct dwarf2_cu *spec_cu;
22243 struct die_info *child;
22244 struct objfile *objfile = cu->per_objfile->objfile;
22245
22246 spec_cu = cu;
22247 spec_die = die_specification (die, &spec_cu);
22248 if (spec_die != NULL)
22249 {
22250 die = spec_die;
22251 cu = spec_cu;
22252 }
22253
22254 for (child = die->child;
22255 child != NULL;
22256 child = child->sibling)
22257 {
22258 if (child->tag == DW_TAG_subprogram)
22259 {
22260 const char *linkage_name = dw2_linkage_name (child, cu);
22261
22262 if (linkage_name != NULL)
22263 {
22264 gdb::unique_xmalloc_ptr<char> actual_name
22265 (cu->language_defn->class_name_from_physname (linkage_name));
22266 const char *name = NULL;
22267
22268 if (actual_name != NULL)
22269 {
22270 const char *die_name = dwarf2_name (die, cu);
22271
22272 if (die_name != NULL
22273 && strcmp (die_name, actual_name.get ()) != 0)
22274 {
22275 /* Strip off the class name from the full name.
22276 We want the prefix. */
22277 int die_name_len = strlen (die_name);
22278 int actual_name_len = strlen (actual_name.get ());
22279 const char *ptr = actual_name.get ();
22280
22281 /* Test for '::' as a sanity check. */
22282 if (actual_name_len > die_name_len + 2
22283 && ptr[actual_name_len - die_name_len - 1] == ':')
22284 name = obstack_strndup (
22285 &objfile->per_bfd->storage_obstack,
22286 ptr, actual_name_len - die_name_len - 2);
22287 }
22288 }
22289 return name;
22290 }
22291 }
22292 }
22293
22294 return NULL;
22295 }
22296
22297 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22298 prefix part in such case. See
22299 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22300
22301 static const char *
22302 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22303 {
22304 struct attribute *attr;
22305 const char *base;
22306
22307 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22308 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22309 return NULL;
22310
22311 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22312 return NULL;
22313
22314 attr = dw2_linkage_name_attr (die, cu);
22315 if (attr == NULL || DW_STRING (attr) == NULL)
22316 return NULL;
22317
22318 /* dwarf2_name had to be already called. */
22319 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22320
22321 /* Strip the base name, keep any leading namespaces/classes. */
22322 base = strrchr (DW_STRING (attr), ':');
22323 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22324 return "";
22325
22326 struct objfile *objfile = cu->per_objfile->objfile;
22327 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22328 DW_STRING (attr),
22329 &base[-1] - DW_STRING (attr));
22330 }
22331
22332 /* Return the name of the namespace/class that DIE is defined within,
22333 or "" if we can't tell. The caller should not xfree the result.
22334
22335 For example, if we're within the method foo() in the following
22336 code:
22337
22338 namespace N {
22339 class C {
22340 void foo () {
22341 }
22342 };
22343 }
22344
22345 then determine_prefix on foo's die will return "N::C". */
22346
22347 static const char *
22348 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22349 {
22350 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22351 struct die_info *parent, *spec_die;
22352 struct dwarf2_cu *spec_cu;
22353 struct type *parent_type;
22354 const char *retval;
22355
22356 if (cu->language != language_cplus
22357 && cu->language != language_fortran && cu->language != language_d
22358 && cu->language != language_rust)
22359 return "";
22360
22361 retval = anonymous_struct_prefix (die, cu);
22362 if (retval)
22363 return retval;
22364
22365 /* We have to be careful in the presence of DW_AT_specification.
22366 For example, with GCC 3.4, given the code
22367
22368 namespace N {
22369 void foo() {
22370 // Definition of N::foo.
22371 }
22372 }
22373
22374 then we'll have a tree of DIEs like this:
22375
22376 1: DW_TAG_compile_unit
22377 2: DW_TAG_namespace // N
22378 3: DW_TAG_subprogram // declaration of N::foo
22379 4: DW_TAG_subprogram // definition of N::foo
22380 DW_AT_specification // refers to die #3
22381
22382 Thus, when processing die #4, we have to pretend that we're in
22383 the context of its DW_AT_specification, namely the contex of die
22384 #3. */
22385 spec_cu = cu;
22386 spec_die = die_specification (die, &spec_cu);
22387 if (spec_die == NULL)
22388 parent = die->parent;
22389 else
22390 {
22391 parent = spec_die->parent;
22392 cu = spec_cu;
22393 }
22394
22395 if (parent == NULL)
22396 return "";
22397 else if (parent->building_fullname)
22398 {
22399 const char *name;
22400 const char *parent_name;
22401
22402 /* It has been seen on RealView 2.2 built binaries,
22403 DW_TAG_template_type_param types actually _defined_ as
22404 children of the parent class:
22405
22406 enum E {};
22407 template class <class Enum> Class{};
22408 Class<enum E> class_e;
22409
22410 1: DW_TAG_class_type (Class)
22411 2: DW_TAG_enumeration_type (E)
22412 3: DW_TAG_enumerator (enum1:0)
22413 3: DW_TAG_enumerator (enum2:1)
22414 ...
22415 2: DW_TAG_template_type_param
22416 DW_AT_type DW_FORM_ref_udata (E)
22417
22418 Besides being broken debug info, it can put GDB into an
22419 infinite loop. Consider:
22420
22421 When we're building the full name for Class<E>, we'll start
22422 at Class, and go look over its template type parameters,
22423 finding E. We'll then try to build the full name of E, and
22424 reach here. We're now trying to build the full name of E,
22425 and look over the parent DIE for containing scope. In the
22426 broken case, if we followed the parent DIE of E, we'd again
22427 find Class, and once again go look at its template type
22428 arguments, etc., etc. Simply don't consider such parent die
22429 as source-level parent of this die (it can't be, the language
22430 doesn't allow it), and break the loop here. */
22431 name = dwarf2_name (die, cu);
22432 parent_name = dwarf2_name (parent, cu);
22433 complaint (_("template param type '%s' defined within parent '%s'"),
22434 name ? name : "<unknown>",
22435 parent_name ? parent_name : "<unknown>");
22436 return "";
22437 }
22438 else
22439 switch (parent->tag)
22440 {
22441 case DW_TAG_namespace:
22442 parent_type = read_type_die (parent, cu);
22443 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22444 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22445 Work around this problem here. */
22446 if (cu->language == language_cplus
22447 && strcmp (parent_type->name (), "::") == 0)
22448 return "";
22449 /* We give a name to even anonymous namespaces. */
22450 return parent_type->name ();
22451 case DW_TAG_class_type:
22452 case DW_TAG_interface_type:
22453 case DW_TAG_structure_type:
22454 case DW_TAG_union_type:
22455 case DW_TAG_module:
22456 parent_type = read_type_die (parent, cu);
22457 if (parent_type->name () != NULL)
22458 return parent_type->name ();
22459 else
22460 /* An anonymous structure is only allowed non-static data
22461 members; no typedefs, no member functions, et cetera.
22462 So it does not need a prefix. */
22463 return "";
22464 case DW_TAG_compile_unit:
22465 case DW_TAG_partial_unit:
22466 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22467 if (cu->language == language_cplus
22468 && !per_objfile->per_bfd->types.empty ()
22469 && die->child != NULL
22470 && (die->tag == DW_TAG_class_type
22471 || die->tag == DW_TAG_structure_type
22472 || die->tag == DW_TAG_union_type))
22473 {
22474 const char *name = guess_full_die_structure_name (die, cu);
22475 if (name != NULL)
22476 return name;
22477 }
22478 return "";
22479 case DW_TAG_subprogram:
22480 /* Nested subroutines in Fortran get a prefix with the name
22481 of the parent's subroutine. */
22482 if (cu->language == language_fortran)
22483 {
22484 if ((die->tag == DW_TAG_subprogram)
22485 && (dwarf2_name (parent, cu) != NULL))
22486 return dwarf2_name (parent, cu);
22487 }
22488 return determine_prefix (parent, cu);
22489 case DW_TAG_enumeration_type:
22490 parent_type = read_type_die (parent, cu);
22491 if (TYPE_DECLARED_CLASS (parent_type))
22492 {
22493 if (parent_type->name () != NULL)
22494 return parent_type->name ();
22495 return "";
22496 }
22497 /* Fall through. */
22498 default:
22499 return determine_prefix (parent, cu);
22500 }
22501 }
22502
22503 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22504 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22505 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22506 an obconcat, otherwise allocate storage for the result. The CU argument is
22507 used to determine the language and hence, the appropriate separator. */
22508
22509 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22510
22511 static char *
22512 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22513 int physname, struct dwarf2_cu *cu)
22514 {
22515 const char *lead = "";
22516 const char *sep;
22517
22518 if (suffix == NULL || suffix[0] == '\0'
22519 || prefix == NULL || prefix[0] == '\0')
22520 sep = "";
22521 else if (cu->language == language_d)
22522 {
22523 /* For D, the 'main' function could be defined in any module, but it
22524 should never be prefixed. */
22525 if (strcmp (suffix, "D main") == 0)
22526 {
22527 prefix = "";
22528 sep = "";
22529 }
22530 else
22531 sep = ".";
22532 }
22533 else if (cu->language == language_fortran && physname)
22534 {
22535 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22536 DW_AT_MIPS_linkage_name is preferred and used instead. */
22537
22538 lead = "__";
22539 sep = "_MOD_";
22540 }
22541 else
22542 sep = "::";
22543
22544 if (prefix == NULL)
22545 prefix = "";
22546 if (suffix == NULL)
22547 suffix = "";
22548
22549 if (obs == NULL)
22550 {
22551 char *retval
22552 = ((char *)
22553 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22554
22555 strcpy (retval, lead);
22556 strcat (retval, prefix);
22557 strcat (retval, sep);
22558 strcat (retval, suffix);
22559 return retval;
22560 }
22561 else
22562 {
22563 /* We have an obstack. */
22564 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22565 }
22566 }
22567
22568 /* Get name of a die, return NULL if not found. */
22569
22570 static const char *
22571 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22572 struct objfile *objfile)
22573 {
22574 if (name && cu->language == language_cplus)
22575 {
22576 gdb::unique_xmalloc_ptr<char> canon_name
22577 = cp_canonicalize_string (name);
22578
22579 if (canon_name != nullptr)
22580 name = objfile->intern (canon_name.get ());
22581 }
22582
22583 return name;
22584 }
22585
22586 /* Get name of a die, return NULL if not found.
22587 Anonymous namespaces are converted to their magic string. */
22588
22589 static const char *
22590 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22591 {
22592 struct attribute *attr;
22593 struct objfile *objfile = cu->per_objfile->objfile;
22594
22595 attr = dwarf2_attr (die, DW_AT_name, cu);
22596 if ((!attr || !DW_STRING (attr))
22597 && die->tag != DW_TAG_namespace
22598 && die->tag != DW_TAG_class_type
22599 && die->tag != DW_TAG_interface_type
22600 && die->tag != DW_TAG_structure_type
22601 && die->tag != DW_TAG_union_type)
22602 return NULL;
22603
22604 switch (die->tag)
22605 {
22606 case DW_TAG_compile_unit:
22607 case DW_TAG_partial_unit:
22608 /* Compilation units have a DW_AT_name that is a filename, not
22609 a source language identifier. */
22610 case DW_TAG_enumeration_type:
22611 case DW_TAG_enumerator:
22612 /* These tags always have simple identifiers already; no need
22613 to canonicalize them. */
22614 return DW_STRING (attr);
22615
22616 case DW_TAG_namespace:
22617 if (attr != NULL && DW_STRING (attr) != NULL)
22618 return DW_STRING (attr);
22619 return CP_ANONYMOUS_NAMESPACE_STR;
22620
22621 case DW_TAG_class_type:
22622 case DW_TAG_interface_type:
22623 case DW_TAG_structure_type:
22624 case DW_TAG_union_type:
22625 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22626 structures or unions. These were of the form "._%d" in GCC 4.1,
22627 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22628 and GCC 4.4. We work around this problem by ignoring these. */
22629 if (attr && DW_STRING (attr)
22630 && (startswith (DW_STRING (attr), "._")
22631 || startswith (DW_STRING (attr), "<anonymous")))
22632 return NULL;
22633
22634 /* GCC might emit a nameless typedef that has a linkage name. See
22635 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22636 if (!attr || DW_STRING (attr) == NULL)
22637 {
22638 attr = dw2_linkage_name_attr (die, cu);
22639 if (attr == NULL || DW_STRING (attr) == NULL)
22640 return NULL;
22641
22642 /* Avoid demangling DW_STRING (attr) the second time on a second
22643 call for the same DIE. */
22644 if (!DW_STRING_IS_CANONICAL (attr))
22645 {
22646 gdb::unique_xmalloc_ptr<char> demangled
22647 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
22648 if (demangled == nullptr)
22649 return nullptr;
22650
22651 DW_STRING (attr) = objfile->intern (demangled.get ());
22652 DW_STRING_IS_CANONICAL (attr) = 1;
22653 }
22654
22655 /* Strip any leading namespaces/classes, keep only the base name.
22656 DW_AT_name for named DIEs does not contain the prefixes. */
22657 const char *base = strrchr (DW_STRING (attr), ':');
22658 if (base && base > DW_STRING (attr) && base[-1] == ':')
22659 return &base[1];
22660 else
22661 return DW_STRING (attr);
22662 }
22663 break;
22664
22665 default:
22666 break;
22667 }
22668
22669 if (!DW_STRING_IS_CANONICAL (attr))
22670 {
22671 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22672 objfile);
22673 DW_STRING_IS_CANONICAL (attr) = 1;
22674 }
22675 return DW_STRING (attr);
22676 }
22677
22678 /* Return the die that this die in an extension of, or NULL if there
22679 is none. *EXT_CU is the CU containing DIE on input, and the CU
22680 containing the return value on output. */
22681
22682 static struct die_info *
22683 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22684 {
22685 struct attribute *attr;
22686
22687 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22688 if (attr == NULL)
22689 return NULL;
22690
22691 return follow_die_ref (die, attr, ext_cu);
22692 }
22693
22694 static void
22695 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22696 {
22697 unsigned int i;
22698
22699 print_spaces (indent, f);
22700 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22701 dwarf_tag_name (die->tag), die->abbrev,
22702 sect_offset_str (die->sect_off));
22703
22704 if (die->parent != NULL)
22705 {
22706 print_spaces (indent, f);
22707 fprintf_unfiltered (f, " parent at offset: %s\n",
22708 sect_offset_str (die->parent->sect_off));
22709 }
22710
22711 print_spaces (indent, f);
22712 fprintf_unfiltered (f, " has children: %s\n",
22713 dwarf_bool_name (die->child != NULL));
22714
22715 print_spaces (indent, f);
22716 fprintf_unfiltered (f, " attributes:\n");
22717
22718 for (i = 0; i < die->num_attrs; ++i)
22719 {
22720 print_spaces (indent, f);
22721 fprintf_unfiltered (f, " %s (%s) ",
22722 dwarf_attr_name (die->attrs[i].name),
22723 dwarf_form_name (die->attrs[i].form));
22724
22725 switch (die->attrs[i].form)
22726 {
22727 case DW_FORM_addr:
22728 case DW_FORM_addrx:
22729 case DW_FORM_GNU_addr_index:
22730 fprintf_unfiltered (f, "address: ");
22731 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22732 break;
22733 case DW_FORM_block2:
22734 case DW_FORM_block4:
22735 case DW_FORM_block:
22736 case DW_FORM_block1:
22737 fprintf_unfiltered (f, "block: size %s",
22738 pulongest (DW_BLOCK (&die->attrs[i])->size));
22739 break;
22740 case DW_FORM_exprloc:
22741 fprintf_unfiltered (f, "expression: size %s",
22742 pulongest (DW_BLOCK (&die->attrs[i])->size));
22743 break;
22744 case DW_FORM_data16:
22745 fprintf_unfiltered (f, "constant of 16 bytes");
22746 break;
22747 case DW_FORM_ref_addr:
22748 fprintf_unfiltered (f, "ref address: ");
22749 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22750 break;
22751 case DW_FORM_GNU_ref_alt:
22752 fprintf_unfiltered (f, "alt ref address: ");
22753 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22754 break;
22755 case DW_FORM_ref1:
22756 case DW_FORM_ref2:
22757 case DW_FORM_ref4:
22758 case DW_FORM_ref8:
22759 case DW_FORM_ref_udata:
22760 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22761 (long) (DW_UNSND (&die->attrs[i])));
22762 break;
22763 case DW_FORM_data1:
22764 case DW_FORM_data2:
22765 case DW_FORM_data4:
22766 case DW_FORM_data8:
22767 case DW_FORM_udata:
22768 case DW_FORM_sdata:
22769 fprintf_unfiltered (f, "constant: %s",
22770 pulongest (DW_UNSND (&die->attrs[i])));
22771 break;
22772 case DW_FORM_sec_offset:
22773 fprintf_unfiltered (f, "section offset: %s",
22774 pulongest (DW_UNSND (&die->attrs[i])));
22775 break;
22776 case DW_FORM_ref_sig8:
22777 fprintf_unfiltered (f, "signature: %s",
22778 hex_string (DW_SIGNATURE (&die->attrs[i])));
22779 break;
22780 case DW_FORM_string:
22781 case DW_FORM_strp:
22782 case DW_FORM_line_strp:
22783 case DW_FORM_strx:
22784 case DW_FORM_GNU_str_index:
22785 case DW_FORM_GNU_strp_alt:
22786 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22787 DW_STRING (&die->attrs[i])
22788 ? DW_STRING (&die->attrs[i]) : "",
22789 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22790 break;
22791 case DW_FORM_flag:
22792 if (DW_UNSND (&die->attrs[i]))
22793 fprintf_unfiltered (f, "flag: TRUE");
22794 else
22795 fprintf_unfiltered (f, "flag: FALSE");
22796 break;
22797 case DW_FORM_flag_present:
22798 fprintf_unfiltered (f, "flag: TRUE");
22799 break;
22800 case DW_FORM_indirect:
22801 /* The reader will have reduced the indirect form to
22802 the "base form" so this form should not occur. */
22803 fprintf_unfiltered (f,
22804 "unexpected attribute form: DW_FORM_indirect");
22805 break;
22806 case DW_FORM_implicit_const:
22807 fprintf_unfiltered (f, "constant: %s",
22808 plongest (DW_SND (&die->attrs[i])));
22809 break;
22810 default:
22811 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22812 die->attrs[i].form);
22813 break;
22814 }
22815 fprintf_unfiltered (f, "\n");
22816 }
22817 }
22818
22819 static void
22820 dump_die_for_error (struct die_info *die)
22821 {
22822 dump_die_shallow (gdb_stderr, 0, die);
22823 }
22824
22825 static void
22826 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22827 {
22828 int indent = level * 4;
22829
22830 gdb_assert (die != NULL);
22831
22832 if (level >= max_level)
22833 return;
22834
22835 dump_die_shallow (f, indent, die);
22836
22837 if (die->child != NULL)
22838 {
22839 print_spaces (indent, f);
22840 fprintf_unfiltered (f, " Children:");
22841 if (level + 1 < max_level)
22842 {
22843 fprintf_unfiltered (f, "\n");
22844 dump_die_1 (f, level + 1, max_level, die->child);
22845 }
22846 else
22847 {
22848 fprintf_unfiltered (f,
22849 " [not printed, max nesting level reached]\n");
22850 }
22851 }
22852
22853 if (die->sibling != NULL && level > 0)
22854 {
22855 dump_die_1 (f, level, max_level, die->sibling);
22856 }
22857 }
22858
22859 /* This is called from the pdie macro in gdbinit.in.
22860 It's not static so gcc will keep a copy callable from gdb. */
22861
22862 void
22863 dump_die (struct die_info *die, int max_level)
22864 {
22865 dump_die_1 (gdb_stdlog, 0, max_level, die);
22866 }
22867
22868 static void
22869 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22870 {
22871 void **slot;
22872
22873 slot = htab_find_slot_with_hash (cu->die_hash, die,
22874 to_underlying (die->sect_off),
22875 INSERT);
22876
22877 *slot = die;
22878 }
22879
22880 /* Follow reference or signature attribute ATTR of SRC_DIE.
22881 On entry *REF_CU is the CU of SRC_DIE.
22882 On exit *REF_CU is the CU of the result. */
22883
22884 static struct die_info *
22885 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22886 struct dwarf2_cu **ref_cu)
22887 {
22888 struct die_info *die;
22889
22890 if (attr->form_is_ref ())
22891 die = follow_die_ref (src_die, attr, ref_cu);
22892 else if (attr->form == DW_FORM_ref_sig8)
22893 die = follow_die_sig (src_die, attr, ref_cu);
22894 else
22895 {
22896 dump_die_for_error (src_die);
22897 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22898 objfile_name ((*ref_cu)->per_objfile->objfile));
22899 }
22900
22901 return die;
22902 }
22903
22904 /* Follow reference OFFSET.
22905 On entry *REF_CU is the CU of the source die referencing OFFSET.
22906 On exit *REF_CU is the CU of the result.
22907 Returns NULL if OFFSET is invalid. */
22908
22909 static struct die_info *
22910 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22911 struct dwarf2_cu **ref_cu)
22912 {
22913 struct die_info temp_die;
22914 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22915 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22916
22917 gdb_assert (cu->per_cu != NULL);
22918
22919 target_cu = cu;
22920
22921 if (cu->per_cu->is_debug_types)
22922 {
22923 /* .debug_types CUs cannot reference anything outside their CU.
22924 If they need to, they have to reference a signatured type via
22925 DW_FORM_ref_sig8. */
22926 if (!cu->header.offset_in_cu_p (sect_off))
22927 return NULL;
22928 }
22929 else if (offset_in_dwz != cu->per_cu->is_dwz
22930 || !cu->header.offset_in_cu_p (sect_off))
22931 {
22932 struct dwarf2_per_cu_data *per_cu;
22933
22934 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22935 per_objfile);
22936
22937 /* If necessary, add it to the queue and load its DIEs. */
22938 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
22939 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
22940
22941 target_cu = per_objfile->get_cu (per_cu);
22942 }
22943 else if (cu->dies == NULL)
22944 {
22945 /* We're loading full DIEs during partial symbol reading. */
22946 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
22947 load_full_comp_unit (cu->per_cu, per_objfile, false, language_minimal);
22948 }
22949
22950 *ref_cu = target_cu;
22951 temp_die.sect_off = sect_off;
22952
22953 if (target_cu != cu)
22954 target_cu->ancestor = cu;
22955
22956 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22957 &temp_die,
22958 to_underlying (sect_off));
22959 }
22960
22961 /* Follow reference attribute ATTR of SRC_DIE.
22962 On entry *REF_CU is the CU of SRC_DIE.
22963 On exit *REF_CU is the CU of the result. */
22964
22965 static struct die_info *
22966 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22967 struct dwarf2_cu **ref_cu)
22968 {
22969 sect_offset sect_off = attr->get_ref_die_offset ();
22970 struct dwarf2_cu *cu = *ref_cu;
22971 struct die_info *die;
22972
22973 die = follow_die_offset (sect_off,
22974 (attr->form == DW_FORM_GNU_ref_alt
22975 || cu->per_cu->is_dwz),
22976 ref_cu);
22977 if (!die)
22978 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22979 "at %s [in module %s]"),
22980 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22981 objfile_name (cu->per_objfile->objfile));
22982
22983 return die;
22984 }
22985
22986 /* See read.h. */
22987
22988 struct dwarf2_locexpr_baton
22989 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22990 dwarf2_per_cu_data *per_cu,
22991 dwarf2_per_objfile *per_objfile,
22992 gdb::function_view<CORE_ADDR ()> get_frame_pc,
22993 bool resolve_abstract_p)
22994 {
22995 struct die_info *die;
22996 struct attribute *attr;
22997 struct dwarf2_locexpr_baton retval;
22998 struct objfile *objfile = per_objfile->objfile;
22999
23000 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23001 if (cu == nullptr)
23002 cu = load_cu (per_cu, per_objfile, false);
23003
23004 if (cu == nullptr)
23005 {
23006 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23007 Instead just throw an error, not much else we can do. */
23008 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23009 sect_offset_str (sect_off), objfile_name (objfile));
23010 }
23011
23012 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23013 if (!die)
23014 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23015 sect_offset_str (sect_off), objfile_name (objfile));
23016
23017 attr = dwarf2_attr (die, DW_AT_location, cu);
23018 if (!attr && resolve_abstract_p
23019 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23020 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23021 {
23022 CORE_ADDR pc = get_frame_pc ();
23023 CORE_ADDR baseaddr = objfile->text_section_offset ();
23024 struct gdbarch *gdbarch = objfile->arch ();
23025
23026 for (const auto &cand_off
23027 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23028 {
23029 struct dwarf2_cu *cand_cu = cu;
23030 struct die_info *cand
23031 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23032 if (!cand
23033 || !cand->parent
23034 || cand->parent->tag != DW_TAG_subprogram)
23035 continue;
23036
23037 CORE_ADDR pc_low, pc_high;
23038 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23039 if (pc_low == ((CORE_ADDR) -1))
23040 continue;
23041 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23042 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23043 if (!(pc_low <= pc && pc < pc_high))
23044 continue;
23045
23046 die = cand;
23047 attr = dwarf2_attr (die, DW_AT_location, cu);
23048 break;
23049 }
23050 }
23051
23052 if (!attr)
23053 {
23054 /* DWARF: "If there is no such attribute, then there is no effect.".
23055 DATA is ignored if SIZE is 0. */
23056
23057 retval.data = NULL;
23058 retval.size = 0;
23059 }
23060 else if (attr->form_is_section_offset ())
23061 {
23062 struct dwarf2_loclist_baton loclist_baton;
23063 CORE_ADDR pc = get_frame_pc ();
23064 size_t size;
23065
23066 fill_in_loclist_baton (cu, &loclist_baton, attr);
23067
23068 retval.data = dwarf2_find_location_expression (&loclist_baton,
23069 &size, pc);
23070 retval.size = size;
23071 }
23072 else
23073 {
23074 if (!attr->form_is_block ())
23075 error (_("Dwarf Error: DIE at %s referenced in module %s "
23076 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23077 sect_offset_str (sect_off), objfile_name (objfile));
23078
23079 retval.data = DW_BLOCK (attr)->data;
23080 retval.size = DW_BLOCK (attr)->size;
23081 }
23082 retval.per_objfile = per_objfile;
23083 retval.per_cu = cu->per_cu;
23084
23085 per_objfile->age_comp_units ();
23086
23087 return retval;
23088 }
23089
23090 /* See read.h. */
23091
23092 struct dwarf2_locexpr_baton
23093 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23094 dwarf2_per_cu_data *per_cu,
23095 dwarf2_per_objfile *per_objfile,
23096 gdb::function_view<CORE_ADDR ()> get_frame_pc)
23097 {
23098 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23099
23100 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
23101 get_frame_pc);
23102 }
23103
23104 /* Write a constant of a given type as target-ordered bytes into
23105 OBSTACK. */
23106
23107 static const gdb_byte *
23108 write_constant_as_bytes (struct obstack *obstack,
23109 enum bfd_endian byte_order,
23110 struct type *type,
23111 ULONGEST value,
23112 LONGEST *len)
23113 {
23114 gdb_byte *result;
23115
23116 *len = TYPE_LENGTH (type);
23117 result = (gdb_byte *) obstack_alloc (obstack, *len);
23118 store_unsigned_integer (result, *len, byte_order, value);
23119
23120 return result;
23121 }
23122
23123 /* See read.h. */
23124
23125 const gdb_byte *
23126 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23127 dwarf2_per_cu_data *per_cu,
23128 dwarf2_per_objfile *per_objfile,
23129 obstack *obstack,
23130 LONGEST *len)
23131 {
23132 struct die_info *die;
23133 struct attribute *attr;
23134 const gdb_byte *result = NULL;
23135 struct type *type;
23136 LONGEST value;
23137 enum bfd_endian byte_order;
23138 struct objfile *objfile = per_objfile->objfile;
23139
23140 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23141 if (cu == nullptr)
23142 cu = load_cu (per_cu, per_objfile, false);
23143
23144 if (cu == nullptr)
23145 {
23146 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23147 Instead just throw an error, not much else we can do. */
23148 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23149 sect_offset_str (sect_off), objfile_name (objfile));
23150 }
23151
23152 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23153 if (!die)
23154 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23155 sect_offset_str (sect_off), objfile_name (objfile));
23156
23157 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23158 if (attr == NULL)
23159 return NULL;
23160
23161 byte_order = (bfd_big_endian (objfile->obfd)
23162 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23163
23164 switch (attr->form)
23165 {
23166 case DW_FORM_addr:
23167 case DW_FORM_addrx:
23168 case DW_FORM_GNU_addr_index:
23169 {
23170 gdb_byte *tem;
23171
23172 *len = cu->header.addr_size;
23173 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23174 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23175 result = tem;
23176 }
23177 break;
23178 case DW_FORM_string:
23179 case DW_FORM_strp:
23180 case DW_FORM_strx:
23181 case DW_FORM_GNU_str_index:
23182 case DW_FORM_GNU_strp_alt:
23183 /* DW_STRING is already allocated on the objfile obstack, point
23184 directly to it. */
23185 result = (const gdb_byte *) DW_STRING (attr);
23186 *len = strlen (DW_STRING (attr));
23187 break;
23188 case DW_FORM_block1:
23189 case DW_FORM_block2:
23190 case DW_FORM_block4:
23191 case DW_FORM_block:
23192 case DW_FORM_exprloc:
23193 case DW_FORM_data16:
23194 result = DW_BLOCK (attr)->data;
23195 *len = DW_BLOCK (attr)->size;
23196 break;
23197
23198 /* The DW_AT_const_value attributes are supposed to carry the
23199 symbol's value "represented as it would be on the target
23200 architecture." By the time we get here, it's already been
23201 converted to host endianness, so we just need to sign- or
23202 zero-extend it as appropriate. */
23203 case DW_FORM_data1:
23204 type = die_type (die, cu);
23205 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23206 if (result == NULL)
23207 result = write_constant_as_bytes (obstack, byte_order,
23208 type, value, len);
23209 break;
23210 case DW_FORM_data2:
23211 type = die_type (die, cu);
23212 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23213 if (result == NULL)
23214 result = write_constant_as_bytes (obstack, byte_order,
23215 type, value, len);
23216 break;
23217 case DW_FORM_data4:
23218 type = die_type (die, cu);
23219 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23220 if (result == NULL)
23221 result = write_constant_as_bytes (obstack, byte_order,
23222 type, value, len);
23223 break;
23224 case DW_FORM_data8:
23225 type = die_type (die, cu);
23226 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23227 if (result == NULL)
23228 result = write_constant_as_bytes (obstack, byte_order,
23229 type, value, len);
23230 break;
23231
23232 case DW_FORM_sdata:
23233 case DW_FORM_implicit_const:
23234 type = die_type (die, cu);
23235 result = write_constant_as_bytes (obstack, byte_order,
23236 type, DW_SND (attr), len);
23237 break;
23238
23239 case DW_FORM_udata:
23240 type = die_type (die, cu);
23241 result = write_constant_as_bytes (obstack, byte_order,
23242 type, DW_UNSND (attr), len);
23243 break;
23244
23245 default:
23246 complaint (_("unsupported const value attribute form: '%s'"),
23247 dwarf_form_name (attr->form));
23248 break;
23249 }
23250
23251 return result;
23252 }
23253
23254 /* See read.h. */
23255
23256 struct type *
23257 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23258 dwarf2_per_cu_data *per_cu,
23259 dwarf2_per_objfile *per_objfile)
23260 {
23261 struct die_info *die;
23262
23263 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23264 if (cu == nullptr)
23265 cu = load_cu (per_cu, per_objfile, false);
23266
23267 if (cu == nullptr)
23268 return nullptr;
23269
23270 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23271 if (!die)
23272 return NULL;
23273
23274 return die_type (die, cu);
23275 }
23276
23277 /* See read.h. */
23278
23279 struct type *
23280 dwarf2_get_die_type (cu_offset die_offset,
23281 dwarf2_per_cu_data *per_cu,
23282 dwarf2_per_objfile *per_objfile)
23283 {
23284 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23285 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
23286 }
23287
23288 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23289 On entry *REF_CU is the CU of SRC_DIE.
23290 On exit *REF_CU is the CU of the result.
23291 Returns NULL if the referenced DIE isn't found. */
23292
23293 static struct die_info *
23294 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23295 struct dwarf2_cu **ref_cu)
23296 {
23297 struct die_info temp_die;
23298 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23299 struct die_info *die;
23300 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
23301
23302
23303 /* While it might be nice to assert sig_type->type == NULL here,
23304 we can get here for DW_AT_imported_declaration where we need
23305 the DIE not the type. */
23306
23307 /* If necessary, add it to the queue and load its DIEs. */
23308
23309 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
23310 language_minimal))
23311 read_signatured_type (sig_type, per_objfile);
23312
23313 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
23314 gdb_assert (sig_cu != NULL);
23315 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23316 temp_die.sect_off = sig_type->type_offset_in_section;
23317 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23318 to_underlying (temp_die.sect_off));
23319 if (die)
23320 {
23321 /* For .gdb_index version 7 keep track of included TUs.
23322 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23323 if (per_objfile->per_bfd->index_table != NULL
23324 && per_objfile->per_bfd->index_table->version <= 7)
23325 {
23326 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23327 }
23328
23329 *ref_cu = sig_cu;
23330 if (sig_cu != cu)
23331 sig_cu->ancestor = cu;
23332
23333 return die;
23334 }
23335
23336 return NULL;
23337 }
23338
23339 /* Follow signatured type referenced by ATTR in SRC_DIE.
23340 On entry *REF_CU is the CU of SRC_DIE.
23341 On exit *REF_CU is the CU of the result.
23342 The result is the DIE of the type.
23343 If the referenced type cannot be found an error is thrown. */
23344
23345 static struct die_info *
23346 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23347 struct dwarf2_cu **ref_cu)
23348 {
23349 ULONGEST signature = DW_SIGNATURE (attr);
23350 struct signatured_type *sig_type;
23351 struct die_info *die;
23352
23353 gdb_assert (attr->form == DW_FORM_ref_sig8);
23354
23355 sig_type = lookup_signatured_type (*ref_cu, signature);
23356 /* sig_type will be NULL if the signatured type is missing from
23357 the debug info. */
23358 if (sig_type == NULL)
23359 {
23360 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23361 " from DIE at %s [in module %s]"),
23362 hex_string (signature), sect_offset_str (src_die->sect_off),
23363 objfile_name ((*ref_cu)->per_objfile->objfile));
23364 }
23365
23366 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23367 if (die == NULL)
23368 {
23369 dump_die_for_error (src_die);
23370 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23371 " from DIE at %s [in module %s]"),
23372 hex_string (signature), sect_offset_str (src_die->sect_off),
23373 objfile_name ((*ref_cu)->per_objfile->objfile));
23374 }
23375
23376 return die;
23377 }
23378
23379 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23380 reading in and processing the type unit if necessary. */
23381
23382 static struct type *
23383 get_signatured_type (struct die_info *die, ULONGEST signature,
23384 struct dwarf2_cu *cu)
23385 {
23386 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23387 struct signatured_type *sig_type;
23388 struct dwarf2_cu *type_cu;
23389 struct die_info *type_die;
23390 struct type *type;
23391
23392 sig_type = lookup_signatured_type (cu, signature);
23393 /* sig_type will be NULL if the signatured type is missing from
23394 the debug info. */
23395 if (sig_type == NULL)
23396 {
23397 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23398 " from DIE at %s [in module %s]"),
23399 hex_string (signature), sect_offset_str (die->sect_off),
23400 objfile_name (per_objfile->objfile));
23401 return build_error_marker_type (cu, die);
23402 }
23403
23404 /* If we already know the type we're done. */
23405 type = per_objfile->get_type_for_signatured_type (sig_type);
23406 if (type != nullptr)
23407 return type;
23408
23409 type_cu = cu;
23410 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23411 if (type_die != NULL)
23412 {
23413 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23414 is created. This is important, for example, because for c++ classes
23415 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23416 type = read_type_die (type_die, type_cu);
23417 if (type == NULL)
23418 {
23419 complaint (_("Dwarf Error: Cannot build signatured type %s"
23420 " referenced from DIE at %s [in module %s]"),
23421 hex_string (signature), sect_offset_str (die->sect_off),
23422 objfile_name (per_objfile->objfile));
23423 type = build_error_marker_type (cu, die);
23424 }
23425 }
23426 else
23427 {
23428 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23429 " from DIE at %s [in module %s]"),
23430 hex_string (signature), sect_offset_str (die->sect_off),
23431 objfile_name (per_objfile->objfile));
23432 type = build_error_marker_type (cu, die);
23433 }
23434
23435 per_objfile->set_type_for_signatured_type (sig_type, type);
23436
23437 return type;
23438 }
23439
23440 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23441 reading in and processing the type unit if necessary. */
23442
23443 static struct type *
23444 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23445 struct dwarf2_cu *cu) /* ARI: editCase function */
23446 {
23447 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23448 if (attr->form_is_ref ())
23449 {
23450 struct dwarf2_cu *type_cu = cu;
23451 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23452
23453 return read_type_die (type_die, type_cu);
23454 }
23455 else if (attr->form == DW_FORM_ref_sig8)
23456 {
23457 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23458 }
23459 else
23460 {
23461 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23462
23463 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23464 " at %s [in module %s]"),
23465 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23466 objfile_name (per_objfile->objfile));
23467 return build_error_marker_type (cu, die);
23468 }
23469 }
23470
23471 /* Load the DIEs associated with type unit PER_CU into memory. */
23472
23473 static void
23474 load_full_type_unit (dwarf2_per_cu_data *per_cu,
23475 dwarf2_per_objfile *per_objfile)
23476 {
23477 struct signatured_type *sig_type;
23478
23479 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23480 gdb_assert (! per_cu->type_unit_group_p ());
23481
23482 /* We have the per_cu, but we need the signatured_type.
23483 Fortunately this is an easy translation. */
23484 gdb_assert (per_cu->is_debug_types);
23485 sig_type = (struct signatured_type *) per_cu;
23486
23487 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
23488
23489 read_signatured_type (sig_type, per_objfile);
23490
23491 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
23492 }
23493
23494 /* Read in a signatured type and build its CU and DIEs.
23495 If the type is a stub for the real type in a DWO file,
23496 read in the real type from the DWO file as well. */
23497
23498 static void
23499 read_signatured_type (signatured_type *sig_type,
23500 dwarf2_per_objfile *per_objfile)
23501 {
23502 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23503
23504 gdb_assert (per_cu->is_debug_types);
23505 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
23506
23507 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
23508
23509 if (!reader.dummy_p)
23510 {
23511 struct dwarf2_cu *cu = reader.cu;
23512 const gdb_byte *info_ptr = reader.info_ptr;
23513
23514 gdb_assert (cu->die_hash == NULL);
23515 cu->die_hash =
23516 htab_create_alloc_ex (cu->header.length / 12,
23517 die_hash,
23518 die_eq,
23519 NULL,
23520 &cu->comp_unit_obstack,
23521 hashtab_obstack_allocate,
23522 dummy_obstack_deallocate);
23523
23524 if (reader.comp_unit_die->has_children)
23525 reader.comp_unit_die->child
23526 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23527 reader.comp_unit_die);
23528 cu->dies = reader.comp_unit_die;
23529 /* comp_unit_die is not stored in die_hash, no need. */
23530
23531 /* We try not to read any attributes in this function, because
23532 not all CUs needed for references have been loaded yet, and
23533 symbol table processing isn't initialized. But we have to
23534 set the CU language, or we won't be able to build types
23535 correctly. Similarly, if we do not read the producer, we can
23536 not apply producer-specific interpretation. */
23537 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23538
23539 reader.keep ();
23540 }
23541
23542 sig_type->per_cu.tu_read = 1;
23543 }
23544
23545 /* Decode simple location descriptions.
23546 Given a pointer to a dwarf block that defines a location, compute
23547 the location and return the value. If COMPUTED is non-null, it is
23548 set to true to indicate that decoding was successful, and false
23549 otherwise. If COMPUTED is null, then this function may emit a
23550 complaint. */
23551
23552 static CORE_ADDR
23553 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
23554 {
23555 struct objfile *objfile = cu->per_objfile->objfile;
23556 size_t i;
23557 size_t size = blk->size;
23558 const gdb_byte *data = blk->data;
23559 CORE_ADDR stack[64];
23560 int stacki;
23561 unsigned int bytes_read, unsnd;
23562 gdb_byte op;
23563
23564 if (computed != nullptr)
23565 *computed = false;
23566
23567 i = 0;
23568 stacki = 0;
23569 stack[stacki] = 0;
23570 stack[++stacki] = 0;
23571
23572 while (i < size)
23573 {
23574 op = data[i++];
23575 switch (op)
23576 {
23577 case DW_OP_lit0:
23578 case DW_OP_lit1:
23579 case DW_OP_lit2:
23580 case DW_OP_lit3:
23581 case DW_OP_lit4:
23582 case DW_OP_lit5:
23583 case DW_OP_lit6:
23584 case DW_OP_lit7:
23585 case DW_OP_lit8:
23586 case DW_OP_lit9:
23587 case DW_OP_lit10:
23588 case DW_OP_lit11:
23589 case DW_OP_lit12:
23590 case DW_OP_lit13:
23591 case DW_OP_lit14:
23592 case DW_OP_lit15:
23593 case DW_OP_lit16:
23594 case DW_OP_lit17:
23595 case DW_OP_lit18:
23596 case DW_OP_lit19:
23597 case DW_OP_lit20:
23598 case DW_OP_lit21:
23599 case DW_OP_lit22:
23600 case DW_OP_lit23:
23601 case DW_OP_lit24:
23602 case DW_OP_lit25:
23603 case DW_OP_lit26:
23604 case DW_OP_lit27:
23605 case DW_OP_lit28:
23606 case DW_OP_lit29:
23607 case DW_OP_lit30:
23608 case DW_OP_lit31:
23609 stack[++stacki] = op - DW_OP_lit0;
23610 break;
23611
23612 case DW_OP_reg0:
23613 case DW_OP_reg1:
23614 case DW_OP_reg2:
23615 case DW_OP_reg3:
23616 case DW_OP_reg4:
23617 case DW_OP_reg5:
23618 case DW_OP_reg6:
23619 case DW_OP_reg7:
23620 case DW_OP_reg8:
23621 case DW_OP_reg9:
23622 case DW_OP_reg10:
23623 case DW_OP_reg11:
23624 case DW_OP_reg12:
23625 case DW_OP_reg13:
23626 case DW_OP_reg14:
23627 case DW_OP_reg15:
23628 case DW_OP_reg16:
23629 case DW_OP_reg17:
23630 case DW_OP_reg18:
23631 case DW_OP_reg19:
23632 case DW_OP_reg20:
23633 case DW_OP_reg21:
23634 case DW_OP_reg22:
23635 case DW_OP_reg23:
23636 case DW_OP_reg24:
23637 case DW_OP_reg25:
23638 case DW_OP_reg26:
23639 case DW_OP_reg27:
23640 case DW_OP_reg28:
23641 case DW_OP_reg29:
23642 case DW_OP_reg30:
23643 case DW_OP_reg31:
23644 stack[++stacki] = op - DW_OP_reg0;
23645 if (i < size)
23646 {
23647 if (computed == nullptr)
23648 dwarf2_complex_location_expr_complaint ();
23649 else
23650 return 0;
23651 }
23652 break;
23653
23654 case DW_OP_regx:
23655 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23656 i += bytes_read;
23657 stack[++stacki] = unsnd;
23658 if (i < size)
23659 {
23660 if (computed == nullptr)
23661 dwarf2_complex_location_expr_complaint ();
23662 else
23663 return 0;
23664 }
23665 break;
23666
23667 case DW_OP_addr:
23668 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23669 &bytes_read);
23670 i += bytes_read;
23671 break;
23672
23673 case DW_OP_const1u:
23674 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23675 i += 1;
23676 break;
23677
23678 case DW_OP_const1s:
23679 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23680 i += 1;
23681 break;
23682
23683 case DW_OP_const2u:
23684 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23685 i += 2;
23686 break;
23687
23688 case DW_OP_const2s:
23689 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23690 i += 2;
23691 break;
23692
23693 case DW_OP_const4u:
23694 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23695 i += 4;
23696 break;
23697
23698 case DW_OP_const4s:
23699 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23700 i += 4;
23701 break;
23702
23703 case DW_OP_const8u:
23704 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23705 i += 8;
23706 break;
23707
23708 case DW_OP_constu:
23709 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23710 &bytes_read);
23711 i += bytes_read;
23712 break;
23713
23714 case DW_OP_consts:
23715 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23716 i += bytes_read;
23717 break;
23718
23719 case DW_OP_dup:
23720 stack[stacki + 1] = stack[stacki];
23721 stacki++;
23722 break;
23723
23724 case DW_OP_plus:
23725 stack[stacki - 1] += stack[stacki];
23726 stacki--;
23727 break;
23728
23729 case DW_OP_plus_uconst:
23730 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23731 &bytes_read);
23732 i += bytes_read;
23733 break;
23734
23735 case DW_OP_minus:
23736 stack[stacki - 1] -= stack[stacki];
23737 stacki--;
23738 break;
23739
23740 case DW_OP_deref:
23741 /* If we're not the last op, then we definitely can't encode
23742 this using GDB's address_class enum. This is valid for partial
23743 global symbols, although the variable's address will be bogus
23744 in the psymtab. */
23745 if (i < size)
23746 {
23747 if (computed == nullptr)
23748 dwarf2_complex_location_expr_complaint ();
23749 else
23750 return 0;
23751 }
23752 break;
23753
23754 case DW_OP_GNU_push_tls_address:
23755 case DW_OP_form_tls_address:
23756 /* The top of the stack has the offset from the beginning
23757 of the thread control block at which the variable is located. */
23758 /* Nothing should follow this operator, so the top of stack would
23759 be returned. */
23760 /* This is valid for partial global symbols, but the variable's
23761 address will be bogus in the psymtab. Make it always at least
23762 non-zero to not look as a variable garbage collected by linker
23763 which have DW_OP_addr 0. */
23764 if (i < size)
23765 {
23766 if (computed == nullptr)
23767 dwarf2_complex_location_expr_complaint ();
23768 else
23769 return 0;
23770 }
23771 stack[stacki]++;
23772 break;
23773
23774 case DW_OP_GNU_uninit:
23775 if (computed != nullptr)
23776 return 0;
23777 break;
23778
23779 case DW_OP_addrx:
23780 case DW_OP_GNU_addr_index:
23781 case DW_OP_GNU_const_index:
23782 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23783 &bytes_read);
23784 i += bytes_read;
23785 break;
23786
23787 default:
23788 if (computed == nullptr)
23789 {
23790 const char *name = get_DW_OP_name (op);
23791
23792 if (name)
23793 complaint (_("unsupported stack op: '%s'"),
23794 name);
23795 else
23796 complaint (_("unsupported stack op: '%02x'"),
23797 op);
23798 }
23799
23800 return (stack[stacki]);
23801 }
23802
23803 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23804 outside of the allocated space. Also enforce minimum>0. */
23805 if (stacki >= ARRAY_SIZE (stack) - 1)
23806 {
23807 if (computed == nullptr)
23808 complaint (_("location description stack overflow"));
23809 return 0;
23810 }
23811
23812 if (stacki <= 0)
23813 {
23814 if (computed == nullptr)
23815 complaint (_("location description stack underflow"));
23816 return 0;
23817 }
23818 }
23819
23820 if (computed != nullptr)
23821 *computed = true;
23822 return (stack[stacki]);
23823 }
23824
23825 /* memory allocation interface */
23826
23827 static struct dwarf_block *
23828 dwarf_alloc_block (struct dwarf2_cu *cu)
23829 {
23830 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23831 }
23832
23833 static struct die_info *
23834 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23835 {
23836 struct die_info *die;
23837 size_t size = sizeof (struct die_info);
23838
23839 if (num_attrs > 1)
23840 size += (num_attrs - 1) * sizeof (struct attribute);
23841
23842 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23843 memset (die, 0, sizeof (struct die_info));
23844 return (die);
23845 }
23846
23847 \f
23848
23849 /* Macro support. */
23850
23851 /* An overload of dwarf_decode_macros that finds the correct section
23852 and ensures it is read in before calling the other overload. */
23853
23854 static void
23855 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23856 int section_is_gnu)
23857 {
23858 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23859 struct objfile *objfile = per_objfile->objfile;
23860 const struct line_header *lh = cu->line_header;
23861 unsigned int offset_size = cu->header.offset_size;
23862 struct dwarf2_section_info *section;
23863 const char *section_name;
23864
23865 if (cu->dwo_unit != nullptr)
23866 {
23867 if (section_is_gnu)
23868 {
23869 section = &cu->dwo_unit->dwo_file->sections.macro;
23870 section_name = ".debug_macro.dwo";
23871 }
23872 else
23873 {
23874 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23875 section_name = ".debug_macinfo.dwo";
23876 }
23877 }
23878 else
23879 {
23880 if (section_is_gnu)
23881 {
23882 section = &per_objfile->per_bfd->macro;
23883 section_name = ".debug_macro";
23884 }
23885 else
23886 {
23887 section = &per_objfile->per_bfd->macinfo;
23888 section_name = ".debug_macinfo";
23889 }
23890 }
23891
23892 section->read (objfile);
23893 if (section->buffer == nullptr)
23894 {
23895 complaint (_("missing %s section"), section_name);
23896 return;
23897 }
23898
23899 buildsym_compunit *builder = cu->get_builder ();
23900
23901 dwarf_decode_macros (per_objfile, builder, section, lh,
23902 offset_size, offset, section_is_gnu);
23903 }
23904
23905 /* Return the .debug_loc section to use for CU.
23906 For DWO files use .debug_loc.dwo. */
23907
23908 static struct dwarf2_section_info *
23909 cu_debug_loc_section (struct dwarf2_cu *cu)
23910 {
23911 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23912
23913 if (cu->dwo_unit)
23914 {
23915 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23916
23917 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23918 }
23919 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
23920 : &per_objfile->per_bfd->loc);
23921 }
23922
23923 /* Return the .debug_rnglists section to use for CU. */
23924 static struct dwarf2_section_info *
23925 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
23926 {
23927 if (cu->header.version < 5)
23928 error (_(".debug_rnglists section cannot be used in DWARF %d"),
23929 cu->header.version);
23930 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23931
23932 /* Make sure we read the .debug_rnglists section from the file that
23933 contains the DW_AT_ranges attribute we are reading. Normally that
23934 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
23935 or DW_TAG_skeleton unit, we always want to read from objfile/linked
23936 program. */
23937 if (cu->dwo_unit != nullptr
23938 && tag != DW_TAG_compile_unit
23939 && tag != DW_TAG_skeleton_unit)
23940 {
23941 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23942
23943 if (sections->rnglists.size > 0)
23944 return &sections->rnglists;
23945 else
23946 error (_(".debug_rnglists section is missing from .dwo file."));
23947 }
23948 return &dwarf2_per_objfile->per_bfd->rnglists;
23949 }
23950
23951 /* A helper function that fills in a dwarf2_loclist_baton. */
23952
23953 static void
23954 fill_in_loclist_baton (struct dwarf2_cu *cu,
23955 struct dwarf2_loclist_baton *baton,
23956 const struct attribute *attr)
23957 {
23958 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23959 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23960
23961 section->read (per_objfile->objfile);
23962
23963 baton->per_objfile = per_objfile;
23964 baton->per_cu = cu->per_cu;
23965 gdb_assert (baton->per_cu);
23966 /* We don't know how long the location list is, but make sure we
23967 don't run off the edge of the section. */
23968 baton->size = section->size - DW_UNSND (attr);
23969 baton->data = section->buffer + DW_UNSND (attr);
23970 if (cu->base_address.has_value ())
23971 baton->base_address = *cu->base_address;
23972 else
23973 baton->base_address = 0;
23974 baton->from_dwo = cu->dwo_unit != NULL;
23975 }
23976
23977 static void
23978 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23979 struct dwarf2_cu *cu, int is_block)
23980 {
23981 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23982 struct objfile *objfile = per_objfile->objfile;
23983 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23984
23985 if (attr->form_is_section_offset ()
23986 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23987 the section. If so, fall through to the complaint in the
23988 other branch. */
23989 && DW_UNSND (attr) < section->get_size (objfile))
23990 {
23991 struct dwarf2_loclist_baton *baton;
23992
23993 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23994
23995 fill_in_loclist_baton (cu, baton, attr);
23996
23997 if (!cu->base_address.has_value ())
23998 complaint (_("Location list used without "
23999 "specifying the CU base address."));
24000
24001 SYMBOL_ACLASS_INDEX (sym) = (is_block
24002 ? dwarf2_loclist_block_index
24003 : dwarf2_loclist_index);
24004 SYMBOL_LOCATION_BATON (sym) = baton;
24005 }
24006 else
24007 {
24008 struct dwarf2_locexpr_baton *baton;
24009
24010 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24011 baton->per_objfile = per_objfile;
24012 baton->per_cu = cu->per_cu;
24013 gdb_assert (baton->per_cu);
24014
24015 if (attr->form_is_block ())
24016 {
24017 /* Note that we're just copying the block's data pointer
24018 here, not the actual data. We're still pointing into the
24019 info_buffer for SYM's objfile; right now we never release
24020 that buffer, but when we do clean up properly this may
24021 need to change. */
24022 baton->size = DW_BLOCK (attr)->size;
24023 baton->data = DW_BLOCK (attr)->data;
24024 }
24025 else
24026 {
24027 dwarf2_invalid_attrib_class_complaint ("location description",
24028 sym->natural_name ());
24029 baton->size = 0;
24030 }
24031
24032 SYMBOL_ACLASS_INDEX (sym) = (is_block
24033 ? dwarf2_locexpr_block_index
24034 : dwarf2_locexpr_index);
24035 SYMBOL_LOCATION_BATON (sym) = baton;
24036 }
24037 }
24038
24039 /* See read.h. */
24040
24041 const comp_unit_head *
24042 dwarf2_per_cu_data::get_header () const
24043 {
24044 if (!m_header_read_in)
24045 {
24046 const gdb_byte *info_ptr
24047 = this->section->buffer + to_underlying (this->sect_off);
24048
24049 memset (&m_header, 0, sizeof (m_header));
24050
24051 read_comp_unit_head (&m_header, info_ptr, this->section,
24052 rcuh_kind::COMPILE);
24053 }
24054
24055 return &m_header;
24056 }
24057
24058 /* See read.h. */
24059
24060 int
24061 dwarf2_per_cu_data::addr_size () const
24062 {
24063 return this->get_header ()->addr_size;
24064 }
24065
24066 /* See read.h. */
24067
24068 int
24069 dwarf2_per_cu_data::offset_size () const
24070 {
24071 return this->get_header ()->offset_size;
24072 }
24073
24074 /* See read.h. */
24075
24076 int
24077 dwarf2_per_cu_data::ref_addr_size () const
24078 {
24079 const comp_unit_head *header = this->get_header ();
24080
24081 if (header->version == 2)
24082 return header->addr_size;
24083 else
24084 return header->offset_size;
24085 }
24086
24087 /* See read.h. */
24088
24089 struct type *
24090 dwarf2_cu::addr_type () const
24091 {
24092 struct objfile *objfile = this->per_objfile->objfile;
24093 struct type *void_type = objfile_type (objfile)->builtin_void;
24094 struct type *addr_type = lookup_pointer_type (void_type);
24095 int addr_size = this->per_cu->addr_size ();
24096
24097 if (TYPE_LENGTH (addr_type) == addr_size)
24098 return addr_type;
24099
24100 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
24101 return addr_type;
24102 }
24103
24104 /* A helper function for dwarf2_find_containing_comp_unit that returns
24105 the index of the result, and that searches a vector. It will
24106 return a result even if the offset in question does not actually
24107 occur in any CU. This is separate so that it can be unit
24108 tested. */
24109
24110 static int
24111 dwarf2_find_containing_comp_unit
24112 (sect_offset sect_off,
24113 unsigned int offset_in_dwz,
24114 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
24115 {
24116 int low, high;
24117
24118 low = 0;
24119 high = all_comp_units.size () - 1;
24120 while (high > low)
24121 {
24122 struct dwarf2_per_cu_data *mid_cu;
24123 int mid = low + (high - low) / 2;
24124
24125 mid_cu = all_comp_units[mid];
24126 if (mid_cu->is_dwz > offset_in_dwz
24127 || (mid_cu->is_dwz == offset_in_dwz
24128 && mid_cu->sect_off + mid_cu->length > sect_off))
24129 high = mid;
24130 else
24131 low = mid + 1;
24132 }
24133 gdb_assert (low == high);
24134 return low;
24135 }
24136
24137 /* Locate the .debug_info compilation unit from CU's objfile which contains
24138 the DIE at OFFSET. Raises an error on failure. */
24139
24140 static struct dwarf2_per_cu_data *
24141 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24142 unsigned int offset_in_dwz,
24143 dwarf2_per_objfile *per_objfile)
24144 {
24145 int low = dwarf2_find_containing_comp_unit
24146 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24147 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
24148
24149 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24150 {
24151 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24152 error (_("Dwarf Error: could not find partial DIE containing "
24153 "offset %s [in module %s]"),
24154 sect_offset_str (sect_off),
24155 bfd_get_filename (per_objfile->objfile->obfd));
24156
24157 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
24158 <= sect_off);
24159 return per_objfile->per_bfd->all_comp_units[low-1];
24160 }
24161 else
24162 {
24163 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
24164 && sect_off >= this_cu->sect_off + this_cu->length)
24165 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24166 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24167 return this_cu;
24168 }
24169 }
24170
24171 #if GDB_SELF_TEST
24172
24173 namespace selftests {
24174 namespace find_containing_comp_unit {
24175
24176 static void
24177 run_test ()
24178 {
24179 struct dwarf2_per_cu_data one {};
24180 struct dwarf2_per_cu_data two {};
24181 struct dwarf2_per_cu_data three {};
24182 struct dwarf2_per_cu_data four {};
24183
24184 one.length = 5;
24185 two.sect_off = sect_offset (one.length);
24186 two.length = 7;
24187
24188 three.length = 5;
24189 three.is_dwz = 1;
24190 four.sect_off = sect_offset (three.length);
24191 four.length = 7;
24192 four.is_dwz = 1;
24193
24194 std::vector<dwarf2_per_cu_data *> units;
24195 units.push_back (&one);
24196 units.push_back (&two);
24197 units.push_back (&three);
24198 units.push_back (&four);
24199
24200 int result;
24201
24202 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24203 SELF_CHECK (units[result] == &one);
24204 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24205 SELF_CHECK (units[result] == &one);
24206 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24207 SELF_CHECK (units[result] == &two);
24208
24209 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24210 SELF_CHECK (units[result] == &three);
24211 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24212 SELF_CHECK (units[result] == &three);
24213 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24214 SELF_CHECK (units[result] == &four);
24215 }
24216
24217 }
24218 }
24219
24220 #endif /* GDB_SELF_TEST */
24221
24222 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
24223
24224 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
24225 dwarf2_per_objfile *per_objfile)
24226 : per_cu (per_cu),
24227 per_objfile (per_objfile),
24228 mark (false),
24229 has_loclist (false),
24230 checked_producer (false),
24231 producer_is_gxx_lt_4_6 (false),
24232 producer_is_gcc_lt_4_3 (false),
24233 producer_is_icc (false),
24234 producer_is_icc_lt_14 (false),
24235 producer_is_codewarrior (false),
24236 processing_has_namespace_info (false)
24237 {
24238 }
24239
24240 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24241
24242 static void
24243 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24244 enum language pretend_language)
24245 {
24246 struct attribute *attr;
24247
24248 /* Set the language we're debugging. */
24249 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24250 if (attr != nullptr)
24251 set_cu_language (DW_UNSND (attr), cu);
24252 else
24253 {
24254 cu->language = pretend_language;
24255 cu->language_defn = language_def (cu->language);
24256 }
24257
24258 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24259 }
24260
24261 /* See read.h. */
24262
24263 dwarf2_cu *
24264 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
24265 {
24266 auto it = m_dwarf2_cus.find (per_cu);
24267 if (it == m_dwarf2_cus.end ())
24268 return nullptr;
24269
24270 return it->second;
24271 }
24272
24273 /* See read.h. */
24274
24275 void
24276 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24277 {
24278 gdb_assert (this->get_cu (per_cu) == nullptr);
24279
24280 m_dwarf2_cus[per_cu] = cu;
24281 }
24282
24283 /* See read.h. */
24284
24285 void
24286 dwarf2_per_objfile::age_comp_units ()
24287 {
24288 /* Start by clearing all marks. */
24289 for (auto pair : m_dwarf2_cus)
24290 pair.second->mark = false;
24291
24292 /* Traverse all CUs, mark them and their dependencies if used recently
24293 enough. */
24294 for (auto pair : m_dwarf2_cus)
24295 {
24296 dwarf2_cu *cu = pair.second;
24297
24298 cu->last_used++;
24299 if (cu->last_used <= dwarf_max_cache_age)
24300 dwarf2_mark (cu);
24301 }
24302
24303 /* Delete all CUs still not marked. */
24304 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
24305 {
24306 dwarf2_cu *cu = it->second;
24307
24308 if (!cu->mark)
24309 {
24310 delete cu;
24311 it = m_dwarf2_cus.erase (it);
24312 }
24313 else
24314 it++;
24315 }
24316 }
24317
24318 /* See read.h. */
24319
24320 void
24321 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
24322 {
24323 auto it = m_dwarf2_cus.find (per_cu);
24324 if (it == m_dwarf2_cus.end ())
24325 return;
24326
24327 delete it->second;
24328
24329 m_dwarf2_cus.erase (it);
24330 }
24331
24332 dwarf2_per_objfile::~dwarf2_per_objfile ()
24333 {
24334 remove_all_cus ();
24335 }
24336
24337 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24338 We store these in a hash table separate from the DIEs, and preserve them
24339 when the DIEs are flushed out of cache.
24340
24341 The CU "per_cu" pointer is needed because offset alone is not enough to
24342 uniquely identify the type. A file may have multiple .debug_types sections,
24343 or the type may come from a DWO file. Furthermore, while it's more logical
24344 to use per_cu->section+offset, with Fission the section with the data is in
24345 the DWO file but we don't know that section at the point we need it.
24346 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24347 because we can enter the lookup routine, get_die_type_at_offset, from
24348 outside this file, and thus won't necessarily have PER_CU->cu.
24349 Fortunately, PER_CU is stable for the life of the objfile. */
24350
24351 struct dwarf2_per_cu_offset_and_type
24352 {
24353 const struct dwarf2_per_cu_data *per_cu;
24354 sect_offset sect_off;
24355 struct type *type;
24356 };
24357
24358 /* Hash function for a dwarf2_per_cu_offset_and_type. */
24359
24360 static hashval_t
24361 per_cu_offset_and_type_hash (const void *item)
24362 {
24363 const struct dwarf2_per_cu_offset_and_type *ofs
24364 = (const struct dwarf2_per_cu_offset_and_type *) item;
24365
24366 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
24367 }
24368
24369 /* Equality function for a dwarf2_per_cu_offset_and_type. */
24370
24371 static int
24372 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
24373 {
24374 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24375 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24376 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24377 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
24378
24379 return (ofs_lhs->per_cu == ofs_rhs->per_cu
24380 && ofs_lhs->sect_off == ofs_rhs->sect_off);
24381 }
24382
24383 /* Set the type associated with DIE to TYPE. Save it in CU's hash
24384 table if necessary. For convenience, return TYPE.
24385
24386 The DIEs reading must have careful ordering to:
24387 * Not cause infinite loops trying to read in DIEs as a prerequisite for
24388 reading current DIE.
24389 * Not trying to dereference contents of still incompletely read in types
24390 while reading in other DIEs.
24391 * Enable referencing still incompletely read in types just by a pointer to
24392 the type without accessing its fields.
24393
24394 Therefore caller should follow these rules:
24395 * Try to fetch any prerequisite types we may need to build this DIE type
24396 before building the type and calling set_die_type.
24397 * After building type call set_die_type for current DIE as soon as
24398 possible before fetching more types to complete the current type.
24399 * Make the type as complete as possible before fetching more types. */
24400
24401 static struct type *
24402 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
24403 {
24404 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24405 struct dwarf2_per_cu_offset_and_type **slot, ofs;
24406 struct objfile *objfile = per_objfile->objfile;
24407 struct attribute *attr;
24408 struct dynamic_prop prop;
24409
24410 /* For Ada types, make sure that the gnat-specific data is always
24411 initialized (if not already set). There are a few types where
24412 we should not be doing so, because the type-specific area is
24413 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24414 where the type-specific area is used to store the floatformat).
24415 But this is not a problem, because the gnat-specific information
24416 is actually not needed for these types. */
24417 if (need_gnat_info (cu)
24418 && type->code () != TYPE_CODE_FUNC
24419 && type->code () != TYPE_CODE_FLT
24420 && type->code () != TYPE_CODE_METHODPTR
24421 && type->code () != TYPE_CODE_MEMBERPTR
24422 && type->code () != TYPE_CODE_METHOD
24423 && !HAVE_GNAT_AUX_INFO (type))
24424 INIT_GNAT_SPECIFIC (type);
24425
24426 /* Read DW_AT_allocated and set in type. */
24427 attr = dwarf2_attr (die, DW_AT_allocated, cu);
24428 if (attr != NULL)
24429 {
24430 struct type *prop_type = cu->addr_sized_int_type (false);
24431 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24432 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
24433 }
24434
24435 /* Read DW_AT_associated and set in type. */
24436 attr = dwarf2_attr (die, DW_AT_associated, cu);
24437 if (attr != NULL)
24438 {
24439 struct type *prop_type = cu->addr_sized_int_type (false);
24440 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24441 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
24442 }
24443
24444 /* Read DW_AT_data_location and set in type. */
24445 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24446 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
24447 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
24448
24449 if (per_objfile->die_type_hash == NULL)
24450 per_objfile->die_type_hash
24451 = htab_up (htab_create_alloc (127,
24452 per_cu_offset_and_type_hash,
24453 per_cu_offset_and_type_eq,
24454 NULL, xcalloc, xfree));
24455
24456 ofs.per_cu = cu->per_cu;
24457 ofs.sect_off = die->sect_off;
24458 ofs.type = type;
24459 slot = (struct dwarf2_per_cu_offset_and_type **)
24460 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
24461 if (*slot)
24462 complaint (_("A problem internal to GDB: DIE %s has type already set"),
24463 sect_offset_str (die->sect_off));
24464 *slot = XOBNEW (&objfile->objfile_obstack,
24465 struct dwarf2_per_cu_offset_and_type);
24466 **slot = ofs;
24467 return type;
24468 }
24469
24470 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
24471 or return NULL if the die does not have a saved type. */
24472
24473 static struct type *
24474 get_die_type_at_offset (sect_offset sect_off,
24475 dwarf2_per_cu_data *per_cu,
24476 dwarf2_per_objfile *per_objfile)
24477 {
24478 struct dwarf2_per_cu_offset_and_type *slot, ofs;
24479
24480 if (per_objfile->die_type_hash == NULL)
24481 return NULL;
24482
24483 ofs.per_cu = per_cu;
24484 ofs.sect_off = sect_off;
24485 slot = ((struct dwarf2_per_cu_offset_and_type *)
24486 htab_find (per_objfile->die_type_hash.get (), &ofs));
24487 if (slot)
24488 return slot->type;
24489 else
24490 return NULL;
24491 }
24492
24493 /* Look up the type for DIE in CU in die_type_hash,
24494 or return NULL if DIE does not have a saved type. */
24495
24496 static struct type *
24497 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24498 {
24499 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
24500 }
24501
24502 /* Add a dependence relationship from CU to REF_PER_CU. */
24503
24504 static void
24505 dwarf2_add_dependence (struct dwarf2_cu *cu,
24506 struct dwarf2_per_cu_data *ref_per_cu)
24507 {
24508 void **slot;
24509
24510 if (cu->dependencies == NULL)
24511 cu->dependencies
24512 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
24513 NULL, &cu->comp_unit_obstack,
24514 hashtab_obstack_allocate,
24515 dummy_obstack_deallocate);
24516
24517 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
24518 if (*slot == NULL)
24519 *slot = ref_per_cu;
24520 }
24521
24522 /* Subroutine of dwarf2_mark to pass to htab_traverse.
24523 Set the mark field in every compilation unit in the
24524 cache that we must keep because we are keeping CU.
24525
24526 DATA is the dwarf2_per_objfile object in which to look up CUs. */
24527
24528 static int
24529 dwarf2_mark_helper (void **slot, void *data)
24530 {
24531 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
24532 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
24533 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
24534
24535 /* cu->dependencies references may not yet have been ever read if QUIT aborts
24536 reading of the chain. As such dependencies remain valid it is not much
24537 useful to track and undo them during QUIT cleanups. */
24538 if (cu == nullptr)
24539 return 1;
24540
24541 if (cu->mark)
24542 return 1;
24543
24544 cu->mark = true;
24545
24546 if (cu->dependencies != nullptr)
24547 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
24548
24549 return 1;
24550 }
24551
24552 /* Set the mark field in CU and in every other compilation unit in the
24553 cache that we must keep because we are keeping CU. */
24554
24555 static void
24556 dwarf2_mark (struct dwarf2_cu *cu)
24557 {
24558 if (cu->mark)
24559 return;
24560
24561 cu->mark = true;
24562
24563 if (cu->dependencies != nullptr)
24564 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
24565 }
24566
24567 /* Trivial hash function for partial_die_info: the hash value of a DIE
24568 is its offset in .debug_info for this objfile. */
24569
24570 static hashval_t
24571 partial_die_hash (const void *item)
24572 {
24573 const struct partial_die_info *part_die
24574 = (const struct partial_die_info *) item;
24575
24576 return to_underlying (part_die->sect_off);
24577 }
24578
24579 /* Trivial comparison function for partial_die_info structures: two DIEs
24580 are equal if they have the same offset. */
24581
24582 static int
24583 partial_die_eq (const void *item_lhs, const void *item_rhs)
24584 {
24585 const struct partial_die_info *part_die_lhs
24586 = (const struct partial_die_info *) item_lhs;
24587 const struct partial_die_info *part_die_rhs
24588 = (const struct partial_die_info *) item_rhs;
24589
24590 return part_die_lhs->sect_off == part_die_rhs->sect_off;
24591 }
24592
24593 struct cmd_list_element *set_dwarf_cmdlist;
24594 struct cmd_list_element *show_dwarf_cmdlist;
24595
24596 static void
24597 show_check_physname (struct ui_file *file, int from_tty,
24598 struct cmd_list_element *c, const char *value)
24599 {
24600 fprintf_filtered (file,
24601 _("Whether to check \"physname\" is %s.\n"),
24602 value);
24603 }
24604
24605 void _initialize_dwarf2_read ();
24606 void
24607 _initialize_dwarf2_read ()
24608 {
24609 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
24610 Set DWARF specific variables.\n\
24611 Configure DWARF variables such as the cache size."),
24612 &set_dwarf_cmdlist, "maintenance set dwarf ",
24613 0/*allow-unknown*/, &maintenance_set_cmdlist);
24614
24615 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
24616 Show DWARF specific variables.\n\
24617 Show DWARF variables such as the cache size."),
24618 &show_dwarf_cmdlist, "maintenance show dwarf ",
24619 0/*allow-unknown*/, &maintenance_show_cmdlist);
24620
24621 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24622 &dwarf_max_cache_age, _("\
24623 Set the upper bound on the age of cached DWARF compilation units."), _("\
24624 Show the upper bound on the age of cached DWARF compilation units."), _("\
24625 A higher limit means that cached compilation units will be stored\n\
24626 in memory longer, and more total memory will be used. Zero disables\n\
24627 caching, which can slow down startup."),
24628 NULL,
24629 show_dwarf_max_cache_age,
24630 &set_dwarf_cmdlist,
24631 &show_dwarf_cmdlist);
24632
24633 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24634 Set debugging of the DWARF reader."), _("\
24635 Show debugging of the DWARF reader."), _("\
24636 When enabled (non-zero), debugging messages are printed during DWARF\n\
24637 reading and symtab expansion. A value of 1 (one) provides basic\n\
24638 information. A value greater than 1 provides more verbose information."),
24639 NULL,
24640 NULL,
24641 &setdebuglist, &showdebuglist);
24642
24643 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24644 Set debugging of the DWARF DIE reader."), _("\
24645 Show debugging of the DWARF DIE reader."), _("\
24646 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24647 The value is the maximum depth to print."),
24648 NULL,
24649 NULL,
24650 &setdebuglist, &showdebuglist);
24651
24652 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24653 Set debugging of the dwarf line reader."), _("\
24654 Show debugging of the dwarf line reader."), _("\
24655 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24656 A value of 1 (one) provides basic information.\n\
24657 A value greater than 1 provides more verbose information."),
24658 NULL,
24659 NULL,
24660 &setdebuglist, &showdebuglist);
24661
24662 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24663 Set cross-checking of \"physname\" code against demangler."), _("\
24664 Show cross-checking of \"physname\" code against demangler."), _("\
24665 When enabled, GDB's internal \"physname\" code is checked against\n\
24666 the demangler."),
24667 NULL, show_check_physname,
24668 &setdebuglist, &showdebuglist);
24669
24670 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24671 no_class, &use_deprecated_index_sections, _("\
24672 Set whether to use deprecated gdb_index sections."), _("\
24673 Show whether to use deprecated gdb_index sections."), _("\
24674 When enabled, deprecated .gdb_index sections are used anyway.\n\
24675 Normally they are ignored either because of a missing feature or\n\
24676 performance issue.\n\
24677 Warning: This option must be enabled before gdb reads the file."),
24678 NULL,
24679 NULL,
24680 &setlist, &showlist);
24681
24682 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24683 &dwarf2_locexpr_funcs);
24684 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24685 &dwarf2_loclist_funcs);
24686
24687 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24688 &dwarf2_block_frame_base_locexpr_funcs);
24689 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24690 &dwarf2_block_frame_base_loclist_funcs);
24691
24692 #if GDB_SELF_TEST
24693 selftests::register_test ("dw2_expand_symtabs_matching",
24694 selftests::dw2_expand_symtabs_matching::run_test);
24695 selftests::register_test ("dwarf2_find_containing_comp_unit",
24696 selftests::find_containing_comp_unit::run_test);
24697 #endif
24698 }
This page took 0.76113 seconds and 5 git commands to generate.