gdb: remove TYPE_VARARGS
[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, void *arg)
2124 {
2125 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2126
2127 /* Note that we only support the standard ELF names, because .dwz
2128 is ELF-only (at the time of writing). */
2129 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2130 {
2131 dwz_file->abbrev.s.section = sectp;
2132 dwz_file->abbrev.size = bfd_section_size (sectp);
2133 }
2134 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2135 {
2136 dwz_file->info.s.section = sectp;
2137 dwz_file->info.size = bfd_section_size (sectp);
2138 }
2139 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2140 {
2141 dwz_file->str.s.section = sectp;
2142 dwz_file->str.size = bfd_section_size (sectp);
2143 }
2144 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2145 {
2146 dwz_file->line.s.section = sectp;
2147 dwz_file->line.size = bfd_section_size (sectp);
2148 }
2149 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2150 {
2151 dwz_file->macro.s.section = sectp;
2152 dwz_file->macro.size = bfd_section_size (sectp);
2153 }
2154 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2155 {
2156 dwz_file->gdb_index.s.section = sectp;
2157 dwz_file->gdb_index.size = bfd_section_size (sectp);
2158 }
2159 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2160 {
2161 dwz_file->debug_names.s.section = sectp;
2162 dwz_file->debug_names.size = bfd_section_size (sectp);
2163 }
2164 }
2165
2166 /* See dwarf2read.h. */
2167
2168 struct dwz_file *
2169 dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
2170 {
2171 const char *filename;
2172 bfd_size_type buildid_len_arg;
2173 size_t buildid_len;
2174 bfd_byte *buildid;
2175
2176 if (per_bfd->dwz_file != NULL)
2177 return per_bfd->dwz_file.get ();
2178
2179 bfd_set_error (bfd_error_no_error);
2180 gdb::unique_xmalloc_ptr<char> data
2181 (bfd_get_alt_debug_link_info (per_bfd->obfd,
2182 &buildid_len_arg, &buildid));
2183 if (data == NULL)
2184 {
2185 if (bfd_get_error () == bfd_error_no_error)
2186 return NULL;
2187 error (_("could not read '.gnu_debugaltlink' section: %s"),
2188 bfd_errmsg (bfd_get_error ()));
2189 }
2190
2191 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2192
2193 buildid_len = (size_t) buildid_len_arg;
2194
2195 filename = data.get ();
2196
2197 std::string abs_storage;
2198 if (!IS_ABSOLUTE_PATH (filename))
2199 {
2200 gdb::unique_xmalloc_ptr<char> abs
2201 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
2202
2203 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2204 filename = abs_storage.c_str ();
2205 }
2206
2207 /* First try the file name given in the section. If that doesn't
2208 work, try to use the build-id instead. */
2209 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
2210 if (dwz_bfd != NULL)
2211 {
2212 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2213 dwz_bfd.reset (nullptr);
2214 }
2215
2216 if (dwz_bfd == NULL)
2217 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2218
2219 if (dwz_bfd == nullptr)
2220 {
2221 gdb::unique_xmalloc_ptr<char> alt_filename;
2222 const char *origname = bfd_get_filename (per_bfd->obfd);
2223
2224 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2225 buildid_len,
2226 origname,
2227 &alt_filename));
2228
2229 if (fd.get () >= 0)
2230 {
2231 /* File successfully retrieved from server. */
2232 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
2233
2234 if (dwz_bfd == nullptr)
2235 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2236 alt_filename.get ());
2237 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2238 dwz_bfd.reset (nullptr);
2239 }
2240 }
2241
2242 if (dwz_bfd == NULL)
2243 error (_("could not find '.gnu_debugaltlink' file for %s"),
2244 bfd_get_filename (per_bfd->obfd));
2245
2246 std::unique_ptr<struct dwz_file> result
2247 (new struct dwz_file (std::move (dwz_bfd)));
2248
2249 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2250 result.get ());
2251
2252 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2253 per_bfd->dwz_file = std::move (result);
2254 return per_bfd->dwz_file.get ();
2255 }
2256 \f
2257 /* DWARF quick_symbols_functions support. */
2258
2259 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2260 unique line tables, so we maintain a separate table of all .debug_line
2261 derived entries to support the sharing.
2262 All the quick functions need is the list of file names. We discard the
2263 line_header when we're done and don't need to record it here. */
2264 struct quick_file_names
2265 {
2266 /* The data used to construct the hash key. */
2267 struct stmt_list_hash hash;
2268
2269 /* The number of entries in file_names, real_names. */
2270 unsigned int num_file_names;
2271
2272 /* The file names from the line table, after being run through
2273 file_full_name. */
2274 const char **file_names;
2275
2276 /* The file names from the line table after being run through
2277 gdb_realpath. These are computed lazily. */
2278 const char **real_names;
2279 };
2280
2281 /* When using the index (and thus not using psymtabs), each CU has an
2282 object of this type. This is used to hold information needed by
2283 the various "quick" methods. */
2284 struct dwarf2_per_cu_quick_data
2285 {
2286 /* The file table. This can be NULL if there was no file table
2287 or it's currently not read in.
2288 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2289 struct quick_file_names *file_names;
2290
2291 /* A temporary mark bit used when iterating over all CUs in
2292 expand_symtabs_matching. */
2293 unsigned int mark : 1;
2294
2295 /* True if we've tried to read the file table and found there isn't one.
2296 There will be no point in trying to read it again next time. */
2297 unsigned int no_file_data : 1;
2298 };
2299
2300 /* Utility hash function for a stmt_list_hash. */
2301
2302 static hashval_t
2303 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2304 {
2305 hashval_t v = 0;
2306
2307 if (stmt_list_hash->dwo_unit != NULL)
2308 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2309 v += to_underlying (stmt_list_hash->line_sect_off);
2310 return v;
2311 }
2312
2313 /* Utility equality function for a stmt_list_hash. */
2314
2315 static int
2316 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2317 const struct stmt_list_hash *rhs)
2318 {
2319 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2320 return 0;
2321 if (lhs->dwo_unit != NULL
2322 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2323 return 0;
2324
2325 return lhs->line_sect_off == rhs->line_sect_off;
2326 }
2327
2328 /* Hash function for a quick_file_names. */
2329
2330 static hashval_t
2331 hash_file_name_entry (const void *e)
2332 {
2333 const struct quick_file_names *file_data
2334 = (const struct quick_file_names *) e;
2335
2336 return hash_stmt_list_entry (&file_data->hash);
2337 }
2338
2339 /* Equality function for a quick_file_names. */
2340
2341 static int
2342 eq_file_name_entry (const void *a, const void *b)
2343 {
2344 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2345 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2346
2347 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2348 }
2349
2350 /* Delete function for a quick_file_names. */
2351
2352 static void
2353 delete_file_name_entry (void *e)
2354 {
2355 struct quick_file_names *file_data = (struct quick_file_names *) e;
2356 int i;
2357
2358 for (i = 0; i < file_data->num_file_names; ++i)
2359 {
2360 xfree ((void*) file_data->file_names[i]);
2361 if (file_data->real_names)
2362 xfree ((void*) file_data->real_names[i]);
2363 }
2364
2365 /* The space for the struct itself lives on the obstack, so we don't
2366 free it here. */
2367 }
2368
2369 /* Create a quick_file_names hash table. */
2370
2371 static htab_up
2372 create_quick_file_names_table (unsigned int nr_initial_entries)
2373 {
2374 return htab_up (htab_create_alloc (nr_initial_entries,
2375 hash_file_name_entry, eq_file_name_entry,
2376 delete_file_name_entry, xcalloc, xfree));
2377 }
2378
2379 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2380 function is unrelated to symtabs, symtab would have to be created afterwards.
2381 You should call age_cached_comp_units after processing the CU. */
2382
2383 static dwarf2_cu *
2384 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2385 bool skip_partial)
2386 {
2387 if (per_cu->is_debug_types)
2388 load_full_type_unit (per_cu, per_objfile);
2389 else
2390 load_full_comp_unit (per_cu, per_objfile, skip_partial, language_minimal);
2391
2392 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2393 if (cu == nullptr)
2394 return nullptr; /* Dummy CU. */
2395
2396 dwarf2_find_base_address (cu->dies, cu);
2397
2398 return cu;
2399 }
2400
2401 /* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2402
2403 static void
2404 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2405 dwarf2_per_objfile *per_objfile, bool skip_partial)
2406 {
2407 /* Skip type_unit_groups, reading the type units they contain
2408 is handled elsewhere. */
2409 if (per_cu->type_unit_group_p ())
2410 return;
2411
2412 /* The destructor of dwarf2_queue_guard frees any entries left on
2413 the queue. After this point we're guaranteed to leave this function
2414 with the dwarf queue empty. */
2415 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2416
2417 if (!per_objfile->symtab_set_p (per_cu))
2418 {
2419 queue_comp_unit (per_cu, per_objfile, language_minimal);
2420 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2421
2422 /* If we just loaded a CU from a DWO, and we're working with an index
2423 that may badly handle TUs, load all the TUs in that DWO as well.
2424 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2425 if (!per_cu->is_debug_types
2426 && cu != NULL
2427 && cu->dwo_unit != NULL
2428 && per_objfile->per_bfd->index_table != NULL
2429 && per_objfile->per_bfd->index_table->version <= 7
2430 /* DWP files aren't supported yet. */
2431 && get_dwp_file (per_objfile) == NULL)
2432 queue_and_load_all_dwo_tus (cu);
2433 }
2434
2435 process_queue (per_objfile);
2436
2437 /* Age the cache, releasing compilation units that have not
2438 been used recently. */
2439 per_objfile->age_comp_units ();
2440 }
2441
2442 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2443 the per-objfile for which this symtab is instantiated.
2444
2445 Returns the resulting symbol table. */
2446
2447 static struct compunit_symtab *
2448 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2449 dwarf2_per_objfile *per_objfile,
2450 bool skip_partial)
2451 {
2452 gdb_assert (per_objfile->per_bfd->using_index);
2453
2454 if (!per_objfile->symtab_set_p (per_cu))
2455 {
2456 free_cached_comp_units freer (per_objfile);
2457 scoped_restore decrementer = increment_reading_symtab ();
2458 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2459 process_cu_includes (per_objfile);
2460 }
2461
2462 return per_objfile->get_symtab (per_cu);
2463 }
2464
2465 /* See declaration. */
2466
2467 dwarf2_per_cu_data *
2468 dwarf2_per_bfd::get_cutu (int index)
2469 {
2470 if (index >= this->all_comp_units.size ())
2471 {
2472 index -= this->all_comp_units.size ();
2473 gdb_assert (index < this->all_type_units.size ());
2474 return &this->all_type_units[index]->per_cu;
2475 }
2476
2477 return this->all_comp_units[index];
2478 }
2479
2480 /* See declaration. */
2481
2482 dwarf2_per_cu_data *
2483 dwarf2_per_bfd::get_cu (int index)
2484 {
2485 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2486
2487 return this->all_comp_units[index];
2488 }
2489
2490 /* See declaration. */
2491
2492 signatured_type *
2493 dwarf2_per_bfd::get_tu (int index)
2494 {
2495 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2496
2497 return this->all_type_units[index];
2498 }
2499
2500 /* See read.h. */
2501
2502 dwarf2_per_cu_data *
2503 dwarf2_per_bfd::allocate_per_cu ()
2504 {
2505 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2506 result->per_bfd = this;
2507 result->index = m_num_psymtabs++;
2508 return result;
2509 }
2510
2511 /* See read.h. */
2512
2513 signatured_type *
2514 dwarf2_per_bfd::allocate_signatured_type ()
2515 {
2516 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2517 result->per_cu.per_bfd = this;
2518 result->per_cu.index = m_num_psymtabs++;
2519 return result;
2520 }
2521
2522 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2523 obstack, and constructed with the specified field values. */
2524
2525 static dwarf2_per_cu_data *
2526 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2527 struct dwarf2_section_info *section,
2528 int is_dwz,
2529 sect_offset sect_off, ULONGEST length)
2530 {
2531 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
2532 the_cu->sect_off = sect_off;
2533 the_cu->length = length;
2534 the_cu->section = section;
2535 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2536 struct dwarf2_per_cu_quick_data);
2537 the_cu->is_dwz = is_dwz;
2538 return the_cu;
2539 }
2540
2541 /* A helper for create_cus_from_index that handles a given list of
2542 CUs. */
2543
2544 static void
2545 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2546 const gdb_byte *cu_list, offset_type n_elements,
2547 struct dwarf2_section_info *section,
2548 int is_dwz)
2549 {
2550 for (offset_type i = 0; i < n_elements; i += 2)
2551 {
2552 gdb_static_assert (sizeof (ULONGEST) >= 8);
2553
2554 sect_offset sect_off
2555 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2556 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2557 cu_list += 2 * 8;
2558
2559 dwarf2_per_cu_data *per_cu
2560 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2561 length);
2562 per_bfd->all_comp_units.push_back (per_cu);
2563 }
2564 }
2565
2566 /* Read the CU list from the mapped index, and use it to create all
2567 the CU objects for PER_BFD. */
2568
2569 static void
2570 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2571 const gdb_byte *cu_list, offset_type cu_list_elements,
2572 const gdb_byte *dwz_list, offset_type dwz_elements)
2573 {
2574 gdb_assert (per_bfd->all_comp_units.empty ());
2575 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2576
2577 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2578 &per_bfd->info, 0);
2579
2580 if (dwz_elements == 0)
2581 return;
2582
2583 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2584 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2585 &dwz->info, 1);
2586 }
2587
2588 /* Create the signatured type hash table from the index. */
2589
2590 static void
2591 create_signatured_type_table_from_index
2592 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2593 const gdb_byte *bytes, offset_type elements)
2594 {
2595 gdb_assert (per_bfd->all_type_units.empty ());
2596 per_bfd->all_type_units.reserve (elements / 3);
2597
2598 htab_up sig_types_hash = allocate_signatured_type_table ();
2599
2600 for (offset_type i = 0; i < elements; i += 3)
2601 {
2602 struct signatured_type *sig_type;
2603 ULONGEST signature;
2604 void **slot;
2605 cu_offset type_offset_in_tu;
2606
2607 gdb_static_assert (sizeof (ULONGEST) >= 8);
2608 sect_offset sect_off
2609 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2610 type_offset_in_tu
2611 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2612 BFD_ENDIAN_LITTLE);
2613 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2614 bytes += 3 * 8;
2615
2616 sig_type = per_bfd->allocate_signatured_type ();
2617 sig_type->signature = signature;
2618 sig_type->type_offset_in_tu = type_offset_in_tu;
2619 sig_type->per_cu.is_debug_types = 1;
2620 sig_type->per_cu.section = section;
2621 sig_type->per_cu.sect_off = sect_off;
2622 sig_type->per_cu.v.quick
2623 = OBSTACK_ZALLOC (&per_bfd->obstack,
2624 struct dwarf2_per_cu_quick_data);
2625
2626 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2627 *slot = sig_type;
2628
2629 per_bfd->all_type_units.push_back (sig_type);
2630 }
2631
2632 per_bfd->signatured_types = std::move (sig_types_hash);
2633 }
2634
2635 /* Create the signatured type hash table from .debug_names. */
2636
2637 static void
2638 create_signatured_type_table_from_debug_names
2639 (dwarf2_per_objfile *per_objfile,
2640 const mapped_debug_names &map,
2641 struct dwarf2_section_info *section,
2642 struct dwarf2_section_info *abbrev_section)
2643 {
2644 struct objfile *objfile = per_objfile->objfile;
2645
2646 section->read (objfile);
2647 abbrev_section->read (objfile);
2648
2649 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2650 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2651
2652 htab_up sig_types_hash = allocate_signatured_type_table ();
2653
2654 for (uint32_t i = 0; i < map.tu_count; ++i)
2655 {
2656 struct signatured_type *sig_type;
2657 void **slot;
2658
2659 sect_offset sect_off
2660 = (sect_offset) (extract_unsigned_integer
2661 (map.tu_table_reordered + i * map.offset_size,
2662 map.offset_size,
2663 map.dwarf5_byte_order));
2664
2665 comp_unit_head cu_header;
2666 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2667 abbrev_section,
2668 section->buffer + to_underlying (sect_off),
2669 rcuh_kind::TYPE);
2670
2671 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
2672 sig_type->signature = cu_header.signature;
2673 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2674 sig_type->per_cu.is_debug_types = 1;
2675 sig_type->per_cu.section = section;
2676 sig_type->per_cu.sect_off = sect_off;
2677 sig_type->per_cu.v.quick
2678 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
2679 struct dwarf2_per_cu_quick_data);
2680
2681 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2682 *slot = sig_type;
2683
2684 per_objfile->per_bfd->all_type_units.push_back (sig_type);
2685 }
2686
2687 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2688 }
2689
2690 /* Read the address map data from the mapped index, and use it to
2691 populate the objfile's psymtabs_addrmap. */
2692
2693 static void
2694 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2695 struct mapped_index *index)
2696 {
2697 struct objfile *objfile = per_objfile->objfile;
2698 struct gdbarch *gdbarch = objfile->arch ();
2699 const gdb_byte *iter, *end;
2700 struct addrmap *mutable_map;
2701 CORE_ADDR baseaddr;
2702
2703 auto_obstack temp_obstack;
2704
2705 mutable_map = addrmap_create_mutable (&temp_obstack);
2706
2707 iter = index->address_table.data ();
2708 end = iter + index->address_table.size ();
2709
2710 baseaddr = objfile->text_section_offset ();
2711
2712 while (iter < end)
2713 {
2714 ULONGEST hi, lo, cu_index;
2715 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2716 iter += 8;
2717 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2718 iter += 8;
2719 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2720 iter += 4;
2721
2722 if (lo > hi)
2723 {
2724 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2725 hex_string (lo), hex_string (hi));
2726 continue;
2727 }
2728
2729 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
2730 {
2731 complaint (_(".gdb_index address table has invalid CU number %u"),
2732 (unsigned) cu_index);
2733 continue;
2734 }
2735
2736 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2737 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2738 addrmap_set_empty (mutable_map, lo, hi - 1,
2739 per_objfile->per_bfd->get_cu (cu_index));
2740 }
2741
2742 objfile->partial_symtabs->psymtabs_addrmap
2743 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2744 }
2745
2746 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2747 populate the objfile's psymtabs_addrmap. */
2748
2749 static void
2750 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2751 struct dwarf2_section_info *section)
2752 {
2753 struct objfile *objfile = per_objfile->objfile;
2754 bfd *abfd = objfile->obfd;
2755 struct gdbarch *gdbarch = objfile->arch ();
2756 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2757
2758 auto_obstack temp_obstack;
2759 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2760
2761 std::unordered_map<sect_offset,
2762 dwarf2_per_cu_data *,
2763 gdb::hash_enum<sect_offset>>
2764 debug_info_offset_to_per_cu;
2765 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
2766 {
2767 const auto insertpair
2768 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2769 if (!insertpair.second)
2770 {
2771 warning (_("Section .debug_aranges in %s has duplicate "
2772 "debug_info_offset %s, ignoring .debug_aranges."),
2773 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2774 return;
2775 }
2776 }
2777
2778 section->read (objfile);
2779
2780 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2781
2782 const gdb_byte *addr = section->buffer;
2783
2784 while (addr < section->buffer + section->size)
2785 {
2786 const gdb_byte *const entry_addr = addr;
2787 unsigned int bytes_read;
2788
2789 const LONGEST entry_length = read_initial_length (abfd, addr,
2790 &bytes_read);
2791 addr += bytes_read;
2792
2793 const gdb_byte *const entry_end = addr + entry_length;
2794 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2795 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2796 if (addr + entry_length > section->buffer + section->size)
2797 {
2798 warning (_("Section .debug_aranges in %s entry at offset %s "
2799 "length %s exceeds section length %s, "
2800 "ignoring .debug_aranges."),
2801 objfile_name (objfile),
2802 plongest (entry_addr - section->buffer),
2803 plongest (bytes_read + entry_length),
2804 pulongest (section->size));
2805 return;
2806 }
2807
2808 /* The version number. */
2809 const uint16_t version = read_2_bytes (abfd, addr);
2810 addr += 2;
2811 if (version != 2)
2812 {
2813 warning (_("Section .debug_aranges in %s entry at offset %s "
2814 "has unsupported version %d, ignoring .debug_aranges."),
2815 objfile_name (objfile),
2816 plongest (entry_addr - section->buffer), version);
2817 return;
2818 }
2819
2820 const uint64_t debug_info_offset
2821 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2822 addr += offset_size;
2823 const auto per_cu_it
2824 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2825 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2826 {
2827 warning (_("Section .debug_aranges in %s entry at offset %s "
2828 "debug_info_offset %s does not exists, "
2829 "ignoring .debug_aranges."),
2830 objfile_name (objfile),
2831 plongest (entry_addr - section->buffer),
2832 pulongest (debug_info_offset));
2833 return;
2834 }
2835 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2836
2837 const uint8_t address_size = *addr++;
2838 if (address_size < 1 || address_size > 8)
2839 {
2840 warning (_("Section .debug_aranges in %s entry at offset %s "
2841 "address_size %u is invalid, ignoring .debug_aranges."),
2842 objfile_name (objfile),
2843 plongest (entry_addr - section->buffer), address_size);
2844 return;
2845 }
2846
2847 const uint8_t segment_selector_size = *addr++;
2848 if (segment_selector_size != 0)
2849 {
2850 warning (_("Section .debug_aranges in %s entry at offset %s "
2851 "segment_selector_size %u is not supported, "
2852 "ignoring .debug_aranges."),
2853 objfile_name (objfile),
2854 plongest (entry_addr - section->buffer),
2855 segment_selector_size);
2856 return;
2857 }
2858
2859 /* Must pad to an alignment boundary that is twice the address
2860 size. It is undocumented by the DWARF standard but GCC does
2861 use it. */
2862 for (size_t padding = ((-(addr - section->buffer))
2863 & (2 * address_size - 1));
2864 padding > 0; padding--)
2865 if (*addr++ != 0)
2866 {
2867 warning (_("Section .debug_aranges in %s entry at offset %s "
2868 "padding is not zero, ignoring .debug_aranges."),
2869 objfile_name (objfile),
2870 plongest (entry_addr - section->buffer));
2871 return;
2872 }
2873
2874 for (;;)
2875 {
2876 if (addr + 2 * address_size > entry_end)
2877 {
2878 warning (_("Section .debug_aranges in %s entry at offset %s "
2879 "address list is not properly terminated, "
2880 "ignoring .debug_aranges."),
2881 objfile_name (objfile),
2882 plongest (entry_addr - section->buffer));
2883 return;
2884 }
2885 ULONGEST start = extract_unsigned_integer (addr, address_size,
2886 dwarf5_byte_order);
2887 addr += address_size;
2888 ULONGEST length = extract_unsigned_integer (addr, address_size,
2889 dwarf5_byte_order);
2890 addr += address_size;
2891 if (start == 0 && length == 0)
2892 break;
2893 if (start == 0 && !per_objfile->per_bfd->has_section_at_zero)
2894 {
2895 /* Symbol was eliminated due to a COMDAT group. */
2896 continue;
2897 }
2898 ULONGEST end = start + length;
2899 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2900 - baseaddr);
2901 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2902 - baseaddr);
2903 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2904 }
2905 }
2906
2907 objfile->partial_symtabs->psymtabs_addrmap
2908 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2909 }
2910
2911 /* Find a slot in the mapped index INDEX for the object named NAME.
2912 If NAME is found, set *VEC_OUT to point to the CU vector in the
2913 constant pool and return true. If NAME cannot be found, return
2914 false. */
2915
2916 static bool
2917 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2918 offset_type **vec_out)
2919 {
2920 offset_type hash;
2921 offset_type slot, step;
2922 int (*cmp) (const char *, const char *);
2923
2924 gdb::unique_xmalloc_ptr<char> without_params;
2925 if (current_language->la_language == language_cplus
2926 || current_language->la_language == language_fortran
2927 || current_language->la_language == language_d)
2928 {
2929 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2930 not contain any. */
2931
2932 if (strchr (name, '(') != NULL)
2933 {
2934 without_params = cp_remove_params (name);
2935
2936 if (without_params != NULL)
2937 name = without_params.get ();
2938 }
2939 }
2940
2941 /* Index version 4 did not support case insensitive searches. But the
2942 indices for case insensitive languages are built in lowercase, therefore
2943 simulate our NAME being searched is also lowercased. */
2944 hash = mapped_index_string_hash ((index->version == 4
2945 && case_sensitivity == case_sensitive_off
2946 ? 5 : index->version),
2947 name);
2948
2949 slot = hash & (index->symbol_table.size () - 1);
2950 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2951 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2952
2953 for (;;)
2954 {
2955 const char *str;
2956
2957 const auto &bucket = index->symbol_table[slot];
2958 if (bucket.name == 0 && bucket.vec == 0)
2959 return false;
2960
2961 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2962 if (!cmp (name, str))
2963 {
2964 *vec_out = (offset_type *) (index->constant_pool
2965 + MAYBE_SWAP (bucket.vec));
2966 return true;
2967 }
2968
2969 slot = (slot + step) & (index->symbol_table.size () - 1);
2970 }
2971 }
2972
2973 /* A helper function that reads the .gdb_index from BUFFER and fills
2974 in MAP. FILENAME is the name of the file containing the data;
2975 it is used for error reporting. DEPRECATED_OK is true if it is
2976 ok to use deprecated sections.
2977
2978 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2979 out parameters that are filled in with information about the CU and
2980 TU lists in the section.
2981
2982 Returns true if all went well, false otherwise. */
2983
2984 static bool
2985 read_gdb_index_from_buffer (const char *filename,
2986 bool deprecated_ok,
2987 gdb::array_view<const gdb_byte> buffer,
2988 struct mapped_index *map,
2989 const gdb_byte **cu_list,
2990 offset_type *cu_list_elements,
2991 const gdb_byte **types_list,
2992 offset_type *types_list_elements)
2993 {
2994 const gdb_byte *addr = &buffer[0];
2995
2996 /* Version check. */
2997 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2998 /* Versions earlier than 3 emitted every copy of a psymbol. This
2999 causes the index to behave very poorly for certain requests. Version 3
3000 contained incomplete addrmap. So, it seems better to just ignore such
3001 indices. */
3002 if (version < 4)
3003 {
3004 static int warning_printed = 0;
3005 if (!warning_printed)
3006 {
3007 warning (_("Skipping obsolete .gdb_index section in %s."),
3008 filename);
3009 warning_printed = 1;
3010 }
3011 return 0;
3012 }
3013 /* Index version 4 uses a different hash function than index version
3014 5 and later.
3015
3016 Versions earlier than 6 did not emit psymbols for inlined
3017 functions. Using these files will cause GDB not to be able to
3018 set breakpoints on inlined functions by name, so we ignore these
3019 indices unless the user has done
3020 "set use-deprecated-index-sections on". */
3021 if (version < 6 && !deprecated_ok)
3022 {
3023 static int warning_printed = 0;
3024 if (!warning_printed)
3025 {
3026 warning (_("\
3027 Skipping deprecated .gdb_index section in %s.\n\
3028 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3029 to use the section anyway."),
3030 filename);
3031 warning_printed = 1;
3032 }
3033 return 0;
3034 }
3035 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3036 of the TU (for symbols coming from TUs),
3037 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3038 Plus gold-generated indices can have duplicate entries for global symbols,
3039 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3040 These are just performance bugs, and we can't distinguish gdb-generated
3041 indices from gold-generated ones, so issue no warning here. */
3042
3043 /* Indexes with higher version than the one supported by GDB may be no
3044 longer backward compatible. */
3045 if (version > 8)
3046 return 0;
3047
3048 map->version = version;
3049
3050 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3051
3052 int i = 0;
3053 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3054 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3055 / 8);
3056 ++i;
3057
3058 *types_list = addr + MAYBE_SWAP (metadata[i]);
3059 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3060 - MAYBE_SWAP (metadata[i]))
3061 / 8);
3062 ++i;
3063
3064 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3065 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3066 map->address_table
3067 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3068 ++i;
3069
3070 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3071 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3072 map->symbol_table
3073 = gdb::array_view<mapped_index::symbol_table_slot>
3074 ((mapped_index::symbol_table_slot *) symbol_table,
3075 (mapped_index::symbol_table_slot *) symbol_table_end);
3076
3077 ++i;
3078 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3079
3080 return 1;
3081 }
3082
3083 /* Callback types for dwarf2_read_gdb_index. */
3084
3085 typedef gdb::function_view
3086 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3087 get_gdb_index_contents_ftype;
3088 typedef gdb::function_view
3089 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3090 get_gdb_index_contents_dwz_ftype;
3091
3092 /* Read .gdb_index. If everything went ok, initialize the "quick"
3093 elements of all the CUs and return 1. Otherwise, return 0. */
3094
3095 static int
3096 dwarf2_read_gdb_index
3097 (dwarf2_per_objfile *per_objfile,
3098 get_gdb_index_contents_ftype get_gdb_index_contents,
3099 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3100 {
3101 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3102 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3103 struct dwz_file *dwz;
3104 struct objfile *objfile = per_objfile->objfile;
3105 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
3106
3107 gdb::array_view<const gdb_byte> main_index_contents
3108 = get_gdb_index_contents (objfile, per_bfd);
3109
3110 if (main_index_contents.empty ())
3111 return 0;
3112
3113 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3114 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3115 use_deprecated_index_sections,
3116 main_index_contents, map.get (), &cu_list,
3117 &cu_list_elements, &types_list,
3118 &types_list_elements))
3119 return 0;
3120
3121 /* Don't use the index if it's empty. */
3122 if (map->symbol_table.empty ())
3123 return 0;
3124
3125 /* If there is a .dwz file, read it so we can get its CU list as
3126 well. */
3127 dwz = dwarf2_get_dwz_file (per_bfd);
3128 if (dwz != NULL)
3129 {
3130 struct mapped_index dwz_map;
3131 const gdb_byte *dwz_types_ignore;
3132 offset_type dwz_types_elements_ignore;
3133
3134 gdb::array_view<const gdb_byte> dwz_index_content
3135 = get_gdb_index_contents_dwz (objfile, dwz);
3136
3137 if (dwz_index_content.empty ())
3138 return 0;
3139
3140 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3141 1, dwz_index_content, &dwz_map,
3142 &dwz_list, &dwz_list_elements,
3143 &dwz_types_ignore,
3144 &dwz_types_elements_ignore))
3145 {
3146 warning (_("could not read '.gdb_index' section from %s; skipping"),
3147 bfd_get_filename (dwz->dwz_bfd.get ()));
3148 return 0;
3149 }
3150 }
3151
3152 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3153 dwz_list_elements);
3154
3155 if (types_list_elements)
3156 {
3157 /* We can only handle a single .debug_types when we have an
3158 index. */
3159 if (per_bfd->types.size () != 1)
3160 return 0;
3161
3162 dwarf2_section_info *section = &per_bfd->types[0];
3163
3164 create_signatured_type_table_from_index (per_bfd, section, types_list,
3165 types_list_elements);
3166 }
3167
3168 create_addrmap_from_index (per_objfile, map.get ());
3169
3170 per_bfd->index_table = std::move (map);
3171 per_bfd->using_index = 1;
3172 per_bfd->quick_file_names_table =
3173 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3174
3175 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
3176 objfiles using the same BFD. */
3177 gdb_assert (per_bfd->partial_symtabs == nullptr);
3178 per_bfd->partial_symtabs = objfile->partial_symtabs;
3179
3180 return 1;
3181 }
3182
3183 /* die_reader_func for dw2_get_file_names. */
3184
3185 static void
3186 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3187 const gdb_byte *info_ptr,
3188 struct die_info *comp_unit_die)
3189 {
3190 struct dwarf2_cu *cu = reader->cu;
3191 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3192 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3193 struct dwarf2_per_cu_data *lh_cu;
3194 struct attribute *attr;
3195 void **slot;
3196 struct quick_file_names *qfn;
3197
3198 gdb_assert (! this_cu->is_debug_types);
3199
3200 /* Our callers never want to match partial units -- instead they
3201 will match the enclosing full CU. */
3202 if (comp_unit_die->tag == DW_TAG_partial_unit)
3203 {
3204 this_cu->v.quick->no_file_data = 1;
3205 return;
3206 }
3207
3208 lh_cu = this_cu;
3209 slot = NULL;
3210
3211 line_header_up lh;
3212 sect_offset line_offset {};
3213
3214 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3215 if (attr != nullptr)
3216 {
3217 struct quick_file_names find_entry;
3218
3219 line_offset = (sect_offset) DW_UNSND (attr);
3220
3221 /* We may have already read in this line header (TU line header sharing).
3222 If we have we're done. */
3223 find_entry.hash.dwo_unit = cu->dwo_unit;
3224 find_entry.hash.line_sect_off = line_offset;
3225 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
3226 &find_entry, INSERT);
3227 if (*slot != NULL)
3228 {
3229 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3230 return;
3231 }
3232
3233 lh = dwarf_decode_line_header (line_offset, cu);
3234 }
3235 if (lh == NULL)
3236 {
3237 lh_cu->v.quick->no_file_data = 1;
3238 return;
3239 }
3240
3241 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
3242 qfn->hash.dwo_unit = cu->dwo_unit;
3243 qfn->hash.line_sect_off = line_offset;
3244 gdb_assert (slot != NULL);
3245 *slot = qfn;
3246
3247 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3248
3249 int offset = 0;
3250 if (strcmp (fnd.name, "<unknown>") != 0)
3251 ++offset;
3252
3253 qfn->num_file_names = offset + lh->file_names_size ();
3254 qfn->file_names =
3255 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
3256 qfn->num_file_names);
3257 if (offset != 0)
3258 qfn->file_names[0] = xstrdup (fnd.name);
3259 for (int i = 0; i < lh->file_names_size (); ++i)
3260 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3261 fnd.comp_dir).release ();
3262 qfn->real_names = NULL;
3263
3264 lh_cu->v.quick->file_names = qfn;
3265 }
3266
3267 /* A helper for the "quick" functions which attempts to read the line
3268 table for THIS_CU. */
3269
3270 static struct quick_file_names *
3271 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3272 dwarf2_per_objfile *per_objfile)
3273 {
3274 /* This should never be called for TUs. */
3275 gdb_assert (! this_cu->is_debug_types);
3276 /* Nor type unit groups. */
3277 gdb_assert (! this_cu->type_unit_group_p ());
3278
3279 if (this_cu->v.quick->file_names != NULL)
3280 return this_cu->v.quick->file_names;
3281 /* If we know there is no line data, no point in looking again. */
3282 if (this_cu->v.quick->no_file_data)
3283 return NULL;
3284
3285 cutu_reader reader (this_cu, per_objfile);
3286 if (!reader.dummy_p)
3287 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3288
3289 if (this_cu->v.quick->no_file_data)
3290 return NULL;
3291 return this_cu->v.quick->file_names;
3292 }
3293
3294 /* A helper for the "quick" functions which computes and caches the
3295 real path for a given file name from the line table. */
3296
3297 static const char *
3298 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3299 struct quick_file_names *qfn, int index)
3300 {
3301 if (qfn->real_names == NULL)
3302 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3303 qfn->num_file_names, const char *);
3304
3305 if (qfn->real_names[index] == NULL)
3306 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3307
3308 return qfn->real_names[index];
3309 }
3310
3311 static struct symtab *
3312 dw2_find_last_source_symtab (struct objfile *objfile)
3313 {
3314 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3315 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3316 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3317
3318 if (cust == NULL)
3319 return NULL;
3320
3321 return compunit_primary_filetab (cust);
3322 }
3323
3324 /* Traversal function for dw2_forget_cached_source_info. */
3325
3326 static int
3327 dw2_free_cached_file_names (void **slot, void *info)
3328 {
3329 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3330
3331 if (file_data->real_names)
3332 {
3333 int i;
3334
3335 for (i = 0; i < file_data->num_file_names; ++i)
3336 {
3337 xfree ((void*) file_data->real_names[i]);
3338 file_data->real_names[i] = NULL;
3339 }
3340 }
3341
3342 return 1;
3343 }
3344
3345 static void
3346 dw2_forget_cached_source_info (struct objfile *objfile)
3347 {
3348 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3349
3350 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
3351 dw2_free_cached_file_names, NULL);
3352 }
3353
3354 /* Helper function for dw2_map_symtabs_matching_filename that expands
3355 the symtabs and calls the iterator. */
3356
3357 static int
3358 dw2_map_expand_apply (struct objfile *objfile,
3359 struct dwarf2_per_cu_data *per_cu,
3360 const char *name, const char *real_path,
3361 gdb::function_view<bool (symtab *)> callback)
3362 {
3363 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3364
3365 /* Don't visit already-expanded CUs. */
3366 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3367 if (per_objfile->symtab_set_p (per_cu))
3368 return 0;
3369
3370 /* This may expand more than one symtab, and we want to iterate over
3371 all of them. */
3372 dw2_instantiate_symtab (per_cu, per_objfile, false);
3373
3374 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3375 last_made, callback);
3376 }
3377
3378 /* Implementation of the map_symtabs_matching_filename method. */
3379
3380 static bool
3381 dw2_map_symtabs_matching_filename
3382 (struct objfile *objfile, const char *name, const char *real_path,
3383 gdb::function_view<bool (symtab *)> callback)
3384 {
3385 const char *name_basename = lbasename (name);
3386 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3387
3388 /* The rule is CUs specify all the files, including those used by
3389 any TU, so there's no need to scan TUs here. */
3390
3391 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3392 {
3393 /* We only need to look at symtabs not already expanded. */
3394 if (per_objfile->symtab_set_p (per_cu))
3395 continue;
3396
3397 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3398 if (file_data == NULL)
3399 continue;
3400
3401 for (int j = 0; j < file_data->num_file_names; ++j)
3402 {
3403 const char *this_name = file_data->file_names[j];
3404 const char *this_real_name;
3405
3406 if (compare_filenames_for_search (this_name, name))
3407 {
3408 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3409 callback))
3410 return true;
3411 continue;
3412 }
3413
3414 /* Before we invoke realpath, which can get expensive when many
3415 files are involved, do a quick comparison of the basenames. */
3416 if (! basenames_may_differ
3417 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3418 continue;
3419
3420 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
3421 if (compare_filenames_for_search (this_real_name, name))
3422 {
3423 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3424 callback))
3425 return true;
3426 continue;
3427 }
3428
3429 if (real_path != NULL)
3430 {
3431 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3432 gdb_assert (IS_ABSOLUTE_PATH (name));
3433 if (this_real_name != NULL
3434 && FILENAME_CMP (real_path, this_real_name) == 0)
3435 {
3436 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3437 callback))
3438 return true;
3439 continue;
3440 }
3441 }
3442 }
3443 }
3444
3445 return false;
3446 }
3447
3448 /* Struct used to manage iterating over all CUs looking for a symbol. */
3449
3450 struct dw2_symtab_iterator
3451 {
3452 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3453 dwarf2_per_objfile *per_objfile;
3454 /* If set, only look for symbols that match that block. Valid values are
3455 GLOBAL_BLOCK and STATIC_BLOCK. */
3456 gdb::optional<block_enum> block_index;
3457 /* The kind of symbol we're looking for. */
3458 domain_enum domain;
3459 /* The list of CUs from the index entry of the symbol,
3460 or NULL if not found. */
3461 offset_type *vec;
3462 /* The next element in VEC to look at. */
3463 int next;
3464 /* The number of elements in VEC, or zero if there is no match. */
3465 int length;
3466 /* Have we seen a global version of the symbol?
3467 If so we can ignore all further global instances.
3468 This is to work around gold/15646, inefficient gold-generated
3469 indices. */
3470 int global_seen;
3471 };
3472
3473 /* Initialize the index symtab iterator ITER, common part. */
3474
3475 static void
3476 dw2_symtab_iter_init_common (struct dw2_symtab_iterator *iter,
3477 dwarf2_per_objfile *per_objfile,
3478 gdb::optional<block_enum> block_index,
3479 domain_enum domain)
3480 {
3481 iter->per_objfile = per_objfile;
3482 iter->block_index = block_index;
3483 iter->domain = domain;
3484 iter->next = 0;
3485 iter->global_seen = 0;
3486 iter->vec = NULL;
3487 iter->length = 0;
3488 }
3489
3490 /* Initialize the index symtab iterator ITER, const char *NAME variant. */
3491
3492 static void
3493 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3494 dwarf2_per_objfile *per_objfile,
3495 gdb::optional<block_enum> block_index,
3496 domain_enum domain,
3497 const char *name)
3498 {
3499 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3500
3501 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3502 /* index is NULL if OBJF_READNOW. */
3503 if (index == NULL)
3504 return;
3505
3506 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3507 iter->length = MAYBE_SWAP (*iter->vec);
3508 }
3509
3510 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3511
3512 static void
3513 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3514 dwarf2_per_objfile *per_objfile,
3515 gdb::optional<block_enum> block_index,
3516 domain_enum domain, offset_type namei)
3517 {
3518 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3519
3520 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3521 /* index is NULL if OBJF_READNOW. */
3522 if (index == NULL)
3523 return;
3524
3525 gdb_assert (!index->symbol_name_slot_invalid (namei));
3526 const auto &bucket = index->symbol_table[namei];
3527
3528 iter->vec = (offset_type *) (index->constant_pool
3529 + MAYBE_SWAP (bucket.vec));
3530 iter->length = MAYBE_SWAP (*iter->vec);
3531 }
3532
3533 /* Return the next matching CU or NULL if there are no more. */
3534
3535 static struct dwarf2_per_cu_data *
3536 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3537 {
3538 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3539
3540 for ( ; iter->next < iter->length; ++iter->next)
3541 {
3542 offset_type cu_index_and_attrs =
3543 MAYBE_SWAP (iter->vec[iter->next + 1]);
3544 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3545 gdb_index_symbol_kind symbol_kind =
3546 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3547 /* Only check the symbol attributes if they're present.
3548 Indices prior to version 7 don't record them,
3549 and indices >= 7 may elide them for certain symbols
3550 (gold does this). */
3551 int attrs_valid =
3552 (per_objfile->per_bfd->index_table->version >= 7
3553 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3554
3555 /* Don't crash on bad data. */
3556 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3557 + per_objfile->per_bfd->all_type_units.size ()))
3558 {
3559 complaint (_(".gdb_index entry has bad CU index"
3560 " [in module %s]"), objfile_name (per_objfile->objfile));
3561 continue;
3562 }
3563
3564 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3565
3566 /* Skip if already read in. */
3567 if (per_objfile->symtab_set_p (per_cu))
3568 continue;
3569
3570 /* Check static vs global. */
3571 if (attrs_valid)
3572 {
3573 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3574
3575 if (iter->block_index.has_value ())
3576 {
3577 bool want_static = *iter->block_index == STATIC_BLOCK;
3578
3579 if (is_static != want_static)
3580 continue;
3581 }
3582
3583 /* Work around gold/15646. */
3584 if (!is_static
3585 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3586 {
3587 if (iter->global_seen)
3588 continue;
3589
3590 iter->global_seen = 1;
3591 }
3592 }
3593
3594 /* Only check the symbol's kind if it has one. */
3595 if (attrs_valid)
3596 {
3597 switch (iter->domain)
3598 {
3599 case VAR_DOMAIN:
3600 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3601 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3602 /* Some types are also in VAR_DOMAIN. */
3603 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3604 continue;
3605 break;
3606 case STRUCT_DOMAIN:
3607 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3608 continue;
3609 break;
3610 case LABEL_DOMAIN:
3611 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3612 continue;
3613 break;
3614 case MODULE_DOMAIN:
3615 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3616 continue;
3617 break;
3618 default:
3619 break;
3620 }
3621 }
3622
3623 ++iter->next;
3624 return per_cu;
3625 }
3626
3627 return NULL;
3628 }
3629
3630 static struct compunit_symtab *
3631 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3632 const char *name, domain_enum domain)
3633 {
3634 struct compunit_symtab *stab_best = NULL;
3635 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3636
3637 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3638
3639 struct dw2_symtab_iterator iter;
3640 struct dwarf2_per_cu_data *per_cu;
3641
3642 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
3643
3644 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3645 {
3646 struct symbol *sym, *with_opaque = NULL;
3647 struct compunit_symtab *stab
3648 = dw2_instantiate_symtab (per_cu, per_objfile, false);
3649 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3650 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3651
3652 sym = block_find_symbol (block, name, domain,
3653 block_find_non_opaque_type_preferred,
3654 &with_opaque);
3655
3656 /* Some caution must be observed with overloaded functions
3657 and methods, since the index will not contain any overload
3658 information (but NAME might contain it). */
3659
3660 if (sym != NULL
3661 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3662 return stab;
3663 if (with_opaque != NULL
3664 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3665 stab_best = stab;
3666
3667 /* Keep looking through other CUs. */
3668 }
3669
3670 return stab_best;
3671 }
3672
3673 static void
3674 dw2_print_stats (struct objfile *objfile)
3675 {
3676 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3677 int total = (per_objfile->per_bfd->all_comp_units.size ()
3678 + per_objfile->per_bfd->all_type_units.size ());
3679 int count = 0;
3680
3681 for (int i = 0; i < total; ++i)
3682 {
3683 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3684
3685 if (!per_objfile->symtab_set_p (per_cu))
3686 ++count;
3687 }
3688 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3689 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3690 }
3691
3692 /* This dumps minimal information about the index.
3693 It is called via "mt print objfiles".
3694 One use is to verify .gdb_index has been loaded by the
3695 gdb.dwarf2/gdb-index.exp testcase. */
3696
3697 static void
3698 dw2_dump (struct objfile *objfile)
3699 {
3700 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3701
3702 gdb_assert (per_objfile->per_bfd->using_index);
3703 printf_filtered (".gdb_index:");
3704 if (per_objfile->per_bfd->index_table != NULL)
3705 {
3706 printf_filtered (" version %d\n",
3707 per_objfile->per_bfd->index_table->version);
3708 }
3709 else
3710 printf_filtered (" faked for \"readnow\"\n");
3711 printf_filtered ("\n");
3712 }
3713
3714 static void
3715 dw2_expand_symtabs_for_function (struct objfile *objfile,
3716 const char *func_name)
3717 {
3718 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3719
3720 struct dw2_symtab_iterator iter;
3721 struct dwarf2_per_cu_data *per_cu;
3722
3723 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
3724
3725 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3726 dw2_instantiate_symtab (per_cu, per_objfile, false);
3727
3728 }
3729
3730 static void
3731 dw2_expand_all_symtabs (struct objfile *objfile)
3732 {
3733 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3734 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3735 + per_objfile->per_bfd->all_type_units.size ());
3736
3737 for (int i = 0; i < total_units; ++i)
3738 {
3739 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3740
3741 /* We don't want to directly expand a partial CU, because if we
3742 read it with the wrong language, then assertion failures can
3743 be triggered later on. See PR symtab/23010. So, tell
3744 dw2_instantiate_symtab to skip partial CUs -- any important
3745 partial CU will be read via DW_TAG_imported_unit anyway. */
3746 dw2_instantiate_symtab (per_cu, per_objfile, true);
3747 }
3748 }
3749
3750 static void
3751 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3752 const char *fullname)
3753 {
3754 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3755
3756 /* We don't need to consider type units here.
3757 This is only called for examining code, e.g. expand_line_sal.
3758 There can be an order of magnitude (or more) more type units
3759 than comp units, and we avoid them if we can. */
3760
3761 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3762 {
3763 /* We only need to look at symtabs not already expanded. */
3764 if (per_objfile->symtab_set_p (per_cu))
3765 continue;
3766
3767 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3768 if (file_data == NULL)
3769 continue;
3770
3771 for (int j = 0; j < file_data->num_file_names; ++j)
3772 {
3773 const char *this_fullname = file_data->file_names[j];
3774
3775 if (filename_cmp (this_fullname, fullname) == 0)
3776 {
3777 dw2_instantiate_symtab (per_cu, per_objfile, false);
3778 break;
3779 }
3780 }
3781 }
3782 }
3783
3784 static void
3785 dw2_expand_symtabs_matching_symbol
3786 (mapped_index_base &index,
3787 const lookup_name_info &lookup_name_in,
3788 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3789 enum search_domain kind,
3790 gdb::function_view<bool (offset_type)> match_callback,
3791 dwarf2_per_objfile *per_objfile);
3792
3793 static void
3794 dw2_expand_symtabs_matching_one
3795 (dwarf2_per_cu_data *per_cu,
3796 dwarf2_per_objfile *per_objfile,
3797 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3798 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3799
3800 static void
3801 dw2_map_matching_symbols
3802 (struct objfile *objfile,
3803 const lookup_name_info &name, domain_enum domain,
3804 int global,
3805 gdb::function_view<symbol_found_callback_ftype> callback,
3806 symbol_compare_ftype *ordered_compare)
3807 {
3808 /* Used for Ada. */
3809 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3810
3811 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3812
3813 if (per_objfile->per_bfd->index_table != nullptr)
3814 {
3815 mapped_index &index = *per_objfile->per_bfd->index_table;
3816
3817 const char *match_name = name.ada ().lookup_name ().c_str ();
3818 auto matcher = [&] (const char *symname)
3819 {
3820 if (ordered_compare == nullptr)
3821 return true;
3822 return ordered_compare (symname, match_name) == 0;
3823 };
3824
3825 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3826 [&] (offset_type namei)
3827 {
3828 struct dw2_symtab_iterator iter;
3829 struct dwarf2_per_cu_data *per_cu;
3830
3831 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3832 namei);
3833 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3834 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3835 nullptr);
3836 return true;
3837 }, per_objfile);
3838 }
3839 else
3840 {
3841 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3842 proceed assuming all symtabs have been read in. */
3843 }
3844
3845 for (compunit_symtab *cust : objfile->compunits ())
3846 {
3847 const struct block *block;
3848
3849 if (cust == NULL)
3850 continue;
3851 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3852 if (!iterate_over_symbols_terminated (block, name,
3853 domain, callback))
3854 return;
3855 }
3856 }
3857
3858 /* Starting from a search name, return the string that finds the upper
3859 bound of all strings that start with SEARCH_NAME in a sorted name
3860 list. Returns the empty string to indicate that the upper bound is
3861 the end of the list. */
3862
3863 static std::string
3864 make_sort_after_prefix_name (const char *search_name)
3865 {
3866 /* When looking to complete "func", we find the upper bound of all
3867 symbols that start with "func" by looking for where we'd insert
3868 the closest string that would follow "func" in lexicographical
3869 order. Usually, that's "func"-with-last-character-incremented,
3870 i.e. "fund". Mind non-ASCII characters, though. Usually those
3871 will be UTF-8 multi-byte sequences, but we can't be certain.
3872 Especially mind the 0xff character, which is a valid character in
3873 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3874 rule out compilers allowing it in identifiers. Note that
3875 conveniently, strcmp/strcasecmp are specified to compare
3876 characters interpreted as unsigned char. So what we do is treat
3877 the whole string as a base 256 number composed of a sequence of
3878 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3879 to 0, and carries 1 to the following more-significant position.
3880 If the very first character in SEARCH_NAME ends up incremented
3881 and carries/overflows, then the upper bound is the end of the
3882 list. The string after the empty string is also the empty
3883 string.
3884
3885 Some examples of this operation:
3886
3887 SEARCH_NAME => "+1" RESULT
3888
3889 "abc" => "abd"
3890 "ab\xff" => "ac"
3891 "\xff" "a" "\xff" => "\xff" "b"
3892 "\xff" => ""
3893 "\xff\xff" => ""
3894 "" => ""
3895
3896 Then, with these symbols for example:
3897
3898 func
3899 func1
3900 fund
3901
3902 completing "func" looks for symbols between "func" and
3903 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3904 which finds "func" and "func1", but not "fund".
3905
3906 And with:
3907
3908 funcÿ (Latin1 'ÿ' [0xff])
3909 funcÿ1
3910 fund
3911
3912 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3913 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3914
3915 And with:
3916
3917 ÿÿ (Latin1 'ÿ' [0xff])
3918 ÿÿ1
3919
3920 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3921 the end of the list.
3922 */
3923 std::string after = search_name;
3924 while (!after.empty () && (unsigned char) after.back () == 0xff)
3925 after.pop_back ();
3926 if (!after.empty ())
3927 after.back () = (unsigned char) after.back () + 1;
3928 return after;
3929 }
3930
3931 /* See declaration. */
3932
3933 std::pair<std::vector<name_component>::const_iterator,
3934 std::vector<name_component>::const_iterator>
3935 mapped_index_base::find_name_components_bounds
3936 (const lookup_name_info &lookup_name_without_params, language lang,
3937 dwarf2_per_objfile *per_objfile) const
3938 {
3939 auto *name_cmp
3940 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3941
3942 const char *lang_name
3943 = lookup_name_without_params.language_lookup_name (lang);
3944
3945 /* Comparison function object for lower_bound that matches against a
3946 given symbol name. */
3947 auto lookup_compare_lower = [&] (const name_component &elem,
3948 const char *name)
3949 {
3950 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3951 const char *elem_name = elem_qualified + elem.name_offset;
3952 return name_cmp (elem_name, name) < 0;
3953 };
3954
3955 /* Comparison function object for upper_bound that matches against a
3956 given symbol name. */
3957 auto lookup_compare_upper = [&] (const char *name,
3958 const name_component &elem)
3959 {
3960 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3961 const char *elem_name = elem_qualified + elem.name_offset;
3962 return name_cmp (name, elem_name) < 0;
3963 };
3964
3965 auto begin = this->name_components.begin ();
3966 auto end = this->name_components.end ();
3967
3968 /* Find the lower bound. */
3969 auto lower = [&] ()
3970 {
3971 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3972 return begin;
3973 else
3974 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3975 } ();
3976
3977 /* Find the upper bound. */
3978 auto upper = [&] ()
3979 {
3980 if (lookup_name_without_params.completion_mode ())
3981 {
3982 /* In completion mode, we want UPPER to point past all
3983 symbols names that have the same prefix. I.e., with
3984 these symbols, and completing "func":
3985
3986 function << lower bound
3987 function1
3988 other_function << upper bound
3989
3990 We find the upper bound by looking for the insertion
3991 point of "func"-with-last-character-incremented,
3992 i.e. "fund". */
3993 std::string after = make_sort_after_prefix_name (lang_name);
3994 if (after.empty ())
3995 return end;
3996 return std::lower_bound (lower, end, after.c_str (),
3997 lookup_compare_lower);
3998 }
3999 else
4000 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4001 } ();
4002
4003 return {lower, upper};
4004 }
4005
4006 /* See declaration. */
4007
4008 void
4009 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
4010 {
4011 if (!this->name_components.empty ())
4012 return;
4013
4014 this->name_components_casing = case_sensitivity;
4015 auto *name_cmp
4016 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4017
4018 /* The code below only knows how to break apart components of C++
4019 symbol names (and other languages that use '::' as
4020 namespace/module separator) and Ada symbol names. */
4021 auto count = this->symbol_name_count ();
4022 for (offset_type idx = 0; idx < count; idx++)
4023 {
4024 if (this->symbol_name_slot_invalid (idx))
4025 continue;
4026
4027 const char *name = this->symbol_name_at (idx, per_objfile);
4028
4029 /* Add each name component to the name component table. */
4030 unsigned int previous_len = 0;
4031
4032 if (strstr (name, "::") != nullptr)
4033 {
4034 for (unsigned int current_len = cp_find_first_component (name);
4035 name[current_len] != '\0';
4036 current_len += cp_find_first_component (name + current_len))
4037 {
4038 gdb_assert (name[current_len] == ':');
4039 this->name_components.push_back ({previous_len, idx});
4040 /* Skip the '::'. */
4041 current_len += 2;
4042 previous_len = current_len;
4043 }
4044 }
4045 else
4046 {
4047 /* Handle the Ada encoded (aka mangled) form here. */
4048 for (const char *iter = strstr (name, "__");
4049 iter != nullptr;
4050 iter = strstr (iter, "__"))
4051 {
4052 this->name_components.push_back ({previous_len, idx});
4053 iter += 2;
4054 previous_len = iter - name;
4055 }
4056 }
4057
4058 this->name_components.push_back ({previous_len, idx});
4059 }
4060
4061 /* Sort name_components elements by name. */
4062 auto name_comp_compare = [&] (const name_component &left,
4063 const name_component &right)
4064 {
4065 const char *left_qualified
4066 = this->symbol_name_at (left.idx, per_objfile);
4067 const char *right_qualified
4068 = this->symbol_name_at (right.idx, per_objfile);
4069
4070 const char *left_name = left_qualified + left.name_offset;
4071 const char *right_name = right_qualified + right.name_offset;
4072
4073 return name_cmp (left_name, right_name) < 0;
4074 };
4075
4076 std::sort (this->name_components.begin (),
4077 this->name_components.end (),
4078 name_comp_compare);
4079 }
4080
4081 /* Helper for dw2_expand_symtabs_matching that works with a
4082 mapped_index_base instead of the containing objfile. This is split
4083 to a separate function in order to be able to unit test the
4084 name_components matching using a mock mapped_index_base. For each
4085 symbol name that matches, calls MATCH_CALLBACK, passing it the
4086 symbol's index in the mapped_index_base symbol table. */
4087
4088 static void
4089 dw2_expand_symtabs_matching_symbol
4090 (mapped_index_base &index,
4091 const lookup_name_info &lookup_name_in,
4092 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4093 enum search_domain kind,
4094 gdb::function_view<bool (offset_type)> match_callback,
4095 dwarf2_per_objfile *per_objfile)
4096 {
4097 lookup_name_info lookup_name_without_params
4098 = lookup_name_in.make_ignore_params ();
4099
4100 /* Build the symbol name component sorted vector, if we haven't
4101 yet. */
4102 index.build_name_components (per_objfile);
4103
4104 /* The same symbol may appear more than once in the range though.
4105 E.g., if we're looking for symbols that complete "w", and we have
4106 a symbol named "w1::w2", we'll find the two name components for
4107 that same symbol in the range. To be sure we only call the
4108 callback once per symbol, we first collect the symbol name
4109 indexes that matched in a temporary vector and ignore
4110 duplicates. */
4111 std::vector<offset_type> matches;
4112
4113 struct name_and_matcher
4114 {
4115 symbol_name_matcher_ftype *matcher;
4116 const char *name;
4117
4118 bool operator== (const name_and_matcher &other) const
4119 {
4120 return matcher == other.matcher && strcmp (name, other.name) == 0;
4121 }
4122 };
4123
4124 /* A vector holding all the different symbol name matchers, for all
4125 languages. */
4126 std::vector<name_and_matcher> matchers;
4127
4128 for (int i = 0; i < nr_languages; i++)
4129 {
4130 enum language lang_e = (enum language) i;
4131
4132 const language_defn *lang = language_def (lang_e);
4133 symbol_name_matcher_ftype *name_matcher
4134 = lang->get_symbol_name_matcher (lookup_name_without_params);
4135
4136 name_and_matcher key {
4137 name_matcher,
4138 lookup_name_without_params.language_lookup_name (lang_e)
4139 };
4140
4141 /* Don't insert the same comparison routine more than once.
4142 Note that we do this linear walk. This is not a problem in
4143 practice because the number of supported languages is
4144 low. */
4145 if (std::find (matchers.begin (), matchers.end (), key)
4146 != matchers.end ())
4147 continue;
4148 matchers.push_back (std::move (key));
4149
4150 auto bounds
4151 = index.find_name_components_bounds (lookup_name_without_params,
4152 lang_e, per_objfile);
4153
4154 /* Now for each symbol name in range, check to see if we have a name
4155 match, and if so, call the MATCH_CALLBACK callback. */
4156
4157 for (; bounds.first != bounds.second; ++bounds.first)
4158 {
4159 const char *qualified
4160 = index.symbol_name_at (bounds.first->idx, per_objfile);
4161
4162 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4163 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4164 continue;
4165
4166 matches.push_back (bounds.first->idx);
4167 }
4168 }
4169
4170 std::sort (matches.begin (), matches.end ());
4171
4172 /* Finally call the callback, once per match. */
4173 ULONGEST prev = -1;
4174 for (offset_type idx : matches)
4175 {
4176 if (prev != idx)
4177 {
4178 if (!match_callback (idx))
4179 break;
4180 prev = idx;
4181 }
4182 }
4183
4184 /* Above we use a type wider than idx's for 'prev', since 0 and
4185 (offset_type)-1 are both possible values. */
4186 static_assert (sizeof (prev) > sizeof (offset_type), "");
4187 }
4188
4189 #if GDB_SELF_TEST
4190
4191 namespace selftests { namespace dw2_expand_symtabs_matching {
4192
4193 /* A mock .gdb_index/.debug_names-like name index table, enough to
4194 exercise dw2_expand_symtabs_matching_symbol, which works with the
4195 mapped_index_base interface. Builds an index from the symbol list
4196 passed as parameter to the constructor. */
4197 class mock_mapped_index : public mapped_index_base
4198 {
4199 public:
4200 mock_mapped_index (gdb::array_view<const char *> symbols)
4201 : m_symbol_table (symbols)
4202 {}
4203
4204 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4205
4206 /* Return the number of names in the symbol table. */
4207 size_t symbol_name_count () const override
4208 {
4209 return m_symbol_table.size ();
4210 }
4211
4212 /* Get the name of the symbol at IDX in the symbol table. */
4213 const char *symbol_name_at
4214 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
4215 {
4216 return m_symbol_table[idx];
4217 }
4218
4219 private:
4220 gdb::array_view<const char *> m_symbol_table;
4221 };
4222
4223 /* Convenience function that converts a NULL pointer to a "<null>"
4224 string, to pass to print routines. */
4225
4226 static const char *
4227 string_or_null (const char *str)
4228 {
4229 return str != NULL ? str : "<null>";
4230 }
4231
4232 /* Check if a lookup_name_info built from
4233 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4234 index. EXPECTED_LIST is the list of expected matches, in expected
4235 matching order. If no match expected, then an empty list is
4236 specified. Returns true on success. On failure prints a warning
4237 indicating the file:line that failed, and returns false. */
4238
4239 static bool
4240 check_match (const char *file, int line,
4241 mock_mapped_index &mock_index,
4242 const char *name, symbol_name_match_type match_type,
4243 bool completion_mode,
4244 std::initializer_list<const char *> expected_list,
4245 dwarf2_per_objfile *per_objfile)
4246 {
4247 lookup_name_info lookup_name (name, match_type, completion_mode);
4248
4249 bool matched = true;
4250
4251 auto mismatch = [&] (const char *expected_str,
4252 const char *got)
4253 {
4254 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4255 "expected=\"%s\", got=\"%s\"\n"),
4256 file, line,
4257 (match_type == symbol_name_match_type::FULL
4258 ? "FULL" : "WILD"),
4259 name, string_or_null (expected_str), string_or_null (got));
4260 matched = false;
4261 };
4262
4263 auto expected_it = expected_list.begin ();
4264 auto expected_end = expected_list.end ();
4265
4266 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4267 NULL, ALL_DOMAIN,
4268 [&] (offset_type idx)
4269 {
4270 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
4271 const char *expected_str
4272 = expected_it == expected_end ? NULL : *expected_it++;
4273
4274 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4275 mismatch (expected_str, matched_name);
4276 return true;
4277 }, per_objfile);
4278
4279 const char *expected_str
4280 = expected_it == expected_end ? NULL : *expected_it++;
4281 if (expected_str != NULL)
4282 mismatch (expected_str, NULL);
4283
4284 return matched;
4285 }
4286
4287 /* The symbols added to the mock mapped_index for testing (in
4288 canonical form). */
4289 static const char *test_symbols[] = {
4290 "function",
4291 "std::bar",
4292 "std::zfunction",
4293 "std::zfunction2",
4294 "w1::w2",
4295 "ns::foo<char*>",
4296 "ns::foo<int>",
4297 "ns::foo<long>",
4298 "ns2::tmpl<int>::foo2",
4299 "(anonymous namespace)::A::B::C",
4300
4301 /* These are used to check that the increment-last-char in the
4302 matching algorithm for completion doesn't match "t1_fund" when
4303 completing "t1_func". */
4304 "t1_func",
4305 "t1_func1",
4306 "t1_fund",
4307 "t1_fund1",
4308
4309 /* A UTF-8 name with multi-byte sequences to make sure that
4310 cp-name-parser understands this as a single identifier ("função"
4311 is "function" in PT). */
4312 u8"u8função",
4313
4314 /* \377 (0xff) is Latin1 'ÿ'. */
4315 "yfunc\377",
4316
4317 /* \377 (0xff) is Latin1 'ÿ'. */
4318 "\377",
4319 "\377\377123",
4320
4321 /* A name with all sorts of complications. Starts with "z" to make
4322 it easier for the completion tests below. */
4323 #define Z_SYM_NAME \
4324 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4325 "::tuple<(anonymous namespace)::ui*, " \
4326 "std::default_delete<(anonymous namespace)::ui>, void>"
4327
4328 Z_SYM_NAME
4329 };
4330
4331 /* Returns true if the mapped_index_base::find_name_component_bounds
4332 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4333 in completion mode. */
4334
4335 static bool
4336 check_find_bounds_finds (mapped_index_base &index,
4337 const char *search_name,
4338 gdb::array_view<const char *> expected_syms,
4339 dwarf2_per_objfile *per_objfile)
4340 {
4341 lookup_name_info lookup_name (search_name,
4342 symbol_name_match_type::FULL, true);
4343
4344 auto bounds = index.find_name_components_bounds (lookup_name,
4345 language_cplus,
4346 per_objfile);
4347
4348 size_t distance = std::distance (bounds.first, bounds.second);
4349 if (distance != expected_syms.size ())
4350 return false;
4351
4352 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4353 {
4354 auto nc_elem = bounds.first + exp_elem;
4355 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
4356 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4357 return false;
4358 }
4359
4360 return true;
4361 }
4362
4363 /* Test the lower-level mapped_index::find_name_component_bounds
4364 method. */
4365
4366 static void
4367 test_mapped_index_find_name_component_bounds ()
4368 {
4369 mock_mapped_index mock_index (test_symbols);
4370
4371 mock_index.build_name_components (NULL /* per_objfile */);
4372
4373 /* Test the lower-level mapped_index::find_name_component_bounds
4374 method in completion mode. */
4375 {
4376 static const char *expected_syms[] = {
4377 "t1_func",
4378 "t1_func1",
4379 };
4380
4381 SELF_CHECK (check_find_bounds_finds
4382 (mock_index, "t1_func", expected_syms,
4383 NULL /* per_objfile */));
4384 }
4385
4386 /* Check that the increment-last-char in the name matching algorithm
4387 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4388 {
4389 static const char *expected_syms1[] = {
4390 "\377",
4391 "\377\377123",
4392 };
4393 SELF_CHECK (check_find_bounds_finds
4394 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
4395
4396 static const char *expected_syms2[] = {
4397 "\377\377123",
4398 };
4399 SELF_CHECK (check_find_bounds_finds
4400 (mock_index, "\377\377", expected_syms2,
4401 NULL /* per_objfile */));
4402 }
4403 }
4404
4405 /* Test dw2_expand_symtabs_matching_symbol. */
4406
4407 static void
4408 test_dw2_expand_symtabs_matching_symbol ()
4409 {
4410 mock_mapped_index mock_index (test_symbols);
4411
4412 /* We let all tests run until the end even if some fails, for debug
4413 convenience. */
4414 bool any_mismatch = false;
4415
4416 /* Create the expected symbols list (an initializer_list). Needed
4417 because lists have commas, and we need to pass them to CHECK,
4418 which is a macro. */
4419 #define EXPECT(...) { __VA_ARGS__ }
4420
4421 /* Wrapper for check_match that passes down the current
4422 __FILE__/__LINE__. */
4423 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4424 any_mismatch |= !check_match (__FILE__, __LINE__, \
4425 mock_index, \
4426 NAME, MATCH_TYPE, COMPLETION_MODE, \
4427 EXPECTED_LIST, NULL)
4428
4429 /* Identity checks. */
4430 for (const char *sym : test_symbols)
4431 {
4432 /* Should be able to match all existing symbols. */
4433 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4434 EXPECT (sym));
4435
4436 /* Should be able to match all existing symbols with
4437 parameters. */
4438 std::string with_params = std::string (sym) + "(int)";
4439 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4440 EXPECT (sym));
4441
4442 /* Should be able to match all existing symbols with
4443 parameters and qualifiers. */
4444 with_params = std::string (sym) + " ( int ) const";
4445 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4446 EXPECT (sym));
4447
4448 /* This should really find sym, but cp-name-parser.y doesn't
4449 know about lvalue/rvalue qualifiers yet. */
4450 with_params = std::string (sym) + " ( int ) &&";
4451 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4452 {});
4453 }
4454
4455 /* Check that the name matching algorithm for completion doesn't get
4456 confused with Latin1 'ÿ' / 0xff. */
4457 {
4458 static const char str[] = "\377";
4459 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4460 EXPECT ("\377", "\377\377123"));
4461 }
4462
4463 /* Check that the increment-last-char in the matching algorithm for
4464 completion doesn't match "t1_fund" when completing "t1_func". */
4465 {
4466 static const char str[] = "t1_func";
4467 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4468 EXPECT ("t1_func", "t1_func1"));
4469 }
4470
4471 /* Check that completion mode works at each prefix of the expected
4472 symbol name. */
4473 {
4474 static const char str[] = "function(int)";
4475 size_t len = strlen (str);
4476 std::string lookup;
4477
4478 for (size_t i = 1; i < len; i++)
4479 {
4480 lookup.assign (str, i);
4481 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4482 EXPECT ("function"));
4483 }
4484 }
4485
4486 /* While "w" is a prefix of both components, the match function
4487 should still only be called once. */
4488 {
4489 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4490 EXPECT ("w1::w2"));
4491 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4492 EXPECT ("w1::w2"));
4493 }
4494
4495 /* Same, with a "complicated" symbol. */
4496 {
4497 static const char str[] = Z_SYM_NAME;
4498 size_t len = strlen (str);
4499 std::string lookup;
4500
4501 for (size_t i = 1; i < len; i++)
4502 {
4503 lookup.assign (str, i);
4504 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4505 EXPECT (Z_SYM_NAME));
4506 }
4507 }
4508
4509 /* In FULL mode, an incomplete symbol doesn't match. */
4510 {
4511 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4512 {});
4513 }
4514
4515 /* A complete symbol with parameters matches any overload, since the
4516 index has no overload info. */
4517 {
4518 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4519 EXPECT ("std::zfunction", "std::zfunction2"));
4520 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4521 EXPECT ("std::zfunction", "std::zfunction2"));
4522 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4523 EXPECT ("std::zfunction", "std::zfunction2"));
4524 }
4525
4526 /* Check that whitespace is ignored appropriately. A symbol with a
4527 template argument list. */
4528 {
4529 static const char expected[] = "ns::foo<int>";
4530 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4531 EXPECT (expected));
4532 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4533 EXPECT (expected));
4534 }
4535
4536 /* Check that whitespace is ignored appropriately. A symbol with a
4537 template argument list that includes a pointer. */
4538 {
4539 static const char expected[] = "ns::foo<char*>";
4540 /* Try both completion and non-completion modes. */
4541 static const bool completion_mode[2] = {false, true};
4542 for (size_t i = 0; i < 2; i++)
4543 {
4544 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4545 completion_mode[i], EXPECT (expected));
4546 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4547 completion_mode[i], EXPECT (expected));
4548
4549 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4550 completion_mode[i], EXPECT (expected));
4551 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4552 completion_mode[i], EXPECT (expected));
4553 }
4554 }
4555
4556 {
4557 /* Check method qualifiers are ignored. */
4558 static const char expected[] = "ns::foo<char*>";
4559 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4560 symbol_name_match_type::FULL, true, EXPECT (expected));
4561 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4562 symbol_name_match_type::FULL, true, EXPECT (expected));
4563 CHECK_MATCH ("foo < char * > ( int ) const",
4564 symbol_name_match_type::WILD, true, EXPECT (expected));
4565 CHECK_MATCH ("foo < char * > ( int ) &&",
4566 symbol_name_match_type::WILD, true, EXPECT (expected));
4567 }
4568
4569 /* Test lookup names that don't match anything. */
4570 {
4571 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4572 {});
4573
4574 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4575 {});
4576 }
4577
4578 /* Some wild matching tests, exercising "(anonymous namespace)",
4579 which should not be confused with a parameter list. */
4580 {
4581 static const char *syms[] = {
4582 "A::B::C",
4583 "B::C",
4584 "C",
4585 "A :: B :: C ( int )",
4586 "B :: C ( int )",
4587 "C ( int )",
4588 };
4589
4590 for (const char *s : syms)
4591 {
4592 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4593 EXPECT ("(anonymous namespace)::A::B::C"));
4594 }
4595 }
4596
4597 {
4598 static const char expected[] = "ns2::tmpl<int>::foo2";
4599 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4600 EXPECT (expected));
4601 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4602 EXPECT (expected));
4603 }
4604
4605 SELF_CHECK (!any_mismatch);
4606
4607 #undef EXPECT
4608 #undef CHECK_MATCH
4609 }
4610
4611 static void
4612 run_test ()
4613 {
4614 test_mapped_index_find_name_component_bounds ();
4615 test_dw2_expand_symtabs_matching_symbol ();
4616 }
4617
4618 }} // namespace selftests::dw2_expand_symtabs_matching
4619
4620 #endif /* GDB_SELF_TEST */
4621
4622 /* If FILE_MATCHER is NULL or if PER_CU has
4623 dwarf2_per_cu_quick_data::MARK set (see
4624 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4625 EXPANSION_NOTIFY on it. */
4626
4627 static void
4628 dw2_expand_symtabs_matching_one
4629 (dwarf2_per_cu_data *per_cu,
4630 dwarf2_per_objfile *per_objfile,
4631 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4632 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4633 {
4634 if (file_matcher == NULL || per_cu->v.quick->mark)
4635 {
4636 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4637
4638 compunit_symtab *symtab
4639 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4640 gdb_assert (symtab != nullptr);
4641
4642 if (expansion_notify != NULL && symtab_was_null)
4643 expansion_notify (symtab);
4644 }
4645 }
4646
4647 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4648 matched, to expand corresponding CUs that were marked. IDX is the
4649 index of the symbol name that matched. */
4650
4651 static void
4652 dw2_expand_marked_cus
4653 (dwarf2_per_objfile *per_objfile, offset_type idx,
4654 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4655 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4656 search_domain kind)
4657 {
4658 offset_type *vec, vec_len, vec_idx;
4659 bool global_seen = false;
4660 mapped_index &index = *per_objfile->per_bfd->index_table;
4661
4662 vec = (offset_type *) (index.constant_pool
4663 + MAYBE_SWAP (index.symbol_table[idx].vec));
4664 vec_len = MAYBE_SWAP (vec[0]);
4665 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4666 {
4667 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4668 /* This value is only valid for index versions >= 7. */
4669 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4670 gdb_index_symbol_kind symbol_kind =
4671 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4672 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4673 /* Only check the symbol attributes if they're present.
4674 Indices prior to version 7 don't record them,
4675 and indices >= 7 may elide them for certain symbols
4676 (gold does this). */
4677 int attrs_valid =
4678 (index.version >= 7
4679 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4680
4681 /* Work around gold/15646. */
4682 if (attrs_valid
4683 && !is_static
4684 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4685 {
4686 if (global_seen)
4687 continue;
4688
4689 global_seen = true;
4690 }
4691
4692 /* Only check the symbol's kind if it has one. */
4693 if (attrs_valid)
4694 {
4695 switch (kind)
4696 {
4697 case VARIABLES_DOMAIN:
4698 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4699 continue;
4700 break;
4701 case FUNCTIONS_DOMAIN:
4702 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4703 continue;
4704 break;
4705 case TYPES_DOMAIN:
4706 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4707 continue;
4708 break;
4709 case MODULES_DOMAIN:
4710 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4711 continue;
4712 break;
4713 default:
4714 break;
4715 }
4716 }
4717
4718 /* Don't crash on bad data. */
4719 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4720 + per_objfile->per_bfd->all_type_units.size ()))
4721 {
4722 complaint (_(".gdb_index entry has bad CU index"
4723 " [in module %s]"), objfile_name (per_objfile->objfile));
4724 continue;
4725 }
4726
4727 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4728 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4729 expansion_notify);
4730 }
4731 }
4732
4733 /* If FILE_MATCHER is non-NULL, set all the
4734 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4735 that match FILE_MATCHER. */
4736
4737 static void
4738 dw_expand_symtabs_matching_file_matcher
4739 (dwarf2_per_objfile *per_objfile,
4740 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4741 {
4742 if (file_matcher == NULL)
4743 return;
4744
4745 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4746 htab_eq_pointer,
4747 NULL, xcalloc, xfree));
4748 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4749 htab_eq_pointer,
4750 NULL, xcalloc, xfree));
4751
4752 /* The rule is CUs specify all the files, including those used by
4753 any TU, so there's no need to scan TUs here. */
4754
4755 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4756 {
4757 QUIT;
4758
4759 per_cu->v.quick->mark = 0;
4760
4761 /* We only need to look at symtabs not already expanded. */
4762 if (per_objfile->symtab_set_p (per_cu))
4763 continue;
4764
4765 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4766 if (file_data == NULL)
4767 continue;
4768
4769 if (htab_find (visited_not_found.get (), file_data) != NULL)
4770 continue;
4771 else if (htab_find (visited_found.get (), file_data) != NULL)
4772 {
4773 per_cu->v.quick->mark = 1;
4774 continue;
4775 }
4776
4777 for (int j = 0; j < file_data->num_file_names; ++j)
4778 {
4779 const char *this_real_name;
4780
4781 if (file_matcher (file_data->file_names[j], false))
4782 {
4783 per_cu->v.quick->mark = 1;
4784 break;
4785 }
4786
4787 /* Before we invoke realpath, which can get expensive when many
4788 files are involved, do a quick comparison of the basenames. */
4789 if (!basenames_may_differ
4790 && !file_matcher (lbasename (file_data->file_names[j]),
4791 true))
4792 continue;
4793
4794 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4795 if (file_matcher (this_real_name, false))
4796 {
4797 per_cu->v.quick->mark = 1;
4798 break;
4799 }
4800 }
4801
4802 void **slot = htab_find_slot (per_cu->v.quick->mark
4803 ? visited_found.get ()
4804 : visited_not_found.get (),
4805 file_data, INSERT);
4806 *slot = file_data;
4807 }
4808 }
4809
4810 static void
4811 dw2_expand_symtabs_matching
4812 (struct objfile *objfile,
4813 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4814 const lookup_name_info *lookup_name,
4815 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4816 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4817 enum search_domain kind)
4818 {
4819 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4820
4821 /* index_table is NULL if OBJF_READNOW. */
4822 if (!per_objfile->per_bfd->index_table)
4823 return;
4824
4825 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4826
4827 if (symbol_matcher == NULL && lookup_name == NULL)
4828 {
4829 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4830 {
4831 QUIT;
4832
4833 dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4834 file_matcher, expansion_notify);
4835 }
4836 return;
4837 }
4838
4839 mapped_index &index = *per_objfile->per_bfd->index_table;
4840
4841 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4842 symbol_matcher,
4843 kind, [&] (offset_type idx)
4844 {
4845 dw2_expand_marked_cus (per_objfile, idx, file_matcher, expansion_notify,
4846 kind);
4847 return true;
4848 }, per_objfile);
4849 }
4850
4851 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4852 symtab. */
4853
4854 static struct compunit_symtab *
4855 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4856 CORE_ADDR pc)
4857 {
4858 int i;
4859
4860 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4861 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4862 return cust;
4863
4864 if (cust->includes == NULL)
4865 return NULL;
4866
4867 for (i = 0; cust->includes[i]; ++i)
4868 {
4869 struct compunit_symtab *s = cust->includes[i];
4870
4871 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4872 if (s != NULL)
4873 return s;
4874 }
4875
4876 return NULL;
4877 }
4878
4879 static struct compunit_symtab *
4880 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4881 struct bound_minimal_symbol msymbol,
4882 CORE_ADDR pc,
4883 struct obj_section *section,
4884 int warn_if_readin)
4885 {
4886 struct dwarf2_per_cu_data *data;
4887 struct compunit_symtab *result;
4888
4889 if (!objfile->partial_symtabs->psymtabs_addrmap)
4890 return NULL;
4891
4892 CORE_ADDR baseaddr = objfile->text_section_offset ();
4893 data = (struct dwarf2_per_cu_data *) addrmap_find
4894 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4895 if (!data)
4896 return NULL;
4897
4898 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4899 if (warn_if_readin && per_objfile->symtab_set_p (data))
4900 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4901 paddress (objfile->arch (), pc));
4902
4903 result = recursively_find_pc_sect_compunit_symtab
4904 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4905
4906 gdb_assert (result != NULL);
4907 return result;
4908 }
4909
4910 static void
4911 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4912 void *data, int need_fullname)
4913 {
4914 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4915
4916 if (!per_objfile->per_bfd->filenames_cache)
4917 {
4918 per_objfile->per_bfd->filenames_cache.emplace ();
4919
4920 htab_up visited (htab_create_alloc (10,
4921 htab_hash_pointer, htab_eq_pointer,
4922 NULL, xcalloc, xfree));
4923
4924 /* The rule is CUs specify all the files, including those used
4925 by any TU, so there's no need to scan TUs here. We can
4926 ignore file names coming from already-expanded CUs. */
4927
4928 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4929 {
4930 if (per_objfile->symtab_set_p (per_cu))
4931 {
4932 void **slot = htab_find_slot (visited.get (),
4933 per_cu->v.quick->file_names,
4934 INSERT);
4935
4936 *slot = per_cu->v.quick->file_names;
4937 }
4938 }
4939
4940 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4941 {
4942 /* We only need to look at symtabs not already expanded. */
4943 if (per_objfile->symtab_set_p (per_cu))
4944 continue;
4945
4946 quick_file_names *file_data
4947 = dw2_get_file_names (per_cu, per_objfile);
4948 if (file_data == NULL)
4949 continue;
4950
4951 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4952 if (*slot)
4953 {
4954 /* Already visited. */
4955 continue;
4956 }
4957 *slot = file_data;
4958
4959 for (int j = 0; j < file_data->num_file_names; ++j)
4960 {
4961 const char *filename = file_data->file_names[j];
4962 per_objfile->per_bfd->filenames_cache->seen (filename);
4963 }
4964 }
4965 }
4966
4967 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
4968 {
4969 gdb::unique_xmalloc_ptr<char> this_real_name;
4970
4971 if (need_fullname)
4972 this_real_name = gdb_realpath (filename);
4973 (*fun) (filename, this_real_name.get (), data);
4974 });
4975 }
4976
4977 static int
4978 dw2_has_symbols (struct objfile *objfile)
4979 {
4980 return 1;
4981 }
4982
4983 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4984 {
4985 dw2_has_symbols,
4986 dw2_find_last_source_symtab,
4987 dw2_forget_cached_source_info,
4988 dw2_map_symtabs_matching_filename,
4989 dw2_lookup_symbol,
4990 NULL,
4991 dw2_print_stats,
4992 dw2_dump,
4993 dw2_expand_symtabs_for_function,
4994 dw2_expand_all_symtabs,
4995 dw2_expand_symtabs_with_fullname,
4996 dw2_map_matching_symbols,
4997 dw2_expand_symtabs_matching,
4998 dw2_find_pc_sect_compunit_symtab,
4999 NULL,
5000 dw2_map_symbol_filenames
5001 };
5002
5003 /* DWARF-5 debug_names reader. */
5004
5005 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5006 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5007
5008 /* A helper function that reads the .debug_names section in SECTION
5009 and fills in MAP. FILENAME is the name of the file containing the
5010 section; it is used for error reporting.
5011
5012 Returns true if all went well, false otherwise. */
5013
5014 static bool
5015 read_debug_names_from_section (struct objfile *objfile,
5016 const char *filename,
5017 struct dwarf2_section_info *section,
5018 mapped_debug_names &map)
5019 {
5020 if (section->empty ())
5021 return false;
5022
5023 /* Older elfutils strip versions could keep the section in the main
5024 executable while splitting it for the separate debug info file. */
5025 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5026 return false;
5027
5028 section->read (objfile);
5029
5030 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
5031
5032 const gdb_byte *addr = section->buffer;
5033
5034 bfd *const abfd = section->get_bfd_owner ();
5035
5036 unsigned int bytes_read;
5037 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5038 addr += bytes_read;
5039
5040 map.dwarf5_is_dwarf64 = bytes_read != 4;
5041 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5042 if (bytes_read + length != section->size)
5043 {
5044 /* There may be multiple per-CU indices. */
5045 warning (_("Section .debug_names in %s length %s does not match "
5046 "section length %s, ignoring .debug_names."),
5047 filename, plongest (bytes_read + length),
5048 pulongest (section->size));
5049 return false;
5050 }
5051
5052 /* The version number. */
5053 uint16_t version = read_2_bytes (abfd, addr);
5054 addr += 2;
5055 if (version != 5)
5056 {
5057 warning (_("Section .debug_names in %s has unsupported version %d, "
5058 "ignoring .debug_names."),
5059 filename, version);
5060 return false;
5061 }
5062
5063 /* Padding. */
5064 uint16_t padding = read_2_bytes (abfd, addr);
5065 addr += 2;
5066 if (padding != 0)
5067 {
5068 warning (_("Section .debug_names in %s has unsupported padding %d, "
5069 "ignoring .debug_names."),
5070 filename, padding);
5071 return false;
5072 }
5073
5074 /* comp_unit_count - The number of CUs in the CU list. */
5075 map.cu_count = read_4_bytes (abfd, addr);
5076 addr += 4;
5077
5078 /* local_type_unit_count - The number of TUs in the local TU
5079 list. */
5080 map.tu_count = read_4_bytes (abfd, addr);
5081 addr += 4;
5082
5083 /* foreign_type_unit_count - The number of TUs in the foreign TU
5084 list. */
5085 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5086 addr += 4;
5087 if (foreign_tu_count != 0)
5088 {
5089 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5090 "ignoring .debug_names."),
5091 filename, static_cast<unsigned long> (foreign_tu_count));
5092 return false;
5093 }
5094
5095 /* bucket_count - The number of hash buckets in the hash lookup
5096 table. */
5097 map.bucket_count = read_4_bytes (abfd, addr);
5098 addr += 4;
5099
5100 /* name_count - The number of unique names in the index. */
5101 map.name_count = read_4_bytes (abfd, addr);
5102 addr += 4;
5103
5104 /* abbrev_table_size - The size in bytes of the abbreviations
5105 table. */
5106 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5107 addr += 4;
5108
5109 /* augmentation_string_size - The size in bytes of the augmentation
5110 string. This value is rounded up to a multiple of 4. */
5111 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5112 addr += 4;
5113 map.augmentation_is_gdb = ((augmentation_string_size
5114 == sizeof (dwarf5_augmentation))
5115 && memcmp (addr, dwarf5_augmentation,
5116 sizeof (dwarf5_augmentation)) == 0);
5117 augmentation_string_size += (-augmentation_string_size) & 3;
5118 addr += augmentation_string_size;
5119
5120 /* List of CUs */
5121 map.cu_table_reordered = addr;
5122 addr += map.cu_count * map.offset_size;
5123
5124 /* List of Local TUs */
5125 map.tu_table_reordered = addr;
5126 addr += map.tu_count * map.offset_size;
5127
5128 /* Hash Lookup Table */
5129 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5130 addr += map.bucket_count * 4;
5131 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5132 addr += map.name_count * 4;
5133
5134 /* Name Table */
5135 map.name_table_string_offs_reordered = addr;
5136 addr += map.name_count * map.offset_size;
5137 map.name_table_entry_offs_reordered = addr;
5138 addr += map.name_count * map.offset_size;
5139
5140 const gdb_byte *abbrev_table_start = addr;
5141 for (;;)
5142 {
5143 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5144 addr += bytes_read;
5145 if (index_num == 0)
5146 break;
5147
5148 const auto insertpair
5149 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5150 if (!insertpair.second)
5151 {
5152 warning (_("Section .debug_names in %s has duplicate index %s, "
5153 "ignoring .debug_names."),
5154 filename, pulongest (index_num));
5155 return false;
5156 }
5157 mapped_debug_names::index_val &indexval = insertpair.first->second;
5158 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5159 addr += bytes_read;
5160
5161 for (;;)
5162 {
5163 mapped_debug_names::index_val::attr attr;
5164 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5165 addr += bytes_read;
5166 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5167 addr += bytes_read;
5168 if (attr.form == DW_FORM_implicit_const)
5169 {
5170 attr.implicit_const = read_signed_leb128 (abfd, addr,
5171 &bytes_read);
5172 addr += bytes_read;
5173 }
5174 if (attr.dw_idx == 0 && attr.form == 0)
5175 break;
5176 indexval.attr_vec.push_back (std::move (attr));
5177 }
5178 }
5179 if (addr != abbrev_table_start + abbrev_table_size)
5180 {
5181 warning (_("Section .debug_names in %s has abbreviation_table "
5182 "of size %s vs. written as %u, ignoring .debug_names."),
5183 filename, plongest (addr - abbrev_table_start),
5184 abbrev_table_size);
5185 return false;
5186 }
5187 map.entry_pool = addr;
5188
5189 return true;
5190 }
5191
5192 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5193 list. */
5194
5195 static void
5196 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
5197 const mapped_debug_names &map,
5198 dwarf2_section_info &section,
5199 bool is_dwz)
5200 {
5201 if (!map.augmentation_is_gdb)
5202 {
5203 for (uint32_t i = 0; i < map.cu_count; ++i)
5204 {
5205 sect_offset sect_off
5206 = (sect_offset) (extract_unsigned_integer
5207 (map.cu_table_reordered + i * map.offset_size,
5208 map.offset_size,
5209 map.dwarf5_byte_order));
5210 /* We don't know the length of the CU, because the CU list in a
5211 .debug_names index can be incomplete, so we can't use the start of
5212 the next CU as end of this CU. We create the CUs here with length 0,
5213 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5214 dwarf2_per_cu_data *per_cu
5215 = create_cu_from_index_list (per_bfd, &section, is_dwz, sect_off, 0);
5216 per_bfd->all_comp_units.push_back (per_cu);
5217 }
5218 }
5219
5220 sect_offset sect_off_prev;
5221 for (uint32_t i = 0; i <= map.cu_count; ++i)
5222 {
5223 sect_offset sect_off_next;
5224 if (i < map.cu_count)
5225 {
5226 sect_off_next
5227 = (sect_offset) (extract_unsigned_integer
5228 (map.cu_table_reordered + i * map.offset_size,
5229 map.offset_size,
5230 map.dwarf5_byte_order));
5231 }
5232 else
5233 sect_off_next = (sect_offset) section.size;
5234 if (i >= 1)
5235 {
5236 const ULONGEST length = sect_off_next - sect_off_prev;
5237 dwarf2_per_cu_data *per_cu
5238 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5239 sect_off_prev, length);
5240 per_bfd->all_comp_units.push_back (per_cu);
5241 }
5242 sect_off_prev = sect_off_next;
5243 }
5244 }
5245
5246 /* Read the CU list from the mapped index, and use it to create all
5247 the CU objects for this dwarf2_per_objfile. */
5248
5249 static void
5250 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
5251 const mapped_debug_names &map,
5252 const mapped_debug_names &dwz_map)
5253 {
5254 gdb_assert (per_bfd->all_comp_units.empty ());
5255 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5256
5257 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
5258 false /* is_dwz */);
5259
5260 if (dwz_map.cu_count == 0)
5261 return;
5262
5263 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5264 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
5265 true /* is_dwz */);
5266 }
5267
5268 /* Read .debug_names. If everything went ok, initialize the "quick"
5269 elements of all the CUs and return true. Otherwise, return false. */
5270
5271 static bool
5272 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
5273 {
5274 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5275 mapped_debug_names dwz_map;
5276 struct objfile *objfile = per_objfile->objfile;
5277 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5278
5279 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5280 &per_objfile->per_bfd->debug_names, *map))
5281 return false;
5282
5283 /* Don't use the index if it's empty. */
5284 if (map->name_count == 0)
5285 return false;
5286
5287 /* If there is a .dwz file, read it so we can get its CU list as
5288 well. */
5289 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5290 if (dwz != NULL)
5291 {
5292 if (!read_debug_names_from_section (objfile,
5293 bfd_get_filename (dwz->dwz_bfd.get ()),
5294 &dwz->debug_names, dwz_map))
5295 {
5296 warning (_("could not read '.debug_names' section from %s; skipping"),
5297 bfd_get_filename (dwz->dwz_bfd.get ()));
5298 return false;
5299 }
5300 }
5301
5302 create_cus_from_debug_names (per_bfd, *map, dwz_map);
5303
5304 if (map->tu_count != 0)
5305 {
5306 /* We can only handle a single .debug_types when we have an
5307 index. */
5308 if (per_bfd->types.size () != 1)
5309 return false;
5310
5311 dwarf2_section_info *section = &per_bfd->types[0];
5312
5313 create_signatured_type_table_from_debug_names
5314 (per_objfile, *map, section, &per_bfd->abbrev);
5315 }
5316
5317 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
5318
5319 per_bfd->debug_names_table = std::move (map);
5320 per_bfd->using_index = 1;
5321 per_bfd->quick_file_names_table =
5322 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
5323
5324 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
5325 objfiles using the same BFD. */
5326 gdb_assert (per_bfd->partial_symtabs == nullptr);
5327 per_bfd->partial_symtabs = objfile->partial_symtabs;
5328
5329 return true;
5330 }
5331
5332 /* Type used to manage iterating over all CUs looking for a symbol for
5333 .debug_names. */
5334
5335 class dw2_debug_names_iterator
5336 {
5337 public:
5338 dw2_debug_names_iterator (const mapped_debug_names &map,
5339 gdb::optional<block_enum> block_index,
5340 domain_enum domain,
5341 const char *name, dwarf2_per_objfile *per_objfile)
5342 : m_map (map), m_block_index (block_index), m_domain (domain),
5343 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5344 m_per_objfile (per_objfile)
5345 {}
5346
5347 dw2_debug_names_iterator (const mapped_debug_names &map,
5348 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
5349 : m_map (map),
5350 m_search (search),
5351 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5352 m_per_objfile (per_objfile)
5353 {}
5354
5355 dw2_debug_names_iterator (const mapped_debug_names &map,
5356 block_enum block_index, domain_enum domain,
5357 uint32_t namei, dwarf2_per_objfile *per_objfile)
5358 : m_map (map), m_block_index (block_index), m_domain (domain),
5359 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5360 m_per_objfile (per_objfile)
5361 {}
5362
5363 /* Return the next matching CU or NULL if there are no more. */
5364 dwarf2_per_cu_data *next ();
5365
5366 private:
5367 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5368 const char *name,
5369 dwarf2_per_objfile *per_objfile);
5370 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5371 uint32_t namei,
5372 dwarf2_per_objfile *per_objfile);
5373
5374 /* The internalized form of .debug_names. */
5375 const mapped_debug_names &m_map;
5376
5377 /* If set, only look for symbols that match that block. Valid values are
5378 GLOBAL_BLOCK and STATIC_BLOCK. */
5379 const gdb::optional<block_enum> m_block_index;
5380
5381 /* The kind of symbol we're looking for. */
5382 const domain_enum m_domain = UNDEF_DOMAIN;
5383 const search_domain m_search = ALL_DOMAIN;
5384
5385 /* The list of CUs from the index entry of the symbol, or NULL if
5386 not found. */
5387 const gdb_byte *m_addr;
5388
5389 dwarf2_per_objfile *m_per_objfile;
5390 };
5391
5392 const char *
5393 mapped_debug_names::namei_to_name
5394 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
5395 {
5396 const ULONGEST namei_string_offs
5397 = extract_unsigned_integer ((name_table_string_offs_reordered
5398 + namei * offset_size),
5399 offset_size,
5400 dwarf5_byte_order);
5401 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
5402 }
5403
5404 /* Find a slot in .debug_names for the object named NAME. If NAME is
5405 found, return pointer to its pool data. If NAME cannot be found,
5406 return NULL. */
5407
5408 const gdb_byte *
5409 dw2_debug_names_iterator::find_vec_in_debug_names
5410 (const mapped_debug_names &map, const char *name,
5411 dwarf2_per_objfile *per_objfile)
5412 {
5413 int (*cmp) (const char *, const char *);
5414
5415 gdb::unique_xmalloc_ptr<char> without_params;
5416 if (current_language->la_language == language_cplus
5417 || current_language->la_language == language_fortran
5418 || current_language->la_language == language_d)
5419 {
5420 /* NAME is already canonical. Drop any qualifiers as
5421 .debug_names does not contain any. */
5422
5423 if (strchr (name, '(') != NULL)
5424 {
5425 without_params = cp_remove_params (name);
5426 if (without_params != NULL)
5427 name = without_params.get ();
5428 }
5429 }
5430
5431 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5432
5433 const uint32_t full_hash = dwarf5_djb_hash (name);
5434 uint32_t namei
5435 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5436 (map.bucket_table_reordered
5437 + (full_hash % map.bucket_count)), 4,
5438 map.dwarf5_byte_order);
5439 if (namei == 0)
5440 return NULL;
5441 --namei;
5442 if (namei >= map.name_count)
5443 {
5444 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5445 "[in module %s]"),
5446 namei, map.name_count,
5447 objfile_name (per_objfile->objfile));
5448 return NULL;
5449 }
5450
5451 for (;;)
5452 {
5453 const uint32_t namei_full_hash
5454 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5455 (map.hash_table_reordered + namei), 4,
5456 map.dwarf5_byte_order);
5457 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5458 return NULL;
5459
5460 if (full_hash == namei_full_hash)
5461 {
5462 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5463
5464 #if 0 /* An expensive sanity check. */
5465 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5466 {
5467 complaint (_("Wrong .debug_names hash for string at index %u "
5468 "[in module %s]"),
5469 namei, objfile_name (dwarf2_per_objfile->objfile));
5470 return NULL;
5471 }
5472 #endif
5473
5474 if (cmp (namei_string, name) == 0)
5475 {
5476 const ULONGEST namei_entry_offs
5477 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5478 + namei * map.offset_size),
5479 map.offset_size, map.dwarf5_byte_order);
5480 return map.entry_pool + namei_entry_offs;
5481 }
5482 }
5483
5484 ++namei;
5485 if (namei >= map.name_count)
5486 return NULL;
5487 }
5488 }
5489
5490 const gdb_byte *
5491 dw2_debug_names_iterator::find_vec_in_debug_names
5492 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5493 {
5494 if (namei >= map.name_count)
5495 {
5496 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5497 "[in module %s]"),
5498 namei, map.name_count,
5499 objfile_name (per_objfile->objfile));
5500 return NULL;
5501 }
5502
5503 const ULONGEST namei_entry_offs
5504 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5505 + namei * map.offset_size),
5506 map.offset_size, map.dwarf5_byte_order);
5507 return map.entry_pool + namei_entry_offs;
5508 }
5509
5510 /* See dw2_debug_names_iterator. */
5511
5512 dwarf2_per_cu_data *
5513 dw2_debug_names_iterator::next ()
5514 {
5515 if (m_addr == NULL)
5516 return NULL;
5517
5518 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5519 struct objfile *objfile = m_per_objfile->objfile;
5520 bfd *const abfd = objfile->obfd;
5521
5522 again:
5523
5524 unsigned int bytes_read;
5525 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5526 m_addr += bytes_read;
5527 if (abbrev == 0)
5528 return NULL;
5529
5530 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5531 if (indexval_it == m_map.abbrev_map.cend ())
5532 {
5533 complaint (_("Wrong .debug_names undefined abbrev code %s "
5534 "[in module %s]"),
5535 pulongest (abbrev), objfile_name (objfile));
5536 return NULL;
5537 }
5538 const mapped_debug_names::index_val &indexval = indexval_it->second;
5539 enum class symbol_linkage {
5540 unknown,
5541 static_,
5542 extern_,
5543 } symbol_linkage_ = symbol_linkage::unknown;
5544 dwarf2_per_cu_data *per_cu = NULL;
5545 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5546 {
5547 ULONGEST ull;
5548 switch (attr.form)
5549 {
5550 case DW_FORM_implicit_const:
5551 ull = attr.implicit_const;
5552 break;
5553 case DW_FORM_flag_present:
5554 ull = 1;
5555 break;
5556 case DW_FORM_udata:
5557 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5558 m_addr += bytes_read;
5559 break;
5560 case DW_FORM_ref4:
5561 ull = read_4_bytes (abfd, m_addr);
5562 m_addr += 4;
5563 break;
5564 case DW_FORM_ref8:
5565 ull = read_8_bytes (abfd, m_addr);
5566 m_addr += 8;
5567 break;
5568 case DW_FORM_ref_sig8:
5569 ull = read_8_bytes (abfd, m_addr);
5570 m_addr += 8;
5571 break;
5572 default:
5573 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5574 dwarf_form_name (attr.form),
5575 objfile_name (objfile));
5576 return NULL;
5577 }
5578 switch (attr.dw_idx)
5579 {
5580 case DW_IDX_compile_unit:
5581 /* Don't crash on bad data. */
5582 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
5583 {
5584 complaint (_(".debug_names entry has bad CU index %s"
5585 " [in module %s]"),
5586 pulongest (ull),
5587 objfile_name (objfile));
5588 continue;
5589 }
5590 per_cu = per_bfd->get_cutu (ull);
5591 break;
5592 case DW_IDX_type_unit:
5593 /* Don't crash on bad data. */
5594 if (ull >= per_bfd->all_type_units.size ())
5595 {
5596 complaint (_(".debug_names entry has bad TU index %s"
5597 " [in module %s]"),
5598 pulongest (ull),
5599 objfile_name (objfile));
5600 continue;
5601 }
5602 per_cu = &per_bfd->get_tu (ull)->per_cu;
5603 break;
5604 case DW_IDX_die_offset:
5605 /* In a per-CU index (as opposed to a per-module index), index
5606 entries without CU attribute implicitly refer to the single CU. */
5607 if (per_cu == NULL)
5608 per_cu = per_bfd->get_cu (0);
5609 break;
5610 case DW_IDX_GNU_internal:
5611 if (!m_map.augmentation_is_gdb)
5612 break;
5613 symbol_linkage_ = symbol_linkage::static_;
5614 break;
5615 case DW_IDX_GNU_external:
5616 if (!m_map.augmentation_is_gdb)
5617 break;
5618 symbol_linkage_ = symbol_linkage::extern_;
5619 break;
5620 }
5621 }
5622
5623 /* Skip if already read in. */
5624 if (m_per_objfile->symtab_set_p (per_cu))
5625 goto again;
5626
5627 /* Check static vs global. */
5628 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5629 {
5630 const bool want_static = *m_block_index == STATIC_BLOCK;
5631 const bool symbol_is_static =
5632 symbol_linkage_ == symbol_linkage::static_;
5633 if (want_static != symbol_is_static)
5634 goto again;
5635 }
5636
5637 /* Match dw2_symtab_iter_next, symbol_kind
5638 and debug_names::psymbol_tag. */
5639 switch (m_domain)
5640 {
5641 case VAR_DOMAIN:
5642 switch (indexval.dwarf_tag)
5643 {
5644 case DW_TAG_variable:
5645 case DW_TAG_subprogram:
5646 /* Some types are also in VAR_DOMAIN. */
5647 case DW_TAG_typedef:
5648 case DW_TAG_structure_type:
5649 break;
5650 default:
5651 goto again;
5652 }
5653 break;
5654 case STRUCT_DOMAIN:
5655 switch (indexval.dwarf_tag)
5656 {
5657 case DW_TAG_typedef:
5658 case DW_TAG_structure_type:
5659 break;
5660 default:
5661 goto again;
5662 }
5663 break;
5664 case LABEL_DOMAIN:
5665 switch (indexval.dwarf_tag)
5666 {
5667 case 0:
5668 case DW_TAG_variable:
5669 break;
5670 default:
5671 goto again;
5672 }
5673 break;
5674 case MODULE_DOMAIN:
5675 switch (indexval.dwarf_tag)
5676 {
5677 case DW_TAG_module:
5678 break;
5679 default:
5680 goto again;
5681 }
5682 break;
5683 default:
5684 break;
5685 }
5686
5687 /* Match dw2_expand_symtabs_matching, symbol_kind and
5688 debug_names::psymbol_tag. */
5689 switch (m_search)
5690 {
5691 case VARIABLES_DOMAIN:
5692 switch (indexval.dwarf_tag)
5693 {
5694 case DW_TAG_variable:
5695 break;
5696 default:
5697 goto again;
5698 }
5699 break;
5700 case FUNCTIONS_DOMAIN:
5701 switch (indexval.dwarf_tag)
5702 {
5703 case DW_TAG_subprogram:
5704 break;
5705 default:
5706 goto again;
5707 }
5708 break;
5709 case TYPES_DOMAIN:
5710 switch (indexval.dwarf_tag)
5711 {
5712 case DW_TAG_typedef:
5713 case DW_TAG_structure_type:
5714 break;
5715 default:
5716 goto again;
5717 }
5718 break;
5719 case MODULES_DOMAIN:
5720 switch (indexval.dwarf_tag)
5721 {
5722 case DW_TAG_module:
5723 break;
5724 default:
5725 goto again;
5726 }
5727 default:
5728 break;
5729 }
5730
5731 return per_cu;
5732 }
5733
5734 static struct compunit_symtab *
5735 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5736 const char *name, domain_enum domain)
5737 {
5738 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5739
5740 const auto &mapp = per_objfile->per_bfd->debug_names_table;
5741 if (!mapp)
5742 {
5743 /* index is NULL if OBJF_READNOW. */
5744 return NULL;
5745 }
5746 const auto &map = *mapp;
5747
5748 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
5749
5750 struct compunit_symtab *stab_best = NULL;
5751 struct dwarf2_per_cu_data *per_cu;
5752 while ((per_cu = iter.next ()) != NULL)
5753 {
5754 struct symbol *sym, *with_opaque = NULL;
5755 compunit_symtab *stab
5756 = dw2_instantiate_symtab (per_cu, per_objfile, false);
5757 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5758 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5759
5760 sym = block_find_symbol (block, name, domain,
5761 block_find_non_opaque_type_preferred,
5762 &with_opaque);
5763
5764 /* Some caution must be observed with overloaded functions and
5765 methods, since the index will not contain any overload
5766 information (but NAME might contain it). */
5767
5768 if (sym != NULL
5769 && strcmp_iw (sym->search_name (), name) == 0)
5770 return stab;
5771 if (with_opaque != NULL
5772 && strcmp_iw (with_opaque->search_name (), name) == 0)
5773 stab_best = stab;
5774
5775 /* Keep looking through other CUs. */
5776 }
5777
5778 return stab_best;
5779 }
5780
5781 /* This dumps minimal information about .debug_names. It is called
5782 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5783 uses this to verify that .debug_names has been loaded. */
5784
5785 static void
5786 dw2_debug_names_dump (struct objfile *objfile)
5787 {
5788 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5789
5790 gdb_assert (per_objfile->per_bfd->using_index);
5791 printf_filtered (".debug_names:");
5792 if (per_objfile->per_bfd->debug_names_table)
5793 printf_filtered (" exists\n");
5794 else
5795 printf_filtered (" faked for \"readnow\"\n");
5796 printf_filtered ("\n");
5797 }
5798
5799 static void
5800 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5801 const char *func_name)
5802 {
5803 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5804
5805 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5806 if (per_objfile->per_bfd->debug_names_table)
5807 {
5808 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5809
5810 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
5811 per_objfile);
5812
5813 struct dwarf2_per_cu_data *per_cu;
5814 while ((per_cu = iter.next ()) != NULL)
5815 dw2_instantiate_symtab (per_cu, per_objfile, false);
5816 }
5817 }
5818
5819 static void
5820 dw2_debug_names_map_matching_symbols
5821 (struct objfile *objfile,
5822 const lookup_name_info &name, domain_enum domain,
5823 int global,
5824 gdb::function_view<symbol_found_callback_ftype> callback,
5825 symbol_compare_ftype *ordered_compare)
5826 {
5827 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5828
5829 /* debug_names_table is NULL if OBJF_READNOW. */
5830 if (!per_objfile->per_bfd->debug_names_table)
5831 return;
5832
5833 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5834 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5835
5836 const char *match_name = name.ada ().lookup_name ().c_str ();
5837 auto matcher = [&] (const char *symname)
5838 {
5839 if (ordered_compare == nullptr)
5840 return true;
5841 return ordered_compare (symname, match_name) == 0;
5842 };
5843
5844 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5845 [&] (offset_type namei)
5846 {
5847 /* The name was matched, now expand corresponding CUs that were
5848 marked. */
5849 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
5850 per_objfile);
5851
5852 struct dwarf2_per_cu_data *per_cu;
5853 while ((per_cu = iter.next ()) != NULL)
5854 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5855 nullptr);
5856 return true;
5857 }, per_objfile);
5858
5859 /* It's a shame we couldn't do this inside the
5860 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5861 that have already been expanded. Instead, this loop matches what
5862 the psymtab code does. */
5863 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5864 {
5865 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
5866 if (symtab != nullptr)
5867 {
5868 const struct block *block
5869 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5870 if (!iterate_over_symbols_terminated (block, name,
5871 domain, callback))
5872 break;
5873 }
5874 }
5875 }
5876
5877 static void
5878 dw2_debug_names_expand_symtabs_matching
5879 (struct objfile *objfile,
5880 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5881 const lookup_name_info *lookup_name,
5882 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5883 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5884 enum search_domain kind)
5885 {
5886 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5887
5888 /* debug_names_table is NULL if OBJF_READNOW. */
5889 if (!per_objfile->per_bfd->debug_names_table)
5890 return;
5891
5892 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5893
5894 if (symbol_matcher == NULL && lookup_name == NULL)
5895 {
5896 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5897 {
5898 QUIT;
5899
5900 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5901 expansion_notify);
5902 }
5903 return;
5904 }
5905
5906 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5907
5908 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5909 symbol_matcher,
5910 kind, [&] (offset_type namei)
5911 {
5912 /* The name was matched, now expand corresponding CUs that were
5913 marked. */
5914 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
5915
5916 struct dwarf2_per_cu_data *per_cu;
5917 while ((per_cu = iter.next ()) != NULL)
5918 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5919 expansion_notify);
5920 return true;
5921 }, per_objfile);
5922 }
5923
5924 const struct quick_symbol_functions dwarf2_debug_names_functions =
5925 {
5926 dw2_has_symbols,
5927 dw2_find_last_source_symtab,
5928 dw2_forget_cached_source_info,
5929 dw2_map_symtabs_matching_filename,
5930 dw2_debug_names_lookup_symbol,
5931 NULL,
5932 dw2_print_stats,
5933 dw2_debug_names_dump,
5934 dw2_debug_names_expand_symtabs_for_function,
5935 dw2_expand_all_symtabs,
5936 dw2_expand_symtabs_with_fullname,
5937 dw2_debug_names_map_matching_symbols,
5938 dw2_debug_names_expand_symtabs_matching,
5939 dw2_find_pc_sect_compunit_symtab,
5940 NULL,
5941 dw2_map_symbol_filenames
5942 };
5943
5944 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5945 to either a dwarf2_per_bfd or dwz_file object. */
5946
5947 template <typename T>
5948 static gdb::array_view<const gdb_byte>
5949 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5950 {
5951 dwarf2_section_info *section = &section_owner->gdb_index;
5952
5953 if (section->empty ())
5954 return {};
5955
5956 /* Older elfutils strip versions could keep the section in the main
5957 executable while splitting it for the separate debug info file. */
5958 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5959 return {};
5960
5961 section->read (obj);
5962
5963 /* dwarf2_section_info::size is a bfd_size_type, while
5964 gdb::array_view works with size_t. On 32-bit hosts, with
5965 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5966 is 32-bit. So we need an explicit narrowing conversion here.
5967 This is fine, because it's impossible to allocate or mmap an
5968 array/buffer larger than what size_t can represent. */
5969 return gdb::make_array_view (section->buffer, section->size);
5970 }
5971
5972 /* Lookup the index cache for the contents of the index associated to
5973 DWARF2_OBJ. */
5974
5975 static gdb::array_view<const gdb_byte>
5976 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5977 {
5978 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5979 if (build_id == nullptr)
5980 return {};
5981
5982 return global_index_cache.lookup_gdb_index (build_id,
5983 &dwarf2_per_bfd->index_cache_res);
5984 }
5985
5986 /* Same as the above, but for DWZ. */
5987
5988 static gdb::array_view<const gdb_byte>
5989 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5990 {
5991 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5992 if (build_id == nullptr)
5993 return {};
5994
5995 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5996 }
5997
5998 /* See symfile.h. */
5999
6000 bool
6001 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6002 {
6003 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6004 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6005
6006 /* If we're about to read full symbols, don't bother with the
6007 indices. In this case we also don't care if some other debug
6008 format is making psymtabs, because they are all about to be
6009 expanded anyway. */
6010 if ((objfile->flags & OBJF_READNOW))
6011 {
6012 /* When using READNOW, the using_index flag (set below) indicates that
6013 PER_BFD was already initialized, when we loaded some other objfile. */
6014 if (per_bfd->using_index)
6015 {
6016 *index_kind = dw_index_kind::GDB_INDEX;
6017 per_objfile->resize_symtabs ();
6018 return true;
6019 }
6020
6021 per_bfd->using_index = 1;
6022 create_all_comp_units (per_objfile);
6023 create_all_type_units (per_objfile);
6024 per_bfd->quick_file_names_table
6025 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
6026 per_objfile->resize_symtabs ();
6027
6028 for (int i = 0; i < (per_bfd->all_comp_units.size ()
6029 + per_bfd->all_type_units.size ()); ++i)
6030 {
6031 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
6032
6033 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6034 struct dwarf2_per_cu_quick_data);
6035 }
6036
6037 /* Return 1 so that gdb sees the "quick" functions. However,
6038 these functions will be no-ops because we will have expanded
6039 all symtabs. */
6040 *index_kind = dw_index_kind::GDB_INDEX;
6041 return true;
6042 }
6043
6044 /* Was a debug names index already read when we processed an objfile sharing
6045 PER_BFD? */
6046 if (per_bfd->debug_names_table != nullptr)
6047 {
6048 *index_kind = dw_index_kind::DEBUG_NAMES;
6049 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
6050 per_objfile->resize_symtabs ();
6051 return true;
6052 }
6053
6054 /* Was a GDB index already read when we processed an objfile sharing
6055 PER_BFD? */
6056 if (per_bfd->index_table != nullptr)
6057 {
6058 *index_kind = dw_index_kind::GDB_INDEX;
6059 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
6060 per_objfile->resize_symtabs ();
6061 return true;
6062 }
6063
6064 /* There might already be partial symtabs built for this BFD. This happens
6065 when loading the same binary twice with the index-cache enabled. If so,
6066 don't try to read an index. The objfile / per_objfile initialization will
6067 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
6068 code path. */
6069 if (per_bfd->partial_symtabs != nullptr)
6070 return false;
6071
6072 if (dwarf2_read_debug_names (per_objfile))
6073 {
6074 *index_kind = dw_index_kind::DEBUG_NAMES;
6075 per_objfile->resize_symtabs ();
6076 return true;
6077 }
6078
6079 if (dwarf2_read_gdb_index (per_objfile,
6080 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
6081 get_gdb_index_contents_from_section<dwz_file>))
6082 {
6083 *index_kind = dw_index_kind::GDB_INDEX;
6084 per_objfile->resize_symtabs ();
6085 return true;
6086 }
6087
6088 /* ... otherwise, try to find the index in the index cache. */
6089 if (dwarf2_read_gdb_index (per_objfile,
6090 get_gdb_index_contents_from_cache,
6091 get_gdb_index_contents_from_cache_dwz))
6092 {
6093 global_index_cache.hit ();
6094 *index_kind = dw_index_kind::GDB_INDEX;
6095 per_objfile->resize_symtabs ();
6096 return true;
6097 }
6098
6099 global_index_cache.miss ();
6100 return false;
6101 }
6102
6103 \f
6104
6105 /* Build a partial symbol table. */
6106
6107 void
6108 dwarf2_build_psymtabs (struct objfile *objfile)
6109 {
6110 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6111 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6112
6113 if (per_bfd->partial_symtabs != nullptr)
6114 {
6115 /* Partial symbols were already read, so now we can simply
6116 attach them. */
6117 objfile->partial_symtabs = per_bfd->partial_symtabs;
6118 per_objfile->resize_symtabs ();
6119 return;
6120 }
6121
6122 init_psymbol_list (objfile, 1024);
6123
6124 try
6125 {
6126 /* This isn't really ideal: all the data we allocate on the
6127 objfile's obstack is still uselessly kept around. However,
6128 freeing it seems unsafe. */
6129 psymtab_discarder psymtabs (objfile);
6130 dwarf2_build_psymtabs_hard (per_objfile);
6131 psymtabs.keep ();
6132
6133 per_objfile->resize_symtabs ();
6134
6135 /* (maybe) store an index in the cache. */
6136 global_index_cache.store (per_objfile);
6137 }
6138 catch (const gdb_exception_error &except)
6139 {
6140 exception_print (gdb_stderr, except);
6141 }
6142
6143 /* Finish by setting the local reference to partial symtabs, so that
6144 we don't try to read them again if reading another objfile with the same
6145 BFD. If we can't in fact share, this won't make a difference anyway as
6146 the dwarf2_per_bfd object won't be shared. */
6147 per_bfd->partial_symtabs = objfile->partial_symtabs;
6148 }
6149
6150 /* Find the base address of the compilation unit for range lists and
6151 location lists. It will normally be specified by DW_AT_low_pc.
6152 In DWARF-3 draft 4, the base address could be overridden by
6153 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6154 compilation units with discontinuous ranges. */
6155
6156 static void
6157 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6158 {
6159 struct attribute *attr;
6160
6161 cu->base_address.reset ();
6162
6163 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6164 if (attr != nullptr)
6165 cu->base_address = attr->value_as_address ();
6166 else
6167 {
6168 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6169 if (attr != nullptr)
6170 cu->base_address = attr->value_as_address ();
6171 }
6172 }
6173
6174 /* Helper function that returns the proper abbrev section for
6175 THIS_CU. */
6176
6177 static struct dwarf2_section_info *
6178 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6179 {
6180 struct dwarf2_section_info *abbrev;
6181 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6182
6183 if (this_cu->is_dwz)
6184 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
6185 else
6186 abbrev = &per_bfd->abbrev;
6187
6188 return abbrev;
6189 }
6190
6191 /* Fetch the abbreviation table offset from a comp or type unit header. */
6192
6193 static sect_offset
6194 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
6195 struct dwarf2_section_info *section,
6196 sect_offset sect_off)
6197 {
6198 bfd *abfd = section->get_bfd_owner ();
6199 const gdb_byte *info_ptr;
6200 unsigned int initial_length_size, offset_size;
6201 uint16_t version;
6202
6203 section->read (per_objfile->objfile);
6204 info_ptr = section->buffer + to_underlying (sect_off);
6205 read_initial_length (abfd, info_ptr, &initial_length_size);
6206 offset_size = initial_length_size == 4 ? 4 : 8;
6207 info_ptr += initial_length_size;
6208
6209 version = read_2_bytes (abfd, info_ptr);
6210 info_ptr += 2;
6211 if (version >= 5)
6212 {
6213 /* Skip unit type and address size. */
6214 info_ptr += 2;
6215 }
6216
6217 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6218 }
6219
6220 /* A partial symtab that is used only for include files. */
6221 struct dwarf2_include_psymtab : public partial_symtab
6222 {
6223 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6224 : partial_symtab (filename, objfile)
6225 {
6226 }
6227
6228 void read_symtab (struct objfile *objfile) override
6229 {
6230 /* It's an include file, no symbols to read for it.
6231 Everything is in the includer symtab. */
6232
6233 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6234 expansion of the includer psymtab. We use the dependencies[0] field to
6235 model the includer. But if we go the regular route of calling
6236 expand_psymtab here, and having expand_psymtab call expand_dependencies
6237 to expand the includer, we'll only use expand_psymtab on the includer
6238 (making it a non-toplevel psymtab), while if we expand the includer via
6239 another path, we'll use read_symtab (making it a toplevel psymtab).
6240 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6241 psymtab, and trigger read_symtab on the includer here directly. */
6242 includer ()->read_symtab (objfile);
6243 }
6244
6245 void expand_psymtab (struct objfile *objfile) override
6246 {
6247 /* This is not called by read_symtab, and should not be called by any
6248 expand_dependencies. */
6249 gdb_assert (false);
6250 }
6251
6252 bool readin_p (struct objfile *objfile) const override
6253 {
6254 return includer ()->readin_p (objfile);
6255 }
6256
6257 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6258 {
6259 return nullptr;
6260 }
6261
6262 private:
6263 partial_symtab *includer () const
6264 {
6265 /* An include psymtab has exactly one dependency: the psymtab that
6266 includes it. */
6267 gdb_assert (this->number_of_dependencies == 1);
6268 return this->dependencies[0];
6269 }
6270 };
6271
6272 /* Allocate a new partial symtab for file named NAME and mark this new
6273 partial symtab as being an include of PST. */
6274
6275 static void
6276 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6277 struct objfile *objfile)
6278 {
6279 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6280
6281 if (!IS_ABSOLUTE_PATH (subpst->filename))
6282 subpst->dirname = pst->dirname;
6283
6284 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6285 subpst->dependencies[0] = pst;
6286 subpst->number_of_dependencies = 1;
6287 }
6288
6289 /* Read the Line Number Program data and extract the list of files
6290 included by the source file represented by PST. Build an include
6291 partial symtab for each of these included files. */
6292
6293 static void
6294 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6295 struct die_info *die,
6296 dwarf2_psymtab *pst)
6297 {
6298 line_header_up lh;
6299 struct attribute *attr;
6300
6301 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6302 if (attr != nullptr)
6303 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6304 if (lh == NULL)
6305 return; /* No linetable, so no includes. */
6306
6307 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6308 that we pass in the raw text_low here; that is ok because we're
6309 only decoding the line table to make include partial symtabs, and
6310 so the addresses aren't really used. */
6311 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6312 pst->raw_text_low (), 1);
6313 }
6314
6315 static hashval_t
6316 hash_signatured_type (const void *item)
6317 {
6318 const struct signatured_type *sig_type
6319 = (const struct signatured_type *) item;
6320
6321 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6322 return sig_type->signature;
6323 }
6324
6325 static int
6326 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6327 {
6328 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6329 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6330
6331 return lhs->signature == rhs->signature;
6332 }
6333
6334 /* Allocate a hash table for signatured types. */
6335
6336 static htab_up
6337 allocate_signatured_type_table ()
6338 {
6339 return htab_up (htab_create_alloc (41,
6340 hash_signatured_type,
6341 eq_signatured_type,
6342 NULL, xcalloc, xfree));
6343 }
6344
6345 /* A helper function to add a signatured type CU to a table. */
6346
6347 static int
6348 add_signatured_type_cu_to_table (void **slot, void *datum)
6349 {
6350 struct signatured_type *sigt = (struct signatured_type *) *slot;
6351 std::vector<signatured_type *> *all_type_units
6352 = (std::vector<signatured_type *> *) datum;
6353
6354 all_type_units->push_back (sigt);
6355
6356 return 1;
6357 }
6358
6359 /* A helper for create_debug_types_hash_table. Read types from SECTION
6360 and fill them into TYPES_HTAB. It will process only type units,
6361 therefore DW_UT_type. */
6362
6363 static void
6364 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
6365 struct dwo_file *dwo_file,
6366 dwarf2_section_info *section, htab_up &types_htab,
6367 rcuh_kind section_kind)
6368 {
6369 struct objfile *objfile = per_objfile->objfile;
6370 struct dwarf2_section_info *abbrev_section;
6371 bfd *abfd;
6372 const gdb_byte *info_ptr, *end_ptr;
6373
6374 abbrev_section = (dwo_file != NULL
6375 ? &dwo_file->sections.abbrev
6376 : &per_objfile->per_bfd->abbrev);
6377
6378 if (dwarf_read_debug)
6379 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6380 section->get_name (),
6381 abbrev_section->get_file_name ());
6382
6383 section->read (objfile);
6384 info_ptr = section->buffer;
6385
6386 if (info_ptr == NULL)
6387 return;
6388
6389 /* We can't set abfd until now because the section may be empty or
6390 not present, in which case the bfd is unknown. */
6391 abfd = section->get_bfd_owner ();
6392
6393 /* We don't use cutu_reader here because we don't need to read
6394 any dies: the signature is in the header. */
6395
6396 end_ptr = info_ptr + section->size;
6397 while (info_ptr < end_ptr)
6398 {
6399 struct signatured_type *sig_type;
6400 struct dwo_unit *dwo_tu;
6401 void **slot;
6402 const gdb_byte *ptr = info_ptr;
6403 struct comp_unit_head header;
6404 unsigned int length;
6405
6406 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6407
6408 /* Initialize it due to a false compiler warning. */
6409 header.signature = -1;
6410 header.type_cu_offset_in_tu = (cu_offset) -1;
6411
6412 /* We need to read the type's signature in order to build the hash
6413 table, but we don't need anything else just yet. */
6414
6415 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
6416 abbrev_section, ptr, section_kind);
6417
6418 length = header.get_length ();
6419
6420 /* Skip dummy type units. */
6421 if (ptr >= info_ptr + length
6422 || peek_abbrev_code (abfd, ptr) == 0
6423 || (header.unit_type != DW_UT_type
6424 && header.unit_type != DW_UT_split_type))
6425 {
6426 info_ptr += length;
6427 continue;
6428 }
6429
6430 if (types_htab == NULL)
6431 {
6432 if (dwo_file)
6433 types_htab = allocate_dwo_unit_table ();
6434 else
6435 types_htab = allocate_signatured_type_table ();
6436 }
6437
6438 if (dwo_file)
6439 {
6440 sig_type = NULL;
6441 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
6442 dwo_tu->dwo_file = dwo_file;
6443 dwo_tu->signature = header.signature;
6444 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6445 dwo_tu->section = section;
6446 dwo_tu->sect_off = sect_off;
6447 dwo_tu->length = length;
6448 }
6449 else
6450 {
6451 /* N.B.: type_offset is not usable if this type uses a DWO file.
6452 The real type_offset is in the DWO file. */
6453 dwo_tu = NULL;
6454 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6455 sig_type->signature = header.signature;
6456 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6457 sig_type->per_cu.is_debug_types = 1;
6458 sig_type->per_cu.section = section;
6459 sig_type->per_cu.sect_off = sect_off;
6460 sig_type->per_cu.length = length;
6461 }
6462
6463 slot = htab_find_slot (types_htab.get (),
6464 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6465 INSERT);
6466 gdb_assert (slot != NULL);
6467 if (*slot != NULL)
6468 {
6469 sect_offset dup_sect_off;
6470
6471 if (dwo_file)
6472 {
6473 const struct dwo_unit *dup_tu
6474 = (const struct dwo_unit *) *slot;
6475
6476 dup_sect_off = dup_tu->sect_off;
6477 }
6478 else
6479 {
6480 const struct signatured_type *dup_tu
6481 = (const struct signatured_type *) *slot;
6482
6483 dup_sect_off = dup_tu->per_cu.sect_off;
6484 }
6485
6486 complaint (_("debug type entry at offset %s is duplicate to"
6487 " the entry at offset %s, signature %s"),
6488 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6489 hex_string (header.signature));
6490 }
6491 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6492
6493 if (dwarf_read_debug > 1)
6494 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6495 sect_offset_str (sect_off),
6496 hex_string (header.signature));
6497
6498 info_ptr += length;
6499 }
6500 }
6501
6502 /* Create the hash table of all entries in the .debug_types
6503 (or .debug_types.dwo) section(s).
6504 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6505 otherwise it is NULL.
6506
6507 The result is a pointer to the hash table or NULL if there are no types.
6508
6509 Note: This function processes DWO files only, not DWP files. */
6510
6511 static void
6512 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
6513 struct dwo_file *dwo_file,
6514 gdb::array_view<dwarf2_section_info> type_sections,
6515 htab_up &types_htab)
6516 {
6517 for (dwarf2_section_info &section : type_sections)
6518 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6519 rcuh_kind::TYPE);
6520 }
6521
6522 /* Create the hash table of all entries in the .debug_types section,
6523 and initialize all_type_units.
6524 The result is zero if there is an error (e.g. missing .debug_types section),
6525 otherwise non-zero. */
6526
6527 static int
6528 create_all_type_units (dwarf2_per_objfile *per_objfile)
6529 {
6530 htab_up types_htab;
6531
6532 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6533 types_htab, rcuh_kind::COMPILE);
6534 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6535 types_htab);
6536 if (types_htab == NULL)
6537 {
6538 per_objfile->per_bfd->signatured_types = NULL;
6539 return 0;
6540 }
6541
6542 per_objfile->per_bfd->signatured_types = std::move (types_htab);
6543
6544 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6545 per_objfile->per_bfd->all_type_units.reserve
6546 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
6547
6548 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
6549 add_signatured_type_cu_to_table,
6550 &per_objfile->per_bfd->all_type_units);
6551
6552 return 1;
6553 }
6554
6555 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6556 If SLOT is non-NULL, it is the entry to use in the hash table.
6557 Otherwise we find one. */
6558
6559 static struct signatured_type *
6560 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6561 {
6562 if (per_objfile->per_bfd->all_type_units.size ()
6563 == per_objfile->per_bfd->all_type_units.capacity ())
6564 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6565
6566 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6567
6568 per_objfile->resize_symtabs ();
6569
6570 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6571 sig_type->signature = sig;
6572 sig_type->per_cu.is_debug_types = 1;
6573 if (per_objfile->per_bfd->using_index)
6574 {
6575 sig_type->per_cu.v.quick =
6576 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6577 struct dwarf2_per_cu_quick_data);
6578 }
6579
6580 if (slot == NULL)
6581 {
6582 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6583 sig_type, INSERT);
6584 }
6585 gdb_assert (*slot == NULL);
6586 *slot = sig_type;
6587 /* The rest of sig_type must be filled in by the caller. */
6588 return sig_type;
6589 }
6590
6591 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6592 Fill in SIG_ENTRY with DWO_ENTRY. */
6593
6594 static void
6595 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
6596 struct signatured_type *sig_entry,
6597 struct dwo_unit *dwo_entry)
6598 {
6599 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6600
6601 /* Make sure we're not clobbering something we don't expect to. */
6602 gdb_assert (! sig_entry->per_cu.queued);
6603 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
6604 if (per_bfd->using_index)
6605 {
6606 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6607 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6608 }
6609 else
6610 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6611 gdb_assert (sig_entry->signature == dwo_entry->signature);
6612 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6613 gdb_assert (sig_entry->type_unit_group == NULL);
6614 gdb_assert (sig_entry->dwo_unit == NULL);
6615
6616 sig_entry->per_cu.section = dwo_entry->section;
6617 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6618 sig_entry->per_cu.length = dwo_entry->length;
6619 sig_entry->per_cu.reading_dwo_directly = 1;
6620 sig_entry->per_cu.per_bfd = per_bfd;
6621 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6622 sig_entry->dwo_unit = dwo_entry;
6623 }
6624
6625 /* Subroutine of lookup_signatured_type.
6626 If we haven't read the TU yet, create the signatured_type data structure
6627 for a TU to be read in directly from a DWO file, bypassing the stub.
6628 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6629 using .gdb_index, then when reading a CU we want to stay in the DWO file
6630 containing that CU. Otherwise we could end up reading several other DWO
6631 files (due to comdat folding) to process the transitive closure of all the
6632 mentioned TUs, and that can be slow. The current DWO file will have every
6633 type signature that it needs.
6634 We only do this for .gdb_index because in the psymtab case we already have
6635 to read all the DWOs to build the type unit groups. */
6636
6637 static struct signatured_type *
6638 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6639 {
6640 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6641 struct dwo_file *dwo_file;
6642 struct dwo_unit find_dwo_entry, *dwo_entry;
6643 struct signatured_type find_sig_entry, *sig_entry;
6644 void **slot;
6645
6646 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6647
6648 /* If TU skeletons have been removed then we may not have read in any
6649 TUs yet. */
6650 if (per_objfile->per_bfd->signatured_types == NULL)
6651 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6652
6653 /* We only ever need to read in one copy of a signatured type.
6654 Use the global signatured_types array to do our own comdat-folding
6655 of types. If this is the first time we're reading this TU, and
6656 the TU has an entry in .gdb_index, replace the recorded data from
6657 .gdb_index with this TU. */
6658
6659 find_sig_entry.signature = sig;
6660 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6661 &find_sig_entry, INSERT);
6662 sig_entry = (struct signatured_type *) *slot;
6663
6664 /* We can get here with the TU already read, *or* in the process of being
6665 read. Don't reassign the global entry to point to this DWO if that's
6666 the case. Also note that if the TU is already being read, it may not
6667 have come from a DWO, the program may be a mix of Fission-compiled
6668 code and non-Fission-compiled code. */
6669
6670 /* Have we already tried to read this TU?
6671 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6672 needn't exist in the global table yet). */
6673 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6674 return sig_entry;
6675
6676 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6677 dwo_unit of the TU itself. */
6678 dwo_file = cu->dwo_unit->dwo_file;
6679
6680 /* Ok, this is the first time we're reading this TU. */
6681 if (dwo_file->tus == NULL)
6682 return NULL;
6683 find_dwo_entry.signature = sig;
6684 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6685 &find_dwo_entry);
6686 if (dwo_entry == NULL)
6687 return NULL;
6688
6689 /* If the global table doesn't have an entry for this TU, add one. */
6690 if (sig_entry == NULL)
6691 sig_entry = add_type_unit (per_objfile, sig, slot);
6692
6693 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6694 sig_entry->per_cu.tu_read = 1;
6695 return sig_entry;
6696 }
6697
6698 /* Subroutine of lookup_signatured_type.
6699 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6700 then try the DWP file. If the TU stub (skeleton) has been removed then
6701 it won't be in .gdb_index. */
6702
6703 static struct signatured_type *
6704 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6705 {
6706 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6707 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6708 struct dwo_unit *dwo_entry;
6709 struct signatured_type find_sig_entry, *sig_entry;
6710 void **slot;
6711
6712 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6713 gdb_assert (dwp_file != NULL);
6714
6715 /* If TU skeletons have been removed then we may not have read in any
6716 TUs yet. */
6717 if (per_objfile->per_bfd->signatured_types == NULL)
6718 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6719
6720 find_sig_entry.signature = sig;
6721 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6722 &find_sig_entry, INSERT);
6723 sig_entry = (struct signatured_type *) *slot;
6724
6725 /* Have we already tried to read this TU?
6726 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6727 needn't exist in the global table yet). */
6728 if (sig_entry != NULL)
6729 return sig_entry;
6730
6731 if (dwp_file->tus == NULL)
6732 return NULL;
6733 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6734 1 /* is_debug_types */);
6735 if (dwo_entry == NULL)
6736 return NULL;
6737
6738 sig_entry = add_type_unit (per_objfile, sig, slot);
6739 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6740
6741 return sig_entry;
6742 }
6743
6744 /* Lookup a signature based type for DW_FORM_ref_sig8.
6745 Returns NULL if signature SIG is not present in the table.
6746 It is up to the caller to complain about this. */
6747
6748 static struct signatured_type *
6749 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6750 {
6751 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6752
6753 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6754 {
6755 /* We're in a DWO/DWP file, and we're using .gdb_index.
6756 These cases require special processing. */
6757 if (get_dwp_file (per_objfile) == NULL)
6758 return lookup_dwo_signatured_type (cu, sig);
6759 else
6760 return lookup_dwp_signatured_type (cu, sig);
6761 }
6762 else
6763 {
6764 struct signatured_type find_entry, *entry;
6765
6766 if (per_objfile->per_bfd->signatured_types == NULL)
6767 return NULL;
6768 find_entry.signature = sig;
6769 entry = ((struct signatured_type *)
6770 htab_find (per_objfile->per_bfd->signatured_types.get (),
6771 &find_entry));
6772 return entry;
6773 }
6774 }
6775
6776 /* Low level DIE reading support. */
6777
6778 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6779
6780 static void
6781 init_cu_die_reader (struct die_reader_specs *reader,
6782 struct dwarf2_cu *cu,
6783 struct dwarf2_section_info *section,
6784 struct dwo_file *dwo_file,
6785 struct abbrev_table *abbrev_table)
6786 {
6787 gdb_assert (section->readin && section->buffer != NULL);
6788 reader->abfd = section->get_bfd_owner ();
6789 reader->cu = cu;
6790 reader->dwo_file = dwo_file;
6791 reader->die_section = section;
6792 reader->buffer = section->buffer;
6793 reader->buffer_end = section->buffer + section->size;
6794 reader->abbrev_table = abbrev_table;
6795 }
6796
6797 /* Subroutine of cutu_reader to simplify it.
6798 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6799 There's just a lot of work to do, and cutu_reader is big enough
6800 already.
6801
6802 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6803 from it to the DIE in the DWO. If NULL we are skipping the stub.
6804 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6805 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6806 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6807 STUB_COMP_DIR may be non-NULL.
6808 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6809 are filled in with the info of the DIE from the DWO file.
6810 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6811 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6812 kept around for at least as long as *RESULT_READER.
6813
6814 The result is non-zero if a valid (non-dummy) DIE was found. */
6815
6816 static int
6817 read_cutu_die_from_dwo (dwarf2_cu *cu,
6818 struct dwo_unit *dwo_unit,
6819 struct die_info *stub_comp_unit_die,
6820 const char *stub_comp_dir,
6821 struct die_reader_specs *result_reader,
6822 const gdb_byte **result_info_ptr,
6823 struct die_info **result_comp_unit_die,
6824 abbrev_table_up *result_dwo_abbrev_table)
6825 {
6826 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6827 dwarf2_per_cu_data *per_cu = cu->per_cu;
6828 struct objfile *objfile = per_objfile->objfile;
6829 bfd *abfd;
6830 const gdb_byte *begin_info_ptr, *info_ptr;
6831 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6832 int i,num_extra_attrs;
6833 struct dwarf2_section_info *dwo_abbrev_section;
6834 struct die_info *comp_unit_die;
6835
6836 /* At most one of these may be provided. */
6837 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6838
6839 /* These attributes aren't processed until later:
6840 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6841 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6842 referenced later. However, these attributes are found in the stub
6843 which we won't have later. In order to not impose this complication
6844 on the rest of the code, we read them here and copy them to the
6845 DWO CU/TU die. */
6846
6847 stmt_list = NULL;
6848 low_pc = NULL;
6849 high_pc = NULL;
6850 ranges = NULL;
6851 comp_dir = NULL;
6852
6853 if (stub_comp_unit_die != NULL)
6854 {
6855 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6856 DWO file. */
6857 if (!per_cu->is_debug_types)
6858 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6859 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6860 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6861 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6862 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6863
6864 cu->addr_base = stub_comp_unit_die->addr_base ();
6865
6866 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6867 here (if needed). We need the value before we can process
6868 DW_AT_ranges. */
6869 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6870 }
6871 else if (stub_comp_dir != NULL)
6872 {
6873 /* Reconstruct the comp_dir attribute to simplify the code below. */
6874 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6875 comp_dir->name = DW_AT_comp_dir;
6876 comp_dir->form = DW_FORM_string;
6877 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6878 DW_STRING (comp_dir) = stub_comp_dir;
6879 }
6880
6881 /* Set up for reading the DWO CU/TU. */
6882 cu->dwo_unit = dwo_unit;
6883 dwarf2_section_info *section = dwo_unit->section;
6884 section->read (objfile);
6885 abfd = section->get_bfd_owner ();
6886 begin_info_ptr = info_ptr = (section->buffer
6887 + to_underlying (dwo_unit->sect_off));
6888 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6889
6890 if (per_cu->is_debug_types)
6891 {
6892 signatured_type *sig_type = (struct signatured_type *) per_cu;
6893
6894 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6895 section, dwo_abbrev_section,
6896 info_ptr, rcuh_kind::TYPE);
6897 /* This is not an assert because it can be caused by bad debug info. */
6898 if (sig_type->signature != cu->header.signature)
6899 {
6900 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6901 " TU at offset %s [in module %s]"),
6902 hex_string (sig_type->signature),
6903 hex_string (cu->header.signature),
6904 sect_offset_str (dwo_unit->sect_off),
6905 bfd_get_filename (abfd));
6906 }
6907 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6908 /* For DWOs coming from DWP files, we don't know the CU length
6909 nor the type's offset in the TU until now. */
6910 dwo_unit->length = cu->header.get_length ();
6911 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6912
6913 /* Establish the type offset that can be used to lookup the type.
6914 For DWO files, we don't know it until now. */
6915 sig_type->type_offset_in_section
6916 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6917 }
6918 else
6919 {
6920 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6921 section, dwo_abbrev_section,
6922 info_ptr, rcuh_kind::COMPILE);
6923 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6924 /* For DWOs coming from DWP files, we don't know the CU length
6925 until now. */
6926 dwo_unit->length = cu->header.get_length ();
6927 }
6928
6929 *result_dwo_abbrev_table
6930 = abbrev_table::read (objfile, dwo_abbrev_section,
6931 cu->header.abbrev_sect_off);
6932 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6933 result_dwo_abbrev_table->get ());
6934
6935 /* Read in the die, but leave space to copy over the attributes
6936 from the stub. This has the benefit of simplifying the rest of
6937 the code - all the work to maintain the illusion of a single
6938 DW_TAG_{compile,type}_unit DIE is done here. */
6939 num_extra_attrs = ((stmt_list != NULL)
6940 + (low_pc != NULL)
6941 + (high_pc != NULL)
6942 + (ranges != NULL)
6943 + (comp_dir != NULL));
6944 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6945 num_extra_attrs);
6946
6947 /* Copy over the attributes from the stub to the DIE we just read in. */
6948 comp_unit_die = *result_comp_unit_die;
6949 i = comp_unit_die->num_attrs;
6950 if (stmt_list != NULL)
6951 comp_unit_die->attrs[i++] = *stmt_list;
6952 if (low_pc != NULL)
6953 comp_unit_die->attrs[i++] = *low_pc;
6954 if (high_pc != NULL)
6955 comp_unit_die->attrs[i++] = *high_pc;
6956 if (ranges != NULL)
6957 comp_unit_die->attrs[i++] = *ranges;
6958 if (comp_dir != NULL)
6959 comp_unit_die->attrs[i++] = *comp_dir;
6960 comp_unit_die->num_attrs += num_extra_attrs;
6961
6962 if (dwarf_die_debug)
6963 {
6964 fprintf_unfiltered (gdb_stdlog,
6965 "Read die from %s@0x%x of %s:\n",
6966 section->get_name (),
6967 (unsigned) (begin_info_ptr - section->buffer),
6968 bfd_get_filename (abfd));
6969 dump_die (comp_unit_die, dwarf_die_debug);
6970 }
6971
6972 /* Skip dummy compilation units. */
6973 if (info_ptr >= begin_info_ptr + dwo_unit->length
6974 || peek_abbrev_code (abfd, info_ptr) == 0)
6975 return 0;
6976
6977 *result_info_ptr = info_ptr;
6978 return 1;
6979 }
6980
6981 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6982 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6983 signature is part of the header. */
6984 static gdb::optional<ULONGEST>
6985 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6986 {
6987 if (cu->header.version >= 5)
6988 return cu->header.signature;
6989 struct attribute *attr;
6990 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6991 if (attr == nullptr)
6992 return gdb::optional<ULONGEST> ();
6993 return DW_UNSND (attr);
6994 }
6995
6996 /* Subroutine of cutu_reader to simplify it.
6997 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6998 Returns NULL if the specified DWO unit cannot be found. */
6999
7000 static struct dwo_unit *
7001 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
7002 {
7003 dwarf2_per_cu_data *per_cu = cu->per_cu;
7004 struct dwo_unit *dwo_unit;
7005 const char *comp_dir;
7006
7007 gdb_assert (cu != NULL);
7008
7009 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7010 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7011 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7012
7013 if (per_cu->is_debug_types)
7014 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
7015 else
7016 {
7017 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7018
7019 if (!signature.has_value ())
7020 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7021 " [in module %s]"),
7022 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
7023
7024 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
7025 }
7026
7027 return dwo_unit;
7028 }
7029
7030 /* Subroutine of cutu_reader to simplify it.
7031 See it for a description of the parameters.
7032 Read a TU directly from a DWO file, bypassing the stub. */
7033
7034 void
7035 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
7036 dwarf2_per_objfile *per_objfile,
7037 dwarf2_cu *existing_cu)
7038 {
7039 struct signatured_type *sig_type;
7040
7041 /* Verify we can do the following downcast, and that we have the
7042 data we need. */
7043 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7044 sig_type = (struct signatured_type *) this_cu;
7045 gdb_assert (sig_type->dwo_unit != NULL);
7046
7047 dwarf2_cu *cu;
7048
7049 if (existing_cu != nullptr)
7050 {
7051 cu = existing_cu;
7052 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
7053 /* There's no need to do the rereading_dwo_cu handling that
7054 cutu_reader does since we don't read the stub. */
7055 }
7056 else
7057 {
7058 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7059 in per_objfile yet. */
7060 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7061 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7062 cu = m_new_cu.get ();
7063 }
7064
7065 /* A future optimization, if needed, would be to use an existing
7066 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7067 could share abbrev tables. */
7068
7069 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
7070 NULL /* stub_comp_unit_die */,
7071 sig_type->dwo_unit->dwo_file->comp_dir,
7072 this, &info_ptr,
7073 &comp_unit_die,
7074 &m_dwo_abbrev_table) == 0)
7075 {
7076 /* Dummy die. */
7077 dummy_p = true;
7078 }
7079 }
7080
7081 /* Initialize a CU (or TU) and read its DIEs.
7082 If the CU defers to a DWO file, read the DWO file as well.
7083
7084 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7085 Otherwise the table specified in the comp unit header is read in and used.
7086 This is an optimization for when we already have the abbrev table.
7087
7088 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7089 allocated. */
7090
7091 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7092 dwarf2_per_objfile *per_objfile,
7093 struct abbrev_table *abbrev_table,
7094 dwarf2_cu *existing_cu,
7095 bool skip_partial)
7096 : die_reader_specs {},
7097 m_this_cu (this_cu)
7098 {
7099 struct objfile *objfile = per_objfile->objfile;
7100 struct dwarf2_section_info *section = this_cu->section;
7101 bfd *abfd = section->get_bfd_owner ();
7102 const gdb_byte *begin_info_ptr;
7103 struct signatured_type *sig_type = NULL;
7104 struct dwarf2_section_info *abbrev_section;
7105 /* Non-zero if CU currently points to a DWO file and we need to
7106 reread it. When this happens we need to reread the skeleton die
7107 before we can reread the DWO file (this only applies to CUs, not TUs). */
7108 int rereading_dwo_cu = 0;
7109
7110 if (dwarf_die_debug)
7111 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7112 this_cu->is_debug_types ? "type" : "comp",
7113 sect_offset_str (this_cu->sect_off));
7114
7115 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7116 file (instead of going through the stub), short-circuit all of this. */
7117 if (this_cu->reading_dwo_directly)
7118 {
7119 /* Narrow down the scope of possibilities to have to understand. */
7120 gdb_assert (this_cu->is_debug_types);
7121 gdb_assert (abbrev_table == NULL);
7122 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
7123 return;
7124 }
7125
7126 /* This is cheap if the section is already read in. */
7127 section->read (objfile);
7128
7129 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7130
7131 abbrev_section = get_abbrev_section_for_cu (this_cu);
7132
7133 dwarf2_cu *cu;
7134
7135 if (existing_cu != nullptr)
7136 {
7137 cu = existing_cu;
7138 /* If this CU is from a DWO file we need to start over, we need to
7139 refetch the attributes from the skeleton CU.
7140 This could be optimized by retrieving those attributes from when we
7141 were here the first time: the previous comp_unit_die was stored in
7142 comp_unit_obstack. But there's no data yet that we need this
7143 optimization. */
7144 if (cu->dwo_unit != NULL)
7145 rereading_dwo_cu = 1;
7146 }
7147 else
7148 {
7149 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7150 in per_objfile yet. */
7151 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7152 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7153 cu = m_new_cu.get ();
7154 }
7155
7156 /* Get the header. */
7157 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7158 {
7159 /* We already have the header, there's no need to read it in again. */
7160 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7161 }
7162 else
7163 {
7164 if (this_cu->is_debug_types)
7165 {
7166 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7167 section, abbrev_section,
7168 info_ptr, rcuh_kind::TYPE);
7169
7170 /* Since per_cu is the first member of struct signatured_type,
7171 we can go from a pointer to one to a pointer to the other. */
7172 sig_type = (struct signatured_type *) this_cu;
7173 gdb_assert (sig_type->signature == cu->header.signature);
7174 gdb_assert (sig_type->type_offset_in_tu
7175 == cu->header.type_cu_offset_in_tu);
7176 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7177
7178 /* LENGTH has not been set yet for type units if we're
7179 using .gdb_index. */
7180 this_cu->length = cu->header.get_length ();
7181
7182 /* Establish the type offset that can be used to lookup the type. */
7183 sig_type->type_offset_in_section =
7184 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7185
7186 this_cu->dwarf_version = cu->header.version;
7187 }
7188 else
7189 {
7190 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7191 section, abbrev_section,
7192 info_ptr,
7193 rcuh_kind::COMPILE);
7194
7195 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7196 if (this_cu->length == 0)
7197 this_cu->length = cu->header.get_length ();
7198 else
7199 gdb_assert (this_cu->length == cu->header.get_length ());
7200 this_cu->dwarf_version = cu->header.version;
7201 }
7202 }
7203
7204 /* Skip dummy compilation units. */
7205 if (info_ptr >= begin_info_ptr + this_cu->length
7206 || peek_abbrev_code (abfd, info_ptr) == 0)
7207 {
7208 dummy_p = true;
7209 return;
7210 }
7211
7212 /* If we don't have them yet, read the abbrevs for this compilation unit.
7213 And if we need to read them now, make sure they're freed when we're
7214 done. */
7215 if (abbrev_table != NULL)
7216 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7217 else
7218 {
7219 m_abbrev_table_holder
7220 = abbrev_table::read (objfile, abbrev_section,
7221 cu->header.abbrev_sect_off);
7222 abbrev_table = m_abbrev_table_holder.get ();
7223 }
7224
7225 /* Read the top level CU/TU die. */
7226 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7227 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7228
7229 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7230 {
7231 dummy_p = true;
7232 return;
7233 }
7234
7235 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7236 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7237 table from the DWO file and pass the ownership over to us. It will be
7238 referenced from READER, so we must make sure to free it after we're done
7239 with READER.
7240
7241 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7242 DWO CU, that this test will fail (the attribute will not be present). */
7243 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7244 if (dwo_name != nullptr)
7245 {
7246 struct dwo_unit *dwo_unit;
7247 struct die_info *dwo_comp_unit_die;
7248
7249 if (comp_unit_die->has_children)
7250 {
7251 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7252 " has children (offset %s) [in module %s]"),
7253 sect_offset_str (this_cu->sect_off),
7254 bfd_get_filename (abfd));
7255 }
7256 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
7257 if (dwo_unit != NULL)
7258 {
7259 if (read_cutu_die_from_dwo (cu, dwo_unit,
7260 comp_unit_die, NULL,
7261 this, &info_ptr,
7262 &dwo_comp_unit_die,
7263 &m_dwo_abbrev_table) == 0)
7264 {
7265 /* Dummy die. */
7266 dummy_p = true;
7267 return;
7268 }
7269 comp_unit_die = dwo_comp_unit_die;
7270 }
7271 else
7272 {
7273 /* Yikes, we couldn't find the rest of the DIE, we only have
7274 the stub. A complaint has already been logged. There's
7275 not much more we can do except pass on the stub DIE to
7276 die_reader_func. We don't want to throw an error on bad
7277 debug info. */
7278 }
7279 }
7280 }
7281
7282 void
7283 cutu_reader::keep ()
7284 {
7285 /* Done, clean up. */
7286 gdb_assert (!dummy_p);
7287 if (m_new_cu != NULL)
7288 {
7289 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7290 now. */
7291 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7292 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
7293 }
7294 }
7295
7296 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7297 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7298 assumed to have already done the lookup to find the DWO file).
7299
7300 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7301 THIS_CU->is_debug_types, but nothing else.
7302
7303 We fill in THIS_CU->length.
7304
7305 THIS_CU->cu is always freed when done.
7306 This is done in order to not leave THIS_CU->cu in a state where we have
7307 to care whether it refers to the "main" CU or the DWO CU.
7308
7309 When parent_cu is passed, it is used to provide a default value for
7310 str_offsets_base and addr_base from the parent. */
7311
7312 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7313 dwarf2_per_objfile *per_objfile,
7314 struct dwarf2_cu *parent_cu,
7315 struct dwo_file *dwo_file)
7316 : die_reader_specs {},
7317 m_this_cu (this_cu)
7318 {
7319 struct objfile *objfile = per_objfile->objfile;
7320 struct dwarf2_section_info *section = this_cu->section;
7321 bfd *abfd = section->get_bfd_owner ();
7322 struct dwarf2_section_info *abbrev_section;
7323 const gdb_byte *begin_info_ptr, *info_ptr;
7324
7325 if (dwarf_die_debug)
7326 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7327 this_cu->is_debug_types ? "type" : "comp",
7328 sect_offset_str (this_cu->sect_off));
7329
7330 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7331
7332 abbrev_section = (dwo_file != NULL
7333 ? &dwo_file->sections.abbrev
7334 : get_abbrev_section_for_cu (this_cu));
7335
7336 /* This is cheap if the section is already read in. */
7337 section->read (objfile);
7338
7339 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7340
7341 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7342 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7343 section, abbrev_section, info_ptr,
7344 (this_cu->is_debug_types
7345 ? rcuh_kind::TYPE
7346 : rcuh_kind::COMPILE));
7347
7348 if (parent_cu != nullptr)
7349 {
7350 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7351 m_new_cu->addr_base = parent_cu->addr_base;
7352 }
7353 this_cu->length = m_new_cu->header.get_length ();
7354
7355 /* Skip dummy compilation units. */
7356 if (info_ptr >= begin_info_ptr + this_cu->length
7357 || peek_abbrev_code (abfd, info_ptr) == 0)
7358 {
7359 dummy_p = true;
7360 return;
7361 }
7362
7363 m_abbrev_table_holder
7364 = abbrev_table::read (objfile, abbrev_section,
7365 m_new_cu->header.abbrev_sect_off);
7366
7367 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7368 m_abbrev_table_holder.get ());
7369 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7370 }
7371
7372 \f
7373 /* Type Unit Groups.
7374
7375 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7376 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7377 so that all types coming from the same compilation (.o file) are grouped
7378 together. A future step could be to put the types in the same symtab as
7379 the CU the types ultimately came from. */
7380
7381 static hashval_t
7382 hash_type_unit_group (const void *item)
7383 {
7384 const struct type_unit_group *tu_group
7385 = (const struct type_unit_group *) item;
7386
7387 return hash_stmt_list_entry (&tu_group->hash);
7388 }
7389
7390 static int
7391 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7392 {
7393 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7394 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7395
7396 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7397 }
7398
7399 /* Allocate a hash table for type unit groups. */
7400
7401 static htab_up
7402 allocate_type_unit_groups_table ()
7403 {
7404 return htab_up (htab_create_alloc (3,
7405 hash_type_unit_group,
7406 eq_type_unit_group,
7407 NULL, xcalloc, xfree));
7408 }
7409
7410 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7411 partial symtabs. We combine several TUs per psymtab to not let the size
7412 of any one psymtab grow too big. */
7413 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7414 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7415
7416 /* Helper routine for get_type_unit_group.
7417 Create the type_unit_group object used to hold one or more TUs. */
7418
7419 static struct type_unit_group *
7420 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7421 {
7422 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7423 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7424 struct dwarf2_per_cu_data *per_cu;
7425 struct type_unit_group *tu_group;
7426
7427 tu_group = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, type_unit_group);
7428 per_cu = &tu_group->per_cu;
7429 per_cu->per_bfd = per_bfd;
7430
7431 if (per_bfd->using_index)
7432 {
7433 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7434 struct dwarf2_per_cu_quick_data);
7435 }
7436 else
7437 {
7438 unsigned int line_offset = to_underlying (line_offset_struct);
7439 dwarf2_psymtab *pst;
7440 std::string name;
7441
7442 /* Give the symtab a useful name for debug purposes. */
7443 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7444 name = string_printf ("<type_units_%d>",
7445 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7446 else
7447 name = string_printf ("<type_units_at_0x%x>", line_offset);
7448
7449 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
7450 pst->anonymous = true;
7451 }
7452
7453 tu_group->hash.dwo_unit = cu->dwo_unit;
7454 tu_group->hash.line_sect_off = line_offset_struct;
7455
7456 return tu_group;
7457 }
7458
7459 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7460 STMT_LIST is a DW_AT_stmt_list attribute. */
7461
7462 static struct type_unit_group *
7463 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7464 {
7465 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7466 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7467 struct type_unit_group *tu_group;
7468 void **slot;
7469 unsigned int line_offset;
7470 struct type_unit_group type_unit_group_for_lookup;
7471
7472 if (per_objfile->per_bfd->type_unit_groups == NULL)
7473 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7474
7475 /* Do we need to create a new group, or can we use an existing one? */
7476
7477 if (stmt_list)
7478 {
7479 line_offset = DW_UNSND (stmt_list);
7480 ++tu_stats->nr_symtab_sharers;
7481 }
7482 else
7483 {
7484 /* Ugh, no stmt_list. Rare, but we have to handle it.
7485 We can do various things here like create one group per TU or
7486 spread them over multiple groups to split up the expansion work.
7487 To avoid worst case scenarios (too many groups or too large groups)
7488 we, umm, group them in bunches. */
7489 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7490 | (tu_stats->nr_stmt_less_type_units
7491 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7492 ++tu_stats->nr_stmt_less_type_units;
7493 }
7494
7495 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7496 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7497 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
7498 &type_unit_group_for_lookup, INSERT);
7499 if (*slot != NULL)
7500 {
7501 tu_group = (struct type_unit_group *) *slot;
7502 gdb_assert (tu_group != NULL);
7503 }
7504 else
7505 {
7506 sect_offset line_offset_struct = (sect_offset) line_offset;
7507 tu_group = create_type_unit_group (cu, line_offset_struct);
7508 *slot = tu_group;
7509 ++tu_stats->nr_symtabs;
7510 }
7511
7512 return tu_group;
7513 }
7514 \f
7515 /* Partial symbol tables. */
7516
7517 /* Create a psymtab named NAME and assign it to PER_CU.
7518
7519 The caller must fill in the following details:
7520 dirname, textlow, texthigh. */
7521
7522 static dwarf2_psymtab *
7523 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7524 dwarf2_per_objfile *per_objfile,
7525 const char *name)
7526 {
7527 struct objfile *objfile = per_objfile->objfile;
7528 dwarf2_psymtab *pst;
7529
7530 pst = new dwarf2_psymtab (name, objfile, per_cu);
7531
7532 pst->psymtabs_addrmap_supported = true;
7533
7534 /* This is the glue that links PST into GDB's symbol API. */
7535 per_cu->v.psymtab = pst;
7536
7537 return pst;
7538 }
7539
7540 /* DIE reader function for process_psymtab_comp_unit. */
7541
7542 static void
7543 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7544 const gdb_byte *info_ptr,
7545 struct die_info *comp_unit_die,
7546 enum language pretend_language)
7547 {
7548 struct dwarf2_cu *cu = reader->cu;
7549 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7550 struct objfile *objfile = per_objfile->objfile;
7551 struct gdbarch *gdbarch = objfile->arch ();
7552 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7553 CORE_ADDR baseaddr;
7554 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7555 dwarf2_psymtab *pst;
7556 enum pc_bounds_kind cu_bounds_kind;
7557 const char *filename;
7558
7559 gdb_assert (! per_cu->is_debug_types);
7560
7561 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7562
7563 /* Allocate a new partial symbol table structure. */
7564 gdb::unique_xmalloc_ptr<char> debug_filename;
7565 static const char artificial[] = "<artificial>";
7566 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7567 if (filename == NULL)
7568 filename = "";
7569 else if (strcmp (filename, artificial) == 0)
7570 {
7571 debug_filename.reset (concat (artificial, "@",
7572 sect_offset_str (per_cu->sect_off),
7573 (char *) NULL));
7574 filename = debug_filename.get ();
7575 }
7576
7577 pst = create_partial_symtab (per_cu, per_objfile, filename);
7578
7579 /* This must be done before calling dwarf2_build_include_psymtabs. */
7580 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7581
7582 baseaddr = objfile->text_section_offset ();
7583
7584 dwarf2_find_base_address (comp_unit_die, cu);
7585
7586 /* Possibly set the default values of LOWPC and HIGHPC from
7587 `DW_AT_ranges'. */
7588 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7589 &best_highpc, cu, pst);
7590 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7591 {
7592 CORE_ADDR low
7593 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7594 - baseaddr);
7595 CORE_ADDR high
7596 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7597 - baseaddr - 1);
7598 /* Store the contiguous range if it is not empty; it can be
7599 empty for CUs with no code. */
7600 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7601 low, high, pst);
7602 }
7603
7604 /* Check if comp unit has_children.
7605 If so, read the rest of the partial symbols from this comp unit.
7606 If not, there's no more debug_info for this comp unit. */
7607 if (comp_unit_die->has_children)
7608 {
7609 struct partial_die_info *first_die;
7610 CORE_ADDR lowpc, highpc;
7611
7612 lowpc = ((CORE_ADDR) -1);
7613 highpc = ((CORE_ADDR) 0);
7614
7615 first_die = load_partial_dies (reader, info_ptr, 1);
7616
7617 scan_partial_symbols (first_die, &lowpc, &highpc,
7618 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7619
7620 /* If we didn't find a lowpc, set it to highpc to avoid
7621 complaints from `maint check'. */
7622 if (lowpc == ((CORE_ADDR) -1))
7623 lowpc = highpc;
7624
7625 /* If the compilation unit didn't have an explicit address range,
7626 then use the information extracted from its child dies. */
7627 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7628 {
7629 best_lowpc = lowpc;
7630 best_highpc = highpc;
7631 }
7632 }
7633 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7634 best_lowpc + baseaddr)
7635 - baseaddr);
7636 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7637 best_highpc + baseaddr)
7638 - baseaddr);
7639
7640 end_psymtab_common (objfile, pst);
7641
7642 if (!cu->per_cu->imported_symtabs_empty ())
7643 {
7644 int i;
7645 int len = cu->per_cu->imported_symtabs_size ();
7646
7647 /* Fill in 'dependencies' here; we fill in 'users' in a
7648 post-pass. */
7649 pst->number_of_dependencies = len;
7650 pst->dependencies
7651 = objfile->partial_symtabs->allocate_dependencies (len);
7652 for (i = 0; i < len; ++i)
7653 {
7654 pst->dependencies[i]
7655 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7656 }
7657
7658 cu->per_cu->imported_symtabs_free ();
7659 }
7660
7661 /* Get the list of files included in the current compilation unit,
7662 and build a psymtab for each of them. */
7663 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7664
7665 if (dwarf_read_debug)
7666 fprintf_unfiltered (gdb_stdlog,
7667 "Psymtab for %s unit @%s: %s - %s"
7668 ", %d global, %d static syms\n",
7669 per_cu->is_debug_types ? "type" : "comp",
7670 sect_offset_str (per_cu->sect_off),
7671 paddress (gdbarch, pst->text_low (objfile)),
7672 paddress (gdbarch, pst->text_high (objfile)),
7673 pst->n_global_syms, pst->n_static_syms);
7674 }
7675
7676 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7677 Process compilation unit THIS_CU for a psymtab. */
7678
7679 static void
7680 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7681 dwarf2_per_objfile *per_objfile,
7682 bool want_partial_unit,
7683 enum language pretend_language)
7684 {
7685 /* If this compilation unit was already read in, free the
7686 cached copy in order to read it in again. This is
7687 necessary because we skipped some symbols when we first
7688 read in the compilation unit (see load_partial_dies).
7689 This problem could be avoided, but the benefit is unclear. */
7690 per_objfile->remove_cu (this_cu);
7691
7692 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7693
7694 switch (reader.comp_unit_die->tag)
7695 {
7696 case DW_TAG_compile_unit:
7697 this_cu->unit_type = DW_UT_compile;
7698 break;
7699 case DW_TAG_partial_unit:
7700 this_cu->unit_type = DW_UT_partial;
7701 break;
7702 default:
7703 abort ();
7704 }
7705
7706 if (reader.dummy_p)
7707 {
7708 /* Nothing. */
7709 }
7710 else if (this_cu->is_debug_types)
7711 build_type_psymtabs_reader (&reader, reader.info_ptr,
7712 reader.comp_unit_die);
7713 else if (want_partial_unit
7714 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7715 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7716 reader.comp_unit_die,
7717 pretend_language);
7718
7719 this_cu->lang = reader.cu->language;
7720
7721 /* Age out any secondary CUs. */
7722 per_objfile->age_comp_units ();
7723 }
7724
7725 /* Reader function for build_type_psymtabs. */
7726
7727 static void
7728 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7729 const gdb_byte *info_ptr,
7730 struct die_info *type_unit_die)
7731 {
7732 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7733 struct objfile *objfile = per_objfile->objfile;
7734 struct dwarf2_cu *cu = reader->cu;
7735 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7736 struct signatured_type *sig_type;
7737 struct type_unit_group *tu_group;
7738 struct attribute *attr;
7739 struct partial_die_info *first_die;
7740 CORE_ADDR lowpc, highpc;
7741 dwarf2_psymtab *pst;
7742
7743 gdb_assert (per_cu->is_debug_types);
7744 sig_type = (struct signatured_type *) per_cu;
7745
7746 if (! type_unit_die->has_children)
7747 return;
7748
7749 attr = type_unit_die->attr (DW_AT_stmt_list);
7750 tu_group = get_type_unit_group (cu, attr);
7751
7752 if (tu_group->tus == nullptr)
7753 tu_group->tus = new std::vector<signatured_type *>;
7754 tu_group->tus->push_back (sig_type);
7755
7756 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7757 pst = create_partial_symtab (per_cu, per_objfile, "");
7758 pst->anonymous = true;
7759
7760 first_die = load_partial_dies (reader, info_ptr, 1);
7761
7762 lowpc = (CORE_ADDR) -1;
7763 highpc = (CORE_ADDR) 0;
7764 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7765
7766 end_psymtab_common (objfile, pst);
7767 }
7768
7769 /* Struct used to sort TUs by their abbreviation table offset. */
7770
7771 struct tu_abbrev_offset
7772 {
7773 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7774 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7775 {}
7776
7777 signatured_type *sig_type;
7778 sect_offset abbrev_offset;
7779 };
7780
7781 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7782
7783 static bool
7784 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7785 const struct tu_abbrev_offset &b)
7786 {
7787 return a.abbrev_offset < b.abbrev_offset;
7788 }
7789
7790 /* Efficiently read all the type units.
7791 This does the bulk of the work for build_type_psymtabs.
7792
7793 The efficiency is because we sort TUs by the abbrev table they use and
7794 only read each abbrev table once. In one program there are 200K TUs
7795 sharing 8K abbrev tables.
7796
7797 The main purpose of this function is to support building the
7798 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7799 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7800 can collapse the search space by grouping them by stmt_list.
7801 The savings can be significant, in the same program from above the 200K TUs
7802 share 8K stmt_list tables.
7803
7804 FUNC is expected to call get_type_unit_group, which will create the
7805 struct type_unit_group if necessary and add it to
7806 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7807
7808 static void
7809 build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
7810 {
7811 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7812 abbrev_table_up abbrev_table;
7813 sect_offset abbrev_offset;
7814
7815 /* It's up to the caller to not call us multiple times. */
7816 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7817
7818 if (per_objfile->per_bfd->all_type_units.empty ())
7819 return;
7820
7821 /* TUs typically share abbrev tables, and there can be way more TUs than
7822 abbrev tables. Sort by abbrev table to reduce the number of times we
7823 read each abbrev table in.
7824 Alternatives are to punt or to maintain a cache of abbrev tables.
7825 This is simpler and efficient enough for now.
7826
7827 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7828 symtab to use). Typically TUs with the same abbrev offset have the same
7829 stmt_list value too so in practice this should work well.
7830
7831 The basic algorithm here is:
7832
7833 sort TUs by abbrev table
7834 for each TU with same abbrev table:
7835 read abbrev table if first user
7836 read TU top level DIE
7837 [IWBN if DWO skeletons had DW_AT_stmt_list]
7838 call FUNC */
7839
7840 if (dwarf_read_debug)
7841 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7842
7843 /* Sort in a separate table to maintain the order of all_type_units
7844 for .gdb_index: TU indices directly index all_type_units. */
7845 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7846 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
7847
7848 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
7849 sorted_by_abbrev.emplace_back
7850 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
7851 sig_type->per_cu.sect_off));
7852
7853 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7854 sort_tu_by_abbrev_offset);
7855
7856 abbrev_offset = (sect_offset) ~(unsigned) 0;
7857
7858 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7859 {
7860 /* Switch to the next abbrev table if necessary. */
7861 if (abbrev_table == NULL
7862 || tu.abbrev_offset != abbrev_offset)
7863 {
7864 abbrev_offset = tu.abbrev_offset;
7865 abbrev_table =
7866 abbrev_table::read (per_objfile->objfile,
7867 &per_objfile->per_bfd->abbrev, abbrev_offset);
7868 ++tu_stats->nr_uniq_abbrev_tables;
7869 }
7870
7871 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
7872 abbrev_table.get (), nullptr, false);
7873 if (!reader.dummy_p)
7874 build_type_psymtabs_reader (&reader, reader.info_ptr,
7875 reader.comp_unit_die);
7876 }
7877 }
7878
7879 /* Print collected type unit statistics. */
7880
7881 static void
7882 print_tu_stats (dwarf2_per_objfile *per_objfile)
7883 {
7884 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7885
7886 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7887 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7888 per_objfile->per_bfd->all_type_units.size ());
7889 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7890 tu_stats->nr_uniq_abbrev_tables);
7891 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7892 tu_stats->nr_symtabs);
7893 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7894 tu_stats->nr_symtab_sharers);
7895 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7896 tu_stats->nr_stmt_less_type_units);
7897 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7898 tu_stats->nr_all_type_units_reallocs);
7899 }
7900
7901 /* Traversal function for build_type_psymtabs. */
7902
7903 static int
7904 build_type_psymtab_dependencies (void **slot, void *info)
7905 {
7906 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7907 struct objfile *objfile = per_objfile->objfile;
7908 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7909 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7910 dwarf2_psymtab *pst = per_cu->v.psymtab;
7911 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7912 int i;
7913
7914 gdb_assert (len > 0);
7915 gdb_assert (per_cu->type_unit_group_p ());
7916
7917 pst->number_of_dependencies = len;
7918 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7919 for (i = 0; i < len; ++i)
7920 {
7921 struct signatured_type *iter = tu_group->tus->at (i);
7922 gdb_assert (iter->per_cu.is_debug_types);
7923 pst->dependencies[i] = iter->per_cu.v.psymtab;
7924 iter->type_unit_group = tu_group;
7925 }
7926
7927 delete tu_group->tus;
7928 tu_group->tus = nullptr;
7929
7930 return 1;
7931 }
7932
7933 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7934 Build partial symbol tables for the .debug_types comp-units. */
7935
7936 static void
7937 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
7938 {
7939 if (! create_all_type_units (per_objfile))
7940 return;
7941
7942 build_type_psymtabs_1 (per_objfile);
7943 }
7944
7945 /* Traversal function for process_skeletonless_type_unit.
7946 Read a TU in a DWO file and build partial symbols for it. */
7947
7948 static int
7949 process_skeletonless_type_unit (void **slot, void *info)
7950 {
7951 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7952 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7953 struct signatured_type find_entry, *entry;
7954
7955 /* If this TU doesn't exist in the global table, add it and read it in. */
7956
7957 if (per_objfile->per_bfd->signatured_types == NULL)
7958 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7959
7960 find_entry.signature = dwo_unit->signature;
7961 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
7962 &find_entry, INSERT);
7963 /* If we've already seen this type there's nothing to do. What's happening
7964 is we're doing our own version of comdat-folding here. */
7965 if (*slot != NULL)
7966 return 1;
7967
7968 /* This does the job that create_all_type_units would have done for
7969 this TU. */
7970 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
7971 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
7972 *slot = entry;
7973
7974 /* This does the job that build_type_psymtabs_1 would have done. */
7975 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
7976 if (!reader.dummy_p)
7977 build_type_psymtabs_reader (&reader, reader.info_ptr,
7978 reader.comp_unit_die);
7979
7980 return 1;
7981 }
7982
7983 /* Traversal function for process_skeletonless_type_units. */
7984
7985 static int
7986 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7987 {
7988 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7989
7990 if (dwo_file->tus != NULL)
7991 htab_traverse_noresize (dwo_file->tus.get (),
7992 process_skeletonless_type_unit, info);
7993
7994 return 1;
7995 }
7996
7997 /* Scan all TUs of DWO files, verifying we've processed them.
7998 This is needed in case a TU was emitted without its skeleton.
7999 Note: This can't be done until we know what all the DWO files are. */
8000
8001 static void
8002 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
8003 {
8004 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8005 if (get_dwp_file (per_objfile) == NULL
8006 && per_objfile->per_bfd->dwo_files != NULL)
8007 {
8008 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
8009 process_dwo_file_for_skeletonless_type_units,
8010 per_objfile);
8011 }
8012 }
8013
8014 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8015
8016 static void
8017 set_partial_user (dwarf2_per_objfile *per_objfile)
8018 {
8019 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
8020 {
8021 dwarf2_psymtab *pst = per_cu->v.psymtab;
8022
8023 if (pst == NULL)
8024 continue;
8025
8026 for (int j = 0; j < pst->number_of_dependencies; ++j)
8027 {
8028 /* Set the 'user' field only if it is not already set. */
8029 if (pst->dependencies[j]->user == NULL)
8030 pst->dependencies[j]->user = pst;
8031 }
8032 }
8033 }
8034
8035 /* Build the partial symbol table by doing a quick pass through the
8036 .debug_info and .debug_abbrev sections. */
8037
8038 static void
8039 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
8040 {
8041 struct objfile *objfile = per_objfile->objfile;
8042
8043 if (dwarf_read_debug)
8044 {
8045 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8046 objfile_name (objfile));
8047 }
8048
8049 scoped_restore restore_reading_psyms
8050 = make_scoped_restore (&per_objfile->per_bfd->reading_partial_symbols,
8051 true);
8052
8053 per_objfile->per_bfd->info.read (objfile);
8054
8055 /* Any cached compilation units will be linked by the per-objfile
8056 read_in_chain. Make sure to free them when we're done. */
8057 free_cached_comp_units freer (per_objfile);
8058
8059 build_type_psymtabs (per_objfile);
8060
8061 create_all_comp_units (per_objfile);
8062
8063 /* Create a temporary address map on a temporary obstack. We later
8064 copy this to the final obstack. */
8065 auto_obstack temp_obstack;
8066
8067 scoped_restore save_psymtabs_addrmap
8068 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8069 addrmap_create_mutable (&temp_obstack));
8070
8071 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
8072 {
8073 if (per_cu->v.psymtab != NULL)
8074 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8075 continue;
8076 process_psymtab_comp_unit (per_cu, per_objfile, false,
8077 language_minimal);
8078 }
8079
8080 /* This has to wait until we read the CUs, we need the list of DWOs. */
8081 process_skeletonless_type_units (per_objfile);
8082
8083 /* Now that all TUs have been processed we can fill in the dependencies. */
8084 if (per_objfile->per_bfd->type_unit_groups != NULL)
8085 {
8086 htab_traverse_noresize (per_objfile->per_bfd->type_unit_groups.get (),
8087 build_type_psymtab_dependencies, per_objfile);
8088 }
8089
8090 if (dwarf_read_debug)
8091 print_tu_stats (per_objfile);
8092
8093 set_partial_user (per_objfile);
8094
8095 objfile->partial_symtabs->psymtabs_addrmap
8096 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8097 objfile->partial_symtabs->obstack ());
8098 /* At this point we want to keep the address map. */
8099 save_psymtabs_addrmap.release ();
8100
8101 if (dwarf_read_debug)
8102 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8103 objfile_name (objfile));
8104 }
8105
8106 /* Load the partial DIEs for a secondary CU into memory.
8107 This is also used when rereading a primary CU with load_all_dies. */
8108
8109 static void
8110 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
8111 dwarf2_per_objfile *per_objfile,
8112 dwarf2_cu *existing_cu)
8113 {
8114 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
8115
8116 if (!reader.dummy_p)
8117 {
8118 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8119 language_minimal);
8120
8121 /* Check if comp unit has_children.
8122 If so, read the rest of the partial symbols from this comp unit.
8123 If not, there's no more debug_info for this comp unit. */
8124 if (reader.comp_unit_die->has_children)
8125 load_partial_dies (&reader, reader.info_ptr, 0);
8126
8127 reader.keep ();
8128 }
8129 }
8130
8131 static void
8132 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
8133 struct dwarf2_section_info *section,
8134 struct dwarf2_section_info *abbrev_section,
8135 unsigned int is_dwz)
8136 {
8137 const gdb_byte *info_ptr;
8138 struct objfile *objfile = per_objfile->objfile;
8139
8140 if (dwarf_read_debug)
8141 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8142 section->get_name (),
8143 section->get_file_name ());
8144
8145 section->read (objfile);
8146
8147 info_ptr = section->buffer;
8148
8149 while (info_ptr < section->buffer + section->size)
8150 {
8151 struct dwarf2_per_cu_data *this_cu;
8152
8153 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8154
8155 comp_unit_head cu_header;
8156 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
8157 abbrev_section, info_ptr,
8158 rcuh_kind::COMPILE);
8159
8160 /* Save the compilation unit for later lookup. */
8161 if (cu_header.unit_type != DW_UT_type)
8162 this_cu = per_objfile->per_bfd->allocate_per_cu ();
8163 else
8164 {
8165 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
8166 sig_type->signature = cu_header.signature;
8167 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8168 this_cu = &sig_type->per_cu;
8169 }
8170 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8171 this_cu->sect_off = sect_off;
8172 this_cu->length = cu_header.length + cu_header.initial_length_size;
8173 this_cu->is_dwz = is_dwz;
8174 this_cu->section = section;
8175
8176 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8177
8178 info_ptr = info_ptr + this_cu->length;
8179 }
8180 }
8181
8182 /* Create a list of all compilation units in OBJFILE.
8183 This is only done for -readnow and building partial symtabs. */
8184
8185 static void
8186 create_all_comp_units (dwarf2_per_objfile *per_objfile)
8187 {
8188 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8189 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8190 &per_objfile->per_bfd->abbrev, 0);
8191
8192 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8193 if (dwz != NULL)
8194 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
8195 }
8196
8197 /* Process all loaded DIEs for compilation unit CU, starting at
8198 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8199 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8200 DW_AT_ranges). See the comments of add_partial_subprogram on how
8201 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8202
8203 static void
8204 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8205 CORE_ADDR *highpc, int set_addrmap,
8206 struct dwarf2_cu *cu)
8207 {
8208 struct partial_die_info *pdi;
8209
8210 /* Now, march along the PDI's, descending into ones which have
8211 interesting children but skipping the children of the other ones,
8212 until we reach the end of the compilation unit. */
8213
8214 pdi = first_die;
8215
8216 while (pdi != NULL)
8217 {
8218 pdi->fixup (cu);
8219
8220 /* Anonymous namespaces or modules have no name but have interesting
8221 children, so we need to look at them. Ditto for anonymous
8222 enums. */
8223
8224 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
8225 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8226 || pdi->tag == DW_TAG_imported_unit
8227 || pdi->tag == DW_TAG_inlined_subroutine)
8228 {
8229 switch (pdi->tag)
8230 {
8231 case DW_TAG_subprogram:
8232 case DW_TAG_inlined_subroutine:
8233 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8234 if (cu->language == language_cplus)
8235 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8236 set_addrmap, cu);
8237 break;
8238 case DW_TAG_constant:
8239 case DW_TAG_variable:
8240 case DW_TAG_typedef:
8241 case DW_TAG_union_type:
8242 if (!pdi->is_declaration
8243 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8244 {
8245 add_partial_symbol (pdi, cu);
8246 }
8247 break;
8248 case DW_TAG_class_type:
8249 case DW_TAG_interface_type:
8250 case DW_TAG_structure_type:
8251 if (!pdi->is_declaration)
8252 {
8253 add_partial_symbol (pdi, cu);
8254 }
8255 if ((cu->language == language_rust
8256 || cu->language == language_cplus) && pdi->has_children)
8257 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8258 set_addrmap, cu);
8259 break;
8260 case DW_TAG_enumeration_type:
8261 if (!pdi->is_declaration)
8262 add_partial_enumeration (pdi, cu);
8263 break;
8264 case DW_TAG_base_type:
8265 case DW_TAG_subrange_type:
8266 /* File scope base type definitions are added to the partial
8267 symbol table. */
8268 add_partial_symbol (pdi, cu);
8269 break;
8270 case DW_TAG_namespace:
8271 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8272 break;
8273 case DW_TAG_module:
8274 if (!pdi->is_declaration)
8275 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8276 break;
8277 case DW_TAG_imported_unit:
8278 {
8279 struct dwarf2_per_cu_data *per_cu;
8280
8281 /* For now we don't handle imported units in type units. */
8282 if (cu->per_cu->is_debug_types)
8283 {
8284 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8285 " supported in type units [in module %s]"),
8286 objfile_name (cu->per_objfile->objfile));
8287 }
8288
8289 per_cu = dwarf2_find_containing_comp_unit
8290 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8291
8292 /* Go read the partial unit, if needed. */
8293 if (per_cu->v.psymtab == NULL)
8294 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8295 cu->language);
8296
8297 cu->per_cu->imported_symtabs_push (per_cu);
8298 }
8299 break;
8300 case DW_TAG_imported_declaration:
8301 add_partial_symbol (pdi, cu);
8302 break;
8303 default:
8304 break;
8305 }
8306 }
8307
8308 /* If the die has a sibling, skip to the sibling. */
8309
8310 pdi = pdi->die_sibling;
8311 }
8312 }
8313
8314 /* Functions used to compute the fully scoped name of a partial DIE.
8315
8316 Normally, this is simple. For C++, the parent DIE's fully scoped
8317 name is concatenated with "::" and the partial DIE's name.
8318 Enumerators are an exception; they use the scope of their parent
8319 enumeration type, i.e. the name of the enumeration type is not
8320 prepended to the enumerator.
8321
8322 There are two complexities. One is DW_AT_specification; in this
8323 case "parent" means the parent of the target of the specification,
8324 instead of the direct parent of the DIE. The other is compilers
8325 which do not emit DW_TAG_namespace; in this case we try to guess
8326 the fully qualified name of structure types from their members'
8327 linkage names. This must be done using the DIE's children rather
8328 than the children of any DW_AT_specification target. We only need
8329 to do this for structures at the top level, i.e. if the target of
8330 any DW_AT_specification (if any; otherwise the DIE itself) does not
8331 have a parent. */
8332
8333 /* Compute the scope prefix associated with PDI's parent, in
8334 compilation unit CU. The result will be allocated on CU's
8335 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8336 field. NULL is returned if no prefix is necessary. */
8337 static const char *
8338 partial_die_parent_scope (struct partial_die_info *pdi,
8339 struct dwarf2_cu *cu)
8340 {
8341 const char *grandparent_scope;
8342 struct partial_die_info *parent, *real_pdi;
8343
8344 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8345 then this means the parent of the specification DIE. */
8346
8347 real_pdi = pdi;
8348 while (real_pdi->has_specification)
8349 {
8350 auto res = find_partial_die (real_pdi->spec_offset,
8351 real_pdi->spec_is_dwz, cu);
8352 real_pdi = res.pdi;
8353 cu = res.cu;
8354 }
8355
8356 parent = real_pdi->die_parent;
8357 if (parent == NULL)
8358 return NULL;
8359
8360 if (parent->scope_set)
8361 return parent->scope;
8362
8363 parent->fixup (cu);
8364
8365 grandparent_scope = partial_die_parent_scope (parent, cu);
8366
8367 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8368 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8369 Work around this problem here. */
8370 if (cu->language == language_cplus
8371 && parent->tag == DW_TAG_namespace
8372 && strcmp (parent->name (cu), "::") == 0
8373 && grandparent_scope == NULL)
8374 {
8375 parent->scope = NULL;
8376 parent->scope_set = 1;
8377 return NULL;
8378 }
8379
8380 /* Nested subroutines in Fortran get a prefix. */
8381 if (pdi->tag == DW_TAG_enumerator)
8382 /* Enumerators should not get the name of the enumeration as a prefix. */
8383 parent->scope = grandparent_scope;
8384 else if (parent->tag == DW_TAG_namespace
8385 || parent->tag == DW_TAG_module
8386 || parent->tag == DW_TAG_structure_type
8387 || parent->tag == DW_TAG_class_type
8388 || parent->tag == DW_TAG_interface_type
8389 || parent->tag == DW_TAG_union_type
8390 || parent->tag == DW_TAG_enumeration_type
8391 || (cu->language == language_fortran
8392 && parent->tag == DW_TAG_subprogram
8393 && pdi->tag == DW_TAG_subprogram))
8394 {
8395 if (grandparent_scope == NULL)
8396 parent->scope = parent->name (cu);
8397 else
8398 parent->scope = typename_concat (&cu->comp_unit_obstack,
8399 grandparent_scope,
8400 parent->name (cu), 0, cu);
8401 }
8402 else
8403 {
8404 /* FIXME drow/2004-04-01: What should we be doing with
8405 function-local names? For partial symbols, we should probably be
8406 ignoring them. */
8407 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8408 dwarf_tag_name (parent->tag),
8409 sect_offset_str (pdi->sect_off));
8410 parent->scope = grandparent_scope;
8411 }
8412
8413 parent->scope_set = 1;
8414 return parent->scope;
8415 }
8416
8417 /* Return the fully scoped name associated with PDI, from compilation unit
8418 CU. The result will be allocated with malloc. */
8419
8420 static gdb::unique_xmalloc_ptr<char>
8421 partial_die_full_name (struct partial_die_info *pdi,
8422 struct dwarf2_cu *cu)
8423 {
8424 const char *parent_scope;
8425
8426 /* If this is a template instantiation, we can not work out the
8427 template arguments from partial DIEs. So, unfortunately, we have
8428 to go through the full DIEs. At least any work we do building
8429 types here will be reused if full symbols are loaded later. */
8430 if (pdi->has_template_arguments)
8431 {
8432 pdi->fixup (cu);
8433
8434 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
8435 {
8436 struct die_info *die;
8437 struct attribute attr;
8438 struct dwarf2_cu *ref_cu = cu;
8439
8440 /* DW_FORM_ref_addr is using section offset. */
8441 attr.name = (enum dwarf_attribute) 0;
8442 attr.form = DW_FORM_ref_addr;
8443 attr.u.unsnd = to_underlying (pdi->sect_off);
8444 die = follow_die_ref (NULL, &attr, &ref_cu);
8445
8446 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8447 }
8448 }
8449
8450 parent_scope = partial_die_parent_scope (pdi, cu);
8451 if (parent_scope == NULL)
8452 return NULL;
8453 else
8454 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8455 pdi->name (cu),
8456 0, cu));
8457 }
8458
8459 static void
8460 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8461 {
8462 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8463 struct objfile *objfile = per_objfile->objfile;
8464 struct gdbarch *gdbarch = objfile->arch ();
8465 CORE_ADDR addr = 0;
8466 const char *actual_name = NULL;
8467 CORE_ADDR baseaddr;
8468
8469 baseaddr = objfile->text_section_offset ();
8470
8471 gdb::unique_xmalloc_ptr<char> built_actual_name
8472 = partial_die_full_name (pdi, cu);
8473 if (built_actual_name != NULL)
8474 actual_name = built_actual_name.get ();
8475
8476 if (actual_name == NULL)
8477 actual_name = pdi->name (cu);
8478
8479 partial_symbol psymbol;
8480 memset (&psymbol, 0, sizeof (psymbol));
8481 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8482 psymbol.ginfo.section = -1;
8483
8484 /* The code below indicates that the psymbol should be installed by
8485 setting this. */
8486 gdb::optional<psymbol_placement> where;
8487
8488 switch (pdi->tag)
8489 {
8490 case DW_TAG_inlined_subroutine:
8491 case DW_TAG_subprogram:
8492 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8493 - baseaddr);
8494 if (pdi->is_external
8495 || cu->language == language_ada
8496 || (cu->language == language_fortran
8497 && pdi->die_parent != NULL
8498 && pdi->die_parent->tag == DW_TAG_subprogram))
8499 {
8500 /* Normally, only "external" DIEs are part of the global scope.
8501 But in Ada and Fortran, we want to be able to access nested
8502 procedures globally. So all Ada and Fortran subprograms are
8503 stored in the global scope. */
8504 where = psymbol_placement::GLOBAL;
8505 }
8506 else
8507 where = psymbol_placement::STATIC;
8508
8509 psymbol.domain = VAR_DOMAIN;
8510 psymbol.aclass = LOC_BLOCK;
8511 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8512 psymbol.ginfo.value.address = addr;
8513
8514 if (pdi->main_subprogram && actual_name != NULL)
8515 set_objfile_main_name (objfile, actual_name, cu->language);
8516 break;
8517 case DW_TAG_constant:
8518 psymbol.domain = VAR_DOMAIN;
8519 psymbol.aclass = LOC_STATIC;
8520 where = (pdi->is_external
8521 ? psymbol_placement::GLOBAL
8522 : psymbol_placement::STATIC);
8523 break;
8524 case DW_TAG_variable:
8525 if (pdi->d.locdesc)
8526 addr = decode_locdesc (pdi->d.locdesc, cu);
8527
8528 if (pdi->d.locdesc
8529 && addr == 0
8530 && !per_objfile->per_bfd->has_section_at_zero)
8531 {
8532 /* A global or static variable may also have been stripped
8533 out by the linker if unused, in which case its address
8534 will be nullified; do not add such variables into partial
8535 symbol table then. */
8536 }
8537 else if (pdi->is_external)
8538 {
8539 /* Global Variable.
8540 Don't enter into the minimal symbol tables as there is
8541 a minimal symbol table entry from the ELF symbols already.
8542 Enter into partial symbol table if it has a location
8543 descriptor or a type.
8544 If the location descriptor is missing, new_symbol will create
8545 a LOC_UNRESOLVED symbol, the address of the variable will then
8546 be determined from the minimal symbol table whenever the variable
8547 is referenced.
8548 The address for the partial symbol table entry is not
8549 used by GDB, but it comes in handy for debugging partial symbol
8550 table building. */
8551
8552 if (pdi->d.locdesc || pdi->has_type)
8553 {
8554 psymbol.domain = VAR_DOMAIN;
8555 psymbol.aclass = LOC_STATIC;
8556 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8557 psymbol.ginfo.value.address = addr;
8558 where = psymbol_placement::GLOBAL;
8559 }
8560 }
8561 else
8562 {
8563 int has_loc = pdi->d.locdesc != NULL;
8564
8565 /* Static Variable. Skip symbols whose value we cannot know (those
8566 without location descriptors or constant values). */
8567 if (!has_loc && !pdi->has_const_value)
8568 return;
8569
8570 psymbol.domain = VAR_DOMAIN;
8571 psymbol.aclass = LOC_STATIC;
8572 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8573 if (has_loc)
8574 psymbol.ginfo.value.address = addr;
8575 where = psymbol_placement::STATIC;
8576 }
8577 break;
8578 case DW_TAG_typedef:
8579 case DW_TAG_base_type:
8580 case DW_TAG_subrange_type:
8581 psymbol.domain = VAR_DOMAIN;
8582 psymbol.aclass = LOC_TYPEDEF;
8583 where = psymbol_placement::STATIC;
8584 break;
8585 case DW_TAG_imported_declaration:
8586 case DW_TAG_namespace:
8587 psymbol.domain = VAR_DOMAIN;
8588 psymbol.aclass = LOC_TYPEDEF;
8589 where = psymbol_placement::GLOBAL;
8590 break;
8591 case DW_TAG_module:
8592 /* With Fortran 77 there might be a "BLOCK DATA" module
8593 available without any name. If so, we skip the module as it
8594 doesn't bring any value. */
8595 if (actual_name != nullptr)
8596 {
8597 psymbol.domain = MODULE_DOMAIN;
8598 psymbol.aclass = LOC_TYPEDEF;
8599 where = psymbol_placement::GLOBAL;
8600 }
8601 break;
8602 case DW_TAG_class_type:
8603 case DW_TAG_interface_type:
8604 case DW_TAG_structure_type:
8605 case DW_TAG_union_type:
8606 case DW_TAG_enumeration_type:
8607 /* Skip external references. The DWARF standard says in the section
8608 about "Structure, Union, and Class Type Entries": "An incomplete
8609 structure, union or class type is represented by a structure,
8610 union or class entry that does not have a byte size attribute
8611 and that has a DW_AT_declaration attribute." */
8612 if (!pdi->has_byte_size && pdi->is_declaration)
8613 return;
8614
8615 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8616 static vs. global. */
8617 psymbol.domain = STRUCT_DOMAIN;
8618 psymbol.aclass = LOC_TYPEDEF;
8619 where = (cu->language == language_cplus
8620 ? psymbol_placement::GLOBAL
8621 : psymbol_placement::STATIC);
8622 break;
8623 case DW_TAG_enumerator:
8624 psymbol.domain = VAR_DOMAIN;
8625 psymbol.aclass = LOC_CONST;
8626 where = (cu->language == language_cplus
8627 ? psymbol_placement::GLOBAL
8628 : psymbol_placement::STATIC);
8629 break;
8630 default:
8631 break;
8632 }
8633
8634 if (where.has_value ())
8635 {
8636 if (built_actual_name != nullptr)
8637 actual_name = objfile->intern (actual_name);
8638 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8639 psymbol.ginfo.set_linkage_name (actual_name);
8640 else
8641 {
8642 psymbol.ginfo.set_demangled_name (actual_name,
8643 &objfile->objfile_obstack);
8644 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8645 }
8646 add_psymbol_to_list (psymbol, *where, objfile);
8647 }
8648 }
8649
8650 /* Read a partial die corresponding to a namespace; also, add a symbol
8651 corresponding to that namespace to the symbol table. NAMESPACE is
8652 the name of the enclosing namespace. */
8653
8654 static void
8655 add_partial_namespace (struct partial_die_info *pdi,
8656 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8657 int set_addrmap, struct dwarf2_cu *cu)
8658 {
8659 /* Add a symbol for the namespace. */
8660
8661 add_partial_symbol (pdi, cu);
8662
8663 /* Now scan partial symbols in that namespace. */
8664
8665 if (pdi->has_children)
8666 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8667 }
8668
8669 /* Read a partial die corresponding to a Fortran module. */
8670
8671 static void
8672 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8673 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8674 {
8675 /* Add a symbol for the namespace. */
8676
8677 add_partial_symbol (pdi, cu);
8678
8679 /* Now scan partial symbols in that module. */
8680
8681 if (pdi->has_children)
8682 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8683 }
8684
8685 /* Read a partial die corresponding to a subprogram or an inlined
8686 subprogram and create a partial symbol for that subprogram.
8687 When the CU language allows it, this routine also defines a partial
8688 symbol for each nested subprogram that this subprogram contains.
8689 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8690 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8691
8692 PDI may also be a lexical block, in which case we simply search
8693 recursively for subprograms defined inside that lexical block.
8694 Again, this is only performed when the CU language allows this
8695 type of definitions. */
8696
8697 static void
8698 add_partial_subprogram (struct partial_die_info *pdi,
8699 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8700 int set_addrmap, struct dwarf2_cu *cu)
8701 {
8702 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8703 {
8704 if (pdi->has_pc_info)
8705 {
8706 if (pdi->lowpc < *lowpc)
8707 *lowpc = pdi->lowpc;
8708 if (pdi->highpc > *highpc)
8709 *highpc = pdi->highpc;
8710 if (set_addrmap)
8711 {
8712 struct objfile *objfile = cu->per_objfile->objfile;
8713 struct gdbarch *gdbarch = objfile->arch ();
8714 CORE_ADDR baseaddr;
8715 CORE_ADDR this_highpc;
8716 CORE_ADDR this_lowpc;
8717
8718 baseaddr = objfile->text_section_offset ();
8719 this_lowpc
8720 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8721 pdi->lowpc + baseaddr)
8722 - baseaddr);
8723 this_highpc
8724 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8725 pdi->highpc + baseaddr)
8726 - baseaddr);
8727 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8728 this_lowpc, this_highpc - 1,
8729 cu->per_cu->v.psymtab);
8730 }
8731 }
8732
8733 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8734 {
8735 if (!pdi->is_declaration)
8736 /* Ignore subprogram DIEs that do not have a name, they are
8737 illegal. Do not emit a complaint at this point, we will
8738 do so when we convert this psymtab into a symtab. */
8739 if (pdi->name (cu))
8740 add_partial_symbol (pdi, cu);
8741 }
8742 }
8743
8744 if (! pdi->has_children)
8745 return;
8746
8747 if (cu->language == language_ada || cu->language == language_fortran)
8748 {
8749 pdi = pdi->die_child;
8750 while (pdi != NULL)
8751 {
8752 pdi->fixup (cu);
8753 if (pdi->tag == DW_TAG_subprogram
8754 || pdi->tag == DW_TAG_inlined_subroutine
8755 || pdi->tag == DW_TAG_lexical_block)
8756 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8757 pdi = pdi->die_sibling;
8758 }
8759 }
8760 }
8761
8762 /* Read a partial die corresponding to an enumeration type. */
8763
8764 static void
8765 add_partial_enumeration (struct partial_die_info *enum_pdi,
8766 struct dwarf2_cu *cu)
8767 {
8768 struct partial_die_info *pdi;
8769
8770 if (enum_pdi->name (cu) != NULL)
8771 add_partial_symbol (enum_pdi, cu);
8772
8773 pdi = enum_pdi->die_child;
8774 while (pdi)
8775 {
8776 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8777 complaint (_("malformed enumerator DIE ignored"));
8778 else
8779 add_partial_symbol (pdi, cu);
8780 pdi = pdi->die_sibling;
8781 }
8782 }
8783
8784 /* Return the initial uleb128 in the die at INFO_PTR. */
8785
8786 static unsigned int
8787 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8788 {
8789 unsigned int bytes_read;
8790
8791 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8792 }
8793
8794 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8795 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8796
8797 Return the corresponding abbrev, or NULL if the number is zero (indicating
8798 an empty DIE). In either case *BYTES_READ will be set to the length of
8799 the initial number. */
8800
8801 static struct abbrev_info *
8802 peek_die_abbrev (const die_reader_specs &reader,
8803 const gdb_byte *info_ptr, unsigned int *bytes_read)
8804 {
8805 dwarf2_cu *cu = reader.cu;
8806 bfd *abfd = cu->per_objfile->objfile->obfd;
8807 unsigned int abbrev_number
8808 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8809
8810 if (abbrev_number == 0)
8811 return NULL;
8812
8813 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8814 if (!abbrev)
8815 {
8816 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8817 " at offset %s [in module %s]"),
8818 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8819 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8820 }
8821
8822 return abbrev;
8823 }
8824
8825 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8826 Returns a pointer to the end of a series of DIEs, terminated by an empty
8827 DIE. Any children of the skipped DIEs will also be skipped. */
8828
8829 static const gdb_byte *
8830 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8831 {
8832 while (1)
8833 {
8834 unsigned int bytes_read;
8835 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8836
8837 if (abbrev == NULL)
8838 return info_ptr + bytes_read;
8839 else
8840 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8841 }
8842 }
8843
8844 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8845 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8846 abbrev corresponding to that skipped uleb128 should be passed in
8847 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8848 children. */
8849
8850 static const gdb_byte *
8851 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8852 struct abbrev_info *abbrev)
8853 {
8854 unsigned int bytes_read;
8855 struct attribute attr;
8856 bfd *abfd = reader->abfd;
8857 struct dwarf2_cu *cu = reader->cu;
8858 const gdb_byte *buffer = reader->buffer;
8859 const gdb_byte *buffer_end = reader->buffer_end;
8860 unsigned int form, i;
8861
8862 for (i = 0; i < abbrev->num_attrs; i++)
8863 {
8864 /* The only abbrev we care about is DW_AT_sibling. */
8865 if (abbrev->attrs[i].name == DW_AT_sibling)
8866 {
8867 bool ignored;
8868 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8869 &ignored);
8870 if (attr.form == DW_FORM_ref_addr)
8871 complaint (_("ignoring absolute DW_AT_sibling"));
8872 else
8873 {
8874 sect_offset off = attr.get_ref_die_offset ();
8875 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8876
8877 if (sibling_ptr < info_ptr)
8878 complaint (_("DW_AT_sibling points backwards"));
8879 else if (sibling_ptr > reader->buffer_end)
8880 reader->die_section->overflow_complaint ();
8881 else
8882 return sibling_ptr;
8883 }
8884 }
8885
8886 /* If it isn't DW_AT_sibling, skip this attribute. */
8887 form = abbrev->attrs[i].form;
8888 skip_attribute:
8889 switch (form)
8890 {
8891 case DW_FORM_ref_addr:
8892 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8893 and later it is offset sized. */
8894 if (cu->header.version == 2)
8895 info_ptr += cu->header.addr_size;
8896 else
8897 info_ptr += cu->header.offset_size;
8898 break;
8899 case DW_FORM_GNU_ref_alt:
8900 info_ptr += cu->header.offset_size;
8901 break;
8902 case DW_FORM_addr:
8903 info_ptr += cu->header.addr_size;
8904 break;
8905 case DW_FORM_data1:
8906 case DW_FORM_ref1:
8907 case DW_FORM_flag:
8908 case DW_FORM_strx1:
8909 info_ptr += 1;
8910 break;
8911 case DW_FORM_flag_present:
8912 case DW_FORM_implicit_const:
8913 break;
8914 case DW_FORM_data2:
8915 case DW_FORM_ref2:
8916 case DW_FORM_strx2:
8917 info_ptr += 2;
8918 break;
8919 case DW_FORM_strx3:
8920 info_ptr += 3;
8921 break;
8922 case DW_FORM_data4:
8923 case DW_FORM_ref4:
8924 case DW_FORM_strx4:
8925 info_ptr += 4;
8926 break;
8927 case DW_FORM_data8:
8928 case DW_FORM_ref8:
8929 case DW_FORM_ref_sig8:
8930 info_ptr += 8;
8931 break;
8932 case DW_FORM_data16:
8933 info_ptr += 16;
8934 break;
8935 case DW_FORM_string:
8936 read_direct_string (abfd, info_ptr, &bytes_read);
8937 info_ptr += bytes_read;
8938 break;
8939 case DW_FORM_sec_offset:
8940 case DW_FORM_strp:
8941 case DW_FORM_GNU_strp_alt:
8942 info_ptr += cu->header.offset_size;
8943 break;
8944 case DW_FORM_exprloc:
8945 case DW_FORM_block:
8946 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8947 info_ptr += bytes_read;
8948 break;
8949 case DW_FORM_block1:
8950 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8951 break;
8952 case DW_FORM_block2:
8953 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8954 break;
8955 case DW_FORM_block4:
8956 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8957 break;
8958 case DW_FORM_addrx:
8959 case DW_FORM_strx:
8960 case DW_FORM_sdata:
8961 case DW_FORM_udata:
8962 case DW_FORM_ref_udata:
8963 case DW_FORM_GNU_addr_index:
8964 case DW_FORM_GNU_str_index:
8965 case DW_FORM_rnglistx:
8966 case DW_FORM_loclistx:
8967 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8968 break;
8969 case DW_FORM_indirect:
8970 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8971 info_ptr += bytes_read;
8972 /* We need to continue parsing from here, so just go back to
8973 the top. */
8974 goto skip_attribute;
8975
8976 default:
8977 error (_("Dwarf Error: Cannot handle %s "
8978 "in DWARF reader [in module %s]"),
8979 dwarf_form_name (form),
8980 bfd_get_filename (abfd));
8981 }
8982 }
8983
8984 if (abbrev->has_children)
8985 return skip_children (reader, info_ptr);
8986 else
8987 return info_ptr;
8988 }
8989
8990 /* Locate ORIG_PDI's sibling.
8991 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8992
8993 static const gdb_byte *
8994 locate_pdi_sibling (const struct die_reader_specs *reader,
8995 struct partial_die_info *orig_pdi,
8996 const gdb_byte *info_ptr)
8997 {
8998 /* Do we know the sibling already? */
8999
9000 if (orig_pdi->sibling)
9001 return orig_pdi->sibling;
9002
9003 /* Are there any children to deal with? */
9004
9005 if (!orig_pdi->has_children)
9006 return info_ptr;
9007
9008 /* Skip the children the long way. */
9009
9010 return skip_children (reader, info_ptr);
9011 }
9012
9013 /* Expand this partial symbol table into a full symbol table. SELF is
9014 not NULL. */
9015
9016 void
9017 dwarf2_psymtab::read_symtab (struct objfile *objfile)
9018 {
9019 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9020
9021 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
9022
9023 /* If this psymtab is constructed from a debug-only objfile, the
9024 has_section_at_zero flag will not necessarily be correct. We
9025 can get the correct value for this flag by looking at the data
9026 associated with the (presumably stripped) associated objfile. */
9027 if (objfile->separate_debug_objfile_backlink)
9028 {
9029 dwarf2_per_objfile *per_objfile_backlink
9030 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9031
9032 per_objfile->per_bfd->has_section_at_zero
9033 = per_objfile_backlink->per_bfd->has_section_at_zero;
9034 }
9035
9036 expand_psymtab (objfile);
9037
9038 process_cu_includes (per_objfile);
9039 }
9040 \f
9041 /* Reading in full CUs. */
9042
9043 /* Add PER_CU to the queue. */
9044
9045 static void
9046 queue_comp_unit (dwarf2_per_cu_data *per_cu,
9047 dwarf2_per_objfile *per_objfile,
9048 enum language pretend_language)
9049 {
9050 per_cu->queued = 1;
9051 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
9052 }
9053
9054 /* If PER_CU is not yet queued, add it to the queue.
9055 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9056 dependency.
9057 The result is non-zero if PER_CU was queued, otherwise the result is zero
9058 meaning either PER_CU is already queued or it is already loaded.
9059
9060 N.B. There is an invariant here that if a CU is queued then it is loaded.
9061 The caller is required to load PER_CU if we return non-zero. */
9062
9063 static int
9064 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9065 dwarf2_per_cu_data *per_cu,
9066 dwarf2_per_objfile *per_objfile,
9067 enum language pretend_language)
9068 {
9069 /* We may arrive here during partial symbol reading, if we need full
9070 DIEs to process an unusual case (e.g. template arguments). Do
9071 not queue PER_CU, just tell our caller to load its DIEs. */
9072 if (per_cu->per_bfd->reading_partial_symbols)
9073 {
9074 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9075
9076 if (cu == NULL || cu->dies == NULL)
9077 return 1;
9078 return 0;
9079 }
9080
9081 /* Mark the dependence relation so that we don't flush PER_CU
9082 too early. */
9083 if (dependent_cu != NULL)
9084 dwarf2_add_dependence (dependent_cu, per_cu);
9085
9086 /* If it's already on the queue, we have nothing to do. */
9087 if (per_cu->queued)
9088 return 0;
9089
9090 /* If the compilation unit is already loaded, just mark it as
9091 used. */
9092 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9093 if (cu != nullptr)
9094 {
9095 cu->last_used = 0;
9096 return 0;
9097 }
9098
9099 /* Add it to the queue. */
9100 queue_comp_unit (per_cu, per_objfile, pretend_language);
9101
9102 return 1;
9103 }
9104
9105 /* Process the queue. */
9106
9107 static void
9108 process_queue (dwarf2_per_objfile *per_objfile)
9109 {
9110 if (dwarf_read_debug)
9111 {
9112 fprintf_unfiltered (gdb_stdlog,
9113 "Expanding one or more symtabs of objfile %s ...\n",
9114 objfile_name (per_objfile->objfile));
9115 }
9116
9117 /* The queue starts out with one item, but following a DIE reference
9118 may load a new CU, adding it to the end of the queue. */
9119 while (!per_objfile->per_bfd->queue.empty ())
9120 {
9121 dwarf2_queue_item &item = per_objfile->per_bfd->queue.front ();
9122 dwarf2_per_cu_data *per_cu = item.per_cu;
9123
9124 if (!per_objfile->symtab_set_p (per_cu))
9125 {
9126 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9127
9128 /* Skip dummy CUs. */
9129 if (cu != nullptr)
9130 {
9131 unsigned int debug_print_threshold;
9132 char buf[100];
9133
9134 if (per_cu->is_debug_types)
9135 {
9136 struct signatured_type *sig_type =
9137 (struct signatured_type *) per_cu;
9138
9139 sprintf (buf, "TU %s at offset %s",
9140 hex_string (sig_type->signature),
9141 sect_offset_str (per_cu->sect_off));
9142 /* There can be 100s of TUs.
9143 Only print them in verbose mode. */
9144 debug_print_threshold = 2;
9145 }
9146 else
9147 {
9148 sprintf (buf, "CU at offset %s",
9149 sect_offset_str (per_cu->sect_off));
9150 debug_print_threshold = 1;
9151 }
9152
9153 if (dwarf_read_debug >= debug_print_threshold)
9154 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9155
9156 if (per_cu->is_debug_types)
9157 process_full_type_unit (cu, item.pretend_language);
9158 else
9159 process_full_comp_unit (cu, item.pretend_language);
9160
9161 if (dwarf_read_debug >= debug_print_threshold)
9162 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9163 }
9164 }
9165
9166 per_cu->queued = 0;
9167 per_objfile->per_bfd->queue.pop ();
9168 }
9169
9170 if (dwarf_read_debug)
9171 {
9172 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9173 objfile_name (per_objfile->objfile));
9174 }
9175 }
9176
9177 /* Read in full symbols for PST, and anything it depends on. */
9178
9179 void
9180 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9181 {
9182 gdb_assert (!readin_p (objfile));
9183
9184 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9185 free_cached_comp_units freer (per_objfile);
9186 expand_dependencies (objfile);
9187
9188 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9189 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9190 }
9191
9192 /* See psympriv.h. */
9193
9194 bool
9195 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9196 {
9197 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9198 return per_objfile->symtab_set_p (per_cu_data);
9199 }
9200
9201 /* See psympriv.h. */
9202
9203 compunit_symtab *
9204 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9205 {
9206 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9207 return per_objfile->get_symtab (per_cu_data);
9208 }
9209
9210 /* Trivial hash function for die_info: the hash value of a DIE
9211 is its offset in .debug_info for this objfile. */
9212
9213 static hashval_t
9214 die_hash (const void *item)
9215 {
9216 const struct die_info *die = (const struct die_info *) item;
9217
9218 return to_underlying (die->sect_off);
9219 }
9220
9221 /* Trivial comparison function for die_info structures: two DIEs
9222 are equal if they have the same offset. */
9223
9224 static int
9225 die_eq (const void *item_lhs, const void *item_rhs)
9226 {
9227 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9228 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9229
9230 return die_lhs->sect_off == die_rhs->sect_off;
9231 }
9232
9233 /* Load the DIEs associated with PER_CU into memory. */
9234
9235 static void
9236 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9237 dwarf2_per_objfile *per_objfile,
9238 bool skip_partial,
9239 enum language pretend_language)
9240 {
9241 gdb_assert (! this_cu->is_debug_types);
9242
9243 dwarf2_cu *existing_cu = per_objfile->get_cu (this_cu);
9244 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
9245 if (reader.dummy_p)
9246 return;
9247
9248 struct dwarf2_cu *cu = reader.cu;
9249 const gdb_byte *info_ptr = reader.info_ptr;
9250
9251 gdb_assert (cu->die_hash == NULL);
9252 cu->die_hash =
9253 htab_create_alloc_ex (cu->header.length / 12,
9254 die_hash,
9255 die_eq,
9256 NULL,
9257 &cu->comp_unit_obstack,
9258 hashtab_obstack_allocate,
9259 dummy_obstack_deallocate);
9260
9261 if (reader.comp_unit_die->has_children)
9262 reader.comp_unit_die->child
9263 = read_die_and_siblings (&reader, reader.info_ptr,
9264 &info_ptr, reader.comp_unit_die);
9265 cu->dies = reader.comp_unit_die;
9266 /* comp_unit_die is not stored in die_hash, no need. */
9267
9268 /* We try not to read any attributes in this function, because not
9269 all CUs needed for references have been loaded yet, and symbol
9270 table processing isn't initialized. But we have to set the CU language,
9271 or we won't be able to build types correctly.
9272 Similarly, if we do not read the producer, we can not apply
9273 producer-specific interpretation. */
9274 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9275
9276 reader.keep ();
9277 }
9278
9279 /* Add a DIE to the delayed physname list. */
9280
9281 static void
9282 add_to_method_list (struct type *type, int fnfield_index, int index,
9283 const char *name, struct die_info *die,
9284 struct dwarf2_cu *cu)
9285 {
9286 struct delayed_method_info mi;
9287 mi.type = type;
9288 mi.fnfield_index = fnfield_index;
9289 mi.index = index;
9290 mi.name = name;
9291 mi.die = die;
9292 cu->method_list.push_back (mi);
9293 }
9294
9295 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9296 "const" / "volatile". If so, decrements LEN by the length of the
9297 modifier and return true. Otherwise return false. */
9298
9299 template<size_t N>
9300 static bool
9301 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9302 {
9303 size_t mod_len = sizeof (mod) - 1;
9304 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9305 {
9306 len -= mod_len;
9307 return true;
9308 }
9309 return false;
9310 }
9311
9312 /* Compute the physnames of any methods on the CU's method list.
9313
9314 The computation of method physnames is delayed in order to avoid the
9315 (bad) condition that one of the method's formal parameters is of an as yet
9316 incomplete type. */
9317
9318 static void
9319 compute_delayed_physnames (struct dwarf2_cu *cu)
9320 {
9321 /* Only C++ delays computing physnames. */
9322 if (cu->method_list.empty ())
9323 return;
9324 gdb_assert (cu->language == language_cplus);
9325
9326 for (const delayed_method_info &mi : cu->method_list)
9327 {
9328 const char *physname;
9329 struct fn_fieldlist *fn_flp
9330 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9331 physname = dwarf2_physname (mi.name, mi.die, cu);
9332 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9333 = physname ? physname : "";
9334
9335 /* Since there's no tag to indicate whether a method is a
9336 const/volatile overload, extract that information out of the
9337 demangled name. */
9338 if (physname != NULL)
9339 {
9340 size_t len = strlen (physname);
9341
9342 while (1)
9343 {
9344 if (physname[len] == ')') /* shortcut */
9345 break;
9346 else if (check_modifier (physname, len, " const"))
9347 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9348 else if (check_modifier (physname, len, " volatile"))
9349 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9350 else
9351 break;
9352 }
9353 }
9354 }
9355
9356 /* The list is no longer needed. */
9357 cu->method_list.clear ();
9358 }
9359
9360 /* Go objects should be embedded in a DW_TAG_module DIE,
9361 and it's not clear if/how imported objects will appear.
9362 To keep Go support simple until that's worked out,
9363 go back through what we've read and create something usable.
9364 We could do this while processing each DIE, and feels kinda cleaner,
9365 but that way is more invasive.
9366 This is to, for example, allow the user to type "p var" or "b main"
9367 without having to specify the package name, and allow lookups
9368 of module.object to work in contexts that use the expression
9369 parser. */
9370
9371 static void
9372 fixup_go_packaging (struct dwarf2_cu *cu)
9373 {
9374 gdb::unique_xmalloc_ptr<char> package_name;
9375 struct pending *list;
9376 int i;
9377
9378 for (list = *cu->get_builder ()->get_global_symbols ();
9379 list != NULL;
9380 list = list->next)
9381 {
9382 for (i = 0; i < list->nsyms; ++i)
9383 {
9384 struct symbol *sym = list->symbol[i];
9385
9386 if (sym->language () == language_go
9387 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9388 {
9389 gdb::unique_xmalloc_ptr<char> this_package_name
9390 (go_symbol_package_name (sym));
9391
9392 if (this_package_name == NULL)
9393 continue;
9394 if (package_name == NULL)
9395 package_name = std::move (this_package_name);
9396 else
9397 {
9398 struct objfile *objfile = cu->per_objfile->objfile;
9399 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9400 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9401 (symbol_symtab (sym) != NULL
9402 ? symtab_to_filename_for_display
9403 (symbol_symtab (sym))
9404 : objfile_name (objfile)),
9405 this_package_name.get (), package_name.get ());
9406 }
9407 }
9408 }
9409 }
9410
9411 if (package_name != NULL)
9412 {
9413 struct objfile *objfile = cu->per_objfile->objfile;
9414 const char *saved_package_name = objfile->intern (package_name.get ());
9415 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9416 saved_package_name);
9417 struct symbol *sym;
9418
9419 sym = new (&objfile->objfile_obstack) symbol;
9420 sym->set_language (language_go, &objfile->objfile_obstack);
9421 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9422 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9423 e.g., "main" finds the "main" module and not C's main(). */
9424 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9425 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9426 SYMBOL_TYPE (sym) = type;
9427
9428 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9429 }
9430 }
9431
9432 /* Allocate a fully-qualified name consisting of the two parts on the
9433 obstack. */
9434
9435 static const char *
9436 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9437 {
9438 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9439 }
9440
9441 /* A helper that allocates a variant part to attach to a Rust enum
9442 type. OBSTACK is where the results should be allocated. TYPE is
9443 the type we're processing. DISCRIMINANT_INDEX is the index of the
9444 discriminant. It must be the index of one of the fields of TYPE,
9445 or -1 to mean there is no discriminant (univariant enum).
9446 DEFAULT_INDEX is the index of the default field; or -1 if there is
9447 no default. RANGES is indexed by "effective" field number (the
9448 field index, but omitting the discriminant and default fields) and
9449 must hold the discriminant values used by the variants. Note that
9450 RANGES must have a lifetime at least as long as OBSTACK -- either
9451 already allocated on it, or static. */
9452
9453 static void
9454 alloc_rust_variant (struct obstack *obstack, struct type *type,
9455 int discriminant_index, int default_index,
9456 gdb::array_view<discriminant_range> ranges)
9457 {
9458 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9459 gdb_assert (discriminant_index == -1
9460 || (discriminant_index >= 0
9461 && discriminant_index < type->num_fields ()));
9462 gdb_assert (default_index == -1
9463 || (default_index >= 0 && default_index < type->num_fields ()));
9464
9465 /* We have one variant for each non-discriminant field. */
9466 int n_variants = type->num_fields ();
9467 if (discriminant_index != -1)
9468 --n_variants;
9469
9470 variant *variants = new (obstack) variant[n_variants];
9471 int var_idx = 0;
9472 int range_idx = 0;
9473 for (int i = 0; i < type->num_fields (); ++i)
9474 {
9475 if (i == discriminant_index)
9476 continue;
9477
9478 variants[var_idx].first_field = i;
9479 variants[var_idx].last_field = i + 1;
9480
9481 /* The default field does not need a range, but other fields do.
9482 We skipped the discriminant above. */
9483 if (i != default_index)
9484 {
9485 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9486 ++range_idx;
9487 }
9488
9489 ++var_idx;
9490 }
9491
9492 gdb_assert (range_idx == ranges.size ());
9493 gdb_assert (var_idx == n_variants);
9494
9495 variant_part *part = new (obstack) variant_part;
9496 part->discriminant_index = discriminant_index;
9497 /* If there is no discriminant, then whether it is signed is of no
9498 consequence. */
9499 part->is_unsigned
9500 = (discriminant_index == -1
9501 ? false
9502 : type->field (discriminant_index).type ()->is_unsigned ());
9503 part->variants = gdb::array_view<variant> (variants, n_variants);
9504
9505 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9506 gdb::array_view<variant_part> *prop_value
9507 = new (storage) gdb::array_view<variant_part> (part, 1);
9508
9509 struct dynamic_prop prop;
9510 prop.set_variant_parts (prop_value);
9511
9512 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9513 }
9514
9515 /* Some versions of rustc emitted enums in an unusual way.
9516
9517 Ordinary enums were emitted as unions. The first element of each
9518 structure in the union was named "RUST$ENUM$DISR". This element
9519 held the discriminant.
9520
9521 These versions of Rust also implemented the "non-zero"
9522 optimization. When the enum had two values, and one is empty and
9523 the other holds a pointer that cannot be zero, the pointer is used
9524 as the discriminant, with a zero value meaning the empty variant.
9525 Here, the union's first member is of the form
9526 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9527 where the fieldnos are the indices of the fields that should be
9528 traversed in order to find the field (which may be several fields deep)
9529 and the variantname is the name of the variant of the case when the
9530 field is zero.
9531
9532 This function recognizes whether TYPE is of one of these forms,
9533 and, if so, smashes it to be a variant type. */
9534
9535 static void
9536 quirk_rust_enum (struct type *type, struct objfile *objfile)
9537 {
9538 gdb_assert (type->code () == TYPE_CODE_UNION);
9539
9540 /* We don't need to deal with empty enums. */
9541 if (type->num_fields () == 0)
9542 return;
9543
9544 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9545 if (type->num_fields () == 1
9546 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9547 {
9548 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9549
9550 /* Decode the field name to find the offset of the
9551 discriminant. */
9552 ULONGEST bit_offset = 0;
9553 struct type *field_type = type->field (0).type ();
9554 while (name[0] >= '0' && name[0] <= '9')
9555 {
9556 char *tail;
9557 unsigned long index = strtoul (name, &tail, 10);
9558 name = tail;
9559 if (*name != '$'
9560 || index >= field_type->num_fields ()
9561 || (TYPE_FIELD_LOC_KIND (field_type, index)
9562 != FIELD_LOC_KIND_BITPOS))
9563 {
9564 complaint (_("Could not parse Rust enum encoding string \"%s\""
9565 "[in module %s]"),
9566 TYPE_FIELD_NAME (type, 0),
9567 objfile_name (objfile));
9568 return;
9569 }
9570 ++name;
9571
9572 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9573 field_type = field_type->field (index).type ();
9574 }
9575
9576 /* Smash this type to be a structure type. We have to do this
9577 because the type has already been recorded. */
9578 type->set_code (TYPE_CODE_STRUCT);
9579 type->set_num_fields (3);
9580 /* Save the field we care about. */
9581 struct field saved_field = type->field (0);
9582 type->set_fields
9583 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9584
9585 /* Put the discriminant at index 0. */
9586 type->field (0).set_type (field_type);
9587 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9588 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9589 SET_FIELD_BITPOS (type->field (0), bit_offset);
9590
9591 /* The order of fields doesn't really matter, so put the real
9592 field at index 1 and the data-less field at index 2. */
9593 type->field (1) = saved_field;
9594 TYPE_FIELD_NAME (type, 1)
9595 = rust_last_path_segment (type->field (1).type ()->name ());
9596 type->field (1).type ()->set_name
9597 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9598 TYPE_FIELD_NAME (type, 1)));
9599
9600 const char *dataless_name
9601 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9602 name);
9603 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9604 dataless_name);
9605 type->field (2).set_type (dataless_type);
9606 /* NAME points into the original discriminant name, which
9607 already has the correct lifetime. */
9608 TYPE_FIELD_NAME (type, 2) = name;
9609 SET_FIELD_BITPOS (type->field (2), 0);
9610
9611 /* Indicate that this is a variant type. */
9612 static discriminant_range ranges[1] = { { 0, 0 } };
9613 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9614 }
9615 /* A union with a single anonymous field is probably an old-style
9616 univariant enum. */
9617 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9618 {
9619 /* Smash this type to be a structure type. We have to do this
9620 because the type has already been recorded. */
9621 type->set_code (TYPE_CODE_STRUCT);
9622
9623 struct type *field_type = type->field (0).type ();
9624 const char *variant_name
9625 = rust_last_path_segment (field_type->name ());
9626 TYPE_FIELD_NAME (type, 0) = variant_name;
9627 field_type->set_name
9628 (rust_fully_qualify (&objfile->objfile_obstack,
9629 type->name (), variant_name));
9630
9631 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
9632 }
9633 else
9634 {
9635 struct type *disr_type = nullptr;
9636 for (int i = 0; i < type->num_fields (); ++i)
9637 {
9638 disr_type = type->field (i).type ();
9639
9640 if (disr_type->code () != TYPE_CODE_STRUCT)
9641 {
9642 /* All fields of a true enum will be structs. */
9643 return;
9644 }
9645 else if (disr_type->num_fields () == 0)
9646 {
9647 /* Could be data-less variant, so keep going. */
9648 disr_type = nullptr;
9649 }
9650 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9651 "RUST$ENUM$DISR") != 0)
9652 {
9653 /* Not a Rust enum. */
9654 return;
9655 }
9656 else
9657 {
9658 /* Found one. */
9659 break;
9660 }
9661 }
9662
9663 /* If we got here without a discriminant, then it's probably
9664 just a union. */
9665 if (disr_type == nullptr)
9666 return;
9667
9668 /* Smash this type to be a structure type. We have to do this
9669 because the type has already been recorded. */
9670 type->set_code (TYPE_CODE_STRUCT);
9671
9672 /* Make space for the discriminant field. */
9673 struct field *disr_field = &disr_type->field (0);
9674 field *new_fields
9675 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9676 * sizeof (struct field)));
9677 memcpy (new_fields + 1, type->fields (),
9678 type->num_fields () * sizeof (struct field));
9679 type->set_fields (new_fields);
9680 type->set_num_fields (type->num_fields () + 1);
9681
9682 /* Install the discriminant at index 0 in the union. */
9683 type->field (0) = *disr_field;
9684 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9685 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9686
9687 /* We need a way to find the correct discriminant given a
9688 variant name. For convenience we build a map here. */
9689 struct type *enum_type = disr_field->type ();
9690 std::unordered_map<std::string, ULONGEST> discriminant_map;
9691 for (int i = 0; i < enum_type->num_fields (); ++i)
9692 {
9693 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9694 {
9695 const char *name
9696 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9697 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9698 }
9699 }
9700
9701 int n_fields = type->num_fields ();
9702 /* We don't need a range entry for the discriminant, but we do
9703 need one for every other field, as there is no default
9704 variant. */
9705 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9706 discriminant_range,
9707 n_fields - 1);
9708 /* Skip the discriminant here. */
9709 for (int i = 1; i < n_fields; ++i)
9710 {
9711 /* Find the final word in the name of this variant's type.
9712 That name can be used to look up the correct
9713 discriminant. */
9714 const char *variant_name
9715 = rust_last_path_segment (type->field (i).type ()->name ());
9716
9717 auto iter = discriminant_map.find (variant_name);
9718 if (iter != discriminant_map.end ())
9719 {
9720 ranges[i - 1].low = iter->second;
9721 ranges[i - 1].high = iter->second;
9722 }
9723
9724 /* In Rust, each element should have the size of the
9725 enclosing enum. */
9726 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9727
9728 /* Remove the discriminant field, if it exists. */
9729 struct type *sub_type = type->field (i).type ();
9730 if (sub_type->num_fields () > 0)
9731 {
9732 sub_type->set_num_fields (sub_type->num_fields () - 1);
9733 sub_type->set_fields (sub_type->fields () + 1);
9734 }
9735 TYPE_FIELD_NAME (type, i) = variant_name;
9736 sub_type->set_name
9737 (rust_fully_qualify (&objfile->objfile_obstack,
9738 type->name (), variant_name));
9739 }
9740
9741 /* Indicate that this is a variant type. */
9742 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9743 gdb::array_view<discriminant_range> (ranges,
9744 n_fields - 1));
9745 }
9746 }
9747
9748 /* Rewrite some Rust unions to be structures with variants parts. */
9749
9750 static void
9751 rust_union_quirks (struct dwarf2_cu *cu)
9752 {
9753 gdb_assert (cu->language == language_rust);
9754 for (type *type_ : cu->rust_unions)
9755 quirk_rust_enum (type_, cu->per_objfile->objfile);
9756 /* We don't need this any more. */
9757 cu->rust_unions.clear ();
9758 }
9759
9760 /* See read.h. */
9761
9762 type_unit_group_unshareable *
9763 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9764 {
9765 auto iter = this->m_type_units.find (tu_group);
9766 if (iter != this->m_type_units.end ())
9767 return iter->second.get ();
9768
9769 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9770 type_unit_group_unshareable *result = uniq.get ();
9771 this->m_type_units[tu_group] = std::move (uniq);
9772 return result;
9773 }
9774
9775 struct type *
9776 dwarf2_per_objfile::get_type_for_signatured_type
9777 (signatured_type *sig_type) const
9778 {
9779 auto iter = this->m_type_map.find (sig_type);
9780 if (iter == this->m_type_map.end ())
9781 return nullptr;
9782
9783 return iter->second;
9784 }
9785
9786 void dwarf2_per_objfile::set_type_for_signatured_type
9787 (signatured_type *sig_type, struct type *type)
9788 {
9789 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9790
9791 this->m_type_map[sig_type] = type;
9792 }
9793
9794 /* A helper function for computing the list of all symbol tables
9795 included by PER_CU. */
9796
9797 static void
9798 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9799 htab_t all_children, htab_t all_type_symtabs,
9800 dwarf2_per_cu_data *per_cu,
9801 dwarf2_per_objfile *per_objfile,
9802 struct compunit_symtab *immediate_parent)
9803 {
9804 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9805 if (*slot != NULL)
9806 {
9807 /* This inclusion and its children have been processed. */
9808 return;
9809 }
9810
9811 *slot = per_cu;
9812
9813 /* Only add a CU if it has a symbol table. */
9814 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9815 if (cust != NULL)
9816 {
9817 /* If this is a type unit only add its symbol table if we haven't
9818 seen it yet (type unit per_cu's can share symtabs). */
9819 if (per_cu->is_debug_types)
9820 {
9821 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9822 if (*slot == NULL)
9823 {
9824 *slot = cust;
9825 result->push_back (cust);
9826 if (cust->user == NULL)
9827 cust->user = immediate_parent;
9828 }
9829 }
9830 else
9831 {
9832 result->push_back (cust);
9833 if (cust->user == NULL)
9834 cust->user = immediate_parent;
9835 }
9836 }
9837
9838 if (!per_cu->imported_symtabs_empty ())
9839 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9840 {
9841 recursively_compute_inclusions (result, all_children,
9842 all_type_symtabs, ptr, per_objfile,
9843 cust);
9844 }
9845 }
9846
9847 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9848 PER_CU. */
9849
9850 static void
9851 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9852 dwarf2_per_objfile *per_objfile)
9853 {
9854 gdb_assert (! per_cu->is_debug_types);
9855
9856 if (!per_cu->imported_symtabs_empty ())
9857 {
9858 int len;
9859 std::vector<compunit_symtab *> result_symtabs;
9860 htab_t all_children, all_type_symtabs;
9861 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9862
9863 /* If we don't have a symtab, we can just skip this case. */
9864 if (cust == NULL)
9865 return;
9866
9867 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9868 NULL, xcalloc, xfree);
9869 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9870 NULL, xcalloc, xfree);
9871
9872 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9873 {
9874 recursively_compute_inclusions (&result_symtabs, all_children,
9875 all_type_symtabs, ptr, per_objfile,
9876 cust);
9877 }
9878
9879 /* Now we have a transitive closure of all the included symtabs. */
9880 len = result_symtabs.size ();
9881 cust->includes
9882 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9883 struct compunit_symtab *, len + 1);
9884 memcpy (cust->includes, result_symtabs.data (),
9885 len * sizeof (compunit_symtab *));
9886 cust->includes[len] = NULL;
9887
9888 htab_delete (all_children);
9889 htab_delete (all_type_symtabs);
9890 }
9891 }
9892
9893 /* Compute the 'includes' field for the symtabs of all the CUs we just
9894 read. */
9895
9896 static void
9897 process_cu_includes (dwarf2_per_objfile *per_objfile)
9898 {
9899 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
9900 {
9901 if (! iter->is_debug_types)
9902 compute_compunit_symtab_includes (iter, per_objfile);
9903 }
9904
9905 per_objfile->per_bfd->just_read_cus.clear ();
9906 }
9907
9908 /* Generate full symbol information for CU, whose DIEs have
9909 already been loaded into memory. */
9910
9911 static void
9912 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9913 {
9914 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9915 struct objfile *objfile = per_objfile->objfile;
9916 struct gdbarch *gdbarch = objfile->arch ();
9917 CORE_ADDR lowpc, highpc;
9918 struct compunit_symtab *cust;
9919 CORE_ADDR baseaddr;
9920 struct block *static_block;
9921 CORE_ADDR addr;
9922
9923 baseaddr = objfile->text_section_offset ();
9924
9925 /* Clear the list here in case something was left over. */
9926 cu->method_list.clear ();
9927
9928 cu->language = pretend_language;
9929 cu->language_defn = language_def (cu->language);
9930
9931 /* Do line number decoding in read_file_scope () */
9932 process_die (cu->dies, cu);
9933
9934 /* For now fudge the Go package. */
9935 if (cu->language == language_go)
9936 fixup_go_packaging (cu);
9937
9938 /* Now that we have processed all the DIEs in the CU, all the types
9939 should be complete, and it should now be safe to compute all of the
9940 physnames. */
9941 compute_delayed_physnames (cu);
9942
9943 if (cu->language == language_rust)
9944 rust_union_quirks (cu);
9945
9946 /* Some compilers don't define a DW_AT_high_pc attribute for the
9947 compilation unit. If the DW_AT_high_pc is missing, synthesize
9948 it, by scanning the DIE's below the compilation unit. */
9949 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9950
9951 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9952 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9953
9954 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9955 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9956 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9957 addrmap to help ensure it has an accurate map of pc values belonging to
9958 this comp unit. */
9959 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9960
9961 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9962 SECT_OFF_TEXT (objfile),
9963 0);
9964
9965 if (cust != NULL)
9966 {
9967 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9968
9969 /* Set symtab language to language from DW_AT_language. If the
9970 compilation is from a C file generated by language preprocessors, do
9971 not set the language if it was already deduced by start_subfile. */
9972 if (!(cu->language == language_c
9973 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9974 COMPUNIT_FILETABS (cust)->language = cu->language;
9975
9976 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9977 produce DW_AT_location with location lists but it can be possibly
9978 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9979 there were bugs in prologue debug info, fixed later in GCC-4.5
9980 by "unwind info for epilogues" patch (which is not directly related).
9981
9982 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9983 needed, it would be wrong due to missing DW_AT_producer there.
9984
9985 Still one can confuse GDB by using non-standard GCC compilation
9986 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9987 */
9988 if (cu->has_loclist && gcc_4_minor >= 5)
9989 cust->locations_valid = 1;
9990
9991 if (gcc_4_minor >= 5)
9992 cust->epilogue_unwind_valid = 1;
9993
9994 cust->call_site_htab = cu->call_site_htab;
9995 }
9996
9997 per_objfile->set_symtab (cu->per_cu, cust);
9998
9999 /* Push it for inclusion processing later. */
10000 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
10001
10002 /* Not needed any more. */
10003 cu->reset_builder ();
10004 }
10005
10006 /* Generate full symbol information for type unit CU, whose DIEs have
10007 already been loaded into memory. */
10008
10009 static void
10010 process_full_type_unit (dwarf2_cu *cu,
10011 enum language pretend_language)
10012 {
10013 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10014 struct objfile *objfile = per_objfile->objfile;
10015 struct compunit_symtab *cust;
10016 struct signatured_type *sig_type;
10017
10018 gdb_assert (cu->per_cu->is_debug_types);
10019 sig_type = (struct signatured_type *) cu->per_cu;
10020
10021 /* Clear the list here in case something was left over. */
10022 cu->method_list.clear ();
10023
10024 cu->language = pretend_language;
10025 cu->language_defn = language_def (cu->language);
10026
10027 /* The symbol tables are set up in read_type_unit_scope. */
10028 process_die (cu->dies, cu);
10029
10030 /* For now fudge the Go package. */
10031 if (cu->language == language_go)
10032 fixup_go_packaging (cu);
10033
10034 /* Now that we have processed all the DIEs in the CU, all the types
10035 should be complete, and it should now be safe to compute all of the
10036 physnames. */
10037 compute_delayed_physnames (cu);
10038
10039 if (cu->language == language_rust)
10040 rust_union_quirks (cu);
10041
10042 /* TUs share symbol tables.
10043 If this is the first TU to use this symtab, complete the construction
10044 of it with end_expandable_symtab. Otherwise, complete the addition of
10045 this TU's symbols to the existing symtab. */
10046 type_unit_group_unshareable *tug_unshare =
10047 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
10048 if (tug_unshare->compunit_symtab == NULL)
10049 {
10050 buildsym_compunit *builder = cu->get_builder ();
10051 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10052 tug_unshare->compunit_symtab = cust;
10053
10054 if (cust != NULL)
10055 {
10056 /* Set symtab language to language from DW_AT_language. If the
10057 compilation is from a C file generated by language preprocessors,
10058 do not set the language if it was already deduced by
10059 start_subfile. */
10060 if (!(cu->language == language_c
10061 && COMPUNIT_FILETABS (cust)->language != language_c))
10062 COMPUNIT_FILETABS (cust)->language = cu->language;
10063 }
10064 }
10065 else
10066 {
10067 cu->get_builder ()->augment_type_symtab ();
10068 cust = tug_unshare->compunit_symtab;
10069 }
10070
10071 per_objfile->set_symtab (cu->per_cu, cust);
10072
10073 /* Not needed any more. */
10074 cu->reset_builder ();
10075 }
10076
10077 /* Process an imported unit DIE. */
10078
10079 static void
10080 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10081 {
10082 struct attribute *attr;
10083
10084 /* For now we don't handle imported units in type units. */
10085 if (cu->per_cu->is_debug_types)
10086 {
10087 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10088 " supported in type units [in module %s]"),
10089 objfile_name (cu->per_objfile->objfile));
10090 }
10091
10092 attr = dwarf2_attr (die, DW_AT_import, cu);
10093 if (attr != NULL)
10094 {
10095 sect_offset sect_off = attr->get_ref_die_offset ();
10096 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10097 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10098 dwarf2_per_cu_data *per_cu
10099 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
10100
10101 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10102 into another compilation unit, at root level. Regard this as a hint,
10103 and ignore it. */
10104 if (die->parent && die->parent->parent == NULL
10105 && per_cu->unit_type == DW_UT_compile
10106 && per_cu->lang == language_cplus)
10107 return;
10108
10109 /* If necessary, add it to the queue and load its DIEs. */
10110 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
10111 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
10112
10113 cu->per_cu->imported_symtabs_push (per_cu);
10114 }
10115 }
10116
10117 /* RAII object that represents a process_die scope: i.e.,
10118 starts/finishes processing a DIE. */
10119 class process_die_scope
10120 {
10121 public:
10122 process_die_scope (die_info *die, dwarf2_cu *cu)
10123 : m_die (die), m_cu (cu)
10124 {
10125 /* We should only be processing DIEs not already in process. */
10126 gdb_assert (!m_die->in_process);
10127 m_die->in_process = true;
10128 }
10129
10130 ~process_die_scope ()
10131 {
10132 m_die->in_process = false;
10133
10134 /* If we're done processing the DIE for the CU that owns the line
10135 header, we don't need the line header anymore. */
10136 if (m_cu->line_header_die_owner == m_die)
10137 {
10138 delete m_cu->line_header;
10139 m_cu->line_header = NULL;
10140 m_cu->line_header_die_owner = NULL;
10141 }
10142 }
10143
10144 private:
10145 die_info *m_die;
10146 dwarf2_cu *m_cu;
10147 };
10148
10149 /* Process a die and its children. */
10150
10151 static void
10152 process_die (struct die_info *die, struct dwarf2_cu *cu)
10153 {
10154 process_die_scope scope (die, cu);
10155
10156 switch (die->tag)
10157 {
10158 case DW_TAG_padding:
10159 break;
10160 case DW_TAG_compile_unit:
10161 case DW_TAG_partial_unit:
10162 read_file_scope (die, cu);
10163 break;
10164 case DW_TAG_type_unit:
10165 read_type_unit_scope (die, cu);
10166 break;
10167 case DW_TAG_subprogram:
10168 /* Nested subprograms in Fortran get a prefix. */
10169 if (cu->language == language_fortran
10170 && die->parent != NULL
10171 && die->parent->tag == DW_TAG_subprogram)
10172 cu->processing_has_namespace_info = true;
10173 /* Fall through. */
10174 case DW_TAG_inlined_subroutine:
10175 read_func_scope (die, cu);
10176 break;
10177 case DW_TAG_lexical_block:
10178 case DW_TAG_try_block:
10179 case DW_TAG_catch_block:
10180 read_lexical_block_scope (die, cu);
10181 break;
10182 case DW_TAG_call_site:
10183 case DW_TAG_GNU_call_site:
10184 read_call_site_scope (die, cu);
10185 break;
10186 case DW_TAG_class_type:
10187 case DW_TAG_interface_type:
10188 case DW_TAG_structure_type:
10189 case DW_TAG_union_type:
10190 process_structure_scope (die, cu);
10191 break;
10192 case DW_TAG_enumeration_type:
10193 process_enumeration_scope (die, cu);
10194 break;
10195
10196 /* These dies have a type, but processing them does not create
10197 a symbol or recurse to process the children. Therefore we can
10198 read them on-demand through read_type_die. */
10199 case DW_TAG_subroutine_type:
10200 case DW_TAG_set_type:
10201 case DW_TAG_array_type:
10202 case DW_TAG_pointer_type:
10203 case DW_TAG_ptr_to_member_type:
10204 case DW_TAG_reference_type:
10205 case DW_TAG_rvalue_reference_type:
10206 case DW_TAG_string_type:
10207 break;
10208
10209 case DW_TAG_base_type:
10210 case DW_TAG_subrange_type:
10211 case DW_TAG_typedef:
10212 /* Add a typedef symbol for the type definition, if it has a
10213 DW_AT_name. */
10214 new_symbol (die, read_type_die (die, cu), cu);
10215 break;
10216 case DW_TAG_common_block:
10217 read_common_block (die, cu);
10218 break;
10219 case DW_TAG_common_inclusion:
10220 break;
10221 case DW_TAG_namespace:
10222 cu->processing_has_namespace_info = true;
10223 read_namespace (die, cu);
10224 break;
10225 case DW_TAG_module:
10226 cu->processing_has_namespace_info = true;
10227 read_module (die, cu);
10228 break;
10229 case DW_TAG_imported_declaration:
10230 cu->processing_has_namespace_info = true;
10231 if (read_namespace_alias (die, cu))
10232 break;
10233 /* The declaration is not a global namespace alias. */
10234 /* Fall through. */
10235 case DW_TAG_imported_module:
10236 cu->processing_has_namespace_info = true;
10237 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10238 || cu->language != language_fortran))
10239 complaint (_("Tag '%s' has unexpected children"),
10240 dwarf_tag_name (die->tag));
10241 read_import_statement (die, cu);
10242 break;
10243
10244 case DW_TAG_imported_unit:
10245 process_imported_unit_die (die, cu);
10246 break;
10247
10248 case DW_TAG_variable:
10249 read_variable (die, cu);
10250 break;
10251
10252 default:
10253 new_symbol (die, NULL, cu);
10254 break;
10255 }
10256 }
10257 \f
10258 /* DWARF name computation. */
10259
10260 /* A helper function for dwarf2_compute_name which determines whether DIE
10261 needs to have the name of the scope prepended to the name listed in the
10262 die. */
10263
10264 static int
10265 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10266 {
10267 struct attribute *attr;
10268
10269 switch (die->tag)
10270 {
10271 case DW_TAG_namespace:
10272 case DW_TAG_typedef:
10273 case DW_TAG_class_type:
10274 case DW_TAG_interface_type:
10275 case DW_TAG_structure_type:
10276 case DW_TAG_union_type:
10277 case DW_TAG_enumeration_type:
10278 case DW_TAG_enumerator:
10279 case DW_TAG_subprogram:
10280 case DW_TAG_inlined_subroutine:
10281 case DW_TAG_member:
10282 case DW_TAG_imported_declaration:
10283 return 1;
10284
10285 case DW_TAG_variable:
10286 case DW_TAG_constant:
10287 /* We only need to prefix "globally" visible variables. These include
10288 any variable marked with DW_AT_external or any variable that
10289 lives in a namespace. [Variables in anonymous namespaces
10290 require prefixing, but they are not DW_AT_external.] */
10291
10292 if (dwarf2_attr (die, DW_AT_specification, cu))
10293 {
10294 struct dwarf2_cu *spec_cu = cu;
10295
10296 return die_needs_namespace (die_specification (die, &spec_cu),
10297 spec_cu);
10298 }
10299
10300 attr = dwarf2_attr (die, DW_AT_external, cu);
10301 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10302 && die->parent->tag != DW_TAG_module)
10303 return 0;
10304 /* A variable in a lexical block of some kind does not need a
10305 namespace, even though in C++ such variables may be external
10306 and have a mangled name. */
10307 if (die->parent->tag == DW_TAG_lexical_block
10308 || die->parent->tag == DW_TAG_try_block
10309 || die->parent->tag == DW_TAG_catch_block
10310 || die->parent->tag == DW_TAG_subprogram)
10311 return 0;
10312 return 1;
10313
10314 default:
10315 return 0;
10316 }
10317 }
10318
10319 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10320 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10321 defined for the given DIE. */
10322
10323 static struct attribute *
10324 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10325 {
10326 struct attribute *attr;
10327
10328 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10329 if (attr == NULL)
10330 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10331
10332 return attr;
10333 }
10334
10335 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10336 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10337 defined for the given DIE. */
10338
10339 static const char *
10340 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10341 {
10342 const char *linkage_name;
10343
10344 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10345 if (linkage_name == NULL)
10346 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10347
10348 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10349 See https://github.com/rust-lang/rust/issues/32925. */
10350 if (cu->language == language_rust && linkage_name != NULL
10351 && strchr (linkage_name, '{') != NULL)
10352 linkage_name = NULL;
10353
10354 return linkage_name;
10355 }
10356
10357 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10358 compute the physname for the object, which include a method's:
10359 - formal parameters (C++),
10360 - receiver type (Go),
10361
10362 The term "physname" is a bit confusing.
10363 For C++, for example, it is the demangled name.
10364 For Go, for example, it's the mangled name.
10365
10366 For Ada, return the DIE's linkage name rather than the fully qualified
10367 name. PHYSNAME is ignored..
10368
10369 The result is allocated on the objfile->per_bfd's obstack and
10370 canonicalized. */
10371
10372 static const char *
10373 dwarf2_compute_name (const char *name,
10374 struct die_info *die, struct dwarf2_cu *cu,
10375 int physname)
10376 {
10377 struct objfile *objfile = cu->per_objfile->objfile;
10378
10379 if (name == NULL)
10380 name = dwarf2_name (die, cu);
10381
10382 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10383 but otherwise compute it by typename_concat inside GDB.
10384 FIXME: Actually this is not really true, or at least not always true.
10385 It's all very confusing. compute_and_set_names doesn't try to demangle
10386 Fortran names because there is no mangling standard. So new_symbol
10387 will set the demangled name to the result of dwarf2_full_name, and it is
10388 the demangled name that GDB uses if it exists. */
10389 if (cu->language == language_ada
10390 || (cu->language == language_fortran && physname))
10391 {
10392 /* For Ada unit, we prefer the linkage name over the name, as
10393 the former contains the exported name, which the user expects
10394 to be able to reference. Ideally, we want the user to be able
10395 to reference this entity using either natural or linkage name,
10396 but we haven't started looking at this enhancement yet. */
10397 const char *linkage_name = dw2_linkage_name (die, cu);
10398
10399 if (linkage_name != NULL)
10400 return linkage_name;
10401 }
10402
10403 /* These are the only languages we know how to qualify names in. */
10404 if (name != NULL
10405 && (cu->language == language_cplus
10406 || cu->language == language_fortran || cu->language == language_d
10407 || cu->language == language_rust))
10408 {
10409 if (die_needs_namespace (die, cu))
10410 {
10411 const char *prefix;
10412 const char *canonical_name = NULL;
10413
10414 string_file buf;
10415
10416 prefix = determine_prefix (die, cu);
10417 if (*prefix != '\0')
10418 {
10419 gdb::unique_xmalloc_ptr<char> prefixed_name
10420 (typename_concat (NULL, prefix, name, physname, cu));
10421
10422 buf.puts (prefixed_name.get ());
10423 }
10424 else
10425 buf.puts (name);
10426
10427 /* Template parameters may be specified in the DIE's DW_AT_name, or
10428 as children with DW_TAG_template_type_param or
10429 DW_TAG_value_type_param. If the latter, add them to the name
10430 here. If the name already has template parameters, then
10431 skip this step; some versions of GCC emit both, and
10432 it is more efficient to use the pre-computed name.
10433
10434 Something to keep in mind about this process: it is very
10435 unlikely, or in some cases downright impossible, to produce
10436 something that will match the mangled name of a function.
10437 If the definition of the function has the same debug info,
10438 we should be able to match up with it anyway. But fallbacks
10439 using the minimal symbol, for instance to find a method
10440 implemented in a stripped copy of libstdc++, will not work.
10441 If we do not have debug info for the definition, we will have to
10442 match them up some other way.
10443
10444 When we do name matching there is a related problem with function
10445 templates; two instantiated function templates are allowed to
10446 differ only by their return types, which we do not add here. */
10447
10448 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10449 {
10450 struct attribute *attr;
10451 struct die_info *child;
10452 int first = 1;
10453
10454 die->building_fullname = 1;
10455
10456 for (child = die->child; child != NULL; child = child->sibling)
10457 {
10458 struct type *type;
10459 LONGEST value;
10460 const gdb_byte *bytes;
10461 struct dwarf2_locexpr_baton *baton;
10462 struct value *v;
10463
10464 if (child->tag != DW_TAG_template_type_param
10465 && child->tag != DW_TAG_template_value_param)
10466 continue;
10467
10468 if (first)
10469 {
10470 buf.puts ("<");
10471 first = 0;
10472 }
10473 else
10474 buf.puts (", ");
10475
10476 attr = dwarf2_attr (child, DW_AT_type, cu);
10477 if (attr == NULL)
10478 {
10479 complaint (_("template parameter missing DW_AT_type"));
10480 buf.puts ("UNKNOWN_TYPE");
10481 continue;
10482 }
10483 type = die_type (child, cu);
10484
10485 if (child->tag == DW_TAG_template_type_param)
10486 {
10487 c_print_type (type, "", &buf, -1, 0, cu->language,
10488 &type_print_raw_options);
10489 continue;
10490 }
10491
10492 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10493 if (attr == NULL)
10494 {
10495 complaint (_("template parameter missing "
10496 "DW_AT_const_value"));
10497 buf.puts ("UNKNOWN_VALUE");
10498 continue;
10499 }
10500
10501 dwarf2_const_value_attr (attr, type, name,
10502 &cu->comp_unit_obstack, cu,
10503 &value, &bytes, &baton);
10504
10505 if (type->has_no_signedness ())
10506 /* GDB prints characters as NUMBER 'CHAR'. If that's
10507 changed, this can use value_print instead. */
10508 c_printchar (value, type, &buf);
10509 else
10510 {
10511 struct value_print_options opts;
10512
10513 if (baton != NULL)
10514 v = dwarf2_evaluate_loc_desc (type, NULL,
10515 baton->data,
10516 baton->size,
10517 baton->per_cu,
10518 baton->per_objfile);
10519 else if (bytes != NULL)
10520 {
10521 v = allocate_value (type);
10522 memcpy (value_contents_writeable (v), bytes,
10523 TYPE_LENGTH (type));
10524 }
10525 else
10526 v = value_from_longest (type, value);
10527
10528 /* Specify decimal so that we do not depend on
10529 the radix. */
10530 get_formatted_print_options (&opts, 'd');
10531 opts.raw = 1;
10532 value_print (v, &buf, &opts);
10533 release_value (v);
10534 }
10535 }
10536
10537 die->building_fullname = 0;
10538
10539 if (!first)
10540 {
10541 /* Close the argument list, with a space if necessary
10542 (nested templates). */
10543 if (!buf.empty () && buf.string ().back () == '>')
10544 buf.puts (" >");
10545 else
10546 buf.puts (">");
10547 }
10548 }
10549
10550 /* For C++ methods, append formal parameter type
10551 information, if PHYSNAME. */
10552
10553 if (physname && die->tag == DW_TAG_subprogram
10554 && cu->language == language_cplus)
10555 {
10556 struct type *type = read_type_die (die, cu);
10557
10558 c_type_print_args (type, &buf, 1, cu->language,
10559 &type_print_raw_options);
10560
10561 if (cu->language == language_cplus)
10562 {
10563 /* Assume that an artificial first parameter is
10564 "this", but do not crash if it is not. RealView
10565 marks unnamed (and thus unused) parameters as
10566 artificial; there is no way to differentiate
10567 the two cases. */
10568 if (type->num_fields () > 0
10569 && TYPE_FIELD_ARTIFICIAL (type, 0)
10570 && type->field (0).type ()->code () == TYPE_CODE_PTR
10571 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
10572 buf.puts (" const");
10573 }
10574 }
10575
10576 const std::string &intermediate_name = buf.string ();
10577
10578 if (cu->language == language_cplus)
10579 canonical_name
10580 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10581 objfile);
10582
10583 /* If we only computed INTERMEDIATE_NAME, or if
10584 INTERMEDIATE_NAME is already canonical, then we need to
10585 intern it. */
10586 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10587 name = objfile->intern (intermediate_name);
10588 else
10589 name = canonical_name;
10590 }
10591 }
10592
10593 return name;
10594 }
10595
10596 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10597 If scope qualifiers are appropriate they will be added. The result
10598 will be allocated on the storage_obstack, or NULL if the DIE does
10599 not have a name. NAME may either be from a previous call to
10600 dwarf2_name or NULL.
10601
10602 The output string will be canonicalized (if C++). */
10603
10604 static const char *
10605 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10606 {
10607 return dwarf2_compute_name (name, die, cu, 0);
10608 }
10609
10610 /* Construct a physname for the given DIE in CU. NAME may either be
10611 from a previous call to dwarf2_name or NULL. The result will be
10612 allocated on the objfile_objstack or NULL if the DIE does not have a
10613 name.
10614
10615 The output string will be canonicalized (if C++). */
10616
10617 static const char *
10618 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10619 {
10620 struct objfile *objfile = cu->per_objfile->objfile;
10621 const char *retval, *mangled = NULL, *canon = NULL;
10622 int need_copy = 1;
10623
10624 /* In this case dwarf2_compute_name is just a shortcut not building anything
10625 on its own. */
10626 if (!die_needs_namespace (die, cu))
10627 return dwarf2_compute_name (name, die, cu, 1);
10628
10629 if (cu->language != language_rust)
10630 mangled = dw2_linkage_name (die, cu);
10631
10632 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10633 has computed. */
10634 gdb::unique_xmalloc_ptr<char> demangled;
10635 if (mangled != NULL)
10636 {
10637
10638 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10639 {
10640 /* Do nothing (do not demangle the symbol name). */
10641 }
10642 else if (cu->language == language_go)
10643 {
10644 /* This is a lie, but we already lie to the caller new_symbol.
10645 new_symbol assumes we return the mangled name.
10646 This just undoes that lie until things are cleaned up. */
10647 }
10648 else
10649 {
10650 /* Use DMGL_RET_DROP for C++ template functions to suppress
10651 their return type. It is easier for GDB users to search
10652 for such functions as `name(params)' than `long name(params)'.
10653 In such case the minimal symbol names do not match the full
10654 symbol names but for template functions there is never a need
10655 to look up their definition from their declaration so
10656 the only disadvantage remains the minimal symbol variant
10657 `long name(params)' does not have the proper inferior type. */
10658 demangled.reset (gdb_demangle (mangled,
10659 (DMGL_PARAMS | DMGL_ANSI
10660 | DMGL_RET_DROP)));
10661 }
10662 if (demangled)
10663 canon = demangled.get ();
10664 else
10665 {
10666 canon = mangled;
10667 need_copy = 0;
10668 }
10669 }
10670
10671 if (canon == NULL || check_physname)
10672 {
10673 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10674
10675 if (canon != NULL && strcmp (physname, canon) != 0)
10676 {
10677 /* It may not mean a bug in GDB. The compiler could also
10678 compute DW_AT_linkage_name incorrectly. But in such case
10679 GDB would need to be bug-to-bug compatible. */
10680
10681 complaint (_("Computed physname <%s> does not match demangled <%s> "
10682 "(from linkage <%s>) - DIE at %s [in module %s]"),
10683 physname, canon, mangled, sect_offset_str (die->sect_off),
10684 objfile_name (objfile));
10685
10686 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10687 is available here - over computed PHYSNAME. It is safer
10688 against both buggy GDB and buggy compilers. */
10689
10690 retval = canon;
10691 }
10692 else
10693 {
10694 retval = physname;
10695 need_copy = 0;
10696 }
10697 }
10698 else
10699 retval = canon;
10700
10701 if (need_copy)
10702 retval = objfile->intern (retval);
10703
10704 return retval;
10705 }
10706
10707 /* Inspect DIE in CU for a namespace alias. If one exists, record
10708 a new symbol for it.
10709
10710 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10711
10712 static int
10713 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10714 {
10715 struct attribute *attr;
10716
10717 /* If the die does not have a name, this is not a namespace
10718 alias. */
10719 attr = dwarf2_attr (die, DW_AT_name, cu);
10720 if (attr != NULL)
10721 {
10722 int num;
10723 struct die_info *d = die;
10724 struct dwarf2_cu *imported_cu = cu;
10725
10726 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10727 keep inspecting DIEs until we hit the underlying import. */
10728 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10729 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10730 {
10731 attr = dwarf2_attr (d, DW_AT_import, cu);
10732 if (attr == NULL)
10733 break;
10734
10735 d = follow_die_ref (d, attr, &imported_cu);
10736 if (d->tag != DW_TAG_imported_declaration)
10737 break;
10738 }
10739
10740 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10741 {
10742 complaint (_("DIE at %s has too many recursively imported "
10743 "declarations"), sect_offset_str (d->sect_off));
10744 return 0;
10745 }
10746
10747 if (attr != NULL)
10748 {
10749 struct type *type;
10750 sect_offset sect_off = attr->get_ref_die_offset ();
10751
10752 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10753 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10754 {
10755 /* This declaration is a global namespace alias. Add
10756 a symbol for it whose type is the aliased namespace. */
10757 new_symbol (die, type, cu);
10758 return 1;
10759 }
10760 }
10761 }
10762
10763 return 0;
10764 }
10765
10766 /* Return the using directives repository (global or local?) to use in the
10767 current context for CU.
10768
10769 For Ada, imported declarations can materialize renamings, which *may* be
10770 global. However it is impossible (for now?) in DWARF to distinguish
10771 "external" imported declarations and "static" ones. As all imported
10772 declarations seem to be static in all other languages, make them all CU-wide
10773 global only in Ada. */
10774
10775 static struct using_direct **
10776 using_directives (struct dwarf2_cu *cu)
10777 {
10778 if (cu->language == language_ada
10779 && cu->get_builder ()->outermost_context_p ())
10780 return cu->get_builder ()->get_global_using_directives ();
10781 else
10782 return cu->get_builder ()->get_local_using_directives ();
10783 }
10784
10785 /* Read the import statement specified by the given die and record it. */
10786
10787 static void
10788 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10789 {
10790 struct objfile *objfile = cu->per_objfile->objfile;
10791 struct attribute *import_attr;
10792 struct die_info *imported_die, *child_die;
10793 struct dwarf2_cu *imported_cu;
10794 const char *imported_name;
10795 const char *imported_name_prefix;
10796 const char *canonical_name;
10797 const char *import_alias;
10798 const char *imported_declaration = NULL;
10799 const char *import_prefix;
10800 std::vector<const char *> excludes;
10801
10802 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10803 if (import_attr == NULL)
10804 {
10805 complaint (_("Tag '%s' has no DW_AT_import"),
10806 dwarf_tag_name (die->tag));
10807 return;
10808 }
10809
10810 imported_cu = cu;
10811 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10812 imported_name = dwarf2_name (imported_die, imported_cu);
10813 if (imported_name == NULL)
10814 {
10815 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10816
10817 The import in the following code:
10818 namespace A
10819 {
10820 typedef int B;
10821 }
10822
10823 int main ()
10824 {
10825 using A::B;
10826 B b;
10827 return b;
10828 }
10829
10830 ...
10831 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10832 <52> DW_AT_decl_file : 1
10833 <53> DW_AT_decl_line : 6
10834 <54> DW_AT_import : <0x75>
10835 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10836 <59> DW_AT_name : B
10837 <5b> DW_AT_decl_file : 1
10838 <5c> DW_AT_decl_line : 2
10839 <5d> DW_AT_type : <0x6e>
10840 ...
10841 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10842 <76> DW_AT_byte_size : 4
10843 <77> DW_AT_encoding : 5 (signed)
10844
10845 imports the wrong die ( 0x75 instead of 0x58 ).
10846 This case will be ignored until the gcc bug is fixed. */
10847 return;
10848 }
10849
10850 /* Figure out the local name after import. */
10851 import_alias = dwarf2_name (die, cu);
10852
10853 /* Figure out where the statement is being imported to. */
10854 import_prefix = determine_prefix (die, cu);
10855
10856 /* Figure out what the scope of the imported die is and prepend it
10857 to the name of the imported die. */
10858 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10859
10860 if (imported_die->tag != DW_TAG_namespace
10861 && imported_die->tag != DW_TAG_module)
10862 {
10863 imported_declaration = imported_name;
10864 canonical_name = imported_name_prefix;
10865 }
10866 else if (strlen (imported_name_prefix) > 0)
10867 canonical_name = obconcat (&objfile->objfile_obstack,
10868 imported_name_prefix,
10869 (cu->language == language_d ? "." : "::"),
10870 imported_name, (char *) NULL);
10871 else
10872 canonical_name = imported_name;
10873
10874 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10875 for (child_die = die->child; child_die && child_die->tag;
10876 child_die = child_die->sibling)
10877 {
10878 /* DWARF-4: A Fortran use statement with a “rename list” may be
10879 represented by an imported module entry with an import attribute
10880 referring to the module and owned entries corresponding to those
10881 entities that are renamed as part of being imported. */
10882
10883 if (child_die->tag != DW_TAG_imported_declaration)
10884 {
10885 complaint (_("child DW_TAG_imported_declaration expected "
10886 "- DIE at %s [in module %s]"),
10887 sect_offset_str (child_die->sect_off),
10888 objfile_name (objfile));
10889 continue;
10890 }
10891
10892 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10893 if (import_attr == NULL)
10894 {
10895 complaint (_("Tag '%s' has no DW_AT_import"),
10896 dwarf_tag_name (child_die->tag));
10897 continue;
10898 }
10899
10900 imported_cu = cu;
10901 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10902 &imported_cu);
10903 imported_name = dwarf2_name (imported_die, imported_cu);
10904 if (imported_name == NULL)
10905 {
10906 complaint (_("child DW_TAG_imported_declaration has unknown "
10907 "imported name - DIE at %s [in module %s]"),
10908 sect_offset_str (child_die->sect_off),
10909 objfile_name (objfile));
10910 continue;
10911 }
10912
10913 excludes.push_back (imported_name);
10914
10915 process_die (child_die, cu);
10916 }
10917
10918 add_using_directive (using_directives (cu),
10919 import_prefix,
10920 canonical_name,
10921 import_alias,
10922 imported_declaration,
10923 excludes,
10924 0,
10925 &objfile->objfile_obstack);
10926 }
10927
10928 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10929 types, but gives them a size of zero. Starting with version 14,
10930 ICC is compatible with GCC. */
10931
10932 static bool
10933 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10934 {
10935 if (!cu->checked_producer)
10936 check_producer (cu);
10937
10938 return cu->producer_is_icc_lt_14;
10939 }
10940
10941 /* ICC generates a DW_AT_type for C void functions. This was observed on
10942 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10943 which says that void functions should not have a DW_AT_type. */
10944
10945 static bool
10946 producer_is_icc (struct dwarf2_cu *cu)
10947 {
10948 if (!cu->checked_producer)
10949 check_producer (cu);
10950
10951 return cu->producer_is_icc;
10952 }
10953
10954 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10955 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10956 this, it was first present in GCC release 4.3.0. */
10957
10958 static bool
10959 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10960 {
10961 if (!cu->checked_producer)
10962 check_producer (cu);
10963
10964 return cu->producer_is_gcc_lt_4_3;
10965 }
10966
10967 static file_and_directory
10968 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10969 {
10970 file_and_directory res;
10971
10972 /* Find the filename. Do not use dwarf2_name here, since the filename
10973 is not a source language identifier. */
10974 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10975 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10976
10977 if (res.comp_dir == NULL
10978 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10979 && IS_ABSOLUTE_PATH (res.name))
10980 {
10981 res.comp_dir_storage = ldirname (res.name);
10982 if (!res.comp_dir_storage.empty ())
10983 res.comp_dir = res.comp_dir_storage.c_str ();
10984 }
10985 if (res.comp_dir != NULL)
10986 {
10987 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10988 directory, get rid of it. */
10989 const char *cp = strchr (res.comp_dir, ':');
10990
10991 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10992 res.comp_dir = cp + 1;
10993 }
10994
10995 if (res.name == NULL)
10996 res.name = "<unknown>";
10997
10998 return res;
10999 }
11000
11001 /* Handle DW_AT_stmt_list for a compilation unit.
11002 DIE is the DW_TAG_compile_unit die for CU.
11003 COMP_DIR is the compilation directory. LOWPC is passed to
11004 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11005
11006 static void
11007 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11008 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11009 {
11010 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11011 struct attribute *attr;
11012 struct line_header line_header_local;
11013 hashval_t line_header_local_hash;
11014 void **slot;
11015 int decode_mapping;
11016
11017 gdb_assert (! cu->per_cu->is_debug_types);
11018
11019 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11020 if (attr == NULL)
11021 return;
11022
11023 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11024
11025 /* The line header hash table is only created if needed (it exists to
11026 prevent redundant reading of the line table for partial_units).
11027 If we're given a partial_unit, we'll need it. If we're given a
11028 compile_unit, then use the line header hash table if it's already
11029 created, but don't create one just yet. */
11030
11031 if (per_objfile->line_header_hash == NULL
11032 && die->tag == DW_TAG_partial_unit)
11033 {
11034 per_objfile->line_header_hash
11035 .reset (htab_create_alloc (127, line_header_hash_voidp,
11036 line_header_eq_voidp,
11037 free_line_header_voidp,
11038 xcalloc, xfree));
11039 }
11040
11041 line_header_local.sect_off = line_offset;
11042 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11043 line_header_local_hash = line_header_hash (&line_header_local);
11044 if (per_objfile->line_header_hash != NULL)
11045 {
11046 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11047 &line_header_local,
11048 line_header_local_hash, NO_INSERT);
11049
11050 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11051 is not present in *SLOT (since if there is something in *SLOT then
11052 it will be for a partial_unit). */
11053 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11054 {
11055 gdb_assert (*slot != NULL);
11056 cu->line_header = (struct line_header *) *slot;
11057 return;
11058 }
11059 }
11060
11061 /* dwarf_decode_line_header does not yet provide sufficient information.
11062 We always have to call also dwarf_decode_lines for it. */
11063 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11064 if (lh == NULL)
11065 return;
11066
11067 cu->line_header = lh.release ();
11068 cu->line_header_die_owner = die;
11069
11070 if (per_objfile->line_header_hash == NULL)
11071 slot = NULL;
11072 else
11073 {
11074 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11075 &line_header_local,
11076 line_header_local_hash, INSERT);
11077 gdb_assert (slot != NULL);
11078 }
11079 if (slot != NULL && *slot == NULL)
11080 {
11081 /* This newly decoded line number information unit will be owned
11082 by line_header_hash hash table. */
11083 *slot = cu->line_header;
11084 cu->line_header_die_owner = NULL;
11085 }
11086 else
11087 {
11088 /* We cannot free any current entry in (*slot) as that struct line_header
11089 may be already used by multiple CUs. Create only temporary decoded
11090 line_header for this CU - it may happen at most once for each line
11091 number information unit. And if we're not using line_header_hash
11092 then this is what we want as well. */
11093 gdb_assert (die->tag != DW_TAG_partial_unit);
11094 }
11095 decode_mapping = (die->tag != DW_TAG_partial_unit);
11096 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11097 decode_mapping);
11098
11099 }
11100
11101 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11102
11103 static void
11104 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11105 {
11106 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11107 struct objfile *objfile = per_objfile->objfile;
11108 struct gdbarch *gdbarch = objfile->arch ();
11109 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11110 CORE_ADDR highpc = ((CORE_ADDR) 0);
11111 struct attribute *attr;
11112 struct die_info *child_die;
11113 CORE_ADDR baseaddr;
11114
11115 prepare_one_comp_unit (cu, die, cu->language);
11116 baseaddr = objfile->text_section_offset ();
11117
11118 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11119
11120 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11121 from finish_block. */
11122 if (lowpc == ((CORE_ADDR) -1))
11123 lowpc = highpc;
11124 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11125
11126 file_and_directory fnd = find_file_and_directory (die, cu);
11127
11128 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11129 standardised yet. As a workaround for the language detection we fall
11130 back to the DW_AT_producer string. */
11131 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11132 cu->language = language_opencl;
11133
11134 /* Similar hack for Go. */
11135 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11136 set_cu_language (DW_LANG_Go, cu);
11137
11138 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11139
11140 /* Decode line number information if present. We do this before
11141 processing child DIEs, so that the line header table is available
11142 for DW_AT_decl_file. */
11143 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11144
11145 /* Process all dies in compilation unit. */
11146 if (die->child != NULL)
11147 {
11148 child_die = die->child;
11149 while (child_die && child_die->tag)
11150 {
11151 process_die (child_die, cu);
11152 child_die = child_die->sibling;
11153 }
11154 }
11155
11156 /* Decode macro information, if present. Dwarf 2 macro information
11157 refers to information in the line number info statement program
11158 header, so we can only read it if we've read the header
11159 successfully. */
11160 attr = dwarf2_attr (die, DW_AT_macros, cu);
11161 if (attr == NULL)
11162 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11163 if (attr && cu->line_header)
11164 {
11165 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11166 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11167
11168 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11169 }
11170 else
11171 {
11172 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11173 if (attr && cu->line_header)
11174 {
11175 unsigned int macro_offset = DW_UNSND (attr);
11176
11177 dwarf_decode_macros (cu, macro_offset, 0);
11178 }
11179 }
11180 }
11181
11182 void
11183 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11184 {
11185 struct type_unit_group *tu_group;
11186 int first_time;
11187 struct attribute *attr;
11188 unsigned int i;
11189 struct signatured_type *sig_type;
11190
11191 gdb_assert (per_cu->is_debug_types);
11192 sig_type = (struct signatured_type *) per_cu;
11193
11194 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11195
11196 /* If we're using .gdb_index (includes -readnow) then
11197 per_cu->type_unit_group may not have been set up yet. */
11198 if (sig_type->type_unit_group == NULL)
11199 sig_type->type_unit_group = get_type_unit_group (this, attr);
11200 tu_group = sig_type->type_unit_group;
11201
11202 /* If we've already processed this stmt_list there's no real need to
11203 do it again, we could fake it and just recreate the part we need
11204 (file name,index -> symtab mapping). If data shows this optimization
11205 is useful we can do it then. */
11206 type_unit_group_unshareable *tug_unshare
11207 = per_objfile->get_type_unit_group_unshareable (tu_group);
11208 first_time = tug_unshare->compunit_symtab == NULL;
11209
11210 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11211 debug info. */
11212 line_header_up lh;
11213 if (attr != NULL)
11214 {
11215 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11216 lh = dwarf_decode_line_header (line_offset, this);
11217 }
11218 if (lh == NULL)
11219 {
11220 if (first_time)
11221 start_symtab ("", NULL, 0);
11222 else
11223 {
11224 gdb_assert (tug_unshare->symtabs == NULL);
11225 gdb_assert (m_builder == nullptr);
11226 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11227 m_builder.reset (new struct buildsym_compunit
11228 (COMPUNIT_OBJFILE (cust), "",
11229 COMPUNIT_DIRNAME (cust),
11230 compunit_language (cust),
11231 0, cust));
11232 list_in_scope = get_builder ()->get_file_symbols ();
11233 }
11234 return;
11235 }
11236
11237 line_header = lh.release ();
11238 line_header_die_owner = die;
11239
11240 if (first_time)
11241 {
11242 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11243
11244 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11245 still initializing it, and our caller (a few levels up)
11246 process_full_type_unit still needs to know if this is the first
11247 time. */
11248
11249 tug_unshare->symtabs
11250 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11251 struct symtab *, line_header->file_names_size ());
11252
11253 auto &file_names = line_header->file_names ();
11254 for (i = 0; i < file_names.size (); ++i)
11255 {
11256 file_entry &fe = file_names[i];
11257 dwarf2_start_subfile (this, fe.name,
11258 fe.include_dir (line_header));
11259 buildsym_compunit *b = get_builder ();
11260 if (b->get_current_subfile ()->symtab == NULL)
11261 {
11262 /* NOTE: start_subfile will recognize when it's been
11263 passed a file it has already seen. So we can't
11264 assume there's a simple mapping from
11265 cu->line_header->file_names to subfiles, plus
11266 cu->line_header->file_names may contain dups. */
11267 b->get_current_subfile ()->symtab
11268 = allocate_symtab (cust, b->get_current_subfile ()->name);
11269 }
11270
11271 fe.symtab = b->get_current_subfile ()->symtab;
11272 tug_unshare->symtabs[i] = fe.symtab;
11273 }
11274 }
11275 else
11276 {
11277 gdb_assert (m_builder == nullptr);
11278 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11279 m_builder.reset (new struct buildsym_compunit
11280 (COMPUNIT_OBJFILE (cust), "",
11281 COMPUNIT_DIRNAME (cust),
11282 compunit_language (cust),
11283 0, cust));
11284 list_in_scope = get_builder ()->get_file_symbols ();
11285
11286 auto &file_names = line_header->file_names ();
11287 for (i = 0; i < file_names.size (); ++i)
11288 {
11289 file_entry &fe = file_names[i];
11290 fe.symtab = tug_unshare->symtabs[i];
11291 }
11292 }
11293
11294 /* The main symtab is allocated last. Type units don't have DW_AT_name
11295 so they don't have a "real" (so to speak) symtab anyway.
11296 There is later code that will assign the main symtab to all symbols
11297 that don't have one. We need to handle the case of a symbol with a
11298 missing symtab (DW_AT_decl_file) anyway. */
11299 }
11300
11301 /* Process DW_TAG_type_unit.
11302 For TUs we want to skip the first top level sibling if it's not the
11303 actual type being defined by this TU. In this case the first top
11304 level sibling is there to provide context only. */
11305
11306 static void
11307 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11308 {
11309 struct die_info *child_die;
11310
11311 prepare_one_comp_unit (cu, die, language_minimal);
11312
11313 /* Initialize (or reinitialize) the machinery for building symtabs.
11314 We do this before processing child DIEs, so that the line header table
11315 is available for DW_AT_decl_file. */
11316 cu->setup_type_unit_groups (die);
11317
11318 if (die->child != NULL)
11319 {
11320 child_die = die->child;
11321 while (child_die && child_die->tag)
11322 {
11323 process_die (child_die, cu);
11324 child_die = child_die->sibling;
11325 }
11326 }
11327 }
11328 \f
11329 /* DWO/DWP files.
11330
11331 http://gcc.gnu.org/wiki/DebugFission
11332 http://gcc.gnu.org/wiki/DebugFissionDWP
11333
11334 To simplify handling of both DWO files ("object" files with the DWARF info)
11335 and DWP files (a file with the DWOs packaged up into one file), we treat
11336 DWP files as having a collection of virtual DWO files. */
11337
11338 static hashval_t
11339 hash_dwo_file (const void *item)
11340 {
11341 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11342 hashval_t hash;
11343
11344 hash = htab_hash_string (dwo_file->dwo_name);
11345 if (dwo_file->comp_dir != NULL)
11346 hash += htab_hash_string (dwo_file->comp_dir);
11347 return hash;
11348 }
11349
11350 static int
11351 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11352 {
11353 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11354 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11355
11356 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11357 return 0;
11358 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11359 return lhs->comp_dir == rhs->comp_dir;
11360 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11361 }
11362
11363 /* Allocate a hash table for DWO files. */
11364
11365 static htab_up
11366 allocate_dwo_file_hash_table ()
11367 {
11368 auto delete_dwo_file = [] (void *item)
11369 {
11370 struct dwo_file *dwo_file = (struct dwo_file *) item;
11371
11372 delete dwo_file;
11373 };
11374
11375 return htab_up (htab_create_alloc (41,
11376 hash_dwo_file,
11377 eq_dwo_file,
11378 delete_dwo_file,
11379 xcalloc, xfree));
11380 }
11381
11382 /* Lookup DWO file DWO_NAME. */
11383
11384 static void **
11385 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
11386 const char *dwo_name,
11387 const char *comp_dir)
11388 {
11389 struct dwo_file find_entry;
11390 void **slot;
11391
11392 if (per_objfile->per_bfd->dwo_files == NULL)
11393 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11394
11395 find_entry.dwo_name = dwo_name;
11396 find_entry.comp_dir = comp_dir;
11397 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
11398 INSERT);
11399
11400 return slot;
11401 }
11402
11403 static hashval_t
11404 hash_dwo_unit (const void *item)
11405 {
11406 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11407
11408 /* This drops the top 32 bits of the id, but is ok for a hash. */
11409 return dwo_unit->signature;
11410 }
11411
11412 static int
11413 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11414 {
11415 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11416 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11417
11418 /* The signature is assumed to be unique within the DWO file.
11419 So while object file CU dwo_id's always have the value zero,
11420 that's OK, assuming each object file DWO file has only one CU,
11421 and that's the rule for now. */
11422 return lhs->signature == rhs->signature;
11423 }
11424
11425 /* Allocate a hash table for DWO CUs,TUs.
11426 There is one of these tables for each of CUs,TUs for each DWO file. */
11427
11428 static htab_up
11429 allocate_dwo_unit_table ()
11430 {
11431 /* Start out with a pretty small number.
11432 Generally DWO files contain only one CU and maybe some TUs. */
11433 return htab_up (htab_create_alloc (3,
11434 hash_dwo_unit,
11435 eq_dwo_unit,
11436 NULL, xcalloc, xfree));
11437 }
11438
11439 /* die_reader_func for create_dwo_cu. */
11440
11441 static void
11442 create_dwo_cu_reader (const struct die_reader_specs *reader,
11443 const gdb_byte *info_ptr,
11444 struct die_info *comp_unit_die,
11445 struct dwo_file *dwo_file,
11446 struct dwo_unit *dwo_unit)
11447 {
11448 struct dwarf2_cu *cu = reader->cu;
11449 sect_offset sect_off = cu->per_cu->sect_off;
11450 struct dwarf2_section_info *section = cu->per_cu->section;
11451
11452 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11453 if (!signature.has_value ())
11454 {
11455 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11456 " its dwo_id [in module %s]"),
11457 sect_offset_str (sect_off), dwo_file->dwo_name);
11458 return;
11459 }
11460
11461 dwo_unit->dwo_file = dwo_file;
11462 dwo_unit->signature = *signature;
11463 dwo_unit->section = section;
11464 dwo_unit->sect_off = sect_off;
11465 dwo_unit->length = cu->per_cu->length;
11466
11467 if (dwarf_read_debug)
11468 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11469 sect_offset_str (sect_off),
11470 hex_string (dwo_unit->signature));
11471 }
11472
11473 /* Create the dwo_units for the CUs in a DWO_FILE.
11474 Note: This function processes DWO files only, not DWP files. */
11475
11476 static void
11477 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
11478 dwarf2_cu *cu, struct dwo_file &dwo_file,
11479 dwarf2_section_info &section, htab_up &cus_htab)
11480 {
11481 struct objfile *objfile = per_objfile->objfile;
11482 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
11483 const gdb_byte *info_ptr, *end_ptr;
11484
11485 section.read (objfile);
11486 info_ptr = section.buffer;
11487
11488 if (info_ptr == NULL)
11489 return;
11490
11491 if (dwarf_read_debug)
11492 {
11493 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11494 section.get_name (),
11495 section.get_file_name ());
11496 }
11497
11498 end_ptr = info_ptr + section.size;
11499 while (info_ptr < end_ptr)
11500 {
11501 struct dwarf2_per_cu_data per_cu;
11502 struct dwo_unit read_unit {};
11503 struct dwo_unit *dwo_unit;
11504 void **slot;
11505 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11506
11507 memset (&per_cu, 0, sizeof (per_cu));
11508 per_cu.per_bfd = per_bfd;
11509 per_cu.is_debug_types = 0;
11510 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11511 per_cu.section = &section;
11512
11513 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
11514 if (!reader.dummy_p)
11515 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11516 &dwo_file, &read_unit);
11517 info_ptr += per_cu.length;
11518
11519 // If the unit could not be parsed, skip it.
11520 if (read_unit.dwo_file == NULL)
11521 continue;
11522
11523 if (cus_htab == NULL)
11524 cus_htab = allocate_dwo_unit_table ();
11525
11526 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11527 struct dwo_unit);
11528 *dwo_unit = read_unit;
11529 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11530 gdb_assert (slot != NULL);
11531 if (*slot != NULL)
11532 {
11533 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11534 sect_offset dup_sect_off = dup_cu->sect_off;
11535
11536 complaint (_("debug cu entry at offset %s is duplicate to"
11537 " the entry at offset %s, signature %s"),
11538 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11539 hex_string (dwo_unit->signature));
11540 }
11541 *slot = (void *)dwo_unit;
11542 }
11543 }
11544
11545 /* DWP file .debug_{cu,tu}_index section format:
11546 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11547 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11548
11549 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11550 officially standard DWP format was published with DWARF v5 and is called
11551 Version 5. There are no versions 3 or 4.
11552
11553 DWP Version 1:
11554
11555 Both index sections have the same format, and serve to map a 64-bit
11556 signature to a set of section numbers. Each section begins with a header,
11557 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11558 indexes, and a pool of 32-bit section numbers. The index sections will be
11559 aligned at 8-byte boundaries in the file.
11560
11561 The index section header consists of:
11562
11563 V, 32 bit version number
11564 -, 32 bits unused
11565 N, 32 bit number of compilation units or type units in the index
11566 M, 32 bit number of slots in the hash table
11567
11568 Numbers are recorded using the byte order of the application binary.
11569
11570 The hash table begins at offset 16 in the section, and consists of an array
11571 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11572 order of the application binary). Unused slots in the hash table are 0.
11573 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11574
11575 The parallel table begins immediately after the hash table
11576 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11577 array of 32-bit indexes (using the byte order of the application binary),
11578 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11579 table contains a 32-bit index into the pool of section numbers. For unused
11580 hash table slots, the corresponding entry in the parallel table will be 0.
11581
11582 The pool of section numbers begins immediately following the hash table
11583 (at offset 16 + 12 * M from the beginning of the section). The pool of
11584 section numbers consists of an array of 32-bit words (using the byte order
11585 of the application binary). Each item in the array is indexed starting
11586 from 0. The hash table entry provides the index of the first section
11587 number in the set. Additional section numbers in the set follow, and the
11588 set is terminated by a 0 entry (section number 0 is not used in ELF).
11589
11590 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11591 section must be the first entry in the set, and the .debug_abbrev.dwo must
11592 be the second entry. Other members of the set may follow in any order.
11593
11594 ---
11595
11596 DWP Versions 2 and 5:
11597
11598 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
11599 and the entries in the index tables are now offsets into these sections.
11600 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11601 section.
11602
11603 Index Section Contents:
11604 Header
11605 Hash Table of Signatures dwp_hash_table.hash_table
11606 Parallel Table of Indices dwp_hash_table.unit_table
11607 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11608 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
11609
11610 The index section header consists of:
11611
11612 V, 32 bit version number
11613 L, 32 bit number of columns in the table of section offsets
11614 N, 32 bit number of compilation units or type units in the index
11615 M, 32 bit number of slots in the hash table
11616
11617 Numbers are recorded using the byte order of the application binary.
11618
11619 The hash table has the same format as version 1.
11620 The parallel table of indices has the same format as version 1,
11621 except that the entries are origin-1 indices into the table of sections
11622 offsets and the table of section sizes.
11623
11624 The table of offsets begins immediately following the parallel table
11625 (at offset 16 + 12 * M from the beginning of the section). The table is
11626 a two-dimensional array of 32-bit words (using the byte order of the
11627 application binary), with L columns and N+1 rows, in row-major order.
11628 Each row in the array is indexed starting from 0. The first row provides
11629 a key to the remaining rows: each column in this row provides an identifier
11630 for a debug section, and the offsets in the same column of subsequent rows
11631 refer to that section. The section identifiers for Version 2 are:
11632
11633 DW_SECT_INFO 1 .debug_info.dwo
11634 DW_SECT_TYPES 2 .debug_types.dwo
11635 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11636 DW_SECT_LINE 4 .debug_line.dwo
11637 DW_SECT_LOC 5 .debug_loc.dwo
11638 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11639 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11640 DW_SECT_MACRO 8 .debug_macro.dwo
11641
11642 The section identifiers for Version 5 are:
11643
11644 DW_SECT_INFO_V5 1 .debug_info.dwo
11645 DW_SECT_RESERVED_V5 2 --
11646 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11647 DW_SECT_LINE_V5 4 .debug_line.dwo
11648 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11649 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11650 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11651 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11652
11653 The offsets provided by the CU and TU index sections are the base offsets
11654 for the contributions made by each CU or TU to the corresponding section
11655 in the package file. Each CU and TU header contains an abbrev_offset
11656 field, used to find the abbreviations table for that CU or TU within the
11657 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11658 be interpreted as relative to the base offset given in the index section.
11659 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11660 should be interpreted as relative to the base offset for .debug_line.dwo,
11661 and offsets into other debug sections obtained from DWARF attributes should
11662 also be interpreted as relative to the corresponding base offset.
11663
11664 The table of sizes begins immediately following the table of offsets.
11665 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11666 with L columns and N rows, in row-major order. Each row in the array is
11667 indexed starting from 1 (row 0 is shared by the two tables).
11668
11669 ---
11670
11671 Hash table lookup is handled the same in version 1 and 2:
11672
11673 We assume that N and M will not exceed 2^32 - 1.
11674 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11675
11676 Given a 64-bit compilation unit signature or a type signature S, an entry
11677 in the hash table is located as follows:
11678
11679 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11680 the low-order k bits all set to 1.
11681
11682 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11683
11684 3) If the hash table entry at index H matches the signature, use that
11685 entry. If the hash table entry at index H is unused (all zeroes),
11686 terminate the search: the signature is not present in the table.
11687
11688 4) Let H = (H + H') modulo M. Repeat at Step 3.
11689
11690 Because M > N and H' and M are relatively prime, the search is guaranteed
11691 to stop at an unused slot or find the match. */
11692
11693 /* Create a hash table to map DWO IDs to their CU/TU entry in
11694 .debug_{info,types}.dwo in DWP_FILE.
11695 Returns NULL if there isn't one.
11696 Note: This function processes DWP files only, not DWO files. */
11697
11698 static struct dwp_hash_table *
11699 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11700 struct dwp_file *dwp_file, int is_debug_types)
11701 {
11702 struct objfile *objfile = per_objfile->objfile;
11703 bfd *dbfd = dwp_file->dbfd.get ();
11704 const gdb_byte *index_ptr, *index_end;
11705 struct dwarf2_section_info *index;
11706 uint32_t version, nr_columns, nr_units, nr_slots;
11707 struct dwp_hash_table *htab;
11708
11709 if (is_debug_types)
11710 index = &dwp_file->sections.tu_index;
11711 else
11712 index = &dwp_file->sections.cu_index;
11713
11714 if (index->empty ())
11715 return NULL;
11716 index->read (objfile);
11717
11718 index_ptr = index->buffer;
11719 index_end = index_ptr + index->size;
11720
11721 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11722 For now it's safe to just read 4 bytes (particularly as it's difficult to
11723 tell if you're dealing with Version 5 before you've read the version). */
11724 version = read_4_bytes (dbfd, index_ptr);
11725 index_ptr += 4;
11726 if (version == 2 || version == 5)
11727 nr_columns = read_4_bytes (dbfd, index_ptr);
11728 else
11729 nr_columns = 0;
11730 index_ptr += 4;
11731 nr_units = read_4_bytes (dbfd, index_ptr);
11732 index_ptr += 4;
11733 nr_slots = read_4_bytes (dbfd, index_ptr);
11734 index_ptr += 4;
11735
11736 if (version != 1 && version != 2 && version != 5)
11737 {
11738 error (_("Dwarf Error: unsupported DWP file version (%s)"
11739 " [in module %s]"),
11740 pulongest (version), dwp_file->name);
11741 }
11742 if (nr_slots != (nr_slots & -nr_slots))
11743 {
11744 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11745 " is not power of 2 [in module %s]"),
11746 pulongest (nr_slots), dwp_file->name);
11747 }
11748
11749 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11750 htab->version = version;
11751 htab->nr_columns = nr_columns;
11752 htab->nr_units = nr_units;
11753 htab->nr_slots = nr_slots;
11754 htab->hash_table = index_ptr;
11755 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11756
11757 /* Exit early if the table is empty. */
11758 if (nr_slots == 0 || nr_units == 0
11759 || (version == 2 && nr_columns == 0)
11760 || (version == 5 && nr_columns == 0))
11761 {
11762 /* All must be zero. */
11763 if (nr_slots != 0 || nr_units != 0
11764 || (version == 2 && nr_columns != 0)
11765 || (version == 5 && nr_columns != 0))
11766 {
11767 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11768 " all zero [in modules %s]"),
11769 dwp_file->name);
11770 }
11771 return htab;
11772 }
11773
11774 if (version == 1)
11775 {
11776 htab->section_pool.v1.indices =
11777 htab->unit_table + sizeof (uint32_t) * nr_slots;
11778 /* It's harder to decide whether the section is too small in v1.
11779 V1 is deprecated anyway so we punt. */
11780 }
11781 else if (version == 2)
11782 {
11783 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11784 int *ids = htab->section_pool.v2.section_ids;
11785 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11786 /* Reverse map for error checking. */
11787 int ids_seen[DW_SECT_MAX + 1];
11788 int i;
11789
11790 if (nr_columns < 2)
11791 {
11792 error (_("Dwarf Error: bad DWP hash table, too few columns"
11793 " in section table [in module %s]"),
11794 dwp_file->name);
11795 }
11796 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11797 {
11798 error (_("Dwarf Error: bad DWP hash table, too many columns"
11799 " in section table [in module %s]"),
11800 dwp_file->name);
11801 }
11802 memset (ids, 255, sizeof_ids);
11803 memset (ids_seen, 255, sizeof (ids_seen));
11804 for (i = 0; i < nr_columns; ++i)
11805 {
11806 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11807
11808 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11809 {
11810 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11811 " in section table [in module %s]"),
11812 id, dwp_file->name);
11813 }
11814 if (ids_seen[id] != -1)
11815 {
11816 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11817 " id %d in section table [in module %s]"),
11818 id, dwp_file->name);
11819 }
11820 ids_seen[id] = i;
11821 ids[i] = id;
11822 }
11823 /* Must have exactly one info or types section. */
11824 if (((ids_seen[DW_SECT_INFO] != -1)
11825 + (ids_seen[DW_SECT_TYPES] != -1))
11826 != 1)
11827 {
11828 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11829 " DWO info/types section [in module %s]"),
11830 dwp_file->name);
11831 }
11832 /* Must have an abbrev section. */
11833 if (ids_seen[DW_SECT_ABBREV] == -1)
11834 {
11835 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11836 " section [in module %s]"),
11837 dwp_file->name);
11838 }
11839 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11840 htab->section_pool.v2.sizes =
11841 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11842 * nr_units * nr_columns);
11843 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11844 * nr_units * nr_columns))
11845 > index_end)
11846 {
11847 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11848 " [in module %s]"),
11849 dwp_file->name);
11850 }
11851 }
11852 else /* version == 5 */
11853 {
11854 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11855 int *ids = htab->section_pool.v5.section_ids;
11856 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11857 /* Reverse map for error checking. */
11858 int ids_seen[DW_SECT_MAX_V5 + 1];
11859
11860 if (nr_columns < 2)
11861 {
11862 error (_("Dwarf Error: bad DWP hash table, too few columns"
11863 " in section table [in module %s]"),
11864 dwp_file->name);
11865 }
11866 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11867 {
11868 error (_("Dwarf Error: bad DWP hash table, too many columns"
11869 " in section table [in module %s]"),
11870 dwp_file->name);
11871 }
11872 memset (ids, 255, sizeof_ids);
11873 memset (ids_seen, 255, sizeof (ids_seen));
11874 for (int i = 0; i < nr_columns; ++i)
11875 {
11876 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11877
11878 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11879 {
11880 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11881 " in section table [in module %s]"),
11882 id, dwp_file->name);
11883 }
11884 if (ids_seen[id] != -1)
11885 {
11886 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11887 " id %d in section table [in module %s]"),
11888 id, dwp_file->name);
11889 }
11890 ids_seen[id] = i;
11891 ids[i] = id;
11892 }
11893 /* Must have seen an info section. */
11894 if (ids_seen[DW_SECT_INFO_V5] == -1)
11895 {
11896 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11897 " DWO info/types section [in module %s]"),
11898 dwp_file->name);
11899 }
11900 /* Must have an abbrev section. */
11901 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11902 {
11903 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11904 " section [in module %s]"),
11905 dwp_file->name);
11906 }
11907 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11908 htab->section_pool.v5.sizes
11909 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11910 * nr_units * nr_columns);
11911 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11912 * nr_units * nr_columns))
11913 > index_end)
11914 {
11915 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11916 " [in module %s]"),
11917 dwp_file->name);
11918 }
11919 }
11920
11921 return htab;
11922 }
11923
11924 /* Update SECTIONS with the data from SECTP.
11925
11926 This function is like the other "locate" section routines that are
11927 passed to bfd_map_over_sections, but in this context the sections to
11928 read comes from the DWP V1 hash table, not the full ELF section table.
11929
11930 The result is non-zero for success, or zero if an error was found. */
11931
11932 static int
11933 locate_v1_virtual_dwo_sections (asection *sectp,
11934 struct virtual_v1_dwo_sections *sections)
11935 {
11936 const struct dwop_section_names *names = &dwop_section_names;
11937
11938 if (section_is_p (sectp->name, &names->abbrev_dwo))
11939 {
11940 /* There can be only one. */
11941 if (sections->abbrev.s.section != NULL)
11942 return 0;
11943 sections->abbrev.s.section = sectp;
11944 sections->abbrev.size = bfd_section_size (sectp);
11945 }
11946 else if (section_is_p (sectp->name, &names->info_dwo)
11947 || section_is_p (sectp->name, &names->types_dwo))
11948 {
11949 /* There can be only one. */
11950 if (sections->info_or_types.s.section != NULL)
11951 return 0;
11952 sections->info_or_types.s.section = sectp;
11953 sections->info_or_types.size = bfd_section_size (sectp);
11954 }
11955 else if (section_is_p (sectp->name, &names->line_dwo))
11956 {
11957 /* There can be only one. */
11958 if (sections->line.s.section != NULL)
11959 return 0;
11960 sections->line.s.section = sectp;
11961 sections->line.size = bfd_section_size (sectp);
11962 }
11963 else if (section_is_p (sectp->name, &names->loc_dwo))
11964 {
11965 /* There can be only one. */
11966 if (sections->loc.s.section != NULL)
11967 return 0;
11968 sections->loc.s.section = sectp;
11969 sections->loc.size = bfd_section_size (sectp);
11970 }
11971 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11972 {
11973 /* There can be only one. */
11974 if (sections->macinfo.s.section != NULL)
11975 return 0;
11976 sections->macinfo.s.section = sectp;
11977 sections->macinfo.size = bfd_section_size (sectp);
11978 }
11979 else if (section_is_p (sectp->name, &names->macro_dwo))
11980 {
11981 /* There can be only one. */
11982 if (sections->macro.s.section != NULL)
11983 return 0;
11984 sections->macro.s.section = sectp;
11985 sections->macro.size = bfd_section_size (sectp);
11986 }
11987 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11988 {
11989 /* There can be only one. */
11990 if (sections->str_offsets.s.section != NULL)
11991 return 0;
11992 sections->str_offsets.s.section = sectp;
11993 sections->str_offsets.size = bfd_section_size (sectp);
11994 }
11995 else
11996 {
11997 /* No other kind of section is valid. */
11998 return 0;
11999 }
12000
12001 return 1;
12002 }
12003
12004 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12005 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12006 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12007 This is for DWP version 1 files. */
12008
12009 static struct dwo_unit *
12010 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
12011 struct dwp_file *dwp_file,
12012 uint32_t unit_index,
12013 const char *comp_dir,
12014 ULONGEST signature, int is_debug_types)
12015 {
12016 const struct dwp_hash_table *dwp_htab =
12017 is_debug_types ? dwp_file->tus : dwp_file->cus;
12018 bfd *dbfd = dwp_file->dbfd.get ();
12019 const char *kind = is_debug_types ? "TU" : "CU";
12020 struct dwo_file *dwo_file;
12021 struct dwo_unit *dwo_unit;
12022 struct virtual_v1_dwo_sections sections;
12023 void **dwo_file_slot;
12024 int i;
12025
12026 gdb_assert (dwp_file->version == 1);
12027
12028 if (dwarf_read_debug)
12029 {
12030 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12031 kind,
12032 pulongest (unit_index), hex_string (signature),
12033 dwp_file->name);
12034 }
12035
12036 /* Fetch the sections of this DWO unit.
12037 Put a limit on the number of sections we look for so that bad data
12038 doesn't cause us to loop forever. */
12039
12040 #define MAX_NR_V1_DWO_SECTIONS \
12041 (1 /* .debug_info or .debug_types */ \
12042 + 1 /* .debug_abbrev */ \
12043 + 1 /* .debug_line */ \
12044 + 1 /* .debug_loc */ \
12045 + 1 /* .debug_str_offsets */ \
12046 + 1 /* .debug_macro or .debug_macinfo */ \
12047 + 1 /* trailing zero */)
12048
12049 memset (&sections, 0, sizeof (sections));
12050
12051 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12052 {
12053 asection *sectp;
12054 uint32_t section_nr =
12055 read_4_bytes (dbfd,
12056 dwp_htab->section_pool.v1.indices
12057 + (unit_index + i) * sizeof (uint32_t));
12058
12059 if (section_nr == 0)
12060 break;
12061 if (section_nr >= dwp_file->num_sections)
12062 {
12063 error (_("Dwarf Error: bad DWP hash table, section number too large"
12064 " [in module %s]"),
12065 dwp_file->name);
12066 }
12067
12068 sectp = dwp_file->elf_sections[section_nr];
12069 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12070 {
12071 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12072 " [in module %s]"),
12073 dwp_file->name);
12074 }
12075 }
12076
12077 if (i < 2
12078 || sections.info_or_types.empty ()
12079 || sections.abbrev.empty ())
12080 {
12081 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12082 " [in module %s]"),
12083 dwp_file->name);
12084 }
12085 if (i == MAX_NR_V1_DWO_SECTIONS)
12086 {
12087 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12088 " [in module %s]"),
12089 dwp_file->name);
12090 }
12091
12092 /* It's easier for the rest of the code if we fake a struct dwo_file and
12093 have dwo_unit "live" in that. At least for now.
12094
12095 The DWP file can be made up of a random collection of CUs and TUs.
12096 However, for each CU + set of TUs that came from the same original DWO
12097 file, we can combine them back into a virtual DWO file to save space
12098 (fewer struct dwo_file objects to allocate). Remember that for really
12099 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12100
12101 std::string virtual_dwo_name =
12102 string_printf ("virtual-dwo/%d-%d-%d-%d",
12103 sections.abbrev.get_id (),
12104 sections.line.get_id (),
12105 sections.loc.get_id (),
12106 sections.str_offsets.get_id ());
12107 /* Can we use an existing virtual DWO file? */
12108 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12109 comp_dir);
12110 /* Create one if necessary. */
12111 if (*dwo_file_slot == NULL)
12112 {
12113 if (dwarf_read_debug)
12114 {
12115 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12116 virtual_dwo_name.c_str ());
12117 }
12118 dwo_file = new struct dwo_file;
12119 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12120 dwo_file->comp_dir = comp_dir;
12121 dwo_file->sections.abbrev = sections.abbrev;
12122 dwo_file->sections.line = sections.line;
12123 dwo_file->sections.loc = sections.loc;
12124 dwo_file->sections.macinfo = sections.macinfo;
12125 dwo_file->sections.macro = sections.macro;
12126 dwo_file->sections.str_offsets = sections.str_offsets;
12127 /* The "str" section is global to the entire DWP file. */
12128 dwo_file->sections.str = dwp_file->sections.str;
12129 /* The info or types section is assigned below to dwo_unit,
12130 there's no need to record it in dwo_file.
12131 Also, we can't simply record type sections in dwo_file because
12132 we record a pointer into the vector in dwo_unit. As we collect more
12133 types we'll grow the vector and eventually have to reallocate space
12134 for it, invalidating all copies of pointers into the previous
12135 contents. */
12136 *dwo_file_slot = dwo_file;
12137 }
12138 else
12139 {
12140 if (dwarf_read_debug)
12141 {
12142 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12143 virtual_dwo_name.c_str ());
12144 }
12145 dwo_file = (struct dwo_file *) *dwo_file_slot;
12146 }
12147
12148 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12149 dwo_unit->dwo_file = dwo_file;
12150 dwo_unit->signature = signature;
12151 dwo_unit->section =
12152 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12153 *dwo_unit->section = sections.info_or_types;
12154 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12155
12156 return dwo_unit;
12157 }
12158
12159 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
12160 simplify them. Given a pointer to the containing section SECTION, and
12161 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
12162 virtual section of just that piece. */
12163
12164 static struct dwarf2_section_info
12165 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
12166 struct dwarf2_section_info *section,
12167 bfd_size_type offset, bfd_size_type size)
12168 {
12169 struct dwarf2_section_info result;
12170 asection *sectp;
12171
12172 gdb_assert (section != NULL);
12173 gdb_assert (!section->is_virtual);
12174
12175 memset (&result, 0, sizeof (result));
12176 result.s.containing_section = section;
12177 result.is_virtual = true;
12178
12179 if (size == 0)
12180 return result;
12181
12182 sectp = section->get_bfd_section ();
12183
12184 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12185 bounds of the real section. This is a pretty-rare event, so just
12186 flag an error (easier) instead of a warning and trying to cope. */
12187 if (sectp == NULL
12188 || offset + size > bfd_section_size (sectp))
12189 {
12190 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
12191 " in section %s [in module %s]"),
12192 sectp ? bfd_section_name (sectp) : "<unknown>",
12193 objfile_name (per_objfile->objfile));
12194 }
12195
12196 result.virtual_offset = offset;
12197 result.size = size;
12198 return result;
12199 }
12200
12201 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12202 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12203 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12204 This is for DWP version 2 files. */
12205
12206 static struct dwo_unit *
12207 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
12208 struct dwp_file *dwp_file,
12209 uint32_t unit_index,
12210 const char *comp_dir,
12211 ULONGEST signature, int is_debug_types)
12212 {
12213 const struct dwp_hash_table *dwp_htab =
12214 is_debug_types ? dwp_file->tus : dwp_file->cus;
12215 bfd *dbfd = dwp_file->dbfd.get ();
12216 const char *kind = is_debug_types ? "TU" : "CU";
12217 struct dwo_file *dwo_file;
12218 struct dwo_unit *dwo_unit;
12219 struct virtual_v2_or_v5_dwo_sections sections;
12220 void **dwo_file_slot;
12221 int i;
12222
12223 gdb_assert (dwp_file->version == 2);
12224
12225 if (dwarf_read_debug)
12226 {
12227 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12228 kind,
12229 pulongest (unit_index), hex_string (signature),
12230 dwp_file->name);
12231 }
12232
12233 /* Fetch the section offsets of this DWO unit. */
12234
12235 memset (&sections, 0, sizeof (sections));
12236
12237 for (i = 0; i < dwp_htab->nr_columns; ++i)
12238 {
12239 uint32_t offset = read_4_bytes (dbfd,
12240 dwp_htab->section_pool.v2.offsets
12241 + (((unit_index - 1) * dwp_htab->nr_columns
12242 + i)
12243 * sizeof (uint32_t)));
12244 uint32_t size = read_4_bytes (dbfd,
12245 dwp_htab->section_pool.v2.sizes
12246 + (((unit_index - 1) * dwp_htab->nr_columns
12247 + i)
12248 * sizeof (uint32_t)));
12249
12250 switch (dwp_htab->section_pool.v2.section_ids[i])
12251 {
12252 case DW_SECT_INFO:
12253 case DW_SECT_TYPES:
12254 sections.info_or_types_offset = offset;
12255 sections.info_or_types_size = size;
12256 break;
12257 case DW_SECT_ABBREV:
12258 sections.abbrev_offset = offset;
12259 sections.abbrev_size = size;
12260 break;
12261 case DW_SECT_LINE:
12262 sections.line_offset = offset;
12263 sections.line_size = size;
12264 break;
12265 case DW_SECT_LOC:
12266 sections.loc_offset = offset;
12267 sections.loc_size = size;
12268 break;
12269 case DW_SECT_STR_OFFSETS:
12270 sections.str_offsets_offset = offset;
12271 sections.str_offsets_size = size;
12272 break;
12273 case DW_SECT_MACINFO:
12274 sections.macinfo_offset = offset;
12275 sections.macinfo_size = size;
12276 break;
12277 case DW_SECT_MACRO:
12278 sections.macro_offset = offset;
12279 sections.macro_size = size;
12280 break;
12281 }
12282 }
12283
12284 /* It's easier for the rest of the code if we fake a struct dwo_file and
12285 have dwo_unit "live" in that. At least for now.
12286
12287 The DWP file can be made up of a random collection of CUs and TUs.
12288 However, for each CU + set of TUs that came from the same original DWO
12289 file, we can combine them back into a virtual DWO file to save space
12290 (fewer struct dwo_file objects to allocate). Remember that for really
12291 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12292
12293 std::string virtual_dwo_name =
12294 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12295 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12296 (long) (sections.line_size ? sections.line_offset : 0),
12297 (long) (sections.loc_size ? sections.loc_offset : 0),
12298 (long) (sections.str_offsets_size
12299 ? sections.str_offsets_offset : 0));
12300 /* Can we use an existing virtual DWO file? */
12301 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12302 comp_dir);
12303 /* Create one if necessary. */
12304 if (*dwo_file_slot == NULL)
12305 {
12306 if (dwarf_read_debug)
12307 {
12308 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12309 virtual_dwo_name.c_str ());
12310 }
12311 dwo_file = new struct dwo_file;
12312 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12313 dwo_file->comp_dir = comp_dir;
12314 dwo_file->sections.abbrev =
12315 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12316 sections.abbrev_offset,
12317 sections.abbrev_size);
12318 dwo_file->sections.line =
12319 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12320 sections.line_offset,
12321 sections.line_size);
12322 dwo_file->sections.loc =
12323 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12324 sections.loc_offset, sections.loc_size);
12325 dwo_file->sections.macinfo =
12326 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12327 sections.macinfo_offset,
12328 sections.macinfo_size);
12329 dwo_file->sections.macro =
12330 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12331 sections.macro_offset,
12332 sections.macro_size);
12333 dwo_file->sections.str_offsets =
12334 create_dwp_v2_or_v5_section (per_objfile,
12335 &dwp_file->sections.str_offsets,
12336 sections.str_offsets_offset,
12337 sections.str_offsets_size);
12338 /* The "str" section is global to the entire DWP file. */
12339 dwo_file->sections.str = dwp_file->sections.str;
12340 /* The info or types section is assigned below to dwo_unit,
12341 there's no need to record it in dwo_file.
12342 Also, we can't simply record type sections in dwo_file because
12343 we record a pointer into the vector in dwo_unit. As we collect more
12344 types we'll grow the vector and eventually have to reallocate space
12345 for it, invalidating all copies of pointers into the previous
12346 contents. */
12347 *dwo_file_slot = dwo_file;
12348 }
12349 else
12350 {
12351 if (dwarf_read_debug)
12352 {
12353 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12354 virtual_dwo_name.c_str ());
12355 }
12356 dwo_file = (struct dwo_file *) *dwo_file_slot;
12357 }
12358
12359 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12360 dwo_unit->dwo_file = dwo_file;
12361 dwo_unit->signature = signature;
12362 dwo_unit->section =
12363 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12364 *dwo_unit->section = create_dwp_v2_or_v5_section
12365 (per_objfile,
12366 is_debug_types
12367 ? &dwp_file->sections.types
12368 : &dwp_file->sections.info,
12369 sections.info_or_types_offset,
12370 sections.info_or_types_size);
12371 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12372
12373 return dwo_unit;
12374 }
12375
12376 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12377 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12378 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12379 This is for DWP version 5 files. */
12380
12381 static struct dwo_unit *
12382 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12383 struct dwp_file *dwp_file,
12384 uint32_t unit_index,
12385 const char *comp_dir,
12386 ULONGEST signature, int is_debug_types)
12387 {
12388 const struct dwp_hash_table *dwp_htab
12389 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12390 bfd *dbfd = dwp_file->dbfd.get ();
12391 const char *kind = is_debug_types ? "TU" : "CU";
12392 struct dwo_file *dwo_file;
12393 struct dwo_unit *dwo_unit;
12394 struct virtual_v2_or_v5_dwo_sections sections {};
12395 void **dwo_file_slot;
12396
12397 gdb_assert (dwp_file->version == 5);
12398
12399 if (dwarf_read_debug)
12400 {
12401 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V5 file: %s\n",
12402 kind,
12403 pulongest (unit_index), hex_string (signature),
12404 dwp_file->name);
12405 }
12406
12407 /* Fetch the section offsets of this DWO unit. */
12408
12409 /* memset (&sections, 0, sizeof (sections)); */
12410
12411 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12412 {
12413 uint32_t offset = read_4_bytes (dbfd,
12414 dwp_htab->section_pool.v5.offsets
12415 + (((unit_index - 1)
12416 * dwp_htab->nr_columns
12417 + i)
12418 * sizeof (uint32_t)));
12419 uint32_t size = read_4_bytes (dbfd,
12420 dwp_htab->section_pool.v5.sizes
12421 + (((unit_index - 1) * dwp_htab->nr_columns
12422 + i)
12423 * sizeof (uint32_t)));
12424
12425 switch (dwp_htab->section_pool.v5.section_ids[i])
12426 {
12427 case DW_SECT_ABBREV_V5:
12428 sections.abbrev_offset = offset;
12429 sections.abbrev_size = size;
12430 break;
12431 case DW_SECT_INFO_V5:
12432 sections.info_or_types_offset = offset;
12433 sections.info_or_types_size = size;
12434 break;
12435 case DW_SECT_LINE_V5:
12436 sections.line_offset = offset;
12437 sections.line_size = size;
12438 break;
12439 case DW_SECT_LOCLISTS_V5:
12440 sections.loclists_offset = offset;
12441 sections.loclists_size = size;
12442 break;
12443 case DW_SECT_MACRO_V5:
12444 sections.macro_offset = offset;
12445 sections.macro_size = size;
12446 break;
12447 case DW_SECT_RNGLISTS_V5:
12448 sections.rnglists_offset = offset;
12449 sections.rnglists_size = size;
12450 break;
12451 case DW_SECT_STR_OFFSETS_V5:
12452 sections.str_offsets_offset = offset;
12453 sections.str_offsets_size = size;
12454 break;
12455 case DW_SECT_RESERVED_V5:
12456 default:
12457 break;
12458 }
12459 }
12460
12461 /* It's easier for the rest of the code if we fake a struct dwo_file and
12462 have dwo_unit "live" in that. At least for now.
12463
12464 The DWP file can be made up of a random collection of CUs and TUs.
12465 However, for each CU + set of TUs that came from the same original DWO
12466 file, we can combine them back into a virtual DWO file to save space
12467 (fewer struct dwo_file objects to allocate). Remember that for really
12468 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12469
12470 std::string virtual_dwo_name =
12471 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
12472 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12473 (long) (sections.line_size ? sections.line_offset : 0),
12474 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12475 (long) (sections.str_offsets_size
12476 ? sections.str_offsets_offset : 0),
12477 (long) (sections.macro_size ? sections.macro_offset : 0),
12478 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
12479 /* Can we use an existing virtual DWO file? */
12480 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
12481 virtual_dwo_name.c_str (),
12482 comp_dir);
12483 /* Create one if necessary. */
12484 if (*dwo_file_slot == NULL)
12485 {
12486 if (dwarf_read_debug)
12487 {
12488 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12489 virtual_dwo_name.c_str ());
12490 }
12491 dwo_file = new struct dwo_file;
12492 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12493 dwo_file->comp_dir = comp_dir;
12494 dwo_file->sections.abbrev =
12495 create_dwp_v2_or_v5_section (per_objfile,
12496 &dwp_file->sections.abbrev,
12497 sections.abbrev_offset,
12498 sections.abbrev_size);
12499 dwo_file->sections.line =
12500 create_dwp_v2_or_v5_section (per_objfile,
12501 &dwp_file->sections.line,
12502 sections.line_offset, sections.line_size);
12503 dwo_file->sections.macro =
12504 create_dwp_v2_or_v5_section (per_objfile,
12505 &dwp_file->sections.macro,
12506 sections.macro_offset,
12507 sections.macro_size);
12508 dwo_file->sections.loclists =
12509 create_dwp_v2_or_v5_section (per_objfile,
12510 &dwp_file->sections.loclists,
12511 sections.loclists_offset,
12512 sections.loclists_size);
12513 dwo_file->sections.rnglists =
12514 create_dwp_v2_or_v5_section (per_objfile,
12515 &dwp_file->sections.rnglists,
12516 sections.rnglists_offset,
12517 sections.rnglists_size);
12518 dwo_file->sections.str_offsets =
12519 create_dwp_v2_or_v5_section (per_objfile,
12520 &dwp_file->sections.str_offsets,
12521 sections.str_offsets_offset,
12522 sections.str_offsets_size);
12523 /* The "str" section is global to the entire DWP file. */
12524 dwo_file->sections.str = dwp_file->sections.str;
12525 /* The info or types section is assigned below to dwo_unit,
12526 there's no need to record it in dwo_file.
12527 Also, we can't simply record type sections in dwo_file because
12528 we record a pointer into the vector in dwo_unit. As we collect more
12529 types we'll grow the vector and eventually have to reallocate space
12530 for it, invalidating all copies of pointers into the previous
12531 contents. */
12532 *dwo_file_slot = dwo_file;
12533 }
12534 else
12535 {
12536 if (dwarf_read_debug)
12537 {
12538 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12539 virtual_dwo_name.c_str ());
12540 }
12541 dwo_file = (struct dwo_file *) *dwo_file_slot;
12542 }
12543
12544 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12545 dwo_unit->dwo_file = dwo_file;
12546 dwo_unit->signature = signature;
12547 dwo_unit->section
12548 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12549 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
12550 &dwp_file->sections.info,
12551 sections.info_or_types_offset,
12552 sections.info_or_types_size);
12553 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12554
12555 return dwo_unit;
12556 }
12557
12558 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12559 Returns NULL if the signature isn't found. */
12560
12561 static struct dwo_unit *
12562 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12563 struct dwp_file *dwp_file, const char *comp_dir,
12564 ULONGEST signature, int is_debug_types)
12565 {
12566 const struct dwp_hash_table *dwp_htab =
12567 is_debug_types ? dwp_file->tus : dwp_file->cus;
12568 bfd *dbfd = dwp_file->dbfd.get ();
12569 uint32_t mask = dwp_htab->nr_slots - 1;
12570 uint32_t hash = signature & mask;
12571 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12572 unsigned int i;
12573 void **slot;
12574 struct dwo_unit find_dwo_cu;
12575
12576 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12577 find_dwo_cu.signature = signature;
12578 slot = htab_find_slot (is_debug_types
12579 ? dwp_file->loaded_tus.get ()
12580 : dwp_file->loaded_cus.get (),
12581 &find_dwo_cu, INSERT);
12582
12583 if (*slot != NULL)
12584 return (struct dwo_unit *) *slot;
12585
12586 /* Use a for loop so that we don't loop forever on bad debug info. */
12587 for (i = 0; i < dwp_htab->nr_slots; ++i)
12588 {
12589 ULONGEST signature_in_table;
12590
12591 signature_in_table =
12592 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12593 if (signature_in_table == signature)
12594 {
12595 uint32_t unit_index =
12596 read_4_bytes (dbfd,
12597 dwp_htab->unit_table + hash * sizeof (uint32_t));
12598
12599 if (dwp_file->version == 1)
12600 {
12601 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12602 unit_index, comp_dir,
12603 signature, is_debug_types);
12604 }
12605 else if (dwp_file->version == 2)
12606 {
12607 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12608 unit_index, comp_dir,
12609 signature, is_debug_types);
12610 }
12611 else /* version == 5 */
12612 {
12613 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12614 unit_index, comp_dir,
12615 signature, is_debug_types);
12616 }
12617 return (struct dwo_unit *) *slot;
12618 }
12619 if (signature_in_table == 0)
12620 return NULL;
12621 hash = (hash + hash2) & mask;
12622 }
12623
12624 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12625 " [in module %s]"),
12626 dwp_file->name);
12627 }
12628
12629 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12630 Open the file specified by FILE_NAME and hand it off to BFD for
12631 preliminary analysis. Return a newly initialized bfd *, which
12632 includes a canonicalized copy of FILE_NAME.
12633 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12634 SEARCH_CWD is true if the current directory is to be searched.
12635 It will be searched before debug-file-directory.
12636 If successful, the file is added to the bfd include table of the
12637 objfile's bfd (see gdb_bfd_record_inclusion).
12638 If unable to find/open the file, return NULL.
12639 NOTE: This function is derived from symfile_bfd_open. */
12640
12641 static gdb_bfd_ref_ptr
12642 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12643 const char *file_name, int is_dwp, int search_cwd)
12644 {
12645 int desc;
12646 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12647 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12648 to debug_file_directory. */
12649 const char *search_path;
12650 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12651
12652 gdb::unique_xmalloc_ptr<char> search_path_holder;
12653 if (search_cwd)
12654 {
12655 if (*debug_file_directory != '\0')
12656 {
12657 search_path_holder.reset (concat (".", dirname_separator_string,
12658 debug_file_directory,
12659 (char *) NULL));
12660 search_path = search_path_holder.get ();
12661 }
12662 else
12663 search_path = ".";
12664 }
12665 else
12666 search_path = debug_file_directory;
12667
12668 openp_flags flags = OPF_RETURN_REALPATH;
12669 if (is_dwp)
12670 flags |= OPF_SEARCH_IN_PATH;
12671
12672 gdb::unique_xmalloc_ptr<char> absolute_name;
12673 desc = openp (search_path, flags, file_name,
12674 O_RDONLY | O_BINARY, &absolute_name);
12675 if (desc < 0)
12676 return NULL;
12677
12678 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12679 gnutarget, desc));
12680 if (sym_bfd == NULL)
12681 return NULL;
12682 bfd_set_cacheable (sym_bfd.get (), 1);
12683
12684 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12685 return NULL;
12686
12687 /* Success. Record the bfd as having been included by the objfile's bfd.
12688 This is important because things like demangled_names_hash lives in the
12689 objfile's per_bfd space and may have references to things like symbol
12690 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12691 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12692
12693 return sym_bfd;
12694 }
12695
12696 /* Try to open DWO file FILE_NAME.
12697 COMP_DIR is the DW_AT_comp_dir attribute.
12698 The result is the bfd handle of the file.
12699 If there is a problem finding or opening the file, return NULL.
12700 Upon success, the canonicalized path of the file is stored in the bfd,
12701 same as symfile_bfd_open. */
12702
12703 static gdb_bfd_ref_ptr
12704 open_dwo_file (dwarf2_per_objfile *per_objfile,
12705 const char *file_name, const char *comp_dir)
12706 {
12707 if (IS_ABSOLUTE_PATH (file_name))
12708 return try_open_dwop_file (per_objfile, file_name,
12709 0 /*is_dwp*/, 0 /*search_cwd*/);
12710
12711 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12712
12713 if (comp_dir != NULL)
12714 {
12715 gdb::unique_xmalloc_ptr<char> path_to_try
12716 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12717
12718 /* NOTE: If comp_dir is a relative path, this will also try the
12719 search path, which seems useful. */
12720 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12721 0 /*is_dwp*/,
12722 1 /*search_cwd*/));
12723 if (abfd != NULL)
12724 return abfd;
12725 }
12726
12727 /* That didn't work, try debug-file-directory, which, despite its name,
12728 is a list of paths. */
12729
12730 if (*debug_file_directory == '\0')
12731 return NULL;
12732
12733 return try_open_dwop_file (per_objfile, file_name,
12734 0 /*is_dwp*/, 1 /*search_cwd*/);
12735 }
12736
12737 /* This function is mapped across the sections and remembers the offset and
12738 size of each of the DWO debugging sections we are interested in. */
12739
12740 static void
12741 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12742 {
12743 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12744 const struct dwop_section_names *names = &dwop_section_names;
12745
12746 if (section_is_p (sectp->name, &names->abbrev_dwo))
12747 {
12748 dwo_sections->abbrev.s.section = sectp;
12749 dwo_sections->abbrev.size = bfd_section_size (sectp);
12750 }
12751 else if (section_is_p (sectp->name, &names->info_dwo))
12752 {
12753 dwo_sections->info.s.section = sectp;
12754 dwo_sections->info.size = bfd_section_size (sectp);
12755 }
12756 else if (section_is_p (sectp->name, &names->line_dwo))
12757 {
12758 dwo_sections->line.s.section = sectp;
12759 dwo_sections->line.size = bfd_section_size (sectp);
12760 }
12761 else if (section_is_p (sectp->name, &names->loc_dwo))
12762 {
12763 dwo_sections->loc.s.section = sectp;
12764 dwo_sections->loc.size = bfd_section_size (sectp);
12765 }
12766 else if (section_is_p (sectp->name, &names->loclists_dwo))
12767 {
12768 dwo_sections->loclists.s.section = sectp;
12769 dwo_sections->loclists.size = bfd_section_size (sectp);
12770 }
12771 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12772 {
12773 dwo_sections->macinfo.s.section = sectp;
12774 dwo_sections->macinfo.size = bfd_section_size (sectp);
12775 }
12776 else if (section_is_p (sectp->name, &names->macro_dwo))
12777 {
12778 dwo_sections->macro.s.section = sectp;
12779 dwo_sections->macro.size = bfd_section_size (sectp);
12780 }
12781 else if (section_is_p (sectp->name, &names->rnglists_dwo))
12782 {
12783 dwo_sections->rnglists.s.section = sectp;
12784 dwo_sections->rnglists.size = bfd_section_size (sectp);
12785 }
12786 else if (section_is_p (sectp->name, &names->str_dwo))
12787 {
12788 dwo_sections->str.s.section = sectp;
12789 dwo_sections->str.size = bfd_section_size (sectp);
12790 }
12791 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12792 {
12793 dwo_sections->str_offsets.s.section = sectp;
12794 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12795 }
12796 else if (section_is_p (sectp->name, &names->types_dwo))
12797 {
12798 struct dwarf2_section_info type_section;
12799
12800 memset (&type_section, 0, sizeof (type_section));
12801 type_section.s.section = sectp;
12802 type_section.size = bfd_section_size (sectp);
12803 dwo_sections->types.push_back (type_section);
12804 }
12805 }
12806
12807 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12808 by PER_CU. This is for the non-DWP case.
12809 The result is NULL if DWO_NAME can't be found. */
12810
12811 static struct dwo_file *
12812 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12813 const char *comp_dir)
12814 {
12815 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12816
12817 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12818 if (dbfd == NULL)
12819 {
12820 if (dwarf_read_debug)
12821 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12822 return NULL;
12823 }
12824
12825 dwo_file_up dwo_file (new struct dwo_file);
12826 dwo_file->dwo_name = dwo_name;
12827 dwo_file->comp_dir = comp_dir;
12828 dwo_file->dbfd = std::move (dbfd);
12829
12830 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12831 &dwo_file->sections);
12832
12833 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12834 dwo_file->cus);
12835
12836 if (cu->per_cu->dwarf_version < 5)
12837 {
12838 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12839 dwo_file->sections.types, dwo_file->tus);
12840 }
12841 else
12842 {
12843 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12844 &dwo_file->sections.info, dwo_file->tus,
12845 rcuh_kind::TYPE);
12846 }
12847
12848 if (dwarf_read_debug)
12849 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12850
12851 return dwo_file.release ();
12852 }
12853
12854 /* This function is mapped across the sections and remembers the offset and
12855 size of each of the DWP debugging sections common to version 1 and 2 that
12856 we are interested in. */
12857
12858 static void
12859 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12860 void *dwp_file_ptr)
12861 {
12862 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12863 const struct dwop_section_names *names = &dwop_section_names;
12864 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12865
12866 /* Record the ELF section number for later lookup: this is what the
12867 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12868 gdb_assert (elf_section_nr < dwp_file->num_sections);
12869 dwp_file->elf_sections[elf_section_nr] = sectp;
12870
12871 /* Look for specific sections that we need. */
12872 if (section_is_p (sectp->name, &names->str_dwo))
12873 {
12874 dwp_file->sections.str.s.section = sectp;
12875 dwp_file->sections.str.size = bfd_section_size (sectp);
12876 }
12877 else if (section_is_p (sectp->name, &names->cu_index))
12878 {
12879 dwp_file->sections.cu_index.s.section = sectp;
12880 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12881 }
12882 else if (section_is_p (sectp->name, &names->tu_index))
12883 {
12884 dwp_file->sections.tu_index.s.section = sectp;
12885 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12886 }
12887 }
12888
12889 /* This function is mapped across the sections and remembers the offset and
12890 size of each of the DWP version 2 debugging sections that we are interested
12891 in. This is split into a separate function because we don't know if we
12892 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12893
12894 static void
12895 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12896 {
12897 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12898 const struct dwop_section_names *names = &dwop_section_names;
12899 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12900
12901 /* Record the ELF section number for later lookup: this is what the
12902 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12903 gdb_assert (elf_section_nr < dwp_file->num_sections);
12904 dwp_file->elf_sections[elf_section_nr] = sectp;
12905
12906 /* Look for specific sections that we need. */
12907 if (section_is_p (sectp->name, &names->abbrev_dwo))
12908 {
12909 dwp_file->sections.abbrev.s.section = sectp;
12910 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12911 }
12912 else if (section_is_p (sectp->name, &names->info_dwo))
12913 {
12914 dwp_file->sections.info.s.section = sectp;
12915 dwp_file->sections.info.size = bfd_section_size (sectp);
12916 }
12917 else if (section_is_p (sectp->name, &names->line_dwo))
12918 {
12919 dwp_file->sections.line.s.section = sectp;
12920 dwp_file->sections.line.size = bfd_section_size (sectp);
12921 }
12922 else if (section_is_p (sectp->name, &names->loc_dwo))
12923 {
12924 dwp_file->sections.loc.s.section = sectp;
12925 dwp_file->sections.loc.size = bfd_section_size (sectp);
12926 }
12927 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12928 {
12929 dwp_file->sections.macinfo.s.section = sectp;
12930 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12931 }
12932 else if (section_is_p (sectp->name, &names->macro_dwo))
12933 {
12934 dwp_file->sections.macro.s.section = sectp;
12935 dwp_file->sections.macro.size = bfd_section_size (sectp);
12936 }
12937 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12938 {
12939 dwp_file->sections.str_offsets.s.section = sectp;
12940 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12941 }
12942 else if (section_is_p (sectp->name, &names->types_dwo))
12943 {
12944 dwp_file->sections.types.s.section = sectp;
12945 dwp_file->sections.types.size = bfd_section_size (sectp);
12946 }
12947 }
12948
12949 /* This function is mapped across the sections and remembers the offset and
12950 size of each of the DWP version 5 debugging sections that we are interested
12951 in. This is split into a separate function because we don't know if we
12952 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12953
12954 static void
12955 dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12956 {
12957 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12958 const struct dwop_section_names *names = &dwop_section_names;
12959 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12960
12961 /* Record the ELF section number for later lookup: this is what the
12962 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12963 gdb_assert (elf_section_nr < dwp_file->num_sections);
12964 dwp_file->elf_sections[elf_section_nr] = sectp;
12965
12966 /* Look for specific sections that we need. */
12967 if (section_is_p (sectp->name, &names->abbrev_dwo))
12968 {
12969 dwp_file->sections.abbrev.s.section = sectp;
12970 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12971 }
12972 else if (section_is_p (sectp->name, &names->info_dwo))
12973 {
12974 dwp_file->sections.info.s.section = sectp;
12975 dwp_file->sections.info.size = bfd_section_size (sectp);
12976 }
12977 else if (section_is_p (sectp->name, &names->line_dwo))
12978 {
12979 dwp_file->sections.line.s.section = sectp;
12980 dwp_file->sections.line.size = bfd_section_size (sectp);
12981 }
12982 else if (section_is_p (sectp->name, &names->loclists_dwo))
12983 {
12984 dwp_file->sections.loclists.s.section = sectp;
12985 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12986 }
12987 else if (section_is_p (sectp->name, &names->macro_dwo))
12988 {
12989 dwp_file->sections.macro.s.section = sectp;
12990 dwp_file->sections.macro.size = bfd_section_size (sectp);
12991 }
12992 else if (section_is_p (sectp->name, &names->rnglists_dwo))
12993 {
12994 dwp_file->sections.rnglists.s.section = sectp;
12995 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12996 }
12997 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12998 {
12999 dwp_file->sections.str_offsets.s.section = sectp;
13000 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13001 }
13002 }
13003
13004 /* Hash function for dwp_file loaded CUs/TUs. */
13005
13006 static hashval_t
13007 hash_dwp_loaded_cutus (const void *item)
13008 {
13009 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13010
13011 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13012 return dwo_unit->signature;
13013 }
13014
13015 /* Equality function for dwp_file loaded CUs/TUs. */
13016
13017 static int
13018 eq_dwp_loaded_cutus (const void *a, const void *b)
13019 {
13020 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13021 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13022
13023 return dua->signature == dub->signature;
13024 }
13025
13026 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13027
13028 static htab_up
13029 allocate_dwp_loaded_cutus_table ()
13030 {
13031 return htab_up (htab_create_alloc (3,
13032 hash_dwp_loaded_cutus,
13033 eq_dwp_loaded_cutus,
13034 NULL, xcalloc, xfree));
13035 }
13036
13037 /* Try to open DWP file FILE_NAME.
13038 The result is the bfd handle of the file.
13039 If there is a problem finding or opening the file, return NULL.
13040 Upon success, the canonicalized path of the file is stored in the bfd,
13041 same as symfile_bfd_open. */
13042
13043 static gdb_bfd_ref_ptr
13044 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
13045 {
13046 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
13047 1 /*is_dwp*/,
13048 1 /*search_cwd*/));
13049 if (abfd != NULL)
13050 return abfd;
13051
13052 /* Work around upstream bug 15652.
13053 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13054 [Whether that's a "bug" is debatable, but it is getting in our way.]
13055 We have no real idea where the dwp file is, because gdb's realpath-ing
13056 of the executable's path may have discarded the needed info.
13057 [IWBN if the dwp file name was recorded in the executable, akin to
13058 .gnu_debuglink, but that doesn't exist yet.]
13059 Strip the directory from FILE_NAME and search again. */
13060 if (*debug_file_directory != '\0')
13061 {
13062 /* Don't implicitly search the current directory here.
13063 If the user wants to search "." to handle this case,
13064 it must be added to debug-file-directory. */
13065 return try_open_dwop_file (per_objfile, lbasename (file_name),
13066 1 /*is_dwp*/,
13067 0 /*search_cwd*/);
13068 }
13069
13070 return NULL;
13071 }
13072
13073 /* Initialize the use of the DWP file for the current objfile.
13074 By convention the name of the DWP file is ${objfile}.dwp.
13075 The result is NULL if it can't be found. */
13076
13077 static std::unique_ptr<struct dwp_file>
13078 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
13079 {
13080 struct objfile *objfile = per_objfile->objfile;
13081
13082 /* Try to find first .dwp for the binary file before any symbolic links
13083 resolving. */
13084
13085 /* If the objfile is a debug file, find the name of the real binary
13086 file and get the name of dwp file from there. */
13087 std::string dwp_name;
13088 if (objfile->separate_debug_objfile_backlink != NULL)
13089 {
13090 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13091 const char *backlink_basename = lbasename (backlink->original_name);
13092
13093 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13094 }
13095 else
13096 dwp_name = objfile->original_name;
13097
13098 dwp_name += ".dwp";
13099
13100 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
13101 if (dbfd == NULL
13102 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13103 {
13104 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13105 dwp_name = objfile_name (objfile);
13106 dwp_name += ".dwp";
13107 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
13108 }
13109
13110 if (dbfd == NULL)
13111 {
13112 if (dwarf_read_debug)
13113 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13114 return std::unique_ptr<dwp_file> ();
13115 }
13116
13117 const char *name = bfd_get_filename (dbfd.get ());
13118 std::unique_ptr<struct dwp_file> dwp_file
13119 (new struct dwp_file (name, std::move (dbfd)));
13120
13121 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13122 dwp_file->elf_sections =
13123 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
13124 dwp_file->num_sections, asection *);
13125
13126 bfd_map_over_sections (dwp_file->dbfd.get (),
13127 dwarf2_locate_common_dwp_sections,
13128 dwp_file.get ());
13129
13130 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
13131
13132 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
13133
13134 /* The DWP file version is stored in the hash table. Oh well. */
13135 if (dwp_file->cus && dwp_file->tus
13136 && dwp_file->cus->version != dwp_file->tus->version)
13137 {
13138 /* Technically speaking, we should try to limp along, but this is
13139 pretty bizarre. We use pulongest here because that's the established
13140 portability solution (e.g, we cannot use %u for uint32_t). */
13141 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13142 " TU version %s [in DWP file %s]"),
13143 pulongest (dwp_file->cus->version),
13144 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13145 }
13146
13147 if (dwp_file->cus)
13148 dwp_file->version = dwp_file->cus->version;
13149 else if (dwp_file->tus)
13150 dwp_file->version = dwp_file->tus->version;
13151 else
13152 dwp_file->version = 2;
13153
13154 if (dwp_file->version == 2)
13155 bfd_map_over_sections (dwp_file->dbfd.get (),
13156 dwarf2_locate_v2_dwp_sections,
13157 dwp_file.get ());
13158 else if (dwp_file->version == 5)
13159 bfd_map_over_sections (dwp_file->dbfd.get (),
13160 dwarf2_locate_v5_dwp_sections,
13161 dwp_file.get ());
13162
13163
13164 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
13165 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
13166
13167 if (dwarf_read_debug)
13168 {
13169 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13170 fprintf_unfiltered (gdb_stdlog,
13171 " %s CUs, %s TUs\n",
13172 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13173 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13174 }
13175
13176 return dwp_file;
13177 }
13178
13179 /* Wrapper around open_and_init_dwp_file, only open it once. */
13180
13181 static struct dwp_file *
13182 get_dwp_file (dwarf2_per_objfile *per_objfile)
13183 {
13184 if (!per_objfile->per_bfd->dwp_checked)
13185 {
13186 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
13187 per_objfile->per_bfd->dwp_checked = 1;
13188 }
13189 return per_objfile->per_bfd->dwp_file.get ();
13190 }
13191
13192 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13193 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13194 or in the DWP file for the objfile, referenced by THIS_UNIT.
13195 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13196 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13197
13198 This is called, for example, when wanting to read a variable with a
13199 complex location. Therefore we don't want to do file i/o for every call.
13200 Therefore we don't want to look for a DWO file on every call.
13201 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13202 then we check if we've already seen DWO_NAME, and only THEN do we check
13203 for a DWO file.
13204
13205 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13206 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13207
13208 static struct dwo_unit *
13209 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13210 ULONGEST signature, int is_debug_types)
13211 {
13212 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13213 struct objfile *objfile = per_objfile->objfile;
13214 const char *kind = is_debug_types ? "TU" : "CU";
13215 void **dwo_file_slot;
13216 struct dwo_file *dwo_file;
13217 struct dwp_file *dwp_file;
13218
13219 /* First see if there's a DWP file.
13220 If we have a DWP file but didn't find the DWO inside it, don't
13221 look for the original DWO file. It makes gdb behave differently
13222 depending on whether one is debugging in the build tree. */
13223
13224 dwp_file = get_dwp_file (per_objfile);
13225 if (dwp_file != NULL)
13226 {
13227 const struct dwp_hash_table *dwp_htab =
13228 is_debug_types ? dwp_file->tus : dwp_file->cus;
13229
13230 if (dwp_htab != NULL)
13231 {
13232 struct dwo_unit *dwo_cutu =
13233 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
13234 is_debug_types);
13235
13236 if (dwo_cutu != NULL)
13237 {
13238 if (dwarf_read_debug)
13239 {
13240 fprintf_unfiltered (gdb_stdlog,
13241 "Virtual DWO %s %s found: @%s\n",
13242 kind, hex_string (signature),
13243 host_address_to_string (dwo_cutu));
13244 }
13245 return dwo_cutu;
13246 }
13247 }
13248 }
13249 else
13250 {
13251 /* No DWP file, look for the DWO file. */
13252
13253 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
13254 if (*dwo_file_slot == NULL)
13255 {
13256 /* Read in the file and build a table of the CUs/TUs it contains. */
13257 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
13258 }
13259 /* NOTE: This will be NULL if unable to open the file. */
13260 dwo_file = (struct dwo_file *) *dwo_file_slot;
13261
13262 if (dwo_file != NULL)
13263 {
13264 struct dwo_unit *dwo_cutu = NULL;
13265
13266 if (is_debug_types && dwo_file->tus)
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
13273 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
13274 &find_dwo_cutu);
13275 }
13276 else if (!is_debug_types && dwo_file->cus)
13277 {
13278 struct dwo_unit find_dwo_cutu;
13279
13280 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13281 find_dwo_cutu.signature = signature;
13282 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
13283 &find_dwo_cutu);
13284 }
13285
13286 if (dwo_cutu != NULL)
13287 {
13288 if (dwarf_read_debug)
13289 {
13290 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13291 kind, dwo_name, hex_string (signature),
13292 host_address_to_string (dwo_cutu));
13293 }
13294 return dwo_cutu;
13295 }
13296 }
13297 }
13298
13299 /* We didn't find it. This could mean a dwo_id mismatch, or
13300 someone deleted the DWO/DWP file, or the search path isn't set up
13301 correctly to find the file. */
13302
13303 if (dwarf_read_debug)
13304 {
13305 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13306 kind, dwo_name, hex_string (signature));
13307 }
13308
13309 /* This is a warning and not a complaint because it can be caused by
13310 pilot error (e.g., user accidentally deleting the DWO). */
13311 {
13312 /* Print the name of the DWP file if we looked there, helps the user
13313 better diagnose the problem. */
13314 std::string dwp_text;
13315
13316 if (dwp_file != NULL)
13317 dwp_text = string_printf (" [in DWP file %s]",
13318 lbasename (dwp_file->name));
13319
13320 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13321 " [in module %s]"),
13322 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13323 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
13324 }
13325 return NULL;
13326 }
13327
13328 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13329 See lookup_dwo_cutu_unit for details. */
13330
13331 static struct dwo_unit *
13332 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13333 ULONGEST signature)
13334 {
13335 gdb_assert (!cu->per_cu->is_debug_types);
13336
13337 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
13338 }
13339
13340 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13341 See lookup_dwo_cutu_unit for details. */
13342
13343 static struct dwo_unit *
13344 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
13345 {
13346 gdb_assert (cu->per_cu->is_debug_types);
13347
13348 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13349
13350 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
13351 }
13352
13353 /* Traversal function for queue_and_load_all_dwo_tus. */
13354
13355 static int
13356 queue_and_load_dwo_tu (void **slot, void *info)
13357 {
13358 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13359 dwarf2_cu *cu = (dwarf2_cu *) info;
13360 ULONGEST signature = dwo_unit->signature;
13361 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
13362
13363 if (sig_type != NULL)
13364 {
13365 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13366
13367 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13368 a real dependency of PER_CU on SIG_TYPE. That is detected later
13369 while processing PER_CU. */
13370 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
13371 load_full_type_unit (sig_cu, cu->per_objfile);
13372 cu->per_cu->imported_symtabs_push (sig_cu);
13373 }
13374
13375 return 1;
13376 }
13377
13378 /* Queue all TUs contained in the DWO of CU to be read in.
13379 The DWO may have the only definition of the type, though it may not be
13380 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13381 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13382
13383 static void
13384 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
13385 {
13386 struct dwo_unit *dwo_unit;
13387 struct dwo_file *dwo_file;
13388
13389 gdb_assert (cu != nullptr);
13390 gdb_assert (!cu->per_cu->is_debug_types);
13391 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
13392
13393 dwo_unit = cu->dwo_unit;
13394 gdb_assert (dwo_unit != NULL);
13395
13396 dwo_file = dwo_unit->dwo_file;
13397 if (dwo_file->tus != NULL)
13398 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
13399 }
13400
13401 /* Read in various DIEs. */
13402
13403 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13404 Inherit only the children of the DW_AT_abstract_origin DIE not being
13405 already referenced by DW_AT_abstract_origin from the children of the
13406 current DIE. */
13407
13408 static void
13409 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13410 {
13411 struct die_info *child_die;
13412 sect_offset *offsetp;
13413 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13414 struct die_info *origin_die;
13415 /* Iterator of the ORIGIN_DIE children. */
13416 struct die_info *origin_child_die;
13417 struct attribute *attr;
13418 struct dwarf2_cu *origin_cu;
13419 struct pending **origin_previous_list_in_scope;
13420
13421 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13422 if (!attr)
13423 return;
13424
13425 /* Note that following die references may follow to a die in a
13426 different cu. */
13427
13428 origin_cu = cu;
13429 origin_die = follow_die_ref (die, attr, &origin_cu);
13430
13431 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13432 symbols in. */
13433 origin_previous_list_in_scope = origin_cu->list_in_scope;
13434 origin_cu->list_in_scope = cu->list_in_scope;
13435
13436 if (die->tag != origin_die->tag
13437 && !(die->tag == DW_TAG_inlined_subroutine
13438 && origin_die->tag == DW_TAG_subprogram))
13439 complaint (_("DIE %s and its abstract origin %s have different tags"),
13440 sect_offset_str (die->sect_off),
13441 sect_offset_str (origin_die->sect_off));
13442
13443 std::vector<sect_offset> offsets;
13444
13445 for (child_die = die->child;
13446 child_die && child_die->tag;
13447 child_die = child_die->sibling)
13448 {
13449 struct die_info *child_origin_die;
13450 struct dwarf2_cu *child_origin_cu;
13451
13452 /* We are trying to process concrete instance entries:
13453 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13454 it's not relevant to our analysis here. i.e. detecting DIEs that are
13455 present in the abstract instance but not referenced in the concrete
13456 one. */
13457 if (child_die->tag == DW_TAG_call_site
13458 || child_die->tag == DW_TAG_GNU_call_site)
13459 continue;
13460
13461 /* For each CHILD_DIE, find the corresponding child of
13462 ORIGIN_DIE. If there is more than one layer of
13463 DW_AT_abstract_origin, follow them all; there shouldn't be,
13464 but GCC versions at least through 4.4 generate this (GCC PR
13465 40573). */
13466 child_origin_die = child_die;
13467 child_origin_cu = cu;
13468 while (1)
13469 {
13470 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13471 child_origin_cu);
13472 if (attr == NULL)
13473 break;
13474 child_origin_die = follow_die_ref (child_origin_die, attr,
13475 &child_origin_cu);
13476 }
13477
13478 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13479 counterpart may exist. */
13480 if (child_origin_die != child_die)
13481 {
13482 if (child_die->tag != child_origin_die->tag
13483 && !(child_die->tag == DW_TAG_inlined_subroutine
13484 && child_origin_die->tag == DW_TAG_subprogram))
13485 complaint (_("Child DIE %s and its abstract origin %s have "
13486 "different tags"),
13487 sect_offset_str (child_die->sect_off),
13488 sect_offset_str (child_origin_die->sect_off));
13489 if (child_origin_die->parent != origin_die)
13490 complaint (_("Child DIE %s and its abstract origin %s have "
13491 "different parents"),
13492 sect_offset_str (child_die->sect_off),
13493 sect_offset_str (child_origin_die->sect_off));
13494 else
13495 offsets.push_back (child_origin_die->sect_off);
13496 }
13497 }
13498 std::sort (offsets.begin (), offsets.end ());
13499 sect_offset *offsets_end = offsets.data () + offsets.size ();
13500 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13501 if (offsetp[-1] == *offsetp)
13502 complaint (_("Multiple children of DIE %s refer "
13503 "to DIE %s as their abstract origin"),
13504 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13505
13506 offsetp = offsets.data ();
13507 origin_child_die = origin_die->child;
13508 while (origin_child_die && origin_child_die->tag)
13509 {
13510 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13511 while (offsetp < offsets_end
13512 && *offsetp < origin_child_die->sect_off)
13513 offsetp++;
13514 if (offsetp >= offsets_end
13515 || *offsetp > origin_child_die->sect_off)
13516 {
13517 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13518 Check whether we're already processing ORIGIN_CHILD_DIE.
13519 This can happen with mutually referenced abstract_origins.
13520 PR 16581. */
13521 if (!origin_child_die->in_process)
13522 process_die (origin_child_die, origin_cu);
13523 }
13524 origin_child_die = origin_child_die->sibling;
13525 }
13526 origin_cu->list_in_scope = origin_previous_list_in_scope;
13527
13528 if (cu != origin_cu)
13529 compute_delayed_physnames (origin_cu);
13530 }
13531
13532 static void
13533 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13534 {
13535 struct objfile *objfile = cu->per_objfile->objfile;
13536 struct gdbarch *gdbarch = objfile->arch ();
13537 struct context_stack *newobj;
13538 CORE_ADDR lowpc;
13539 CORE_ADDR highpc;
13540 struct die_info *child_die;
13541 struct attribute *attr, *call_line, *call_file;
13542 const char *name;
13543 CORE_ADDR baseaddr;
13544 struct block *block;
13545 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13546 std::vector<struct symbol *> template_args;
13547 struct template_symbol *templ_func = NULL;
13548
13549 if (inlined_func)
13550 {
13551 /* If we do not have call site information, we can't show the
13552 caller of this inlined function. That's too confusing, so
13553 only use the scope for local variables. */
13554 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13555 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13556 if (call_line == NULL || call_file == NULL)
13557 {
13558 read_lexical_block_scope (die, cu);
13559 return;
13560 }
13561 }
13562
13563 baseaddr = objfile->text_section_offset ();
13564
13565 name = dwarf2_name (die, cu);
13566
13567 /* Ignore functions with missing or empty names. These are actually
13568 illegal according to the DWARF standard. */
13569 if (name == NULL)
13570 {
13571 complaint (_("missing name for subprogram DIE at %s"),
13572 sect_offset_str (die->sect_off));
13573 return;
13574 }
13575
13576 /* Ignore functions with missing or invalid low and high pc attributes. */
13577 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13578 <= PC_BOUNDS_INVALID)
13579 {
13580 attr = dwarf2_attr (die, DW_AT_external, cu);
13581 if (!attr || !DW_UNSND (attr))
13582 complaint (_("cannot get low and high bounds "
13583 "for subprogram DIE at %s"),
13584 sect_offset_str (die->sect_off));
13585 return;
13586 }
13587
13588 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13589 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13590
13591 /* If we have any template arguments, then we must allocate a
13592 different sort of symbol. */
13593 for (child_die = die->child; child_die; child_die = child_die->sibling)
13594 {
13595 if (child_die->tag == DW_TAG_template_type_param
13596 || child_die->tag == DW_TAG_template_value_param)
13597 {
13598 templ_func = new (&objfile->objfile_obstack) template_symbol;
13599 templ_func->subclass = SYMBOL_TEMPLATE;
13600 break;
13601 }
13602 }
13603
13604 newobj = cu->get_builder ()->push_context (0, lowpc);
13605 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13606 (struct symbol *) templ_func);
13607
13608 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13609 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13610 cu->language);
13611
13612 /* If there is a location expression for DW_AT_frame_base, record
13613 it. */
13614 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13615 if (attr != nullptr)
13616 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13617
13618 /* If there is a location for the static link, record it. */
13619 newobj->static_link = NULL;
13620 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13621 if (attr != nullptr)
13622 {
13623 newobj->static_link
13624 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13625 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13626 cu->addr_type ());
13627 }
13628
13629 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13630
13631 if (die->child != NULL)
13632 {
13633 child_die = die->child;
13634 while (child_die && child_die->tag)
13635 {
13636 if (child_die->tag == DW_TAG_template_type_param
13637 || child_die->tag == DW_TAG_template_value_param)
13638 {
13639 struct symbol *arg = new_symbol (child_die, NULL, cu);
13640
13641 if (arg != NULL)
13642 template_args.push_back (arg);
13643 }
13644 else
13645 process_die (child_die, cu);
13646 child_die = child_die->sibling;
13647 }
13648 }
13649
13650 inherit_abstract_dies (die, cu);
13651
13652 /* If we have a DW_AT_specification, we might need to import using
13653 directives from the context of the specification DIE. See the
13654 comment in determine_prefix. */
13655 if (cu->language == language_cplus
13656 && dwarf2_attr (die, DW_AT_specification, cu))
13657 {
13658 struct dwarf2_cu *spec_cu = cu;
13659 struct die_info *spec_die = die_specification (die, &spec_cu);
13660
13661 while (spec_die)
13662 {
13663 child_die = spec_die->child;
13664 while (child_die && child_die->tag)
13665 {
13666 if (child_die->tag == DW_TAG_imported_module)
13667 process_die (child_die, spec_cu);
13668 child_die = child_die->sibling;
13669 }
13670
13671 /* In some cases, GCC generates specification DIEs that
13672 themselves contain DW_AT_specification attributes. */
13673 spec_die = die_specification (spec_die, &spec_cu);
13674 }
13675 }
13676
13677 struct context_stack cstk = cu->get_builder ()->pop_context ();
13678 /* Make a block for the local symbols within. */
13679 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13680 cstk.static_link, lowpc, highpc);
13681
13682 /* For C++, set the block's scope. */
13683 if ((cu->language == language_cplus
13684 || cu->language == language_fortran
13685 || cu->language == language_d
13686 || cu->language == language_rust)
13687 && cu->processing_has_namespace_info)
13688 block_set_scope (block, determine_prefix (die, cu),
13689 &objfile->objfile_obstack);
13690
13691 /* If we have address ranges, record them. */
13692 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13693
13694 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13695
13696 /* Attach template arguments to function. */
13697 if (!template_args.empty ())
13698 {
13699 gdb_assert (templ_func != NULL);
13700
13701 templ_func->n_template_arguments = template_args.size ();
13702 templ_func->template_arguments
13703 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13704 templ_func->n_template_arguments);
13705 memcpy (templ_func->template_arguments,
13706 template_args.data (),
13707 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13708
13709 /* Make sure that the symtab is set on the new symbols. Even
13710 though they don't appear in this symtab directly, other parts
13711 of gdb assume that symbols do, and this is reasonably
13712 true. */
13713 for (symbol *sym : template_args)
13714 symbol_set_symtab (sym, symbol_symtab (templ_func));
13715 }
13716
13717 /* In C++, we can have functions nested inside functions (e.g., when
13718 a function declares a class that has methods). This means that
13719 when we finish processing a function scope, we may need to go
13720 back to building a containing block's symbol lists. */
13721 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13722 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13723
13724 /* If we've finished processing a top-level function, subsequent
13725 symbols go in the file symbol list. */
13726 if (cu->get_builder ()->outermost_context_p ())
13727 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13728 }
13729
13730 /* Process all the DIES contained within a lexical block scope. Start
13731 a new scope, process the dies, and then close the scope. */
13732
13733 static void
13734 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13735 {
13736 struct objfile *objfile = cu->per_objfile->objfile;
13737 struct gdbarch *gdbarch = objfile->arch ();
13738 CORE_ADDR lowpc, highpc;
13739 struct die_info *child_die;
13740 CORE_ADDR baseaddr;
13741
13742 baseaddr = objfile->text_section_offset ();
13743
13744 /* Ignore blocks with missing or invalid low and high pc attributes. */
13745 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13746 as multiple lexical blocks? Handling children in a sane way would
13747 be nasty. Might be easier to properly extend generic blocks to
13748 describe ranges. */
13749 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13750 {
13751 case PC_BOUNDS_NOT_PRESENT:
13752 /* DW_TAG_lexical_block has no attributes, process its children as if
13753 there was no wrapping by that DW_TAG_lexical_block.
13754 GCC does no longer produces such DWARF since GCC r224161. */
13755 for (child_die = die->child;
13756 child_die != NULL && child_die->tag;
13757 child_die = child_die->sibling)
13758 {
13759 /* We might already be processing this DIE. This can happen
13760 in an unusual circumstance -- where a subroutine A
13761 appears lexically in another subroutine B, but A actually
13762 inlines B. The recursion is broken here, rather than in
13763 inherit_abstract_dies, because it seems better to simply
13764 drop concrete children here. */
13765 if (!child_die->in_process)
13766 process_die (child_die, cu);
13767 }
13768 return;
13769 case PC_BOUNDS_INVALID:
13770 return;
13771 }
13772 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13773 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13774
13775 cu->get_builder ()->push_context (0, lowpc);
13776 if (die->child != NULL)
13777 {
13778 child_die = die->child;
13779 while (child_die && child_die->tag)
13780 {
13781 process_die (child_die, cu);
13782 child_die = child_die->sibling;
13783 }
13784 }
13785 inherit_abstract_dies (die, cu);
13786 struct context_stack cstk = cu->get_builder ()->pop_context ();
13787
13788 if (*cu->get_builder ()->get_local_symbols () != NULL
13789 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13790 {
13791 struct block *block
13792 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13793 cstk.start_addr, highpc);
13794
13795 /* Note that recording ranges after traversing children, as we
13796 do here, means that recording a parent's ranges entails
13797 walking across all its children's ranges as they appear in
13798 the address map, which is quadratic behavior.
13799
13800 It would be nicer to record the parent's ranges before
13801 traversing its children, simply overriding whatever you find
13802 there. But since we don't even decide whether to create a
13803 block until after we've traversed its children, that's hard
13804 to do. */
13805 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13806 }
13807 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13808 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13809 }
13810
13811 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13812
13813 static void
13814 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13815 {
13816 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13817 struct objfile *objfile = per_objfile->objfile;
13818 struct gdbarch *gdbarch = objfile->arch ();
13819 CORE_ADDR pc, baseaddr;
13820 struct attribute *attr;
13821 struct call_site *call_site, call_site_local;
13822 void **slot;
13823 int nparams;
13824 struct die_info *child_die;
13825
13826 baseaddr = objfile->text_section_offset ();
13827
13828 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13829 if (attr == NULL)
13830 {
13831 /* This was a pre-DWARF-5 GNU extension alias
13832 for DW_AT_call_return_pc. */
13833 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13834 }
13835 if (!attr)
13836 {
13837 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13838 "DIE %s [in module %s]"),
13839 sect_offset_str (die->sect_off), objfile_name (objfile));
13840 return;
13841 }
13842 pc = attr->value_as_address () + baseaddr;
13843 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13844
13845 if (cu->call_site_htab == NULL)
13846 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13847 NULL, &objfile->objfile_obstack,
13848 hashtab_obstack_allocate, NULL);
13849 call_site_local.pc = pc;
13850 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13851 if (*slot != NULL)
13852 {
13853 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13854 "DIE %s [in module %s]"),
13855 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13856 objfile_name (objfile));
13857 return;
13858 }
13859
13860 /* Count parameters at the caller. */
13861
13862 nparams = 0;
13863 for (child_die = die->child; child_die && child_die->tag;
13864 child_die = child_die->sibling)
13865 {
13866 if (child_die->tag != DW_TAG_call_site_parameter
13867 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13868 {
13869 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13870 "DW_TAG_call_site child DIE %s [in module %s]"),
13871 child_die->tag, sect_offset_str (child_die->sect_off),
13872 objfile_name (objfile));
13873 continue;
13874 }
13875
13876 nparams++;
13877 }
13878
13879 call_site
13880 = ((struct call_site *)
13881 obstack_alloc (&objfile->objfile_obstack,
13882 sizeof (*call_site)
13883 + (sizeof (*call_site->parameter) * (nparams - 1))));
13884 *slot = call_site;
13885 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13886 call_site->pc = pc;
13887
13888 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13889 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13890 {
13891 struct die_info *func_die;
13892
13893 /* Skip also over DW_TAG_inlined_subroutine. */
13894 for (func_die = die->parent;
13895 func_die && func_die->tag != DW_TAG_subprogram
13896 && func_die->tag != DW_TAG_subroutine_type;
13897 func_die = func_die->parent);
13898
13899 /* DW_AT_call_all_calls is a superset
13900 of DW_AT_call_all_tail_calls. */
13901 if (func_die
13902 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13903 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13904 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13905 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13906 {
13907 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13908 not complete. But keep CALL_SITE for look ups via call_site_htab,
13909 both the initial caller containing the real return address PC and
13910 the final callee containing the current PC of a chain of tail
13911 calls do not need to have the tail call list complete. But any
13912 function candidate for a virtual tail call frame searched via
13913 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13914 determined unambiguously. */
13915 }
13916 else
13917 {
13918 struct type *func_type = NULL;
13919
13920 if (func_die)
13921 func_type = get_die_type (func_die, cu);
13922 if (func_type != NULL)
13923 {
13924 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13925
13926 /* Enlist this call site to the function. */
13927 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13928 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13929 }
13930 else
13931 complaint (_("Cannot find function owning DW_TAG_call_site "
13932 "DIE %s [in module %s]"),
13933 sect_offset_str (die->sect_off), objfile_name (objfile));
13934 }
13935 }
13936
13937 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13938 if (attr == NULL)
13939 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13940 if (attr == NULL)
13941 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13942 if (attr == NULL)
13943 {
13944 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13945 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13946 }
13947 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13948 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13949 /* Keep NULL DWARF_BLOCK. */;
13950 else if (attr->form_is_block ())
13951 {
13952 struct dwarf2_locexpr_baton *dlbaton;
13953
13954 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13955 dlbaton->data = DW_BLOCK (attr)->data;
13956 dlbaton->size = DW_BLOCK (attr)->size;
13957 dlbaton->per_objfile = per_objfile;
13958 dlbaton->per_cu = cu->per_cu;
13959
13960 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13961 }
13962 else if (attr->form_is_ref ())
13963 {
13964 struct dwarf2_cu *target_cu = cu;
13965 struct die_info *target_die;
13966
13967 target_die = follow_die_ref (die, attr, &target_cu);
13968 gdb_assert (target_cu->per_objfile->objfile == objfile);
13969 if (die_is_declaration (target_die, target_cu))
13970 {
13971 const char *target_physname;
13972
13973 /* Prefer the mangled name; otherwise compute the demangled one. */
13974 target_physname = dw2_linkage_name (target_die, target_cu);
13975 if (target_physname == NULL)
13976 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13977 if (target_physname == NULL)
13978 complaint (_("DW_AT_call_target target DIE has invalid "
13979 "physname, for referencing DIE %s [in module %s]"),
13980 sect_offset_str (die->sect_off), objfile_name (objfile));
13981 else
13982 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13983 }
13984 else
13985 {
13986 CORE_ADDR lowpc;
13987
13988 /* DW_AT_entry_pc should be preferred. */
13989 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13990 <= PC_BOUNDS_INVALID)
13991 complaint (_("DW_AT_call_target target DIE has invalid "
13992 "low pc, for referencing DIE %s [in module %s]"),
13993 sect_offset_str (die->sect_off), objfile_name (objfile));
13994 else
13995 {
13996 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13997 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13998 }
13999 }
14000 }
14001 else
14002 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14003 "block nor reference, for DIE %s [in module %s]"),
14004 sect_offset_str (die->sect_off), objfile_name (objfile));
14005
14006 call_site->per_cu = cu->per_cu;
14007 call_site->per_objfile = per_objfile;
14008
14009 for (child_die = die->child;
14010 child_die && child_die->tag;
14011 child_die = child_die->sibling)
14012 {
14013 struct call_site_parameter *parameter;
14014 struct attribute *loc, *origin;
14015
14016 if (child_die->tag != DW_TAG_call_site_parameter
14017 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14018 {
14019 /* Already printed the complaint above. */
14020 continue;
14021 }
14022
14023 gdb_assert (call_site->parameter_count < nparams);
14024 parameter = &call_site->parameter[call_site->parameter_count];
14025
14026 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14027 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14028 register is contained in DW_AT_call_value. */
14029
14030 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14031 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14032 if (origin == NULL)
14033 {
14034 /* This was a pre-DWARF-5 GNU extension alias
14035 for DW_AT_call_parameter. */
14036 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14037 }
14038 if (loc == NULL && origin != NULL && origin->form_is_ref ())
14039 {
14040 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14041
14042 sect_offset sect_off = origin->get_ref_die_offset ();
14043 if (!cu->header.offset_in_cu_p (sect_off))
14044 {
14045 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14046 binding can be done only inside one CU. Such referenced DIE
14047 therefore cannot be even moved to DW_TAG_partial_unit. */
14048 complaint (_("DW_AT_call_parameter offset is not in CU for "
14049 "DW_TAG_call_site child DIE %s [in module %s]"),
14050 sect_offset_str (child_die->sect_off),
14051 objfile_name (objfile));
14052 continue;
14053 }
14054 parameter->u.param_cu_off
14055 = (cu_offset) (sect_off - cu->header.sect_off);
14056 }
14057 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
14058 {
14059 complaint (_("No DW_FORM_block* DW_AT_location for "
14060 "DW_TAG_call_site child DIE %s [in module %s]"),
14061 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14062 continue;
14063 }
14064 else
14065 {
14066 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14067 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14068 if (parameter->u.dwarf_reg != -1)
14069 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14070 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14071 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14072 &parameter->u.fb_offset))
14073 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14074 else
14075 {
14076 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14077 "for DW_FORM_block* DW_AT_location is supported for "
14078 "DW_TAG_call_site child DIE %s "
14079 "[in module %s]"),
14080 sect_offset_str (child_die->sect_off),
14081 objfile_name (objfile));
14082 continue;
14083 }
14084 }
14085
14086 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14087 if (attr == NULL)
14088 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14089 if (attr == NULL || !attr->form_is_block ())
14090 {
14091 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14092 "DW_TAG_call_site child DIE %s [in module %s]"),
14093 sect_offset_str (child_die->sect_off),
14094 objfile_name (objfile));
14095 continue;
14096 }
14097 parameter->value = DW_BLOCK (attr)->data;
14098 parameter->value_size = DW_BLOCK (attr)->size;
14099
14100 /* Parameters are not pre-cleared by memset above. */
14101 parameter->data_value = NULL;
14102 parameter->data_value_size = 0;
14103 call_site->parameter_count++;
14104
14105 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14106 if (attr == NULL)
14107 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14108 if (attr != nullptr)
14109 {
14110 if (!attr->form_is_block ())
14111 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14112 "DW_TAG_call_site child DIE %s [in module %s]"),
14113 sect_offset_str (child_die->sect_off),
14114 objfile_name (objfile));
14115 else
14116 {
14117 parameter->data_value = DW_BLOCK (attr)->data;
14118 parameter->data_value_size = DW_BLOCK (attr)->size;
14119 }
14120 }
14121 }
14122 }
14123
14124 /* Helper function for read_variable. If DIE represents a virtual
14125 table, then return the type of the concrete object that is
14126 associated with the virtual table. Otherwise, return NULL. */
14127
14128 static struct type *
14129 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14130 {
14131 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14132 if (attr == NULL)
14133 return NULL;
14134
14135 /* Find the type DIE. */
14136 struct die_info *type_die = NULL;
14137 struct dwarf2_cu *type_cu = cu;
14138
14139 if (attr->form_is_ref ())
14140 type_die = follow_die_ref (die, attr, &type_cu);
14141 if (type_die == NULL)
14142 return NULL;
14143
14144 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14145 return NULL;
14146 return die_containing_type (type_die, type_cu);
14147 }
14148
14149 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14150
14151 static void
14152 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14153 {
14154 struct rust_vtable_symbol *storage = NULL;
14155
14156 if (cu->language == language_rust)
14157 {
14158 struct type *containing_type = rust_containing_type (die, cu);
14159
14160 if (containing_type != NULL)
14161 {
14162 struct objfile *objfile = cu->per_objfile->objfile;
14163
14164 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
14165 storage->concrete_type = containing_type;
14166 storage->subclass = SYMBOL_RUST_VTABLE;
14167 }
14168 }
14169
14170 struct symbol *res = new_symbol (die, NULL, cu, storage);
14171 struct attribute *abstract_origin
14172 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14173 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14174 if (res == NULL && loc && abstract_origin)
14175 {
14176 /* We have a variable without a name, but with a location and an abstract
14177 origin. This may be a concrete instance of an abstract variable
14178 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14179 later. */
14180 struct dwarf2_cu *origin_cu = cu;
14181 struct die_info *origin_die
14182 = follow_die_ref (die, abstract_origin, &origin_cu);
14183 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14184 per_objfile->per_bfd->abstract_to_concrete
14185 [origin_die->sect_off].push_back (die->sect_off);
14186 }
14187 }
14188
14189 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14190 reading .debug_rnglists.
14191 Callback's type should be:
14192 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14193 Return true if the attributes are present and valid, otherwise,
14194 return false. */
14195
14196 template <typename Callback>
14197 static bool
14198 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14199 dwarf_tag tag, Callback &&callback)
14200 {
14201 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14202 struct objfile *objfile = per_objfile->objfile;
14203 bfd *obfd = objfile->obfd;
14204 /* Base address selection entry. */
14205 gdb::optional<CORE_ADDR> base;
14206 const gdb_byte *buffer;
14207 CORE_ADDR baseaddr;
14208 bool overflow = false;
14209 ULONGEST addr_index;
14210 struct dwarf2_section_info *rnglists_section;
14211
14212 base = cu->base_address;
14213 rnglists_section = cu_debug_rnglists_section (cu, tag);
14214 rnglists_section->read (objfile);
14215
14216 if (offset >= rnglists_section->size)
14217 {
14218 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14219 offset);
14220 return false;
14221 }
14222 buffer = rnglists_section->buffer + offset;
14223
14224 baseaddr = objfile->text_section_offset ();
14225
14226 while (1)
14227 {
14228 /* Initialize it due to a false compiler warning. */
14229 CORE_ADDR range_beginning = 0, range_end = 0;
14230 const gdb_byte *buf_end = (rnglists_section->buffer
14231 + rnglists_section->size);
14232 unsigned int bytes_read;
14233
14234 if (buffer == buf_end)
14235 {
14236 overflow = true;
14237 break;
14238 }
14239 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14240 switch (rlet)
14241 {
14242 case DW_RLE_end_of_list:
14243 break;
14244 case DW_RLE_base_address:
14245 if (buffer + cu->header.addr_size > buf_end)
14246 {
14247 overflow = true;
14248 break;
14249 }
14250 base = cu->header.read_address (obfd, buffer, &bytes_read);
14251 buffer += bytes_read;
14252 break;
14253 case DW_RLE_base_addressx:
14254 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14255 buffer += bytes_read;
14256 base = read_addr_index (cu, addr_index);
14257 break;
14258 case DW_RLE_start_length:
14259 if (buffer + cu->header.addr_size > buf_end)
14260 {
14261 overflow = true;
14262 break;
14263 }
14264 range_beginning = cu->header.read_address (obfd, buffer,
14265 &bytes_read);
14266 buffer += bytes_read;
14267 range_end = (range_beginning
14268 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14269 buffer += bytes_read;
14270 if (buffer > buf_end)
14271 {
14272 overflow = true;
14273 break;
14274 }
14275 break;
14276 case DW_RLE_startx_length:
14277 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14278 buffer += bytes_read;
14279 range_beginning = read_addr_index (cu, addr_index);
14280 if (buffer > buf_end)
14281 {
14282 overflow = true;
14283 break;
14284 }
14285 range_end = (range_beginning
14286 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14287 buffer += bytes_read;
14288 break;
14289 case DW_RLE_offset_pair:
14290 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14291 buffer += bytes_read;
14292 if (buffer > buf_end)
14293 {
14294 overflow = true;
14295 break;
14296 }
14297 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14298 buffer += bytes_read;
14299 if (buffer > buf_end)
14300 {
14301 overflow = true;
14302 break;
14303 }
14304 break;
14305 case DW_RLE_start_end:
14306 if (buffer + 2 * cu->header.addr_size > buf_end)
14307 {
14308 overflow = true;
14309 break;
14310 }
14311 range_beginning = cu->header.read_address (obfd, buffer,
14312 &bytes_read);
14313 buffer += bytes_read;
14314 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
14315 buffer += bytes_read;
14316 break;
14317 case DW_RLE_startx_endx:
14318 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14319 buffer += bytes_read;
14320 range_beginning = read_addr_index (cu, addr_index);
14321 if (buffer > buf_end)
14322 {
14323 overflow = true;
14324 break;
14325 }
14326 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14327 buffer += bytes_read;
14328 range_end = read_addr_index (cu, addr_index);
14329 break;
14330 default:
14331 complaint (_("Invalid .debug_rnglists data (no base address)"));
14332 return false;
14333 }
14334 if (rlet == DW_RLE_end_of_list || overflow)
14335 break;
14336 if (rlet == DW_RLE_base_address)
14337 continue;
14338
14339 if (range_beginning > range_end)
14340 {
14341 /* Inverted range entries are invalid. */
14342 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14343 return false;
14344 }
14345
14346 /* Empty range entries have no effect. */
14347 if (range_beginning == range_end)
14348 continue;
14349
14350 /* Only DW_RLE_offset_pair needs the base address added. */
14351 if (rlet == DW_RLE_offset_pair)
14352 {
14353 if (!base.has_value ())
14354 {
14355 /* We have no valid base address for the DW_RLE_offset_pair. */
14356 complaint (_("Invalid .debug_rnglists data (no base address for "
14357 "DW_RLE_offset_pair)"));
14358 return false;
14359 }
14360
14361 range_beginning += *base;
14362 range_end += *base;
14363 }
14364
14365 /* A not-uncommon case of bad debug info.
14366 Don't pollute the addrmap with bad data. */
14367 if (range_beginning + baseaddr == 0
14368 && !per_objfile->per_bfd->has_section_at_zero)
14369 {
14370 complaint (_(".debug_rnglists entry has start address of zero"
14371 " [in module %s]"), objfile_name (objfile));
14372 continue;
14373 }
14374
14375 callback (range_beginning, range_end);
14376 }
14377
14378 if (overflow)
14379 {
14380 complaint (_("Offset %d is not terminated "
14381 "for DW_AT_ranges attribute"),
14382 offset);
14383 return false;
14384 }
14385
14386 return true;
14387 }
14388
14389 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14390 Callback's type should be:
14391 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14392 Return 1 if the attributes are present and valid, otherwise, return 0. */
14393
14394 template <typename Callback>
14395 static int
14396 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
14397 Callback &&callback)
14398 {
14399 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14400 struct objfile *objfile = per_objfile->objfile;
14401 struct comp_unit_head *cu_header = &cu->header;
14402 bfd *obfd = objfile->obfd;
14403 unsigned int addr_size = cu_header->addr_size;
14404 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14405 /* Base address selection entry. */
14406 gdb::optional<CORE_ADDR> base;
14407 unsigned int dummy;
14408 const gdb_byte *buffer;
14409 CORE_ADDR baseaddr;
14410
14411 if (cu_header->version >= 5)
14412 return dwarf2_rnglists_process (offset, cu, tag, callback);
14413
14414 base = cu->base_address;
14415
14416 per_objfile->per_bfd->ranges.read (objfile);
14417 if (offset >= per_objfile->per_bfd->ranges.size)
14418 {
14419 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14420 offset);
14421 return 0;
14422 }
14423 buffer = per_objfile->per_bfd->ranges.buffer + offset;
14424
14425 baseaddr = objfile->text_section_offset ();
14426
14427 while (1)
14428 {
14429 CORE_ADDR range_beginning, range_end;
14430
14431 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
14432 buffer += addr_size;
14433 range_end = cu->header.read_address (obfd, buffer, &dummy);
14434 buffer += addr_size;
14435 offset += 2 * addr_size;
14436
14437 /* An end of list marker is a pair of zero addresses. */
14438 if (range_beginning == 0 && range_end == 0)
14439 /* Found the end of list entry. */
14440 break;
14441
14442 /* Each base address selection entry is a pair of 2 values.
14443 The first is the largest possible address, the second is
14444 the base address. Check for a base address here. */
14445 if ((range_beginning & mask) == mask)
14446 {
14447 /* If we found the largest possible address, then we already
14448 have the base address in range_end. */
14449 base = range_end;
14450 continue;
14451 }
14452
14453 if (!base.has_value ())
14454 {
14455 /* We have no valid base address for the ranges
14456 data. */
14457 complaint (_("Invalid .debug_ranges data (no base address)"));
14458 return 0;
14459 }
14460
14461 if (range_beginning > range_end)
14462 {
14463 /* Inverted range entries are invalid. */
14464 complaint (_("Invalid .debug_ranges data (inverted range)"));
14465 return 0;
14466 }
14467
14468 /* Empty range entries have no effect. */
14469 if (range_beginning == range_end)
14470 continue;
14471
14472 range_beginning += *base;
14473 range_end += *base;
14474
14475 /* A not-uncommon case of bad debug info.
14476 Don't pollute the addrmap with bad data. */
14477 if (range_beginning + baseaddr == 0
14478 && !per_objfile->per_bfd->has_section_at_zero)
14479 {
14480 complaint (_(".debug_ranges entry has start address of zero"
14481 " [in module %s]"), objfile_name (objfile));
14482 continue;
14483 }
14484
14485 callback (range_beginning, range_end);
14486 }
14487
14488 return 1;
14489 }
14490
14491 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14492 Return 1 if the attributes are present and valid, otherwise, return 0.
14493 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14494
14495 static int
14496 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14497 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14498 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
14499 {
14500 struct objfile *objfile = cu->per_objfile->objfile;
14501 struct gdbarch *gdbarch = objfile->arch ();
14502 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14503 int low_set = 0;
14504 CORE_ADDR low = 0;
14505 CORE_ADDR high = 0;
14506 int retval;
14507
14508 retval = dwarf2_ranges_process (offset, cu, tag,
14509 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14510 {
14511 if (ranges_pst != NULL)
14512 {
14513 CORE_ADDR lowpc;
14514 CORE_ADDR highpc;
14515
14516 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14517 range_beginning + baseaddr)
14518 - baseaddr);
14519 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14520 range_end + baseaddr)
14521 - baseaddr);
14522 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14523 lowpc, highpc - 1, ranges_pst);
14524 }
14525
14526 /* FIXME: This is recording everything as a low-high
14527 segment of consecutive addresses. We should have a
14528 data structure for discontiguous block ranges
14529 instead. */
14530 if (! low_set)
14531 {
14532 low = range_beginning;
14533 high = range_end;
14534 low_set = 1;
14535 }
14536 else
14537 {
14538 if (range_beginning < low)
14539 low = range_beginning;
14540 if (range_end > high)
14541 high = range_end;
14542 }
14543 });
14544 if (!retval)
14545 return 0;
14546
14547 if (! low_set)
14548 /* If the first entry is an end-of-list marker, the range
14549 describes an empty scope, i.e. no instructions. */
14550 return 0;
14551
14552 if (low_return)
14553 *low_return = low;
14554 if (high_return)
14555 *high_return = high;
14556 return 1;
14557 }
14558
14559 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14560 definition for the return value. *LOWPC and *HIGHPC are set iff
14561 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14562
14563 static enum pc_bounds_kind
14564 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14565 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14566 dwarf2_psymtab *pst)
14567 {
14568 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14569 struct attribute *attr;
14570 struct attribute *attr_high;
14571 CORE_ADDR low = 0;
14572 CORE_ADDR high = 0;
14573 enum pc_bounds_kind ret;
14574
14575 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14576 if (attr_high)
14577 {
14578 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14579 if (attr != nullptr)
14580 {
14581 low = attr->value_as_address ();
14582 high = attr_high->value_as_address ();
14583 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14584 high += low;
14585 }
14586 else
14587 /* Found high w/o low attribute. */
14588 return PC_BOUNDS_INVALID;
14589
14590 /* Found consecutive range of addresses. */
14591 ret = PC_BOUNDS_HIGH_LOW;
14592 }
14593 else
14594 {
14595 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14596 if (attr != NULL)
14597 {
14598 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14599 We take advantage of the fact that DW_AT_ranges does not appear
14600 in DW_TAG_compile_unit of DWO files.
14601
14602 Attributes of the form DW_FORM_rnglistx have already had their
14603 value changed by read_rnglist_index and already include
14604 DW_AT_rnglists_base, so don't need to add the ranges base,
14605 either. */
14606 int need_ranges_base = (die->tag != DW_TAG_compile_unit
14607 && attr->form != DW_FORM_rnglistx);
14608 unsigned int ranges_offset = (DW_UNSND (attr)
14609 + (need_ranges_base
14610 ? cu->ranges_base
14611 : 0));
14612
14613 /* Value of the DW_AT_ranges attribute is the offset in the
14614 .debug_ranges section. */
14615 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14616 die->tag))
14617 return PC_BOUNDS_INVALID;
14618 /* Found discontinuous range of addresses. */
14619 ret = PC_BOUNDS_RANGES;
14620 }
14621 else
14622 return PC_BOUNDS_NOT_PRESENT;
14623 }
14624
14625 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14626 if (high <= low)
14627 return PC_BOUNDS_INVALID;
14628
14629 /* When using the GNU linker, .gnu.linkonce. sections are used to
14630 eliminate duplicate copies of functions and vtables and such.
14631 The linker will arbitrarily choose one and discard the others.
14632 The AT_*_pc values for such functions refer to local labels in
14633 these sections. If the section from that file was discarded, the
14634 labels are not in the output, so the relocs get a value of 0.
14635 If this is a discarded function, mark the pc bounds as invalid,
14636 so that GDB will ignore it. */
14637 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14638 return PC_BOUNDS_INVALID;
14639
14640 *lowpc = low;
14641 if (highpc)
14642 *highpc = high;
14643 return ret;
14644 }
14645
14646 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14647 its low and high PC addresses. Do nothing if these addresses could not
14648 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14649 and HIGHPC to the high address if greater than HIGHPC. */
14650
14651 static void
14652 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14653 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14654 struct dwarf2_cu *cu)
14655 {
14656 CORE_ADDR low, high;
14657 struct die_info *child = die->child;
14658
14659 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14660 {
14661 *lowpc = std::min (*lowpc, low);
14662 *highpc = std::max (*highpc, high);
14663 }
14664
14665 /* If the language does not allow nested subprograms (either inside
14666 subprograms or lexical blocks), we're done. */
14667 if (cu->language != language_ada)
14668 return;
14669
14670 /* Check all the children of the given DIE. If it contains nested
14671 subprograms, then check their pc bounds. Likewise, we need to
14672 check lexical blocks as well, as they may also contain subprogram
14673 definitions. */
14674 while (child && child->tag)
14675 {
14676 if (child->tag == DW_TAG_subprogram
14677 || child->tag == DW_TAG_lexical_block)
14678 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14679 child = child->sibling;
14680 }
14681 }
14682
14683 /* Get the low and high pc's represented by the scope DIE, and store
14684 them in *LOWPC and *HIGHPC. If the correct values can't be
14685 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14686
14687 static void
14688 get_scope_pc_bounds (struct die_info *die,
14689 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14690 struct dwarf2_cu *cu)
14691 {
14692 CORE_ADDR best_low = (CORE_ADDR) -1;
14693 CORE_ADDR best_high = (CORE_ADDR) 0;
14694 CORE_ADDR current_low, current_high;
14695
14696 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14697 >= PC_BOUNDS_RANGES)
14698 {
14699 best_low = current_low;
14700 best_high = current_high;
14701 }
14702 else
14703 {
14704 struct die_info *child = die->child;
14705
14706 while (child && child->tag)
14707 {
14708 switch (child->tag) {
14709 case DW_TAG_subprogram:
14710 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14711 break;
14712 case DW_TAG_namespace:
14713 case DW_TAG_module:
14714 /* FIXME: carlton/2004-01-16: Should we do this for
14715 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14716 that current GCC's always emit the DIEs corresponding
14717 to definitions of methods of classes as children of a
14718 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14719 the DIEs giving the declarations, which could be
14720 anywhere). But I don't see any reason why the
14721 standards says that they have to be there. */
14722 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14723
14724 if (current_low != ((CORE_ADDR) -1))
14725 {
14726 best_low = std::min (best_low, current_low);
14727 best_high = std::max (best_high, current_high);
14728 }
14729 break;
14730 default:
14731 /* Ignore. */
14732 break;
14733 }
14734
14735 child = child->sibling;
14736 }
14737 }
14738
14739 *lowpc = best_low;
14740 *highpc = best_high;
14741 }
14742
14743 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14744 in DIE. */
14745
14746 static void
14747 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14748 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14749 {
14750 struct objfile *objfile = cu->per_objfile->objfile;
14751 struct gdbarch *gdbarch = objfile->arch ();
14752 struct attribute *attr;
14753 struct attribute *attr_high;
14754
14755 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14756 if (attr_high)
14757 {
14758 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14759 if (attr != nullptr)
14760 {
14761 CORE_ADDR low = attr->value_as_address ();
14762 CORE_ADDR high = attr_high->value_as_address ();
14763
14764 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14765 high += low;
14766
14767 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14768 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14769 cu->get_builder ()->record_block_range (block, low, high - 1);
14770 }
14771 }
14772
14773 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14774 if (attr != nullptr)
14775 {
14776 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14777 We take advantage of the fact that DW_AT_ranges does not appear
14778 in DW_TAG_compile_unit of DWO files.
14779
14780 Attributes of the form DW_FORM_rnglistx have already had their
14781 value changed by read_rnglist_index and already include
14782 DW_AT_rnglists_base, so don't need to add the ranges base,
14783 either. */
14784 int need_ranges_base = (die->tag != DW_TAG_compile_unit
14785 && attr->form != DW_FORM_rnglistx);
14786
14787 /* The value of the DW_AT_ranges attribute is the offset of the
14788 address range list in the .debug_ranges section. */
14789 unsigned long offset = (DW_UNSND (attr)
14790 + (need_ranges_base ? cu->ranges_base : 0));
14791
14792 std::vector<blockrange> blockvec;
14793 dwarf2_ranges_process (offset, cu, die->tag,
14794 [&] (CORE_ADDR start, CORE_ADDR end)
14795 {
14796 start += baseaddr;
14797 end += baseaddr;
14798 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14799 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14800 cu->get_builder ()->record_block_range (block, start, end - 1);
14801 blockvec.emplace_back (start, end);
14802 });
14803
14804 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14805 }
14806 }
14807
14808 /* Check whether the producer field indicates either of GCC < 4.6, or the
14809 Intel C/C++ compiler, and cache the result in CU. */
14810
14811 static void
14812 check_producer (struct dwarf2_cu *cu)
14813 {
14814 int major, minor;
14815
14816 if (cu->producer == NULL)
14817 {
14818 /* For unknown compilers expect their behavior is DWARF version
14819 compliant.
14820
14821 GCC started to support .debug_types sections by -gdwarf-4 since
14822 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14823 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14824 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14825 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14826 }
14827 else if (producer_is_gcc (cu->producer, &major, &minor))
14828 {
14829 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14830 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14831 }
14832 else if (producer_is_icc (cu->producer, &major, &minor))
14833 {
14834 cu->producer_is_icc = true;
14835 cu->producer_is_icc_lt_14 = major < 14;
14836 }
14837 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14838 cu->producer_is_codewarrior = true;
14839 else
14840 {
14841 /* For other non-GCC compilers, expect their behavior is DWARF version
14842 compliant. */
14843 }
14844
14845 cu->checked_producer = true;
14846 }
14847
14848 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14849 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14850 during 4.6.0 experimental. */
14851
14852 static bool
14853 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14854 {
14855 if (!cu->checked_producer)
14856 check_producer (cu);
14857
14858 return cu->producer_is_gxx_lt_4_6;
14859 }
14860
14861
14862 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14863 with incorrect is_stmt attributes. */
14864
14865 static bool
14866 producer_is_codewarrior (struct dwarf2_cu *cu)
14867 {
14868 if (!cu->checked_producer)
14869 check_producer (cu);
14870
14871 return cu->producer_is_codewarrior;
14872 }
14873
14874 /* Return the default accessibility type if it is not overridden by
14875 DW_AT_accessibility. */
14876
14877 static enum dwarf_access_attribute
14878 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14879 {
14880 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14881 {
14882 /* The default DWARF 2 accessibility for members is public, the default
14883 accessibility for inheritance is private. */
14884
14885 if (die->tag != DW_TAG_inheritance)
14886 return DW_ACCESS_public;
14887 else
14888 return DW_ACCESS_private;
14889 }
14890 else
14891 {
14892 /* DWARF 3+ defines the default accessibility a different way. The same
14893 rules apply now for DW_TAG_inheritance as for the members and it only
14894 depends on the container kind. */
14895
14896 if (die->parent->tag == DW_TAG_class_type)
14897 return DW_ACCESS_private;
14898 else
14899 return DW_ACCESS_public;
14900 }
14901 }
14902
14903 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14904 offset. If the attribute was not found return 0, otherwise return
14905 1. If it was found but could not properly be handled, set *OFFSET
14906 to 0. */
14907
14908 static int
14909 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14910 LONGEST *offset)
14911 {
14912 struct attribute *attr;
14913
14914 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14915 if (attr != NULL)
14916 {
14917 *offset = 0;
14918
14919 /* Note that we do not check for a section offset first here.
14920 This is because DW_AT_data_member_location is new in DWARF 4,
14921 so if we see it, we can assume that a constant form is really
14922 a constant and not a section offset. */
14923 if (attr->form_is_constant ())
14924 *offset = attr->constant_value (0);
14925 else if (attr->form_is_section_offset ())
14926 dwarf2_complex_location_expr_complaint ();
14927 else if (attr->form_is_block ())
14928 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14929 else
14930 dwarf2_complex_location_expr_complaint ();
14931
14932 return 1;
14933 }
14934
14935 return 0;
14936 }
14937
14938 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14939
14940 static void
14941 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14942 struct field *field)
14943 {
14944 struct attribute *attr;
14945
14946 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14947 if (attr != NULL)
14948 {
14949 if (attr->form_is_constant ())
14950 {
14951 LONGEST offset = attr->constant_value (0);
14952 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14953 }
14954 else if (attr->form_is_section_offset ())
14955 dwarf2_complex_location_expr_complaint ();
14956 else if (attr->form_is_block ())
14957 {
14958 bool handled;
14959 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14960 if (handled)
14961 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14962 else
14963 {
14964 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14965 struct objfile *objfile = per_objfile->objfile;
14966 struct dwarf2_locexpr_baton *dlbaton
14967 = XOBNEW (&objfile->objfile_obstack,
14968 struct dwarf2_locexpr_baton);
14969 dlbaton->data = DW_BLOCK (attr)->data;
14970 dlbaton->size = DW_BLOCK (attr)->size;
14971 /* When using this baton, we want to compute the address
14972 of the field, not the value. This is why
14973 is_reference is set to false here. */
14974 dlbaton->is_reference = false;
14975 dlbaton->per_objfile = per_objfile;
14976 dlbaton->per_cu = cu->per_cu;
14977
14978 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14979 }
14980 }
14981 else
14982 dwarf2_complex_location_expr_complaint ();
14983 }
14984 }
14985
14986 /* Add an aggregate field to the field list. */
14987
14988 static void
14989 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14990 struct dwarf2_cu *cu)
14991 {
14992 struct objfile *objfile = cu->per_objfile->objfile;
14993 struct gdbarch *gdbarch = objfile->arch ();
14994 struct nextfield *new_field;
14995 struct attribute *attr;
14996 struct field *fp;
14997 const char *fieldname = "";
14998
14999 if (die->tag == DW_TAG_inheritance)
15000 {
15001 fip->baseclasses.emplace_back ();
15002 new_field = &fip->baseclasses.back ();
15003 }
15004 else
15005 {
15006 fip->fields.emplace_back ();
15007 new_field = &fip->fields.back ();
15008 }
15009
15010 new_field->offset = die->sect_off;
15011
15012 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15013 if (attr != nullptr)
15014 new_field->accessibility = DW_UNSND (attr);
15015 else
15016 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15017 if (new_field->accessibility != DW_ACCESS_public)
15018 fip->non_public_fields = true;
15019
15020 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15021 if (attr != nullptr)
15022 new_field->virtuality = DW_UNSND (attr);
15023 else
15024 new_field->virtuality = DW_VIRTUALITY_none;
15025
15026 fp = &new_field->field;
15027
15028 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15029 {
15030 /* Data member other than a C++ static data member. */
15031
15032 /* Get type of field. */
15033 fp->set_type (die_type (die, cu));
15034
15035 SET_FIELD_BITPOS (*fp, 0);
15036
15037 /* Get bit size of field (zero if none). */
15038 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15039 if (attr != nullptr)
15040 {
15041 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15042 }
15043 else
15044 {
15045 FIELD_BITSIZE (*fp) = 0;
15046 }
15047
15048 /* Get bit offset of field. */
15049 handle_data_member_location (die, cu, fp);
15050 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15051 if (attr != nullptr)
15052 {
15053 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
15054 {
15055 /* For big endian bits, the DW_AT_bit_offset gives the
15056 additional bit offset from the MSB of the containing
15057 anonymous object to the MSB of the field. We don't
15058 have to do anything special since we don't need to
15059 know the size of the anonymous object. */
15060 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15061 }
15062 else
15063 {
15064 /* For little endian bits, compute the bit offset to the
15065 MSB of the anonymous object, subtract off the number of
15066 bits from the MSB of the field to the MSB of the
15067 object, and then subtract off the number of bits of
15068 the field itself. The result is the bit offset of
15069 the LSB of the field. */
15070 int anonymous_size;
15071 int bit_offset = DW_UNSND (attr);
15072
15073 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15074 if (attr != nullptr)
15075 {
15076 /* The size of the anonymous object containing
15077 the bit field is explicit, so use the
15078 indicated size (in bytes). */
15079 anonymous_size = DW_UNSND (attr);
15080 }
15081 else
15082 {
15083 /* The size of the anonymous object containing
15084 the bit field must be inferred from the type
15085 attribute of the data member containing the
15086 bit field. */
15087 anonymous_size = TYPE_LENGTH (fp->type ());
15088 }
15089 SET_FIELD_BITPOS (*fp,
15090 (FIELD_BITPOS (*fp)
15091 + anonymous_size * bits_per_byte
15092 - bit_offset - FIELD_BITSIZE (*fp)));
15093 }
15094 }
15095 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15096 if (attr != NULL)
15097 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15098 + attr->constant_value (0)));
15099
15100 /* Get name of field. */
15101 fieldname = dwarf2_name (die, cu);
15102 if (fieldname == NULL)
15103 fieldname = "";
15104
15105 /* The name is already allocated along with this objfile, so we don't
15106 need to duplicate it for the type. */
15107 fp->name = fieldname;
15108
15109 /* Change accessibility for artificial fields (e.g. virtual table
15110 pointer or virtual base class pointer) to private. */
15111 if (dwarf2_attr (die, DW_AT_artificial, cu))
15112 {
15113 FIELD_ARTIFICIAL (*fp) = 1;
15114 new_field->accessibility = DW_ACCESS_private;
15115 fip->non_public_fields = true;
15116 }
15117 }
15118 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15119 {
15120 /* C++ static member. */
15121
15122 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15123 is a declaration, but all versions of G++ as of this writing
15124 (so through at least 3.2.1) incorrectly generate
15125 DW_TAG_variable tags. */
15126
15127 const char *physname;
15128
15129 /* Get name of field. */
15130 fieldname = dwarf2_name (die, cu);
15131 if (fieldname == NULL)
15132 return;
15133
15134 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15135 if (attr
15136 /* Only create a symbol if this is an external value.
15137 new_symbol checks this and puts the value in the global symbol
15138 table, which we want. If it is not external, new_symbol
15139 will try to put the value in cu->list_in_scope which is wrong. */
15140 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15141 {
15142 /* A static const member, not much different than an enum as far as
15143 we're concerned, except that we can support more types. */
15144 new_symbol (die, NULL, cu);
15145 }
15146
15147 /* Get physical name. */
15148 physname = dwarf2_physname (fieldname, die, cu);
15149
15150 /* The name is already allocated along with this objfile, so we don't
15151 need to duplicate it for the type. */
15152 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15153 fp->set_type (die_type (die, cu));
15154 FIELD_NAME (*fp) = fieldname;
15155 }
15156 else if (die->tag == DW_TAG_inheritance)
15157 {
15158 /* C++ base class field. */
15159 handle_data_member_location (die, cu, fp);
15160 FIELD_BITSIZE (*fp) = 0;
15161 fp->set_type (die_type (die, cu));
15162 FIELD_NAME (*fp) = fp->type ()->name ();
15163 }
15164 else
15165 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15166 }
15167
15168 /* Can the type given by DIE define another type? */
15169
15170 static bool
15171 type_can_define_types (const struct die_info *die)
15172 {
15173 switch (die->tag)
15174 {
15175 case DW_TAG_typedef:
15176 case DW_TAG_class_type:
15177 case DW_TAG_structure_type:
15178 case DW_TAG_union_type:
15179 case DW_TAG_enumeration_type:
15180 return true;
15181
15182 default:
15183 return false;
15184 }
15185 }
15186
15187 /* Add a type definition defined in the scope of the FIP's class. */
15188
15189 static void
15190 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15191 struct dwarf2_cu *cu)
15192 {
15193 struct decl_field fp;
15194 memset (&fp, 0, sizeof (fp));
15195
15196 gdb_assert (type_can_define_types (die));
15197
15198 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15199 fp.name = dwarf2_name (die, cu);
15200 fp.type = read_type_die (die, cu);
15201
15202 /* Save accessibility. */
15203 enum dwarf_access_attribute accessibility;
15204 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15205 if (attr != NULL)
15206 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15207 else
15208 accessibility = dwarf2_default_access_attribute (die, cu);
15209 switch (accessibility)
15210 {
15211 case DW_ACCESS_public:
15212 /* The assumed value if neither private nor protected. */
15213 break;
15214 case DW_ACCESS_private:
15215 fp.is_private = 1;
15216 break;
15217 case DW_ACCESS_protected:
15218 fp.is_protected = 1;
15219 break;
15220 default:
15221 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15222 }
15223
15224 if (die->tag == DW_TAG_typedef)
15225 fip->typedef_field_list.push_back (fp);
15226 else
15227 fip->nested_types_list.push_back (fp);
15228 }
15229
15230 /* A convenience typedef that's used when finding the discriminant
15231 field for a variant part. */
15232 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
15233 offset_map_type;
15234
15235 /* Compute the discriminant range for a given variant. OBSTACK is
15236 where the results will be stored. VARIANT is the variant to
15237 process. IS_UNSIGNED indicates whether the discriminant is signed
15238 or unsigned. */
15239
15240 static const gdb::array_view<discriminant_range>
15241 convert_variant_range (struct obstack *obstack, const variant_field &variant,
15242 bool is_unsigned)
15243 {
15244 std::vector<discriminant_range> ranges;
15245
15246 if (variant.default_branch)
15247 return {};
15248
15249 if (variant.discr_list_data == nullptr)
15250 {
15251 discriminant_range r
15252 = {variant.discriminant_value, variant.discriminant_value};
15253 ranges.push_back (r);
15254 }
15255 else
15256 {
15257 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15258 variant.discr_list_data->size);
15259 while (!data.empty ())
15260 {
15261 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15262 {
15263 complaint (_("invalid discriminant marker: %d"), data[0]);
15264 break;
15265 }
15266 bool is_range = data[0] == DW_DSC_range;
15267 data = data.slice (1);
15268
15269 ULONGEST low, high;
15270 unsigned int bytes_read;
15271
15272 if (data.empty ())
15273 {
15274 complaint (_("DW_AT_discr_list missing low value"));
15275 break;
15276 }
15277 if (is_unsigned)
15278 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15279 else
15280 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15281 &bytes_read);
15282 data = data.slice (bytes_read);
15283
15284 if (is_range)
15285 {
15286 if (data.empty ())
15287 {
15288 complaint (_("DW_AT_discr_list missing high value"));
15289 break;
15290 }
15291 if (is_unsigned)
15292 high = read_unsigned_leb128 (nullptr, data.data (),
15293 &bytes_read);
15294 else
15295 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15296 &bytes_read);
15297 data = data.slice (bytes_read);
15298 }
15299 else
15300 high = low;
15301
15302 ranges.push_back ({ low, high });
15303 }
15304 }
15305
15306 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15307 ranges.size ());
15308 std::copy (ranges.begin (), ranges.end (), result);
15309 return gdb::array_view<discriminant_range> (result, ranges.size ());
15310 }
15311
15312 static const gdb::array_view<variant_part> create_variant_parts
15313 (struct obstack *obstack,
15314 const offset_map_type &offset_map,
15315 struct field_info *fi,
15316 const std::vector<variant_part_builder> &variant_parts);
15317
15318 /* Fill in a "struct variant" for a given variant field. RESULT is
15319 the variant to fill in. OBSTACK is where any needed allocations
15320 will be done. OFFSET_MAP holds the mapping from section offsets to
15321 fields for the type. FI describes the fields of the type we're
15322 processing. FIELD is the variant field we're converting. */
15323
15324 static void
15325 create_one_variant (variant &result, struct obstack *obstack,
15326 const offset_map_type &offset_map,
15327 struct field_info *fi, const variant_field &field)
15328 {
15329 result.discriminants = convert_variant_range (obstack, field, false);
15330 result.first_field = field.first_field + fi->baseclasses.size ();
15331 result.last_field = field.last_field + fi->baseclasses.size ();
15332 result.parts = create_variant_parts (obstack, offset_map, fi,
15333 field.variant_parts);
15334 }
15335
15336 /* Fill in a "struct variant_part" for a given variant part. RESULT
15337 is the variant part to fill in. OBSTACK is where any needed
15338 allocations will be done. OFFSET_MAP holds the mapping from
15339 section offsets to fields for the type. FI describes the fields of
15340 the type we're processing. BUILDER is the variant part to be
15341 converted. */
15342
15343 static void
15344 create_one_variant_part (variant_part &result,
15345 struct obstack *obstack,
15346 const offset_map_type &offset_map,
15347 struct field_info *fi,
15348 const variant_part_builder &builder)
15349 {
15350 auto iter = offset_map.find (builder.discriminant_offset);
15351 if (iter == offset_map.end ())
15352 {
15353 result.discriminant_index = -1;
15354 /* Doesn't matter. */
15355 result.is_unsigned = false;
15356 }
15357 else
15358 {
15359 result.discriminant_index = iter->second;
15360 result.is_unsigned
15361 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
15362 }
15363
15364 size_t n = builder.variants.size ();
15365 variant *output = new (obstack) variant[n];
15366 for (size_t i = 0; i < n; ++i)
15367 create_one_variant (output[i], obstack, offset_map, fi,
15368 builder.variants[i]);
15369
15370 result.variants = gdb::array_view<variant> (output, n);
15371 }
15372
15373 /* Create a vector of variant parts that can be attached to a type.
15374 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15375 holds the mapping from section offsets to fields for the type. FI
15376 describes the fields of the type we're processing. VARIANT_PARTS
15377 is the vector to convert. */
15378
15379 static const gdb::array_view<variant_part>
15380 create_variant_parts (struct obstack *obstack,
15381 const offset_map_type &offset_map,
15382 struct field_info *fi,
15383 const std::vector<variant_part_builder> &variant_parts)
15384 {
15385 if (variant_parts.empty ())
15386 return {};
15387
15388 size_t n = variant_parts.size ();
15389 variant_part *result = new (obstack) variant_part[n];
15390 for (size_t i = 0; i < n; ++i)
15391 create_one_variant_part (result[i], obstack, offset_map, fi,
15392 variant_parts[i]);
15393
15394 return gdb::array_view<variant_part> (result, n);
15395 }
15396
15397 /* Compute the variant part vector for FIP, attaching it to TYPE when
15398 done. */
15399
15400 static void
15401 add_variant_property (struct field_info *fip, struct type *type,
15402 struct dwarf2_cu *cu)
15403 {
15404 /* Map section offsets of fields to their field index. Note the
15405 field index here does not take the number of baseclasses into
15406 account. */
15407 offset_map_type offset_map;
15408 for (int i = 0; i < fip->fields.size (); ++i)
15409 offset_map[fip->fields[i].offset] = i;
15410
15411 struct objfile *objfile = cu->per_objfile->objfile;
15412 gdb::array_view<variant_part> parts
15413 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15414 fip->variant_parts);
15415
15416 struct dynamic_prop prop;
15417 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15418 obstack_copy (&objfile->objfile_obstack, &parts,
15419 sizeof (parts)));
15420
15421 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
15422 }
15423
15424 /* Create the vector of fields, and attach it to the type. */
15425
15426 static void
15427 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15428 struct dwarf2_cu *cu)
15429 {
15430 int nfields = fip->nfields ();
15431
15432 /* Record the field count, allocate space for the array of fields,
15433 and create blank accessibility bitfields if necessary. */
15434 type->set_num_fields (nfields);
15435 type->set_fields
15436 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
15437
15438 if (fip->non_public_fields && cu->language != language_ada)
15439 {
15440 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15441
15442 TYPE_FIELD_PRIVATE_BITS (type) =
15443 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15444 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15445
15446 TYPE_FIELD_PROTECTED_BITS (type) =
15447 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15448 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15449
15450 TYPE_FIELD_IGNORE_BITS (type) =
15451 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15452 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15453 }
15454
15455 /* If the type has baseclasses, allocate and clear a bit vector for
15456 TYPE_FIELD_VIRTUAL_BITS. */
15457 if (!fip->baseclasses.empty () && cu->language != language_ada)
15458 {
15459 int num_bytes = B_BYTES (fip->baseclasses.size ());
15460 unsigned char *pointer;
15461
15462 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15463 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15464 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15465 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15466 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15467 }
15468
15469 if (!fip->variant_parts.empty ())
15470 add_variant_property (fip, type, cu);
15471
15472 /* Copy the saved-up fields into the field vector. */
15473 for (int i = 0; i < nfields; ++i)
15474 {
15475 struct nextfield &field
15476 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15477 : fip->fields[i - fip->baseclasses.size ()]);
15478
15479 type->field (i) = field.field;
15480 switch (field.accessibility)
15481 {
15482 case DW_ACCESS_private:
15483 if (cu->language != language_ada)
15484 SET_TYPE_FIELD_PRIVATE (type, i);
15485 break;
15486
15487 case DW_ACCESS_protected:
15488 if (cu->language != language_ada)
15489 SET_TYPE_FIELD_PROTECTED (type, i);
15490 break;
15491
15492 case DW_ACCESS_public:
15493 break;
15494
15495 default:
15496 /* Unknown accessibility. Complain and treat it as public. */
15497 {
15498 complaint (_("unsupported accessibility %d"),
15499 field.accessibility);
15500 }
15501 break;
15502 }
15503 if (i < fip->baseclasses.size ())
15504 {
15505 switch (field.virtuality)
15506 {
15507 case DW_VIRTUALITY_virtual:
15508 case DW_VIRTUALITY_pure_virtual:
15509 if (cu->language == language_ada)
15510 error (_("unexpected virtuality in component of Ada type"));
15511 SET_TYPE_FIELD_VIRTUAL (type, i);
15512 break;
15513 }
15514 }
15515 }
15516 }
15517
15518 /* Return true if this member function is a constructor, false
15519 otherwise. */
15520
15521 static int
15522 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15523 {
15524 const char *fieldname;
15525 const char *type_name;
15526 int len;
15527
15528 if (die->parent == NULL)
15529 return 0;
15530
15531 if (die->parent->tag != DW_TAG_structure_type
15532 && die->parent->tag != DW_TAG_union_type
15533 && die->parent->tag != DW_TAG_class_type)
15534 return 0;
15535
15536 fieldname = dwarf2_name (die, cu);
15537 type_name = dwarf2_name (die->parent, cu);
15538 if (fieldname == NULL || type_name == NULL)
15539 return 0;
15540
15541 len = strlen (fieldname);
15542 return (strncmp (fieldname, type_name, len) == 0
15543 && (type_name[len] == '\0' || type_name[len] == '<'));
15544 }
15545
15546 /* Check if the given VALUE is a recognized enum
15547 dwarf_defaulted_attribute constant according to DWARF5 spec,
15548 Table 7.24. */
15549
15550 static bool
15551 is_valid_DW_AT_defaulted (ULONGEST value)
15552 {
15553 switch (value)
15554 {
15555 case DW_DEFAULTED_no:
15556 case DW_DEFAULTED_in_class:
15557 case DW_DEFAULTED_out_of_class:
15558 return true;
15559 }
15560
15561 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
15562 return false;
15563 }
15564
15565 /* Add a member function to the proper fieldlist. */
15566
15567 static void
15568 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15569 struct type *type, struct dwarf2_cu *cu)
15570 {
15571 struct objfile *objfile = cu->per_objfile->objfile;
15572 struct attribute *attr;
15573 int i;
15574 struct fnfieldlist *flp = nullptr;
15575 struct fn_field *fnp;
15576 const char *fieldname;
15577 struct type *this_type;
15578 enum dwarf_access_attribute accessibility;
15579
15580 if (cu->language == language_ada)
15581 error (_("unexpected member function in Ada type"));
15582
15583 /* Get name of member function. */
15584 fieldname = dwarf2_name (die, cu);
15585 if (fieldname == NULL)
15586 return;
15587
15588 /* Look up member function name in fieldlist. */
15589 for (i = 0; i < fip->fnfieldlists.size (); i++)
15590 {
15591 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15592 {
15593 flp = &fip->fnfieldlists[i];
15594 break;
15595 }
15596 }
15597
15598 /* Create a new fnfieldlist if necessary. */
15599 if (flp == nullptr)
15600 {
15601 fip->fnfieldlists.emplace_back ();
15602 flp = &fip->fnfieldlists.back ();
15603 flp->name = fieldname;
15604 i = fip->fnfieldlists.size () - 1;
15605 }
15606
15607 /* Create a new member function field and add it to the vector of
15608 fnfieldlists. */
15609 flp->fnfields.emplace_back ();
15610 fnp = &flp->fnfields.back ();
15611
15612 /* Delay processing of the physname until later. */
15613 if (cu->language == language_cplus)
15614 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15615 die, cu);
15616 else
15617 {
15618 const char *physname = dwarf2_physname (fieldname, die, cu);
15619 fnp->physname = physname ? physname : "";
15620 }
15621
15622 fnp->type = alloc_type (objfile);
15623 this_type = read_type_die (die, cu);
15624 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15625 {
15626 int nparams = this_type->num_fields ();
15627
15628 /* TYPE is the domain of this method, and THIS_TYPE is the type
15629 of the method itself (TYPE_CODE_METHOD). */
15630 smash_to_method_type (fnp->type, type,
15631 TYPE_TARGET_TYPE (this_type),
15632 this_type->fields (),
15633 this_type->num_fields (),
15634 this_type->has_varargs ());
15635
15636 /* Handle static member functions.
15637 Dwarf2 has no clean way to discern C++ static and non-static
15638 member functions. G++ helps GDB by marking the first
15639 parameter for non-static member functions (which is the this
15640 pointer) as artificial. We obtain this information from
15641 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15642 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15643 fnp->voffset = VOFFSET_STATIC;
15644 }
15645 else
15646 complaint (_("member function type missing for '%s'"),
15647 dwarf2_full_name (fieldname, die, cu));
15648
15649 /* Get fcontext from DW_AT_containing_type if present. */
15650 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15651 fnp->fcontext = die_containing_type (die, cu);
15652
15653 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15654 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15655
15656 /* Get accessibility. */
15657 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15658 if (attr != nullptr)
15659 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15660 else
15661 accessibility = dwarf2_default_access_attribute (die, cu);
15662 switch (accessibility)
15663 {
15664 case DW_ACCESS_private:
15665 fnp->is_private = 1;
15666 break;
15667 case DW_ACCESS_protected:
15668 fnp->is_protected = 1;
15669 break;
15670 }
15671
15672 /* Check for artificial methods. */
15673 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15674 if (attr && DW_UNSND (attr) != 0)
15675 fnp->is_artificial = 1;
15676
15677 /* Check for defaulted methods. */
15678 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15679 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15680 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15681
15682 /* Check for deleted methods. */
15683 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15684 if (attr != nullptr && DW_UNSND (attr) != 0)
15685 fnp->is_deleted = 1;
15686
15687 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15688
15689 /* Get index in virtual function table if it is a virtual member
15690 function. For older versions of GCC, this is an offset in the
15691 appropriate virtual table, as specified by DW_AT_containing_type.
15692 For everyone else, it is an expression to be evaluated relative
15693 to the object address. */
15694
15695 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15696 if (attr != nullptr)
15697 {
15698 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
15699 {
15700 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15701 {
15702 /* Old-style GCC. */
15703 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15704 }
15705 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15706 || (DW_BLOCK (attr)->size > 1
15707 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15708 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15709 {
15710 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15711 if ((fnp->voffset % cu->header.addr_size) != 0)
15712 dwarf2_complex_location_expr_complaint ();
15713 else
15714 fnp->voffset /= cu->header.addr_size;
15715 fnp->voffset += 2;
15716 }
15717 else
15718 dwarf2_complex_location_expr_complaint ();
15719
15720 if (!fnp->fcontext)
15721 {
15722 /* If there is no `this' field and no DW_AT_containing_type,
15723 we cannot actually find a base class context for the
15724 vtable! */
15725 if (this_type->num_fields () == 0
15726 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15727 {
15728 complaint (_("cannot determine context for virtual member "
15729 "function \"%s\" (offset %s)"),
15730 fieldname, sect_offset_str (die->sect_off));
15731 }
15732 else
15733 {
15734 fnp->fcontext
15735 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15736 }
15737 }
15738 }
15739 else if (attr->form_is_section_offset ())
15740 {
15741 dwarf2_complex_location_expr_complaint ();
15742 }
15743 else
15744 {
15745 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15746 fieldname);
15747 }
15748 }
15749 else
15750 {
15751 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15752 if (attr && DW_UNSND (attr))
15753 {
15754 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15755 complaint (_("Member function \"%s\" (offset %s) is virtual "
15756 "but the vtable offset is not specified"),
15757 fieldname, sect_offset_str (die->sect_off));
15758 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15759 TYPE_CPLUS_DYNAMIC (type) = 1;
15760 }
15761 }
15762 }
15763
15764 /* Create the vector of member function fields, and attach it to the type. */
15765
15766 static void
15767 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15768 struct dwarf2_cu *cu)
15769 {
15770 if (cu->language == language_ada)
15771 error (_("unexpected member functions in Ada type"));
15772
15773 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15774 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15775 TYPE_ALLOC (type,
15776 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15777
15778 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15779 {
15780 struct fnfieldlist &nf = fip->fnfieldlists[i];
15781 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15782
15783 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15784 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15785 fn_flp->fn_fields = (struct fn_field *)
15786 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15787
15788 for (int k = 0; k < nf.fnfields.size (); ++k)
15789 fn_flp->fn_fields[k] = nf.fnfields[k];
15790 }
15791
15792 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15793 }
15794
15795 /* Returns non-zero if NAME is the name of a vtable member in CU's
15796 language, zero otherwise. */
15797 static int
15798 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15799 {
15800 static const char vptr[] = "_vptr";
15801
15802 /* Look for the C++ form of the vtable. */
15803 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15804 return 1;
15805
15806 return 0;
15807 }
15808
15809 /* GCC outputs unnamed structures that are really pointers to member
15810 functions, with the ABI-specified layout. If TYPE describes
15811 such a structure, smash it into a member function type.
15812
15813 GCC shouldn't do this; it should just output pointer to member DIEs.
15814 This is GCC PR debug/28767. */
15815
15816 static void
15817 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15818 {
15819 struct type *pfn_type, *self_type, *new_type;
15820
15821 /* Check for a structure with no name and two children. */
15822 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15823 return;
15824
15825 /* Check for __pfn and __delta members. */
15826 if (TYPE_FIELD_NAME (type, 0) == NULL
15827 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15828 || TYPE_FIELD_NAME (type, 1) == NULL
15829 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15830 return;
15831
15832 /* Find the type of the method. */
15833 pfn_type = type->field (0).type ();
15834 if (pfn_type == NULL
15835 || pfn_type->code () != TYPE_CODE_PTR
15836 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15837 return;
15838
15839 /* Look for the "this" argument. */
15840 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15841 if (pfn_type->num_fields () == 0
15842 /* || pfn_type->field (0).type () == NULL */
15843 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15844 return;
15845
15846 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15847 new_type = alloc_type (objfile);
15848 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15849 pfn_type->fields (), pfn_type->num_fields (),
15850 pfn_type->has_varargs ());
15851 smash_to_methodptr_type (type, new_type);
15852 }
15853
15854 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15855 appropriate error checking and issuing complaints if there is a
15856 problem. */
15857
15858 static ULONGEST
15859 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15860 {
15861 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15862
15863 if (attr == nullptr)
15864 return 0;
15865
15866 if (!attr->form_is_constant ())
15867 {
15868 complaint (_("DW_AT_alignment must have constant form"
15869 " - DIE at %s [in module %s]"),
15870 sect_offset_str (die->sect_off),
15871 objfile_name (cu->per_objfile->objfile));
15872 return 0;
15873 }
15874
15875 ULONGEST align;
15876 if (attr->form == DW_FORM_sdata)
15877 {
15878 LONGEST val = DW_SND (attr);
15879 if (val < 0)
15880 {
15881 complaint (_("DW_AT_alignment value must not be negative"
15882 " - DIE at %s [in module %s]"),
15883 sect_offset_str (die->sect_off),
15884 objfile_name (cu->per_objfile->objfile));
15885 return 0;
15886 }
15887 align = val;
15888 }
15889 else
15890 align = DW_UNSND (attr);
15891
15892 if (align == 0)
15893 {
15894 complaint (_("DW_AT_alignment value must not be zero"
15895 " - DIE at %s [in module %s]"),
15896 sect_offset_str (die->sect_off),
15897 objfile_name (cu->per_objfile->objfile));
15898 return 0;
15899 }
15900 if ((align & (align - 1)) != 0)
15901 {
15902 complaint (_("DW_AT_alignment value must be a power of 2"
15903 " - DIE at %s [in module %s]"),
15904 sect_offset_str (die->sect_off),
15905 objfile_name (cu->per_objfile->objfile));
15906 return 0;
15907 }
15908
15909 return align;
15910 }
15911
15912 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15913 the alignment for TYPE. */
15914
15915 static void
15916 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15917 struct type *type)
15918 {
15919 if (!set_type_align (type, get_alignment (cu, die)))
15920 complaint (_("DW_AT_alignment value too large"
15921 " - DIE at %s [in module %s]"),
15922 sect_offset_str (die->sect_off),
15923 objfile_name (cu->per_objfile->objfile));
15924 }
15925
15926 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15927 constant for a type, according to DWARF5 spec, Table 5.5. */
15928
15929 static bool
15930 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15931 {
15932 switch (value)
15933 {
15934 case DW_CC_normal:
15935 case DW_CC_pass_by_reference:
15936 case DW_CC_pass_by_value:
15937 return true;
15938
15939 default:
15940 complaint (_("unrecognized DW_AT_calling_convention value "
15941 "(%s) for a type"), pulongest (value));
15942 return false;
15943 }
15944 }
15945
15946 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15947 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15948 also according to GNU-specific values (see include/dwarf2.h). */
15949
15950 static bool
15951 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15952 {
15953 switch (value)
15954 {
15955 case DW_CC_normal:
15956 case DW_CC_program:
15957 case DW_CC_nocall:
15958 return true;
15959
15960 case DW_CC_GNU_renesas_sh:
15961 case DW_CC_GNU_borland_fastcall_i386:
15962 case DW_CC_GDB_IBM_OpenCL:
15963 return true;
15964
15965 default:
15966 complaint (_("unrecognized DW_AT_calling_convention value "
15967 "(%s) for a subroutine"), pulongest (value));
15968 return false;
15969 }
15970 }
15971
15972 /* Called when we find the DIE that starts a structure or union scope
15973 (definition) to create a type for the structure or union. Fill in
15974 the type's name and general properties; the members will not be
15975 processed until process_structure_scope. A symbol table entry for
15976 the type will also not be done until process_structure_scope (assuming
15977 the type has a name).
15978
15979 NOTE: we need to call these functions regardless of whether or not the
15980 DIE has a DW_AT_name attribute, since it might be an anonymous
15981 structure or union. This gets the type entered into our set of
15982 user defined types. */
15983
15984 static struct type *
15985 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15986 {
15987 struct objfile *objfile = cu->per_objfile->objfile;
15988 struct type *type;
15989 struct attribute *attr;
15990 const char *name;
15991
15992 /* If the definition of this type lives in .debug_types, read that type.
15993 Don't follow DW_AT_specification though, that will take us back up
15994 the chain and we want to go down. */
15995 attr = die->attr (DW_AT_signature);
15996 if (attr != nullptr)
15997 {
15998 type = get_DW_AT_signature_type (die, attr, cu);
15999
16000 /* The type's CU may not be the same as CU.
16001 Ensure TYPE is recorded with CU in die_type_hash. */
16002 return set_die_type (die, type, cu);
16003 }
16004
16005 type = alloc_type (objfile);
16006 INIT_CPLUS_SPECIFIC (type);
16007
16008 name = dwarf2_name (die, cu);
16009 if (name != NULL)
16010 {
16011 if (cu->language == language_cplus
16012 || cu->language == language_d
16013 || cu->language == language_rust)
16014 {
16015 const char *full_name = dwarf2_full_name (name, die, cu);
16016
16017 /* dwarf2_full_name might have already finished building the DIE's
16018 type. If so, there is no need to continue. */
16019 if (get_die_type (die, cu) != NULL)
16020 return get_die_type (die, cu);
16021
16022 type->set_name (full_name);
16023 }
16024 else
16025 {
16026 /* The name is already allocated along with this objfile, so
16027 we don't need to duplicate it for the type. */
16028 type->set_name (name);
16029 }
16030 }
16031
16032 if (die->tag == DW_TAG_structure_type)
16033 {
16034 type->set_code (TYPE_CODE_STRUCT);
16035 }
16036 else if (die->tag == DW_TAG_union_type)
16037 {
16038 type->set_code (TYPE_CODE_UNION);
16039 }
16040 else
16041 {
16042 type->set_code (TYPE_CODE_STRUCT);
16043 }
16044
16045 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16046 TYPE_DECLARED_CLASS (type) = 1;
16047
16048 /* Store the calling convention in the type if it's available in
16049 the die. Otherwise the calling convention remains set to
16050 the default value DW_CC_normal. */
16051 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16052 if (attr != nullptr
16053 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
16054 {
16055 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16056 TYPE_CPLUS_CALLING_CONVENTION (type)
16057 = (enum dwarf_calling_convention) (DW_UNSND (attr));
16058 }
16059
16060 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16061 if (attr != nullptr)
16062 {
16063 if (attr->form_is_constant ())
16064 TYPE_LENGTH (type) = DW_UNSND (attr);
16065 else
16066 {
16067 struct dynamic_prop prop;
16068 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
16069 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
16070 TYPE_LENGTH (type) = 0;
16071 }
16072 }
16073 else
16074 {
16075 TYPE_LENGTH (type) = 0;
16076 }
16077
16078 maybe_set_alignment (cu, die, type);
16079
16080 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
16081 {
16082 /* ICC<14 does not output the required DW_AT_declaration on
16083 incomplete types, but gives them a size of zero. */
16084 type->set_is_stub (true);
16085 }
16086 else
16087 TYPE_STUB_SUPPORTED (type) = 1;
16088
16089 if (die_is_declaration (die, cu))
16090 type->set_is_stub (true);
16091 else if (attr == NULL && die->child == NULL
16092 && producer_is_realview (cu->producer))
16093 /* RealView does not output the required DW_AT_declaration
16094 on incomplete types. */
16095 type->set_is_stub (true);
16096
16097 /* We need to add the type field to the die immediately so we don't
16098 infinitely recurse when dealing with pointers to the structure
16099 type within the structure itself. */
16100 set_die_type (die, type, cu);
16101
16102 /* set_die_type should be already done. */
16103 set_descriptive_type (type, die, cu);
16104
16105 return type;
16106 }
16107
16108 static void handle_struct_member_die
16109 (struct die_info *child_die,
16110 struct type *type,
16111 struct field_info *fi,
16112 std::vector<struct symbol *> *template_args,
16113 struct dwarf2_cu *cu);
16114
16115 /* A helper for handle_struct_member_die that handles
16116 DW_TAG_variant_part. */
16117
16118 static void
16119 handle_variant_part (struct die_info *die, struct type *type,
16120 struct field_info *fi,
16121 std::vector<struct symbol *> *template_args,
16122 struct dwarf2_cu *cu)
16123 {
16124 variant_part_builder *new_part;
16125 if (fi->current_variant_part == nullptr)
16126 {
16127 fi->variant_parts.emplace_back ();
16128 new_part = &fi->variant_parts.back ();
16129 }
16130 else if (!fi->current_variant_part->processing_variant)
16131 {
16132 complaint (_("nested DW_TAG_variant_part seen "
16133 "- DIE at %s [in module %s]"),
16134 sect_offset_str (die->sect_off),
16135 objfile_name (cu->per_objfile->objfile));
16136 return;
16137 }
16138 else
16139 {
16140 variant_field &current = fi->current_variant_part->variants.back ();
16141 current.variant_parts.emplace_back ();
16142 new_part = &current.variant_parts.back ();
16143 }
16144
16145 /* When we recurse, we want callees to add to this new variant
16146 part. */
16147 scoped_restore save_current_variant_part
16148 = make_scoped_restore (&fi->current_variant_part, new_part);
16149
16150 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16151 if (discr == NULL)
16152 {
16153 /* It's a univariant form, an extension we support. */
16154 }
16155 else if (discr->form_is_ref ())
16156 {
16157 struct dwarf2_cu *target_cu = cu;
16158 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16159
16160 new_part->discriminant_offset = target_die->sect_off;
16161 }
16162 else
16163 {
16164 complaint (_("DW_AT_discr does not have DIE reference form"
16165 " - DIE at %s [in module %s]"),
16166 sect_offset_str (die->sect_off),
16167 objfile_name (cu->per_objfile->objfile));
16168 }
16169
16170 for (die_info *child_die = die->child;
16171 child_die != NULL;
16172 child_die = child_die->sibling)
16173 handle_struct_member_die (child_die, type, fi, template_args, cu);
16174 }
16175
16176 /* A helper for handle_struct_member_die that handles
16177 DW_TAG_variant. */
16178
16179 static void
16180 handle_variant (struct die_info *die, struct type *type,
16181 struct field_info *fi,
16182 std::vector<struct symbol *> *template_args,
16183 struct dwarf2_cu *cu)
16184 {
16185 if (fi->current_variant_part == nullptr)
16186 {
16187 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
16188 "- DIE at %s [in module %s]"),
16189 sect_offset_str (die->sect_off),
16190 objfile_name (cu->per_objfile->objfile));
16191 return;
16192 }
16193 if (fi->current_variant_part->processing_variant)
16194 {
16195 complaint (_("nested DW_TAG_variant seen "
16196 "- DIE at %s [in module %s]"),
16197 sect_offset_str (die->sect_off),
16198 objfile_name (cu->per_objfile->objfile));
16199 return;
16200 }
16201
16202 scoped_restore save_processing_variant
16203 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16204 true);
16205
16206 fi->current_variant_part->variants.emplace_back ();
16207 variant_field &variant = fi->current_variant_part->variants.back ();
16208 variant.first_field = fi->fields.size ();
16209
16210 /* In a variant we want to get the discriminant and also add a
16211 field for our sole member child. */
16212 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
16213 if (discr == nullptr)
16214 {
16215 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
16216 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
16217 variant.default_branch = true;
16218 else
16219 variant.discr_list_data = DW_BLOCK (discr);
16220 }
16221 else
16222 variant.discriminant_value = DW_UNSND (discr);
16223
16224 for (die_info *variant_child = die->child;
16225 variant_child != NULL;
16226 variant_child = variant_child->sibling)
16227 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16228
16229 variant.last_field = fi->fields.size ();
16230 }
16231
16232 /* A helper for process_structure_scope that handles a single member
16233 DIE. */
16234
16235 static void
16236 handle_struct_member_die (struct die_info *child_die, struct type *type,
16237 struct field_info *fi,
16238 std::vector<struct symbol *> *template_args,
16239 struct dwarf2_cu *cu)
16240 {
16241 if (child_die->tag == DW_TAG_member
16242 || child_die->tag == DW_TAG_variable)
16243 {
16244 /* NOTE: carlton/2002-11-05: A C++ static data member
16245 should be a DW_TAG_member that is a declaration, but
16246 all versions of G++ as of this writing (so through at
16247 least 3.2.1) incorrectly generate DW_TAG_variable
16248 tags for them instead. */
16249 dwarf2_add_field (fi, child_die, cu);
16250 }
16251 else if (child_die->tag == DW_TAG_subprogram)
16252 {
16253 /* Rust doesn't have member functions in the C++ sense.
16254 However, it does emit ordinary functions as children
16255 of a struct DIE. */
16256 if (cu->language == language_rust)
16257 read_func_scope (child_die, cu);
16258 else
16259 {
16260 /* C++ member function. */
16261 dwarf2_add_member_fn (fi, child_die, type, cu);
16262 }
16263 }
16264 else if (child_die->tag == DW_TAG_inheritance)
16265 {
16266 /* C++ base class field. */
16267 dwarf2_add_field (fi, child_die, cu);
16268 }
16269 else if (type_can_define_types (child_die))
16270 dwarf2_add_type_defn (fi, child_die, cu);
16271 else if (child_die->tag == DW_TAG_template_type_param
16272 || child_die->tag == DW_TAG_template_value_param)
16273 {
16274 struct symbol *arg = new_symbol (child_die, NULL, cu);
16275
16276 if (arg != NULL)
16277 template_args->push_back (arg);
16278 }
16279 else if (child_die->tag == DW_TAG_variant_part)
16280 handle_variant_part (child_die, type, fi, template_args, cu);
16281 else if (child_die->tag == DW_TAG_variant)
16282 handle_variant (child_die, type, fi, template_args, cu);
16283 }
16284
16285 /* Finish creating a structure or union type, including filling in
16286 its members and creating a symbol for it. */
16287
16288 static void
16289 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16290 {
16291 struct objfile *objfile = cu->per_objfile->objfile;
16292 struct die_info *child_die;
16293 struct type *type;
16294
16295 type = get_die_type (die, cu);
16296 if (type == NULL)
16297 type = read_structure_type (die, cu);
16298
16299 bool has_template_parameters = false;
16300 if (die->child != NULL && ! die_is_declaration (die, cu))
16301 {
16302 struct field_info fi;
16303 std::vector<struct symbol *> template_args;
16304
16305 child_die = die->child;
16306
16307 while (child_die && child_die->tag)
16308 {
16309 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16310 child_die = child_die->sibling;
16311 }
16312
16313 /* Attach template arguments to type. */
16314 if (!template_args.empty ())
16315 {
16316 has_template_parameters = true;
16317 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16318 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16319 TYPE_TEMPLATE_ARGUMENTS (type)
16320 = XOBNEWVEC (&objfile->objfile_obstack,
16321 struct symbol *,
16322 TYPE_N_TEMPLATE_ARGUMENTS (type));
16323 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16324 template_args.data (),
16325 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16326 * sizeof (struct symbol *)));
16327 }
16328
16329 /* Attach fields and member functions to the type. */
16330 if (fi.nfields () > 0)
16331 dwarf2_attach_fields_to_type (&fi, type, cu);
16332 if (!fi.fnfieldlists.empty ())
16333 {
16334 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16335
16336 /* Get the type which refers to the base class (possibly this
16337 class itself) which contains the vtable pointer for the current
16338 class from the DW_AT_containing_type attribute. This use of
16339 DW_AT_containing_type is a GNU extension. */
16340
16341 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16342 {
16343 struct type *t = die_containing_type (die, cu);
16344
16345 set_type_vptr_basetype (type, t);
16346 if (type == t)
16347 {
16348 int i;
16349
16350 /* Our own class provides vtbl ptr. */
16351 for (i = t->num_fields () - 1;
16352 i >= TYPE_N_BASECLASSES (t);
16353 --i)
16354 {
16355 const char *fieldname = TYPE_FIELD_NAME (t, i);
16356
16357 if (is_vtable_name (fieldname, cu))
16358 {
16359 set_type_vptr_fieldno (type, i);
16360 break;
16361 }
16362 }
16363
16364 /* Complain if virtual function table field not found. */
16365 if (i < TYPE_N_BASECLASSES (t))
16366 complaint (_("virtual function table pointer "
16367 "not found when defining class '%s'"),
16368 type->name () ? type->name () : "");
16369 }
16370 else
16371 {
16372 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16373 }
16374 }
16375 else if (cu->producer
16376 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16377 {
16378 /* The IBM XLC compiler does not provide direct indication
16379 of the containing type, but the vtable pointer is
16380 always named __vfp. */
16381
16382 int i;
16383
16384 for (i = type->num_fields () - 1;
16385 i >= TYPE_N_BASECLASSES (type);
16386 --i)
16387 {
16388 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16389 {
16390 set_type_vptr_fieldno (type, i);
16391 set_type_vptr_basetype (type, type);
16392 break;
16393 }
16394 }
16395 }
16396 }
16397
16398 /* Copy fi.typedef_field_list linked list elements content into the
16399 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16400 if (!fi.typedef_field_list.empty ())
16401 {
16402 int count = fi.typedef_field_list.size ();
16403
16404 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16405 TYPE_TYPEDEF_FIELD_ARRAY (type)
16406 = ((struct decl_field *)
16407 TYPE_ALLOC (type,
16408 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16409 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16410
16411 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16412 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16413 }
16414
16415 /* Copy fi.nested_types_list linked list elements content into the
16416 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16417 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16418 {
16419 int count = fi.nested_types_list.size ();
16420
16421 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16422 TYPE_NESTED_TYPES_ARRAY (type)
16423 = ((struct decl_field *)
16424 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16425 TYPE_NESTED_TYPES_COUNT (type) = count;
16426
16427 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16428 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16429 }
16430 }
16431
16432 quirk_gcc_member_function_pointer (type, objfile);
16433 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16434 cu->rust_unions.push_back (type);
16435
16436 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16437 snapshots) has been known to create a die giving a declaration
16438 for a class that has, as a child, a die giving a definition for a
16439 nested class. So we have to process our children even if the
16440 current die is a declaration. Normally, of course, a declaration
16441 won't have any children at all. */
16442
16443 child_die = die->child;
16444
16445 while (child_die != NULL && child_die->tag)
16446 {
16447 if (child_die->tag == DW_TAG_member
16448 || child_die->tag == DW_TAG_variable
16449 || child_die->tag == DW_TAG_inheritance
16450 || child_die->tag == DW_TAG_template_value_param
16451 || child_die->tag == DW_TAG_template_type_param)
16452 {
16453 /* Do nothing. */
16454 }
16455 else
16456 process_die (child_die, cu);
16457
16458 child_die = child_die->sibling;
16459 }
16460
16461 /* Do not consider external references. According to the DWARF standard,
16462 these DIEs are identified by the fact that they have no byte_size
16463 attribute, and a declaration attribute. */
16464 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16465 || !die_is_declaration (die, cu)
16466 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16467 {
16468 struct symbol *sym = new_symbol (die, type, cu);
16469
16470 if (has_template_parameters)
16471 {
16472 struct symtab *symtab;
16473 if (sym != nullptr)
16474 symtab = symbol_symtab (sym);
16475 else if (cu->line_header != nullptr)
16476 {
16477 /* Any related symtab will do. */
16478 symtab
16479 = cu->line_header->file_names ()[0].symtab;
16480 }
16481 else
16482 {
16483 symtab = nullptr;
16484 complaint (_("could not find suitable "
16485 "symtab for template parameter"
16486 " - DIE at %s [in module %s]"),
16487 sect_offset_str (die->sect_off),
16488 objfile_name (objfile));
16489 }
16490
16491 if (symtab != nullptr)
16492 {
16493 /* Make sure that the symtab is set on the new symbols.
16494 Even though they don't appear in this symtab directly,
16495 other parts of gdb assume that symbols do, and this is
16496 reasonably true. */
16497 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16498 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16499 }
16500 }
16501 }
16502 }
16503
16504 /* Assuming DIE is an enumeration type, and TYPE is its associated
16505 type, update TYPE using some information only available in DIE's
16506 children. In particular, the fields are computed. */
16507
16508 static void
16509 update_enumeration_type_from_children (struct die_info *die,
16510 struct type *type,
16511 struct dwarf2_cu *cu)
16512 {
16513 struct die_info *child_die;
16514 int unsigned_enum = 1;
16515 int flag_enum = 1;
16516
16517 auto_obstack obstack;
16518 std::vector<struct field> fields;
16519
16520 for (child_die = die->child;
16521 child_die != NULL && child_die->tag;
16522 child_die = child_die->sibling)
16523 {
16524 struct attribute *attr;
16525 LONGEST value;
16526 const gdb_byte *bytes;
16527 struct dwarf2_locexpr_baton *baton;
16528 const char *name;
16529
16530 if (child_die->tag != DW_TAG_enumerator)
16531 continue;
16532
16533 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16534 if (attr == NULL)
16535 continue;
16536
16537 name = dwarf2_name (child_die, cu);
16538 if (name == NULL)
16539 name = "<anonymous enumerator>";
16540
16541 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16542 &value, &bytes, &baton);
16543 if (value < 0)
16544 {
16545 unsigned_enum = 0;
16546 flag_enum = 0;
16547 }
16548 else
16549 {
16550 if (count_one_bits_ll (value) >= 2)
16551 flag_enum = 0;
16552 }
16553
16554 fields.emplace_back ();
16555 struct field &field = fields.back ();
16556 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16557 SET_FIELD_ENUMVAL (field, value);
16558 }
16559
16560 if (!fields.empty ())
16561 {
16562 type->set_num_fields (fields.size ());
16563 type->set_fields
16564 ((struct field *)
16565 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16566 memcpy (type->fields (), fields.data (),
16567 sizeof (struct field) * fields.size ());
16568 }
16569
16570 if (unsigned_enum)
16571 type->set_is_unsigned (true);
16572
16573 if (flag_enum)
16574 TYPE_FLAG_ENUM (type) = 1;
16575 }
16576
16577 /* Given a DW_AT_enumeration_type die, set its type. We do not
16578 complete the type's fields yet, or create any symbols. */
16579
16580 static struct type *
16581 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16582 {
16583 struct objfile *objfile = cu->per_objfile->objfile;
16584 struct type *type;
16585 struct attribute *attr;
16586 const char *name;
16587
16588 /* If the definition of this type lives in .debug_types, read that type.
16589 Don't follow DW_AT_specification though, that will take us back up
16590 the chain and we want to go down. */
16591 attr = die->attr (DW_AT_signature);
16592 if (attr != nullptr)
16593 {
16594 type = get_DW_AT_signature_type (die, attr, cu);
16595
16596 /* The type's CU may not be the same as CU.
16597 Ensure TYPE is recorded with CU in die_type_hash. */
16598 return set_die_type (die, type, cu);
16599 }
16600
16601 type = alloc_type (objfile);
16602
16603 type->set_code (TYPE_CODE_ENUM);
16604 name = dwarf2_full_name (NULL, die, cu);
16605 if (name != NULL)
16606 type->set_name (name);
16607
16608 attr = dwarf2_attr (die, DW_AT_type, cu);
16609 if (attr != NULL)
16610 {
16611 struct type *underlying_type = die_type (die, cu);
16612
16613 TYPE_TARGET_TYPE (type) = underlying_type;
16614 }
16615
16616 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16617 if (attr != nullptr)
16618 {
16619 TYPE_LENGTH (type) = DW_UNSND (attr);
16620 }
16621 else
16622 {
16623 TYPE_LENGTH (type) = 0;
16624 }
16625
16626 maybe_set_alignment (cu, die, type);
16627
16628 /* The enumeration DIE can be incomplete. In Ada, any type can be
16629 declared as private in the package spec, and then defined only
16630 inside the package body. Such types are known as Taft Amendment
16631 Types. When another package uses such a type, an incomplete DIE
16632 may be generated by the compiler. */
16633 if (die_is_declaration (die, cu))
16634 type->set_is_stub (true);
16635
16636 /* If this type has an underlying type that is not a stub, then we
16637 may use its attributes. We always use the "unsigned" attribute
16638 in this situation, because ordinarily we guess whether the type
16639 is unsigned -- but the guess can be wrong and the underlying type
16640 can tell us the reality. However, we defer to a local size
16641 attribute if one exists, because this lets the compiler override
16642 the underlying type if needed. */
16643 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16644 {
16645 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16646 underlying_type = check_typedef (underlying_type);
16647
16648 type->set_is_unsigned (underlying_type->is_unsigned ());
16649
16650 if (TYPE_LENGTH (type) == 0)
16651 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16652
16653 if (TYPE_RAW_ALIGN (type) == 0
16654 && TYPE_RAW_ALIGN (underlying_type) != 0)
16655 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16656 }
16657
16658 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16659
16660 set_die_type (die, type, cu);
16661
16662 /* Finish the creation of this type by using the enum's children.
16663 Note that, as usual, this must come after set_die_type to avoid
16664 infinite recursion when trying to compute the names of the
16665 enumerators. */
16666 update_enumeration_type_from_children (die, type, cu);
16667
16668 return type;
16669 }
16670
16671 /* Given a pointer to a die which begins an enumeration, process all
16672 the dies that define the members of the enumeration, and create the
16673 symbol for the enumeration type.
16674
16675 NOTE: We reverse the order of the element list. */
16676
16677 static void
16678 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16679 {
16680 struct type *this_type;
16681
16682 this_type = get_die_type (die, cu);
16683 if (this_type == NULL)
16684 this_type = read_enumeration_type (die, cu);
16685
16686 if (die->child != NULL)
16687 {
16688 struct die_info *child_die;
16689 const char *name;
16690
16691 child_die = die->child;
16692 while (child_die && child_die->tag)
16693 {
16694 if (child_die->tag != DW_TAG_enumerator)
16695 {
16696 process_die (child_die, cu);
16697 }
16698 else
16699 {
16700 name = dwarf2_name (child_die, cu);
16701 if (name)
16702 new_symbol (child_die, this_type, cu);
16703 }
16704
16705 child_die = child_die->sibling;
16706 }
16707 }
16708
16709 /* If we are reading an enum from a .debug_types unit, and the enum
16710 is a declaration, and the enum is not the signatured type in the
16711 unit, then we do not want to add a symbol for it. Adding a
16712 symbol would in some cases obscure the true definition of the
16713 enum, giving users an incomplete type when the definition is
16714 actually available. Note that we do not want to do this for all
16715 enums which are just declarations, because C++0x allows forward
16716 enum declarations. */
16717 if (cu->per_cu->is_debug_types
16718 && die_is_declaration (die, cu))
16719 {
16720 struct signatured_type *sig_type;
16721
16722 sig_type = (struct signatured_type *) cu->per_cu;
16723 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16724 if (sig_type->type_offset_in_section != die->sect_off)
16725 return;
16726 }
16727
16728 new_symbol (die, this_type, cu);
16729 }
16730
16731 /* Extract all information from a DW_TAG_array_type DIE and put it in
16732 the DIE's type field. For now, this only handles one dimensional
16733 arrays. */
16734
16735 static struct type *
16736 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16737 {
16738 struct objfile *objfile = cu->per_objfile->objfile;
16739 struct die_info *child_die;
16740 struct type *type;
16741 struct type *element_type, *range_type, *index_type;
16742 struct attribute *attr;
16743 const char *name;
16744 struct dynamic_prop *byte_stride_prop = NULL;
16745 unsigned int bit_stride = 0;
16746
16747 element_type = die_type (die, cu);
16748
16749 /* The die_type call above may have already set the type for this DIE. */
16750 type = get_die_type (die, cu);
16751 if (type)
16752 return type;
16753
16754 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16755 if (attr != NULL)
16756 {
16757 int stride_ok;
16758 struct type *prop_type = cu->addr_sized_int_type (false);
16759
16760 byte_stride_prop
16761 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16762 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16763 prop_type);
16764 if (!stride_ok)
16765 {
16766 complaint (_("unable to read array DW_AT_byte_stride "
16767 " - DIE at %s [in module %s]"),
16768 sect_offset_str (die->sect_off),
16769 objfile_name (cu->per_objfile->objfile));
16770 /* Ignore this attribute. We will likely not be able to print
16771 arrays of this type correctly, but there is little we can do
16772 to help if we cannot read the attribute's value. */
16773 byte_stride_prop = NULL;
16774 }
16775 }
16776
16777 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16778 if (attr != NULL)
16779 bit_stride = DW_UNSND (attr);
16780
16781 /* Irix 6.2 native cc creates array types without children for
16782 arrays with unspecified length. */
16783 if (die->child == NULL)
16784 {
16785 index_type = objfile_type (objfile)->builtin_int;
16786 range_type = create_static_range_type (NULL, index_type, 0, -1);
16787 type = create_array_type_with_stride (NULL, element_type, range_type,
16788 byte_stride_prop, bit_stride);
16789 return set_die_type (die, type, cu);
16790 }
16791
16792 std::vector<struct type *> range_types;
16793 child_die = die->child;
16794 while (child_die && child_die->tag)
16795 {
16796 if (child_die->tag == DW_TAG_subrange_type)
16797 {
16798 struct type *child_type = read_type_die (child_die, cu);
16799
16800 if (child_type != NULL)
16801 {
16802 /* The range type was succesfully read. Save it for the
16803 array type creation. */
16804 range_types.push_back (child_type);
16805 }
16806 }
16807 child_die = child_die->sibling;
16808 }
16809
16810 /* Dwarf2 dimensions are output from left to right, create the
16811 necessary array types in backwards order. */
16812
16813 type = element_type;
16814
16815 if (read_array_order (die, cu) == DW_ORD_col_major)
16816 {
16817 int i = 0;
16818
16819 while (i < range_types.size ())
16820 type = create_array_type_with_stride (NULL, type, range_types[i++],
16821 byte_stride_prop, bit_stride);
16822 }
16823 else
16824 {
16825 size_t ndim = range_types.size ();
16826 while (ndim-- > 0)
16827 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16828 byte_stride_prop, bit_stride);
16829 }
16830
16831 /* Understand Dwarf2 support for vector types (like they occur on
16832 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16833 array type. This is not part of the Dwarf2/3 standard yet, but a
16834 custom vendor extension. The main difference between a regular
16835 array and the vector variant is that vectors are passed by value
16836 to functions. */
16837 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16838 if (attr != nullptr)
16839 make_vector_type (type);
16840
16841 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16842 implementation may choose to implement triple vectors using this
16843 attribute. */
16844 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16845 if (attr != nullptr)
16846 {
16847 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16848 TYPE_LENGTH (type) = DW_UNSND (attr);
16849 else
16850 complaint (_("DW_AT_byte_size for array type smaller "
16851 "than the total size of elements"));
16852 }
16853
16854 name = dwarf2_name (die, cu);
16855 if (name)
16856 type->set_name (name);
16857
16858 maybe_set_alignment (cu, die, type);
16859
16860 /* Install the type in the die. */
16861 set_die_type (die, type, cu);
16862
16863 /* set_die_type should be already done. */
16864 set_descriptive_type (type, die, cu);
16865
16866 return type;
16867 }
16868
16869 static enum dwarf_array_dim_ordering
16870 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16871 {
16872 struct attribute *attr;
16873
16874 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16875
16876 if (attr != nullptr)
16877 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16878
16879 /* GNU F77 is a special case, as at 08/2004 array type info is the
16880 opposite order to the dwarf2 specification, but data is still
16881 laid out as per normal fortran.
16882
16883 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16884 version checking. */
16885
16886 if (cu->language == language_fortran
16887 && cu->producer && strstr (cu->producer, "GNU F77"))
16888 {
16889 return DW_ORD_row_major;
16890 }
16891
16892 switch (cu->language_defn->la_array_ordering)
16893 {
16894 case array_column_major:
16895 return DW_ORD_col_major;
16896 case array_row_major:
16897 default:
16898 return DW_ORD_row_major;
16899 };
16900 }
16901
16902 /* Extract all information from a DW_TAG_set_type DIE and put it in
16903 the DIE's type field. */
16904
16905 static struct type *
16906 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16907 {
16908 struct type *domain_type, *set_type;
16909 struct attribute *attr;
16910
16911 domain_type = die_type (die, cu);
16912
16913 /* The die_type call above may have already set the type for this DIE. */
16914 set_type = get_die_type (die, cu);
16915 if (set_type)
16916 return set_type;
16917
16918 set_type = create_set_type (NULL, domain_type);
16919
16920 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16921 if (attr != nullptr)
16922 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16923
16924 maybe_set_alignment (cu, die, set_type);
16925
16926 return set_die_type (die, set_type, cu);
16927 }
16928
16929 /* A helper for read_common_block that creates a locexpr baton.
16930 SYM is the symbol which we are marking as computed.
16931 COMMON_DIE is the DIE for the common block.
16932 COMMON_LOC is the location expression attribute for the common
16933 block itself.
16934 MEMBER_LOC is the location expression attribute for the particular
16935 member of the common block that we are processing.
16936 CU is the CU from which the above come. */
16937
16938 static void
16939 mark_common_block_symbol_computed (struct symbol *sym,
16940 struct die_info *common_die,
16941 struct attribute *common_loc,
16942 struct attribute *member_loc,
16943 struct dwarf2_cu *cu)
16944 {
16945 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16946 struct objfile *objfile = per_objfile->objfile;
16947 struct dwarf2_locexpr_baton *baton;
16948 gdb_byte *ptr;
16949 unsigned int cu_off;
16950 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16951 LONGEST offset = 0;
16952
16953 gdb_assert (common_loc && member_loc);
16954 gdb_assert (common_loc->form_is_block ());
16955 gdb_assert (member_loc->form_is_block ()
16956 || member_loc->form_is_constant ());
16957
16958 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16959 baton->per_objfile = per_objfile;
16960 baton->per_cu = cu->per_cu;
16961 gdb_assert (baton->per_cu);
16962
16963 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16964
16965 if (member_loc->form_is_constant ())
16966 {
16967 offset = member_loc->constant_value (0);
16968 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16969 }
16970 else
16971 baton->size += DW_BLOCK (member_loc)->size;
16972
16973 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16974 baton->data = ptr;
16975
16976 *ptr++ = DW_OP_call4;
16977 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16978 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16979 ptr += 4;
16980
16981 if (member_loc->form_is_constant ())
16982 {
16983 *ptr++ = DW_OP_addr;
16984 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16985 ptr += cu->header.addr_size;
16986 }
16987 else
16988 {
16989 /* We have to copy the data here, because DW_OP_call4 will only
16990 use a DW_AT_location attribute. */
16991 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16992 ptr += DW_BLOCK (member_loc)->size;
16993 }
16994
16995 *ptr++ = DW_OP_plus;
16996 gdb_assert (ptr - baton->data == baton->size);
16997
16998 SYMBOL_LOCATION_BATON (sym) = baton;
16999 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
17000 }
17001
17002 /* Create appropriate locally-scoped variables for all the
17003 DW_TAG_common_block entries. Also create a struct common_block
17004 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
17005 is used to separate the common blocks name namespace from regular
17006 variable names. */
17007
17008 static void
17009 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
17010 {
17011 struct attribute *attr;
17012
17013 attr = dwarf2_attr (die, DW_AT_location, cu);
17014 if (attr != nullptr)
17015 {
17016 /* Support the .debug_loc offsets. */
17017 if (attr->form_is_block ())
17018 {
17019 /* Ok. */
17020 }
17021 else if (attr->form_is_section_offset ())
17022 {
17023 dwarf2_complex_location_expr_complaint ();
17024 attr = NULL;
17025 }
17026 else
17027 {
17028 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17029 "common block member");
17030 attr = NULL;
17031 }
17032 }
17033
17034 if (die->child != NULL)
17035 {
17036 struct objfile *objfile = cu->per_objfile->objfile;
17037 struct die_info *child_die;
17038 size_t n_entries = 0, size;
17039 struct common_block *common_block;
17040 struct symbol *sym;
17041
17042 for (child_die = die->child;
17043 child_die && child_die->tag;
17044 child_die = child_die->sibling)
17045 ++n_entries;
17046
17047 size = (sizeof (struct common_block)
17048 + (n_entries - 1) * sizeof (struct symbol *));
17049 common_block
17050 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17051 size);
17052 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17053 common_block->n_entries = 0;
17054
17055 for (child_die = die->child;
17056 child_die && child_die->tag;
17057 child_die = child_die->sibling)
17058 {
17059 /* Create the symbol in the DW_TAG_common_block block in the current
17060 symbol scope. */
17061 sym = new_symbol (child_die, NULL, cu);
17062 if (sym != NULL)
17063 {
17064 struct attribute *member_loc;
17065
17066 common_block->contents[common_block->n_entries++] = sym;
17067
17068 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17069 cu);
17070 if (member_loc)
17071 {
17072 /* GDB has handled this for a long time, but it is
17073 not specified by DWARF. It seems to have been
17074 emitted by gfortran at least as recently as:
17075 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17076 complaint (_("Variable in common block has "
17077 "DW_AT_data_member_location "
17078 "- DIE at %s [in module %s]"),
17079 sect_offset_str (child_die->sect_off),
17080 objfile_name (objfile));
17081
17082 if (member_loc->form_is_section_offset ())
17083 dwarf2_complex_location_expr_complaint ();
17084 else if (member_loc->form_is_constant ()
17085 || member_loc->form_is_block ())
17086 {
17087 if (attr != nullptr)
17088 mark_common_block_symbol_computed (sym, die, attr,
17089 member_loc, cu);
17090 }
17091 else
17092 dwarf2_complex_location_expr_complaint ();
17093 }
17094 }
17095 }
17096
17097 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17098 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
17099 }
17100 }
17101
17102 /* Create a type for a C++ namespace. */
17103
17104 static struct type *
17105 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
17106 {
17107 struct objfile *objfile = cu->per_objfile->objfile;
17108 const char *previous_prefix, *name;
17109 int is_anonymous;
17110 struct type *type;
17111
17112 /* For extensions, reuse the type of the original namespace. */
17113 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17114 {
17115 struct die_info *ext_die;
17116 struct dwarf2_cu *ext_cu = cu;
17117
17118 ext_die = dwarf2_extension (die, &ext_cu);
17119 type = read_type_die (ext_die, ext_cu);
17120
17121 /* EXT_CU may not be the same as CU.
17122 Ensure TYPE is recorded with CU in die_type_hash. */
17123 return set_die_type (die, type, cu);
17124 }
17125
17126 name = namespace_name (die, &is_anonymous, cu);
17127
17128 /* Now build the name of the current namespace. */
17129
17130 previous_prefix = determine_prefix (die, cu);
17131 if (previous_prefix[0] != '\0')
17132 name = typename_concat (&objfile->objfile_obstack,
17133 previous_prefix, name, 0, cu);
17134
17135 /* Create the type. */
17136 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17137
17138 return set_die_type (die, type, cu);
17139 }
17140
17141 /* Read a namespace scope. */
17142
17143 static void
17144 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17145 {
17146 struct objfile *objfile = cu->per_objfile->objfile;
17147 int is_anonymous;
17148
17149 /* Add a symbol associated to this if we haven't seen the namespace
17150 before. Also, add a using directive if it's an anonymous
17151 namespace. */
17152
17153 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17154 {
17155 struct type *type;
17156
17157 type = read_type_die (die, cu);
17158 new_symbol (die, type, cu);
17159
17160 namespace_name (die, &is_anonymous, cu);
17161 if (is_anonymous)
17162 {
17163 const char *previous_prefix = determine_prefix (die, cu);
17164
17165 std::vector<const char *> excludes;
17166 add_using_directive (using_directives (cu),
17167 previous_prefix, type->name (), NULL,
17168 NULL, excludes, 0, &objfile->objfile_obstack);
17169 }
17170 }
17171
17172 if (die->child != NULL)
17173 {
17174 struct die_info *child_die = die->child;
17175
17176 while (child_die && child_die->tag)
17177 {
17178 process_die (child_die, cu);
17179 child_die = child_die->sibling;
17180 }
17181 }
17182 }
17183
17184 /* Read a Fortran module as type. This DIE can be only a declaration used for
17185 imported module. Still we need that type as local Fortran "use ... only"
17186 declaration imports depend on the created type in determine_prefix. */
17187
17188 static struct type *
17189 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17190 {
17191 struct objfile *objfile = cu->per_objfile->objfile;
17192 const char *module_name;
17193 struct type *type;
17194
17195 module_name = dwarf2_name (die, cu);
17196 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17197
17198 return set_die_type (die, type, cu);
17199 }
17200
17201 /* Read a Fortran module. */
17202
17203 static void
17204 read_module (struct die_info *die, struct dwarf2_cu *cu)
17205 {
17206 struct die_info *child_die = die->child;
17207 struct type *type;
17208
17209 type = read_type_die (die, cu);
17210 new_symbol (die, type, cu);
17211
17212 while (child_die && child_die->tag)
17213 {
17214 process_die (child_die, cu);
17215 child_die = child_die->sibling;
17216 }
17217 }
17218
17219 /* Return the name of the namespace represented by DIE. Set
17220 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17221 namespace. */
17222
17223 static const char *
17224 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17225 {
17226 struct die_info *current_die;
17227 const char *name = NULL;
17228
17229 /* Loop through the extensions until we find a name. */
17230
17231 for (current_die = die;
17232 current_die != NULL;
17233 current_die = dwarf2_extension (die, &cu))
17234 {
17235 /* We don't use dwarf2_name here so that we can detect the absence
17236 of a name -> anonymous namespace. */
17237 name = dwarf2_string_attr (die, DW_AT_name, cu);
17238
17239 if (name != NULL)
17240 break;
17241 }
17242
17243 /* Is it an anonymous namespace? */
17244
17245 *is_anonymous = (name == NULL);
17246 if (*is_anonymous)
17247 name = CP_ANONYMOUS_NAMESPACE_STR;
17248
17249 return name;
17250 }
17251
17252 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17253 the user defined type vector. */
17254
17255 static struct type *
17256 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17257 {
17258 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
17259 struct comp_unit_head *cu_header = &cu->header;
17260 struct type *type;
17261 struct attribute *attr_byte_size;
17262 struct attribute *attr_address_class;
17263 int byte_size, addr_class;
17264 struct type *target_type;
17265
17266 target_type = die_type (die, cu);
17267
17268 /* The die_type call above may have already set the type for this DIE. */
17269 type = get_die_type (die, cu);
17270 if (type)
17271 return type;
17272
17273 type = lookup_pointer_type (target_type);
17274
17275 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17276 if (attr_byte_size)
17277 byte_size = DW_UNSND (attr_byte_size);
17278 else
17279 byte_size = cu_header->addr_size;
17280
17281 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17282 if (attr_address_class)
17283 addr_class = DW_UNSND (attr_address_class);
17284 else
17285 addr_class = DW_ADDR_none;
17286
17287 ULONGEST alignment = get_alignment (cu, die);
17288
17289 /* If the pointer size, alignment, or address class is different
17290 than the default, create a type variant marked as such and set
17291 the length accordingly. */
17292 if (TYPE_LENGTH (type) != byte_size
17293 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17294 && alignment != TYPE_RAW_ALIGN (type))
17295 || addr_class != DW_ADDR_none)
17296 {
17297 if (gdbarch_address_class_type_flags_p (gdbarch))
17298 {
17299 int type_flags;
17300
17301 type_flags = gdbarch_address_class_type_flags
17302 (gdbarch, byte_size, addr_class);
17303 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17304 == 0);
17305 type = make_type_with_address_space (type, type_flags);
17306 }
17307 else if (TYPE_LENGTH (type) != byte_size)
17308 {
17309 complaint (_("invalid pointer size %d"), byte_size);
17310 }
17311 else if (TYPE_RAW_ALIGN (type) != alignment)
17312 {
17313 complaint (_("Invalid DW_AT_alignment"
17314 " - DIE at %s [in module %s]"),
17315 sect_offset_str (die->sect_off),
17316 objfile_name (cu->per_objfile->objfile));
17317 }
17318 else
17319 {
17320 /* Should we also complain about unhandled address classes? */
17321 }
17322 }
17323
17324 TYPE_LENGTH (type) = byte_size;
17325 set_type_align (type, alignment);
17326 return set_die_type (die, type, cu);
17327 }
17328
17329 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17330 the user defined type vector. */
17331
17332 static struct type *
17333 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17334 {
17335 struct type *type;
17336 struct type *to_type;
17337 struct type *domain;
17338
17339 to_type = die_type (die, cu);
17340 domain = die_containing_type (die, cu);
17341
17342 /* The calls above may have already set the type for this DIE. */
17343 type = get_die_type (die, cu);
17344 if (type)
17345 return type;
17346
17347 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17348 type = lookup_methodptr_type (to_type);
17349 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17350 {
17351 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17352
17353 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17354 to_type->fields (), to_type->num_fields (),
17355 to_type->has_varargs ());
17356 type = lookup_methodptr_type (new_type);
17357 }
17358 else
17359 type = lookup_memberptr_type (to_type, domain);
17360
17361 return set_die_type (die, type, cu);
17362 }
17363
17364 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17365 the user defined type vector. */
17366
17367 static struct type *
17368 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17369 enum type_code refcode)
17370 {
17371 struct comp_unit_head *cu_header = &cu->header;
17372 struct type *type, *target_type;
17373 struct attribute *attr;
17374
17375 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17376
17377 target_type = die_type (die, cu);
17378
17379 /* The die_type call above may have already set the type for this DIE. */
17380 type = get_die_type (die, cu);
17381 if (type)
17382 return type;
17383
17384 type = lookup_reference_type (target_type, refcode);
17385 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17386 if (attr != nullptr)
17387 {
17388 TYPE_LENGTH (type) = DW_UNSND (attr);
17389 }
17390 else
17391 {
17392 TYPE_LENGTH (type) = cu_header->addr_size;
17393 }
17394 maybe_set_alignment (cu, die, type);
17395 return set_die_type (die, type, cu);
17396 }
17397
17398 /* Add the given cv-qualifiers to the element type of the array. GCC
17399 outputs DWARF type qualifiers that apply to an array, not the
17400 element type. But GDB relies on the array element type to carry
17401 the cv-qualifiers. This mimics section 6.7.3 of the C99
17402 specification. */
17403
17404 static struct type *
17405 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17406 struct type *base_type, int cnst, int voltl)
17407 {
17408 struct type *el_type, *inner_array;
17409
17410 base_type = copy_type (base_type);
17411 inner_array = base_type;
17412
17413 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17414 {
17415 TYPE_TARGET_TYPE (inner_array) =
17416 copy_type (TYPE_TARGET_TYPE (inner_array));
17417 inner_array = TYPE_TARGET_TYPE (inner_array);
17418 }
17419
17420 el_type = TYPE_TARGET_TYPE (inner_array);
17421 cnst |= TYPE_CONST (el_type);
17422 voltl |= TYPE_VOLATILE (el_type);
17423 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17424
17425 return set_die_type (die, base_type, cu);
17426 }
17427
17428 static struct type *
17429 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17430 {
17431 struct type *base_type, *cv_type;
17432
17433 base_type = die_type (die, cu);
17434
17435 /* The die_type call above may have already set the type for this DIE. */
17436 cv_type = get_die_type (die, cu);
17437 if (cv_type)
17438 return cv_type;
17439
17440 /* In case the const qualifier is applied to an array type, the element type
17441 is so qualified, not the array type (section 6.7.3 of C99). */
17442 if (base_type->code () == TYPE_CODE_ARRAY)
17443 return add_array_cv_type (die, cu, base_type, 1, 0);
17444
17445 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17446 return set_die_type (die, cv_type, cu);
17447 }
17448
17449 static struct type *
17450 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17451 {
17452 struct type *base_type, *cv_type;
17453
17454 base_type = die_type (die, cu);
17455
17456 /* The die_type call above may have already set the type for this DIE. */
17457 cv_type = get_die_type (die, cu);
17458 if (cv_type)
17459 return cv_type;
17460
17461 /* In case the volatile qualifier is applied to an array type, the
17462 element type is so qualified, not the array type (section 6.7.3
17463 of C99). */
17464 if (base_type->code () == TYPE_CODE_ARRAY)
17465 return add_array_cv_type (die, cu, base_type, 0, 1);
17466
17467 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17468 return set_die_type (die, cv_type, cu);
17469 }
17470
17471 /* Handle DW_TAG_restrict_type. */
17472
17473 static struct type *
17474 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17475 {
17476 struct type *base_type, *cv_type;
17477
17478 base_type = die_type (die, cu);
17479
17480 /* The die_type call above may have already set the type for this DIE. */
17481 cv_type = get_die_type (die, cu);
17482 if (cv_type)
17483 return cv_type;
17484
17485 cv_type = make_restrict_type (base_type);
17486 return set_die_type (die, cv_type, cu);
17487 }
17488
17489 /* Handle DW_TAG_atomic_type. */
17490
17491 static struct type *
17492 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17493 {
17494 struct type *base_type, *cv_type;
17495
17496 base_type = die_type (die, cu);
17497
17498 /* The die_type call above may have already set the type for this DIE. */
17499 cv_type = get_die_type (die, cu);
17500 if (cv_type)
17501 return cv_type;
17502
17503 cv_type = make_atomic_type (base_type);
17504 return set_die_type (die, cv_type, cu);
17505 }
17506
17507 /* Extract all information from a DW_TAG_string_type DIE and add to
17508 the user defined type vector. It isn't really a user defined type,
17509 but it behaves like one, with other DIE's using an AT_user_def_type
17510 attribute to reference it. */
17511
17512 static struct type *
17513 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17514 {
17515 struct objfile *objfile = cu->per_objfile->objfile;
17516 struct gdbarch *gdbarch = objfile->arch ();
17517 struct type *type, *range_type, *index_type, *char_type;
17518 struct attribute *attr;
17519 struct dynamic_prop prop;
17520 bool length_is_constant = true;
17521 LONGEST length;
17522
17523 /* There are a couple of places where bit sizes might be made use of
17524 when parsing a DW_TAG_string_type, however, no producer that we know
17525 of make use of these. Handling bit sizes that are a multiple of the
17526 byte size is easy enough, but what about other bit sizes? Lets deal
17527 with that problem when we have to. Warn about these attributes being
17528 unsupported, then parse the type and ignore them like we always
17529 have. */
17530 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17531 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17532 {
17533 static bool warning_printed = false;
17534 if (!warning_printed)
17535 {
17536 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17537 "currently supported on DW_TAG_string_type."));
17538 warning_printed = true;
17539 }
17540 }
17541
17542 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17543 if (attr != nullptr && !attr->form_is_constant ())
17544 {
17545 /* The string length describes the location at which the length of
17546 the string can be found. The size of the length field can be
17547 specified with one of the attributes below. */
17548 struct type *prop_type;
17549 struct attribute *len
17550 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17551 if (len == nullptr)
17552 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17553 if (len != nullptr && len->form_is_constant ())
17554 {
17555 /* Pass 0 as the default as we know this attribute is constant
17556 and the default value will not be returned. */
17557 LONGEST sz = len->constant_value (0);
17558 prop_type = cu->per_objfile->int_type (sz, true);
17559 }
17560 else
17561 {
17562 /* If the size is not specified then we assume it is the size of
17563 an address on this target. */
17564 prop_type = cu->addr_sized_int_type (true);
17565 }
17566
17567 /* Convert the attribute into a dynamic property. */
17568 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17569 length = 1;
17570 else
17571 length_is_constant = false;
17572 }
17573 else if (attr != nullptr)
17574 {
17575 /* This DW_AT_string_length just contains the length with no
17576 indirection. There's no need to create a dynamic property in this
17577 case. Pass 0 for the default value as we know it will not be
17578 returned in this case. */
17579 length = attr->constant_value (0);
17580 }
17581 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17582 {
17583 /* We don't currently support non-constant byte sizes for strings. */
17584 length = attr->constant_value (1);
17585 }
17586 else
17587 {
17588 /* Use 1 as a fallback length if we have nothing else. */
17589 length = 1;
17590 }
17591
17592 index_type = objfile_type (objfile)->builtin_int;
17593 if (length_is_constant)
17594 range_type = create_static_range_type (NULL, index_type, 1, length);
17595 else
17596 {
17597 struct dynamic_prop low_bound;
17598
17599 low_bound.set_const_val (1);
17600 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17601 }
17602 char_type = language_string_char_type (cu->language_defn, gdbarch);
17603 type = create_string_type (NULL, char_type, range_type);
17604
17605 return set_die_type (die, type, cu);
17606 }
17607
17608 /* Assuming that DIE corresponds to a function, returns nonzero
17609 if the function is prototyped. */
17610
17611 static int
17612 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17613 {
17614 struct attribute *attr;
17615
17616 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17617 if (attr && (DW_UNSND (attr) != 0))
17618 return 1;
17619
17620 /* The DWARF standard implies that the DW_AT_prototyped attribute
17621 is only meaningful for C, but the concept also extends to other
17622 languages that allow unprototyped functions (Eg: Objective C).
17623 For all other languages, assume that functions are always
17624 prototyped. */
17625 if (cu->language != language_c
17626 && cu->language != language_objc
17627 && cu->language != language_opencl)
17628 return 1;
17629
17630 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17631 prototyped and unprototyped functions; default to prototyped,
17632 since that is more common in modern code (and RealView warns
17633 about unprototyped functions). */
17634 if (producer_is_realview (cu->producer))
17635 return 1;
17636
17637 return 0;
17638 }
17639
17640 /* Handle DIES due to C code like:
17641
17642 struct foo
17643 {
17644 int (*funcp)(int a, long l);
17645 int b;
17646 };
17647
17648 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17649
17650 static struct type *
17651 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17652 {
17653 struct objfile *objfile = cu->per_objfile->objfile;
17654 struct type *type; /* Type that this function returns. */
17655 struct type *ftype; /* Function that returns above type. */
17656 struct attribute *attr;
17657
17658 type = die_type (die, cu);
17659
17660 /* The die_type call above may have already set the type for this DIE. */
17661 ftype = get_die_type (die, cu);
17662 if (ftype)
17663 return ftype;
17664
17665 ftype = lookup_function_type (type);
17666
17667 if (prototyped_function_p (die, cu))
17668 ftype->set_is_prototyped (true);
17669
17670 /* Store the calling convention in the type if it's available in
17671 the subroutine die. Otherwise set the calling convention to
17672 the default value DW_CC_normal. */
17673 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17674 if (attr != nullptr
17675 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17676 TYPE_CALLING_CONVENTION (ftype)
17677 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17678 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17679 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17680 else
17681 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17682
17683 /* Record whether the function returns normally to its caller or not
17684 if the DWARF producer set that information. */
17685 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17686 if (attr && (DW_UNSND (attr) != 0))
17687 TYPE_NO_RETURN (ftype) = 1;
17688
17689 /* We need to add the subroutine type to the die immediately so
17690 we don't infinitely recurse when dealing with parameters
17691 declared as the same subroutine type. */
17692 set_die_type (die, ftype, cu);
17693
17694 if (die->child != NULL)
17695 {
17696 struct type *void_type = objfile_type (objfile)->builtin_void;
17697 struct die_info *child_die;
17698 int nparams, iparams;
17699
17700 /* Count the number of parameters.
17701 FIXME: GDB currently ignores vararg functions, but knows about
17702 vararg member functions. */
17703 nparams = 0;
17704 child_die = die->child;
17705 while (child_die && child_die->tag)
17706 {
17707 if (child_die->tag == DW_TAG_formal_parameter)
17708 nparams++;
17709 else if (child_die->tag == DW_TAG_unspecified_parameters)
17710 ftype->set_has_varargs (true);
17711
17712 child_die = child_die->sibling;
17713 }
17714
17715 /* Allocate storage for parameters and fill them in. */
17716 ftype->set_num_fields (nparams);
17717 ftype->set_fields
17718 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17719
17720 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17721 even if we error out during the parameters reading below. */
17722 for (iparams = 0; iparams < nparams; iparams++)
17723 ftype->field (iparams).set_type (void_type);
17724
17725 iparams = 0;
17726 child_die = die->child;
17727 while (child_die && child_die->tag)
17728 {
17729 if (child_die->tag == DW_TAG_formal_parameter)
17730 {
17731 struct type *arg_type;
17732
17733 /* DWARF version 2 has no clean way to discern C++
17734 static and non-static member functions. G++ helps
17735 GDB by marking the first parameter for non-static
17736 member functions (which is the this pointer) as
17737 artificial. We pass this information to
17738 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17739
17740 DWARF version 3 added DW_AT_object_pointer, which GCC
17741 4.5 does not yet generate. */
17742 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17743 if (attr != nullptr)
17744 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17745 else
17746 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17747 arg_type = die_type (child_die, cu);
17748
17749 /* RealView does not mark THIS as const, which the testsuite
17750 expects. GCC marks THIS as const in method definitions,
17751 but not in the class specifications (GCC PR 43053). */
17752 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17753 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17754 {
17755 int is_this = 0;
17756 struct dwarf2_cu *arg_cu = cu;
17757 const char *name = dwarf2_name (child_die, cu);
17758
17759 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17760 if (attr != nullptr)
17761 {
17762 /* If the compiler emits this, use it. */
17763 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17764 is_this = 1;
17765 }
17766 else if (name && strcmp (name, "this") == 0)
17767 /* Function definitions will have the argument names. */
17768 is_this = 1;
17769 else if (name == NULL && iparams == 0)
17770 /* Declarations may not have the names, so like
17771 elsewhere in GDB, assume an artificial first
17772 argument is "this". */
17773 is_this = 1;
17774
17775 if (is_this)
17776 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17777 arg_type, 0);
17778 }
17779
17780 ftype->field (iparams).set_type (arg_type);
17781 iparams++;
17782 }
17783 child_die = child_die->sibling;
17784 }
17785 }
17786
17787 return ftype;
17788 }
17789
17790 static struct type *
17791 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17792 {
17793 struct objfile *objfile = cu->per_objfile->objfile;
17794 const char *name = NULL;
17795 struct type *this_type, *target_type;
17796
17797 name = dwarf2_full_name (NULL, die, cu);
17798 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17799 this_type->set_target_is_stub (true);
17800 set_die_type (die, this_type, cu);
17801 target_type = die_type (die, cu);
17802 if (target_type != this_type)
17803 TYPE_TARGET_TYPE (this_type) = target_type;
17804 else
17805 {
17806 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17807 spec and cause infinite loops in GDB. */
17808 complaint (_("Self-referential DW_TAG_typedef "
17809 "- DIE at %s [in module %s]"),
17810 sect_offset_str (die->sect_off), objfile_name (objfile));
17811 TYPE_TARGET_TYPE (this_type) = NULL;
17812 }
17813 if (name == NULL)
17814 {
17815 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17816 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17817 Handle these by just returning the target type, rather than
17818 constructing an anonymous typedef type and trying to handle this
17819 elsewhere. */
17820 set_die_type (die, target_type, cu);
17821 return target_type;
17822 }
17823 return this_type;
17824 }
17825
17826 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17827 (which may be different from NAME) to the architecture back-end to allow
17828 it to guess the correct format if necessary. */
17829
17830 static struct type *
17831 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17832 const char *name_hint, enum bfd_endian byte_order)
17833 {
17834 struct gdbarch *gdbarch = objfile->arch ();
17835 const struct floatformat **format;
17836 struct type *type;
17837
17838 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17839 if (format)
17840 type = init_float_type (objfile, bits, name, format, byte_order);
17841 else
17842 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17843
17844 return type;
17845 }
17846
17847 /* Allocate an integer type of size BITS and name NAME. */
17848
17849 static struct type *
17850 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17851 int bits, int unsigned_p, const char *name)
17852 {
17853 struct type *type;
17854
17855 /* Versions of Intel's C Compiler generate an integer type called "void"
17856 instead of using DW_TAG_unspecified_type. This has been seen on
17857 at least versions 14, 17, and 18. */
17858 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17859 && strcmp (name, "void") == 0)
17860 type = objfile_type (objfile)->builtin_void;
17861 else
17862 type = init_integer_type (objfile, bits, unsigned_p, name);
17863
17864 return type;
17865 }
17866
17867 /* Initialise and return a floating point type of size BITS suitable for
17868 use as a component of a complex number. The NAME_HINT is passed through
17869 when initialising the floating point type and is the name of the complex
17870 type.
17871
17872 As DWARF doesn't currently provide an explicit name for the components
17873 of a complex number, but it can be helpful to have these components
17874 named, we try to select a suitable name based on the size of the
17875 component. */
17876 static struct type *
17877 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17878 struct objfile *objfile,
17879 int bits, const char *name_hint,
17880 enum bfd_endian byte_order)
17881 {
17882 gdbarch *gdbarch = objfile->arch ();
17883 struct type *tt = nullptr;
17884
17885 /* Try to find a suitable floating point builtin type of size BITS.
17886 We're going to use the name of this type as the name for the complex
17887 target type that we are about to create. */
17888 switch (cu->language)
17889 {
17890 case language_fortran:
17891 switch (bits)
17892 {
17893 case 32:
17894 tt = builtin_f_type (gdbarch)->builtin_real;
17895 break;
17896 case 64:
17897 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17898 break;
17899 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17900 case 128:
17901 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17902 break;
17903 }
17904 break;
17905 default:
17906 switch (bits)
17907 {
17908 case 32:
17909 tt = builtin_type (gdbarch)->builtin_float;
17910 break;
17911 case 64:
17912 tt = builtin_type (gdbarch)->builtin_double;
17913 break;
17914 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17915 case 128:
17916 tt = builtin_type (gdbarch)->builtin_long_double;
17917 break;
17918 }
17919 break;
17920 }
17921
17922 /* If the type we found doesn't match the size we were looking for, then
17923 pretend we didn't find a type at all, the complex target type we
17924 create will then be nameless. */
17925 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17926 tt = nullptr;
17927
17928 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17929 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17930 }
17931
17932 /* Find a representation of a given base type and install
17933 it in the TYPE field of the die. */
17934
17935 static struct type *
17936 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17937 {
17938 struct objfile *objfile = cu->per_objfile->objfile;
17939 struct type *type;
17940 struct attribute *attr;
17941 int encoding = 0, bits = 0;
17942 const char *name;
17943 gdbarch *arch;
17944
17945 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17946 if (attr != nullptr)
17947 encoding = DW_UNSND (attr);
17948 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17949 if (attr != nullptr)
17950 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17951 name = dwarf2_name (die, cu);
17952 if (!name)
17953 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17954
17955 arch = objfile->arch ();
17956 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17957
17958 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17959 if (attr)
17960 {
17961 int endianity = DW_UNSND (attr);
17962
17963 switch (endianity)
17964 {
17965 case DW_END_big:
17966 byte_order = BFD_ENDIAN_BIG;
17967 break;
17968 case DW_END_little:
17969 byte_order = BFD_ENDIAN_LITTLE;
17970 break;
17971 default:
17972 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17973 break;
17974 }
17975 }
17976
17977 switch (encoding)
17978 {
17979 case DW_ATE_address:
17980 /* Turn DW_ATE_address into a void * pointer. */
17981 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17982 type = init_pointer_type (objfile, bits, name, type);
17983 break;
17984 case DW_ATE_boolean:
17985 type = init_boolean_type (objfile, bits, 1, name);
17986 break;
17987 case DW_ATE_complex_float:
17988 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17989 byte_order);
17990 if (type->code () == TYPE_CODE_ERROR)
17991 {
17992 if (name == nullptr)
17993 {
17994 struct obstack *obstack
17995 = &cu->per_objfile->objfile->objfile_obstack;
17996 name = obconcat (obstack, "_Complex ", type->name (),
17997 nullptr);
17998 }
17999 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18000 }
18001 else
18002 type = init_complex_type (name, type);
18003 break;
18004 case DW_ATE_decimal_float:
18005 type = init_decfloat_type (objfile, bits, name);
18006 break;
18007 case DW_ATE_float:
18008 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
18009 break;
18010 case DW_ATE_signed:
18011 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18012 break;
18013 case DW_ATE_unsigned:
18014 if (cu->language == language_fortran
18015 && name
18016 && startswith (name, "character("))
18017 type = init_character_type (objfile, bits, 1, name);
18018 else
18019 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18020 break;
18021 case DW_ATE_signed_char:
18022 if (cu->language == language_ada || cu->language == language_m2
18023 || cu->language == language_pascal
18024 || cu->language == language_fortran)
18025 type = init_character_type (objfile, bits, 0, name);
18026 else
18027 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18028 break;
18029 case DW_ATE_unsigned_char:
18030 if (cu->language == language_ada || cu->language == language_m2
18031 || cu->language == language_pascal
18032 || cu->language == language_fortran
18033 || cu->language == language_rust)
18034 type = init_character_type (objfile, bits, 1, name);
18035 else
18036 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18037 break;
18038 case DW_ATE_UTF:
18039 {
18040 if (bits == 16)
18041 type = builtin_type (arch)->builtin_char16;
18042 else if (bits == 32)
18043 type = builtin_type (arch)->builtin_char32;
18044 else
18045 {
18046 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
18047 bits);
18048 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18049 }
18050 return set_die_type (die, type, cu);
18051 }
18052 break;
18053
18054 default:
18055 complaint (_("unsupported DW_AT_encoding: '%s'"),
18056 dwarf_type_encoding_name (encoding));
18057 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18058 break;
18059 }
18060
18061 if (name && strcmp (name, "char") == 0)
18062 type->set_has_no_signedness (true);
18063
18064 maybe_set_alignment (cu, die, type);
18065
18066 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
18067
18068 return set_die_type (die, type, cu);
18069 }
18070
18071 /* Parse dwarf attribute if it's a block, reference or constant and put the
18072 resulting value of the attribute into struct bound_prop.
18073 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18074
18075 static int
18076 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18077 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18078 struct type *default_type)
18079 {
18080 struct dwarf2_property_baton *baton;
18081 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18082 struct objfile *objfile = per_objfile->objfile;
18083 struct obstack *obstack = &objfile->objfile_obstack;
18084
18085 gdb_assert (default_type != NULL);
18086
18087 if (attr == NULL || prop == NULL)
18088 return 0;
18089
18090 if (attr->form_is_block ())
18091 {
18092 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18093 baton->property_type = default_type;
18094 baton->locexpr.per_cu = cu->per_cu;
18095 baton->locexpr.per_objfile = per_objfile;
18096 baton->locexpr.size = DW_BLOCK (attr)->size;
18097 baton->locexpr.data = DW_BLOCK (attr)->data;
18098 switch (attr->name)
18099 {
18100 case DW_AT_string_length:
18101 baton->locexpr.is_reference = true;
18102 break;
18103 default:
18104 baton->locexpr.is_reference = false;
18105 break;
18106 }
18107
18108 prop->set_locexpr (baton);
18109 gdb_assert (prop->baton () != NULL);
18110 }
18111 else if (attr->form_is_ref ())
18112 {
18113 struct dwarf2_cu *target_cu = cu;
18114 struct die_info *target_die;
18115 struct attribute *target_attr;
18116
18117 target_die = follow_die_ref (die, attr, &target_cu);
18118 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18119 if (target_attr == NULL)
18120 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18121 target_cu);
18122 if (target_attr == NULL)
18123 return 0;
18124
18125 switch (target_attr->name)
18126 {
18127 case DW_AT_location:
18128 if (target_attr->form_is_section_offset ())
18129 {
18130 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18131 baton->property_type = die_type (target_die, target_cu);
18132 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18133 prop->set_loclist (baton);
18134 gdb_assert (prop->baton () != NULL);
18135 }
18136 else if (target_attr->form_is_block ())
18137 {
18138 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18139 baton->property_type = die_type (target_die, target_cu);
18140 baton->locexpr.per_cu = cu->per_cu;
18141 baton->locexpr.per_objfile = per_objfile;
18142 baton->locexpr.size = DW_BLOCK (target_attr)->size;
18143 baton->locexpr.data = DW_BLOCK (target_attr)->data;
18144 baton->locexpr.is_reference = true;
18145 prop->set_locexpr (baton);
18146 gdb_assert (prop->baton () != NULL);
18147 }
18148 else
18149 {
18150 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18151 "dynamic property");
18152 return 0;
18153 }
18154 break;
18155 case DW_AT_data_member_location:
18156 {
18157 LONGEST offset;
18158
18159 if (!handle_data_member_location (target_die, target_cu,
18160 &offset))
18161 return 0;
18162
18163 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18164 baton->property_type = read_type_die (target_die->parent,
18165 target_cu);
18166 baton->offset_info.offset = offset;
18167 baton->offset_info.type = die_type (target_die, target_cu);
18168 prop->set_addr_offset (baton);
18169 break;
18170 }
18171 }
18172 }
18173 else if (attr->form_is_constant ())
18174 prop->set_const_val (attr->constant_value (0));
18175 else
18176 {
18177 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18178 dwarf2_name (die, cu));
18179 return 0;
18180 }
18181
18182 return 1;
18183 }
18184
18185 /* See read.h. */
18186
18187 struct type *
18188 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
18189 {
18190 struct type *int_type;
18191
18192 /* Helper macro to examine the various builtin types. */
18193 #define TRY_TYPE(F) \
18194 int_type = (unsigned_p \
18195 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18196 : objfile_type (objfile)->builtin_ ## F); \
18197 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
18198 return int_type
18199
18200 TRY_TYPE (char);
18201 TRY_TYPE (short);
18202 TRY_TYPE (int);
18203 TRY_TYPE (long);
18204 TRY_TYPE (long_long);
18205
18206 #undef TRY_TYPE
18207
18208 gdb_assert_not_reached ("unable to find suitable integer type");
18209 }
18210
18211 /* See read.h. */
18212
18213 struct type *
18214 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
18215 {
18216 int addr_size = this->per_cu->addr_size ();
18217 return this->per_objfile->int_type (addr_size, unsigned_p);
18218 }
18219
18220 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18221 present (which is valid) then compute the default type based on the
18222 compilation units address size. */
18223
18224 static struct type *
18225 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18226 {
18227 struct type *index_type = die_type (die, cu);
18228
18229 /* Dwarf-2 specifications explicitly allows to create subrange types
18230 without specifying a base type.
18231 In that case, the base type must be set to the type of
18232 the lower bound, upper bound or count, in that order, if any of these
18233 three attributes references an object that has a type.
18234 If no base type is found, the Dwarf-2 specifications say that
18235 a signed integer type of size equal to the size of an address should
18236 be used.
18237 For the following C code: `extern char gdb_int [];'
18238 GCC produces an empty range DIE.
18239 FIXME: muller/2010-05-28: Possible references to object for low bound,
18240 high bound or count are not yet handled by this code. */
18241 if (index_type->code () == TYPE_CODE_VOID)
18242 index_type = cu->addr_sized_int_type (false);
18243
18244 return index_type;
18245 }
18246
18247 /* Read the given DW_AT_subrange DIE. */
18248
18249 static struct type *
18250 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18251 {
18252 struct type *base_type, *orig_base_type;
18253 struct type *range_type;
18254 struct attribute *attr;
18255 struct dynamic_prop low, high;
18256 int low_default_is_valid;
18257 int high_bound_is_count = 0;
18258 const char *name;
18259 ULONGEST negative_mask;
18260
18261 orig_base_type = read_subrange_index_type (die, cu);
18262
18263 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18264 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18265 creating the range type, but we use the result of check_typedef
18266 when examining properties of the type. */
18267 base_type = check_typedef (orig_base_type);
18268
18269 /* The die_type call above may have already set the type for this DIE. */
18270 range_type = get_die_type (die, cu);
18271 if (range_type)
18272 return range_type;
18273
18274 high.set_const_val (0);
18275
18276 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18277 omitting DW_AT_lower_bound. */
18278 switch (cu->language)
18279 {
18280 case language_c:
18281 case language_cplus:
18282 low.set_const_val (0);
18283 low_default_is_valid = 1;
18284 break;
18285 case language_fortran:
18286 low.set_const_val (1);
18287 low_default_is_valid = 1;
18288 break;
18289 case language_d:
18290 case language_objc:
18291 case language_rust:
18292 low.set_const_val (0);
18293 low_default_is_valid = (cu->header.version >= 4);
18294 break;
18295 case language_ada:
18296 case language_m2:
18297 case language_pascal:
18298 low.set_const_val (1);
18299 low_default_is_valid = (cu->header.version >= 4);
18300 break;
18301 default:
18302 low.set_const_val (0);
18303 low_default_is_valid = 0;
18304 break;
18305 }
18306
18307 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18308 if (attr != nullptr)
18309 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18310 else if (!low_default_is_valid)
18311 complaint (_("Missing DW_AT_lower_bound "
18312 "- DIE at %s [in module %s]"),
18313 sect_offset_str (die->sect_off),
18314 objfile_name (cu->per_objfile->objfile));
18315
18316 struct attribute *attr_ub, *attr_count;
18317 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18318 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18319 {
18320 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18321 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18322 {
18323 /* If bounds are constant do the final calculation here. */
18324 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18325 high.set_const_val (low.const_val () + high.const_val () - 1);
18326 else
18327 high_bound_is_count = 1;
18328 }
18329 else
18330 {
18331 if (attr_ub != NULL)
18332 complaint (_("Unresolved DW_AT_upper_bound "
18333 "- DIE at %s [in module %s]"),
18334 sect_offset_str (die->sect_off),
18335 objfile_name (cu->per_objfile->objfile));
18336 if (attr_count != NULL)
18337 complaint (_("Unresolved DW_AT_count "
18338 "- DIE at %s [in module %s]"),
18339 sect_offset_str (die->sect_off),
18340 objfile_name (cu->per_objfile->objfile));
18341 }
18342 }
18343
18344 LONGEST bias = 0;
18345 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18346 if (bias_attr != nullptr && bias_attr->form_is_constant ())
18347 bias = bias_attr->constant_value (0);
18348
18349 /* Normally, the DWARF producers are expected to use a signed
18350 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18351 But this is unfortunately not always the case, as witnessed
18352 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18353 is used instead. To work around that ambiguity, we treat
18354 the bounds as signed, and thus sign-extend their values, when
18355 the base type is signed. */
18356 negative_mask =
18357 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18358 if (low.kind () == PROP_CONST
18359 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
18360 low.set_const_val (low.const_val () | negative_mask);
18361 if (high.kind () == PROP_CONST
18362 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
18363 high.set_const_val (high.const_val () | negative_mask);
18364
18365 /* Check for bit and byte strides. */
18366 struct dynamic_prop byte_stride_prop;
18367 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18368 if (attr_byte_stride != nullptr)
18369 {
18370 struct type *prop_type = cu->addr_sized_int_type (false);
18371 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18372 prop_type);
18373 }
18374
18375 struct dynamic_prop bit_stride_prop;
18376 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18377 if (attr_bit_stride != nullptr)
18378 {
18379 /* It only makes sense to have either a bit or byte stride. */
18380 if (attr_byte_stride != nullptr)
18381 {
18382 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18383 "- DIE at %s [in module %s]"),
18384 sect_offset_str (die->sect_off),
18385 objfile_name (cu->per_objfile->objfile));
18386 attr_bit_stride = nullptr;
18387 }
18388 else
18389 {
18390 struct type *prop_type = cu->addr_sized_int_type (false);
18391 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18392 prop_type);
18393 }
18394 }
18395
18396 if (attr_byte_stride != nullptr
18397 || attr_bit_stride != nullptr)
18398 {
18399 bool byte_stride_p = (attr_byte_stride != nullptr);
18400 struct dynamic_prop *stride
18401 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18402
18403 range_type
18404 = create_range_type_with_stride (NULL, orig_base_type, &low,
18405 &high, bias, stride, byte_stride_p);
18406 }
18407 else
18408 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18409
18410 if (high_bound_is_count)
18411 range_type->bounds ()->flag_upper_bound_is_count = 1;
18412
18413 /* Ada expects an empty array on no boundary attributes. */
18414 if (attr == NULL && cu->language != language_ada)
18415 range_type->bounds ()->high.set_undefined ();
18416
18417 name = dwarf2_name (die, cu);
18418 if (name)
18419 range_type->set_name (name);
18420
18421 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18422 if (attr != nullptr)
18423 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18424
18425 maybe_set_alignment (cu, die, range_type);
18426
18427 set_die_type (die, range_type, cu);
18428
18429 /* set_die_type should be already done. */
18430 set_descriptive_type (range_type, die, cu);
18431
18432 return range_type;
18433 }
18434
18435 static struct type *
18436 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18437 {
18438 struct type *type;
18439
18440 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
18441 type->set_name (dwarf2_name (die, cu));
18442
18443 /* In Ada, an unspecified type is typically used when the description
18444 of the type is deferred to a different unit. When encountering
18445 such a type, we treat it as a stub, and try to resolve it later on,
18446 when needed. */
18447 if (cu->language == language_ada)
18448 type->set_is_stub (true);
18449
18450 return set_die_type (die, type, cu);
18451 }
18452
18453 /* Read a single die and all its descendents. Set the die's sibling
18454 field to NULL; set other fields in the die correctly, and set all
18455 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18456 location of the info_ptr after reading all of those dies. PARENT
18457 is the parent of the die in question. */
18458
18459 static struct die_info *
18460 read_die_and_children (const struct die_reader_specs *reader,
18461 const gdb_byte *info_ptr,
18462 const gdb_byte **new_info_ptr,
18463 struct die_info *parent)
18464 {
18465 struct die_info *die;
18466 const gdb_byte *cur_ptr;
18467
18468 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
18469 if (die == NULL)
18470 {
18471 *new_info_ptr = cur_ptr;
18472 return NULL;
18473 }
18474 store_in_ref_table (die, reader->cu);
18475
18476 if (die->has_children)
18477 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18478 else
18479 {
18480 die->child = NULL;
18481 *new_info_ptr = cur_ptr;
18482 }
18483
18484 die->sibling = NULL;
18485 die->parent = parent;
18486 return die;
18487 }
18488
18489 /* Read a die, all of its descendents, and all of its siblings; set
18490 all of the fields of all of the dies correctly. Arguments are as
18491 in read_die_and_children. */
18492
18493 static struct die_info *
18494 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18495 const gdb_byte *info_ptr,
18496 const gdb_byte **new_info_ptr,
18497 struct die_info *parent)
18498 {
18499 struct die_info *first_die, *last_sibling;
18500 const gdb_byte *cur_ptr;
18501
18502 cur_ptr = info_ptr;
18503 first_die = last_sibling = NULL;
18504
18505 while (1)
18506 {
18507 struct die_info *die
18508 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18509
18510 if (die == NULL)
18511 {
18512 *new_info_ptr = cur_ptr;
18513 return first_die;
18514 }
18515
18516 if (!first_die)
18517 first_die = die;
18518 else
18519 last_sibling->sibling = die;
18520
18521 last_sibling = die;
18522 }
18523 }
18524
18525 /* Read a die, all of its descendents, and all of its siblings; set
18526 all of the fields of all of the dies correctly. Arguments are as
18527 in read_die_and_children.
18528 This the main entry point for reading a DIE and all its children. */
18529
18530 static struct die_info *
18531 read_die_and_siblings (const struct die_reader_specs *reader,
18532 const gdb_byte *info_ptr,
18533 const gdb_byte **new_info_ptr,
18534 struct die_info *parent)
18535 {
18536 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18537 new_info_ptr, parent);
18538
18539 if (dwarf_die_debug)
18540 {
18541 fprintf_unfiltered (gdb_stdlog,
18542 "Read die from %s@0x%x of %s:\n",
18543 reader->die_section->get_name (),
18544 (unsigned) (info_ptr - reader->die_section->buffer),
18545 bfd_get_filename (reader->abfd));
18546 dump_die (die, dwarf_die_debug);
18547 }
18548
18549 return die;
18550 }
18551
18552 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18553 attributes.
18554 The caller is responsible for filling in the extra attributes
18555 and updating (*DIEP)->num_attrs.
18556 Set DIEP to point to a newly allocated die with its information,
18557 except for its child, sibling, and parent fields. */
18558
18559 static const gdb_byte *
18560 read_full_die_1 (const struct die_reader_specs *reader,
18561 struct die_info **diep, const gdb_byte *info_ptr,
18562 int num_extra_attrs)
18563 {
18564 unsigned int abbrev_number, bytes_read, i;
18565 struct abbrev_info *abbrev;
18566 struct die_info *die;
18567 struct dwarf2_cu *cu = reader->cu;
18568 bfd *abfd = reader->abfd;
18569
18570 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18571 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18572 info_ptr += bytes_read;
18573 if (!abbrev_number)
18574 {
18575 *diep = NULL;
18576 return info_ptr;
18577 }
18578
18579 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18580 if (!abbrev)
18581 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18582 abbrev_number,
18583 bfd_get_filename (abfd));
18584
18585 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18586 die->sect_off = sect_off;
18587 die->tag = abbrev->tag;
18588 die->abbrev = abbrev_number;
18589 die->has_children = abbrev->has_children;
18590
18591 /* Make the result usable.
18592 The caller needs to update num_attrs after adding the extra
18593 attributes. */
18594 die->num_attrs = abbrev->num_attrs;
18595
18596 std::vector<int> indexes_that_need_reprocess;
18597 for (i = 0; i < abbrev->num_attrs; ++i)
18598 {
18599 bool need_reprocess;
18600 info_ptr =
18601 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18602 info_ptr, &need_reprocess);
18603 if (need_reprocess)
18604 indexes_that_need_reprocess.push_back (i);
18605 }
18606
18607 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18608 if (attr != nullptr)
18609 cu->str_offsets_base = DW_UNSND (attr);
18610
18611 attr = die->attr (DW_AT_loclists_base);
18612 if (attr != nullptr)
18613 cu->loclist_base = DW_UNSND (attr);
18614
18615 auto maybe_addr_base = die->addr_base ();
18616 if (maybe_addr_base.has_value ())
18617 cu->addr_base = *maybe_addr_base;
18618
18619 attr = die->attr (DW_AT_rnglists_base);
18620 if (attr != nullptr)
18621 cu->ranges_base = DW_UNSND (attr);
18622
18623 for (int index : indexes_that_need_reprocess)
18624 read_attribute_reprocess (reader, &die->attrs[index], die->tag);
18625 *diep = die;
18626 return info_ptr;
18627 }
18628
18629 /* Read a die and all its attributes.
18630 Set DIEP to point to a newly allocated die with its information,
18631 except for its child, sibling, and parent fields. */
18632
18633 static const gdb_byte *
18634 read_full_die (const struct die_reader_specs *reader,
18635 struct die_info **diep, const gdb_byte *info_ptr)
18636 {
18637 const gdb_byte *result;
18638
18639 result = read_full_die_1 (reader, diep, info_ptr, 0);
18640
18641 if (dwarf_die_debug)
18642 {
18643 fprintf_unfiltered (gdb_stdlog,
18644 "Read die from %s@0x%x of %s:\n",
18645 reader->die_section->get_name (),
18646 (unsigned) (info_ptr - reader->die_section->buffer),
18647 bfd_get_filename (reader->abfd));
18648 dump_die (*diep, dwarf_die_debug);
18649 }
18650
18651 return result;
18652 }
18653 \f
18654
18655 /* Returns nonzero if TAG represents a type that we might generate a partial
18656 symbol for. */
18657
18658 static int
18659 is_type_tag_for_partial (int tag)
18660 {
18661 switch (tag)
18662 {
18663 #if 0
18664 /* Some types that would be reasonable to generate partial symbols for,
18665 that we don't at present. */
18666 case DW_TAG_array_type:
18667 case DW_TAG_file_type:
18668 case DW_TAG_ptr_to_member_type:
18669 case DW_TAG_set_type:
18670 case DW_TAG_string_type:
18671 case DW_TAG_subroutine_type:
18672 #endif
18673 case DW_TAG_base_type:
18674 case DW_TAG_class_type:
18675 case DW_TAG_interface_type:
18676 case DW_TAG_enumeration_type:
18677 case DW_TAG_structure_type:
18678 case DW_TAG_subrange_type:
18679 case DW_TAG_typedef:
18680 case DW_TAG_union_type:
18681 return 1;
18682 default:
18683 return 0;
18684 }
18685 }
18686
18687 /* Load all DIEs that are interesting for partial symbols into memory. */
18688
18689 static struct partial_die_info *
18690 load_partial_dies (const struct die_reader_specs *reader,
18691 const gdb_byte *info_ptr, int building_psymtab)
18692 {
18693 struct dwarf2_cu *cu = reader->cu;
18694 struct objfile *objfile = cu->per_objfile->objfile;
18695 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18696 unsigned int bytes_read;
18697 unsigned int load_all = 0;
18698 int nesting_level = 1;
18699
18700 parent_die = NULL;
18701 last_die = NULL;
18702
18703 gdb_assert (cu->per_cu != NULL);
18704 if (cu->per_cu->load_all_dies)
18705 load_all = 1;
18706
18707 cu->partial_dies
18708 = htab_create_alloc_ex (cu->header.length / 12,
18709 partial_die_hash,
18710 partial_die_eq,
18711 NULL,
18712 &cu->comp_unit_obstack,
18713 hashtab_obstack_allocate,
18714 dummy_obstack_deallocate);
18715
18716 while (1)
18717 {
18718 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18719
18720 /* A NULL abbrev means the end of a series of children. */
18721 if (abbrev == NULL)
18722 {
18723 if (--nesting_level == 0)
18724 return first_die;
18725
18726 info_ptr += bytes_read;
18727 last_die = parent_die;
18728 parent_die = parent_die->die_parent;
18729 continue;
18730 }
18731
18732 /* Check for template arguments. We never save these; if
18733 they're seen, we just mark the parent, and go on our way. */
18734 if (parent_die != NULL
18735 && cu->language == language_cplus
18736 && (abbrev->tag == DW_TAG_template_type_param
18737 || abbrev->tag == DW_TAG_template_value_param))
18738 {
18739 parent_die->has_template_arguments = 1;
18740
18741 if (!load_all)
18742 {
18743 /* We don't need a partial DIE for the template argument. */
18744 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18745 continue;
18746 }
18747 }
18748
18749 /* We only recurse into c++ subprograms looking for template arguments.
18750 Skip their other children. */
18751 if (!load_all
18752 && cu->language == language_cplus
18753 && parent_die != NULL
18754 && parent_die->tag == DW_TAG_subprogram
18755 && abbrev->tag != DW_TAG_inlined_subroutine)
18756 {
18757 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18758 continue;
18759 }
18760
18761 /* Check whether this DIE is interesting enough to save. Normally
18762 we would not be interested in members here, but there may be
18763 later variables referencing them via DW_AT_specification (for
18764 static members). */
18765 if (!load_all
18766 && !is_type_tag_for_partial (abbrev->tag)
18767 && abbrev->tag != DW_TAG_constant
18768 && abbrev->tag != DW_TAG_enumerator
18769 && abbrev->tag != DW_TAG_subprogram
18770 && abbrev->tag != DW_TAG_inlined_subroutine
18771 && abbrev->tag != DW_TAG_lexical_block
18772 && abbrev->tag != DW_TAG_variable
18773 && abbrev->tag != DW_TAG_namespace
18774 && abbrev->tag != DW_TAG_module
18775 && abbrev->tag != DW_TAG_member
18776 && abbrev->tag != DW_TAG_imported_unit
18777 && abbrev->tag != DW_TAG_imported_declaration)
18778 {
18779 /* Otherwise we skip to the next sibling, if any. */
18780 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18781 continue;
18782 }
18783
18784 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18785 abbrev);
18786
18787 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18788
18789 /* This two-pass algorithm for processing partial symbols has a
18790 high cost in cache pressure. Thus, handle some simple cases
18791 here which cover the majority of C partial symbols. DIEs
18792 which neither have specification tags in them, nor could have
18793 specification tags elsewhere pointing at them, can simply be
18794 processed and discarded.
18795
18796 This segment is also optional; scan_partial_symbols and
18797 add_partial_symbol will handle these DIEs if we chain
18798 them in normally. When compilers which do not emit large
18799 quantities of duplicate debug information are more common,
18800 this code can probably be removed. */
18801
18802 /* Any complete simple types at the top level (pretty much all
18803 of them, for a language without namespaces), can be processed
18804 directly. */
18805 if (parent_die == NULL
18806 && pdi.has_specification == 0
18807 && pdi.is_declaration == 0
18808 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18809 || pdi.tag == DW_TAG_base_type
18810 || pdi.tag == DW_TAG_subrange_type))
18811 {
18812 if (building_psymtab && pdi.raw_name != NULL)
18813 add_partial_symbol (&pdi, cu);
18814
18815 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18816 continue;
18817 }
18818
18819 /* The exception for DW_TAG_typedef with has_children above is
18820 a workaround of GCC PR debug/47510. In the case of this complaint
18821 type_name_or_error will error on such types later.
18822
18823 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18824 it could not find the child DIEs referenced later, this is checked
18825 above. In correct DWARF DW_TAG_typedef should have no children. */
18826
18827 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18828 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18829 "- DIE at %s [in module %s]"),
18830 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18831
18832 /* If we're at the second level, and we're an enumerator, and
18833 our parent has no specification (meaning possibly lives in a
18834 namespace elsewhere), then we can add the partial symbol now
18835 instead of queueing it. */
18836 if (pdi.tag == DW_TAG_enumerator
18837 && parent_die != NULL
18838 && parent_die->die_parent == NULL
18839 && parent_die->tag == DW_TAG_enumeration_type
18840 && parent_die->has_specification == 0)
18841 {
18842 if (pdi.raw_name == NULL)
18843 complaint (_("malformed enumerator DIE ignored"));
18844 else if (building_psymtab)
18845 add_partial_symbol (&pdi, cu);
18846
18847 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18848 continue;
18849 }
18850
18851 struct partial_die_info *part_die
18852 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18853
18854 /* We'll save this DIE so link it in. */
18855 part_die->die_parent = parent_die;
18856 part_die->die_sibling = NULL;
18857 part_die->die_child = NULL;
18858
18859 if (last_die && last_die == parent_die)
18860 last_die->die_child = part_die;
18861 else if (last_die)
18862 last_die->die_sibling = part_die;
18863
18864 last_die = part_die;
18865
18866 if (first_die == NULL)
18867 first_die = part_die;
18868
18869 /* Maybe add the DIE to the hash table. Not all DIEs that we
18870 find interesting need to be in the hash table, because we
18871 also have the parent/sibling/child chains; only those that we
18872 might refer to by offset later during partial symbol reading.
18873
18874 For now this means things that might have be the target of a
18875 DW_AT_specification, DW_AT_abstract_origin, or
18876 DW_AT_extension. DW_AT_extension will refer only to
18877 namespaces; DW_AT_abstract_origin refers to functions (and
18878 many things under the function DIE, but we do not recurse
18879 into function DIEs during partial symbol reading) and
18880 possibly variables as well; DW_AT_specification refers to
18881 declarations. Declarations ought to have the DW_AT_declaration
18882 flag. It happens that GCC forgets to put it in sometimes, but
18883 only for functions, not for types.
18884
18885 Adding more things than necessary to the hash table is harmless
18886 except for the performance cost. Adding too few will result in
18887 wasted time in find_partial_die, when we reread the compilation
18888 unit with load_all_dies set. */
18889
18890 if (load_all
18891 || abbrev->tag == DW_TAG_constant
18892 || abbrev->tag == DW_TAG_subprogram
18893 || abbrev->tag == DW_TAG_variable
18894 || abbrev->tag == DW_TAG_namespace
18895 || part_die->is_declaration)
18896 {
18897 void **slot;
18898
18899 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18900 to_underlying (part_die->sect_off),
18901 INSERT);
18902 *slot = part_die;
18903 }
18904
18905 /* For some DIEs we want to follow their children (if any). For C
18906 we have no reason to follow the children of structures; for other
18907 languages we have to, so that we can get at method physnames
18908 to infer fully qualified class names, for DW_AT_specification,
18909 and for C++ template arguments. For C++, we also look one level
18910 inside functions to find template arguments (if the name of the
18911 function does not already contain the template arguments).
18912
18913 For Ada and Fortran, we need to scan the children of subprograms
18914 and lexical blocks as well because these languages allow the
18915 definition of nested entities that could be interesting for the
18916 debugger, such as nested subprograms for instance. */
18917 if (last_die->has_children
18918 && (load_all
18919 || last_die->tag == DW_TAG_namespace
18920 || last_die->tag == DW_TAG_module
18921 || last_die->tag == DW_TAG_enumeration_type
18922 || (cu->language == language_cplus
18923 && last_die->tag == DW_TAG_subprogram
18924 && (last_die->raw_name == NULL
18925 || strchr (last_die->raw_name, '<') == NULL))
18926 || (cu->language != language_c
18927 && (last_die->tag == DW_TAG_class_type
18928 || last_die->tag == DW_TAG_interface_type
18929 || last_die->tag == DW_TAG_structure_type
18930 || last_die->tag == DW_TAG_union_type))
18931 || ((cu->language == language_ada
18932 || cu->language == language_fortran)
18933 && (last_die->tag == DW_TAG_subprogram
18934 || last_die->tag == DW_TAG_lexical_block))))
18935 {
18936 nesting_level++;
18937 parent_die = last_die;
18938 continue;
18939 }
18940
18941 /* Otherwise we skip to the next sibling, if any. */
18942 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18943
18944 /* Back to the top, do it again. */
18945 }
18946 }
18947
18948 partial_die_info::partial_die_info (sect_offset sect_off_,
18949 struct abbrev_info *abbrev)
18950 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18951 {
18952 }
18953
18954 /* See class definition. */
18955
18956 const char *
18957 partial_die_info::name (dwarf2_cu *cu)
18958 {
18959 if (!canonical_name && raw_name != nullptr)
18960 {
18961 struct objfile *objfile = cu->per_objfile->objfile;
18962 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
18963 canonical_name = 1;
18964 }
18965
18966 return raw_name;
18967 }
18968
18969 /* Read a minimal amount of information into the minimal die structure.
18970 INFO_PTR should point just after the initial uleb128 of a DIE. */
18971
18972 const gdb_byte *
18973 partial_die_info::read (const struct die_reader_specs *reader,
18974 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18975 {
18976 struct dwarf2_cu *cu = reader->cu;
18977 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18978 unsigned int i;
18979 int has_low_pc_attr = 0;
18980 int has_high_pc_attr = 0;
18981 int high_pc_relative = 0;
18982
18983 for (i = 0; i < abbrev.num_attrs; ++i)
18984 {
18985 attribute attr;
18986 bool need_reprocess;
18987 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18988 info_ptr, &need_reprocess);
18989 /* String and address offsets that need to do the reprocessing have
18990 already been read at this point, so there is no need to wait until
18991 the loop terminates to do the reprocessing. */
18992 if (need_reprocess)
18993 read_attribute_reprocess (reader, &attr, tag);
18994 /* Store the data if it is of an attribute we want to keep in a
18995 partial symbol table. */
18996 switch (attr.name)
18997 {
18998 case DW_AT_name:
18999 switch (tag)
19000 {
19001 case DW_TAG_compile_unit:
19002 case DW_TAG_partial_unit:
19003 case DW_TAG_type_unit:
19004 /* Compilation units have a DW_AT_name that is a filename, not
19005 a source language identifier. */
19006 case DW_TAG_enumeration_type:
19007 case DW_TAG_enumerator:
19008 /* These tags always have simple identifiers already; no need
19009 to canonicalize them. */
19010 canonical_name = 1;
19011 raw_name = DW_STRING (&attr);
19012 break;
19013 default:
19014 canonical_name = 0;
19015 raw_name = DW_STRING (&attr);
19016 break;
19017 }
19018 break;
19019 case DW_AT_linkage_name:
19020 case DW_AT_MIPS_linkage_name:
19021 /* Note that both forms of linkage name might appear. We
19022 assume they will be the same, and we only store the last
19023 one we see. */
19024 linkage_name = attr.value_as_string ();
19025 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
19026 See https://github.com/rust-lang/rust/issues/32925. */
19027 if (cu->language == language_rust && linkage_name != NULL
19028 && strchr (linkage_name, '{') != NULL)
19029 linkage_name = NULL;
19030 break;
19031 case DW_AT_low_pc:
19032 has_low_pc_attr = 1;
19033 lowpc = attr.value_as_address ();
19034 break;
19035 case DW_AT_high_pc:
19036 has_high_pc_attr = 1;
19037 highpc = attr.value_as_address ();
19038 if (cu->header.version >= 4 && attr.form_is_constant ())
19039 high_pc_relative = 1;
19040 break;
19041 case DW_AT_location:
19042 /* Support the .debug_loc offsets. */
19043 if (attr.form_is_block ())
19044 {
19045 d.locdesc = DW_BLOCK (&attr);
19046 }
19047 else if (attr.form_is_section_offset ())
19048 {
19049 dwarf2_complex_location_expr_complaint ();
19050 }
19051 else
19052 {
19053 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19054 "partial symbol information");
19055 }
19056 break;
19057 case DW_AT_external:
19058 is_external = DW_UNSND (&attr);
19059 break;
19060 case DW_AT_declaration:
19061 is_declaration = DW_UNSND (&attr);
19062 break;
19063 case DW_AT_type:
19064 has_type = 1;
19065 break;
19066 case DW_AT_abstract_origin:
19067 case DW_AT_specification:
19068 case DW_AT_extension:
19069 has_specification = 1;
19070 spec_offset = attr.get_ref_die_offset ();
19071 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19072 || cu->per_cu->is_dwz);
19073 break;
19074 case DW_AT_sibling:
19075 /* Ignore absolute siblings, they might point outside of
19076 the current compile unit. */
19077 if (attr.form == DW_FORM_ref_addr)
19078 complaint (_("ignoring absolute DW_AT_sibling"));
19079 else
19080 {
19081 const gdb_byte *buffer = reader->buffer;
19082 sect_offset off = attr.get_ref_die_offset ();
19083 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19084
19085 if (sibling_ptr < info_ptr)
19086 complaint (_("DW_AT_sibling points backwards"));
19087 else if (sibling_ptr > reader->buffer_end)
19088 reader->die_section->overflow_complaint ();
19089 else
19090 sibling = sibling_ptr;
19091 }
19092 break;
19093 case DW_AT_byte_size:
19094 has_byte_size = 1;
19095 break;
19096 case DW_AT_const_value:
19097 has_const_value = 1;
19098 break;
19099 case DW_AT_calling_convention:
19100 /* DWARF doesn't provide a way to identify a program's source-level
19101 entry point. DW_AT_calling_convention attributes are only meant
19102 to describe functions' calling conventions.
19103
19104 However, because it's a necessary piece of information in
19105 Fortran, and before DWARF 4 DW_CC_program was the only
19106 piece of debugging information whose definition refers to
19107 a 'main program' at all, several compilers marked Fortran
19108 main programs with DW_CC_program --- even when those
19109 functions use the standard calling conventions.
19110
19111 Although DWARF now specifies a way to provide this
19112 information, we support this practice for backward
19113 compatibility. */
19114 if (DW_UNSND (&attr) == DW_CC_program
19115 && cu->language == language_fortran)
19116 main_subprogram = 1;
19117 break;
19118 case DW_AT_inline:
19119 if (DW_UNSND (&attr) == DW_INL_inlined
19120 || DW_UNSND (&attr) == DW_INL_declared_inlined)
19121 may_be_inlined = 1;
19122 break;
19123
19124 case DW_AT_import:
19125 if (tag == DW_TAG_imported_unit)
19126 {
19127 d.sect_off = attr.get_ref_die_offset ();
19128 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19129 || cu->per_cu->is_dwz);
19130 }
19131 break;
19132
19133 case DW_AT_main_subprogram:
19134 main_subprogram = DW_UNSND (&attr);
19135 break;
19136
19137 case DW_AT_ranges:
19138 {
19139 /* DW_AT_rnglists_base does not apply to DIEs from the DWO
19140 skeleton. We take advantage of the fact the DW_AT_ranges
19141 does not appear in DW_TAG_compile_unit of DWO files.
19142
19143 Attributes of the form DW_FORM_rnglistx have already had
19144 their value changed by read_rnglist_index and already
19145 include DW_AT_rnglists_base, so don't need to add the ranges
19146 base, either. */
19147 int need_ranges_base = (tag != DW_TAG_compile_unit
19148 && attr.form != DW_FORM_rnglistx);
19149 unsigned int ranges_offset = (DW_UNSND (&attr)
19150 + (need_ranges_base
19151 ? cu->ranges_base
19152 : 0));
19153
19154 /* Value of the DW_AT_ranges attribute is the offset in the
19155 .debug_ranges section. */
19156 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19157 nullptr, tag))
19158 has_pc_info = 1;
19159 }
19160 break;
19161
19162 default:
19163 break;
19164 }
19165 }
19166
19167 /* For Ada, if both the name and the linkage name appear, we prefer
19168 the latter. This lets "catch exception" work better, regardless
19169 of the order in which the name and linkage name were emitted.
19170 Really, though, this is just a workaround for the fact that gdb
19171 doesn't store both the name and the linkage name. */
19172 if (cu->language == language_ada && linkage_name != nullptr)
19173 raw_name = linkage_name;
19174
19175 if (high_pc_relative)
19176 highpc += lowpc;
19177
19178 if (has_low_pc_attr && has_high_pc_attr)
19179 {
19180 /* When using the GNU linker, .gnu.linkonce. sections are used to
19181 eliminate duplicate copies of functions and vtables and such.
19182 The linker will arbitrarily choose one and discard the others.
19183 The AT_*_pc values for such functions refer to local labels in
19184 these sections. If the section from that file was discarded, the
19185 labels are not in the output, so the relocs get a value of 0.
19186 If this is a discarded function, mark the pc bounds as invalid,
19187 so that GDB will ignore it. */
19188 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
19189 {
19190 struct objfile *objfile = per_objfile->objfile;
19191 struct gdbarch *gdbarch = objfile->arch ();
19192
19193 complaint (_("DW_AT_low_pc %s is zero "
19194 "for DIE at %s [in module %s]"),
19195 paddress (gdbarch, lowpc),
19196 sect_offset_str (sect_off),
19197 objfile_name (objfile));
19198 }
19199 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19200 else if (lowpc >= highpc)
19201 {
19202 struct objfile *objfile = per_objfile->objfile;
19203 struct gdbarch *gdbarch = objfile->arch ();
19204
19205 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19206 "for DIE at %s [in module %s]"),
19207 paddress (gdbarch, lowpc),
19208 paddress (gdbarch, highpc),
19209 sect_offset_str (sect_off),
19210 objfile_name (objfile));
19211 }
19212 else
19213 has_pc_info = 1;
19214 }
19215
19216 return info_ptr;
19217 }
19218
19219 /* Find a cached partial DIE at OFFSET in CU. */
19220
19221 struct partial_die_info *
19222 dwarf2_cu::find_partial_die (sect_offset sect_off)
19223 {
19224 struct partial_die_info *lookup_die = NULL;
19225 struct partial_die_info part_die (sect_off);
19226
19227 lookup_die = ((struct partial_die_info *)
19228 htab_find_with_hash (partial_dies, &part_die,
19229 to_underlying (sect_off)));
19230
19231 return lookup_die;
19232 }
19233
19234 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19235 except in the case of .debug_types DIEs which do not reference
19236 outside their CU (they do however referencing other types via
19237 DW_FORM_ref_sig8). */
19238
19239 static const struct cu_partial_die_info
19240 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19241 {
19242 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19243 struct objfile *objfile = per_objfile->objfile;
19244 struct partial_die_info *pd = NULL;
19245
19246 if (offset_in_dwz == cu->per_cu->is_dwz
19247 && cu->header.offset_in_cu_p (sect_off))
19248 {
19249 pd = cu->find_partial_die (sect_off);
19250 if (pd != NULL)
19251 return { cu, pd };
19252 /* We missed recording what we needed.
19253 Load all dies and try again. */
19254 }
19255 else
19256 {
19257 /* TUs don't reference other CUs/TUs (except via type signatures). */
19258 if (cu->per_cu->is_debug_types)
19259 {
19260 error (_("Dwarf Error: Type Unit at offset %s contains"
19261 " external reference to offset %s [in module %s].\n"),
19262 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19263 bfd_get_filename (objfile->obfd));
19264 }
19265 dwarf2_per_cu_data *per_cu
19266 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19267 per_objfile);
19268
19269 cu = per_objfile->get_cu (per_cu);
19270 if (cu == NULL || cu->partial_dies == NULL)
19271 load_partial_comp_unit (per_cu, per_objfile, nullptr);
19272
19273 cu = per_objfile->get_cu (per_cu);
19274
19275 cu->last_used = 0;
19276 pd = cu->find_partial_die (sect_off);
19277 }
19278
19279 /* If we didn't find it, and not all dies have been loaded,
19280 load them all and try again. */
19281
19282 if (pd == NULL && cu->per_cu->load_all_dies == 0)
19283 {
19284 cu->per_cu->load_all_dies = 1;
19285
19286 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19287 THIS_CU->cu may already be in use. So we can't just free it and
19288 replace its DIEs with the ones we read in. Instead, we leave those
19289 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19290 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19291 set. */
19292 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
19293
19294 pd = cu->find_partial_die (sect_off);
19295 }
19296
19297 if (pd == NULL)
19298 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
19299 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19300 return { cu, pd };
19301 }
19302
19303 /* See if we can figure out if the class lives in a namespace. We do
19304 this by looking for a member function; its demangled name will
19305 contain namespace info, if there is any. */
19306
19307 static void
19308 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19309 struct dwarf2_cu *cu)
19310 {
19311 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19312 what template types look like, because the demangler
19313 frequently doesn't give the same name as the debug info. We
19314 could fix this by only using the demangled name to get the
19315 prefix (but see comment in read_structure_type). */
19316
19317 struct partial_die_info *real_pdi;
19318 struct partial_die_info *child_pdi;
19319
19320 /* If this DIE (this DIE's specification, if any) has a parent, then
19321 we should not do this. We'll prepend the parent's fully qualified
19322 name when we create the partial symbol. */
19323
19324 real_pdi = struct_pdi;
19325 while (real_pdi->has_specification)
19326 {
19327 auto res = find_partial_die (real_pdi->spec_offset,
19328 real_pdi->spec_is_dwz, cu);
19329 real_pdi = res.pdi;
19330 cu = res.cu;
19331 }
19332
19333 if (real_pdi->die_parent != NULL)
19334 return;
19335
19336 for (child_pdi = struct_pdi->die_child;
19337 child_pdi != NULL;
19338 child_pdi = child_pdi->die_sibling)
19339 {
19340 if (child_pdi->tag == DW_TAG_subprogram
19341 && child_pdi->linkage_name != NULL)
19342 {
19343 gdb::unique_xmalloc_ptr<char> actual_class_name
19344 (cu->language_defn->class_name_from_physname
19345 (child_pdi->linkage_name));
19346 if (actual_class_name != NULL)
19347 {
19348 struct objfile *objfile = cu->per_objfile->objfile;
19349 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19350 struct_pdi->canonical_name = 1;
19351 }
19352 break;
19353 }
19354 }
19355 }
19356
19357 /* Return true if a DIE with TAG may have the DW_AT_const_value
19358 attribute. */
19359
19360 static bool
19361 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19362 {
19363 switch (tag)
19364 {
19365 case DW_TAG_constant:
19366 case DW_TAG_enumerator:
19367 case DW_TAG_formal_parameter:
19368 case DW_TAG_template_value_param:
19369 case DW_TAG_variable:
19370 return true;
19371 }
19372
19373 return false;
19374 }
19375
19376 void
19377 partial_die_info::fixup (struct dwarf2_cu *cu)
19378 {
19379 /* Once we've fixed up a die, there's no point in doing so again.
19380 This also avoids a memory leak if we were to call
19381 guess_partial_die_structure_name multiple times. */
19382 if (fixup_called)
19383 return;
19384
19385 /* If we found a reference attribute and the DIE has no name, try
19386 to find a name in the referred to DIE. */
19387
19388 if (raw_name == NULL && has_specification)
19389 {
19390 struct partial_die_info *spec_die;
19391
19392 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19393 spec_die = res.pdi;
19394 cu = res.cu;
19395
19396 spec_die->fixup (cu);
19397
19398 if (spec_die->raw_name)
19399 {
19400 raw_name = spec_die->raw_name;
19401 canonical_name = spec_die->canonical_name;
19402
19403 /* Copy DW_AT_external attribute if it is set. */
19404 if (spec_die->is_external)
19405 is_external = spec_die->is_external;
19406 }
19407 }
19408
19409 if (!has_const_value && has_specification
19410 && can_have_DW_AT_const_value_p (tag))
19411 {
19412 struct partial_die_info *spec_die;
19413
19414 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19415 spec_die = res.pdi;
19416 cu = res.cu;
19417
19418 spec_die->fixup (cu);
19419
19420 if (spec_die->has_const_value)
19421 {
19422 /* Copy DW_AT_const_value attribute if it is set. */
19423 has_const_value = spec_die->has_const_value;
19424 }
19425 }
19426
19427 /* Set default names for some unnamed DIEs. */
19428
19429 if (raw_name == NULL && tag == DW_TAG_namespace)
19430 {
19431 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
19432 canonical_name = 1;
19433 }
19434
19435 /* If there is no parent die to provide a namespace, and there are
19436 children, see if we can determine the namespace from their linkage
19437 name. */
19438 if (cu->language == language_cplus
19439 && !cu->per_objfile->per_bfd->types.empty ()
19440 && die_parent == NULL
19441 && has_children
19442 && (tag == DW_TAG_class_type
19443 || tag == DW_TAG_structure_type
19444 || tag == DW_TAG_union_type))
19445 guess_partial_die_structure_name (this, cu);
19446
19447 /* GCC might emit a nameless struct or union that has a linkage
19448 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19449 if (raw_name == NULL
19450 && (tag == DW_TAG_class_type
19451 || tag == DW_TAG_interface_type
19452 || tag == DW_TAG_structure_type
19453 || tag == DW_TAG_union_type)
19454 && linkage_name != NULL)
19455 {
19456 gdb::unique_xmalloc_ptr<char> demangled
19457 (gdb_demangle (linkage_name, DMGL_TYPES));
19458 if (demangled != nullptr)
19459 {
19460 const char *base;
19461
19462 /* Strip any leading namespaces/classes, keep only the base name.
19463 DW_AT_name for named DIEs does not contain the prefixes. */
19464 base = strrchr (demangled.get (), ':');
19465 if (base && base > demangled.get () && base[-1] == ':')
19466 base++;
19467 else
19468 base = demangled.get ();
19469
19470 struct objfile *objfile = cu->per_objfile->objfile;
19471 raw_name = objfile->intern (base);
19472 canonical_name = 1;
19473 }
19474 }
19475
19476 fixup_called = 1;
19477 }
19478
19479 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
19480 contents from the given SECTION in the HEADER. */
19481 static void
19482 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
19483 struct dwarf2_section_info *section)
19484 {
19485 unsigned int bytes_read;
19486 bfd *abfd = section->get_bfd_owner ();
19487 const gdb_byte *info_ptr = section->buffer;
19488 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19489 info_ptr += bytes_read;
19490 header->version = read_2_bytes (abfd, info_ptr);
19491 info_ptr += 2;
19492 header->addr_size = read_1_byte (abfd, info_ptr);
19493 info_ptr += 1;
19494 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19495 info_ptr += 1;
19496 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19497 }
19498
19499 /* Return the DW_AT_loclists_base value for the CU. */
19500 static ULONGEST
19501 lookup_loclist_base (struct dwarf2_cu *cu)
19502 {
19503 /* For the .dwo unit, the loclist_base points to the first offset following
19504 the header. The header consists of the following entities-
19505 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19506 bit format)
19507 2. version (2 bytes)
19508 3. address size (1 byte)
19509 4. segment selector size (1 byte)
19510 5. offset entry count (4 bytes)
19511 These sizes are derived as per the DWARFv5 standard. */
19512 if (cu->dwo_unit != nullptr)
19513 {
19514 if (cu->header.initial_length_size == 4)
19515 return LOCLIST_HEADER_SIZE32;
19516 return LOCLIST_HEADER_SIZE64;
19517 }
19518 return cu->loclist_base;
19519 }
19520
19521 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19522 array of offsets in the .debug_loclists section. */
19523 static CORE_ADDR
19524 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19525 {
19526 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19527 struct objfile *objfile = per_objfile->objfile;
19528 bfd *abfd = objfile->obfd;
19529 ULONGEST loclist_base = lookup_loclist_base (cu);
19530 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19531
19532 section->read (objfile);
19533 if (section->buffer == NULL)
19534 complaint (_("DW_FORM_loclistx used without .debug_loclists "
19535 "section [in module %s]"), objfile_name (objfile));
19536 struct loclists_rnglists_header header;
19537 read_loclists_rnglists_header (&header, section);
19538 if (loclist_index >= header.offset_entry_count)
19539 complaint (_("DW_FORM_loclistx pointing outside of "
19540 ".debug_loclists offset array [in module %s]"),
19541 objfile_name (objfile));
19542 if (loclist_base + loclist_index * cu->header.offset_size
19543 >= section->size)
19544 complaint (_("DW_FORM_loclistx pointing outside of "
19545 ".debug_loclists section [in module %s]"),
19546 objfile_name (objfile));
19547 const gdb_byte *info_ptr
19548 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
19549
19550 if (cu->header.offset_size == 4)
19551 return bfd_get_32 (abfd, info_ptr) + loclist_base;
19552 else
19553 return bfd_get_64 (abfd, info_ptr) + loclist_base;
19554 }
19555
19556 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
19557 array of offsets in the .debug_rnglists section. */
19558 static CORE_ADDR
19559 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
19560 dwarf_tag tag)
19561 {
19562 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19563 struct objfile *objfile = dwarf2_per_objfile->objfile;
19564 bfd *abfd = objfile->obfd;
19565 ULONGEST rnglist_header_size =
19566 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
19567 : RNGLIST_HEADER_SIZE64);
19568 ULONGEST rnglist_base =
19569 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->ranges_base;
19570 ULONGEST start_offset =
19571 rnglist_base + rnglist_index * cu->header.offset_size;
19572
19573 /* Get rnglists section. */
19574 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
19575
19576 /* Read the rnglists section content. */
19577 section->read (objfile);
19578 if (section->buffer == nullptr)
19579 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
19580 "[in module %s]"),
19581 objfile_name (objfile));
19582
19583 /* Verify the rnglist index is valid. */
19584 struct loclists_rnglists_header header;
19585 read_loclists_rnglists_header (&header, section);
19586 if (rnglist_index >= header.offset_entry_count)
19587 error (_("DW_FORM_rnglistx index pointing outside of "
19588 ".debug_rnglists offset array [in module %s]"),
19589 objfile_name (objfile));
19590
19591 /* Validate that the offset is within the section's range. */
19592 if (start_offset >= section->size)
19593 error (_("DW_FORM_rnglistx pointing outside of "
19594 ".debug_rnglists section [in module %s]"),
19595 objfile_name (objfile));
19596
19597 /* Validate that reading won't go beyond the end of the section. */
19598 if (start_offset + cu->header.offset_size > rnglist_base + section->size)
19599 error (_("Reading DW_FORM_rnglistx index beyond end of"
19600 ".debug_rnglists section [in module %s]"),
19601 objfile_name (objfile));
19602
19603 const gdb_byte *info_ptr = section->buffer + start_offset;
19604
19605 if (cu->header.offset_size == 4)
19606 return read_4_bytes (abfd, info_ptr) + rnglist_base;
19607 else
19608 return read_8_bytes (abfd, info_ptr) + rnglist_base;
19609 }
19610
19611 /* Process the attributes that had to be skipped in the first round. These
19612 attributes are the ones that need str_offsets_base or addr_base attributes.
19613 They could not have been processed in the first round, because at the time
19614 the values of str_offsets_base or addr_base may not have been known. */
19615 static void
19616 read_attribute_reprocess (const struct die_reader_specs *reader,
19617 struct attribute *attr, dwarf_tag tag)
19618 {
19619 struct dwarf2_cu *cu = reader->cu;
19620 switch (attr->form)
19621 {
19622 case DW_FORM_addrx:
19623 case DW_FORM_GNU_addr_index:
19624 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
19625 break;
19626 case DW_FORM_loclistx:
19627 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
19628 break;
19629 case DW_FORM_rnglistx:
19630 DW_UNSND (attr) = read_rnglist_index (cu, DW_UNSND (attr), tag);
19631 break;
19632 case DW_FORM_strx:
19633 case DW_FORM_strx1:
19634 case DW_FORM_strx2:
19635 case DW_FORM_strx3:
19636 case DW_FORM_strx4:
19637 case DW_FORM_GNU_str_index:
19638 {
19639 unsigned int str_index = DW_UNSND (attr);
19640 if (reader->dwo_file != NULL)
19641 {
19642 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19643 DW_STRING_IS_CANONICAL (attr) = 0;
19644 }
19645 else
19646 {
19647 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19648 DW_STRING_IS_CANONICAL (attr) = 0;
19649 }
19650 break;
19651 }
19652 default:
19653 gdb_assert_not_reached (_("Unexpected DWARF form."));
19654 }
19655 }
19656
19657 /* Read an attribute value described by an attribute form. */
19658
19659 static const gdb_byte *
19660 read_attribute_value (const struct die_reader_specs *reader,
19661 struct attribute *attr, unsigned form,
19662 LONGEST implicit_const, const gdb_byte *info_ptr,
19663 bool *need_reprocess)
19664 {
19665 struct dwarf2_cu *cu = reader->cu;
19666 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19667 struct objfile *objfile = per_objfile->objfile;
19668 bfd *abfd = reader->abfd;
19669 struct comp_unit_head *cu_header = &cu->header;
19670 unsigned int bytes_read;
19671 struct dwarf_block *blk;
19672 *need_reprocess = false;
19673
19674 attr->form = (enum dwarf_form) form;
19675 switch (form)
19676 {
19677 case DW_FORM_ref_addr:
19678 if (cu->header.version == 2)
19679 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19680 &bytes_read);
19681 else
19682 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19683 &bytes_read);
19684 info_ptr += bytes_read;
19685 break;
19686 case DW_FORM_GNU_ref_alt:
19687 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19688 info_ptr += bytes_read;
19689 break;
19690 case DW_FORM_addr:
19691 {
19692 struct gdbarch *gdbarch = objfile->arch ();
19693 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19694 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19695 info_ptr += bytes_read;
19696 }
19697 break;
19698 case DW_FORM_block2:
19699 blk = dwarf_alloc_block (cu);
19700 blk->size = read_2_bytes (abfd, info_ptr);
19701 info_ptr += 2;
19702 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19703 info_ptr += blk->size;
19704 DW_BLOCK (attr) = blk;
19705 break;
19706 case DW_FORM_block4:
19707 blk = dwarf_alloc_block (cu);
19708 blk->size = read_4_bytes (abfd, info_ptr);
19709 info_ptr += 4;
19710 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19711 info_ptr += blk->size;
19712 DW_BLOCK (attr) = blk;
19713 break;
19714 case DW_FORM_data2:
19715 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19716 info_ptr += 2;
19717 break;
19718 case DW_FORM_data4:
19719 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19720 info_ptr += 4;
19721 break;
19722 case DW_FORM_data8:
19723 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19724 info_ptr += 8;
19725 break;
19726 case DW_FORM_data16:
19727 blk = dwarf_alloc_block (cu);
19728 blk->size = 16;
19729 blk->data = read_n_bytes (abfd, info_ptr, 16);
19730 info_ptr += 16;
19731 DW_BLOCK (attr) = blk;
19732 break;
19733 case DW_FORM_sec_offset:
19734 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19735 info_ptr += bytes_read;
19736 break;
19737 case DW_FORM_loclistx:
19738 {
19739 *need_reprocess = true;
19740 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19741 info_ptr += bytes_read;
19742 }
19743 break;
19744 case DW_FORM_string:
19745 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19746 DW_STRING_IS_CANONICAL (attr) = 0;
19747 info_ptr += bytes_read;
19748 break;
19749 case DW_FORM_strp:
19750 if (!cu->per_cu->is_dwz)
19751 {
19752 DW_STRING (attr) = read_indirect_string (per_objfile,
19753 abfd, info_ptr, cu_header,
19754 &bytes_read);
19755 DW_STRING_IS_CANONICAL (attr) = 0;
19756 info_ptr += bytes_read;
19757 break;
19758 }
19759 /* FALLTHROUGH */
19760 case DW_FORM_line_strp:
19761 if (!cu->per_cu->is_dwz)
19762 {
19763 DW_STRING (attr) = per_objfile->read_line_string (info_ptr, cu_header,
19764 &bytes_read);
19765 DW_STRING_IS_CANONICAL (attr) = 0;
19766 info_ptr += bytes_read;
19767 break;
19768 }
19769 /* FALLTHROUGH */
19770 case DW_FORM_GNU_strp_alt:
19771 {
19772 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
19773 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19774 &bytes_read);
19775
19776 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
19777 DW_STRING_IS_CANONICAL (attr) = 0;
19778 info_ptr += bytes_read;
19779 }
19780 break;
19781 case DW_FORM_exprloc:
19782 case DW_FORM_block:
19783 blk = dwarf_alloc_block (cu);
19784 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19785 info_ptr += bytes_read;
19786 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19787 info_ptr += blk->size;
19788 DW_BLOCK (attr) = blk;
19789 break;
19790 case DW_FORM_block1:
19791 blk = dwarf_alloc_block (cu);
19792 blk->size = read_1_byte (abfd, info_ptr);
19793 info_ptr += 1;
19794 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19795 info_ptr += blk->size;
19796 DW_BLOCK (attr) = blk;
19797 break;
19798 case DW_FORM_data1:
19799 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19800 info_ptr += 1;
19801 break;
19802 case DW_FORM_flag:
19803 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19804 info_ptr += 1;
19805 break;
19806 case DW_FORM_flag_present:
19807 DW_UNSND (attr) = 1;
19808 break;
19809 case DW_FORM_sdata:
19810 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19811 info_ptr += bytes_read;
19812 break;
19813 case DW_FORM_rnglistx:
19814 *need_reprocess = true;
19815 /* FALLTHROUGH */
19816 case DW_FORM_udata:
19817 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19818 info_ptr += bytes_read;
19819 break;
19820 case DW_FORM_ref1:
19821 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19822 + read_1_byte (abfd, info_ptr));
19823 info_ptr += 1;
19824 break;
19825 case DW_FORM_ref2:
19826 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19827 + read_2_bytes (abfd, info_ptr));
19828 info_ptr += 2;
19829 break;
19830 case DW_FORM_ref4:
19831 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19832 + read_4_bytes (abfd, info_ptr));
19833 info_ptr += 4;
19834 break;
19835 case DW_FORM_ref8:
19836 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19837 + read_8_bytes (abfd, info_ptr));
19838 info_ptr += 8;
19839 break;
19840 case DW_FORM_ref_sig8:
19841 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19842 info_ptr += 8;
19843 break;
19844 case DW_FORM_ref_udata:
19845 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19846 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19847 info_ptr += bytes_read;
19848 break;
19849 case DW_FORM_indirect:
19850 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19851 info_ptr += bytes_read;
19852 if (form == DW_FORM_implicit_const)
19853 {
19854 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19855 info_ptr += bytes_read;
19856 }
19857 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19858 info_ptr, need_reprocess);
19859 break;
19860 case DW_FORM_implicit_const:
19861 DW_SND (attr) = implicit_const;
19862 break;
19863 case DW_FORM_addrx:
19864 case DW_FORM_GNU_addr_index:
19865 *need_reprocess = true;
19866 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19867 info_ptr += bytes_read;
19868 break;
19869 case DW_FORM_strx:
19870 case DW_FORM_strx1:
19871 case DW_FORM_strx2:
19872 case DW_FORM_strx3:
19873 case DW_FORM_strx4:
19874 case DW_FORM_GNU_str_index:
19875 {
19876 ULONGEST str_index;
19877 if (form == DW_FORM_strx1)
19878 {
19879 str_index = read_1_byte (abfd, info_ptr);
19880 info_ptr += 1;
19881 }
19882 else if (form == DW_FORM_strx2)
19883 {
19884 str_index = read_2_bytes (abfd, info_ptr);
19885 info_ptr += 2;
19886 }
19887 else if (form == DW_FORM_strx3)
19888 {
19889 str_index = read_3_bytes (abfd, info_ptr);
19890 info_ptr += 3;
19891 }
19892 else if (form == DW_FORM_strx4)
19893 {
19894 str_index = read_4_bytes (abfd, info_ptr);
19895 info_ptr += 4;
19896 }
19897 else
19898 {
19899 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19900 info_ptr += bytes_read;
19901 }
19902 *need_reprocess = true;
19903 DW_UNSND (attr) = str_index;
19904 }
19905 break;
19906 default:
19907 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19908 dwarf_form_name (form),
19909 bfd_get_filename (abfd));
19910 }
19911
19912 /* Super hack. */
19913 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19914 attr->form = DW_FORM_GNU_ref_alt;
19915
19916 /* We have seen instances where the compiler tried to emit a byte
19917 size attribute of -1 which ended up being encoded as an unsigned
19918 0xffffffff. Although 0xffffffff is technically a valid size value,
19919 an object of this size seems pretty unlikely so we can relatively
19920 safely treat these cases as if the size attribute was invalid and
19921 treat them as zero by default. */
19922 if (attr->name == DW_AT_byte_size
19923 && form == DW_FORM_data4
19924 && DW_UNSND (attr) >= 0xffffffff)
19925 {
19926 complaint
19927 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19928 hex_string (DW_UNSND (attr)));
19929 DW_UNSND (attr) = 0;
19930 }
19931
19932 return info_ptr;
19933 }
19934
19935 /* Read an attribute described by an abbreviated attribute. */
19936
19937 static const gdb_byte *
19938 read_attribute (const struct die_reader_specs *reader,
19939 struct attribute *attr, struct attr_abbrev *abbrev,
19940 const gdb_byte *info_ptr, bool *need_reprocess)
19941 {
19942 attr->name = abbrev->name;
19943 return read_attribute_value (reader, attr, abbrev->form,
19944 abbrev->implicit_const, info_ptr,
19945 need_reprocess);
19946 }
19947
19948 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19949
19950 static const char *
19951 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
19952 LONGEST str_offset)
19953 {
19954 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
19955 str_offset, "DW_FORM_strp");
19956 }
19957
19958 /* Return pointer to string at .debug_str offset as read from BUF.
19959 BUF is assumed to be in a compilation unit described by CU_HEADER.
19960 Return *BYTES_READ_PTR count of bytes read from BUF. */
19961
19962 static const char *
19963 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
19964 const gdb_byte *buf,
19965 const struct comp_unit_head *cu_header,
19966 unsigned int *bytes_read_ptr)
19967 {
19968 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19969
19970 return read_indirect_string_at_offset (per_objfile, str_offset);
19971 }
19972
19973 /* See read.h. */
19974
19975 const char *
19976 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19977 const struct comp_unit_head *cu_header,
19978 unsigned int *bytes_read_ptr)
19979 {
19980 bfd *abfd = objfile->obfd;
19981 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19982
19983 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19984 }
19985
19986 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19987 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19988 ADDR_SIZE is the size of addresses from the CU header. */
19989
19990 static CORE_ADDR
19991 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
19992 gdb::optional<ULONGEST> addr_base, int addr_size)
19993 {
19994 struct objfile *objfile = per_objfile->objfile;
19995 bfd *abfd = objfile->obfd;
19996 const gdb_byte *info_ptr;
19997 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19998
19999 per_objfile->per_bfd->addr.read (objfile);
20000 if (per_objfile->per_bfd->addr.buffer == NULL)
20001 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20002 objfile_name (objfile));
20003 if (addr_base_or_zero + addr_index * addr_size
20004 >= per_objfile->per_bfd->addr.size)
20005 error (_("DW_FORM_addr_index pointing outside of "
20006 ".debug_addr section [in module %s]"),
20007 objfile_name (objfile));
20008 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20009 + addr_index * addr_size);
20010 if (addr_size == 4)
20011 return bfd_get_32 (abfd, info_ptr);
20012 else
20013 return bfd_get_64 (abfd, info_ptr);
20014 }
20015
20016 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20017
20018 static CORE_ADDR
20019 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20020 {
20021 return read_addr_index_1 (cu->per_objfile, addr_index,
20022 cu->addr_base, cu->header.addr_size);
20023 }
20024
20025 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20026
20027 static CORE_ADDR
20028 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20029 unsigned int *bytes_read)
20030 {
20031 bfd *abfd = cu->per_objfile->objfile->obfd;
20032 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20033
20034 return read_addr_index (cu, addr_index);
20035 }
20036
20037 /* See read.h. */
20038
20039 CORE_ADDR
20040 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20041 dwarf2_per_objfile *per_objfile,
20042 unsigned int addr_index)
20043 {
20044 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20045 gdb::optional<ULONGEST> addr_base;
20046 int addr_size;
20047
20048 /* We need addr_base and addr_size.
20049 If we don't have PER_CU->cu, we have to get it.
20050 Nasty, but the alternative is storing the needed info in PER_CU,
20051 which at this point doesn't seem justified: it's not clear how frequently
20052 it would get used and it would increase the size of every PER_CU.
20053 Entry points like dwarf2_per_cu_addr_size do a similar thing
20054 so we're not in uncharted territory here.
20055 Alas we need to be a bit more complicated as addr_base is contained
20056 in the DIE.
20057
20058 We don't need to read the entire CU(/TU).
20059 We just need the header and top level die.
20060
20061 IWBN to use the aging mechanism to let us lazily later discard the CU.
20062 For now we skip this optimization. */
20063
20064 if (cu != NULL)
20065 {
20066 addr_base = cu->addr_base;
20067 addr_size = cu->header.addr_size;
20068 }
20069 else
20070 {
20071 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
20072 addr_base = reader.cu->addr_base;
20073 addr_size = reader.cu->header.addr_size;
20074 }
20075
20076 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
20077 }
20078
20079 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20080 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20081 DWO file. */
20082
20083 static const char *
20084 read_str_index (struct dwarf2_cu *cu,
20085 struct dwarf2_section_info *str_section,
20086 struct dwarf2_section_info *str_offsets_section,
20087 ULONGEST str_offsets_base, ULONGEST str_index)
20088 {
20089 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20090 struct objfile *objfile = per_objfile->objfile;
20091 const char *objf_name = objfile_name (objfile);
20092 bfd *abfd = objfile->obfd;
20093 const gdb_byte *info_ptr;
20094 ULONGEST str_offset;
20095 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20096
20097 str_section->read (objfile);
20098 str_offsets_section->read (objfile);
20099 if (str_section->buffer == NULL)
20100 error (_("%s used without %s section"
20101 " in CU at offset %s [in module %s]"),
20102 form_name, str_section->get_name (),
20103 sect_offset_str (cu->header.sect_off), objf_name);
20104 if (str_offsets_section->buffer == NULL)
20105 error (_("%s used without %s section"
20106 " in CU at offset %s [in module %s]"),
20107 form_name, str_section->get_name (),
20108 sect_offset_str (cu->header.sect_off), objf_name);
20109 info_ptr = (str_offsets_section->buffer
20110 + str_offsets_base
20111 + str_index * cu->header.offset_size);
20112 if (cu->header.offset_size == 4)
20113 str_offset = bfd_get_32 (abfd, info_ptr);
20114 else
20115 str_offset = bfd_get_64 (abfd, info_ptr);
20116 if (str_offset >= str_section->size)
20117 error (_("Offset from %s pointing outside of"
20118 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20119 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20120 return (const char *) (str_section->buffer + str_offset);
20121 }
20122
20123 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20124
20125 static const char *
20126 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20127 {
20128 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20129 ? reader->cu->header.addr_size : 0;
20130 return read_str_index (reader->cu,
20131 &reader->dwo_file->sections.str,
20132 &reader->dwo_file->sections.str_offsets,
20133 str_offsets_base, str_index);
20134 }
20135
20136 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20137
20138 static const char *
20139 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20140 {
20141 struct objfile *objfile = cu->per_objfile->objfile;
20142 const char *objf_name = objfile_name (objfile);
20143 static const char form_name[] = "DW_FORM_GNU_str_index";
20144 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20145
20146 if (!cu->str_offsets_base.has_value ())
20147 error (_("%s used in Fission stub without %s"
20148 " in CU at offset 0x%lx [in module %s]"),
20149 form_name, str_offsets_attr_name,
20150 (long) cu->header.offset_size, objf_name);
20151
20152 return read_str_index (cu,
20153 &cu->per_objfile->per_bfd->str,
20154 &cu->per_objfile->per_bfd->str_offsets,
20155 *cu->str_offsets_base, str_index);
20156 }
20157
20158 /* Return the length of an LEB128 number in BUF. */
20159
20160 static int
20161 leb128_size (const gdb_byte *buf)
20162 {
20163 const gdb_byte *begin = buf;
20164 gdb_byte byte;
20165
20166 while (1)
20167 {
20168 byte = *buf++;
20169 if ((byte & 128) == 0)
20170 return buf - begin;
20171 }
20172 }
20173
20174 static void
20175 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20176 {
20177 switch (lang)
20178 {
20179 case DW_LANG_C89:
20180 case DW_LANG_C99:
20181 case DW_LANG_C11:
20182 case DW_LANG_C:
20183 case DW_LANG_UPC:
20184 cu->language = language_c;
20185 break;
20186 case DW_LANG_Java:
20187 case DW_LANG_C_plus_plus:
20188 case DW_LANG_C_plus_plus_11:
20189 case DW_LANG_C_plus_plus_14:
20190 cu->language = language_cplus;
20191 break;
20192 case DW_LANG_D:
20193 cu->language = language_d;
20194 break;
20195 case DW_LANG_Fortran77:
20196 case DW_LANG_Fortran90:
20197 case DW_LANG_Fortran95:
20198 case DW_LANG_Fortran03:
20199 case DW_LANG_Fortran08:
20200 cu->language = language_fortran;
20201 break;
20202 case DW_LANG_Go:
20203 cu->language = language_go;
20204 break;
20205 case DW_LANG_Mips_Assembler:
20206 cu->language = language_asm;
20207 break;
20208 case DW_LANG_Ada83:
20209 case DW_LANG_Ada95:
20210 cu->language = language_ada;
20211 break;
20212 case DW_LANG_Modula2:
20213 cu->language = language_m2;
20214 break;
20215 case DW_LANG_Pascal83:
20216 cu->language = language_pascal;
20217 break;
20218 case DW_LANG_ObjC:
20219 cu->language = language_objc;
20220 break;
20221 case DW_LANG_Rust:
20222 case DW_LANG_Rust_old:
20223 cu->language = language_rust;
20224 break;
20225 case DW_LANG_Cobol74:
20226 case DW_LANG_Cobol85:
20227 default:
20228 cu->language = language_minimal;
20229 break;
20230 }
20231 cu->language_defn = language_def (cu->language);
20232 }
20233
20234 /* Return the named attribute or NULL if not there. */
20235
20236 static struct attribute *
20237 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20238 {
20239 for (;;)
20240 {
20241 unsigned int i;
20242 struct attribute *spec = NULL;
20243
20244 for (i = 0; i < die->num_attrs; ++i)
20245 {
20246 if (die->attrs[i].name == name)
20247 return &die->attrs[i];
20248 if (die->attrs[i].name == DW_AT_specification
20249 || die->attrs[i].name == DW_AT_abstract_origin)
20250 spec = &die->attrs[i];
20251 }
20252
20253 if (!spec)
20254 break;
20255
20256 die = follow_die_ref (die, spec, &cu);
20257 }
20258
20259 return NULL;
20260 }
20261
20262 /* Return the string associated with a string-typed attribute, or NULL if it
20263 is either not found or is of an incorrect type. */
20264
20265 static const char *
20266 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20267 {
20268 struct attribute *attr;
20269 const char *str = NULL;
20270
20271 attr = dwarf2_attr (die, name, cu);
20272
20273 if (attr != NULL)
20274 {
20275 str = attr->value_as_string ();
20276 if (str == nullptr)
20277 complaint (_("string type expected for attribute %s for "
20278 "DIE at %s in module %s"),
20279 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20280 objfile_name (cu->per_objfile->objfile));
20281 }
20282
20283 return str;
20284 }
20285
20286 /* Return the dwo name or NULL if not present. If present, it is in either
20287 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20288 static const char *
20289 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20290 {
20291 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20292 if (dwo_name == nullptr)
20293 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20294 return dwo_name;
20295 }
20296
20297 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20298 and holds a non-zero value. This function should only be used for
20299 DW_FORM_flag or DW_FORM_flag_present attributes. */
20300
20301 static int
20302 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20303 {
20304 struct attribute *attr = dwarf2_attr (die, name, cu);
20305
20306 return (attr && DW_UNSND (attr));
20307 }
20308
20309 static int
20310 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20311 {
20312 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20313 which value is non-zero. However, we have to be careful with
20314 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20315 (via dwarf2_flag_true_p) follows this attribute. So we may
20316 end up accidently finding a declaration attribute that belongs
20317 to a different DIE referenced by the specification attribute,
20318 even though the given DIE does not have a declaration attribute. */
20319 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20320 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20321 }
20322
20323 /* Return the die giving the specification for DIE, if there is
20324 one. *SPEC_CU is the CU containing DIE on input, and the CU
20325 containing the return value on output. If there is no
20326 specification, but there is an abstract origin, that is
20327 returned. */
20328
20329 static struct die_info *
20330 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20331 {
20332 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20333 *spec_cu);
20334
20335 if (spec_attr == NULL)
20336 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20337
20338 if (spec_attr == NULL)
20339 return NULL;
20340 else
20341 return follow_die_ref (die, spec_attr, spec_cu);
20342 }
20343
20344 /* Stub for free_line_header to match void * callback types. */
20345
20346 static void
20347 free_line_header_voidp (void *arg)
20348 {
20349 struct line_header *lh = (struct line_header *) arg;
20350
20351 delete lh;
20352 }
20353
20354 /* A convenience function to find the proper .debug_line section for a CU. */
20355
20356 static struct dwarf2_section_info *
20357 get_debug_line_section (struct dwarf2_cu *cu)
20358 {
20359 struct dwarf2_section_info *section;
20360 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20361
20362 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20363 DWO file. */
20364 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20365 section = &cu->dwo_unit->dwo_file->sections.line;
20366 else if (cu->per_cu->is_dwz)
20367 {
20368 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
20369
20370 section = &dwz->line;
20371 }
20372 else
20373 section = &per_objfile->per_bfd->line;
20374
20375 return section;
20376 }
20377
20378 /* Read the statement program header starting at OFFSET in
20379 .debug_line, or .debug_line.dwo. Return a pointer
20380 to a struct line_header, allocated using xmalloc.
20381 Returns NULL if there is a problem reading the header, e.g., if it
20382 has a version we don't understand.
20383
20384 NOTE: the strings in the include directory and file name tables of
20385 the returned object point into the dwarf line section buffer,
20386 and must not be freed. */
20387
20388 static line_header_up
20389 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20390 {
20391 struct dwarf2_section_info *section;
20392 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20393
20394 section = get_debug_line_section (cu);
20395 section->read (per_objfile->objfile);
20396 if (section->buffer == NULL)
20397 {
20398 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20399 complaint (_("missing .debug_line.dwo section"));
20400 else
20401 complaint (_("missing .debug_line section"));
20402 return 0;
20403 }
20404
20405 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
20406 per_objfile, section, &cu->header);
20407 }
20408
20409 /* Subroutine of dwarf_decode_lines to simplify it.
20410 Return the file name of the psymtab for the given file_entry.
20411 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20412 If space for the result is malloc'd, *NAME_HOLDER will be set.
20413 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20414
20415 static const char *
20416 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
20417 const dwarf2_psymtab *pst,
20418 const char *comp_dir,
20419 gdb::unique_xmalloc_ptr<char> *name_holder)
20420 {
20421 const char *include_name = fe.name;
20422 const char *include_name_to_compare = include_name;
20423 const char *pst_filename;
20424 int file_is_pst;
20425
20426 const char *dir_name = fe.include_dir (lh);
20427
20428 gdb::unique_xmalloc_ptr<char> hold_compare;
20429 if (!IS_ABSOLUTE_PATH (include_name)
20430 && (dir_name != NULL || comp_dir != NULL))
20431 {
20432 /* Avoid creating a duplicate psymtab for PST.
20433 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20434 Before we do the comparison, however, we need to account
20435 for DIR_NAME and COMP_DIR.
20436 First prepend dir_name (if non-NULL). If we still don't
20437 have an absolute path prepend comp_dir (if non-NULL).
20438 However, the directory we record in the include-file's
20439 psymtab does not contain COMP_DIR (to match the
20440 corresponding symtab(s)).
20441
20442 Example:
20443
20444 bash$ cd /tmp
20445 bash$ gcc -g ./hello.c
20446 include_name = "hello.c"
20447 dir_name = "."
20448 DW_AT_comp_dir = comp_dir = "/tmp"
20449 DW_AT_name = "./hello.c"
20450
20451 */
20452
20453 if (dir_name != NULL)
20454 {
20455 name_holder->reset (concat (dir_name, SLASH_STRING,
20456 include_name, (char *) NULL));
20457 include_name = name_holder->get ();
20458 include_name_to_compare = include_name;
20459 }
20460 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20461 {
20462 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20463 include_name, (char *) NULL));
20464 include_name_to_compare = hold_compare.get ();
20465 }
20466 }
20467
20468 pst_filename = pst->filename;
20469 gdb::unique_xmalloc_ptr<char> copied_name;
20470 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20471 {
20472 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20473 pst_filename, (char *) NULL));
20474 pst_filename = copied_name.get ();
20475 }
20476
20477 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20478
20479 if (file_is_pst)
20480 return NULL;
20481 return include_name;
20482 }
20483
20484 /* State machine to track the state of the line number program. */
20485
20486 class lnp_state_machine
20487 {
20488 public:
20489 /* Initialize a machine state for the start of a line number
20490 program. */
20491 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20492 bool record_lines_p);
20493
20494 file_entry *current_file ()
20495 {
20496 /* lh->file_names is 0-based, but the file name numbers in the
20497 statement program are 1-based. */
20498 return m_line_header->file_name_at (m_file);
20499 }
20500
20501 /* Record the line in the state machine. END_SEQUENCE is true if
20502 we're processing the end of a sequence. */
20503 void record_line (bool end_sequence);
20504
20505 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
20506 nop-out rest of the lines in this sequence. */
20507 void check_line_address (struct dwarf2_cu *cu,
20508 const gdb_byte *line_ptr,
20509 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20510
20511 void handle_set_discriminator (unsigned int discriminator)
20512 {
20513 m_discriminator = discriminator;
20514 m_line_has_non_zero_discriminator |= discriminator != 0;
20515 }
20516
20517 /* Handle DW_LNE_set_address. */
20518 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20519 {
20520 m_op_index = 0;
20521 address += baseaddr;
20522 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20523 }
20524
20525 /* Handle DW_LNS_advance_pc. */
20526 void handle_advance_pc (CORE_ADDR adjust);
20527
20528 /* Handle a special opcode. */
20529 void handle_special_opcode (unsigned char op_code);
20530
20531 /* Handle DW_LNS_advance_line. */
20532 void handle_advance_line (int line_delta)
20533 {
20534 advance_line (line_delta);
20535 }
20536
20537 /* Handle DW_LNS_set_file. */
20538 void handle_set_file (file_name_index file);
20539
20540 /* Handle DW_LNS_negate_stmt. */
20541 void handle_negate_stmt ()
20542 {
20543 m_is_stmt = !m_is_stmt;
20544 }
20545
20546 /* Handle DW_LNS_const_add_pc. */
20547 void handle_const_add_pc ();
20548
20549 /* Handle DW_LNS_fixed_advance_pc. */
20550 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20551 {
20552 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20553 m_op_index = 0;
20554 }
20555
20556 /* Handle DW_LNS_copy. */
20557 void handle_copy ()
20558 {
20559 record_line (false);
20560 m_discriminator = 0;
20561 }
20562
20563 /* Handle DW_LNE_end_sequence. */
20564 void handle_end_sequence ()
20565 {
20566 m_currently_recording_lines = true;
20567 }
20568
20569 private:
20570 /* Advance the line by LINE_DELTA. */
20571 void advance_line (int line_delta)
20572 {
20573 m_line += line_delta;
20574
20575 if (line_delta != 0)
20576 m_line_has_non_zero_discriminator = m_discriminator != 0;
20577 }
20578
20579 struct dwarf2_cu *m_cu;
20580
20581 gdbarch *m_gdbarch;
20582
20583 /* True if we're recording lines.
20584 Otherwise we're building partial symtabs and are just interested in
20585 finding include files mentioned by the line number program. */
20586 bool m_record_lines_p;
20587
20588 /* The line number header. */
20589 line_header *m_line_header;
20590
20591 /* These are part of the standard DWARF line number state machine,
20592 and initialized according to the DWARF spec. */
20593
20594 unsigned char m_op_index = 0;
20595 /* The line table index of the current file. */
20596 file_name_index m_file = 1;
20597 unsigned int m_line = 1;
20598
20599 /* These are initialized in the constructor. */
20600
20601 CORE_ADDR m_address;
20602 bool m_is_stmt;
20603 unsigned int m_discriminator;
20604
20605 /* Additional bits of state we need to track. */
20606
20607 /* The last file that we called dwarf2_start_subfile for.
20608 This is only used for TLLs. */
20609 unsigned int m_last_file = 0;
20610 /* The last file a line number was recorded for. */
20611 struct subfile *m_last_subfile = NULL;
20612
20613 /* The address of the last line entry. */
20614 CORE_ADDR m_last_address;
20615
20616 /* Set to true when a previous line at the same address (using
20617 m_last_address) had m_is_stmt true. This is reset to false when a
20618 line entry at a new address (m_address different to m_last_address) is
20619 processed. */
20620 bool m_stmt_at_address = false;
20621
20622 /* When true, record the lines we decode. */
20623 bool m_currently_recording_lines = false;
20624
20625 /* The last line number that was recorded, used to coalesce
20626 consecutive entries for the same line. This can happen, for
20627 example, when discriminators are present. PR 17276. */
20628 unsigned int m_last_line = 0;
20629 bool m_line_has_non_zero_discriminator = false;
20630 };
20631
20632 void
20633 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20634 {
20635 CORE_ADDR addr_adj = (((m_op_index + adjust)
20636 / m_line_header->maximum_ops_per_instruction)
20637 * m_line_header->minimum_instruction_length);
20638 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20639 m_op_index = ((m_op_index + adjust)
20640 % m_line_header->maximum_ops_per_instruction);
20641 }
20642
20643 void
20644 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20645 {
20646 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20647 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20648 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20649 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
20650 / m_line_header->maximum_ops_per_instruction)
20651 * m_line_header->minimum_instruction_length);
20652 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20653 m_op_index = ((m_op_index + adj_opcode_d)
20654 % m_line_header->maximum_ops_per_instruction);
20655
20656 int line_delta = m_line_header->line_base + adj_opcode_r;
20657 advance_line (line_delta);
20658 record_line (false);
20659 m_discriminator = 0;
20660 }
20661
20662 void
20663 lnp_state_machine::handle_set_file (file_name_index file)
20664 {
20665 m_file = file;
20666
20667 const file_entry *fe = current_file ();
20668 if (fe == NULL)
20669 dwarf2_debug_line_missing_file_complaint ();
20670 else if (m_record_lines_p)
20671 {
20672 const char *dir = fe->include_dir (m_line_header);
20673
20674 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20675 m_line_has_non_zero_discriminator = m_discriminator != 0;
20676 dwarf2_start_subfile (m_cu, fe->name, dir);
20677 }
20678 }
20679
20680 void
20681 lnp_state_machine::handle_const_add_pc ()
20682 {
20683 CORE_ADDR adjust
20684 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20685
20686 CORE_ADDR addr_adj
20687 = (((m_op_index + adjust)
20688 / m_line_header->maximum_ops_per_instruction)
20689 * m_line_header->minimum_instruction_length);
20690
20691 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20692 m_op_index = ((m_op_index + adjust)
20693 % m_line_header->maximum_ops_per_instruction);
20694 }
20695
20696 /* Return non-zero if we should add LINE to the line number table.
20697 LINE is the line to add, LAST_LINE is the last line that was added,
20698 LAST_SUBFILE is the subfile for LAST_LINE.
20699 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20700 had a non-zero discriminator.
20701
20702 We have to be careful in the presence of discriminators.
20703 E.g., for this line:
20704
20705 for (i = 0; i < 100000; i++);
20706
20707 clang can emit four line number entries for that one line,
20708 each with a different discriminator.
20709 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20710
20711 However, we want gdb to coalesce all four entries into one.
20712 Otherwise the user could stepi into the middle of the line and
20713 gdb would get confused about whether the pc really was in the
20714 middle of the line.
20715
20716 Things are further complicated by the fact that two consecutive
20717 line number entries for the same line is a heuristic used by gcc
20718 to denote the end of the prologue. So we can't just discard duplicate
20719 entries, we have to be selective about it. The heuristic we use is
20720 that we only collapse consecutive entries for the same line if at least
20721 one of those entries has a non-zero discriminator. PR 17276.
20722
20723 Note: Addresses in the line number state machine can never go backwards
20724 within one sequence, thus this coalescing is ok. */
20725
20726 static int
20727 dwarf_record_line_p (struct dwarf2_cu *cu,
20728 unsigned int line, unsigned int last_line,
20729 int line_has_non_zero_discriminator,
20730 struct subfile *last_subfile)
20731 {
20732 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20733 return 1;
20734 if (line != last_line)
20735 return 1;
20736 /* Same line for the same file that we've seen already.
20737 As a last check, for pr 17276, only record the line if the line
20738 has never had a non-zero discriminator. */
20739 if (!line_has_non_zero_discriminator)
20740 return 1;
20741 return 0;
20742 }
20743
20744 /* Use the CU's builder to record line number LINE beginning at
20745 address ADDRESS in the line table of subfile SUBFILE. */
20746
20747 static void
20748 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20749 unsigned int line, CORE_ADDR address, bool is_stmt,
20750 struct dwarf2_cu *cu)
20751 {
20752 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20753
20754 if (dwarf_line_debug)
20755 {
20756 fprintf_unfiltered (gdb_stdlog,
20757 "Recording line %u, file %s, address %s\n",
20758 line, lbasename (subfile->name),
20759 paddress (gdbarch, address));
20760 }
20761
20762 if (cu != nullptr)
20763 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
20764 }
20765
20766 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20767 Mark the end of a set of line number records.
20768 The arguments are the same as for dwarf_record_line_1.
20769 If SUBFILE is NULL the request is ignored. */
20770
20771 static void
20772 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20773 CORE_ADDR address, struct dwarf2_cu *cu)
20774 {
20775 if (subfile == NULL)
20776 return;
20777
20778 if (dwarf_line_debug)
20779 {
20780 fprintf_unfiltered (gdb_stdlog,
20781 "Finishing current line, file %s, address %s\n",
20782 lbasename (subfile->name),
20783 paddress (gdbarch, address));
20784 }
20785
20786 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
20787 }
20788
20789 void
20790 lnp_state_machine::record_line (bool end_sequence)
20791 {
20792 if (dwarf_line_debug)
20793 {
20794 fprintf_unfiltered (gdb_stdlog,
20795 "Processing actual line %u: file %u,"
20796 " address %s, is_stmt %u, discrim %u%s\n",
20797 m_line, m_file,
20798 paddress (m_gdbarch, m_address),
20799 m_is_stmt, m_discriminator,
20800 (end_sequence ? "\t(end sequence)" : ""));
20801 }
20802
20803 file_entry *fe = current_file ();
20804
20805 if (fe == NULL)
20806 dwarf2_debug_line_missing_file_complaint ();
20807 /* For now we ignore lines not starting on an instruction boundary.
20808 But not when processing end_sequence for compatibility with the
20809 previous version of the code. */
20810 else if (m_op_index == 0 || end_sequence)
20811 {
20812 fe->included_p = 1;
20813 if (m_record_lines_p)
20814 {
20815 /* When we switch files we insert an end maker in the first file,
20816 switch to the second file and add a new line entry. The
20817 problem is that the end marker inserted in the first file will
20818 discard any previous line entries at the same address. If the
20819 line entries in the first file are marked as is-stmt, while
20820 the new line in the second file is non-stmt, then this means
20821 the end marker will discard is-stmt lines so we can have a
20822 non-stmt line. This means that there are less addresses at
20823 which the user can insert a breakpoint.
20824
20825 To improve this we track the last address in m_last_address,
20826 and whether we have seen an is-stmt at this address. Then
20827 when switching files, if we have seen a stmt at the current
20828 address, and we are switching to create a non-stmt line, then
20829 discard the new line. */
20830 bool file_changed
20831 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
20832 bool ignore_this_line
20833 = ((file_changed && !end_sequence && m_last_address == m_address
20834 && !m_is_stmt && m_stmt_at_address)
20835 || (!end_sequence && m_line == 0));
20836
20837 if ((file_changed && !ignore_this_line) || end_sequence)
20838 {
20839 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20840 m_currently_recording_lines ? m_cu : nullptr);
20841 }
20842
20843 if (!end_sequence && !ignore_this_line)
20844 {
20845 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20846
20847 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20848 m_line_has_non_zero_discriminator,
20849 m_last_subfile))
20850 {
20851 buildsym_compunit *builder = m_cu->get_builder ();
20852 dwarf_record_line_1 (m_gdbarch,
20853 builder->get_current_subfile (),
20854 m_line, m_address, is_stmt,
20855 m_currently_recording_lines ? m_cu : nullptr);
20856 }
20857 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20858 m_last_line = m_line;
20859 }
20860 }
20861 }
20862
20863 /* Track whether we have seen any m_is_stmt true at m_address in case we
20864 have multiple line table entries all at m_address. */
20865 if (m_last_address != m_address)
20866 {
20867 m_stmt_at_address = false;
20868 m_last_address = m_address;
20869 }
20870 m_stmt_at_address |= m_is_stmt;
20871 }
20872
20873 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20874 line_header *lh, bool record_lines_p)
20875 {
20876 m_cu = cu;
20877 m_gdbarch = arch;
20878 m_record_lines_p = record_lines_p;
20879 m_line_header = lh;
20880
20881 m_currently_recording_lines = true;
20882
20883 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20884 was a line entry for it so that the backend has a chance to adjust it
20885 and also record it in case it needs it. This is currently used by MIPS
20886 code, cf. `mips_adjust_dwarf2_line'. */
20887 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20888 m_is_stmt = lh->default_is_stmt;
20889 m_discriminator = 0;
20890
20891 m_last_address = m_address;
20892 m_stmt_at_address = false;
20893 }
20894
20895 void
20896 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20897 const gdb_byte *line_ptr,
20898 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20899 {
20900 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
20901 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
20902 located at 0x0. In this case, additionally check that if
20903 ADDRESS < UNRELOCATED_LOWPC. */
20904
20905 if ((address == 0 && address < unrelocated_lowpc)
20906 || address == (CORE_ADDR) -1)
20907 {
20908 /* This line table is for a function which has been
20909 GCd by the linker. Ignore it. PR gdb/12528 */
20910
20911 struct objfile *objfile = cu->per_objfile->objfile;
20912 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20913
20914 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20915 line_offset, objfile_name (objfile));
20916 m_currently_recording_lines = false;
20917 /* Note: m_currently_recording_lines is left as false until we see
20918 DW_LNE_end_sequence. */
20919 }
20920 }
20921
20922 /* Subroutine of dwarf_decode_lines to simplify it.
20923 Process the line number information in LH.
20924 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20925 program in order to set included_p for every referenced header. */
20926
20927 static void
20928 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20929 const int decode_for_pst_p, CORE_ADDR lowpc)
20930 {
20931 const gdb_byte *line_ptr, *extended_end;
20932 const gdb_byte *line_end;
20933 unsigned int bytes_read, extended_len;
20934 unsigned char op_code, extended_op;
20935 CORE_ADDR baseaddr;
20936 struct objfile *objfile = cu->per_objfile->objfile;
20937 bfd *abfd = objfile->obfd;
20938 struct gdbarch *gdbarch = objfile->arch ();
20939 /* True if we're recording line info (as opposed to building partial
20940 symtabs and just interested in finding include files mentioned by
20941 the line number program). */
20942 bool record_lines_p = !decode_for_pst_p;
20943
20944 baseaddr = objfile->text_section_offset ();
20945
20946 line_ptr = lh->statement_program_start;
20947 line_end = lh->statement_program_end;
20948
20949 /* Read the statement sequences until there's nothing left. */
20950 while (line_ptr < line_end)
20951 {
20952 /* The DWARF line number program state machine. Reset the state
20953 machine at the start of each sequence. */
20954 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20955 bool end_sequence = false;
20956
20957 if (record_lines_p)
20958 {
20959 /* Start a subfile for the current file of the state
20960 machine. */
20961 const file_entry *fe = state_machine.current_file ();
20962
20963 if (fe != NULL)
20964 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20965 }
20966
20967 /* Decode the table. */
20968 while (line_ptr < line_end && !end_sequence)
20969 {
20970 op_code = read_1_byte (abfd, line_ptr);
20971 line_ptr += 1;
20972
20973 if (op_code >= lh->opcode_base)
20974 {
20975 /* Special opcode. */
20976 state_machine.handle_special_opcode (op_code);
20977 }
20978 else switch (op_code)
20979 {
20980 case DW_LNS_extended_op:
20981 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20982 &bytes_read);
20983 line_ptr += bytes_read;
20984 extended_end = line_ptr + extended_len;
20985 extended_op = read_1_byte (abfd, line_ptr);
20986 line_ptr += 1;
20987 if (DW_LNE_lo_user <= extended_op
20988 && extended_op <= DW_LNE_hi_user)
20989 {
20990 /* Vendor extension, ignore. */
20991 line_ptr = extended_end;
20992 break;
20993 }
20994 switch (extended_op)
20995 {
20996 case DW_LNE_end_sequence:
20997 state_machine.handle_end_sequence ();
20998 end_sequence = true;
20999 break;
21000 case DW_LNE_set_address:
21001 {
21002 CORE_ADDR address
21003 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21004 line_ptr += bytes_read;
21005
21006 state_machine.check_line_address (cu, line_ptr,
21007 lowpc - baseaddr, address);
21008 state_machine.handle_set_address (baseaddr, address);
21009 }
21010 break;
21011 case DW_LNE_define_file:
21012 {
21013 const char *cur_file;
21014 unsigned int mod_time, length;
21015 dir_index dindex;
21016
21017 cur_file = read_direct_string (abfd, line_ptr,
21018 &bytes_read);
21019 line_ptr += bytes_read;
21020 dindex = (dir_index)
21021 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21022 line_ptr += bytes_read;
21023 mod_time =
21024 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21025 line_ptr += bytes_read;
21026 length =
21027 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21028 line_ptr += bytes_read;
21029 lh->add_file_name (cur_file, dindex, mod_time, length);
21030 }
21031 break;
21032 case DW_LNE_set_discriminator:
21033 {
21034 /* The discriminator is not interesting to the
21035 debugger; just ignore it. We still need to
21036 check its value though:
21037 if there are consecutive entries for the same
21038 (non-prologue) line we want to coalesce them.
21039 PR 17276. */
21040 unsigned int discr
21041 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21042 line_ptr += bytes_read;
21043
21044 state_machine.handle_set_discriminator (discr);
21045 }
21046 break;
21047 default:
21048 complaint (_("mangled .debug_line section"));
21049 return;
21050 }
21051 /* Make sure that we parsed the extended op correctly. If e.g.
21052 we expected a different address size than the producer used,
21053 we may have read the wrong number of bytes. */
21054 if (line_ptr != extended_end)
21055 {
21056 complaint (_("mangled .debug_line section"));
21057 return;
21058 }
21059 break;
21060 case DW_LNS_copy:
21061 state_machine.handle_copy ();
21062 break;
21063 case DW_LNS_advance_pc:
21064 {
21065 CORE_ADDR adjust
21066 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21067 line_ptr += bytes_read;
21068
21069 state_machine.handle_advance_pc (adjust);
21070 }
21071 break;
21072 case DW_LNS_advance_line:
21073 {
21074 int line_delta
21075 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21076 line_ptr += bytes_read;
21077
21078 state_machine.handle_advance_line (line_delta);
21079 }
21080 break;
21081 case DW_LNS_set_file:
21082 {
21083 file_name_index file
21084 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21085 &bytes_read);
21086 line_ptr += bytes_read;
21087
21088 state_machine.handle_set_file (file);
21089 }
21090 break;
21091 case DW_LNS_set_column:
21092 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21093 line_ptr += bytes_read;
21094 break;
21095 case DW_LNS_negate_stmt:
21096 state_machine.handle_negate_stmt ();
21097 break;
21098 case DW_LNS_set_basic_block:
21099 break;
21100 /* Add to the address register of the state machine the
21101 address increment value corresponding to special opcode
21102 255. I.e., this value is scaled by the minimum
21103 instruction length since special opcode 255 would have
21104 scaled the increment. */
21105 case DW_LNS_const_add_pc:
21106 state_machine.handle_const_add_pc ();
21107 break;
21108 case DW_LNS_fixed_advance_pc:
21109 {
21110 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21111 line_ptr += 2;
21112
21113 state_machine.handle_fixed_advance_pc (addr_adj);
21114 }
21115 break;
21116 default:
21117 {
21118 /* Unknown standard opcode, ignore it. */
21119 int i;
21120
21121 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21122 {
21123 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21124 line_ptr += bytes_read;
21125 }
21126 }
21127 }
21128 }
21129
21130 if (!end_sequence)
21131 dwarf2_debug_line_missing_end_sequence_complaint ();
21132
21133 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21134 in which case we still finish recording the last line). */
21135 state_machine.record_line (true);
21136 }
21137 }
21138
21139 /* Decode the Line Number Program (LNP) for the given line_header
21140 structure and CU. The actual information extracted and the type
21141 of structures created from the LNP depends on the value of PST.
21142
21143 1. If PST is NULL, then this procedure uses the data from the program
21144 to create all necessary symbol tables, and their linetables.
21145
21146 2. If PST is not NULL, this procedure reads the program to determine
21147 the list of files included by the unit represented by PST, and
21148 builds all the associated partial symbol tables.
21149
21150 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21151 It is used for relative paths in the line table.
21152 NOTE: When processing partial symtabs (pst != NULL),
21153 comp_dir == pst->dirname.
21154
21155 NOTE: It is important that psymtabs have the same file name (via strcmp)
21156 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21157 symtab we don't use it in the name of the psymtabs we create.
21158 E.g. expand_line_sal requires this when finding psymtabs to expand.
21159 A good testcase for this is mb-inline.exp.
21160
21161 LOWPC is the lowest address in CU (or 0 if not known).
21162
21163 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21164 for its PC<->lines mapping information. Otherwise only the filename
21165 table is read in. */
21166
21167 static void
21168 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21169 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21170 CORE_ADDR lowpc, int decode_mapping)
21171 {
21172 struct objfile *objfile = cu->per_objfile->objfile;
21173 const int decode_for_pst_p = (pst != NULL);
21174
21175 if (decode_mapping)
21176 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21177
21178 if (decode_for_pst_p)
21179 {
21180 /* Now that we're done scanning the Line Header Program, we can
21181 create the psymtab of each included file. */
21182 for (auto &file_entry : lh->file_names ())
21183 if (file_entry.included_p == 1)
21184 {
21185 gdb::unique_xmalloc_ptr<char> name_holder;
21186 const char *include_name =
21187 psymtab_include_file_name (lh, file_entry, pst,
21188 comp_dir, &name_holder);
21189 if (include_name != NULL)
21190 dwarf2_create_include_psymtab (include_name, pst, objfile);
21191 }
21192 }
21193 else
21194 {
21195 /* Make sure a symtab is created for every file, even files
21196 which contain only variables (i.e. no code with associated
21197 line numbers). */
21198 buildsym_compunit *builder = cu->get_builder ();
21199 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21200
21201 for (auto &fe : lh->file_names ())
21202 {
21203 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21204 if (builder->get_current_subfile ()->symtab == NULL)
21205 {
21206 builder->get_current_subfile ()->symtab
21207 = allocate_symtab (cust,
21208 builder->get_current_subfile ()->name);
21209 }
21210 fe.symtab = builder->get_current_subfile ()->symtab;
21211 }
21212 }
21213 }
21214
21215 /* Start a subfile for DWARF. FILENAME is the name of the file and
21216 DIRNAME the name of the source directory which contains FILENAME
21217 or NULL if not known.
21218 This routine tries to keep line numbers from identical absolute and
21219 relative file names in a common subfile.
21220
21221 Using the `list' example from the GDB testsuite, which resides in
21222 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21223 of /srcdir/list0.c yields the following debugging information for list0.c:
21224
21225 DW_AT_name: /srcdir/list0.c
21226 DW_AT_comp_dir: /compdir
21227 files.files[0].name: list0.h
21228 files.files[0].dir: /srcdir
21229 files.files[1].name: list0.c
21230 files.files[1].dir: /srcdir
21231
21232 The line number information for list0.c has to end up in a single
21233 subfile, so that `break /srcdir/list0.c:1' works as expected.
21234 start_subfile will ensure that this happens provided that we pass the
21235 concatenation of files.files[1].dir and files.files[1].name as the
21236 subfile's name. */
21237
21238 static void
21239 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21240 const char *dirname)
21241 {
21242 gdb::unique_xmalloc_ptr<char> copy;
21243
21244 /* In order not to lose the line information directory,
21245 we concatenate it to the filename when it makes sense.
21246 Note that the Dwarf3 standard says (speaking of filenames in line
21247 information): ``The directory index is ignored for file names
21248 that represent full path names''. Thus ignoring dirname in the
21249 `else' branch below isn't an issue. */
21250
21251 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21252 {
21253 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21254 filename = copy.get ();
21255 }
21256
21257 cu->get_builder ()->start_subfile (filename);
21258 }
21259
21260 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21261 buildsym_compunit constructor. */
21262
21263 struct compunit_symtab *
21264 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21265 CORE_ADDR low_pc)
21266 {
21267 gdb_assert (m_builder == nullptr);
21268
21269 m_builder.reset (new struct buildsym_compunit
21270 (this->per_objfile->objfile,
21271 name, comp_dir, language, low_pc));
21272
21273 list_in_scope = get_builder ()->get_file_symbols ();
21274
21275 get_builder ()->record_debugformat ("DWARF 2");
21276 get_builder ()->record_producer (producer);
21277
21278 processing_has_namespace_info = false;
21279
21280 return get_builder ()->get_compunit_symtab ();
21281 }
21282
21283 static void
21284 var_decode_location (struct attribute *attr, struct symbol *sym,
21285 struct dwarf2_cu *cu)
21286 {
21287 struct objfile *objfile = cu->per_objfile->objfile;
21288 struct comp_unit_head *cu_header = &cu->header;
21289
21290 /* NOTE drow/2003-01-30: There used to be a comment and some special
21291 code here to turn a symbol with DW_AT_external and a
21292 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21293 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21294 with some versions of binutils) where shared libraries could have
21295 relocations against symbols in their debug information - the
21296 minimal symbol would have the right address, but the debug info
21297 would not. It's no longer necessary, because we will explicitly
21298 apply relocations when we read in the debug information now. */
21299
21300 /* A DW_AT_location attribute with no contents indicates that a
21301 variable has been optimized away. */
21302 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
21303 {
21304 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21305 return;
21306 }
21307
21308 /* Handle one degenerate form of location expression specially, to
21309 preserve GDB's previous behavior when section offsets are
21310 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21311 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21312
21313 if (attr->form_is_block ()
21314 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21315 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21316 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21317 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
21318 && (DW_BLOCK (attr)->size
21319 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21320 {
21321 unsigned int dummy;
21322
21323 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21324 SET_SYMBOL_VALUE_ADDRESS
21325 (sym, cu->header.read_address (objfile->obfd,
21326 DW_BLOCK (attr)->data + 1,
21327 &dummy));
21328 else
21329 SET_SYMBOL_VALUE_ADDRESS
21330 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21331 &dummy));
21332 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21333 fixup_symbol_section (sym, objfile);
21334 SET_SYMBOL_VALUE_ADDRESS
21335 (sym,
21336 SYMBOL_VALUE_ADDRESS (sym)
21337 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
21338 return;
21339 }
21340
21341 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21342 expression evaluator, and use LOC_COMPUTED only when necessary
21343 (i.e. when the value of a register or memory location is
21344 referenced, or a thread-local block, etc.). Then again, it might
21345 not be worthwhile. I'm assuming that it isn't unless performance
21346 or memory numbers show me otherwise. */
21347
21348 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21349
21350 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21351 cu->has_loclist = true;
21352 }
21353
21354 /* Given a pointer to a DWARF information entry, figure out if we need
21355 to make a symbol table entry for it, and if so, create a new entry
21356 and return a pointer to it.
21357 If TYPE is NULL, determine symbol type from the die, otherwise
21358 used the passed type.
21359 If SPACE is not NULL, use it to hold the new symbol. If it is
21360 NULL, allocate a new symbol on the objfile's obstack. */
21361
21362 static struct symbol *
21363 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21364 struct symbol *space)
21365 {
21366 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21367 struct objfile *objfile = per_objfile->objfile;
21368 struct gdbarch *gdbarch = objfile->arch ();
21369 struct symbol *sym = NULL;
21370 const char *name;
21371 struct attribute *attr = NULL;
21372 struct attribute *attr2 = NULL;
21373 CORE_ADDR baseaddr;
21374 struct pending **list_to_add = NULL;
21375
21376 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21377
21378 baseaddr = objfile->text_section_offset ();
21379
21380 name = dwarf2_name (die, cu);
21381 if (name)
21382 {
21383 int suppress_add = 0;
21384
21385 if (space)
21386 sym = space;
21387 else
21388 sym = new (&objfile->objfile_obstack) symbol;
21389 OBJSTAT (objfile, n_syms++);
21390
21391 /* Cache this symbol's name and the name's demangled form (if any). */
21392 sym->set_language (cu->language, &objfile->objfile_obstack);
21393 /* Fortran does not have mangling standard and the mangling does differ
21394 between gfortran, iFort etc. */
21395 const char *physname
21396 = (cu->language == language_fortran
21397 ? dwarf2_full_name (name, die, cu)
21398 : dwarf2_physname (name, die, cu));
21399 const char *linkagename = dw2_linkage_name (die, cu);
21400
21401 if (linkagename == nullptr || cu->language == language_ada)
21402 sym->set_linkage_name (physname);
21403 else
21404 {
21405 sym->set_demangled_name (physname, &objfile->objfile_obstack);
21406 sym->set_linkage_name (linkagename);
21407 }
21408
21409 /* Default assumptions.
21410 Use the passed type or decode it from the die. */
21411 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21412 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21413 if (type != NULL)
21414 SYMBOL_TYPE (sym) = type;
21415 else
21416 SYMBOL_TYPE (sym) = die_type (die, cu);
21417 attr = dwarf2_attr (die,
21418 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21419 cu);
21420 if (attr != nullptr)
21421 {
21422 SYMBOL_LINE (sym) = DW_UNSND (attr);
21423 }
21424
21425 attr = dwarf2_attr (die,
21426 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21427 cu);
21428 if (attr != nullptr)
21429 {
21430 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21431 struct file_entry *fe;
21432
21433 if (cu->line_header != NULL)
21434 fe = cu->line_header->file_name_at (file_index);
21435 else
21436 fe = NULL;
21437
21438 if (fe == NULL)
21439 complaint (_("file index out of range"));
21440 else
21441 symbol_set_symtab (sym, fe->symtab);
21442 }
21443
21444 switch (die->tag)
21445 {
21446 case DW_TAG_label:
21447 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21448 if (attr != nullptr)
21449 {
21450 CORE_ADDR addr;
21451
21452 addr = attr->value_as_address ();
21453 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21454 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21455 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21456 }
21457 else
21458 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21459 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21460 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21461 add_symbol_to_list (sym, cu->list_in_scope);
21462 break;
21463 case DW_TAG_subprogram:
21464 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21465 finish_block. */
21466 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21467 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21468 if ((attr2 && (DW_UNSND (attr2) != 0))
21469 || cu->language == language_ada
21470 || cu->language == language_fortran)
21471 {
21472 /* Subprograms marked external are stored as a global symbol.
21473 Ada and Fortran subprograms, whether marked external or
21474 not, are always stored as a global symbol, because we want
21475 to be able to access them globally. For instance, we want
21476 to be able to break on a nested subprogram without having
21477 to specify the context. */
21478 list_to_add = cu->get_builder ()->get_global_symbols ();
21479 }
21480 else
21481 {
21482 list_to_add = cu->list_in_scope;
21483 }
21484 break;
21485 case DW_TAG_inlined_subroutine:
21486 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21487 finish_block. */
21488 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21489 SYMBOL_INLINED (sym) = 1;
21490 list_to_add = cu->list_in_scope;
21491 break;
21492 case DW_TAG_template_value_param:
21493 suppress_add = 1;
21494 /* Fall through. */
21495 case DW_TAG_constant:
21496 case DW_TAG_variable:
21497 case DW_TAG_member:
21498 /* Compilation with minimal debug info may result in
21499 variables with missing type entries. Change the
21500 misleading `void' type to something sensible. */
21501 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
21502 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21503
21504 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21505 /* In the case of DW_TAG_member, we should only be called for
21506 static const members. */
21507 if (die->tag == DW_TAG_member)
21508 {
21509 /* dwarf2_add_field uses die_is_declaration,
21510 so we do the same. */
21511 gdb_assert (die_is_declaration (die, cu));
21512 gdb_assert (attr);
21513 }
21514 if (attr != nullptr)
21515 {
21516 dwarf2_const_value (attr, sym, cu);
21517 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21518 if (!suppress_add)
21519 {
21520 if (attr2 && (DW_UNSND (attr2) != 0))
21521 list_to_add = cu->get_builder ()->get_global_symbols ();
21522 else
21523 list_to_add = cu->list_in_scope;
21524 }
21525 break;
21526 }
21527 attr = dwarf2_attr (die, DW_AT_location, cu);
21528 if (attr != nullptr)
21529 {
21530 var_decode_location (attr, sym, cu);
21531 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21532
21533 /* Fortran explicitly imports any global symbols to the local
21534 scope by DW_TAG_common_block. */
21535 if (cu->language == language_fortran && die->parent
21536 && die->parent->tag == DW_TAG_common_block)
21537 attr2 = NULL;
21538
21539 if (SYMBOL_CLASS (sym) == LOC_STATIC
21540 && SYMBOL_VALUE_ADDRESS (sym) == 0
21541 && !per_objfile->per_bfd->has_section_at_zero)
21542 {
21543 /* When a static variable is eliminated by the linker,
21544 the corresponding debug information is not stripped
21545 out, but the variable address is set to null;
21546 do not add such variables into symbol table. */
21547 }
21548 else if (attr2 && (DW_UNSND (attr2) != 0))
21549 {
21550 if (SYMBOL_CLASS (sym) == LOC_STATIC
21551 && (objfile->flags & OBJF_MAINLINE) == 0
21552 && per_objfile->per_bfd->can_copy)
21553 {
21554 /* A global static variable might be subject to
21555 copy relocation. We first check for a local
21556 minsym, though, because maybe the symbol was
21557 marked hidden, in which case this would not
21558 apply. */
21559 bound_minimal_symbol found
21560 = (lookup_minimal_symbol_linkage
21561 (sym->linkage_name (), objfile));
21562 if (found.minsym != nullptr)
21563 sym->maybe_copied = 1;
21564 }
21565
21566 /* A variable with DW_AT_external is never static,
21567 but it may be block-scoped. */
21568 list_to_add
21569 = ((cu->list_in_scope
21570 == cu->get_builder ()->get_file_symbols ())
21571 ? cu->get_builder ()->get_global_symbols ()
21572 : cu->list_in_scope);
21573 }
21574 else
21575 list_to_add = cu->list_in_scope;
21576 }
21577 else
21578 {
21579 /* We do not know the address of this symbol.
21580 If it is an external symbol and we have type information
21581 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21582 The address of the variable will then be determined from
21583 the minimal symbol table whenever the variable is
21584 referenced. */
21585 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21586
21587 /* Fortran explicitly imports any global symbols to the local
21588 scope by DW_TAG_common_block. */
21589 if (cu->language == language_fortran && die->parent
21590 && die->parent->tag == DW_TAG_common_block)
21591 {
21592 /* SYMBOL_CLASS doesn't matter here because
21593 read_common_block is going to reset it. */
21594 if (!suppress_add)
21595 list_to_add = cu->list_in_scope;
21596 }
21597 else if (attr2 && (DW_UNSND (attr2) != 0)
21598 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21599 {
21600 /* A variable with DW_AT_external is never static, but it
21601 may be block-scoped. */
21602 list_to_add
21603 = ((cu->list_in_scope
21604 == cu->get_builder ()->get_file_symbols ())
21605 ? cu->get_builder ()->get_global_symbols ()
21606 : cu->list_in_scope);
21607
21608 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21609 }
21610 else if (!die_is_declaration (die, cu))
21611 {
21612 /* Use the default LOC_OPTIMIZED_OUT class. */
21613 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21614 if (!suppress_add)
21615 list_to_add = cu->list_in_scope;
21616 }
21617 }
21618 break;
21619 case DW_TAG_formal_parameter:
21620 {
21621 /* If we are inside a function, mark this as an argument. If
21622 not, we might be looking at an argument to an inlined function
21623 when we do not have enough information to show inlined frames;
21624 pretend it's a local variable in that case so that the user can
21625 still see it. */
21626 struct context_stack *curr
21627 = cu->get_builder ()->get_current_context_stack ();
21628 if (curr != nullptr && curr->name != nullptr)
21629 SYMBOL_IS_ARGUMENT (sym) = 1;
21630 attr = dwarf2_attr (die, DW_AT_location, cu);
21631 if (attr != nullptr)
21632 {
21633 var_decode_location (attr, sym, cu);
21634 }
21635 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21636 if (attr != nullptr)
21637 {
21638 dwarf2_const_value (attr, sym, cu);
21639 }
21640
21641 list_to_add = cu->list_in_scope;
21642 }
21643 break;
21644 case DW_TAG_unspecified_parameters:
21645 /* From varargs functions; gdb doesn't seem to have any
21646 interest in this information, so just ignore it for now.
21647 (FIXME?) */
21648 break;
21649 case DW_TAG_template_type_param:
21650 suppress_add = 1;
21651 /* Fall through. */
21652 case DW_TAG_class_type:
21653 case DW_TAG_interface_type:
21654 case DW_TAG_structure_type:
21655 case DW_TAG_union_type:
21656 case DW_TAG_set_type:
21657 case DW_TAG_enumeration_type:
21658 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21659 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21660
21661 {
21662 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21663 really ever be static objects: otherwise, if you try
21664 to, say, break of a class's method and you're in a file
21665 which doesn't mention that class, it won't work unless
21666 the check for all static symbols in lookup_symbol_aux
21667 saves you. See the OtherFileClass tests in
21668 gdb.c++/namespace.exp. */
21669
21670 if (!suppress_add)
21671 {
21672 buildsym_compunit *builder = cu->get_builder ();
21673 list_to_add
21674 = (cu->list_in_scope == builder->get_file_symbols ()
21675 && cu->language == language_cplus
21676 ? builder->get_global_symbols ()
21677 : cu->list_in_scope);
21678
21679 /* The semantics of C++ state that "struct foo {
21680 ... }" also defines a typedef for "foo". */
21681 if (cu->language == language_cplus
21682 || cu->language == language_ada
21683 || cu->language == language_d
21684 || cu->language == language_rust)
21685 {
21686 /* The symbol's name is already allocated along
21687 with this objfile, so we don't need to
21688 duplicate it for the type. */
21689 if (SYMBOL_TYPE (sym)->name () == 0)
21690 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
21691 }
21692 }
21693 }
21694 break;
21695 case DW_TAG_typedef:
21696 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21697 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21698 list_to_add = cu->list_in_scope;
21699 break;
21700 case DW_TAG_base_type:
21701 case DW_TAG_subrange_type:
21702 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21703 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21704 list_to_add = cu->list_in_scope;
21705 break;
21706 case DW_TAG_enumerator:
21707 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21708 if (attr != nullptr)
21709 {
21710 dwarf2_const_value (attr, sym, cu);
21711 }
21712 {
21713 /* NOTE: carlton/2003-11-10: See comment above in the
21714 DW_TAG_class_type, etc. block. */
21715
21716 list_to_add
21717 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21718 && cu->language == language_cplus
21719 ? cu->get_builder ()->get_global_symbols ()
21720 : cu->list_in_scope);
21721 }
21722 break;
21723 case DW_TAG_imported_declaration:
21724 case DW_TAG_namespace:
21725 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21726 list_to_add = cu->get_builder ()->get_global_symbols ();
21727 break;
21728 case DW_TAG_module:
21729 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21730 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21731 list_to_add = cu->get_builder ()->get_global_symbols ();
21732 break;
21733 case DW_TAG_common_block:
21734 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21735 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21736 add_symbol_to_list (sym, cu->list_in_scope);
21737 break;
21738 default:
21739 /* Not a tag we recognize. Hopefully we aren't processing
21740 trash data, but since we must specifically ignore things
21741 we don't recognize, there is nothing else we should do at
21742 this point. */
21743 complaint (_("unsupported tag: '%s'"),
21744 dwarf_tag_name (die->tag));
21745 break;
21746 }
21747
21748 if (suppress_add)
21749 {
21750 sym->hash_next = objfile->template_symbols;
21751 objfile->template_symbols = sym;
21752 list_to_add = NULL;
21753 }
21754
21755 if (list_to_add != NULL)
21756 add_symbol_to_list (sym, list_to_add);
21757
21758 /* For the benefit of old versions of GCC, check for anonymous
21759 namespaces based on the demangled name. */
21760 if (!cu->processing_has_namespace_info
21761 && cu->language == language_cplus)
21762 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21763 }
21764 return (sym);
21765 }
21766
21767 /* Given an attr with a DW_FORM_dataN value in host byte order,
21768 zero-extend it as appropriate for the symbol's type. The DWARF
21769 standard (v4) is not entirely clear about the meaning of using
21770 DW_FORM_dataN for a constant with a signed type, where the type is
21771 wider than the data. The conclusion of a discussion on the DWARF
21772 list was that this is unspecified. We choose to always zero-extend
21773 because that is the interpretation long in use by GCC. */
21774
21775 static gdb_byte *
21776 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21777 struct dwarf2_cu *cu, LONGEST *value, int bits)
21778 {
21779 struct objfile *objfile = cu->per_objfile->objfile;
21780 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21781 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21782 LONGEST l = DW_UNSND (attr);
21783
21784 if (bits < sizeof (*value) * 8)
21785 {
21786 l &= ((LONGEST) 1 << bits) - 1;
21787 *value = l;
21788 }
21789 else if (bits == sizeof (*value) * 8)
21790 *value = l;
21791 else
21792 {
21793 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21794 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21795 return bytes;
21796 }
21797
21798 return NULL;
21799 }
21800
21801 /* Read a constant value from an attribute. Either set *VALUE, or if
21802 the value does not fit in *VALUE, set *BYTES - either already
21803 allocated on the objfile obstack, or newly allocated on OBSTACK,
21804 or, set *BATON, if we translated the constant to a location
21805 expression. */
21806
21807 static void
21808 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21809 const char *name, struct obstack *obstack,
21810 struct dwarf2_cu *cu,
21811 LONGEST *value, const gdb_byte **bytes,
21812 struct dwarf2_locexpr_baton **baton)
21813 {
21814 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21815 struct objfile *objfile = per_objfile->objfile;
21816 struct comp_unit_head *cu_header = &cu->header;
21817 struct dwarf_block *blk;
21818 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21819 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21820
21821 *value = 0;
21822 *bytes = NULL;
21823 *baton = NULL;
21824
21825 switch (attr->form)
21826 {
21827 case DW_FORM_addr:
21828 case DW_FORM_addrx:
21829 case DW_FORM_GNU_addr_index:
21830 {
21831 gdb_byte *data;
21832
21833 if (TYPE_LENGTH (type) != cu_header->addr_size)
21834 dwarf2_const_value_length_mismatch_complaint (name,
21835 cu_header->addr_size,
21836 TYPE_LENGTH (type));
21837 /* Symbols of this form are reasonably rare, so we just
21838 piggyback on the existing location code rather than writing
21839 a new implementation of symbol_computed_ops. */
21840 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21841 (*baton)->per_objfile = per_objfile;
21842 (*baton)->per_cu = cu->per_cu;
21843 gdb_assert ((*baton)->per_cu);
21844
21845 (*baton)->size = 2 + cu_header->addr_size;
21846 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21847 (*baton)->data = data;
21848
21849 data[0] = DW_OP_addr;
21850 store_unsigned_integer (&data[1], cu_header->addr_size,
21851 byte_order, DW_ADDR (attr));
21852 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21853 }
21854 break;
21855 case DW_FORM_string:
21856 case DW_FORM_strp:
21857 case DW_FORM_strx:
21858 case DW_FORM_GNU_str_index:
21859 case DW_FORM_GNU_strp_alt:
21860 /* DW_STRING is already allocated on the objfile obstack, point
21861 directly to it. */
21862 *bytes = (const gdb_byte *) DW_STRING (attr);
21863 break;
21864 case DW_FORM_block1:
21865 case DW_FORM_block2:
21866 case DW_FORM_block4:
21867 case DW_FORM_block:
21868 case DW_FORM_exprloc:
21869 case DW_FORM_data16:
21870 blk = DW_BLOCK (attr);
21871 if (TYPE_LENGTH (type) != blk->size)
21872 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21873 TYPE_LENGTH (type));
21874 *bytes = blk->data;
21875 break;
21876
21877 /* The DW_AT_const_value attributes are supposed to carry the
21878 symbol's value "represented as it would be on the target
21879 architecture." By the time we get here, it's already been
21880 converted to host endianness, so we just need to sign- or
21881 zero-extend it as appropriate. */
21882 case DW_FORM_data1:
21883 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21884 break;
21885 case DW_FORM_data2:
21886 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21887 break;
21888 case DW_FORM_data4:
21889 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21890 break;
21891 case DW_FORM_data8:
21892 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21893 break;
21894
21895 case DW_FORM_sdata:
21896 case DW_FORM_implicit_const:
21897 *value = DW_SND (attr);
21898 break;
21899
21900 case DW_FORM_udata:
21901 *value = DW_UNSND (attr);
21902 break;
21903
21904 default:
21905 complaint (_("unsupported const value attribute form: '%s'"),
21906 dwarf_form_name (attr->form));
21907 *value = 0;
21908 break;
21909 }
21910 }
21911
21912
21913 /* Copy constant value from an attribute to a symbol. */
21914
21915 static void
21916 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21917 struct dwarf2_cu *cu)
21918 {
21919 struct objfile *objfile = cu->per_objfile->objfile;
21920 LONGEST value;
21921 const gdb_byte *bytes;
21922 struct dwarf2_locexpr_baton *baton;
21923
21924 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21925 sym->print_name (),
21926 &objfile->objfile_obstack, cu,
21927 &value, &bytes, &baton);
21928
21929 if (baton != NULL)
21930 {
21931 SYMBOL_LOCATION_BATON (sym) = baton;
21932 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21933 }
21934 else if (bytes != NULL)
21935 {
21936 SYMBOL_VALUE_BYTES (sym) = bytes;
21937 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21938 }
21939 else
21940 {
21941 SYMBOL_VALUE (sym) = value;
21942 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21943 }
21944 }
21945
21946 /* Return the type of the die in question using its DW_AT_type attribute. */
21947
21948 static struct type *
21949 die_type (struct die_info *die, struct dwarf2_cu *cu)
21950 {
21951 struct attribute *type_attr;
21952
21953 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21954 if (!type_attr)
21955 {
21956 struct objfile *objfile = cu->per_objfile->objfile;
21957 /* A missing DW_AT_type represents a void type. */
21958 return objfile_type (objfile)->builtin_void;
21959 }
21960
21961 return lookup_die_type (die, type_attr, cu);
21962 }
21963
21964 /* True iff CU's producer generates GNAT Ada auxiliary information
21965 that allows to find parallel types through that information instead
21966 of having to do expensive parallel lookups by type name. */
21967
21968 static int
21969 need_gnat_info (struct dwarf2_cu *cu)
21970 {
21971 /* Assume that the Ada compiler was GNAT, which always produces
21972 the auxiliary information. */
21973 return (cu->language == language_ada);
21974 }
21975
21976 /* Return the auxiliary type of the die in question using its
21977 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21978 attribute is not present. */
21979
21980 static struct type *
21981 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21982 {
21983 struct attribute *type_attr;
21984
21985 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21986 if (!type_attr)
21987 return NULL;
21988
21989 return lookup_die_type (die, type_attr, cu);
21990 }
21991
21992 /* If DIE has a descriptive_type attribute, then set the TYPE's
21993 descriptive type accordingly. */
21994
21995 static void
21996 set_descriptive_type (struct type *type, struct die_info *die,
21997 struct dwarf2_cu *cu)
21998 {
21999 struct type *descriptive_type = die_descriptive_type (die, cu);
22000
22001 if (descriptive_type)
22002 {
22003 ALLOCATE_GNAT_AUX_TYPE (type);
22004 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22005 }
22006 }
22007
22008 /* Return the containing type of the die in question using its
22009 DW_AT_containing_type attribute. */
22010
22011 static struct type *
22012 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22013 {
22014 struct attribute *type_attr;
22015 struct objfile *objfile = cu->per_objfile->objfile;
22016
22017 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22018 if (!type_attr)
22019 error (_("Dwarf Error: Problem turning containing type into gdb type "
22020 "[in module %s]"), objfile_name (objfile));
22021
22022 return lookup_die_type (die, type_attr, cu);
22023 }
22024
22025 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22026
22027 static struct type *
22028 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22029 {
22030 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22031 struct objfile *objfile = per_objfile->objfile;
22032 char *saved;
22033
22034 std::string message
22035 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22036 objfile_name (objfile),
22037 sect_offset_str (cu->header.sect_off),
22038 sect_offset_str (die->sect_off));
22039 saved = obstack_strdup (&objfile->objfile_obstack, message);
22040
22041 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22042 }
22043
22044 /* Look up the type of DIE in CU using its type attribute ATTR.
22045 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22046 DW_AT_containing_type.
22047 If there is no type substitute an error marker. */
22048
22049 static struct type *
22050 lookup_die_type (struct die_info *die, const struct attribute *attr,
22051 struct dwarf2_cu *cu)
22052 {
22053 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22054 struct objfile *objfile = per_objfile->objfile;
22055 struct type *this_type;
22056
22057 gdb_assert (attr->name == DW_AT_type
22058 || attr->name == DW_AT_GNAT_descriptive_type
22059 || attr->name == DW_AT_containing_type);
22060
22061 /* First see if we have it cached. */
22062
22063 if (attr->form == DW_FORM_GNU_ref_alt)
22064 {
22065 struct dwarf2_per_cu_data *per_cu;
22066 sect_offset sect_off = attr->get_ref_die_offset ();
22067
22068 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22069 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
22070 }
22071 else if (attr->form_is_ref ())
22072 {
22073 sect_offset sect_off = attr->get_ref_die_offset ();
22074
22075 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
22076 }
22077 else if (attr->form == DW_FORM_ref_sig8)
22078 {
22079 ULONGEST signature = DW_SIGNATURE (attr);
22080
22081 return get_signatured_type (die, signature, cu);
22082 }
22083 else
22084 {
22085 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22086 " at %s [in module %s]"),
22087 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22088 objfile_name (objfile));
22089 return build_error_marker_type (cu, die);
22090 }
22091
22092 /* If not cached we need to read it in. */
22093
22094 if (this_type == NULL)
22095 {
22096 struct die_info *type_die = NULL;
22097 struct dwarf2_cu *type_cu = cu;
22098
22099 if (attr->form_is_ref ())
22100 type_die = follow_die_ref (die, attr, &type_cu);
22101 if (type_die == NULL)
22102 return build_error_marker_type (cu, die);
22103 /* If we find the type now, it's probably because the type came
22104 from an inter-CU reference and the type's CU got expanded before
22105 ours. */
22106 this_type = read_type_die (type_die, type_cu);
22107 }
22108
22109 /* If we still don't have a type use an error marker. */
22110
22111 if (this_type == NULL)
22112 return build_error_marker_type (cu, die);
22113
22114 return this_type;
22115 }
22116
22117 /* Return the type in DIE, CU.
22118 Returns NULL for invalid types.
22119
22120 This first does a lookup in die_type_hash,
22121 and only reads the die in if necessary.
22122
22123 NOTE: This can be called when reading in partial or full symbols. */
22124
22125 static struct type *
22126 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22127 {
22128 struct type *this_type;
22129
22130 this_type = get_die_type (die, cu);
22131 if (this_type)
22132 return this_type;
22133
22134 return read_type_die_1 (die, cu);
22135 }
22136
22137 /* Read the type in DIE, CU.
22138 Returns NULL for invalid types. */
22139
22140 static struct type *
22141 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22142 {
22143 struct type *this_type = NULL;
22144
22145 switch (die->tag)
22146 {
22147 case DW_TAG_class_type:
22148 case DW_TAG_interface_type:
22149 case DW_TAG_structure_type:
22150 case DW_TAG_union_type:
22151 this_type = read_structure_type (die, cu);
22152 break;
22153 case DW_TAG_enumeration_type:
22154 this_type = read_enumeration_type (die, cu);
22155 break;
22156 case DW_TAG_subprogram:
22157 case DW_TAG_subroutine_type:
22158 case DW_TAG_inlined_subroutine:
22159 this_type = read_subroutine_type (die, cu);
22160 break;
22161 case DW_TAG_array_type:
22162 this_type = read_array_type (die, cu);
22163 break;
22164 case DW_TAG_set_type:
22165 this_type = read_set_type (die, cu);
22166 break;
22167 case DW_TAG_pointer_type:
22168 this_type = read_tag_pointer_type (die, cu);
22169 break;
22170 case DW_TAG_ptr_to_member_type:
22171 this_type = read_tag_ptr_to_member_type (die, cu);
22172 break;
22173 case DW_TAG_reference_type:
22174 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22175 break;
22176 case DW_TAG_rvalue_reference_type:
22177 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22178 break;
22179 case DW_TAG_const_type:
22180 this_type = read_tag_const_type (die, cu);
22181 break;
22182 case DW_TAG_volatile_type:
22183 this_type = read_tag_volatile_type (die, cu);
22184 break;
22185 case DW_TAG_restrict_type:
22186 this_type = read_tag_restrict_type (die, cu);
22187 break;
22188 case DW_TAG_string_type:
22189 this_type = read_tag_string_type (die, cu);
22190 break;
22191 case DW_TAG_typedef:
22192 this_type = read_typedef (die, cu);
22193 break;
22194 case DW_TAG_subrange_type:
22195 this_type = read_subrange_type (die, cu);
22196 break;
22197 case DW_TAG_base_type:
22198 this_type = read_base_type (die, cu);
22199 break;
22200 case DW_TAG_unspecified_type:
22201 this_type = read_unspecified_type (die, cu);
22202 break;
22203 case DW_TAG_namespace:
22204 this_type = read_namespace_type (die, cu);
22205 break;
22206 case DW_TAG_module:
22207 this_type = read_module_type (die, cu);
22208 break;
22209 case DW_TAG_atomic_type:
22210 this_type = read_tag_atomic_type (die, cu);
22211 break;
22212 default:
22213 complaint (_("unexpected tag in read_type_die: '%s'"),
22214 dwarf_tag_name (die->tag));
22215 break;
22216 }
22217
22218 return this_type;
22219 }
22220
22221 /* See if we can figure out if the class lives in a namespace. We do
22222 this by looking for a member function; its demangled name will
22223 contain namespace info, if there is any.
22224 Return the computed name or NULL.
22225 Space for the result is allocated on the objfile's obstack.
22226 This is the full-die version of guess_partial_die_structure_name.
22227 In this case we know DIE has no useful parent. */
22228
22229 static const char *
22230 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22231 {
22232 struct die_info *spec_die;
22233 struct dwarf2_cu *spec_cu;
22234 struct die_info *child;
22235 struct objfile *objfile = cu->per_objfile->objfile;
22236
22237 spec_cu = cu;
22238 spec_die = die_specification (die, &spec_cu);
22239 if (spec_die != NULL)
22240 {
22241 die = spec_die;
22242 cu = spec_cu;
22243 }
22244
22245 for (child = die->child;
22246 child != NULL;
22247 child = child->sibling)
22248 {
22249 if (child->tag == DW_TAG_subprogram)
22250 {
22251 const char *linkage_name = dw2_linkage_name (child, cu);
22252
22253 if (linkage_name != NULL)
22254 {
22255 gdb::unique_xmalloc_ptr<char> actual_name
22256 (cu->language_defn->class_name_from_physname (linkage_name));
22257 const char *name = NULL;
22258
22259 if (actual_name != NULL)
22260 {
22261 const char *die_name = dwarf2_name (die, cu);
22262
22263 if (die_name != NULL
22264 && strcmp (die_name, actual_name.get ()) != 0)
22265 {
22266 /* Strip off the class name from the full name.
22267 We want the prefix. */
22268 int die_name_len = strlen (die_name);
22269 int actual_name_len = strlen (actual_name.get ());
22270 const char *ptr = actual_name.get ();
22271
22272 /* Test for '::' as a sanity check. */
22273 if (actual_name_len > die_name_len + 2
22274 && ptr[actual_name_len - die_name_len - 1] == ':')
22275 name = obstack_strndup (
22276 &objfile->per_bfd->storage_obstack,
22277 ptr, actual_name_len - die_name_len - 2);
22278 }
22279 }
22280 return name;
22281 }
22282 }
22283 }
22284
22285 return NULL;
22286 }
22287
22288 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22289 prefix part in such case. See
22290 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22291
22292 static const char *
22293 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22294 {
22295 struct attribute *attr;
22296 const char *base;
22297
22298 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22299 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22300 return NULL;
22301
22302 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22303 return NULL;
22304
22305 attr = dw2_linkage_name_attr (die, cu);
22306 if (attr == NULL || DW_STRING (attr) == NULL)
22307 return NULL;
22308
22309 /* dwarf2_name had to be already called. */
22310 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22311
22312 /* Strip the base name, keep any leading namespaces/classes. */
22313 base = strrchr (DW_STRING (attr), ':');
22314 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22315 return "";
22316
22317 struct objfile *objfile = cu->per_objfile->objfile;
22318 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22319 DW_STRING (attr),
22320 &base[-1] - DW_STRING (attr));
22321 }
22322
22323 /* Return the name of the namespace/class that DIE is defined within,
22324 or "" if we can't tell. The caller should not xfree the result.
22325
22326 For example, if we're within the method foo() in the following
22327 code:
22328
22329 namespace N {
22330 class C {
22331 void foo () {
22332 }
22333 };
22334 }
22335
22336 then determine_prefix on foo's die will return "N::C". */
22337
22338 static const char *
22339 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22340 {
22341 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22342 struct die_info *parent, *spec_die;
22343 struct dwarf2_cu *spec_cu;
22344 struct type *parent_type;
22345 const char *retval;
22346
22347 if (cu->language != language_cplus
22348 && cu->language != language_fortran && cu->language != language_d
22349 && cu->language != language_rust)
22350 return "";
22351
22352 retval = anonymous_struct_prefix (die, cu);
22353 if (retval)
22354 return retval;
22355
22356 /* We have to be careful in the presence of DW_AT_specification.
22357 For example, with GCC 3.4, given the code
22358
22359 namespace N {
22360 void foo() {
22361 // Definition of N::foo.
22362 }
22363 }
22364
22365 then we'll have a tree of DIEs like this:
22366
22367 1: DW_TAG_compile_unit
22368 2: DW_TAG_namespace // N
22369 3: DW_TAG_subprogram // declaration of N::foo
22370 4: DW_TAG_subprogram // definition of N::foo
22371 DW_AT_specification // refers to die #3
22372
22373 Thus, when processing die #4, we have to pretend that we're in
22374 the context of its DW_AT_specification, namely the contex of die
22375 #3. */
22376 spec_cu = cu;
22377 spec_die = die_specification (die, &spec_cu);
22378 if (spec_die == NULL)
22379 parent = die->parent;
22380 else
22381 {
22382 parent = spec_die->parent;
22383 cu = spec_cu;
22384 }
22385
22386 if (parent == NULL)
22387 return "";
22388 else if (parent->building_fullname)
22389 {
22390 const char *name;
22391 const char *parent_name;
22392
22393 /* It has been seen on RealView 2.2 built binaries,
22394 DW_TAG_template_type_param types actually _defined_ as
22395 children of the parent class:
22396
22397 enum E {};
22398 template class <class Enum> Class{};
22399 Class<enum E> class_e;
22400
22401 1: DW_TAG_class_type (Class)
22402 2: DW_TAG_enumeration_type (E)
22403 3: DW_TAG_enumerator (enum1:0)
22404 3: DW_TAG_enumerator (enum2:1)
22405 ...
22406 2: DW_TAG_template_type_param
22407 DW_AT_type DW_FORM_ref_udata (E)
22408
22409 Besides being broken debug info, it can put GDB into an
22410 infinite loop. Consider:
22411
22412 When we're building the full name for Class<E>, we'll start
22413 at Class, and go look over its template type parameters,
22414 finding E. We'll then try to build the full name of E, and
22415 reach here. We're now trying to build the full name of E,
22416 and look over the parent DIE for containing scope. In the
22417 broken case, if we followed the parent DIE of E, we'd again
22418 find Class, and once again go look at its template type
22419 arguments, etc., etc. Simply don't consider such parent die
22420 as source-level parent of this die (it can't be, the language
22421 doesn't allow it), and break the loop here. */
22422 name = dwarf2_name (die, cu);
22423 parent_name = dwarf2_name (parent, cu);
22424 complaint (_("template param type '%s' defined within parent '%s'"),
22425 name ? name : "<unknown>",
22426 parent_name ? parent_name : "<unknown>");
22427 return "";
22428 }
22429 else
22430 switch (parent->tag)
22431 {
22432 case DW_TAG_namespace:
22433 parent_type = read_type_die (parent, cu);
22434 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22435 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22436 Work around this problem here. */
22437 if (cu->language == language_cplus
22438 && strcmp (parent_type->name (), "::") == 0)
22439 return "";
22440 /* We give a name to even anonymous namespaces. */
22441 return parent_type->name ();
22442 case DW_TAG_class_type:
22443 case DW_TAG_interface_type:
22444 case DW_TAG_structure_type:
22445 case DW_TAG_union_type:
22446 case DW_TAG_module:
22447 parent_type = read_type_die (parent, cu);
22448 if (parent_type->name () != NULL)
22449 return parent_type->name ();
22450 else
22451 /* An anonymous structure is only allowed non-static data
22452 members; no typedefs, no member functions, et cetera.
22453 So it does not need a prefix. */
22454 return "";
22455 case DW_TAG_compile_unit:
22456 case DW_TAG_partial_unit:
22457 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22458 if (cu->language == language_cplus
22459 && !per_objfile->per_bfd->types.empty ()
22460 && die->child != NULL
22461 && (die->tag == DW_TAG_class_type
22462 || die->tag == DW_TAG_structure_type
22463 || die->tag == DW_TAG_union_type))
22464 {
22465 const char *name = guess_full_die_structure_name (die, cu);
22466 if (name != NULL)
22467 return name;
22468 }
22469 return "";
22470 case DW_TAG_subprogram:
22471 /* Nested subroutines in Fortran get a prefix with the name
22472 of the parent's subroutine. */
22473 if (cu->language == language_fortran)
22474 {
22475 if ((die->tag == DW_TAG_subprogram)
22476 && (dwarf2_name (parent, cu) != NULL))
22477 return dwarf2_name (parent, cu);
22478 }
22479 return determine_prefix (parent, cu);
22480 case DW_TAG_enumeration_type:
22481 parent_type = read_type_die (parent, cu);
22482 if (TYPE_DECLARED_CLASS (parent_type))
22483 {
22484 if (parent_type->name () != NULL)
22485 return parent_type->name ();
22486 return "";
22487 }
22488 /* Fall through. */
22489 default:
22490 return determine_prefix (parent, cu);
22491 }
22492 }
22493
22494 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22495 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22496 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22497 an obconcat, otherwise allocate storage for the result. The CU argument is
22498 used to determine the language and hence, the appropriate separator. */
22499
22500 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22501
22502 static char *
22503 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22504 int physname, struct dwarf2_cu *cu)
22505 {
22506 const char *lead = "";
22507 const char *sep;
22508
22509 if (suffix == NULL || suffix[0] == '\0'
22510 || prefix == NULL || prefix[0] == '\0')
22511 sep = "";
22512 else if (cu->language == language_d)
22513 {
22514 /* For D, the 'main' function could be defined in any module, but it
22515 should never be prefixed. */
22516 if (strcmp (suffix, "D main") == 0)
22517 {
22518 prefix = "";
22519 sep = "";
22520 }
22521 else
22522 sep = ".";
22523 }
22524 else if (cu->language == language_fortran && physname)
22525 {
22526 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22527 DW_AT_MIPS_linkage_name is preferred and used instead. */
22528
22529 lead = "__";
22530 sep = "_MOD_";
22531 }
22532 else
22533 sep = "::";
22534
22535 if (prefix == NULL)
22536 prefix = "";
22537 if (suffix == NULL)
22538 suffix = "";
22539
22540 if (obs == NULL)
22541 {
22542 char *retval
22543 = ((char *)
22544 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22545
22546 strcpy (retval, lead);
22547 strcat (retval, prefix);
22548 strcat (retval, sep);
22549 strcat (retval, suffix);
22550 return retval;
22551 }
22552 else
22553 {
22554 /* We have an obstack. */
22555 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22556 }
22557 }
22558
22559 /* Get name of a die, return NULL if not found. */
22560
22561 static const char *
22562 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22563 struct objfile *objfile)
22564 {
22565 if (name && cu->language == language_cplus)
22566 {
22567 gdb::unique_xmalloc_ptr<char> canon_name
22568 = cp_canonicalize_string (name);
22569
22570 if (canon_name != nullptr)
22571 name = objfile->intern (canon_name.get ());
22572 }
22573
22574 return name;
22575 }
22576
22577 /* Get name of a die, return NULL if not found.
22578 Anonymous namespaces are converted to their magic string. */
22579
22580 static const char *
22581 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22582 {
22583 struct attribute *attr;
22584 struct objfile *objfile = cu->per_objfile->objfile;
22585
22586 attr = dwarf2_attr (die, DW_AT_name, cu);
22587 if ((!attr || !DW_STRING (attr))
22588 && die->tag != DW_TAG_namespace
22589 && die->tag != DW_TAG_class_type
22590 && die->tag != DW_TAG_interface_type
22591 && die->tag != DW_TAG_structure_type
22592 && die->tag != DW_TAG_union_type)
22593 return NULL;
22594
22595 switch (die->tag)
22596 {
22597 case DW_TAG_compile_unit:
22598 case DW_TAG_partial_unit:
22599 /* Compilation units have a DW_AT_name that is a filename, not
22600 a source language identifier. */
22601 case DW_TAG_enumeration_type:
22602 case DW_TAG_enumerator:
22603 /* These tags always have simple identifiers already; no need
22604 to canonicalize them. */
22605 return DW_STRING (attr);
22606
22607 case DW_TAG_namespace:
22608 if (attr != NULL && DW_STRING (attr) != NULL)
22609 return DW_STRING (attr);
22610 return CP_ANONYMOUS_NAMESPACE_STR;
22611
22612 case DW_TAG_class_type:
22613 case DW_TAG_interface_type:
22614 case DW_TAG_structure_type:
22615 case DW_TAG_union_type:
22616 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22617 structures or unions. These were of the form "._%d" in GCC 4.1,
22618 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22619 and GCC 4.4. We work around this problem by ignoring these. */
22620 if (attr && DW_STRING (attr)
22621 && (startswith (DW_STRING (attr), "._")
22622 || startswith (DW_STRING (attr), "<anonymous")))
22623 return NULL;
22624
22625 /* GCC might emit a nameless typedef that has a linkage name. See
22626 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22627 if (!attr || DW_STRING (attr) == NULL)
22628 {
22629 attr = dw2_linkage_name_attr (die, cu);
22630 if (attr == NULL || DW_STRING (attr) == NULL)
22631 return NULL;
22632
22633 /* Avoid demangling DW_STRING (attr) the second time on a second
22634 call for the same DIE. */
22635 if (!DW_STRING_IS_CANONICAL (attr))
22636 {
22637 gdb::unique_xmalloc_ptr<char> demangled
22638 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
22639 if (demangled == nullptr)
22640 return nullptr;
22641
22642 DW_STRING (attr) = objfile->intern (demangled.get ());
22643 DW_STRING_IS_CANONICAL (attr) = 1;
22644 }
22645
22646 /* Strip any leading namespaces/classes, keep only the base name.
22647 DW_AT_name for named DIEs does not contain the prefixes. */
22648 const char *base = strrchr (DW_STRING (attr), ':');
22649 if (base && base > DW_STRING (attr) && base[-1] == ':')
22650 return &base[1];
22651 else
22652 return DW_STRING (attr);
22653 }
22654 break;
22655
22656 default:
22657 break;
22658 }
22659
22660 if (!DW_STRING_IS_CANONICAL (attr))
22661 {
22662 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22663 objfile);
22664 DW_STRING_IS_CANONICAL (attr) = 1;
22665 }
22666 return DW_STRING (attr);
22667 }
22668
22669 /* Return the die that this die in an extension of, or NULL if there
22670 is none. *EXT_CU is the CU containing DIE on input, and the CU
22671 containing the return value on output. */
22672
22673 static struct die_info *
22674 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22675 {
22676 struct attribute *attr;
22677
22678 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22679 if (attr == NULL)
22680 return NULL;
22681
22682 return follow_die_ref (die, attr, ext_cu);
22683 }
22684
22685 static void
22686 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22687 {
22688 unsigned int i;
22689
22690 print_spaces (indent, f);
22691 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22692 dwarf_tag_name (die->tag), die->abbrev,
22693 sect_offset_str (die->sect_off));
22694
22695 if (die->parent != NULL)
22696 {
22697 print_spaces (indent, f);
22698 fprintf_unfiltered (f, " parent at offset: %s\n",
22699 sect_offset_str (die->parent->sect_off));
22700 }
22701
22702 print_spaces (indent, f);
22703 fprintf_unfiltered (f, " has children: %s\n",
22704 dwarf_bool_name (die->child != NULL));
22705
22706 print_spaces (indent, f);
22707 fprintf_unfiltered (f, " attributes:\n");
22708
22709 for (i = 0; i < die->num_attrs; ++i)
22710 {
22711 print_spaces (indent, f);
22712 fprintf_unfiltered (f, " %s (%s) ",
22713 dwarf_attr_name (die->attrs[i].name),
22714 dwarf_form_name (die->attrs[i].form));
22715
22716 switch (die->attrs[i].form)
22717 {
22718 case DW_FORM_addr:
22719 case DW_FORM_addrx:
22720 case DW_FORM_GNU_addr_index:
22721 fprintf_unfiltered (f, "address: ");
22722 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22723 break;
22724 case DW_FORM_block2:
22725 case DW_FORM_block4:
22726 case DW_FORM_block:
22727 case DW_FORM_block1:
22728 fprintf_unfiltered (f, "block: size %s",
22729 pulongest (DW_BLOCK (&die->attrs[i])->size));
22730 break;
22731 case DW_FORM_exprloc:
22732 fprintf_unfiltered (f, "expression: size %s",
22733 pulongest (DW_BLOCK (&die->attrs[i])->size));
22734 break;
22735 case DW_FORM_data16:
22736 fprintf_unfiltered (f, "constant of 16 bytes");
22737 break;
22738 case DW_FORM_ref_addr:
22739 fprintf_unfiltered (f, "ref address: ");
22740 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22741 break;
22742 case DW_FORM_GNU_ref_alt:
22743 fprintf_unfiltered (f, "alt ref address: ");
22744 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22745 break;
22746 case DW_FORM_ref1:
22747 case DW_FORM_ref2:
22748 case DW_FORM_ref4:
22749 case DW_FORM_ref8:
22750 case DW_FORM_ref_udata:
22751 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22752 (long) (DW_UNSND (&die->attrs[i])));
22753 break;
22754 case DW_FORM_data1:
22755 case DW_FORM_data2:
22756 case DW_FORM_data4:
22757 case DW_FORM_data8:
22758 case DW_FORM_udata:
22759 case DW_FORM_sdata:
22760 fprintf_unfiltered (f, "constant: %s",
22761 pulongest (DW_UNSND (&die->attrs[i])));
22762 break;
22763 case DW_FORM_sec_offset:
22764 fprintf_unfiltered (f, "section offset: %s",
22765 pulongest (DW_UNSND (&die->attrs[i])));
22766 break;
22767 case DW_FORM_ref_sig8:
22768 fprintf_unfiltered (f, "signature: %s",
22769 hex_string (DW_SIGNATURE (&die->attrs[i])));
22770 break;
22771 case DW_FORM_string:
22772 case DW_FORM_strp:
22773 case DW_FORM_line_strp:
22774 case DW_FORM_strx:
22775 case DW_FORM_GNU_str_index:
22776 case DW_FORM_GNU_strp_alt:
22777 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22778 DW_STRING (&die->attrs[i])
22779 ? DW_STRING (&die->attrs[i]) : "",
22780 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22781 break;
22782 case DW_FORM_flag:
22783 if (DW_UNSND (&die->attrs[i]))
22784 fprintf_unfiltered (f, "flag: TRUE");
22785 else
22786 fprintf_unfiltered (f, "flag: FALSE");
22787 break;
22788 case DW_FORM_flag_present:
22789 fprintf_unfiltered (f, "flag: TRUE");
22790 break;
22791 case DW_FORM_indirect:
22792 /* The reader will have reduced the indirect form to
22793 the "base form" so this form should not occur. */
22794 fprintf_unfiltered (f,
22795 "unexpected attribute form: DW_FORM_indirect");
22796 break;
22797 case DW_FORM_implicit_const:
22798 fprintf_unfiltered (f, "constant: %s",
22799 plongest (DW_SND (&die->attrs[i])));
22800 break;
22801 default:
22802 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22803 die->attrs[i].form);
22804 break;
22805 }
22806 fprintf_unfiltered (f, "\n");
22807 }
22808 }
22809
22810 static void
22811 dump_die_for_error (struct die_info *die)
22812 {
22813 dump_die_shallow (gdb_stderr, 0, die);
22814 }
22815
22816 static void
22817 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22818 {
22819 int indent = level * 4;
22820
22821 gdb_assert (die != NULL);
22822
22823 if (level >= max_level)
22824 return;
22825
22826 dump_die_shallow (f, indent, die);
22827
22828 if (die->child != NULL)
22829 {
22830 print_spaces (indent, f);
22831 fprintf_unfiltered (f, " Children:");
22832 if (level + 1 < max_level)
22833 {
22834 fprintf_unfiltered (f, "\n");
22835 dump_die_1 (f, level + 1, max_level, die->child);
22836 }
22837 else
22838 {
22839 fprintf_unfiltered (f,
22840 " [not printed, max nesting level reached]\n");
22841 }
22842 }
22843
22844 if (die->sibling != NULL && level > 0)
22845 {
22846 dump_die_1 (f, level, max_level, die->sibling);
22847 }
22848 }
22849
22850 /* This is called from the pdie macro in gdbinit.in.
22851 It's not static so gcc will keep a copy callable from gdb. */
22852
22853 void
22854 dump_die (struct die_info *die, int max_level)
22855 {
22856 dump_die_1 (gdb_stdlog, 0, max_level, die);
22857 }
22858
22859 static void
22860 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22861 {
22862 void **slot;
22863
22864 slot = htab_find_slot_with_hash (cu->die_hash, die,
22865 to_underlying (die->sect_off),
22866 INSERT);
22867
22868 *slot = die;
22869 }
22870
22871 /* Follow reference or signature attribute ATTR of SRC_DIE.
22872 On entry *REF_CU is the CU of SRC_DIE.
22873 On exit *REF_CU is the CU of the result. */
22874
22875 static struct die_info *
22876 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22877 struct dwarf2_cu **ref_cu)
22878 {
22879 struct die_info *die;
22880
22881 if (attr->form_is_ref ())
22882 die = follow_die_ref (src_die, attr, ref_cu);
22883 else if (attr->form == DW_FORM_ref_sig8)
22884 die = follow_die_sig (src_die, attr, ref_cu);
22885 else
22886 {
22887 dump_die_for_error (src_die);
22888 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22889 objfile_name ((*ref_cu)->per_objfile->objfile));
22890 }
22891
22892 return die;
22893 }
22894
22895 /* Follow reference OFFSET.
22896 On entry *REF_CU is the CU of the source die referencing OFFSET.
22897 On exit *REF_CU is the CU of the result.
22898 Returns NULL if OFFSET is invalid. */
22899
22900 static struct die_info *
22901 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22902 struct dwarf2_cu **ref_cu)
22903 {
22904 struct die_info temp_die;
22905 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22906 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22907
22908 gdb_assert (cu->per_cu != NULL);
22909
22910 target_cu = cu;
22911
22912 if (cu->per_cu->is_debug_types)
22913 {
22914 /* .debug_types CUs cannot reference anything outside their CU.
22915 If they need to, they have to reference a signatured type via
22916 DW_FORM_ref_sig8. */
22917 if (!cu->header.offset_in_cu_p (sect_off))
22918 return NULL;
22919 }
22920 else if (offset_in_dwz != cu->per_cu->is_dwz
22921 || !cu->header.offset_in_cu_p (sect_off))
22922 {
22923 struct dwarf2_per_cu_data *per_cu;
22924
22925 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22926 per_objfile);
22927
22928 /* If necessary, add it to the queue and load its DIEs. */
22929 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
22930 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
22931
22932 target_cu = per_objfile->get_cu (per_cu);
22933 }
22934 else if (cu->dies == NULL)
22935 {
22936 /* We're loading full DIEs during partial symbol reading. */
22937 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
22938 load_full_comp_unit (cu->per_cu, per_objfile, false, language_minimal);
22939 }
22940
22941 *ref_cu = target_cu;
22942 temp_die.sect_off = sect_off;
22943
22944 if (target_cu != cu)
22945 target_cu->ancestor = cu;
22946
22947 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22948 &temp_die,
22949 to_underlying (sect_off));
22950 }
22951
22952 /* Follow reference attribute ATTR of SRC_DIE.
22953 On entry *REF_CU is the CU of SRC_DIE.
22954 On exit *REF_CU is the CU of the result. */
22955
22956 static struct die_info *
22957 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22958 struct dwarf2_cu **ref_cu)
22959 {
22960 sect_offset sect_off = attr->get_ref_die_offset ();
22961 struct dwarf2_cu *cu = *ref_cu;
22962 struct die_info *die;
22963
22964 die = follow_die_offset (sect_off,
22965 (attr->form == DW_FORM_GNU_ref_alt
22966 || cu->per_cu->is_dwz),
22967 ref_cu);
22968 if (!die)
22969 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22970 "at %s [in module %s]"),
22971 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22972 objfile_name (cu->per_objfile->objfile));
22973
22974 return die;
22975 }
22976
22977 /* See read.h. */
22978
22979 struct dwarf2_locexpr_baton
22980 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22981 dwarf2_per_cu_data *per_cu,
22982 dwarf2_per_objfile *per_objfile,
22983 gdb::function_view<CORE_ADDR ()> get_frame_pc,
22984 bool resolve_abstract_p)
22985 {
22986 struct die_info *die;
22987 struct attribute *attr;
22988 struct dwarf2_locexpr_baton retval;
22989 struct objfile *objfile = per_objfile->objfile;
22990
22991 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22992 if (cu == nullptr)
22993 cu = load_cu (per_cu, per_objfile, false);
22994
22995 if (cu == nullptr)
22996 {
22997 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22998 Instead just throw an error, not much else we can do. */
22999 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23000 sect_offset_str (sect_off), objfile_name (objfile));
23001 }
23002
23003 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23004 if (!die)
23005 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23006 sect_offset_str (sect_off), objfile_name (objfile));
23007
23008 attr = dwarf2_attr (die, DW_AT_location, cu);
23009 if (!attr && resolve_abstract_p
23010 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23011 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23012 {
23013 CORE_ADDR pc = get_frame_pc ();
23014 CORE_ADDR baseaddr = objfile->text_section_offset ();
23015 struct gdbarch *gdbarch = objfile->arch ();
23016
23017 for (const auto &cand_off
23018 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23019 {
23020 struct dwarf2_cu *cand_cu = cu;
23021 struct die_info *cand
23022 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23023 if (!cand
23024 || !cand->parent
23025 || cand->parent->tag != DW_TAG_subprogram)
23026 continue;
23027
23028 CORE_ADDR pc_low, pc_high;
23029 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23030 if (pc_low == ((CORE_ADDR) -1))
23031 continue;
23032 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23033 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23034 if (!(pc_low <= pc && pc < pc_high))
23035 continue;
23036
23037 die = cand;
23038 attr = dwarf2_attr (die, DW_AT_location, cu);
23039 break;
23040 }
23041 }
23042
23043 if (!attr)
23044 {
23045 /* DWARF: "If there is no such attribute, then there is no effect.".
23046 DATA is ignored if SIZE is 0. */
23047
23048 retval.data = NULL;
23049 retval.size = 0;
23050 }
23051 else if (attr->form_is_section_offset ())
23052 {
23053 struct dwarf2_loclist_baton loclist_baton;
23054 CORE_ADDR pc = get_frame_pc ();
23055 size_t size;
23056
23057 fill_in_loclist_baton (cu, &loclist_baton, attr);
23058
23059 retval.data = dwarf2_find_location_expression (&loclist_baton,
23060 &size, pc);
23061 retval.size = size;
23062 }
23063 else
23064 {
23065 if (!attr->form_is_block ())
23066 error (_("Dwarf Error: DIE at %s referenced in module %s "
23067 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23068 sect_offset_str (sect_off), objfile_name (objfile));
23069
23070 retval.data = DW_BLOCK (attr)->data;
23071 retval.size = DW_BLOCK (attr)->size;
23072 }
23073 retval.per_objfile = per_objfile;
23074 retval.per_cu = cu->per_cu;
23075
23076 per_objfile->age_comp_units ();
23077
23078 return retval;
23079 }
23080
23081 /* See read.h. */
23082
23083 struct dwarf2_locexpr_baton
23084 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23085 dwarf2_per_cu_data *per_cu,
23086 dwarf2_per_objfile *per_objfile,
23087 gdb::function_view<CORE_ADDR ()> get_frame_pc)
23088 {
23089 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23090
23091 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
23092 get_frame_pc);
23093 }
23094
23095 /* Write a constant of a given type as target-ordered bytes into
23096 OBSTACK. */
23097
23098 static const gdb_byte *
23099 write_constant_as_bytes (struct obstack *obstack,
23100 enum bfd_endian byte_order,
23101 struct type *type,
23102 ULONGEST value,
23103 LONGEST *len)
23104 {
23105 gdb_byte *result;
23106
23107 *len = TYPE_LENGTH (type);
23108 result = (gdb_byte *) obstack_alloc (obstack, *len);
23109 store_unsigned_integer (result, *len, byte_order, value);
23110
23111 return result;
23112 }
23113
23114 /* See read.h. */
23115
23116 const gdb_byte *
23117 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23118 dwarf2_per_cu_data *per_cu,
23119 dwarf2_per_objfile *per_objfile,
23120 obstack *obstack,
23121 LONGEST *len)
23122 {
23123 struct die_info *die;
23124 struct attribute *attr;
23125 const gdb_byte *result = NULL;
23126 struct type *type;
23127 LONGEST value;
23128 enum bfd_endian byte_order;
23129 struct objfile *objfile = per_objfile->objfile;
23130
23131 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23132 if (cu == nullptr)
23133 cu = load_cu (per_cu, per_objfile, false);
23134
23135 if (cu == nullptr)
23136 {
23137 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23138 Instead just throw an error, not much else we can do. */
23139 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23140 sect_offset_str (sect_off), objfile_name (objfile));
23141 }
23142
23143 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23144 if (!die)
23145 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23146 sect_offset_str (sect_off), objfile_name (objfile));
23147
23148 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23149 if (attr == NULL)
23150 return NULL;
23151
23152 byte_order = (bfd_big_endian (objfile->obfd)
23153 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23154
23155 switch (attr->form)
23156 {
23157 case DW_FORM_addr:
23158 case DW_FORM_addrx:
23159 case DW_FORM_GNU_addr_index:
23160 {
23161 gdb_byte *tem;
23162
23163 *len = cu->header.addr_size;
23164 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23165 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23166 result = tem;
23167 }
23168 break;
23169 case DW_FORM_string:
23170 case DW_FORM_strp:
23171 case DW_FORM_strx:
23172 case DW_FORM_GNU_str_index:
23173 case DW_FORM_GNU_strp_alt:
23174 /* DW_STRING is already allocated on the objfile obstack, point
23175 directly to it. */
23176 result = (const gdb_byte *) DW_STRING (attr);
23177 *len = strlen (DW_STRING (attr));
23178 break;
23179 case DW_FORM_block1:
23180 case DW_FORM_block2:
23181 case DW_FORM_block4:
23182 case DW_FORM_block:
23183 case DW_FORM_exprloc:
23184 case DW_FORM_data16:
23185 result = DW_BLOCK (attr)->data;
23186 *len = DW_BLOCK (attr)->size;
23187 break;
23188
23189 /* The DW_AT_const_value attributes are supposed to carry the
23190 symbol's value "represented as it would be on the target
23191 architecture." By the time we get here, it's already been
23192 converted to host endianness, so we just need to sign- or
23193 zero-extend it as appropriate. */
23194 case DW_FORM_data1:
23195 type = die_type (die, cu);
23196 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23197 if (result == NULL)
23198 result = write_constant_as_bytes (obstack, byte_order,
23199 type, value, len);
23200 break;
23201 case DW_FORM_data2:
23202 type = die_type (die, cu);
23203 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23204 if (result == NULL)
23205 result = write_constant_as_bytes (obstack, byte_order,
23206 type, value, len);
23207 break;
23208 case DW_FORM_data4:
23209 type = die_type (die, cu);
23210 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23211 if (result == NULL)
23212 result = write_constant_as_bytes (obstack, byte_order,
23213 type, value, len);
23214 break;
23215 case DW_FORM_data8:
23216 type = die_type (die, cu);
23217 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23218 if (result == NULL)
23219 result = write_constant_as_bytes (obstack, byte_order,
23220 type, value, len);
23221 break;
23222
23223 case DW_FORM_sdata:
23224 case DW_FORM_implicit_const:
23225 type = die_type (die, cu);
23226 result = write_constant_as_bytes (obstack, byte_order,
23227 type, DW_SND (attr), len);
23228 break;
23229
23230 case DW_FORM_udata:
23231 type = die_type (die, cu);
23232 result = write_constant_as_bytes (obstack, byte_order,
23233 type, DW_UNSND (attr), len);
23234 break;
23235
23236 default:
23237 complaint (_("unsupported const value attribute form: '%s'"),
23238 dwarf_form_name (attr->form));
23239 break;
23240 }
23241
23242 return result;
23243 }
23244
23245 /* See read.h. */
23246
23247 struct type *
23248 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23249 dwarf2_per_cu_data *per_cu,
23250 dwarf2_per_objfile *per_objfile)
23251 {
23252 struct die_info *die;
23253
23254 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23255 if (cu == nullptr)
23256 cu = load_cu (per_cu, per_objfile, false);
23257
23258 if (cu == nullptr)
23259 return nullptr;
23260
23261 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23262 if (!die)
23263 return NULL;
23264
23265 return die_type (die, cu);
23266 }
23267
23268 /* See read.h. */
23269
23270 struct type *
23271 dwarf2_get_die_type (cu_offset die_offset,
23272 dwarf2_per_cu_data *per_cu,
23273 dwarf2_per_objfile *per_objfile)
23274 {
23275 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23276 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
23277 }
23278
23279 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23280 On entry *REF_CU is the CU of SRC_DIE.
23281 On exit *REF_CU is the CU of the result.
23282 Returns NULL if the referenced DIE isn't found. */
23283
23284 static struct die_info *
23285 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23286 struct dwarf2_cu **ref_cu)
23287 {
23288 struct die_info temp_die;
23289 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23290 struct die_info *die;
23291 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
23292
23293
23294 /* While it might be nice to assert sig_type->type == NULL here,
23295 we can get here for DW_AT_imported_declaration where we need
23296 the DIE not the type. */
23297
23298 /* If necessary, add it to the queue and load its DIEs. */
23299
23300 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
23301 language_minimal))
23302 read_signatured_type (sig_type, per_objfile);
23303
23304 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
23305 gdb_assert (sig_cu != NULL);
23306 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23307 temp_die.sect_off = sig_type->type_offset_in_section;
23308 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23309 to_underlying (temp_die.sect_off));
23310 if (die)
23311 {
23312 /* For .gdb_index version 7 keep track of included TUs.
23313 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23314 if (per_objfile->per_bfd->index_table != NULL
23315 && per_objfile->per_bfd->index_table->version <= 7)
23316 {
23317 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23318 }
23319
23320 *ref_cu = sig_cu;
23321 if (sig_cu != cu)
23322 sig_cu->ancestor = cu;
23323
23324 return die;
23325 }
23326
23327 return NULL;
23328 }
23329
23330 /* Follow signatured type referenced by ATTR in SRC_DIE.
23331 On entry *REF_CU is the CU of SRC_DIE.
23332 On exit *REF_CU is the CU of the result.
23333 The result is the DIE of the type.
23334 If the referenced type cannot be found an error is thrown. */
23335
23336 static struct die_info *
23337 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23338 struct dwarf2_cu **ref_cu)
23339 {
23340 ULONGEST signature = DW_SIGNATURE (attr);
23341 struct signatured_type *sig_type;
23342 struct die_info *die;
23343
23344 gdb_assert (attr->form == DW_FORM_ref_sig8);
23345
23346 sig_type = lookup_signatured_type (*ref_cu, signature);
23347 /* sig_type will be NULL if the signatured type is missing from
23348 the debug info. */
23349 if (sig_type == NULL)
23350 {
23351 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23352 " from DIE at %s [in module %s]"),
23353 hex_string (signature), sect_offset_str (src_die->sect_off),
23354 objfile_name ((*ref_cu)->per_objfile->objfile));
23355 }
23356
23357 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23358 if (die == NULL)
23359 {
23360 dump_die_for_error (src_die);
23361 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23362 " from DIE at %s [in module %s]"),
23363 hex_string (signature), sect_offset_str (src_die->sect_off),
23364 objfile_name ((*ref_cu)->per_objfile->objfile));
23365 }
23366
23367 return die;
23368 }
23369
23370 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23371 reading in and processing the type unit if necessary. */
23372
23373 static struct type *
23374 get_signatured_type (struct die_info *die, ULONGEST signature,
23375 struct dwarf2_cu *cu)
23376 {
23377 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23378 struct signatured_type *sig_type;
23379 struct dwarf2_cu *type_cu;
23380 struct die_info *type_die;
23381 struct type *type;
23382
23383 sig_type = lookup_signatured_type (cu, signature);
23384 /* sig_type will be NULL if the signatured type is missing from
23385 the debug info. */
23386 if (sig_type == NULL)
23387 {
23388 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23389 " from DIE at %s [in module %s]"),
23390 hex_string (signature), sect_offset_str (die->sect_off),
23391 objfile_name (per_objfile->objfile));
23392 return build_error_marker_type (cu, die);
23393 }
23394
23395 /* If we already know the type we're done. */
23396 type = per_objfile->get_type_for_signatured_type (sig_type);
23397 if (type != nullptr)
23398 return type;
23399
23400 type_cu = cu;
23401 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23402 if (type_die != NULL)
23403 {
23404 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23405 is created. This is important, for example, because for c++ classes
23406 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23407 type = read_type_die (type_die, type_cu);
23408 if (type == NULL)
23409 {
23410 complaint (_("Dwarf Error: Cannot build signatured type %s"
23411 " referenced from DIE at %s [in module %s]"),
23412 hex_string (signature), sect_offset_str (die->sect_off),
23413 objfile_name (per_objfile->objfile));
23414 type = build_error_marker_type (cu, die);
23415 }
23416 }
23417 else
23418 {
23419 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23420 " 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 per_objfile->set_type_for_signatured_type (sig_type, type);
23427
23428 return type;
23429 }
23430
23431 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23432 reading in and processing the type unit if necessary. */
23433
23434 static struct type *
23435 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23436 struct dwarf2_cu *cu) /* ARI: editCase function */
23437 {
23438 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23439 if (attr->form_is_ref ())
23440 {
23441 struct dwarf2_cu *type_cu = cu;
23442 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23443
23444 return read_type_die (type_die, type_cu);
23445 }
23446 else if (attr->form == DW_FORM_ref_sig8)
23447 {
23448 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23449 }
23450 else
23451 {
23452 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23453
23454 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23455 " at %s [in module %s]"),
23456 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23457 objfile_name (per_objfile->objfile));
23458 return build_error_marker_type (cu, die);
23459 }
23460 }
23461
23462 /* Load the DIEs associated with type unit PER_CU into memory. */
23463
23464 static void
23465 load_full_type_unit (dwarf2_per_cu_data *per_cu,
23466 dwarf2_per_objfile *per_objfile)
23467 {
23468 struct signatured_type *sig_type;
23469
23470 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23471 gdb_assert (! per_cu->type_unit_group_p ());
23472
23473 /* We have the per_cu, but we need the signatured_type.
23474 Fortunately this is an easy translation. */
23475 gdb_assert (per_cu->is_debug_types);
23476 sig_type = (struct signatured_type *) per_cu;
23477
23478 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
23479
23480 read_signatured_type (sig_type, per_objfile);
23481
23482 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
23483 }
23484
23485 /* Read in a signatured type and build its CU and DIEs.
23486 If the type is a stub for the real type in a DWO file,
23487 read in the real type from the DWO file as well. */
23488
23489 static void
23490 read_signatured_type (signatured_type *sig_type,
23491 dwarf2_per_objfile *per_objfile)
23492 {
23493 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23494
23495 gdb_assert (per_cu->is_debug_types);
23496 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
23497
23498 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
23499
23500 if (!reader.dummy_p)
23501 {
23502 struct dwarf2_cu *cu = reader.cu;
23503 const gdb_byte *info_ptr = reader.info_ptr;
23504
23505 gdb_assert (cu->die_hash == NULL);
23506 cu->die_hash =
23507 htab_create_alloc_ex (cu->header.length / 12,
23508 die_hash,
23509 die_eq,
23510 NULL,
23511 &cu->comp_unit_obstack,
23512 hashtab_obstack_allocate,
23513 dummy_obstack_deallocate);
23514
23515 if (reader.comp_unit_die->has_children)
23516 reader.comp_unit_die->child
23517 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23518 reader.comp_unit_die);
23519 cu->dies = reader.comp_unit_die;
23520 /* comp_unit_die is not stored in die_hash, no need. */
23521
23522 /* We try not to read any attributes in this function, because
23523 not all CUs needed for references have been loaded yet, and
23524 symbol table processing isn't initialized. But we have to
23525 set the CU language, or we won't be able to build types
23526 correctly. Similarly, if we do not read the producer, we can
23527 not apply producer-specific interpretation. */
23528 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23529
23530 reader.keep ();
23531 }
23532
23533 sig_type->per_cu.tu_read = 1;
23534 }
23535
23536 /* Decode simple location descriptions.
23537 Given a pointer to a dwarf block that defines a location, compute
23538 the location and return the value. If COMPUTED is non-null, it is
23539 set to true to indicate that decoding was successful, and false
23540 otherwise. If COMPUTED is null, then this function may emit a
23541 complaint. */
23542
23543 static CORE_ADDR
23544 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
23545 {
23546 struct objfile *objfile = cu->per_objfile->objfile;
23547 size_t i;
23548 size_t size = blk->size;
23549 const gdb_byte *data = blk->data;
23550 CORE_ADDR stack[64];
23551 int stacki;
23552 unsigned int bytes_read, unsnd;
23553 gdb_byte op;
23554
23555 if (computed != nullptr)
23556 *computed = false;
23557
23558 i = 0;
23559 stacki = 0;
23560 stack[stacki] = 0;
23561 stack[++stacki] = 0;
23562
23563 while (i < size)
23564 {
23565 op = data[i++];
23566 switch (op)
23567 {
23568 case DW_OP_lit0:
23569 case DW_OP_lit1:
23570 case DW_OP_lit2:
23571 case DW_OP_lit3:
23572 case DW_OP_lit4:
23573 case DW_OP_lit5:
23574 case DW_OP_lit6:
23575 case DW_OP_lit7:
23576 case DW_OP_lit8:
23577 case DW_OP_lit9:
23578 case DW_OP_lit10:
23579 case DW_OP_lit11:
23580 case DW_OP_lit12:
23581 case DW_OP_lit13:
23582 case DW_OP_lit14:
23583 case DW_OP_lit15:
23584 case DW_OP_lit16:
23585 case DW_OP_lit17:
23586 case DW_OP_lit18:
23587 case DW_OP_lit19:
23588 case DW_OP_lit20:
23589 case DW_OP_lit21:
23590 case DW_OP_lit22:
23591 case DW_OP_lit23:
23592 case DW_OP_lit24:
23593 case DW_OP_lit25:
23594 case DW_OP_lit26:
23595 case DW_OP_lit27:
23596 case DW_OP_lit28:
23597 case DW_OP_lit29:
23598 case DW_OP_lit30:
23599 case DW_OP_lit31:
23600 stack[++stacki] = op - DW_OP_lit0;
23601 break;
23602
23603 case DW_OP_reg0:
23604 case DW_OP_reg1:
23605 case DW_OP_reg2:
23606 case DW_OP_reg3:
23607 case DW_OP_reg4:
23608 case DW_OP_reg5:
23609 case DW_OP_reg6:
23610 case DW_OP_reg7:
23611 case DW_OP_reg8:
23612 case DW_OP_reg9:
23613 case DW_OP_reg10:
23614 case DW_OP_reg11:
23615 case DW_OP_reg12:
23616 case DW_OP_reg13:
23617 case DW_OP_reg14:
23618 case DW_OP_reg15:
23619 case DW_OP_reg16:
23620 case DW_OP_reg17:
23621 case DW_OP_reg18:
23622 case DW_OP_reg19:
23623 case DW_OP_reg20:
23624 case DW_OP_reg21:
23625 case DW_OP_reg22:
23626 case DW_OP_reg23:
23627 case DW_OP_reg24:
23628 case DW_OP_reg25:
23629 case DW_OP_reg26:
23630 case DW_OP_reg27:
23631 case DW_OP_reg28:
23632 case DW_OP_reg29:
23633 case DW_OP_reg30:
23634 case DW_OP_reg31:
23635 stack[++stacki] = op - DW_OP_reg0;
23636 if (i < size)
23637 {
23638 if (computed == nullptr)
23639 dwarf2_complex_location_expr_complaint ();
23640 else
23641 return 0;
23642 }
23643 break;
23644
23645 case DW_OP_regx:
23646 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23647 i += bytes_read;
23648 stack[++stacki] = unsnd;
23649 if (i < size)
23650 {
23651 if (computed == nullptr)
23652 dwarf2_complex_location_expr_complaint ();
23653 else
23654 return 0;
23655 }
23656 break;
23657
23658 case DW_OP_addr:
23659 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23660 &bytes_read);
23661 i += bytes_read;
23662 break;
23663
23664 case DW_OP_const1u:
23665 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23666 i += 1;
23667 break;
23668
23669 case DW_OP_const1s:
23670 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23671 i += 1;
23672 break;
23673
23674 case DW_OP_const2u:
23675 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23676 i += 2;
23677 break;
23678
23679 case DW_OP_const2s:
23680 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23681 i += 2;
23682 break;
23683
23684 case DW_OP_const4u:
23685 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23686 i += 4;
23687 break;
23688
23689 case DW_OP_const4s:
23690 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23691 i += 4;
23692 break;
23693
23694 case DW_OP_const8u:
23695 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23696 i += 8;
23697 break;
23698
23699 case DW_OP_constu:
23700 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23701 &bytes_read);
23702 i += bytes_read;
23703 break;
23704
23705 case DW_OP_consts:
23706 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23707 i += bytes_read;
23708 break;
23709
23710 case DW_OP_dup:
23711 stack[stacki + 1] = stack[stacki];
23712 stacki++;
23713 break;
23714
23715 case DW_OP_plus:
23716 stack[stacki - 1] += stack[stacki];
23717 stacki--;
23718 break;
23719
23720 case DW_OP_plus_uconst:
23721 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23722 &bytes_read);
23723 i += bytes_read;
23724 break;
23725
23726 case DW_OP_minus:
23727 stack[stacki - 1] -= stack[stacki];
23728 stacki--;
23729 break;
23730
23731 case DW_OP_deref:
23732 /* If we're not the last op, then we definitely can't encode
23733 this using GDB's address_class enum. This is valid for partial
23734 global symbols, although the variable's address will be bogus
23735 in the psymtab. */
23736 if (i < size)
23737 {
23738 if (computed == nullptr)
23739 dwarf2_complex_location_expr_complaint ();
23740 else
23741 return 0;
23742 }
23743 break;
23744
23745 case DW_OP_GNU_push_tls_address:
23746 case DW_OP_form_tls_address:
23747 /* The top of the stack has the offset from the beginning
23748 of the thread control block at which the variable is located. */
23749 /* Nothing should follow this operator, so the top of stack would
23750 be returned. */
23751 /* This is valid for partial global symbols, but the variable's
23752 address will be bogus in the psymtab. Make it always at least
23753 non-zero to not look as a variable garbage collected by linker
23754 which have DW_OP_addr 0. */
23755 if (i < size)
23756 {
23757 if (computed == nullptr)
23758 dwarf2_complex_location_expr_complaint ();
23759 else
23760 return 0;
23761 }
23762 stack[stacki]++;
23763 break;
23764
23765 case DW_OP_GNU_uninit:
23766 if (computed != nullptr)
23767 return 0;
23768 break;
23769
23770 case DW_OP_addrx:
23771 case DW_OP_GNU_addr_index:
23772 case DW_OP_GNU_const_index:
23773 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23774 &bytes_read);
23775 i += bytes_read;
23776 break;
23777
23778 default:
23779 if (computed == nullptr)
23780 {
23781 const char *name = get_DW_OP_name (op);
23782
23783 if (name)
23784 complaint (_("unsupported stack op: '%s'"),
23785 name);
23786 else
23787 complaint (_("unsupported stack op: '%02x'"),
23788 op);
23789 }
23790
23791 return (stack[stacki]);
23792 }
23793
23794 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23795 outside of the allocated space. Also enforce minimum>0. */
23796 if (stacki >= ARRAY_SIZE (stack) - 1)
23797 {
23798 if (computed == nullptr)
23799 complaint (_("location description stack overflow"));
23800 return 0;
23801 }
23802
23803 if (stacki <= 0)
23804 {
23805 if (computed == nullptr)
23806 complaint (_("location description stack underflow"));
23807 return 0;
23808 }
23809 }
23810
23811 if (computed != nullptr)
23812 *computed = true;
23813 return (stack[stacki]);
23814 }
23815
23816 /* memory allocation interface */
23817
23818 static struct dwarf_block *
23819 dwarf_alloc_block (struct dwarf2_cu *cu)
23820 {
23821 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23822 }
23823
23824 static struct die_info *
23825 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23826 {
23827 struct die_info *die;
23828 size_t size = sizeof (struct die_info);
23829
23830 if (num_attrs > 1)
23831 size += (num_attrs - 1) * sizeof (struct attribute);
23832
23833 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23834 memset (die, 0, sizeof (struct die_info));
23835 return (die);
23836 }
23837
23838 \f
23839
23840 /* Macro support. */
23841
23842 /* An overload of dwarf_decode_macros that finds the correct section
23843 and ensures it is read in before calling the other overload. */
23844
23845 static void
23846 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23847 int section_is_gnu)
23848 {
23849 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23850 struct objfile *objfile = per_objfile->objfile;
23851 const struct line_header *lh = cu->line_header;
23852 unsigned int offset_size = cu->header.offset_size;
23853 struct dwarf2_section_info *section;
23854 const char *section_name;
23855
23856 if (cu->dwo_unit != nullptr)
23857 {
23858 if (section_is_gnu)
23859 {
23860 section = &cu->dwo_unit->dwo_file->sections.macro;
23861 section_name = ".debug_macro.dwo";
23862 }
23863 else
23864 {
23865 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23866 section_name = ".debug_macinfo.dwo";
23867 }
23868 }
23869 else
23870 {
23871 if (section_is_gnu)
23872 {
23873 section = &per_objfile->per_bfd->macro;
23874 section_name = ".debug_macro";
23875 }
23876 else
23877 {
23878 section = &per_objfile->per_bfd->macinfo;
23879 section_name = ".debug_macinfo";
23880 }
23881 }
23882
23883 section->read (objfile);
23884 if (section->buffer == nullptr)
23885 {
23886 complaint (_("missing %s section"), section_name);
23887 return;
23888 }
23889
23890 buildsym_compunit *builder = cu->get_builder ();
23891
23892 dwarf_decode_macros (per_objfile, builder, section, lh,
23893 offset_size, offset, section_is_gnu);
23894 }
23895
23896 /* Return the .debug_loc section to use for CU.
23897 For DWO files use .debug_loc.dwo. */
23898
23899 static struct dwarf2_section_info *
23900 cu_debug_loc_section (struct dwarf2_cu *cu)
23901 {
23902 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23903
23904 if (cu->dwo_unit)
23905 {
23906 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23907
23908 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23909 }
23910 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
23911 : &per_objfile->per_bfd->loc);
23912 }
23913
23914 /* Return the .debug_rnglists section to use for CU. */
23915 static struct dwarf2_section_info *
23916 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
23917 {
23918 if (cu->header.version < 5)
23919 error (_(".debug_rnglists section cannot be used in DWARF %d"),
23920 cu->header.version);
23921 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23922
23923 /* Make sure we read the .debug_rnglists section from the file that
23924 contains the DW_AT_ranges attribute we are reading. Normally that
23925 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
23926 or DW_TAG_skeleton unit, we always want to read from objfile/linked
23927 program. */
23928 if (cu->dwo_unit != nullptr
23929 && tag != DW_TAG_compile_unit
23930 && tag != DW_TAG_skeleton_unit)
23931 {
23932 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23933
23934 if (sections->rnglists.size > 0)
23935 return &sections->rnglists;
23936 else
23937 error (_(".debug_rnglists section is missing from .dwo file."));
23938 }
23939 return &dwarf2_per_objfile->per_bfd->rnglists;
23940 }
23941
23942 /* A helper function that fills in a dwarf2_loclist_baton. */
23943
23944 static void
23945 fill_in_loclist_baton (struct dwarf2_cu *cu,
23946 struct dwarf2_loclist_baton *baton,
23947 const struct attribute *attr)
23948 {
23949 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23950 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23951
23952 section->read (per_objfile->objfile);
23953
23954 baton->per_objfile = per_objfile;
23955 baton->per_cu = cu->per_cu;
23956 gdb_assert (baton->per_cu);
23957 /* We don't know how long the location list is, but make sure we
23958 don't run off the edge of the section. */
23959 baton->size = section->size - DW_UNSND (attr);
23960 baton->data = section->buffer + DW_UNSND (attr);
23961 if (cu->base_address.has_value ())
23962 baton->base_address = *cu->base_address;
23963 else
23964 baton->base_address = 0;
23965 baton->from_dwo = cu->dwo_unit != NULL;
23966 }
23967
23968 static void
23969 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23970 struct dwarf2_cu *cu, int is_block)
23971 {
23972 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23973 struct objfile *objfile = per_objfile->objfile;
23974 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23975
23976 if (attr->form_is_section_offset ()
23977 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23978 the section. If so, fall through to the complaint in the
23979 other branch. */
23980 && DW_UNSND (attr) < section->get_size (objfile))
23981 {
23982 struct dwarf2_loclist_baton *baton;
23983
23984 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23985
23986 fill_in_loclist_baton (cu, baton, attr);
23987
23988 if (!cu->base_address.has_value ())
23989 complaint (_("Location list used without "
23990 "specifying the CU base address."));
23991
23992 SYMBOL_ACLASS_INDEX (sym) = (is_block
23993 ? dwarf2_loclist_block_index
23994 : dwarf2_loclist_index);
23995 SYMBOL_LOCATION_BATON (sym) = baton;
23996 }
23997 else
23998 {
23999 struct dwarf2_locexpr_baton *baton;
24000
24001 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24002 baton->per_objfile = per_objfile;
24003 baton->per_cu = cu->per_cu;
24004 gdb_assert (baton->per_cu);
24005
24006 if (attr->form_is_block ())
24007 {
24008 /* Note that we're just copying the block's data pointer
24009 here, not the actual data. We're still pointing into the
24010 info_buffer for SYM's objfile; right now we never release
24011 that buffer, but when we do clean up properly this may
24012 need to change. */
24013 baton->size = DW_BLOCK (attr)->size;
24014 baton->data = DW_BLOCK (attr)->data;
24015 }
24016 else
24017 {
24018 dwarf2_invalid_attrib_class_complaint ("location description",
24019 sym->natural_name ());
24020 baton->size = 0;
24021 }
24022
24023 SYMBOL_ACLASS_INDEX (sym) = (is_block
24024 ? dwarf2_locexpr_block_index
24025 : dwarf2_locexpr_index);
24026 SYMBOL_LOCATION_BATON (sym) = baton;
24027 }
24028 }
24029
24030 /* See read.h. */
24031
24032 const comp_unit_head *
24033 dwarf2_per_cu_data::get_header () const
24034 {
24035 if (!m_header_read_in)
24036 {
24037 const gdb_byte *info_ptr
24038 = this->section->buffer + to_underlying (this->sect_off);
24039
24040 memset (&m_header, 0, sizeof (m_header));
24041
24042 read_comp_unit_head (&m_header, info_ptr, this->section,
24043 rcuh_kind::COMPILE);
24044 }
24045
24046 return &m_header;
24047 }
24048
24049 /* See read.h. */
24050
24051 int
24052 dwarf2_per_cu_data::addr_size () const
24053 {
24054 return this->get_header ()->addr_size;
24055 }
24056
24057 /* See read.h. */
24058
24059 int
24060 dwarf2_per_cu_data::offset_size () const
24061 {
24062 return this->get_header ()->offset_size;
24063 }
24064
24065 /* See read.h. */
24066
24067 int
24068 dwarf2_per_cu_data::ref_addr_size () const
24069 {
24070 const comp_unit_head *header = this->get_header ();
24071
24072 if (header->version == 2)
24073 return header->addr_size;
24074 else
24075 return header->offset_size;
24076 }
24077
24078 /* See read.h. */
24079
24080 struct type *
24081 dwarf2_cu::addr_type () const
24082 {
24083 struct objfile *objfile = this->per_objfile->objfile;
24084 struct type *void_type = objfile_type (objfile)->builtin_void;
24085 struct type *addr_type = lookup_pointer_type (void_type);
24086 int addr_size = this->per_cu->addr_size ();
24087
24088 if (TYPE_LENGTH (addr_type) == addr_size)
24089 return addr_type;
24090
24091 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
24092 return addr_type;
24093 }
24094
24095 /* A helper function for dwarf2_find_containing_comp_unit that returns
24096 the index of the result, and that searches a vector. It will
24097 return a result even if the offset in question does not actually
24098 occur in any CU. This is separate so that it can be unit
24099 tested. */
24100
24101 static int
24102 dwarf2_find_containing_comp_unit
24103 (sect_offset sect_off,
24104 unsigned int offset_in_dwz,
24105 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
24106 {
24107 int low, high;
24108
24109 low = 0;
24110 high = all_comp_units.size () - 1;
24111 while (high > low)
24112 {
24113 struct dwarf2_per_cu_data *mid_cu;
24114 int mid = low + (high - low) / 2;
24115
24116 mid_cu = all_comp_units[mid];
24117 if (mid_cu->is_dwz > offset_in_dwz
24118 || (mid_cu->is_dwz == offset_in_dwz
24119 && mid_cu->sect_off + mid_cu->length > sect_off))
24120 high = mid;
24121 else
24122 low = mid + 1;
24123 }
24124 gdb_assert (low == high);
24125 return low;
24126 }
24127
24128 /* Locate the .debug_info compilation unit from CU's objfile which contains
24129 the DIE at OFFSET. Raises an error on failure. */
24130
24131 static struct dwarf2_per_cu_data *
24132 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24133 unsigned int offset_in_dwz,
24134 dwarf2_per_objfile *per_objfile)
24135 {
24136 int low = dwarf2_find_containing_comp_unit
24137 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24138 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
24139
24140 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24141 {
24142 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24143 error (_("Dwarf Error: could not find partial DIE containing "
24144 "offset %s [in module %s]"),
24145 sect_offset_str (sect_off),
24146 bfd_get_filename (per_objfile->objfile->obfd));
24147
24148 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
24149 <= sect_off);
24150 return per_objfile->per_bfd->all_comp_units[low-1];
24151 }
24152 else
24153 {
24154 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
24155 && sect_off >= this_cu->sect_off + this_cu->length)
24156 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24157 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24158 return this_cu;
24159 }
24160 }
24161
24162 #if GDB_SELF_TEST
24163
24164 namespace selftests {
24165 namespace find_containing_comp_unit {
24166
24167 static void
24168 run_test ()
24169 {
24170 struct dwarf2_per_cu_data one {};
24171 struct dwarf2_per_cu_data two {};
24172 struct dwarf2_per_cu_data three {};
24173 struct dwarf2_per_cu_data four {};
24174
24175 one.length = 5;
24176 two.sect_off = sect_offset (one.length);
24177 two.length = 7;
24178
24179 three.length = 5;
24180 three.is_dwz = 1;
24181 four.sect_off = sect_offset (three.length);
24182 four.length = 7;
24183 four.is_dwz = 1;
24184
24185 std::vector<dwarf2_per_cu_data *> units;
24186 units.push_back (&one);
24187 units.push_back (&two);
24188 units.push_back (&three);
24189 units.push_back (&four);
24190
24191 int result;
24192
24193 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24194 SELF_CHECK (units[result] == &one);
24195 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24196 SELF_CHECK (units[result] == &one);
24197 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24198 SELF_CHECK (units[result] == &two);
24199
24200 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24201 SELF_CHECK (units[result] == &three);
24202 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24203 SELF_CHECK (units[result] == &three);
24204 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24205 SELF_CHECK (units[result] == &four);
24206 }
24207
24208 }
24209 }
24210
24211 #endif /* GDB_SELF_TEST */
24212
24213 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
24214
24215 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
24216 dwarf2_per_objfile *per_objfile)
24217 : per_cu (per_cu),
24218 per_objfile (per_objfile),
24219 mark (false),
24220 has_loclist (false),
24221 checked_producer (false),
24222 producer_is_gxx_lt_4_6 (false),
24223 producer_is_gcc_lt_4_3 (false),
24224 producer_is_icc (false),
24225 producer_is_icc_lt_14 (false),
24226 producer_is_codewarrior (false),
24227 processing_has_namespace_info (false)
24228 {
24229 }
24230
24231 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24232
24233 static void
24234 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24235 enum language pretend_language)
24236 {
24237 struct attribute *attr;
24238
24239 /* Set the language we're debugging. */
24240 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24241 if (attr != nullptr)
24242 set_cu_language (DW_UNSND (attr), cu);
24243 else
24244 {
24245 cu->language = pretend_language;
24246 cu->language_defn = language_def (cu->language);
24247 }
24248
24249 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24250 }
24251
24252 /* See read.h. */
24253
24254 dwarf2_cu *
24255 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
24256 {
24257 auto it = m_dwarf2_cus.find (per_cu);
24258 if (it == m_dwarf2_cus.end ())
24259 return nullptr;
24260
24261 return it->second;
24262 }
24263
24264 /* See read.h. */
24265
24266 void
24267 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24268 {
24269 gdb_assert (this->get_cu (per_cu) == nullptr);
24270
24271 m_dwarf2_cus[per_cu] = cu;
24272 }
24273
24274 /* See read.h. */
24275
24276 void
24277 dwarf2_per_objfile::age_comp_units ()
24278 {
24279 /* Start by clearing all marks. */
24280 for (auto pair : m_dwarf2_cus)
24281 pair.second->mark = false;
24282
24283 /* Traverse all CUs, mark them and their dependencies if used recently
24284 enough. */
24285 for (auto pair : m_dwarf2_cus)
24286 {
24287 dwarf2_cu *cu = pair.second;
24288
24289 cu->last_used++;
24290 if (cu->last_used <= dwarf_max_cache_age)
24291 dwarf2_mark (cu);
24292 }
24293
24294 /* Delete all CUs still not marked. */
24295 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
24296 {
24297 dwarf2_cu *cu = it->second;
24298
24299 if (!cu->mark)
24300 {
24301 delete cu;
24302 it = m_dwarf2_cus.erase (it);
24303 }
24304 else
24305 it++;
24306 }
24307 }
24308
24309 /* See read.h. */
24310
24311 void
24312 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
24313 {
24314 auto it = m_dwarf2_cus.find (per_cu);
24315 if (it == m_dwarf2_cus.end ())
24316 return;
24317
24318 delete it->second;
24319
24320 m_dwarf2_cus.erase (it);
24321 }
24322
24323 dwarf2_per_objfile::~dwarf2_per_objfile ()
24324 {
24325 remove_all_cus ();
24326 }
24327
24328 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24329 We store these in a hash table separate from the DIEs, and preserve them
24330 when the DIEs are flushed out of cache.
24331
24332 The CU "per_cu" pointer is needed because offset alone is not enough to
24333 uniquely identify the type. A file may have multiple .debug_types sections,
24334 or the type may come from a DWO file. Furthermore, while it's more logical
24335 to use per_cu->section+offset, with Fission the section with the data is in
24336 the DWO file but we don't know that section at the point we need it.
24337 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24338 because we can enter the lookup routine, get_die_type_at_offset, from
24339 outside this file, and thus won't necessarily have PER_CU->cu.
24340 Fortunately, PER_CU is stable for the life of the objfile. */
24341
24342 struct dwarf2_per_cu_offset_and_type
24343 {
24344 const struct dwarf2_per_cu_data *per_cu;
24345 sect_offset sect_off;
24346 struct type *type;
24347 };
24348
24349 /* Hash function for a dwarf2_per_cu_offset_and_type. */
24350
24351 static hashval_t
24352 per_cu_offset_and_type_hash (const void *item)
24353 {
24354 const struct dwarf2_per_cu_offset_and_type *ofs
24355 = (const struct dwarf2_per_cu_offset_and_type *) item;
24356
24357 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
24358 }
24359
24360 /* Equality function for a dwarf2_per_cu_offset_and_type. */
24361
24362 static int
24363 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
24364 {
24365 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24366 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24367 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24368 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
24369
24370 return (ofs_lhs->per_cu == ofs_rhs->per_cu
24371 && ofs_lhs->sect_off == ofs_rhs->sect_off);
24372 }
24373
24374 /* Set the type associated with DIE to TYPE. Save it in CU's hash
24375 table if necessary. For convenience, return TYPE.
24376
24377 The DIEs reading must have careful ordering to:
24378 * Not cause infinite loops trying to read in DIEs as a prerequisite for
24379 reading current DIE.
24380 * Not trying to dereference contents of still incompletely read in types
24381 while reading in other DIEs.
24382 * Enable referencing still incompletely read in types just by a pointer to
24383 the type without accessing its fields.
24384
24385 Therefore caller should follow these rules:
24386 * Try to fetch any prerequisite types we may need to build this DIE type
24387 before building the type and calling set_die_type.
24388 * After building type call set_die_type for current DIE as soon as
24389 possible before fetching more types to complete the current type.
24390 * Make the type as complete as possible before fetching more types. */
24391
24392 static struct type *
24393 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
24394 {
24395 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24396 struct dwarf2_per_cu_offset_and_type **slot, ofs;
24397 struct objfile *objfile = per_objfile->objfile;
24398 struct attribute *attr;
24399 struct dynamic_prop prop;
24400
24401 /* For Ada types, make sure that the gnat-specific data is always
24402 initialized (if not already set). There are a few types where
24403 we should not be doing so, because the type-specific area is
24404 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24405 where the type-specific area is used to store the floatformat).
24406 But this is not a problem, because the gnat-specific information
24407 is actually not needed for these types. */
24408 if (need_gnat_info (cu)
24409 && type->code () != TYPE_CODE_FUNC
24410 && type->code () != TYPE_CODE_FLT
24411 && type->code () != TYPE_CODE_METHODPTR
24412 && type->code () != TYPE_CODE_MEMBERPTR
24413 && type->code () != TYPE_CODE_METHOD
24414 && !HAVE_GNAT_AUX_INFO (type))
24415 INIT_GNAT_SPECIFIC (type);
24416
24417 /* Read DW_AT_allocated and set in type. */
24418 attr = dwarf2_attr (die, DW_AT_allocated, cu);
24419 if (attr != NULL)
24420 {
24421 struct type *prop_type = cu->addr_sized_int_type (false);
24422 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24423 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
24424 }
24425
24426 /* Read DW_AT_associated and set in type. */
24427 attr = dwarf2_attr (die, DW_AT_associated, 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_ASSOCIATED, prop);
24433 }
24434
24435 /* Read DW_AT_data_location and set in type. */
24436 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24437 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
24438 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
24439
24440 if (per_objfile->die_type_hash == NULL)
24441 per_objfile->die_type_hash
24442 = htab_up (htab_create_alloc (127,
24443 per_cu_offset_and_type_hash,
24444 per_cu_offset_and_type_eq,
24445 NULL, xcalloc, xfree));
24446
24447 ofs.per_cu = cu->per_cu;
24448 ofs.sect_off = die->sect_off;
24449 ofs.type = type;
24450 slot = (struct dwarf2_per_cu_offset_and_type **)
24451 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
24452 if (*slot)
24453 complaint (_("A problem internal to GDB: DIE %s has type already set"),
24454 sect_offset_str (die->sect_off));
24455 *slot = XOBNEW (&objfile->objfile_obstack,
24456 struct dwarf2_per_cu_offset_and_type);
24457 **slot = ofs;
24458 return type;
24459 }
24460
24461 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
24462 or return NULL if the die does not have a saved type. */
24463
24464 static struct type *
24465 get_die_type_at_offset (sect_offset sect_off,
24466 dwarf2_per_cu_data *per_cu,
24467 dwarf2_per_objfile *per_objfile)
24468 {
24469 struct dwarf2_per_cu_offset_and_type *slot, ofs;
24470
24471 if (per_objfile->die_type_hash == NULL)
24472 return NULL;
24473
24474 ofs.per_cu = per_cu;
24475 ofs.sect_off = sect_off;
24476 slot = ((struct dwarf2_per_cu_offset_and_type *)
24477 htab_find (per_objfile->die_type_hash.get (), &ofs));
24478 if (slot)
24479 return slot->type;
24480 else
24481 return NULL;
24482 }
24483
24484 /* Look up the type for DIE in CU in die_type_hash,
24485 or return NULL if DIE does not have a saved type. */
24486
24487 static struct type *
24488 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24489 {
24490 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
24491 }
24492
24493 /* Add a dependence relationship from CU to REF_PER_CU. */
24494
24495 static void
24496 dwarf2_add_dependence (struct dwarf2_cu *cu,
24497 struct dwarf2_per_cu_data *ref_per_cu)
24498 {
24499 void **slot;
24500
24501 if (cu->dependencies == NULL)
24502 cu->dependencies
24503 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
24504 NULL, &cu->comp_unit_obstack,
24505 hashtab_obstack_allocate,
24506 dummy_obstack_deallocate);
24507
24508 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
24509 if (*slot == NULL)
24510 *slot = ref_per_cu;
24511 }
24512
24513 /* Subroutine of dwarf2_mark to pass to htab_traverse.
24514 Set the mark field in every compilation unit in the
24515 cache that we must keep because we are keeping CU.
24516
24517 DATA is the dwarf2_per_objfile object in which to look up CUs. */
24518
24519 static int
24520 dwarf2_mark_helper (void **slot, void *data)
24521 {
24522 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
24523 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
24524 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
24525
24526 /* cu->dependencies references may not yet have been ever read if QUIT aborts
24527 reading of the chain. As such dependencies remain valid it is not much
24528 useful to track and undo them during QUIT cleanups. */
24529 if (cu == nullptr)
24530 return 1;
24531
24532 if (cu->mark)
24533 return 1;
24534
24535 cu->mark = true;
24536
24537 if (cu->dependencies != nullptr)
24538 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
24539
24540 return 1;
24541 }
24542
24543 /* Set the mark field in CU and in every other compilation unit in the
24544 cache that we must keep because we are keeping CU. */
24545
24546 static void
24547 dwarf2_mark (struct dwarf2_cu *cu)
24548 {
24549 if (cu->mark)
24550 return;
24551
24552 cu->mark = true;
24553
24554 if (cu->dependencies != nullptr)
24555 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
24556 }
24557
24558 /* Trivial hash function for partial_die_info: the hash value of a DIE
24559 is its offset in .debug_info for this objfile. */
24560
24561 static hashval_t
24562 partial_die_hash (const void *item)
24563 {
24564 const struct partial_die_info *part_die
24565 = (const struct partial_die_info *) item;
24566
24567 return to_underlying (part_die->sect_off);
24568 }
24569
24570 /* Trivial comparison function for partial_die_info structures: two DIEs
24571 are equal if they have the same offset. */
24572
24573 static int
24574 partial_die_eq (const void *item_lhs, const void *item_rhs)
24575 {
24576 const struct partial_die_info *part_die_lhs
24577 = (const struct partial_die_info *) item_lhs;
24578 const struct partial_die_info *part_die_rhs
24579 = (const struct partial_die_info *) item_rhs;
24580
24581 return part_die_lhs->sect_off == part_die_rhs->sect_off;
24582 }
24583
24584 struct cmd_list_element *set_dwarf_cmdlist;
24585 struct cmd_list_element *show_dwarf_cmdlist;
24586
24587 static void
24588 show_check_physname (struct ui_file *file, int from_tty,
24589 struct cmd_list_element *c, const char *value)
24590 {
24591 fprintf_filtered (file,
24592 _("Whether to check \"physname\" is %s.\n"),
24593 value);
24594 }
24595
24596 void _initialize_dwarf2_read ();
24597 void
24598 _initialize_dwarf2_read ()
24599 {
24600 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
24601 Set DWARF specific variables.\n\
24602 Configure DWARF variables such as the cache size."),
24603 &set_dwarf_cmdlist, "maintenance set dwarf ",
24604 0/*allow-unknown*/, &maintenance_set_cmdlist);
24605
24606 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
24607 Show DWARF specific variables.\n\
24608 Show DWARF variables such as the cache size."),
24609 &show_dwarf_cmdlist, "maintenance show dwarf ",
24610 0/*allow-unknown*/, &maintenance_show_cmdlist);
24611
24612 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24613 &dwarf_max_cache_age, _("\
24614 Set the upper bound on the age of cached DWARF compilation units."), _("\
24615 Show the upper bound on the age of cached DWARF compilation units."), _("\
24616 A higher limit means that cached compilation units will be stored\n\
24617 in memory longer, and more total memory will be used. Zero disables\n\
24618 caching, which can slow down startup."),
24619 NULL,
24620 show_dwarf_max_cache_age,
24621 &set_dwarf_cmdlist,
24622 &show_dwarf_cmdlist);
24623
24624 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24625 Set debugging of the DWARF reader."), _("\
24626 Show debugging of the DWARF reader."), _("\
24627 When enabled (non-zero), debugging messages are printed during DWARF\n\
24628 reading and symtab expansion. A value of 1 (one) provides basic\n\
24629 information. A value greater than 1 provides more verbose information."),
24630 NULL,
24631 NULL,
24632 &setdebuglist, &showdebuglist);
24633
24634 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24635 Set debugging of the DWARF DIE reader."), _("\
24636 Show debugging of the DWARF DIE reader."), _("\
24637 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24638 The value is the maximum depth to print."),
24639 NULL,
24640 NULL,
24641 &setdebuglist, &showdebuglist);
24642
24643 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24644 Set debugging of the dwarf line reader."), _("\
24645 Show debugging of the dwarf line reader."), _("\
24646 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24647 A value of 1 (one) provides basic information.\n\
24648 A value greater than 1 provides more verbose information."),
24649 NULL,
24650 NULL,
24651 &setdebuglist, &showdebuglist);
24652
24653 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24654 Set cross-checking of \"physname\" code against demangler."), _("\
24655 Show cross-checking of \"physname\" code against demangler."), _("\
24656 When enabled, GDB's internal \"physname\" code is checked against\n\
24657 the demangler."),
24658 NULL, show_check_physname,
24659 &setdebuglist, &showdebuglist);
24660
24661 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24662 no_class, &use_deprecated_index_sections, _("\
24663 Set whether to use deprecated gdb_index sections."), _("\
24664 Show whether to use deprecated gdb_index sections."), _("\
24665 When enabled, deprecated .gdb_index sections are used anyway.\n\
24666 Normally they are ignored either because of a missing feature or\n\
24667 performance issue.\n\
24668 Warning: This option must be enabled before gdb reads the file."),
24669 NULL,
24670 NULL,
24671 &setlist, &showlist);
24672
24673 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24674 &dwarf2_locexpr_funcs);
24675 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24676 &dwarf2_loclist_funcs);
24677
24678 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24679 &dwarf2_block_frame_base_locexpr_funcs);
24680 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24681 &dwarf2_block_frame_base_loclist_funcs);
24682
24683 #if GDB_SELF_TEST
24684 selftests::register_test ("dw2_expand_symtabs_matching",
24685 selftests::dw2_expand_symtabs_matching::run_test);
24686 selftests::register_test ("dwarf2_find_containing_comp_unit",
24687 selftests::find_containing_comp_unit::run_test);
24688 #endif
24689 }
This page took 0.547196 seconds and 5 git commands to generate.