gdb, gdbserver, gdbsupport: fix leading space vs tabs issues
[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 *);
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 dwarf2_cu *existing_cu,
1610 bool skip_partial,
1611 enum language pretend_language);
1612
1613 static void process_full_comp_unit (dwarf2_cu *cu,
1614 enum language pretend_language);
1615
1616 static void process_full_type_unit (dwarf2_cu *cu,
1617 enum language pretend_language);
1618
1619 static void dwarf2_add_dependence (struct dwarf2_cu *,
1620 struct dwarf2_per_cu_data *);
1621
1622 static void dwarf2_mark (struct dwarf2_cu *);
1623
1624 static struct type *get_die_type_at_offset (sect_offset,
1625 dwarf2_per_cu_data *per_cu,
1626 dwarf2_per_objfile *per_objfile);
1627
1628 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1629
1630 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1631 dwarf2_per_objfile *per_objfile,
1632 enum language pretend_language);
1633
1634 static void process_queue (dwarf2_per_objfile *per_objfile);
1635
1636 /* Class, the destructor of which frees all allocated queue entries. This
1637 will only have work to do if an error was thrown while processing the
1638 dwarf. If no error was thrown then the queue entries should have all
1639 been processed, and freed, as we went along. */
1640
1641 class dwarf2_queue_guard
1642 {
1643 public:
1644 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1645 : m_per_objfile (per_objfile)
1646 {
1647 }
1648
1649 /* Free any entries remaining on the queue. There should only be
1650 entries left if we hit an error while processing the dwarf. */
1651 ~dwarf2_queue_guard ()
1652 {
1653 /* Ensure that no memory is allocated by the queue. */
1654 std::queue<dwarf2_queue_item> empty;
1655 std::swap (m_per_objfile->per_bfd->queue, empty);
1656 }
1657
1658 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1659
1660 private:
1661 dwarf2_per_objfile *m_per_objfile;
1662 };
1663
1664 dwarf2_queue_item::~dwarf2_queue_item ()
1665 {
1666 /* Anything still marked queued is likely to be in an
1667 inconsistent state, so discard it. */
1668 if (per_cu->queued)
1669 {
1670 per_objfile->remove_cu (per_cu);
1671 per_cu->queued = 0;
1672 }
1673 }
1674
1675 /* The return type of find_file_and_directory. Note, the enclosed
1676 string pointers are only valid while this object is valid. */
1677
1678 struct file_and_directory
1679 {
1680 /* The filename. This is never NULL. */
1681 const char *name;
1682
1683 /* The compilation directory. NULL if not known. If we needed to
1684 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1685 points directly to the DW_AT_comp_dir string attribute owned by
1686 the obstack that owns the DIE. */
1687 const char *comp_dir;
1688
1689 /* If we needed to build a new string for comp_dir, this is what
1690 owns the storage. */
1691 std::string comp_dir_storage;
1692 };
1693
1694 static file_and_directory find_file_and_directory (struct die_info *die,
1695 struct dwarf2_cu *cu);
1696
1697 static htab_up allocate_signatured_type_table ();
1698
1699 static htab_up allocate_dwo_unit_table ();
1700
1701 static struct dwo_unit *lookup_dwo_unit_in_dwp
1702 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1703 const char *comp_dir, ULONGEST signature, int is_debug_types);
1704
1705 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1706
1707 static struct dwo_unit *lookup_dwo_comp_unit
1708 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1709 ULONGEST signature);
1710
1711 static struct dwo_unit *lookup_dwo_type_unit
1712 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1713
1714 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1715
1716 /* A unique pointer to a dwo_file. */
1717
1718 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1719
1720 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1721
1722 static void check_producer (struct dwarf2_cu *cu);
1723
1724 static void free_line_header_voidp (void *arg);
1725 \f
1726 /* Various complaints about symbol reading that don't abort the process. */
1727
1728 static void
1729 dwarf2_debug_line_missing_file_complaint (void)
1730 {
1731 complaint (_(".debug_line section has line data without a file"));
1732 }
1733
1734 static void
1735 dwarf2_debug_line_missing_end_sequence_complaint (void)
1736 {
1737 complaint (_(".debug_line section has line "
1738 "program sequence without an end"));
1739 }
1740
1741 static void
1742 dwarf2_complex_location_expr_complaint (void)
1743 {
1744 complaint (_("location expression too complex"));
1745 }
1746
1747 static void
1748 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1749 int arg3)
1750 {
1751 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1752 arg1, arg2, arg3);
1753 }
1754
1755 static void
1756 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1757 {
1758 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1759 arg1, arg2);
1760 }
1761
1762 /* Hash function for line_header_hash. */
1763
1764 static hashval_t
1765 line_header_hash (const struct line_header *ofs)
1766 {
1767 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1768 }
1769
1770 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1771
1772 static hashval_t
1773 line_header_hash_voidp (const void *item)
1774 {
1775 const struct line_header *ofs = (const struct line_header *) item;
1776
1777 return line_header_hash (ofs);
1778 }
1779
1780 /* Equality function for line_header_hash. */
1781
1782 static int
1783 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1784 {
1785 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1786 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1787
1788 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1789 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1790 }
1791
1792 \f
1793
1794 /* See declaration. */
1795
1796 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1797 bool can_copy_)
1798 : obfd (obfd),
1799 can_copy (can_copy_)
1800 {
1801 if (names == NULL)
1802 names = &dwarf2_elf_names;
1803
1804 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1805 locate_sections (obfd, sec, *names);
1806 }
1807
1808 dwarf2_per_bfd::~dwarf2_per_bfd ()
1809 {
1810 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1811 per_cu->imported_symtabs_free ();
1812
1813 for (signatured_type *sig_type : all_type_units)
1814 sig_type->per_cu.imported_symtabs_free ();
1815
1816 /* Everything else should be on this->obstack. */
1817 }
1818
1819 /* See read.h. */
1820
1821 void
1822 dwarf2_per_objfile::remove_all_cus ()
1823 {
1824 for (auto pair : m_dwarf2_cus)
1825 delete pair.second;
1826
1827 m_dwarf2_cus.clear ();
1828 }
1829
1830 /* A helper class that calls free_cached_comp_units on
1831 destruction. */
1832
1833 class free_cached_comp_units
1834 {
1835 public:
1836
1837 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1838 : m_per_objfile (per_objfile)
1839 {
1840 }
1841
1842 ~free_cached_comp_units ()
1843 {
1844 m_per_objfile->remove_all_cus ();
1845 }
1846
1847 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1848
1849 private:
1850
1851 dwarf2_per_objfile *m_per_objfile;
1852 };
1853
1854 /* See read.h. */
1855
1856 bool
1857 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1858 {
1859 gdb_assert (per_cu->index < this->m_symtabs.size ());
1860
1861 return this->m_symtabs[per_cu->index] != nullptr;
1862 }
1863
1864 /* See read.h. */
1865
1866 compunit_symtab *
1867 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1868 {
1869 gdb_assert (per_cu->index < this->m_symtabs.size ());
1870
1871 return this->m_symtabs[per_cu->index];
1872 }
1873
1874 /* See read.h. */
1875
1876 void
1877 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1878 compunit_symtab *symtab)
1879 {
1880 gdb_assert (per_cu->index < this->m_symtabs.size ());
1881 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1882
1883 this->m_symtabs[per_cu->index] = symtab;
1884 }
1885
1886 /* Try to locate the sections we need for DWARF 2 debugging
1887 information and return true if we have enough to do something.
1888 NAMES points to the dwarf2 section names, or is NULL if the standard
1889 ELF names are used. CAN_COPY is true for formats where symbol
1890 interposition is possible and so symbol values must follow copy
1891 relocation rules. */
1892
1893 int
1894 dwarf2_has_info (struct objfile *objfile,
1895 const struct dwarf2_debug_sections *names,
1896 bool can_copy)
1897 {
1898 if (objfile->flags & OBJF_READNEVER)
1899 return 0;
1900
1901 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1902
1903 if (per_objfile == NULL)
1904 {
1905 dwarf2_per_bfd *per_bfd;
1906
1907 /* We can share a "dwarf2_per_bfd" with other objfiles if the BFD
1908 doesn't require relocations and if there aren't partial symbols
1909 from some other reader. */
1910 if (!objfile_has_partial_symbols (objfile)
1911 && !gdb_bfd_requires_relocations (objfile->obfd))
1912 {
1913 /* See if one has been created for this BFD yet. */
1914 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1915
1916 if (per_bfd == nullptr)
1917 {
1918 /* No, create it now. */
1919 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1920 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1921 }
1922 }
1923 else
1924 {
1925 /* No sharing possible, create one specifically for this objfile. */
1926 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1927 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1928 }
1929
1930 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1931 }
1932
1933 return (!per_objfile->per_bfd->info.is_virtual
1934 && per_objfile->per_bfd->info.s.section != NULL
1935 && !per_objfile->per_bfd->abbrev.is_virtual
1936 && per_objfile->per_bfd->abbrev.s.section != NULL);
1937 }
1938
1939 /* When loading sections, we look either for uncompressed section or for
1940 compressed section names. */
1941
1942 static int
1943 section_is_p (const char *section_name,
1944 const struct dwarf2_section_names *names)
1945 {
1946 if (names->normal != NULL
1947 && strcmp (section_name, names->normal) == 0)
1948 return 1;
1949 if (names->compressed != NULL
1950 && strcmp (section_name, names->compressed) == 0)
1951 return 1;
1952 return 0;
1953 }
1954
1955 /* See declaration. */
1956
1957 void
1958 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1959 const dwarf2_debug_sections &names)
1960 {
1961 flagword aflag = bfd_section_flags (sectp);
1962
1963 if ((aflag & SEC_HAS_CONTENTS) == 0)
1964 {
1965 }
1966 else if (elf_section_data (sectp)->this_hdr.sh_size
1967 > bfd_get_file_size (abfd))
1968 {
1969 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1970 warning (_("Discarding section %s which has a section size (%s"
1971 ") larger than the file size [in module %s]"),
1972 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1973 bfd_get_filename (abfd));
1974 }
1975 else if (section_is_p (sectp->name, &names.info))
1976 {
1977 this->info.s.section = sectp;
1978 this->info.size = bfd_section_size (sectp);
1979 }
1980 else if (section_is_p (sectp->name, &names.abbrev))
1981 {
1982 this->abbrev.s.section = sectp;
1983 this->abbrev.size = bfd_section_size (sectp);
1984 }
1985 else if (section_is_p (sectp->name, &names.line))
1986 {
1987 this->line.s.section = sectp;
1988 this->line.size = bfd_section_size (sectp);
1989 }
1990 else if (section_is_p (sectp->name, &names.loc))
1991 {
1992 this->loc.s.section = sectp;
1993 this->loc.size = bfd_section_size (sectp);
1994 }
1995 else if (section_is_p (sectp->name, &names.loclists))
1996 {
1997 this->loclists.s.section = sectp;
1998 this->loclists.size = bfd_section_size (sectp);
1999 }
2000 else if (section_is_p (sectp->name, &names.macinfo))
2001 {
2002 this->macinfo.s.section = sectp;
2003 this->macinfo.size = bfd_section_size (sectp);
2004 }
2005 else if (section_is_p (sectp->name, &names.macro))
2006 {
2007 this->macro.s.section = sectp;
2008 this->macro.size = bfd_section_size (sectp);
2009 }
2010 else if (section_is_p (sectp->name, &names.str))
2011 {
2012 this->str.s.section = sectp;
2013 this->str.size = bfd_section_size (sectp);
2014 }
2015 else if (section_is_p (sectp->name, &names.str_offsets))
2016 {
2017 this->str_offsets.s.section = sectp;
2018 this->str_offsets.size = bfd_section_size (sectp);
2019 }
2020 else if (section_is_p (sectp->name, &names.line_str))
2021 {
2022 this->line_str.s.section = sectp;
2023 this->line_str.size = bfd_section_size (sectp);
2024 }
2025 else if (section_is_p (sectp->name, &names.addr))
2026 {
2027 this->addr.s.section = sectp;
2028 this->addr.size = bfd_section_size (sectp);
2029 }
2030 else if (section_is_p (sectp->name, &names.frame))
2031 {
2032 this->frame.s.section = sectp;
2033 this->frame.size = bfd_section_size (sectp);
2034 }
2035 else if (section_is_p (sectp->name, &names.eh_frame))
2036 {
2037 this->eh_frame.s.section = sectp;
2038 this->eh_frame.size = bfd_section_size (sectp);
2039 }
2040 else if (section_is_p (sectp->name, &names.ranges))
2041 {
2042 this->ranges.s.section = sectp;
2043 this->ranges.size = bfd_section_size (sectp);
2044 }
2045 else if (section_is_p (sectp->name, &names.rnglists))
2046 {
2047 this->rnglists.s.section = sectp;
2048 this->rnglists.size = bfd_section_size (sectp);
2049 }
2050 else if (section_is_p (sectp->name, &names.types))
2051 {
2052 struct dwarf2_section_info type_section;
2053
2054 memset (&type_section, 0, sizeof (type_section));
2055 type_section.s.section = sectp;
2056 type_section.size = bfd_section_size (sectp);
2057
2058 this->types.push_back (type_section);
2059 }
2060 else if (section_is_p (sectp->name, &names.gdb_index))
2061 {
2062 this->gdb_index.s.section = sectp;
2063 this->gdb_index.size = bfd_section_size (sectp);
2064 }
2065 else if (section_is_p (sectp->name, &names.debug_names))
2066 {
2067 this->debug_names.s.section = sectp;
2068 this->debug_names.size = bfd_section_size (sectp);
2069 }
2070 else if (section_is_p (sectp->name, &names.debug_aranges))
2071 {
2072 this->debug_aranges.s.section = sectp;
2073 this->debug_aranges.size = bfd_section_size (sectp);
2074 }
2075
2076 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2077 && bfd_section_vma (sectp) == 0)
2078 this->has_section_at_zero = true;
2079 }
2080
2081 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2082 SECTION_NAME. */
2083
2084 void
2085 dwarf2_get_section_info (struct objfile *objfile,
2086 enum dwarf2_section_enum sect,
2087 asection **sectp, const gdb_byte **bufp,
2088 bfd_size_type *sizep)
2089 {
2090 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2091 struct dwarf2_section_info *info;
2092
2093 /* We may see an objfile without any DWARF, in which case we just
2094 return nothing. */
2095 if (per_objfile == NULL)
2096 {
2097 *sectp = NULL;
2098 *bufp = NULL;
2099 *sizep = 0;
2100 return;
2101 }
2102 switch (sect)
2103 {
2104 case DWARF2_DEBUG_FRAME:
2105 info = &per_objfile->per_bfd->frame;
2106 break;
2107 case DWARF2_EH_FRAME:
2108 info = &per_objfile->per_bfd->eh_frame;
2109 break;
2110 default:
2111 gdb_assert_not_reached ("unexpected section");
2112 }
2113
2114 info->read (objfile);
2115
2116 *sectp = info->get_bfd_section ();
2117 *bufp = info->buffer;
2118 *sizep = info->size;
2119 }
2120
2121 /* A helper function to find the sections for a .dwz file. */
2122
2123 static void
2124 locate_dwz_sections (bfd *abfd, asection *sectp, dwz_file *dwz_file)
2125 {
2126 /* Note that we only support the standard ELF names, because .dwz
2127 is ELF-only (at the time of writing). */
2128 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2129 {
2130 dwz_file->abbrev.s.section = sectp;
2131 dwz_file->abbrev.size = bfd_section_size (sectp);
2132 }
2133 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2134 {
2135 dwz_file->info.s.section = sectp;
2136 dwz_file->info.size = bfd_section_size (sectp);
2137 }
2138 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2139 {
2140 dwz_file->str.s.section = sectp;
2141 dwz_file->str.size = bfd_section_size (sectp);
2142 }
2143 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2144 {
2145 dwz_file->line.s.section = sectp;
2146 dwz_file->line.size = bfd_section_size (sectp);
2147 }
2148 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2149 {
2150 dwz_file->macro.s.section = sectp;
2151 dwz_file->macro.size = bfd_section_size (sectp);
2152 }
2153 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2154 {
2155 dwz_file->gdb_index.s.section = sectp;
2156 dwz_file->gdb_index.size = bfd_section_size (sectp);
2157 }
2158 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2159 {
2160 dwz_file->debug_names.s.section = sectp;
2161 dwz_file->debug_names.size = bfd_section_size (sectp);
2162 }
2163 }
2164
2165 /* See dwarf2read.h. */
2166
2167 struct dwz_file *
2168 dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
2169 {
2170 const char *filename;
2171 bfd_size_type buildid_len_arg;
2172 size_t buildid_len;
2173 bfd_byte *buildid;
2174
2175 if (per_bfd->dwz_file != NULL)
2176 return per_bfd->dwz_file.get ();
2177
2178 bfd_set_error (bfd_error_no_error);
2179 gdb::unique_xmalloc_ptr<char> data
2180 (bfd_get_alt_debug_link_info (per_bfd->obfd,
2181 &buildid_len_arg, &buildid));
2182 if (data == NULL)
2183 {
2184 if (bfd_get_error () == bfd_error_no_error)
2185 return NULL;
2186 error (_("could not read '.gnu_debugaltlink' section: %s"),
2187 bfd_errmsg (bfd_get_error ()));
2188 }
2189
2190 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2191
2192 buildid_len = (size_t) buildid_len_arg;
2193
2194 filename = data.get ();
2195
2196 std::string abs_storage;
2197 if (!IS_ABSOLUTE_PATH (filename))
2198 {
2199 gdb::unique_xmalloc_ptr<char> abs
2200 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
2201
2202 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2203 filename = abs_storage.c_str ();
2204 }
2205
2206 /* First try the file name given in the section. If that doesn't
2207 work, try to use the build-id instead. */
2208 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
2209 if (dwz_bfd != NULL)
2210 {
2211 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2212 dwz_bfd.reset (nullptr);
2213 }
2214
2215 if (dwz_bfd == NULL)
2216 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2217
2218 if (dwz_bfd == nullptr)
2219 {
2220 gdb::unique_xmalloc_ptr<char> alt_filename;
2221 const char *origname = bfd_get_filename (per_bfd->obfd);
2222
2223 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2224 buildid_len,
2225 origname,
2226 &alt_filename));
2227
2228 if (fd.get () >= 0)
2229 {
2230 /* File successfully retrieved from server. */
2231 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
2232
2233 if (dwz_bfd == nullptr)
2234 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2235 alt_filename.get ());
2236 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2237 dwz_bfd.reset (nullptr);
2238 }
2239 }
2240
2241 if (dwz_bfd == NULL)
2242 error (_("could not find '.gnu_debugaltlink' file for %s"),
2243 bfd_get_filename (per_bfd->obfd));
2244
2245 std::unique_ptr<struct dwz_file> result
2246 (new struct dwz_file (std::move (dwz_bfd)));
2247
2248 for (asection *sec : gdb_bfd_sections (result->dwz_bfd))
2249 locate_dwz_sections (result->dwz_bfd.get (), sec, result.get ());
2250
2251 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2252 per_bfd->dwz_file = std::move (result);
2253 return per_bfd->dwz_file.get ();
2254 }
2255 \f
2256 /* DWARF quick_symbols_functions support. */
2257
2258 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2259 unique line tables, so we maintain a separate table of all .debug_line
2260 derived entries to support the sharing.
2261 All the quick functions need is the list of file names. We discard the
2262 line_header when we're done and don't need to record it here. */
2263 struct quick_file_names
2264 {
2265 /* The data used to construct the hash key. */
2266 struct stmt_list_hash hash;
2267
2268 /* The number of entries in file_names, real_names. */
2269 unsigned int num_file_names;
2270
2271 /* The file names from the line table, after being run through
2272 file_full_name. */
2273 const char **file_names;
2274
2275 /* The file names from the line table after being run through
2276 gdb_realpath. These are computed lazily. */
2277 const char **real_names;
2278 };
2279
2280 /* When using the index (and thus not using psymtabs), each CU has an
2281 object of this type. This is used to hold information needed by
2282 the various "quick" methods. */
2283 struct dwarf2_per_cu_quick_data
2284 {
2285 /* The file table. This can be NULL if there was no file table
2286 or it's currently not read in.
2287 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2288 struct quick_file_names *file_names;
2289
2290 /* A temporary mark bit used when iterating over all CUs in
2291 expand_symtabs_matching. */
2292 unsigned int mark : 1;
2293
2294 /* True if we've tried to read the file table and found there isn't one.
2295 There will be no point in trying to read it again next time. */
2296 unsigned int no_file_data : 1;
2297 };
2298
2299 /* Utility hash function for a stmt_list_hash. */
2300
2301 static hashval_t
2302 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2303 {
2304 hashval_t v = 0;
2305
2306 if (stmt_list_hash->dwo_unit != NULL)
2307 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2308 v += to_underlying (stmt_list_hash->line_sect_off);
2309 return v;
2310 }
2311
2312 /* Utility equality function for a stmt_list_hash. */
2313
2314 static int
2315 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2316 const struct stmt_list_hash *rhs)
2317 {
2318 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2319 return 0;
2320 if (lhs->dwo_unit != NULL
2321 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2322 return 0;
2323
2324 return lhs->line_sect_off == rhs->line_sect_off;
2325 }
2326
2327 /* Hash function for a quick_file_names. */
2328
2329 static hashval_t
2330 hash_file_name_entry (const void *e)
2331 {
2332 const struct quick_file_names *file_data
2333 = (const struct quick_file_names *) e;
2334
2335 return hash_stmt_list_entry (&file_data->hash);
2336 }
2337
2338 /* Equality function for a quick_file_names. */
2339
2340 static int
2341 eq_file_name_entry (const void *a, const void *b)
2342 {
2343 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2344 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2345
2346 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2347 }
2348
2349 /* Delete function for a quick_file_names. */
2350
2351 static void
2352 delete_file_name_entry (void *e)
2353 {
2354 struct quick_file_names *file_data = (struct quick_file_names *) e;
2355 int i;
2356
2357 for (i = 0; i < file_data->num_file_names; ++i)
2358 {
2359 xfree ((void*) file_data->file_names[i]);
2360 if (file_data->real_names)
2361 xfree ((void*) file_data->real_names[i]);
2362 }
2363
2364 /* The space for the struct itself lives on the obstack, so we don't
2365 free it here. */
2366 }
2367
2368 /* Create a quick_file_names hash table. */
2369
2370 static htab_up
2371 create_quick_file_names_table (unsigned int nr_initial_entries)
2372 {
2373 return htab_up (htab_create_alloc (nr_initial_entries,
2374 hash_file_name_entry, eq_file_name_entry,
2375 delete_file_name_entry, xcalloc, xfree));
2376 }
2377
2378 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2379 function is unrelated to symtabs, symtab would have to be created afterwards.
2380 You should call age_cached_comp_units after processing the CU. */
2381
2382 static dwarf2_cu *
2383 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2384 bool skip_partial)
2385 {
2386 if (per_cu->is_debug_types)
2387 load_full_type_unit (per_cu, per_objfile);
2388 else
2389 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2390 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 && attr->form_is_unsigned ())
3216 {
3217 struct quick_file_names find_entry;
3218
3219 line_offset = (sect_offset) attr->as_unsigned ();
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 try
6123 {
6124 /* This isn't really ideal: all the data we allocate on the
6125 objfile's obstack is still uselessly kept around. However,
6126 freeing it seems unsafe. */
6127 psymtab_discarder psymtabs (objfile);
6128 dwarf2_build_psymtabs_hard (per_objfile);
6129 psymtabs.keep ();
6130
6131 per_objfile->resize_symtabs ();
6132
6133 /* (maybe) store an index in the cache. */
6134 global_index_cache.store (per_objfile);
6135 }
6136 catch (const gdb_exception_error &except)
6137 {
6138 exception_print (gdb_stderr, except);
6139 }
6140
6141 /* Finish by setting the local reference to partial symtabs, so that
6142 we don't try to read them again if reading another objfile with the same
6143 BFD. If we can't in fact share, this won't make a difference anyway as
6144 the dwarf2_per_bfd object won't be shared. */
6145 per_bfd->partial_symtabs = objfile->partial_symtabs;
6146 }
6147
6148 /* Find the base address of the compilation unit for range lists and
6149 location lists. It will normally be specified by DW_AT_low_pc.
6150 In DWARF-3 draft 4, the base address could be overridden by
6151 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6152 compilation units with discontinuous ranges. */
6153
6154 static void
6155 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6156 {
6157 struct attribute *attr;
6158
6159 cu->base_address.reset ();
6160
6161 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6162 if (attr != nullptr)
6163 cu->base_address = attr->as_address ();
6164 else
6165 {
6166 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6167 if (attr != nullptr)
6168 cu->base_address = attr->as_address ();
6169 }
6170 }
6171
6172 /* Helper function that returns the proper abbrev section for
6173 THIS_CU. */
6174
6175 static struct dwarf2_section_info *
6176 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6177 {
6178 struct dwarf2_section_info *abbrev;
6179 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6180
6181 if (this_cu->is_dwz)
6182 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
6183 else
6184 abbrev = &per_bfd->abbrev;
6185
6186 return abbrev;
6187 }
6188
6189 /* Fetch the abbreviation table offset from a comp or type unit header. */
6190
6191 static sect_offset
6192 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
6193 struct dwarf2_section_info *section,
6194 sect_offset sect_off)
6195 {
6196 bfd *abfd = section->get_bfd_owner ();
6197 const gdb_byte *info_ptr;
6198 unsigned int initial_length_size, offset_size;
6199 uint16_t version;
6200
6201 section->read (per_objfile->objfile);
6202 info_ptr = section->buffer + to_underlying (sect_off);
6203 read_initial_length (abfd, info_ptr, &initial_length_size);
6204 offset_size = initial_length_size == 4 ? 4 : 8;
6205 info_ptr += initial_length_size;
6206
6207 version = read_2_bytes (abfd, info_ptr);
6208 info_ptr += 2;
6209 if (version >= 5)
6210 {
6211 /* Skip unit type and address size. */
6212 info_ptr += 2;
6213 }
6214
6215 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6216 }
6217
6218 /* A partial symtab that is used only for include files. */
6219 struct dwarf2_include_psymtab : public partial_symtab
6220 {
6221 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6222 : partial_symtab (filename, objfile)
6223 {
6224 }
6225
6226 void read_symtab (struct objfile *objfile) override
6227 {
6228 /* It's an include file, no symbols to read for it.
6229 Everything is in the includer symtab. */
6230
6231 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6232 expansion of the includer psymtab. We use the dependencies[0] field to
6233 model the includer. But if we go the regular route of calling
6234 expand_psymtab here, and having expand_psymtab call expand_dependencies
6235 to expand the includer, we'll only use expand_psymtab on the includer
6236 (making it a non-toplevel psymtab), while if we expand the includer via
6237 another path, we'll use read_symtab (making it a toplevel psymtab).
6238 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6239 psymtab, and trigger read_symtab on the includer here directly. */
6240 includer ()->read_symtab (objfile);
6241 }
6242
6243 void expand_psymtab (struct objfile *objfile) override
6244 {
6245 /* This is not called by read_symtab, and should not be called by any
6246 expand_dependencies. */
6247 gdb_assert (false);
6248 }
6249
6250 bool readin_p (struct objfile *objfile) const override
6251 {
6252 return includer ()->readin_p (objfile);
6253 }
6254
6255 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6256 {
6257 return nullptr;
6258 }
6259
6260 private:
6261 partial_symtab *includer () const
6262 {
6263 /* An include psymtab has exactly one dependency: the psymtab that
6264 includes it. */
6265 gdb_assert (this->number_of_dependencies == 1);
6266 return this->dependencies[0];
6267 }
6268 };
6269
6270 /* Allocate a new partial symtab for file named NAME and mark this new
6271 partial symtab as being an include of PST. */
6272
6273 static void
6274 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6275 struct objfile *objfile)
6276 {
6277 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6278
6279 if (!IS_ABSOLUTE_PATH (subpst->filename))
6280 subpst->dirname = pst->dirname;
6281
6282 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6283 subpst->dependencies[0] = pst;
6284 subpst->number_of_dependencies = 1;
6285 }
6286
6287 /* Read the Line Number Program data and extract the list of files
6288 included by the source file represented by PST. Build an include
6289 partial symtab for each of these included files. */
6290
6291 static void
6292 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6293 struct die_info *die,
6294 dwarf2_psymtab *pst)
6295 {
6296 line_header_up lh;
6297 struct attribute *attr;
6298
6299 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6300 if (attr != nullptr && attr->form_is_unsigned ())
6301 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
6302 if (lh == NULL)
6303 return; /* No linetable, so no includes. */
6304
6305 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6306 that we pass in the raw text_low here; that is ok because we're
6307 only decoding the line table to make include partial symtabs, and
6308 so the addresses aren't really used. */
6309 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6310 pst->raw_text_low (), 1);
6311 }
6312
6313 static hashval_t
6314 hash_signatured_type (const void *item)
6315 {
6316 const struct signatured_type *sig_type
6317 = (const struct signatured_type *) item;
6318
6319 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6320 return sig_type->signature;
6321 }
6322
6323 static int
6324 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6325 {
6326 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6327 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6328
6329 return lhs->signature == rhs->signature;
6330 }
6331
6332 /* Allocate a hash table for signatured types. */
6333
6334 static htab_up
6335 allocate_signatured_type_table ()
6336 {
6337 return htab_up (htab_create_alloc (41,
6338 hash_signatured_type,
6339 eq_signatured_type,
6340 NULL, xcalloc, xfree));
6341 }
6342
6343 /* A helper function to add a signatured type CU to a table. */
6344
6345 static int
6346 add_signatured_type_cu_to_table (void **slot, void *datum)
6347 {
6348 struct signatured_type *sigt = (struct signatured_type *) *slot;
6349 std::vector<signatured_type *> *all_type_units
6350 = (std::vector<signatured_type *> *) datum;
6351
6352 all_type_units->push_back (sigt);
6353
6354 return 1;
6355 }
6356
6357 /* A helper for create_debug_types_hash_table. Read types from SECTION
6358 and fill them into TYPES_HTAB. It will process only type units,
6359 therefore DW_UT_type. */
6360
6361 static void
6362 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
6363 struct dwo_file *dwo_file,
6364 dwarf2_section_info *section, htab_up &types_htab,
6365 rcuh_kind section_kind)
6366 {
6367 struct objfile *objfile = per_objfile->objfile;
6368 struct dwarf2_section_info *abbrev_section;
6369 bfd *abfd;
6370 const gdb_byte *info_ptr, *end_ptr;
6371
6372 abbrev_section = (dwo_file != NULL
6373 ? &dwo_file->sections.abbrev
6374 : &per_objfile->per_bfd->abbrev);
6375
6376 if (dwarf_read_debug)
6377 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6378 section->get_name (),
6379 abbrev_section->get_file_name ());
6380
6381 section->read (objfile);
6382 info_ptr = section->buffer;
6383
6384 if (info_ptr == NULL)
6385 return;
6386
6387 /* We can't set abfd until now because the section may be empty or
6388 not present, in which case the bfd is unknown. */
6389 abfd = section->get_bfd_owner ();
6390
6391 /* We don't use cutu_reader here because we don't need to read
6392 any dies: the signature is in the header. */
6393
6394 end_ptr = info_ptr + section->size;
6395 while (info_ptr < end_ptr)
6396 {
6397 struct signatured_type *sig_type;
6398 struct dwo_unit *dwo_tu;
6399 void **slot;
6400 const gdb_byte *ptr = info_ptr;
6401 struct comp_unit_head header;
6402 unsigned int length;
6403
6404 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6405
6406 /* Initialize it due to a false compiler warning. */
6407 header.signature = -1;
6408 header.type_cu_offset_in_tu = (cu_offset) -1;
6409
6410 /* We need to read the type's signature in order to build the hash
6411 table, but we don't need anything else just yet. */
6412
6413 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
6414 abbrev_section, ptr, section_kind);
6415
6416 length = header.get_length ();
6417
6418 /* Skip dummy type units. */
6419 if (ptr >= info_ptr + length
6420 || peek_abbrev_code (abfd, ptr) == 0
6421 || (header.unit_type != DW_UT_type
6422 && header.unit_type != DW_UT_split_type))
6423 {
6424 info_ptr += length;
6425 continue;
6426 }
6427
6428 if (types_htab == NULL)
6429 {
6430 if (dwo_file)
6431 types_htab = allocate_dwo_unit_table ();
6432 else
6433 types_htab = allocate_signatured_type_table ();
6434 }
6435
6436 if (dwo_file)
6437 {
6438 sig_type = NULL;
6439 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
6440 dwo_tu->dwo_file = dwo_file;
6441 dwo_tu->signature = header.signature;
6442 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6443 dwo_tu->section = section;
6444 dwo_tu->sect_off = sect_off;
6445 dwo_tu->length = length;
6446 }
6447 else
6448 {
6449 /* N.B.: type_offset is not usable if this type uses a DWO file.
6450 The real type_offset is in the DWO file. */
6451 dwo_tu = NULL;
6452 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6453 sig_type->signature = header.signature;
6454 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6455 sig_type->per_cu.is_debug_types = 1;
6456 sig_type->per_cu.section = section;
6457 sig_type->per_cu.sect_off = sect_off;
6458 sig_type->per_cu.length = length;
6459 }
6460
6461 slot = htab_find_slot (types_htab.get (),
6462 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6463 INSERT);
6464 gdb_assert (slot != NULL);
6465 if (*slot != NULL)
6466 {
6467 sect_offset dup_sect_off;
6468
6469 if (dwo_file)
6470 {
6471 const struct dwo_unit *dup_tu
6472 = (const struct dwo_unit *) *slot;
6473
6474 dup_sect_off = dup_tu->sect_off;
6475 }
6476 else
6477 {
6478 const struct signatured_type *dup_tu
6479 = (const struct signatured_type *) *slot;
6480
6481 dup_sect_off = dup_tu->per_cu.sect_off;
6482 }
6483
6484 complaint (_("debug type entry at offset %s is duplicate to"
6485 " the entry at offset %s, signature %s"),
6486 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6487 hex_string (header.signature));
6488 }
6489 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6490
6491 if (dwarf_read_debug > 1)
6492 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6493 sect_offset_str (sect_off),
6494 hex_string (header.signature));
6495
6496 info_ptr += length;
6497 }
6498 }
6499
6500 /* Create the hash table of all entries in the .debug_types
6501 (or .debug_types.dwo) section(s).
6502 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6503 otherwise it is NULL.
6504
6505 The result is a pointer to the hash table or NULL if there are no types.
6506
6507 Note: This function processes DWO files only, not DWP files. */
6508
6509 static void
6510 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
6511 struct dwo_file *dwo_file,
6512 gdb::array_view<dwarf2_section_info> type_sections,
6513 htab_up &types_htab)
6514 {
6515 for (dwarf2_section_info &section : type_sections)
6516 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6517 rcuh_kind::TYPE);
6518 }
6519
6520 /* Create the hash table of all entries in the .debug_types section,
6521 and initialize all_type_units.
6522 The result is zero if there is an error (e.g. missing .debug_types section),
6523 otherwise non-zero. */
6524
6525 static int
6526 create_all_type_units (dwarf2_per_objfile *per_objfile)
6527 {
6528 htab_up types_htab;
6529
6530 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6531 types_htab, rcuh_kind::COMPILE);
6532 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6533 types_htab);
6534 if (types_htab == NULL)
6535 {
6536 per_objfile->per_bfd->signatured_types = NULL;
6537 return 0;
6538 }
6539
6540 per_objfile->per_bfd->signatured_types = std::move (types_htab);
6541
6542 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6543 per_objfile->per_bfd->all_type_units.reserve
6544 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
6545
6546 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
6547 add_signatured_type_cu_to_table,
6548 &per_objfile->per_bfd->all_type_units);
6549
6550 return 1;
6551 }
6552
6553 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6554 If SLOT is non-NULL, it is the entry to use in the hash table.
6555 Otherwise we find one. */
6556
6557 static struct signatured_type *
6558 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6559 {
6560 if (per_objfile->per_bfd->all_type_units.size ()
6561 == per_objfile->per_bfd->all_type_units.capacity ())
6562 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6563
6564 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6565
6566 per_objfile->resize_symtabs ();
6567
6568 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6569 sig_type->signature = sig;
6570 sig_type->per_cu.is_debug_types = 1;
6571 if (per_objfile->per_bfd->using_index)
6572 {
6573 sig_type->per_cu.v.quick =
6574 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6575 struct dwarf2_per_cu_quick_data);
6576 }
6577
6578 if (slot == NULL)
6579 {
6580 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6581 sig_type, INSERT);
6582 }
6583 gdb_assert (*slot == NULL);
6584 *slot = sig_type;
6585 /* The rest of sig_type must be filled in by the caller. */
6586 return sig_type;
6587 }
6588
6589 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6590 Fill in SIG_ENTRY with DWO_ENTRY. */
6591
6592 static void
6593 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
6594 struct signatured_type *sig_entry,
6595 struct dwo_unit *dwo_entry)
6596 {
6597 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6598
6599 /* Make sure we're not clobbering something we don't expect to. */
6600 gdb_assert (! sig_entry->per_cu.queued);
6601 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
6602 if (per_bfd->using_index)
6603 {
6604 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6605 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6606 }
6607 else
6608 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6609 gdb_assert (sig_entry->signature == dwo_entry->signature);
6610 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6611 gdb_assert (sig_entry->type_unit_group == NULL);
6612 gdb_assert (sig_entry->dwo_unit == NULL);
6613
6614 sig_entry->per_cu.section = dwo_entry->section;
6615 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6616 sig_entry->per_cu.length = dwo_entry->length;
6617 sig_entry->per_cu.reading_dwo_directly = 1;
6618 sig_entry->per_cu.per_bfd = per_bfd;
6619 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6620 sig_entry->dwo_unit = dwo_entry;
6621 }
6622
6623 /* Subroutine of lookup_signatured_type.
6624 If we haven't read the TU yet, create the signatured_type data structure
6625 for a TU to be read in directly from a DWO file, bypassing the stub.
6626 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6627 using .gdb_index, then when reading a CU we want to stay in the DWO file
6628 containing that CU. Otherwise we could end up reading several other DWO
6629 files (due to comdat folding) to process the transitive closure of all the
6630 mentioned TUs, and that can be slow. The current DWO file will have every
6631 type signature that it needs.
6632 We only do this for .gdb_index because in the psymtab case we already have
6633 to read all the DWOs to build the type unit groups. */
6634
6635 static struct signatured_type *
6636 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6637 {
6638 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6639 struct dwo_file *dwo_file;
6640 struct dwo_unit find_dwo_entry, *dwo_entry;
6641 struct signatured_type find_sig_entry, *sig_entry;
6642 void **slot;
6643
6644 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6645
6646 /* If TU skeletons have been removed then we may not have read in any
6647 TUs yet. */
6648 if (per_objfile->per_bfd->signatured_types == NULL)
6649 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6650
6651 /* We only ever need to read in one copy of a signatured type.
6652 Use the global signatured_types array to do our own comdat-folding
6653 of types. If this is the first time we're reading this TU, and
6654 the TU has an entry in .gdb_index, replace the recorded data from
6655 .gdb_index with this TU. */
6656
6657 find_sig_entry.signature = sig;
6658 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6659 &find_sig_entry, INSERT);
6660 sig_entry = (struct signatured_type *) *slot;
6661
6662 /* We can get here with the TU already read, *or* in the process of being
6663 read. Don't reassign the global entry to point to this DWO if that's
6664 the case. Also note that if the TU is already being read, it may not
6665 have come from a DWO, the program may be a mix of Fission-compiled
6666 code and non-Fission-compiled code. */
6667
6668 /* Have we already tried to read this TU?
6669 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6670 needn't exist in the global table yet). */
6671 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6672 return sig_entry;
6673
6674 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6675 dwo_unit of the TU itself. */
6676 dwo_file = cu->dwo_unit->dwo_file;
6677
6678 /* Ok, this is the first time we're reading this TU. */
6679 if (dwo_file->tus == NULL)
6680 return NULL;
6681 find_dwo_entry.signature = sig;
6682 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6683 &find_dwo_entry);
6684 if (dwo_entry == NULL)
6685 return NULL;
6686
6687 /* If the global table doesn't have an entry for this TU, add one. */
6688 if (sig_entry == NULL)
6689 sig_entry = add_type_unit (per_objfile, sig, slot);
6690
6691 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6692 sig_entry->per_cu.tu_read = 1;
6693 return sig_entry;
6694 }
6695
6696 /* Subroutine of lookup_signatured_type.
6697 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6698 then try the DWP file. If the TU stub (skeleton) has been removed then
6699 it won't be in .gdb_index. */
6700
6701 static struct signatured_type *
6702 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6703 {
6704 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6705 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6706 struct dwo_unit *dwo_entry;
6707 struct signatured_type find_sig_entry, *sig_entry;
6708 void **slot;
6709
6710 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6711 gdb_assert (dwp_file != NULL);
6712
6713 /* If TU skeletons have been removed then we may not have read in any
6714 TUs yet. */
6715 if (per_objfile->per_bfd->signatured_types == NULL)
6716 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6717
6718 find_sig_entry.signature = sig;
6719 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6720 &find_sig_entry, INSERT);
6721 sig_entry = (struct signatured_type *) *slot;
6722
6723 /* Have we already tried to read this TU?
6724 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6725 needn't exist in the global table yet). */
6726 if (sig_entry != NULL)
6727 return sig_entry;
6728
6729 if (dwp_file->tus == NULL)
6730 return NULL;
6731 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6732 1 /* is_debug_types */);
6733 if (dwo_entry == NULL)
6734 return NULL;
6735
6736 sig_entry = add_type_unit (per_objfile, sig, slot);
6737 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6738
6739 return sig_entry;
6740 }
6741
6742 /* Lookup a signature based type for DW_FORM_ref_sig8.
6743 Returns NULL if signature SIG is not present in the table.
6744 It is up to the caller to complain about this. */
6745
6746 static struct signatured_type *
6747 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6748 {
6749 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6750
6751 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6752 {
6753 /* We're in a DWO/DWP file, and we're using .gdb_index.
6754 These cases require special processing. */
6755 if (get_dwp_file (per_objfile) == NULL)
6756 return lookup_dwo_signatured_type (cu, sig);
6757 else
6758 return lookup_dwp_signatured_type (cu, sig);
6759 }
6760 else
6761 {
6762 struct signatured_type find_entry, *entry;
6763
6764 if (per_objfile->per_bfd->signatured_types == NULL)
6765 return NULL;
6766 find_entry.signature = sig;
6767 entry = ((struct signatured_type *)
6768 htab_find (per_objfile->per_bfd->signatured_types.get (),
6769 &find_entry));
6770 return entry;
6771 }
6772 }
6773
6774 /* Low level DIE reading support. */
6775
6776 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6777
6778 static void
6779 init_cu_die_reader (struct die_reader_specs *reader,
6780 struct dwarf2_cu *cu,
6781 struct dwarf2_section_info *section,
6782 struct dwo_file *dwo_file,
6783 struct abbrev_table *abbrev_table)
6784 {
6785 gdb_assert (section->readin && section->buffer != NULL);
6786 reader->abfd = section->get_bfd_owner ();
6787 reader->cu = cu;
6788 reader->dwo_file = dwo_file;
6789 reader->die_section = section;
6790 reader->buffer = section->buffer;
6791 reader->buffer_end = section->buffer + section->size;
6792 reader->abbrev_table = abbrev_table;
6793 }
6794
6795 /* Subroutine of cutu_reader to simplify it.
6796 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6797 There's just a lot of work to do, and cutu_reader is big enough
6798 already.
6799
6800 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6801 from it to the DIE in the DWO. If NULL we are skipping the stub.
6802 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6803 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6804 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6805 STUB_COMP_DIR may be non-NULL.
6806 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6807 are filled in with the info of the DIE from the DWO file.
6808 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6809 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6810 kept around for at least as long as *RESULT_READER.
6811
6812 The result is non-zero if a valid (non-dummy) DIE was found. */
6813
6814 static int
6815 read_cutu_die_from_dwo (dwarf2_cu *cu,
6816 struct dwo_unit *dwo_unit,
6817 struct die_info *stub_comp_unit_die,
6818 const char *stub_comp_dir,
6819 struct die_reader_specs *result_reader,
6820 const gdb_byte **result_info_ptr,
6821 struct die_info **result_comp_unit_die,
6822 abbrev_table_up *result_dwo_abbrev_table)
6823 {
6824 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6825 dwarf2_per_cu_data *per_cu = cu->per_cu;
6826 struct objfile *objfile = per_objfile->objfile;
6827 bfd *abfd;
6828 const gdb_byte *begin_info_ptr, *info_ptr;
6829 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6830 int i,num_extra_attrs;
6831 struct dwarf2_section_info *dwo_abbrev_section;
6832 struct die_info *comp_unit_die;
6833
6834 /* At most one of these may be provided. */
6835 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6836
6837 /* These attributes aren't processed until later:
6838 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6839 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6840 referenced later. However, these attributes are found in the stub
6841 which we won't have later. In order to not impose this complication
6842 on the rest of the code, we read them here and copy them to the
6843 DWO CU/TU die. */
6844
6845 stmt_list = NULL;
6846 low_pc = NULL;
6847 high_pc = NULL;
6848 ranges = NULL;
6849 comp_dir = NULL;
6850
6851 if (stub_comp_unit_die != NULL)
6852 {
6853 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6854 DWO file. */
6855 if (!per_cu->is_debug_types)
6856 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6857 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6858 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6859 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6860 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6861
6862 cu->addr_base = stub_comp_unit_die->addr_base ();
6863
6864 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6865 here (if needed). We need the value before we can process
6866 DW_AT_ranges. */
6867 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6868 }
6869 else if (stub_comp_dir != NULL)
6870 {
6871 /* Reconstruct the comp_dir attribute to simplify the code below. */
6872 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
6873 comp_dir->name = DW_AT_comp_dir;
6874 comp_dir->form = DW_FORM_string;
6875 comp_dir->set_string_noncanonical (stub_comp_dir);
6876 }
6877
6878 /* Set up for reading the DWO CU/TU. */
6879 cu->dwo_unit = dwo_unit;
6880 dwarf2_section_info *section = dwo_unit->section;
6881 section->read (objfile);
6882 abfd = section->get_bfd_owner ();
6883 begin_info_ptr = info_ptr = (section->buffer
6884 + to_underlying (dwo_unit->sect_off));
6885 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6886
6887 if (per_cu->is_debug_types)
6888 {
6889 signatured_type *sig_type = (struct signatured_type *) per_cu;
6890
6891 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6892 section, dwo_abbrev_section,
6893 info_ptr, rcuh_kind::TYPE);
6894 /* This is not an assert because it can be caused by bad debug info. */
6895 if (sig_type->signature != cu->header.signature)
6896 {
6897 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6898 " TU at offset %s [in module %s]"),
6899 hex_string (sig_type->signature),
6900 hex_string (cu->header.signature),
6901 sect_offset_str (dwo_unit->sect_off),
6902 bfd_get_filename (abfd));
6903 }
6904 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6905 /* For DWOs coming from DWP files, we don't know the CU length
6906 nor the type's offset in the TU until now. */
6907 dwo_unit->length = cu->header.get_length ();
6908 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6909
6910 /* Establish the type offset that can be used to lookup the type.
6911 For DWO files, we don't know it until now. */
6912 sig_type->type_offset_in_section
6913 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6914 }
6915 else
6916 {
6917 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6918 section, dwo_abbrev_section,
6919 info_ptr, rcuh_kind::COMPILE);
6920 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6921 /* For DWOs coming from DWP files, we don't know the CU length
6922 until now. */
6923 dwo_unit->length = cu->header.get_length ();
6924 }
6925
6926 *result_dwo_abbrev_table
6927 = abbrev_table::read (objfile, dwo_abbrev_section,
6928 cu->header.abbrev_sect_off);
6929 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6930 result_dwo_abbrev_table->get ());
6931
6932 /* Read in the die, but leave space to copy over the attributes
6933 from the stub. This has the benefit of simplifying the rest of
6934 the code - all the work to maintain the illusion of a single
6935 DW_TAG_{compile,type}_unit DIE is done here. */
6936 num_extra_attrs = ((stmt_list != NULL)
6937 + (low_pc != NULL)
6938 + (high_pc != NULL)
6939 + (ranges != NULL)
6940 + (comp_dir != NULL));
6941 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6942 num_extra_attrs);
6943
6944 /* Copy over the attributes from the stub to the DIE we just read in. */
6945 comp_unit_die = *result_comp_unit_die;
6946 i = comp_unit_die->num_attrs;
6947 if (stmt_list != NULL)
6948 comp_unit_die->attrs[i++] = *stmt_list;
6949 if (low_pc != NULL)
6950 comp_unit_die->attrs[i++] = *low_pc;
6951 if (high_pc != NULL)
6952 comp_unit_die->attrs[i++] = *high_pc;
6953 if (ranges != NULL)
6954 comp_unit_die->attrs[i++] = *ranges;
6955 if (comp_dir != NULL)
6956 comp_unit_die->attrs[i++] = *comp_dir;
6957 comp_unit_die->num_attrs += num_extra_attrs;
6958
6959 if (dwarf_die_debug)
6960 {
6961 fprintf_unfiltered (gdb_stdlog,
6962 "Read die from %s@0x%x of %s:\n",
6963 section->get_name (),
6964 (unsigned) (begin_info_ptr - section->buffer),
6965 bfd_get_filename (abfd));
6966 dump_die (comp_unit_die, dwarf_die_debug);
6967 }
6968
6969 /* Skip dummy compilation units. */
6970 if (info_ptr >= begin_info_ptr + dwo_unit->length
6971 || peek_abbrev_code (abfd, info_ptr) == 0)
6972 return 0;
6973
6974 *result_info_ptr = info_ptr;
6975 return 1;
6976 }
6977
6978 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6979 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6980 signature is part of the header. */
6981 static gdb::optional<ULONGEST>
6982 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6983 {
6984 if (cu->header.version >= 5)
6985 return cu->header.signature;
6986 struct attribute *attr;
6987 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6988 if (attr == nullptr || !attr->form_is_unsigned ())
6989 return gdb::optional<ULONGEST> ();
6990 return attr->as_unsigned ();
6991 }
6992
6993 /* Subroutine of cutu_reader to simplify it.
6994 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6995 Returns NULL if the specified DWO unit cannot be found. */
6996
6997 static struct dwo_unit *
6998 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6999 {
7000 dwarf2_per_cu_data *per_cu = cu->per_cu;
7001 struct dwo_unit *dwo_unit;
7002 const char *comp_dir;
7003
7004 gdb_assert (cu != NULL);
7005
7006 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7007 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7008 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7009
7010 if (per_cu->is_debug_types)
7011 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
7012 else
7013 {
7014 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7015
7016 if (!signature.has_value ())
7017 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7018 " [in module %s]"),
7019 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
7020
7021 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
7022 }
7023
7024 return dwo_unit;
7025 }
7026
7027 /* Subroutine of cutu_reader to simplify it.
7028 See it for a description of the parameters.
7029 Read a TU directly from a DWO file, bypassing the stub. */
7030
7031 void
7032 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
7033 dwarf2_per_objfile *per_objfile,
7034 dwarf2_cu *existing_cu)
7035 {
7036 struct signatured_type *sig_type;
7037
7038 /* Verify we can do the following downcast, and that we have the
7039 data we need. */
7040 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7041 sig_type = (struct signatured_type *) this_cu;
7042 gdb_assert (sig_type->dwo_unit != NULL);
7043
7044 dwarf2_cu *cu;
7045
7046 if (existing_cu != nullptr)
7047 {
7048 cu = existing_cu;
7049 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
7050 /* There's no need to do the rereading_dwo_cu handling that
7051 cutu_reader does since we don't read the stub. */
7052 }
7053 else
7054 {
7055 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7056 in per_objfile yet. */
7057 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7058 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7059 cu = m_new_cu.get ();
7060 }
7061
7062 /* A future optimization, if needed, would be to use an existing
7063 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7064 could share abbrev tables. */
7065
7066 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
7067 NULL /* stub_comp_unit_die */,
7068 sig_type->dwo_unit->dwo_file->comp_dir,
7069 this, &info_ptr,
7070 &comp_unit_die,
7071 &m_dwo_abbrev_table) == 0)
7072 {
7073 /* Dummy die. */
7074 dummy_p = true;
7075 }
7076 }
7077
7078 /* Initialize a CU (or TU) and read its DIEs.
7079 If the CU defers to a DWO file, read the DWO file as well.
7080
7081 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7082 Otherwise the table specified in the comp unit header is read in and used.
7083 This is an optimization for when we already have the abbrev table.
7084
7085 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7086 allocated. */
7087
7088 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7089 dwarf2_per_objfile *per_objfile,
7090 struct abbrev_table *abbrev_table,
7091 dwarf2_cu *existing_cu,
7092 bool skip_partial)
7093 : die_reader_specs {},
7094 m_this_cu (this_cu)
7095 {
7096 struct objfile *objfile = per_objfile->objfile;
7097 struct dwarf2_section_info *section = this_cu->section;
7098 bfd *abfd = section->get_bfd_owner ();
7099 const gdb_byte *begin_info_ptr;
7100 struct signatured_type *sig_type = NULL;
7101 struct dwarf2_section_info *abbrev_section;
7102 /* Non-zero if CU currently points to a DWO file and we need to
7103 reread it. When this happens we need to reread the skeleton die
7104 before we can reread the DWO file (this only applies to CUs, not TUs). */
7105 int rereading_dwo_cu = 0;
7106
7107 if (dwarf_die_debug)
7108 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7109 this_cu->is_debug_types ? "type" : "comp",
7110 sect_offset_str (this_cu->sect_off));
7111
7112 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7113 file (instead of going through the stub), short-circuit all of this. */
7114 if (this_cu->reading_dwo_directly)
7115 {
7116 /* Narrow down the scope of possibilities to have to understand. */
7117 gdb_assert (this_cu->is_debug_types);
7118 gdb_assert (abbrev_table == NULL);
7119 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
7120 return;
7121 }
7122
7123 /* This is cheap if the section is already read in. */
7124 section->read (objfile);
7125
7126 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7127
7128 abbrev_section = get_abbrev_section_for_cu (this_cu);
7129
7130 dwarf2_cu *cu;
7131
7132 if (existing_cu != nullptr)
7133 {
7134 cu = existing_cu;
7135 /* If this CU is from a DWO file we need to start over, we need to
7136 refetch the attributes from the skeleton CU.
7137 This could be optimized by retrieving those attributes from when we
7138 were here the first time: the previous comp_unit_die was stored in
7139 comp_unit_obstack. But there's no data yet that we need this
7140 optimization. */
7141 if (cu->dwo_unit != NULL)
7142 rereading_dwo_cu = 1;
7143 }
7144 else
7145 {
7146 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7147 in per_objfile yet. */
7148 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7149 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7150 cu = m_new_cu.get ();
7151 }
7152
7153 /* Get the header. */
7154 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7155 {
7156 /* We already have the header, there's no need to read it in again. */
7157 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7158 }
7159 else
7160 {
7161 if (this_cu->is_debug_types)
7162 {
7163 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7164 section, abbrev_section,
7165 info_ptr, rcuh_kind::TYPE);
7166
7167 /* Since per_cu is the first member of struct signatured_type,
7168 we can go from a pointer to one to a pointer to the other. */
7169 sig_type = (struct signatured_type *) this_cu;
7170 gdb_assert (sig_type->signature == cu->header.signature);
7171 gdb_assert (sig_type->type_offset_in_tu
7172 == cu->header.type_cu_offset_in_tu);
7173 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7174
7175 /* LENGTH has not been set yet for type units if we're
7176 using .gdb_index. */
7177 this_cu->length = cu->header.get_length ();
7178
7179 /* Establish the type offset that can be used to lookup the type. */
7180 sig_type->type_offset_in_section =
7181 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7182
7183 this_cu->dwarf_version = cu->header.version;
7184 }
7185 else
7186 {
7187 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7188 section, abbrev_section,
7189 info_ptr,
7190 rcuh_kind::COMPILE);
7191
7192 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7193 if (this_cu->length == 0)
7194 this_cu->length = cu->header.get_length ();
7195 else
7196 gdb_assert (this_cu->length == cu->header.get_length ());
7197 this_cu->dwarf_version = cu->header.version;
7198 }
7199 }
7200
7201 /* Skip dummy compilation units. */
7202 if (info_ptr >= begin_info_ptr + this_cu->length
7203 || peek_abbrev_code (abfd, info_ptr) == 0)
7204 {
7205 dummy_p = true;
7206 return;
7207 }
7208
7209 /* If we don't have them yet, read the abbrevs for this compilation unit.
7210 And if we need to read them now, make sure they're freed when we're
7211 done. */
7212 if (abbrev_table != NULL)
7213 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7214 else
7215 {
7216 m_abbrev_table_holder
7217 = abbrev_table::read (objfile, abbrev_section,
7218 cu->header.abbrev_sect_off);
7219 abbrev_table = m_abbrev_table_holder.get ();
7220 }
7221
7222 /* Read the top level CU/TU die. */
7223 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7224 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7225
7226 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7227 {
7228 dummy_p = true;
7229 return;
7230 }
7231
7232 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7233 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7234 table from the DWO file and pass the ownership over to us. It will be
7235 referenced from READER, so we must make sure to free it after we're done
7236 with READER.
7237
7238 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7239 DWO CU, that this test will fail (the attribute will not be present). */
7240 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7241 if (dwo_name != nullptr)
7242 {
7243 struct dwo_unit *dwo_unit;
7244 struct die_info *dwo_comp_unit_die;
7245
7246 if (comp_unit_die->has_children)
7247 {
7248 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7249 " has children (offset %s) [in module %s]"),
7250 sect_offset_str (this_cu->sect_off),
7251 bfd_get_filename (abfd));
7252 }
7253 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
7254 if (dwo_unit != NULL)
7255 {
7256 if (read_cutu_die_from_dwo (cu, dwo_unit,
7257 comp_unit_die, NULL,
7258 this, &info_ptr,
7259 &dwo_comp_unit_die,
7260 &m_dwo_abbrev_table) == 0)
7261 {
7262 /* Dummy die. */
7263 dummy_p = true;
7264 return;
7265 }
7266 comp_unit_die = dwo_comp_unit_die;
7267 }
7268 else
7269 {
7270 /* Yikes, we couldn't find the rest of the DIE, we only have
7271 the stub. A complaint has already been logged. There's
7272 not much more we can do except pass on the stub DIE to
7273 die_reader_func. We don't want to throw an error on bad
7274 debug info. */
7275 }
7276 }
7277 }
7278
7279 void
7280 cutu_reader::keep ()
7281 {
7282 /* Done, clean up. */
7283 gdb_assert (!dummy_p);
7284 if (m_new_cu != NULL)
7285 {
7286 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7287 now. */
7288 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7289 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
7290 }
7291 }
7292
7293 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7294 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7295 assumed to have already done the lookup to find the DWO file).
7296
7297 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7298 THIS_CU->is_debug_types, but nothing else.
7299
7300 We fill in THIS_CU->length.
7301
7302 THIS_CU->cu is always freed when done.
7303 This is done in order to not leave THIS_CU->cu in a state where we have
7304 to care whether it refers to the "main" CU or the DWO CU.
7305
7306 When parent_cu is passed, it is used to provide a default value for
7307 str_offsets_base and addr_base from the parent. */
7308
7309 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7310 dwarf2_per_objfile *per_objfile,
7311 struct dwarf2_cu *parent_cu,
7312 struct dwo_file *dwo_file)
7313 : die_reader_specs {},
7314 m_this_cu (this_cu)
7315 {
7316 struct objfile *objfile = per_objfile->objfile;
7317 struct dwarf2_section_info *section = this_cu->section;
7318 bfd *abfd = section->get_bfd_owner ();
7319 struct dwarf2_section_info *abbrev_section;
7320 const gdb_byte *begin_info_ptr, *info_ptr;
7321
7322 if (dwarf_die_debug)
7323 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7324 this_cu->is_debug_types ? "type" : "comp",
7325 sect_offset_str (this_cu->sect_off));
7326
7327 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7328
7329 abbrev_section = (dwo_file != NULL
7330 ? &dwo_file->sections.abbrev
7331 : get_abbrev_section_for_cu (this_cu));
7332
7333 /* This is cheap if the section is already read in. */
7334 section->read (objfile);
7335
7336 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7337
7338 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7339 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7340 section, abbrev_section, info_ptr,
7341 (this_cu->is_debug_types
7342 ? rcuh_kind::TYPE
7343 : rcuh_kind::COMPILE));
7344
7345 if (parent_cu != nullptr)
7346 {
7347 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7348 m_new_cu->addr_base = parent_cu->addr_base;
7349 }
7350 this_cu->length = m_new_cu->header.get_length ();
7351
7352 /* Skip dummy compilation units. */
7353 if (info_ptr >= begin_info_ptr + this_cu->length
7354 || peek_abbrev_code (abfd, info_ptr) == 0)
7355 {
7356 dummy_p = true;
7357 return;
7358 }
7359
7360 m_abbrev_table_holder
7361 = abbrev_table::read (objfile, abbrev_section,
7362 m_new_cu->header.abbrev_sect_off);
7363
7364 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7365 m_abbrev_table_holder.get ());
7366 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7367 }
7368
7369 \f
7370 /* Type Unit Groups.
7371
7372 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7373 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7374 so that all types coming from the same compilation (.o file) are grouped
7375 together. A future step could be to put the types in the same symtab as
7376 the CU the types ultimately came from. */
7377
7378 static hashval_t
7379 hash_type_unit_group (const void *item)
7380 {
7381 const struct type_unit_group *tu_group
7382 = (const struct type_unit_group *) item;
7383
7384 return hash_stmt_list_entry (&tu_group->hash);
7385 }
7386
7387 static int
7388 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7389 {
7390 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7391 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7392
7393 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7394 }
7395
7396 /* Allocate a hash table for type unit groups. */
7397
7398 static htab_up
7399 allocate_type_unit_groups_table ()
7400 {
7401 return htab_up (htab_create_alloc (3,
7402 hash_type_unit_group,
7403 eq_type_unit_group,
7404 NULL, xcalloc, xfree));
7405 }
7406
7407 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7408 partial symtabs. We combine several TUs per psymtab to not let the size
7409 of any one psymtab grow too big. */
7410 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7411 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7412
7413 /* Helper routine for get_type_unit_group.
7414 Create the type_unit_group object used to hold one or more TUs. */
7415
7416 static struct type_unit_group *
7417 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7418 {
7419 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7420 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7421 struct dwarf2_per_cu_data *per_cu;
7422 struct type_unit_group *tu_group;
7423
7424 tu_group = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, type_unit_group);
7425 per_cu = &tu_group->per_cu;
7426 per_cu->per_bfd = per_bfd;
7427
7428 if (per_bfd->using_index)
7429 {
7430 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7431 struct dwarf2_per_cu_quick_data);
7432 }
7433 else
7434 {
7435 unsigned int line_offset = to_underlying (line_offset_struct);
7436 dwarf2_psymtab *pst;
7437 std::string name;
7438
7439 /* Give the symtab a useful name for debug purposes. */
7440 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7441 name = string_printf ("<type_units_%d>",
7442 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7443 else
7444 name = string_printf ("<type_units_at_0x%x>", line_offset);
7445
7446 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
7447 pst->anonymous = true;
7448 }
7449
7450 tu_group->hash.dwo_unit = cu->dwo_unit;
7451 tu_group->hash.line_sect_off = line_offset_struct;
7452
7453 return tu_group;
7454 }
7455
7456 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7457 STMT_LIST is a DW_AT_stmt_list attribute. */
7458
7459 static struct type_unit_group *
7460 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7461 {
7462 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7463 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7464 struct type_unit_group *tu_group;
7465 void **slot;
7466 unsigned int line_offset;
7467 struct type_unit_group type_unit_group_for_lookup;
7468
7469 if (per_objfile->per_bfd->type_unit_groups == NULL)
7470 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7471
7472 /* Do we need to create a new group, or can we use an existing one? */
7473
7474 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
7475 {
7476 line_offset = stmt_list->as_unsigned ();
7477 ++tu_stats->nr_symtab_sharers;
7478 }
7479 else
7480 {
7481 /* Ugh, no stmt_list. Rare, but we have to handle it.
7482 We can do various things here like create one group per TU or
7483 spread them over multiple groups to split up the expansion work.
7484 To avoid worst case scenarios (too many groups or too large groups)
7485 we, umm, group them in bunches. */
7486 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7487 | (tu_stats->nr_stmt_less_type_units
7488 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7489 ++tu_stats->nr_stmt_less_type_units;
7490 }
7491
7492 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7493 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7494 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
7495 &type_unit_group_for_lookup, INSERT);
7496 if (*slot != NULL)
7497 {
7498 tu_group = (struct type_unit_group *) *slot;
7499 gdb_assert (tu_group != NULL);
7500 }
7501 else
7502 {
7503 sect_offset line_offset_struct = (sect_offset) line_offset;
7504 tu_group = create_type_unit_group (cu, line_offset_struct);
7505 *slot = tu_group;
7506 ++tu_stats->nr_symtabs;
7507 }
7508
7509 return tu_group;
7510 }
7511 \f
7512 /* Partial symbol tables. */
7513
7514 /* Create a psymtab named NAME and assign it to PER_CU.
7515
7516 The caller must fill in the following details:
7517 dirname, textlow, texthigh. */
7518
7519 static dwarf2_psymtab *
7520 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7521 dwarf2_per_objfile *per_objfile,
7522 const char *name)
7523 {
7524 struct objfile *objfile = per_objfile->objfile;
7525 dwarf2_psymtab *pst;
7526
7527 pst = new dwarf2_psymtab (name, objfile, per_cu);
7528
7529 pst->psymtabs_addrmap_supported = true;
7530
7531 /* This is the glue that links PST into GDB's symbol API. */
7532 per_cu->v.psymtab = pst;
7533
7534 return pst;
7535 }
7536
7537 /* DIE reader function for process_psymtab_comp_unit. */
7538
7539 static void
7540 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7541 const gdb_byte *info_ptr,
7542 struct die_info *comp_unit_die,
7543 enum language pretend_language)
7544 {
7545 struct dwarf2_cu *cu = reader->cu;
7546 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7547 struct objfile *objfile = per_objfile->objfile;
7548 struct gdbarch *gdbarch = objfile->arch ();
7549 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7550 CORE_ADDR baseaddr;
7551 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7552 dwarf2_psymtab *pst;
7553 enum pc_bounds_kind cu_bounds_kind;
7554 const char *filename;
7555
7556 gdb_assert (! per_cu->is_debug_types);
7557
7558 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7559
7560 /* Allocate a new partial symbol table structure. */
7561 gdb::unique_xmalloc_ptr<char> debug_filename;
7562 static const char artificial[] = "<artificial>";
7563 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7564 if (filename == NULL)
7565 filename = "";
7566 else if (strcmp (filename, artificial) == 0)
7567 {
7568 debug_filename.reset (concat (artificial, "@",
7569 sect_offset_str (per_cu->sect_off),
7570 (char *) NULL));
7571 filename = debug_filename.get ();
7572 }
7573
7574 pst = create_partial_symtab (per_cu, per_objfile, filename);
7575
7576 /* This must be done before calling dwarf2_build_include_psymtabs. */
7577 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7578
7579 baseaddr = objfile->text_section_offset ();
7580
7581 dwarf2_find_base_address (comp_unit_die, cu);
7582
7583 /* Possibly set the default values of LOWPC and HIGHPC from
7584 `DW_AT_ranges'. */
7585 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7586 &best_highpc, cu, pst);
7587 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7588 {
7589 CORE_ADDR low
7590 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7591 - baseaddr);
7592 CORE_ADDR high
7593 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7594 - baseaddr - 1);
7595 /* Store the contiguous range if it is not empty; it can be
7596 empty for CUs with no code. */
7597 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7598 low, high, pst);
7599 }
7600
7601 /* Check if comp unit has_children.
7602 If so, read the rest of the partial symbols from this comp unit.
7603 If not, there's no more debug_info for this comp unit. */
7604 if (comp_unit_die->has_children)
7605 {
7606 struct partial_die_info *first_die;
7607 CORE_ADDR lowpc, highpc;
7608
7609 lowpc = ((CORE_ADDR) -1);
7610 highpc = ((CORE_ADDR) 0);
7611
7612 first_die = load_partial_dies (reader, info_ptr, 1);
7613
7614 scan_partial_symbols (first_die, &lowpc, &highpc,
7615 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7616
7617 /* If we didn't find a lowpc, set it to highpc to avoid
7618 complaints from `maint check'. */
7619 if (lowpc == ((CORE_ADDR) -1))
7620 lowpc = highpc;
7621
7622 /* If the compilation unit didn't have an explicit address range,
7623 then use the information extracted from its child dies. */
7624 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7625 {
7626 best_lowpc = lowpc;
7627 best_highpc = highpc;
7628 }
7629 }
7630 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7631 best_lowpc + baseaddr)
7632 - baseaddr);
7633 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7634 best_highpc + baseaddr)
7635 - baseaddr);
7636
7637 pst->end ();
7638
7639 if (!cu->per_cu->imported_symtabs_empty ())
7640 {
7641 int i;
7642 int len = cu->per_cu->imported_symtabs_size ();
7643
7644 /* Fill in 'dependencies' here; we fill in 'users' in a
7645 post-pass. */
7646 pst->number_of_dependencies = len;
7647 pst->dependencies
7648 = objfile->partial_symtabs->allocate_dependencies (len);
7649 for (i = 0; i < len; ++i)
7650 {
7651 pst->dependencies[i]
7652 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7653 }
7654
7655 cu->per_cu->imported_symtabs_free ();
7656 }
7657
7658 /* Get the list of files included in the current compilation unit,
7659 and build a psymtab for each of them. */
7660 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7661
7662 if (dwarf_read_debug)
7663 fprintf_unfiltered (gdb_stdlog,
7664 "Psymtab for %s unit @%s: %s - %s"
7665 ", %d global, %d static syms\n",
7666 per_cu->is_debug_types ? "type" : "comp",
7667 sect_offset_str (per_cu->sect_off),
7668 paddress (gdbarch, pst->text_low (objfile)),
7669 paddress (gdbarch, pst->text_high (objfile)),
7670 (int) pst->global_psymbols.size (),
7671 (int) pst->static_psymbols.size ());
7672 }
7673
7674 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7675 Process compilation unit THIS_CU for a psymtab. */
7676
7677 static void
7678 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7679 dwarf2_per_objfile *per_objfile,
7680 bool want_partial_unit,
7681 enum language pretend_language)
7682 {
7683 /* If this compilation unit was already read in, free the
7684 cached copy in order to read it in again. This is
7685 necessary because we skipped some symbols when we first
7686 read in the compilation unit (see load_partial_dies).
7687 This problem could be avoided, but the benefit is unclear. */
7688 per_objfile->remove_cu (this_cu);
7689
7690 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7691
7692 switch (reader.comp_unit_die->tag)
7693 {
7694 case DW_TAG_compile_unit:
7695 this_cu->unit_type = DW_UT_compile;
7696 break;
7697 case DW_TAG_partial_unit:
7698 this_cu->unit_type = DW_UT_partial;
7699 break;
7700 default:
7701 abort ();
7702 }
7703
7704 if (reader.dummy_p)
7705 {
7706 /* Nothing. */
7707 }
7708 else if (this_cu->is_debug_types)
7709 build_type_psymtabs_reader (&reader, reader.info_ptr,
7710 reader.comp_unit_die);
7711 else if (want_partial_unit
7712 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7713 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7714 reader.comp_unit_die,
7715 pretend_language);
7716
7717 this_cu->lang = reader.cu->language;
7718
7719 /* Age out any secondary CUs. */
7720 per_objfile->age_comp_units ();
7721 }
7722
7723 /* Reader function for build_type_psymtabs. */
7724
7725 static void
7726 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7727 const gdb_byte *info_ptr,
7728 struct die_info *type_unit_die)
7729 {
7730 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7731 struct dwarf2_cu *cu = reader->cu;
7732 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7733 struct signatured_type *sig_type;
7734 struct type_unit_group *tu_group;
7735 struct attribute *attr;
7736 struct partial_die_info *first_die;
7737 CORE_ADDR lowpc, highpc;
7738 dwarf2_psymtab *pst;
7739
7740 gdb_assert (per_cu->is_debug_types);
7741 sig_type = (struct signatured_type *) per_cu;
7742
7743 if (! type_unit_die->has_children)
7744 return;
7745
7746 attr = type_unit_die->attr (DW_AT_stmt_list);
7747 tu_group = get_type_unit_group (cu, attr);
7748
7749 if (tu_group->tus == nullptr)
7750 tu_group->tus = new std::vector<signatured_type *>;
7751 tu_group->tus->push_back (sig_type);
7752
7753 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7754 pst = create_partial_symtab (per_cu, per_objfile, "");
7755 pst->anonymous = true;
7756
7757 first_die = load_partial_dies (reader, info_ptr, 1);
7758
7759 lowpc = (CORE_ADDR) -1;
7760 highpc = (CORE_ADDR) 0;
7761 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7762
7763 pst->end ();
7764 }
7765
7766 /* Struct used to sort TUs by their abbreviation table offset. */
7767
7768 struct tu_abbrev_offset
7769 {
7770 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7771 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7772 {}
7773
7774 signatured_type *sig_type;
7775 sect_offset abbrev_offset;
7776 };
7777
7778 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7779
7780 static bool
7781 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7782 const struct tu_abbrev_offset &b)
7783 {
7784 return a.abbrev_offset < b.abbrev_offset;
7785 }
7786
7787 /* Efficiently read all the type units.
7788 This does the bulk of the work for build_type_psymtabs.
7789
7790 The efficiency is because we sort TUs by the abbrev table they use and
7791 only read each abbrev table once. In one program there are 200K TUs
7792 sharing 8K abbrev tables.
7793
7794 The main purpose of this function is to support building the
7795 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7796 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7797 can collapse the search space by grouping them by stmt_list.
7798 The savings can be significant, in the same program from above the 200K TUs
7799 share 8K stmt_list tables.
7800
7801 FUNC is expected to call get_type_unit_group, which will create the
7802 struct type_unit_group if necessary and add it to
7803 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7804
7805 static void
7806 build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
7807 {
7808 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7809 abbrev_table_up abbrev_table;
7810 sect_offset abbrev_offset;
7811
7812 /* It's up to the caller to not call us multiple times. */
7813 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7814
7815 if (per_objfile->per_bfd->all_type_units.empty ())
7816 return;
7817
7818 /* TUs typically share abbrev tables, and there can be way more TUs than
7819 abbrev tables. Sort by abbrev table to reduce the number of times we
7820 read each abbrev table in.
7821 Alternatives are to punt or to maintain a cache of abbrev tables.
7822 This is simpler and efficient enough for now.
7823
7824 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7825 symtab to use). Typically TUs with the same abbrev offset have the same
7826 stmt_list value too so in practice this should work well.
7827
7828 The basic algorithm here is:
7829
7830 sort TUs by abbrev table
7831 for each TU with same abbrev table:
7832 read abbrev table if first user
7833 read TU top level DIE
7834 [IWBN if DWO skeletons had DW_AT_stmt_list]
7835 call FUNC */
7836
7837 if (dwarf_read_debug)
7838 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7839
7840 /* Sort in a separate table to maintain the order of all_type_units
7841 for .gdb_index: TU indices directly index all_type_units. */
7842 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7843 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
7844
7845 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
7846 sorted_by_abbrev.emplace_back
7847 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
7848 sig_type->per_cu.sect_off));
7849
7850 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7851 sort_tu_by_abbrev_offset);
7852
7853 abbrev_offset = (sect_offset) ~(unsigned) 0;
7854
7855 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7856 {
7857 /* Switch to the next abbrev table if necessary. */
7858 if (abbrev_table == NULL
7859 || tu.abbrev_offset != abbrev_offset)
7860 {
7861 abbrev_offset = tu.abbrev_offset;
7862 abbrev_table =
7863 abbrev_table::read (per_objfile->objfile,
7864 &per_objfile->per_bfd->abbrev, abbrev_offset);
7865 ++tu_stats->nr_uniq_abbrev_tables;
7866 }
7867
7868 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
7869 abbrev_table.get (), nullptr, false);
7870 if (!reader.dummy_p)
7871 build_type_psymtabs_reader (&reader, reader.info_ptr,
7872 reader.comp_unit_die);
7873 }
7874 }
7875
7876 /* Print collected type unit statistics. */
7877
7878 static void
7879 print_tu_stats (dwarf2_per_objfile *per_objfile)
7880 {
7881 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7882
7883 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7884 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7885 per_objfile->per_bfd->all_type_units.size ());
7886 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7887 tu_stats->nr_uniq_abbrev_tables);
7888 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7889 tu_stats->nr_symtabs);
7890 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7891 tu_stats->nr_symtab_sharers);
7892 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7893 tu_stats->nr_stmt_less_type_units);
7894 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7895 tu_stats->nr_all_type_units_reallocs);
7896 }
7897
7898 /* Traversal function for build_type_psymtabs. */
7899
7900 static int
7901 build_type_psymtab_dependencies (void **slot, void *info)
7902 {
7903 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7904 struct objfile *objfile = per_objfile->objfile;
7905 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7906 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7907 dwarf2_psymtab *pst = per_cu->v.psymtab;
7908 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7909 int i;
7910
7911 gdb_assert (len > 0);
7912 gdb_assert (per_cu->type_unit_group_p ());
7913
7914 pst->number_of_dependencies = len;
7915 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7916 for (i = 0; i < len; ++i)
7917 {
7918 struct signatured_type *iter = tu_group->tus->at (i);
7919 gdb_assert (iter->per_cu.is_debug_types);
7920 pst->dependencies[i] = iter->per_cu.v.psymtab;
7921 iter->type_unit_group = tu_group;
7922 }
7923
7924 delete tu_group->tus;
7925 tu_group->tus = nullptr;
7926
7927 return 1;
7928 }
7929
7930 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7931 Build partial symbol tables for the .debug_types comp-units. */
7932
7933 static void
7934 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
7935 {
7936 if (! create_all_type_units (per_objfile))
7937 return;
7938
7939 build_type_psymtabs_1 (per_objfile);
7940 }
7941
7942 /* Traversal function for process_skeletonless_type_unit.
7943 Read a TU in a DWO file and build partial symbols for it. */
7944
7945 static int
7946 process_skeletonless_type_unit (void **slot, void *info)
7947 {
7948 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7949 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7950 struct signatured_type find_entry, *entry;
7951
7952 /* If this TU doesn't exist in the global table, add it and read it in. */
7953
7954 if (per_objfile->per_bfd->signatured_types == NULL)
7955 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7956
7957 find_entry.signature = dwo_unit->signature;
7958 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
7959 &find_entry, INSERT);
7960 /* If we've already seen this type there's nothing to do. What's happening
7961 is we're doing our own version of comdat-folding here. */
7962 if (*slot != NULL)
7963 return 1;
7964
7965 /* This does the job that create_all_type_units would have done for
7966 this TU. */
7967 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
7968 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
7969 *slot = entry;
7970
7971 /* This does the job that build_type_psymtabs_1 would have done. */
7972 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
7973 if (!reader.dummy_p)
7974 build_type_psymtabs_reader (&reader, reader.info_ptr,
7975 reader.comp_unit_die);
7976
7977 return 1;
7978 }
7979
7980 /* Traversal function for process_skeletonless_type_units. */
7981
7982 static int
7983 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7984 {
7985 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7986
7987 if (dwo_file->tus != NULL)
7988 htab_traverse_noresize (dwo_file->tus.get (),
7989 process_skeletonless_type_unit, info);
7990
7991 return 1;
7992 }
7993
7994 /* Scan all TUs of DWO files, verifying we've processed them.
7995 This is needed in case a TU was emitted without its skeleton.
7996 Note: This can't be done until we know what all the DWO files are. */
7997
7998 static void
7999 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
8000 {
8001 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8002 if (get_dwp_file (per_objfile) == NULL
8003 && per_objfile->per_bfd->dwo_files != NULL)
8004 {
8005 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
8006 process_dwo_file_for_skeletonless_type_units,
8007 per_objfile);
8008 }
8009 }
8010
8011 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8012
8013 static void
8014 set_partial_user (dwarf2_per_objfile *per_objfile)
8015 {
8016 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
8017 {
8018 dwarf2_psymtab *pst = per_cu->v.psymtab;
8019
8020 if (pst == NULL)
8021 continue;
8022
8023 for (int j = 0; j < pst->number_of_dependencies; ++j)
8024 {
8025 /* Set the 'user' field only if it is not already set. */
8026 if (pst->dependencies[j]->user == NULL)
8027 pst->dependencies[j]->user = pst;
8028 }
8029 }
8030 }
8031
8032 /* Build the partial symbol table by doing a quick pass through the
8033 .debug_info and .debug_abbrev sections. */
8034
8035 static void
8036 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
8037 {
8038 struct objfile *objfile = per_objfile->objfile;
8039
8040 if (dwarf_read_debug)
8041 {
8042 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8043 objfile_name (objfile));
8044 }
8045
8046 scoped_restore restore_reading_psyms
8047 = make_scoped_restore (&per_objfile->per_bfd->reading_partial_symbols,
8048 true);
8049
8050 per_objfile->per_bfd->info.read (objfile);
8051
8052 /* Any cached compilation units will be linked by the per-objfile
8053 read_in_chain. Make sure to free them when we're done. */
8054 free_cached_comp_units freer (per_objfile);
8055
8056 build_type_psymtabs (per_objfile);
8057
8058 create_all_comp_units (per_objfile);
8059
8060 /* Create a temporary address map on a temporary obstack. We later
8061 copy this to the final obstack. */
8062 auto_obstack temp_obstack;
8063
8064 scoped_restore save_psymtabs_addrmap
8065 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8066 addrmap_create_mutable (&temp_obstack));
8067
8068 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
8069 {
8070 if (per_cu->v.psymtab != NULL)
8071 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8072 continue;
8073 process_psymtab_comp_unit (per_cu, per_objfile, false,
8074 language_minimal);
8075 }
8076
8077 /* This has to wait until we read the CUs, we need the list of DWOs. */
8078 process_skeletonless_type_units (per_objfile);
8079
8080 /* Now that all TUs have been processed we can fill in the dependencies. */
8081 if (per_objfile->per_bfd->type_unit_groups != NULL)
8082 {
8083 htab_traverse_noresize (per_objfile->per_bfd->type_unit_groups.get (),
8084 build_type_psymtab_dependencies, per_objfile);
8085 }
8086
8087 if (dwarf_read_debug)
8088 print_tu_stats (per_objfile);
8089
8090 set_partial_user (per_objfile);
8091
8092 objfile->partial_symtabs->psymtabs_addrmap
8093 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8094 objfile->partial_symtabs->obstack ());
8095 /* At this point we want to keep the address map. */
8096 save_psymtabs_addrmap.release ();
8097
8098 if (dwarf_read_debug)
8099 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8100 objfile_name (objfile));
8101 }
8102
8103 /* Load the partial DIEs for a secondary CU into memory.
8104 This is also used when rereading a primary CU with load_all_dies. */
8105
8106 static void
8107 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
8108 dwarf2_per_objfile *per_objfile,
8109 dwarf2_cu *existing_cu)
8110 {
8111 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
8112
8113 if (!reader.dummy_p)
8114 {
8115 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8116 language_minimal);
8117
8118 /* Check if comp unit has_children.
8119 If so, read the rest of the partial symbols from this comp unit.
8120 If not, there's no more debug_info for this comp unit. */
8121 if (reader.comp_unit_die->has_children)
8122 load_partial_dies (&reader, reader.info_ptr, 0);
8123
8124 reader.keep ();
8125 }
8126 }
8127
8128 static void
8129 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
8130 struct dwarf2_section_info *section,
8131 struct dwarf2_section_info *abbrev_section,
8132 unsigned int is_dwz)
8133 {
8134 const gdb_byte *info_ptr;
8135 struct objfile *objfile = per_objfile->objfile;
8136
8137 if (dwarf_read_debug)
8138 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8139 section->get_name (),
8140 section->get_file_name ());
8141
8142 section->read (objfile);
8143
8144 info_ptr = section->buffer;
8145
8146 while (info_ptr < section->buffer + section->size)
8147 {
8148 struct dwarf2_per_cu_data *this_cu;
8149
8150 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8151
8152 comp_unit_head cu_header;
8153 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
8154 abbrev_section, info_ptr,
8155 rcuh_kind::COMPILE);
8156
8157 /* Save the compilation unit for later lookup. */
8158 if (cu_header.unit_type != DW_UT_type)
8159 this_cu = per_objfile->per_bfd->allocate_per_cu ();
8160 else
8161 {
8162 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
8163 sig_type->signature = cu_header.signature;
8164 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8165 this_cu = &sig_type->per_cu;
8166 }
8167 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8168 this_cu->sect_off = sect_off;
8169 this_cu->length = cu_header.length + cu_header.initial_length_size;
8170 this_cu->is_dwz = is_dwz;
8171 this_cu->section = section;
8172
8173 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8174
8175 info_ptr = info_ptr + this_cu->length;
8176 }
8177 }
8178
8179 /* Create a list of all compilation units in OBJFILE.
8180 This is only done for -readnow and building partial symtabs. */
8181
8182 static void
8183 create_all_comp_units (dwarf2_per_objfile *per_objfile)
8184 {
8185 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8186 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8187 &per_objfile->per_bfd->abbrev, 0);
8188
8189 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8190 if (dwz != NULL)
8191 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
8192 }
8193
8194 /* Process all loaded DIEs for compilation unit CU, starting at
8195 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8196 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8197 DW_AT_ranges). See the comments of add_partial_subprogram on how
8198 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8199
8200 static void
8201 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8202 CORE_ADDR *highpc, int set_addrmap,
8203 struct dwarf2_cu *cu)
8204 {
8205 struct partial_die_info *pdi;
8206
8207 /* Now, march along the PDI's, descending into ones which have
8208 interesting children but skipping the children of the other ones,
8209 until we reach the end of the compilation unit. */
8210
8211 pdi = first_die;
8212
8213 while (pdi != NULL)
8214 {
8215 pdi->fixup (cu);
8216
8217 /* Anonymous namespaces or modules have no name but have interesting
8218 children, so we need to look at them. Ditto for anonymous
8219 enums. */
8220
8221 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
8222 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8223 || pdi->tag == DW_TAG_imported_unit
8224 || pdi->tag == DW_TAG_inlined_subroutine)
8225 {
8226 switch (pdi->tag)
8227 {
8228 case DW_TAG_subprogram:
8229 case DW_TAG_inlined_subroutine:
8230 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8231 if (cu->language == language_cplus)
8232 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8233 set_addrmap, cu);
8234 break;
8235 case DW_TAG_constant:
8236 case DW_TAG_variable:
8237 case DW_TAG_typedef:
8238 case DW_TAG_union_type:
8239 if (!pdi->is_declaration
8240 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8241 {
8242 add_partial_symbol (pdi, cu);
8243 }
8244 break;
8245 case DW_TAG_class_type:
8246 case DW_TAG_interface_type:
8247 case DW_TAG_structure_type:
8248 if (!pdi->is_declaration)
8249 {
8250 add_partial_symbol (pdi, cu);
8251 }
8252 if ((cu->language == language_rust
8253 || cu->language == language_cplus) && pdi->has_children)
8254 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8255 set_addrmap, cu);
8256 break;
8257 case DW_TAG_enumeration_type:
8258 if (!pdi->is_declaration)
8259 add_partial_enumeration (pdi, cu);
8260 break;
8261 case DW_TAG_base_type:
8262 case DW_TAG_subrange_type:
8263 /* File scope base type definitions are added to the partial
8264 symbol table. */
8265 add_partial_symbol (pdi, cu);
8266 break;
8267 case DW_TAG_namespace:
8268 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8269 break;
8270 case DW_TAG_module:
8271 if (!pdi->is_declaration)
8272 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8273 break;
8274 case DW_TAG_imported_unit:
8275 {
8276 struct dwarf2_per_cu_data *per_cu;
8277
8278 /* For now we don't handle imported units in type units. */
8279 if (cu->per_cu->is_debug_types)
8280 {
8281 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8282 " supported in type units [in module %s]"),
8283 objfile_name (cu->per_objfile->objfile));
8284 }
8285
8286 per_cu = dwarf2_find_containing_comp_unit
8287 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8288
8289 /* Go read the partial unit, if needed. */
8290 if (per_cu->v.psymtab == NULL)
8291 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8292 cu->language);
8293
8294 cu->per_cu->imported_symtabs_push (per_cu);
8295 }
8296 break;
8297 case DW_TAG_imported_declaration:
8298 add_partial_symbol (pdi, cu);
8299 break;
8300 default:
8301 break;
8302 }
8303 }
8304
8305 /* If the die has a sibling, skip to the sibling. */
8306
8307 pdi = pdi->die_sibling;
8308 }
8309 }
8310
8311 /* Functions used to compute the fully scoped name of a partial DIE.
8312
8313 Normally, this is simple. For C++, the parent DIE's fully scoped
8314 name is concatenated with "::" and the partial DIE's name.
8315 Enumerators are an exception; they use the scope of their parent
8316 enumeration type, i.e. the name of the enumeration type is not
8317 prepended to the enumerator.
8318
8319 There are two complexities. One is DW_AT_specification; in this
8320 case "parent" means the parent of the target of the specification,
8321 instead of the direct parent of the DIE. The other is compilers
8322 which do not emit DW_TAG_namespace; in this case we try to guess
8323 the fully qualified name of structure types from their members'
8324 linkage names. This must be done using the DIE's children rather
8325 than the children of any DW_AT_specification target. We only need
8326 to do this for structures at the top level, i.e. if the target of
8327 any DW_AT_specification (if any; otherwise the DIE itself) does not
8328 have a parent. */
8329
8330 /* Compute the scope prefix associated with PDI's parent, in
8331 compilation unit CU. The result will be allocated on CU's
8332 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8333 field. NULL is returned if no prefix is necessary. */
8334 static const char *
8335 partial_die_parent_scope (struct partial_die_info *pdi,
8336 struct dwarf2_cu *cu)
8337 {
8338 const char *grandparent_scope;
8339 struct partial_die_info *parent, *real_pdi;
8340
8341 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8342 then this means the parent of the specification DIE. */
8343
8344 real_pdi = pdi;
8345 while (real_pdi->has_specification)
8346 {
8347 auto res = find_partial_die (real_pdi->spec_offset,
8348 real_pdi->spec_is_dwz, cu);
8349 real_pdi = res.pdi;
8350 cu = res.cu;
8351 }
8352
8353 parent = real_pdi->die_parent;
8354 if (parent == NULL)
8355 return NULL;
8356
8357 if (parent->scope_set)
8358 return parent->scope;
8359
8360 parent->fixup (cu);
8361
8362 grandparent_scope = partial_die_parent_scope (parent, cu);
8363
8364 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8365 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8366 Work around this problem here. */
8367 if (cu->language == language_cplus
8368 && parent->tag == DW_TAG_namespace
8369 && strcmp (parent->name (cu), "::") == 0
8370 && grandparent_scope == NULL)
8371 {
8372 parent->scope = NULL;
8373 parent->scope_set = 1;
8374 return NULL;
8375 }
8376
8377 /* Nested subroutines in Fortran get a prefix. */
8378 if (pdi->tag == DW_TAG_enumerator)
8379 /* Enumerators should not get the name of the enumeration as a prefix. */
8380 parent->scope = grandparent_scope;
8381 else if (parent->tag == DW_TAG_namespace
8382 || parent->tag == DW_TAG_module
8383 || parent->tag == DW_TAG_structure_type
8384 || parent->tag == DW_TAG_class_type
8385 || parent->tag == DW_TAG_interface_type
8386 || parent->tag == DW_TAG_union_type
8387 || parent->tag == DW_TAG_enumeration_type
8388 || (cu->language == language_fortran
8389 && parent->tag == DW_TAG_subprogram
8390 && pdi->tag == DW_TAG_subprogram))
8391 {
8392 if (grandparent_scope == NULL)
8393 parent->scope = parent->name (cu);
8394 else
8395 parent->scope = typename_concat (&cu->comp_unit_obstack,
8396 grandparent_scope,
8397 parent->name (cu), 0, cu);
8398 }
8399 else
8400 {
8401 /* FIXME drow/2004-04-01: What should we be doing with
8402 function-local names? For partial symbols, we should probably be
8403 ignoring them. */
8404 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8405 dwarf_tag_name (parent->tag),
8406 sect_offset_str (pdi->sect_off));
8407 parent->scope = grandparent_scope;
8408 }
8409
8410 parent->scope_set = 1;
8411 return parent->scope;
8412 }
8413
8414 /* Return the fully scoped name associated with PDI, from compilation unit
8415 CU. The result will be allocated with malloc. */
8416
8417 static gdb::unique_xmalloc_ptr<char>
8418 partial_die_full_name (struct partial_die_info *pdi,
8419 struct dwarf2_cu *cu)
8420 {
8421 const char *parent_scope;
8422
8423 /* If this is a template instantiation, we can not work out the
8424 template arguments from partial DIEs. So, unfortunately, we have
8425 to go through the full DIEs. At least any work we do building
8426 types here will be reused if full symbols are loaded later. */
8427 if (pdi->has_template_arguments)
8428 {
8429 pdi->fixup (cu);
8430
8431 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
8432 {
8433 struct die_info *die;
8434 struct attribute attr;
8435 struct dwarf2_cu *ref_cu = cu;
8436
8437 /* DW_FORM_ref_addr is using section offset. */
8438 attr.name = (enum dwarf_attribute) 0;
8439 attr.form = DW_FORM_ref_addr;
8440 attr.u.unsnd = to_underlying (pdi->sect_off);
8441 die = follow_die_ref (NULL, &attr, &ref_cu);
8442
8443 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8444 }
8445 }
8446
8447 parent_scope = partial_die_parent_scope (pdi, cu);
8448 if (parent_scope == NULL)
8449 return NULL;
8450 else
8451 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8452 pdi->name (cu),
8453 0, cu));
8454 }
8455
8456 static void
8457 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8458 {
8459 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8460 struct objfile *objfile = per_objfile->objfile;
8461 struct gdbarch *gdbarch = objfile->arch ();
8462 CORE_ADDR addr = 0;
8463 const char *actual_name = NULL;
8464 CORE_ADDR baseaddr;
8465
8466 baseaddr = objfile->text_section_offset ();
8467
8468 gdb::unique_xmalloc_ptr<char> built_actual_name
8469 = partial_die_full_name (pdi, cu);
8470 if (built_actual_name != NULL)
8471 actual_name = built_actual_name.get ();
8472
8473 if (actual_name == NULL)
8474 actual_name = pdi->name (cu);
8475
8476 partial_symbol psymbol;
8477 memset (&psymbol, 0, sizeof (psymbol));
8478 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8479 psymbol.ginfo.section = -1;
8480
8481 /* The code below indicates that the psymbol should be installed by
8482 setting this. */
8483 gdb::optional<psymbol_placement> where;
8484
8485 switch (pdi->tag)
8486 {
8487 case DW_TAG_inlined_subroutine:
8488 case DW_TAG_subprogram:
8489 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8490 - baseaddr);
8491 if (pdi->is_external
8492 || cu->language == language_ada
8493 || (cu->language == language_fortran
8494 && pdi->die_parent != NULL
8495 && pdi->die_parent->tag == DW_TAG_subprogram))
8496 {
8497 /* Normally, only "external" DIEs are part of the global scope.
8498 But in Ada and Fortran, we want to be able to access nested
8499 procedures globally. So all Ada and Fortran subprograms are
8500 stored in the global scope. */
8501 where = psymbol_placement::GLOBAL;
8502 }
8503 else
8504 where = psymbol_placement::STATIC;
8505
8506 psymbol.domain = VAR_DOMAIN;
8507 psymbol.aclass = LOC_BLOCK;
8508 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8509 psymbol.ginfo.value.address = addr;
8510
8511 if (pdi->main_subprogram && actual_name != NULL)
8512 set_objfile_main_name (objfile, actual_name, cu->language);
8513 break;
8514 case DW_TAG_constant:
8515 psymbol.domain = VAR_DOMAIN;
8516 psymbol.aclass = LOC_STATIC;
8517 where = (pdi->is_external
8518 ? psymbol_placement::GLOBAL
8519 : psymbol_placement::STATIC);
8520 break;
8521 case DW_TAG_variable:
8522 if (pdi->d.locdesc)
8523 addr = decode_locdesc (pdi->d.locdesc, cu);
8524
8525 if (pdi->d.locdesc
8526 && addr == 0
8527 && !per_objfile->per_bfd->has_section_at_zero)
8528 {
8529 /* A global or static variable may also have been stripped
8530 out by the linker if unused, in which case its address
8531 will be nullified; do not add such variables into partial
8532 symbol table then. */
8533 }
8534 else if (pdi->is_external)
8535 {
8536 /* Global Variable.
8537 Don't enter into the minimal symbol tables as there is
8538 a minimal symbol table entry from the ELF symbols already.
8539 Enter into partial symbol table if it has a location
8540 descriptor or a type.
8541 If the location descriptor is missing, new_symbol will create
8542 a LOC_UNRESOLVED symbol, the address of the variable will then
8543 be determined from the minimal symbol table whenever the variable
8544 is referenced.
8545 The address for the partial symbol table entry is not
8546 used by GDB, but it comes in handy for debugging partial symbol
8547 table building. */
8548
8549 if (pdi->d.locdesc || pdi->has_type)
8550 {
8551 psymbol.domain = VAR_DOMAIN;
8552 psymbol.aclass = LOC_STATIC;
8553 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8554 psymbol.ginfo.value.address = addr;
8555 where = psymbol_placement::GLOBAL;
8556 }
8557 }
8558 else
8559 {
8560 int has_loc = pdi->d.locdesc != NULL;
8561
8562 /* Static Variable. Skip symbols whose value we cannot know (those
8563 without location descriptors or constant values). */
8564 if (!has_loc && !pdi->has_const_value)
8565 return;
8566
8567 psymbol.domain = VAR_DOMAIN;
8568 psymbol.aclass = LOC_STATIC;
8569 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8570 if (has_loc)
8571 psymbol.ginfo.value.address = addr;
8572 where = psymbol_placement::STATIC;
8573 }
8574 break;
8575 case DW_TAG_typedef:
8576 case DW_TAG_base_type:
8577 case DW_TAG_subrange_type:
8578 psymbol.domain = VAR_DOMAIN;
8579 psymbol.aclass = LOC_TYPEDEF;
8580 where = psymbol_placement::STATIC;
8581 break;
8582 case DW_TAG_imported_declaration:
8583 case DW_TAG_namespace:
8584 psymbol.domain = VAR_DOMAIN;
8585 psymbol.aclass = LOC_TYPEDEF;
8586 where = psymbol_placement::GLOBAL;
8587 break;
8588 case DW_TAG_module:
8589 /* With Fortran 77 there might be a "BLOCK DATA" module
8590 available without any name. If so, we skip the module as it
8591 doesn't bring any value. */
8592 if (actual_name != nullptr)
8593 {
8594 psymbol.domain = MODULE_DOMAIN;
8595 psymbol.aclass = LOC_TYPEDEF;
8596 where = psymbol_placement::GLOBAL;
8597 }
8598 break;
8599 case DW_TAG_class_type:
8600 case DW_TAG_interface_type:
8601 case DW_TAG_structure_type:
8602 case DW_TAG_union_type:
8603 case DW_TAG_enumeration_type:
8604 /* Skip external references. The DWARF standard says in the section
8605 about "Structure, Union, and Class Type Entries": "An incomplete
8606 structure, union or class type is represented by a structure,
8607 union or class entry that does not have a byte size attribute
8608 and that has a DW_AT_declaration attribute." */
8609 if (!pdi->has_byte_size && pdi->is_declaration)
8610 return;
8611
8612 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8613 static vs. global. */
8614 psymbol.domain = STRUCT_DOMAIN;
8615 psymbol.aclass = LOC_TYPEDEF;
8616 where = (cu->language == language_cplus
8617 ? psymbol_placement::GLOBAL
8618 : psymbol_placement::STATIC);
8619 break;
8620 case DW_TAG_enumerator:
8621 psymbol.domain = VAR_DOMAIN;
8622 psymbol.aclass = LOC_CONST;
8623 where = (cu->language == language_cplus
8624 ? psymbol_placement::GLOBAL
8625 : psymbol_placement::STATIC);
8626 break;
8627 default:
8628 break;
8629 }
8630
8631 if (where.has_value ())
8632 {
8633 if (built_actual_name != nullptr)
8634 actual_name = objfile->intern (actual_name);
8635 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8636 psymbol.ginfo.set_linkage_name (actual_name);
8637 else
8638 {
8639 psymbol.ginfo.set_demangled_name (actual_name,
8640 &objfile->objfile_obstack);
8641 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8642 }
8643 cu->per_cu->v.psymtab->add_psymbol (psymbol, *where, objfile);
8644 }
8645 }
8646
8647 /* Read a partial die corresponding to a namespace; also, add a symbol
8648 corresponding to that namespace to the symbol table. NAMESPACE is
8649 the name of the enclosing namespace. */
8650
8651 static void
8652 add_partial_namespace (struct partial_die_info *pdi,
8653 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8654 int set_addrmap, struct dwarf2_cu *cu)
8655 {
8656 /* Add a symbol for the namespace. */
8657
8658 add_partial_symbol (pdi, cu);
8659
8660 /* Now scan partial symbols in that namespace. */
8661
8662 if (pdi->has_children)
8663 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8664 }
8665
8666 /* Read a partial die corresponding to a Fortran module. */
8667
8668 static void
8669 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8670 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8671 {
8672 /* Add a symbol for the namespace. */
8673
8674 add_partial_symbol (pdi, cu);
8675
8676 /* Now scan partial symbols in that module. */
8677
8678 if (pdi->has_children)
8679 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8680 }
8681
8682 /* Read a partial die corresponding to a subprogram or an inlined
8683 subprogram and create a partial symbol for that subprogram.
8684 When the CU language allows it, this routine also defines a partial
8685 symbol for each nested subprogram that this subprogram contains.
8686 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8687 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8688
8689 PDI may also be a lexical block, in which case we simply search
8690 recursively for subprograms defined inside that lexical block.
8691 Again, this is only performed when the CU language allows this
8692 type of definitions. */
8693
8694 static void
8695 add_partial_subprogram (struct partial_die_info *pdi,
8696 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8697 int set_addrmap, struct dwarf2_cu *cu)
8698 {
8699 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8700 {
8701 if (pdi->has_pc_info)
8702 {
8703 if (pdi->lowpc < *lowpc)
8704 *lowpc = pdi->lowpc;
8705 if (pdi->highpc > *highpc)
8706 *highpc = pdi->highpc;
8707 if (set_addrmap)
8708 {
8709 struct objfile *objfile = cu->per_objfile->objfile;
8710 struct gdbarch *gdbarch = objfile->arch ();
8711 CORE_ADDR baseaddr;
8712 CORE_ADDR this_highpc;
8713 CORE_ADDR this_lowpc;
8714
8715 baseaddr = objfile->text_section_offset ();
8716 this_lowpc
8717 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8718 pdi->lowpc + baseaddr)
8719 - baseaddr);
8720 this_highpc
8721 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8722 pdi->highpc + baseaddr)
8723 - baseaddr);
8724 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8725 this_lowpc, this_highpc - 1,
8726 cu->per_cu->v.psymtab);
8727 }
8728 }
8729
8730 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8731 {
8732 if (!pdi->is_declaration)
8733 /* Ignore subprogram DIEs that do not have a name, they are
8734 illegal. Do not emit a complaint at this point, we will
8735 do so when we convert this psymtab into a symtab. */
8736 if (pdi->name (cu))
8737 add_partial_symbol (pdi, cu);
8738 }
8739 }
8740
8741 if (! pdi->has_children)
8742 return;
8743
8744 if (cu->language == language_ada || cu->language == language_fortran)
8745 {
8746 pdi = pdi->die_child;
8747 while (pdi != NULL)
8748 {
8749 pdi->fixup (cu);
8750 if (pdi->tag == DW_TAG_subprogram
8751 || pdi->tag == DW_TAG_inlined_subroutine
8752 || pdi->tag == DW_TAG_lexical_block)
8753 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8754 pdi = pdi->die_sibling;
8755 }
8756 }
8757 }
8758
8759 /* Read a partial die corresponding to an enumeration type. */
8760
8761 static void
8762 add_partial_enumeration (struct partial_die_info *enum_pdi,
8763 struct dwarf2_cu *cu)
8764 {
8765 struct partial_die_info *pdi;
8766
8767 if (enum_pdi->name (cu) != NULL)
8768 add_partial_symbol (enum_pdi, cu);
8769
8770 pdi = enum_pdi->die_child;
8771 while (pdi)
8772 {
8773 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8774 complaint (_("malformed enumerator DIE ignored"));
8775 else
8776 add_partial_symbol (pdi, cu);
8777 pdi = pdi->die_sibling;
8778 }
8779 }
8780
8781 /* Return the initial uleb128 in the die at INFO_PTR. */
8782
8783 static unsigned int
8784 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8785 {
8786 unsigned int bytes_read;
8787
8788 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8789 }
8790
8791 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8792 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8793
8794 Return the corresponding abbrev, or NULL if the number is zero (indicating
8795 an empty DIE). In either case *BYTES_READ will be set to the length of
8796 the initial number. */
8797
8798 static struct abbrev_info *
8799 peek_die_abbrev (const die_reader_specs &reader,
8800 const gdb_byte *info_ptr, unsigned int *bytes_read)
8801 {
8802 dwarf2_cu *cu = reader.cu;
8803 bfd *abfd = cu->per_objfile->objfile->obfd;
8804 unsigned int abbrev_number
8805 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8806
8807 if (abbrev_number == 0)
8808 return NULL;
8809
8810 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8811 if (!abbrev)
8812 {
8813 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8814 " at offset %s [in module %s]"),
8815 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8816 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8817 }
8818
8819 return abbrev;
8820 }
8821
8822 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8823 Returns a pointer to the end of a series of DIEs, terminated by an empty
8824 DIE. Any children of the skipped DIEs will also be skipped. */
8825
8826 static const gdb_byte *
8827 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8828 {
8829 while (1)
8830 {
8831 unsigned int bytes_read;
8832 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8833
8834 if (abbrev == NULL)
8835 return info_ptr + bytes_read;
8836 else
8837 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8838 }
8839 }
8840
8841 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8842 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8843 abbrev corresponding to that skipped uleb128 should be passed in
8844 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8845 children. */
8846
8847 static const gdb_byte *
8848 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8849 struct abbrev_info *abbrev)
8850 {
8851 unsigned int bytes_read;
8852 struct attribute attr;
8853 bfd *abfd = reader->abfd;
8854 struct dwarf2_cu *cu = reader->cu;
8855 const gdb_byte *buffer = reader->buffer;
8856 const gdb_byte *buffer_end = reader->buffer_end;
8857 unsigned int form, i;
8858
8859 for (i = 0; i < abbrev->num_attrs; i++)
8860 {
8861 /* The only abbrev we care about is DW_AT_sibling. */
8862 if (abbrev->attrs[i].name == DW_AT_sibling)
8863 {
8864 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8865 if (attr.form == DW_FORM_ref_addr)
8866 complaint (_("ignoring absolute DW_AT_sibling"));
8867 else
8868 {
8869 sect_offset off = attr.get_ref_die_offset ();
8870 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8871
8872 if (sibling_ptr < info_ptr)
8873 complaint (_("DW_AT_sibling points backwards"));
8874 else if (sibling_ptr > reader->buffer_end)
8875 reader->die_section->overflow_complaint ();
8876 else
8877 return sibling_ptr;
8878 }
8879 }
8880
8881 /* If it isn't DW_AT_sibling, skip this attribute. */
8882 form = abbrev->attrs[i].form;
8883 skip_attribute:
8884 switch (form)
8885 {
8886 case DW_FORM_ref_addr:
8887 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8888 and later it is offset sized. */
8889 if (cu->header.version == 2)
8890 info_ptr += cu->header.addr_size;
8891 else
8892 info_ptr += cu->header.offset_size;
8893 break;
8894 case DW_FORM_GNU_ref_alt:
8895 info_ptr += cu->header.offset_size;
8896 break;
8897 case DW_FORM_addr:
8898 info_ptr += cu->header.addr_size;
8899 break;
8900 case DW_FORM_data1:
8901 case DW_FORM_ref1:
8902 case DW_FORM_flag:
8903 case DW_FORM_strx1:
8904 info_ptr += 1;
8905 break;
8906 case DW_FORM_flag_present:
8907 case DW_FORM_implicit_const:
8908 break;
8909 case DW_FORM_data2:
8910 case DW_FORM_ref2:
8911 case DW_FORM_strx2:
8912 info_ptr += 2;
8913 break;
8914 case DW_FORM_strx3:
8915 info_ptr += 3;
8916 break;
8917 case DW_FORM_data4:
8918 case DW_FORM_ref4:
8919 case DW_FORM_strx4:
8920 info_ptr += 4;
8921 break;
8922 case DW_FORM_data8:
8923 case DW_FORM_ref8:
8924 case DW_FORM_ref_sig8:
8925 info_ptr += 8;
8926 break;
8927 case DW_FORM_data16:
8928 info_ptr += 16;
8929 break;
8930 case DW_FORM_string:
8931 read_direct_string (abfd, info_ptr, &bytes_read);
8932 info_ptr += bytes_read;
8933 break;
8934 case DW_FORM_sec_offset:
8935 case DW_FORM_strp:
8936 case DW_FORM_GNU_strp_alt:
8937 info_ptr += cu->header.offset_size;
8938 break;
8939 case DW_FORM_exprloc:
8940 case DW_FORM_block:
8941 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8942 info_ptr += bytes_read;
8943 break;
8944 case DW_FORM_block1:
8945 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8946 break;
8947 case DW_FORM_block2:
8948 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8949 break;
8950 case DW_FORM_block4:
8951 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8952 break;
8953 case DW_FORM_addrx:
8954 case DW_FORM_strx:
8955 case DW_FORM_sdata:
8956 case DW_FORM_udata:
8957 case DW_FORM_ref_udata:
8958 case DW_FORM_GNU_addr_index:
8959 case DW_FORM_GNU_str_index:
8960 case DW_FORM_rnglistx:
8961 case DW_FORM_loclistx:
8962 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8963 break;
8964 case DW_FORM_indirect:
8965 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8966 info_ptr += bytes_read;
8967 /* We need to continue parsing from here, so just go back to
8968 the top. */
8969 goto skip_attribute;
8970
8971 default:
8972 error (_("Dwarf Error: Cannot handle %s "
8973 "in DWARF reader [in module %s]"),
8974 dwarf_form_name (form),
8975 bfd_get_filename (abfd));
8976 }
8977 }
8978
8979 if (abbrev->has_children)
8980 return skip_children (reader, info_ptr);
8981 else
8982 return info_ptr;
8983 }
8984
8985 /* Locate ORIG_PDI's sibling.
8986 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8987
8988 static const gdb_byte *
8989 locate_pdi_sibling (const struct die_reader_specs *reader,
8990 struct partial_die_info *orig_pdi,
8991 const gdb_byte *info_ptr)
8992 {
8993 /* Do we know the sibling already? */
8994
8995 if (orig_pdi->sibling)
8996 return orig_pdi->sibling;
8997
8998 /* Are there any children to deal with? */
8999
9000 if (!orig_pdi->has_children)
9001 return info_ptr;
9002
9003 /* Skip the children the long way. */
9004
9005 return skip_children (reader, info_ptr);
9006 }
9007
9008 /* Expand this partial symbol table into a full symbol table. SELF is
9009 not NULL. */
9010
9011 void
9012 dwarf2_psymtab::read_symtab (struct objfile *objfile)
9013 {
9014 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9015
9016 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
9017
9018 /* If this psymtab is constructed from a debug-only objfile, the
9019 has_section_at_zero flag will not necessarily be correct. We
9020 can get the correct value for this flag by looking at the data
9021 associated with the (presumably stripped) associated objfile. */
9022 if (objfile->separate_debug_objfile_backlink)
9023 {
9024 dwarf2_per_objfile *per_objfile_backlink
9025 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9026
9027 per_objfile->per_bfd->has_section_at_zero
9028 = per_objfile_backlink->per_bfd->has_section_at_zero;
9029 }
9030
9031 expand_psymtab (objfile);
9032
9033 process_cu_includes (per_objfile);
9034 }
9035 \f
9036 /* Reading in full CUs. */
9037
9038 /* Add PER_CU to the queue. */
9039
9040 static void
9041 queue_comp_unit (dwarf2_per_cu_data *per_cu,
9042 dwarf2_per_objfile *per_objfile,
9043 enum language pretend_language)
9044 {
9045 per_cu->queued = 1;
9046 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
9047 }
9048
9049 /* If PER_CU is not yet queued, add it to the queue.
9050 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9051 dependency.
9052 The result is non-zero if PER_CU was queued, otherwise the result is zero
9053 meaning either PER_CU is already queued or it is already loaded.
9054
9055 N.B. There is an invariant here that if a CU is queued then it is loaded.
9056 The caller is required to load PER_CU if we return non-zero. */
9057
9058 static int
9059 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9060 dwarf2_per_cu_data *per_cu,
9061 dwarf2_per_objfile *per_objfile,
9062 enum language pretend_language)
9063 {
9064 /* We may arrive here during partial symbol reading, if we need full
9065 DIEs to process an unusual case (e.g. template arguments). Do
9066 not queue PER_CU, just tell our caller to load its DIEs. */
9067 if (per_cu->per_bfd->reading_partial_symbols)
9068 {
9069 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9070
9071 if (cu == NULL || cu->dies == NULL)
9072 return 1;
9073 return 0;
9074 }
9075
9076 /* Mark the dependence relation so that we don't flush PER_CU
9077 too early. */
9078 if (dependent_cu != NULL)
9079 dwarf2_add_dependence (dependent_cu, per_cu);
9080
9081 /* If it's already on the queue, we have nothing to do. */
9082 if (per_cu->queued)
9083 return 0;
9084
9085 /* If the compilation unit is already loaded, just mark it as
9086 used. */
9087 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9088 if (cu != nullptr)
9089 {
9090 cu->last_used = 0;
9091 return 0;
9092 }
9093
9094 /* Add it to the queue. */
9095 queue_comp_unit (per_cu, per_objfile, pretend_language);
9096
9097 return 1;
9098 }
9099
9100 /* Process the queue. */
9101
9102 static void
9103 process_queue (dwarf2_per_objfile *per_objfile)
9104 {
9105 if (dwarf_read_debug)
9106 {
9107 fprintf_unfiltered (gdb_stdlog,
9108 "Expanding one or more symtabs of objfile %s ...\n",
9109 objfile_name (per_objfile->objfile));
9110 }
9111
9112 /* The queue starts out with one item, but following a DIE reference
9113 may load a new CU, adding it to the end of the queue. */
9114 while (!per_objfile->per_bfd->queue.empty ())
9115 {
9116 dwarf2_queue_item &item = per_objfile->per_bfd->queue.front ();
9117 dwarf2_per_cu_data *per_cu = item.per_cu;
9118
9119 if (!per_objfile->symtab_set_p (per_cu))
9120 {
9121 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9122
9123 /* Skip dummy CUs. */
9124 if (cu != nullptr)
9125 {
9126 unsigned int debug_print_threshold;
9127 char buf[100];
9128
9129 if (per_cu->is_debug_types)
9130 {
9131 struct signatured_type *sig_type =
9132 (struct signatured_type *) per_cu;
9133
9134 sprintf (buf, "TU %s at offset %s",
9135 hex_string (sig_type->signature),
9136 sect_offset_str (per_cu->sect_off));
9137 /* There can be 100s of TUs.
9138 Only print them in verbose mode. */
9139 debug_print_threshold = 2;
9140 }
9141 else
9142 {
9143 sprintf (buf, "CU at offset %s",
9144 sect_offset_str (per_cu->sect_off));
9145 debug_print_threshold = 1;
9146 }
9147
9148 if (dwarf_read_debug >= debug_print_threshold)
9149 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9150
9151 if (per_cu->is_debug_types)
9152 process_full_type_unit (cu, item.pretend_language);
9153 else
9154 process_full_comp_unit (cu, item.pretend_language);
9155
9156 if (dwarf_read_debug >= debug_print_threshold)
9157 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9158 }
9159 }
9160
9161 per_cu->queued = 0;
9162 per_objfile->per_bfd->queue.pop ();
9163 }
9164
9165 if (dwarf_read_debug)
9166 {
9167 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9168 objfile_name (per_objfile->objfile));
9169 }
9170 }
9171
9172 /* Read in full symbols for PST, and anything it depends on. */
9173
9174 void
9175 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9176 {
9177 gdb_assert (!readin_p (objfile));
9178
9179 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9180 free_cached_comp_units freer (per_objfile);
9181 expand_dependencies (objfile);
9182
9183 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9184 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9185 }
9186
9187 /* See psympriv.h. */
9188
9189 bool
9190 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9191 {
9192 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9193 return per_objfile->symtab_set_p (per_cu_data);
9194 }
9195
9196 /* See psympriv.h. */
9197
9198 compunit_symtab *
9199 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9200 {
9201 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9202 return per_objfile->get_symtab (per_cu_data);
9203 }
9204
9205 /* Trivial hash function for die_info: the hash value of a DIE
9206 is its offset in .debug_info for this objfile. */
9207
9208 static hashval_t
9209 die_hash (const void *item)
9210 {
9211 const struct die_info *die = (const struct die_info *) item;
9212
9213 return to_underlying (die->sect_off);
9214 }
9215
9216 /* Trivial comparison function for die_info structures: two DIEs
9217 are equal if they have the same offset. */
9218
9219 static int
9220 die_eq (const void *item_lhs, const void *item_rhs)
9221 {
9222 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9223 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9224
9225 return die_lhs->sect_off == die_rhs->sect_off;
9226 }
9227
9228 /* Load the DIEs associated with PER_CU into memory.
9229
9230 In some cases, the caller, while reading partial symbols, will need to load
9231 the full symbols for the CU for some reason. It will already have a
9232 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
9233 rather than creating a new one. */
9234
9235 static void
9236 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9237 dwarf2_per_objfile *per_objfile,
9238 dwarf2_cu *existing_cu,
9239 bool skip_partial,
9240 enum language pretend_language)
9241 {
9242 gdb_assert (! this_cu->is_debug_types);
9243
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 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9861
9862 /* If we don't have a symtab, we can just skip this case. */
9863 if (cust == NULL)
9864 return;
9865
9866 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9867 htab_eq_pointer,
9868 NULL, xcalloc, xfree));
9869 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9870 htab_eq_pointer,
9871 NULL, xcalloc, xfree));
9872
9873 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9874 {
9875 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9876 all_type_symtabs.get (), ptr,
9877 per_objfile, cust);
9878 }
9879
9880 /* Now we have a transitive closure of all the included symtabs. */
9881 len = result_symtabs.size ();
9882 cust->includes
9883 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9884 struct compunit_symtab *, len + 1);
9885 memcpy (cust->includes, result_symtabs.data (),
9886 len * sizeof (compunit_symtab *));
9887 cust->includes[len] = NULL;
9888 }
9889 }
9890
9891 /* Compute the 'includes' field for the symtabs of all the CUs we just
9892 read. */
9893
9894 static void
9895 process_cu_includes (dwarf2_per_objfile *per_objfile)
9896 {
9897 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
9898 {
9899 if (! iter->is_debug_types)
9900 compute_compunit_symtab_includes (iter, per_objfile);
9901 }
9902
9903 per_objfile->per_bfd->just_read_cus.clear ();
9904 }
9905
9906 /* Generate full symbol information for CU, whose DIEs have
9907 already been loaded into memory. */
9908
9909 static void
9910 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9911 {
9912 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9913 struct objfile *objfile = per_objfile->objfile;
9914 struct gdbarch *gdbarch = objfile->arch ();
9915 CORE_ADDR lowpc, highpc;
9916 struct compunit_symtab *cust;
9917 CORE_ADDR baseaddr;
9918 struct block *static_block;
9919 CORE_ADDR addr;
9920
9921 baseaddr = objfile->text_section_offset ();
9922
9923 /* Clear the list here in case something was left over. */
9924 cu->method_list.clear ();
9925
9926 cu->language = pretend_language;
9927 cu->language_defn = language_def (cu->language);
9928
9929 dwarf2_find_base_address (cu->dies, cu);
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, per_objfile->get_cu (per_cu),
10112 false, cu->language);
10113
10114 cu->per_cu->imported_symtabs_push (per_cu);
10115 }
10116 }
10117
10118 /* RAII object that represents a process_die scope: i.e.,
10119 starts/finishes processing a DIE. */
10120 class process_die_scope
10121 {
10122 public:
10123 process_die_scope (die_info *die, dwarf2_cu *cu)
10124 : m_die (die), m_cu (cu)
10125 {
10126 /* We should only be processing DIEs not already in process. */
10127 gdb_assert (!m_die->in_process);
10128 m_die->in_process = true;
10129 }
10130
10131 ~process_die_scope ()
10132 {
10133 m_die->in_process = false;
10134
10135 /* If we're done processing the DIE for the CU that owns the line
10136 header, we don't need the line header anymore. */
10137 if (m_cu->line_header_die_owner == m_die)
10138 {
10139 delete m_cu->line_header;
10140 m_cu->line_header = NULL;
10141 m_cu->line_header_die_owner = NULL;
10142 }
10143 }
10144
10145 private:
10146 die_info *m_die;
10147 dwarf2_cu *m_cu;
10148 };
10149
10150 /* Process a die and its children. */
10151
10152 static void
10153 process_die (struct die_info *die, struct dwarf2_cu *cu)
10154 {
10155 process_die_scope scope (die, cu);
10156
10157 switch (die->tag)
10158 {
10159 case DW_TAG_padding:
10160 break;
10161 case DW_TAG_compile_unit:
10162 case DW_TAG_partial_unit:
10163 read_file_scope (die, cu);
10164 break;
10165 case DW_TAG_type_unit:
10166 read_type_unit_scope (die, cu);
10167 break;
10168 case DW_TAG_subprogram:
10169 /* Nested subprograms in Fortran get a prefix. */
10170 if (cu->language == language_fortran
10171 && die->parent != NULL
10172 && die->parent->tag == DW_TAG_subprogram)
10173 cu->processing_has_namespace_info = true;
10174 /* Fall through. */
10175 case DW_TAG_inlined_subroutine:
10176 read_func_scope (die, cu);
10177 break;
10178 case DW_TAG_lexical_block:
10179 case DW_TAG_try_block:
10180 case DW_TAG_catch_block:
10181 read_lexical_block_scope (die, cu);
10182 break;
10183 case DW_TAG_call_site:
10184 case DW_TAG_GNU_call_site:
10185 read_call_site_scope (die, cu);
10186 break;
10187 case DW_TAG_class_type:
10188 case DW_TAG_interface_type:
10189 case DW_TAG_structure_type:
10190 case DW_TAG_union_type:
10191 process_structure_scope (die, cu);
10192 break;
10193 case DW_TAG_enumeration_type:
10194 process_enumeration_scope (die, cu);
10195 break;
10196
10197 /* These dies have a type, but processing them does not create
10198 a symbol or recurse to process the children. Therefore we can
10199 read them on-demand through read_type_die. */
10200 case DW_TAG_subroutine_type:
10201 case DW_TAG_set_type:
10202 case DW_TAG_array_type:
10203 case DW_TAG_pointer_type:
10204 case DW_TAG_ptr_to_member_type:
10205 case DW_TAG_reference_type:
10206 case DW_TAG_rvalue_reference_type:
10207 case DW_TAG_string_type:
10208 break;
10209
10210 case DW_TAG_base_type:
10211 case DW_TAG_subrange_type:
10212 case DW_TAG_typedef:
10213 /* Add a typedef symbol for the type definition, if it has a
10214 DW_AT_name. */
10215 new_symbol (die, read_type_die (die, cu), cu);
10216 break;
10217 case DW_TAG_common_block:
10218 read_common_block (die, cu);
10219 break;
10220 case DW_TAG_common_inclusion:
10221 break;
10222 case DW_TAG_namespace:
10223 cu->processing_has_namespace_info = true;
10224 read_namespace (die, cu);
10225 break;
10226 case DW_TAG_module:
10227 cu->processing_has_namespace_info = true;
10228 read_module (die, cu);
10229 break;
10230 case DW_TAG_imported_declaration:
10231 cu->processing_has_namespace_info = true;
10232 if (read_namespace_alias (die, cu))
10233 break;
10234 /* The declaration is not a global namespace alias. */
10235 /* Fall through. */
10236 case DW_TAG_imported_module:
10237 cu->processing_has_namespace_info = true;
10238 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10239 || cu->language != language_fortran))
10240 complaint (_("Tag '%s' has unexpected children"),
10241 dwarf_tag_name (die->tag));
10242 read_import_statement (die, cu);
10243 break;
10244
10245 case DW_TAG_imported_unit:
10246 process_imported_unit_die (die, cu);
10247 break;
10248
10249 case DW_TAG_variable:
10250 read_variable (die, cu);
10251 break;
10252
10253 default:
10254 new_symbol (die, NULL, cu);
10255 break;
10256 }
10257 }
10258 \f
10259 /* DWARF name computation. */
10260
10261 /* A helper function for dwarf2_compute_name which determines whether DIE
10262 needs to have the name of the scope prepended to the name listed in the
10263 die. */
10264
10265 static int
10266 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10267 {
10268 struct attribute *attr;
10269
10270 switch (die->tag)
10271 {
10272 case DW_TAG_namespace:
10273 case DW_TAG_typedef:
10274 case DW_TAG_class_type:
10275 case DW_TAG_interface_type:
10276 case DW_TAG_structure_type:
10277 case DW_TAG_union_type:
10278 case DW_TAG_enumeration_type:
10279 case DW_TAG_enumerator:
10280 case DW_TAG_subprogram:
10281 case DW_TAG_inlined_subroutine:
10282 case DW_TAG_member:
10283 case DW_TAG_imported_declaration:
10284 return 1;
10285
10286 case DW_TAG_variable:
10287 case DW_TAG_constant:
10288 /* We only need to prefix "globally" visible variables. These include
10289 any variable marked with DW_AT_external or any variable that
10290 lives in a namespace. [Variables in anonymous namespaces
10291 require prefixing, but they are not DW_AT_external.] */
10292
10293 if (dwarf2_attr (die, DW_AT_specification, cu))
10294 {
10295 struct dwarf2_cu *spec_cu = cu;
10296
10297 return die_needs_namespace (die_specification (die, &spec_cu),
10298 spec_cu);
10299 }
10300
10301 attr = dwarf2_attr (die, DW_AT_external, cu);
10302 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10303 && die->parent->tag != DW_TAG_module)
10304 return 0;
10305 /* A variable in a lexical block of some kind does not need a
10306 namespace, even though in C++ such variables may be external
10307 and have a mangled name. */
10308 if (die->parent->tag == DW_TAG_lexical_block
10309 || die->parent->tag == DW_TAG_try_block
10310 || die->parent->tag == DW_TAG_catch_block
10311 || die->parent->tag == DW_TAG_subprogram)
10312 return 0;
10313 return 1;
10314
10315 default:
10316 return 0;
10317 }
10318 }
10319
10320 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10321 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10322 defined for the given DIE. */
10323
10324 static struct attribute *
10325 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10326 {
10327 struct attribute *attr;
10328
10329 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10330 if (attr == NULL)
10331 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10332
10333 return attr;
10334 }
10335
10336 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10337 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10338 defined for the given DIE. */
10339
10340 static const char *
10341 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10342 {
10343 const char *linkage_name;
10344
10345 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10346 if (linkage_name == NULL)
10347 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10348
10349 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10350 See https://github.com/rust-lang/rust/issues/32925. */
10351 if (cu->language == language_rust && linkage_name != NULL
10352 && strchr (linkage_name, '{') != NULL)
10353 linkage_name = NULL;
10354
10355 return linkage_name;
10356 }
10357
10358 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10359 compute the physname for the object, which include a method's:
10360 - formal parameters (C++),
10361 - receiver type (Go),
10362
10363 The term "physname" is a bit confusing.
10364 For C++, for example, it is the demangled name.
10365 For Go, for example, it's the mangled name.
10366
10367 For Ada, return the DIE's linkage name rather than the fully qualified
10368 name. PHYSNAME is ignored..
10369
10370 The result is allocated on the objfile->per_bfd's obstack and
10371 canonicalized. */
10372
10373 static const char *
10374 dwarf2_compute_name (const char *name,
10375 struct die_info *die, struct dwarf2_cu *cu,
10376 int physname)
10377 {
10378 struct objfile *objfile = cu->per_objfile->objfile;
10379
10380 if (name == NULL)
10381 name = dwarf2_name (die, cu);
10382
10383 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10384 but otherwise compute it by typename_concat inside GDB.
10385 FIXME: Actually this is not really true, or at least not always true.
10386 It's all very confusing. compute_and_set_names doesn't try to demangle
10387 Fortran names because there is no mangling standard. So new_symbol
10388 will set the demangled name to the result of dwarf2_full_name, and it is
10389 the demangled name that GDB uses if it exists. */
10390 if (cu->language == language_ada
10391 || (cu->language == language_fortran && physname))
10392 {
10393 /* For Ada unit, we prefer the linkage name over the name, as
10394 the former contains the exported name, which the user expects
10395 to be able to reference. Ideally, we want the user to be able
10396 to reference this entity using either natural or linkage name,
10397 but we haven't started looking at this enhancement yet. */
10398 const char *linkage_name = dw2_linkage_name (die, cu);
10399
10400 if (linkage_name != NULL)
10401 return linkage_name;
10402 }
10403
10404 /* These are the only languages we know how to qualify names in. */
10405 if (name != NULL
10406 && (cu->language == language_cplus
10407 || cu->language == language_fortran || cu->language == language_d
10408 || cu->language == language_rust))
10409 {
10410 if (die_needs_namespace (die, cu))
10411 {
10412 const char *prefix;
10413 const char *canonical_name = NULL;
10414
10415 string_file buf;
10416
10417 prefix = determine_prefix (die, cu);
10418 if (*prefix != '\0')
10419 {
10420 gdb::unique_xmalloc_ptr<char> prefixed_name
10421 (typename_concat (NULL, prefix, name, physname, cu));
10422
10423 buf.puts (prefixed_name.get ());
10424 }
10425 else
10426 buf.puts (name);
10427
10428 /* Template parameters may be specified in the DIE's DW_AT_name, or
10429 as children with DW_TAG_template_type_param or
10430 DW_TAG_value_type_param. If the latter, add them to the name
10431 here. If the name already has template parameters, then
10432 skip this step; some versions of GCC emit both, and
10433 it is more efficient to use the pre-computed name.
10434
10435 Something to keep in mind about this process: it is very
10436 unlikely, or in some cases downright impossible, to produce
10437 something that will match the mangled name of a function.
10438 If the definition of the function has the same debug info,
10439 we should be able to match up with it anyway. But fallbacks
10440 using the minimal symbol, for instance to find a method
10441 implemented in a stripped copy of libstdc++, will not work.
10442 If we do not have debug info for the definition, we will have to
10443 match them up some other way.
10444
10445 When we do name matching there is a related problem with function
10446 templates; two instantiated function templates are allowed to
10447 differ only by their return types, which we do not add here. */
10448
10449 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10450 {
10451 struct attribute *attr;
10452 struct die_info *child;
10453 int first = 1;
10454
10455 die->building_fullname = 1;
10456
10457 for (child = die->child; child != NULL; child = child->sibling)
10458 {
10459 struct type *type;
10460 LONGEST value;
10461 const gdb_byte *bytes;
10462 struct dwarf2_locexpr_baton *baton;
10463 struct value *v;
10464
10465 if (child->tag != DW_TAG_template_type_param
10466 && child->tag != DW_TAG_template_value_param)
10467 continue;
10468
10469 if (first)
10470 {
10471 buf.puts ("<");
10472 first = 0;
10473 }
10474 else
10475 buf.puts (", ");
10476
10477 attr = dwarf2_attr (child, DW_AT_type, cu);
10478 if (attr == NULL)
10479 {
10480 complaint (_("template parameter missing DW_AT_type"));
10481 buf.puts ("UNKNOWN_TYPE");
10482 continue;
10483 }
10484 type = die_type (child, cu);
10485
10486 if (child->tag == DW_TAG_template_type_param)
10487 {
10488 c_print_type (type, "", &buf, -1, 0, cu->language,
10489 &type_print_raw_options);
10490 continue;
10491 }
10492
10493 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10494 if (attr == NULL)
10495 {
10496 complaint (_("template parameter missing "
10497 "DW_AT_const_value"));
10498 buf.puts ("UNKNOWN_VALUE");
10499 continue;
10500 }
10501
10502 dwarf2_const_value_attr (attr, type, name,
10503 &cu->comp_unit_obstack, cu,
10504 &value, &bytes, &baton);
10505
10506 if (type->has_no_signedness ())
10507 /* GDB prints characters as NUMBER 'CHAR'. If that's
10508 changed, this can use value_print instead. */
10509 c_printchar (value, type, &buf);
10510 else
10511 {
10512 struct value_print_options opts;
10513
10514 if (baton != NULL)
10515 v = dwarf2_evaluate_loc_desc (type, NULL,
10516 baton->data,
10517 baton->size,
10518 baton->per_cu,
10519 baton->per_objfile);
10520 else if (bytes != NULL)
10521 {
10522 v = allocate_value (type);
10523 memcpy (value_contents_writeable (v), bytes,
10524 TYPE_LENGTH (type));
10525 }
10526 else
10527 v = value_from_longest (type, value);
10528
10529 /* Specify decimal so that we do not depend on
10530 the radix. */
10531 get_formatted_print_options (&opts, 'd');
10532 opts.raw = 1;
10533 value_print (v, &buf, &opts);
10534 release_value (v);
10535 }
10536 }
10537
10538 die->building_fullname = 0;
10539
10540 if (!first)
10541 {
10542 /* Close the argument list, with a space if necessary
10543 (nested templates). */
10544 if (!buf.empty () && buf.string ().back () == '>')
10545 buf.puts (" >");
10546 else
10547 buf.puts (">");
10548 }
10549 }
10550
10551 /* For C++ methods, append formal parameter type
10552 information, if PHYSNAME. */
10553
10554 if (physname && die->tag == DW_TAG_subprogram
10555 && cu->language == language_cplus)
10556 {
10557 struct type *type = read_type_die (die, cu);
10558
10559 c_type_print_args (type, &buf, 1, cu->language,
10560 &type_print_raw_options);
10561
10562 if (cu->language == language_cplus)
10563 {
10564 /* Assume that an artificial first parameter is
10565 "this", but do not crash if it is not. RealView
10566 marks unnamed (and thus unused) parameters as
10567 artificial; there is no way to differentiate
10568 the two cases. */
10569 if (type->num_fields () > 0
10570 && TYPE_FIELD_ARTIFICIAL (type, 0)
10571 && type->field (0).type ()->code () == TYPE_CODE_PTR
10572 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
10573 buf.puts (" const");
10574 }
10575 }
10576
10577 const std::string &intermediate_name = buf.string ();
10578
10579 if (cu->language == language_cplus)
10580 canonical_name
10581 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10582 objfile);
10583
10584 /* If we only computed INTERMEDIATE_NAME, or if
10585 INTERMEDIATE_NAME is already canonical, then we need to
10586 intern it. */
10587 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10588 name = objfile->intern (intermediate_name);
10589 else
10590 name = canonical_name;
10591 }
10592 }
10593
10594 return name;
10595 }
10596
10597 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10598 If scope qualifiers are appropriate they will be added. The result
10599 will be allocated on the storage_obstack, or NULL if the DIE does
10600 not have a name. NAME may either be from a previous call to
10601 dwarf2_name or NULL.
10602
10603 The output string will be canonicalized (if C++). */
10604
10605 static const char *
10606 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10607 {
10608 return dwarf2_compute_name (name, die, cu, 0);
10609 }
10610
10611 /* Construct a physname for the given DIE in CU. NAME may either be
10612 from a previous call to dwarf2_name or NULL. The result will be
10613 allocated on the objfile_objstack or NULL if the DIE does not have a
10614 name.
10615
10616 The output string will be canonicalized (if C++). */
10617
10618 static const char *
10619 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10620 {
10621 struct objfile *objfile = cu->per_objfile->objfile;
10622 const char *retval, *mangled = NULL, *canon = NULL;
10623 int need_copy = 1;
10624
10625 /* In this case dwarf2_compute_name is just a shortcut not building anything
10626 on its own. */
10627 if (!die_needs_namespace (die, cu))
10628 return dwarf2_compute_name (name, die, cu, 1);
10629
10630 if (cu->language != language_rust)
10631 mangled = dw2_linkage_name (die, cu);
10632
10633 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10634 has computed. */
10635 gdb::unique_xmalloc_ptr<char> demangled;
10636 if (mangled != NULL)
10637 {
10638
10639 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
10640 {
10641 /* Do nothing (do not demangle the symbol name). */
10642 }
10643 else
10644 {
10645 /* Use DMGL_RET_DROP for C++ template functions to suppress
10646 their return type. It is easier for GDB users to search
10647 for such functions as `name(params)' than `long name(params)'.
10648 In such case the minimal symbol names do not match the full
10649 symbol names but for template functions there is never a need
10650 to look up their definition from their declaration so
10651 the only disadvantage remains the minimal symbol variant
10652 `long name(params)' does not have the proper inferior type. */
10653 demangled.reset (gdb_demangle (mangled,
10654 (DMGL_PARAMS | DMGL_ANSI
10655 | DMGL_RET_DROP)));
10656 }
10657 if (demangled)
10658 canon = demangled.get ();
10659 else
10660 {
10661 canon = mangled;
10662 need_copy = 0;
10663 }
10664 }
10665
10666 if (canon == NULL || check_physname)
10667 {
10668 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10669
10670 if (canon != NULL && strcmp (physname, canon) != 0)
10671 {
10672 /* It may not mean a bug in GDB. The compiler could also
10673 compute DW_AT_linkage_name incorrectly. But in such case
10674 GDB would need to be bug-to-bug compatible. */
10675
10676 complaint (_("Computed physname <%s> does not match demangled <%s> "
10677 "(from linkage <%s>) - DIE at %s [in module %s]"),
10678 physname, canon, mangled, sect_offset_str (die->sect_off),
10679 objfile_name (objfile));
10680
10681 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10682 is available here - over computed PHYSNAME. It is safer
10683 against both buggy GDB and buggy compilers. */
10684
10685 retval = canon;
10686 }
10687 else
10688 {
10689 retval = physname;
10690 need_copy = 0;
10691 }
10692 }
10693 else
10694 retval = canon;
10695
10696 if (need_copy)
10697 retval = objfile->intern (retval);
10698
10699 return retval;
10700 }
10701
10702 /* Inspect DIE in CU for a namespace alias. If one exists, record
10703 a new symbol for it.
10704
10705 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10706
10707 static int
10708 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10709 {
10710 struct attribute *attr;
10711
10712 /* If the die does not have a name, this is not a namespace
10713 alias. */
10714 attr = dwarf2_attr (die, DW_AT_name, cu);
10715 if (attr != NULL)
10716 {
10717 int num;
10718 struct die_info *d = die;
10719 struct dwarf2_cu *imported_cu = cu;
10720
10721 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10722 keep inspecting DIEs until we hit the underlying import. */
10723 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10724 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10725 {
10726 attr = dwarf2_attr (d, DW_AT_import, cu);
10727 if (attr == NULL)
10728 break;
10729
10730 d = follow_die_ref (d, attr, &imported_cu);
10731 if (d->tag != DW_TAG_imported_declaration)
10732 break;
10733 }
10734
10735 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10736 {
10737 complaint (_("DIE at %s has too many recursively imported "
10738 "declarations"), sect_offset_str (d->sect_off));
10739 return 0;
10740 }
10741
10742 if (attr != NULL)
10743 {
10744 struct type *type;
10745 sect_offset sect_off = attr->get_ref_die_offset ();
10746
10747 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10748 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10749 {
10750 /* This declaration is a global namespace alias. Add
10751 a symbol for it whose type is the aliased namespace. */
10752 new_symbol (die, type, cu);
10753 return 1;
10754 }
10755 }
10756 }
10757
10758 return 0;
10759 }
10760
10761 /* Return the using directives repository (global or local?) to use in the
10762 current context for CU.
10763
10764 For Ada, imported declarations can materialize renamings, which *may* be
10765 global. However it is impossible (for now?) in DWARF to distinguish
10766 "external" imported declarations and "static" ones. As all imported
10767 declarations seem to be static in all other languages, make them all CU-wide
10768 global only in Ada. */
10769
10770 static struct using_direct **
10771 using_directives (struct dwarf2_cu *cu)
10772 {
10773 if (cu->language == language_ada
10774 && cu->get_builder ()->outermost_context_p ())
10775 return cu->get_builder ()->get_global_using_directives ();
10776 else
10777 return cu->get_builder ()->get_local_using_directives ();
10778 }
10779
10780 /* Read the import statement specified by the given die and record it. */
10781
10782 static void
10783 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10784 {
10785 struct objfile *objfile = cu->per_objfile->objfile;
10786 struct attribute *import_attr;
10787 struct die_info *imported_die, *child_die;
10788 struct dwarf2_cu *imported_cu;
10789 const char *imported_name;
10790 const char *imported_name_prefix;
10791 const char *canonical_name;
10792 const char *import_alias;
10793 const char *imported_declaration = NULL;
10794 const char *import_prefix;
10795 std::vector<const char *> excludes;
10796
10797 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10798 if (import_attr == NULL)
10799 {
10800 complaint (_("Tag '%s' has no DW_AT_import"),
10801 dwarf_tag_name (die->tag));
10802 return;
10803 }
10804
10805 imported_cu = cu;
10806 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10807 imported_name = dwarf2_name (imported_die, imported_cu);
10808 if (imported_name == NULL)
10809 {
10810 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10811
10812 The import in the following code:
10813 namespace A
10814 {
10815 typedef int B;
10816 }
10817
10818 int main ()
10819 {
10820 using A::B;
10821 B b;
10822 return b;
10823 }
10824
10825 ...
10826 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10827 <52> DW_AT_decl_file : 1
10828 <53> DW_AT_decl_line : 6
10829 <54> DW_AT_import : <0x75>
10830 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10831 <59> DW_AT_name : B
10832 <5b> DW_AT_decl_file : 1
10833 <5c> DW_AT_decl_line : 2
10834 <5d> DW_AT_type : <0x6e>
10835 ...
10836 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10837 <76> DW_AT_byte_size : 4
10838 <77> DW_AT_encoding : 5 (signed)
10839
10840 imports the wrong die ( 0x75 instead of 0x58 ).
10841 This case will be ignored until the gcc bug is fixed. */
10842 return;
10843 }
10844
10845 /* Figure out the local name after import. */
10846 import_alias = dwarf2_name (die, cu);
10847
10848 /* Figure out where the statement is being imported to. */
10849 import_prefix = determine_prefix (die, cu);
10850
10851 /* Figure out what the scope of the imported die is and prepend it
10852 to the name of the imported die. */
10853 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10854
10855 if (imported_die->tag != DW_TAG_namespace
10856 && imported_die->tag != DW_TAG_module)
10857 {
10858 imported_declaration = imported_name;
10859 canonical_name = imported_name_prefix;
10860 }
10861 else if (strlen (imported_name_prefix) > 0)
10862 canonical_name = obconcat (&objfile->objfile_obstack,
10863 imported_name_prefix,
10864 (cu->language == language_d ? "." : "::"),
10865 imported_name, (char *) NULL);
10866 else
10867 canonical_name = imported_name;
10868
10869 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10870 for (child_die = die->child; child_die && child_die->tag;
10871 child_die = child_die->sibling)
10872 {
10873 /* DWARF-4: A Fortran use statement with a “rename list” may be
10874 represented by an imported module entry with an import attribute
10875 referring to the module and owned entries corresponding to those
10876 entities that are renamed as part of being imported. */
10877
10878 if (child_die->tag != DW_TAG_imported_declaration)
10879 {
10880 complaint (_("child DW_TAG_imported_declaration expected "
10881 "- DIE at %s [in module %s]"),
10882 sect_offset_str (child_die->sect_off),
10883 objfile_name (objfile));
10884 continue;
10885 }
10886
10887 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10888 if (import_attr == NULL)
10889 {
10890 complaint (_("Tag '%s' has no DW_AT_import"),
10891 dwarf_tag_name (child_die->tag));
10892 continue;
10893 }
10894
10895 imported_cu = cu;
10896 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10897 &imported_cu);
10898 imported_name = dwarf2_name (imported_die, imported_cu);
10899 if (imported_name == NULL)
10900 {
10901 complaint (_("child DW_TAG_imported_declaration has unknown "
10902 "imported name - DIE at %s [in module %s]"),
10903 sect_offset_str (child_die->sect_off),
10904 objfile_name (objfile));
10905 continue;
10906 }
10907
10908 excludes.push_back (imported_name);
10909
10910 process_die (child_die, cu);
10911 }
10912
10913 add_using_directive (using_directives (cu),
10914 import_prefix,
10915 canonical_name,
10916 import_alias,
10917 imported_declaration,
10918 excludes,
10919 0,
10920 &objfile->objfile_obstack);
10921 }
10922
10923 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10924 types, but gives them a size of zero. Starting with version 14,
10925 ICC is compatible with GCC. */
10926
10927 static bool
10928 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10929 {
10930 if (!cu->checked_producer)
10931 check_producer (cu);
10932
10933 return cu->producer_is_icc_lt_14;
10934 }
10935
10936 /* ICC generates a DW_AT_type for C void functions. This was observed on
10937 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10938 which says that void functions should not have a DW_AT_type. */
10939
10940 static bool
10941 producer_is_icc (struct dwarf2_cu *cu)
10942 {
10943 if (!cu->checked_producer)
10944 check_producer (cu);
10945
10946 return cu->producer_is_icc;
10947 }
10948
10949 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10950 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10951 this, it was first present in GCC release 4.3.0. */
10952
10953 static bool
10954 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10955 {
10956 if (!cu->checked_producer)
10957 check_producer (cu);
10958
10959 return cu->producer_is_gcc_lt_4_3;
10960 }
10961
10962 static file_and_directory
10963 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10964 {
10965 file_and_directory res;
10966
10967 /* Find the filename. Do not use dwarf2_name here, since the filename
10968 is not a source language identifier. */
10969 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10970 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10971
10972 if (res.comp_dir == NULL
10973 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10974 && IS_ABSOLUTE_PATH (res.name))
10975 {
10976 res.comp_dir_storage = ldirname (res.name);
10977 if (!res.comp_dir_storage.empty ())
10978 res.comp_dir = res.comp_dir_storage.c_str ();
10979 }
10980 if (res.comp_dir != NULL)
10981 {
10982 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10983 directory, get rid of it. */
10984 const char *cp = strchr (res.comp_dir, ':');
10985
10986 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10987 res.comp_dir = cp + 1;
10988 }
10989
10990 if (res.name == NULL)
10991 res.name = "<unknown>";
10992
10993 return res;
10994 }
10995
10996 /* Handle DW_AT_stmt_list for a compilation unit.
10997 DIE is the DW_TAG_compile_unit die for CU.
10998 COMP_DIR is the compilation directory. LOWPC is passed to
10999 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11000
11001 static void
11002 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11003 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11004 {
11005 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11006 struct attribute *attr;
11007 struct line_header line_header_local;
11008 hashval_t line_header_local_hash;
11009 void **slot;
11010 int decode_mapping;
11011
11012 gdb_assert (! cu->per_cu->is_debug_types);
11013
11014 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11015 if (attr == NULL || !attr->form_is_unsigned ())
11016 return;
11017
11018 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
11019
11020 /* The line header hash table is only created if needed (it exists to
11021 prevent redundant reading of the line table for partial_units).
11022 If we're given a partial_unit, we'll need it. If we're given a
11023 compile_unit, then use the line header hash table if it's already
11024 created, but don't create one just yet. */
11025
11026 if (per_objfile->line_header_hash == NULL
11027 && die->tag == DW_TAG_partial_unit)
11028 {
11029 per_objfile->line_header_hash
11030 .reset (htab_create_alloc (127, line_header_hash_voidp,
11031 line_header_eq_voidp,
11032 free_line_header_voidp,
11033 xcalloc, xfree));
11034 }
11035
11036 line_header_local.sect_off = line_offset;
11037 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11038 line_header_local_hash = line_header_hash (&line_header_local);
11039 if (per_objfile->line_header_hash != NULL)
11040 {
11041 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11042 &line_header_local,
11043 line_header_local_hash, NO_INSERT);
11044
11045 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11046 is not present in *SLOT (since if there is something in *SLOT then
11047 it will be for a partial_unit). */
11048 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11049 {
11050 gdb_assert (*slot != NULL);
11051 cu->line_header = (struct line_header *) *slot;
11052 return;
11053 }
11054 }
11055
11056 /* dwarf_decode_line_header does not yet provide sufficient information.
11057 We always have to call also dwarf_decode_lines for it. */
11058 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11059 if (lh == NULL)
11060 return;
11061
11062 cu->line_header = lh.release ();
11063 cu->line_header_die_owner = die;
11064
11065 if (per_objfile->line_header_hash == NULL)
11066 slot = NULL;
11067 else
11068 {
11069 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11070 &line_header_local,
11071 line_header_local_hash, INSERT);
11072 gdb_assert (slot != NULL);
11073 }
11074 if (slot != NULL && *slot == NULL)
11075 {
11076 /* This newly decoded line number information unit will be owned
11077 by line_header_hash hash table. */
11078 *slot = cu->line_header;
11079 cu->line_header_die_owner = NULL;
11080 }
11081 else
11082 {
11083 /* We cannot free any current entry in (*slot) as that struct line_header
11084 may be already used by multiple CUs. Create only temporary decoded
11085 line_header for this CU - it may happen at most once for each line
11086 number information unit. And if we're not using line_header_hash
11087 then this is what we want as well. */
11088 gdb_assert (die->tag != DW_TAG_partial_unit);
11089 }
11090 decode_mapping = (die->tag != DW_TAG_partial_unit);
11091 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11092 decode_mapping);
11093
11094 }
11095
11096 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11097
11098 static void
11099 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11100 {
11101 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11102 struct objfile *objfile = per_objfile->objfile;
11103 struct gdbarch *gdbarch = objfile->arch ();
11104 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11105 CORE_ADDR highpc = ((CORE_ADDR) 0);
11106 struct attribute *attr;
11107 struct die_info *child_die;
11108 CORE_ADDR baseaddr;
11109
11110 prepare_one_comp_unit (cu, die, cu->language);
11111 baseaddr = objfile->text_section_offset ();
11112
11113 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11114
11115 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11116 from finish_block. */
11117 if (lowpc == ((CORE_ADDR) -1))
11118 lowpc = highpc;
11119 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11120
11121 file_and_directory fnd = find_file_and_directory (die, cu);
11122
11123 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11124 standardised yet. As a workaround for the language detection we fall
11125 back to the DW_AT_producer string. */
11126 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11127 cu->language = language_opencl;
11128
11129 /* Similar hack for Go. */
11130 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11131 set_cu_language (DW_LANG_Go, cu);
11132
11133 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11134
11135 /* Decode line number information if present. We do this before
11136 processing child DIEs, so that the line header table is available
11137 for DW_AT_decl_file. */
11138 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11139
11140 /* Process all dies in compilation unit. */
11141 if (die->child != NULL)
11142 {
11143 child_die = die->child;
11144 while (child_die && child_die->tag)
11145 {
11146 process_die (child_die, cu);
11147 child_die = child_die->sibling;
11148 }
11149 }
11150
11151 /* Decode macro information, if present. Dwarf 2 macro information
11152 refers to information in the line number info statement program
11153 header, so we can only read it if we've read the header
11154 successfully. */
11155 attr = dwarf2_attr (die, DW_AT_macros, cu);
11156 if (attr == NULL)
11157 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11158 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
11159 {
11160 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11161 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11162
11163 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
11164 }
11165 else
11166 {
11167 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11168 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
11169 {
11170 unsigned int macro_offset = attr->as_unsigned ();
11171
11172 dwarf_decode_macros (cu, macro_offset, 0);
11173 }
11174 }
11175 }
11176
11177 void
11178 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11179 {
11180 struct type_unit_group *tu_group;
11181 int first_time;
11182 struct attribute *attr;
11183 unsigned int i;
11184 struct signatured_type *sig_type;
11185
11186 gdb_assert (per_cu->is_debug_types);
11187 sig_type = (struct signatured_type *) per_cu;
11188
11189 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11190
11191 /* If we're using .gdb_index (includes -readnow) then
11192 per_cu->type_unit_group may not have been set up yet. */
11193 if (sig_type->type_unit_group == NULL)
11194 sig_type->type_unit_group = get_type_unit_group (this, attr);
11195 tu_group = sig_type->type_unit_group;
11196
11197 /* If we've already processed this stmt_list there's no real need to
11198 do it again, we could fake it and just recreate the part we need
11199 (file name,index -> symtab mapping). If data shows this optimization
11200 is useful we can do it then. */
11201 type_unit_group_unshareable *tug_unshare
11202 = per_objfile->get_type_unit_group_unshareable (tu_group);
11203 first_time = tug_unshare->compunit_symtab == NULL;
11204
11205 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11206 debug info. */
11207 line_header_up lh;
11208 if (attr != NULL && attr->form_is_unsigned ())
11209 {
11210 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
11211 lh = dwarf_decode_line_header (line_offset, this);
11212 }
11213 if (lh == NULL)
11214 {
11215 if (first_time)
11216 start_symtab ("", NULL, 0);
11217 else
11218 {
11219 gdb_assert (tug_unshare->symtabs == NULL);
11220 gdb_assert (m_builder == nullptr);
11221 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11222 m_builder.reset (new struct buildsym_compunit
11223 (COMPUNIT_OBJFILE (cust), "",
11224 COMPUNIT_DIRNAME (cust),
11225 compunit_language (cust),
11226 0, cust));
11227 list_in_scope = get_builder ()->get_file_symbols ();
11228 }
11229 return;
11230 }
11231
11232 line_header = lh.release ();
11233 line_header_die_owner = die;
11234
11235 if (first_time)
11236 {
11237 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11238
11239 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11240 still initializing it, and our caller (a few levels up)
11241 process_full_type_unit still needs to know if this is the first
11242 time. */
11243
11244 tug_unshare->symtabs
11245 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11246 struct symtab *, line_header->file_names_size ());
11247
11248 auto &file_names = line_header->file_names ();
11249 for (i = 0; i < file_names.size (); ++i)
11250 {
11251 file_entry &fe = file_names[i];
11252 dwarf2_start_subfile (this, fe.name,
11253 fe.include_dir (line_header));
11254 buildsym_compunit *b = get_builder ();
11255 if (b->get_current_subfile ()->symtab == NULL)
11256 {
11257 /* NOTE: start_subfile will recognize when it's been
11258 passed a file it has already seen. So we can't
11259 assume there's a simple mapping from
11260 cu->line_header->file_names to subfiles, plus
11261 cu->line_header->file_names may contain dups. */
11262 b->get_current_subfile ()->symtab
11263 = allocate_symtab (cust, b->get_current_subfile ()->name);
11264 }
11265
11266 fe.symtab = b->get_current_subfile ()->symtab;
11267 tug_unshare->symtabs[i] = fe.symtab;
11268 }
11269 }
11270 else
11271 {
11272 gdb_assert (m_builder == nullptr);
11273 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11274 m_builder.reset (new struct buildsym_compunit
11275 (COMPUNIT_OBJFILE (cust), "",
11276 COMPUNIT_DIRNAME (cust),
11277 compunit_language (cust),
11278 0, cust));
11279 list_in_scope = get_builder ()->get_file_symbols ();
11280
11281 auto &file_names = line_header->file_names ();
11282 for (i = 0; i < file_names.size (); ++i)
11283 {
11284 file_entry &fe = file_names[i];
11285 fe.symtab = tug_unshare->symtabs[i];
11286 }
11287 }
11288
11289 /* The main symtab is allocated last. Type units don't have DW_AT_name
11290 so they don't have a "real" (so to speak) symtab anyway.
11291 There is later code that will assign the main symtab to all symbols
11292 that don't have one. We need to handle the case of a symbol with a
11293 missing symtab (DW_AT_decl_file) anyway. */
11294 }
11295
11296 /* Process DW_TAG_type_unit.
11297 For TUs we want to skip the first top level sibling if it's not the
11298 actual type being defined by this TU. In this case the first top
11299 level sibling is there to provide context only. */
11300
11301 static void
11302 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11303 {
11304 struct die_info *child_die;
11305
11306 prepare_one_comp_unit (cu, die, language_minimal);
11307
11308 /* Initialize (or reinitialize) the machinery for building symtabs.
11309 We do this before processing child DIEs, so that the line header table
11310 is available for DW_AT_decl_file. */
11311 cu->setup_type_unit_groups (die);
11312
11313 if (die->child != NULL)
11314 {
11315 child_die = die->child;
11316 while (child_die && child_die->tag)
11317 {
11318 process_die (child_die, cu);
11319 child_die = child_die->sibling;
11320 }
11321 }
11322 }
11323 \f
11324 /* DWO/DWP files.
11325
11326 http://gcc.gnu.org/wiki/DebugFission
11327 http://gcc.gnu.org/wiki/DebugFissionDWP
11328
11329 To simplify handling of both DWO files ("object" files with the DWARF info)
11330 and DWP files (a file with the DWOs packaged up into one file), we treat
11331 DWP files as having a collection of virtual DWO files. */
11332
11333 static hashval_t
11334 hash_dwo_file (const void *item)
11335 {
11336 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11337 hashval_t hash;
11338
11339 hash = htab_hash_string (dwo_file->dwo_name);
11340 if (dwo_file->comp_dir != NULL)
11341 hash += htab_hash_string (dwo_file->comp_dir);
11342 return hash;
11343 }
11344
11345 static int
11346 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11347 {
11348 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11349 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11350
11351 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11352 return 0;
11353 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11354 return lhs->comp_dir == rhs->comp_dir;
11355 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11356 }
11357
11358 /* Allocate a hash table for DWO files. */
11359
11360 static htab_up
11361 allocate_dwo_file_hash_table ()
11362 {
11363 auto delete_dwo_file = [] (void *item)
11364 {
11365 struct dwo_file *dwo_file = (struct dwo_file *) item;
11366
11367 delete dwo_file;
11368 };
11369
11370 return htab_up (htab_create_alloc (41,
11371 hash_dwo_file,
11372 eq_dwo_file,
11373 delete_dwo_file,
11374 xcalloc, xfree));
11375 }
11376
11377 /* Lookup DWO file DWO_NAME. */
11378
11379 static void **
11380 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
11381 const char *dwo_name,
11382 const char *comp_dir)
11383 {
11384 struct dwo_file find_entry;
11385 void **slot;
11386
11387 if (per_objfile->per_bfd->dwo_files == NULL)
11388 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11389
11390 find_entry.dwo_name = dwo_name;
11391 find_entry.comp_dir = comp_dir;
11392 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
11393 INSERT);
11394
11395 return slot;
11396 }
11397
11398 static hashval_t
11399 hash_dwo_unit (const void *item)
11400 {
11401 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11402
11403 /* This drops the top 32 bits of the id, but is ok for a hash. */
11404 return dwo_unit->signature;
11405 }
11406
11407 static int
11408 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11409 {
11410 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11411 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11412
11413 /* The signature is assumed to be unique within the DWO file.
11414 So while object file CU dwo_id's always have the value zero,
11415 that's OK, assuming each object file DWO file has only one CU,
11416 and that's the rule for now. */
11417 return lhs->signature == rhs->signature;
11418 }
11419
11420 /* Allocate a hash table for DWO CUs,TUs.
11421 There is one of these tables for each of CUs,TUs for each DWO file. */
11422
11423 static htab_up
11424 allocate_dwo_unit_table ()
11425 {
11426 /* Start out with a pretty small number.
11427 Generally DWO files contain only one CU and maybe some TUs. */
11428 return htab_up (htab_create_alloc (3,
11429 hash_dwo_unit,
11430 eq_dwo_unit,
11431 NULL, xcalloc, xfree));
11432 }
11433
11434 /* die_reader_func for create_dwo_cu. */
11435
11436 static void
11437 create_dwo_cu_reader (const struct die_reader_specs *reader,
11438 const gdb_byte *info_ptr,
11439 struct die_info *comp_unit_die,
11440 struct dwo_file *dwo_file,
11441 struct dwo_unit *dwo_unit)
11442 {
11443 struct dwarf2_cu *cu = reader->cu;
11444 sect_offset sect_off = cu->per_cu->sect_off;
11445 struct dwarf2_section_info *section = cu->per_cu->section;
11446
11447 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11448 if (!signature.has_value ())
11449 {
11450 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11451 " its dwo_id [in module %s]"),
11452 sect_offset_str (sect_off), dwo_file->dwo_name);
11453 return;
11454 }
11455
11456 dwo_unit->dwo_file = dwo_file;
11457 dwo_unit->signature = *signature;
11458 dwo_unit->section = section;
11459 dwo_unit->sect_off = sect_off;
11460 dwo_unit->length = cu->per_cu->length;
11461
11462 if (dwarf_read_debug)
11463 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11464 sect_offset_str (sect_off),
11465 hex_string (dwo_unit->signature));
11466 }
11467
11468 /* Create the dwo_units for the CUs in a DWO_FILE.
11469 Note: This function processes DWO files only, not DWP files. */
11470
11471 static void
11472 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
11473 dwarf2_cu *cu, struct dwo_file &dwo_file,
11474 dwarf2_section_info &section, htab_up &cus_htab)
11475 {
11476 struct objfile *objfile = per_objfile->objfile;
11477 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
11478 const gdb_byte *info_ptr, *end_ptr;
11479
11480 section.read (objfile);
11481 info_ptr = section.buffer;
11482
11483 if (info_ptr == NULL)
11484 return;
11485
11486 if (dwarf_read_debug)
11487 {
11488 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11489 section.get_name (),
11490 section.get_file_name ());
11491 }
11492
11493 end_ptr = info_ptr + section.size;
11494 while (info_ptr < end_ptr)
11495 {
11496 struct dwarf2_per_cu_data per_cu;
11497 struct dwo_unit read_unit {};
11498 struct dwo_unit *dwo_unit;
11499 void **slot;
11500 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11501
11502 memset (&per_cu, 0, sizeof (per_cu));
11503 per_cu.per_bfd = per_bfd;
11504 per_cu.is_debug_types = 0;
11505 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11506 per_cu.section = &section;
11507
11508 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
11509 if (!reader.dummy_p)
11510 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11511 &dwo_file, &read_unit);
11512 info_ptr += per_cu.length;
11513
11514 // If the unit could not be parsed, skip it.
11515 if (read_unit.dwo_file == NULL)
11516 continue;
11517
11518 if (cus_htab == NULL)
11519 cus_htab = allocate_dwo_unit_table ();
11520
11521 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11522 struct dwo_unit);
11523 *dwo_unit = read_unit;
11524 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11525 gdb_assert (slot != NULL);
11526 if (*slot != NULL)
11527 {
11528 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11529 sect_offset dup_sect_off = dup_cu->sect_off;
11530
11531 complaint (_("debug cu entry at offset %s is duplicate to"
11532 " the entry at offset %s, signature %s"),
11533 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11534 hex_string (dwo_unit->signature));
11535 }
11536 *slot = (void *)dwo_unit;
11537 }
11538 }
11539
11540 /* DWP file .debug_{cu,tu}_index section format:
11541 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11542 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11543
11544 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11545 officially standard DWP format was published with DWARF v5 and is called
11546 Version 5. There are no versions 3 or 4.
11547
11548 DWP Version 1:
11549
11550 Both index sections have the same format, and serve to map a 64-bit
11551 signature to a set of section numbers. Each section begins with a header,
11552 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11553 indexes, and a pool of 32-bit section numbers. The index sections will be
11554 aligned at 8-byte boundaries in the file.
11555
11556 The index section header consists of:
11557
11558 V, 32 bit version number
11559 -, 32 bits unused
11560 N, 32 bit number of compilation units or type units in the index
11561 M, 32 bit number of slots in the hash table
11562
11563 Numbers are recorded using the byte order of the application binary.
11564
11565 The hash table begins at offset 16 in the section, and consists of an array
11566 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11567 order of the application binary). Unused slots in the hash table are 0.
11568 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11569
11570 The parallel table begins immediately after the hash table
11571 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11572 array of 32-bit indexes (using the byte order of the application binary),
11573 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11574 table contains a 32-bit index into the pool of section numbers. For unused
11575 hash table slots, the corresponding entry in the parallel table will be 0.
11576
11577 The pool of section numbers begins immediately following the hash table
11578 (at offset 16 + 12 * M from the beginning of the section). The pool of
11579 section numbers consists of an array of 32-bit words (using the byte order
11580 of the application binary). Each item in the array is indexed starting
11581 from 0. The hash table entry provides the index of the first section
11582 number in the set. Additional section numbers in the set follow, and the
11583 set is terminated by a 0 entry (section number 0 is not used in ELF).
11584
11585 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11586 section must be the first entry in the set, and the .debug_abbrev.dwo must
11587 be the second entry. Other members of the set may follow in any order.
11588
11589 ---
11590
11591 DWP Versions 2 and 5:
11592
11593 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
11594 and the entries in the index tables are now offsets into these sections.
11595 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11596 section.
11597
11598 Index Section Contents:
11599 Header
11600 Hash Table of Signatures dwp_hash_table.hash_table
11601 Parallel Table of Indices dwp_hash_table.unit_table
11602 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11603 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
11604
11605 The index section header consists of:
11606
11607 V, 32 bit version number
11608 L, 32 bit number of columns in the table of section offsets
11609 N, 32 bit number of compilation units or type units in the index
11610 M, 32 bit number of slots in the hash table
11611
11612 Numbers are recorded using the byte order of the application binary.
11613
11614 The hash table has the same format as version 1.
11615 The parallel table of indices has the same format as version 1,
11616 except that the entries are origin-1 indices into the table of sections
11617 offsets and the table of section sizes.
11618
11619 The table of offsets begins immediately following the parallel table
11620 (at offset 16 + 12 * M from the beginning of the section). The table is
11621 a two-dimensional array of 32-bit words (using the byte order of the
11622 application binary), with L columns and N+1 rows, in row-major order.
11623 Each row in the array is indexed starting from 0. The first row provides
11624 a key to the remaining rows: each column in this row provides an identifier
11625 for a debug section, and the offsets in the same column of subsequent rows
11626 refer to that section. The section identifiers for Version 2 are:
11627
11628 DW_SECT_INFO 1 .debug_info.dwo
11629 DW_SECT_TYPES 2 .debug_types.dwo
11630 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11631 DW_SECT_LINE 4 .debug_line.dwo
11632 DW_SECT_LOC 5 .debug_loc.dwo
11633 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11634 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11635 DW_SECT_MACRO 8 .debug_macro.dwo
11636
11637 The section identifiers for Version 5 are:
11638
11639 DW_SECT_INFO_V5 1 .debug_info.dwo
11640 DW_SECT_RESERVED_V5 2 --
11641 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11642 DW_SECT_LINE_V5 4 .debug_line.dwo
11643 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11644 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11645 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11646 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11647
11648 The offsets provided by the CU and TU index sections are the base offsets
11649 for the contributions made by each CU or TU to the corresponding section
11650 in the package file. Each CU and TU header contains an abbrev_offset
11651 field, used to find the abbreviations table for that CU or TU within the
11652 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11653 be interpreted as relative to the base offset given in the index section.
11654 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11655 should be interpreted as relative to the base offset for .debug_line.dwo,
11656 and offsets into other debug sections obtained from DWARF attributes should
11657 also be interpreted as relative to the corresponding base offset.
11658
11659 The table of sizes begins immediately following the table of offsets.
11660 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11661 with L columns and N rows, in row-major order. Each row in the array is
11662 indexed starting from 1 (row 0 is shared by the two tables).
11663
11664 ---
11665
11666 Hash table lookup is handled the same in version 1 and 2:
11667
11668 We assume that N and M will not exceed 2^32 - 1.
11669 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11670
11671 Given a 64-bit compilation unit signature or a type signature S, an entry
11672 in the hash table is located as follows:
11673
11674 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11675 the low-order k bits all set to 1.
11676
11677 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11678
11679 3) If the hash table entry at index H matches the signature, use that
11680 entry. If the hash table entry at index H is unused (all zeroes),
11681 terminate the search: the signature is not present in the table.
11682
11683 4) Let H = (H + H') modulo M. Repeat at Step 3.
11684
11685 Because M > N and H' and M are relatively prime, the search is guaranteed
11686 to stop at an unused slot or find the match. */
11687
11688 /* Create a hash table to map DWO IDs to their CU/TU entry in
11689 .debug_{info,types}.dwo in DWP_FILE.
11690 Returns NULL if there isn't one.
11691 Note: This function processes DWP files only, not DWO files. */
11692
11693 static struct dwp_hash_table *
11694 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11695 struct dwp_file *dwp_file, int is_debug_types)
11696 {
11697 struct objfile *objfile = per_objfile->objfile;
11698 bfd *dbfd = dwp_file->dbfd.get ();
11699 const gdb_byte *index_ptr, *index_end;
11700 struct dwarf2_section_info *index;
11701 uint32_t version, nr_columns, nr_units, nr_slots;
11702 struct dwp_hash_table *htab;
11703
11704 if (is_debug_types)
11705 index = &dwp_file->sections.tu_index;
11706 else
11707 index = &dwp_file->sections.cu_index;
11708
11709 if (index->empty ())
11710 return NULL;
11711 index->read (objfile);
11712
11713 index_ptr = index->buffer;
11714 index_end = index_ptr + index->size;
11715
11716 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11717 For now it's safe to just read 4 bytes (particularly as it's difficult to
11718 tell if you're dealing with Version 5 before you've read the version). */
11719 version = read_4_bytes (dbfd, index_ptr);
11720 index_ptr += 4;
11721 if (version == 2 || version == 5)
11722 nr_columns = read_4_bytes (dbfd, index_ptr);
11723 else
11724 nr_columns = 0;
11725 index_ptr += 4;
11726 nr_units = read_4_bytes (dbfd, index_ptr);
11727 index_ptr += 4;
11728 nr_slots = read_4_bytes (dbfd, index_ptr);
11729 index_ptr += 4;
11730
11731 if (version != 1 && version != 2 && version != 5)
11732 {
11733 error (_("Dwarf Error: unsupported DWP file version (%s)"
11734 " [in module %s]"),
11735 pulongest (version), dwp_file->name);
11736 }
11737 if (nr_slots != (nr_slots & -nr_slots))
11738 {
11739 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11740 " is not power of 2 [in module %s]"),
11741 pulongest (nr_slots), dwp_file->name);
11742 }
11743
11744 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11745 htab->version = version;
11746 htab->nr_columns = nr_columns;
11747 htab->nr_units = nr_units;
11748 htab->nr_slots = nr_slots;
11749 htab->hash_table = index_ptr;
11750 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11751
11752 /* Exit early if the table is empty. */
11753 if (nr_slots == 0 || nr_units == 0
11754 || (version == 2 && nr_columns == 0)
11755 || (version == 5 && nr_columns == 0))
11756 {
11757 /* All must be zero. */
11758 if (nr_slots != 0 || nr_units != 0
11759 || (version == 2 && nr_columns != 0)
11760 || (version == 5 && nr_columns != 0))
11761 {
11762 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11763 " all zero [in modules %s]"),
11764 dwp_file->name);
11765 }
11766 return htab;
11767 }
11768
11769 if (version == 1)
11770 {
11771 htab->section_pool.v1.indices =
11772 htab->unit_table + sizeof (uint32_t) * nr_slots;
11773 /* It's harder to decide whether the section is too small in v1.
11774 V1 is deprecated anyway so we punt. */
11775 }
11776 else if (version == 2)
11777 {
11778 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11779 int *ids = htab->section_pool.v2.section_ids;
11780 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11781 /* Reverse map for error checking. */
11782 int ids_seen[DW_SECT_MAX + 1];
11783 int i;
11784
11785 if (nr_columns < 2)
11786 {
11787 error (_("Dwarf Error: bad DWP hash table, too few columns"
11788 " in section table [in module %s]"),
11789 dwp_file->name);
11790 }
11791 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11792 {
11793 error (_("Dwarf Error: bad DWP hash table, too many columns"
11794 " in section table [in module %s]"),
11795 dwp_file->name);
11796 }
11797 memset (ids, 255, sizeof_ids);
11798 memset (ids_seen, 255, sizeof (ids_seen));
11799 for (i = 0; i < nr_columns; ++i)
11800 {
11801 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11802
11803 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11804 {
11805 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11806 " in section table [in module %s]"),
11807 id, dwp_file->name);
11808 }
11809 if (ids_seen[id] != -1)
11810 {
11811 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11812 " id %d in section table [in module %s]"),
11813 id, dwp_file->name);
11814 }
11815 ids_seen[id] = i;
11816 ids[i] = id;
11817 }
11818 /* Must have exactly one info or types section. */
11819 if (((ids_seen[DW_SECT_INFO] != -1)
11820 + (ids_seen[DW_SECT_TYPES] != -1))
11821 != 1)
11822 {
11823 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11824 " DWO info/types section [in module %s]"),
11825 dwp_file->name);
11826 }
11827 /* Must have an abbrev section. */
11828 if (ids_seen[DW_SECT_ABBREV] == -1)
11829 {
11830 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11831 " section [in module %s]"),
11832 dwp_file->name);
11833 }
11834 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11835 htab->section_pool.v2.sizes =
11836 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11837 * nr_units * nr_columns);
11838 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11839 * nr_units * nr_columns))
11840 > index_end)
11841 {
11842 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11843 " [in module %s]"),
11844 dwp_file->name);
11845 }
11846 }
11847 else /* version == 5 */
11848 {
11849 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11850 int *ids = htab->section_pool.v5.section_ids;
11851 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11852 /* Reverse map for error checking. */
11853 int ids_seen[DW_SECT_MAX_V5 + 1];
11854
11855 if (nr_columns < 2)
11856 {
11857 error (_("Dwarf Error: bad DWP hash table, too few columns"
11858 " in section table [in module %s]"),
11859 dwp_file->name);
11860 }
11861 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11862 {
11863 error (_("Dwarf Error: bad DWP hash table, too many columns"
11864 " in section table [in module %s]"),
11865 dwp_file->name);
11866 }
11867 memset (ids, 255, sizeof_ids);
11868 memset (ids_seen, 255, sizeof (ids_seen));
11869 for (int i = 0; i < nr_columns; ++i)
11870 {
11871 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11872
11873 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11874 {
11875 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11876 " in section table [in module %s]"),
11877 id, dwp_file->name);
11878 }
11879 if (ids_seen[id] != -1)
11880 {
11881 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11882 " id %d in section table [in module %s]"),
11883 id, dwp_file->name);
11884 }
11885 ids_seen[id] = i;
11886 ids[i] = id;
11887 }
11888 /* Must have seen an info section. */
11889 if (ids_seen[DW_SECT_INFO_V5] == -1)
11890 {
11891 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11892 " DWO info/types section [in module %s]"),
11893 dwp_file->name);
11894 }
11895 /* Must have an abbrev section. */
11896 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11897 {
11898 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11899 " section [in module %s]"),
11900 dwp_file->name);
11901 }
11902 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11903 htab->section_pool.v5.sizes
11904 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11905 * nr_units * nr_columns);
11906 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11907 * nr_units * nr_columns))
11908 > index_end)
11909 {
11910 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11911 " [in module %s]"),
11912 dwp_file->name);
11913 }
11914 }
11915
11916 return htab;
11917 }
11918
11919 /* Update SECTIONS with the data from SECTP.
11920
11921 This function is like the other "locate" section routines, but in
11922 this context the sections to read comes from the DWP V1 hash table,
11923 not the full ELF section table.
11924
11925 The result is non-zero for success, or zero if an error was found. */
11926
11927 static int
11928 locate_v1_virtual_dwo_sections (asection *sectp,
11929 struct virtual_v1_dwo_sections *sections)
11930 {
11931 const struct dwop_section_names *names = &dwop_section_names;
11932
11933 if (section_is_p (sectp->name, &names->abbrev_dwo))
11934 {
11935 /* There can be only one. */
11936 if (sections->abbrev.s.section != NULL)
11937 return 0;
11938 sections->abbrev.s.section = sectp;
11939 sections->abbrev.size = bfd_section_size (sectp);
11940 }
11941 else if (section_is_p (sectp->name, &names->info_dwo)
11942 || section_is_p (sectp->name, &names->types_dwo))
11943 {
11944 /* There can be only one. */
11945 if (sections->info_or_types.s.section != NULL)
11946 return 0;
11947 sections->info_or_types.s.section = sectp;
11948 sections->info_or_types.size = bfd_section_size (sectp);
11949 }
11950 else if (section_is_p (sectp->name, &names->line_dwo))
11951 {
11952 /* There can be only one. */
11953 if (sections->line.s.section != NULL)
11954 return 0;
11955 sections->line.s.section = sectp;
11956 sections->line.size = bfd_section_size (sectp);
11957 }
11958 else if (section_is_p (sectp->name, &names->loc_dwo))
11959 {
11960 /* There can be only one. */
11961 if (sections->loc.s.section != NULL)
11962 return 0;
11963 sections->loc.s.section = sectp;
11964 sections->loc.size = bfd_section_size (sectp);
11965 }
11966 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11967 {
11968 /* There can be only one. */
11969 if (sections->macinfo.s.section != NULL)
11970 return 0;
11971 sections->macinfo.s.section = sectp;
11972 sections->macinfo.size = bfd_section_size (sectp);
11973 }
11974 else if (section_is_p (sectp->name, &names->macro_dwo))
11975 {
11976 /* There can be only one. */
11977 if (sections->macro.s.section != NULL)
11978 return 0;
11979 sections->macro.s.section = sectp;
11980 sections->macro.size = bfd_section_size (sectp);
11981 }
11982 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11983 {
11984 /* There can be only one. */
11985 if (sections->str_offsets.s.section != NULL)
11986 return 0;
11987 sections->str_offsets.s.section = sectp;
11988 sections->str_offsets.size = bfd_section_size (sectp);
11989 }
11990 else
11991 {
11992 /* No other kind of section is valid. */
11993 return 0;
11994 }
11995
11996 return 1;
11997 }
11998
11999 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12000 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12001 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12002 This is for DWP version 1 files. */
12003
12004 static struct dwo_unit *
12005 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
12006 struct dwp_file *dwp_file,
12007 uint32_t unit_index,
12008 const char *comp_dir,
12009 ULONGEST signature, int is_debug_types)
12010 {
12011 const struct dwp_hash_table *dwp_htab =
12012 is_debug_types ? dwp_file->tus : dwp_file->cus;
12013 bfd *dbfd = dwp_file->dbfd.get ();
12014 const char *kind = is_debug_types ? "TU" : "CU";
12015 struct dwo_file *dwo_file;
12016 struct dwo_unit *dwo_unit;
12017 struct virtual_v1_dwo_sections sections;
12018 void **dwo_file_slot;
12019 int i;
12020
12021 gdb_assert (dwp_file->version == 1);
12022
12023 if (dwarf_read_debug)
12024 {
12025 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12026 kind,
12027 pulongest (unit_index), hex_string (signature),
12028 dwp_file->name);
12029 }
12030
12031 /* Fetch the sections of this DWO unit.
12032 Put a limit on the number of sections we look for so that bad data
12033 doesn't cause us to loop forever. */
12034
12035 #define MAX_NR_V1_DWO_SECTIONS \
12036 (1 /* .debug_info or .debug_types */ \
12037 + 1 /* .debug_abbrev */ \
12038 + 1 /* .debug_line */ \
12039 + 1 /* .debug_loc */ \
12040 + 1 /* .debug_str_offsets */ \
12041 + 1 /* .debug_macro or .debug_macinfo */ \
12042 + 1 /* trailing zero */)
12043
12044 memset (&sections, 0, sizeof (sections));
12045
12046 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12047 {
12048 asection *sectp;
12049 uint32_t section_nr =
12050 read_4_bytes (dbfd,
12051 dwp_htab->section_pool.v1.indices
12052 + (unit_index + i) * sizeof (uint32_t));
12053
12054 if (section_nr == 0)
12055 break;
12056 if (section_nr >= dwp_file->num_sections)
12057 {
12058 error (_("Dwarf Error: bad DWP hash table, section number too large"
12059 " [in module %s]"),
12060 dwp_file->name);
12061 }
12062
12063 sectp = dwp_file->elf_sections[section_nr];
12064 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12065 {
12066 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12067 " [in module %s]"),
12068 dwp_file->name);
12069 }
12070 }
12071
12072 if (i < 2
12073 || sections.info_or_types.empty ()
12074 || sections.abbrev.empty ())
12075 {
12076 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12077 " [in module %s]"),
12078 dwp_file->name);
12079 }
12080 if (i == MAX_NR_V1_DWO_SECTIONS)
12081 {
12082 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12083 " [in module %s]"),
12084 dwp_file->name);
12085 }
12086
12087 /* It's easier for the rest of the code if we fake a struct dwo_file and
12088 have dwo_unit "live" in that. At least for now.
12089
12090 The DWP file can be made up of a random collection of CUs and TUs.
12091 However, for each CU + set of TUs that came from the same original DWO
12092 file, we can combine them back into a virtual DWO file to save space
12093 (fewer struct dwo_file objects to allocate). Remember that for really
12094 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12095
12096 std::string virtual_dwo_name =
12097 string_printf ("virtual-dwo/%d-%d-%d-%d",
12098 sections.abbrev.get_id (),
12099 sections.line.get_id (),
12100 sections.loc.get_id (),
12101 sections.str_offsets.get_id ());
12102 /* Can we use an existing virtual DWO file? */
12103 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12104 comp_dir);
12105 /* Create one if necessary. */
12106 if (*dwo_file_slot == NULL)
12107 {
12108 if (dwarf_read_debug)
12109 {
12110 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12111 virtual_dwo_name.c_str ());
12112 }
12113 dwo_file = new struct dwo_file;
12114 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12115 dwo_file->comp_dir = comp_dir;
12116 dwo_file->sections.abbrev = sections.abbrev;
12117 dwo_file->sections.line = sections.line;
12118 dwo_file->sections.loc = sections.loc;
12119 dwo_file->sections.macinfo = sections.macinfo;
12120 dwo_file->sections.macro = sections.macro;
12121 dwo_file->sections.str_offsets = sections.str_offsets;
12122 /* The "str" section is global to the entire DWP file. */
12123 dwo_file->sections.str = dwp_file->sections.str;
12124 /* The info or types section is assigned below to dwo_unit,
12125 there's no need to record it in dwo_file.
12126 Also, we can't simply record type sections in dwo_file because
12127 we record a pointer into the vector in dwo_unit. As we collect more
12128 types we'll grow the vector and eventually have to reallocate space
12129 for it, invalidating all copies of pointers into the previous
12130 contents. */
12131 *dwo_file_slot = dwo_file;
12132 }
12133 else
12134 {
12135 if (dwarf_read_debug)
12136 {
12137 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12138 virtual_dwo_name.c_str ());
12139 }
12140 dwo_file = (struct dwo_file *) *dwo_file_slot;
12141 }
12142
12143 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12144 dwo_unit->dwo_file = dwo_file;
12145 dwo_unit->signature = signature;
12146 dwo_unit->section =
12147 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12148 *dwo_unit->section = sections.info_or_types;
12149 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12150
12151 return dwo_unit;
12152 }
12153
12154 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
12155 simplify them. Given a pointer to the containing section SECTION, and
12156 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
12157 virtual section of just that piece. */
12158
12159 static struct dwarf2_section_info
12160 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
12161 struct dwarf2_section_info *section,
12162 bfd_size_type offset, bfd_size_type size)
12163 {
12164 struct dwarf2_section_info result;
12165 asection *sectp;
12166
12167 gdb_assert (section != NULL);
12168 gdb_assert (!section->is_virtual);
12169
12170 memset (&result, 0, sizeof (result));
12171 result.s.containing_section = section;
12172 result.is_virtual = true;
12173
12174 if (size == 0)
12175 return result;
12176
12177 sectp = section->get_bfd_section ();
12178
12179 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12180 bounds of the real section. This is a pretty-rare event, so just
12181 flag an error (easier) instead of a warning and trying to cope. */
12182 if (sectp == NULL
12183 || offset + size > bfd_section_size (sectp))
12184 {
12185 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
12186 " in section %s [in module %s]"),
12187 sectp ? bfd_section_name (sectp) : "<unknown>",
12188 objfile_name (per_objfile->objfile));
12189 }
12190
12191 result.virtual_offset = offset;
12192 result.size = size;
12193 return result;
12194 }
12195
12196 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12197 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12198 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12199 This is for DWP version 2 files. */
12200
12201 static struct dwo_unit *
12202 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
12203 struct dwp_file *dwp_file,
12204 uint32_t unit_index,
12205 const char *comp_dir,
12206 ULONGEST signature, int is_debug_types)
12207 {
12208 const struct dwp_hash_table *dwp_htab =
12209 is_debug_types ? dwp_file->tus : dwp_file->cus;
12210 bfd *dbfd = dwp_file->dbfd.get ();
12211 const char *kind = is_debug_types ? "TU" : "CU";
12212 struct dwo_file *dwo_file;
12213 struct dwo_unit *dwo_unit;
12214 struct virtual_v2_or_v5_dwo_sections sections;
12215 void **dwo_file_slot;
12216 int i;
12217
12218 gdb_assert (dwp_file->version == 2);
12219
12220 if (dwarf_read_debug)
12221 {
12222 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12223 kind,
12224 pulongest (unit_index), hex_string (signature),
12225 dwp_file->name);
12226 }
12227
12228 /* Fetch the section offsets of this DWO unit. */
12229
12230 memset (&sections, 0, sizeof (sections));
12231
12232 for (i = 0; i < dwp_htab->nr_columns; ++i)
12233 {
12234 uint32_t offset = read_4_bytes (dbfd,
12235 dwp_htab->section_pool.v2.offsets
12236 + (((unit_index - 1) * dwp_htab->nr_columns
12237 + i)
12238 * sizeof (uint32_t)));
12239 uint32_t size = read_4_bytes (dbfd,
12240 dwp_htab->section_pool.v2.sizes
12241 + (((unit_index - 1) * dwp_htab->nr_columns
12242 + i)
12243 * sizeof (uint32_t)));
12244
12245 switch (dwp_htab->section_pool.v2.section_ids[i])
12246 {
12247 case DW_SECT_INFO:
12248 case DW_SECT_TYPES:
12249 sections.info_or_types_offset = offset;
12250 sections.info_or_types_size = size;
12251 break;
12252 case DW_SECT_ABBREV:
12253 sections.abbrev_offset = offset;
12254 sections.abbrev_size = size;
12255 break;
12256 case DW_SECT_LINE:
12257 sections.line_offset = offset;
12258 sections.line_size = size;
12259 break;
12260 case DW_SECT_LOC:
12261 sections.loc_offset = offset;
12262 sections.loc_size = size;
12263 break;
12264 case DW_SECT_STR_OFFSETS:
12265 sections.str_offsets_offset = offset;
12266 sections.str_offsets_size = size;
12267 break;
12268 case DW_SECT_MACINFO:
12269 sections.macinfo_offset = offset;
12270 sections.macinfo_size = size;
12271 break;
12272 case DW_SECT_MACRO:
12273 sections.macro_offset = offset;
12274 sections.macro_size = size;
12275 break;
12276 }
12277 }
12278
12279 /* It's easier for the rest of the code if we fake a struct dwo_file and
12280 have dwo_unit "live" in that. At least for now.
12281
12282 The DWP file can be made up of a random collection of CUs and TUs.
12283 However, for each CU + set of TUs that came from the same original DWO
12284 file, we can combine them back into a virtual DWO file to save space
12285 (fewer struct dwo_file objects to allocate). Remember that for really
12286 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12287
12288 std::string virtual_dwo_name =
12289 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12290 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12291 (long) (sections.line_size ? sections.line_offset : 0),
12292 (long) (sections.loc_size ? sections.loc_offset : 0),
12293 (long) (sections.str_offsets_size
12294 ? sections.str_offsets_offset : 0));
12295 /* Can we use an existing virtual DWO file? */
12296 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12297 comp_dir);
12298 /* Create one if necessary. */
12299 if (*dwo_file_slot == NULL)
12300 {
12301 if (dwarf_read_debug)
12302 {
12303 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12304 virtual_dwo_name.c_str ());
12305 }
12306 dwo_file = new struct dwo_file;
12307 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12308 dwo_file->comp_dir = comp_dir;
12309 dwo_file->sections.abbrev =
12310 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12311 sections.abbrev_offset,
12312 sections.abbrev_size);
12313 dwo_file->sections.line =
12314 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12315 sections.line_offset,
12316 sections.line_size);
12317 dwo_file->sections.loc =
12318 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12319 sections.loc_offset, sections.loc_size);
12320 dwo_file->sections.macinfo =
12321 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12322 sections.macinfo_offset,
12323 sections.macinfo_size);
12324 dwo_file->sections.macro =
12325 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12326 sections.macro_offset,
12327 sections.macro_size);
12328 dwo_file->sections.str_offsets =
12329 create_dwp_v2_or_v5_section (per_objfile,
12330 &dwp_file->sections.str_offsets,
12331 sections.str_offsets_offset,
12332 sections.str_offsets_size);
12333 /* The "str" section is global to the entire DWP file. */
12334 dwo_file->sections.str = dwp_file->sections.str;
12335 /* The info or types section is assigned below to dwo_unit,
12336 there's no need to record it in dwo_file.
12337 Also, we can't simply record type sections in dwo_file because
12338 we record a pointer into the vector in dwo_unit. As we collect more
12339 types we'll grow the vector and eventually have to reallocate space
12340 for it, invalidating all copies of pointers into the previous
12341 contents. */
12342 *dwo_file_slot = dwo_file;
12343 }
12344 else
12345 {
12346 if (dwarf_read_debug)
12347 {
12348 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12349 virtual_dwo_name.c_str ());
12350 }
12351 dwo_file = (struct dwo_file *) *dwo_file_slot;
12352 }
12353
12354 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12355 dwo_unit->dwo_file = dwo_file;
12356 dwo_unit->signature = signature;
12357 dwo_unit->section =
12358 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12359 *dwo_unit->section = create_dwp_v2_or_v5_section
12360 (per_objfile,
12361 is_debug_types
12362 ? &dwp_file->sections.types
12363 : &dwp_file->sections.info,
12364 sections.info_or_types_offset,
12365 sections.info_or_types_size);
12366 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12367
12368 return dwo_unit;
12369 }
12370
12371 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12372 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12373 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12374 This is for DWP version 5 files. */
12375
12376 static struct dwo_unit *
12377 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12378 struct dwp_file *dwp_file,
12379 uint32_t unit_index,
12380 const char *comp_dir,
12381 ULONGEST signature, int is_debug_types)
12382 {
12383 const struct dwp_hash_table *dwp_htab
12384 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12385 bfd *dbfd = dwp_file->dbfd.get ();
12386 const char *kind = is_debug_types ? "TU" : "CU";
12387 struct dwo_file *dwo_file;
12388 struct dwo_unit *dwo_unit;
12389 struct virtual_v2_or_v5_dwo_sections sections {};
12390 void **dwo_file_slot;
12391
12392 gdb_assert (dwp_file->version == 5);
12393
12394 if (dwarf_read_debug)
12395 {
12396 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V5 file: %s\n",
12397 kind,
12398 pulongest (unit_index), hex_string (signature),
12399 dwp_file->name);
12400 }
12401
12402 /* Fetch the section offsets of this DWO unit. */
12403
12404 /* memset (&sections, 0, sizeof (sections)); */
12405
12406 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12407 {
12408 uint32_t offset = read_4_bytes (dbfd,
12409 dwp_htab->section_pool.v5.offsets
12410 + (((unit_index - 1)
12411 * dwp_htab->nr_columns
12412 + i)
12413 * sizeof (uint32_t)));
12414 uint32_t size = read_4_bytes (dbfd,
12415 dwp_htab->section_pool.v5.sizes
12416 + (((unit_index - 1) * dwp_htab->nr_columns
12417 + i)
12418 * sizeof (uint32_t)));
12419
12420 switch (dwp_htab->section_pool.v5.section_ids[i])
12421 {
12422 case DW_SECT_ABBREV_V5:
12423 sections.abbrev_offset = offset;
12424 sections.abbrev_size = size;
12425 break;
12426 case DW_SECT_INFO_V5:
12427 sections.info_or_types_offset = offset;
12428 sections.info_or_types_size = size;
12429 break;
12430 case DW_SECT_LINE_V5:
12431 sections.line_offset = offset;
12432 sections.line_size = size;
12433 break;
12434 case DW_SECT_LOCLISTS_V5:
12435 sections.loclists_offset = offset;
12436 sections.loclists_size = size;
12437 break;
12438 case DW_SECT_MACRO_V5:
12439 sections.macro_offset = offset;
12440 sections.macro_size = size;
12441 break;
12442 case DW_SECT_RNGLISTS_V5:
12443 sections.rnglists_offset = offset;
12444 sections.rnglists_size = size;
12445 break;
12446 case DW_SECT_STR_OFFSETS_V5:
12447 sections.str_offsets_offset = offset;
12448 sections.str_offsets_size = size;
12449 break;
12450 case DW_SECT_RESERVED_V5:
12451 default:
12452 break;
12453 }
12454 }
12455
12456 /* It's easier for the rest of the code if we fake a struct dwo_file and
12457 have dwo_unit "live" in that. At least for now.
12458
12459 The DWP file can be made up of a random collection of CUs and TUs.
12460 However, for each CU + set of TUs that came from the same original DWO
12461 file, we can combine them back into a virtual DWO file to save space
12462 (fewer struct dwo_file objects to allocate). Remember that for really
12463 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12464
12465 std::string virtual_dwo_name =
12466 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
12467 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12468 (long) (sections.line_size ? sections.line_offset : 0),
12469 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12470 (long) (sections.str_offsets_size
12471 ? sections.str_offsets_offset : 0),
12472 (long) (sections.macro_size ? sections.macro_offset : 0),
12473 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
12474 /* Can we use an existing virtual DWO file? */
12475 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
12476 virtual_dwo_name.c_str (),
12477 comp_dir);
12478 /* Create one if necessary. */
12479 if (*dwo_file_slot == NULL)
12480 {
12481 if (dwarf_read_debug)
12482 {
12483 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12484 virtual_dwo_name.c_str ());
12485 }
12486 dwo_file = new struct dwo_file;
12487 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12488 dwo_file->comp_dir = comp_dir;
12489 dwo_file->sections.abbrev =
12490 create_dwp_v2_or_v5_section (per_objfile,
12491 &dwp_file->sections.abbrev,
12492 sections.abbrev_offset,
12493 sections.abbrev_size);
12494 dwo_file->sections.line =
12495 create_dwp_v2_or_v5_section (per_objfile,
12496 &dwp_file->sections.line,
12497 sections.line_offset, sections.line_size);
12498 dwo_file->sections.macro =
12499 create_dwp_v2_or_v5_section (per_objfile,
12500 &dwp_file->sections.macro,
12501 sections.macro_offset,
12502 sections.macro_size);
12503 dwo_file->sections.loclists =
12504 create_dwp_v2_or_v5_section (per_objfile,
12505 &dwp_file->sections.loclists,
12506 sections.loclists_offset,
12507 sections.loclists_size);
12508 dwo_file->sections.rnglists =
12509 create_dwp_v2_or_v5_section (per_objfile,
12510 &dwp_file->sections.rnglists,
12511 sections.rnglists_offset,
12512 sections.rnglists_size);
12513 dwo_file->sections.str_offsets =
12514 create_dwp_v2_or_v5_section (per_objfile,
12515 &dwp_file->sections.str_offsets,
12516 sections.str_offsets_offset,
12517 sections.str_offsets_size);
12518 /* The "str" section is global to the entire DWP file. */
12519 dwo_file->sections.str = dwp_file->sections.str;
12520 /* The info or types section is assigned below to dwo_unit,
12521 there's no need to record it in dwo_file.
12522 Also, we can't simply record type sections in dwo_file because
12523 we record a pointer into the vector in dwo_unit. As we collect more
12524 types we'll grow the vector and eventually have to reallocate space
12525 for it, invalidating all copies of pointers into the previous
12526 contents. */
12527 *dwo_file_slot = dwo_file;
12528 }
12529 else
12530 {
12531 if (dwarf_read_debug)
12532 {
12533 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12534 virtual_dwo_name.c_str ());
12535 }
12536 dwo_file = (struct dwo_file *) *dwo_file_slot;
12537 }
12538
12539 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12540 dwo_unit->dwo_file = dwo_file;
12541 dwo_unit->signature = signature;
12542 dwo_unit->section
12543 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12544 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
12545 &dwp_file->sections.info,
12546 sections.info_or_types_offset,
12547 sections.info_or_types_size);
12548 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12549
12550 return dwo_unit;
12551 }
12552
12553 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12554 Returns NULL if the signature isn't found. */
12555
12556 static struct dwo_unit *
12557 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12558 struct dwp_file *dwp_file, const char *comp_dir,
12559 ULONGEST signature, int is_debug_types)
12560 {
12561 const struct dwp_hash_table *dwp_htab =
12562 is_debug_types ? dwp_file->tus : dwp_file->cus;
12563 bfd *dbfd = dwp_file->dbfd.get ();
12564 uint32_t mask = dwp_htab->nr_slots - 1;
12565 uint32_t hash = signature & mask;
12566 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12567 unsigned int i;
12568 void **slot;
12569 struct dwo_unit find_dwo_cu;
12570
12571 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12572 find_dwo_cu.signature = signature;
12573 slot = htab_find_slot (is_debug_types
12574 ? dwp_file->loaded_tus.get ()
12575 : dwp_file->loaded_cus.get (),
12576 &find_dwo_cu, INSERT);
12577
12578 if (*slot != NULL)
12579 return (struct dwo_unit *) *slot;
12580
12581 /* Use a for loop so that we don't loop forever on bad debug info. */
12582 for (i = 0; i < dwp_htab->nr_slots; ++i)
12583 {
12584 ULONGEST signature_in_table;
12585
12586 signature_in_table =
12587 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12588 if (signature_in_table == signature)
12589 {
12590 uint32_t unit_index =
12591 read_4_bytes (dbfd,
12592 dwp_htab->unit_table + hash * sizeof (uint32_t));
12593
12594 if (dwp_file->version == 1)
12595 {
12596 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12597 unit_index, comp_dir,
12598 signature, is_debug_types);
12599 }
12600 else if (dwp_file->version == 2)
12601 {
12602 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12603 unit_index, comp_dir,
12604 signature, is_debug_types);
12605 }
12606 else /* version == 5 */
12607 {
12608 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12609 unit_index, comp_dir,
12610 signature, is_debug_types);
12611 }
12612 return (struct dwo_unit *) *slot;
12613 }
12614 if (signature_in_table == 0)
12615 return NULL;
12616 hash = (hash + hash2) & mask;
12617 }
12618
12619 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12620 " [in module %s]"),
12621 dwp_file->name);
12622 }
12623
12624 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12625 Open the file specified by FILE_NAME and hand it off to BFD for
12626 preliminary analysis. Return a newly initialized bfd *, which
12627 includes a canonicalized copy of FILE_NAME.
12628 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12629 SEARCH_CWD is true if the current directory is to be searched.
12630 It will be searched before debug-file-directory.
12631 If successful, the file is added to the bfd include table of the
12632 objfile's bfd (see gdb_bfd_record_inclusion).
12633 If unable to find/open the file, return NULL.
12634 NOTE: This function is derived from symfile_bfd_open. */
12635
12636 static gdb_bfd_ref_ptr
12637 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12638 const char *file_name, int is_dwp, int search_cwd)
12639 {
12640 int desc;
12641 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12642 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12643 to debug_file_directory. */
12644 const char *search_path;
12645 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12646
12647 gdb::unique_xmalloc_ptr<char> search_path_holder;
12648 if (search_cwd)
12649 {
12650 if (*debug_file_directory != '\0')
12651 {
12652 search_path_holder.reset (concat (".", dirname_separator_string,
12653 debug_file_directory,
12654 (char *) NULL));
12655 search_path = search_path_holder.get ();
12656 }
12657 else
12658 search_path = ".";
12659 }
12660 else
12661 search_path = debug_file_directory;
12662
12663 openp_flags flags = OPF_RETURN_REALPATH;
12664 if (is_dwp)
12665 flags |= OPF_SEARCH_IN_PATH;
12666
12667 gdb::unique_xmalloc_ptr<char> absolute_name;
12668 desc = openp (search_path, flags, file_name,
12669 O_RDONLY | O_BINARY, &absolute_name);
12670 if (desc < 0)
12671 return NULL;
12672
12673 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12674 gnutarget, desc));
12675 if (sym_bfd == NULL)
12676 return NULL;
12677 bfd_set_cacheable (sym_bfd.get (), 1);
12678
12679 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12680 return NULL;
12681
12682 /* Success. Record the bfd as having been included by the objfile's bfd.
12683 This is important because things like demangled_names_hash lives in the
12684 objfile's per_bfd space and may have references to things like symbol
12685 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12686 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12687
12688 return sym_bfd;
12689 }
12690
12691 /* Try to open DWO file FILE_NAME.
12692 COMP_DIR is the DW_AT_comp_dir attribute.
12693 The result is the bfd handle of the file.
12694 If there is a problem finding or opening the file, return NULL.
12695 Upon success, the canonicalized path of the file is stored in the bfd,
12696 same as symfile_bfd_open. */
12697
12698 static gdb_bfd_ref_ptr
12699 open_dwo_file (dwarf2_per_objfile *per_objfile,
12700 const char *file_name, const char *comp_dir)
12701 {
12702 if (IS_ABSOLUTE_PATH (file_name))
12703 return try_open_dwop_file (per_objfile, file_name,
12704 0 /*is_dwp*/, 0 /*search_cwd*/);
12705
12706 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12707
12708 if (comp_dir != NULL)
12709 {
12710 gdb::unique_xmalloc_ptr<char> path_to_try
12711 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12712
12713 /* NOTE: If comp_dir is a relative path, this will also try the
12714 search path, which seems useful. */
12715 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12716 0 /*is_dwp*/,
12717 1 /*search_cwd*/));
12718 if (abfd != NULL)
12719 return abfd;
12720 }
12721
12722 /* That didn't work, try debug-file-directory, which, despite its name,
12723 is a list of paths. */
12724
12725 if (*debug_file_directory == '\0')
12726 return NULL;
12727
12728 return try_open_dwop_file (per_objfile, file_name,
12729 0 /*is_dwp*/, 1 /*search_cwd*/);
12730 }
12731
12732 /* This function is mapped across the sections and remembers the offset and
12733 size of each of the DWO debugging sections we are interested in. */
12734
12735 static void
12736 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12737 dwo_sections *dwo_sections)
12738 {
12739 const struct dwop_section_names *names = &dwop_section_names;
12740
12741 if (section_is_p (sectp->name, &names->abbrev_dwo))
12742 {
12743 dwo_sections->abbrev.s.section = sectp;
12744 dwo_sections->abbrev.size = bfd_section_size (sectp);
12745 }
12746 else if (section_is_p (sectp->name, &names->info_dwo))
12747 {
12748 dwo_sections->info.s.section = sectp;
12749 dwo_sections->info.size = bfd_section_size (sectp);
12750 }
12751 else if (section_is_p (sectp->name, &names->line_dwo))
12752 {
12753 dwo_sections->line.s.section = sectp;
12754 dwo_sections->line.size = bfd_section_size (sectp);
12755 }
12756 else if (section_is_p (sectp->name, &names->loc_dwo))
12757 {
12758 dwo_sections->loc.s.section = sectp;
12759 dwo_sections->loc.size = bfd_section_size (sectp);
12760 }
12761 else if (section_is_p (sectp->name, &names->loclists_dwo))
12762 {
12763 dwo_sections->loclists.s.section = sectp;
12764 dwo_sections->loclists.size = bfd_section_size (sectp);
12765 }
12766 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12767 {
12768 dwo_sections->macinfo.s.section = sectp;
12769 dwo_sections->macinfo.size = bfd_section_size (sectp);
12770 }
12771 else if (section_is_p (sectp->name, &names->macro_dwo))
12772 {
12773 dwo_sections->macro.s.section = sectp;
12774 dwo_sections->macro.size = bfd_section_size (sectp);
12775 }
12776 else if (section_is_p (sectp->name, &names->rnglists_dwo))
12777 {
12778 dwo_sections->rnglists.s.section = sectp;
12779 dwo_sections->rnglists.size = bfd_section_size (sectp);
12780 }
12781 else if (section_is_p (sectp->name, &names->str_dwo))
12782 {
12783 dwo_sections->str.s.section = sectp;
12784 dwo_sections->str.size = bfd_section_size (sectp);
12785 }
12786 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12787 {
12788 dwo_sections->str_offsets.s.section = sectp;
12789 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12790 }
12791 else if (section_is_p (sectp->name, &names->types_dwo))
12792 {
12793 struct dwarf2_section_info type_section;
12794
12795 memset (&type_section, 0, sizeof (type_section));
12796 type_section.s.section = sectp;
12797 type_section.size = bfd_section_size (sectp);
12798 dwo_sections->types.push_back (type_section);
12799 }
12800 }
12801
12802 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12803 by PER_CU. This is for the non-DWP case.
12804 The result is NULL if DWO_NAME can't be found. */
12805
12806 static struct dwo_file *
12807 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12808 const char *comp_dir)
12809 {
12810 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12811
12812 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12813 if (dbfd == NULL)
12814 {
12815 if (dwarf_read_debug)
12816 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12817 return NULL;
12818 }
12819
12820 dwo_file_up dwo_file (new struct dwo_file);
12821 dwo_file->dwo_name = dwo_name;
12822 dwo_file->comp_dir = comp_dir;
12823 dwo_file->dbfd = std::move (dbfd);
12824
12825 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12826 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12827 &dwo_file->sections);
12828
12829 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12830 dwo_file->cus);
12831
12832 if (cu->per_cu->dwarf_version < 5)
12833 {
12834 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12835 dwo_file->sections.types, dwo_file->tus);
12836 }
12837 else
12838 {
12839 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12840 &dwo_file->sections.info, dwo_file->tus,
12841 rcuh_kind::TYPE);
12842 }
12843
12844 if (dwarf_read_debug)
12845 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12846
12847 return dwo_file.release ();
12848 }
12849
12850 /* This function is mapped across the sections and remembers the offset and
12851 size of each of the DWP debugging sections common to version 1 and 2 that
12852 we are interested in. */
12853
12854 static void
12855 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12856 dwp_file *dwp_file)
12857 {
12858 const struct dwop_section_names *names = &dwop_section_names;
12859 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12860
12861 /* Record the ELF section number for later lookup: this is what the
12862 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12863 gdb_assert (elf_section_nr < dwp_file->num_sections);
12864 dwp_file->elf_sections[elf_section_nr] = sectp;
12865
12866 /* Look for specific sections that we need. */
12867 if (section_is_p (sectp->name, &names->str_dwo))
12868 {
12869 dwp_file->sections.str.s.section = sectp;
12870 dwp_file->sections.str.size = bfd_section_size (sectp);
12871 }
12872 else if (section_is_p (sectp->name, &names->cu_index))
12873 {
12874 dwp_file->sections.cu_index.s.section = sectp;
12875 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12876 }
12877 else if (section_is_p (sectp->name, &names->tu_index))
12878 {
12879 dwp_file->sections.tu_index.s.section = sectp;
12880 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12881 }
12882 }
12883
12884 /* This function is mapped across the sections and remembers the offset and
12885 size of each of the DWP version 2 debugging sections that we are interested
12886 in. This is split into a separate function because we don't know if we
12887 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12888
12889 static void
12890 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12891 {
12892 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12893 const struct dwop_section_names *names = &dwop_section_names;
12894 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12895
12896 /* Record the ELF section number for later lookup: this is what the
12897 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12898 gdb_assert (elf_section_nr < dwp_file->num_sections);
12899 dwp_file->elf_sections[elf_section_nr] = sectp;
12900
12901 /* Look for specific sections that we need. */
12902 if (section_is_p (sectp->name, &names->abbrev_dwo))
12903 {
12904 dwp_file->sections.abbrev.s.section = sectp;
12905 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12906 }
12907 else if (section_is_p (sectp->name, &names->info_dwo))
12908 {
12909 dwp_file->sections.info.s.section = sectp;
12910 dwp_file->sections.info.size = bfd_section_size (sectp);
12911 }
12912 else if (section_is_p (sectp->name, &names->line_dwo))
12913 {
12914 dwp_file->sections.line.s.section = sectp;
12915 dwp_file->sections.line.size = bfd_section_size (sectp);
12916 }
12917 else if (section_is_p (sectp->name, &names->loc_dwo))
12918 {
12919 dwp_file->sections.loc.s.section = sectp;
12920 dwp_file->sections.loc.size = bfd_section_size (sectp);
12921 }
12922 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12923 {
12924 dwp_file->sections.macinfo.s.section = sectp;
12925 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12926 }
12927 else if (section_is_p (sectp->name, &names->macro_dwo))
12928 {
12929 dwp_file->sections.macro.s.section = sectp;
12930 dwp_file->sections.macro.size = bfd_section_size (sectp);
12931 }
12932 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12933 {
12934 dwp_file->sections.str_offsets.s.section = sectp;
12935 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12936 }
12937 else if (section_is_p (sectp->name, &names->types_dwo))
12938 {
12939 dwp_file->sections.types.s.section = sectp;
12940 dwp_file->sections.types.size = bfd_section_size (sectp);
12941 }
12942 }
12943
12944 /* This function is mapped across the sections and remembers the offset and
12945 size of each of the DWP version 5 debugging sections that we are interested
12946 in. This is split into a separate function because we don't know if we
12947 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12948
12949 static void
12950 dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12951 {
12952 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12953 const struct dwop_section_names *names = &dwop_section_names;
12954 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12955
12956 /* Record the ELF section number for later lookup: this is what the
12957 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12958 gdb_assert (elf_section_nr < dwp_file->num_sections);
12959 dwp_file->elf_sections[elf_section_nr] = sectp;
12960
12961 /* Look for specific sections that we need. */
12962 if (section_is_p (sectp->name, &names->abbrev_dwo))
12963 {
12964 dwp_file->sections.abbrev.s.section = sectp;
12965 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12966 }
12967 else if (section_is_p (sectp->name, &names->info_dwo))
12968 {
12969 dwp_file->sections.info.s.section = sectp;
12970 dwp_file->sections.info.size = bfd_section_size (sectp);
12971 }
12972 else if (section_is_p (sectp->name, &names->line_dwo))
12973 {
12974 dwp_file->sections.line.s.section = sectp;
12975 dwp_file->sections.line.size = bfd_section_size (sectp);
12976 }
12977 else if (section_is_p (sectp->name, &names->loclists_dwo))
12978 {
12979 dwp_file->sections.loclists.s.section = sectp;
12980 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12981 }
12982 else if (section_is_p (sectp->name, &names->macro_dwo))
12983 {
12984 dwp_file->sections.macro.s.section = sectp;
12985 dwp_file->sections.macro.size = bfd_section_size (sectp);
12986 }
12987 else if (section_is_p (sectp->name, &names->rnglists_dwo))
12988 {
12989 dwp_file->sections.rnglists.s.section = sectp;
12990 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12991 }
12992 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12993 {
12994 dwp_file->sections.str_offsets.s.section = sectp;
12995 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12996 }
12997 }
12998
12999 /* Hash function for dwp_file loaded CUs/TUs. */
13000
13001 static hashval_t
13002 hash_dwp_loaded_cutus (const void *item)
13003 {
13004 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13005
13006 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13007 return dwo_unit->signature;
13008 }
13009
13010 /* Equality function for dwp_file loaded CUs/TUs. */
13011
13012 static int
13013 eq_dwp_loaded_cutus (const void *a, const void *b)
13014 {
13015 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13016 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13017
13018 return dua->signature == dub->signature;
13019 }
13020
13021 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13022
13023 static htab_up
13024 allocate_dwp_loaded_cutus_table ()
13025 {
13026 return htab_up (htab_create_alloc (3,
13027 hash_dwp_loaded_cutus,
13028 eq_dwp_loaded_cutus,
13029 NULL, xcalloc, xfree));
13030 }
13031
13032 /* Try to open DWP file FILE_NAME.
13033 The result is the bfd handle of the file.
13034 If there is a problem finding or opening the file, return NULL.
13035 Upon success, the canonicalized path of the file is stored in the bfd,
13036 same as symfile_bfd_open. */
13037
13038 static gdb_bfd_ref_ptr
13039 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
13040 {
13041 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
13042 1 /*is_dwp*/,
13043 1 /*search_cwd*/));
13044 if (abfd != NULL)
13045 return abfd;
13046
13047 /* Work around upstream bug 15652.
13048 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13049 [Whether that's a "bug" is debatable, but it is getting in our way.]
13050 We have no real idea where the dwp file is, because gdb's realpath-ing
13051 of the executable's path may have discarded the needed info.
13052 [IWBN if the dwp file name was recorded in the executable, akin to
13053 .gnu_debuglink, but that doesn't exist yet.]
13054 Strip the directory from FILE_NAME and search again. */
13055 if (*debug_file_directory != '\0')
13056 {
13057 /* Don't implicitly search the current directory here.
13058 If the user wants to search "." to handle this case,
13059 it must be added to debug-file-directory. */
13060 return try_open_dwop_file (per_objfile, lbasename (file_name),
13061 1 /*is_dwp*/,
13062 0 /*search_cwd*/);
13063 }
13064
13065 return NULL;
13066 }
13067
13068 /* Initialize the use of the DWP file for the current objfile.
13069 By convention the name of the DWP file is ${objfile}.dwp.
13070 The result is NULL if it can't be found. */
13071
13072 static std::unique_ptr<struct dwp_file>
13073 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
13074 {
13075 struct objfile *objfile = per_objfile->objfile;
13076
13077 /* Try to find first .dwp for the binary file before any symbolic links
13078 resolving. */
13079
13080 /* If the objfile is a debug file, find the name of the real binary
13081 file and get the name of dwp file from there. */
13082 std::string dwp_name;
13083 if (objfile->separate_debug_objfile_backlink != NULL)
13084 {
13085 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13086 const char *backlink_basename = lbasename (backlink->original_name);
13087
13088 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13089 }
13090 else
13091 dwp_name = objfile->original_name;
13092
13093 dwp_name += ".dwp";
13094
13095 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
13096 if (dbfd == NULL
13097 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13098 {
13099 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13100 dwp_name = objfile_name (objfile);
13101 dwp_name += ".dwp";
13102 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
13103 }
13104
13105 if (dbfd == NULL)
13106 {
13107 if (dwarf_read_debug)
13108 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13109 return std::unique_ptr<dwp_file> ();
13110 }
13111
13112 const char *name = bfd_get_filename (dbfd.get ());
13113 std::unique_ptr<struct dwp_file> dwp_file
13114 (new struct dwp_file (name, std::move (dbfd)));
13115
13116 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13117 dwp_file->elf_sections =
13118 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
13119 dwp_file->num_sections, asection *);
13120
13121 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13122 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
13123 dwp_file.get ());
13124
13125 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
13126
13127 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
13128
13129 /* The DWP file version is stored in the hash table. Oh well. */
13130 if (dwp_file->cus && dwp_file->tus
13131 && dwp_file->cus->version != dwp_file->tus->version)
13132 {
13133 /* Technically speaking, we should try to limp along, but this is
13134 pretty bizarre. We use pulongest here because that's the established
13135 portability solution (e.g, we cannot use %u for uint32_t). */
13136 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13137 " TU version %s [in DWP file %s]"),
13138 pulongest (dwp_file->cus->version),
13139 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13140 }
13141
13142 if (dwp_file->cus)
13143 dwp_file->version = dwp_file->cus->version;
13144 else if (dwp_file->tus)
13145 dwp_file->version = dwp_file->tus->version;
13146 else
13147 dwp_file->version = 2;
13148
13149 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13150 {
13151 if (dwp_file->version == 2)
13152 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
13153 dwp_file.get ());
13154 else
13155 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
13156 dwp_file.get ());
13157 }
13158
13159 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
13160 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
13161
13162 if (dwarf_read_debug)
13163 {
13164 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13165 fprintf_unfiltered (gdb_stdlog,
13166 " %s CUs, %s TUs\n",
13167 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13168 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13169 }
13170
13171 return dwp_file;
13172 }
13173
13174 /* Wrapper around open_and_init_dwp_file, only open it once. */
13175
13176 static struct dwp_file *
13177 get_dwp_file (dwarf2_per_objfile *per_objfile)
13178 {
13179 if (!per_objfile->per_bfd->dwp_checked)
13180 {
13181 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
13182 per_objfile->per_bfd->dwp_checked = 1;
13183 }
13184 return per_objfile->per_bfd->dwp_file.get ();
13185 }
13186
13187 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13188 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13189 or in the DWP file for the objfile, referenced by THIS_UNIT.
13190 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13191 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13192
13193 This is called, for example, when wanting to read a variable with a
13194 complex location. Therefore we don't want to do file i/o for every call.
13195 Therefore we don't want to look for a DWO file on every call.
13196 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13197 then we check if we've already seen DWO_NAME, and only THEN do we check
13198 for a DWO file.
13199
13200 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13201 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13202
13203 static struct dwo_unit *
13204 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13205 ULONGEST signature, int is_debug_types)
13206 {
13207 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13208 struct objfile *objfile = per_objfile->objfile;
13209 const char *kind = is_debug_types ? "TU" : "CU";
13210 void **dwo_file_slot;
13211 struct dwo_file *dwo_file;
13212 struct dwp_file *dwp_file;
13213
13214 /* First see if there's a DWP file.
13215 If we have a DWP file but didn't find the DWO inside it, don't
13216 look for the original DWO file. It makes gdb behave differently
13217 depending on whether one is debugging in the build tree. */
13218
13219 dwp_file = get_dwp_file (per_objfile);
13220 if (dwp_file != NULL)
13221 {
13222 const struct dwp_hash_table *dwp_htab =
13223 is_debug_types ? dwp_file->tus : dwp_file->cus;
13224
13225 if (dwp_htab != NULL)
13226 {
13227 struct dwo_unit *dwo_cutu =
13228 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
13229 is_debug_types);
13230
13231 if (dwo_cutu != NULL)
13232 {
13233 if (dwarf_read_debug)
13234 {
13235 fprintf_unfiltered (gdb_stdlog,
13236 "Virtual DWO %s %s found: @%s\n",
13237 kind, hex_string (signature),
13238 host_address_to_string (dwo_cutu));
13239 }
13240 return dwo_cutu;
13241 }
13242 }
13243 }
13244 else
13245 {
13246 /* No DWP file, look for the DWO file. */
13247
13248 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
13249 if (*dwo_file_slot == NULL)
13250 {
13251 /* Read in the file and build a table of the CUs/TUs it contains. */
13252 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
13253 }
13254 /* NOTE: This will be NULL if unable to open the file. */
13255 dwo_file = (struct dwo_file *) *dwo_file_slot;
13256
13257 if (dwo_file != NULL)
13258 {
13259 struct dwo_unit *dwo_cutu = NULL;
13260
13261 if (is_debug_types && dwo_file->tus)
13262 {
13263 struct dwo_unit find_dwo_cutu;
13264
13265 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13266 find_dwo_cutu.signature = signature;
13267 dwo_cutu
13268 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
13269 &find_dwo_cutu);
13270 }
13271 else if (!is_debug_types && dwo_file->cus)
13272 {
13273 struct dwo_unit find_dwo_cutu;
13274
13275 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13276 find_dwo_cutu.signature = signature;
13277 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
13278 &find_dwo_cutu);
13279 }
13280
13281 if (dwo_cutu != NULL)
13282 {
13283 if (dwarf_read_debug)
13284 {
13285 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13286 kind, dwo_name, hex_string (signature),
13287 host_address_to_string (dwo_cutu));
13288 }
13289 return dwo_cutu;
13290 }
13291 }
13292 }
13293
13294 /* We didn't find it. This could mean a dwo_id mismatch, or
13295 someone deleted the DWO/DWP file, or the search path isn't set up
13296 correctly to find the file. */
13297
13298 if (dwarf_read_debug)
13299 {
13300 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13301 kind, dwo_name, hex_string (signature));
13302 }
13303
13304 /* This is a warning and not a complaint because it can be caused by
13305 pilot error (e.g., user accidentally deleting the DWO). */
13306 {
13307 /* Print the name of the DWP file if we looked there, helps the user
13308 better diagnose the problem. */
13309 std::string dwp_text;
13310
13311 if (dwp_file != NULL)
13312 dwp_text = string_printf (" [in DWP file %s]",
13313 lbasename (dwp_file->name));
13314
13315 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13316 " [in module %s]"),
13317 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13318 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
13319 }
13320 return NULL;
13321 }
13322
13323 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13324 See lookup_dwo_cutu_unit for details. */
13325
13326 static struct dwo_unit *
13327 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13328 ULONGEST signature)
13329 {
13330 gdb_assert (!cu->per_cu->is_debug_types);
13331
13332 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
13333 }
13334
13335 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13336 See lookup_dwo_cutu_unit for details. */
13337
13338 static struct dwo_unit *
13339 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
13340 {
13341 gdb_assert (cu->per_cu->is_debug_types);
13342
13343 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13344
13345 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
13346 }
13347
13348 /* Traversal function for queue_and_load_all_dwo_tus. */
13349
13350 static int
13351 queue_and_load_dwo_tu (void **slot, void *info)
13352 {
13353 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13354 dwarf2_cu *cu = (dwarf2_cu *) info;
13355 ULONGEST signature = dwo_unit->signature;
13356 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
13357
13358 if (sig_type != NULL)
13359 {
13360 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13361
13362 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13363 a real dependency of PER_CU on SIG_TYPE. That is detected later
13364 while processing PER_CU. */
13365 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
13366 load_full_type_unit (sig_cu, cu->per_objfile);
13367 cu->per_cu->imported_symtabs_push (sig_cu);
13368 }
13369
13370 return 1;
13371 }
13372
13373 /* Queue all TUs contained in the DWO of CU to be read in.
13374 The DWO may have the only definition of the type, though it may not be
13375 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13376 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13377
13378 static void
13379 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
13380 {
13381 struct dwo_unit *dwo_unit;
13382 struct dwo_file *dwo_file;
13383
13384 gdb_assert (cu != nullptr);
13385 gdb_assert (!cu->per_cu->is_debug_types);
13386 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
13387
13388 dwo_unit = cu->dwo_unit;
13389 gdb_assert (dwo_unit != NULL);
13390
13391 dwo_file = dwo_unit->dwo_file;
13392 if (dwo_file->tus != NULL)
13393 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
13394 }
13395
13396 /* Read in various DIEs. */
13397
13398 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13399 Inherit only the children of the DW_AT_abstract_origin DIE not being
13400 already referenced by DW_AT_abstract_origin from the children of the
13401 current DIE. */
13402
13403 static void
13404 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13405 {
13406 struct die_info *child_die;
13407 sect_offset *offsetp;
13408 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13409 struct die_info *origin_die;
13410 /* Iterator of the ORIGIN_DIE children. */
13411 struct die_info *origin_child_die;
13412 struct attribute *attr;
13413 struct dwarf2_cu *origin_cu;
13414 struct pending **origin_previous_list_in_scope;
13415
13416 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13417 if (!attr)
13418 return;
13419
13420 /* Note that following die references may follow to a die in a
13421 different cu. */
13422
13423 origin_cu = cu;
13424 origin_die = follow_die_ref (die, attr, &origin_cu);
13425
13426 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13427 symbols in. */
13428 origin_previous_list_in_scope = origin_cu->list_in_scope;
13429 origin_cu->list_in_scope = cu->list_in_scope;
13430
13431 if (die->tag != origin_die->tag
13432 && !(die->tag == DW_TAG_inlined_subroutine
13433 && origin_die->tag == DW_TAG_subprogram))
13434 complaint (_("DIE %s and its abstract origin %s have different tags"),
13435 sect_offset_str (die->sect_off),
13436 sect_offset_str (origin_die->sect_off));
13437
13438 std::vector<sect_offset> offsets;
13439
13440 for (child_die = die->child;
13441 child_die && child_die->tag;
13442 child_die = child_die->sibling)
13443 {
13444 struct die_info *child_origin_die;
13445 struct dwarf2_cu *child_origin_cu;
13446
13447 /* We are trying to process concrete instance entries:
13448 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13449 it's not relevant to our analysis here. i.e. detecting DIEs that are
13450 present in the abstract instance but not referenced in the concrete
13451 one. */
13452 if (child_die->tag == DW_TAG_call_site
13453 || child_die->tag == DW_TAG_GNU_call_site)
13454 continue;
13455
13456 /* For each CHILD_DIE, find the corresponding child of
13457 ORIGIN_DIE. If there is more than one layer of
13458 DW_AT_abstract_origin, follow them all; there shouldn't be,
13459 but GCC versions at least through 4.4 generate this (GCC PR
13460 40573). */
13461 child_origin_die = child_die;
13462 child_origin_cu = cu;
13463 while (1)
13464 {
13465 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13466 child_origin_cu);
13467 if (attr == NULL)
13468 break;
13469 child_origin_die = follow_die_ref (child_origin_die, attr,
13470 &child_origin_cu);
13471 }
13472
13473 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13474 counterpart may exist. */
13475 if (child_origin_die != child_die)
13476 {
13477 if (child_die->tag != child_origin_die->tag
13478 && !(child_die->tag == DW_TAG_inlined_subroutine
13479 && child_origin_die->tag == DW_TAG_subprogram))
13480 complaint (_("Child DIE %s and its abstract origin %s have "
13481 "different tags"),
13482 sect_offset_str (child_die->sect_off),
13483 sect_offset_str (child_origin_die->sect_off));
13484 if (child_origin_die->parent != origin_die)
13485 complaint (_("Child DIE %s and its abstract origin %s have "
13486 "different parents"),
13487 sect_offset_str (child_die->sect_off),
13488 sect_offset_str (child_origin_die->sect_off));
13489 else
13490 offsets.push_back (child_origin_die->sect_off);
13491 }
13492 }
13493 std::sort (offsets.begin (), offsets.end ());
13494 sect_offset *offsets_end = offsets.data () + offsets.size ();
13495 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13496 if (offsetp[-1] == *offsetp)
13497 complaint (_("Multiple children of DIE %s refer "
13498 "to DIE %s as their abstract origin"),
13499 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13500
13501 offsetp = offsets.data ();
13502 origin_child_die = origin_die->child;
13503 while (origin_child_die && origin_child_die->tag)
13504 {
13505 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13506 while (offsetp < offsets_end
13507 && *offsetp < origin_child_die->sect_off)
13508 offsetp++;
13509 if (offsetp >= offsets_end
13510 || *offsetp > origin_child_die->sect_off)
13511 {
13512 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13513 Check whether we're already processing ORIGIN_CHILD_DIE.
13514 This can happen with mutually referenced abstract_origins.
13515 PR 16581. */
13516 if (!origin_child_die->in_process)
13517 process_die (origin_child_die, origin_cu);
13518 }
13519 origin_child_die = origin_child_die->sibling;
13520 }
13521 origin_cu->list_in_scope = origin_previous_list_in_scope;
13522
13523 if (cu != origin_cu)
13524 compute_delayed_physnames (origin_cu);
13525 }
13526
13527 static void
13528 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13529 {
13530 struct objfile *objfile = cu->per_objfile->objfile;
13531 struct gdbarch *gdbarch = objfile->arch ();
13532 struct context_stack *newobj;
13533 CORE_ADDR lowpc;
13534 CORE_ADDR highpc;
13535 struct die_info *child_die;
13536 struct attribute *attr, *call_line, *call_file;
13537 const char *name;
13538 CORE_ADDR baseaddr;
13539 struct block *block;
13540 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13541 std::vector<struct symbol *> template_args;
13542 struct template_symbol *templ_func = NULL;
13543
13544 if (inlined_func)
13545 {
13546 /* If we do not have call site information, we can't show the
13547 caller of this inlined function. That's too confusing, so
13548 only use the scope for local variables. */
13549 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13550 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13551 if (call_line == NULL || call_file == NULL)
13552 {
13553 read_lexical_block_scope (die, cu);
13554 return;
13555 }
13556 }
13557
13558 baseaddr = objfile->text_section_offset ();
13559
13560 name = dwarf2_name (die, cu);
13561
13562 /* Ignore functions with missing or empty names. These are actually
13563 illegal according to the DWARF standard. */
13564 if (name == NULL)
13565 {
13566 complaint (_("missing name for subprogram DIE at %s"),
13567 sect_offset_str (die->sect_off));
13568 return;
13569 }
13570
13571 /* Ignore functions with missing or invalid low and high pc attributes. */
13572 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13573 <= PC_BOUNDS_INVALID)
13574 {
13575 attr = dwarf2_attr (die, DW_AT_external, cu);
13576 if (attr == nullptr || !attr->as_boolean ())
13577 complaint (_("cannot get low and high bounds "
13578 "for subprogram DIE at %s"),
13579 sect_offset_str (die->sect_off));
13580 return;
13581 }
13582
13583 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13584 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13585
13586 /* If we have any template arguments, then we must allocate a
13587 different sort of symbol. */
13588 for (child_die = die->child; child_die; child_die = child_die->sibling)
13589 {
13590 if (child_die->tag == DW_TAG_template_type_param
13591 || child_die->tag == DW_TAG_template_value_param)
13592 {
13593 templ_func = new (&objfile->objfile_obstack) template_symbol;
13594 templ_func->subclass = SYMBOL_TEMPLATE;
13595 break;
13596 }
13597 }
13598
13599 newobj = cu->get_builder ()->push_context (0, lowpc);
13600 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13601 (struct symbol *) templ_func);
13602
13603 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13604 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13605 cu->language);
13606
13607 /* If there is a location expression for DW_AT_frame_base, record
13608 it. */
13609 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13610 if (attr != nullptr)
13611 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13612
13613 /* If there is a location for the static link, record it. */
13614 newobj->static_link = NULL;
13615 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13616 if (attr != nullptr)
13617 {
13618 newobj->static_link
13619 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13620 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13621 cu->addr_type ());
13622 }
13623
13624 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13625
13626 if (die->child != NULL)
13627 {
13628 child_die = die->child;
13629 while (child_die && child_die->tag)
13630 {
13631 if (child_die->tag == DW_TAG_template_type_param
13632 || child_die->tag == DW_TAG_template_value_param)
13633 {
13634 struct symbol *arg = new_symbol (child_die, NULL, cu);
13635
13636 if (arg != NULL)
13637 template_args.push_back (arg);
13638 }
13639 else
13640 process_die (child_die, cu);
13641 child_die = child_die->sibling;
13642 }
13643 }
13644
13645 inherit_abstract_dies (die, cu);
13646
13647 /* If we have a DW_AT_specification, we might need to import using
13648 directives from the context of the specification DIE. See the
13649 comment in determine_prefix. */
13650 if (cu->language == language_cplus
13651 && dwarf2_attr (die, DW_AT_specification, cu))
13652 {
13653 struct dwarf2_cu *spec_cu = cu;
13654 struct die_info *spec_die = die_specification (die, &spec_cu);
13655
13656 while (spec_die)
13657 {
13658 child_die = spec_die->child;
13659 while (child_die && child_die->tag)
13660 {
13661 if (child_die->tag == DW_TAG_imported_module)
13662 process_die (child_die, spec_cu);
13663 child_die = child_die->sibling;
13664 }
13665
13666 /* In some cases, GCC generates specification DIEs that
13667 themselves contain DW_AT_specification attributes. */
13668 spec_die = die_specification (spec_die, &spec_cu);
13669 }
13670 }
13671
13672 struct context_stack cstk = cu->get_builder ()->pop_context ();
13673 /* Make a block for the local symbols within. */
13674 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13675 cstk.static_link, lowpc, highpc);
13676
13677 /* For C++, set the block's scope. */
13678 if ((cu->language == language_cplus
13679 || cu->language == language_fortran
13680 || cu->language == language_d
13681 || cu->language == language_rust)
13682 && cu->processing_has_namespace_info)
13683 block_set_scope (block, determine_prefix (die, cu),
13684 &objfile->objfile_obstack);
13685
13686 /* If we have address ranges, record them. */
13687 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13688
13689 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13690
13691 /* Attach template arguments to function. */
13692 if (!template_args.empty ())
13693 {
13694 gdb_assert (templ_func != NULL);
13695
13696 templ_func->n_template_arguments = template_args.size ();
13697 templ_func->template_arguments
13698 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13699 templ_func->n_template_arguments);
13700 memcpy (templ_func->template_arguments,
13701 template_args.data (),
13702 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13703
13704 /* Make sure that the symtab is set on the new symbols. Even
13705 though they don't appear in this symtab directly, other parts
13706 of gdb assume that symbols do, and this is reasonably
13707 true. */
13708 for (symbol *sym : template_args)
13709 symbol_set_symtab (sym, symbol_symtab (templ_func));
13710 }
13711
13712 /* In C++, we can have functions nested inside functions (e.g., when
13713 a function declares a class that has methods). This means that
13714 when we finish processing a function scope, we may need to go
13715 back to building a containing block's symbol lists. */
13716 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13717 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13718
13719 /* If we've finished processing a top-level function, subsequent
13720 symbols go in the file symbol list. */
13721 if (cu->get_builder ()->outermost_context_p ())
13722 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13723 }
13724
13725 /* Process all the DIES contained within a lexical block scope. Start
13726 a new scope, process the dies, and then close the scope. */
13727
13728 static void
13729 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13730 {
13731 struct objfile *objfile = cu->per_objfile->objfile;
13732 struct gdbarch *gdbarch = objfile->arch ();
13733 CORE_ADDR lowpc, highpc;
13734 struct die_info *child_die;
13735 CORE_ADDR baseaddr;
13736
13737 baseaddr = objfile->text_section_offset ();
13738
13739 /* Ignore blocks with missing or invalid low and high pc attributes. */
13740 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13741 as multiple lexical blocks? Handling children in a sane way would
13742 be nasty. Might be easier to properly extend generic blocks to
13743 describe ranges. */
13744 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13745 {
13746 case PC_BOUNDS_NOT_PRESENT:
13747 /* DW_TAG_lexical_block has no attributes, process its children as if
13748 there was no wrapping by that DW_TAG_lexical_block.
13749 GCC does no longer produces such DWARF since GCC r224161. */
13750 for (child_die = die->child;
13751 child_die != NULL && child_die->tag;
13752 child_die = child_die->sibling)
13753 {
13754 /* We might already be processing this DIE. This can happen
13755 in an unusual circumstance -- where a subroutine A
13756 appears lexically in another subroutine B, but A actually
13757 inlines B. The recursion is broken here, rather than in
13758 inherit_abstract_dies, because it seems better to simply
13759 drop concrete children here. */
13760 if (!child_die->in_process)
13761 process_die (child_die, cu);
13762 }
13763 return;
13764 case PC_BOUNDS_INVALID:
13765 return;
13766 }
13767 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13768 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13769
13770 cu->get_builder ()->push_context (0, lowpc);
13771 if (die->child != NULL)
13772 {
13773 child_die = die->child;
13774 while (child_die && child_die->tag)
13775 {
13776 process_die (child_die, cu);
13777 child_die = child_die->sibling;
13778 }
13779 }
13780 inherit_abstract_dies (die, cu);
13781 struct context_stack cstk = cu->get_builder ()->pop_context ();
13782
13783 if (*cu->get_builder ()->get_local_symbols () != NULL
13784 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13785 {
13786 struct block *block
13787 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13788 cstk.start_addr, highpc);
13789
13790 /* Note that recording ranges after traversing children, as we
13791 do here, means that recording a parent's ranges entails
13792 walking across all its children's ranges as they appear in
13793 the address map, which is quadratic behavior.
13794
13795 It would be nicer to record the parent's ranges before
13796 traversing its children, simply overriding whatever you find
13797 there. But since we don't even decide whether to create a
13798 block until after we've traversed its children, that's hard
13799 to do. */
13800 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13801 }
13802 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13803 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13804 }
13805
13806 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13807
13808 static void
13809 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13810 {
13811 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13812 struct objfile *objfile = per_objfile->objfile;
13813 struct gdbarch *gdbarch = objfile->arch ();
13814 CORE_ADDR pc, baseaddr;
13815 struct attribute *attr;
13816 struct call_site *call_site, call_site_local;
13817 void **slot;
13818 int nparams;
13819 struct die_info *child_die;
13820
13821 baseaddr = objfile->text_section_offset ();
13822
13823 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13824 if (attr == NULL)
13825 {
13826 /* This was a pre-DWARF-5 GNU extension alias
13827 for DW_AT_call_return_pc. */
13828 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13829 }
13830 if (!attr)
13831 {
13832 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13833 "DIE %s [in module %s]"),
13834 sect_offset_str (die->sect_off), objfile_name (objfile));
13835 return;
13836 }
13837 pc = attr->as_address () + baseaddr;
13838 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13839
13840 if (cu->call_site_htab == NULL)
13841 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13842 NULL, &objfile->objfile_obstack,
13843 hashtab_obstack_allocate, NULL);
13844 call_site_local.pc = pc;
13845 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13846 if (*slot != NULL)
13847 {
13848 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13849 "DIE %s [in module %s]"),
13850 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13851 objfile_name (objfile));
13852 return;
13853 }
13854
13855 /* Count parameters at the caller. */
13856
13857 nparams = 0;
13858 for (child_die = die->child; child_die && child_die->tag;
13859 child_die = child_die->sibling)
13860 {
13861 if (child_die->tag != DW_TAG_call_site_parameter
13862 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13863 {
13864 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13865 "DW_TAG_call_site child DIE %s [in module %s]"),
13866 child_die->tag, sect_offset_str (child_die->sect_off),
13867 objfile_name (objfile));
13868 continue;
13869 }
13870
13871 nparams++;
13872 }
13873
13874 call_site
13875 = ((struct call_site *)
13876 obstack_alloc (&objfile->objfile_obstack,
13877 sizeof (*call_site)
13878 + (sizeof (*call_site->parameter) * (nparams - 1))));
13879 *slot = call_site;
13880 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13881 call_site->pc = pc;
13882
13883 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13884 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13885 {
13886 struct die_info *func_die;
13887
13888 /* Skip also over DW_TAG_inlined_subroutine. */
13889 for (func_die = die->parent;
13890 func_die && func_die->tag != DW_TAG_subprogram
13891 && func_die->tag != DW_TAG_subroutine_type;
13892 func_die = func_die->parent);
13893
13894 /* DW_AT_call_all_calls is a superset
13895 of DW_AT_call_all_tail_calls. */
13896 if (func_die
13897 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13898 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13899 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13900 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13901 {
13902 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13903 not complete. But keep CALL_SITE for look ups via call_site_htab,
13904 both the initial caller containing the real return address PC and
13905 the final callee containing the current PC of a chain of tail
13906 calls do not need to have the tail call list complete. But any
13907 function candidate for a virtual tail call frame searched via
13908 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13909 determined unambiguously. */
13910 }
13911 else
13912 {
13913 struct type *func_type = NULL;
13914
13915 if (func_die)
13916 func_type = get_die_type (func_die, cu);
13917 if (func_type != NULL)
13918 {
13919 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13920
13921 /* Enlist this call site to the function. */
13922 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13923 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13924 }
13925 else
13926 complaint (_("Cannot find function owning DW_TAG_call_site "
13927 "DIE %s [in module %s]"),
13928 sect_offset_str (die->sect_off), objfile_name (objfile));
13929 }
13930 }
13931
13932 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13933 if (attr == NULL)
13934 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13935 if (attr == NULL)
13936 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13937 if (attr == NULL)
13938 {
13939 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13940 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13941 }
13942 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13943 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
13944 /* Keep NULL DWARF_BLOCK. */;
13945 else if (attr->form_is_block ())
13946 {
13947 struct dwarf2_locexpr_baton *dlbaton;
13948 struct dwarf_block *block = attr->as_block ();
13949
13950 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13951 dlbaton->data = block->data;
13952 dlbaton->size = block->size;
13953 dlbaton->per_objfile = per_objfile;
13954 dlbaton->per_cu = cu->per_cu;
13955
13956 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13957 }
13958 else if (attr->form_is_ref ())
13959 {
13960 struct dwarf2_cu *target_cu = cu;
13961 struct die_info *target_die;
13962
13963 target_die = follow_die_ref (die, attr, &target_cu);
13964 gdb_assert (target_cu->per_objfile->objfile == objfile);
13965 if (die_is_declaration (target_die, target_cu))
13966 {
13967 const char *target_physname;
13968
13969 /* Prefer the mangled name; otherwise compute the demangled one. */
13970 target_physname = dw2_linkage_name (target_die, target_cu);
13971 if (target_physname == NULL)
13972 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13973 if (target_physname == NULL)
13974 complaint (_("DW_AT_call_target target DIE has invalid "
13975 "physname, for referencing DIE %s [in module %s]"),
13976 sect_offset_str (die->sect_off), objfile_name (objfile));
13977 else
13978 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13979 }
13980 else
13981 {
13982 CORE_ADDR lowpc;
13983
13984 /* DW_AT_entry_pc should be preferred. */
13985 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13986 <= PC_BOUNDS_INVALID)
13987 complaint (_("DW_AT_call_target target DIE has invalid "
13988 "low pc, for referencing DIE %s [in module %s]"),
13989 sect_offset_str (die->sect_off), objfile_name (objfile));
13990 else
13991 {
13992 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13993 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13994 }
13995 }
13996 }
13997 else
13998 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13999 "block nor reference, for DIE %s [in module %s]"),
14000 sect_offset_str (die->sect_off), objfile_name (objfile));
14001
14002 call_site->per_cu = cu->per_cu;
14003 call_site->per_objfile = per_objfile;
14004
14005 for (child_die = die->child;
14006 child_die && child_die->tag;
14007 child_die = child_die->sibling)
14008 {
14009 struct call_site_parameter *parameter;
14010 struct attribute *loc, *origin;
14011
14012 if (child_die->tag != DW_TAG_call_site_parameter
14013 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14014 {
14015 /* Already printed the complaint above. */
14016 continue;
14017 }
14018
14019 gdb_assert (call_site->parameter_count < nparams);
14020 parameter = &call_site->parameter[call_site->parameter_count];
14021
14022 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14023 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14024 register is contained in DW_AT_call_value. */
14025
14026 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14027 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14028 if (origin == NULL)
14029 {
14030 /* This was a pre-DWARF-5 GNU extension alias
14031 for DW_AT_call_parameter. */
14032 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14033 }
14034 if (loc == NULL && origin != NULL && origin->form_is_ref ())
14035 {
14036 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14037
14038 sect_offset sect_off = origin->get_ref_die_offset ();
14039 if (!cu->header.offset_in_cu_p (sect_off))
14040 {
14041 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14042 binding can be done only inside one CU. Such referenced DIE
14043 therefore cannot be even moved to DW_TAG_partial_unit. */
14044 complaint (_("DW_AT_call_parameter offset is not in CU for "
14045 "DW_TAG_call_site child DIE %s [in module %s]"),
14046 sect_offset_str (child_die->sect_off),
14047 objfile_name (objfile));
14048 continue;
14049 }
14050 parameter->u.param_cu_off
14051 = (cu_offset) (sect_off - cu->header.sect_off);
14052 }
14053 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
14054 {
14055 complaint (_("No DW_FORM_block* DW_AT_location for "
14056 "DW_TAG_call_site child DIE %s [in module %s]"),
14057 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14058 continue;
14059 }
14060 else
14061 {
14062 struct dwarf_block *block = loc->as_block ();
14063
14064 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14065 (block->data, &block->data[block->size]);
14066 if (parameter->u.dwarf_reg != -1)
14067 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14068 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
14069 &block->data[block->size],
14070 &parameter->u.fb_offset))
14071 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14072 else
14073 {
14074 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14075 "for DW_FORM_block* DW_AT_location is supported for "
14076 "DW_TAG_call_site child DIE %s "
14077 "[in module %s]"),
14078 sect_offset_str (child_die->sect_off),
14079 objfile_name (objfile));
14080 continue;
14081 }
14082 }
14083
14084 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14085 if (attr == NULL)
14086 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14087 if (attr == NULL || !attr->form_is_block ())
14088 {
14089 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14090 "DW_TAG_call_site child DIE %s [in module %s]"),
14091 sect_offset_str (child_die->sect_off),
14092 objfile_name (objfile));
14093 continue;
14094 }
14095
14096 struct dwarf_block *block = attr->as_block ();
14097 parameter->value = block->data;
14098 parameter->value_size = block->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 block = attr->as_block ();
14118 parameter->data_value = block->data;
14119 parameter->data_value_size = block->size;
14120 }
14121 }
14122 }
14123 }
14124
14125 /* Helper function for read_variable. If DIE represents a virtual
14126 table, then return the type of the concrete object that is
14127 associated with the virtual table. Otherwise, return NULL. */
14128
14129 static struct type *
14130 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14131 {
14132 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14133 if (attr == NULL)
14134 return NULL;
14135
14136 /* Find the type DIE. */
14137 struct die_info *type_die = NULL;
14138 struct dwarf2_cu *type_cu = cu;
14139
14140 if (attr->form_is_ref ())
14141 type_die = follow_die_ref (die, attr, &type_cu);
14142 if (type_die == NULL)
14143 return NULL;
14144
14145 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14146 return NULL;
14147 return die_containing_type (type_die, type_cu);
14148 }
14149
14150 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14151
14152 static void
14153 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14154 {
14155 struct rust_vtable_symbol *storage = NULL;
14156
14157 if (cu->language == language_rust)
14158 {
14159 struct type *containing_type = rust_containing_type (die, cu);
14160
14161 if (containing_type != NULL)
14162 {
14163 struct objfile *objfile = cu->per_objfile->objfile;
14164
14165 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
14166 storage->concrete_type = containing_type;
14167 storage->subclass = SYMBOL_RUST_VTABLE;
14168 }
14169 }
14170
14171 struct symbol *res = new_symbol (die, NULL, cu, storage);
14172 struct attribute *abstract_origin
14173 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14174 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14175 if (res == NULL && loc && abstract_origin)
14176 {
14177 /* We have a variable without a name, but with a location and an abstract
14178 origin. This may be a concrete instance of an abstract variable
14179 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14180 later. */
14181 struct dwarf2_cu *origin_cu = cu;
14182 struct die_info *origin_die
14183 = follow_die_ref (die, abstract_origin, &origin_cu);
14184 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14185 per_objfile->per_bfd->abstract_to_concrete
14186 [origin_die->sect_off].push_back (die->sect_off);
14187 }
14188 }
14189
14190 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14191 reading .debug_rnglists.
14192 Callback's type should be:
14193 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14194 Return true if the attributes are present and valid, otherwise,
14195 return false. */
14196
14197 template <typename Callback>
14198 static bool
14199 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14200 dwarf_tag tag, Callback &&callback)
14201 {
14202 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14203 struct objfile *objfile = per_objfile->objfile;
14204 bfd *obfd = objfile->obfd;
14205 /* Base address selection entry. */
14206 gdb::optional<CORE_ADDR> base;
14207 const gdb_byte *buffer;
14208 CORE_ADDR baseaddr;
14209 bool overflow = false;
14210 ULONGEST addr_index;
14211 struct dwarf2_section_info *rnglists_section;
14212
14213 base = cu->base_address;
14214 rnglists_section = cu_debug_rnglists_section (cu, tag);
14215 rnglists_section->read (objfile);
14216
14217 if (offset >= rnglists_section->size)
14218 {
14219 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14220 offset);
14221 return false;
14222 }
14223 buffer = rnglists_section->buffer + offset;
14224
14225 baseaddr = objfile->text_section_offset ();
14226
14227 while (1)
14228 {
14229 /* Initialize it due to a false compiler warning. */
14230 CORE_ADDR range_beginning = 0, range_end = 0;
14231 const gdb_byte *buf_end = (rnglists_section->buffer
14232 + rnglists_section->size);
14233 unsigned int bytes_read;
14234
14235 if (buffer == buf_end)
14236 {
14237 overflow = true;
14238 break;
14239 }
14240 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14241 switch (rlet)
14242 {
14243 case DW_RLE_end_of_list:
14244 break;
14245 case DW_RLE_base_address:
14246 if (buffer + cu->header.addr_size > buf_end)
14247 {
14248 overflow = true;
14249 break;
14250 }
14251 base = cu->header.read_address (obfd, buffer, &bytes_read);
14252 buffer += bytes_read;
14253 break;
14254 case DW_RLE_base_addressx:
14255 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14256 buffer += bytes_read;
14257 base = read_addr_index (cu, addr_index);
14258 break;
14259 case DW_RLE_start_length:
14260 if (buffer + cu->header.addr_size > buf_end)
14261 {
14262 overflow = true;
14263 break;
14264 }
14265 range_beginning = cu->header.read_address (obfd, buffer,
14266 &bytes_read);
14267 buffer += bytes_read;
14268 range_end = (range_beginning
14269 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14270 buffer += bytes_read;
14271 if (buffer > buf_end)
14272 {
14273 overflow = true;
14274 break;
14275 }
14276 break;
14277 case DW_RLE_startx_length:
14278 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14279 buffer += bytes_read;
14280 range_beginning = read_addr_index (cu, addr_index);
14281 if (buffer > buf_end)
14282 {
14283 overflow = true;
14284 break;
14285 }
14286 range_end = (range_beginning
14287 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14288 buffer += bytes_read;
14289 break;
14290 case DW_RLE_offset_pair:
14291 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14292 buffer += bytes_read;
14293 if (buffer > buf_end)
14294 {
14295 overflow = true;
14296 break;
14297 }
14298 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14299 buffer += bytes_read;
14300 if (buffer > buf_end)
14301 {
14302 overflow = true;
14303 break;
14304 }
14305 break;
14306 case DW_RLE_start_end:
14307 if (buffer + 2 * cu->header.addr_size > buf_end)
14308 {
14309 overflow = true;
14310 break;
14311 }
14312 range_beginning = cu->header.read_address (obfd, buffer,
14313 &bytes_read);
14314 buffer += bytes_read;
14315 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
14316 buffer += bytes_read;
14317 break;
14318 case DW_RLE_startx_endx:
14319 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14320 buffer += bytes_read;
14321 range_beginning = read_addr_index (cu, addr_index);
14322 if (buffer > buf_end)
14323 {
14324 overflow = true;
14325 break;
14326 }
14327 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14328 buffer += bytes_read;
14329 range_end = read_addr_index (cu, addr_index);
14330 break;
14331 default:
14332 complaint (_("Invalid .debug_rnglists data (no base address)"));
14333 return false;
14334 }
14335 if (rlet == DW_RLE_end_of_list || overflow)
14336 break;
14337 if (rlet == DW_RLE_base_address)
14338 continue;
14339
14340 if (range_beginning > range_end)
14341 {
14342 /* Inverted range entries are invalid. */
14343 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14344 return false;
14345 }
14346
14347 /* Empty range entries have no effect. */
14348 if (range_beginning == range_end)
14349 continue;
14350
14351 /* Only DW_RLE_offset_pair needs the base address added. */
14352 if (rlet == DW_RLE_offset_pair)
14353 {
14354 if (!base.has_value ())
14355 {
14356 /* We have no valid base address for the DW_RLE_offset_pair. */
14357 complaint (_("Invalid .debug_rnglists data (no base address for "
14358 "DW_RLE_offset_pair)"));
14359 return false;
14360 }
14361
14362 range_beginning += *base;
14363 range_end += *base;
14364 }
14365
14366 /* A not-uncommon case of bad debug info.
14367 Don't pollute the addrmap with bad data. */
14368 if (range_beginning + baseaddr == 0
14369 && !per_objfile->per_bfd->has_section_at_zero)
14370 {
14371 complaint (_(".debug_rnglists entry has start address of zero"
14372 " [in module %s]"), objfile_name (objfile));
14373 continue;
14374 }
14375
14376 callback (range_beginning, range_end);
14377 }
14378
14379 if (overflow)
14380 {
14381 complaint (_("Offset %d is not terminated "
14382 "for DW_AT_ranges attribute"),
14383 offset);
14384 return false;
14385 }
14386
14387 return true;
14388 }
14389
14390 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14391 Callback's type should be:
14392 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14393 Return 1 if the attributes are present and valid, otherwise, return 0. */
14394
14395 template <typename Callback>
14396 static int
14397 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
14398 Callback &&callback)
14399 {
14400 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14401 struct objfile *objfile = per_objfile->objfile;
14402 struct comp_unit_head *cu_header = &cu->header;
14403 bfd *obfd = objfile->obfd;
14404 unsigned int addr_size = cu_header->addr_size;
14405 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14406 /* Base address selection entry. */
14407 gdb::optional<CORE_ADDR> base;
14408 unsigned int dummy;
14409 const gdb_byte *buffer;
14410 CORE_ADDR baseaddr;
14411
14412 if (cu_header->version >= 5)
14413 return dwarf2_rnglists_process (offset, cu, tag, callback);
14414
14415 base = cu->base_address;
14416
14417 per_objfile->per_bfd->ranges.read (objfile);
14418 if (offset >= per_objfile->per_bfd->ranges.size)
14419 {
14420 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14421 offset);
14422 return 0;
14423 }
14424 buffer = per_objfile->per_bfd->ranges.buffer + offset;
14425
14426 baseaddr = objfile->text_section_offset ();
14427
14428 while (1)
14429 {
14430 CORE_ADDR range_beginning, range_end;
14431
14432 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
14433 buffer += addr_size;
14434 range_end = cu->header.read_address (obfd, buffer, &dummy);
14435 buffer += addr_size;
14436 offset += 2 * addr_size;
14437
14438 /* An end of list marker is a pair of zero addresses. */
14439 if (range_beginning == 0 && range_end == 0)
14440 /* Found the end of list entry. */
14441 break;
14442
14443 /* Each base address selection entry is a pair of 2 values.
14444 The first is the largest possible address, the second is
14445 the base address. Check for a base address here. */
14446 if ((range_beginning & mask) == mask)
14447 {
14448 /* If we found the largest possible address, then we already
14449 have the base address in range_end. */
14450 base = range_end;
14451 continue;
14452 }
14453
14454 if (!base.has_value ())
14455 {
14456 /* We have no valid base address for the ranges
14457 data. */
14458 complaint (_("Invalid .debug_ranges data (no base address)"));
14459 return 0;
14460 }
14461
14462 if (range_beginning > range_end)
14463 {
14464 /* Inverted range entries are invalid. */
14465 complaint (_("Invalid .debug_ranges data (inverted range)"));
14466 return 0;
14467 }
14468
14469 /* Empty range entries have no effect. */
14470 if (range_beginning == range_end)
14471 continue;
14472
14473 range_beginning += *base;
14474 range_end += *base;
14475
14476 /* A not-uncommon case of bad debug info.
14477 Don't pollute the addrmap with bad data. */
14478 if (range_beginning + baseaddr == 0
14479 && !per_objfile->per_bfd->has_section_at_zero)
14480 {
14481 complaint (_(".debug_ranges entry has start address of zero"
14482 " [in module %s]"), objfile_name (objfile));
14483 continue;
14484 }
14485
14486 callback (range_beginning, range_end);
14487 }
14488
14489 return 1;
14490 }
14491
14492 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14493 Return 1 if the attributes are present and valid, otherwise, return 0.
14494 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14495
14496 static int
14497 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14498 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14499 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
14500 {
14501 struct objfile *objfile = cu->per_objfile->objfile;
14502 struct gdbarch *gdbarch = objfile->arch ();
14503 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14504 int low_set = 0;
14505 CORE_ADDR low = 0;
14506 CORE_ADDR high = 0;
14507 int retval;
14508
14509 retval = dwarf2_ranges_process (offset, cu, tag,
14510 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14511 {
14512 if (ranges_pst != NULL)
14513 {
14514 CORE_ADDR lowpc;
14515 CORE_ADDR highpc;
14516
14517 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14518 range_beginning + baseaddr)
14519 - baseaddr);
14520 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14521 range_end + baseaddr)
14522 - baseaddr);
14523 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14524 lowpc, highpc - 1, ranges_pst);
14525 }
14526
14527 /* FIXME: This is recording everything as a low-high
14528 segment of consecutive addresses. We should have a
14529 data structure for discontiguous block ranges
14530 instead. */
14531 if (! low_set)
14532 {
14533 low = range_beginning;
14534 high = range_end;
14535 low_set = 1;
14536 }
14537 else
14538 {
14539 if (range_beginning < low)
14540 low = range_beginning;
14541 if (range_end > high)
14542 high = range_end;
14543 }
14544 });
14545 if (!retval)
14546 return 0;
14547
14548 if (! low_set)
14549 /* If the first entry is an end-of-list marker, the range
14550 describes an empty scope, i.e. no instructions. */
14551 return 0;
14552
14553 if (low_return)
14554 *low_return = low;
14555 if (high_return)
14556 *high_return = high;
14557 return 1;
14558 }
14559
14560 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14561 definition for the return value. *LOWPC and *HIGHPC are set iff
14562 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14563
14564 static enum pc_bounds_kind
14565 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14566 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14567 dwarf2_psymtab *pst)
14568 {
14569 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14570 struct attribute *attr;
14571 struct attribute *attr_high;
14572 CORE_ADDR low = 0;
14573 CORE_ADDR high = 0;
14574 enum pc_bounds_kind ret;
14575
14576 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14577 if (attr_high)
14578 {
14579 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14580 if (attr != nullptr)
14581 {
14582 low = attr->as_address ();
14583 high = attr_high->as_address ();
14584 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14585 high += low;
14586 }
14587 else
14588 /* Found high w/o low attribute. */
14589 return PC_BOUNDS_INVALID;
14590
14591 /* Found consecutive range of addresses. */
14592 ret = PC_BOUNDS_HIGH_LOW;
14593 }
14594 else
14595 {
14596 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14597 if (attr != nullptr && attr->form_is_unsigned ())
14598 {
14599 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14600 We take advantage of the fact that DW_AT_ranges does not appear
14601 in DW_TAG_compile_unit of DWO files.
14602
14603 Attributes of the form DW_FORM_rnglistx have already had their
14604 value changed by read_rnglist_index and already include
14605 DW_AT_rnglists_base, so don't need to add the ranges base,
14606 either. */
14607 int need_ranges_base = (die->tag != DW_TAG_compile_unit
14608 && attr->form != DW_FORM_rnglistx);
14609 unsigned int ranges_offset = (attr->as_unsigned ()
14610 + (need_ranges_base
14611 ? cu->ranges_base
14612 : 0));
14613
14614 /* Value of the DW_AT_ranges attribute is the offset in the
14615 .debug_ranges section. */
14616 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14617 die->tag))
14618 return PC_BOUNDS_INVALID;
14619 /* Found discontinuous range of addresses. */
14620 ret = PC_BOUNDS_RANGES;
14621 }
14622 else
14623 return PC_BOUNDS_NOT_PRESENT;
14624 }
14625
14626 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14627 if (high <= low)
14628 return PC_BOUNDS_INVALID;
14629
14630 /* When using the GNU linker, .gnu.linkonce. sections are used to
14631 eliminate duplicate copies of functions and vtables and such.
14632 The linker will arbitrarily choose one and discard the others.
14633 The AT_*_pc values for such functions refer to local labels in
14634 these sections. If the section from that file was discarded, the
14635 labels are not in the output, so the relocs get a value of 0.
14636 If this is a discarded function, mark the pc bounds as invalid,
14637 so that GDB will ignore it. */
14638 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14639 return PC_BOUNDS_INVALID;
14640
14641 *lowpc = low;
14642 if (highpc)
14643 *highpc = high;
14644 return ret;
14645 }
14646
14647 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14648 its low and high PC addresses. Do nothing if these addresses could not
14649 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14650 and HIGHPC to the high address if greater than HIGHPC. */
14651
14652 static void
14653 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14654 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14655 struct dwarf2_cu *cu)
14656 {
14657 CORE_ADDR low, high;
14658 struct die_info *child = die->child;
14659
14660 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14661 {
14662 *lowpc = std::min (*lowpc, low);
14663 *highpc = std::max (*highpc, high);
14664 }
14665
14666 /* If the language does not allow nested subprograms (either inside
14667 subprograms or lexical blocks), we're done. */
14668 if (cu->language != language_ada)
14669 return;
14670
14671 /* Check all the children of the given DIE. If it contains nested
14672 subprograms, then check their pc bounds. Likewise, we need to
14673 check lexical blocks as well, as they may also contain subprogram
14674 definitions. */
14675 while (child && child->tag)
14676 {
14677 if (child->tag == DW_TAG_subprogram
14678 || child->tag == DW_TAG_lexical_block)
14679 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14680 child = child->sibling;
14681 }
14682 }
14683
14684 /* Get the low and high pc's represented by the scope DIE, and store
14685 them in *LOWPC and *HIGHPC. If the correct values can't be
14686 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14687
14688 static void
14689 get_scope_pc_bounds (struct die_info *die,
14690 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14691 struct dwarf2_cu *cu)
14692 {
14693 CORE_ADDR best_low = (CORE_ADDR) -1;
14694 CORE_ADDR best_high = (CORE_ADDR) 0;
14695 CORE_ADDR current_low, current_high;
14696
14697 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14698 >= PC_BOUNDS_RANGES)
14699 {
14700 best_low = current_low;
14701 best_high = current_high;
14702 }
14703 else
14704 {
14705 struct die_info *child = die->child;
14706
14707 while (child && child->tag)
14708 {
14709 switch (child->tag) {
14710 case DW_TAG_subprogram:
14711 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14712 break;
14713 case DW_TAG_namespace:
14714 case DW_TAG_module:
14715 /* FIXME: carlton/2004-01-16: Should we do this for
14716 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14717 that current GCC's always emit the DIEs corresponding
14718 to definitions of methods of classes as children of a
14719 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14720 the DIEs giving the declarations, which could be
14721 anywhere). But I don't see any reason why the
14722 standards says that they have to be there. */
14723 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14724
14725 if (current_low != ((CORE_ADDR) -1))
14726 {
14727 best_low = std::min (best_low, current_low);
14728 best_high = std::max (best_high, current_high);
14729 }
14730 break;
14731 default:
14732 /* Ignore. */
14733 break;
14734 }
14735
14736 child = child->sibling;
14737 }
14738 }
14739
14740 *lowpc = best_low;
14741 *highpc = best_high;
14742 }
14743
14744 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14745 in DIE. */
14746
14747 static void
14748 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14749 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14750 {
14751 struct objfile *objfile = cu->per_objfile->objfile;
14752 struct gdbarch *gdbarch = objfile->arch ();
14753 struct attribute *attr;
14754 struct attribute *attr_high;
14755
14756 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14757 if (attr_high)
14758 {
14759 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14760 if (attr != nullptr)
14761 {
14762 CORE_ADDR low = attr->as_address ();
14763 CORE_ADDR high = attr_high->as_address ();
14764
14765 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14766 high += low;
14767
14768 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14769 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14770 cu->get_builder ()->record_block_range (block, low, high - 1);
14771 }
14772 }
14773
14774 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14775 if (attr != nullptr && attr->form_is_unsigned ())
14776 {
14777 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14778 We take advantage of the fact that DW_AT_ranges does not appear
14779 in DW_TAG_compile_unit of DWO files.
14780
14781 Attributes of the form DW_FORM_rnglistx have already had their
14782 value changed by read_rnglist_index and already include
14783 DW_AT_rnglists_base, so don't need to add the ranges base,
14784 either. */
14785 int need_ranges_base = (die->tag != DW_TAG_compile_unit
14786 && attr->form != DW_FORM_rnglistx);
14787
14788 /* The value of the DW_AT_ranges attribute is the offset of the
14789 address range list in the .debug_ranges section. */
14790 unsigned long offset = (attr->as_unsigned ()
14791 + (need_ranges_base ? cu->ranges_base : 0));
14792
14793 std::vector<blockrange> blockvec;
14794 dwarf2_ranges_process (offset, cu, die->tag,
14795 [&] (CORE_ADDR start, CORE_ADDR end)
14796 {
14797 start += baseaddr;
14798 end += baseaddr;
14799 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14800 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14801 cu->get_builder ()->record_block_range (block, start, end - 1);
14802 blockvec.emplace_back (start, end);
14803 });
14804
14805 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14806 }
14807 }
14808
14809 /* Check whether the producer field indicates either of GCC < 4.6, or the
14810 Intel C/C++ compiler, and cache the result in CU. */
14811
14812 static void
14813 check_producer (struct dwarf2_cu *cu)
14814 {
14815 int major, minor;
14816
14817 if (cu->producer == NULL)
14818 {
14819 /* For unknown compilers expect their behavior is DWARF version
14820 compliant.
14821
14822 GCC started to support .debug_types sections by -gdwarf-4 since
14823 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14824 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14825 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14826 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14827 }
14828 else if (producer_is_gcc (cu->producer, &major, &minor))
14829 {
14830 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14831 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14832 }
14833 else if (producer_is_icc (cu->producer, &major, &minor))
14834 {
14835 cu->producer_is_icc = true;
14836 cu->producer_is_icc_lt_14 = major < 14;
14837 }
14838 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14839 cu->producer_is_codewarrior = true;
14840 else
14841 {
14842 /* For other non-GCC compilers, expect their behavior is DWARF version
14843 compliant. */
14844 }
14845
14846 cu->checked_producer = true;
14847 }
14848
14849 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14850 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14851 during 4.6.0 experimental. */
14852
14853 static bool
14854 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14855 {
14856 if (!cu->checked_producer)
14857 check_producer (cu);
14858
14859 return cu->producer_is_gxx_lt_4_6;
14860 }
14861
14862
14863 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14864 with incorrect is_stmt attributes. */
14865
14866 static bool
14867 producer_is_codewarrior (struct dwarf2_cu *cu)
14868 {
14869 if (!cu->checked_producer)
14870 check_producer (cu);
14871
14872 return cu->producer_is_codewarrior;
14873 }
14874
14875 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
14876 If that attribute is not available, return the appropriate
14877 default. */
14878
14879 static enum dwarf_access_attribute
14880 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14881 {
14882 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14883 if (attr != nullptr)
14884 {
14885 LONGEST value = attr->constant_value (-1);
14886 if (value == DW_ACCESS_public
14887 || value == DW_ACCESS_protected
14888 || value == DW_ACCESS_private)
14889 return (dwarf_access_attribute) value;
14890 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14891 plongest (value));
14892 }
14893
14894 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14895 {
14896 /* The default DWARF 2 accessibility for members is public, the default
14897 accessibility for inheritance is private. */
14898
14899 if (die->tag != DW_TAG_inheritance)
14900 return DW_ACCESS_public;
14901 else
14902 return DW_ACCESS_private;
14903 }
14904 else
14905 {
14906 /* DWARF 3+ defines the default accessibility a different way. The same
14907 rules apply now for DW_TAG_inheritance as for the members and it only
14908 depends on the container kind. */
14909
14910 if (die->parent->tag == DW_TAG_class_type)
14911 return DW_ACCESS_private;
14912 else
14913 return DW_ACCESS_public;
14914 }
14915 }
14916
14917 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14918 offset. If the attribute was not found return 0, otherwise return
14919 1. If it was found but could not properly be handled, set *OFFSET
14920 to 0. */
14921
14922 static int
14923 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14924 LONGEST *offset)
14925 {
14926 struct attribute *attr;
14927
14928 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14929 if (attr != NULL)
14930 {
14931 *offset = 0;
14932
14933 /* Note that we do not check for a section offset first here.
14934 This is because DW_AT_data_member_location is new in DWARF 4,
14935 so if we see it, we can assume that a constant form is really
14936 a constant and not a section offset. */
14937 if (attr->form_is_constant ())
14938 *offset = attr->constant_value (0);
14939 else if (attr->form_is_section_offset ())
14940 dwarf2_complex_location_expr_complaint ();
14941 else if (attr->form_is_block ())
14942 *offset = decode_locdesc (attr->as_block (), cu);
14943 else
14944 dwarf2_complex_location_expr_complaint ();
14945
14946 return 1;
14947 }
14948
14949 return 0;
14950 }
14951
14952 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14953
14954 static void
14955 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14956 struct field *field)
14957 {
14958 struct attribute *attr;
14959
14960 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14961 if (attr != NULL)
14962 {
14963 if (attr->form_is_constant ())
14964 {
14965 LONGEST offset = attr->constant_value (0);
14966 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14967 }
14968 else if (attr->form_is_section_offset ())
14969 dwarf2_complex_location_expr_complaint ();
14970 else if (attr->form_is_block ())
14971 {
14972 bool handled;
14973 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
14974 if (handled)
14975 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14976 else
14977 {
14978 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14979 struct objfile *objfile = per_objfile->objfile;
14980 struct dwarf2_locexpr_baton *dlbaton
14981 = XOBNEW (&objfile->objfile_obstack,
14982 struct dwarf2_locexpr_baton);
14983 dlbaton->data = attr->as_block ()->data;
14984 dlbaton->size = attr->as_block ()->size;
14985 /* When using this baton, we want to compute the address
14986 of the field, not the value. This is why
14987 is_reference is set to false here. */
14988 dlbaton->is_reference = false;
14989 dlbaton->per_objfile = per_objfile;
14990 dlbaton->per_cu = cu->per_cu;
14991
14992 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14993 }
14994 }
14995 else
14996 dwarf2_complex_location_expr_complaint ();
14997 }
14998 }
14999
15000 /* Add an aggregate field to the field list. */
15001
15002 static void
15003 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15004 struct dwarf2_cu *cu)
15005 {
15006 struct objfile *objfile = cu->per_objfile->objfile;
15007 struct gdbarch *gdbarch = objfile->arch ();
15008 struct nextfield *new_field;
15009 struct attribute *attr;
15010 struct field *fp;
15011 const char *fieldname = "";
15012
15013 if (die->tag == DW_TAG_inheritance)
15014 {
15015 fip->baseclasses.emplace_back ();
15016 new_field = &fip->baseclasses.back ();
15017 }
15018 else
15019 {
15020 fip->fields.emplace_back ();
15021 new_field = &fip->fields.back ();
15022 }
15023
15024 new_field->offset = die->sect_off;
15025
15026 new_field->accessibility = dwarf2_access_attribute (die, cu);
15027 if (new_field->accessibility != DW_ACCESS_public)
15028 fip->non_public_fields = true;
15029
15030 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15031 if (attr != nullptr)
15032 new_field->virtuality = attr->as_virtuality ();
15033 else
15034 new_field->virtuality = DW_VIRTUALITY_none;
15035
15036 fp = &new_field->field;
15037
15038 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15039 {
15040 /* Data member other than a C++ static data member. */
15041
15042 /* Get type of field. */
15043 fp->set_type (die_type (die, cu));
15044
15045 SET_FIELD_BITPOS (*fp, 0);
15046
15047 /* Get bit size of field (zero if none). */
15048 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15049 if (attr != nullptr)
15050 {
15051 FIELD_BITSIZE (*fp) = attr->constant_value (0);
15052 }
15053 else
15054 {
15055 FIELD_BITSIZE (*fp) = 0;
15056 }
15057
15058 /* Get bit offset of field. */
15059 handle_data_member_location (die, cu, fp);
15060 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15061 if (attr != nullptr && attr->form_is_constant ())
15062 {
15063 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
15064 {
15065 /* For big endian bits, the DW_AT_bit_offset gives the
15066 additional bit offset from the MSB of the containing
15067 anonymous object to the MSB of the field. We don't
15068 have to do anything special since we don't need to
15069 know the size of the anonymous object. */
15070 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15071 + attr->constant_value (0)));
15072 }
15073 else
15074 {
15075 /* For little endian bits, compute the bit offset to the
15076 MSB of the anonymous object, subtract off the number of
15077 bits from the MSB of the field to the MSB of the
15078 object, and then subtract off the number of bits of
15079 the field itself. The result is the bit offset of
15080 the LSB of the field. */
15081 int anonymous_size;
15082 int bit_offset = attr->constant_value (0);
15083
15084 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15085 if (attr != nullptr && attr->form_is_constant ())
15086 {
15087 /* The size of the anonymous object containing
15088 the bit field is explicit, so use the
15089 indicated size (in bytes). */
15090 anonymous_size = attr->constant_value (0);
15091 }
15092 else
15093 {
15094 /* The size of the anonymous object containing
15095 the bit field must be inferred from the type
15096 attribute of the data member containing the
15097 bit field. */
15098 anonymous_size = TYPE_LENGTH (fp->type ());
15099 }
15100 SET_FIELD_BITPOS (*fp,
15101 (FIELD_BITPOS (*fp)
15102 + anonymous_size * bits_per_byte
15103 - bit_offset - FIELD_BITSIZE (*fp)));
15104 }
15105 }
15106 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15107 if (attr != NULL)
15108 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15109 + attr->constant_value (0)));
15110
15111 /* Get name of field. */
15112 fieldname = dwarf2_name (die, cu);
15113 if (fieldname == NULL)
15114 fieldname = "";
15115
15116 /* The name is already allocated along with this objfile, so we don't
15117 need to duplicate it for the type. */
15118 fp->name = fieldname;
15119
15120 /* Change accessibility for artificial fields (e.g. virtual table
15121 pointer or virtual base class pointer) to private. */
15122 if (dwarf2_attr (die, DW_AT_artificial, cu))
15123 {
15124 FIELD_ARTIFICIAL (*fp) = 1;
15125 new_field->accessibility = DW_ACCESS_private;
15126 fip->non_public_fields = true;
15127 }
15128 }
15129 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15130 {
15131 /* C++ static member. */
15132
15133 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15134 is a declaration, but all versions of G++ as of this writing
15135 (so through at least 3.2.1) incorrectly generate
15136 DW_TAG_variable tags. */
15137
15138 const char *physname;
15139
15140 /* Get name of field. */
15141 fieldname = dwarf2_name (die, cu);
15142 if (fieldname == NULL)
15143 return;
15144
15145 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15146 if (attr
15147 /* Only create a symbol if this is an external value.
15148 new_symbol checks this and puts the value in the global symbol
15149 table, which we want. If it is not external, new_symbol
15150 will try to put the value in cu->list_in_scope which is wrong. */
15151 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15152 {
15153 /* A static const member, not much different than an enum as far as
15154 we're concerned, except that we can support more types. */
15155 new_symbol (die, NULL, cu);
15156 }
15157
15158 /* Get physical name. */
15159 physname = dwarf2_physname (fieldname, die, cu);
15160
15161 /* The name is already allocated along with this objfile, so we don't
15162 need to duplicate it for the type. */
15163 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15164 fp->set_type (die_type (die, cu));
15165 FIELD_NAME (*fp) = fieldname;
15166 }
15167 else if (die->tag == DW_TAG_inheritance)
15168 {
15169 /* C++ base class field. */
15170 handle_data_member_location (die, cu, fp);
15171 FIELD_BITSIZE (*fp) = 0;
15172 fp->set_type (die_type (die, cu));
15173 FIELD_NAME (*fp) = fp->type ()->name ();
15174 }
15175 else
15176 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15177 }
15178
15179 /* Can the type given by DIE define another type? */
15180
15181 static bool
15182 type_can_define_types (const struct die_info *die)
15183 {
15184 switch (die->tag)
15185 {
15186 case DW_TAG_typedef:
15187 case DW_TAG_class_type:
15188 case DW_TAG_structure_type:
15189 case DW_TAG_union_type:
15190 case DW_TAG_enumeration_type:
15191 return true;
15192
15193 default:
15194 return false;
15195 }
15196 }
15197
15198 /* Add a type definition defined in the scope of the FIP's class. */
15199
15200 static void
15201 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15202 struct dwarf2_cu *cu)
15203 {
15204 struct decl_field fp;
15205 memset (&fp, 0, sizeof (fp));
15206
15207 gdb_assert (type_can_define_types (die));
15208
15209 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15210 fp.name = dwarf2_name (die, cu);
15211 fp.type = read_type_die (die, cu);
15212
15213 /* Save accessibility. */
15214 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15215 switch (accessibility)
15216 {
15217 case DW_ACCESS_public:
15218 /* The assumed value if neither private nor protected. */
15219 break;
15220 case DW_ACCESS_private:
15221 fp.is_private = 1;
15222 break;
15223 case DW_ACCESS_protected:
15224 fp.is_protected = 1;
15225 break;
15226 }
15227
15228 if (die->tag == DW_TAG_typedef)
15229 fip->typedef_field_list.push_back (fp);
15230 else
15231 fip->nested_types_list.push_back (fp);
15232 }
15233
15234 /* A convenience typedef that's used when finding the discriminant
15235 field for a variant part. */
15236 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
15237 offset_map_type;
15238
15239 /* Compute the discriminant range for a given variant. OBSTACK is
15240 where the results will be stored. VARIANT is the variant to
15241 process. IS_UNSIGNED indicates whether the discriminant is signed
15242 or unsigned. */
15243
15244 static const gdb::array_view<discriminant_range>
15245 convert_variant_range (struct obstack *obstack, const variant_field &variant,
15246 bool is_unsigned)
15247 {
15248 std::vector<discriminant_range> ranges;
15249
15250 if (variant.default_branch)
15251 return {};
15252
15253 if (variant.discr_list_data == nullptr)
15254 {
15255 discriminant_range r
15256 = {variant.discriminant_value, variant.discriminant_value};
15257 ranges.push_back (r);
15258 }
15259 else
15260 {
15261 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15262 variant.discr_list_data->size);
15263 while (!data.empty ())
15264 {
15265 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15266 {
15267 complaint (_("invalid discriminant marker: %d"), data[0]);
15268 break;
15269 }
15270 bool is_range = data[0] == DW_DSC_range;
15271 data = data.slice (1);
15272
15273 ULONGEST low, high;
15274 unsigned int bytes_read;
15275
15276 if (data.empty ())
15277 {
15278 complaint (_("DW_AT_discr_list missing low value"));
15279 break;
15280 }
15281 if (is_unsigned)
15282 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15283 else
15284 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15285 &bytes_read);
15286 data = data.slice (bytes_read);
15287
15288 if (is_range)
15289 {
15290 if (data.empty ())
15291 {
15292 complaint (_("DW_AT_discr_list missing high value"));
15293 break;
15294 }
15295 if (is_unsigned)
15296 high = read_unsigned_leb128 (nullptr, data.data (),
15297 &bytes_read);
15298 else
15299 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15300 &bytes_read);
15301 data = data.slice (bytes_read);
15302 }
15303 else
15304 high = low;
15305
15306 ranges.push_back ({ low, high });
15307 }
15308 }
15309
15310 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15311 ranges.size ());
15312 std::copy (ranges.begin (), ranges.end (), result);
15313 return gdb::array_view<discriminant_range> (result, ranges.size ());
15314 }
15315
15316 static const gdb::array_view<variant_part> create_variant_parts
15317 (struct obstack *obstack,
15318 const offset_map_type &offset_map,
15319 struct field_info *fi,
15320 const std::vector<variant_part_builder> &variant_parts);
15321
15322 /* Fill in a "struct variant" for a given variant field. RESULT is
15323 the variant to fill in. OBSTACK is where any needed allocations
15324 will be done. OFFSET_MAP holds the mapping from section offsets to
15325 fields for the type. FI describes the fields of the type we're
15326 processing. FIELD is the variant field we're converting. */
15327
15328 static void
15329 create_one_variant (variant &result, struct obstack *obstack,
15330 const offset_map_type &offset_map,
15331 struct field_info *fi, const variant_field &field)
15332 {
15333 result.discriminants = convert_variant_range (obstack, field, false);
15334 result.first_field = field.first_field + fi->baseclasses.size ();
15335 result.last_field = field.last_field + fi->baseclasses.size ();
15336 result.parts = create_variant_parts (obstack, offset_map, fi,
15337 field.variant_parts);
15338 }
15339
15340 /* Fill in a "struct variant_part" for a given variant part. RESULT
15341 is the variant part to fill in. OBSTACK is where any needed
15342 allocations will be done. OFFSET_MAP holds the mapping from
15343 section offsets to fields for the type. FI describes the fields of
15344 the type we're processing. BUILDER is the variant part to be
15345 converted. */
15346
15347 static void
15348 create_one_variant_part (variant_part &result,
15349 struct obstack *obstack,
15350 const offset_map_type &offset_map,
15351 struct field_info *fi,
15352 const variant_part_builder &builder)
15353 {
15354 auto iter = offset_map.find (builder.discriminant_offset);
15355 if (iter == offset_map.end ())
15356 {
15357 result.discriminant_index = -1;
15358 /* Doesn't matter. */
15359 result.is_unsigned = false;
15360 }
15361 else
15362 {
15363 result.discriminant_index = iter->second;
15364 result.is_unsigned
15365 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
15366 }
15367
15368 size_t n = builder.variants.size ();
15369 variant *output = new (obstack) variant[n];
15370 for (size_t i = 0; i < n; ++i)
15371 create_one_variant (output[i], obstack, offset_map, fi,
15372 builder.variants[i]);
15373
15374 result.variants = gdb::array_view<variant> (output, n);
15375 }
15376
15377 /* Create a vector of variant parts that can be attached to a type.
15378 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15379 holds the mapping from section offsets to fields for the type. FI
15380 describes the fields of the type we're processing. VARIANT_PARTS
15381 is the vector to convert. */
15382
15383 static const gdb::array_view<variant_part>
15384 create_variant_parts (struct obstack *obstack,
15385 const offset_map_type &offset_map,
15386 struct field_info *fi,
15387 const std::vector<variant_part_builder> &variant_parts)
15388 {
15389 if (variant_parts.empty ())
15390 return {};
15391
15392 size_t n = variant_parts.size ();
15393 variant_part *result = new (obstack) variant_part[n];
15394 for (size_t i = 0; i < n; ++i)
15395 create_one_variant_part (result[i], obstack, offset_map, fi,
15396 variant_parts[i]);
15397
15398 return gdb::array_view<variant_part> (result, n);
15399 }
15400
15401 /* Compute the variant part vector for FIP, attaching it to TYPE when
15402 done. */
15403
15404 static void
15405 add_variant_property (struct field_info *fip, struct type *type,
15406 struct dwarf2_cu *cu)
15407 {
15408 /* Map section offsets of fields to their field index. Note the
15409 field index here does not take the number of baseclasses into
15410 account. */
15411 offset_map_type offset_map;
15412 for (int i = 0; i < fip->fields.size (); ++i)
15413 offset_map[fip->fields[i].offset] = i;
15414
15415 struct objfile *objfile = cu->per_objfile->objfile;
15416 gdb::array_view<variant_part> parts
15417 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15418 fip->variant_parts);
15419
15420 struct dynamic_prop prop;
15421 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15422 obstack_copy (&objfile->objfile_obstack, &parts,
15423 sizeof (parts)));
15424
15425 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
15426 }
15427
15428 /* Create the vector of fields, and attach it to the type. */
15429
15430 static void
15431 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15432 struct dwarf2_cu *cu)
15433 {
15434 int nfields = fip->nfields ();
15435
15436 /* Record the field count, allocate space for the array of fields,
15437 and create blank accessibility bitfields if necessary. */
15438 type->set_num_fields (nfields);
15439 type->set_fields
15440 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
15441
15442 if (fip->non_public_fields && cu->language != language_ada)
15443 {
15444 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15445
15446 TYPE_FIELD_PRIVATE_BITS (type) =
15447 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15448 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15449
15450 TYPE_FIELD_PROTECTED_BITS (type) =
15451 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15452 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15453
15454 TYPE_FIELD_IGNORE_BITS (type) =
15455 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15456 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15457 }
15458
15459 /* If the type has baseclasses, allocate and clear a bit vector for
15460 TYPE_FIELD_VIRTUAL_BITS. */
15461 if (!fip->baseclasses.empty () && cu->language != language_ada)
15462 {
15463 int num_bytes = B_BYTES (fip->baseclasses.size ());
15464 unsigned char *pointer;
15465
15466 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15467 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15468 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15469 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15470 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15471 }
15472
15473 if (!fip->variant_parts.empty ())
15474 add_variant_property (fip, type, cu);
15475
15476 /* Copy the saved-up fields into the field vector. */
15477 for (int i = 0; i < nfields; ++i)
15478 {
15479 struct nextfield &field
15480 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15481 : fip->fields[i - fip->baseclasses.size ()]);
15482
15483 type->field (i) = field.field;
15484 switch (field.accessibility)
15485 {
15486 case DW_ACCESS_private:
15487 if (cu->language != language_ada)
15488 SET_TYPE_FIELD_PRIVATE (type, i);
15489 break;
15490
15491 case DW_ACCESS_protected:
15492 if (cu->language != language_ada)
15493 SET_TYPE_FIELD_PROTECTED (type, i);
15494 break;
15495
15496 case DW_ACCESS_public:
15497 break;
15498
15499 default:
15500 /* Unknown accessibility. Complain and treat it as public. */
15501 {
15502 complaint (_("unsupported accessibility %d"),
15503 field.accessibility);
15504 }
15505 break;
15506 }
15507 if (i < fip->baseclasses.size ())
15508 {
15509 switch (field.virtuality)
15510 {
15511 case DW_VIRTUALITY_virtual:
15512 case DW_VIRTUALITY_pure_virtual:
15513 if (cu->language == language_ada)
15514 error (_("unexpected virtuality in component of Ada type"));
15515 SET_TYPE_FIELD_VIRTUAL (type, i);
15516 break;
15517 }
15518 }
15519 }
15520 }
15521
15522 /* Return true if this member function is a constructor, false
15523 otherwise. */
15524
15525 static int
15526 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15527 {
15528 const char *fieldname;
15529 const char *type_name;
15530 int len;
15531
15532 if (die->parent == NULL)
15533 return 0;
15534
15535 if (die->parent->tag != DW_TAG_structure_type
15536 && die->parent->tag != DW_TAG_union_type
15537 && die->parent->tag != DW_TAG_class_type)
15538 return 0;
15539
15540 fieldname = dwarf2_name (die, cu);
15541 type_name = dwarf2_name (die->parent, cu);
15542 if (fieldname == NULL || type_name == NULL)
15543 return 0;
15544
15545 len = strlen (fieldname);
15546 return (strncmp (fieldname, type_name, len) == 0
15547 && (type_name[len] == '\0' || type_name[len] == '<'));
15548 }
15549
15550 /* Add a member function to the proper fieldlist. */
15551
15552 static void
15553 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15554 struct type *type, struct dwarf2_cu *cu)
15555 {
15556 struct objfile *objfile = cu->per_objfile->objfile;
15557 struct attribute *attr;
15558 int i;
15559 struct fnfieldlist *flp = nullptr;
15560 struct fn_field *fnp;
15561 const char *fieldname;
15562 struct type *this_type;
15563
15564 if (cu->language == language_ada)
15565 error (_("unexpected member function in Ada type"));
15566
15567 /* Get name of member function. */
15568 fieldname = dwarf2_name (die, cu);
15569 if (fieldname == NULL)
15570 return;
15571
15572 /* Look up member function name in fieldlist. */
15573 for (i = 0; i < fip->fnfieldlists.size (); i++)
15574 {
15575 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15576 {
15577 flp = &fip->fnfieldlists[i];
15578 break;
15579 }
15580 }
15581
15582 /* Create a new fnfieldlist if necessary. */
15583 if (flp == nullptr)
15584 {
15585 fip->fnfieldlists.emplace_back ();
15586 flp = &fip->fnfieldlists.back ();
15587 flp->name = fieldname;
15588 i = fip->fnfieldlists.size () - 1;
15589 }
15590
15591 /* Create a new member function field and add it to the vector of
15592 fnfieldlists. */
15593 flp->fnfields.emplace_back ();
15594 fnp = &flp->fnfields.back ();
15595
15596 /* Delay processing of the physname until later. */
15597 if (cu->language == language_cplus)
15598 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15599 die, cu);
15600 else
15601 {
15602 const char *physname = dwarf2_physname (fieldname, die, cu);
15603 fnp->physname = physname ? physname : "";
15604 }
15605
15606 fnp->type = alloc_type (objfile);
15607 this_type = read_type_die (die, cu);
15608 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15609 {
15610 int nparams = this_type->num_fields ();
15611
15612 /* TYPE is the domain of this method, and THIS_TYPE is the type
15613 of the method itself (TYPE_CODE_METHOD). */
15614 smash_to_method_type (fnp->type, type,
15615 TYPE_TARGET_TYPE (this_type),
15616 this_type->fields (),
15617 this_type->num_fields (),
15618 this_type->has_varargs ());
15619
15620 /* Handle static member functions.
15621 Dwarf2 has no clean way to discern C++ static and non-static
15622 member functions. G++ helps GDB by marking the first
15623 parameter for non-static member functions (which is the this
15624 pointer) as artificial. We obtain this information from
15625 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15626 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15627 fnp->voffset = VOFFSET_STATIC;
15628 }
15629 else
15630 complaint (_("member function type missing for '%s'"),
15631 dwarf2_full_name (fieldname, die, cu));
15632
15633 /* Get fcontext from DW_AT_containing_type if present. */
15634 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15635 fnp->fcontext = die_containing_type (die, cu);
15636
15637 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15638 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15639
15640 /* Get accessibility. */
15641 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15642 switch (accessibility)
15643 {
15644 case DW_ACCESS_private:
15645 fnp->is_private = 1;
15646 break;
15647 case DW_ACCESS_protected:
15648 fnp->is_protected = 1;
15649 break;
15650 }
15651
15652 /* Check for artificial methods. */
15653 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15654 if (attr && attr->as_boolean ())
15655 fnp->is_artificial = 1;
15656
15657 /* Check for defaulted methods. */
15658 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15659 if (attr != nullptr)
15660 fnp->defaulted = attr->defaulted ();
15661
15662 /* Check for deleted methods. */
15663 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15664 if (attr != nullptr && attr->as_boolean ())
15665 fnp->is_deleted = 1;
15666
15667 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15668
15669 /* Get index in virtual function table if it is a virtual member
15670 function. For older versions of GCC, this is an offset in the
15671 appropriate virtual table, as specified by DW_AT_containing_type.
15672 For everyone else, it is an expression to be evaluated relative
15673 to the object address. */
15674
15675 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15676 if (attr != nullptr)
15677 {
15678 if (attr->form_is_block () && attr->as_block ()->size > 0)
15679 {
15680 struct dwarf_block *block = attr->as_block ();
15681
15682 if (block->data[0] == DW_OP_constu)
15683 {
15684 /* Old-style GCC. */
15685 fnp->voffset = decode_locdesc (block, cu) + 2;
15686 }
15687 else if (block->data[0] == DW_OP_deref
15688 || (block->size > 1
15689 && block->data[0] == DW_OP_deref_size
15690 && block->data[1] == cu->header.addr_size))
15691 {
15692 fnp->voffset = decode_locdesc (block, cu);
15693 if ((fnp->voffset % cu->header.addr_size) != 0)
15694 dwarf2_complex_location_expr_complaint ();
15695 else
15696 fnp->voffset /= cu->header.addr_size;
15697 fnp->voffset += 2;
15698 }
15699 else
15700 dwarf2_complex_location_expr_complaint ();
15701
15702 if (!fnp->fcontext)
15703 {
15704 /* If there is no `this' field and no DW_AT_containing_type,
15705 we cannot actually find a base class context for the
15706 vtable! */
15707 if (this_type->num_fields () == 0
15708 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15709 {
15710 complaint (_("cannot determine context for virtual member "
15711 "function \"%s\" (offset %s)"),
15712 fieldname, sect_offset_str (die->sect_off));
15713 }
15714 else
15715 {
15716 fnp->fcontext
15717 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15718 }
15719 }
15720 }
15721 else if (attr->form_is_section_offset ())
15722 {
15723 dwarf2_complex_location_expr_complaint ();
15724 }
15725 else
15726 {
15727 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15728 fieldname);
15729 }
15730 }
15731 else
15732 {
15733 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15734 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
15735 {
15736 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15737 complaint (_("Member function \"%s\" (offset %s) is virtual "
15738 "but the vtable offset is not specified"),
15739 fieldname, sect_offset_str (die->sect_off));
15740 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15741 TYPE_CPLUS_DYNAMIC (type) = 1;
15742 }
15743 }
15744 }
15745
15746 /* Create the vector of member function fields, and attach it to the type. */
15747
15748 static void
15749 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15750 struct dwarf2_cu *cu)
15751 {
15752 if (cu->language == language_ada)
15753 error (_("unexpected member functions in Ada type"));
15754
15755 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15756 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15757 TYPE_ALLOC (type,
15758 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15759
15760 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15761 {
15762 struct fnfieldlist &nf = fip->fnfieldlists[i];
15763 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15764
15765 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15766 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15767 fn_flp->fn_fields = (struct fn_field *)
15768 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15769
15770 for (int k = 0; k < nf.fnfields.size (); ++k)
15771 fn_flp->fn_fields[k] = nf.fnfields[k];
15772 }
15773
15774 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15775 }
15776
15777 /* Returns non-zero if NAME is the name of a vtable member in CU's
15778 language, zero otherwise. */
15779 static int
15780 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15781 {
15782 static const char vptr[] = "_vptr";
15783
15784 /* Look for the C++ form of the vtable. */
15785 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15786 return 1;
15787
15788 return 0;
15789 }
15790
15791 /* GCC outputs unnamed structures that are really pointers to member
15792 functions, with the ABI-specified layout. If TYPE describes
15793 such a structure, smash it into a member function type.
15794
15795 GCC shouldn't do this; it should just output pointer to member DIEs.
15796 This is GCC PR debug/28767. */
15797
15798 static void
15799 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15800 {
15801 struct type *pfn_type, *self_type, *new_type;
15802
15803 /* Check for a structure with no name and two children. */
15804 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15805 return;
15806
15807 /* Check for __pfn and __delta members. */
15808 if (TYPE_FIELD_NAME (type, 0) == NULL
15809 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15810 || TYPE_FIELD_NAME (type, 1) == NULL
15811 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15812 return;
15813
15814 /* Find the type of the method. */
15815 pfn_type = type->field (0).type ();
15816 if (pfn_type == NULL
15817 || pfn_type->code () != TYPE_CODE_PTR
15818 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15819 return;
15820
15821 /* Look for the "this" argument. */
15822 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15823 if (pfn_type->num_fields () == 0
15824 /* || pfn_type->field (0).type () == NULL */
15825 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15826 return;
15827
15828 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15829 new_type = alloc_type (objfile);
15830 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15831 pfn_type->fields (), pfn_type->num_fields (),
15832 pfn_type->has_varargs ());
15833 smash_to_methodptr_type (type, new_type);
15834 }
15835
15836 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15837 appropriate error checking and issuing complaints if there is a
15838 problem. */
15839
15840 static ULONGEST
15841 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15842 {
15843 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15844
15845 if (attr == nullptr)
15846 return 0;
15847
15848 if (!attr->form_is_constant ())
15849 {
15850 complaint (_("DW_AT_alignment must have constant form"
15851 " - DIE at %s [in module %s]"),
15852 sect_offset_str (die->sect_off),
15853 objfile_name (cu->per_objfile->objfile));
15854 return 0;
15855 }
15856
15857 LONGEST val = attr->constant_value (0);
15858 if (val < 0)
15859 {
15860 complaint (_("DW_AT_alignment value must not be negative"
15861 " - DIE at %s [in module %s]"),
15862 sect_offset_str (die->sect_off),
15863 objfile_name (cu->per_objfile->objfile));
15864 return 0;
15865 }
15866 ULONGEST align = val;
15867
15868 if (align == 0)
15869 {
15870 complaint (_("DW_AT_alignment value must not be zero"
15871 " - DIE at %s [in module %s]"),
15872 sect_offset_str (die->sect_off),
15873 objfile_name (cu->per_objfile->objfile));
15874 return 0;
15875 }
15876 if ((align & (align - 1)) != 0)
15877 {
15878 complaint (_("DW_AT_alignment value must be a power of 2"
15879 " - DIE at %s [in module %s]"),
15880 sect_offset_str (die->sect_off),
15881 objfile_name (cu->per_objfile->objfile));
15882 return 0;
15883 }
15884
15885 return align;
15886 }
15887
15888 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15889 the alignment for TYPE. */
15890
15891 static void
15892 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15893 struct type *type)
15894 {
15895 if (!set_type_align (type, get_alignment (cu, die)))
15896 complaint (_("DW_AT_alignment value too large"
15897 " - DIE at %s [in module %s]"),
15898 sect_offset_str (die->sect_off),
15899 objfile_name (cu->per_objfile->objfile));
15900 }
15901
15902 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15903 constant for a type, according to DWARF5 spec, Table 5.5. */
15904
15905 static bool
15906 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15907 {
15908 switch (value)
15909 {
15910 case DW_CC_normal:
15911 case DW_CC_pass_by_reference:
15912 case DW_CC_pass_by_value:
15913 return true;
15914
15915 default:
15916 complaint (_("unrecognized DW_AT_calling_convention value "
15917 "(%s) for a type"), pulongest (value));
15918 return false;
15919 }
15920 }
15921
15922 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15923 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15924 also according to GNU-specific values (see include/dwarf2.h). */
15925
15926 static bool
15927 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15928 {
15929 switch (value)
15930 {
15931 case DW_CC_normal:
15932 case DW_CC_program:
15933 case DW_CC_nocall:
15934 return true;
15935
15936 case DW_CC_GNU_renesas_sh:
15937 case DW_CC_GNU_borland_fastcall_i386:
15938 case DW_CC_GDB_IBM_OpenCL:
15939 return true;
15940
15941 default:
15942 complaint (_("unrecognized DW_AT_calling_convention value "
15943 "(%s) for a subroutine"), pulongest (value));
15944 return false;
15945 }
15946 }
15947
15948 /* Called when we find the DIE that starts a structure or union scope
15949 (definition) to create a type for the structure or union. Fill in
15950 the type's name and general properties; the members will not be
15951 processed until process_structure_scope. A symbol table entry for
15952 the type will also not be done until process_structure_scope (assuming
15953 the type has a name).
15954
15955 NOTE: we need to call these functions regardless of whether or not the
15956 DIE has a DW_AT_name attribute, since it might be an anonymous
15957 structure or union. This gets the type entered into our set of
15958 user defined types. */
15959
15960 static struct type *
15961 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15962 {
15963 struct objfile *objfile = cu->per_objfile->objfile;
15964 struct type *type;
15965 struct attribute *attr;
15966 const char *name;
15967
15968 /* If the definition of this type lives in .debug_types, read that type.
15969 Don't follow DW_AT_specification though, that will take us back up
15970 the chain and we want to go down. */
15971 attr = die->attr (DW_AT_signature);
15972 if (attr != nullptr)
15973 {
15974 type = get_DW_AT_signature_type (die, attr, cu);
15975
15976 /* The type's CU may not be the same as CU.
15977 Ensure TYPE is recorded with CU in die_type_hash. */
15978 return set_die_type (die, type, cu);
15979 }
15980
15981 type = alloc_type (objfile);
15982 INIT_CPLUS_SPECIFIC (type);
15983
15984 name = dwarf2_name (die, cu);
15985 if (name != NULL)
15986 {
15987 if (cu->language == language_cplus
15988 || cu->language == language_d
15989 || cu->language == language_rust)
15990 {
15991 const char *full_name = dwarf2_full_name (name, die, cu);
15992
15993 /* dwarf2_full_name might have already finished building the DIE's
15994 type. If so, there is no need to continue. */
15995 if (get_die_type (die, cu) != NULL)
15996 return get_die_type (die, cu);
15997
15998 type->set_name (full_name);
15999 }
16000 else
16001 {
16002 /* The name is already allocated along with this objfile, so
16003 we don't need to duplicate it for the type. */
16004 type->set_name (name);
16005 }
16006 }
16007
16008 if (die->tag == DW_TAG_structure_type)
16009 {
16010 type->set_code (TYPE_CODE_STRUCT);
16011 }
16012 else if (die->tag == DW_TAG_union_type)
16013 {
16014 type->set_code (TYPE_CODE_UNION);
16015 }
16016 else
16017 {
16018 type->set_code (TYPE_CODE_STRUCT);
16019 }
16020
16021 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16022 TYPE_DECLARED_CLASS (type) = 1;
16023
16024 /* Store the calling convention in the type if it's available in
16025 the die. Otherwise the calling convention remains set to
16026 the default value DW_CC_normal. */
16027 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16028 if (attr != nullptr
16029 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
16030 {
16031 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16032 TYPE_CPLUS_CALLING_CONVENTION (type)
16033 = (enum dwarf_calling_convention) (attr->constant_value (0));
16034 }
16035
16036 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16037 if (attr != nullptr)
16038 {
16039 if (attr->form_is_constant ())
16040 TYPE_LENGTH (type) = attr->constant_value (0);
16041 else
16042 {
16043 struct dynamic_prop prop;
16044 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
16045 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
16046 TYPE_LENGTH (type) = 0;
16047 }
16048 }
16049 else
16050 {
16051 TYPE_LENGTH (type) = 0;
16052 }
16053
16054 maybe_set_alignment (cu, die, type);
16055
16056 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
16057 {
16058 /* ICC<14 does not output the required DW_AT_declaration on
16059 incomplete types, but gives them a size of zero. */
16060 type->set_is_stub (true);
16061 }
16062 else
16063 type->set_stub_is_supported (true);
16064
16065 if (die_is_declaration (die, cu))
16066 type->set_is_stub (true);
16067 else if (attr == NULL && die->child == NULL
16068 && producer_is_realview (cu->producer))
16069 /* RealView does not output the required DW_AT_declaration
16070 on incomplete types. */
16071 type->set_is_stub (true);
16072
16073 /* We need to add the type field to the die immediately so we don't
16074 infinitely recurse when dealing with pointers to the structure
16075 type within the structure itself. */
16076 set_die_type (die, type, cu);
16077
16078 /* set_die_type should be already done. */
16079 set_descriptive_type (type, die, cu);
16080
16081 return type;
16082 }
16083
16084 static void handle_struct_member_die
16085 (struct die_info *child_die,
16086 struct type *type,
16087 struct field_info *fi,
16088 std::vector<struct symbol *> *template_args,
16089 struct dwarf2_cu *cu);
16090
16091 /* A helper for handle_struct_member_die that handles
16092 DW_TAG_variant_part. */
16093
16094 static void
16095 handle_variant_part (struct die_info *die, struct type *type,
16096 struct field_info *fi,
16097 std::vector<struct symbol *> *template_args,
16098 struct dwarf2_cu *cu)
16099 {
16100 variant_part_builder *new_part;
16101 if (fi->current_variant_part == nullptr)
16102 {
16103 fi->variant_parts.emplace_back ();
16104 new_part = &fi->variant_parts.back ();
16105 }
16106 else if (!fi->current_variant_part->processing_variant)
16107 {
16108 complaint (_("nested DW_TAG_variant_part seen "
16109 "- DIE at %s [in module %s]"),
16110 sect_offset_str (die->sect_off),
16111 objfile_name (cu->per_objfile->objfile));
16112 return;
16113 }
16114 else
16115 {
16116 variant_field &current = fi->current_variant_part->variants.back ();
16117 current.variant_parts.emplace_back ();
16118 new_part = &current.variant_parts.back ();
16119 }
16120
16121 /* When we recurse, we want callees to add to this new variant
16122 part. */
16123 scoped_restore save_current_variant_part
16124 = make_scoped_restore (&fi->current_variant_part, new_part);
16125
16126 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16127 if (discr == NULL)
16128 {
16129 /* It's a univariant form, an extension we support. */
16130 }
16131 else if (discr->form_is_ref ())
16132 {
16133 struct dwarf2_cu *target_cu = cu;
16134 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16135
16136 new_part->discriminant_offset = target_die->sect_off;
16137 }
16138 else
16139 {
16140 complaint (_("DW_AT_discr does not have DIE reference form"
16141 " - DIE at %s [in module %s]"),
16142 sect_offset_str (die->sect_off),
16143 objfile_name (cu->per_objfile->objfile));
16144 }
16145
16146 for (die_info *child_die = die->child;
16147 child_die != NULL;
16148 child_die = child_die->sibling)
16149 handle_struct_member_die (child_die, type, fi, template_args, cu);
16150 }
16151
16152 /* A helper for handle_struct_member_die that handles
16153 DW_TAG_variant. */
16154
16155 static void
16156 handle_variant (struct die_info *die, struct type *type,
16157 struct field_info *fi,
16158 std::vector<struct symbol *> *template_args,
16159 struct dwarf2_cu *cu)
16160 {
16161 if (fi->current_variant_part == nullptr)
16162 {
16163 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
16164 "- DIE at %s [in module %s]"),
16165 sect_offset_str (die->sect_off),
16166 objfile_name (cu->per_objfile->objfile));
16167 return;
16168 }
16169 if (fi->current_variant_part->processing_variant)
16170 {
16171 complaint (_("nested DW_TAG_variant seen "
16172 "- DIE at %s [in module %s]"),
16173 sect_offset_str (die->sect_off),
16174 objfile_name (cu->per_objfile->objfile));
16175 return;
16176 }
16177
16178 scoped_restore save_processing_variant
16179 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16180 true);
16181
16182 fi->current_variant_part->variants.emplace_back ();
16183 variant_field &variant = fi->current_variant_part->variants.back ();
16184 variant.first_field = fi->fields.size ();
16185
16186 /* In a variant we want to get the discriminant and also add a
16187 field for our sole member child. */
16188 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
16189 if (discr == nullptr || !discr->form_is_constant ())
16190 {
16191 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
16192 if (discr == nullptr || discr->as_block ()->size == 0)
16193 variant.default_branch = true;
16194 else
16195 variant.discr_list_data = discr->as_block ();
16196 }
16197 else
16198 variant.discriminant_value = discr->constant_value (0);
16199
16200 for (die_info *variant_child = die->child;
16201 variant_child != NULL;
16202 variant_child = variant_child->sibling)
16203 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16204
16205 variant.last_field = fi->fields.size ();
16206 }
16207
16208 /* A helper for process_structure_scope that handles a single member
16209 DIE. */
16210
16211 static void
16212 handle_struct_member_die (struct die_info *child_die, struct type *type,
16213 struct field_info *fi,
16214 std::vector<struct symbol *> *template_args,
16215 struct dwarf2_cu *cu)
16216 {
16217 if (child_die->tag == DW_TAG_member
16218 || child_die->tag == DW_TAG_variable)
16219 {
16220 /* NOTE: carlton/2002-11-05: A C++ static data member
16221 should be a DW_TAG_member that is a declaration, but
16222 all versions of G++ as of this writing (so through at
16223 least 3.2.1) incorrectly generate DW_TAG_variable
16224 tags for them instead. */
16225 dwarf2_add_field (fi, child_die, cu);
16226 }
16227 else if (child_die->tag == DW_TAG_subprogram)
16228 {
16229 /* Rust doesn't have member functions in the C++ sense.
16230 However, it does emit ordinary functions as children
16231 of a struct DIE. */
16232 if (cu->language == language_rust)
16233 read_func_scope (child_die, cu);
16234 else
16235 {
16236 /* C++ member function. */
16237 dwarf2_add_member_fn (fi, child_die, type, cu);
16238 }
16239 }
16240 else if (child_die->tag == DW_TAG_inheritance)
16241 {
16242 /* C++ base class field. */
16243 dwarf2_add_field (fi, child_die, cu);
16244 }
16245 else if (type_can_define_types (child_die))
16246 dwarf2_add_type_defn (fi, child_die, cu);
16247 else if (child_die->tag == DW_TAG_template_type_param
16248 || child_die->tag == DW_TAG_template_value_param)
16249 {
16250 struct symbol *arg = new_symbol (child_die, NULL, cu);
16251
16252 if (arg != NULL)
16253 template_args->push_back (arg);
16254 }
16255 else if (child_die->tag == DW_TAG_variant_part)
16256 handle_variant_part (child_die, type, fi, template_args, cu);
16257 else if (child_die->tag == DW_TAG_variant)
16258 handle_variant (child_die, type, fi, template_args, cu);
16259 }
16260
16261 /* Finish creating a structure or union type, including filling in
16262 its members and creating a symbol for it. */
16263
16264 static void
16265 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16266 {
16267 struct objfile *objfile = cu->per_objfile->objfile;
16268 struct die_info *child_die;
16269 struct type *type;
16270
16271 type = get_die_type (die, cu);
16272 if (type == NULL)
16273 type = read_structure_type (die, cu);
16274
16275 bool has_template_parameters = false;
16276 if (die->child != NULL && ! die_is_declaration (die, cu))
16277 {
16278 struct field_info fi;
16279 std::vector<struct symbol *> template_args;
16280
16281 child_die = die->child;
16282
16283 while (child_die && child_die->tag)
16284 {
16285 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16286 child_die = child_die->sibling;
16287 }
16288
16289 /* Attach template arguments to type. */
16290 if (!template_args.empty ())
16291 {
16292 has_template_parameters = true;
16293 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16294 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16295 TYPE_TEMPLATE_ARGUMENTS (type)
16296 = XOBNEWVEC (&objfile->objfile_obstack,
16297 struct symbol *,
16298 TYPE_N_TEMPLATE_ARGUMENTS (type));
16299 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16300 template_args.data (),
16301 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16302 * sizeof (struct symbol *)));
16303 }
16304
16305 /* Attach fields and member functions to the type. */
16306 if (fi.nfields () > 0)
16307 dwarf2_attach_fields_to_type (&fi, type, cu);
16308 if (!fi.fnfieldlists.empty ())
16309 {
16310 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16311
16312 /* Get the type which refers to the base class (possibly this
16313 class itself) which contains the vtable pointer for the current
16314 class from the DW_AT_containing_type attribute. This use of
16315 DW_AT_containing_type is a GNU extension. */
16316
16317 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16318 {
16319 struct type *t = die_containing_type (die, cu);
16320
16321 set_type_vptr_basetype (type, t);
16322 if (type == t)
16323 {
16324 int i;
16325
16326 /* Our own class provides vtbl ptr. */
16327 for (i = t->num_fields () - 1;
16328 i >= TYPE_N_BASECLASSES (t);
16329 --i)
16330 {
16331 const char *fieldname = TYPE_FIELD_NAME (t, i);
16332
16333 if (is_vtable_name (fieldname, cu))
16334 {
16335 set_type_vptr_fieldno (type, i);
16336 break;
16337 }
16338 }
16339
16340 /* Complain if virtual function table field not found. */
16341 if (i < TYPE_N_BASECLASSES (t))
16342 complaint (_("virtual function table pointer "
16343 "not found when defining class '%s'"),
16344 type->name () ? type->name () : "");
16345 }
16346 else
16347 {
16348 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16349 }
16350 }
16351 else if (cu->producer
16352 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16353 {
16354 /* The IBM XLC compiler does not provide direct indication
16355 of the containing type, but the vtable pointer is
16356 always named __vfp. */
16357
16358 int i;
16359
16360 for (i = type->num_fields () - 1;
16361 i >= TYPE_N_BASECLASSES (type);
16362 --i)
16363 {
16364 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16365 {
16366 set_type_vptr_fieldno (type, i);
16367 set_type_vptr_basetype (type, type);
16368 break;
16369 }
16370 }
16371 }
16372 }
16373
16374 /* Copy fi.typedef_field_list linked list elements content into the
16375 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16376 if (!fi.typedef_field_list.empty ())
16377 {
16378 int count = fi.typedef_field_list.size ();
16379
16380 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16381 TYPE_TYPEDEF_FIELD_ARRAY (type)
16382 = ((struct decl_field *)
16383 TYPE_ALLOC (type,
16384 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16385 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16386
16387 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16388 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16389 }
16390
16391 /* Copy fi.nested_types_list linked list elements content into the
16392 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16393 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16394 {
16395 int count = fi.nested_types_list.size ();
16396
16397 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16398 TYPE_NESTED_TYPES_ARRAY (type)
16399 = ((struct decl_field *)
16400 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16401 TYPE_NESTED_TYPES_COUNT (type) = count;
16402
16403 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16404 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16405 }
16406 }
16407
16408 quirk_gcc_member_function_pointer (type, objfile);
16409 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16410 cu->rust_unions.push_back (type);
16411
16412 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16413 snapshots) has been known to create a die giving a declaration
16414 for a class that has, as a child, a die giving a definition for a
16415 nested class. So we have to process our children even if the
16416 current die is a declaration. Normally, of course, a declaration
16417 won't have any children at all. */
16418
16419 child_die = die->child;
16420
16421 while (child_die != NULL && child_die->tag)
16422 {
16423 if (child_die->tag == DW_TAG_member
16424 || child_die->tag == DW_TAG_variable
16425 || child_die->tag == DW_TAG_inheritance
16426 || child_die->tag == DW_TAG_template_value_param
16427 || child_die->tag == DW_TAG_template_type_param)
16428 {
16429 /* Do nothing. */
16430 }
16431 else
16432 process_die (child_die, cu);
16433
16434 child_die = child_die->sibling;
16435 }
16436
16437 /* Do not consider external references. According to the DWARF standard,
16438 these DIEs are identified by the fact that they have no byte_size
16439 attribute, and a declaration attribute. */
16440 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16441 || !die_is_declaration (die, cu)
16442 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16443 {
16444 struct symbol *sym = new_symbol (die, type, cu);
16445
16446 if (has_template_parameters)
16447 {
16448 struct symtab *symtab;
16449 if (sym != nullptr)
16450 symtab = symbol_symtab (sym);
16451 else if (cu->line_header != nullptr)
16452 {
16453 /* Any related symtab will do. */
16454 symtab
16455 = cu->line_header->file_names ()[0].symtab;
16456 }
16457 else
16458 {
16459 symtab = nullptr;
16460 complaint (_("could not find suitable "
16461 "symtab for template parameter"
16462 " - DIE at %s [in module %s]"),
16463 sect_offset_str (die->sect_off),
16464 objfile_name (objfile));
16465 }
16466
16467 if (symtab != nullptr)
16468 {
16469 /* Make sure that the symtab is set on the new symbols.
16470 Even though they don't appear in this symtab directly,
16471 other parts of gdb assume that symbols do, and this is
16472 reasonably true. */
16473 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16474 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16475 }
16476 }
16477 }
16478 }
16479
16480 /* Assuming DIE is an enumeration type, and TYPE is its associated
16481 type, update TYPE using some information only available in DIE's
16482 children. In particular, the fields are computed. */
16483
16484 static void
16485 update_enumeration_type_from_children (struct die_info *die,
16486 struct type *type,
16487 struct dwarf2_cu *cu)
16488 {
16489 struct die_info *child_die;
16490 int unsigned_enum = 1;
16491 int flag_enum = 1;
16492
16493 auto_obstack obstack;
16494 std::vector<struct field> fields;
16495
16496 for (child_die = die->child;
16497 child_die != NULL && child_die->tag;
16498 child_die = child_die->sibling)
16499 {
16500 struct attribute *attr;
16501 LONGEST value;
16502 const gdb_byte *bytes;
16503 struct dwarf2_locexpr_baton *baton;
16504 const char *name;
16505
16506 if (child_die->tag != DW_TAG_enumerator)
16507 continue;
16508
16509 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16510 if (attr == NULL)
16511 continue;
16512
16513 name = dwarf2_name (child_die, cu);
16514 if (name == NULL)
16515 name = "<anonymous enumerator>";
16516
16517 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16518 &value, &bytes, &baton);
16519 if (value < 0)
16520 {
16521 unsigned_enum = 0;
16522 flag_enum = 0;
16523 }
16524 else
16525 {
16526 if (count_one_bits_ll (value) >= 2)
16527 flag_enum = 0;
16528 }
16529
16530 fields.emplace_back ();
16531 struct field &field = fields.back ();
16532 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16533 SET_FIELD_ENUMVAL (field, value);
16534 }
16535
16536 if (!fields.empty ())
16537 {
16538 type->set_num_fields (fields.size ());
16539 type->set_fields
16540 ((struct field *)
16541 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16542 memcpy (type->fields (), fields.data (),
16543 sizeof (struct field) * fields.size ());
16544 }
16545
16546 if (unsigned_enum)
16547 type->set_is_unsigned (true);
16548
16549 if (flag_enum)
16550 TYPE_FLAG_ENUM (type) = 1;
16551 }
16552
16553 /* Given a DW_AT_enumeration_type die, set its type. We do not
16554 complete the type's fields yet, or create any symbols. */
16555
16556 static struct type *
16557 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16558 {
16559 struct objfile *objfile = cu->per_objfile->objfile;
16560 struct type *type;
16561 struct attribute *attr;
16562 const char *name;
16563
16564 /* If the definition of this type lives in .debug_types, read that type.
16565 Don't follow DW_AT_specification though, that will take us back up
16566 the chain and we want to go down. */
16567 attr = die->attr (DW_AT_signature);
16568 if (attr != nullptr)
16569 {
16570 type = get_DW_AT_signature_type (die, attr, cu);
16571
16572 /* The type's CU may not be the same as CU.
16573 Ensure TYPE is recorded with CU in die_type_hash. */
16574 return set_die_type (die, type, cu);
16575 }
16576
16577 type = alloc_type (objfile);
16578
16579 type->set_code (TYPE_CODE_ENUM);
16580 name = dwarf2_full_name (NULL, die, cu);
16581 if (name != NULL)
16582 type->set_name (name);
16583
16584 attr = dwarf2_attr (die, DW_AT_type, cu);
16585 if (attr != NULL)
16586 {
16587 struct type *underlying_type = die_type (die, cu);
16588
16589 TYPE_TARGET_TYPE (type) = underlying_type;
16590 }
16591
16592 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16593 if (attr != nullptr)
16594 {
16595 TYPE_LENGTH (type) = attr->constant_value (0);
16596 }
16597 else
16598 {
16599 TYPE_LENGTH (type) = 0;
16600 }
16601
16602 maybe_set_alignment (cu, die, type);
16603
16604 /* The enumeration DIE can be incomplete. In Ada, any type can be
16605 declared as private in the package spec, and then defined only
16606 inside the package body. Such types are known as Taft Amendment
16607 Types. When another package uses such a type, an incomplete DIE
16608 may be generated by the compiler. */
16609 if (die_is_declaration (die, cu))
16610 type->set_is_stub (true);
16611
16612 /* If this type has an underlying type that is not a stub, then we
16613 may use its attributes. We always use the "unsigned" attribute
16614 in this situation, because ordinarily we guess whether the type
16615 is unsigned -- but the guess can be wrong and the underlying type
16616 can tell us the reality. However, we defer to a local size
16617 attribute if one exists, because this lets the compiler override
16618 the underlying type if needed. */
16619 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16620 {
16621 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16622 underlying_type = check_typedef (underlying_type);
16623
16624 type->set_is_unsigned (underlying_type->is_unsigned ());
16625
16626 if (TYPE_LENGTH (type) == 0)
16627 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16628
16629 if (TYPE_RAW_ALIGN (type) == 0
16630 && TYPE_RAW_ALIGN (underlying_type) != 0)
16631 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16632 }
16633
16634 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16635
16636 set_die_type (die, type, cu);
16637
16638 /* Finish the creation of this type by using the enum's children.
16639 Note that, as usual, this must come after set_die_type to avoid
16640 infinite recursion when trying to compute the names of the
16641 enumerators. */
16642 update_enumeration_type_from_children (die, type, cu);
16643
16644 return type;
16645 }
16646
16647 /* Given a pointer to a die which begins an enumeration, process all
16648 the dies that define the members of the enumeration, and create the
16649 symbol for the enumeration type.
16650
16651 NOTE: We reverse the order of the element list. */
16652
16653 static void
16654 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16655 {
16656 struct type *this_type;
16657
16658 this_type = get_die_type (die, cu);
16659 if (this_type == NULL)
16660 this_type = read_enumeration_type (die, cu);
16661
16662 if (die->child != NULL)
16663 {
16664 struct die_info *child_die;
16665 const char *name;
16666
16667 child_die = die->child;
16668 while (child_die && child_die->tag)
16669 {
16670 if (child_die->tag != DW_TAG_enumerator)
16671 {
16672 process_die (child_die, cu);
16673 }
16674 else
16675 {
16676 name = dwarf2_name (child_die, cu);
16677 if (name)
16678 new_symbol (child_die, this_type, cu);
16679 }
16680
16681 child_die = child_die->sibling;
16682 }
16683 }
16684
16685 /* If we are reading an enum from a .debug_types unit, and the enum
16686 is a declaration, and the enum is not the signatured type in the
16687 unit, then we do not want to add a symbol for it. Adding a
16688 symbol would in some cases obscure the true definition of the
16689 enum, giving users an incomplete type when the definition is
16690 actually available. Note that we do not want to do this for all
16691 enums which are just declarations, because C++0x allows forward
16692 enum declarations. */
16693 if (cu->per_cu->is_debug_types
16694 && die_is_declaration (die, cu))
16695 {
16696 struct signatured_type *sig_type;
16697
16698 sig_type = (struct signatured_type *) cu->per_cu;
16699 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16700 if (sig_type->type_offset_in_section != die->sect_off)
16701 return;
16702 }
16703
16704 new_symbol (die, this_type, cu);
16705 }
16706
16707 /* Extract all information from a DW_TAG_array_type DIE and put it in
16708 the DIE's type field. For now, this only handles one dimensional
16709 arrays. */
16710
16711 static struct type *
16712 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16713 {
16714 struct objfile *objfile = cu->per_objfile->objfile;
16715 struct die_info *child_die;
16716 struct type *type;
16717 struct type *element_type, *range_type, *index_type;
16718 struct attribute *attr;
16719 const char *name;
16720 struct dynamic_prop *byte_stride_prop = NULL;
16721 unsigned int bit_stride = 0;
16722
16723 element_type = die_type (die, cu);
16724
16725 /* The die_type call above may have already set the type for this DIE. */
16726 type = get_die_type (die, cu);
16727 if (type)
16728 return type;
16729
16730 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16731 if (attr != NULL)
16732 {
16733 int stride_ok;
16734 struct type *prop_type = cu->addr_sized_int_type (false);
16735
16736 byte_stride_prop
16737 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16738 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16739 prop_type);
16740 if (!stride_ok)
16741 {
16742 complaint (_("unable to read array DW_AT_byte_stride "
16743 " - DIE at %s [in module %s]"),
16744 sect_offset_str (die->sect_off),
16745 objfile_name (cu->per_objfile->objfile));
16746 /* Ignore this attribute. We will likely not be able to print
16747 arrays of this type correctly, but there is little we can do
16748 to help if we cannot read the attribute's value. */
16749 byte_stride_prop = NULL;
16750 }
16751 }
16752
16753 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16754 if (attr != NULL)
16755 bit_stride = attr->constant_value (0);
16756
16757 /* Irix 6.2 native cc creates array types without children for
16758 arrays with unspecified length. */
16759 if (die->child == NULL)
16760 {
16761 index_type = objfile_type (objfile)->builtin_int;
16762 range_type = create_static_range_type (NULL, index_type, 0, -1);
16763 type = create_array_type_with_stride (NULL, element_type, range_type,
16764 byte_stride_prop, bit_stride);
16765 return set_die_type (die, type, cu);
16766 }
16767
16768 std::vector<struct type *> range_types;
16769 child_die = die->child;
16770 while (child_die && child_die->tag)
16771 {
16772 if (child_die->tag == DW_TAG_subrange_type)
16773 {
16774 struct type *child_type = read_type_die (child_die, cu);
16775
16776 if (child_type != NULL)
16777 {
16778 /* The range type was succesfully read. Save it for the
16779 array type creation. */
16780 range_types.push_back (child_type);
16781 }
16782 }
16783 child_die = child_die->sibling;
16784 }
16785
16786 /* Dwarf2 dimensions are output from left to right, create the
16787 necessary array types in backwards order. */
16788
16789 type = element_type;
16790
16791 if (read_array_order (die, cu) == DW_ORD_col_major)
16792 {
16793 int i = 0;
16794
16795 while (i < range_types.size ())
16796 type = create_array_type_with_stride (NULL, type, range_types[i++],
16797 byte_stride_prop, bit_stride);
16798 }
16799 else
16800 {
16801 size_t ndim = range_types.size ();
16802 while (ndim-- > 0)
16803 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16804 byte_stride_prop, bit_stride);
16805 }
16806
16807 /* Understand Dwarf2 support for vector types (like they occur on
16808 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16809 array type. This is not part of the Dwarf2/3 standard yet, but a
16810 custom vendor extension. The main difference between a regular
16811 array and the vector variant is that vectors are passed by value
16812 to functions. */
16813 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16814 if (attr != nullptr)
16815 make_vector_type (type);
16816
16817 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16818 implementation may choose to implement triple vectors using this
16819 attribute. */
16820 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16821 if (attr != nullptr && attr->form_is_unsigned ())
16822 {
16823 if (attr->as_unsigned () >= TYPE_LENGTH (type))
16824 TYPE_LENGTH (type) = attr->as_unsigned ();
16825 else
16826 complaint (_("DW_AT_byte_size for array type smaller "
16827 "than the total size of elements"));
16828 }
16829
16830 name = dwarf2_name (die, cu);
16831 if (name)
16832 type->set_name (name);
16833
16834 maybe_set_alignment (cu, die, type);
16835
16836 /* Install the type in the die. */
16837 set_die_type (die, type, cu);
16838
16839 /* set_die_type should be already done. */
16840 set_descriptive_type (type, die, cu);
16841
16842 return type;
16843 }
16844
16845 static enum dwarf_array_dim_ordering
16846 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16847 {
16848 struct attribute *attr;
16849
16850 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16851
16852 if (attr != nullptr)
16853 {
16854 LONGEST val = attr->constant_value (-1);
16855 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
16856 return (enum dwarf_array_dim_ordering) val;
16857 }
16858
16859 /* GNU F77 is a special case, as at 08/2004 array type info is the
16860 opposite order to the dwarf2 specification, but data is still
16861 laid out as per normal fortran.
16862
16863 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16864 version checking. */
16865
16866 if (cu->language == language_fortran
16867 && cu->producer && strstr (cu->producer, "GNU F77"))
16868 {
16869 return DW_ORD_row_major;
16870 }
16871
16872 switch (cu->language_defn->array_ordering ())
16873 {
16874 case array_column_major:
16875 return DW_ORD_col_major;
16876 case array_row_major:
16877 default:
16878 return DW_ORD_row_major;
16879 };
16880 }
16881
16882 /* Extract all information from a DW_TAG_set_type DIE and put it in
16883 the DIE's type field. */
16884
16885 static struct type *
16886 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16887 {
16888 struct type *domain_type, *set_type;
16889 struct attribute *attr;
16890
16891 domain_type = die_type (die, cu);
16892
16893 /* The die_type call above may have already set the type for this DIE. */
16894 set_type = get_die_type (die, cu);
16895 if (set_type)
16896 return set_type;
16897
16898 set_type = create_set_type (NULL, domain_type);
16899
16900 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16901 if (attr != nullptr && attr->form_is_unsigned ())
16902 TYPE_LENGTH (set_type) = attr->as_unsigned ();
16903
16904 maybe_set_alignment (cu, die, set_type);
16905
16906 return set_die_type (die, set_type, cu);
16907 }
16908
16909 /* A helper for read_common_block that creates a locexpr baton.
16910 SYM is the symbol which we are marking as computed.
16911 COMMON_DIE is the DIE for the common block.
16912 COMMON_LOC is the location expression attribute for the common
16913 block itself.
16914 MEMBER_LOC is the location expression attribute for the particular
16915 member of the common block that we are processing.
16916 CU is the CU from which the above come. */
16917
16918 static void
16919 mark_common_block_symbol_computed (struct symbol *sym,
16920 struct die_info *common_die,
16921 struct attribute *common_loc,
16922 struct attribute *member_loc,
16923 struct dwarf2_cu *cu)
16924 {
16925 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16926 struct objfile *objfile = per_objfile->objfile;
16927 struct dwarf2_locexpr_baton *baton;
16928 gdb_byte *ptr;
16929 unsigned int cu_off;
16930 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16931 LONGEST offset = 0;
16932
16933 gdb_assert (common_loc && member_loc);
16934 gdb_assert (common_loc->form_is_block ());
16935 gdb_assert (member_loc->form_is_block ()
16936 || member_loc->form_is_constant ());
16937
16938 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16939 baton->per_objfile = per_objfile;
16940 baton->per_cu = cu->per_cu;
16941 gdb_assert (baton->per_cu);
16942
16943 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16944
16945 if (member_loc->form_is_constant ())
16946 {
16947 offset = member_loc->constant_value (0);
16948 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16949 }
16950 else
16951 baton->size += member_loc->as_block ()->size;
16952
16953 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16954 baton->data = ptr;
16955
16956 *ptr++ = DW_OP_call4;
16957 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16958 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16959 ptr += 4;
16960
16961 if (member_loc->form_is_constant ())
16962 {
16963 *ptr++ = DW_OP_addr;
16964 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16965 ptr += cu->header.addr_size;
16966 }
16967 else
16968 {
16969 /* We have to copy the data here, because DW_OP_call4 will only
16970 use a DW_AT_location attribute. */
16971 struct dwarf_block *block = member_loc->as_block ();
16972 memcpy (ptr, block->data, block->size);
16973 ptr += block->size;
16974 }
16975
16976 *ptr++ = DW_OP_plus;
16977 gdb_assert (ptr - baton->data == baton->size);
16978
16979 SYMBOL_LOCATION_BATON (sym) = baton;
16980 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16981 }
16982
16983 /* Create appropriate locally-scoped variables for all the
16984 DW_TAG_common_block entries. Also create a struct common_block
16985 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16986 is used to separate the common blocks name namespace from regular
16987 variable names. */
16988
16989 static void
16990 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16991 {
16992 struct attribute *attr;
16993
16994 attr = dwarf2_attr (die, DW_AT_location, cu);
16995 if (attr != nullptr)
16996 {
16997 /* Support the .debug_loc offsets. */
16998 if (attr->form_is_block ())
16999 {
17000 /* Ok. */
17001 }
17002 else if (attr->form_is_section_offset ())
17003 {
17004 dwarf2_complex_location_expr_complaint ();
17005 attr = NULL;
17006 }
17007 else
17008 {
17009 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17010 "common block member");
17011 attr = NULL;
17012 }
17013 }
17014
17015 if (die->child != NULL)
17016 {
17017 struct objfile *objfile = cu->per_objfile->objfile;
17018 struct die_info *child_die;
17019 size_t n_entries = 0, size;
17020 struct common_block *common_block;
17021 struct symbol *sym;
17022
17023 for (child_die = die->child;
17024 child_die && child_die->tag;
17025 child_die = child_die->sibling)
17026 ++n_entries;
17027
17028 size = (sizeof (struct common_block)
17029 + (n_entries - 1) * sizeof (struct symbol *));
17030 common_block
17031 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17032 size);
17033 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17034 common_block->n_entries = 0;
17035
17036 for (child_die = die->child;
17037 child_die && child_die->tag;
17038 child_die = child_die->sibling)
17039 {
17040 /* Create the symbol in the DW_TAG_common_block block in the current
17041 symbol scope. */
17042 sym = new_symbol (child_die, NULL, cu);
17043 if (sym != NULL)
17044 {
17045 struct attribute *member_loc;
17046
17047 common_block->contents[common_block->n_entries++] = sym;
17048
17049 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17050 cu);
17051 if (member_loc)
17052 {
17053 /* GDB has handled this for a long time, but it is
17054 not specified by DWARF. It seems to have been
17055 emitted by gfortran at least as recently as:
17056 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17057 complaint (_("Variable in common block has "
17058 "DW_AT_data_member_location "
17059 "- DIE at %s [in module %s]"),
17060 sect_offset_str (child_die->sect_off),
17061 objfile_name (objfile));
17062
17063 if (member_loc->form_is_section_offset ())
17064 dwarf2_complex_location_expr_complaint ();
17065 else if (member_loc->form_is_constant ()
17066 || member_loc->form_is_block ())
17067 {
17068 if (attr != nullptr)
17069 mark_common_block_symbol_computed (sym, die, attr,
17070 member_loc, cu);
17071 }
17072 else
17073 dwarf2_complex_location_expr_complaint ();
17074 }
17075 }
17076 }
17077
17078 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17079 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
17080 }
17081 }
17082
17083 /* Create a type for a C++ namespace. */
17084
17085 static struct type *
17086 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
17087 {
17088 struct objfile *objfile = cu->per_objfile->objfile;
17089 const char *previous_prefix, *name;
17090 int is_anonymous;
17091 struct type *type;
17092
17093 /* For extensions, reuse the type of the original namespace. */
17094 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17095 {
17096 struct die_info *ext_die;
17097 struct dwarf2_cu *ext_cu = cu;
17098
17099 ext_die = dwarf2_extension (die, &ext_cu);
17100 type = read_type_die (ext_die, ext_cu);
17101
17102 /* EXT_CU may not be the same as CU.
17103 Ensure TYPE is recorded with CU in die_type_hash. */
17104 return set_die_type (die, type, cu);
17105 }
17106
17107 name = namespace_name (die, &is_anonymous, cu);
17108
17109 /* Now build the name of the current namespace. */
17110
17111 previous_prefix = determine_prefix (die, cu);
17112 if (previous_prefix[0] != '\0')
17113 name = typename_concat (&objfile->objfile_obstack,
17114 previous_prefix, name, 0, cu);
17115
17116 /* Create the type. */
17117 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17118
17119 return set_die_type (die, type, cu);
17120 }
17121
17122 /* Read a namespace scope. */
17123
17124 static void
17125 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17126 {
17127 struct objfile *objfile = cu->per_objfile->objfile;
17128 int is_anonymous;
17129
17130 /* Add a symbol associated to this if we haven't seen the namespace
17131 before. Also, add a using directive if it's an anonymous
17132 namespace. */
17133
17134 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17135 {
17136 struct type *type;
17137
17138 type = read_type_die (die, cu);
17139 new_symbol (die, type, cu);
17140
17141 namespace_name (die, &is_anonymous, cu);
17142 if (is_anonymous)
17143 {
17144 const char *previous_prefix = determine_prefix (die, cu);
17145
17146 std::vector<const char *> excludes;
17147 add_using_directive (using_directives (cu),
17148 previous_prefix, type->name (), NULL,
17149 NULL, excludes, 0, &objfile->objfile_obstack);
17150 }
17151 }
17152
17153 if (die->child != NULL)
17154 {
17155 struct die_info *child_die = die->child;
17156
17157 while (child_die && child_die->tag)
17158 {
17159 process_die (child_die, cu);
17160 child_die = child_die->sibling;
17161 }
17162 }
17163 }
17164
17165 /* Read a Fortran module as type. This DIE can be only a declaration used for
17166 imported module. Still we need that type as local Fortran "use ... only"
17167 declaration imports depend on the created type in determine_prefix. */
17168
17169 static struct type *
17170 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17171 {
17172 struct objfile *objfile = cu->per_objfile->objfile;
17173 const char *module_name;
17174 struct type *type;
17175
17176 module_name = dwarf2_name (die, cu);
17177 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17178
17179 return set_die_type (die, type, cu);
17180 }
17181
17182 /* Read a Fortran module. */
17183
17184 static void
17185 read_module (struct die_info *die, struct dwarf2_cu *cu)
17186 {
17187 struct die_info *child_die = die->child;
17188 struct type *type;
17189
17190 type = read_type_die (die, cu);
17191 new_symbol (die, type, cu);
17192
17193 while (child_die && child_die->tag)
17194 {
17195 process_die (child_die, cu);
17196 child_die = child_die->sibling;
17197 }
17198 }
17199
17200 /* Return the name of the namespace represented by DIE. Set
17201 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17202 namespace. */
17203
17204 static const char *
17205 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17206 {
17207 struct die_info *current_die;
17208 const char *name = NULL;
17209
17210 /* Loop through the extensions until we find a name. */
17211
17212 for (current_die = die;
17213 current_die != NULL;
17214 current_die = dwarf2_extension (die, &cu))
17215 {
17216 /* We don't use dwarf2_name here so that we can detect the absence
17217 of a name -> anonymous namespace. */
17218 name = dwarf2_string_attr (die, DW_AT_name, cu);
17219
17220 if (name != NULL)
17221 break;
17222 }
17223
17224 /* Is it an anonymous namespace? */
17225
17226 *is_anonymous = (name == NULL);
17227 if (*is_anonymous)
17228 name = CP_ANONYMOUS_NAMESPACE_STR;
17229
17230 return name;
17231 }
17232
17233 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17234 the user defined type vector. */
17235
17236 static struct type *
17237 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17238 {
17239 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
17240 struct comp_unit_head *cu_header = &cu->header;
17241 struct type *type;
17242 struct attribute *attr_byte_size;
17243 struct attribute *attr_address_class;
17244 int byte_size, addr_class;
17245 struct type *target_type;
17246
17247 target_type = die_type (die, cu);
17248
17249 /* The die_type call above may have already set the type for this DIE. */
17250 type = get_die_type (die, cu);
17251 if (type)
17252 return type;
17253
17254 type = lookup_pointer_type (target_type);
17255
17256 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17257 if (attr_byte_size)
17258 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
17259 else
17260 byte_size = cu_header->addr_size;
17261
17262 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17263 if (attr_address_class)
17264 addr_class = attr_address_class->constant_value (DW_ADDR_none);
17265 else
17266 addr_class = DW_ADDR_none;
17267
17268 ULONGEST alignment = get_alignment (cu, die);
17269
17270 /* If the pointer size, alignment, or address class is different
17271 than the default, create a type variant marked as such and set
17272 the length accordingly. */
17273 if (TYPE_LENGTH (type) != byte_size
17274 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17275 && alignment != TYPE_RAW_ALIGN (type))
17276 || addr_class != DW_ADDR_none)
17277 {
17278 if (gdbarch_address_class_type_flags_p (gdbarch))
17279 {
17280 type_instance_flags type_flags
17281 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17282 addr_class);
17283 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17284 == 0);
17285 type = make_type_with_address_space (type, type_flags);
17286 }
17287 else if (TYPE_LENGTH (type) != byte_size)
17288 {
17289 complaint (_("invalid pointer size %d"), byte_size);
17290 }
17291 else if (TYPE_RAW_ALIGN (type) != alignment)
17292 {
17293 complaint (_("Invalid DW_AT_alignment"
17294 " - DIE at %s [in module %s]"),
17295 sect_offset_str (die->sect_off),
17296 objfile_name (cu->per_objfile->objfile));
17297 }
17298 else
17299 {
17300 /* Should we also complain about unhandled address classes? */
17301 }
17302 }
17303
17304 TYPE_LENGTH (type) = byte_size;
17305 set_type_align (type, alignment);
17306 return set_die_type (die, type, cu);
17307 }
17308
17309 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17310 the user defined type vector. */
17311
17312 static struct type *
17313 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17314 {
17315 struct type *type;
17316 struct type *to_type;
17317 struct type *domain;
17318
17319 to_type = die_type (die, cu);
17320 domain = die_containing_type (die, cu);
17321
17322 /* The calls above may have already set the type for this DIE. */
17323 type = get_die_type (die, cu);
17324 if (type)
17325 return type;
17326
17327 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17328 type = lookup_methodptr_type (to_type);
17329 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17330 {
17331 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17332
17333 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17334 to_type->fields (), to_type->num_fields (),
17335 to_type->has_varargs ());
17336 type = lookup_methodptr_type (new_type);
17337 }
17338 else
17339 type = lookup_memberptr_type (to_type, domain);
17340
17341 return set_die_type (die, type, cu);
17342 }
17343
17344 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17345 the user defined type vector. */
17346
17347 static struct type *
17348 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17349 enum type_code refcode)
17350 {
17351 struct comp_unit_head *cu_header = &cu->header;
17352 struct type *type, *target_type;
17353 struct attribute *attr;
17354
17355 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17356
17357 target_type = die_type (die, cu);
17358
17359 /* The die_type call above may have already set the type for this DIE. */
17360 type = get_die_type (die, cu);
17361 if (type)
17362 return type;
17363
17364 type = lookup_reference_type (target_type, refcode);
17365 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17366 if (attr != nullptr)
17367 {
17368 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
17369 }
17370 else
17371 {
17372 TYPE_LENGTH (type) = cu_header->addr_size;
17373 }
17374 maybe_set_alignment (cu, die, type);
17375 return set_die_type (die, type, cu);
17376 }
17377
17378 /* Add the given cv-qualifiers to the element type of the array. GCC
17379 outputs DWARF type qualifiers that apply to an array, not the
17380 element type. But GDB relies on the array element type to carry
17381 the cv-qualifiers. This mimics section 6.7.3 of the C99
17382 specification. */
17383
17384 static struct type *
17385 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17386 struct type *base_type, int cnst, int voltl)
17387 {
17388 struct type *el_type, *inner_array;
17389
17390 base_type = copy_type (base_type);
17391 inner_array = base_type;
17392
17393 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17394 {
17395 TYPE_TARGET_TYPE (inner_array) =
17396 copy_type (TYPE_TARGET_TYPE (inner_array));
17397 inner_array = TYPE_TARGET_TYPE (inner_array);
17398 }
17399
17400 el_type = TYPE_TARGET_TYPE (inner_array);
17401 cnst |= TYPE_CONST (el_type);
17402 voltl |= TYPE_VOLATILE (el_type);
17403 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17404
17405 return set_die_type (die, base_type, cu);
17406 }
17407
17408 static struct type *
17409 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17410 {
17411 struct type *base_type, *cv_type;
17412
17413 base_type = die_type (die, cu);
17414
17415 /* The die_type call above may have already set the type for this DIE. */
17416 cv_type = get_die_type (die, cu);
17417 if (cv_type)
17418 return cv_type;
17419
17420 /* In case the const qualifier is applied to an array type, the element type
17421 is so qualified, not the array type (section 6.7.3 of C99). */
17422 if (base_type->code () == TYPE_CODE_ARRAY)
17423 return add_array_cv_type (die, cu, base_type, 1, 0);
17424
17425 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17426 return set_die_type (die, cv_type, cu);
17427 }
17428
17429 static struct type *
17430 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17431 {
17432 struct type *base_type, *cv_type;
17433
17434 base_type = die_type (die, cu);
17435
17436 /* The die_type call above may have already set the type for this DIE. */
17437 cv_type = get_die_type (die, cu);
17438 if (cv_type)
17439 return cv_type;
17440
17441 /* In case the volatile qualifier is applied to an array type, the
17442 element type is so qualified, not the array type (section 6.7.3
17443 of C99). */
17444 if (base_type->code () == TYPE_CODE_ARRAY)
17445 return add_array_cv_type (die, cu, base_type, 0, 1);
17446
17447 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17448 return set_die_type (die, cv_type, cu);
17449 }
17450
17451 /* Handle DW_TAG_restrict_type. */
17452
17453 static struct type *
17454 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17455 {
17456 struct type *base_type, *cv_type;
17457
17458 base_type = die_type (die, cu);
17459
17460 /* The die_type call above may have already set the type for this DIE. */
17461 cv_type = get_die_type (die, cu);
17462 if (cv_type)
17463 return cv_type;
17464
17465 cv_type = make_restrict_type (base_type);
17466 return set_die_type (die, cv_type, cu);
17467 }
17468
17469 /* Handle DW_TAG_atomic_type. */
17470
17471 static struct type *
17472 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17473 {
17474 struct type *base_type, *cv_type;
17475
17476 base_type = die_type (die, cu);
17477
17478 /* The die_type call above may have already set the type for this DIE. */
17479 cv_type = get_die_type (die, cu);
17480 if (cv_type)
17481 return cv_type;
17482
17483 cv_type = make_atomic_type (base_type);
17484 return set_die_type (die, cv_type, cu);
17485 }
17486
17487 /* Extract all information from a DW_TAG_string_type DIE and add to
17488 the user defined type vector. It isn't really a user defined type,
17489 but it behaves like one, with other DIE's using an AT_user_def_type
17490 attribute to reference it. */
17491
17492 static struct type *
17493 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17494 {
17495 struct objfile *objfile = cu->per_objfile->objfile;
17496 struct gdbarch *gdbarch = objfile->arch ();
17497 struct type *type, *range_type, *index_type, *char_type;
17498 struct attribute *attr;
17499 struct dynamic_prop prop;
17500 bool length_is_constant = true;
17501 LONGEST length;
17502
17503 /* There are a couple of places where bit sizes might be made use of
17504 when parsing a DW_TAG_string_type, however, no producer that we know
17505 of make use of these. Handling bit sizes that are a multiple of the
17506 byte size is easy enough, but what about other bit sizes? Lets deal
17507 with that problem when we have to. Warn about these attributes being
17508 unsupported, then parse the type and ignore them like we always
17509 have. */
17510 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17511 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17512 {
17513 static bool warning_printed = false;
17514 if (!warning_printed)
17515 {
17516 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17517 "currently supported on DW_TAG_string_type."));
17518 warning_printed = true;
17519 }
17520 }
17521
17522 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17523 if (attr != nullptr && !attr->form_is_constant ())
17524 {
17525 /* The string length describes the location at which the length of
17526 the string can be found. The size of the length field can be
17527 specified with one of the attributes below. */
17528 struct type *prop_type;
17529 struct attribute *len
17530 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17531 if (len == nullptr)
17532 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17533 if (len != nullptr && len->form_is_constant ())
17534 {
17535 /* Pass 0 as the default as we know this attribute is constant
17536 and the default value will not be returned. */
17537 LONGEST sz = len->constant_value (0);
17538 prop_type = cu->per_objfile->int_type (sz, true);
17539 }
17540 else
17541 {
17542 /* If the size is not specified then we assume it is the size of
17543 an address on this target. */
17544 prop_type = cu->addr_sized_int_type (true);
17545 }
17546
17547 /* Convert the attribute into a dynamic property. */
17548 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17549 length = 1;
17550 else
17551 length_is_constant = false;
17552 }
17553 else if (attr != nullptr)
17554 {
17555 /* This DW_AT_string_length just contains the length with no
17556 indirection. There's no need to create a dynamic property in this
17557 case. Pass 0 for the default value as we know it will not be
17558 returned in this case. */
17559 length = attr->constant_value (0);
17560 }
17561 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17562 {
17563 /* We don't currently support non-constant byte sizes for strings. */
17564 length = attr->constant_value (1);
17565 }
17566 else
17567 {
17568 /* Use 1 as a fallback length if we have nothing else. */
17569 length = 1;
17570 }
17571
17572 index_type = objfile_type (objfile)->builtin_int;
17573 if (length_is_constant)
17574 range_type = create_static_range_type (NULL, index_type, 1, length);
17575 else
17576 {
17577 struct dynamic_prop low_bound;
17578
17579 low_bound.set_const_val (1);
17580 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17581 }
17582 char_type = language_string_char_type (cu->language_defn, gdbarch);
17583 type = create_string_type (NULL, char_type, range_type);
17584
17585 return set_die_type (die, type, cu);
17586 }
17587
17588 /* Assuming that DIE corresponds to a function, returns nonzero
17589 if the function is prototyped. */
17590
17591 static int
17592 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17593 {
17594 struct attribute *attr;
17595
17596 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17597 if (attr && attr->as_boolean ())
17598 return 1;
17599
17600 /* The DWARF standard implies that the DW_AT_prototyped attribute
17601 is only meaningful for C, but the concept also extends to other
17602 languages that allow unprototyped functions (Eg: Objective C).
17603 For all other languages, assume that functions are always
17604 prototyped. */
17605 if (cu->language != language_c
17606 && cu->language != language_objc
17607 && cu->language != language_opencl)
17608 return 1;
17609
17610 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17611 prototyped and unprototyped functions; default to prototyped,
17612 since that is more common in modern code (and RealView warns
17613 about unprototyped functions). */
17614 if (producer_is_realview (cu->producer))
17615 return 1;
17616
17617 return 0;
17618 }
17619
17620 /* Handle DIES due to C code like:
17621
17622 struct foo
17623 {
17624 int (*funcp)(int a, long l);
17625 int b;
17626 };
17627
17628 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17629
17630 static struct type *
17631 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17632 {
17633 struct objfile *objfile = cu->per_objfile->objfile;
17634 struct type *type; /* Type that this function returns. */
17635 struct type *ftype; /* Function that returns above type. */
17636 struct attribute *attr;
17637
17638 type = die_type (die, cu);
17639
17640 /* The die_type call above may have already set the type for this DIE. */
17641 ftype = get_die_type (die, cu);
17642 if (ftype)
17643 return ftype;
17644
17645 ftype = lookup_function_type (type);
17646
17647 if (prototyped_function_p (die, cu))
17648 ftype->set_is_prototyped (true);
17649
17650 /* Store the calling convention in the type if it's available in
17651 the subroutine die. Otherwise set the calling convention to
17652 the default value DW_CC_normal. */
17653 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17654 if (attr != nullptr
17655 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
17656 TYPE_CALLING_CONVENTION (ftype)
17657 = (enum dwarf_calling_convention) attr->constant_value (0);
17658 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17659 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17660 else
17661 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17662
17663 /* Record whether the function returns normally to its caller or not
17664 if the DWARF producer set that information. */
17665 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17666 if (attr && attr->as_boolean ())
17667 TYPE_NO_RETURN (ftype) = 1;
17668
17669 /* We need to add the subroutine type to the die immediately so
17670 we don't infinitely recurse when dealing with parameters
17671 declared as the same subroutine type. */
17672 set_die_type (die, ftype, cu);
17673
17674 if (die->child != NULL)
17675 {
17676 struct type *void_type = objfile_type (objfile)->builtin_void;
17677 struct die_info *child_die;
17678 int nparams, iparams;
17679
17680 /* Count the number of parameters.
17681 FIXME: GDB currently ignores vararg functions, but knows about
17682 vararg member functions. */
17683 nparams = 0;
17684 child_die = die->child;
17685 while (child_die && child_die->tag)
17686 {
17687 if (child_die->tag == DW_TAG_formal_parameter)
17688 nparams++;
17689 else if (child_die->tag == DW_TAG_unspecified_parameters)
17690 ftype->set_has_varargs (true);
17691
17692 child_die = child_die->sibling;
17693 }
17694
17695 /* Allocate storage for parameters and fill them in. */
17696 ftype->set_num_fields (nparams);
17697 ftype->set_fields
17698 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17699
17700 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17701 even if we error out during the parameters reading below. */
17702 for (iparams = 0; iparams < nparams; iparams++)
17703 ftype->field (iparams).set_type (void_type);
17704
17705 iparams = 0;
17706 child_die = die->child;
17707 while (child_die && child_die->tag)
17708 {
17709 if (child_die->tag == DW_TAG_formal_parameter)
17710 {
17711 struct type *arg_type;
17712
17713 /* DWARF version 2 has no clean way to discern C++
17714 static and non-static member functions. G++ helps
17715 GDB by marking the first parameter for non-static
17716 member functions (which is the this pointer) as
17717 artificial. We pass this information to
17718 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17719
17720 DWARF version 3 added DW_AT_object_pointer, which GCC
17721 4.5 does not yet generate. */
17722 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17723 if (attr != nullptr)
17724 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
17725 else
17726 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17727 arg_type = die_type (child_die, cu);
17728
17729 /* RealView does not mark THIS as const, which the testsuite
17730 expects. GCC marks THIS as const in method definitions,
17731 but not in the class specifications (GCC PR 43053). */
17732 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17733 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17734 {
17735 int is_this = 0;
17736 struct dwarf2_cu *arg_cu = cu;
17737 const char *name = dwarf2_name (child_die, cu);
17738
17739 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17740 if (attr != nullptr)
17741 {
17742 /* If the compiler emits this, use it. */
17743 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17744 is_this = 1;
17745 }
17746 else if (name && strcmp (name, "this") == 0)
17747 /* Function definitions will have the argument names. */
17748 is_this = 1;
17749 else if (name == NULL && iparams == 0)
17750 /* Declarations may not have the names, so like
17751 elsewhere in GDB, assume an artificial first
17752 argument is "this". */
17753 is_this = 1;
17754
17755 if (is_this)
17756 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17757 arg_type, 0);
17758 }
17759
17760 ftype->field (iparams).set_type (arg_type);
17761 iparams++;
17762 }
17763 child_die = child_die->sibling;
17764 }
17765 }
17766
17767 return ftype;
17768 }
17769
17770 static struct type *
17771 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17772 {
17773 struct objfile *objfile = cu->per_objfile->objfile;
17774 const char *name = NULL;
17775 struct type *this_type, *target_type;
17776
17777 name = dwarf2_full_name (NULL, die, cu);
17778 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17779 this_type->set_target_is_stub (true);
17780 set_die_type (die, this_type, cu);
17781 target_type = die_type (die, cu);
17782 if (target_type != this_type)
17783 TYPE_TARGET_TYPE (this_type) = target_type;
17784 else
17785 {
17786 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17787 spec and cause infinite loops in GDB. */
17788 complaint (_("Self-referential DW_TAG_typedef "
17789 "- DIE at %s [in module %s]"),
17790 sect_offset_str (die->sect_off), objfile_name (objfile));
17791 TYPE_TARGET_TYPE (this_type) = NULL;
17792 }
17793 if (name == NULL)
17794 {
17795 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17796 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17797 Handle these by just returning the target type, rather than
17798 constructing an anonymous typedef type and trying to handle this
17799 elsewhere. */
17800 set_die_type (die, target_type, cu);
17801 return target_type;
17802 }
17803 return this_type;
17804 }
17805
17806 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17807 (which may be different from NAME) to the architecture back-end to allow
17808 it to guess the correct format if necessary. */
17809
17810 static struct type *
17811 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17812 const char *name_hint, enum bfd_endian byte_order)
17813 {
17814 struct gdbarch *gdbarch = objfile->arch ();
17815 const struct floatformat **format;
17816 struct type *type;
17817
17818 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17819 if (format)
17820 type = init_float_type (objfile, bits, name, format, byte_order);
17821 else
17822 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17823
17824 return type;
17825 }
17826
17827 /* Allocate an integer type of size BITS and name NAME. */
17828
17829 static struct type *
17830 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17831 int bits, int unsigned_p, const char *name)
17832 {
17833 struct type *type;
17834
17835 /* Versions of Intel's C Compiler generate an integer type called "void"
17836 instead of using DW_TAG_unspecified_type. This has been seen on
17837 at least versions 14, 17, and 18. */
17838 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17839 && strcmp (name, "void") == 0)
17840 type = objfile_type (objfile)->builtin_void;
17841 else
17842 type = init_integer_type (objfile, bits, unsigned_p, name);
17843
17844 return type;
17845 }
17846
17847 /* Initialise and return a floating point type of size BITS suitable for
17848 use as a component of a complex number. The NAME_HINT is passed through
17849 when initialising the floating point type and is the name of the complex
17850 type.
17851
17852 As DWARF doesn't currently provide an explicit name for the components
17853 of a complex number, but it can be helpful to have these components
17854 named, we try to select a suitable name based on the size of the
17855 component. */
17856 static struct type *
17857 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17858 struct objfile *objfile,
17859 int bits, const char *name_hint,
17860 enum bfd_endian byte_order)
17861 {
17862 gdbarch *gdbarch = objfile->arch ();
17863 struct type *tt = nullptr;
17864
17865 /* Try to find a suitable floating point builtin type of size BITS.
17866 We're going to use the name of this type as the name for the complex
17867 target type that we are about to create. */
17868 switch (cu->language)
17869 {
17870 case language_fortran:
17871 switch (bits)
17872 {
17873 case 32:
17874 tt = builtin_f_type (gdbarch)->builtin_real;
17875 break;
17876 case 64:
17877 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17878 break;
17879 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17880 case 128:
17881 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17882 break;
17883 }
17884 break;
17885 default:
17886 switch (bits)
17887 {
17888 case 32:
17889 tt = builtin_type (gdbarch)->builtin_float;
17890 break;
17891 case 64:
17892 tt = builtin_type (gdbarch)->builtin_double;
17893 break;
17894 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17895 case 128:
17896 tt = builtin_type (gdbarch)->builtin_long_double;
17897 break;
17898 }
17899 break;
17900 }
17901
17902 /* If the type we found doesn't match the size we were looking for, then
17903 pretend we didn't find a type at all, the complex target type we
17904 create will then be nameless. */
17905 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17906 tt = nullptr;
17907
17908 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17909 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17910 }
17911
17912 /* Find a representation of a given base type and install
17913 it in the TYPE field of the die. */
17914
17915 static struct type *
17916 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17917 {
17918 struct objfile *objfile = cu->per_objfile->objfile;
17919 struct type *type;
17920 struct attribute *attr;
17921 int encoding = 0, bits = 0;
17922 const char *name;
17923 gdbarch *arch;
17924
17925 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17926 if (attr != nullptr && attr->form_is_constant ())
17927 encoding = attr->constant_value (0);
17928 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17929 if (attr != nullptr)
17930 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
17931 name = dwarf2_name (die, cu);
17932 if (!name)
17933 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17934
17935 arch = objfile->arch ();
17936 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17937
17938 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17939 if (attr != nullptr && attr->form_is_constant ())
17940 {
17941 int endianity = attr->constant_value (0);
17942
17943 switch (endianity)
17944 {
17945 case DW_END_big:
17946 byte_order = BFD_ENDIAN_BIG;
17947 break;
17948 case DW_END_little:
17949 byte_order = BFD_ENDIAN_LITTLE;
17950 break;
17951 default:
17952 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17953 break;
17954 }
17955 }
17956
17957 switch (encoding)
17958 {
17959 case DW_ATE_address:
17960 /* Turn DW_ATE_address into a void * pointer. */
17961 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17962 type = init_pointer_type (objfile, bits, name, type);
17963 break;
17964 case DW_ATE_boolean:
17965 type = init_boolean_type (objfile, bits, 1, name);
17966 break;
17967 case DW_ATE_complex_float:
17968 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17969 byte_order);
17970 if (type->code () == TYPE_CODE_ERROR)
17971 {
17972 if (name == nullptr)
17973 {
17974 struct obstack *obstack
17975 = &cu->per_objfile->objfile->objfile_obstack;
17976 name = obconcat (obstack, "_Complex ", type->name (),
17977 nullptr);
17978 }
17979 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17980 }
17981 else
17982 type = init_complex_type (name, type);
17983 break;
17984 case DW_ATE_decimal_float:
17985 type = init_decfloat_type (objfile, bits, name);
17986 break;
17987 case DW_ATE_float:
17988 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17989 break;
17990 case DW_ATE_signed:
17991 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17992 break;
17993 case DW_ATE_unsigned:
17994 if (cu->language == language_fortran
17995 && name
17996 && startswith (name, "character("))
17997 type = init_character_type (objfile, bits, 1, name);
17998 else
17999 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18000 break;
18001 case DW_ATE_signed_char:
18002 if (cu->language == language_ada || cu->language == language_m2
18003 || cu->language == language_pascal
18004 || cu->language == language_fortran)
18005 type = init_character_type (objfile, bits, 0, name);
18006 else
18007 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18008 break;
18009 case DW_ATE_unsigned_char:
18010 if (cu->language == language_ada || cu->language == language_m2
18011 || cu->language == language_pascal
18012 || cu->language == language_fortran
18013 || cu->language == language_rust)
18014 type = init_character_type (objfile, bits, 1, name);
18015 else
18016 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18017 break;
18018 case DW_ATE_UTF:
18019 {
18020 if (bits == 16)
18021 type = builtin_type (arch)->builtin_char16;
18022 else if (bits == 32)
18023 type = builtin_type (arch)->builtin_char32;
18024 else
18025 {
18026 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
18027 bits);
18028 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18029 }
18030 return set_die_type (die, type, cu);
18031 }
18032 break;
18033
18034 default:
18035 complaint (_("unsupported DW_AT_encoding: '%s'"),
18036 dwarf_type_encoding_name (encoding));
18037 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18038 break;
18039 }
18040
18041 if (name && strcmp (name, "char") == 0)
18042 type->set_has_no_signedness (true);
18043
18044 maybe_set_alignment (cu, die, type);
18045
18046 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18047
18048 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18049 {
18050 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18051 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
18052 {
18053 unsigned real_bit_size = attr->as_unsigned ();
18054 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18055 /* Only use the attributes if they make sense together. */
18056 if (attr == nullptr
18057 || (attr->as_unsigned () + real_bit_size
18058 <= 8 * TYPE_LENGTH (type)))
18059 {
18060 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18061 = real_bit_size;
18062 if (attr != nullptr)
18063 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18064 = attr->as_unsigned ();
18065 }
18066 }
18067 }
18068
18069 return set_die_type (die, type, cu);
18070 }
18071
18072 /* Parse dwarf attribute if it's a block, reference or constant and put the
18073 resulting value of the attribute into struct bound_prop.
18074 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18075
18076 static int
18077 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18078 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18079 struct type *default_type)
18080 {
18081 struct dwarf2_property_baton *baton;
18082 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18083 struct objfile *objfile = per_objfile->objfile;
18084 struct obstack *obstack = &objfile->objfile_obstack;
18085
18086 gdb_assert (default_type != NULL);
18087
18088 if (attr == NULL || prop == NULL)
18089 return 0;
18090
18091 if (attr->form_is_block ())
18092 {
18093 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18094 baton->property_type = default_type;
18095 baton->locexpr.per_cu = cu->per_cu;
18096 baton->locexpr.per_objfile = per_objfile;
18097
18098 struct dwarf_block *block = attr->as_block ();
18099 baton->locexpr.size = block->size;
18100 baton->locexpr.data = block->data;
18101 switch (attr->name)
18102 {
18103 case DW_AT_string_length:
18104 baton->locexpr.is_reference = true;
18105 break;
18106 default:
18107 baton->locexpr.is_reference = false;
18108 break;
18109 }
18110
18111 prop->set_locexpr (baton);
18112 gdb_assert (prop->baton () != NULL);
18113 }
18114 else if (attr->form_is_ref ())
18115 {
18116 struct dwarf2_cu *target_cu = cu;
18117 struct die_info *target_die;
18118 struct attribute *target_attr;
18119
18120 target_die = follow_die_ref (die, attr, &target_cu);
18121 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18122 if (target_attr == NULL)
18123 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18124 target_cu);
18125 if (target_attr == NULL)
18126 return 0;
18127
18128 switch (target_attr->name)
18129 {
18130 case DW_AT_location:
18131 if (target_attr->form_is_section_offset ())
18132 {
18133 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18134 baton->property_type = die_type (target_die, target_cu);
18135 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18136 prop->set_loclist (baton);
18137 gdb_assert (prop->baton () != NULL);
18138 }
18139 else if (target_attr->form_is_block ())
18140 {
18141 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18142 baton->property_type = die_type (target_die, target_cu);
18143 baton->locexpr.per_cu = cu->per_cu;
18144 baton->locexpr.per_objfile = per_objfile;
18145 struct dwarf_block *block = target_attr->as_block ();
18146 baton->locexpr.size = block->size;
18147 baton->locexpr.data = block->data;
18148 baton->locexpr.is_reference = true;
18149 prop->set_locexpr (baton);
18150 gdb_assert (prop->baton () != NULL);
18151 }
18152 else
18153 {
18154 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18155 "dynamic property");
18156 return 0;
18157 }
18158 break;
18159 case DW_AT_data_member_location:
18160 {
18161 LONGEST offset;
18162
18163 if (!handle_data_member_location (target_die, target_cu,
18164 &offset))
18165 return 0;
18166
18167 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18168 baton->property_type = read_type_die (target_die->parent,
18169 target_cu);
18170 baton->offset_info.offset = offset;
18171 baton->offset_info.type = die_type (target_die, target_cu);
18172 prop->set_addr_offset (baton);
18173 break;
18174 }
18175 }
18176 }
18177 else if (attr->form_is_constant ())
18178 prop->set_const_val (attr->constant_value (0));
18179 else
18180 {
18181 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18182 dwarf2_name (die, cu));
18183 return 0;
18184 }
18185
18186 return 1;
18187 }
18188
18189 /* See read.h. */
18190
18191 struct type *
18192 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
18193 {
18194 struct type *int_type;
18195
18196 /* Helper macro to examine the various builtin types. */
18197 #define TRY_TYPE(F) \
18198 int_type = (unsigned_p \
18199 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18200 : objfile_type (objfile)->builtin_ ## F); \
18201 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
18202 return int_type
18203
18204 TRY_TYPE (char);
18205 TRY_TYPE (short);
18206 TRY_TYPE (int);
18207 TRY_TYPE (long);
18208 TRY_TYPE (long_long);
18209
18210 #undef TRY_TYPE
18211
18212 gdb_assert_not_reached ("unable to find suitable integer type");
18213 }
18214
18215 /* See read.h. */
18216
18217 struct type *
18218 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
18219 {
18220 int addr_size = this->per_cu->addr_size ();
18221 return this->per_objfile->int_type (addr_size, unsigned_p);
18222 }
18223
18224 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18225 present (which is valid) then compute the default type based on the
18226 compilation units address size. */
18227
18228 static struct type *
18229 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18230 {
18231 struct type *index_type = die_type (die, cu);
18232
18233 /* Dwarf-2 specifications explicitly allows to create subrange types
18234 without specifying a base type.
18235 In that case, the base type must be set to the type of
18236 the lower bound, upper bound or count, in that order, if any of these
18237 three attributes references an object that has a type.
18238 If no base type is found, the Dwarf-2 specifications say that
18239 a signed integer type of size equal to the size of an address should
18240 be used.
18241 For the following C code: `extern char gdb_int [];'
18242 GCC produces an empty range DIE.
18243 FIXME: muller/2010-05-28: Possible references to object for low bound,
18244 high bound or count are not yet handled by this code. */
18245 if (index_type->code () == TYPE_CODE_VOID)
18246 index_type = cu->addr_sized_int_type (false);
18247
18248 return index_type;
18249 }
18250
18251 /* Read the given DW_AT_subrange DIE. */
18252
18253 static struct type *
18254 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18255 {
18256 struct type *base_type, *orig_base_type;
18257 struct type *range_type;
18258 struct attribute *attr;
18259 struct dynamic_prop low, high;
18260 int low_default_is_valid;
18261 int high_bound_is_count = 0;
18262 const char *name;
18263 ULONGEST negative_mask;
18264
18265 orig_base_type = read_subrange_index_type (die, cu);
18266
18267 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18268 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18269 creating the range type, but we use the result of check_typedef
18270 when examining properties of the type. */
18271 base_type = check_typedef (orig_base_type);
18272
18273 /* The die_type call above may have already set the type for this DIE. */
18274 range_type = get_die_type (die, cu);
18275 if (range_type)
18276 return range_type;
18277
18278 high.set_const_val (0);
18279
18280 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18281 omitting DW_AT_lower_bound. */
18282 switch (cu->language)
18283 {
18284 case language_c:
18285 case language_cplus:
18286 low.set_const_val (0);
18287 low_default_is_valid = 1;
18288 break;
18289 case language_fortran:
18290 low.set_const_val (1);
18291 low_default_is_valid = 1;
18292 break;
18293 case language_d:
18294 case language_objc:
18295 case language_rust:
18296 low.set_const_val (0);
18297 low_default_is_valid = (cu->header.version >= 4);
18298 break;
18299 case language_ada:
18300 case language_m2:
18301 case language_pascal:
18302 low.set_const_val (1);
18303 low_default_is_valid = (cu->header.version >= 4);
18304 break;
18305 default:
18306 low.set_const_val (0);
18307 low_default_is_valid = 0;
18308 break;
18309 }
18310
18311 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18312 if (attr != nullptr)
18313 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18314 else if (!low_default_is_valid)
18315 complaint (_("Missing DW_AT_lower_bound "
18316 "- DIE at %s [in module %s]"),
18317 sect_offset_str (die->sect_off),
18318 objfile_name (cu->per_objfile->objfile));
18319
18320 struct attribute *attr_ub, *attr_count;
18321 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18322 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18323 {
18324 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18325 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18326 {
18327 /* If bounds are constant do the final calculation here. */
18328 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18329 high.set_const_val (low.const_val () + high.const_val () - 1);
18330 else
18331 high_bound_is_count = 1;
18332 }
18333 else
18334 {
18335 if (attr_ub != NULL)
18336 complaint (_("Unresolved DW_AT_upper_bound "
18337 "- DIE at %s [in module %s]"),
18338 sect_offset_str (die->sect_off),
18339 objfile_name (cu->per_objfile->objfile));
18340 if (attr_count != NULL)
18341 complaint (_("Unresolved DW_AT_count "
18342 "- DIE at %s [in module %s]"),
18343 sect_offset_str (die->sect_off),
18344 objfile_name (cu->per_objfile->objfile));
18345 }
18346 }
18347
18348 LONGEST bias = 0;
18349 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18350 if (bias_attr != nullptr && bias_attr->form_is_constant ())
18351 bias = bias_attr->constant_value (0);
18352
18353 /* Normally, the DWARF producers are expected to use a signed
18354 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18355 But this is unfortunately not always the case, as witnessed
18356 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18357 is used instead. To work around that ambiguity, we treat
18358 the bounds as signed, and thus sign-extend their values, when
18359 the base type is signed. */
18360 negative_mask =
18361 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18362 if (low.kind () == PROP_CONST
18363 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
18364 low.set_const_val (low.const_val () | negative_mask);
18365 if (high.kind () == PROP_CONST
18366 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
18367 high.set_const_val (high.const_val () | negative_mask);
18368
18369 /* Check for bit and byte strides. */
18370 struct dynamic_prop byte_stride_prop;
18371 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18372 if (attr_byte_stride != nullptr)
18373 {
18374 struct type *prop_type = cu->addr_sized_int_type (false);
18375 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18376 prop_type);
18377 }
18378
18379 struct dynamic_prop bit_stride_prop;
18380 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18381 if (attr_bit_stride != nullptr)
18382 {
18383 /* It only makes sense to have either a bit or byte stride. */
18384 if (attr_byte_stride != nullptr)
18385 {
18386 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18387 "- DIE at %s [in module %s]"),
18388 sect_offset_str (die->sect_off),
18389 objfile_name (cu->per_objfile->objfile));
18390 attr_bit_stride = nullptr;
18391 }
18392 else
18393 {
18394 struct type *prop_type = cu->addr_sized_int_type (false);
18395 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18396 prop_type);
18397 }
18398 }
18399
18400 if (attr_byte_stride != nullptr
18401 || attr_bit_stride != nullptr)
18402 {
18403 bool byte_stride_p = (attr_byte_stride != nullptr);
18404 struct dynamic_prop *stride
18405 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18406
18407 range_type
18408 = create_range_type_with_stride (NULL, orig_base_type, &low,
18409 &high, bias, stride, byte_stride_p);
18410 }
18411 else
18412 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18413
18414 if (high_bound_is_count)
18415 range_type->bounds ()->flag_upper_bound_is_count = 1;
18416
18417 /* Ada expects an empty array on no boundary attributes. */
18418 if (attr == NULL && cu->language != language_ada)
18419 range_type->bounds ()->high.set_undefined ();
18420
18421 name = dwarf2_name (die, cu);
18422 if (name)
18423 range_type->set_name (name);
18424
18425 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18426 if (attr != nullptr)
18427 TYPE_LENGTH (range_type) = attr->constant_value (0);
18428
18429 maybe_set_alignment (cu, die, range_type);
18430
18431 set_die_type (die, range_type, cu);
18432
18433 /* set_die_type should be already done. */
18434 set_descriptive_type (range_type, die, cu);
18435
18436 return range_type;
18437 }
18438
18439 static struct type *
18440 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18441 {
18442 struct type *type;
18443
18444 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
18445 type->set_name (dwarf2_name (die, cu));
18446
18447 /* In Ada, an unspecified type is typically used when the description
18448 of the type is deferred to a different unit. When encountering
18449 such a type, we treat it as a stub, and try to resolve it later on,
18450 when needed. */
18451 if (cu->language == language_ada)
18452 type->set_is_stub (true);
18453
18454 return set_die_type (die, type, cu);
18455 }
18456
18457 /* Read a single die and all its descendents. Set the die's sibling
18458 field to NULL; set other fields in the die correctly, and set all
18459 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18460 location of the info_ptr after reading all of those dies. PARENT
18461 is the parent of the die in question. */
18462
18463 static struct die_info *
18464 read_die_and_children (const struct die_reader_specs *reader,
18465 const gdb_byte *info_ptr,
18466 const gdb_byte **new_info_ptr,
18467 struct die_info *parent)
18468 {
18469 struct die_info *die;
18470 const gdb_byte *cur_ptr;
18471
18472 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
18473 if (die == NULL)
18474 {
18475 *new_info_ptr = cur_ptr;
18476 return NULL;
18477 }
18478 store_in_ref_table (die, reader->cu);
18479
18480 if (die->has_children)
18481 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18482 else
18483 {
18484 die->child = NULL;
18485 *new_info_ptr = cur_ptr;
18486 }
18487
18488 die->sibling = NULL;
18489 die->parent = parent;
18490 return die;
18491 }
18492
18493 /* Read a die, all of its descendents, and all of its siblings; set
18494 all of the fields of all of the dies correctly. Arguments are as
18495 in read_die_and_children. */
18496
18497 static struct die_info *
18498 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18499 const gdb_byte *info_ptr,
18500 const gdb_byte **new_info_ptr,
18501 struct die_info *parent)
18502 {
18503 struct die_info *first_die, *last_sibling;
18504 const gdb_byte *cur_ptr;
18505
18506 cur_ptr = info_ptr;
18507 first_die = last_sibling = NULL;
18508
18509 while (1)
18510 {
18511 struct die_info *die
18512 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18513
18514 if (die == NULL)
18515 {
18516 *new_info_ptr = cur_ptr;
18517 return first_die;
18518 }
18519
18520 if (!first_die)
18521 first_die = die;
18522 else
18523 last_sibling->sibling = die;
18524
18525 last_sibling = die;
18526 }
18527 }
18528
18529 /* Read a die, all of its descendents, and all of its siblings; set
18530 all of the fields of all of the dies correctly. Arguments are as
18531 in read_die_and_children.
18532 This the main entry point for reading a DIE and all its children. */
18533
18534 static struct die_info *
18535 read_die_and_siblings (const struct die_reader_specs *reader,
18536 const gdb_byte *info_ptr,
18537 const gdb_byte **new_info_ptr,
18538 struct die_info *parent)
18539 {
18540 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18541 new_info_ptr, parent);
18542
18543 if (dwarf_die_debug)
18544 {
18545 fprintf_unfiltered (gdb_stdlog,
18546 "Read die from %s@0x%x of %s:\n",
18547 reader->die_section->get_name (),
18548 (unsigned) (info_ptr - reader->die_section->buffer),
18549 bfd_get_filename (reader->abfd));
18550 dump_die (die, dwarf_die_debug);
18551 }
18552
18553 return die;
18554 }
18555
18556 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18557 attributes.
18558 The caller is responsible for filling in the extra attributes
18559 and updating (*DIEP)->num_attrs.
18560 Set DIEP to point to a newly allocated die with its information,
18561 except for its child, sibling, and parent fields. */
18562
18563 static const gdb_byte *
18564 read_full_die_1 (const struct die_reader_specs *reader,
18565 struct die_info **diep, const gdb_byte *info_ptr,
18566 int num_extra_attrs)
18567 {
18568 unsigned int abbrev_number, bytes_read, i;
18569 struct abbrev_info *abbrev;
18570 struct die_info *die;
18571 struct dwarf2_cu *cu = reader->cu;
18572 bfd *abfd = reader->abfd;
18573
18574 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18575 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18576 info_ptr += bytes_read;
18577 if (!abbrev_number)
18578 {
18579 *diep = NULL;
18580 return info_ptr;
18581 }
18582
18583 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18584 if (!abbrev)
18585 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18586 abbrev_number,
18587 bfd_get_filename (abfd));
18588
18589 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18590 die->sect_off = sect_off;
18591 die->tag = abbrev->tag;
18592 die->abbrev = abbrev_number;
18593 die->has_children = abbrev->has_children;
18594
18595 /* Make the result usable.
18596 The caller needs to update num_attrs after adding the extra
18597 attributes. */
18598 die->num_attrs = abbrev->num_attrs;
18599
18600 bool any_need_reprocess = false;
18601 for (i = 0; i < abbrev->num_attrs; ++i)
18602 {
18603 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18604 info_ptr);
18605 if (die->attrs[i].requires_reprocessing_p ())
18606 any_need_reprocess = true;
18607 }
18608
18609 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18610 if (attr != nullptr && attr->form_is_unsigned ())
18611 cu->str_offsets_base = attr->as_unsigned ();
18612
18613 attr = die->attr (DW_AT_loclists_base);
18614 if (attr != nullptr)
18615 cu->loclist_base = attr->as_unsigned ();
18616
18617 auto maybe_addr_base = die->addr_base ();
18618 if (maybe_addr_base.has_value ())
18619 cu->addr_base = *maybe_addr_base;
18620
18621 attr = die->attr (DW_AT_rnglists_base);
18622 if (attr != nullptr)
18623 cu->ranges_base = attr->as_unsigned ();
18624
18625 if (any_need_reprocess)
18626 {
18627 for (i = 0; i < abbrev->num_attrs; ++i)
18628 {
18629 if (die->attrs[i].requires_reprocessing_p ())
18630 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
18631 }
18632 }
18633 *diep = die;
18634 return info_ptr;
18635 }
18636
18637 /* Read a die and all its attributes.
18638 Set DIEP to point to a newly allocated die with its information,
18639 except for its child, sibling, and parent fields. */
18640
18641 static const gdb_byte *
18642 read_full_die (const struct die_reader_specs *reader,
18643 struct die_info **diep, const gdb_byte *info_ptr)
18644 {
18645 const gdb_byte *result;
18646
18647 result = read_full_die_1 (reader, diep, info_ptr, 0);
18648
18649 if (dwarf_die_debug)
18650 {
18651 fprintf_unfiltered (gdb_stdlog,
18652 "Read die from %s@0x%x of %s:\n",
18653 reader->die_section->get_name (),
18654 (unsigned) (info_ptr - reader->die_section->buffer),
18655 bfd_get_filename (reader->abfd));
18656 dump_die (*diep, dwarf_die_debug);
18657 }
18658
18659 return result;
18660 }
18661 \f
18662
18663 /* Returns nonzero if TAG represents a type that we might generate a partial
18664 symbol for. */
18665
18666 static int
18667 is_type_tag_for_partial (int tag)
18668 {
18669 switch (tag)
18670 {
18671 #if 0
18672 /* Some types that would be reasonable to generate partial symbols for,
18673 that we don't at present. */
18674 case DW_TAG_array_type:
18675 case DW_TAG_file_type:
18676 case DW_TAG_ptr_to_member_type:
18677 case DW_TAG_set_type:
18678 case DW_TAG_string_type:
18679 case DW_TAG_subroutine_type:
18680 #endif
18681 case DW_TAG_base_type:
18682 case DW_TAG_class_type:
18683 case DW_TAG_interface_type:
18684 case DW_TAG_enumeration_type:
18685 case DW_TAG_structure_type:
18686 case DW_TAG_subrange_type:
18687 case DW_TAG_typedef:
18688 case DW_TAG_union_type:
18689 return 1;
18690 default:
18691 return 0;
18692 }
18693 }
18694
18695 /* Load all DIEs that are interesting for partial symbols into memory. */
18696
18697 static struct partial_die_info *
18698 load_partial_dies (const struct die_reader_specs *reader,
18699 const gdb_byte *info_ptr, int building_psymtab)
18700 {
18701 struct dwarf2_cu *cu = reader->cu;
18702 struct objfile *objfile = cu->per_objfile->objfile;
18703 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18704 unsigned int bytes_read;
18705 unsigned int load_all = 0;
18706 int nesting_level = 1;
18707
18708 parent_die = NULL;
18709 last_die = NULL;
18710
18711 gdb_assert (cu->per_cu != NULL);
18712 if (cu->per_cu->load_all_dies)
18713 load_all = 1;
18714
18715 cu->partial_dies
18716 = htab_create_alloc_ex (cu->header.length / 12,
18717 partial_die_hash,
18718 partial_die_eq,
18719 NULL,
18720 &cu->comp_unit_obstack,
18721 hashtab_obstack_allocate,
18722 dummy_obstack_deallocate);
18723
18724 while (1)
18725 {
18726 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18727
18728 /* A NULL abbrev means the end of a series of children. */
18729 if (abbrev == NULL)
18730 {
18731 if (--nesting_level == 0)
18732 return first_die;
18733
18734 info_ptr += bytes_read;
18735 last_die = parent_die;
18736 parent_die = parent_die->die_parent;
18737 continue;
18738 }
18739
18740 /* Check for template arguments. We never save these; if
18741 they're seen, we just mark the parent, and go on our way. */
18742 if (parent_die != NULL
18743 && cu->language == language_cplus
18744 && (abbrev->tag == DW_TAG_template_type_param
18745 || abbrev->tag == DW_TAG_template_value_param))
18746 {
18747 parent_die->has_template_arguments = 1;
18748
18749 if (!load_all)
18750 {
18751 /* We don't need a partial DIE for the template argument. */
18752 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18753 continue;
18754 }
18755 }
18756
18757 /* We only recurse into c++ subprograms looking for template arguments.
18758 Skip their other children. */
18759 if (!load_all
18760 && cu->language == language_cplus
18761 && parent_die != NULL
18762 && parent_die->tag == DW_TAG_subprogram
18763 && abbrev->tag != DW_TAG_inlined_subroutine)
18764 {
18765 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18766 continue;
18767 }
18768
18769 /* Check whether this DIE is interesting enough to save. Normally
18770 we would not be interested in members here, but there may be
18771 later variables referencing them via DW_AT_specification (for
18772 static members). */
18773 if (!load_all
18774 && !is_type_tag_for_partial (abbrev->tag)
18775 && abbrev->tag != DW_TAG_constant
18776 && abbrev->tag != DW_TAG_enumerator
18777 && abbrev->tag != DW_TAG_subprogram
18778 && abbrev->tag != DW_TAG_inlined_subroutine
18779 && abbrev->tag != DW_TAG_lexical_block
18780 && abbrev->tag != DW_TAG_variable
18781 && abbrev->tag != DW_TAG_namespace
18782 && abbrev->tag != DW_TAG_module
18783 && abbrev->tag != DW_TAG_member
18784 && abbrev->tag != DW_TAG_imported_unit
18785 && abbrev->tag != DW_TAG_imported_declaration)
18786 {
18787 /* Otherwise we skip to the next sibling, if any. */
18788 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18789 continue;
18790 }
18791
18792 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18793 abbrev);
18794
18795 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18796
18797 /* This two-pass algorithm for processing partial symbols has a
18798 high cost in cache pressure. Thus, handle some simple cases
18799 here which cover the majority of C partial symbols. DIEs
18800 which neither have specification tags in them, nor could have
18801 specification tags elsewhere pointing at them, can simply be
18802 processed and discarded.
18803
18804 This segment is also optional; scan_partial_symbols and
18805 add_partial_symbol will handle these DIEs if we chain
18806 them in normally. When compilers which do not emit large
18807 quantities of duplicate debug information are more common,
18808 this code can probably be removed. */
18809
18810 /* Any complete simple types at the top level (pretty much all
18811 of them, for a language without namespaces), can be processed
18812 directly. */
18813 if (parent_die == NULL
18814 && pdi.has_specification == 0
18815 && pdi.is_declaration == 0
18816 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18817 || pdi.tag == DW_TAG_base_type
18818 || pdi.tag == DW_TAG_subrange_type))
18819 {
18820 if (building_psymtab && pdi.raw_name != NULL)
18821 add_partial_symbol (&pdi, cu);
18822
18823 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18824 continue;
18825 }
18826
18827 /* The exception for DW_TAG_typedef with has_children above is
18828 a workaround of GCC PR debug/47510. In the case of this complaint
18829 type_name_or_error will error on such types later.
18830
18831 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18832 it could not find the child DIEs referenced later, this is checked
18833 above. In correct DWARF DW_TAG_typedef should have no children. */
18834
18835 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18836 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18837 "- DIE at %s [in module %s]"),
18838 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18839
18840 /* If we're at the second level, and we're an enumerator, and
18841 our parent has no specification (meaning possibly lives in a
18842 namespace elsewhere), then we can add the partial symbol now
18843 instead of queueing it. */
18844 if (pdi.tag == DW_TAG_enumerator
18845 && parent_die != NULL
18846 && parent_die->die_parent == NULL
18847 && parent_die->tag == DW_TAG_enumeration_type
18848 && parent_die->has_specification == 0)
18849 {
18850 if (pdi.raw_name == NULL)
18851 complaint (_("malformed enumerator DIE ignored"));
18852 else if (building_psymtab)
18853 add_partial_symbol (&pdi, cu);
18854
18855 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18856 continue;
18857 }
18858
18859 struct partial_die_info *part_die
18860 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18861
18862 /* We'll save this DIE so link it in. */
18863 part_die->die_parent = parent_die;
18864 part_die->die_sibling = NULL;
18865 part_die->die_child = NULL;
18866
18867 if (last_die && last_die == parent_die)
18868 last_die->die_child = part_die;
18869 else if (last_die)
18870 last_die->die_sibling = part_die;
18871
18872 last_die = part_die;
18873
18874 if (first_die == NULL)
18875 first_die = part_die;
18876
18877 /* Maybe add the DIE to the hash table. Not all DIEs that we
18878 find interesting need to be in the hash table, because we
18879 also have the parent/sibling/child chains; only those that we
18880 might refer to by offset later during partial symbol reading.
18881
18882 For now this means things that might have be the target of a
18883 DW_AT_specification, DW_AT_abstract_origin, or
18884 DW_AT_extension. DW_AT_extension will refer only to
18885 namespaces; DW_AT_abstract_origin refers to functions (and
18886 many things under the function DIE, but we do not recurse
18887 into function DIEs during partial symbol reading) and
18888 possibly variables as well; DW_AT_specification refers to
18889 declarations. Declarations ought to have the DW_AT_declaration
18890 flag. It happens that GCC forgets to put it in sometimes, but
18891 only for functions, not for types.
18892
18893 Adding more things than necessary to the hash table is harmless
18894 except for the performance cost. Adding too few will result in
18895 wasted time in find_partial_die, when we reread the compilation
18896 unit with load_all_dies set. */
18897
18898 if (load_all
18899 || abbrev->tag == DW_TAG_constant
18900 || abbrev->tag == DW_TAG_subprogram
18901 || abbrev->tag == DW_TAG_variable
18902 || abbrev->tag == DW_TAG_namespace
18903 || part_die->is_declaration)
18904 {
18905 void **slot;
18906
18907 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18908 to_underlying (part_die->sect_off),
18909 INSERT);
18910 *slot = part_die;
18911 }
18912
18913 /* For some DIEs we want to follow their children (if any). For C
18914 we have no reason to follow the children of structures; for other
18915 languages we have to, so that we can get at method physnames
18916 to infer fully qualified class names, for DW_AT_specification,
18917 and for C++ template arguments. For C++, we also look one level
18918 inside functions to find template arguments (if the name of the
18919 function does not already contain the template arguments).
18920
18921 For Ada and Fortran, we need to scan the children of subprograms
18922 and lexical blocks as well because these languages allow the
18923 definition of nested entities that could be interesting for the
18924 debugger, such as nested subprograms for instance. */
18925 if (last_die->has_children
18926 && (load_all
18927 || last_die->tag == DW_TAG_namespace
18928 || last_die->tag == DW_TAG_module
18929 || last_die->tag == DW_TAG_enumeration_type
18930 || (cu->language == language_cplus
18931 && last_die->tag == DW_TAG_subprogram
18932 && (last_die->raw_name == NULL
18933 || strchr (last_die->raw_name, '<') == NULL))
18934 || (cu->language != language_c
18935 && (last_die->tag == DW_TAG_class_type
18936 || last_die->tag == DW_TAG_interface_type
18937 || last_die->tag == DW_TAG_structure_type
18938 || last_die->tag == DW_TAG_union_type))
18939 || ((cu->language == language_ada
18940 || cu->language == language_fortran)
18941 && (last_die->tag == DW_TAG_subprogram
18942 || last_die->tag == DW_TAG_lexical_block))))
18943 {
18944 nesting_level++;
18945 parent_die = last_die;
18946 continue;
18947 }
18948
18949 /* Otherwise we skip to the next sibling, if any. */
18950 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18951
18952 /* Back to the top, do it again. */
18953 }
18954 }
18955
18956 partial_die_info::partial_die_info (sect_offset sect_off_,
18957 struct abbrev_info *abbrev)
18958 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18959 {
18960 }
18961
18962 /* See class definition. */
18963
18964 const char *
18965 partial_die_info::name (dwarf2_cu *cu)
18966 {
18967 if (!canonical_name && raw_name != nullptr)
18968 {
18969 struct objfile *objfile = cu->per_objfile->objfile;
18970 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
18971 canonical_name = 1;
18972 }
18973
18974 return raw_name;
18975 }
18976
18977 /* Read a minimal amount of information into the minimal die structure.
18978 INFO_PTR should point just after the initial uleb128 of a DIE. */
18979
18980 const gdb_byte *
18981 partial_die_info::read (const struct die_reader_specs *reader,
18982 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18983 {
18984 struct dwarf2_cu *cu = reader->cu;
18985 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18986 unsigned int i;
18987 int has_low_pc_attr = 0;
18988 int has_high_pc_attr = 0;
18989 int high_pc_relative = 0;
18990
18991 for (i = 0; i < abbrev.num_attrs; ++i)
18992 {
18993 attribute attr;
18994 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18995 /* String and address offsets that need to do the reprocessing have
18996 already been read at this point, so there is no need to wait until
18997 the loop terminates to do the reprocessing. */
18998 if (attr.requires_reprocessing_p ())
18999 read_attribute_reprocess (reader, &attr, tag);
19000 /* Store the data if it is of an attribute we want to keep in a
19001 partial symbol table. */
19002 switch (attr.name)
19003 {
19004 case DW_AT_name:
19005 switch (tag)
19006 {
19007 case DW_TAG_compile_unit:
19008 case DW_TAG_partial_unit:
19009 case DW_TAG_type_unit:
19010 /* Compilation units have a DW_AT_name that is a filename, not
19011 a source language identifier. */
19012 case DW_TAG_enumeration_type:
19013 case DW_TAG_enumerator:
19014 /* These tags always have simple identifiers already; no need
19015 to canonicalize them. */
19016 canonical_name = 1;
19017 raw_name = attr.as_string ();
19018 break;
19019 default:
19020 canonical_name = 0;
19021 raw_name = attr.as_string ();
19022 break;
19023 }
19024 break;
19025 case DW_AT_linkage_name:
19026 case DW_AT_MIPS_linkage_name:
19027 /* Note that both forms of linkage name might appear. We
19028 assume they will be the same, and we only store the last
19029 one we see. */
19030 linkage_name = attr.as_string ();
19031 break;
19032 case DW_AT_low_pc:
19033 has_low_pc_attr = 1;
19034 lowpc = attr.as_address ();
19035 break;
19036 case DW_AT_high_pc:
19037 has_high_pc_attr = 1;
19038 highpc = attr.as_address ();
19039 if (cu->header.version >= 4 && attr.form_is_constant ())
19040 high_pc_relative = 1;
19041 break;
19042 case DW_AT_location:
19043 /* Support the .debug_loc offsets. */
19044 if (attr.form_is_block ())
19045 {
19046 d.locdesc = attr.as_block ();
19047 }
19048 else if (attr.form_is_section_offset ())
19049 {
19050 dwarf2_complex_location_expr_complaint ();
19051 }
19052 else
19053 {
19054 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19055 "partial symbol information");
19056 }
19057 break;
19058 case DW_AT_external:
19059 is_external = attr.as_boolean ();
19060 break;
19061 case DW_AT_declaration:
19062 is_declaration = attr.as_boolean ();
19063 break;
19064 case DW_AT_type:
19065 has_type = 1;
19066 break;
19067 case DW_AT_abstract_origin:
19068 case DW_AT_specification:
19069 case DW_AT_extension:
19070 has_specification = 1;
19071 spec_offset = attr.get_ref_die_offset ();
19072 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19073 || cu->per_cu->is_dwz);
19074 break;
19075 case DW_AT_sibling:
19076 /* Ignore absolute siblings, they might point outside of
19077 the current compile unit. */
19078 if (attr.form == DW_FORM_ref_addr)
19079 complaint (_("ignoring absolute DW_AT_sibling"));
19080 else
19081 {
19082 const gdb_byte *buffer = reader->buffer;
19083 sect_offset off = attr.get_ref_die_offset ();
19084 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19085
19086 if (sibling_ptr < info_ptr)
19087 complaint (_("DW_AT_sibling points backwards"));
19088 else if (sibling_ptr > reader->buffer_end)
19089 reader->die_section->overflow_complaint ();
19090 else
19091 sibling = sibling_ptr;
19092 }
19093 break;
19094 case DW_AT_byte_size:
19095 has_byte_size = 1;
19096 break;
19097 case DW_AT_const_value:
19098 has_const_value = 1;
19099 break;
19100 case DW_AT_calling_convention:
19101 /* DWARF doesn't provide a way to identify a program's source-level
19102 entry point. DW_AT_calling_convention attributes are only meant
19103 to describe functions' calling conventions.
19104
19105 However, because it's a necessary piece of information in
19106 Fortran, and before DWARF 4 DW_CC_program was the only
19107 piece of debugging information whose definition refers to
19108 a 'main program' at all, several compilers marked Fortran
19109 main programs with DW_CC_program --- even when those
19110 functions use the standard calling conventions.
19111
19112 Although DWARF now specifies a way to provide this
19113 information, we support this practice for backward
19114 compatibility. */
19115 if (attr.constant_value (0) == DW_CC_program
19116 && cu->language == language_fortran)
19117 main_subprogram = 1;
19118 break;
19119 case DW_AT_inline:
19120 {
19121 LONGEST value = attr.constant_value (-1);
19122 if (value == DW_INL_inlined
19123 || value == DW_INL_declared_inlined)
19124 may_be_inlined = 1;
19125 }
19126 break;
19127
19128 case DW_AT_import:
19129 if (tag == DW_TAG_imported_unit)
19130 {
19131 d.sect_off = attr.get_ref_die_offset ();
19132 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19133 || cu->per_cu->is_dwz);
19134 }
19135 break;
19136
19137 case DW_AT_main_subprogram:
19138 main_subprogram = attr.as_boolean ();
19139 break;
19140
19141 case DW_AT_ranges:
19142 {
19143 /* DW_AT_rnglists_base does not apply to DIEs from the DWO
19144 skeleton. We take advantage of the fact the DW_AT_ranges
19145 does not appear in DW_TAG_compile_unit of DWO files.
19146
19147 Attributes of the form DW_FORM_rnglistx have already had
19148 their value changed by read_rnglist_index and already
19149 include DW_AT_rnglists_base, so don't need to add the ranges
19150 base, either. */
19151 int need_ranges_base = (tag != DW_TAG_compile_unit
19152 && attr.form != DW_FORM_rnglistx);
19153 /* It would be nice to reuse dwarf2_get_pc_bounds here,
19154 but that requires a full DIE, so instead we just
19155 reimplement it. */
19156 unsigned int ranges_offset = (attr.constant_value (0)
19157 + (need_ranges_base
19158 ? cu->ranges_base
19159 : 0));
19160
19161 /* Value of the DW_AT_ranges attribute is the offset in the
19162 .debug_ranges section. */
19163 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19164 nullptr, tag))
19165 has_pc_info = 1;
19166 }
19167 break;
19168
19169 default:
19170 break;
19171 }
19172 }
19173
19174 /* For Ada, if both the name and the linkage name appear, we prefer
19175 the latter. This lets "catch exception" work better, regardless
19176 of the order in which the name and linkage name were emitted.
19177 Really, though, this is just a workaround for the fact that gdb
19178 doesn't store both the name and the linkage name. */
19179 if (cu->language == language_ada && linkage_name != nullptr)
19180 raw_name = linkage_name;
19181
19182 if (high_pc_relative)
19183 highpc += lowpc;
19184
19185 if (has_low_pc_attr && has_high_pc_attr)
19186 {
19187 /* When using the GNU linker, .gnu.linkonce. sections are used to
19188 eliminate duplicate copies of functions and vtables and such.
19189 The linker will arbitrarily choose one and discard the others.
19190 The AT_*_pc values for such functions refer to local labels in
19191 these sections. If the section from that file was discarded, the
19192 labels are not in the output, so the relocs get a value of 0.
19193 If this is a discarded function, mark the pc bounds as invalid,
19194 so that GDB will ignore it. */
19195 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
19196 {
19197 struct objfile *objfile = per_objfile->objfile;
19198 struct gdbarch *gdbarch = objfile->arch ();
19199
19200 complaint (_("DW_AT_low_pc %s is zero "
19201 "for DIE at %s [in module %s]"),
19202 paddress (gdbarch, lowpc),
19203 sect_offset_str (sect_off),
19204 objfile_name (objfile));
19205 }
19206 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19207 else if (lowpc >= highpc)
19208 {
19209 struct objfile *objfile = per_objfile->objfile;
19210 struct gdbarch *gdbarch = objfile->arch ();
19211
19212 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19213 "for DIE at %s [in module %s]"),
19214 paddress (gdbarch, lowpc),
19215 paddress (gdbarch, highpc),
19216 sect_offset_str (sect_off),
19217 objfile_name (objfile));
19218 }
19219 else
19220 has_pc_info = 1;
19221 }
19222
19223 return info_ptr;
19224 }
19225
19226 /* Find a cached partial DIE at OFFSET in CU. */
19227
19228 struct partial_die_info *
19229 dwarf2_cu::find_partial_die (sect_offset sect_off)
19230 {
19231 struct partial_die_info *lookup_die = NULL;
19232 struct partial_die_info part_die (sect_off);
19233
19234 lookup_die = ((struct partial_die_info *)
19235 htab_find_with_hash (partial_dies, &part_die,
19236 to_underlying (sect_off)));
19237
19238 return lookup_die;
19239 }
19240
19241 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19242 except in the case of .debug_types DIEs which do not reference
19243 outside their CU (they do however referencing other types via
19244 DW_FORM_ref_sig8). */
19245
19246 static const struct cu_partial_die_info
19247 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19248 {
19249 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19250 struct objfile *objfile = per_objfile->objfile;
19251 struct partial_die_info *pd = NULL;
19252
19253 if (offset_in_dwz == cu->per_cu->is_dwz
19254 && cu->header.offset_in_cu_p (sect_off))
19255 {
19256 pd = cu->find_partial_die (sect_off);
19257 if (pd != NULL)
19258 return { cu, pd };
19259 /* We missed recording what we needed.
19260 Load all dies and try again. */
19261 }
19262 else
19263 {
19264 /* TUs don't reference other CUs/TUs (except via type signatures). */
19265 if (cu->per_cu->is_debug_types)
19266 {
19267 error (_("Dwarf Error: Type Unit at offset %s contains"
19268 " external reference to offset %s [in module %s].\n"),
19269 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19270 bfd_get_filename (objfile->obfd));
19271 }
19272 dwarf2_per_cu_data *per_cu
19273 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19274 per_objfile);
19275
19276 cu = per_objfile->get_cu (per_cu);
19277 if (cu == NULL || cu->partial_dies == NULL)
19278 load_partial_comp_unit (per_cu, per_objfile, nullptr);
19279
19280 cu = per_objfile->get_cu (per_cu);
19281
19282 cu->last_used = 0;
19283 pd = cu->find_partial_die (sect_off);
19284 }
19285
19286 /* If we didn't find it, and not all dies have been loaded,
19287 load them all and try again. */
19288
19289 if (pd == NULL && cu->per_cu->load_all_dies == 0)
19290 {
19291 cu->per_cu->load_all_dies = 1;
19292
19293 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19294 THIS_CU->cu may already be in use. So we can't just free it and
19295 replace its DIEs with the ones we read in. Instead, we leave those
19296 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19297 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19298 set. */
19299 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
19300
19301 pd = cu->find_partial_die (sect_off);
19302 }
19303
19304 if (pd == NULL)
19305 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
19306 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19307 return { cu, pd };
19308 }
19309
19310 /* See if we can figure out if the class lives in a namespace. We do
19311 this by looking for a member function; its demangled name will
19312 contain namespace info, if there is any. */
19313
19314 static void
19315 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19316 struct dwarf2_cu *cu)
19317 {
19318 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19319 what template types look like, because the demangler
19320 frequently doesn't give the same name as the debug info. We
19321 could fix this by only using the demangled name to get the
19322 prefix (but see comment in read_structure_type). */
19323
19324 struct partial_die_info *real_pdi;
19325 struct partial_die_info *child_pdi;
19326
19327 /* If this DIE (this DIE's specification, if any) has a parent, then
19328 we should not do this. We'll prepend the parent's fully qualified
19329 name when we create the partial symbol. */
19330
19331 real_pdi = struct_pdi;
19332 while (real_pdi->has_specification)
19333 {
19334 auto res = find_partial_die (real_pdi->spec_offset,
19335 real_pdi->spec_is_dwz, cu);
19336 real_pdi = res.pdi;
19337 cu = res.cu;
19338 }
19339
19340 if (real_pdi->die_parent != NULL)
19341 return;
19342
19343 for (child_pdi = struct_pdi->die_child;
19344 child_pdi != NULL;
19345 child_pdi = child_pdi->die_sibling)
19346 {
19347 if (child_pdi->tag == DW_TAG_subprogram
19348 && child_pdi->linkage_name != NULL)
19349 {
19350 gdb::unique_xmalloc_ptr<char> actual_class_name
19351 (cu->language_defn->class_name_from_physname
19352 (child_pdi->linkage_name));
19353 if (actual_class_name != NULL)
19354 {
19355 struct objfile *objfile = cu->per_objfile->objfile;
19356 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19357 struct_pdi->canonical_name = 1;
19358 }
19359 break;
19360 }
19361 }
19362 }
19363
19364 /* Return true if a DIE with TAG may have the DW_AT_const_value
19365 attribute. */
19366
19367 static bool
19368 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19369 {
19370 switch (tag)
19371 {
19372 case DW_TAG_constant:
19373 case DW_TAG_enumerator:
19374 case DW_TAG_formal_parameter:
19375 case DW_TAG_template_value_param:
19376 case DW_TAG_variable:
19377 return true;
19378 }
19379
19380 return false;
19381 }
19382
19383 void
19384 partial_die_info::fixup (struct dwarf2_cu *cu)
19385 {
19386 /* Once we've fixed up a die, there's no point in doing so again.
19387 This also avoids a memory leak if we were to call
19388 guess_partial_die_structure_name multiple times. */
19389 if (fixup_called)
19390 return;
19391
19392 /* If we found a reference attribute and the DIE has no name, try
19393 to find a name in the referred to DIE. */
19394
19395 if (raw_name == NULL && has_specification)
19396 {
19397 struct partial_die_info *spec_die;
19398
19399 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19400 spec_die = res.pdi;
19401 cu = res.cu;
19402
19403 spec_die->fixup (cu);
19404
19405 if (spec_die->raw_name)
19406 {
19407 raw_name = spec_die->raw_name;
19408 canonical_name = spec_die->canonical_name;
19409
19410 /* Copy DW_AT_external attribute if it is set. */
19411 if (spec_die->is_external)
19412 is_external = spec_die->is_external;
19413 }
19414 }
19415
19416 if (!has_const_value && has_specification
19417 && can_have_DW_AT_const_value_p (tag))
19418 {
19419 struct partial_die_info *spec_die;
19420
19421 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19422 spec_die = res.pdi;
19423 cu = res.cu;
19424
19425 spec_die->fixup (cu);
19426
19427 if (spec_die->has_const_value)
19428 {
19429 /* Copy DW_AT_const_value attribute if it is set. */
19430 has_const_value = spec_die->has_const_value;
19431 }
19432 }
19433
19434 /* Set default names for some unnamed DIEs. */
19435
19436 if (raw_name == NULL && tag == DW_TAG_namespace)
19437 {
19438 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
19439 canonical_name = 1;
19440 }
19441
19442 /* If there is no parent die to provide a namespace, and there are
19443 children, see if we can determine the namespace from their linkage
19444 name. */
19445 if (cu->language == language_cplus
19446 && !cu->per_objfile->per_bfd->types.empty ()
19447 && die_parent == NULL
19448 && has_children
19449 && (tag == DW_TAG_class_type
19450 || tag == DW_TAG_structure_type
19451 || tag == DW_TAG_union_type))
19452 guess_partial_die_structure_name (this, cu);
19453
19454 /* GCC might emit a nameless struct or union that has a linkage
19455 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19456 if (raw_name == NULL
19457 && (tag == DW_TAG_class_type
19458 || tag == DW_TAG_interface_type
19459 || tag == DW_TAG_structure_type
19460 || tag == DW_TAG_union_type)
19461 && linkage_name != NULL)
19462 {
19463 gdb::unique_xmalloc_ptr<char> demangled
19464 (gdb_demangle (linkage_name, DMGL_TYPES));
19465 if (demangled != nullptr)
19466 {
19467 const char *base;
19468
19469 /* Strip any leading namespaces/classes, keep only the base name.
19470 DW_AT_name for named DIEs does not contain the prefixes. */
19471 base = strrchr (demangled.get (), ':');
19472 if (base && base > demangled.get () && base[-1] == ':')
19473 base++;
19474 else
19475 base = demangled.get ();
19476
19477 struct objfile *objfile = cu->per_objfile->objfile;
19478 raw_name = objfile->intern (base);
19479 canonical_name = 1;
19480 }
19481 }
19482
19483 fixup_called = 1;
19484 }
19485
19486 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
19487 contents from the given SECTION in the HEADER. */
19488 static void
19489 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
19490 struct dwarf2_section_info *section)
19491 {
19492 unsigned int bytes_read;
19493 bfd *abfd = section->get_bfd_owner ();
19494 const gdb_byte *info_ptr = section->buffer;
19495 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19496 info_ptr += bytes_read;
19497 header->version = read_2_bytes (abfd, info_ptr);
19498 info_ptr += 2;
19499 header->addr_size = read_1_byte (abfd, info_ptr);
19500 info_ptr += 1;
19501 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19502 info_ptr += 1;
19503 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19504 }
19505
19506 /* Return the DW_AT_loclists_base value for the CU. */
19507 static ULONGEST
19508 lookup_loclist_base (struct dwarf2_cu *cu)
19509 {
19510 /* For the .dwo unit, the loclist_base points to the first offset following
19511 the header. The header consists of the following entities-
19512 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19513 bit format)
19514 2. version (2 bytes)
19515 3. address size (1 byte)
19516 4. segment selector size (1 byte)
19517 5. offset entry count (4 bytes)
19518 These sizes are derived as per the DWARFv5 standard. */
19519 if (cu->dwo_unit != nullptr)
19520 {
19521 if (cu->header.initial_length_size == 4)
19522 return LOCLIST_HEADER_SIZE32;
19523 return LOCLIST_HEADER_SIZE64;
19524 }
19525 return cu->loclist_base;
19526 }
19527
19528 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19529 array of offsets in the .debug_loclists section. */
19530 static CORE_ADDR
19531 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19532 {
19533 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19534 struct objfile *objfile = per_objfile->objfile;
19535 bfd *abfd = objfile->obfd;
19536 ULONGEST loclist_base = lookup_loclist_base (cu);
19537 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19538
19539 section->read (objfile);
19540 if (section->buffer == NULL)
19541 complaint (_("DW_FORM_loclistx used without .debug_loclists "
19542 "section [in module %s]"), objfile_name (objfile));
19543 struct loclists_rnglists_header header;
19544 read_loclists_rnglists_header (&header, section);
19545 if (loclist_index >= header.offset_entry_count)
19546 complaint (_("DW_FORM_loclistx pointing outside of "
19547 ".debug_loclists offset array [in module %s]"),
19548 objfile_name (objfile));
19549 if (loclist_base + loclist_index * cu->header.offset_size
19550 >= section->size)
19551 complaint (_("DW_FORM_loclistx pointing outside of "
19552 ".debug_loclists section [in module %s]"),
19553 objfile_name (objfile));
19554 const gdb_byte *info_ptr
19555 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
19556
19557 if (cu->header.offset_size == 4)
19558 return bfd_get_32 (abfd, info_ptr) + loclist_base;
19559 else
19560 return bfd_get_64 (abfd, info_ptr) + loclist_base;
19561 }
19562
19563 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
19564 array of offsets in the .debug_rnglists section. */
19565 static CORE_ADDR
19566 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
19567 dwarf_tag tag)
19568 {
19569 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19570 struct objfile *objfile = dwarf2_per_objfile->objfile;
19571 bfd *abfd = objfile->obfd;
19572 ULONGEST rnglist_header_size =
19573 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
19574 : RNGLIST_HEADER_SIZE64);
19575 ULONGEST rnglist_base =
19576 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->ranges_base;
19577 ULONGEST start_offset =
19578 rnglist_base + rnglist_index * cu->header.offset_size;
19579
19580 /* Get rnglists section. */
19581 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
19582
19583 /* Read the rnglists section content. */
19584 section->read (objfile);
19585 if (section->buffer == nullptr)
19586 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
19587 "[in module %s]"),
19588 objfile_name (objfile));
19589
19590 /* Verify the rnglist index is valid. */
19591 struct loclists_rnglists_header header;
19592 read_loclists_rnglists_header (&header, section);
19593 if (rnglist_index >= header.offset_entry_count)
19594 error (_("DW_FORM_rnglistx index pointing outside of "
19595 ".debug_rnglists offset array [in module %s]"),
19596 objfile_name (objfile));
19597
19598 /* Validate that the offset is within the section's range. */
19599 if (start_offset >= section->size)
19600 error (_("DW_FORM_rnglistx pointing outside of "
19601 ".debug_rnglists section [in module %s]"),
19602 objfile_name (objfile));
19603
19604 /* Validate that reading won't go beyond the end of the section. */
19605 if (start_offset + cu->header.offset_size > rnglist_base + section->size)
19606 error (_("Reading DW_FORM_rnglistx index beyond end of"
19607 ".debug_rnglists section [in module %s]"),
19608 objfile_name (objfile));
19609
19610 const gdb_byte *info_ptr = section->buffer + start_offset;
19611
19612 if (cu->header.offset_size == 4)
19613 return read_4_bytes (abfd, info_ptr) + rnglist_base;
19614 else
19615 return read_8_bytes (abfd, info_ptr) + rnglist_base;
19616 }
19617
19618 /* Process the attributes that had to be skipped in the first round. These
19619 attributes are the ones that need str_offsets_base or addr_base attributes.
19620 They could not have been processed in the first round, because at the time
19621 the values of str_offsets_base or addr_base may not have been known. */
19622 static void
19623 read_attribute_reprocess (const struct die_reader_specs *reader,
19624 struct attribute *attr, dwarf_tag tag)
19625 {
19626 struct dwarf2_cu *cu = reader->cu;
19627 switch (attr->form)
19628 {
19629 case DW_FORM_addrx:
19630 case DW_FORM_GNU_addr_index:
19631 attr->set_address (read_addr_index (cu,
19632 attr->as_unsigned_reprocess ()));
19633 break;
19634 case DW_FORM_loclistx:
19635 attr->set_address (read_loclist_index (cu, attr->as_unsigned ()));
19636 break;
19637 case DW_FORM_rnglistx:
19638 attr->set_address (read_rnglist_index (cu, attr->as_unsigned (), tag));
19639 break;
19640 case DW_FORM_strx:
19641 case DW_FORM_strx1:
19642 case DW_FORM_strx2:
19643 case DW_FORM_strx3:
19644 case DW_FORM_strx4:
19645 case DW_FORM_GNU_str_index:
19646 {
19647 unsigned int str_index = attr->as_unsigned_reprocess ();
19648 gdb_assert (!attr->canonical_string_p ());
19649 if (reader->dwo_file != NULL)
19650 attr->set_string_noncanonical (read_dwo_str_index (reader,
19651 str_index));
19652 else
19653 attr->set_string_noncanonical (read_stub_str_index (cu,
19654 str_index));
19655 break;
19656 }
19657 default:
19658 gdb_assert_not_reached (_("Unexpected DWARF form."));
19659 }
19660 }
19661
19662 /* Read an attribute value described by an attribute form. */
19663
19664 static const gdb_byte *
19665 read_attribute_value (const struct die_reader_specs *reader,
19666 struct attribute *attr, unsigned form,
19667 LONGEST implicit_const, const gdb_byte *info_ptr)
19668 {
19669 struct dwarf2_cu *cu = reader->cu;
19670 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19671 struct objfile *objfile = per_objfile->objfile;
19672 bfd *abfd = reader->abfd;
19673 struct comp_unit_head *cu_header = &cu->header;
19674 unsigned int bytes_read;
19675 struct dwarf_block *blk;
19676
19677 attr->form = (enum dwarf_form) form;
19678 switch (form)
19679 {
19680 case DW_FORM_ref_addr:
19681 if (cu->header.version == 2)
19682 attr->set_unsigned (cu->header.read_address (abfd, info_ptr,
19683 &bytes_read));
19684 else
19685 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
19686 &bytes_read));
19687 info_ptr += bytes_read;
19688 break;
19689 case DW_FORM_GNU_ref_alt:
19690 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
19691 &bytes_read));
19692 info_ptr += bytes_read;
19693 break;
19694 case DW_FORM_addr:
19695 {
19696 struct gdbarch *gdbarch = objfile->arch ();
19697 CORE_ADDR addr = cu->header.read_address (abfd, info_ptr, &bytes_read);
19698 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
19699 attr->set_address (addr);
19700 info_ptr += bytes_read;
19701 }
19702 break;
19703 case DW_FORM_block2:
19704 blk = dwarf_alloc_block (cu);
19705 blk->size = read_2_bytes (abfd, info_ptr);
19706 info_ptr += 2;
19707 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19708 info_ptr += blk->size;
19709 attr->set_block (blk);
19710 break;
19711 case DW_FORM_block4:
19712 blk = dwarf_alloc_block (cu);
19713 blk->size = read_4_bytes (abfd, info_ptr);
19714 info_ptr += 4;
19715 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19716 info_ptr += blk->size;
19717 attr->set_block (blk);
19718 break;
19719 case DW_FORM_data2:
19720 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
19721 info_ptr += 2;
19722 break;
19723 case DW_FORM_data4:
19724 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
19725 info_ptr += 4;
19726 break;
19727 case DW_FORM_data8:
19728 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
19729 info_ptr += 8;
19730 break;
19731 case DW_FORM_data16:
19732 blk = dwarf_alloc_block (cu);
19733 blk->size = 16;
19734 blk->data = read_n_bytes (abfd, info_ptr, 16);
19735 info_ptr += 16;
19736 attr->set_block (blk);
19737 break;
19738 case DW_FORM_sec_offset:
19739 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
19740 &bytes_read));
19741 info_ptr += bytes_read;
19742 break;
19743 case DW_FORM_loclistx:
19744 {
19745 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19746 &bytes_read));
19747 info_ptr += bytes_read;
19748 }
19749 break;
19750 case DW_FORM_string:
19751 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
19752 &bytes_read));
19753 info_ptr += bytes_read;
19754 break;
19755 case DW_FORM_strp:
19756 if (!cu->per_cu->is_dwz)
19757 {
19758 attr->set_string_noncanonical
19759 (read_indirect_string (per_objfile,
19760 abfd, info_ptr, cu_header,
19761 &bytes_read));
19762 info_ptr += bytes_read;
19763 break;
19764 }
19765 /* FALLTHROUGH */
19766 case DW_FORM_line_strp:
19767 if (!cu->per_cu->is_dwz)
19768 {
19769 attr->set_string_noncanonical
19770 (per_objfile->read_line_string (info_ptr, cu_header,
19771 &bytes_read));
19772 info_ptr += bytes_read;
19773 break;
19774 }
19775 /* FALLTHROUGH */
19776 case DW_FORM_GNU_strp_alt:
19777 {
19778 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
19779 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19780 &bytes_read);
19781
19782 attr->set_string_noncanonical
19783 (dwz->read_string (objfile, str_offset));
19784 info_ptr += bytes_read;
19785 }
19786 break;
19787 case DW_FORM_exprloc:
19788 case DW_FORM_block:
19789 blk = dwarf_alloc_block (cu);
19790 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19791 info_ptr += bytes_read;
19792 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19793 info_ptr += blk->size;
19794 attr->set_block (blk);
19795 break;
19796 case DW_FORM_block1:
19797 blk = dwarf_alloc_block (cu);
19798 blk->size = read_1_byte (abfd, info_ptr);
19799 info_ptr += 1;
19800 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19801 info_ptr += blk->size;
19802 attr->set_block (blk);
19803 break;
19804 case DW_FORM_data1:
19805 case DW_FORM_flag:
19806 attr->set_unsigned (read_1_byte (abfd, info_ptr));
19807 info_ptr += 1;
19808 break;
19809 case DW_FORM_flag_present:
19810 attr->set_unsigned (1);
19811 break;
19812 case DW_FORM_sdata:
19813 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
19814 info_ptr += bytes_read;
19815 break;
19816 case DW_FORM_rnglistx:
19817 {
19818 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19819 &bytes_read));
19820 info_ptr += bytes_read;
19821 }
19822 break;
19823 case DW_FORM_udata:
19824 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19825 info_ptr += bytes_read;
19826 break;
19827 case DW_FORM_ref1:
19828 attr->set_unsigned ((to_underlying (cu->header.sect_off)
19829 + read_1_byte (abfd, info_ptr)));
19830 info_ptr += 1;
19831 break;
19832 case DW_FORM_ref2:
19833 attr->set_unsigned ((to_underlying (cu->header.sect_off)
19834 + read_2_bytes (abfd, info_ptr)));
19835 info_ptr += 2;
19836 break;
19837 case DW_FORM_ref4:
19838 attr->set_unsigned ((to_underlying (cu->header.sect_off)
19839 + read_4_bytes (abfd, info_ptr)));
19840 info_ptr += 4;
19841 break;
19842 case DW_FORM_ref8:
19843 attr->set_unsigned ((to_underlying (cu->header.sect_off)
19844 + read_8_bytes (abfd, info_ptr)));
19845 info_ptr += 8;
19846 break;
19847 case DW_FORM_ref_sig8:
19848 attr->set_signature (read_8_bytes (abfd, info_ptr));
19849 info_ptr += 8;
19850 break;
19851 case DW_FORM_ref_udata:
19852 attr->set_unsigned ((to_underlying (cu->header.sect_off)
19853 + read_unsigned_leb128 (abfd, info_ptr,
19854 &bytes_read)));
19855 info_ptr += bytes_read;
19856 break;
19857 case DW_FORM_indirect:
19858 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19859 info_ptr += bytes_read;
19860 if (form == DW_FORM_implicit_const)
19861 {
19862 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19863 info_ptr += bytes_read;
19864 }
19865 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19866 info_ptr);
19867 break;
19868 case DW_FORM_implicit_const:
19869 attr->set_signed (implicit_const);
19870 break;
19871 case DW_FORM_addrx:
19872 case DW_FORM_GNU_addr_index:
19873 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19874 &bytes_read));
19875 info_ptr += bytes_read;
19876 break;
19877 case DW_FORM_strx:
19878 case DW_FORM_strx1:
19879 case DW_FORM_strx2:
19880 case DW_FORM_strx3:
19881 case DW_FORM_strx4:
19882 case DW_FORM_GNU_str_index:
19883 {
19884 ULONGEST str_index;
19885 if (form == DW_FORM_strx1)
19886 {
19887 str_index = read_1_byte (abfd, info_ptr);
19888 info_ptr += 1;
19889 }
19890 else if (form == DW_FORM_strx2)
19891 {
19892 str_index = read_2_bytes (abfd, info_ptr);
19893 info_ptr += 2;
19894 }
19895 else if (form == DW_FORM_strx3)
19896 {
19897 str_index = read_3_bytes (abfd, info_ptr);
19898 info_ptr += 3;
19899 }
19900 else if (form == DW_FORM_strx4)
19901 {
19902 str_index = read_4_bytes (abfd, info_ptr);
19903 info_ptr += 4;
19904 }
19905 else
19906 {
19907 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19908 info_ptr += bytes_read;
19909 }
19910 attr->set_unsigned_reprocess (str_index);
19911 }
19912 break;
19913 default:
19914 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19915 dwarf_form_name (form),
19916 bfd_get_filename (abfd));
19917 }
19918
19919 /* Super hack. */
19920 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19921 attr->form = DW_FORM_GNU_ref_alt;
19922
19923 /* We have seen instances where the compiler tried to emit a byte
19924 size attribute of -1 which ended up being encoded as an unsigned
19925 0xffffffff. Although 0xffffffff is technically a valid size value,
19926 an object of this size seems pretty unlikely so we can relatively
19927 safely treat these cases as if the size attribute was invalid and
19928 treat them as zero by default. */
19929 if (attr->name == DW_AT_byte_size
19930 && form == DW_FORM_data4
19931 && attr->as_unsigned () >= 0xffffffff)
19932 {
19933 complaint
19934 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19935 hex_string (attr->as_unsigned ()));
19936 attr->set_unsigned (0);
19937 }
19938
19939 return info_ptr;
19940 }
19941
19942 /* Read an attribute described by an abbreviated attribute. */
19943
19944 static const gdb_byte *
19945 read_attribute (const struct die_reader_specs *reader,
19946 struct attribute *attr, struct attr_abbrev *abbrev,
19947 const gdb_byte *info_ptr)
19948 {
19949 attr->name = abbrev->name;
19950 attr->string_is_canonical = 0;
19951 attr->requires_reprocessing = 0;
19952 return read_attribute_value (reader, attr, abbrev->form,
19953 abbrev->implicit_const, info_ptr);
19954 }
19955
19956 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19957
19958 static const char *
19959 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
19960 LONGEST str_offset)
19961 {
19962 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
19963 str_offset, "DW_FORM_strp");
19964 }
19965
19966 /* Return pointer to string at .debug_str offset as read from BUF.
19967 BUF is assumed to be in a compilation unit described by CU_HEADER.
19968 Return *BYTES_READ_PTR count of bytes read from BUF. */
19969
19970 static const char *
19971 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
19972 const gdb_byte *buf,
19973 const struct comp_unit_head *cu_header,
19974 unsigned int *bytes_read_ptr)
19975 {
19976 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19977
19978 return read_indirect_string_at_offset (per_objfile, str_offset);
19979 }
19980
19981 /* See read.h. */
19982
19983 const char *
19984 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19985 const struct comp_unit_head *cu_header,
19986 unsigned int *bytes_read_ptr)
19987 {
19988 bfd *abfd = objfile->obfd;
19989 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19990
19991 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19992 }
19993
19994 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19995 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19996 ADDR_SIZE is the size of addresses from the CU header. */
19997
19998 static CORE_ADDR
19999 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20000 gdb::optional<ULONGEST> addr_base, int addr_size)
20001 {
20002 struct objfile *objfile = per_objfile->objfile;
20003 bfd *abfd = objfile->obfd;
20004 const gdb_byte *info_ptr;
20005 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20006
20007 per_objfile->per_bfd->addr.read (objfile);
20008 if (per_objfile->per_bfd->addr.buffer == NULL)
20009 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20010 objfile_name (objfile));
20011 if (addr_base_or_zero + addr_index * addr_size
20012 >= per_objfile->per_bfd->addr.size)
20013 error (_("DW_FORM_addr_index pointing outside of "
20014 ".debug_addr section [in module %s]"),
20015 objfile_name (objfile));
20016 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20017 + addr_index * addr_size);
20018 if (addr_size == 4)
20019 return bfd_get_32 (abfd, info_ptr);
20020 else
20021 return bfd_get_64 (abfd, info_ptr);
20022 }
20023
20024 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20025
20026 static CORE_ADDR
20027 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20028 {
20029 return read_addr_index_1 (cu->per_objfile, addr_index,
20030 cu->addr_base, cu->header.addr_size);
20031 }
20032
20033 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20034
20035 static CORE_ADDR
20036 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20037 unsigned int *bytes_read)
20038 {
20039 bfd *abfd = cu->per_objfile->objfile->obfd;
20040 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20041
20042 return read_addr_index (cu, addr_index);
20043 }
20044
20045 /* See read.h. */
20046
20047 CORE_ADDR
20048 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20049 dwarf2_per_objfile *per_objfile,
20050 unsigned int addr_index)
20051 {
20052 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20053 gdb::optional<ULONGEST> addr_base;
20054 int addr_size;
20055
20056 /* We need addr_base and addr_size.
20057 If we don't have PER_CU->cu, we have to get it.
20058 Nasty, but the alternative is storing the needed info in PER_CU,
20059 which at this point doesn't seem justified: it's not clear how frequently
20060 it would get used and it would increase the size of every PER_CU.
20061 Entry points like dwarf2_per_cu_addr_size do a similar thing
20062 so we're not in uncharted territory here.
20063 Alas we need to be a bit more complicated as addr_base is contained
20064 in the DIE.
20065
20066 We don't need to read the entire CU(/TU).
20067 We just need the header and top level die.
20068
20069 IWBN to use the aging mechanism to let us lazily later discard the CU.
20070 For now we skip this optimization. */
20071
20072 if (cu != NULL)
20073 {
20074 addr_base = cu->addr_base;
20075 addr_size = cu->header.addr_size;
20076 }
20077 else
20078 {
20079 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
20080 addr_base = reader.cu->addr_base;
20081 addr_size = reader.cu->header.addr_size;
20082 }
20083
20084 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
20085 }
20086
20087 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20088 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20089 DWO file. */
20090
20091 static const char *
20092 read_str_index (struct dwarf2_cu *cu,
20093 struct dwarf2_section_info *str_section,
20094 struct dwarf2_section_info *str_offsets_section,
20095 ULONGEST str_offsets_base, ULONGEST str_index)
20096 {
20097 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20098 struct objfile *objfile = per_objfile->objfile;
20099 const char *objf_name = objfile_name (objfile);
20100 bfd *abfd = objfile->obfd;
20101 const gdb_byte *info_ptr;
20102 ULONGEST str_offset;
20103 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20104
20105 str_section->read (objfile);
20106 str_offsets_section->read (objfile);
20107 if (str_section->buffer == NULL)
20108 error (_("%s used without %s section"
20109 " in CU at offset %s [in module %s]"),
20110 form_name, str_section->get_name (),
20111 sect_offset_str (cu->header.sect_off), objf_name);
20112 if (str_offsets_section->buffer == NULL)
20113 error (_("%s used without %s section"
20114 " in CU at offset %s [in module %s]"),
20115 form_name, str_section->get_name (),
20116 sect_offset_str (cu->header.sect_off), objf_name);
20117 info_ptr = (str_offsets_section->buffer
20118 + str_offsets_base
20119 + str_index * cu->header.offset_size);
20120 if (cu->header.offset_size == 4)
20121 str_offset = bfd_get_32 (abfd, info_ptr);
20122 else
20123 str_offset = bfd_get_64 (abfd, info_ptr);
20124 if (str_offset >= str_section->size)
20125 error (_("Offset from %s pointing outside of"
20126 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20127 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20128 return (const char *) (str_section->buffer + str_offset);
20129 }
20130
20131 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20132
20133 static const char *
20134 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20135 {
20136 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20137 ? reader->cu->header.addr_size : 0;
20138 return read_str_index (reader->cu,
20139 &reader->dwo_file->sections.str,
20140 &reader->dwo_file->sections.str_offsets,
20141 str_offsets_base, str_index);
20142 }
20143
20144 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20145
20146 static const char *
20147 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20148 {
20149 struct objfile *objfile = cu->per_objfile->objfile;
20150 const char *objf_name = objfile_name (objfile);
20151 static const char form_name[] = "DW_FORM_GNU_str_index";
20152 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20153
20154 if (!cu->str_offsets_base.has_value ())
20155 error (_("%s used in Fission stub without %s"
20156 " in CU at offset 0x%lx [in module %s]"),
20157 form_name, str_offsets_attr_name,
20158 (long) cu->header.offset_size, objf_name);
20159
20160 return read_str_index (cu,
20161 &cu->per_objfile->per_bfd->str,
20162 &cu->per_objfile->per_bfd->str_offsets,
20163 *cu->str_offsets_base, str_index);
20164 }
20165
20166 /* Return the length of an LEB128 number in BUF. */
20167
20168 static int
20169 leb128_size (const gdb_byte *buf)
20170 {
20171 const gdb_byte *begin = buf;
20172 gdb_byte byte;
20173
20174 while (1)
20175 {
20176 byte = *buf++;
20177 if ((byte & 128) == 0)
20178 return buf - begin;
20179 }
20180 }
20181
20182 static void
20183 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20184 {
20185 switch (lang)
20186 {
20187 case DW_LANG_C89:
20188 case DW_LANG_C99:
20189 case DW_LANG_C11:
20190 case DW_LANG_C:
20191 case DW_LANG_UPC:
20192 cu->language = language_c;
20193 break;
20194 case DW_LANG_Java:
20195 case DW_LANG_C_plus_plus:
20196 case DW_LANG_C_plus_plus_11:
20197 case DW_LANG_C_plus_plus_14:
20198 cu->language = language_cplus;
20199 break;
20200 case DW_LANG_D:
20201 cu->language = language_d;
20202 break;
20203 case DW_LANG_Fortran77:
20204 case DW_LANG_Fortran90:
20205 case DW_LANG_Fortran95:
20206 case DW_LANG_Fortran03:
20207 case DW_LANG_Fortran08:
20208 cu->language = language_fortran;
20209 break;
20210 case DW_LANG_Go:
20211 cu->language = language_go;
20212 break;
20213 case DW_LANG_Mips_Assembler:
20214 cu->language = language_asm;
20215 break;
20216 case DW_LANG_Ada83:
20217 case DW_LANG_Ada95:
20218 cu->language = language_ada;
20219 break;
20220 case DW_LANG_Modula2:
20221 cu->language = language_m2;
20222 break;
20223 case DW_LANG_Pascal83:
20224 cu->language = language_pascal;
20225 break;
20226 case DW_LANG_ObjC:
20227 cu->language = language_objc;
20228 break;
20229 case DW_LANG_Rust:
20230 case DW_LANG_Rust_old:
20231 cu->language = language_rust;
20232 break;
20233 case DW_LANG_Cobol74:
20234 case DW_LANG_Cobol85:
20235 default:
20236 cu->language = language_minimal;
20237 break;
20238 }
20239 cu->language_defn = language_def (cu->language);
20240 }
20241
20242 /* Return the named attribute or NULL if not there. */
20243
20244 static struct attribute *
20245 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20246 {
20247 for (;;)
20248 {
20249 unsigned int i;
20250 struct attribute *spec = NULL;
20251
20252 for (i = 0; i < die->num_attrs; ++i)
20253 {
20254 if (die->attrs[i].name == name)
20255 return &die->attrs[i];
20256 if (die->attrs[i].name == DW_AT_specification
20257 || die->attrs[i].name == DW_AT_abstract_origin)
20258 spec = &die->attrs[i];
20259 }
20260
20261 if (!spec)
20262 break;
20263
20264 die = follow_die_ref (die, spec, &cu);
20265 }
20266
20267 return NULL;
20268 }
20269
20270 /* Return the string associated with a string-typed attribute, or NULL if it
20271 is either not found or is of an incorrect type. */
20272
20273 static const char *
20274 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20275 {
20276 struct attribute *attr;
20277 const char *str = NULL;
20278
20279 attr = dwarf2_attr (die, name, cu);
20280
20281 if (attr != NULL)
20282 {
20283 str = attr->as_string ();
20284 if (str == nullptr)
20285 complaint (_("string type expected for attribute %s for "
20286 "DIE at %s in module %s"),
20287 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20288 objfile_name (cu->per_objfile->objfile));
20289 }
20290
20291 return str;
20292 }
20293
20294 /* Return the dwo name or NULL if not present. If present, it is in either
20295 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20296 static const char *
20297 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20298 {
20299 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20300 if (dwo_name == nullptr)
20301 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20302 return dwo_name;
20303 }
20304
20305 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20306 and holds a non-zero value. This function should only be used for
20307 DW_FORM_flag or DW_FORM_flag_present attributes. */
20308
20309 static int
20310 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20311 {
20312 struct attribute *attr = dwarf2_attr (die, name, cu);
20313
20314 return attr != nullptr && attr->as_boolean ();
20315 }
20316
20317 static int
20318 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20319 {
20320 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20321 which value is non-zero. However, we have to be careful with
20322 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20323 (via dwarf2_flag_true_p) follows this attribute. So we may
20324 end up accidently finding a declaration attribute that belongs
20325 to a different DIE referenced by the specification attribute,
20326 even though the given DIE does not have a declaration attribute. */
20327 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20328 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20329 }
20330
20331 /* Return the die giving the specification for DIE, if there is
20332 one. *SPEC_CU is the CU containing DIE on input, and the CU
20333 containing the return value on output. If there is no
20334 specification, but there is an abstract origin, that is
20335 returned. */
20336
20337 static struct die_info *
20338 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20339 {
20340 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20341 *spec_cu);
20342
20343 if (spec_attr == NULL)
20344 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20345
20346 if (spec_attr == NULL)
20347 return NULL;
20348 else
20349 return follow_die_ref (die, spec_attr, spec_cu);
20350 }
20351
20352 /* Stub for free_line_header to match void * callback types. */
20353
20354 static void
20355 free_line_header_voidp (void *arg)
20356 {
20357 struct line_header *lh = (struct line_header *) arg;
20358
20359 delete lh;
20360 }
20361
20362 /* A convenience function to find the proper .debug_line section for a CU. */
20363
20364 static struct dwarf2_section_info *
20365 get_debug_line_section (struct dwarf2_cu *cu)
20366 {
20367 struct dwarf2_section_info *section;
20368 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20369
20370 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20371 DWO file. */
20372 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20373 section = &cu->dwo_unit->dwo_file->sections.line;
20374 else if (cu->per_cu->is_dwz)
20375 {
20376 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
20377
20378 section = &dwz->line;
20379 }
20380 else
20381 section = &per_objfile->per_bfd->line;
20382
20383 return section;
20384 }
20385
20386 /* Read the statement program header starting at OFFSET in
20387 .debug_line, or .debug_line.dwo. Return a pointer
20388 to a struct line_header, allocated using xmalloc.
20389 Returns NULL if there is a problem reading the header, e.g., if it
20390 has a version we don't understand.
20391
20392 NOTE: the strings in the include directory and file name tables of
20393 the returned object point into the dwarf line section buffer,
20394 and must not be freed. */
20395
20396 static line_header_up
20397 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20398 {
20399 struct dwarf2_section_info *section;
20400 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20401
20402 section = get_debug_line_section (cu);
20403 section->read (per_objfile->objfile);
20404 if (section->buffer == NULL)
20405 {
20406 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20407 complaint (_("missing .debug_line.dwo section"));
20408 else
20409 complaint (_("missing .debug_line section"));
20410 return 0;
20411 }
20412
20413 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
20414 per_objfile, section, &cu->header);
20415 }
20416
20417 /* Subroutine of dwarf_decode_lines to simplify it.
20418 Return the file name of the psymtab for the given file_entry.
20419 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20420 If space for the result is malloc'd, *NAME_HOLDER will be set.
20421 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20422
20423 static const char *
20424 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
20425 const dwarf2_psymtab *pst,
20426 const char *comp_dir,
20427 gdb::unique_xmalloc_ptr<char> *name_holder)
20428 {
20429 const char *include_name = fe.name;
20430 const char *include_name_to_compare = include_name;
20431 const char *pst_filename;
20432 int file_is_pst;
20433
20434 const char *dir_name = fe.include_dir (lh);
20435
20436 gdb::unique_xmalloc_ptr<char> hold_compare;
20437 if (!IS_ABSOLUTE_PATH (include_name)
20438 && (dir_name != NULL || comp_dir != NULL))
20439 {
20440 /* Avoid creating a duplicate psymtab for PST.
20441 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20442 Before we do the comparison, however, we need to account
20443 for DIR_NAME and COMP_DIR.
20444 First prepend dir_name (if non-NULL). If we still don't
20445 have an absolute path prepend comp_dir (if non-NULL).
20446 However, the directory we record in the include-file's
20447 psymtab does not contain COMP_DIR (to match the
20448 corresponding symtab(s)).
20449
20450 Example:
20451
20452 bash$ cd /tmp
20453 bash$ gcc -g ./hello.c
20454 include_name = "hello.c"
20455 dir_name = "."
20456 DW_AT_comp_dir = comp_dir = "/tmp"
20457 DW_AT_name = "./hello.c"
20458
20459 */
20460
20461 if (dir_name != NULL)
20462 {
20463 name_holder->reset (concat (dir_name, SLASH_STRING,
20464 include_name, (char *) NULL));
20465 include_name = name_holder->get ();
20466 include_name_to_compare = include_name;
20467 }
20468 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20469 {
20470 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20471 include_name, (char *) NULL));
20472 include_name_to_compare = hold_compare.get ();
20473 }
20474 }
20475
20476 pst_filename = pst->filename;
20477 gdb::unique_xmalloc_ptr<char> copied_name;
20478 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20479 {
20480 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20481 pst_filename, (char *) NULL));
20482 pst_filename = copied_name.get ();
20483 }
20484
20485 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20486
20487 if (file_is_pst)
20488 return NULL;
20489 return include_name;
20490 }
20491
20492 /* State machine to track the state of the line number program. */
20493
20494 class lnp_state_machine
20495 {
20496 public:
20497 /* Initialize a machine state for the start of a line number
20498 program. */
20499 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20500 bool record_lines_p);
20501
20502 file_entry *current_file ()
20503 {
20504 /* lh->file_names is 0-based, but the file name numbers in the
20505 statement program are 1-based. */
20506 return m_line_header->file_name_at (m_file);
20507 }
20508
20509 /* Record the line in the state machine. END_SEQUENCE is true if
20510 we're processing the end of a sequence. */
20511 void record_line (bool end_sequence);
20512
20513 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
20514 nop-out rest of the lines in this sequence. */
20515 void check_line_address (struct dwarf2_cu *cu,
20516 const gdb_byte *line_ptr,
20517 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20518
20519 void handle_set_discriminator (unsigned int discriminator)
20520 {
20521 m_discriminator = discriminator;
20522 m_line_has_non_zero_discriminator |= discriminator != 0;
20523 }
20524
20525 /* Handle DW_LNE_set_address. */
20526 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20527 {
20528 m_op_index = 0;
20529 address += baseaddr;
20530 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20531 }
20532
20533 /* Handle DW_LNS_advance_pc. */
20534 void handle_advance_pc (CORE_ADDR adjust);
20535
20536 /* Handle a special opcode. */
20537 void handle_special_opcode (unsigned char op_code);
20538
20539 /* Handle DW_LNS_advance_line. */
20540 void handle_advance_line (int line_delta)
20541 {
20542 advance_line (line_delta);
20543 }
20544
20545 /* Handle DW_LNS_set_file. */
20546 void handle_set_file (file_name_index file);
20547
20548 /* Handle DW_LNS_negate_stmt. */
20549 void handle_negate_stmt ()
20550 {
20551 m_is_stmt = !m_is_stmt;
20552 }
20553
20554 /* Handle DW_LNS_const_add_pc. */
20555 void handle_const_add_pc ();
20556
20557 /* Handle DW_LNS_fixed_advance_pc. */
20558 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20559 {
20560 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20561 m_op_index = 0;
20562 }
20563
20564 /* Handle DW_LNS_copy. */
20565 void handle_copy ()
20566 {
20567 record_line (false);
20568 m_discriminator = 0;
20569 }
20570
20571 /* Handle DW_LNE_end_sequence. */
20572 void handle_end_sequence ()
20573 {
20574 m_currently_recording_lines = true;
20575 }
20576
20577 private:
20578 /* Advance the line by LINE_DELTA. */
20579 void advance_line (int line_delta)
20580 {
20581 m_line += line_delta;
20582
20583 if (line_delta != 0)
20584 m_line_has_non_zero_discriminator = m_discriminator != 0;
20585 }
20586
20587 struct dwarf2_cu *m_cu;
20588
20589 gdbarch *m_gdbarch;
20590
20591 /* True if we're recording lines.
20592 Otherwise we're building partial symtabs and are just interested in
20593 finding include files mentioned by the line number program. */
20594 bool m_record_lines_p;
20595
20596 /* The line number header. */
20597 line_header *m_line_header;
20598
20599 /* These are part of the standard DWARF line number state machine,
20600 and initialized according to the DWARF spec. */
20601
20602 unsigned char m_op_index = 0;
20603 /* The line table index of the current file. */
20604 file_name_index m_file = 1;
20605 unsigned int m_line = 1;
20606
20607 /* These are initialized in the constructor. */
20608
20609 CORE_ADDR m_address;
20610 bool m_is_stmt;
20611 unsigned int m_discriminator;
20612
20613 /* Additional bits of state we need to track. */
20614
20615 /* The last file that we called dwarf2_start_subfile for.
20616 This is only used for TLLs. */
20617 unsigned int m_last_file = 0;
20618 /* The last file a line number was recorded for. */
20619 struct subfile *m_last_subfile = NULL;
20620
20621 /* The address of the last line entry. */
20622 CORE_ADDR m_last_address;
20623
20624 /* Set to true when a previous line at the same address (using
20625 m_last_address) had m_is_stmt true. This is reset to false when a
20626 line entry at a new address (m_address different to m_last_address) is
20627 processed. */
20628 bool m_stmt_at_address = false;
20629
20630 /* When true, record the lines we decode. */
20631 bool m_currently_recording_lines = false;
20632
20633 /* The last line number that was recorded, used to coalesce
20634 consecutive entries for the same line. This can happen, for
20635 example, when discriminators are present. PR 17276. */
20636 unsigned int m_last_line = 0;
20637 bool m_line_has_non_zero_discriminator = false;
20638 };
20639
20640 void
20641 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20642 {
20643 CORE_ADDR addr_adj = (((m_op_index + adjust)
20644 / m_line_header->maximum_ops_per_instruction)
20645 * m_line_header->minimum_instruction_length);
20646 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20647 m_op_index = ((m_op_index + adjust)
20648 % m_line_header->maximum_ops_per_instruction);
20649 }
20650
20651 void
20652 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20653 {
20654 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20655 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20656 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20657 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
20658 / m_line_header->maximum_ops_per_instruction)
20659 * m_line_header->minimum_instruction_length);
20660 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20661 m_op_index = ((m_op_index + adj_opcode_d)
20662 % m_line_header->maximum_ops_per_instruction);
20663
20664 int line_delta = m_line_header->line_base + adj_opcode_r;
20665 advance_line (line_delta);
20666 record_line (false);
20667 m_discriminator = 0;
20668 }
20669
20670 void
20671 lnp_state_machine::handle_set_file (file_name_index file)
20672 {
20673 m_file = file;
20674
20675 const file_entry *fe = current_file ();
20676 if (fe == NULL)
20677 dwarf2_debug_line_missing_file_complaint ();
20678 else if (m_record_lines_p)
20679 {
20680 const char *dir = fe->include_dir (m_line_header);
20681
20682 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20683 m_line_has_non_zero_discriminator = m_discriminator != 0;
20684 dwarf2_start_subfile (m_cu, fe->name, dir);
20685 }
20686 }
20687
20688 void
20689 lnp_state_machine::handle_const_add_pc ()
20690 {
20691 CORE_ADDR adjust
20692 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20693
20694 CORE_ADDR addr_adj
20695 = (((m_op_index + adjust)
20696 / m_line_header->maximum_ops_per_instruction)
20697 * m_line_header->minimum_instruction_length);
20698
20699 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20700 m_op_index = ((m_op_index + adjust)
20701 % m_line_header->maximum_ops_per_instruction);
20702 }
20703
20704 /* Return non-zero if we should add LINE to the line number table.
20705 LINE is the line to add, LAST_LINE is the last line that was added,
20706 LAST_SUBFILE is the subfile for LAST_LINE.
20707 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20708 had a non-zero discriminator.
20709
20710 We have to be careful in the presence of discriminators.
20711 E.g., for this line:
20712
20713 for (i = 0; i < 100000; i++);
20714
20715 clang can emit four line number entries for that one line,
20716 each with a different discriminator.
20717 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20718
20719 However, we want gdb to coalesce all four entries into one.
20720 Otherwise the user could stepi into the middle of the line and
20721 gdb would get confused about whether the pc really was in the
20722 middle of the line.
20723
20724 Things are further complicated by the fact that two consecutive
20725 line number entries for the same line is a heuristic used by gcc
20726 to denote the end of the prologue. So we can't just discard duplicate
20727 entries, we have to be selective about it. The heuristic we use is
20728 that we only collapse consecutive entries for the same line if at least
20729 one of those entries has a non-zero discriminator. PR 17276.
20730
20731 Note: Addresses in the line number state machine can never go backwards
20732 within one sequence, thus this coalescing is ok. */
20733
20734 static int
20735 dwarf_record_line_p (struct dwarf2_cu *cu,
20736 unsigned int line, unsigned int last_line,
20737 int line_has_non_zero_discriminator,
20738 struct subfile *last_subfile)
20739 {
20740 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20741 return 1;
20742 if (line != last_line)
20743 return 1;
20744 /* Same line for the same file that we've seen already.
20745 As a last check, for pr 17276, only record the line if the line
20746 has never had a non-zero discriminator. */
20747 if (!line_has_non_zero_discriminator)
20748 return 1;
20749 return 0;
20750 }
20751
20752 /* Use the CU's builder to record line number LINE beginning at
20753 address ADDRESS in the line table of subfile SUBFILE. */
20754
20755 static void
20756 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20757 unsigned int line, CORE_ADDR address, bool is_stmt,
20758 struct dwarf2_cu *cu)
20759 {
20760 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20761
20762 if (dwarf_line_debug)
20763 {
20764 fprintf_unfiltered (gdb_stdlog,
20765 "Recording line %u, file %s, address %s\n",
20766 line, lbasename (subfile->name),
20767 paddress (gdbarch, address));
20768 }
20769
20770 if (cu != nullptr)
20771 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
20772 }
20773
20774 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20775 Mark the end of a set of line number records.
20776 The arguments are the same as for dwarf_record_line_1.
20777 If SUBFILE is NULL the request is ignored. */
20778
20779 static void
20780 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20781 CORE_ADDR address, struct dwarf2_cu *cu)
20782 {
20783 if (subfile == NULL)
20784 return;
20785
20786 if (dwarf_line_debug)
20787 {
20788 fprintf_unfiltered (gdb_stdlog,
20789 "Finishing current line, file %s, address %s\n",
20790 lbasename (subfile->name),
20791 paddress (gdbarch, address));
20792 }
20793
20794 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
20795 }
20796
20797 void
20798 lnp_state_machine::record_line (bool end_sequence)
20799 {
20800 if (dwarf_line_debug)
20801 {
20802 fprintf_unfiltered (gdb_stdlog,
20803 "Processing actual line %u: file %u,"
20804 " address %s, is_stmt %u, discrim %u%s\n",
20805 m_line, m_file,
20806 paddress (m_gdbarch, m_address),
20807 m_is_stmt, m_discriminator,
20808 (end_sequence ? "\t(end sequence)" : ""));
20809 }
20810
20811 file_entry *fe = current_file ();
20812
20813 if (fe == NULL)
20814 dwarf2_debug_line_missing_file_complaint ();
20815 /* For now we ignore lines not starting on an instruction boundary.
20816 But not when processing end_sequence for compatibility with the
20817 previous version of the code. */
20818 else if (m_op_index == 0 || end_sequence)
20819 {
20820 fe->included_p = 1;
20821 if (m_record_lines_p)
20822 {
20823 /* When we switch files we insert an end maker in the first file,
20824 switch to the second file and add a new line entry. The
20825 problem is that the end marker inserted in the first file will
20826 discard any previous line entries at the same address. If the
20827 line entries in the first file are marked as is-stmt, while
20828 the new line in the second file is non-stmt, then this means
20829 the end marker will discard is-stmt lines so we can have a
20830 non-stmt line. This means that there are less addresses at
20831 which the user can insert a breakpoint.
20832
20833 To improve this we track the last address in m_last_address,
20834 and whether we have seen an is-stmt at this address. Then
20835 when switching files, if we have seen a stmt at the current
20836 address, and we are switching to create a non-stmt line, then
20837 discard the new line. */
20838 bool file_changed
20839 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
20840 bool ignore_this_line
20841 = ((file_changed && !end_sequence && m_last_address == m_address
20842 && !m_is_stmt && m_stmt_at_address)
20843 || (!end_sequence && m_line == 0));
20844
20845 if ((file_changed && !ignore_this_line) || end_sequence)
20846 {
20847 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20848 m_currently_recording_lines ? m_cu : nullptr);
20849 }
20850
20851 if (!end_sequence && !ignore_this_line)
20852 {
20853 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20854
20855 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20856 m_line_has_non_zero_discriminator,
20857 m_last_subfile))
20858 {
20859 buildsym_compunit *builder = m_cu->get_builder ();
20860 dwarf_record_line_1 (m_gdbarch,
20861 builder->get_current_subfile (),
20862 m_line, m_address, is_stmt,
20863 m_currently_recording_lines ? m_cu : nullptr);
20864 }
20865 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20866 m_last_line = m_line;
20867 }
20868 }
20869 }
20870
20871 /* Track whether we have seen any m_is_stmt true at m_address in case we
20872 have multiple line table entries all at m_address. */
20873 if (m_last_address != m_address)
20874 {
20875 m_stmt_at_address = false;
20876 m_last_address = m_address;
20877 }
20878 m_stmt_at_address |= m_is_stmt;
20879 }
20880
20881 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20882 line_header *lh, bool record_lines_p)
20883 {
20884 m_cu = cu;
20885 m_gdbarch = arch;
20886 m_record_lines_p = record_lines_p;
20887 m_line_header = lh;
20888
20889 m_currently_recording_lines = true;
20890
20891 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20892 was a line entry for it so that the backend has a chance to adjust it
20893 and also record it in case it needs it. This is currently used by MIPS
20894 code, cf. `mips_adjust_dwarf2_line'. */
20895 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20896 m_is_stmt = lh->default_is_stmt;
20897 m_discriminator = 0;
20898
20899 m_last_address = m_address;
20900 m_stmt_at_address = false;
20901 }
20902
20903 void
20904 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20905 const gdb_byte *line_ptr,
20906 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20907 {
20908 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
20909 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
20910 located at 0x0. In this case, additionally check that if
20911 ADDRESS < UNRELOCATED_LOWPC. */
20912
20913 if ((address == 0 && address < unrelocated_lowpc)
20914 || address == (CORE_ADDR) -1)
20915 {
20916 /* This line table is for a function which has been
20917 GCd by the linker. Ignore it. PR gdb/12528 */
20918
20919 struct objfile *objfile = cu->per_objfile->objfile;
20920 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20921
20922 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20923 line_offset, objfile_name (objfile));
20924 m_currently_recording_lines = false;
20925 /* Note: m_currently_recording_lines is left as false until we see
20926 DW_LNE_end_sequence. */
20927 }
20928 }
20929
20930 /* Subroutine of dwarf_decode_lines to simplify it.
20931 Process the line number information in LH.
20932 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20933 program in order to set included_p for every referenced header. */
20934
20935 static void
20936 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20937 const int decode_for_pst_p, CORE_ADDR lowpc)
20938 {
20939 const gdb_byte *line_ptr, *extended_end;
20940 const gdb_byte *line_end;
20941 unsigned int bytes_read, extended_len;
20942 unsigned char op_code, extended_op;
20943 CORE_ADDR baseaddr;
20944 struct objfile *objfile = cu->per_objfile->objfile;
20945 bfd *abfd = objfile->obfd;
20946 struct gdbarch *gdbarch = objfile->arch ();
20947 /* True if we're recording line info (as opposed to building partial
20948 symtabs and just interested in finding include files mentioned by
20949 the line number program). */
20950 bool record_lines_p = !decode_for_pst_p;
20951
20952 baseaddr = objfile->text_section_offset ();
20953
20954 line_ptr = lh->statement_program_start;
20955 line_end = lh->statement_program_end;
20956
20957 /* Read the statement sequences until there's nothing left. */
20958 while (line_ptr < line_end)
20959 {
20960 /* The DWARF line number program state machine. Reset the state
20961 machine at the start of each sequence. */
20962 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20963 bool end_sequence = false;
20964
20965 if (record_lines_p)
20966 {
20967 /* Start a subfile for the current file of the state
20968 machine. */
20969 const file_entry *fe = state_machine.current_file ();
20970
20971 if (fe != NULL)
20972 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20973 }
20974
20975 /* Decode the table. */
20976 while (line_ptr < line_end && !end_sequence)
20977 {
20978 op_code = read_1_byte (abfd, line_ptr);
20979 line_ptr += 1;
20980
20981 if (op_code >= lh->opcode_base)
20982 {
20983 /* Special opcode. */
20984 state_machine.handle_special_opcode (op_code);
20985 }
20986 else switch (op_code)
20987 {
20988 case DW_LNS_extended_op:
20989 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20990 &bytes_read);
20991 line_ptr += bytes_read;
20992 extended_end = line_ptr + extended_len;
20993 extended_op = read_1_byte (abfd, line_ptr);
20994 line_ptr += 1;
20995 if (DW_LNE_lo_user <= extended_op
20996 && extended_op <= DW_LNE_hi_user)
20997 {
20998 /* Vendor extension, ignore. */
20999 line_ptr = extended_end;
21000 break;
21001 }
21002 switch (extended_op)
21003 {
21004 case DW_LNE_end_sequence:
21005 state_machine.handle_end_sequence ();
21006 end_sequence = true;
21007 break;
21008 case DW_LNE_set_address:
21009 {
21010 CORE_ADDR address
21011 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21012 line_ptr += bytes_read;
21013
21014 state_machine.check_line_address (cu, line_ptr,
21015 lowpc - baseaddr, address);
21016 state_machine.handle_set_address (baseaddr, address);
21017 }
21018 break;
21019 case DW_LNE_define_file:
21020 {
21021 const char *cur_file;
21022 unsigned int mod_time, length;
21023 dir_index dindex;
21024
21025 cur_file = read_direct_string (abfd, line_ptr,
21026 &bytes_read);
21027 line_ptr += bytes_read;
21028 dindex = (dir_index)
21029 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21030 line_ptr += bytes_read;
21031 mod_time =
21032 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21033 line_ptr += bytes_read;
21034 length =
21035 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21036 line_ptr += bytes_read;
21037 lh->add_file_name (cur_file, dindex, mod_time, length);
21038 }
21039 break;
21040 case DW_LNE_set_discriminator:
21041 {
21042 /* The discriminator is not interesting to the
21043 debugger; just ignore it. We still need to
21044 check its value though:
21045 if there are consecutive entries for the same
21046 (non-prologue) line we want to coalesce them.
21047 PR 17276. */
21048 unsigned int discr
21049 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21050 line_ptr += bytes_read;
21051
21052 state_machine.handle_set_discriminator (discr);
21053 }
21054 break;
21055 default:
21056 complaint (_("mangled .debug_line section"));
21057 return;
21058 }
21059 /* Make sure that we parsed the extended op correctly. If e.g.
21060 we expected a different address size than the producer used,
21061 we may have read the wrong number of bytes. */
21062 if (line_ptr != extended_end)
21063 {
21064 complaint (_("mangled .debug_line section"));
21065 return;
21066 }
21067 break;
21068 case DW_LNS_copy:
21069 state_machine.handle_copy ();
21070 break;
21071 case DW_LNS_advance_pc:
21072 {
21073 CORE_ADDR adjust
21074 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21075 line_ptr += bytes_read;
21076
21077 state_machine.handle_advance_pc (adjust);
21078 }
21079 break;
21080 case DW_LNS_advance_line:
21081 {
21082 int line_delta
21083 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21084 line_ptr += bytes_read;
21085
21086 state_machine.handle_advance_line (line_delta);
21087 }
21088 break;
21089 case DW_LNS_set_file:
21090 {
21091 file_name_index file
21092 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21093 &bytes_read);
21094 line_ptr += bytes_read;
21095
21096 state_machine.handle_set_file (file);
21097 }
21098 break;
21099 case DW_LNS_set_column:
21100 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21101 line_ptr += bytes_read;
21102 break;
21103 case DW_LNS_negate_stmt:
21104 state_machine.handle_negate_stmt ();
21105 break;
21106 case DW_LNS_set_basic_block:
21107 break;
21108 /* Add to the address register of the state machine the
21109 address increment value corresponding to special opcode
21110 255. I.e., this value is scaled by the minimum
21111 instruction length since special opcode 255 would have
21112 scaled the increment. */
21113 case DW_LNS_const_add_pc:
21114 state_machine.handle_const_add_pc ();
21115 break;
21116 case DW_LNS_fixed_advance_pc:
21117 {
21118 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21119 line_ptr += 2;
21120
21121 state_machine.handle_fixed_advance_pc (addr_adj);
21122 }
21123 break;
21124 default:
21125 {
21126 /* Unknown standard opcode, ignore it. */
21127 int i;
21128
21129 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21130 {
21131 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21132 line_ptr += bytes_read;
21133 }
21134 }
21135 }
21136 }
21137
21138 if (!end_sequence)
21139 dwarf2_debug_line_missing_end_sequence_complaint ();
21140
21141 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21142 in which case we still finish recording the last line). */
21143 state_machine.record_line (true);
21144 }
21145 }
21146
21147 /* Decode the Line Number Program (LNP) for the given line_header
21148 structure and CU. The actual information extracted and the type
21149 of structures created from the LNP depends on the value of PST.
21150
21151 1. If PST is NULL, then this procedure uses the data from the program
21152 to create all necessary symbol tables, and their linetables.
21153
21154 2. If PST is not NULL, this procedure reads the program to determine
21155 the list of files included by the unit represented by PST, and
21156 builds all the associated partial symbol tables.
21157
21158 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21159 It is used for relative paths in the line table.
21160 NOTE: When processing partial symtabs (pst != NULL),
21161 comp_dir == pst->dirname.
21162
21163 NOTE: It is important that psymtabs have the same file name (via strcmp)
21164 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21165 symtab we don't use it in the name of the psymtabs we create.
21166 E.g. expand_line_sal requires this when finding psymtabs to expand.
21167 A good testcase for this is mb-inline.exp.
21168
21169 LOWPC is the lowest address in CU (or 0 if not known).
21170
21171 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21172 for its PC<->lines mapping information. Otherwise only the filename
21173 table is read in. */
21174
21175 static void
21176 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21177 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21178 CORE_ADDR lowpc, int decode_mapping)
21179 {
21180 struct objfile *objfile = cu->per_objfile->objfile;
21181 const int decode_for_pst_p = (pst != NULL);
21182
21183 if (decode_mapping)
21184 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21185
21186 if (decode_for_pst_p)
21187 {
21188 /* Now that we're done scanning the Line Header Program, we can
21189 create the psymtab of each included file. */
21190 for (auto &file_entry : lh->file_names ())
21191 if (file_entry.included_p == 1)
21192 {
21193 gdb::unique_xmalloc_ptr<char> name_holder;
21194 const char *include_name =
21195 psymtab_include_file_name (lh, file_entry, pst,
21196 comp_dir, &name_holder);
21197 if (include_name != NULL)
21198 dwarf2_create_include_psymtab (include_name, pst, objfile);
21199 }
21200 }
21201 else
21202 {
21203 /* Make sure a symtab is created for every file, even files
21204 which contain only variables (i.e. no code with associated
21205 line numbers). */
21206 buildsym_compunit *builder = cu->get_builder ();
21207 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21208
21209 for (auto &fe : lh->file_names ())
21210 {
21211 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21212 if (builder->get_current_subfile ()->symtab == NULL)
21213 {
21214 builder->get_current_subfile ()->symtab
21215 = allocate_symtab (cust,
21216 builder->get_current_subfile ()->name);
21217 }
21218 fe.symtab = builder->get_current_subfile ()->symtab;
21219 }
21220 }
21221 }
21222
21223 /* Start a subfile for DWARF. FILENAME is the name of the file and
21224 DIRNAME the name of the source directory which contains FILENAME
21225 or NULL if not known.
21226 This routine tries to keep line numbers from identical absolute and
21227 relative file names in a common subfile.
21228
21229 Using the `list' example from the GDB testsuite, which resides in
21230 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21231 of /srcdir/list0.c yields the following debugging information for list0.c:
21232
21233 DW_AT_name: /srcdir/list0.c
21234 DW_AT_comp_dir: /compdir
21235 files.files[0].name: list0.h
21236 files.files[0].dir: /srcdir
21237 files.files[1].name: list0.c
21238 files.files[1].dir: /srcdir
21239
21240 The line number information for list0.c has to end up in a single
21241 subfile, so that `break /srcdir/list0.c:1' works as expected.
21242 start_subfile will ensure that this happens provided that we pass the
21243 concatenation of files.files[1].dir and files.files[1].name as the
21244 subfile's name. */
21245
21246 static void
21247 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21248 const char *dirname)
21249 {
21250 gdb::unique_xmalloc_ptr<char> copy;
21251
21252 /* In order not to lose the line information directory,
21253 we concatenate it to the filename when it makes sense.
21254 Note that the Dwarf3 standard says (speaking of filenames in line
21255 information): ``The directory index is ignored for file names
21256 that represent full path names''. Thus ignoring dirname in the
21257 `else' branch below isn't an issue. */
21258
21259 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21260 {
21261 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21262 filename = copy.get ();
21263 }
21264
21265 cu->get_builder ()->start_subfile (filename);
21266 }
21267
21268 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21269 buildsym_compunit constructor. */
21270
21271 struct compunit_symtab *
21272 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21273 CORE_ADDR low_pc)
21274 {
21275 gdb_assert (m_builder == nullptr);
21276
21277 m_builder.reset (new struct buildsym_compunit
21278 (this->per_objfile->objfile,
21279 name, comp_dir, language, low_pc));
21280
21281 list_in_scope = get_builder ()->get_file_symbols ();
21282
21283 get_builder ()->record_debugformat ("DWARF 2");
21284 get_builder ()->record_producer (producer);
21285
21286 processing_has_namespace_info = false;
21287
21288 return get_builder ()->get_compunit_symtab ();
21289 }
21290
21291 static void
21292 var_decode_location (struct attribute *attr, struct symbol *sym,
21293 struct dwarf2_cu *cu)
21294 {
21295 struct objfile *objfile = cu->per_objfile->objfile;
21296 struct comp_unit_head *cu_header = &cu->header;
21297
21298 /* NOTE drow/2003-01-30: There used to be a comment and some special
21299 code here to turn a symbol with DW_AT_external and a
21300 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21301 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21302 with some versions of binutils) where shared libraries could have
21303 relocations against symbols in their debug information - the
21304 minimal symbol would have the right address, but the debug info
21305 would not. It's no longer necessary, because we will explicitly
21306 apply relocations when we read in the debug information now. */
21307
21308 /* A DW_AT_location attribute with no contents indicates that a
21309 variable has been optimized away. */
21310 if (attr->form_is_block () && attr->as_block ()->size == 0)
21311 {
21312 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21313 return;
21314 }
21315
21316 /* Handle one degenerate form of location expression specially, to
21317 preserve GDB's previous behavior when section offsets are
21318 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21319 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21320
21321 if (attr->form_is_block ())
21322 {
21323 struct dwarf_block *block = attr->as_block ();
21324
21325 if ((block->data[0] == DW_OP_addr
21326 && block->size == 1 + cu_header->addr_size)
21327 || ((block->data[0] == DW_OP_GNU_addr_index
21328 || block->data[0] == DW_OP_addrx)
21329 && (block->size
21330 == 1 + leb128_size (&block->data[1]))))
21331 {
21332 unsigned int dummy;
21333
21334 if (block->data[0] == DW_OP_addr)
21335 SET_SYMBOL_VALUE_ADDRESS
21336 (sym, cu->header.read_address (objfile->obfd,
21337 block->data + 1,
21338 &dummy));
21339 else
21340 SET_SYMBOL_VALUE_ADDRESS
21341 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
21342 &dummy));
21343 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21344 fixup_symbol_section (sym, objfile);
21345 SET_SYMBOL_VALUE_ADDRESS
21346 (sym,
21347 SYMBOL_VALUE_ADDRESS (sym)
21348 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
21349 return;
21350 }
21351 }
21352
21353 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21354 expression evaluator, and use LOC_COMPUTED only when necessary
21355 (i.e. when the value of a register or memory location is
21356 referenced, or a thread-local block, etc.). Then again, it might
21357 not be worthwhile. I'm assuming that it isn't unless performance
21358 or memory numbers show me otherwise. */
21359
21360 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21361
21362 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21363 cu->has_loclist = true;
21364 }
21365
21366 /* Given a pointer to a DWARF information entry, figure out if we need
21367 to make a symbol table entry for it, and if so, create a new entry
21368 and return a pointer to it.
21369 If TYPE is NULL, determine symbol type from the die, otherwise
21370 used the passed type.
21371 If SPACE is not NULL, use it to hold the new symbol. If it is
21372 NULL, allocate a new symbol on the objfile's obstack. */
21373
21374 static struct symbol *
21375 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21376 struct symbol *space)
21377 {
21378 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21379 struct objfile *objfile = per_objfile->objfile;
21380 struct gdbarch *gdbarch = objfile->arch ();
21381 struct symbol *sym = NULL;
21382 const char *name;
21383 struct attribute *attr = NULL;
21384 struct attribute *attr2 = NULL;
21385 CORE_ADDR baseaddr;
21386 struct pending **list_to_add = NULL;
21387
21388 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21389
21390 baseaddr = objfile->text_section_offset ();
21391
21392 name = dwarf2_name (die, cu);
21393 if (name)
21394 {
21395 int suppress_add = 0;
21396
21397 if (space)
21398 sym = space;
21399 else
21400 sym = new (&objfile->objfile_obstack) symbol;
21401 OBJSTAT (objfile, n_syms++);
21402
21403 /* Cache this symbol's name and the name's demangled form (if any). */
21404 sym->set_language (cu->language, &objfile->objfile_obstack);
21405 /* Fortran does not have mangling standard and the mangling does differ
21406 between gfortran, iFort etc. */
21407 const char *physname
21408 = (cu->language == language_fortran
21409 ? dwarf2_full_name (name, die, cu)
21410 : dwarf2_physname (name, die, cu));
21411 const char *linkagename = dw2_linkage_name (die, cu);
21412
21413 if (linkagename == nullptr || cu->language == language_ada)
21414 sym->set_linkage_name (physname);
21415 else
21416 {
21417 sym->set_demangled_name (physname, &objfile->objfile_obstack);
21418 sym->set_linkage_name (linkagename);
21419 }
21420
21421 /* Default assumptions.
21422 Use the passed type or decode it from the die. */
21423 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21424 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21425 if (type != NULL)
21426 SYMBOL_TYPE (sym) = type;
21427 else
21428 SYMBOL_TYPE (sym) = die_type (die, cu);
21429 attr = dwarf2_attr (die,
21430 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21431 cu);
21432 if (attr != nullptr)
21433 SYMBOL_LINE (sym) = attr->constant_value (0);
21434
21435 attr = dwarf2_attr (die,
21436 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21437 cu);
21438 if (attr != nullptr && attr->form_is_unsigned ())
21439 {
21440 file_name_index file_index
21441 = (file_name_index) attr->as_unsigned ();
21442 struct file_entry *fe;
21443
21444 if (cu->line_header != NULL)
21445 fe = cu->line_header->file_name_at (file_index);
21446 else
21447 fe = NULL;
21448
21449 if (fe == NULL)
21450 complaint (_("file index out of range"));
21451 else
21452 symbol_set_symtab (sym, fe->symtab);
21453 }
21454
21455 switch (die->tag)
21456 {
21457 case DW_TAG_label:
21458 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21459 if (attr != nullptr)
21460 {
21461 CORE_ADDR addr;
21462
21463 addr = attr->as_address ();
21464 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21465 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21466 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21467 }
21468 else
21469 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21470 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21471 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21472 add_symbol_to_list (sym, cu->list_in_scope);
21473 break;
21474 case DW_TAG_subprogram:
21475 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21476 finish_block. */
21477 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21478 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21479 if ((attr2 != nullptr && attr2->as_boolean ())
21480 || cu->language == language_ada
21481 || cu->language == language_fortran)
21482 {
21483 /* Subprograms marked external are stored as a global symbol.
21484 Ada and Fortran subprograms, whether marked external or
21485 not, are always stored as a global symbol, because we want
21486 to be able to access them globally. For instance, we want
21487 to be able to break on a nested subprogram without having
21488 to specify the context. */
21489 list_to_add = cu->get_builder ()->get_global_symbols ();
21490 }
21491 else
21492 {
21493 list_to_add = cu->list_in_scope;
21494 }
21495 break;
21496 case DW_TAG_inlined_subroutine:
21497 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21498 finish_block. */
21499 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21500 SYMBOL_INLINED (sym) = 1;
21501 list_to_add = cu->list_in_scope;
21502 break;
21503 case DW_TAG_template_value_param:
21504 suppress_add = 1;
21505 /* Fall through. */
21506 case DW_TAG_constant:
21507 case DW_TAG_variable:
21508 case DW_TAG_member:
21509 /* Compilation with minimal debug info may result in
21510 variables with missing type entries. Change the
21511 misleading `void' type to something sensible. */
21512 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
21513 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21514
21515 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21516 /* In the case of DW_TAG_member, we should only be called for
21517 static const members. */
21518 if (die->tag == DW_TAG_member)
21519 {
21520 /* dwarf2_add_field uses die_is_declaration,
21521 so we do the same. */
21522 gdb_assert (die_is_declaration (die, cu));
21523 gdb_assert (attr);
21524 }
21525 if (attr != nullptr)
21526 {
21527 dwarf2_const_value (attr, sym, cu);
21528 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21529 if (!suppress_add)
21530 {
21531 if (attr2 != nullptr && attr2->as_boolean ())
21532 list_to_add = cu->get_builder ()->get_global_symbols ();
21533 else
21534 list_to_add = cu->list_in_scope;
21535 }
21536 break;
21537 }
21538 attr = dwarf2_attr (die, DW_AT_location, cu);
21539 if (attr != nullptr)
21540 {
21541 var_decode_location (attr, sym, cu);
21542 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21543
21544 /* Fortran explicitly imports any global symbols to the local
21545 scope by DW_TAG_common_block. */
21546 if (cu->language == language_fortran && die->parent
21547 && die->parent->tag == DW_TAG_common_block)
21548 attr2 = NULL;
21549
21550 if (SYMBOL_CLASS (sym) == LOC_STATIC
21551 && SYMBOL_VALUE_ADDRESS (sym) == 0
21552 && !per_objfile->per_bfd->has_section_at_zero)
21553 {
21554 /* When a static variable is eliminated by the linker,
21555 the corresponding debug information is not stripped
21556 out, but the variable address is set to null;
21557 do not add such variables into symbol table. */
21558 }
21559 else if (attr2 != nullptr && attr2->as_boolean ())
21560 {
21561 if (SYMBOL_CLASS (sym) == LOC_STATIC
21562 && (objfile->flags & OBJF_MAINLINE) == 0
21563 && per_objfile->per_bfd->can_copy)
21564 {
21565 /* A global static variable might be subject to
21566 copy relocation. We first check for a local
21567 minsym, though, because maybe the symbol was
21568 marked hidden, in which case this would not
21569 apply. */
21570 bound_minimal_symbol found
21571 = (lookup_minimal_symbol_linkage
21572 (sym->linkage_name (), objfile));
21573 if (found.minsym != nullptr)
21574 sym->maybe_copied = 1;
21575 }
21576
21577 /* A variable with DW_AT_external is never static,
21578 but it may be block-scoped. */
21579 list_to_add
21580 = ((cu->list_in_scope
21581 == cu->get_builder ()->get_file_symbols ())
21582 ? cu->get_builder ()->get_global_symbols ()
21583 : cu->list_in_scope);
21584 }
21585 else
21586 list_to_add = cu->list_in_scope;
21587 }
21588 else
21589 {
21590 /* We do not know the address of this symbol.
21591 If it is an external symbol and we have type information
21592 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21593 The address of the variable will then be determined from
21594 the minimal symbol table whenever the variable is
21595 referenced. */
21596 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21597
21598 /* Fortran explicitly imports any global symbols to the local
21599 scope by DW_TAG_common_block. */
21600 if (cu->language == language_fortran && die->parent
21601 && die->parent->tag == DW_TAG_common_block)
21602 {
21603 /* SYMBOL_CLASS doesn't matter here because
21604 read_common_block is going to reset it. */
21605 if (!suppress_add)
21606 list_to_add = cu->list_in_scope;
21607 }
21608 else if (attr2 != nullptr && attr2->as_boolean ()
21609 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21610 {
21611 /* A variable with DW_AT_external is never static, but it
21612 may be block-scoped. */
21613 list_to_add
21614 = ((cu->list_in_scope
21615 == cu->get_builder ()->get_file_symbols ())
21616 ? cu->get_builder ()->get_global_symbols ()
21617 : cu->list_in_scope);
21618
21619 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21620 }
21621 else if (!die_is_declaration (die, cu))
21622 {
21623 /* Use the default LOC_OPTIMIZED_OUT class. */
21624 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21625 if (!suppress_add)
21626 list_to_add = cu->list_in_scope;
21627 }
21628 }
21629 break;
21630 case DW_TAG_formal_parameter:
21631 {
21632 /* If we are inside a function, mark this as an argument. If
21633 not, we might be looking at an argument to an inlined function
21634 when we do not have enough information to show inlined frames;
21635 pretend it's a local variable in that case so that the user can
21636 still see it. */
21637 struct context_stack *curr
21638 = cu->get_builder ()->get_current_context_stack ();
21639 if (curr != nullptr && curr->name != nullptr)
21640 SYMBOL_IS_ARGUMENT (sym) = 1;
21641 attr = dwarf2_attr (die, DW_AT_location, cu);
21642 if (attr != nullptr)
21643 {
21644 var_decode_location (attr, sym, cu);
21645 }
21646 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21647 if (attr != nullptr)
21648 {
21649 dwarf2_const_value (attr, sym, cu);
21650 }
21651
21652 list_to_add = cu->list_in_scope;
21653 }
21654 break;
21655 case DW_TAG_unspecified_parameters:
21656 /* From varargs functions; gdb doesn't seem to have any
21657 interest in this information, so just ignore it for now.
21658 (FIXME?) */
21659 break;
21660 case DW_TAG_template_type_param:
21661 suppress_add = 1;
21662 /* Fall through. */
21663 case DW_TAG_class_type:
21664 case DW_TAG_interface_type:
21665 case DW_TAG_structure_type:
21666 case DW_TAG_union_type:
21667 case DW_TAG_set_type:
21668 case DW_TAG_enumeration_type:
21669 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21670 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21671
21672 {
21673 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21674 really ever be static objects: otherwise, if you try
21675 to, say, break of a class's method and you're in a file
21676 which doesn't mention that class, it won't work unless
21677 the check for all static symbols in lookup_symbol_aux
21678 saves you. See the OtherFileClass tests in
21679 gdb.c++/namespace.exp. */
21680
21681 if (!suppress_add)
21682 {
21683 buildsym_compunit *builder = cu->get_builder ();
21684 list_to_add
21685 = (cu->list_in_scope == builder->get_file_symbols ()
21686 && cu->language == language_cplus
21687 ? builder->get_global_symbols ()
21688 : cu->list_in_scope);
21689
21690 /* The semantics of C++ state that "struct foo {
21691 ... }" also defines a typedef for "foo". */
21692 if (cu->language == language_cplus
21693 || cu->language == language_ada
21694 || cu->language == language_d
21695 || cu->language == language_rust)
21696 {
21697 /* The symbol's name is already allocated along
21698 with this objfile, so we don't need to
21699 duplicate it for the type. */
21700 if (SYMBOL_TYPE (sym)->name () == 0)
21701 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
21702 }
21703 }
21704 }
21705 break;
21706 case DW_TAG_typedef:
21707 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21708 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21709 list_to_add = cu->list_in_scope;
21710 break;
21711 case DW_TAG_base_type:
21712 case DW_TAG_subrange_type:
21713 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21714 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21715 list_to_add = cu->list_in_scope;
21716 break;
21717 case DW_TAG_enumerator:
21718 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21719 if (attr != nullptr)
21720 {
21721 dwarf2_const_value (attr, sym, cu);
21722 }
21723 {
21724 /* NOTE: carlton/2003-11-10: See comment above in the
21725 DW_TAG_class_type, etc. block. */
21726
21727 list_to_add
21728 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21729 && cu->language == language_cplus
21730 ? cu->get_builder ()->get_global_symbols ()
21731 : cu->list_in_scope);
21732 }
21733 break;
21734 case DW_TAG_imported_declaration:
21735 case DW_TAG_namespace:
21736 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21737 list_to_add = cu->get_builder ()->get_global_symbols ();
21738 break;
21739 case DW_TAG_module:
21740 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21741 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21742 list_to_add = cu->get_builder ()->get_global_symbols ();
21743 break;
21744 case DW_TAG_common_block:
21745 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21746 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21747 add_symbol_to_list (sym, cu->list_in_scope);
21748 break;
21749 default:
21750 /* Not a tag we recognize. Hopefully we aren't processing
21751 trash data, but since we must specifically ignore things
21752 we don't recognize, there is nothing else we should do at
21753 this point. */
21754 complaint (_("unsupported tag: '%s'"),
21755 dwarf_tag_name (die->tag));
21756 break;
21757 }
21758
21759 if (suppress_add)
21760 {
21761 sym->hash_next = objfile->template_symbols;
21762 objfile->template_symbols = sym;
21763 list_to_add = NULL;
21764 }
21765
21766 if (list_to_add != NULL)
21767 add_symbol_to_list (sym, list_to_add);
21768
21769 /* For the benefit of old versions of GCC, check for anonymous
21770 namespaces based on the demangled name. */
21771 if (!cu->processing_has_namespace_info
21772 && cu->language == language_cplus)
21773 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21774 }
21775 return (sym);
21776 }
21777
21778 /* Given an attr with a DW_FORM_dataN value in host byte order,
21779 zero-extend it as appropriate for the symbol's type. The DWARF
21780 standard (v4) is not entirely clear about the meaning of using
21781 DW_FORM_dataN for a constant with a signed type, where the type is
21782 wider than the data. The conclusion of a discussion on the DWARF
21783 list was that this is unspecified. We choose to always zero-extend
21784 because that is the interpretation long in use by GCC. */
21785
21786 static gdb_byte *
21787 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21788 struct dwarf2_cu *cu, LONGEST *value, int bits)
21789 {
21790 struct objfile *objfile = cu->per_objfile->objfile;
21791 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21792 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21793 LONGEST l = attr->constant_value (0);
21794
21795 if (bits < sizeof (*value) * 8)
21796 {
21797 l &= ((LONGEST) 1 << bits) - 1;
21798 *value = l;
21799 }
21800 else if (bits == sizeof (*value) * 8)
21801 *value = l;
21802 else
21803 {
21804 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21805 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21806 return bytes;
21807 }
21808
21809 return NULL;
21810 }
21811
21812 /* Read a constant value from an attribute. Either set *VALUE, or if
21813 the value does not fit in *VALUE, set *BYTES - either already
21814 allocated on the objfile obstack, or newly allocated on OBSTACK,
21815 or, set *BATON, if we translated the constant to a location
21816 expression. */
21817
21818 static void
21819 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21820 const char *name, struct obstack *obstack,
21821 struct dwarf2_cu *cu,
21822 LONGEST *value, const gdb_byte **bytes,
21823 struct dwarf2_locexpr_baton **baton)
21824 {
21825 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21826 struct objfile *objfile = per_objfile->objfile;
21827 struct comp_unit_head *cu_header = &cu->header;
21828 struct dwarf_block *blk;
21829 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21830 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21831
21832 *value = 0;
21833 *bytes = NULL;
21834 *baton = NULL;
21835
21836 switch (attr->form)
21837 {
21838 case DW_FORM_addr:
21839 case DW_FORM_addrx:
21840 case DW_FORM_GNU_addr_index:
21841 {
21842 gdb_byte *data;
21843
21844 if (TYPE_LENGTH (type) != cu_header->addr_size)
21845 dwarf2_const_value_length_mismatch_complaint (name,
21846 cu_header->addr_size,
21847 TYPE_LENGTH (type));
21848 /* Symbols of this form are reasonably rare, so we just
21849 piggyback on the existing location code rather than writing
21850 a new implementation of symbol_computed_ops. */
21851 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21852 (*baton)->per_objfile = per_objfile;
21853 (*baton)->per_cu = cu->per_cu;
21854 gdb_assert ((*baton)->per_cu);
21855
21856 (*baton)->size = 2 + cu_header->addr_size;
21857 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21858 (*baton)->data = data;
21859
21860 data[0] = DW_OP_addr;
21861 store_unsigned_integer (&data[1], cu_header->addr_size,
21862 byte_order, attr->as_address ());
21863 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21864 }
21865 break;
21866 case DW_FORM_string:
21867 case DW_FORM_strp:
21868 case DW_FORM_strx:
21869 case DW_FORM_GNU_str_index:
21870 case DW_FORM_GNU_strp_alt:
21871 /* The string is already allocated on the objfile obstack, point
21872 directly to it. */
21873 *bytes = (const gdb_byte *) attr->as_string ();
21874 break;
21875 case DW_FORM_block1:
21876 case DW_FORM_block2:
21877 case DW_FORM_block4:
21878 case DW_FORM_block:
21879 case DW_FORM_exprloc:
21880 case DW_FORM_data16:
21881 blk = attr->as_block ();
21882 if (TYPE_LENGTH (type) != blk->size)
21883 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21884 TYPE_LENGTH (type));
21885 *bytes = blk->data;
21886 break;
21887
21888 /* The DW_AT_const_value attributes are supposed to carry the
21889 symbol's value "represented as it would be on the target
21890 architecture." By the time we get here, it's already been
21891 converted to host endianness, so we just need to sign- or
21892 zero-extend it as appropriate. */
21893 case DW_FORM_data1:
21894 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21895 break;
21896 case DW_FORM_data2:
21897 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21898 break;
21899 case DW_FORM_data4:
21900 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21901 break;
21902 case DW_FORM_data8:
21903 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21904 break;
21905
21906 case DW_FORM_sdata:
21907 case DW_FORM_implicit_const:
21908 *value = attr->as_signed ();
21909 break;
21910
21911 case DW_FORM_udata:
21912 *value = attr->as_unsigned ();
21913 break;
21914
21915 default:
21916 complaint (_("unsupported const value attribute form: '%s'"),
21917 dwarf_form_name (attr->form));
21918 *value = 0;
21919 break;
21920 }
21921 }
21922
21923
21924 /* Copy constant value from an attribute to a symbol. */
21925
21926 static void
21927 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21928 struct dwarf2_cu *cu)
21929 {
21930 struct objfile *objfile = cu->per_objfile->objfile;
21931 LONGEST value;
21932 const gdb_byte *bytes;
21933 struct dwarf2_locexpr_baton *baton;
21934
21935 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21936 sym->print_name (),
21937 &objfile->objfile_obstack, cu,
21938 &value, &bytes, &baton);
21939
21940 if (baton != NULL)
21941 {
21942 SYMBOL_LOCATION_BATON (sym) = baton;
21943 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21944 }
21945 else if (bytes != NULL)
21946 {
21947 SYMBOL_VALUE_BYTES (sym) = bytes;
21948 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21949 }
21950 else
21951 {
21952 SYMBOL_VALUE (sym) = value;
21953 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21954 }
21955 }
21956
21957 /* Return the type of the die in question using its DW_AT_type attribute. */
21958
21959 static struct type *
21960 die_type (struct die_info *die, struct dwarf2_cu *cu)
21961 {
21962 struct attribute *type_attr;
21963
21964 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21965 if (!type_attr)
21966 {
21967 struct objfile *objfile = cu->per_objfile->objfile;
21968 /* A missing DW_AT_type represents a void type. */
21969 return objfile_type (objfile)->builtin_void;
21970 }
21971
21972 return lookup_die_type (die, type_attr, cu);
21973 }
21974
21975 /* True iff CU's producer generates GNAT Ada auxiliary information
21976 that allows to find parallel types through that information instead
21977 of having to do expensive parallel lookups by type name. */
21978
21979 static int
21980 need_gnat_info (struct dwarf2_cu *cu)
21981 {
21982 /* Assume that the Ada compiler was GNAT, which always produces
21983 the auxiliary information. */
21984 return (cu->language == language_ada);
21985 }
21986
21987 /* Return the auxiliary type of the die in question using its
21988 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21989 attribute is not present. */
21990
21991 static struct type *
21992 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21993 {
21994 struct attribute *type_attr;
21995
21996 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21997 if (!type_attr)
21998 return NULL;
21999
22000 return lookup_die_type (die, type_attr, cu);
22001 }
22002
22003 /* If DIE has a descriptive_type attribute, then set the TYPE's
22004 descriptive type accordingly. */
22005
22006 static void
22007 set_descriptive_type (struct type *type, struct die_info *die,
22008 struct dwarf2_cu *cu)
22009 {
22010 struct type *descriptive_type = die_descriptive_type (die, cu);
22011
22012 if (descriptive_type)
22013 {
22014 ALLOCATE_GNAT_AUX_TYPE (type);
22015 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22016 }
22017 }
22018
22019 /* Return the containing type of the die in question using its
22020 DW_AT_containing_type attribute. */
22021
22022 static struct type *
22023 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22024 {
22025 struct attribute *type_attr;
22026 struct objfile *objfile = cu->per_objfile->objfile;
22027
22028 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22029 if (!type_attr)
22030 error (_("Dwarf Error: Problem turning containing type into gdb type "
22031 "[in module %s]"), objfile_name (objfile));
22032
22033 return lookup_die_type (die, type_attr, cu);
22034 }
22035
22036 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22037
22038 static struct type *
22039 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22040 {
22041 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22042 struct objfile *objfile = per_objfile->objfile;
22043 char *saved;
22044
22045 std::string message
22046 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22047 objfile_name (objfile),
22048 sect_offset_str (cu->header.sect_off),
22049 sect_offset_str (die->sect_off));
22050 saved = obstack_strdup (&objfile->objfile_obstack, message);
22051
22052 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22053 }
22054
22055 /* Look up the type of DIE in CU using its type attribute ATTR.
22056 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22057 DW_AT_containing_type.
22058 If there is no type substitute an error marker. */
22059
22060 static struct type *
22061 lookup_die_type (struct die_info *die, const struct attribute *attr,
22062 struct dwarf2_cu *cu)
22063 {
22064 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22065 struct objfile *objfile = per_objfile->objfile;
22066 struct type *this_type;
22067
22068 gdb_assert (attr->name == DW_AT_type
22069 || attr->name == DW_AT_GNAT_descriptive_type
22070 || attr->name == DW_AT_containing_type);
22071
22072 /* First see if we have it cached. */
22073
22074 if (attr->form == DW_FORM_GNU_ref_alt)
22075 {
22076 struct dwarf2_per_cu_data *per_cu;
22077 sect_offset sect_off = attr->get_ref_die_offset ();
22078
22079 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22080 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
22081 }
22082 else if (attr->form_is_ref ())
22083 {
22084 sect_offset sect_off = attr->get_ref_die_offset ();
22085
22086 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
22087 }
22088 else if (attr->form == DW_FORM_ref_sig8)
22089 {
22090 ULONGEST signature = attr->as_signature ();
22091
22092 return get_signatured_type (die, signature, cu);
22093 }
22094 else
22095 {
22096 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22097 " at %s [in module %s]"),
22098 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22099 objfile_name (objfile));
22100 return build_error_marker_type (cu, die);
22101 }
22102
22103 /* If not cached we need to read it in. */
22104
22105 if (this_type == NULL)
22106 {
22107 struct die_info *type_die = NULL;
22108 struct dwarf2_cu *type_cu = cu;
22109
22110 if (attr->form_is_ref ())
22111 type_die = follow_die_ref (die, attr, &type_cu);
22112 if (type_die == NULL)
22113 return build_error_marker_type (cu, die);
22114 /* If we find the type now, it's probably because the type came
22115 from an inter-CU reference and the type's CU got expanded before
22116 ours. */
22117 this_type = read_type_die (type_die, type_cu);
22118 }
22119
22120 /* If we still don't have a type use an error marker. */
22121
22122 if (this_type == NULL)
22123 return build_error_marker_type (cu, die);
22124
22125 return this_type;
22126 }
22127
22128 /* Return the type in DIE, CU.
22129 Returns NULL for invalid types.
22130
22131 This first does a lookup in die_type_hash,
22132 and only reads the die in if necessary.
22133
22134 NOTE: This can be called when reading in partial or full symbols. */
22135
22136 static struct type *
22137 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22138 {
22139 struct type *this_type;
22140
22141 this_type = get_die_type (die, cu);
22142 if (this_type)
22143 return this_type;
22144
22145 return read_type_die_1 (die, cu);
22146 }
22147
22148 /* Read the type in DIE, CU.
22149 Returns NULL for invalid types. */
22150
22151 static struct type *
22152 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22153 {
22154 struct type *this_type = NULL;
22155
22156 switch (die->tag)
22157 {
22158 case DW_TAG_class_type:
22159 case DW_TAG_interface_type:
22160 case DW_TAG_structure_type:
22161 case DW_TAG_union_type:
22162 this_type = read_structure_type (die, cu);
22163 break;
22164 case DW_TAG_enumeration_type:
22165 this_type = read_enumeration_type (die, cu);
22166 break;
22167 case DW_TAG_subprogram:
22168 case DW_TAG_subroutine_type:
22169 case DW_TAG_inlined_subroutine:
22170 this_type = read_subroutine_type (die, cu);
22171 break;
22172 case DW_TAG_array_type:
22173 this_type = read_array_type (die, cu);
22174 break;
22175 case DW_TAG_set_type:
22176 this_type = read_set_type (die, cu);
22177 break;
22178 case DW_TAG_pointer_type:
22179 this_type = read_tag_pointer_type (die, cu);
22180 break;
22181 case DW_TAG_ptr_to_member_type:
22182 this_type = read_tag_ptr_to_member_type (die, cu);
22183 break;
22184 case DW_TAG_reference_type:
22185 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22186 break;
22187 case DW_TAG_rvalue_reference_type:
22188 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22189 break;
22190 case DW_TAG_const_type:
22191 this_type = read_tag_const_type (die, cu);
22192 break;
22193 case DW_TAG_volatile_type:
22194 this_type = read_tag_volatile_type (die, cu);
22195 break;
22196 case DW_TAG_restrict_type:
22197 this_type = read_tag_restrict_type (die, cu);
22198 break;
22199 case DW_TAG_string_type:
22200 this_type = read_tag_string_type (die, cu);
22201 break;
22202 case DW_TAG_typedef:
22203 this_type = read_typedef (die, cu);
22204 break;
22205 case DW_TAG_subrange_type:
22206 this_type = read_subrange_type (die, cu);
22207 break;
22208 case DW_TAG_base_type:
22209 this_type = read_base_type (die, cu);
22210 break;
22211 case DW_TAG_unspecified_type:
22212 this_type = read_unspecified_type (die, cu);
22213 break;
22214 case DW_TAG_namespace:
22215 this_type = read_namespace_type (die, cu);
22216 break;
22217 case DW_TAG_module:
22218 this_type = read_module_type (die, cu);
22219 break;
22220 case DW_TAG_atomic_type:
22221 this_type = read_tag_atomic_type (die, cu);
22222 break;
22223 default:
22224 complaint (_("unexpected tag in read_type_die: '%s'"),
22225 dwarf_tag_name (die->tag));
22226 break;
22227 }
22228
22229 return this_type;
22230 }
22231
22232 /* See if we can figure out if the class lives in a namespace. We do
22233 this by looking for a member function; its demangled name will
22234 contain namespace info, if there is any.
22235 Return the computed name or NULL.
22236 Space for the result is allocated on the objfile's obstack.
22237 This is the full-die version of guess_partial_die_structure_name.
22238 In this case we know DIE has no useful parent. */
22239
22240 static const char *
22241 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22242 {
22243 struct die_info *spec_die;
22244 struct dwarf2_cu *spec_cu;
22245 struct die_info *child;
22246 struct objfile *objfile = cu->per_objfile->objfile;
22247
22248 spec_cu = cu;
22249 spec_die = die_specification (die, &spec_cu);
22250 if (spec_die != NULL)
22251 {
22252 die = spec_die;
22253 cu = spec_cu;
22254 }
22255
22256 for (child = die->child;
22257 child != NULL;
22258 child = child->sibling)
22259 {
22260 if (child->tag == DW_TAG_subprogram)
22261 {
22262 const char *linkage_name = dw2_linkage_name (child, cu);
22263
22264 if (linkage_name != NULL)
22265 {
22266 gdb::unique_xmalloc_ptr<char> actual_name
22267 (cu->language_defn->class_name_from_physname (linkage_name));
22268 const char *name = NULL;
22269
22270 if (actual_name != NULL)
22271 {
22272 const char *die_name = dwarf2_name (die, cu);
22273
22274 if (die_name != NULL
22275 && strcmp (die_name, actual_name.get ()) != 0)
22276 {
22277 /* Strip off the class name from the full name.
22278 We want the prefix. */
22279 int die_name_len = strlen (die_name);
22280 int actual_name_len = strlen (actual_name.get ());
22281 const char *ptr = actual_name.get ();
22282
22283 /* Test for '::' as a sanity check. */
22284 if (actual_name_len > die_name_len + 2
22285 && ptr[actual_name_len - die_name_len - 1] == ':')
22286 name = obstack_strndup (
22287 &objfile->per_bfd->storage_obstack,
22288 ptr, actual_name_len - die_name_len - 2);
22289 }
22290 }
22291 return name;
22292 }
22293 }
22294 }
22295
22296 return NULL;
22297 }
22298
22299 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22300 prefix part in such case. See
22301 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22302
22303 static const char *
22304 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22305 {
22306 struct attribute *attr;
22307 const char *base;
22308
22309 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22310 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22311 return NULL;
22312
22313 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22314 return NULL;
22315
22316 attr = dw2_linkage_name_attr (die, cu);
22317 const char *attr_name = attr->as_string ();
22318 if (attr == NULL || attr_name == NULL)
22319 return NULL;
22320
22321 /* dwarf2_name had to be already called. */
22322 gdb_assert (attr->canonical_string_p ());
22323
22324 /* Strip the base name, keep any leading namespaces/classes. */
22325 base = strrchr (attr_name, ':');
22326 if (base == NULL || base == attr_name || base[-1] != ':')
22327 return "";
22328
22329 struct objfile *objfile = cu->per_objfile->objfile;
22330 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22331 attr_name,
22332 &base[-1] - attr_name);
22333 }
22334
22335 /* Return the name of the namespace/class that DIE is defined within,
22336 or "" if we can't tell. The caller should not xfree the result.
22337
22338 For example, if we're within the method foo() in the following
22339 code:
22340
22341 namespace N {
22342 class C {
22343 void foo () {
22344 }
22345 };
22346 }
22347
22348 then determine_prefix on foo's die will return "N::C". */
22349
22350 static const char *
22351 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22352 {
22353 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22354 struct die_info *parent, *spec_die;
22355 struct dwarf2_cu *spec_cu;
22356 struct type *parent_type;
22357 const char *retval;
22358
22359 if (cu->language != language_cplus
22360 && cu->language != language_fortran && cu->language != language_d
22361 && cu->language != language_rust)
22362 return "";
22363
22364 retval = anonymous_struct_prefix (die, cu);
22365 if (retval)
22366 return retval;
22367
22368 /* We have to be careful in the presence of DW_AT_specification.
22369 For example, with GCC 3.4, given the code
22370
22371 namespace N {
22372 void foo() {
22373 // Definition of N::foo.
22374 }
22375 }
22376
22377 then we'll have a tree of DIEs like this:
22378
22379 1: DW_TAG_compile_unit
22380 2: DW_TAG_namespace // N
22381 3: DW_TAG_subprogram // declaration of N::foo
22382 4: DW_TAG_subprogram // definition of N::foo
22383 DW_AT_specification // refers to die #3
22384
22385 Thus, when processing die #4, we have to pretend that we're in
22386 the context of its DW_AT_specification, namely the contex of die
22387 #3. */
22388 spec_cu = cu;
22389 spec_die = die_specification (die, &spec_cu);
22390 if (spec_die == NULL)
22391 parent = die->parent;
22392 else
22393 {
22394 parent = spec_die->parent;
22395 cu = spec_cu;
22396 }
22397
22398 if (parent == NULL)
22399 return "";
22400 else if (parent->building_fullname)
22401 {
22402 const char *name;
22403 const char *parent_name;
22404
22405 /* It has been seen on RealView 2.2 built binaries,
22406 DW_TAG_template_type_param types actually _defined_ as
22407 children of the parent class:
22408
22409 enum E {};
22410 template class <class Enum> Class{};
22411 Class<enum E> class_e;
22412
22413 1: DW_TAG_class_type (Class)
22414 2: DW_TAG_enumeration_type (E)
22415 3: DW_TAG_enumerator (enum1:0)
22416 3: DW_TAG_enumerator (enum2:1)
22417 ...
22418 2: DW_TAG_template_type_param
22419 DW_AT_type DW_FORM_ref_udata (E)
22420
22421 Besides being broken debug info, it can put GDB into an
22422 infinite loop. Consider:
22423
22424 When we're building the full name for Class<E>, we'll start
22425 at Class, and go look over its template type parameters,
22426 finding E. We'll then try to build the full name of E, and
22427 reach here. We're now trying to build the full name of E,
22428 and look over the parent DIE for containing scope. In the
22429 broken case, if we followed the parent DIE of E, we'd again
22430 find Class, and once again go look at its template type
22431 arguments, etc., etc. Simply don't consider such parent die
22432 as source-level parent of this die (it can't be, the language
22433 doesn't allow it), and break the loop here. */
22434 name = dwarf2_name (die, cu);
22435 parent_name = dwarf2_name (parent, cu);
22436 complaint (_("template param type '%s' defined within parent '%s'"),
22437 name ? name : "<unknown>",
22438 parent_name ? parent_name : "<unknown>");
22439 return "";
22440 }
22441 else
22442 switch (parent->tag)
22443 {
22444 case DW_TAG_namespace:
22445 parent_type = read_type_die (parent, cu);
22446 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22447 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22448 Work around this problem here. */
22449 if (cu->language == language_cplus
22450 && strcmp (parent_type->name (), "::") == 0)
22451 return "";
22452 /* We give a name to even anonymous namespaces. */
22453 return parent_type->name ();
22454 case DW_TAG_class_type:
22455 case DW_TAG_interface_type:
22456 case DW_TAG_structure_type:
22457 case DW_TAG_union_type:
22458 case DW_TAG_module:
22459 parent_type = read_type_die (parent, cu);
22460 if (parent_type->name () != NULL)
22461 return parent_type->name ();
22462 else
22463 /* An anonymous structure is only allowed non-static data
22464 members; no typedefs, no member functions, et cetera.
22465 So it does not need a prefix. */
22466 return "";
22467 case DW_TAG_compile_unit:
22468 case DW_TAG_partial_unit:
22469 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22470 if (cu->language == language_cplus
22471 && !per_objfile->per_bfd->types.empty ()
22472 && die->child != NULL
22473 && (die->tag == DW_TAG_class_type
22474 || die->tag == DW_TAG_structure_type
22475 || die->tag == DW_TAG_union_type))
22476 {
22477 const char *name = guess_full_die_structure_name (die, cu);
22478 if (name != NULL)
22479 return name;
22480 }
22481 return "";
22482 case DW_TAG_subprogram:
22483 /* Nested subroutines in Fortran get a prefix with the name
22484 of the parent's subroutine. */
22485 if (cu->language == language_fortran)
22486 {
22487 if ((die->tag == DW_TAG_subprogram)
22488 && (dwarf2_name (parent, cu) != NULL))
22489 return dwarf2_name (parent, cu);
22490 }
22491 return determine_prefix (parent, cu);
22492 case DW_TAG_enumeration_type:
22493 parent_type = read_type_die (parent, cu);
22494 if (TYPE_DECLARED_CLASS (parent_type))
22495 {
22496 if (parent_type->name () != NULL)
22497 return parent_type->name ();
22498 return "";
22499 }
22500 /* Fall through. */
22501 default:
22502 return determine_prefix (parent, cu);
22503 }
22504 }
22505
22506 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22507 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22508 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22509 an obconcat, otherwise allocate storage for the result. The CU argument is
22510 used to determine the language and hence, the appropriate separator. */
22511
22512 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22513
22514 static char *
22515 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22516 int physname, struct dwarf2_cu *cu)
22517 {
22518 const char *lead = "";
22519 const char *sep;
22520
22521 if (suffix == NULL || suffix[0] == '\0'
22522 || prefix == NULL || prefix[0] == '\0')
22523 sep = "";
22524 else if (cu->language == language_d)
22525 {
22526 /* For D, the 'main' function could be defined in any module, but it
22527 should never be prefixed. */
22528 if (strcmp (suffix, "D main") == 0)
22529 {
22530 prefix = "";
22531 sep = "";
22532 }
22533 else
22534 sep = ".";
22535 }
22536 else if (cu->language == language_fortran && physname)
22537 {
22538 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22539 DW_AT_MIPS_linkage_name is preferred and used instead. */
22540
22541 lead = "__";
22542 sep = "_MOD_";
22543 }
22544 else
22545 sep = "::";
22546
22547 if (prefix == NULL)
22548 prefix = "";
22549 if (suffix == NULL)
22550 suffix = "";
22551
22552 if (obs == NULL)
22553 {
22554 char *retval
22555 = ((char *)
22556 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22557
22558 strcpy (retval, lead);
22559 strcat (retval, prefix);
22560 strcat (retval, sep);
22561 strcat (retval, suffix);
22562 return retval;
22563 }
22564 else
22565 {
22566 /* We have an obstack. */
22567 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22568 }
22569 }
22570
22571 /* Get name of a die, return NULL if not found. */
22572
22573 static const char *
22574 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22575 struct objfile *objfile)
22576 {
22577 if (name && cu->language == language_cplus)
22578 {
22579 gdb::unique_xmalloc_ptr<char> canon_name
22580 = cp_canonicalize_string (name);
22581
22582 if (canon_name != nullptr)
22583 name = objfile->intern (canon_name.get ());
22584 }
22585
22586 return name;
22587 }
22588
22589 /* Get name of a die, return NULL if not found.
22590 Anonymous namespaces are converted to their magic string. */
22591
22592 static const char *
22593 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22594 {
22595 struct attribute *attr;
22596 struct objfile *objfile = cu->per_objfile->objfile;
22597
22598 attr = dwarf2_attr (die, DW_AT_name, cu);
22599 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
22600 if (attr_name == nullptr
22601 && die->tag != DW_TAG_namespace
22602 && die->tag != DW_TAG_class_type
22603 && die->tag != DW_TAG_interface_type
22604 && die->tag != DW_TAG_structure_type
22605 && die->tag != DW_TAG_union_type)
22606 return NULL;
22607
22608 switch (die->tag)
22609 {
22610 case DW_TAG_compile_unit:
22611 case DW_TAG_partial_unit:
22612 /* Compilation units have a DW_AT_name that is a filename, not
22613 a source language identifier. */
22614 case DW_TAG_enumeration_type:
22615 case DW_TAG_enumerator:
22616 /* These tags always have simple identifiers already; no need
22617 to canonicalize them. */
22618 return attr_name;
22619
22620 case DW_TAG_namespace:
22621 if (attr_name != nullptr)
22622 return attr_name;
22623 return CP_ANONYMOUS_NAMESPACE_STR;
22624
22625 case DW_TAG_class_type:
22626 case DW_TAG_interface_type:
22627 case DW_TAG_structure_type:
22628 case DW_TAG_union_type:
22629 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22630 structures or unions. These were of the form "._%d" in GCC 4.1,
22631 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22632 and GCC 4.4. We work around this problem by ignoring these. */
22633 if (attr_name != nullptr
22634 && (startswith (attr_name, "._")
22635 || startswith (attr_name, "<anonymous")))
22636 return NULL;
22637
22638 /* GCC might emit a nameless typedef that has a linkage name. See
22639 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22640 if (!attr || attr_name == NULL)
22641 {
22642 attr = dw2_linkage_name_attr (die, cu);
22643 attr_name = attr == nullptr ? nullptr : attr->as_string ();
22644 if (attr == NULL || attr_name == NULL)
22645 return NULL;
22646
22647 /* Avoid demangling attr_name the second time on a second
22648 call for the same DIE. */
22649 if (!attr->canonical_string_p ())
22650 {
22651 gdb::unique_xmalloc_ptr<char> demangled
22652 (gdb_demangle (attr_name, DMGL_TYPES));
22653 if (demangled == nullptr)
22654 return nullptr;
22655
22656 attr->set_string_canonical (objfile->intern (demangled.get ()));
22657 attr_name = attr->as_string ();
22658 }
22659
22660 /* Strip any leading namespaces/classes, keep only the
22661 base name. DW_AT_name for named DIEs does not
22662 contain the prefixes. */
22663 const char *base = strrchr (attr_name, ':');
22664 if (base && base > attr_name && base[-1] == ':')
22665 return &base[1];
22666 else
22667 return attr_name;
22668 }
22669 break;
22670
22671 default:
22672 break;
22673 }
22674
22675 if (!attr->canonical_string_p ())
22676 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
22677 objfile));
22678 return attr->as_string ();
22679 }
22680
22681 /* Return the die that this die in an extension of, or NULL if there
22682 is none. *EXT_CU is the CU containing DIE on input, and the CU
22683 containing the return value on output. */
22684
22685 static struct die_info *
22686 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22687 {
22688 struct attribute *attr;
22689
22690 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22691 if (attr == NULL)
22692 return NULL;
22693
22694 return follow_die_ref (die, attr, ext_cu);
22695 }
22696
22697 static void
22698 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22699 {
22700 unsigned int i;
22701
22702 print_spaces (indent, f);
22703 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22704 dwarf_tag_name (die->tag), die->abbrev,
22705 sect_offset_str (die->sect_off));
22706
22707 if (die->parent != NULL)
22708 {
22709 print_spaces (indent, f);
22710 fprintf_unfiltered (f, " parent at offset: %s\n",
22711 sect_offset_str (die->parent->sect_off));
22712 }
22713
22714 print_spaces (indent, f);
22715 fprintf_unfiltered (f, " has children: %s\n",
22716 dwarf_bool_name (die->child != NULL));
22717
22718 print_spaces (indent, f);
22719 fprintf_unfiltered (f, " attributes:\n");
22720
22721 for (i = 0; i < die->num_attrs; ++i)
22722 {
22723 print_spaces (indent, f);
22724 fprintf_unfiltered (f, " %s (%s) ",
22725 dwarf_attr_name (die->attrs[i].name),
22726 dwarf_form_name (die->attrs[i].form));
22727
22728 switch (die->attrs[i].form)
22729 {
22730 case DW_FORM_addr:
22731 case DW_FORM_addrx:
22732 case DW_FORM_GNU_addr_index:
22733 fprintf_unfiltered (f, "address: ");
22734 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
22735 break;
22736 case DW_FORM_block2:
22737 case DW_FORM_block4:
22738 case DW_FORM_block:
22739 case DW_FORM_block1:
22740 fprintf_unfiltered (f, "block: size %s",
22741 pulongest (die->attrs[i].as_block ()->size));
22742 break;
22743 case DW_FORM_exprloc:
22744 fprintf_unfiltered (f, "expression: size %s",
22745 pulongest (die->attrs[i].as_block ()->size));
22746 break;
22747 case DW_FORM_data16:
22748 fprintf_unfiltered (f, "constant of 16 bytes");
22749 break;
22750 case DW_FORM_ref_addr:
22751 fprintf_unfiltered (f, "ref address: ");
22752 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
22753 break;
22754 case DW_FORM_GNU_ref_alt:
22755 fprintf_unfiltered (f, "alt ref address: ");
22756 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
22757 break;
22758 case DW_FORM_ref1:
22759 case DW_FORM_ref2:
22760 case DW_FORM_ref4:
22761 case DW_FORM_ref8:
22762 case DW_FORM_ref_udata:
22763 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22764 (long) (die->attrs[i].as_unsigned ()));
22765 break;
22766 case DW_FORM_data1:
22767 case DW_FORM_data2:
22768 case DW_FORM_data4:
22769 case DW_FORM_data8:
22770 case DW_FORM_udata:
22771 fprintf_unfiltered (f, "constant: %s",
22772 pulongest (die->attrs[i].as_unsigned ()));
22773 break;
22774 case DW_FORM_sec_offset:
22775 fprintf_unfiltered (f, "section offset: %s",
22776 pulongest (die->attrs[i].as_unsigned ()));
22777 break;
22778 case DW_FORM_ref_sig8:
22779 fprintf_unfiltered (f, "signature: %s",
22780 hex_string (die->attrs[i].as_signature ()));
22781 break;
22782 case DW_FORM_string:
22783 case DW_FORM_strp:
22784 case DW_FORM_line_strp:
22785 case DW_FORM_strx:
22786 case DW_FORM_GNU_str_index:
22787 case DW_FORM_GNU_strp_alt:
22788 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22789 die->attrs[i].as_string ()
22790 ? die->attrs[i].as_string () : "",
22791 die->attrs[i].canonical_string_p () ? "is" : "not");
22792 break;
22793 case DW_FORM_flag:
22794 if (die->attrs[i].as_boolean ())
22795 fprintf_unfiltered (f, "flag: TRUE");
22796 else
22797 fprintf_unfiltered (f, "flag: FALSE");
22798 break;
22799 case DW_FORM_flag_present:
22800 fprintf_unfiltered (f, "flag: TRUE");
22801 break;
22802 case DW_FORM_indirect:
22803 /* The reader will have reduced the indirect form to
22804 the "base form" so this form should not occur. */
22805 fprintf_unfiltered (f,
22806 "unexpected attribute form: DW_FORM_indirect");
22807 break;
22808 case DW_FORM_sdata:
22809 case DW_FORM_implicit_const:
22810 fprintf_unfiltered (f, "constant: %s",
22811 plongest (die->attrs[i].as_signed ()));
22812 break;
22813 default:
22814 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22815 die->attrs[i].form);
22816 break;
22817 }
22818 fprintf_unfiltered (f, "\n");
22819 }
22820 }
22821
22822 static void
22823 dump_die_for_error (struct die_info *die)
22824 {
22825 dump_die_shallow (gdb_stderr, 0, die);
22826 }
22827
22828 static void
22829 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22830 {
22831 int indent = level * 4;
22832
22833 gdb_assert (die != NULL);
22834
22835 if (level >= max_level)
22836 return;
22837
22838 dump_die_shallow (f, indent, die);
22839
22840 if (die->child != NULL)
22841 {
22842 print_spaces (indent, f);
22843 fprintf_unfiltered (f, " Children:");
22844 if (level + 1 < max_level)
22845 {
22846 fprintf_unfiltered (f, "\n");
22847 dump_die_1 (f, level + 1, max_level, die->child);
22848 }
22849 else
22850 {
22851 fprintf_unfiltered (f,
22852 " [not printed, max nesting level reached]\n");
22853 }
22854 }
22855
22856 if (die->sibling != NULL && level > 0)
22857 {
22858 dump_die_1 (f, level, max_level, die->sibling);
22859 }
22860 }
22861
22862 /* This is called from the pdie macro in gdbinit.in.
22863 It's not static so gcc will keep a copy callable from gdb. */
22864
22865 void
22866 dump_die (struct die_info *die, int max_level)
22867 {
22868 dump_die_1 (gdb_stdlog, 0, max_level, die);
22869 }
22870
22871 static void
22872 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22873 {
22874 void **slot;
22875
22876 slot = htab_find_slot_with_hash (cu->die_hash, die,
22877 to_underlying (die->sect_off),
22878 INSERT);
22879
22880 *slot = die;
22881 }
22882
22883 /* Follow reference or signature attribute ATTR of SRC_DIE.
22884 On entry *REF_CU is the CU of SRC_DIE.
22885 On exit *REF_CU is the CU of the result. */
22886
22887 static struct die_info *
22888 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22889 struct dwarf2_cu **ref_cu)
22890 {
22891 struct die_info *die;
22892
22893 if (attr->form_is_ref ())
22894 die = follow_die_ref (src_die, attr, ref_cu);
22895 else if (attr->form == DW_FORM_ref_sig8)
22896 die = follow_die_sig (src_die, attr, ref_cu);
22897 else
22898 {
22899 dump_die_for_error (src_die);
22900 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22901 objfile_name ((*ref_cu)->per_objfile->objfile));
22902 }
22903
22904 return die;
22905 }
22906
22907 /* Follow reference OFFSET.
22908 On entry *REF_CU is the CU of the source die referencing OFFSET.
22909 On exit *REF_CU is the CU of the result.
22910 Returns NULL if OFFSET is invalid. */
22911
22912 static struct die_info *
22913 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22914 struct dwarf2_cu **ref_cu)
22915 {
22916 struct die_info temp_die;
22917 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22918 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22919
22920 gdb_assert (cu->per_cu != NULL);
22921
22922 target_cu = cu;
22923
22924 if (cu->per_cu->is_debug_types)
22925 {
22926 /* .debug_types CUs cannot reference anything outside their CU.
22927 If they need to, they have to reference a signatured type via
22928 DW_FORM_ref_sig8. */
22929 if (!cu->header.offset_in_cu_p (sect_off))
22930 return NULL;
22931 }
22932 else if (offset_in_dwz != cu->per_cu->is_dwz
22933 || !cu->header.offset_in_cu_p (sect_off))
22934 {
22935 struct dwarf2_per_cu_data *per_cu;
22936
22937 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22938 per_objfile);
22939
22940 /* If necessary, add it to the queue and load its DIEs. */
22941 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
22942 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
22943 false, cu->language);
22944
22945 target_cu = per_objfile->get_cu (per_cu);
22946 }
22947 else if (cu->dies == NULL)
22948 {
22949 /* We're loading full DIEs during partial symbol reading. */
22950 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
22951 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
22952 language_minimal);
22953 }
22954
22955 *ref_cu = target_cu;
22956 temp_die.sect_off = sect_off;
22957
22958 if (target_cu != cu)
22959 target_cu->ancestor = cu;
22960
22961 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22962 &temp_die,
22963 to_underlying (sect_off));
22964 }
22965
22966 /* Follow reference attribute ATTR of SRC_DIE.
22967 On entry *REF_CU is the CU of SRC_DIE.
22968 On exit *REF_CU is the CU of the result. */
22969
22970 static struct die_info *
22971 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22972 struct dwarf2_cu **ref_cu)
22973 {
22974 sect_offset sect_off = attr->get_ref_die_offset ();
22975 struct dwarf2_cu *cu = *ref_cu;
22976 struct die_info *die;
22977
22978 die = follow_die_offset (sect_off,
22979 (attr->form == DW_FORM_GNU_ref_alt
22980 || cu->per_cu->is_dwz),
22981 ref_cu);
22982 if (!die)
22983 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22984 "at %s [in module %s]"),
22985 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22986 objfile_name (cu->per_objfile->objfile));
22987
22988 return die;
22989 }
22990
22991 /* See read.h. */
22992
22993 struct dwarf2_locexpr_baton
22994 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22995 dwarf2_per_cu_data *per_cu,
22996 dwarf2_per_objfile *per_objfile,
22997 gdb::function_view<CORE_ADDR ()> get_frame_pc,
22998 bool resolve_abstract_p)
22999 {
23000 struct die_info *die;
23001 struct attribute *attr;
23002 struct dwarf2_locexpr_baton retval;
23003 struct objfile *objfile = per_objfile->objfile;
23004
23005 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23006 if (cu == nullptr)
23007 cu = load_cu (per_cu, per_objfile, false);
23008
23009 if (cu == nullptr)
23010 {
23011 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23012 Instead just throw an error, not much else we can do. */
23013 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23014 sect_offset_str (sect_off), objfile_name (objfile));
23015 }
23016
23017 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23018 if (!die)
23019 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23020 sect_offset_str (sect_off), objfile_name (objfile));
23021
23022 attr = dwarf2_attr (die, DW_AT_location, cu);
23023 if (!attr && resolve_abstract_p
23024 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23025 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23026 {
23027 CORE_ADDR pc = get_frame_pc ();
23028 CORE_ADDR baseaddr = objfile->text_section_offset ();
23029 struct gdbarch *gdbarch = objfile->arch ();
23030
23031 for (const auto &cand_off
23032 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23033 {
23034 struct dwarf2_cu *cand_cu = cu;
23035 struct die_info *cand
23036 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23037 if (!cand
23038 || !cand->parent
23039 || cand->parent->tag != DW_TAG_subprogram)
23040 continue;
23041
23042 CORE_ADDR pc_low, pc_high;
23043 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23044 if (pc_low == ((CORE_ADDR) -1))
23045 continue;
23046 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23047 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23048 if (!(pc_low <= pc && pc < pc_high))
23049 continue;
23050
23051 die = cand;
23052 attr = dwarf2_attr (die, DW_AT_location, cu);
23053 break;
23054 }
23055 }
23056
23057 if (!attr)
23058 {
23059 /* DWARF: "If there is no such attribute, then there is no effect.".
23060 DATA is ignored if SIZE is 0. */
23061
23062 retval.data = NULL;
23063 retval.size = 0;
23064 }
23065 else if (attr->form_is_section_offset ())
23066 {
23067 struct dwarf2_loclist_baton loclist_baton;
23068 CORE_ADDR pc = get_frame_pc ();
23069 size_t size;
23070
23071 fill_in_loclist_baton (cu, &loclist_baton, attr);
23072
23073 retval.data = dwarf2_find_location_expression (&loclist_baton,
23074 &size, pc);
23075 retval.size = size;
23076 }
23077 else
23078 {
23079 if (!attr->form_is_block ())
23080 error (_("Dwarf Error: DIE at %s referenced in module %s "
23081 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23082 sect_offset_str (sect_off), objfile_name (objfile));
23083
23084 struct dwarf_block *block = attr->as_block ();
23085 retval.data = block->data;
23086 retval.size = block->size;
23087 }
23088 retval.per_objfile = per_objfile;
23089 retval.per_cu = cu->per_cu;
23090
23091 per_objfile->age_comp_units ();
23092
23093 return retval;
23094 }
23095
23096 /* See read.h. */
23097
23098 struct dwarf2_locexpr_baton
23099 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23100 dwarf2_per_cu_data *per_cu,
23101 dwarf2_per_objfile *per_objfile,
23102 gdb::function_view<CORE_ADDR ()> get_frame_pc)
23103 {
23104 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23105
23106 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
23107 get_frame_pc);
23108 }
23109
23110 /* Write a constant of a given type as target-ordered bytes into
23111 OBSTACK. */
23112
23113 static const gdb_byte *
23114 write_constant_as_bytes (struct obstack *obstack,
23115 enum bfd_endian byte_order,
23116 struct type *type,
23117 ULONGEST value,
23118 LONGEST *len)
23119 {
23120 gdb_byte *result;
23121
23122 *len = TYPE_LENGTH (type);
23123 result = (gdb_byte *) obstack_alloc (obstack, *len);
23124 store_unsigned_integer (result, *len, byte_order, value);
23125
23126 return result;
23127 }
23128
23129 /* See read.h. */
23130
23131 const gdb_byte *
23132 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23133 dwarf2_per_cu_data *per_cu,
23134 dwarf2_per_objfile *per_objfile,
23135 obstack *obstack,
23136 LONGEST *len)
23137 {
23138 struct die_info *die;
23139 struct attribute *attr;
23140 const gdb_byte *result = NULL;
23141 struct type *type;
23142 LONGEST value;
23143 enum bfd_endian byte_order;
23144 struct objfile *objfile = per_objfile->objfile;
23145
23146 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23147 if (cu == nullptr)
23148 cu = load_cu (per_cu, per_objfile, false);
23149
23150 if (cu == nullptr)
23151 {
23152 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23153 Instead just throw an error, not much else we can do. */
23154 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23155 sect_offset_str (sect_off), objfile_name (objfile));
23156 }
23157
23158 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23159 if (!die)
23160 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23161 sect_offset_str (sect_off), objfile_name (objfile));
23162
23163 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23164 if (attr == NULL)
23165 return NULL;
23166
23167 byte_order = (bfd_big_endian (objfile->obfd)
23168 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23169
23170 switch (attr->form)
23171 {
23172 case DW_FORM_addr:
23173 case DW_FORM_addrx:
23174 case DW_FORM_GNU_addr_index:
23175 {
23176 gdb_byte *tem;
23177
23178 *len = cu->header.addr_size;
23179 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23180 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
23181 result = tem;
23182 }
23183 break;
23184 case DW_FORM_string:
23185 case DW_FORM_strp:
23186 case DW_FORM_strx:
23187 case DW_FORM_GNU_str_index:
23188 case DW_FORM_GNU_strp_alt:
23189 /* The string is already allocated on the objfile obstack, point
23190 directly to it. */
23191 {
23192 const char *attr_name = attr->as_string ();
23193 result = (const gdb_byte *) attr_name;
23194 *len = strlen (attr_name);
23195 }
23196 break;
23197 case DW_FORM_block1:
23198 case DW_FORM_block2:
23199 case DW_FORM_block4:
23200 case DW_FORM_block:
23201 case DW_FORM_exprloc:
23202 case DW_FORM_data16:
23203 {
23204 struct dwarf_block *block = attr->as_block ();
23205 result = block->data;
23206 *len = block->size;
23207 }
23208 break;
23209
23210 /* The DW_AT_const_value attributes are supposed to carry the
23211 symbol's value "represented as it would be on the target
23212 architecture." By the time we get here, it's already been
23213 converted to host endianness, so we just need to sign- or
23214 zero-extend it as appropriate. */
23215 case DW_FORM_data1:
23216 type = die_type (die, cu);
23217 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23218 if (result == NULL)
23219 result = write_constant_as_bytes (obstack, byte_order,
23220 type, value, len);
23221 break;
23222 case DW_FORM_data2:
23223 type = die_type (die, cu);
23224 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23225 if (result == NULL)
23226 result = write_constant_as_bytes (obstack, byte_order,
23227 type, value, len);
23228 break;
23229 case DW_FORM_data4:
23230 type = die_type (die, cu);
23231 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23232 if (result == NULL)
23233 result = write_constant_as_bytes (obstack, byte_order,
23234 type, value, len);
23235 break;
23236 case DW_FORM_data8:
23237 type = die_type (die, cu);
23238 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23239 if (result == NULL)
23240 result = write_constant_as_bytes (obstack, byte_order,
23241 type, value, len);
23242 break;
23243
23244 case DW_FORM_sdata:
23245 case DW_FORM_implicit_const:
23246 type = die_type (die, cu);
23247 result = write_constant_as_bytes (obstack, byte_order,
23248 type, attr->as_signed (), len);
23249 break;
23250
23251 case DW_FORM_udata:
23252 type = die_type (die, cu);
23253 result = write_constant_as_bytes (obstack, byte_order,
23254 type, attr->as_unsigned (), len);
23255 break;
23256
23257 default:
23258 complaint (_("unsupported const value attribute form: '%s'"),
23259 dwarf_form_name (attr->form));
23260 break;
23261 }
23262
23263 return result;
23264 }
23265
23266 /* See read.h. */
23267
23268 struct type *
23269 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23270 dwarf2_per_cu_data *per_cu,
23271 dwarf2_per_objfile *per_objfile)
23272 {
23273 struct die_info *die;
23274
23275 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23276 if (cu == nullptr)
23277 cu = load_cu (per_cu, per_objfile, false);
23278
23279 if (cu == nullptr)
23280 return nullptr;
23281
23282 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23283 if (!die)
23284 return NULL;
23285
23286 return die_type (die, cu);
23287 }
23288
23289 /* See read.h. */
23290
23291 struct type *
23292 dwarf2_get_die_type (cu_offset die_offset,
23293 dwarf2_per_cu_data *per_cu,
23294 dwarf2_per_objfile *per_objfile)
23295 {
23296 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23297 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
23298 }
23299
23300 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23301 On entry *REF_CU is the CU of SRC_DIE.
23302 On exit *REF_CU is the CU of the result.
23303 Returns NULL if the referenced DIE isn't found. */
23304
23305 static struct die_info *
23306 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23307 struct dwarf2_cu **ref_cu)
23308 {
23309 struct die_info temp_die;
23310 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23311 struct die_info *die;
23312 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
23313
23314
23315 /* While it might be nice to assert sig_type->type == NULL here,
23316 we can get here for DW_AT_imported_declaration where we need
23317 the DIE not the type. */
23318
23319 /* If necessary, add it to the queue and load its DIEs. */
23320
23321 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
23322 language_minimal))
23323 read_signatured_type (sig_type, per_objfile);
23324
23325 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
23326 gdb_assert (sig_cu != NULL);
23327 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23328 temp_die.sect_off = sig_type->type_offset_in_section;
23329 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23330 to_underlying (temp_die.sect_off));
23331 if (die)
23332 {
23333 /* For .gdb_index version 7 keep track of included TUs.
23334 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23335 if (per_objfile->per_bfd->index_table != NULL
23336 && per_objfile->per_bfd->index_table->version <= 7)
23337 {
23338 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23339 }
23340
23341 *ref_cu = sig_cu;
23342 if (sig_cu != cu)
23343 sig_cu->ancestor = cu;
23344
23345 return die;
23346 }
23347
23348 return NULL;
23349 }
23350
23351 /* Follow signatured type referenced by ATTR in SRC_DIE.
23352 On entry *REF_CU is the CU of SRC_DIE.
23353 On exit *REF_CU is the CU of the result.
23354 The result is the DIE of the type.
23355 If the referenced type cannot be found an error is thrown. */
23356
23357 static struct die_info *
23358 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23359 struct dwarf2_cu **ref_cu)
23360 {
23361 ULONGEST signature = attr->as_signature ();
23362 struct signatured_type *sig_type;
23363 struct die_info *die;
23364
23365 gdb_assert (attr->form == DW_FORM_ref_sig8);
23366
23367 sig_type = lookup_signatured_type (*ref_cu, signature);
23368 /* sig_type will be NULL if the signatured type is missing from
23369 the debug info. */
23370 if (sig_type == NULL)
23371 {
23372 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23373 " from DIE at %s [in module %s]"),
23374 hex_string (signature), sect_offset_str (src_die->sect_off),
23375 objfile_name ((*ref_cu)->per_objfile->objfile));
23376 }
23377
23378 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23379 if (die == NULL)
23380 {
23381 dump_die_for_error (src_die);
23382 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23383 " from DIE at %s [in module %s]"),
23384 hex_string (signature), sect_offset_str (src_die->sect_off),
23385 objfile_name ((*ref_cu)->per_objfile->objfile));
23386 }
23387
23388 return die;
23389 }
23390
23391 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23392 reading in and processing the type unit if necessary. */
23393
23394 static struct type *
23395 get_signatured_type (struct die_info *die, ULONGEST signature,
23396 struct dwarf2_cu *cu)
23397 {
23398 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23399 struct signatured_type *sig_type;
23400 struct dwarf2_cu *type_cu;
23401 struct die_info *type_die;
23402 struct type *type;
23403
23404 sig_type = lookup_signatured_type (cu, signature);
23405 /* sig_type will be NULL if the signatured type is missing from
23406 the debug info. */
23407 if (sig_type == NULL)
23408 {
23409 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23410 " from DIE at %s [in module %s]"),
23411 hex_string (signature), sect_offset_str (die->sect_off),
23412 objfile_name (per_objfile->objfile));
23413 return build_error_marker_type (cu, die);
23414 }
23415
23416 /* If we already know the type we're done. */
23417 type = per_objfile->get_type_for_signatured_type (sig_type);
23418 if (type != nullptr)
23419 return type;
23420
23421 type_cu = cu;
23422 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23423 if (type_die != NULL)
23424 {
23425 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23426 is created. This is important, for example, because for c++ classes
23427 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23428 type = read_type_die (type_die, type_cu);
23429 if (type == NULL)
23430 {
23431 complaint (_("Dwarf Error: Cannot build signatured type %s"
23432 " referenced from DIE at %s [in module %s]"),
23433 hex_string (signature), sect_offset_str (die->sect_off),
23434 objfile_name (per_objfile->objfile));
23435 type = build_error_marker_type (cu, die);
23436 }
23437 }
23438 else
23439 {
23440 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23441 " from DIE at %s [in module %s]"),
23442 hex_string (signature), sect_offset_str (die->sect_off),
23443 objfile_name (per_objfile->objfile));
23444 type = build_error_marker_type (cu, die);
23445 }
23446
23447 per_objfile->set_type_for_signatured_type (sig_type, type);
23448
23449 return type;
23450 }
23451
23452 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23453 reading in and processing the type unit if necessary. */
23454
23455 static struct type *
23456 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23457 struct dwarf2_cu *cu) /* ARI: editCase function */
23458 {
23459 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23460 if (attr->form_is_ref ())
23461 {
23462 struct dwarf2_cu *type_cu = cu;
23463 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23464
23465 return read_type_die (type_die, type_cu);
23466 }
23467 else if (attr->form == DW_FORM_ref_sig8)
23468 {
23469 return get_signatured_type (die, attr->as_signature (), cu);
23470 }
23471 else
23472 {
23473 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23474
23475 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23476 " at %s [in module %s]"),
23477 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23478 objfile_name (per_objfile->objfile));
23479 return build_error_marker_type (cu, die);
23480 }
23481 }
23482
23483 /* Load the DIEs associated with type unit PER_CU into memory. */
23484
23485 static void
23486 load_full_type_unit (dwarf2_per_cu_data *per_cu,
23487 dwarf2_per_objfile *per_objfile)
23488 {
23489 struct signatured_type *sig_type;
23490
23491 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23492 gdb_assert (! per_cu->type_unit_group_p ());
23493
23494 /* We have the per_cu, but we need the signatured_type.
23495 Fortunately this is an easy translation. */
23496 gdb_assert (per_cu->is_debug_types);
23497 sig_type = (struct signatured_type *) per_cu;
23498
23499 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
23500
23501 read_signatured_type (sig_type, per_objfile);
23502
23503 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
23504 }
23505
23506 /* Read in a signatured type and build its CU and DIEs.
23507 If the type is a stub for the real type in a DWO file,
23508 read in the real type from the DWO file as well. */
23509
23510 static void
23511 read_signatured_type (signatured_type *sig_type,
23512 dwarf2_per_objfile *per_objfile)
23513 {
23514 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23515
23516 gdb_assert (per_cu->is_debug_types);
23517 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
23518
23519 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
23520
23521 if (!reader.dummy_p)
23522 {
23523 struct dwarf2_cu *cu = reader.cu;
23524 const gdb_byte *info_ptr = reader.info_ptr;
23525
23526 gdb_assert (cu->die_hash == NULL);
23527 cu->die_hash =
23528 htab_create_alloc_ex (cu->header.length / 12,
23529 die_hash,
23530 die_eq,
23531 NULL,
23532 &cu->comp_unit_obstack,
23533 hashtab_obstack_allocate,
23534 dummy_obstack_deallocate);
23535
23536 if (reader.comp_unit_die->has_children)
23537 reader.comp_unit_die->child
23538 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23539 reader.comp_unit_die);
23540 cu->dies = reader.comp_unit_die;
23541 /* comp_unit_die is not stored in die_hash, no need. */
23542
23543 /* We try not to read any attributes in this function, because
23544 not all CUs needed for references have been loaded yet, and
23545 symbol table processing isn't initialized. But we have to
23546 set the CU language, or we won't be able to build types
23547 correctly. Similarly, if we do not read the producer, we can
23548 not apply producer-specific interpretation. */
23549 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23550
23551 reader.keep ();
23552 }
23553
23554 sig_type->per_cu.tu_read = 1;
23555 }
23556
23557 /* Decode simple location descriptions.
23558 Given a pointer to a dwarf block that defines a location, compute
23559 the location and return the value. If COMPUTED is non-null, it is
23560 set to true to indicate that decoding was successful, and false
23561 otherwise. If COMPUTED is null, then this function may emit a
23562 complaint. */
23563
23564 static CORE_ADDR
23565 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
23566 {
23567 struct objfile *objfile = cu->per_objfile->objfile;
23568 size_t i;
23569 size_t size = blk->size;
23570 const gdb_byte *data = blk->data;
23571 CORE_ADDR stack[64];
23572 int stacki;
23573 unsigned int bytes_read, unsnd;
23574 gdb_byte op;
23575
23576 if (computed != nullptr)
23577 *computed = false;
23578
23579 i = 0;
23580 stacki = 0;
23581 stack[stacki] = 0;
23582 stack[++stacki] = 0;
23583
23584 while (i < size)
23585 {
23586 op = data[i++];
23587 switch (op)
23588 {
23589 case DW_OP_lit0:
23590 case DW_OP_lit1:
23591 case DW_OP_lit2:
23592 case DW_OP_lit3:
23593 case DW_OP_lit4:
23594 case DW_OP_lit5:
23595 case DW_OP_lit6:
23596 case DW_OP_lit7:
23597 case DW_OP_lit8:
23598 case DW_OP_lit9:
23599 case DW_OP_lit10:
23600 case DW_OP_lit11:
23601 case DW_OP_lit12:
23602 case DW_OP_lit13:
23603 case DW_OP_lit14:
23604 case DW_OP_lit15:
23605 case DW_OP_lit16:
23606 case DW_OP_lit17:
23607 case DW_OP_lit18:
23608 case DW_OP_lit19:
23609 case DW_OP_lit20:
23610 case DW_OP_lit21:
23611 case DW_OP_lit22:
23612 case DW_OP_lit23:
23613 case DW_OP_lit24:
23614 case DW_OP_lit25:
23615 case DW_OP_lit26:
23616 case DW_OP_lit27:
23617 case DW_OP_lit28:
23618 case DW_OP_lit29:
23619 case DW_OP_lit30:
23620 case DW_OP_lit31:
23621 stack[++stacki] = op - DW_OP_lit0;
23622 break;
23623
23624 case DW_OP_reg0:
23625 case DW_OP_reg1:
23626 case DW_OP_reg2:
23627 case DW_OP_reg3:
23628 case DW_OP_reg4:
23629 case DW_OP_reg5:
23630 case DW_OP_reg6:
23631 case DW_OP_reg7:
23632 case DW_OP_reg8:
23633 case DW_OP_reg9:
23634 case DW_OP_reg10:
23635 case DW_OP_reg11:
23636 case DW_OP_reg12:
23637 case DW_OP_reg13:
23638 case DW_OP_reg14:
23639 case DW_OP_reg15:
23640 case DW_OP_reg16:
23641 case DW_OP_reg17:
23642 case DW_OP_reg18:
23643 case DW_OP_reg19:
23644 case DW_OP_reg20:
23645 case DW_OP_reg21:
23646 case DW_OP_reg22:
23647 case DW_OP_reg23:
23648 case DW_OP_reg24:
23649 case DW_OP_reg25:
23650 case DW_OP_reg26:
23651 case DW_OP_reg27:
23652 case DW_OP_reg28:
23653 case DW_OP_reg29:
23654 case DW_OP_reg30:
23655 case DW_OP_reg31:
23656 stack[++stacki] = op - DW_OP_reg0;
23657 if (i < size)
23658 {
23659 if (computed == nullptr)
23660 dwarf2_complex_location_expr_complaint ();
23661 else
23662 return 0;
23663 }
23664 break;
23665
23666 case DW_OP_regx:
23667 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23668 i += bytes_read;
23669 stack[++stacki] = unsnd;
23670 if (i < size)
23671 {
23672 if (computed == nullptr)
23673 dwarf2_complex_location_expr_complaint ();
23674 else
23675 return 0;
23676 }
23677 break;
23678
23679 case DW_OP_addr:
23680 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23681 &bytes_read);
23682 i += bytes_read;
23683 break;
23684
23685 case DW_OP_const1u:
23686 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23687 i += 1;
23688 break;
23689
23690 case DW_OP_const1s:
23691 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23692 i += 1;
23693 break;
23694
23695 case DW_OP_const2u:
23696 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23697 i += 2;
23698 break;
23699
23700 case DW_OP_const2s:
23701 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23702 i += 2;
23703 break;
23704
23705 case DW_OP_const4u:
23706 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23707 i += 4;
23708 break;
23709
23710 case DW_OP_const4s:
23711 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23712 i += 4;
23713 break;
23714
23715 case DW_OP_const8u:
23716 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23717 i += 8;
23718 break;
23719
23720 case DW_OP_constu:
23721 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23722 &bytes_read);
23723 i += bytes_read;
23724 break;
23725
23726 case DW_OP_consts:
23727 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23728 i += bytes_read;
23729 break;
23730
23731 case DW_OP_dup:
23732 stack[stacki + 1] = stack[stacki];
23733 stacki++;
23734 break;
23735
23736 case DW_OP_plus:
23737 stack[stacki - 1] += stack[stacki];
23738 stacki--;
23739 break;
23740
23741 case DW_OP_plus_uconst:
23742 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23743 &bytes_read);
23744 i += bytes_read;
23745 break;
23746
23747 case DW_OP_minus:
23748 stack[stacki - 1] -= stack[stacki];
23749 stacki--;
23750 break;
23751
23752 case DW_OP_deref:
23753 /* If we're not the last op, then we definitely can't encode
23754 this using GDB's address_class enum. This is valid for partial
23755 global symbols, although the variable's address will be bogus
23756 in the psymtab. */
23757 if (i < size)
23758 {
23759 if (computed == nullptr)
23760 dwarf2_complex_location_expr_complaint ();
23761 else
23762 return 0;
23763 }
23764 break;
23765
23766 case DW_OP_GNU_push_tls_address:
23767 case DW_OP_form_tls_address:
23768 /* The top of the stack has the offset from the beginning
23769 of the thread control block at which the variable is located. */
23770 /* Nothing should follow this operator, so the top of stack would
23771 be returned. */
23772 /* This is valid for partial global symbols, but the variable's
23773 address will be bogus in the psymtab. Make it always at least
23774 non-zero to not look as a variable garbage collected by linker
23775 which have DW_OP_addr 0. */
23776 if (i < size)
23777 {
23778 if (computed == nullptr)
23779 dwarf2_complex_location_expr_complaint ();
23780 else
23781 return 0;
23782 }
23783 stack[stacki]++;
23784 break;
23785
23786 case DW_OP_GNU_uninit:
23787 if (computed != nullptr)
23788 return 0;
23789 break;
23790
23791 case DW_OP_addrx:
23792 case DW_OP_GNU_addr_index:
23793 case DW_OP_GNU_const_index:
23794 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23795 &bytes_read);
23796 i += bytes_read;
23797 break;
23798
23799 default:
23800 if (computed == nullptr)
23801 {
23802 const char *name = get_DW_OP_name (op);
23803
23804 if (name)
23805 complaint (_("unsupported stack op: '%s'"),
23806 name);
23807 else
23808 complaint (_("unsupported stack op: '%02x'"),
23809 op);
23810 }
23811
23812 return (stack[stacki]);
23813 }
23814
23815 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23816 outside of the allocated space. Also enforce minimum>0. */
23817 if (stacki >= ARRAY_SIZE (stack) - 1)
23818 {
23819 if (computed == nullptr)
23820 complaint (_("location description stack overflow"));
23821 return 0;
23822 }
23823
23824 if (stacki <= 0)
23825 {
23826 if (computed == nullptr)
23827 complaint (_("location description stack underflow"));
23828 return 0;
23829 }
23830 }
23831
23832 if (computed != nullptr)
23833 *computed = true;
23834 return (stack[stacki]);
23835 }
23836
23837 /* memory allocation interface */
23838
23839 static struct dwarf_block *
23840 dwarf_alloc_block (struct dwarf2_cu *cu)
23841 {
23842 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23843 }
23844
23845 static struct die_info *
23846 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23847 {
23848 struct die_info *die;
23849 size_t size = sizeof (struct die_info);
23850
23851 if (num_attrs > 1)
23852 size += (num_attrs - 1) * sizeof (struct attribute);
23853
23854 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23855 memset (die, 0, sizeof (struct die_info));
23856 return (die);
23857 }
23858
23859 \f
23860
23861 /* Macro support. */
23862
23863 /* An overload of dwarf_decode_macros that finds the correct section
23864 and ensures it is read in before calling the other overload. */
23865
23866 static void
23867 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23868 int section_is_gnu)
23869 {
23870 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23871 struct objfile *objfile = per_objfile->objfile;
23872 const struct line_header *lh = cu->line_header;
23873 unsigned int offset_size = cu->header.offset_size;
23874 struct dwarf2_section_info *section;
23875 const char *section_name;
23876
23877 if (cu->dwo_unit != nullptr)
23878 {
23879 if (section_is_gnu)
23880 {
23881 section = &cu->dwo_unit->dwo_file->sections.macro;
23882 section_name = ".debug_macro.dwo";
23883 }
23884 else
23885 {
23886 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23887 section_name = ".debug_macinfo.dwo";
23888 }
23889 }
23890 else
23891 {
23892 if (section_is_gnu)
23893 {
23894 section = &per_objfile->per_bfd->macro;
23895 section_name = ".debug_macro";
23896 }
23897 else
23898 {
23899 section = &per_objfile->per_bfd->macinfo;
23900 section_name = ".debug_macinfo";
23901 }
23902 }
23903
23904 section->read (objfile);
23905 if (section->buffer == nullptr)
23906 {
23907 complaint (_("missing %s section"), section_name);
23908 return;
23909 }
23910
23911 buildsym_compunit *builder = cu->get_builder ();
23912
23913 struct dwarf2_section_info *str_offsets_section;
23914 struct dwarf2_section_info *str_section;
23915 ULONGEST str_offsets_base;
23916
23917 if (cu->dwo_unit != nullptr)
23918 {
23919 str_offsets_section = &cu->dwo_unit->dwo_file
23920 ->sections.str_offsets;
23921 str_section = &cu->dwo_unit->dwo_file->sections.str;
23922 str_offsets_base = cu->header.addr_size;
23923 }
23924 else
23925 {
23926 str_offsets_section = &per_objfile->per_bfd->str_offsets;
23927 str_section = &per_objfile->per_bfd->str;
23928 str_offsets_base = *cu->str_offsets_base;
23929 }
23930
23931 dwarf_decode_macros (per_objfile, builder, section, lh,
23932 offset_size, offset, str_section, str_offsets_section,
23933 str_offsets_base, section_is_gnu);
23934 }
23935
23936 /* Return the .debug_loc section to use for CU.
23937 For DWO files use .debug_loc.dwo. */
23938
23939 static struct dwarf2_section_info *
23940 cu_debug_loc_section (struct dwarf2_cu *cu)
23941 {
23942 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23943
23944 if (cu->dwo_unit)
23945 {
23946 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23947
23948 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23949 }
23950 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
23951 : &per_objfile->per_bfd->loc);
23952 }
23953
23954 /* Return the .debug_rnglists section to use for CU. */
23955 static struct dwarf2_section_info *
23956 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
23957 {
23958 if (cu->header.version < 5)
23959 error (_(".debug_rnglists section cannot be used in DWARF %d"),
23960 cu->header.version);
23961 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23962
23963 /* Make sure we read the .debug_rnglists section from the file that
23964 contains the DW_AT_ranges attribute we are reading. Normally that
23965 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
23966 or DW_TAG_skeleton unit, we always want to read from objfile/linked
23967 program. */
23968 if (cu->dwo_unit != nullptr
23969 && tag != DW_TAG_compile_unit
23970 && tag != DW_TAG_skeleton_unit)
23971 {
23972 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23973
23974 if (sections->rnglists.size > 0)
23975 return &sections->rnglists;
23976 else
23977 error (_(".debug_rnglists section is missing from .dwo file."));
23978 }
23979 return &dwarf2_per_objfile->per_bfd->rnglists;
23980 }
23981
23982 /* A helper function that fills in a dwarf2_loclist_baton. */
23983
23984 static void
23985 fill_in_loclist_baton (struct dwarf2_cu *cu,
23986 struct dwarf2_loclist_baton *baton,
23987 const struct attribute *attr)
23988 {
23989 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23990 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23991
23992 section->read (per_objfile->objfile);
23993
23994 baton->per_objfile = per_objfile;
23995 baton->per_cu = cu->per_cu;
23996 gdb_assert (baton->per_cu);
23997 /* We don't know how long the location list is, but make sure we
23998 don't run off the edge of the section. */
23999 baton->size = section->size - attr->as_unsigned ();
24000 baton->data = section->buffer + attr->as_unsigned ();
24001 if (cu->base_address.has_value ())
24002 baton->base_address = *cu->base_address;
24003 else
24004 baton->base_address = 0;
24005 baton->from_dwo = cu->dwo_unit != NULL;
24006 }
24007
24008 static void
24009 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24010 struct dwarf2_cu *cu, int is_block)
24011 {
24012 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24013 struct objfile *objfile = per_objfile->objfile;
24014 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24015
24016 if (attr->form_is_section_offset ()
24017 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24018 the section. If so, fall through to the complaint in the
24019 other branch. */
24020 && attr->as_unsigned () < section->get_size (objfile))
24021 {
24022 struct dwarf2_loclist_baton *baton;
24023
24024 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24025
24026 fill_in_loclist_baton (cu, baton, attr);
24027
24028 if (!cu->base_address.has_value ())
24029 complaint (_("Location list used without "
24030 "specifying the CU base address."));
24031
24032 SYMBOL_ACLASS_INDEX (sym) = (is_block
24033 ? dwarf2_loclist_block_index
24034 : dwarf2_loclist_index);
24035 SYMBOL_LOCATION_BATON (sym) = baton;
24036 }
24037 else
24038 {
24039 struct dwarf2_locexpr_baton *baton;
24040
24041 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24042 baton->per_objfile = per_objfile;
24043 baton->per_cu = cu->per_cu;
24044 gdb_assert (baton->per_cu);
24045
24046 if (attr->form_is_block ())
24047 {
24048 /* Note that we're just copying the block's data pointer
24049 here, not the actual data. We're still pointing into the
24050 info_buffer for SYM's objfile; right now we never release
24051 that buffer, but when we do clean up properly this may
24052 need to change. */
24053 struct dwarf_block *block = attr->as_block ();
24054 baton->size = block->size;
24055 baton->data = block->data;
24056 }
24057 else
24058 {
24059 dwarf2_invalid_attrib_class_complaint ("location description",
24060 sym->natural_name ());
24061 baton->size = 0;
24062 }
24063
24064 SYMBOL_ACLASS_INDEX (sym) = (is_block
24065 ? dwarf2_locexpr_block_index
24066 : dwarf2_locexpr_index);
24067 SYMBOL_LOCATION_BATON (sym) = baton;
24068 }
24069 }
24070
24071 /* See read.h. */
24072
24073 const comp_unit_head *
24074 dwarf2_per_cu_data::get_header () const
24075 {
24076 if (!m_header_read_in)
24077 {
24078 const gdb_byte *info_ptr
24079 = this->section->buffer + to_underlying (this->sect_off);
24080
24081 memset (&m_header, 0, sizeof (m_header));
24082
24083 read_comp_unit_head (&m_header, info_ptr, this->section,
24084 rcuh_kind::COMPILE);
24085 }
24086
24087 return &m_header;
24088 }
24089
24090 /* See read.h. */
24091
24092 int
24093 dwarf2_per_cu_data::addr_size () const
24094 {
24095 return this->get_header ()->addr_size;
24096 }
24097
24098 /* See read.h. */
24099
24100 int
24101 dwarf2_per_cu_data::offset_size () const
24102 {
24103 return this->get_header ()->offset_size;
24104 }
24105
24106 /* See read.h. */
24107
24108 int
24109 dwarf2_per_cu_data::ref_addr_size () const
24110 {
24111 const comp_unit_head *header = this->get_header ();
24112
24113 if (header->version == 2)
24114 return header->addr_size;
24115 else
24116 return header->offset_size;
24117 }
24118
24119 /* See read.h. */
24120
24121 struct type *
24122 dwarf2_cu::addr_type () const
24123 {
24124 struct objfile *objfile = this->per_objfile->objfile;
24125 struct type *void_type = objfile_type (objfile)->builtin_void;
24126 struct type *addr_type = lookup_pointer_type (void_type);
24127 int addr_size = this->per_cu->addr_size ();
24128
24129 if (TYPE_LENGTH (addr_type) == addr_size)
24130 return addr_type;
24131
24132 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
24133 return addr_type;
24134 }
24135
24136 /* A helper function for dwarf2_find_containing_comp_unit that returns
24137 the index of the result, and that searches a vector. It will
24138 return a result even if the offset in question does not actually
24139 occur in any CU. This is separate so that it can be unit
24140 tested. */
24141
24142 static int
24143 dwarf2_find_containing_comp_unit
24144 (sect_offset sect_off,
24145 unsigned int offset_in_dwz,
24146 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
24147 {
24148 int low, high;
24149
24150 low = 0;
24151 high = all_comp_units.size () - 1;
24152 while (high > low)
24153 {
24154 struct dwarf2_per_cu_data *mid_cu;
24155 int mid = low + (high - low) / 2;
24156
24157 mid_cu = all_comp_units[mid];
24158 if (mid_cu->is_dwz > offset_in_dwz
24159 || (mid_cu->is_dwz == offset_in_dwz
24160 && mid_cu->sect_off + mid_cu->length > sect_off))
24161 high = mid;
24162 else
24163 low = mid + 1;
24164 }
24165 gdb_assert (low == high);
24166 return low;
24167 }
24168
24169 /* Locate the .debug_info compilation unit from CU's objfile which contains
24170 the DIE at OFFSET. Raises an error on failure. */
24171
24172 static struct dwarf2_per_cu_data *
24173 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24174 unsigned int offset_in_dwz,
24175 dwarf2_per_objfile *per_objfile)
24176 {
24177 int low = dwarf2_find_containing_comp_unit
24178 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24179 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
24180
24181 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24182 {
24183 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24184 error (_("Dwarf Error: could not find partial DIE containing "
24185 "offset %s [in module %s]"),
24186 sect_offset_str (sect_off),
24187 bfd_get_filename (per_objfile->objfile->obfd));
24188
24189 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
24190 <= sect_off);
24191 return per_objfile->per_bfd->all_comp_units[low-1];
24192 }
24193 else
24194 {
24195 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
24196 && sect_off >= this_cu->sect_off + this_cu->length)
24197 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24198 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24199 return this_cu;
24200 }
24201 }
24202
24203 #if GDB_SELF_TEST
24204
24205 namespace selftests {
24206 namespace find_containing_comp_unit {
24207
24208 static void
24209 run_test ()
24210 {
24211 struct dwarf2_per_cu_data one {};
24212 struct dwarf2_per_cu_data two {};
24213 struct dwarf2_per_cu_data three {};
24214 struct dwarf2_per_cu_data four {};
24215
24216 one.length = 5;
24217 two.sect_off = sect_offset (one.length);
24218 two.length = 7;
24219
24220 three.length = 5;
24221 three.is_dwz = 1;
24222 four.sect_off = sect_offset (three.length);
24223 four.length = 7;
24224 four.is_dwz = 1;
24225
24226 std::vector<dwarf2_per_cu_data *> units;
24227 units.push_back (&one);
24228 units.push_back (&two);
24229 units.push_back (&three);
24230 units.push_back (&four);
24231
24232 int result;
24233
24234 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24235 SELF_CHECK (units[result] == &one);
24236 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24237 SELF_CHECK (units[result] == &one);
24238 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24239 SELF_CHECK (units[result] == &two);
24240
24241 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24242 SELF_CHECK (units[result] == &three);
24243 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24244 SELF_CHECK (units[result] == &three);
24245 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24246 SELF_CHECK (units[result] == &four);
24247 }
24248
24249 }
24250 }
24251
24252 #endif /* GDB_SELF_TEST */
24253
24254 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
24255
24256 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
24257 dwarf2_per_objfile *per_objfile)
24258 : per_cu (per_cu),
24259 per_objfile (per_objfile),
24260 mark (false),
24261 has_loclist (false),
24262 checked_producer (false),
24263 producer_is_gxx_lt_4_6 (false),
24264 producer_is_gcc_lt_4_3 (false),
24265 producer_is_icc (false),
24266 producer_is_icc_lt_14 (false),
24267 producer_is_codewarrior (false),
24268 processing_has_namespace_info (false)
24269 {
24270 }
24271
24272 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24273
24274 static void
24275 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24276 enum language pretend_language)
24277 {
24278 struct attribute *attr;
24279
24280 /* Set the language we're debugging. */
24281 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24282 if (attr != nullptr)
24283 set_cu_language (attr->constant_value (0), cu);
24284 else
24285 {
24286 cu->language = pretend_language;
24287 cu->language_defn = language_def (cu->language);
24288 }
24289
24290 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24291 }
24292
24293 /* See read.h. */
24294
24295 dwarf2_cu *
24296 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
24297 {
24298 auto it = m_dwarf2_cus.find (per_cu);
24299 if (it == m_dwarf2_cus.end ())
24300 return nullptr;
24301
24302 return it->second;
24303 }
24304
24305 /* See read.h. */
24306
24307 void
24308 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24309 {
24310 gdb_assert (this->get_cu (per_cu) == nullptr);
24311
24312 m_dwarf2_cus[per_cu] = cu;
24313 }
24314
24315 /* See read.h. */
24316
24317 void
24318 dwarf2_per_objfile::age_comp_units ()
24319 {
24320 /* Start by clearing all marks. */
24321 for (auto pair : m_dwarf2_cus)
24322 pair.second->mark = false;
24323
24324 /* Traverse all CUs, mark them and their dependencies if used recently
24325 enough. */
24326 for (auto pair : m_dwarf2_cus)
24327 {
24328 dwarf2_cu *cu = pair.second;
24329
24330 cu->last_used++;
24331 if (cu->last_used <= dwarf_max_cache_age)
24332 dwarf2_mark (cu);
24333 }
24334
24335 /* Delete all CUs still not marked. */
24336 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
24337 {
24338 dwarf2_cu *cu = it->second;
24339
24340 if (!cu->mark)
24341 {
24342 delete cu;
24343 it = m_dwarf2_cus.erase (it);
24344 }
24345 else
24346 it++;
24347 }
24348 }
24349
24350 /* See read.h. */
24351
24352 void
24353 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
24354 {
24355 auto it = m_dwarf2_cus.find (per_cu);
24356 if (it == m_dwarf2_cus.end ())
24357 return;
24358
24359 delete it->second;
24360
24361 m_dwarf2_cus.erase (it);
24362 }
24363
24364 dwarf2_per_objfile::~dwarf2_per_objfile ()
24365 {
24366 remove_all_cus ();
24367 }
24368
24369 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24370 We store these in a hash table separate from the DIEs, and preserve them
24371 when the DIEs are flushed out of cache.
24372
24373 The CU "per_cu" pointer is needed because offset alone is not enough to
24374 uniquely identify the type. A file may have multiple .debug_types sections,
24375 or the type may come from a DWO file. Furthermore, while it's more logical
24376 to use per_cu->section+offset, with Fission the section with the data is in
24377 the DWO file but we don't know that section at the point we need it.
24378 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24379 because we can enter the lookup routine, get_die_type_at_offset, from
24380 outside this file, and thus won't necessarily have PER_CU->cu.
24381 Fortunately, PER_CU is stable for the life of the objfile. */
24382
24383 struct dwarf2_per_cu_offset_and_type
24384 {
24385 const struct dwarf2_per_cu_data *per_cu;
24386 sect_offset sect_off;
24387 struct type *type;
24388 };
24389
24390 /* Hash function for a dwarf2_per_cu_offset_and_type. */
24391
24392 static hashval_t
24393 per_cu_offset_and_type_hash (const void *item)
24394 {
24395 const struct dwarf2_per_cu_offset_and_type *ofs
24396 = (const struct dwarf2_per_cu_offset_and_type *) item;
24397
24398 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
24399 }
24400
24401 /* Equality function for a dwarf2_per_cu_offset_and_type. */
24402
24403 static int
24404 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
24405 {
24406 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24407 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24408 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24409 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
24410
24411 return (ofs_lhs->per_cu == ofs_rhs->per_cu
24412 && ofs_lhs->sect_off == ofs_rhs->sect_off);
24413 }
24414
24415 /* Set the type associated with DIE to TYPE. Save it in CU's hash
24416 table if necessary. For convenience, return TYPE.
24417
24418 The DIEs reading must have careful ordering to:
24419 * Not cause infinite loops trying to read in DIEs as a prerequisite for
24420 reading current DIE.
24421 * Not trying to dereference contents of still incompletely read in types
24422 while reading in other DIEs.
24423 * Enable referencing still incompletely read in types just by a pointer to
24424 the type without accessing its fields.
24425
24426 Therefore caller should follow these rules:
24427 * Try to fetch any prerequisite types we may need to build this DIE type
24428 before building the type and calling set_die_type.
24429 * After building type call set_die_type for current DIE as soon as
24430 possible before fetching more types to complete the current type.
24431 * Make the type as complete as possible before fetching more types. */
24432
24433 static struct type *
24434 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
24435 {
24436 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24437 struct dwarf2_per_cu_offset_and_type **slot, ofs;
24438 struct objfile *objfile = per_objfile->objfile;
24439 struct attribute *attr;
24440 struct dynamic_prop prop;
24441
24442 /* For Ada types, make sure that the gnat-specific data is always
24443 initialized (if not already set). There are a few types where
24444 we should not be doing so, because the type-specific area is
24445 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24446 where the type-specific area is used to store the floatformat).
24447 But this is not a problem, because the gnat-specific information
24448 is actually not needed for these types. */
24449 if (need_gnat_info (cu)
24450 && type->code () != TYPE_CODE_FUNC
24451 && type->code () != TYPE_CODE_FLT
24452 && type->code () != TYPE_CODE_METHODPTR
24453 && type->code () != TYPE_CODE_MEMBERPTR
24454 && type->code () != TYPE_CODE_METHOD
24455 && !HAVE_GNAT_AUX_INFO (type))
24456 INIT_GNAT_SPECIFIC (type);
24457
24458 /* Read DW_AT_allocated and set in type. */
24459 attr = dwarf2_attr (die, DW_AT_allocated, cu);
24460 if (attr != NULL)
24461 {
24462 struct type *prop_type = cu->addr_sized_int_type (false);
24463 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24464 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
24465 }
24466
24467 /* Read DW_AT_associated and set in type. */
24468 attr = dwarf2_attr (die, DW_AT_associated, cu);
24469 if (attr != NULL)
24470 {
24471 struct type *prop_type = cu->addr_sized_int_type (false);
24472 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24473 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
24474 }
24475
24476 /* Read DW_AT_data_location and set in type. */
24477 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24478 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
24479 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
24480
24481 if (per_objfile->die_type_hash == NULL)
24482 per_objfile->die_type_hash
24483 = htab_up (htab_create_alloc (127,
24484 per_cu_offset_and_type_hash,
24485 per_cu_offset_and_type_eq,
24486 NULL, xcalloc, xfree));
24487
24488 ofs.per_cu = cu->per_cu;
24489 ofs.sect_off = die->sect_off;
24490 ofs.type = type;
24491 slot = (struct dwarf2_per_cu_offset_and_type **)
24492 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
24493 if (*slot)
24494 complaint (_("A problem internal to GDB: DIE %s has type already set"),
24495 sect_offset_str (die->sect_off));
24496 *slot = XOBNEW (&objfile->objfile_obstack,
24497 struct dwarf2_per_cu_offset_and_type);
24498 **slot = ofs;
24499 return type;
24500 }
24501
24502 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
24503 or return NULL if the die does not have a saved type. */
24504
24505 static struct type *
24506 get_die_type_at_offset (sect_offset sect_off,
24507 dwarf2_per_cu_data *per_cu,
24508 dwarf2_per_objfile *per_objfile)
24509 {
24510 struct dwarf2_per_cu_offset_and_type *slot, ofs;
24511
24512 if (per_objfile->die_type_hash == NULL)
24513 return NULL;
24514
24515 ofs.per_cu = per_cu;
24516 ofs.sect_off = sect_off;
24517 slot = ((struct dwarf2_per_cu_offset_and_type *)
24518 htab_find (per_objfile->die_type_hash.get (), &ofs));
24519 if (slot)
24520 return slot->type;
24521 else
24522 return NULL;
24523 }
24524
24525 /* Look up the type for DIE in CU in die_type_hash,
24526 or return NULL if DIE does not have a saved type. */
24527
24528 static struct type *
24529 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24530 {
24531 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
24532 }
24533
24534 /* Add a dependence relationship from CU to REF_PER_CU. */
24535
24536 static void
24537 dwarf2_add_dependence (struct dwarf2_cu *cu,
24538 struct dwarf2_per_cu_data *ref_per_cu)
24539 {
24540 void **slot;
24541
24542 if (cu->dependencies == NULL)
24543 cu->dependencies
24544 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
24545 NULL, &cu->comp_unit_obstack,
24546 hashtab_obstack_allocate,
24547 dummy_obstack_deallocate);
24548
24549 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
24550 if (*slot == NULL)
24551 *slot = ref_per_cu;
24552 }
24553
24554 /* Subroutine of dwarf2_mark to pass to htab_traverse.
24555 Set the mark field in every compilation unit in the
24556 cache that we must keep because we are keeping CU.
24557
24558 DATA is the dwarf2_per_objfile object in which to look up CUs. */
24559
24560 static int
24561 dwarf2_mark_helper (void **slot, void *data)
24562 {
24563 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
24564 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
24565 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
24566
24567 /* cu->dependencies references may not yet have been ever read if QUIT aborts
24568 reading of the chain. As such dependencies remain valid it is not much
24569 useful to track and undo them during QUIT cleanups. */
24570 if (cu == nullptr)
24571 return 1;
24572
24573 if (cu->mark)
24574 return 1;
24575
24576 cu->mark = true;
24577
24578 if (cu->dependencies != nullptr)
24579 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
24580
24581 return 1;
24582 }
24583
24584 /* Set the mark field in CU and in every other compilation unit in the
24585 cache that we must keep because we are keeping CU. */
24586
24587 static void
24588 dwarf2_mark (struct dwarf2_cu *cu)
24589 {
24590 if (cu->mark)
24591 return;
24592
24593 cu->mark = true;
24594
24595 if (cu->dependencies != nullptr)
24596 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
24597 }
24598
24599 /* Trivial hash function for partial_die_info: the hash value of a DIE
24600 is its offset in .debug_info for this objfile. */
24601
24602 static hashval_t
24603 partial_die_hash (const void *item)
24604 {
24605 const struct partial_die_info *part_die
24606 = (const struct partial_die_info *) item;
24607
24608 return to_underlying (part_die->sect_off);
24609 }
24610
24611 /* Trivial comparison function for partial_die_info structures: two DIEs
24612 are equal if they have the same offset. */
24613
24614 static int
24615 partial_die_eq (const void *item_lhs, const void *item_rhs)
24616 {
24617 const struct partial_die_info *part_die_lhs
24618 = (const struct partial_die_info *) item_lhs;
24619 const struct partial_die_info *part_die_rhs
24620 = (const struct partial_die_info *) item_rhs;
24621
24622 return part_die_lhs->sect_off == part_die_rhs->sect_off;
24623 }
24624
24625 struct cmd_list_element *set_dwarf_cmdlist;
24626 struct cmd_list_element *show_dwarf_cmdlist;
24627
24628 static void
24629 show_check_physname (struct ui_file *file, int from_tty,
24630 struct cmd_list_element *c, const char *value)
24631 {
24632 fprintf_filtered (file,
24633 _("Whether to check \"physname\" is %s.\n"),
24634 value);
24635 }
24636
24637 void _initialize_dwarf2_read ();
24638 void
24639 _initialize_dwarf2_read ()
24640 {
24641 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
24642 Set DWARF specific variables.\n\
24643 Configure DWARF variables such as the cache size."),
24644 &set_dwarf_cmdlist, "maintenance set dwarf ",
24645 0/*allow-unknown*/, &maintenance_set_cmdlist);
24646
24647 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
24648 Show DWARF specific variables.\n\
24649 Show DWARF variables such as the cache size."),
24650 &show_dwarf_cmdlist, "maintenance show dwarf ",
24651 0/*allow-unknown*/, &maintenance_show_cmdlist);
24652
24653 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24654 &dwarf_max_cache_age, _("\
24655 Set the upper bound on the age of cached DWARF compilation units."), _("\
24656 Show the upper bound on the age of cached DWARF compilation units."), _("\
24657 A higher limit means that cached compilation units will be stored\n\
24658 in memory longer, and more total memory will be used. Zero disables\n\
24659 caching, which can slow down startup."),
24660 NULL,
24661 show_dwarf_max_cache_age,
24662 &set_dwarf_cmdlist,
24663 &show_dwarf_cmdlist);
24664
24665 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24666 Set debugging of the DWARF reader."), _("\
24667 Show debugging of the DWARF reader."), _("\
24668 When enabled (non-zero), debugging messages are printed during DWARF\n\
24669 reading and symtab expansion. A value of 1 (one) provides basic\n\
24670 information. A value greater than 1 provides more verbose information."),
24671 NULL,
24672 NULL,
24673 &setdebuglist, &showdebuglist);
24674
24675 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24676 Set debugging of the DWARF DIE reader."), _("\
24677 Show debugging of the DWARF DIE reader."), _("\
24678 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24679 The value is the maximum depth to print."),
24680 NULL,
24681 NULL,
24682 &setdebuglist, &showdebuglist);
24683
24684 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24685 Set debugging of the dwarf line reader."), _("\
24686 Show debugging of the dwarf line reader."), _("\
24687 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24688 A value of 1 (one) provides basic information.\n\
24689 A value greater than 1 provides more verbose information."),
24690 NULL,
24691 NULL,
24692 &setdebuglist, &showdebuglist);
24693
24694 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24695 Set cross-checking of \"physname\" code against demangler."), _("\
24696 Show cross-checking of \"physname\" code against demangler."), _("\
24697 When enabled, GDB's internal \"physname\" code is checked against\n\
24698 the demangler."),
24699 NULL, show_check_physname,
24700 &setdebuglist, &showdebuglist);
24701
24702 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24703 no_class, &use_deprecated_index_sections, _("\
24704 Set whether to use deprecated gdb_index sections."), _("\
24705 Show whether to use deprecated gdb_index sections."), _("\
24706 When enabled, deprecated .gdb_index sections are used anyway.\n\
24707 Normally they are ignored either because of a missing feature or\n\
24708 performance issue.\n\
24709 Warning: This option must be enabled before gdb reads the file."),
24710 NULL,
24711 NULL,
24712 &setlist, &showlist);
24713
24714 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24715 &dwarf2_locexpr_funcs);
24716 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24717 &dwarf2_loclist_funcs);
24718
24719 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24720 &dwarf2_block_frame_base_locexpr_funcs);
24721 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24722 &dwarf2_block_frame_base_loclist_funcs);
24723
24724 #if GDB_SELF_TEST
24725 selftests::register_test ("dw2_expand_symtabs_matching",
24726 selftests::dw2_expand_symtabs_matching::run_test);
24727 selftests::register_test ("dwarf2_find_containing_comp_unit",
24728 selftests::find_containing_comp_unit::run_test);
24729 #endif
24730 }
This page took 0.54859 seconds and 5 git commands to generate.